-
Notifications
You must be signed in to change notification settings - Fork 1
/
lib.php
349 lines (287 loc) · 9.46 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
<?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/>.
//namespace repository_nextcloud;
/**
* ownCloud repository plugin library.
*
* @package repository_nextcloud
* @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
*/
defined('MOODLE_INTERNAL') || die();
/** @noinspection PhpUndefinedVariableInspection */
require_once($CFG->dirroot . '/repository/lib.php');
require_once('lib/Exceptions/ToolOauth2SetupIncompleteException.php');
require_once('lib/Models/RemoteFile.php');
require_once('lib/Models/CloudFiles.php');
use repository_nextcloud\Exceptions\ToolOauth2SetupIncompleteException;
use repository_nextcloud\Models\CloudFiles;
use tool_oauth2owncloud\owncloud;
class repository_nextcloud extends repository {
/** @var owncloud */
private $nc;
/** @var stdClass */
private $cfg;
public function __construct($repoId, $context = SYSCONTEXTID, $options = array()) {
parent::__construct($repoId, $context, $options);
$this->setGlobals();
try {
$returnUrl = new moodle_url(
'/repository/repository_callback.php', [
'callback' => 'yes',
'repo_id' => $repoId,
'sesskey' => sesskey(),
]
);
$this->nc = new owncloud($returnUrl);
$this->checkToolOauth2Setup();
} catch (ToolOauth2SetupIncompleteException $e) {
self::printWarning();
}
}
public static function getLinkToToolOauth2() {
global $CFG;
return $CFG->wwwroot . '/' . $CFG->admin . '/settings.php?section=oauth2owncloud';
}
/**
* get globals from moodle and set as a local variable.
*/
private function setGlobals() {
global $CFG;
$this->cfg = $CFG;
}
/**
* Check if setup of the tool_oauth2owncloud is done.
*
* @throws ToolOauth2SetupIncompleteException
*/
private function checkToolOauth2Setup() {
if ($this->nc->check_data() === false) {
throw new ToolOauth2SetupIncompleteException('Oauth2 Setup is not complete');
}
}
/**
* Output method, which prints a warning inside an activity, which uses the ownCloud repository.
*/
private static function printWarning() {
global $OUTPUT;
$str = get_string('missing_settings_admin', 'tool_oauth2owncloud');
if (has_capability('moodle/site:config', context_system::instance())) {
print($OUTPUT->notification(
'<a href="' . self::getLinkToToolOauth2()
. '" target="_blank" rel="noopener noreferrer">' . $str
. '</a>', 'warning'
));
} else {
print($OUTPUT->notification($str));
}
}
/**
* 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() {
try {
$this->checkToolOauth2Setup();
return (parent::is_visible());
} catch (ToolOauth2SetupIncompleteException $e) {
return false;
}
}
/**
* 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->nc->open()) {
return false;
}
$this->nc->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 = '') {
$ncFiles = new CloudFiles($path);
if (!$this->nc->open()) {
return $ncFiles->toArray();
}
$ncFiles->parseFolderContent($this->nc->get_listing(urldecode($ncFiles->getPath())));
return $ncFiles->toArray();
}
/**
* 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 Exception
*/
public function get_link($url) {
if ($url === '') {
throw new Exception('get_link on empty path');
}
$response = $this->nc->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.
*
* @param stored_file $storedFile
* @param int $lifetime
* @param int $filter
* @param bool $forceDownload
* @param array|null $options
*/
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->nc->check_login();
}
/**
* Prints a simple Login Button which redirects to an authorization window from the cloud.
*
* @return mixed login window properties.
*/
public function print_login() {
$url = $this->nc->get_login_url();
if ($this->options['ajax']) {
$ret = [];
$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'),
['target' => '_blank', 'rel' => 'noopener noreferrer']
);
}
}
/**
* Deletes the held Access Token and prints the Login window.
*
* @return array login window properties.
*/
public function logout() {
$this->nc->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->nc->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 $OUTPUT;
// A notification is added to the settings page in form of a notification.
$html = $OUTPUT->notification(
get_string(
'settings', 'repository_nextcloud',
'<a href="' . self::getLinkToToolOauth2()
. '" target="_blank" rel="noopener noreferrer">' .
get_string('oauth2', 'repository_nextcloud') . '</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;
}
}