From 19098c49d9d8fa0f35e80c3fd195951c751011a0 Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Thu, 14 Jul 2016 13:13:11 +0800 Subject: [PATCH 1/3] MDL-52051 oauth: expires_in is optional --- lib/oauthlib.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/oauthlib.php b/lib/oauthlib.php index 423480c2329..7d83e82e68b 100644 --- a/lib/oauthlib.php +++ b/lib/oauthlib.php @@ -517,7 +517,10 @@ abstract class oauth2_client extends curl { // Store the token an expiry time. $accesstoken = new stdClass; $accesstoken->token = $r->access_token; - $accesstoken->expires = (time() + ($r->expires_in - 10)); // Expires 10 seconds before actual expiry. + if (isset($r->expires_in)) { + // Expires 10 seconds before actual expiry. + $accesstoken->expires = (time() + ($r->expires_in - 10)); + } $this->store_token($accesstoken); return true; From 8069695b30eeb9590fd01c54fd2b197f4fb30c99 Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Tue, 13 Sep 2016 12:14:27 +0800 Subject: [PATCH 2/3] MDL-52051 repository_dropbox: Move v1 API aside --- repository/dropbox/classes/legacy.php | 654 ++++++++++++++ .../legacy_dropbox.php} | 2 +- repository/dropbox/lib.php | 833 ++++++------------ 3 files changed, 900 insertions(+), 589 deletions(-) create mode 100644 repository/dropbox/classes/legacy.php rename repository/dropbox/{locallib.php => classes/legacy_dropbox.php} (98%) diff --git a/repository/dropbox/classes/legacy.php b/repository/dropbox/classes/legacy.php new file mode 100644 index 00000000000..8dc34248ba9 --- /dev/null +++ b/repository/dropbox/classes/legacy.php @@ -0,0 +1,654 @@ +. + +/** + * This plugin is used to access user's dropbox files + * + * @since Moodle 2.0 + * @package repository_dropbox + * @copyright 2012 Marina Glancy + * @copyright 2010 Dongsheng Cai {@link http://dongsheng.org} + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +defined('MOODLE_INTERNAL') || die(); + +require_once($CFG->dirroot . '/repository/lib.php'); + +/** + * Repository to access Dropbox files + * + * @package repository_dropbox + * @copyright 2010 Dongsheng Cai + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class repository_dropbox_legacy extends repository { + /** @var dropbox the instance of dropbox client */ + private $dropbox; + /** @var array files */ + public $files; + /** @var bool flag of login status */ + public $logged = false; + /** @var int maximum size of file to cache in moodle filepool */ + public $cachelimit = null; + + /** @var int cached file ttl */ + private $cachedfilettl = null; + + /** + * Constructor of dropbox plugin + * + * @param int $repositoryid + * @param stdClass $context + * @param array $options + */ + public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array()) { + global $CFG; + $options['page'] = optional_param('p', 1, PARAM_INT); + parent::__construct($repositoryid, $context, $options); + + $this->setting = 'dropbox_'; + + $this->dropbox_key = $this->get_option('dropbox_key'); + $this->dropbox_secret = $this->get_option('dropbox_secret'); + + // One day. + $this->cachedfilettl = 60 * 60 * 24; + + if (isset($options['access_key'])) { + $this->access_key = $options['access_key']; + } else { + $this->access_key = get_user_preferences($this->setting.'_access_key', ''); + } + if (isset($options['access_secret'])) { + $this->access_secret = $options['access_secret']; + } else { + $this->access_secret = get_user_preferences($this->setting.'_access_secret', ''); + } + + if (!empty($this->access_key) && !empty($this->access_secret)) { + $this->logged = true; + } + + $callbackurl = new moodle_url($CFG->wwwroot.'/repository/repository_callback.php', array( + 'callback' => 'yes', + 'repo_id' => $repositoryid + )); + + $args = array( + 'oauth_consumer_key' => $this->dropbox_key, + 'oauth_consumer_secret' => $this->dropbox_secret, + 'oauth_callback' => $callbackurl->out(false), + 'api_root' => 'https://api.dropbox.com/1/oauth', + ); + + $this->dropbox = new repository_dropbox_legacy_dropbox($args); + } + + /** + * Get the type name (no whitespace) + * For a human readable name, use get_readablename() + * + * @return string the type name + */ + public function get_typename() { + return 'dropbox'; + } + + /** + * Set access key + * + * @param string $access_key + */ + public function set_access_key($access_key) { + $this->access_key = $access_key; + } + + /** + * Set access secret + * + * @param string $access_secret + */ + public function set_access_secret($access_secret) { + $this->access_secret = $access_secret; + } + + + /** + * Check if moodle has got access token and secret + * + * @return bool + */ + public function check_login() { + return !empty($this->logged); + } + + /** + * Generate dropbox login url + * + * @return array + */ + public function print_login() { + $result = $this->dropbox->request_token(); + set_user_preference($this->setting.'_request_secret', $result['oauth_token_secret']); + $url = $result['authorize_url']; + if ($this->options['ajax']) { + $ret = array(); + $popup_btn = new stdClass(); + $popup_btn->type = 'popup'; + $popup_btn->url = $url; + $ret['login'] = array($popup_btn); + return $ret; + } else { + echo ''.get_string('login', 'repository').''; + } + } + + /** + * Request access token + * + * @return array + */ + public function callback() { + $token = optional_param('oauth_token', '', PARAM_TEXT); + $secret = get_user_preferences($this->setting.'_request_secret', ''); + $access_token = $this->dropbox->get_access_token($token, $secret); + set_user_preference($this->setting.'_access_key', $access_token['oauth_token']); + set_user_preference($this->setting.'_access_secret', $access_token['oauth_token_secret']); + } + + /** + * Get dropbox files + * + * @param string $path + * @param int $page + * @return array + */ + public function get_listing($path = '', $page = '1') { + global $OUTPUT; + if (empty($path) || $path == '/') { + $path = '/'; + } else { + $path = file_correct_filepath($path); + } + $encoded_path = str_replace("%2F", "/", rawurlencode($path)); + + $list = array(); + $list['list'] = array(); + $list['manage'] = 'https://www.dropbox.com/home'; + $list['dynload'] = true; + $list['nosearch'] = true; + $list['logouturl'] = 'https://www.dropbox.com/logout'; + $list['message'] = get_string('logoutdesc', 'repository_dropbox'); + // Process breadcrumb trail. + $list['path'] = array( + array('name' => get_string('dropbox', 'repository_dropbox'), 'path' => '/') + ); + + $result = $this->dropbox->get_listing($encoded_path, $this->access_key, $this->access_secret); + + if (!is_object($result) || empty($result)) { + return $list; + } + if (empty($result->path)) { + $current_path = '/'; + } else { + $current_path = file_correct_filepath($result->path); + } + + $trail = ''; + if (!empty($path)) { + $parts = explode('/', $path); + if (count($parts) > 1) { + foreach ($parts as $part) { + if (!empty($part)) { + $trail .= ('/'.$part); + $list['path'][] = array('name' => $part, 'path' => $trail); + } + } + } else { + $list['path'][] = array('name' => $path, 'path' => $path); + } + } + + if (!empty($result->error)) { + // Reset access key. + set_user_preference($this->setting.'_access_key', ''); + set_user_preference($this->setting.'_access_secret', ''); + throw new repository_exception('repositoryerror', 'repository', '', $result->error); + } + if (empty($result->contents) or !is_array($result->contents)) { + return $list; + } + $files = $result->contents; + $dirslist = array(); + $fileslist = array(); + foreach ($files as $file) { + if ($file->is_dir) { + $dirslist[] = array( + 'title' => substr($file->path, strpos($file->path, $current_path) + strlen($current_path)), + 'path' => file_correct_filepath($file->path), + 'date' => strtotime($file->modified), + 'thumbnail' => $OUTPUT->pix_url(file_folder_icon(64))->out(false), + 'thumbnail_height' => 64, + 'thumbnail_width' => 64, + 'children' => array(), + ); + } else { + $thumbnail = null; + if ($file->thumb_exists) { + $thumburl = new moodle_url('/repository/dropbox/thumbnail.php', + array('repo_id' => $this->id, + 'ctx_id' => $this->context->id, + 'source' => $file->path, + 'rev' => $file->rev // include revision to avoid cache problems + )); + $thumbnail = $thumburl->out(false); + } + $fileslist[] = array( + 'title' => substr($file->path, strpos($file->path, $current_path) + strlen($current_path)), + 'source' => $file->path, + 'size' => $file->bytes, + 'date' => strtotime($file->modified), + 'thumbnail' => $OUTPUT->pix_url(file_extension_icon($file->path, 64))->out(false), + 'realthumbnail' => $thumbnail, + 'thumbnail_height' => 64, + 'thumbnail_width' => 64, + ); + } + } + $fileslist = array_filter($fileslist, array($this, 'filter')); + $list['list'] = array_merge($dirslist, array_values($fileslist)); + return $list; + } + + /** + * Displays a thumbnail for current user's dropbox file + * + * @param string $string + */ + public function send_thumbnail($source) { + global $CFG; + $saveas = $this->prepare_file(''); + try { + $access_key = get_user_preferences($this->setting.'_access_key', ''); + $access_secret = get_user_preferences($this->setting.'_access_secret', ''); + $this->dropbox->set_access_token($access_key, $access_secret); + $this->dropbox->get_thumbnail($source, $saveas, $CFG->repositorysyncimagetimeout); + $content = file_get_contents($saveas); + unlink($saveas); + // Set 30 days lifetime for the image. If the image is changed in dropbox it will have + // different revision number and URL will be different. It is completely safe + // to cache thumbnail in the browser for a long time. + send_file($content, basename($source), 30*24*60*60, 0, true); + } catch (Exception $e) {} + } + + /** + * Logout from dropbox + * @return array + */ + public function logout() { + set_user_preference($this->setting.'_access_key', ''); + set_user_preference($this->setting.'_access_secret', ''); + $this->access_key = ''; + $this->access_secret = ''; + return $this->print_login(); + } + + /** + * Fixes references in DB that contains user credentials + * + * @param string $reference contents of DB field files_reference.reference + */ + public function fix_old_style_reference($reference) { + global $CFG; + $ref = unserialize($reference); + if (!isset($ref->url)) { + $this->dropbox->set_access_token($ref->access_key, $ref->access_secret); + $ref->url = $this->dropbox->get_file_share_link($ref->path, $CFG->repositorygetfiletimeout); + if (!$ref->url) { + // Some error occurred, do not fix reference for now. + return $reference; + } + } + unset($ref->access_key); + unset($ref->access_secret); + $newreference = serialize($ref); + if ($newreference !== $reference) { + // We need to update references in the database. + global $DB; + $params = array( + 'newreference' => $newreference, + 'newhash' => sha1($newreference), + 'reference' => $reference, + 'hash' => sha1($reference), + 'repoid' => $this->id + ); + $refid = $DB->get_field_sql('SELECT id FROM {files_reference} + WHERE reference = :reference AND referencehash = :hash + AND repositoryid = :repoid', $params); + if (!$refid) { + return $newreference; + } + $existingrefid = $DB->get_field_sql('SELECT id FROM {files_reference} + WHERE reference = :newreference AND referencehash = :newhash + AND repositoryid = :repoid', $params); + if ($existingrefid) { + // The same reference already exists, we unlink all files from it, + // link them to the current reference and remove the old one. + $DB->execute('UPDATE {files} SET referencefileid = :refid + WHERE referencefileid = :existingrefid', + array('refid' => $refid, 'existingrefid' => $existingrefid)); + $DB->delete_records('files_reference', array('id' => $existingrefid)); + } + // Update the reference. + $params['refid'] = $refid; + $DB->execute('UPDATE {files_reference} + SET reference = :newreference, referencehash = :newhash + WHERE id = :refid', $params); + } + return $newreference; + } + + /** + * Converts a URL received from dropbox API function 'shares' into URL that + * can be used to download/access file directly + * + * @param string $sharedurl + * @return string + */ + private function get_file_download_link($sharedurl) { + $url = new \moodle_url($sharedurl); + $url->param('dl', 1); + + return $url->out(false); + } + + /** + * Downloads a file from external repository and saves it in temp dir + * + * @throws moodle_exception when file could not be downloaded + * + * @param string $reference the content of files.reference field or result of + * function {@link repository_dropbox::get_file_reference()} + * @param string $saveas filename (without path) to save the downloaded file in the + * temporary directory, if omitted or file already exists the new filename will be generated + * @return array with elements: + * path: internal location of the file + * url: URL to the source (from parameters) + */ + public function get_file($reference, $saveas = '') { + global $CFG; + $ref = unserialize($reference); + $saveas = $this->prepare_file($saveas); + if (isset($ref->access_key) && isset($ref->access_secret) && isset($ref->path)) { + $this->dropbox->set_access_token($ref->access_key, $ref->access_secret); + return $this->dropbox->get_file($ref->path, $saveas, $CFG->repositorygetfiletimeout); + } else if (isset($ref->url)) { + $c = new curl; + $url = $this->get_file_download_link($ref->url); + $result = $c->download_one($url, null, array('filepath' => $saveas, 'timeout' => $CFG->repositorygetfiletimeout, 'followlocation' => true)); + $info = $c->get_info(); + if ($result !== true || !isset($info['http_code']) || $info['http_code'] != 200) { + throw new moodle_exception('errorwhiledownload', 'repository', '', $result); + } + return array('path' => $saveas, 'url' => $url); + } + throw new moodle_exception('cannotdownload', 'repository'); + } + + /** + * Dropbox plugin supports all kinds of files + * + * @return array + */ + public function supported_filetypes() { + return '*'; + } + + /** + * User cannot use the external link to dropbox + * + * @return int + */ + public function supported_returntypes() { + return FILE_INTERNAL | FILE_REFERENCE | FILE_EXTERNAL; + } + + /** + * Return file URL for external link + * + * @param string $reference the result of get_file_reference() + * @return string + */ + public function get_link($reference) { + global $CFG; + $ref = unserialize($reference); + if (!isset($ref->url)) { + $this->dropbox->set_access_token($ref->access_key, $ref->access_secret); + $ref->url = $this->dropbox->get_file_share_link($ref->path, $CFG->repositorygetfiletimeout); + } + return $this->get_file_download_link($ref->url); + } + + /** + * Prepare file reference information + * + * @param string $source + * @return string file referece + */ + public function get_file_reference($source) { + global $USER, $CFG; + $reference = new stdClass; + $reference->path = $source; + $reference->userid = $USER->id; + $reference->username = fullname($USER); + $reference->access_key = get_user_preferences($this->setting.'_access_key', ''); + $reference->access_secret = get_user_preferences($this->setting.'_access_secret', ''); + + // By API we don't know if we need this reference to just download a file from dropbox + // into moodle filepool or create a reference. Since we need to create a shared link + // only in case of reference we analyze the script parameter. + $usefilereference = optional_param('usefilereference', false, PARAM_BOOL); + if ($usefilereference) { + $this->dropbox->set_access_token($reference->access_key, $reference->access_secret); + $url = $this->dropbox->get_file_share_link($source, $CFG->repositorygetfiletimeout); + if ($url) { + unset($reference->access_key); + unset($reference->access_secret); + $reference->url = $url; + } + } + return serialize($reference); + } + + public function sync_reference(stored_file $file) { + global $CFG; + + if ($file->get_referencelastsync() + DAYSECS > time()) { + // Synchronise not more often than once a day. + return false; + } + $ref = unserialize($file->get_reference()); + if (!isset($ref->url)) { + // This is an old-style reference in DB. We need to fix it. + $ref = unserialize($this->fix_old_style_reference($file->get_reference())); + } + if (!isset($ref->url)) { + return false; + } + $c = new curl; + $url = $this->get_file_download_link($ref->url); + if (file_extension_in_typegroup($ref->path, 'web_image')) { + $saveas = $this->prepare_file(''); + try { + $result = $c->download_one($url, array(), array('filepath' => $saveas, 'timeout' => $CFG->repositorysyncimagetimeout, 'followlocation' => true)); + $info = $c->get_info(); + if ($result === true && isset($info['http_code']) && $info['http_code'] == 200) { + $fs = get_file_storage(); + list($contenthash, $filesize, $newfile) = $fs->add_file_to_pool($saveas); + $file->set_synchronized($contenthash, $filesize); + return true; + } + } catch (Exception $e) { + } + } + $c->get($url, null, array('timeout' => $CFG->repositorysyncimagetimeout, 'followlocation' => true, 'nobody' => true)); + $info = $c->get_info(); + if (isset($info['http_code']) && $info['http_code'] == 200 && + array_key_exists('download_content_length', $info) && + $info['download_content_length'] >= 0) { + $filesize = (int)$info['download_content_length']; + $file->set_synchronized(null, $filesize); + return true; + } + $file->set_missingsource(); + return true; + } + + /** + * Cache file from external repository by reference. + * + * Dropbox repository regularly caches all external files that are smaller than + * {@link repository_dropbox::max_cache_bytes()} + * + * @param string $reference this reference is generated by + * repository::get_file_reference() + * @param stored_file $storedfile created file reference + */ + public function cache_file_by_reference($reference, $storedfile) { + try { + $this->import_external_file_contents($storedfile, $this->max_cache_bytes()); + } catch (Exception $e) { + } + } + + /** + * Return human readable reference information. + * {@link stored_file::get_reference()} + * + * @param string $reference + * @param int $filestatus status of the file, 0 - ok, 666 - source missing + * @return string + */ + public function get_reference_details($reference, $filestatus = 0) { + global $USER; + $ref = unserialize($reference); + $detailsprefix = $this->get_name(); + if (isset($ref->userid) && $ref->userid != $USER->id && isset($ref->username)) { + $detailsprefix .= ' ('.$ref->username.')'; + } + $details = $detailsprefix; + if (isset($ref->path)) { + $details .= ': '. $ref->path; + } + if (isset($ref->path) && !$filestatus) { + // Indicate this is from dropbox with path. + return $details; + } else { + if (isset($ref->url)) { + $details = $detailsprefix. ': '. $ref->url; + } + return get_string('lostsource', 'repository', $details); + } + } + + /** + * Return the source information + * + * @param string $source + * @return string + */ + public function get_file_source_info($source) { + global $USER; + return 'Dropbox ('.fullname($USER).'): ' . $source; + } + + /** + * Repository method to serve the referenced file + * + * This method is ivoked from {@link send_stored_file()}. + * Dropbox repository first caches the file by reading it into temporary folder and then + * serves from there. + * + * @param stored_file $storedfile the file that contains the reference + * @param int $lifetime Number of seconds before the file should expire from caches (null means $CFG->filelifetime) + * @param int $filter 0 (default)=no filtering, 1=all files, 2=html files only + * @param bool $forcedownload If true (default false), forces download of file rather than view in browser/plugin + * @param array $options additional options affecting the file serving + */ + public function send_file($storedfile, $lifetime=null , $filter=0, $forcedownload=false, array $options = null) { + $ref = unserialize($storedfile->get_reference()); + if ($storedfile->get_filesize() > $this->max_cache_bytes()) { + header('Location: '.$this->get_file_download_link($ref->url)); + die; + } + try { + $this->import_external_file_contents($storedfile, $this->max_cache_bytes()); + if (!is_array($options)) { + $options = array(); + } + $options['sendcachedexternalfile'] = true; + send_stored_file($storedfile, $lifetime, $filter, $forcedownload, $options); + } catch (moodle_exception $e) { + // Redirect to Dropbox, it will show the error. + // We redirect to Dropbox shared link, not to download link here! + header('Location: '.$ref->url); + die; + } + } + + /** + * Returns the maximum size of the Dropbox files to cache in moodle. + * + * Note that {@link repository_dropbox::sync_reference()} will try to cache images even + * when they are bigger in order to generate thumbnails. However there is + * a small timeout for downloading images for synchronisation and it will + * probably fail if the image is too big. + * + * @return int + */ + public function max_cache_bytes() { + if ($this->cachelimit === null) { + $this->cachelimit = (int) get_config('dropbox', 'dropbox_cachelimit'); + } + return $this->cachelimit; + } + + /** + * Caches all references to Dropbox files in moodle filepool + * + * Invoked by {@link repository_dropbox_cron()}. Only files smaller than + * {@link repository_dropbox::max_cache_bytes()} and only files which + * synchronisation timeout have not expired are cached. + */ + public function cron() { + $fs = get_file_storage(); + $files = $fs->get_external_files($this->id); + $fetchedreferences = []; + foreach ($files as $file) { + if (isset($fetchedreferences[$file->get_referencefileid()])) { + continue; + } + try { + // This call will cache all files that are smaller than max_cache_bytes() + // and synchronise file size of all others + $this->import_external_file_contents($file, $this->max_cache_bytes()); + $fetchedreferences[$file->get_referencefileid()] = true; + } catch (moodle_exception $e) {} + } + } +} diff --git a/repository/dropbox/locallib.php b/repository/dropbox/classes/legacy_dropbox.php similarity index 98% rename from repository/dropbox/locallib.php rename to repository/dropbox/classes/legacy_dropbox.php index d376685558b..5cfb5183081 100644 --- a/repository/dropbox/locallib.php +++ b/repository/dropbox/classes/legacy_dropbox.php @@ -35,7 +35,7 @@ require_once($CFG->libdir.'/oauthlib.php'); * @copyright 2010 Dongsheng Cai * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class dropbox extends oauth_helper { +class repository_dropbox_legacy_dropbox extends oauth_helper { /** @var string dropbox access type, can be dropbox or sandbox */ private $mode = 'dropbox'; /** @var string dropbox api url*/ diff --git a/repository/dropbox/lib.php b/repository/dropbox/lib.php index 46611afa177..f96be02e811 100644 --- a/repository/dropbox/lib.php +++ b/repository/dropbox/lib.php @@ -24,7 +24,6 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ require_once($CFG->dirroot . '/repository/lib.php'); -require_once(dirname(__FILE__).'/locallib.php'); /** * Repository to access Dropbox files @@ -34,419 +33,214 @@ require_once(dirname(__FILE__).'/locallib.php'); * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class repository_dropbox extends repository { - /** @var dropbox the instance of dropbox client */ - private $dropbox; - /** @var array files */ - public $files; - /** @var bool flag of login status */ - public $logged=false; - /** @var int maximum size of file to cache in moodle filepool */ - public $cachelimit=null; - - /** @var int cached file ttl */ - private $cachedfilettl = null; + /** + * @var int The maximum file size to cache in the moodle filepool. + */ + public $cachelimit = null; /** - * Constructor of dropbox plugin - * - * @param int $repositoryid - * @param stdClass $context - * @param array $options + * @var repository The actual repository. */ - public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array()) { - global $CFG; - $options['page'] = optional_param('p', 1, PARAM_INT); + protected $legacy; + + /** + * Constructor of dropbox plugin. + * + * @inheritDocs + */ + public function __construct($repositoryid, $context = SYSCONTEXTID, $options = []) { + $this->legacy = new repository_dropbox_legacy($repositoryid, $context, $options); + parent::__construct($repositoryid, $context, $options); - - $this->setting = 'dropbox_'; - - $this->dropbox_key = $this->get_option('dropbox_key'); - $this->dropbox_secret = $this->get_option('dropbox_secret'); - - // one day - $this->cachedfilettl = 60 * 60 * 24; - - if (isset($options['access_key'])) { - $this->access_key = $options['access_key']; - } else { - $this->access_key = get_user_preferences($this->setting.'_access_key', ''); - } - if (isset($options['access_secret'])) { - $this->access_secret = $options['access_secret']; - } else { - $this->access_secret = get_user_preferences($this->setting.'_access_secret', ''); - } - - if (!empty($this->access_key) && !empty($this->access_secret)) { - $this->logged = true; - } - - $callbackurl = new moodle_url($CFG->wwwroot.'/repository/repository_callback.php', array( - 'callback'=>'yes', - 'repo_id'=>$repositoryid - )); - - $args = array( - 'oauth_consumer_key'=>$this->dropbox_key, - 'oauth_consumer_secret'=>$this->dropbox_secret, - 'oauth_callback' => $callbackurl->out(false), - 'api_root' => 'https://api.dropbox.com/1/oauth', - ); - - $this->dropbox = new dropbox($args); } /** - * Set access key + * Repository method to serve the referenced file. * - * @param string $access_key + * @inheritDocs */ - public function set_access_key($access_key) { - $this->access_key = $access_key; + public function send_file($storedfile, $lifetime=null , $filter=0, $forcedownload=false, array $options = null) { + return $this->legacy->send_file($storedfile, $lifetime, $filter, $forcedownload, $options); } /** - * Set access secret + * Return human readable reference information. + * {@link stored_file::get_reference()} * - * @param string $access_secret + * @inheritDocs */ - public function set_access_secret($access_secret) { - $this->access_secret = $access_secret; + public function get_reference_details($reference, $filestatus = 0) { + return $this->legacy->get_reference_details($reference, $filestatus); } - /** - * Check if moodle has got access token and secret + * Cache file from external repository by reference. + * {@link repository::get_file_reference()} + * {@link repository::get_file()} + * Invoked at MOODLE/repository/repository_ajax.php. * - * @return bool + * @inheritDocs */ - public function check_login() { - return !empty($this->logged); + public function cache_file_by_reference($reference, $storedfile) { + return $this->legacy->cache_file_by_reference($reference, $storedfile); } /** - * Generate dropbox login url + * Return the source information. * - * @return array - */ - public function print_login() { - $result = $this->dropbox->request_token(); - set_user_preference($this->setting.'_request_secret', $result['oauth_token_secret']); - $url = $result['authorize_url']; - if ($this->options['ajax']) { - $ret = array(); - $popup_btn = new stdClass(); - $popup_btn->type = 'popup'; - $popup_btn->url = $url; - $ret['login'] = array($popup_btn); - return $ret; - } else { - echo ''.get_string('login', 'repository').''; - } - } - - /** - * Request access token + * The result of the function is stored in files.source field. It may be analysed + * when the source file is lost or repository may use it to display human-readable + * location of reference original. * - * @return array - */ - public function callback() { - $token = optional_param('oauth_token', '', PARAM_TEXT); - $secret = get_user_preferences($this->setting.'_request_secret', ''); - $access_token = $this->dropbox->get_access_token($token, $secret); - set_user_preference($this->setting.'_access_key', $access_token['oauth_token']); - set_user_preference($this->setting.'_access_secret', $access_token['oauth_token_secret']); - } - - /** - * Get dropbox files + * This method is called when file is picked for the first time only. When file + * (either copy or a reference) is already in moodle and it is being picked + * again to another file area (also as a copy or as a reference), the value of + * files.source is copied. * - * @param string $path - * @param int $page - * @return array + * @inheritDocs */ - public function get_listing($path = '', $page = '1') { - global $OUTPUT; - if (empty($path) || $path=='/') { - $path = '/'; - } else { - $path = file_correct_filepath($path); - } - $encoded_path = str_replace("%2F", "/", rawurlencode($path)); - - $list = array(); - $list['list'] = array(); - $list['manage'] = 'https://www.dropbox.com/home'; - $list['dynload'] = true; - $list['nosearch'] = true; - $list['logouturl'] = 'https://www.dropbox.com/logout'; - $list['message'] = get_string('logoutdesc', 'repository_dropbox'); - // process breadcrumb trail - $list['path'] = array( - array('name'=>get_string('dropbox', 'repository_dropbox'), 'path'=>'/') - ); - - $result = $this->dropbox->get_listing($encoded_path, $this->access_key, $this->access_secret); - - if (!is_object($result) || empty($result)) { - return $list; - } - if (empty($result->path)) { - $current_path = '/'; - } else { - $current_path = file_correct_filepath($result->path); - } - - $trail = ''; - if (!empty($path)) { - $parts = explode('/', $path); - if (count($parts) > 1) { - foreach ($parts as $part) { - if (!empty($part)) { - $trail .= ('/'.$part); - $list['path'][] = array('name'=>$part, 'path'=>$trail); - } - } - } else { - $list['path'][] = array('name'=>$path, 'path'=>$path); - } - } - - if (!empty($result->error)) { - // reset access key - set_user_preference($this->setting.'_access_key', ''); - set_user_preference($this->setting.'_access_secret', ''); - throw new repository_exception('repositoryerror', 'repository', '', $result->error); - } - if (empty($result->contents) or !is_array($result->contents)) { - return $list; - } - $files = $result->contents; - $dirslist = array(); - $fileslist = array(); - foreach ($files as $file) { - if ($file->is_dir) { - $dirslist[] = array( - 'title' => substr($file->path, strpos($file->path, $current_path)+strlen($current_path)), - 'path' => file_correct_filepath($file->path), - 'date' => strtotime($file->modified), - 'thumbnail' => $OUTPUT->pix_url(file_folder_icon(64))->out(false), - 'thumbnail_height' => 64, - 'thumbnail_width' => 64, - 'children' => array(), - ); - } else { - $thumbnail = null; - if ($file->thumb_exists) { - $thumburl = new moodle_url('/repository/dropbox/thumbnail.php', - array('repo_id' => $this->id, - 'ctx_id' => $this->context->id, - 'source' => $file->path, - 'rev' => $file->rev // include revision to avoid cache problems - )); - $thumbnail = $thumburl->out(false); - } - $fileslist[] = array( - 'title' => substr($file->path, strpos($file->path, $current_path)+strlen($current_path)), - 'source' => $file->path, - 'size' => $file->bytes, - 'date' => strtotime($file->modified), - 'thumbnail' => $OUTPUT->pix_url(file_extension_icon($file->path, 64))->out(false), - 'realthumbnail' => $thumbnail, - 'thumbnail_height' => 64, - 'thumbnail_width' => 64, - ); - } - } - $fileslist = array_filter($fileslist, array($this, 'filter')); - $list['list'] = array_merge($dirslist, array_values($fileslist)); - return $list; + public function get_file_source_info($source) { + return $this->legacy->get_file_source_info($source); } /** - * Displays a thumbnail for current user's dropbox file + * Prepare file reference information. * - * @param string $string + * @inheritDocs */ - public function send_thumbnail($source) { - global $CFG; - $saveas = $this->prepare_file(''); - try { - $access_key = get_user_preferences($this->setting.'_access_key', ''); - $access_secret = get_user_preferences($this->setting.'_access_secret', ''); - $this->dropbox->set_access_token($access_key, $access_secret); - $this->dropbox->get_thumbnail($source, $saveas, $CFG->repositorysyncimagetimeout); - $content = file_get_contents($saveas); - unlink($saveas); - // set 30 days lifetime for the image. If the image is changed in dropbox it will have - // different revision number and URL will be different. It is completely safe - // to cache thumbnail in the browser for a long time - send_file($content, basename($source), 30*24*60*60, 0, true); - } catch (Exception $e) {} + public function get_file_reference($source) { + return $this->legacy->get_file_reference($source); } /** - * Logout from dropbox - * @return array - */ - public function logout() { - set_user_preference($this->setting.'_access_key', ''); - set_user_preference($this->setting.'_access_secret', ''); - $this->access_key = ''; - $this->access_secret = ''; - return $this->print_login(); - } - - /** - * Set dropbox option - * @param array $options - * @return mixed - */ - public function set_option($options = array()) { - if (!empty($options['dropbox_key'])) { - set_config('dropbox_key', trim($options['dropbox_key']), 'dropbox'); - } - if (!empty($options['dropbox_secret'])) { - set_config('dropbox_secret', trim($options['dropbox_secret']), 'dropbox'); - } - if (!empty($options['dropbox_cachelimit'])) { - $this->cachelimit = (int)trim($options['dropbox_cachelimit']); - set_config('dropbox_cachelimit', $this->cachelimit, 'dropbox'); - } - unset($options['dropbox_key']); - unset($options['dropbox_secret']); - unset($options['dropbox_cachelimit']); - $ret = parent::set_option($options); - return $ret; - } - - /** - * Get dropbox options - * @param string $config - * @return mixed - */ - public function get_option($config = '') { - if ($config==='dropbox_key') { - return trim(get_config('dropbox', 'dropbox_key')); - } elseif ($config==='dropbox_secret') { - return trim(get_config('dropbox', 'dropbox_secret')); - } elseif ($config==='dropbox_cachelimit') { - return $this->max_cache_bytes(); - } else { - $options = parent::get_option(); - $options['dropbox_key'] = trim(get_config('dropbox', 'dropbox_key')); - $options['dropbox_secret'] = trim(get_config('dropbox', 'dropbox_secret')); - $options['dropbox_cachelimit'] = $this->max_cache_bytes(); - } - return $options; - } - - /** - * Fixes references in DB that contains user credentials + * Return file URL for external link. * - * @param string $reference contents of DB field files_reference.reference + * @inheritDocs */ - public function fix_old_style_reference($reference) { - global $CFG; - $ref = unserialize($reference); - if (!isset($ref->url)) { - $this->dropbox->set_access_token($ref->access_key, $ref->access_secret); - $ref->url = $this->dropbox->get_file_share_link($ref->path, $CFG->repositorygetfiletimeout); - if (!$ref->url) { - // some error occurred, do not fix reference for now - return $reference; - } - } - unset($ref->access_key); - unset($ref->access_secret); - $newreference = serialize($ref); - if ($newreference !== $reference) { - // we need to update references in the database - global $DB; - $params = array( - 'newreference' => $newreference, - 'newhash' => sha1($newreference), - 'reference' => $reference, - 'hash' => sha1($reference), - 'repoid' => $this->id - ); - $refid = $DB->get_field_sql('SELECT id FROM {files_reference} - WHERE reference = :reference AND referencehash = :hash - AND repositoryid = :repoid', $params); - if (!$refid) { - return $newreference; - } - $existingrefid = $DB->get_field_sql('SELECT id FROM {files_reference} - WHERE reference = :newreference AND referencehash = :newhash - AND repositoryid = :repoid', $params); - if ($existingrefid) { - // the same reference already exists, we unlink all files from it, - // link them to the current reference and remove the old one - $DB->execute('UPDATE {files} SET referencefileid = :refid - WHERE referencefileid = :existingrefid', - array('refid' => $refid, 'existingrefid' => $existingrefid)); - $DB->delete_records('files_reference', array('id' => $existingrefid)); - } - // update the reference - $params['refid'] = $refid; - $DB->execute('UPDATE {files_reference} - SET reference = :newreference, referencehash = :newhash - WHERE id = :refid', $params); - } - return $newreference; + public function get_link($reference) { + return $this->legacy->get_link($reference); } /** - * Converts a URL received from dropbox API function 'shares' into URL that - * can be used to download/access file directly + * Downloads a file from external repository and saves it in temp dir. * - * @param string $sharedurl - * @return string - */ - private function get_file_download_link($sharedurl) { - return preg_replace('|^(\w*://)www(.dropbox.com)|','\1dl\2',$sharedurl); - } - - /** - * Downloads a file from external repository and saves it in temp dir - * - * @throws moodle_exception when file could not be downloaded - * - * @param string $reference the content of files.reference field or result of - * function {@link repository_dropbox::get_file_reference()} - * @param string $saveas filename (without path) to save the downloaded file in the - * temporary directory, if omitted or file already exists the new filename will be generated - * @return array with elements: - * path: internal location of the file - * url: URL to the source (from parameters) + * @inheritDocs */ public function get_file($reference, $saveas = '') { - global $CFG; - $ref = unserialize($reference); - $saveas = $this->prepare_file($saveas); - if (isset($ref->access_key) && isset($ref->access_secret) && isset($ref->path)) { - $this->dropbox->set_access_token($ref->access_key, $ref->access_secret); - return $this->dropbox->get_file($ref->path, $saveas, $CFG->repositorygetfiletimeout); - } else if (isset($ref->url)) { - $c = new curl; - $url = $this->get_file_download_link($ref->url); - $result = $c->download_one($url, null, array('filepath' => $saveas, 'timeout' => $CFG->repositorygetfiletimeout, 'followlocation' => true)); - $info = $c->get_info(); - if ($result !== true || !isset($info['http_code']) || $info['http_code'] != 200) { - throw new moodle_exception('errorwhiledownload', 'repository', '', $result); - } - return array('path'=>$saveas, 'url'=>$url); - } - throw new moodle_exception('cannotdownload', 'repository'); + return $this->legacy->get_file($reference, $saveas); + } + + /** + * Dropbox plugin supports all kinds of files. + * + * @inheritDocs + */ + public function supported_filetypes() { + return $this->legacy->supported_filetypes(); + } + + /** + * User cannot use the external link to dropbox. + * + * @inheritDocs + */ + public function supported_returntypes() { + return $this->legacy->supported_returntypes(); + } + + /** + * Get dropbox files. + * + * @inheritDocs + */ + public function get_listing($path = '', $page = '1') { + return $this->legacy->get_listing($path, $page); + } + + /** + * Get dropbox files in the specified path. + * + * @param string $query The search query + * @param int $page The page number + * @return array + */ + public function search($query, $page = 0) { + return parent::search($query, $page); + } + + /** + * Displays a thumbnail for current user's dropbox file. + * + * @inheritDocs + */ + public function send_thumbnail($source) { + return $this->legacy->send_thumbnail($source); + } + + /** + * Fixes references in DB that contains user credentials. + * + * @param string $packed Content of DB field files_reference.reference + * @return string New serialized reference + */ + protected function fix_old_style_reference($packed) { + return $this->legacy->fix_old_style_reference($packed); } /** - * Add Plugin settings input to Moodle form + * Logout from dropbox. * - * @param moodleform $mform Moodle form (passed by reference) - * @param string $classname repository class name + * @inheritDocs + */ + public function logout() { + return $this->legacy->logout(); + } + + /** + * Check if moodle has got access token and secret. + * + * @inheritDocs + */ + public function check_login() { + return $this->legacy->check_login(); + } + + /** + * Generate dropbox login url. + * + * @inheritDocs + */ + public function print_login() { + return $this->legacy->print_login(); + } + + /** + * Request access token. + * + * @inheritDocs + */ + public function callback() { + return $this->legacy->callback(); + } + + /** + * Caches all references to Dropbox files in moodle filepool. + * + * Invoked by {@link repository_dropbox_cron()}. Only files smaller than + * {@link repository_dropbox::max_cache_bytes()} and only files which + * synchronisation timeout have not expired are cached. + * + * @inheritDocs + */ + public function cron() { + return $this->legacy->cron(); + } + + /** + * Add Plugin settings input to Moodle form. + * + * @inheritDocs */ public static function type_config_form($mform, $classname = 'repository') { - global $CFG; parent::type_config_form($mform); $key = get_config('dropbox', 'dropbox_key'); $secret = get_config('dropbox', 'dropbox_secret'); @@ -458,200 +252,116 @@ class repository_dropbox extends repository { $secret = ''; } - $strrequired = get_string('required'); - $mform->addElement('text', 'dropbox_key', get_string('apikey', 'repository_dropbox'), array('value'=>$key,'size' => '40')); $mform->setType('dropbox_key', PARAM_RAW_TRIMMED); $mform->addElement('text', 'dropbox_secret', get_string('secret', 'repository_dropbox'), array('value'=>$secret,'size' => '40')); - $mform->addRule('dropbox_key', $strrequired, 'required', null, 'client'); - $mform->addRule('dropbox_secret', $strrequired, 'required', null, 'client'); + $mform->addRule('dropbox_key', get_string('required'), 'required', null, 'client'); + $mform->addRule('dropbox_secret', get_string('required'), 'required', null, 'client'); $mform->setType('dropbox_secret', PARAM_RAW_TRIMMED); - $str_getkey = get_string('instruction', 'repository_dropbox'); - $mform->addElement('static', null, '', $str_getkey); + $mform->addElement('static', null, '', get_string('instruction', 'repository_dropbox')); $mform->addElement('text', 'dropbox_cachelimit', get_string('cachelimit', 'repository_dropbox'), array('size' => '40')); $mform->addRule('dropbox_cachelimit', null, 'numeric', null, 'client'); $mform->setType('dropbox_cachelimit', PARAM_INT); $mform->addElement('static', 'dropbox_cachelimit_info', '', get_string('cachelimit_info', 'repository_dropbox')); + } /** - * Option names of dropbox plugin + * Set options. * - * @return array + * @param array $options + * @return mixed + */ + public function set_option($options = []) { + if (!empty($options['dropbox_key'])) { + set_config('dropbox_key', trim($options['dropbox_key']), 'dropbox'); + unset($options['dropbox_key']); + } + if (!empty($options['dropbox_secret'])) { + set_config('dropbox_secret', trim($options['dropbox_secret']), 'dropbox'); + unset($options['dropbox_secret']); + } + if (!empty($options['dropbox_cachelimit'])) { + $this->cachelimit = (int) trim($options['dropbox_cachelimit']); + set_config('dropbox_cachelimit', $this->cachelimit, 'dropbox'); + unset($options['dropbox_cachelimit']); + } + + return parent::set_option($options); + } + + /** + * Get dropbox options + * @param string $config + * @return mixed + */ + public function get_option($config = '') { + if ($config === 'dropbox_key') { + return trim(get_config('dropbox', 'dropbox_key')); + } else if ($config === 'dropbox_secret') { + return trim(get_config('dropbox', 'dropbox_secret')); + } else if ($config === 'dropbox_cachelimit') { + return $this->max_cache_bytes(); + } else { + $options = parent::get_option(); + $options['dropbox_key'] = trim(get_config('dropbox', 'dropbox_key')); + $options['dropbox_secret'] = trim(get_config('dropbox', 'dropbox_secret')); + $options['dropbox_cachelimit'] = $this->max_cache_bytes(); + } + + return $options; + } + + /** + * Option names of dropbox plugin. + * + * @inheritDocs */ public static function get_type_option_names() { - return array('dropbox_key', 'dropbox_secret', 'pluginname', 'dropbox_cachelimit'); + return [ + 'dropbox_key', + 'dropbox_secret', + 'pluginname', + 'dropbox_cachelimit', + ]; } /** - * Dropbox plugin supports all kinds of files + * Performs synchronisation of an external file if the previous one has expired. * - * @return array - */ - public function supported_filetypes() { - return '*'; - } - - /** - * User cannot use the external link to dropbox + * This function must be implemented for external repositories supporting + * FILE_REFERENCE, it is called for existing aliases when their filesize, + * contenthash or timemodified are requested. It is not called for internal + * repositories (see {@link repository::has_moodle_files()}), references to + * internal files are updated immediately when source is modified. * - * @return int - */ - public function supported_returntypes() { - return FILE_INTERNAL | FILE_REFERENCE | FILE_EXTERNAL; - } - - /** - * Return file URL for external link + * Referenced files may optionally keep their content in Moodle filepool (for + * thumbnail generation or to be able to serve cached copy). In this + * case both contenthash and filesize need to be synchronized. Otherwise repositories + * should use contenthash of empty file and correct filesize in bytes. * - * @param string $reference the result of get_file_reference() - * @return string - */ - public function get_link($reference) { - global $CFG; - $ref = unserialize($reference); - if (!isset($ref->url)) { - $this->dropbox->set_access_token($ref->access_key, $ref->access_secret); - $ref->url = $this->dropbox->get_file_share_link($ref->path, $CFG->repositorygetfiletimeout); - } - return $this->get_file_download_link($ref->url); - } - - /** - * Prepare file reference information + * Note that this function may be run for EACH file that needs to be synchronised at the + * moment. If anything is being downloaded or requested from external sources there + * should be a small timeout. The synchronisation is performed to update the size of + * the file and/or to update image and re-generated image preview. There is nothing + * fatal if syncronisation fails but it is fatal if syncronisation takes too long + * and hangs the script generating a page. * - * @param string $source - * @return string file referece + * Note: If you wish to call $file->get_filesize(), $file->get_contenthash() or + * $file->get_timemodified() make sure that recursion does not happen. + * + * Called from {@link stored_file::sync_external_file()} + * + * @inheritDocs */ - public function get_file_reference($source) { - global $USER, $CFG; - $reference = new stdClass; - $reference->path = $source; - $reference->userid = $USER->id; - $reference->username = fullname($USER); - $reference->access_key = get_user_preferences($this->setting.'_access_key', ''); - $reference->access_secret = get_user_preferences($this->setting.'_access_secret', ''); - - // by API we don't know if we need this reference to just download a file from dropbox - // into moodle filepool or create a reference. Since we need to create a shared link - // only in case of reference we analyze the script parameter - $usefilereference = optional_param('usefilereference', false, PARAM_BOOL); - if ($usefilereference) { - $this->dropbox->set_access_token($reference->access_key, $reference->access_secret); - $url = $this->dropbox->get_file_share_link($source, $CFG->repositorygetfiletimeout); - if ($url) { - unset($reference->access_key); - unset($reference->access_secret); - $reference->url = $url; - } - } - return serialize($reference); - } - public function sync_reference(stored_file $file) { - global $CFG; - - if ($file->get_referencelastsync() + DAYSECS > time()) { - // Synchronise not more often than once a day. - return false; - } - $ref = unserialize($file->get_reference()); - if (!isset($ref->url)) { - // this is an old-style reference in DB. We need to fix it - $ref = unserialize($this->fix_old_style_reference($file->get_reference())); - } - if (!isset($ref->url)) { - return false; - } - $c = new curl; - $url = $this->get_file_download_link($ref->url); - if (file_extension_in_typegroup($ref->path, 'web_image')) { - $saveas = $this->prepare_file(''); - try { - $result = $c->download_one($url, array(), array('filepath' => $saveas, 'timeout' => $CFG->repositorysyncimagetimeout, 'followlocation' => true)); - $info = $c->get_info(); - if ($result === true && isset($info['http_code']) && $info['http_code'] == 200) { - $fs = get_file_storage(); - list($contenthash, $filesize, $newfile) = $fs->add_file_to_pool($saveas); - $file->set_synchronized($contenthash, $filesize); - return true; - } - } catch (Exception $e) {} - } - $c->get($url, null, array('timeout' => $CFG->repositorysyncimagetimeout, 'followlocation' => true, 'nobody' => true)); - $info = $c->get_info(); - if (isset($info['http_code']) && $info['http_code'] == 200 && - array_key_exists('download_content_length', $info) && - $info['download_content_length'] >= 0) { - $filesize = (int)$info['download_content_length']; - $file->set_synchronized(null, $filesize); - return true; - } - $file->set_missingsource(); - return true; + return $this->legacy->sync_reference($file); } /** - * Cache file from external repository by reference - * - * Dropbox repository regularly caches all external files that are smaller than - * {@link repository_dropbox::max_cache_bytes()} - * - * @param string $reference this reference is generated by - * repository::get_file_reference() - * @param stored_file $storedfile created file reference - */ - public function cache_file_by_reference($reference, $storedfile) { - try { - $this->import_external_file_contents($storedfile, $this->max_cache_bytes()); - } catch (Exception $e) {} - } - - /** - * Return human readable reference information - * {@link stored_file::get_reference()} - * - * @param string $reference - * @param int $filestatus status of the file, 0 - ok, 666 - source missing - * @return string - */ - public function get_reference_details($reference, $filestatus = 0) { - global $USER; - $ref = unserialize($reference); - $detailsprefix = $this->get_name(); - if (isset($ref->userid) && $ref->userid != $USER->id && isset($ref->username)) { - $detailsprefix .= ' ('.$ref->username.')'; - } - $details = $detailsprefix; - if (isset($ref->path)) { - $details .= ': '. $ref->path; - } - if (isset($ref->path) && !$filestatus) { - // Indicate this is from dropbox with path - return $details; - } else { - if (isset($ref->url)) { - $details = $detailsprefix. ': '. $ref->url; - } - return get_string('lostsource', 'repository', $details); - } - } - - /** - * Return the source information - * - * @param string $source - * @return string - */ - public function get_file_source_info($source) { - global $USER; - return 'Dropbox ('.fullname($USER).'): ' . $source; - } - - /** - * Returns the maximum size of the Dropbox files to cache in moodle + * Returns the maximum size of the Dropbox files to cache in moodle. * * Note that {@link repository_dropbox::sync_reference()} will try to cache images even * when they are bigger in order to generate thumbnails. However there is @@ -662,67 +372,14 @@ class repository_dropbox extends repository { */ public function max_cache_bytes() { if ($this->cachelimit === null) { - $this->cachelimit = (int)get_config('dropbox', 'dropbox_cachelimit'); + $this->cachelimit = (int) get_config('dropbox', 'dropbox_cachelimit'); } return $this->cachelimit; } - - /** - * Repository method to serve the referenced file - * - * This method is ivoked from {@link send_stored_file()}. - * Dropbox repository first caches the file by reading it into temporary folder and then - * serves from there. - * - * @param stored_file $storedfile the file that contains the reference - * @param int $lifetime Number of seconds before the file should expire from caches (null means $CFG->filelifetime) - * @param int $filter 0 (default)=no filtering, 1=all files, 2=html files only - * @param bool $forcedownload If true (default false), forces download of file rather than view in browser/plugin - * @param array $options additional options affecting the file serving - */ - public function send_file($storedfile, $lifetime=null , $filter=0, $forcedownload=false, array $options = null) { - $ref = unserialize($storedfile->get_reference()); - if ($storedfile->get_filesize() > $this->max_cache_bytes()) { - header('Location: '.$this->get_file_download_link($ref->url)); - die; - } - try { - $this->import_external_file_contents($storedfile, $this->max_cache_bytes()); - if (!is_array($options)) { - $options = array(); - } - $options['sendcachedexternalfile'] = true; - send_stored_file($storedfile, $lifetime, $filter, $forcedownload, $options); - } catch (moodle_exception $e) { - // redirect to Dropbox, it will show the error. - // We redirect to Dropbox shared link, not to download link here! - header('Location: '.$ref->url); - die; - } - } - - /** - * Caches all references to Dropbox files in moodle filepool - * - * Invoked by {@link repository_dropbox_cron()}. Only files smaller than - * {@link repository_dropbox::max_cache_bytes()} and only files which - * synchronisation timeout have not expired are cached. - */ - public function cron() { - $fs = get_file_storage(); - $files = $fs->get_external_files($this->id); - foreach ($files as $file) { - try { - // This call will cache all files that are smaller than max_cache_bytes() - // and synchronise file size of all others - $this->import_external_file_contents($file, $this->max_cache_bytes()); - } catch (moodle_exception $e) {} - } - } } /** - * Dropbox plugin cron task + * Dropbox plugin cron task. */ function repository_dropbox_cron() { $instances = repository::get_instances(array('type'=>'dropbox')); From 7609261d84fcf3858b67002da63ca0e2b6097284 Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Tue, 13 Sep 2016 13:35:39 +0800 Subject: [PATCH 3/3] MDL-52051 repository_dropbox: Add the V2 API --- .../classes/authentication_exception.php | 38 ++ repository/dropbox/classes/dropbox.php | 368 +++++++++++ .../dropbox/classes/dropbox_exception.php | 38 ++ .../dropbox/classes/provider_exception.php | 38 ++ .../dropbox/classes/rate_limit_exception.php | 44 ++ repository/dropbox/db/upgrade.php | 6 +- .../dropbox/lang/en/repository_dropbox.php | 6 + repository/dropbox/lib.php | 605 ++++++++++++++++- repository/dropbox/tests/api_test.php | 617 ++++++++++++++++++ repository/dropbox/tests/generator/lib.php | 3 + repository/dropbox/version.php | 2 +- repository/tests/generator_test.php | 3 +- 12 files changed, 1744 insertions(+), 24 deletions(-) create mode 100644 repository/dropbox/classes/authentication_exception.php create mode 100644 repository/dropbox/classes/dropbox.php create mode 100644 repository/dropbox/classes/dropbox_exception.php create mode 100644 repository/dropbox/classes/provider_exception.php create mode 100644 repository/dropbox/classes/rate_limit_exception.php create mode 100644 repository/dropbox/tests/api_test.php diff --git a/repository/dropbox/classes/authentication_exception.php b/repository/dropbox/classes/authentication_exception.php new file mode 100644 index 00000000000..7bd9196415f --- /dev/null +++ b/repository/dropbox/classes/authentication_exception.php @@ -0,0 +1,38 @@ +. + +/** + * Dropbox Authentication exception. + * + * @since Moodle 3.2 + * @package repository_dropbox + * @copyright Andrew Nicols + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace repository_dropbox; + +defined('MOODLE_INTERNAL') || die(); + +/** + * Dropbox Authentication exception. + * + * @package repository_dropbox + * @copyright Andrew Nicols + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class authentication_exception extends dropbox_exception { +} diff --git a/repository/dropbox/classes/dropbox.php b/repository/dropbox/classes/dropbox.php new file mode 100644 index 00000000000..3507c7a1fe3 --- /dev/null +++ b/repository/dropbox/classes/dropbox.php @@ -0,0 +1,368 @@ +. + +/** + * Dropbox V2 API. + * + * @since Moodle 3.2 + * @package repository_dropbox + * @copyright Andrew Nicols + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace repository_dropbox; + +defined('MOODLE_INTERNAL') || die(); + +require_once($CFG->libdir . '/oauthlib.php'); + +/** + * Dropbox V2 API. + * + * @package repository_dropbox + * @copyright Andrew Nicols + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class dropbox extends \oauth2_client { + + /** + * Create the DropBox API Client. + * + * @param string $key The API key + * @param string $secret The API secret + * @param string $callback The callback URL + */ + public function __construct($key, $secret, $callback) { + parent::__construct($key, $secret, $callback, ''); + } + + /** + * Returns the auth url for OAuth 2.0 request. + * + * @return string the auth url + */ + protected function auth_url() { + return 'https://www.dropbox.com/oauth2/authorize'; + } + + /** + * Returns the token url for OAuth 2.0 request. + * + * @return string the auth url + */ + protected function token_url() { + return 'https://api.dropboxapi.com/oauth2/token'; + } + + /** + * Return the constructed API endpoint URL. + * + * @param string $endpoint The endpoint to be contacted + * @return moodle_url The constructed API URL + */ + protected function get_api_endpoint($endpoint) { + return new \moodle_url('https://api.dropboxapi.com/2/' . $endpoint); + } + + /** + * Return the constructed content endpoint URL. + * + * @param string $endpoint The endpoint to be contacted + * @return moodle_url The constructed content URL + */ + protected function get_content_endpoint($endpoint) { + return new \moodle_url('https://api-content.dropbox.com/2/' . $endpoint); + } + + /** + * Make an API call against the specified endpoint with supplied data. + * + * @param string $endpoint The endpoint to be contacted + * @param array $data Any data to pass to the endpoint + * @return object Content decoded from the endpoint + */ + protected function fetch_dropbox_data($endpoint, $data = []) { + $url = $this->get_api_endpoint($endpoint); + $this->cleanopt(); + $this->resetHeader(); + + if ($data === null) { + // Some API endpoints explicitly expect a data submission of 'null'. + $options['CURLOPT_POSTFIELDS'] = 'null'; + } else { + $options['CURLOPT_POSTFIELDS'] = json_encode($data); + } + $options['CURLOPT_POST'] = 1; + $this->setHeader('Content-Type: application/json'); + + $response = $this->request($url, $options); + $result = json_decode($response); + + $this->check_and_handle_api_errors($result); + + if ($this->has_additional_results($result)) { + // Any API endpoint returning 'has_more' will provide a cursor, and also have a matching endpoint suffixed + // with /continue which takes that cursor. + if (preg_match('_/continue$_', $endpoint) === 0) { + // Only add /continue if it is not already present. + $endpoint .= '/continue'; + } + + // Fetch the next page of results. + $additionaldata = $this->fetch_dropbox_data($endpoint, [ + 'cursor' => $result->cursor, + ]); + + // Merge the list of entries. + $result->entries = array_merge($result->entries, $additionaldata->entries); + } + + if (isset($result->has_more)) { + // Unset the cursor and has_more flags. + unset($result->cursor); + unset($result->has_more); + } + + return $result; + } + + /** + * Whether the supplied result is paginated and not the final page. + * + * @param object $result The result of an operation + * @return boolean + */ + public function has_additional_results($result) { + return !empty($result->has_more) && !empty($result->cursor); + } + + /** + * Fetch content from the specified endpoint with the supplied data. + * + * @param string $endpoint The endpoint to be contacted + * @param array $data Any data to pass to the endpoint + * @return string The returned data + */ + protected function fetch_dropbox_content($endpoint, $data = []) { + $url = $this->get_content_endpoint($endpoint); + $this->cleanopt(); + $this->resetHeader(); + + $options['CURLOPT_POST'] = 1; + $this->setHeader('Content-Type: '); + $this->setHeader('Dropbox-API-Arg: ' . json_encode($data)); + + $response = $this->request($url, $options); + + $this->check_and_handle_api_errors($response); + return $response; + } + + /** + * Check for an attempt to handle API errors. + * + * This function attempts to deal with errors as per + * https://www.dropbox.com/developers/documentation/http/documentation#error-handling. + * + * @param string $data The returned content. + * @throws moodle_exception + */ + protected function check_and_handle_api_errors($data) { + if ($this->info['http_code'] == 200) { + // Dropbox only returns errors on non-200 response codes. + return; + } + + switch($this->info['http_code']) { + case 400: + // Bad input parameter. Error message should indicate which one and why. + throw new \coding_exception('Invalid input parameter passed to DropBox API.'); + break; + case 401: + // Bad or expired token. This can happen if the access token is expired or if the access token has been + // revoked by Dropbox or the user. To fix this, you should re-authenticate the user. + throw new authentication_exception('Authentication token expired'); + break; + case 409: + // Endpoint-specific error. Look to the response body for the specifics of the error. + throw new \coding_exception('Endpoint specific error: ' . $data); + break; + case 429: + // Your app is making too many requests for the given user or team and is being rate limited. Your app + // should wait for the number of seconds specified in the "Retry-After" response header before trying + // again. + throw new rate_limit_exception(); + break; + default: + break; + } + + if ($this->info['http_code'] >= 500 && $this->info['http_code'] < 600) { + throw new \invalid_response_exception($this->info['http_code'] . ": " . $data); + } + } + + /** + * Get file listing from dropbox. + * + * @param string $path The path to query + * @return object The returned directory listing, or null on failure + */ + public function get_listing($path = '') { + if ($path === '/') { + $path = ''; + } + + $data = $this->fetch_dropbox_data('files/list_folder', [ + 'path' => $path, + ]); + + return $data; + } + + /** + * Get file search results from dropbox. + * + * @param string $query The search query + * @return object The returned directory listing, or null on failure + */ + public function search($query = '') { + $data = $this->fetch_dropbox_data('files/search', [ + 'path' => '', + 'query' => $query, + ]); + + return $data; + } + + /** + * Whether the entry is expected to have a thumbnail. + * See docs at https://www.dropbox.com/developers/documentation/http/documentation#files-get_thumbnail. + * + * @param object $entry The file entry received from the DropBox API + * @return boolean Whether dropbox has a thumbnail available + */ + public function supports_thumbnail($entry) { + if ($entry->{".tag"} !== "file") { + // Not a file. No thumbnail available. + return false; + } + + // Thumbnails are available for files under 20MB with file extensions jpg, jpeg, png, tiff, tif, gif, and bmp. + if ($entry->size > 20 * 1024 * 1024) { + return false; + } + + $supportedtypes = [ + 'jpg' => true, + 'jpeg' => true, + 'png' => true, + 'tiff' => true, + 'tif' => true, + 'gif' => true, + 'bmp' => true, + ]; + + $extension = substr($entry->path_lower, strrpos($entry->path_lower, '.') + 1); + return isset($supportedtypes[$extension]) && $supportedtypes[$extension]; + } + + /** + * Retrieves the thumbnail for the content, as supplied by dropbox. + * + * @param string $path The path to fetch a thumbnail for + * @return string Thumbnail image content + */ + public function get_thumbnail($path) { + $content = $this->fetch_dropbox_content('files/get_thumbnail', [ + 'path' => $path, + ]); + + return $content; + } + + /** + * Fetch a valid public share link for the specified file. + * + * @param string $id The file path or file id of the file to fetch information for. + * @return object An object containing the id, path, size, and URL of the entry + */ + public function get_file_share_info($id) { + // Attempt to fetch any existing shared link first. + $data = $this->fetch_dropbox_data('sharing/list_shared_links', [ + 'path' => $id, + ]); + + if (isset($data->links)) { + $link = reset($data->links); + if (isset($link->{".tag"}) && $link->{".tag"} === "file") { + return $this->normalize_file_share_info($link); + } + } + + // No existing link available. + // Create a new one. + $link = $this->fetch_dropbox_data('sharing/create_shared_link_with_settings', [ + 'path' => $id, + 'settings' => [ + 'requested_visibility' => 'public', + ], + ]); + + if (isset($link->{".tag"}) && $link->{".tag"} === "file") { + return $this->normalize_file_share_info($link); + } + + // Some kind of error we don't know how to handle at this stage. + return null; + } + + /** + * Normalize the file share info. + * + * @param object $entry Information retrieved from share endpoints + * @return object Normalized entry information to store as repository information + */ + protected function normalize_file_share_info($entry) { + return (object) [ + 'id' => $entry->id, + 'path' => $entry->path_lower, + 'url' => $entry->url, + ]; + } + + /** + * Process the callback. + */ + public function callback() { + $this->log_out(); + $this->is_logged_in(); + } + + /** + * Revoke the current access token. + * + * @return string + */ + public function logout() { + try { + $this->fetch_dropbox_data('auth/token/revoke', null); + } catch(authentication_exception $e) { + // An authentication_exception may be expected if the token has + // already expired. + } + } +} diff --git a/repository/dropbox/classes/dropbox_exception.php b/repository/dropbox/classes/dropbox_exception.php new file mode 100644 index 00000000000..d11cca61a72 --- /dev/null +++ b/repository/dropbox/classes/dropbox_exception.php @@ -0,0 +1,38 @@ +. + +/** + * General Dropbox Exception. + * + * @since Moodle 3.2 + * @package repository_dropbox + * @copyright Andrew Nicols + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace repository_dropbox; + +defined('MOODLE_INTERNAL') || die(); + +/** + * General Dropbox Exception. + * + * @package repository_dropbox + * @copyright Andrew Nicols + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class dropbox_exception extends \moodle_exception { +} diff --git a/repository/dropbox/classes/provider_exception.php b/repository/dropbox/classes/provider_exception.php new file mode 100644 index 00000000000..d4d3bcb7ce0 --- /dev/null +++ b/repository/dropbox/classes/provider_exception.php @@ -0,0 +1,38 @@ +. + +/** + * Upstream issue exception. + * + * @since Moodle 3.2 + * @package repository_dropbox + * @copyright Andrew Nicols + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace repository_dropbox; + +defined('MOODLE_INTERNAL') || die(); + +/** + * Upstream issue exception. + * + * @package repository_dropbox + * @copyright Andrew Nicols + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class provider_exception extends dropbox_exception { +} diff --git a/repository/dropbox/classes/rate_limit_exception.php b/repository/dropbox/classes/rate_limit_exception.php new file mode 100644 index 00000000000..060b610b7d7 --- /dev/null +++ b/repository/dropbox/classes/rate_limit_exception.php @@ -0,0 +1,44 @@ +. + +/** + * Dropbox Rate Limit Encountered. + * + * @since Moodle 3.2 + * @package repository_dropbox + * @copyright Andrew Nicols + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace repository_dropbox; + +defined('MOODLE_INTERNAL') || die(); + +/** + * Dropbox Rate Limit Encountered. + * + * @package repository_dropbox + * @copyright Andrew Nicols + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class rate_limit_exception extends dropbox_exception { + /** + * Constructor for rate_limit_exception. + */ + public function __construct() { + parent::__construct('Rate limit hit'); + } +} diff --git a/repository/dropbox/db/upgrade.php b/repository/dropbox/db/upgrade.php index 1be51fa4035..1d8bef949eb 100644 --- a/repository/dropbox/db/upgrade.php +++ b/repository/dropbox/db/upgrade.php @@ -21,8 +21,6 @@ defined('MOODLE_INTERNAL') || die(); * @return bool result */ function xmldb_repository_dropbox_upgrade($oldversion) { - global $CFG; - // Moodle v2.8.0 release upgrade line. // Put any upgrade step following this. @@ -35,5 +33,9 @@ function xmldb_repository_dropbox_upgrade($oldversion) { // Moodle v3.1.0 release upgrade line. // Put any upgrade step following this. + if ($oldversion < 2016052301) { + set_config('legacyapi', 1, 'dropbox'); + upgrade_plugin_savepoint(true, 2016052301, 'repository', 'dropbox'); + } return true; } diff --git a/repository/dropbox/lang/en/repository_dropbox.php b/repository/dropbox/lang/en/repository_dropbox.php index 6f0728ddc7c..19ec249aad0 100644 --- a/repository/dropbox/lang/en/repository_dropbox.php +++ b/repository/dropbox/lang/en/repository_dropbox.php @@ -34,3 +34,9 @@ $string['cachelimit'] = 'Cache limit'; $string['cachelimit_info'] = 'Enter the maximum size of files (in bytes) to be cached on server for Dropbox aliases/shortcuts. Cached files will be served when the source is no longer available. Empty value or zero mean caching of all files regardless of size.'; $string['dropbox:view'] = 'View a Dropbox folder'; $string['logoutdesc'] = '(Logout when you finish using Dropbox)'; + +$string['usev2api'] = 'Use the OAuth 2 API (recommended)'; +$string['uselegacyapi'] = 'Use the deprecated v1 API'; +$string['legacyapihelpsupported'] = 'Dropbox introduced the OAuth 2 API in June 2015. You can configure a new application at https://www.dropbox.com/developers/apps/ and specify the OAuth 2 Redirect URI shown below.'; +$string['oauth2redirecturi'] = 'OAuth 2 Redirect URI'; +$string['oauth2title'] = 'OAuth 2 API'; diff --git a/repository/dropbox/lib.php b/repository/dropbox/lib.php index f96be02e811..b9557bad84d 100644 --- a/repository/dropbox/lib.php +++ b/repository/dropbox/lib.php @@ -33,6 +33,11 @@ require_once($CFG->dirroot . '/repository/lib.php'); * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class repository_dropbox extends repository { + /** + * @var dropbox The instance of dropbox client. + */ + protected $dropbox; + /** * @var int The maximum file size to cache in the moodle filepool. */ @@ -49,9 +54,41 @@ class repository_dropbox extends repository { * @inheritDocs */ public function __construct($repositoryid, $context = SYSCONTEXTID, $options = []) { - $this->legacy = new repository_dropbox_legacy($repositoryid, $context, $options); + if (self::use_legacy_api()) { + $this->legacy = new repository_dropbox_legacy($repositoryid, $context, $options); + parent::__construct($repositoryid, $context, $options); + + return; + } + + $options['page'] = optional_param('p', 1, PARAM_INT); parent::__construct($repositoryid, $context, $options); + + $returnurl = new moodle_url('/repository/repository_callback.php', [ + 'callback' => 'yes', + 'repo_id' => $repositoryid, + 'sesskey' => sesskey(), + ]); + + // Create the dropbox API instance. + $key = get_config('dropbox', 'dropbox_key'); + $secret = get_config('dropbox', 'dropbox_secret'); + $this->dropbox = new repository_dropbox\dropbox( + $key, + $secret, + $returnurl + ); + } + + /** + * Determine whether to use the legacy (v1) API. + * + * @return bool + */ + protected static function use_legacy_api() { + $uselegacyapi = get_config('dropbox', 'legacyapi'); + return !empty($uselegacyapi); } /** @@ -60,7 +97,42 @@ class repository_dropbox extends repository { * @inheritDocs */ public function send_file($storedfile, $lifetime=null , $filter=0, $forcedownload=false, array $options = null) { - return $this->legacy->send_file($storedfile, $lifetime, $filter, $forcedownload, $options); + if (self::use_legacy_api()) { + return $this->legacy->send_file($storedfile, $lifetime, $filter, $forcedownload, $options); + } + + $reference = $this->unpack_reference($storedfile->get_reference()); + + $maxcachesize = $this->max_cache_bytes(); + if (empty($maxcachesize)) { + // Always cache the file, regardless of size. + $cachefile = true; + } else { + // Size available. Only cache if it is under maxcachesize. + $cachefile = $storedfile->get_filesize() < $maxcachesize; + } + + if (!$cachefile) { + \core\session\manager::write_close(); + header('Location: ' . $this->get_file_download_link($reference->url)); + die; + } + + try { + $this->import_external_file_contents($storedfile, $this->max_cache_bytes()); + if (!is_array($options)) { + $options = array(); + } + $options['sendcachedexternalfile'] = true; + \core\session\manager::write_close(); + send_stored_file($storedfile, $lifetime, $filter, $forcedownload, $options); + } catch (moodle_exception $e) { + // Redirect to Dropbox, it will show the error. + // Note: We redirect to Dropbox shared link, not to the download link here! + \core\session\manager::write_close(); + header('Location: ' . $reference->url); + die; + } } /** @@ -70,7 +142,29 @@ class repository_dropbox extends repository { * @inheritDocs */ public function get_reference_details($reference, $filestatus = 0) { - return $this->legacy->get_reference_details($reference, $filestatus); + if (self::use_legacy_api()) { + return $this->legacy->get_reference_details($reference, $filestatus); + } + + global $USER; + $ref = unserialize($reference); + $detailsprefix = $this->get_name(); + if (isset($ref->userid) && $ref->userid != $USER->id && isset($ref->username)) { + $detailsprefix .= ' ('.$ref->username.')'; + } + $details = $detailsprefix; + if (isset($ref->path)) { + $details .= ': '. $ref->path; + } + if (isset($ref->path) && !$filestatus) { + // Indicate this is from dropbox with path. + return $details; + } else { + if (isset($ref->url)) { + $details = $detailsprefix. ': '. $ref->url; + } + return get_string('lostsource', 'repository', $details); + } } /** @@ -82,7 +176,15 @@ class repository_dropbox extends repository { * @inheritDocs */ public function cache_file_by_reference($reference, $storedfile) { - return $this->legacy->cache_file_by_reference($reference, $storedfile); + if (self::use_legacy_api()) { + return $this->legacy->cache_file_by_reference($reference, $storedfile); + } + + try { + $this->import_external_file_contents($storedfile, $this->max_cache_bytes()); + } catch (Exception $e) { + // Cache failure should not cause a fatal error. This is only a nice-to-have feature. + } } /** @@ -100,7 +202,12 @@ class repository_dropbox extends repository { * @inheritDocs */ public function get_file_source_info($source) { - return $this->legacy->get_file_source_info($source); + if (self::use_legacy_api()) { + return $this->legacy->get_file_source_info($source); + } + + global $USER; + return 'Dropbox ('.fullname($USER).'): ' . $source; } /** @@ -109,7 +216,25 @@ class repository_dropbox extends repository { * @inheritDocs */ public function get_file_reference($source) { - return $this->legacy->get_file_reference($source); + if (self::use_legacy_api()) { + return $this->legacy->get_file_reference($source); + } + + global $USER; + $reference = new stdClass; + $reference->userid = $USER->id; + $reference->username = fullname($USER); + $reference->path = $source; + + // Determine whether we are downloading the file, or should use a file reference. + $usefilereference = optional_param('usefilereference', false, PARAM_BOOL); + if ($usefilereference) { + if ($data = $this->dropbox->get_file_share_info($source)) { + $reference = (object) array_merge((array) $data, (array) $reference); + } + } + + return serialize($reference); } /** @@ -118,7 +243,13 @@ class repository_dropbox extends repository { * @inheritDocs */ public function get_link($reference) { - return $this->legacy->get_link($reference); + if (self::use_legacy_api()) { + return $this->legacy->get_link($reference); + } + + $unpacked = $this->unpack_reference($reference); + + return $this->get_file_download_link($unpacked->url); } /** @@ -127,7 +258,19 @@ class repository_dropbox extends repository { * @inheritDocs */ public function get_file($reference, $saveas = '') { - return $this->legacy->get_file($reference, $saveas); + if (self::use_legacy_api()) { + return $this->legacy->get_file($reference, $saveas); + } + + $unpacked = $this->unpack_reference($reference); + + // This is a shared link, and hopefully it is still active. + $downloadlink = $this->get_file_download_link($unpacked->url); + + $saveas = $this->prepare_file($saveas); + file_put_contents($saveas, fopen($downloadlink, 'r')); + + return ['path' => $saveas]; } /** @@ -136,7 +279,11 @@ class repository_dropbox extends repository { * @inheritDocs */ public function supported_filetypes() { - return $this->legacy->supported_filetypes(); + if (self::use_legacy_api()) { + return $this->legacy->supported_filetypes(); + } + + return '*'; } /** @@ -145,7 +292,11 @@ class repository_dropbox extends repository { * @inheritDocs */ public function supported_returntypes() { - return $this->legacy->supported_returntypes(); + if (self::use_legacy_api()) { + return $this->legacy->supported_returntypes(); + } + + return FILE_INTERNAL | FILE_REFERENCE | FILE_EXTERNAL; } /** @@ -154,7 +305,48 @@ class repository_dropbox extends repository { * @inheritDocs */ public function get_listing($path = '', $page = '1') { - return $this->legacy->get_listing($path, $page); + if (self::use_legacy_api()) { + return $this->legacy->get_listing($path, $page); + } + + if (empty($path) || $path == '/') { + $path = ''; + } else { + $path = file_correct_filepath($path); + } + + $list = [ + 'list' => [], + 'manage' => 'https://www.dropbox.com/home', + 'logouturl' => 'https://www.dropbox.com/logout', + 'message' => get_string('logoutdesc', 'repository_dropbox'), + 'dynload' => true, + 'path' => $this->process_breadcrumbs($path), + ]; + + // Note - we deliberately do not catch the coding exceptions here. + try { + $result = $this->dropbox->get_listing($path); + } catch (\repository_dropbox\authentication_exception $e) { + // The token has expired. + return $this->print_login(); + } catch (\repository_dropbox\dropbox_exception $e) { + // There was some other form of non-coding failure. + // This could be a rate limit, or it could be a server-side error. + // Just return early instead. + return $list; + } + + if (!is_object($result) || empty($result)) { + return $list; + } + + if (empty($result->entries) or !is_array($result->entries)) { + return $list; + } + + $list['list'] = $this->process_entries($result->entries); + return $list; } /** @@ -165,7 +357,41 @@ class repository_dropbox extends repository { * @return array */ public function search($query, $page = 0) { - return parent::search($query, $page); + if (self::use_legacy_api()) { + return parent::search($query, $page); + } + + $list = [ + 'list' => [], + 'manage' => 'https://www.dropbox.com/home', + 'logouturl' => 'https://www.dropbox.com/logout', + 'message' => get_string('logoutdesc', 'repository_dropbox'), + 'dynload' => true, + ]; + + // Note - we deliberately do not catch the coding exceptions here. + try { + $result = $this->dropbox->search($query); + } catch (\repository_dropbox\authentication_exception $e) { + // The token has expired. + return $this->print_login(); + } catch (\repository_dropbox\dropbox_exception $e) { + // There was some other form of non-coding failure. + // This could be a rate limit, or it could be a server-side error. + // Just return early instead. + return $list; + } + + if (!is_object($result) || empty($result)) { + return $list; + } + + if (empty($result->matches) or !is_array($result->matches)) { + return $list; + } + + $list['list'] = $this->process_entries($result->matches); + return $list; } /** @@ -174,7 +400,16 @@ class repository_dropbox extends repository { * @inheritDocs */ public function send_thumbnail($source) { - return $this->legacy->send_thumbnail($source); + if (self::use_legacy_api()) { + return $this->legacy->send_thumbnail($source); + } + + $content = $this->dropbox->get_thumbnail($source); + + // Set 30 days lifetime for the image. + // If the image is changed in dropbox it will have different revision number and URL will be different. + // It is completely safe to cache the thumbnail in the browser for a long time. + send_file($content, basename($source), 30 * DAYSECS, 0, true); } /** @@ -184,15 +419,103 @@ class repository_dropbox extends repository { * @return string New serialized reference */ protected function fix_old_style_reference($packed) { - return $this->legacy->fix_old_style_reference($packed); + if (self::use_legacy_api()) { + throw new \coding_exception(__FUNCTION__ . ' is a v2 API function only'); + } + + $ref = unserialize($packed); + $ref = $this->dropbox->get_file_share_info($ref->path); + if (!$ref || empty($ref->url)) { + // Some error occurred, do not fix reference for now. + return $packed; + } + + $newreference = serialize($ref); + if ($newreference !== $packed) { + // We need to update references in the database. + global $DB; + $params = array( + 'newreference' => $newreference, + 'newhash' => sha1($newreference), + 'reference' => $packed, + 'hash' => sha1($packed), + 'repoid' => $this->id, + ); + $refid = $DB->get_field_sql('SELECT id FROM {files_reference} + WHERE reference = :reference AND referencehash = :hash + AND repositoryid = :repoid', $params); + if (!$refid) { + return $newreference; + } + + $existingrefid = $DB->get_field_sql('SELECT id FROM {files_reference} + WHERE reference = :newreference AND referencehash = :newhash + AND repositoryid = :repoid', $params); + if ($existingrefid) { + // The same reference already exists, we unlink all files from it, + // link them to the current reference and remove the old one. + $DB->execute('UPDATE {files} SET referencefileid = :refid + WHERE referencefileid = :existingrefid', + array('refid' => $refid, 'existingrefid' => $existingrefid)); + $DB->delete_records('files_reference', array('id' => $existingrefid)); + } + + // Update the reference. + $params['refid'] = $refid; + $DB->execute('UPDATE {files_reference} + SET reference = :newreference, referencehash = :newhash + WHERE id = :refid', $params); + } + return $newreference; } + + /** + * Unpack the supplied serialized reference, fixing it if required. + * + * @param string $packed The packed reference + * @return object The unpacked reference + */ + protected function unpack_reference($packed) { + if (self::use_legacy_api()) { + return $this->legacy->fix_old_style_reference($packed); + } + + $reference = unserialize($packed); + if (empty($reference->url)) { + // The reference is missing some information. Attempt to update it. + return unserialize($this->fix_old_style_reference($packed)); + } + + return $reference; + } + + /** + * Converts a URL received from dropbox API function 'shares' into URL that + * can be used to download/access file directly + * + * @param string $sharedurl + * @return string + */ + protected function get_file_download_link($sharedurl) { + $url = new \moodle_url($sharedurl); + $url->param('dl', 1); + + return $url->out(false); + } + /** * Logout from dropbox. * * @inheritDocs */ public function logout() { - return $this->legacy->logout(); + if (self::use_legacy_api()) { + return $this->legacy->logout(); + } + + $this->dropbox->logout(); + + return $this->print_login(); } /** @@ -201,7 +524,11 @@ class repository_dropbox extends repository { * @inheritDocs */ public function check_login() { - return $this->legacy->check_login(); + if (self::use_legacy_api()) { + return $this->legacy->check_login(); + } + + return $this->dropbox->is_logged_in(); } /** @@ -210,7 +537,21 @@ class repository_dropbox extends repository { * @inheritDocs */ public function print_login() { - return $this->legacy->print_login(); + if (self::use_legacy_api()) { + return $this->legacy->print_login(); + } + + $url = $this->dropbox->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')); + } } /** @@ -219,7 +560,11 @@ class repository_dropbox extends repository { * @inheritDocs */ public function callback() { - return $this->legacy->callback(); + if (self::use_legacy_api()) { + return $this->legacy->callback(); + } + + $this->dropbox->callback(); } /** @@ -232,7 +577,26 @@ class repository_dropbox extends repository { * @inheritDocs */ public function cron() { - return $this->legacy->cron(); + if (self::use_legacy_api()) { + return $this->legacy->cron(); + } + + $fs = get_file_storage(); + $files = $fs->get_external_files($this->id); + $fetchedreferences = []; + foreach ($files as $file) { + if (isset($fetchedreferences[$file->get_referencefileid()])) { + continue; + } + try { + // This call will cache all files that are smaller than max_cache_bytes() + // and synchronise file size of all others. + $this->import_external_file_contents($file, $this->max_cache_bytes()); + $fetchedreferences[$file->get_referencefileid()] = true; + } catch (moodle_exception $e) { + // If an exception is thrown, just continue. This is only a pre-fetch to help speed up general use. + } + } } /** @@ -261,6 +625,19 @@ class repository_dropbox extends repository { $mform->setType('dropbox_secret', PARAM_RAW_TRIMMED); $mform->addElement('static', null, '', get_string('instruction', 'repository_dropbox')); + $mform->addElement('static', null, + get_string('oauth2title', 'repository_dropbox'), + get_string('legacyapihelpsupported', 'repository_dropbox')); + $mform->addElement('static', null, + get_string('oauth2redirecturi', 'repository_dropbox'), + self::get_oauth2callbackurl()->out() + ); + + // The Legacy API is still available so give the user the option of which version to use. + $mform->addElement('radio', 'legacyapi', get_string('usev2api', 'repository_dropbox'), null, false); + $mform->addElement('radio', 'legacyapi', get_string('uselegacyapi', 'repository_dropbox'), null, true); + $mform->setDefault('legacyapi', false); + $mform->addElement('text', 'dropbox_cachelimit', get_string('cachelimit', 'repository_dropbox'), array('size' => '40')); $mform->addRule('dropbox_cachelimit', null, 'numeric', null, 'client'); $mform->setType('dropbox_cachelimit', PARAM_INT); @@ -289,6 +666,11 @@ class repository_dropbox extends repository { unset($options['dropbox_cachelimit']); } + if (!empty($options['legacyapi'])) { + set_config('legacyapi', $this->legacyapi, 'dropbox'); + unset($options['legacyapi']); + } + return parent::set_option($options); } @@ -304,16 +686,30 @@ class repository_dropbox extends repository { return trim(get_config('dropbox', 'dropbox_secret')); } else if ($config === 'dropbox_cachelimit') { return $this->max_cache_bytes(); + } elseif ($config === 'legacyapi') { + return trim(get_config('dropbox', 'legacyapi')); } else { $options = parent::get_option(); $options['dropbox_key'] = trim(get_config('dropbox', 'dropbox_key')); $options['dropbox_secret'] = trim(get_config('dropbox', 'dropbox_secret')); $options['dropbox_cachelimit'] = $this->max_cache_bytes(); + $options['legacyapi'] = trim(get_config('dropbox', 'legacyapi')); } return $options; } + /** + * Return the OAuth 2 Redirect URI. + * + * @return moodle_url + */ + public static function get_oauth2callbackurl() { + global $CFG; + + return new moodle_url($CFG->httpswwwroot . '/admin/oauth2callback.php'); + } + /** * Option names of dropbox plugin. * @@ -321,6 +717,7 @@ class repository_dropbox extends repository { */ public static function get_type_option_names() { return [ + 'legacyapi', 'dropbox_key', 'dropbox_secret', 'pluginname', @@ -357,7 +754,175 @@ class repository_dropbox extends repository { * @inheritDocs */ public function sync_reference(stored_file $file) { - return $this->legacy->sync_reference($file); + if (self::use_legacy_api()) { + return $this->legacy->sync_reference($file); + } + + global $CFG; + + if ($file->get_referencelastsync() + DAYSECS > time()) { + // Only synchronise once per day. + return false; + } + + $reference = $this->unpack_reference($file->get_reference()); + if (!isset($reference->url)) { + // The URL to sync with is missing. + return false; + } + + $c = new curl; + $url = $this->get_file_download_link($reference->url); + if (file_extension_in_typegroup($reference->path, 'web_image')) { + $saveas = $this->prepare_file(''); + try { + $result = $c->download_one($url, [], [ + 'filepath' => $saveas, + 'timeout' => $CFG->repositorysyncimagetimeout, + 'followlocation' => true, + ]); + $info = $c->get_info(); + if ($result === true && isset($info['http_code']) && $info['http_code'] == 200) { + $fs = get_file_storage(); + list($contenthash, $filesize, ) = $fs->add_file_to_pool($saveas); + $file->set_synchronized($contenthash, $filesize); + return true; + } + } catch (Exception $e) { + // IF the download_one fails, we will attempt to download + // again with get() anyway. + } + } + + $c->get($url, null, array('timeout' => $CFG->repositorysyncimagetimeout, 'followlocation' => true, 'nobody' => true)); + $info = $c->get_info(); + if (isset($info['http_code']) && $info['http_code'] == 200 && + array_key_exists('download_content_length', $info) && + $info['download_content_length'] >= 0) { + $filesize = (int)$info['download_content_length']; + $file->set_synchronized(null, $filesize); + return true; + } + $file->set_missingsource(); + return true; + } + + /** + * Process a standard entries list. + * + * @param array $entries The list of entries returned from the API + * @return array The manipulated entries for display in the file picker + */ + protected function process_entries(array $entries) { + if (self::use_legacy_api()) { + throw new \coding_exception(__FUNCTION__ . ' is a v2 API function only'); + } + + global $OUTPUT; + + $dirslist = []; + $fileslist = []; + foreach ($entries as $entry) { + $entrydata = $entry; + if (isset($entrydata->metadata)) { + // If this is metadata, fetch the metadata content. + // We only use the consistent parts of the file, folder, and metadata. + $entrydata = $entrydata->metadata; + } + if ($entrydata->{".tag"} === "folder") { + $dirslist[] = [ + 'title' => $entrydata->name, + // Use the display path here rather than lower. + // Dropbox is case insensitive but this leads to more accurate breadcrumbs. + 'path' => file_correct_filepath($entrydata->path_display), + 'thumbnail' => $OUTPUT->pix_url(file_folder_icon(64))->out(false), + 'thumbnail_height' => 64, + 'thumbnail_width' => 64, + 'children' => array(), + ]; + } else if ($entrydata->{".tag"} === "file") { + $fileslist[] = [ + 'title' => $entrydata->name, + // Use the path_lower here to make life easier elsewhere. + 'source' => $entrydata->path_lower, + 'size' => $entrydata->size, + 'date' => strtotime($entrydata->client_modified), + 'thumbnail' => $OUTPUT->pix_url(file_extension_icon($entrydata->path_lower, 64))->out(false), + 'realthumbnail' => $this->get_thumbnail_url($entrydata), + 'thumbnail_height' => 64, + 'thumbnail_width' => 64, + ]; + } + } + + $fileslist = array_filter($fileslist, array($this, 'filter')); + + return array_merge($dirslist, array_values($fileslist)); + } + + /** + * Process the breadcrumbs for a listing. + * + * @param string $path The path to create breadcrumbs for + * @return array + */ + protected function process_breadcrumbs($path) { + if (self::use_legacy_api()) { + throw new \coding_exception(__FUNCTION__ . ' is a v2 API function only'); + } + + // Process breadcrumb trail. + // Note: Dropbox is case insensitive. + // Without performing an additional API call, it isn't possible to get the path_display. + // As a result, the path here is the path_lower. + $breadcrumbs = [ + [ + 'path' => '/', + 'name' => get_string('dropbox', 'repository_dropbox'), + ], + ]; + + $path = rtrim($path, '/'); + $directories = explode('/', $path); + $pathtodate = ''; + foreach ($directories as $directory) { + if ($directory === '') { + continue; + } + $pathtodate .= '/' . $directory; + $breadcrumbs[] = [ + 'path' => $pathtodate, + 'name' => $directory, + ]; + } + + return $breadcrumbs; + } + + /** + * Grab the thumbnail URL for the specified entry. + * + * @param object $entry The file entry as retrieved from the API + * @return moodle_url + */ + protected function get_thumbnail_url($entry) { + if (self::use_legacy_api()) { + throw new \coding_exception(__FUNCTION__ . ' is a v2 API function only'); + } + + if ($this->dropbox->supports_thumbnail($entry)) { + $thumburl = new moodle_url('/repository/dropbox/thumbnail.php', [ + // The id field in dropbox is unique - no need to specify a revision. + 'source' => $entry->id, + 'path' => $entry->path_lower, + + 'repo_id' => $this->id, + 'ctx_id' => $this->context->id, + ]); + return $thumburl->out(false); + } + + return ''; } /** diff --git a/repository/dropbox/tests/api_test.php b/repository/dropbox/tests/api_test.php new file mode 100644 index 00000000000..55e4ca7f7fe --- /dev/null +++ b/repository/dropbox/tests/api_test.php @@ -0,0 +1,617 @@ +. + +/** + * Tests for the Dropbox API (v2). + * + * @package repository_dropbox + * @copyright Andrew Nicols + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +/** + * Tests for the Dropbox API (v2). + * + * @package repository_dropbox + * @copyright Andrew Nicols + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class repository_dropbox_api_testcase extends advanced_testcase { + /** + * Data provider for has_additional_results. + * + * @return array + */ + public function has_additional_results_provider() { + return [ + 'No more results' => [ + (object) [ + 'has_more' => false, + 'cursor' => '', + ], + false + ], + 'Has more, No cursor' => [ + (object) [ + 'has_more' => true, + 'cursor' => '', + ], + false + ], + 'Has more, Has cursor' => [ + (object) [ + 'has_more' => true, + 'cursor' => 'example_cursor', + ], + true + ], + 'Missing has_more' => [ + (object) [ + 'cursor' => 'example_cursor', + ], + false + ], + 'Missing cursor' => [ + (object) [ + 'has_more' => 'example_cursor', + ], + false + ], + ]; + } + + /** + * Tests for the has_additional_results API function. + * + * @dataProvider has_additional_results_provider + * @param object $result The data to test + * @param bool $expected The expected result + */ + public function test_has_additional_results($result, $expected) { + $mock = $this->getMockBuilder('\repository_dropbox\dropbox') + ->disableOriginalConstructor() + ->setMethods(null) + ->getMock(); + + $this->assertEquals($expected, $mock->has_additional_results($result)); + } + + /** + * Data provider for check_and_handle_api_errors. + * + * @return array + */ + public function check_and_handle_api_errors_provider() { + return [ + '200 http_code' => [ + ['http_code' => 200], + '', + null, + null, + ], + '400 http_code' => [ + ['http_code' => 400], + 'Unused', + 'coding_exception', + 'Invalid input parameter passed to DropBox API.', + ], + '401 http_code' => [ + ['http_code' => 401], + 'Unused', + '\repository_dropbox\authentication_exception', + 'Authentication token expired', + ], + '409 http_code' => [ + ['http_code' => 409], + 'Some data here', + 'coding_exception', + 'Endpoint specific error: Some data here', + ], + '429 http_code' => [ + ['http_code' => 429], + 'Unused', + '\repository_dropbox\rate_limit_exception', + 'Rate limit hit', + ], + '500 http_code' => [ + ['http_code' => 500], + 'Response body', + 'invalid_response_exception', + '500: Response body', + ], + '599 http_code' => [ + ['http_code' => 599], + 'Response body', + 'invalid_response_exception', + '599: Response body', + ], + '600 http_code (invalid, but not officially an error)' => [ + ['http_code' => 600], + '', + null, + null, + ], + ]; + } + + /** + * Tests for check_and_handle_api_errors. + * + * @dataProvider check_and_handle_api_errors_provider + * @param object $info The response to test + * @param string $data The contented returned by the curl call + * @param string $exception The name of the expected exception + * @param string $exceptionmessage The expected message in the exception + */ + public function test_check_and_handle_api_errors($info, $data, $exception, $exceptionmessage) { + $mock = $this->getMockBuilder('\repository_dropbox\dropbox') + ->disableOriginalConstructor() + ->setMethods(null) + ->getMock(); + + $mock->info = $info; + + $rc = new \ReflectionClass('\repository_dropbox\dropbox'); + $rcm = $rc->getMethod('check_and_handle_api_errors'); + $rcm->setAccessible(true); + + if ($exception) { + $this->setExpectedException($exception, $exceptionmessage); + } + + $result = $rcm->invoke($mock, $data); + + $this->assertNull($result); + } + + /** + * Data provider for the supports_thumbnail function. + * + * @return array + */ + public function supports_thumbnail_provider() { + $tests = [ + 'Only files support thumbnails' => [ + (object) ['.tag' => 'folder'], + false, + ], + 'Dropbox currently only supports thumbnail generation for files under 20MB' => [ + (object) [ + '.tag' => 'file', + 'size' => 21 * 1024 * 1024, + ], + false, + ], + 'Unusual file extension containing a working format but ending in a non-working one' => [ + (object) [ + '.tag' => 'file', + 'size' => 100 * 1024, + 'path_lower' => 'Example.jpg.pdf', + ], + false, + ], + 'Unusual file extension ending in a working extension' => [ + (object) [ + '.tag' => 'file', + 'size' => 100 * 1024, + 'path_lower' => 'Example.pdf.jpg', + ], + true, + ], + ]; + + // See docs at https://www.dropbox.com/developers/documentation/http/documentation#files-get_thumbnail. + $types = [ + 'pdf' => false, + 'doc' => false, + 'docx' => false, + 'jpg' => true, + 'jpeg' => true, + 'png' => true, + 'tiff' => true, + 'tif' => true, + 'gif' => true, + 'bmp' => true, + ]; + foreach ($types as $type => $result) { + $tests["Test support for {$type}"] = [ + (object) [ + '.tag' => 'file', + 'size' => 100 * 1024, + 'path_lower' => "example_filename.{$type}", + ], + $result, + ]; + } + + return $tests; + } + + /** + * Test the supports_thumbnail function. + * + * @dataProvider supports_thumbnail_provider + * @param object $entry The entry to test + * @param bool $expected Whether this entry supports thumbnail generation + */ + public function test_supports_thumbnail($entry, $expected) { + $mock = $this->getMockBuilder('\repository_dropbox\dropbox') + ->disableOriginalConstructor() + ->setMethods(null) + ->getMock(); + + $this->assertEquals($expected, $mock->supports_thumbnail($entry)); + } + + /** + * Test that the logout makes a call to the correct revocation endpoint. + */ + public function test_logout_revocation() { + $mock = $this->getMockBuilder('\repository_dropbox\dropbox') + ->disableOriginalConstructor() + ->setMethods(['fetch_dropbox_data']) + ->getMock(); + + $mock->expects($this->once()) + ->method('fetch_dropbox_data') + ->with($this->equalTo('auth/token/revoke'), $this->equalTo(null)); + + $this->assertNull($mock->logout()); + } + + /** + * Test that the logout function catches authentication_exception exceptions and discards them. + */ + public function test_logout_revocation_catch_auth_exception() { + $mock = $this->getMockBuilder('\repository_dropbox\dropbox') + ->disableOriginalConstructor() + ->setMethods(['fetch_dropbox_data']) + ->getMock(); + + $mock->expects($this->once()) + ->method('fetch_dropbox_data') + ->will($this->throwException(new \repository_dropbox\authentication_exception('Exception should be caught'))); + + $this->assertNull($mock->logout()); + } + + /** + * Test that the logout function does not catch any other exception. + */ + public function test_logout_revocation_does_not_catch_other_exceptions() { + $mock = $this->getMockBuilder('\repository_dropbox\dropbox') + ->disableOriginalConstructor() + ->setMethods(['fetch_dropbox_data']) + ->getMock(); + + $mock->expects($this->once()) + ->method('fetch_dropbox_data') + ->will($this->throwException(new \repository_dropbox\rate_limit_exception)); + + $this->setExpectedException('\repository_dropbox\rate_limit_exception'); + $mock->logout(); + } + + /** + * Test basic fetch_dropbox_data function. + */ + public function test_fetch_dropbox_data_endpoint() { + $mock = $this->getMockBuilder('\repository_dropbox\dropbox') + ->disableOriginalConstructor() + ->setMethods([ + 'request', + 'get_api_endpoint', + 'get_content_endpoint', + ]) + ->getMock(); + + $endpoint = 'testEndpoint'; + + // The fetch_dropbox_data call should be called against the standard endpoint only. + $mock->expects($this->once()) + ->method('get_api_endpoint') + ->with($endpoint) + ->will($this->returnValue("https://example.com/api/2/{$endpoint}")); + + $mock->expects($this->never()) + ->method('get_content_endpoint'); + + $mock->expects($this->once()) + ->method('request') + ->will($this->returnValue(json_encode([]))); + + // Make the call. + $rc = new \ReflectionClass('\repository_dropbox\dropbox'); + $rcm = $rc->getMethod('fetch_dropbox_data'); + $rcm->setAccessible(true); + $rcm->invoke($mock, $endpoint); + } + + /** + * Some Dropbox endpoints require that the POSTFIELDS be set to null exactly. + */ + public function test_fetch_dropbox_data_postfields_null() { + $mock = $this->getMockBuilder('\repository_dropbox\dropbox') + ->disableOriginalConstructor() + ->setMethods([ + 'request', + ]) + ->getMock(); + + $endpoint = 'testEndpoint'; + + $mock->expects($this->once()) + ->method('request') + ->with($this->anything(), $this->callback(function($d) { + return $d['CURLOPT_POSTFIELDS'] === 'null'; + })) + ->will($this->returnValue(json_encode([]))); + + // Make the call. + $rc = new \ReflectionClass('\repository_dropbox\dropbox'); + $rcm = $rc->getMethod('fetch_dropbox_data'); + $rcm->setAccessible(true); + $rcm->invoke($mock, $endpoint, null); + } + + /** + * When data is specified, it should be json_encoded in POSTFIELDS. + */ + public function test_fetch_dropbox_data_postfields_data() { + $mock = $this->getMockBuilder('\repository_dropbox\dropbox') + ->disableOriginalConstructor() + ->setMethods([ + 'request', + ]) + ->getMock(); + + $endpoint = 'testEndpoint'; + $data = ['something' => 'somevalue']; + + $mock->expects($this->once()) + ->method('request') + ->with($this->anything(), $this->callback(function($d) use ($data) { + return $d['CURLOPT_POSTFIELDS'] === json_encode($data); + })) + ->will($this->returnValue(json_encode([]))); + + // Make the call. + $rc = new \ReflectionClass('\repository_dropbox\dropbox'); + $rcm = $rc->getMethod('fetch_dropbox_data'); + $rcm->setAccessible(true); + $rcm->invoke($mock, $endpoint, $data); + } + + /** + * When more results are available, these should be fetched until there are no more. + */ + public function test_fetch_dropbox_data_recurse_on_additional_records() { + $mock = $this->getMockBuilder('\repository_dropbox\dropbox') + ->disableOriginalConstructor() + ->setMethods([ + 'request', + 'get_api_endpoint', + ]) + ->getMock(); + + $endpoint = 'testEndpoint'; + + // We can't detect if fetch_dropbox_data was called twice because + // we can' + $mock->expects($this->exactly(3)) + ->method('request') + ->will($this->onConsecutiveCalls( + json_encode(['has_more' => true, 'cursor' => 'Example', 'entries' => ['foo', 'bar']]), + json_encode(['has_more' => true, 'cursor' => 'Example', 'entries' => ['baz']]), + json_encode(['has_more' => false, 'cursor' => '', 'entries' => ['bum']]) + )); + + // We automatically adjust for the /continue endpoint. + $mock->expects($this->exactly(3)) + ->method('get_api_endpoint') + ->withConsecutive(['testEndpoint'], ['testEndpoint/continue'], ['testEndpoint/continue']) + ->willReturn($this->onConsecutiveCalls( + 'https://example.com/api/2/testEndpoint', + 'https://example.com/api/2/testEndpoint/continue', + 'https://example.com/api/2/testEndpoint/continue' + )); + + // Make the call. + $rc = new \ReflectionClass('\repository_dropbox\dropbox'); + $rcm = $rc->getMethod('fetch_dropbox_data'); + $rcm->setAccessible(true); + $result = $rcm->invoke($mock, $endpoint, null); + + $this->assertEquals([ + 'foo', + 'bar', + 'baz', + 'bum', + ], $result->entries); + + $this->assertFalse(isset($result->cursor)); + $this->assertFalse(isset($result->has_more)); + } + + /** + * Base tests for the fetch_dropbox_content function. + */ + public function test_fetch_dropbox_content() { + $mock = $this->getMockBuilder('\repository_dropbox\dropbox') + ->disableOriginalConstructor() + ->setMethods([ + 'request', + 'setHeader', + 'get_content_endpoint', + 'get_api_endpoint', + 'check_and_handle_api_errors', + ]) + ->getMock(); + + $data = ['exampledata' => 'examplevalue']; + $endpoint = 'getContent'; + $url = "https://example.com/api/2/{$endpoint}"; + $response = 'Example content'; + + // Only the content endpoint should be called. + $mock->expects($this->once()) + ->method('get_content_endpoint') + ->with($endpoint) + ->will($this->returnValue($url)); + + $mock->expects($this->never()) + ->method('get_api_endpoint'); + + $mock->expects($this->exactly(2)) + ->method('setHeader') + ->withConsecutive( + [$this->equalTo('Content-Type: ')], + [$this->equalTo('Dropbox-API-Arg: ' . json_encode($data))] + ); + + // Only one request should be made, and it should forcibly be a POST. + $mock->expects($this->once()) + ->method('request') + ->with($this->equalTo($url), $this->callback(function($options) { + return $options['CURLOPT_POST'] === 1; + })) + ->willReturn($response); + + $mock->expects($this->once()) + ->method('check_and_handle_api_errors') + ->with($this->equalTo($response)) + ; + + // Make the call. + $rc = new \ReflectionClass('\repository_dropbox\dropbox'); + $rcm = $rc->getMethod('fetch_dropbox_content'); + $rcm->setAccessible(true); + $result = $rcm->invoke($mock, $endpoint, $data); + + $this->assertEquals($response, $result); + } + + /** + * Test that the get_file_share_info function returns an existing link if one is available. + */ + public function test_get_file_share_info_existing() { + $mock = $this->getMockBuilder('\repository_dropbox\dropbox') + ->disableOriginalConstructor() + ->setMethods([ + 'fetch_dropbox_data', + 'normalize_file_share_info', + ]) + ->getMock(); + + $id = 'LifeTheUniverseAndEverything'; + $file = (object) ['.tag' => 'file', 'id' => $id, 'path_lower' => 'SomeValue']; + $sharelink = 'https://example.com/share/link'; + + // Mock fetch_dropbox_data to return an existing file. + $mock->expects($this->once()) + ->method('fetch_dropbox_data') + ->with( + $this->equalTo('sharing/list_shared_links'), + $this->equalTo(['path' => $id]) + ) + ->willReturn((object) ['links' => [$file]]); + + $mock->expects($this->once()) + ->method('normalize_file_share_info') + ->with($this->equalTo($file)) + ->will($this->returnValue($sharelink)); + + $this->assertEquals($sharelink, $mock->get_file_share_info($id)); + } + + /** + * Test that the get_file_share_info function creates a new link if one is not available. + */ + public function test_get_file_share_info_new() { + $mock = $this->getMockBuilder('\repository_dropbox\dropbox') + ->disableOriginalConstructor() + ->setMethods([ + 'fetch_dropbox_data', + 'normalize_file_share_info', + ]) + ->getMock(); + + $id = 'LifeTheUniverseAndEverything'; + $file = (object) ['.tag' => 'file', 'id' => $id, 'path_lower' => 'SomeValue']; + $sharelink = 'https://example.com/share/link'; + + // Mock fetch_dropbox_data to return an existing file. + $mock->expects($this->exactly(2)) + ->method('fetch_dropbox_data') + ->withConsecutive( + [$this->equalTo('sharing/list_shared_links'), $this->equalTo(['path' => $id])], + [$this->equalTo('sharing/create_shared_link_with_settings'), $this->equalTo([ + 'path' => $id, + 'settings' => [ + 'requested_visibility' => 'public', + ] + ])] + ) + ->will($this->onConsecutiveCalls( + (object) ['links' => []], + $file + )); + + $mock->expects($this->once()) + ->method('normalize_file_share_info') + ->with($this->equalTo($file)) + ->will($this->returnValue($sharelink)); + + $this->assertEquals($sharelink, $mock->get_file_share_info($id)); + } + + /** + * Test failure behaviour with get_file_share_info fails to create a new link. + */ + public function test_get_file_share_info_new_failure() { + $mock = $this->getMockBuilder('\repository_dropbox\dropbox') + ->disableOriginalConstructor() + ->setMethods([ + 'fetch_dropbox_data', + 'normalize_file_share_info', + ]) + ->getMock(); + + $id = 'LifeTheUniverseAndEverything'; + + // Mock fetch_dropbox_data to return an existing file. + $mock->expects($this->exactly(2)) + ->method('fetch_dropbox_data') + ->withConsecutive( + [$this->equalTo('sharing/list_shared_links'), $this->equalTo(['path' => $id])], + [$this->equalTo('sharing/create_shared_link_with_settings'), $this->equalTo([ + 'path' => $id, + 'settings' => [ + 'requested_visibility' => 'public', + ] + ])] + ) + ->will($this->onConsecutiveCalls( + (object) ['links' => []], + null + )); + + $mock->expects($this->never()) + ->method('normalize_file_share_info'); + + $this->assertNull($mock->get_file_share_info($id)); + } +} diff --git a/repository/dropbox/tests/generator/lib.php b/repository/dropbox/tests/generator/lib.php index 1f4db858265..f47b8a3bd3b 100644 --- a/repository/dropbox/tests/generator/lib.php +++ b/repository/dropbox/tests/generator/lib.php @@ -50,6 +50,9 @@ class repository_dropbox_generator extends testing_repository_generator { if (!isset($record['dropbox_cachelimit'])) { $record['dropbox_cachelimit'] = 0; } + if (!isset($record['legacyapi'])) { + $record['legacyapi'] = 0; + } return $record; } diff --git a/repository/dropbox/version.php b/repository/dropbox/version.php index 8f24b7835c6..3819789026a 100644 --- a/repository/dropbox/version.php +++ b/repository/dropbox/version.php @@ -25,6 +25,6 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2016052300; // The current plugin version (Date: YYYYMMDDXX) +$plugin->version = 2016052301; // The current plugin version (Date: YYYYMMDDXX) $plugin->requires = 2016051900; // Requires this Moodle version $plugin->component = 'repository_dropbox'; // Full name of the plugin (used for diagnostics) diff --git a/repository/tests/generator_test.php b/repository/tests/generator_test.php index 9f24d63f1dc..541a9a27b8b 100644 --- a/repository/tests/generator_test.php +++ b/repository/tests/generator_test.php @@ -105,6 +105,7 @@ class core_repository_generator_testcase extends advanced_testcase { $record->dropbox_key = '12345'; $record->dropbox_secret = '67890'; $record->dropbox_cachelimit = '123'; + $record->legacyapi = '0'; $dropbox = $this->getDataGenerator()->create_repository_type('dropbox', $record); $config = get_config('dropbox'); @@ -112,7 +113,7 @@ class core_repository_generator_testcase extends advanced_testcase { $record->enablecourseinstances = '0'; $this->assertEquals($record, $config); $this->assertEquals('Custom Dropbox', - $DB->get_field('repository_instances', 'name', array('typeid' => $dropbox->id), MUST_EXIST)); + $DB->get_field('repository_instances', 'name', array('typeid' => $dropbox->id), MUST_EXIST)); // Multiple instances. $record = new stdClass();