Merge branch 'MDL-72036-310' of git://github.com/sarjona/moodle into MOODLE_310_STABLE
[moodle.git] / repository / webdav / lib.php
blob8c0abbe3d14927159d5d73aba839b023f67ef6a8
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
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.
9 //
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/>.
18 /**
19 * This plugin is used to access webdav files
21 * @since Moodle 2.0
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');
29 /**
30 * repository_webdav class
32 * @since Moodle 2.0
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'])) {
42 return;
44 if ($this->options['webdav_auth'] == 'none') {
45 $this->options['webdav_auth'] = false;
47 if (empty($this->options['webdav_type'])) {
48 $this->webdav_type = '';
49 } else {
50 $this->webdav_type = 'ssl://';
52 if (empty($this->options['webdav_port'])) {
53 $port = '';
54 if (empty($this->webdav_type)) {
55 $this->webdav_port = 80;
56 } else {
57 $this->webdav_port = 443;
58 $port = ':443';
60 } else {
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() {
71 return true;
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()) {
78 return false;
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() {
85 return false;
87 public function get_listing($path='', $page = '') {
88 global $CFG, $OUTPUT;
89 $list = array();
90 $ret = array();
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()) {
97 return $ret;
99 $webdavpath = rtrim('/'.ltrim($this->options['webdav_path'], '/ '), '/ '); // without slash in the end
100 if (empty($path) || $path =='/') {
101 $path = '/';
102 } else {
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)) {
113 return $ret;
115 $folders = array();
116 $files = array();
117 foreach ($dir as $v) {
118 if (!empty($v['lastmodified'])) {
119 $v['lastmodified'] = strtotime($v['lastmodified']);
120 } else {
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') {
132 // a folder
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),
137 'children'=>array(),
138 'datemodified'=>$v['lastmodified'],
139 'path'=>$v['href']
142 }else{
143 // a file
144 $size = !empty($v['getcontentlength'])? $v['getcontentlength']:'';
145 $files[strtoupper($title)] = array(
146 'title'=>$title,
147 'thumbnail' => $OUTPUT->image_url(file_extension_icon($title, 90))->out(false),
148 'size'=>$size,
149 'datemodified'=>$v['lastmodified'],
150 'source'=>$v['href']
154 ksort($files);
155 ksort($folders);
156 $ret['list'] = array_merge($folders, $files);
157 return $ret;
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);
176 $choices = array();
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?
198 * @return bool
200 public function contains_private_data() {
201 return false;