MDL-34290 oauthlib_helper support for POST request
[moodle.git] / repository / dropbox / lib.php
CommitLineData
3e123368 1<?php
3e123368
DC
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
e35194be
DC
18 * This plugin is used to access user's dropbox files
19 *
3e123368 20 * @since 2.0
67233725
DC
21 * @package repository_dropbox
22 * @copyright 2010 Dongsheng Cai {@link http://dongsheng.org}
d078f6d3 23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
3e123368 24 */
67233725 25require_once($CFG->dirroot . '/repository/lib.php');
e35194be
DC
26require_once(dirname(__FILE__).'/locallib.php');
27
28class repository_dropbox extends repository {
67233725 29 /** @var dropbox the instance of dropbox client */
e35194be 30 private $dropbox;
67233725 31 /** @var array files */
e35194be 32 public $files;
67233725 33 /** @var bool flag of login status */
e35194be
DC
34 public $logged=false;
35
67233725
DC
36 /** @var int cached file ttl */
37 private $cachedfilettl = null;
38
e35194be
DC
39 /**
40 * Constructor of dropbox plugin
67233725 41 *
e35194be 42 * @param int $repositoryid
67233725 43 * @param stdClass $context
e35194be
DC
44 * @param array $options
45 */
46 public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array()) {
67233725 47 global $CFG;
e35194be
DC
48 $options['page'] = optional_param('p', 1, PARAM_INT);
49 parent::__construct($repositoryid, $context, $options);
50
51 $this->setting = 'dropbox_';
52
53 $this->dropbox_key = $this->get_option('dropbox_key');
54 $this->dropbox_secret = $this->get_option('dropbox_secret');
55
67233725
DC
56 // one day
57 $this->cachedfilettl = 60 * 60 * 24;
58
59 if (isset($options['access_key'])) {
60 $this->access_key = $options['access_key'];
61 } else {
62 $this->access_key = get_user_preferences($this->setting.'_access_key', '');
63 }
64 if (isset($options['access_secret'])) {
65 $this->access_secret = $options['access_secret'];
66 } else {
67 $this->access_secret = get_user_preferences($this->setting.'_access_secret', '');
68 }
e35194be
DC
69
70 if (!empty($this->access_key) && !empty($this->access_secret)) {
71 $this->logged = true;
72 }
73
67233725 74 $callbackurl = new moodle_url($CFG->wwwroot.'/repository/repository_callback.php', array(
e35194be
DC
75 'callback'=>'yes',
76 'repo_id'=>$repositoryid
77 ));
78
79 $args = array(
80 'oauth_consumer_key'=>$this->dropbox_key,
81 'oauth_consumer_secret'=>$this->dropbox_secret,
67233725 82 'oauth_callback' => $callbackurl->out(false),
c745d1e8 83 'api_root' => 'https://www.dropbox.com/1/oauth',
e35194be
DC
84 );
85
86 $this->dropbox = new dropbox($args);
87 }
88
67233725
DC
89 /**
90 * Set access key
91 *
92 * @param string $access_key
93 */
94 public function set_access_key($access_key) {
95 $this->access_key = $access_key;
96 }
97
98 /**
99 * Set access secret
100 *
101 * @param string $access_secret
102 */
103 public function set_access_secret($access_secret) {
104 $this->access_secret = $access_secret;
105 }
106
107
e35194be
DC
108 /**
109 * Check if moodle has got access token and secret
67233725 110 *
e35194be
DC
111 * @return bool
112 */
113 public function check_login() {
114 return !empty($this->logged);
115 }
116
117 /**
118 * Generate dropbox login url
67233725 119 *
e35194be
DC
120 * @return array
121 */
122 public function print_login() {
123 $result = $this->dropbox->request_token();
124 set_user_preference($this->setting.'_request_secret', $result['oauth_token_secret']);
125 $url = $result['authorize_url'];
126 if ($this->options['ajax']) {
127 $ret = array();
6bdfef5d 128 $popup_btn = new stdClass();
e35194be
DC
129 $popup_btn->type = 'popup';
130 $popup_btn->url = $url;
131 $ret['login'] = array($popup_btn);
132 return $ret;
133 } else {
4b05a49b 134 echo '<a target="_blank" href="'.$url.'">'.get_string('login', 'repository').'</a>';
e35194be
DC
135 }
136 }
137
138 /**
139 * Request access token
67233725 140 *
e35194be
DC
141 * @return array
142 */
143 public function callback() {
144 $token = optional_param('oauth_token', '', PARAM_TEXT);
145 $secret = get_user_preferences($this->setting.'_request_secret', '');
146 $access_token = $this->dropbox->get_access_token($token, $secret);
147 set_user_preference($this->setting.'_access_key', $access_token['oauth_token']);
148 set_user_preference($this->setting.'_access_secret', $access_token['oauth_token_secret']);
149 }
150
151 /**
152 * Get dropbox files
67233725 153 *
e35194be
DC
154 * @param string $path
155 * @param int $page
156 * @return array
157 */
158 public function get_listing($path = '', $page = '1') {
159 global $OUTPUT;
160 if (empty($path) || $path=='/') {
161 $path = '/';
162 } else {
163 $path = file_correct_filepath($path);
164 }
ab603b24 165 $encoded_path = str_replace("%2F", "/", rawurlencode($path));
e35194be
DC
166
167 $list = array();
168 $list['list'] = array();
32368e99
DC
169 $list['manage'] = false;
170 $list['dynload'] = true;
171 $list['nosearch'] = true;
ab603b24 172 // process breadcrumb trail
e35194be 173 $list['path'] = array(
0e61528d 174 array('name'=>get_string('dropbox', 'repository_dropbox'), 'path'=>'/')
e35194be 175 );
32368e99 176
ab603b24 177 $result = $this->dropbox->get_listing($encoded_path, $this->access_key, $this->access_secret);
0e61528d 178
32368e99
DC
179 if (!is_object($result) || empty($result)) {
180 return $list;
181 }
182 if (empty($result->path)) {
183 $current_path = '/';
184 } else {
185 $current_path = file_correct_filepath($result->path);
186 }
187
e35194be
DC
188 $trail = '';
189 if (!empty($path)) {
190 $parts = explode('/', $path);
191 if (count($parts) > 1) {
192 foreach ($parts as $part) {
193 if (!empty($part)) {
194 $trail .= ('/'.$part);
195 $list['path'][] = array('name'=>$part, 'path'=>$trail);
196 }
197 }
198 } else {
199 $list['path'][] = array('name'=>$path, 'path'=>$path);
200 }
201 }
e35194be 202
810aea11
DC
203 if (!empty($result->error)) {
204 // reset access key
205 set_user_preference($this->setting.'_access_key', '');
206 set_user_preference($this->setting.'_access_secret', '');
207 throw new repository_exception('repositoryerror', 'repository', '', $result->error);
208 }
209 if (empty($result->contents) or !is_array($result->contents)) {
32368e99
DC
210 return $list;
211 }
810aea11 212 $files = $result->contents;
e35194be
DC
213 foreach ($files as $file) {
214 if ($file->is_dir) {
215 $list['list'][] = array(
216 'title' => substr($file->path, strpos($file->path, $current_path)+strlen($current_path)),
217 'path' => file_correct_filepath($file->path),
218 'size' => $file->size,
219 'date' => $file->modified,
559276b1 220 'thumbnail' => $OUTPUT->pix_url(file_folder_icon(90))->out(false),
e35194be
DC
221 'children' => array(),
222 );
223 } else {
224 $list['list'][] = array(
225 'title' => substr($file->path, strpos($file->path, $current_path)+strlen($current_path)),
226 'source' => $file->path,
227 'size' => $file->size,
228 'date' => $file->modified,
559276b1 229 'thumbnail' => $OUTPUT->pix_url(file_extension_icon($file->path, 90))->out(false)
e35194be
DC
230 );
231 }
232 }
233 return $list;
234 }
235 /**
236 * Logout from dropbox
237 * @return array
238 */
239 public function logout() {
240 set_user_preference($this->setting.'_access_key', '');
241 set_user_preference($this->setting.'_access_secret', '');
242 $this->access_key = '';
243 $this->access_secret = '';
244 return $this->print_login();
245 }
246
247 /**
248 * Set dropbox option
249 * @param array $options
250 * @return mixed
251 */
252 public function set_option($options = array()) {
253 if (!empty($options['dropbox_key'])) {
254 set_config('dropbox_key', trim($options['dropbox_key']), 'dropbox');
255 }
256 if (!empty($options['dropbox_secret'])) {
257 set_config('dropbox_secret', trim($options['dropbox_secret']), 'dropbox');
258 }
259 unset($options['dropbox_key']);
260 unset($options['dropbox_secret']);
261 $ret = parent::set_option($options);
262 return $ret;
263 }
264
265 /**
266 * Get dropbox options
267 * @param string $config
268 * @return mixed
269 */
270 public function get_option($config = '') {
271 if ($config==='dropbox_key') {
272 return trim(get_config('dropbox', 'dropbox_key'));
273 } elseif ($config==='dropbox_secret') {
274 return trim(get_config('dropbox', 'dropbox_secret'));
275 } else {
276 $options['dropbox_key'] = trim(get_config('dropbox', 'dropbox_key'));
277 $options['dropbox_secret'] = trim(get_config('dropbox', 'dropbox_secret'));
278 }
279 $options = parent::get_option($config);
280 return $options;
281 }
282
283 /**
59cb7598 284 * Downloads a file from external repository and saves it in temp dir
e35194be 285 *
59cb7598
MG
286 * @throws moodle_exception when file could not be downloaded
287 *
288 * @param string $reference the content of files.reference field
289 * @param string $filename filename (without path) to save the downloaded file in the
290 * temporary directory, if omitted or file already exists the new filename will be generated
291 * @return array with elements:
292 * path: internal location of the file
293 * url: URL to the source (from parameters)
e35194be 294 */
59cb7598
MG
295 public function get_file($reference, $saveas = '') {
296 $reference = unserialize($reference);
297 $this->dropbox->set_access_token($reference->access_key, $reference->access_secret);
991aa8ab 298 $saveas = $this->prepare_file($saveas);
59cb7598 299 return $this->dropbox->get_file($reference->path, $saveas);
e35194be
DC
300 }
301 /**
302 * Add Plugin settings input to Moodle form
67233725 303 *
e35194be
DC
304 * @param object $mform
305 */
68a7c9a6 306 public static function type_config_form($mform, $classname = 'repository') {
e35194be 307 global $CFG;
a5adfa26 308 parent::type_config_form($mform);
e35194be
DC
309 $key = get_config('dropbox', 'dropbox_key');
310 $secret = get_config('dropbox', 'dropbox_secret');
311
312 if (empty($key)) {
313 $key = '';
314 }
315 if (empty($secret)) {
316 $secret = '';
317 }
318
319 $strrequired = get_string('required');
320
321 $mform->addElement('text', 'dropbox_key', get_string('apikey', 'repository_dropbox'), array('value'=>$key,'size' => '40'));
322 $mform->addElement('text', 'dropbox_secret', get_string('secret', 'repository_dropbox'), array('value'=>$secret,'size' => '40'));
323
324 $mform->addRule('dropbox_key', $strrequired, 'required', null, 'client');
325 $mform->addRule('dropbox_secret', $strrequired, 'required', null, 'client');
326 $str_getkey = get_string('instruction', 'repository_dropbox');
327 $mform->addElement('static', null, '', $str_getkey);
328 }
329
330 /**
331 * Option names of dropbox plugin
67233725 332 *
e35194be
DC
333 * @return array
334 */
335 public static function get_type_option_names() {
a5adfa26 336 return array('dropbox_key', 'dropbox_secret', 'pluginname');
e35194be
DC
337 }
338
339 /**
340 * Dropbox plugin supports all kinds of files
67233725 341 *
e35194be
DC
342 * @return array
343 */
344 public function supported_filetypes() {
345 return '*';
346 }
3e123368 347
e35194be
DC
348 /**
349 * User cannot use the external link to dropbox
67233725 350 *
e35194be
DC
351 * @return int
352 */
353 public function supported_returntypes() {
67233725
DC
354 return FILE_INTERNAL | FILE_REFERENCE;
355 }
356
357 /**
358 * Prepare file reference information
359 *
360 * @param string $source
361 * @return string file referece
362 */
363 public function get_file_reference($source) {
59cb7598 364 global $USER;
67233725
DC
365 $reference = new stdClass;
366 $reference->path = $source;
367 $reference->access_key = get_user_preferences($this->setting.'_access_key', '');
368 $reference->access_secret = get_user_preferences($this->setting.'_access_secret', '');
59cb7598
MG
369 $reference->userid = $USER->id;
370 $reference->username = fullname($USER);
67233725
DC
371 return serialize($reference);
372 }
373
374 /**
0b2bfbd1 375 * Returns information about file in this repository by reference
67233725
DC
376 * {@link repository::get_file_reference()}
377 * {@link repository::get_file()}
378 *
0b2bfbd1
MG
379 * Returns null if file not found or is not readable
380 *
67233725 381 * @param stdClass $reference file reference db record
0b2bfbd1 382 * @return null|stdClass that has 'filepath' property
67233725
DC
383 */
384 public function get_file_by_reference($reference) {
385 $reference = unserialize($reference->reference);
386 $cachedfilepath = cache_file::get($reference, array('ttl' => $this->cachedfilettl));
387 if ($cachedfilepath === false) {
388 // Cache the file.
389 $this->set_access_key($reference->access_key);
390 $this->set_access_secret($reference->access_secret);
391 $path = $this->get_file($reference->path);
392 $cachedfilepath = cache_file::create_from_file($reference, $path['path']);
59cb7598 393 }
0b2bfbd1
MG
394 if ($cachedfilepath && is_readable($cachedfilepath)) {
395 return (object)array('filepath' => $cachedfilepath);
396 } else {
397 return null;
398 }
59cb7598 399 }
67233725
DC
400
401 /**
402 * Get file from external repository by reference
403 * {@link repository::get_file_reference()}
404 * {@link repository::get_file()}
405 *
406 * @param string $reference this reference is generated by
407 * repository::get_file_reference()
408 * @param stored_file $storedfile created file reference
409 */
410 public function cache_file_by_reference($reference, $storedfile) {
411 $reference = unserialize($reference);
59cb7598
MG
412 $path = $this->get_file($reference);
413 cache_file::create_from_file($reference->path, $path['path']);
67233725
DC
414 }
415
416 /**
417 * Return human readable reference information
418 * {@link stored_file::get_reference()}
419 *
420 * @param string $reference
0b2bfbd1
MG
421 * @param int $filestatus status of the file, 0 - ok, 666 - source missing
422 * @return string
67233725 423 */
0b2bfbd1 424 public function get_reference_details($reference, $filestatus = 0) {
59cb7598 425 global $USER;
67233725 426 $ref = unserialize($reference);
0b2bfbd1 427 $details = $this->get_name();
59cb7598
MG
428 if (isset($ref->userid) && $ref->userid != $USER->id && isset($ref->username)) {
429 $details .= ' ('.$ref->username.')';
430 }
0b2bfbd1
MG
431 if (isset($ref->path)) {
432 $details .= ': '. $ref->path;
433 }
434 if (isset($ref->path) && !$filestatus) {
435 // Indicate this is from dropbox with path
436 return $details;
437 } else {
438 return get_string('lostsource', 'repository', $details);
439 }
67233725
DC
440 }
441
d6453211
DC
442 /**
443 * Return the source information
444 *
59cb7598
MG
445 * @param string $source
446 * @return string
d6453211 447 */
59cb7598
MG
448 public function get_file_source_info($source) {
449 global $USER;
450 return 'Dropbox ('.fullname($USER).'): ' . $source;
d6453211
DC
451 }
452
67233725 453 /**
0b2bfbd1
MG
454 * Repository method to serve the referenced file
455 *
456 * This method is ivoked from {@link send_stored_file()}.
457 * Dropbox repository first caches the file by reading it into temporary folder and then
458 * serves from there.
67233725 459 *
0b2bfbd1 460 * @param stored_file $storedfile the file that contains the reference
67233725
DC
461 * @param int $lifetime Number of seconds before the file should expire from caches (default 24 hours)
462 * @param int $filter 0 (default)=no filtering, 1=all files, 2=html files only
463 * @param bool $forcedownload If true (default false), forces download of file rather than view in browser/plugin
464 * @param array $options additional options affecting the file serving
465 */
466 public function send_file($storedfile, $lifetime=86400 , $filter=0, $forcedownload=false, array $options = null) {
0b2bfbd1
MG
467 $fileinfo = $this->get_file_by_reference((object)array('reference' => $storedfile->get_reference()));
468 if ($fileinfo && !empty($fileinfo->filepath) && is_readable($fileinfo->filepath)) {
469 $filename = $storedfile->get_filename();
470 if ($options && isset($options['filename'])) {
471 $filename = $options['filename'];
472 }
473 $dontdie = ($options && isset($options['dontdie']));
474 send_file($fileinfo->filepath, $filename, $lifetime , $filter, false, $forcedownload, '', $dontdie);
475 } else {
476 send_file_not_found();
67233725 477 }
67233725
DC
478 }
479
480 public function cron() {
481 $fs = get_file_storage();
482 $files = $fs->get_external_files($this->id);
483 foreach ($files as $file) {
484 $reference = unserialize($file->get_reference());
485
486 $cachedfile = cache_file::get($reference);
487 if ($cachedfile === false) {
488 // Re-fetch resource.
59cb7598
MG
489 $path = $this->get_file($reference);
490 cache_file::create_from_file($reference->path, $path['path']);
67233725
DC
491 }
492 }
493 }
494}
495
496/**
497 * Dropbox plugin cron task
498 */
499function repository_dropbox_cron() {
500 $instances = repository::get_instances(array('type'=>'dropbox'));
501 foreach ($instances as $instance) {
502 $instance->cron();
3e123368
DC
503 }
504}