3 // This file is part of Moodle - http://moodle.org/
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
19 * This plugin is used to access webdav files
22 * @package repository_webdav
23 * @copyright 2010 Dongsheng Cai {@link http://dongsheng.org}
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 require_once($CFG->dirroot
. '/repository/lib.php');
27 require_once($CFG->libdir
.'/webdavlib.php');
30 * repository_webdav class
33 * @package repository_webdav
34 * @copyright 2009 Dongsheng Cai {@link http://dongsheng.org}
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 class repository_webdav
extends repository
{
38 public function __construct($repositoryid, $context = SYSCONTEXTID
, $options = array()) {
39 parent
::__construct($repositoryid, $context, $options);
40 // set up webdav client
41 if (empty($this->options
['webdav_server'])) {
44 if ($this->options
['webdav_auth'] == 'none') {
45 $this->options
['webdav_auth'] = false;
47 if (empty($this->options
['webdav_type'])) {
48 $this->webdav_type
= '';
50 $this->webdav_type
= 'ssl://';
52 if (empty($this->options
['webdav_port'])) {
54 if (empty($this->webdav_type
)) {
55 $this->webdav_port
= 80;
57 $this->webdav_port
= 443;
61 $this->webdav_port
= $this->options
['webdav_port'];
62 $port = ':' . $this->webdav_port
;
64 $this->webdav_host
= $this->webdav_type
.$this->options
['webdav_server'].$port;
65 $this->dav
= new webdav_client($this->options
['webdav_server'], $this->options
['webdav_user'],
66 $this->options
['webdav_password'], $this->options
['webdav_auth'], $this->webdav_type
);
67 $this->dav
->port
= $this->webdav_port
;
68 $this->dav
->debug
= false;
70 public function check_login() {
73 public function get_file($url, $title = '') {
74 $url = urldecode($url);
75 // Prepare a file with an arbitrary name - cannot be $title because of special chars (cf. MDL-57002).
76 $path = $this->prepare_file(uniqid());
77 if (!$this->dav
->open()) {
80 $webdavpath = rtrim('/'.ltrim($this->options
['webdav_path'], '/ '), '/ '); // without slash in the end
81 $this->dav
->get_file($webdavpath. $url, $path);
82 return array('path'=>$path);
84 public function global_search() {
87 public function get_listing($path='', $page = '') {
91 $ret['dynload'] = true;
92 $ret['nosearch'] = true;
93 $ret['nologin'] = true;
94 $ret['path'] = array(array('name'=>get_string('webdav', 'repository_webdav'), 'path'=>''));
95 $ret['list'] = array();
96 if (!$this->dav
->open()) {
99 $webdavpath = rtrim('/'.ltrim($this->options
['webdav_path'], '/ '), '/ '); // without slash in the end
100 if (empty($path) ||
$path =='/') {
103 $chunks = preg_split('|/|', trim($path, '/'));
104 for ($i = 0; $i < count($chunks); $i++
) {
105 $ret['path'][] = array(
106 'name' => urldecode($chunks[$i]),
107 'path' => '/'. join('/', array_slice($chunks, 0, $i+
1)). '/'
111 $dir = $this->dav
->ls($webdavpath. urldecode($path));
112 if (!is_array($dir)) {
117 foreach ($dir as $v) {
118 if (!empty($v['lastmodified'])) {
119 $v['lastmodified'] = strtotime($v['lastmodified']);
121 $v['lastmodified'] = null;
124 // Remove the server URL from the path (if present), otherwise links will not work - MDL-37014
125 $server = preg_quote($this->options
['webdav_server']);
126 $v['href'] = preg_replace("#https?://{$server}#", '', $v['href']);
127 // Extracting object title from absolute path
128 $v['href'] = substr(urldecode($v['href']), strlen($webdavpath));
129 $title = substr($v['href'], strlen($path));
131 if (!empty($v['resourcetype']) && $v['resourcetype'] == 'collection') {
133 if ($path != $v['href']) {
134 $folders[strtoupper($title)] = array(
135 'title'=>rtrim($title, '/'),
136 'thumbnail'=>$OUTPUT->image_url(file_folder_icon(90))->out(false),
138 'datemodified'=>$v['lastmodified'],
144 $size = !empty($v['getcontentlength'])?
$v['getcontentlength']:'';
145 $files[strtoupper($title)] = array(
147 'thumbnail' => $OUTPUT->image_url(file_extension_icon($title, 90))->out(false),
149 'datemodified'=>$v['lastmodified'],
156 $ret['list'] = array_merge($folders, $files);
159 public static function get_instance_option_names() {
160 return array('webdav_type', 'webdav_server', 'webdav_port', 'webdav_path', 'webdav_user', 'webdav_password', 'webdav_auth');
163 public static function instance_config_form($mform) {
164 $choices = array(0 => get_string('http', 'repository_webdav'), 1 => get_string('https', 'repository_webdav'));
165 $mform->addElement('select', 'webdav_type', get_string('webdav_type', 'repository_webdav'), $choices);
166 $mform->addRule('webdav_type', get_string('required'), 'required', null, 'client');
168 $mform->addElement('text', 'webdav_server', get_string('webdav_server', 'repository_webdav'), array('size' => '40'));
169 $mform->addRule('webdav_server', get_string('required'), 'required', null, 'client');
170 $mform->setType('webdav_server', PARAM_HOST
);
172 $mform->addElement('text', 'webdav_path', get_string('webdav_path', 'repository_webdav'), array('size' => '40'));
173 $mform->addRule('webdav_path', get_string('required'), 'required', null, 'client');
174 $mform->setType('webdav_path', PARAM_PATH
);
177 $choices['none'] = get_string('none');
178 $choices['basic'] = get_string('webdavbasicauth', 'repository_webdav');
179 $choices['digest'] = get_string('webdavdigestauth', 'repository_webdav');
180 $mform->addElement('select', 'webdav_auth', get_string('authentication', 'admin'), $choices);
181 $mform->addRule('webdav_auth', get_string('required'), 'required', null, 'client');
183 $mform->addElement('text', 'webdav_port', get_string('webdav_port', 'repository_webdav'), array('size' => '40'));
184 $mform->setType('webdav_port', PARAM_INT
);
185 $mform->addElement('text', 'webdav_user', get_string('webdav_user', 'repository_webdav'), array('size' => '40'));
186 $mform->setType('webdav_user', PARAM_RAW_TRIMMED
); // Not for us to clean.
187 $mform->addElement('passwordunmask', 'webdav_password', get_string('webdav_password', 'repository_webdav'),
188 array('size' => '40'));
190 public function supported_returntypes() {
191 return (FILE_INTERNAL | FILE_EXTERNAL
);
196 * Is this repository accessing private data?
200 public function contains_private_data() {