mirrored from git://git.moodle.org/moodle.git
/
lib.php
213 lines (198 loc) · 8.56 KB
/
lib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<?php
// 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/>.
/**
* repository_webdav class
*
* @since 2.0
* @package repository
* @subpackage webdav
* @copyright 2009 Dongsheng Cai
* @author Dongsheng Cai <dongsheng@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once($CFG->libdir.'/webdavlib.php');
class repository_webdav extends repository {
public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array()) {
parent::__construct($repositoryid, $context, $options);
// set up webdav client
if (empty($this->options['webdav_server'])) {
return;
}
if ($this->options['webdav_auth'] == 'none') {
$this->options['webdav_auth'] = false;
}
if (empty($this->options['webdav_type'])) {
$this->webdav_type = '';
} else {
$this->webdav_type = 'ssl://';
}
if (empty($this->options['webdav_port'])) {
$port = '';
if (empty($this->webdav_type)) {
$this->webdav_port = 80;
} else {
$this->webdav_port = 443;
$port = ':443';
}
} else {
$this->webdav_port = $this->options['webdav_port'];
$port = ':' . $this->webdav_port;
}
$this->webdav_host = $this->webdav_type.$this->options['webdav_server'].$port;
$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;
$this->dav->debug = false;
}
public function check_login() {
return true;
}
public function get_file($url, $title) {
$url = urldecode($url);
$path = $this->prepare_file($title);
if (!$this->dav->open()) {
return false;
}
$this->dav->get_file($url, $path);
return array('path'=>$path);
}
public function global_search() {
return false;
}
public function get_listing($path='', $page = '') {
global $CFG, $OUTPUT;
$list = array();
$ret = array();
$ret['dynload'] = true;
$ret['nosearch'] = true;
$ret['nologin'] = true;
$ret['path'] = array(array('name'=>get_string('webdav', 'repository_webdav'), 'path'=>0));
$ret['list'] = array();
if (!$this->dav->open()) {
return $ret;
}
$webdavroot = '/' . trim($this->options['webdav_path'], './@#$ ');
if ($webdavroot != '/') {
$webdavroot .= '/';
}
if (empty($path)) {
$path = $webdavroot;
$dir = $this->dav->ls($path);
} else {
$path = urldecode($path);
if (empty($this->webdav_type)) {
$partern = '#http://'.$this->webdav_host.'/#';
} else {
$partern = '#https://'.$this->webdav_host.'/#';
}
$path = '/'.preg_replace($partern, '', ltrim($path, '/'));
$dir = $this->dav->ls($path);
}
// Building breadcrumb
$pathrel = preg_replace('|^' . preg_quote($webdavroot) . '|', '', $path);
if (!empty($pathrel)) {
$chunks = preg_split('|/|', trim($pathrel, '/'));
for ($i = 0; $i < count($chunks); $i++) {
$ret['path'][] = array(
'name' => urldecode($chunks[$i]),
'path' => $webdavroot . join('/', array_slice($chunks, 0, $i+1)). '/'
);
}
}
if (!is_array($dir)) {
return $ret;
}
$files = array();
$folders = array();
foreach ($dir as $v) {
if (!empty($v['creationdate'])) {
$ts = $this->dav->iso8601totime($v['creationdate']);
$filedate = userdate($ts);
} else {
$filedate = '';
}
// 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']);
if (!empty($v['resourcetype']) && $v['resourcetype'] == 'collection') {
// a folder
if (ltrim($path, '/') != urldecode(ltrim($v['href'], '/'))) {
$matches = array();
preg_match('#(\w+)$#i', $v['href'], $matches);
if (!empty($matches[1])) {
$title = urldecode($matches[1]);
} else {
$title = urldecode($v['href']);
}
$folders[strtoupper($title)] = array(
'title'=>urldecode(basename($title)),
'thumbnail'=>$OUTPUT->pix_url('f/folder-32')->out(false),
'children'=>array(),
'date'=>$filedate,
'size'=>0,
'path'=>$v['href']
);
}
} else {
// A file.
$path = rtrim($path,'/');
if (empty($path)) {
$title = urldecode($v['href']);
} else {
$title = urldecode(substr($v['href'], strpos($v['href'], $path) + strlen($path)));
}
$title = basename($title);
$size = !empty($v['getcontentlength'])? $v['getcontentlength']:'';
$files[strtoupper($title)] = array(
'title'=>$title,
'thumbnail' => $OUTPUT->pix_url(file_extension_icon($title, 32))->out(false),
'size'=>$size,
'date'=>$filedate,
'source'=>$v['href']
);
}
}
ksort($files);
ksort($folders);
$ret['list'] = array_merge($folders, $files);
return $ret;
}
public static function get_instance_option_names() {
return array('webdav_type', 'webdav_server', 'webdav_port', 'webdav_path', 'webdav_user', 'webdav_password', 'webdav_auth');
}
public function instance_config_form($mform) {
$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');
$mform->addElement('text', 'webdav_server', get_string('webdav_server', 'repository_webdav'), array('size' => '40'));
$mform->addRule('webdav_server', get_string('required'), 'required', null, 'client');
$mform->addElement('text', 'webdav_path', get_string('webdav_path', 'repository_webdav'), array('size' => '40'));
$mform->addRule('webdav_path', get_string('required'), 'required', null, 'client');
$choices = array();
$choices['none'] = get_string('none');
$choices['basic'] = get_string('webdavbasicauth', 'repository_webdav');
$choices['digest'] = get_string('webdavdigestauth', 'repository_webdav');
$mform->addElement('select', 'webdav_auth', get_string('authentication', 'admin'), $choices);
$mform->addRule('webdav_auth', get_string('required'), 'required', null, 'client');
$mform->addElement('text', 'webdav_port', get_string('webdav_port', 'repository_webdav'), array('size' => '40'));
$mform->addElement('text', 'webdav_user', get_string('webdav_user', 'repository_webdav'), array('size' => '40'));
$mform->addElement('password', 'webdav_password', get_string('webdav_password', 'repository_webdav'),
array('size' => '40'));
}
public function supported_returntypes() {
return (FILE_INTERNAL | FILE_EXTERNAL);
}
}