forked from learnweb/moodle-repository_owncloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.php
387 lines (336 loc) · 15 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
<?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/>.
/**
* ownCloud repository plugin library.
*
* @package repository_owncloud
* @copyright 2017 Westfälische Wilhelms-Universität Münster (WWU Münster)
* @author Projektseminar Uni Münster
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
// @codeCoverageIgnoreStart
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/repository/lib.php');
// @codeCoverageIgnoreEnd
use tool_oauth2owncloud\owncloud;
/**
* ownCloud repository class.
*
* @package repository_owncloud
* @copyright 2017 Westfälische Universität Münster (WWU Münster)
* @author Projektseminar Uni Münster
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class repository_owncloud extends repository {
/** @var null|owncloud the ownCloud client. */
private $owncloud = null;
public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array()) {
parent::__construct($repositoryid, $context, $options);
// The WebDav client is no longer handled in here.
$returnurl = new moodle_url('/repository/repository_callback.php', [
'callback' => 'yes',
'repo_id' => $repositoryid,
'sesskey' => sesskey(),
]);
// The owncloud Object, which is described in the Admin Tool oauth2owncloud
// is created. From now on is will handle all interactions with the owncloud OAuth2 Client.
$this->owncloud = new owncloud($returnurl);
// Checks, whether all the required data is available. $this->options['checked'] is set to true, if the
// data was checked once to prevent multiple printings of the warning.
if (empty($this->options['checked'])) {
$this->options['checked'] = true;
$this->options['success'] = $this->owncloud->check_data();
if (!$this->options['success']) {
$this->print_warning();
}
}
}
/**
* Output method, which prints a warning inside an activity, which uses the ownCloud repository.
*
* @codeCoverageIgnore
*/
private function print_warning() {
global $CFG, $OUTPUT;
$sitecontext = context_system::instance();
if (has_capability('moodle/site:config', $sitecontext)) {
$link = $CFG->wwwroot . '/' . $CFG->admin . '/settings.php?section=oauth2owncloud';
// Generates a link to the admin setting page.
echo $OUTPUT->notification('<a href="' . $link . '" target="_blank" rel="noopener noreferrer">
' . get_string('missing_settings_admin', 'tool_oauth2owncloud') . '</a>', 'warning');
} else {
// Otherwise, just print a notification, bacause the current user cannot configure admin
// settings himself.
echo $OUTPUT->notification(get_string('missing_settings_user', 'tool_oauth2owncloud'));
}
}
/**
* If the plugin is set to hidden in the settings or any client settings date is missing,
* the plugin is set to invisible and thus, not shown in the file picker.
*
* @return bool false, if set to hidden or settings data is missing.
*/
public function is_visible() {
if (!parent::is_visible()) {
return false;
} else {
// If any settings data is missing, return false.
return $this->options['success'];
}
}
/**
* This function does exactly the same as in the WebDAV repository. The only difference is, that
* the ownCloud OAuth2 client uses OAuth2 instead of Basic Authentication.
*
* @param string $url relative path to the file.
* @param string $title title of the file.
* @return array|bool returns either the moodle path to the file or false.
*/
public function get_file($url, $title = '') {
$url = urldecode($url);
$path = $this->prepare_file($title);
if (!$this->owncloud->open()) {
return false;
}
$this->owncloud->get_file($url, $path);
return array('path' => $path);
}
/**
* This function does exactly the same as in the WebDAV repository. The only difference is, that
* the ownCloud OAuth2 client uses OAuth2 instead of Basic Authentication.
*
* @param string $path relative path to the directory or file.
* @param string $page page number (given multiple pages of elements).
* @return array directory properties.
*/
public function get_listing($path='', $page = '') {
global $CFG, $OUTPUT;
// Array, which will have to be returned by this function.
$ret = array();
// Tells the file picker to fetch the list dynamically. An AJAX request is send to the server,
// as soon as the user opens a folder.
$ret['dynload'] = true;
// Search is disabled in this plugin.
$ret['nosearch'] = true;
// We need to provide a login link, because the user needs login himself with his own ownCloud
// user account.
$ret['nologin'] = false;
// Contains all parent paths to the current path.
$ret['path'] = array(array('name' => get_string('owncloud', 'repository_owncloud'), 'path' => ''));
// Contains all file/folder information and is required to build the file/folder tree.
$ret['list'] = array();
$sitecontext = context_system::instance();
if (has_capability('moodle/site:config', $sitecontext)) {
// URL to manage a external repository. It is displayed in the file picker and in this case directs
// the settings page of the oauth2owncloud admin tool.
$ret['manage'] = $CFG->wwwroot.'/'.$CFG->admin.'settings.php?section=oauth2owncloud';
}
// Before any WebDAV method can be executed, a WebDAV client socket needs to be opened
// which connects to the server.
if (!$this->owncloud->open()) {
return $ret;
}
if (empty($path) || $path == '/') {
$path = '/';
} else {
// This calculates all the parents paths form the current path. This is shown in the
// navigation bar of the file picker.
$chunks = preg_split('|/|', trim($path, '/'));
// Every sub-path to the last part of the current path, is a parent path.
for ($i = 0; $i < count($chunks); $i++) {
$ret['path'][] = array(
'name' => urldecode($chunks[$i]),
'path' => '/'. join('/', array_slice($chunks, 0, $i + 1)). '/'
);
}
}
// The WebDav methods are getting outsourced and encapsulated to the owncloud class.
// Since the paths, which are received from the PROPFIND WebDAV method are url encoded
// (because they depict actual web-paths), the received paths need to be decoded back
// for the plugin to be able to work with them.
$dir = $this->owncloud->get_listing(urldecode($path));
// The method get_listing return all information about all child files/folders of the
// current directory. If no information was received, the directory must be empty.
if (!is_array($dir)) {
return $ret;
}
$folders = array();
$files = array();
$webdavpath = rtrim('/'.ltrim(get_config('tool_oauth2owncloud', 'path'), '/ '), '/ ');
foreach ($dir as $v) {
if (!empty($v['lastmodified'])) {
$v['lastmodified'] = strtotime($v['lastmodified']);
} else {
$v['lastmodified'] = null;
}
// Remove the server URL from the path (if present), otherwise links will not work - MDL-37014.
$server = preg_quote(get_config('tool_oauth2owncloud', 'server'));
$v['href'] = preg_replace("#https?://{$server}#", '', $v['href']);
// Extracting object title from absolute path.
$v['href'] = substr(urldecode($v['href']), strlen($webdavpath));
$title = substr($v['href'], strlen($path));
if (!empty($v['resourcetype']) && $v['resourcetype'] == 'collection') {
// A folder.
if ($path != $v['href']) {
$folders[strtoupper($title)] = array(
'title' => rtrim($title, '/'),
'thumbnail' => $OUTPUT->pix_url(file_folder_icon(90))->out(false),
'children' => array(),
'datemodified' => $v['lastmodified'],
'path' => $v['href']
);
}
} else {
// A file.
$size = !empty($v['getcontentlength']) ? $v['getcontentlength'] : '';
$files[strtoupper($title)] = array(
'title' => $title,
'thumbnail' => $OUTPUT->pix_url(file_extension_icon($title, 90))->out(false),
'size' => $size,
'datemodified' => $v['lastmodified'],
'source' => $v['href']
);
}
}
ksort($files);
ksort($folders);
$ret['list'] = array_merge($folders, $files);
return $ret;
}
/**
* Method to generate a download link for a chosen file (in the file picker).
* Creates a share for the chosen file and fetches the specific file ID through
* the OCS Share API (ownCloud).
*
* @param string $url relative path to the chosen file
* @return string the generated downloadlink.
* @throws repository_exception if $url is empty an exception is thrown.
*/
public function get_link($url) {
$response = $this->owncloud->get_link($url);
return $response['link'];
}
/**
* This method converts the source from the file picker (chosen by the user) into
* information, which will be received by methods that fetch files/references from
* the ownCloud server.
*
* @param string $source source of the file, returned by repository as 'source' and received back from user (not cleaned)
* @return string file reference, ready to be stored
*/
public function get_file_reference($source) {
$usefilereference = optional_param('usefilereference', false, PARAM_BOOL);
$reference = $source;
// If a filereference was requested, a public link to the file has to be generated and returned.
if ($usefilereference) {
$reference = $this->get_link($source);
}
// Otherwise, the simple relative path to the file is enough.
return $reference;
}
/**
* Method that generates a reference link to the chosen file.
*
* @codeCoverageIgnore
*/
public function send_file($storedfile, $lifetime=86400 , $filter=0, $forcedownload=false, array $options = null) {
// Delivers a download link to the concerning file.
redirect($storedfile->get_reference());
}
/**
* Function which checks whether the user is logged in on the ownCloud instance.
*
* @return bool false, if no Access Token is set or can be requested.
*/
public function check_login() {
return $this->owncloud->check_login();
}
/**
* Prints a simple Login Button which redirects to an authorization window from ownCloud.
*
* @return mixed login window properties.
*/
public function print_login() {
$url = $this->owncloud->get_login_url();
if ($this->options['ajax']) {
$ret = array();
$btn = new \stdClass();
$btn->type = 'popup';
$btn->url = $url->out(false);
$ret['login'] = array($btn);
return $ret;
} else {
echo html_writer::link($url, get_string('login', 'repository'),
array('target' => '_blank', 'rel' => 'noopener noreferrer'));
}
}
/**
* Deletes the held Access Token and prints the Login window.
*
* @return array login window properties.
*/
public function logout() {
$this->owncloud->log_out();
set_user_preference('oC_token', null);
return $this->print_login();
}
/**
* Sets up access token after the redirection from ownCloud.
*/
public function callback() {
$this->owncloud->check_login();
}
/**
* This method adds a notification to the settings form, which redirects to the OAuth 2.0 client.
*
* @codeCoverageIgnore
* @param moodleform $mform Moodle form (passed by reference)
* @param string $classname repository class name
*/
public static function type_config_form($mform, $classname = 'repository') {
global $CFG, $OUTPUT;
$link = $CFG->wwwroot.'/'.$CFG->admin.'/tool/oauth2owncloud/index.php';
// A notification is added to the settings page in form of a notification.
$html = $OUTPUT->notification(get_string('settings', 'repository_owncloud',
'<a href="'.$link.'" target="_blank" rel="noopener noreferrer">'.
get_string('oauth2', 'repository_owncloud') .'</a>'), 'warning');
$mform->addElement('html', $html);
parent::type_config_form($mform);
}
/**
* Method to define which filetypes are supported (hardcoded can not be changed in Admin Menu)
*
* For a full list of possible types and groups, look in lib/filelib.php, function get_mimetypes_array()
*
* @return string '*' means this repository support any files
*/
public function supported_filetypes() {
return '*';
}
/**
* Method to define which Files are supported (hardcoded can not be changed in Admin Menu)
*
* Can choose FILE_REFERENCE|FILE_INTERNAL|FILE_EXTERNAL
* FILE_INTERNAL - the file is uploaded/downloaded and stored directly within the Moodle file system
* FILE_EXTERNAL - the file stays in the external repository and is accessed from there directly
* FILE_REFERENCE - the file may be cached locally, but is automatically synchronised, as required,
* with any changes to the external original
* @return int return type bitmask supported
*/
public function supported_returntypes() {
return FILE_INTERNAL | FILE_EXTERNAL | FILE_REFERENCE;
}
}