2008-10-21 06:30:50 +00:00
|
|
|
<?php
|
2009-12-17 03:40:38 +00:00
|
|
|
|
|
|
|
// This file is part of Moodle - http://moodle.org/
|
|
|
|
//
|
|
|
|
// Moodle is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// Moodle is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2008-10-21 06:30:50 +00:00
|
|
|
/**
|
2012-05-12 04:14:53 +08:00
|
|
|
* This plugin is used to access webdav files
|
2008-10-21 06:30:50 +00:00
|
|
|
*
|
2014-05-19 17:03:04 +01:00
|
|
|
* @since Moodle 2.0
|
2012-05-12 04:14:53 +08:00
|
|
|
* @package repository_webdav
|
|
|
|
* @copyright 2010 Dongsheng Cai {@link http://dongsheng.org}
|
2010-09-06 11:29:21 +00:00
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
2008-10-21 06:30:50 +00:00
|
|
|
*/
|
2012-05-12 04:14:53 +08:00
|
|
|
require_once($CFG->dirroot . '/repository/lib.php');
|
2008-10-21 06:30:50 +00:00
|
|
|
require_once($CFG->libdir.'/webdavlib.php');
|
|
|
|
|
2012-05-12 04:14:53 +08:00
|
|
|
/**
|
|
|
|
* repository_webdav class
|
|
|
|
*
|
2014-05-19 17:03:04 +01:00
|
|
|
* @since Moodle 2.0
|
2012-05-12 04:14:53 +08:00
|
|
|
* @package repository_webdav
|
|
|
|
* @copyright 2009 Dongsheng Cai {@link http://dongsheng.org}
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
*/
|
2008-10-21 06:30:50 +00:00
|
|
|
class repository_webdav extends repository {
|
2023-02-09 09:16:13 +07:00
|
|
|
|
|
|
|
/** @var string webdav type. */
|
|
|
|
protected $webdav_type;
|
|
|
|
|
|
|
|
/** @var mixed webdav port. */
|
|
|
|
protected $webdav_port;
|
|
|
|
|
|
|
|
/** @var string webdav host. */
|
|
|
|
protected $webdav_host;
|
|
|
|
|
|
|
|
/** @var webdav_client webdav client. */
|
|
|
|
protected $dav;
|
|
|
|
|
2010-03-16 03:21:59 +00:00
|
|
|
public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array()) {
|
2008-10-21 06:30:50 +00:00
|
|
|
parent::__construct($repositoryid, $context, $options);
|
2009-04-23 09:50:30 +00:00
|
|
|
// set up webdav client
|
2009-06-25 04:58:58 +00:00
|
|
|
if (empty($this->options['webdav_server'])) {
|
2009-04-03 02:16:48 +00:00
|
|
|
return;
|
|
|
|
}
|
2010-09-24 04:10:46 +00:00
|
|
|
if ($this->options['webdav_auth'] == 'none') {
|
|
|
|
$this->options['webdav_auth'] = false;
|
|
|
|
}
|
2009-06-25 04:58:58 +00:00
|
|
|
if (empty($this->options['webdav_type'])) {
|
2010-11-04 03:46:41 +00:00
|
|
|
$this->webdav_type = '';
|
2009-04-23 09:50:30 +00:00
|
|
|
} else {
|
2010-11-04 03:46:41 +00:00
|
|
|
$this->webdav_type = 'ssl://';
|
2009-04-23 09:50:30 +00:00
|
|
|
}
|
2009-06-25 04:58:58 +00:00
|
|
|
if (empty($this->options['webdav_port'])) {
|
2012-08-02 12:04:32 +08:00
|
|
|
$port = '';
|
2010-11-04 03:46:41 +00:00
|
|
|
if (empty($this->webdav_type)) {
|
2012-08-02 12:04:32 +08:00
|
|
|
$this->webdav_port = 80;
|
2009-04-23 09:50:30 +00:00
|
|
|
} else {
|
2012-08-02 12:04:32 +08:00
|
|
|
$this->webdav_port = 443;
|
|
|
|
$port = ':443';
|
2009-04-23 09:50:30 +00:00
|
|
|
}
|
2008-10-21 06:30:50 +00:00
|
|
|
} else {
|
2012-08-02 12:04:32 +08:00
|
|
|
$this->webdav_port = $this->options['webdav_port'];
|
|
|
|
$port = ':' . $this->webdav_port;
|
2008-10-21 06:30:50 +00:00
|
|
|
}
|
2010-11-04 03:46:41 +00:00
|
|
|
$this->webdav_host = $this->webdav_type.$this->options['webdav_server'].$port;
|
2012-08-02 12:04:32 +08:00
|
|
|
$this->dav = new webdav_client($this->options['webdav_server'], $this->options['webdav_user'],
|
|
|
|
$this->options['webdav_password'], $this->options['webdav_auth'], $this->webdav_type);
|
|
|
|
$this->dav->port = $this->webdav_port;
|
2010-09-24 04:10:46 +00:00
|
|
|
$this->dav->debug = false;
|
2008-10-21 06:30:50 +00:00
|
|
|
}
|
|
|
|
public function check_login() {
|
|
|
|
return true;
|
|
|
|
}
|
2012-03-26 11:47:15 +02:00
|
|
|
public function get_file($url, $title = '') {
|
2011-05-16 15:35:16 +02:00
|
|
|
$url = urldecode($url);
|
2017-04-26 10:44:52 +02:00
|
|
|
// Prepare a file with an arbitrary name - cannot be $title because of special chars (cf. MDL-57002).
|
|
|
|
$path = $this->prepare_file(uniqid());
|
2010-09-24 04:10:46 +00:00
|
|
|
if (!$this->dav->open()) {
|
2009-04-23 09:50:30 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-04-18 09:39:58 +08:00
|
|
|
$webdavpath = rtrim('/'.ltrim($this->options['webdav_path'], '/ '), '/ '); // without slash in the end
|
2012-11-12 16:51:06 +00:00
|
|
|
$this->dav->get_file($webdavpath. $url, $path);
|
2010-03-29 03:39:08 +00:00
|
|
|
return array('path'=>$path);
|
2008-10-21 06:30:50 +00:00
|
|
|
}
|
|
|
|
public function global_search() {
|
|
|
|
return false;
|
|
|
|
}
|
2009-02-18 06:52:54 +00:00
|
|
|
public function get_listing($path='', $page = '') {
|
2009-06-30 02:12:16 +00:00
|
|
|
global $CFG, $OUTPUT;
|
2008-10-21 06:30:50 +00:00
|
|
|
$list = array();
|
|
|
|
$ret = array();
|
|
|
|
$ret['dynload'] = true;
|
2008-10-22 02:50:32 +00:00
|
|
|
$ret['nosearch'] = true;
|
|
|
|
$ret['nologin'] = true;
|
2012-04-18 09:39:58 +08:00
|
|
|
$ret['path'] = array(array('name'=>get_string('webdav', 'repository_webdav'), 'path'=>''));
|
2009-04-23 09:50:30 +00:00
|
|
|
$ret['list'] = array();
|
2010-09-24 04:10:46 +00:00
|
|
|
if (!$this->dav->open()) {
|
2009-04-23 09:50:30 +00:00
|
|
|
return $ret;
|
|
|
|
}
|
2012-04-18 09:39:58 +08:00
|
|
|
$webdavpath = rtrim('/'.ltrim($this->options['webdav_path'], '/ '), '/ '); // without slash in the end
|
|
|
|
if (empty($path) || $path =='/') {
|
|
|
|
$path = '/';
|
2008-10-21 06:30:50 +00:00
|
|
|
} else {
|
2012-04-18 09:39:58 +08:00
|
|
|
$chunks = preg_split('|/|', trim($path, '/'));
|
|
|
|
for ($i = 0; $i < count($chunks); $i++) {
|
|
|
|
$ret['path'][] = array(
|
|
|
|
'name' => urldecode($chunks[$i]),
|
|
|
|
'path' => '/'. join('/', array_slice($chunks, 0, $i+1)). '/'
|
|
|
|
);
|
2009-04-23 09:50:30 +00:00
|
|
|
}
|
2008-10-21 06:30:50 +00:00
|
|
|
}
|
2012-04-18 09:39:58 +08:00
|
|
|
$dir = $this->dav->ls($webdavpath. urldecode($path));
|
2008-10-21 06:30:50 +00:00
|
|
|
if (!is_array($dir)) {
|
|
|
|
return $ret;
|
|
|
|
}
|
2012-04-18 09:39:58 +08:00
|
|
|
$folders = array();
|
|
|
|
$files = array();
|
2008-10-21 06:30:50 +00:00
|
|
|
foreach ($dir as $v) {
|
2012-04-18 09:39:58 +08:00
|
|
|
if (!empty($v['lastmodified'])) {
|
|
|
|
$v['lastmodified'] = strtotime($v['lastmodified']);
|
2009-04-23 09:50:30 +00:00
|
|
|
} else {
|
2012-04-18 09:39:58 +08:00
|
|
|
$v['lastmodified'] = null;
|
2009-04-23 09:50:30 +00:00
|
|
|
}
|
2012-08-02 12:04:32 +08:00
|
|
|
|
2012-12-05 14:05:24 +00:00
|
|
|
// Remove the server URL from the path (if present), otherwise links will not work - MDL-37014
|
|
|
|
$server = preg_quote($this->options['webdav_server']);
|
|
|
|
$v['href'] = preg_replace("#https?://{$server}#", '', $v['href']);
|
2012-08-02 12:04:32 +08:00
|
|
|
// Extracting object title from absolute path
|
|
|
|
$v['href'] = substr(urldecode($v['href']), strlen($webdavpath));
|
|
|
|
$title = substr($v['href'], strlen($path));
|
|
|
|
|
2008-10-21 06:30:50 +00:00
|
|
|
if (!empty($v['resourcetype']) && $v['resourcetype'] == 'collection') {
|
2023-08-03 10:35:29 +02:00
|
|
|
// A folder.
|
2012-04-18 09:39:58 +08:00
|
|
|
if ($path != $v['href']) {
|
2012-08-02 12:04:32 +08:00
|
|
|
$folders[strtoupper($title)] = array(
|
2023-08-03 10:35:29 +02:00
|
|
|
'title' => rtrim($title, '/'),
|
|
|
|
'thumbnail' => $OUTPUT->image_url(file_folder_icon())->out(false),
|
|
|
|
'children' => array(),
|
|
|
|
'datemodified' => $v['lastmodified'],
|
|
|
|
'path' => $v['href'],
|
2008-10-21 06:30:50 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}else{
|
2023-08-03 10:35:29 +02:00
|
|
|
// A file.
|
2009-04-23 09:50:30 +00:00
|
|
|
$size = !empty($v['getcontentlength'])? $v['getcontentlength']:'';
|
2012-08-02 12:04:32 +08:00
|
|
|
$files[strtoupper($title)] = array(
|
2023-08-03 10:35:29 +02:00
|
|
|
'title' => $title,
|
|
|
|
'thumbnail' => $OUTPUT->image_url(file_extension_icon($title))->out(false),
|
|
|
|
'size' => $size,
|
|
|
|
'datemodified' => $v['lastmodified'],
|
|
|
|
'source' => $v['href'],
|
2008-10-21 06:30:50 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2012-08-02 12:04:32 +08:00
|
|
|
ksort($files);
|
|
|
|
ksort($folders);
|
2012-04-18 09:39:58 +08:00
|
|
|
$ret['list'] = array_merge($folders, $files);
|
2008-10-21 06:30:50 +00:00
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
public static function get_instance_option_names() {
|
2010-09-24 04:10:46 +00:00
|
|
|
return array('webdav_type', 'webdav_server', 'webdav_port', 'webdav_path', 'webdav_user', 'webdav_password', 'webdav_auth');
|
2008-10-21 06:30:50 +00:00
|
|
|
}
|
|
|
|
|
2012-06-12 22:15:09 +08:00
|
|
|
public static function instance_config_form($mform) {
|
2009-04-23 09:50:30 +00:00
|
|
|
$choices = array(0 => get_string('http', 'repository_webdav'), 1 => get_string('https', 'repository_webdav'));
|
|
|
|
$mform->addElement('select', 'webdav_type', get_string('webdav_type', 'repository_webdav'), $choices);
|
|
|
|
$mform->addRule('webdav_type', get_string('required'), 'required', null, 'client');
|
|
|
|
|
2008-10-21 06:30:50 +00:00
|
|
|
$mform->addElement('text', 'webdav_server', get_string('webdav_server', 'repository_webdav'), array('size' => '40'));
|
|
|
|
$mform->addRule('webdav_server', get_string('required'), 'required', null, 'client');
|
2013-04-03 11:00:57 +08:00
|
|
|
$mform->setType('webdav_server', PARAM_HOST);
|
2010-09-24 04:10:46 +00:00
|
|
|
|
|
|
|
$mform->addElement('text', 'webdav_path', get_string('webdav_path', 'repository_webdav'), array('size' => '40'));
|
|
|
|
$mform->addRule('webdav_path', get_string('required'), 'required', null, 'client');
|
2013-04-03 11:00:57 +08:00
|
|
|
$mform->setType('webdav_path', PARAM_PATH);
|
2010-09-24 04:10:46 +00:00
|
|
|
|
|
|
|
$choices = array();
|
|
|
|
$choices['none'] = get_string('none');
|
|
|
|
$choices['basic'] = get_string('webdavbasicauth', 'repository_webdav');
|
2012-08-02 16:29:00 +08:00
|
|
|
$choices['digest'] = get_string('webdavdigestauth', 'repository_webdav');
|
2010-09-24 04:10:46 +00:00
|
|
|
$mform->addElement('select', 'webdav_auth', get_string('authentication', 'admin'), $choices);
|
|
|
|
$mform->addRule('webdav_auth', get_string('required'), 'required', null, 'client');
|
|
|
|
|
2008-10-21 06:30:50 +00:00
|
|
|
$mform->addElement('text', 'webdav_port', get_string('webdav_port', 'repository_webdav'), array('size' => '40'));
|
2013-04-03 11:00:57 +08:00
|
|
|
$mform->setType('webdav_port', PARAM_INT);
|
2008-10-21 06:30:50 +00:00
|
|
|
$mform->addElement('text', 'webdav_user', get_string('webdav_user', 'repository_webdav'), array('size' => '40'));
|
2013-04-03 11:00:57 +08:00
|
|
|
$mform->setType('webdav_user', PARAM_RAW_TRIMMED); // Not for us to clean.
|
2019-03-27 14:02:18 +00:00
|
|
|
$mform->addElement('passwordunmask', 'webdav_password', get_string('webdav_password', 'repository_webdav'),
|
2013-03-04 13:45:32 +13:00
|
|
|
array('size' => '40'));
|
2008-10-21 06:30:50 +00:00
|
|
|
}
|
2009-11-02 06:45:12 +00:00
|
|
|
public function supported_returntypes() {
|
|
|
|
return (FILE_INTERNAL | FILE_EXTERNAL);
|
|
|
|
}
|
2013-02-04 14:58:46 +08:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Is this repository accessing private data?
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function contains_private_data() {
|
|
|
|
return false;
|
|
|
|
}
|
2008-10-21 06:30:50 +00:00
|
|
|
}
|