Merge branch 'MDL-81457-main' of https://github.com/andrewnicols/moodle
[moodle.git] / repository / dropbox / lib.php
blob469fe3f2dc19e9e1f7650764ca99c6d7aab09372
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 Moodle 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');
28 /**
29 * Repository to access Dropbox files
31 * @package repository_dropbox
32 * @copyright 2010 Dongsheng Cai
33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 class repository_dropbox extends repository {
36 /**
37 * @var dropbox The instance of dropbox client.
39 private $dropbox;
41 /**
42 * @var int The maximum file size to cache in the moodle filepool.
44 public $cachelimit = null;
46 /**
47 * Constructor of dropbox plugin.
49 * @inheritDocs
51 public function __construct($repositoryid, $context = SYSCONTEXTID, $options = []) {
52 $options['page'] = optional_param('p', 1, PARAM_INT);
53 parent::__construct($repositoryid, $context, $options);
55 $returnurl = new moodle_url('/repository/repository_callback.php', [
56 'callback' => 'yes',
57 'repo_id' => $repositoryid,
58 'sesskey' => sesskey(),
59 ]);
61 // Create the dropbox API instance.
62 $issuer = \core\oauth2\api::get_issuer(get_config('dropbox', 'dropbox_issuerid'));
63 $this->dropbox = new repository_dropbox\dropbox($issuer, $returnurl);
66 /**
67 * Repository method to serve the referenced file.
69 * @inheritDocs
71 public function send_file($storedfile, $lifetime=null , $filter=0, $forcedownload=false, array $options = null) {
72 $reference = $this->unpack_reference($storedfile->get_reference());
74 $maxcachesize = $this->max_cache_bytes();
75 if (empty($maxcachesize)) {
76 // Always cache the file, regardless of size.
77 $cachefile = true;
78 } else {
79 // Size available. Only cache if it is under maxcachesize.
80 $cachefile = $storedfile->get_filesize() < $maxcachesize;
83 if (!$cachefile) {
84 \core\session\manager::write_close();
85 header('Location: ' . $this->get_file_download_link($reference->url));
86 die;
89 try {
90 $this->import_external_file_contents($storedfile, $this->max_cache_bytes());
91 if (!is_array($options)) {
92 $options = array();
94 $options['sendcachedexternalfile'] = true;
95 \core\session\manager::write_close();
96 send_stored_file($storedfile, $lifetime, $filter, $forcedownload, $options);
97 } catch (moodle_exception $e) {
98 // Redirect to Dropbox, it will show the error.
99 // Note: We redirect to Dropbox shared link, not to the download link here!
100 \core\session\manager::write_close();
101 header('Location: ' . $reference->url);
102 die;
107 * Return human readable reference information.
108 * {@link stored_file::get_reference()}
110 * @inheritDocs
112 public function get_reference_details($reference, $filestatus = 0) {
113 global $USER;
114 $ref = unserialize($reference);
115 $detailsprefix = $this->get_name();
116 if (isset($ref->userid) && $ref->userid != $USER->id && isset($ref->username)) {
117 $detailsprefix .= ' ('.$ref->username.')';
119 $details = $detailsprefix;
120 if (isset($ref->path)) {
121 $details .= ': '. $ref->path;
123 if (isset($ref->path) && !$filestatus) {
124 // Indicate this is from dropbox with path.
125 return $details;
126 } else {
127 if (isset($ref->url)) {
128 $details = $detailsprefix. ': '. $ref->url;
130 return get_string('lostsource', 'repository', $details);
135 * Cache file from external repository by reference.
136 * {@link repository::get_file_reference()}
137 * {@link repository::get_file()}
138 * Invoked at MOODLE/repository/repository_ajax.php.
140 * @inheritDocs
142 public function cache_file_by_reference($reference, $storedfile) {
143 try {
144 $this->import_external_file_contents($storedfile, $this->max_cache_bytes());
145 } catch (Exception $e) {
146 // Cache failure should not cause a fatal error. This is only a nice-to-have feature.
151 * Return the source information.
153 * The result of the function is stored in files.source field. It may be analysed
154 * when the source file is lost or repository may use it to display human-readable
155 * location of reference original.
157 * This method is called when file is picked for the first time only. When file
158 * (either copy or a reference) is already in moodle and it is being picked
159 * again to another file area (also as a copy or as a reference), the value of
160 * files.source is copied.
162 * @inheritDocs
164 public function get_file_source_info($source) {
165 global $USER;
166 return 'Dropbox ('.fullname($USER).'): ' . $source;
170 * Prepare file reference information.
172 * @inheritDocs
174 public function get_file_reference($source) {
175 global $USER;
176 $reference = new stdClass;
177 $reference->userid = $USER->id;
178 $reference->username = fullname($USER);
179 $reference->path = $source;
181 // Determine whether we are downloading the file, or should use a file reference.
182 $usefilereference = optional_param('usefilereference', false, PARAM_BOOL);
183 if ($usefilereference) {
184 if ($data = $this->dropbox->get_file_share_info($source)) {
185 $reference = (object) array_merge((array) $data, (array) $reference);
189 return serialize($reference);
193 * Return file URL for external link.
195 * @inheritDocs
197 public function get_link($reference) {
198 $unpacked = $this->unpack_reference($reference);
200 return $this->get_file_download_link($unpacked->url);
204 * Downloads a file from external repository and saves it in temp dir.
206 * @inheritDocs
208 public function get_file($reference, $saveas = '') {
209 $unpacked = $this->unpack_reference($reference);
211 // This is a shared link, and hopefully it is still active.
212 $downloadlink = $this->get_file_download_link($unpacked->url);
214 $saveas = $this->prepare_file($saveas);
215 file_put_contents($saveas, fopen($downloadlink, 'r'));
217 return ['path' => $saveas];
221 * Dropbox plugin supports all kinds of files.
223 * @inheritDocs
225 public function supported_filetypes() {
226 return '*';
230 * User cannot use the external link to dropbox.
232 * @inheritDocs
234 public function supported_returntypes() {
235 return FILE_INTERNAL | FILE_REFERENCE | FILE_EXTERNAL;
239 * Get dropbox files.
241 * @inheritDocs
243 public function get_listing($path = '', $page = '1') {
244 if (empty($path) || $path == '/') {
245 $path = '';
246 } else {
247 $path = file_correct_filepath($path);
250 $list = [
251 'list' => [],
252 'manage' => 'https://www.dropbox.com/home',
253 'logouturl' => 'https://www.dropbox.com/logout',
254 'message' => get_string('logoutdesc', 'repository_dropbox'),
255 'dynload' => true,
256 'path' => $this->process_breadcrumbs($path),
259 // Note - we deliberately do not catch the coding exceptions here.
260 try {
261 $result = $this->dropbox->get_listing($path);
262 } catch (\repository_dropbox\authentication_exception $e) {
263 // The token has expired.
264 return $this->print_login();
265 } catch (\repository_dropbox\dropbox_exception $e) {
266 // There was some other form of non-coding failure.
267 // This could be a rate limit, or it could be a server-side error.
268 // Just return early instead.
269 return $list;
272 if (!is_object($result) || empty($result)) {
273 return $list;
276 if (empty($result->entries) or !is_array($result->entries)) {
277 return $list;
280 $list['list'] = $this->process_entries($result->entries);
281 return $list;
285 * Get dropbox files in the specified path.
287 * @param string $query The search query
288 * @param int $page The page number
289 * @return array
291 public function search($query, $page = 0) {
292 $list = [
293 'list' => [],
294 'manage' => 'https://www.dropbox.com/home',
295 'logouturl' => 'https://www.dropbox.com/logout',
296 'message' => get_string('logoutdesc', 'repository_dropbox'),
297 'dynload' => true,
300 // Note - we deliberately do not catch the coding exceptions here.
301 try {
302 $result = $this->dropbox->search($query);
303 } catch (\repository_dropbox\authentication_exception $e) {
304 // The token has expired.
305 return $this->print_login();
306 } catch (\repository_dropbox\dropbox_exception $e) {
307 // There was some other form of non-coding failure.
308 // This could be a rate limit, or it could be a server-side error.
309 // Just return early instead.
310 return $list;
313 if (!is_object($result) || empty($result)) {
314 return $list;
317 if (empty($result->matches) or !is_array($result->matches)) {
318 return $list;
321 $list['list'] = $this->process_entries($result->matches);
322 return $list;
326 * Displays a thumbnail for current user's dropbox file.
328 * @inheritDocs
330 public function send_thumbnail($source) {
331 $content = $this->dropbox->get_thumbnail($source);
333 // Set 30 days lifetime for the image.
334 // If the image is changed in dropbox it will have different revision number and URL will be different.
335 // It is completely safe to cache the thumbnail in the browser for a long time.
336 send_file($content, basename($source), 30 * DAYSECS, 0, true);
340 * Fixes references in DB that contains user credentials.
342 * @param string $packed Content of DB field files_reference.reference
343 * @return string New serialized reference
345 protected function fix_old_style_reference($packed) {
346 $ref = unserialize($packed);
347 $ref = $this->dropbox->get_file_share_info($ref->path);
348 if (!$ref || empty($ref->url)) {
349 // Some error occurred, do not fix reference for now.
350 return $packed;
353 $newreference = serialize($ref);
354 if ($newreference !== $packed) {
355 // We need to update references in the database.
356 global $DB;
357 $params = array(
358 'newreference' => $newreference,
359 'newhash' => sha1($newreference),
360 'reference' => $packed,
361 'hash' => sha1($packed),
362 'repoid' => $this->id,
364 $refid = $DB->get_field_sql('SELECT id FROM {files_reference}
365 WHERE reference = :reference AND referencehash = :hash
366 AND repositoryid = :repoid', $params);
367 if (!$refid) {
368 return $newreference;
371 $existingrefid = $DB->get_field_sql('SELECT id FROM {files_reference}
372 WHERE reference = :newreference AND referencehash = :newhash
373 AND repositoryid = :repoid', $params);
374 if ($existingrefid) {
375 // The same reference already exists, we unlink all files from it,
376 // link them to the current reference and remove the old one.
377 $DB->execute('UPDATE {files} SET referencefileid = :refid
378 WHERE referencefileid = :existingrefid',
379 array('refid' => $refid, 'existingrefid' => $existingrefid));
380 $DB->delete_records('files_reference', array('id' => $existingrefid));
383 // Update the reference.
384 $params['refid'] = $refid;
385 $DB->execute('UPDATE {files_reference}
386 SET reference = :newreference, referencehash = :newhash
387 WHERE id = :refid', $params);
389 return $newreference;
393 * Unpack the supplied serialized reference, fixing it if required.
395 * @param string $packed The packed reference
396 * @return object The unpacked reference
398 protected function unpack_reference($packed) {
399 $reference = unserialize($packed);
400 if (empty($reference->url)) {
401 // The reference is missing some information. Attempt to update it.
402 return unserialize($this->fix_old_style_reference($packed));
405 return $reference;
409 * Converts a URL received from dropbox API function 'shares' into URL that
410 * can be used to download/access file directly
412 * @param string $sharedurl
413 * @return string
415 protected function get_file_download_link($sharedurl) {
416 $url = new \moodle_url($sharedurl);
417 $url->param('dl', 1);
419 return $url->out(false);
423 * Logout from dropbox.
425 * @inheritDocs
427 public function logout() {
428 $this->dropbox->logout();
430 return $this->print_login();
434 * Check if the dropbox is logged in via the oauth process.
436 * @inheritDocs
438 public function check_login() {
439 return $this->dropbox->is_logged_in();
443 * Generate dropbox login url.
445 * @inheritDocs
447 public function print_login() {
448 $url = $this->dropbox->get_login_url();
449 if ($this->options['ajax']) {
450 $ret = array();
451 $btn = new \stdClass();
452 $btn->type = 'popup';
453 $btn->url = $url->out(false);
454 $ret['login'] = array($btn);
455 return $ret;
456 } else {
457 echo html_writer::link($url, get_string('login', 'repository'), array('target' => '_blank'));
462 * Request access token.
464 * @inheritDocs
466 public function callback() {
467 $this->dropbox->callback();
471 * Caches all references to Dropbox files in moodle filepool.
473 * Invoked by {@link repository_dropbox_cron()}. Only files smaller than
474 * {@link repository_dropbox::max_cache_bytes()} and only files which
475 * synchronisation timeout have not expired are cached.
477 * @inheritDocs
479 public function cron() {
480 $fs = get_file_storage();
481 $files = $fs->get_external_files($this->id);
482 $fetchedreferences = [];
483 foreach ($files as $file) {
484 if (isset($fetchedreferences[$file->get_referencefileid()])) {
485 continue;
487 try {
488 // This call will cache all files that are smaller than max_cache_bytes()
489 // and synchronise file size of all others.
490 $this->import_external_file_contents($file, $this->max_cache_bytes());
491 $fetchedreferences[$file->get_referencefileid()] = true;
492 } catch (moodle_exception $e) {
493 // If an exception is thrown, just continue. This is only a pre-fetch to help speed up general use.
499 * Add Plugin settings input to Moodle form.
501 * @inheritDocs
503 public static function type_config_form($mform, $classname = 'repository') {
504 parent::type_config_form($mform);
505 $options = [];
506 $issuers = \core\oauth2\api::get_all_issuers();
507 foreach ($issuers as $issuer) {
508 $options[$issuer->get('id')] = s($issuer->get('name'));
510 $strrequired = get_string('required');
511 $mform->addElement('select', 'dropbox_issuerid', get_string('issuer', 'repository_dropbox'), $options);
512 $mform->addHelpButton('dropbox_issuerid', 'issuer', 'repository_dropbox');
513 $mform->addRule('dropbox_issuerid', $strrequired, 'required', null, 'client');
514 $mform->addElement('text', 'dropbox_cachelimit', get_string('cachelimit', 'repository_dropbox'), array('size' => '40'));
515 $mform->addRule('dropbox_cachelimit', null, 'numeric', null, 'client');
516 $mform->setType('dropbox_cachelimit', PARAM_INT);
517 $mform->addElement('static', 'dropbox_cachelimit_info', '', get_string('cachelimit_info', 'repository_dropbox'));
522 * Set options.
524 * @param array $options
525 * @return mixed
527 public function set_option($options = []) {
528 if (!empty($options['dropbox_issuerid'])) {
529 set_config('dropbox_issuerid', trim($options['dropbox_issuerid']), 'dropbox');
530 unset($options['dropbox_issuerid']);
532 if (!empty($options['dropbox_cachelimit'])) {
533 $this->cachelimit = (int) trim($options['dropbox_cachelimit']);
534 set_config('dropbox_cachelimit', $this->cachelimit, 'dropbox');
535 unset($options['dropbox_cachelimit']);
538 return parent::set_option($options);
542 * Get dropbox options
543 * @param string $config
544 * @return mixed
546 public function get_option($config = '') {
547 if ($config === 'dropbox_issuerid') {
548 return trim(get_config('dropbox', 'dropbox_issuerid'));
549 } else if ($config === 'dropbox_cachelimit') {
550 return $this->max_cache_bytes();
551 } else {
552 $options = parent::get_option();
553 $options['dropbox_issuerid'] = trim(get_config('dropbox', 'dropbox_issuerid'));
554 $options['dropbox_cachelimit'] = $this->max_cache_bytes();
557 return $options;
561 * Return the OAuth 2 Redirect URI.
563 * @return moodle_url
565 public static function get_oauth2callbackurl() {
566 global $CFG;
568 return new moodle_url('/admin/oauth2callback.php');
572 * Option names of dropbox plugin.
574 * @inheritDocs
576 public static function get_type_option_names() {
577 return [
578 'dropbox_issuerid',
579 'pluginname',
580 'dropbox_cachelimit',
585 * Performs synchronisation of an external file if the previous one has expired.
587 * This function must be implemented for external repositories supporting
588 * FILE_REFERENCE, it is called for existing aliases when their filesize,
589 * contenthash or timemodified are requested. It is not called for internal
590 * repositories (see {@link repository::has_moodle_files()}), references to
591 * internal files are updated immediately when source is modified.
593 * Referenced files may optionally keep their content in Moodle filepool (for
594 * thumbnail generation or to be able to serve cached copy). In this
595 * case both contenthash and filesize need to be synchronized. Otherwise repositories
596 * should use contenthash of empty file and correct filesize in bytes.
598 * Note that this function may be run for EACH file that needs to be synchronised at the
599 * moment. If anything is being downloaded or requested from external sources there
600 * should be a small timeout. The synchronisation is performed to update the size of
601 * the file and/or to update image and re-generated image preview. There is nothing
602 * fatal if syncronisation fails but it is fatal if syncronisation takes too long
603 * and hangs the script generating a page.
605 * Note: If you wish to call $file->get_filesize(), $file->get_contenthash() or
606 * $file->get_timemodified() make sure that recursion does not happen.
608 * Called from {@link stored_file::sync_external_file()}
610 * @inheritDocs
612 public function sync_reference(stored_file $file) {
613 global $CFG;
615 if ($file->get_referencelastsync() + DAYSECS > time()) {
616 // Only synchronise once per day.
617 return false;
620 $reference = $this->unpack_reference($file->get_reference());
621 if (!isset($reference->url)) {
622 // The URL to sync with is missing.
623 return false;
626 $c = new curl;
627 $url = $this->get_file_download_link($reference->url);
628 if (file_extension_in_typegroup($reference->path, 'web_image')) {
629 $saveas = $this->prepare_file('');
630 try {
631 $result = $c->download_one($url, [], [
632 'filepath' => $saveas,
633 'timeout' => $CFG->repositorysyncimagetimeout,
634 'followlocation' => true,
636 $info = $c->get_info();
637 if ($result === true && isset($info['http_code']) && $info['http_code'] == 200) {
638 $file->set_synchronised_content_from_file($saveas);
639 return true;
641 } catch (Exception $e) {
642 // IF the download_one fails, we will attempt to download
643 // again with get() anyway.
647 $c->get($url, null, array('timeout' => $CFG->repositorysyncimagetimeout, 'followlocation' => true, 'nobody' => true));
648 $info = $c->get_info();
649 if (isset($info['http_code']) && $info['http_code'] == 200 &&
650 array_key_exists('download_content_length', $info) &&
651 $info['download_content_length'] >= 0) {
652 $filesize = (int)$info['download_content_length'];
653 $file->set_synchronized(null, $filesize);
654 return true;
656 $file->set_missingsource();
657 return true;
661 * Process a standard entries list.
663 * @param array $entries The list of entries returned from the API
664 * @return array The manipulated entries for display in the file picker
666 protected function process_entries(array $entries) {
667 global $OUTPUT;
669 $dirslist = [];
670 $fileslist = [];
671 foreach ($entries as $entry) {
672 $entrydata = $entry;
673 if (isset($entrydata->metadata)) {
674 // If this is metadata, fetch the metadata content.
675 // We only use the consistent parts of the file, folder, and metadata.
676 $entrydata = $entrydata->metadata;
679 // Due to a change in the api, the actual content is in a nested metadata tree.
680 if ($entrydata->{".tag"} == "metadata" && isset($entrydata->metadata)) {
681 $entrydata = $entrydata->metadata;
684 if ($entrydata->{".tag"} === "folder") {
685 $dirslist[] = [
686 'title' => $entrydata->name,
687 // Use the display path here rather than lower.
688 // Dropbox is case insensitive but this leads to more accurate breadcrumbs.
689 'path' => file_correct_filepath($entrydata->path_display),
690 'thumbnail' => $OUTPUT->image_url(file_folder_icon())->out(false),
691 'thumbnail_height' => 64,
692 'thumbnail_width' => 64,
693 'children' => array(),
695 } else if ($entrydata->{".tag"} === "file") {
696 $fileslist[] = [
697 'title' => $entrydata->name,
698 // Use the path_lower here to make life easier elsewhere.
699 'source' => $entrydata->path_lower,
700 'size' => $entrydata->size,
701 'date' => strtotime($entrydata->client_modified),
702 'thumbnail' => $OUTPUT->image_url(file_extension_icon($entrydata->path_lower))->out(false),
703 'realthumbnail' => $this->get_thumbnail_url($entrydata),
704 'thumbnail_height' => 64,
705 'thumbnail_width' => 64,
710 $fileslist = array_filter($fileslist, array($this, 'filter'));
712 return array_merge($dirslist, array_values($fileslist));
716 * Process the breadcrumbs for a listing.
718 * @param string $path The path to create breadcrumbs for
719 * @return array
721 protected function process_breadcrumbs($path) {
722 // Process breadcrumb trail.
723 // Note: Dropbox is case insensitive.
724 // Without performing an additional API call, it isn't possible to get the path_display.
725 // As a result, the path here is the path_lower.
726 $breadcrumbs = [
728 'path' => '/',
729 'name' => get_string('dropbox', 'repository_dropbox'),
733 $path = rtrim($path, '/');
734 $directories = explode('/', $path);
735 $pathtodate = '';
736 foreach ($directories as $directory) {
737 if ($directory === '') {
738 continue;
740 $pathtodate .= '/' . $directory;
741 $breadcrumbs[] = [
742 'path' => $pathtodate,
743 'name' => $directory,
747 return $breadcrumbs;
751 * Grab the thumbnail URL for the specified entry.
753 * @param object $entry The file entry as retrieved from the API
754 * @return moodle_url
756 protected function get_thumbnail_url($entry) {
757 if ($this->dropbox->supports_thumbnail($entry)) {
758 $thumburl = new moodle_url('/repository/dropbox/thumbnail.php', [
759 // The id field in dropbox is unique - no need to specify a revision.
760 'source' => $entry->id,
761 'path' => $entry->path_lower,
763 'repo_id' => $this->id,
764 'ctx_id' => $this->context->id,
766 return $thumburl->out(false);
769 return '';
773 * Returns the maximum size of the Dropbox files to cache in moodle.
775 * Note that {@link repository_dropbox::sync_reference()} will try to cache images even
776 * when they are bigger in order to generate thumbnails. However there is
777 * a small timeout for downloading images for synchronisation and it will
778 * probably fail if the image is too big.
780 * @return int
782 public function max_cache_bytes() {
783 if ($this->cachelimit === null) {
784 $this->cachelimit = (int) get_config('dropbox', 'dropbox_cachelimit');
786 return $this->cachelimit;