MDL-38878 behat: Also update composer dependencies after site install/reinstall
[moodle.git] / repository / dropbox / lib.php
blob002cc74a903acbb7f0c0ae59ccbec04ed50bff5c
1 <?php
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.
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/>.
17 /**
18 * This plugin is used to access user's dropbox files
20 * @since 2.0
21 * @package repository_dropbox
22 * @copyright 2012 Marina Glancy
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(dirname(__FILE__).'/locallib.php');
29 /**
30 * Repository to access Dropbox files
32 * @package repository_dropbox
33 * @copyright 2010 Dongsheng Cai
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 class repository_dropbox extends repository {
37 /** @var dropbox the instance of dropbox client */
38 private $dropbox;
39 /** @var array files */
40 public $files;
41 /** @var bool flag of login status */
42 public $logged=false;
43 /** @var int maximum size of file to cache in moodle filepool */
44 public $cachelimit=null;
46 /** @var int cached file ttl */
47 private $cachedfilettl = null;
49 /**
50 * Constructor of dropbox plugin
52 * @param int $repositoryid
53 * @param stdClass $context
54 * @param array $options
56 public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array()) {
57 global $CFG;
58 $options['page'] = optional_param('p', 1, PARAM_INT);
59 parent::__construct($repositoryid, $context, $options);
61 $this->setting = 'dropbox_';
63 $this->dropbox_key = $this->get_option('dropbox_key');
64 $this->dropbox_secret = $this->get_option('dropbox_secret');
66 // one day
67 $this->cachedfilettl = 60 * 60 * 24;
69 if (isset($options['access_key'])) {
70 $this->access_key = $options['access_key'];
71 } else {
72 $this->access_key = get_user_preferences($this->setting.'_access_key', '');
74 if (isset($options['access_secret'])) {
75 $this->access_secret = $options['access_secret'];
76 } else {
77 $this->access_secret = get_user_preferences($this->setting.'_access_secret', '');
80 if (!empty($this->access_key) && !empty($this->access_secret)) {
81 $this->logged = true;
84 $callbackurl = new moodle_url($CFG->wwwroot.'/repository/repository_callback.php', array(
85 'callback'=>'yes',
86 'repo_id'=>$repositoryid
87 ));
89 $args = array(
90 'oauth_consumer_key'=>$this->dropbox_key,
91 'oauth_consumer_secret'=>$this->dropbox_secret,
92 'oauth_callback' => $callbackurl->out(false),
93 'api_root' => 'https://www.dropbox.com/1/oauth',
96 $this->dropbox = new dropbox($args);
99 /**
100 * Set access key
102 * @param string $access_key
104 public function set_access_key($access_key) {
105 $this->access_key = $access_key;
109 * Set access secret
111 * @param string $access_secret
113 public function set_access_secret($access_secret) {
114 $this->access_secret = $access_secret;
119 * Check if moodle has got access token and secret
121 * @return bool
123 public function check_login() {
124 return !empty($this->logged);
128 * Generate dropbox login url
130 * @return array
132 public function print_login() {
133 $result = $this->dropbox->request_token();
134 set_user_preference($this->setting.'_request_secret', $result['oauth_token_secret']);
135 $url = $result['authorize_url'];
136 if ($this->options['ajax']) {
137 $ret = array();
138 $popup_btn = new stdClass();
139 $popup_btn->type = 'popup';
140 $popup_btn->url = $url;
141 $ret['login'] = array($popup_btn);
142 return $ret;
143 } else {
144 echo '<a target="_blank" href="'.$url.'">'.get_string('login', 'repository').'</a>';
149 * Request access token
151 * @return array
153 public function callback() {
154 $token = optional_param('oauth_token', '', PARAM_TEXT);
155 $secret = get_user_preferences($this->setting.'_request_secret', '');
156 $access_token = $this->dropbox->get_access_token($token, $secret);
157 set_user_preference($this->setting.'_access_key', $access_token['oauth_token']);
158 set_user_preference($this->setting.'_access_secret', $access_token['oauth_token_secret']);
162 * Get dropbox files
164 * @param string $path
165 * @param int $page
166 * @return array
168 public function get_listing($path = '', $page = '1') {
169 global $OUTPUT;
170 if (empty($path) || $path=='/') {
171 $path = '/';
172 } else {
173 $path = file_correct_filepath($path);
175 $encoded_path = str_replace("%2F", "/", rawurlencode($path));
177 $list = array();
178 $list['list'] = array();
179 $list['manage'] = 'https://www.dropbox.com/home';
180 $list['dynload'] = true;
181 $list['nosearch'] = true;
182 $list['logouturl'] = 'https://www.dropbox.com/logout';
183 $list['message'] = get_string('logoutdesc', 'repository_dropbox');
184 // process breadcrumb trail
185 $list['path'] = array(
186 array('name'=>get_string('dropbox', 'repository_dropbox'), 'path'=>'/')
189 $result = $this->dropbox->get_listing($encoded_path, $this->access_key, $this->access_secret);
191 if (!is_object($result) || empty($result)) {
192 return $list;
194 if (empty($result->path)) {
195 $current_path = '/';
196 } else {
197 $current_path = file_correct_filepath($result->path);
200 $trail = '';
201 if (!empty($path)) {
202 $parts = explode('/', $path);
203 if (count($parts) > 1) {
204 foreach ($parts as $part) {
205 if (!empty($part)) {
206 $trail .= ('/'.$part);
207 $list['path'][] = array('name'=>$part, 'path'=>$trail);
210 } else {
211 $list['path'][] = array('name'=>$path, 'path'=>$path);
215 if (!empty($result->error)) {
216 // reset access key
217 set_user_preference($this->setting.'_access_key', '');
218 set_user_preference($this->setting.'_access_secret', '');
219 throw new repository_exception('repositoryerror', 'repository', '', $result->error);
221 if (empty($result->contents) or !is_array($result->contents)) {
222 return $list;
224 $files = $result->contents;
225 $dirslist = array();
226 $fileslist = array();
227 foreach ($files as $file) {
228 if ($file->is_dir) {
229 $dirslist[] = array(
230 'title' => substr($file->path, strpos($file->path, $current_path)+strlen($current_path)),
231 'path' => file_correct_filepath($file->path),
232 'date' => strtotime($file->modified),
233 'thumbnail' => $OUTPUT->pix_url(file_folder_icon(64))->out(false),
234 'thumbnail_height' => 64,
235 'thumbnail_width' => 64,
236 'children' => array(),
238 } else {
239 $thumbnail = null;
240 if ($file->thumb_exists) {
241 $thumburl = new moodle_url('/repository/dropbox/thumbnail.php',
242 array('repo_id' => $this->id,
243 'ctx_id' => $this->context->id,
244 'source' => $file->path,
245 'rev' => $file->rev // include revision to avoid cache problems
247 $thumbnail = $thumburl->out(false);
249 $fileslist[] = array(
250 'title' => substr($file->path, strpos($file->path, $current_path)+strlen($current_path)),
251 'source' => $file->path,
252 'size' => $file->bytes,
253 'date' => strtotime($file->modified),
254 'thumbnail' => $OUTPUT->pix_url(file_extension_icon($file->path, 64))->out(false),
255 'realthumbnail' => $thumbnail,
256 'thumbnail_height' => 64,
257 'thumbnail_width' => 64,
261 $fileslist = array_filter($fileslist, array($this, 'filter'));
262 $list['list'] = array_merge($dirslist, array_values($fileslist));
263 return $list;
267 * Displays a thumbnail for current user's dropbox file
269 * @param string $string
271 public function send_thumbnail($source) {
272 $saveas = $this->prepare_file('');
273 try {
274 $access_key = get_user_preferences($this->setting.'_access_key', '');
275 $access_secret = get_user_preferences($this->setting.'_access_secret', '');
276 $this->dropbox->set_access_token($access_key, $access_secret);
277 $this->dropbox->get_thumbnail($source, $saveas, self::SYNCIMAGE_TIMEOUT);
278 $content = file_get_contents($saveas);
279 unlink($saveas);
280 // set 30 days lifetime for the image. If the image is changed in dropbox it will have
281 // different revision number and URL will be different. It is completely safe
282 // to cache thumbnail in the browser for a long time
283 send_file($content, basename($source), 30*24*60*60, 0, true);
284 } catch (Exception $e) {}
288 * Logout from dropbox
289 * @return array
291 public function logout() {
292 set_user_preference($this->setting.'_access_key', '');
293 set_user_preference($this->setting.'_access_secret', '');
294 $this->access_key = '';
295 $this->access_secret = '';
296 return $this->print_login();
300 * Set dropbox option
301 * @param array $options
302 * @return mixed
304 public function set_option($options = array()) {
305 if (!empty($options['dropbox_key'])) {
306 set_config('dropbox_key', trim($options['dropbox_key']), 'dropbox');
308 if (!empty($options['dropbox_secret'])) {
309 set_config('dropbox_secret', trim($options['dropbox_secret']), 'dropbox');
311 if (!empty($options['dropbox_cachelimit'])) {
312 $this->cachelimit = (int)trim($options['dropbox_cachelimit']);
313 set_config('dropbox_cachelimit', $this->cachelimit, 'dropbox');
315 unset($options['dropbox_key']);
316 unset($options['dropbox_secret']);
317 unset($options['dropbox_cachelimit']);
318 $ret = parent::set_option($options);
319 return $ret;
323 * Get dropbox options
324 * @param string $config
325 * @return mixed
327 public function get_option($config = '') {
328 if ($config==='dropbox_key') {
329 return trim(get_config('dropbox', 'dropbox_key'));
330 } elseif ($config==='dropbox_secret') {
331 return trim(get_config('dropbox', 'dropbox_secret'));
332 } elseif ($config==='dropbox_cachelimit') {
333 return $this->max_cache_bytes();
334 } else {
335 $options = parent::get_option();
336 $options['dropbox_key'] = trim(get_config('dropbox', 'dropbox_key'));
337 $options['dropbox_secret'] = trim(get_config('dropbox', 'dropbox_secret'));
338 $options['dropbox_cachelimit'] = $this->max_cache_bytes();
340 return $options;
344 * Fixes references in DB that contains user credentials
346 * @param string $reference contents of DB field files_reference.reference
348 public function fix_old_style_reference($reference) {
349 $ref = unserialize($reference);
350 if (!isset($ref->url)) {
351 $this->dropbox->set_access_token($ref->access_key, $ref->access_secret);
352 $ref->url = $this->dropbox->get_file_share_link($ref->path, self::GETFILE_TIMEOUT);
353 if (!$ref->url) {
354 // some error occurred, do not fix reference for now
355 return $reference;
358 unset($ref->access_key);
359 unset($ref->access_secret);
360 $newreference = serialize($ref);
361 if ($newreference !== $reference) {
362 // we need to update references in the database
363 global $DB;
364 $params = array(
365 'newreference' => $newreference,
366 'newhash' => sha1($newreference),
367 'reference' => $reference,
368 'hash' => sha1($reference),
369 'repoid' => $this->id
371 $refid = $DB->get_field_sql('SELECT id FROM {files_reference}
372 WHERE reference = :reference AND referencehash = :hash
373 AND repositoryid = :repoid', $params);
374 if (!$refid) {
375 return $newreference;
377 $existingrefid = $DB->get_field_sql('SELECT id FROM {files_reference}
378 WHERE reference = :newreference AND referencehash = :newhash
379 AND repositoryid = :repoid', $params);
380 if ($existingrefid) {
381 // the same reference already exists, we unlink all files from it,
382 // link them to the current reference and remove the old one
383 $DB->execute('UPDATE {files} SET referencefileid = :refid
384 WHERE referencefileid = :existingrefid',
385 array('refid' => $refid, 'existingrefid' => $existingrefid));
386 $DB->delete_records('files_reference', array('id' => $existingrefid));
388 // update the reference
389 $params['refid'] = $refid;
390 $DB->execute('UPDATE {files_reference}
391 SET reference = :newreference, referencehash = :newhash
392 WHERE id = :refid', $params);
394 return $newreference;
398 * Converts a URL received from dropbox API function 'shares' into URL that
399 * can be used to download/access file directly
401 * @param string $sharedurl
402 * @return string
404 private function get_file_download_link($sharedurl) {
405 return preg_replace('|^(\w*://)www(.dropbox.com)|','\1dl\2',$sharedurl);
409 * Downloads a file from external repository and saves it in temp dir
411 * @throws moodle_exception when file could not be downloaded
413 * @param string $reference the content of files.reference field or result of
414 * function {@link repository_dropbox::get_file_reference()}
415 * @param string $saveas filename (without path) to save the downloaded file in the
416 * temporary directory, if omitted or file already exists the new filename will be generated
417 * @return array with elements:
418 * path: internal location of the file
419 * url: URL to the source (from parameters)
421 public function get_file($reference, $saveas = '') {
422 $ref = unserialize($reference);
423 $saveas = $this->prepare_file($saveas);
424 if (isset($ref->access_key) && isset($ref->access_secret) && isset($ref->path)) {
425 $this->dropbox->set_access_token($ref->access_key, $ref->access_secret);
426 return $this->dropbox->get_file($ref->path, $saveas, self::GETFILE_TIMEOUT);
427 } else if (isset($ref->url)) {
428 $c = new curl;
429 $url = $this->get_file_download_link($ref->url);
430 $result = $c->download_one($url, null, array('filepath' => $saveas, 'timeout' => self::GETFILE_TIMEOUT, 'followlocation' => true));
431 $info = $c->get_info();
432 if ($result !== true || !isset($info['http_code']) || $info['http_code'] != 200) {
433 throw new moodle_exception('errorwhiledownload', 'repository', '', $result);
435 return array('path'=>$saveas, 'url'=>$url);
437 throw new moodle_exception('cannotdownload', 'repository');
440 * Add Plugin settings input to Moodle form
442 * @param moodleform $mform Moodle form (passed by reference)
443 * @param string $classname repository class name
445 public static function type_config_form($mform, $classname = 'repository') {
446 global $CFG;
447 parent::type_config_form($mform);
448 $key = get_config('dropbox', 'dropbox_key');
449 $secret = get_config('dropbox', 'dropbox_secret');
451 if (empty($key)) {
452 $key = '';
454 if (empty($secret)) {
455 $secret = '';
458 $strrequired = get_string('required');
460 $mform->addElement('text', 'dropbox_key', get_string('apikey', 'repository_dropbox'), array('value'=>$key,'size' => '40'));
461 $mform->setType('dropbox_key', PARAM_RAW_TRIMMED);
462 $mform->addElement('text', 'dropbox_secret', get_string('secret', 'repository_dropbox'), array('value'=>$secret,'size' => '40'));
464 $mform->addRule('dropbox_key', $strrequired, 'required', null, 'client');
465 $mform->addRule('dropbox_secret', $strrequired, 'required', null, 'client');
466 $mform->setType('dropbox_secret', PARAM_RAW_TRIMMED);
467 $str_getkey = get_string('instruction', 'repository_dropbox');
468 $mform->addElement('static', null, '', $str_getkey);
470 $mform->addElement('text', 'dropbox_cachelimit', get_string('cachelimit', 'repository_dropbox'), array('size' => '40'));
471 $mform->addRule('dropbox_cachelimit', null, 'numeric', null, 'client');
472 $mform->setType('dropbox_cachelimit', PARAM_INT);
473 $mform->addElement('static', 'dropbox_cachelimit_info', '', get_string('cachelimit_info', 'repository_dropbox'));
477 * Option names of dropbox plugin
479 * @return array
481 public static function get_type_option_names() {
482 return array('dropbox_key', 'dropbox_secret', 'pluginname', 'dropbox_cachelimit');
486 * Dropbox plugin supports all kinds of files
488 * @return array
490 public function supported_filetypes() {
491 return '*';
495 * User cannot use the external link to dropbox
497 * @return int
499 public function supported_returntypes() {
500 return FILE_INTERNAL | FILE_REFERENCE | FILE_EXTERNAL;
504 * Return file URL for external link
506 * @param string $reference the result of get_file_reference()
507 * @return string
509 public function get_link($reference) {
510 $ref = unserialize($reference);
511 if (!isset($ref->url)) {
512 $this->dropbox->set_access_token($ref->access_key, $ref->access_secret);
513 $ref->url = $this->dropbox->get_file_share_link($ref->path, self::GETFILE_TIMEOUT);
515 return $this->get_file_download_link($ref->url);
519 * Prepare file reference information
521 * @param string $source
522 * @return string file referece
524 public function get_file_reference($source) {
525 global $USER;
526 $reference = new stdClass;
527 $reference->path = $source;
528 $reference->userid = $USER->id;
529 $reference->username = fullname($USER);
530 $reference->access_key = get_user_preferences($this->setting.'_access_key', '');
531 $reference->access_secret = get_user_preferences($this->setting.'_access_secret', '');
533 // by API we don't know if we need this reference to just download a file from dropbox
534 // into moodle filepool or create a reference. Since we need to create a shared link
535 // only in case of reference we analyze the script parameter
536 $usefilereference = optional_param('usefilereference', false, PARAM_BOOL);
537 if ($usefilereference) {
538 $this->dropbox->set_access_token($reference->access_key, $reference->access_secret);
539 $url = $this->dropbox->get_file_share_link($source, self::GETFILE_TIMEOUT);
540 if ($url) {
541 unset($reference->access_key);
542 unset($reference->access_secret);
543 $reference->url = $url;
546 return serialize($reference);
550 * Returns information about file in this repository by reference
551 * {@link repository::get_file_reference()}
552 * {@link repository::get_file()}
554 * Returns null if file not found or is not readable
556 * @param stdClass $reference file reference db record
557 * @return null|stdClass that has 'filepath' property
559 public function get_file_by_reference($reference) {
560 global $USER;
561 $ref = unserialize($reference->reference);
562 if (!isset($ref->url)) {
563 // this is an old-style reference in DB. We need to fix it
564 $ref = unserialize($this->fix_old_style_reference($reference->reference));
566 if (!isset($ref->url)) {
567 return null;
569 $c = new curl;
570 $url = $this->get_file_download_link($ref->url);
571 if (file_extension_in_typegroup($ref->path, 'web_image')) {
572 $saveas = $this->prepare_file('');
573 try {
574 $result = $c->download_one($url, array(), array('filepath' => $saveas, 'timeout' => self::SYNCIMAGE_TIMEOUT, 'followlocation' => true));
575 $info = $c->get_info();
576 if ($result === true && isset($info['http_code']) && $info['http_code'] == 200) {
577 return (object)array('filepath' => $saveas);
579 } catch (Exception $e) {}
581 $c->get($url, null, array('timeout' => self::SYNCIMAGE_TIMEOUT, 'followlocation' => true, 'nobody' => true));
582 $info = $c->get_info();
583 if (isset($info['http_code']) && $info['http_code'] == 200 &&
584 array_key_exists('download_content_length', $info) &&
585 $info['download_content_length'] >= 0) {
586 return (object)array('filesize' => (int)$info['download_content_length']);
588 return null;
592 * Cache file from external repository by reference
594 * Dropbox repository regularly caches all external files that are smaller than
595 * {@link repository_dropbox::max_cache_bytes()}
597 * @param string $reference this reference is generated by
598 * repository::get_file_reference()
599 * @param stored_file $storedfile created file reference
601 public function cache_file_by_reference($reference, $storedfile) {
602 try {
603 $this->import_external_file_contents($storedfile, $this->max_cache_bytes());
604 } catch (Exception $e) {}
608 * Return human readable reference information
609 * {@link stored_file::get_reference()}
611 * @param string $reference
612 * @param int $filestatus status of the file, 0 - ok, 666 - source missing
613 * @return string
615 public function get_reference_details($reference, $filestatus = 0) {
616 global $USER;
617 $ref = unserialize($reference);
618 $detailsprefix = $this->get_name();
619 if (isset($ref->userid) && $ref->userid != $USER->id && isset($ref->username)) {
620 $detailsprefix .= ' ('.$ref->username.')';
622 $details = $detailsprefix;
623 if (isset($ref->path)) {
624 $details .= ': '. $ref->path;
626 if (isset($ref->path) && !$filestatus) {
627 // Indicate this is from dropbox with path
628 return $details;
629 } else {
630 if (isset($ref->url)) {
631 $details = $detailsprefix. ': '. $ref->url;
633 return get_string('lostsource', 'repository', $details);
638 * Return the source information
640 * @param string $source
641 * @return string
643 public function get_file_source_info($source) {
644 global $USER;
645 return 'Dropbox ('.fullname($USER).'): ' . $source;
649 * Returns the maximum size of the Dropbox files to cache in moodle
651 * Note that {@link repository_dropbox::get_file_by_reference()} called by
652 * {@link repository::sync_external_file()} will try to cache images even
653 * when they are bigger in order to generate thumbnails. However there is
654 * a small timeout for downloading images for synchronisation and it will
655 * probably fail if the image is too big.
657 * @return int
659 public function max_cache_bytes() {
660 if ($this->cachelimit === null) {
661 $this->cachelimit = (int)get_config('dropbox', 'dropbox_cachelimit');
663 return $this->cachelimit;
667 * Repository method to serve the referenced file
669 * This method is ivoked from {@link send_stored_file()}.
670 * Dropbox repository first caches the file by reading it into temporary folder and then
671 * serves from there.
673 * @param stored_file $storedfile the file that contains the reference
674 * @param int $lifetime Number of seconds before the file should expire from caches (default 24 hours)
675 * @param int $filter 0 (default)=no filtering, 1=all files, 2=html files only
676 * @param bool $forcedownload If true (default false), forces download of file rather than view in browser/plugin
677 * @param array $options additional options affecting the file serving
679 public function send_file($storedfile, $lifetime=86400 , $filter=0, $forcedownload=false, array $options = null) {
680 $ref = unserialize($storedfile->get_reference());
681 if ($storedfile->get_filesize() > $this->max_cache_bytes()) {
682 header('Location: '.$this->get_file_download_link($ref->url));
683 die;
685 try {
686 $this->import_external_file_contents($storedfile, $this->max_cache_bytes());
687 if (!is_array($options)) {
688 $options = array();
690 $options['sendcachedexternalfile'] = true;
691 send_stored_file($storedfile, $lifetime, $filter, $forcedownload, $options);
692 } catch (moodle_exception $e) {
693 // redirect to Dropbox, it will show the error.
694 // We redirect to Dropbox shared link, not to download link here!
695 header('Location: '.$ref->url);
696 die;
701 * Caches all references to Dropbox files in moodle filepool
703 * Invoked by {@link repository_dropbox_cron()}. Only files smaller than
704 * {@link repository_dropbox::max_cache_bytes()} and only files which
705 * synchronisation timeout have not expired are cached.
707 public function cron() {
708 $fs = get_file_storage();
709 $files = $fs->get_external_files($this->id);
710 foreach ($files as $file) {
711 try {
712 // This call will cache all files that are smaller than max_cache_bytes()
713 // and synchronise file size of all others
714 $this->import_external_file_contents($file, $this->max_cache_bytes());
715 } catch (moodle_exception $e) {}
721 * Dropbox plugin cron task
723 function repository_dropbox_cron() {
724 $instances = repository::get_instances(array('type'=>'dropbox'));
725 foreach ($instances as $instance) {
726 $instance->cron();