From 38bd68868d8f368ece03fe10ccc28b7dc1a464bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Mudr=C3=A1k?= Date: Thu, 24 Aug 2017 20:20:45 +0200 Subject: [PATCH] MDL-59645 portfolio_flickr: Make the Flickr portfolio export work again The patch modifies the Flickr portfolio to make it use of the new oauth1 based client. --- portfolio/flickr/lib.php | 116 +++++++++++++++++++++++++-------------- 1 file changed, 76 insertions(+), 40 deletions(-) diff --git a/portfolio/flickr/lib.php b/portfolio/flickr/lib.php index df85a00a3f9..afbd4ab67e0 100644 --- a/portfolio/flickr/lib.php +++ b/portfolio/flickr/lib.php @@ -26,12 +26,12 @@ defined('MOODLE_INTERNAL') || die(); require_once($CFG->libdir.'/portfolio/plugin.php'); require_once($CFG->libdir.'/filelib.php'); -require_once($CFG->libdir.'/flickrlib.php'); +require_once($CFG->libdir.'/flickrclient.php'); class portfolio_plugin_flickr extends portfolio_plugin_push_base { + /** @var flickr_client */ private $flickr; - private $token; private $raw_sets; public function supported_formats() { @@ -52,25 +52,32 @@ class portfolio_plugin_flickr extends portfolio_plugin_push_base { $filesize = $file->get_filesize(); if ($file->is_valid_image()) { - $return = $this->flickr->upload($file, array( - 'title' => $this->get_export_config('title'), - 'description' => $this->get_export_config('description'), - 'tags' => $this->get_export_config('tags'), - 'is_public' => $this->get_export_config('is_public'), - 'is_friend' => $this->get_export_config('is_friend'), - 'is_family' => $this->get_export_config('is_family'), - 'safety_level' => $this->get_export_config('safety_level'), - 'content_type' => $this->get_export_config('content_type'), - 'hidden' => $this->get_export_config('hidden'))); - if ($return) { - // Attach photo to a set if requested - if ($this->get_export_config('set') && !empty($this->flickr->parsed_response['photoid'])) { - $this->flickr->photosets_addPhoto($this->get_export_config('set'), - $this->flickr->parsed_response['photoid']); - } - } else { - throw new portfolio_plugin_exception('uploadfailed', 'portfolio_flickr', - $this->flickr->error_code . ': ' . $this->flickr->error_msg); + $photoid = $this->flickr->upload($file, [ + 'title' => $this->get_export_config('title'), + 'description' => $this->get_export_config('description'), + 'tags' => $this->get_export_config('tags'), + 'is_public' => $this->get_export_config('is_public'), + 'is_friend' => $this->get_export_config('is_friend'), + 'is_family' => $this->get_export_config('is_family'), + 'safety_level' => $this->get_export_config('safety_level'), + 'content_type' => $this->get_export_config('content_type'), + 'hidden' => $this->get_export_config('hidden'), + ]); + + if ($photoid === false) { + $this->set_user_config([ + 'accesstoken' => null, + 'accesstokensecret' => null, + ]); + throw new portfolio_plugin_exception('uploadfailed', 'portfolio_flickr', '', 'Authentication failed'); + } + + // Attach photo to a set if requested. + if ($this->get_export_config('set')) { + $result = $this->flickr->call('photosets.addPhoto', [ + 'photoset_id' => $this->get_export_config('set'), + 'photo_id' => $photoid, + ], 'POST'); } } } @@ -81,7 +88,7 @@ class portfolio_plugin_flickr extends portfolio_plugin_push_base { } public function get_interactive_continue_url() { - return $this->flickr->urls_getUserPhotos(); + return 'https://www.flickr.com/photos/organize'; } public function expected_time($callertime) { @@ -119,43 +126,57 @@ class portfolio_plugin_flickr extends portfolio_plugin_push_base { } public function get_allowed_user_config() { - return array('authtoken', 'nsid'); + return array('accesstoken', 'accesstokensecret'); } public function steal_control($stage) { if ($stage != PORTFOLIO_STAGE_CONFIG) { return false; } - if ($this->token) { + + $accesstoken = $this->get_user_config('accesstoken'); + $accesstokensecret = $this->get_user_config('accesstokensecret'); + + $callbackurl = new moodle_url('/portfolio/add.php', ['postcontrol' => 1, 'type' => 'flickr']); + $this->flickr = new flickr_client($this->get_config('apikey'), $this->get_config('sharedsecret'), $callbackurl); + + if (!empty($accesstoken) && !empty($accesstokensecret)) { + // The user has authenticated us already. + $this->flickr->set_access_token($accesstoken, $accesstokensecret); return false; } - $token = $this->get_user_config('authtoken', $this->get('user')->id); - $nsid = $this->get_user_config('nsid', $this->get('user')->id); + $reqtoken = $this->flickr->request_token(); + $this->flickr->set_request_token_secret(['caller' => 'portfolio_flickr'], $reqtoken['oauth_token_secret']); - $this->flickr = new phpFlickr($this->get_config('apikey'), $this->get_config('sharedsecret'), $token); + $authurl = new moodle_url($reqtoken['authorize_url'], ['perms' => 'write']); - if (!empty($token)) { - $this->token = $token; - $this->flickr = new phpFlickr($this->get_config('apikey'), $this->get_config('sharedsecret'), $token); - return false; - } - return $this->flickr->auth('write'); + return $authurl->out(false); } public function post_control($stage, $params) { if ($stage != PORTFOLIO_STAGE_CONFIG) { return; } - if (!array_key_exists('frob', $params) || empty($params['frob'])) { + + if (empty($params['oauth_token']) || empty($params['oauth_verifier'])) { throw new portfolio_plugin_exception('noauthtoken', 'portfolio_flickr'); } - $this->flickr = new phpFlickr($this->get_config('apikey'), $this->get_config('sharedsecret')); + $callbackurl = new moodle_url('/portfolio/add.php', ['postcontrol' => 1, 'type' => 'flickr']); + $this->flickr = new flickr_client($this->get_config('apikey'), $this->get_config('sharedsecret'), $callbackurl); - $auth_info = $this->flickr->auth_getToken($params['frob']); + $secret = $this->flickr->get_request_token_secret(['caller' => 'portfolio_flickr']); - $this->set_user_config(array('authtoken' => $auth_info['token'], 'nsid' => $auth_info['user']['nsid']), $this->get('user')->id); + // Exchange the request token for the access token. + $accesstoken = $this->flickr->get_access_token($params['oauth_token'], $secret, $params['oauth_verifier']); + + // Store the access token and the access token secret as the user + // config so that we can use it on behalf of the user in next exports. + $this->set_user_config([ + 'accesstoken' => $accesstoken['oauth_token'], + 'accesstokensecret' => $accesstoken['oauth_token_secret'], + ]); } public function export_config_form(&$mform) { @@ -198,14 +219,29 @@ class portfolio_plugin_flickr extends portfolio_plugin_push_base { } } + /** + * Fetches a list of current user's photosets (albums) on flickr. + * + * @return array (int)id => (string)title + */ private function get_sets() { + if (empty($this->raw_sets)) { - $this->raw_sets = $this->flickr->photosets_getList(); + $this->raw_sets = $this->flickr->call('photosets.getList'); + } + + if ($this->raw_sets === false) { + // Authentication failed, drop the locally stored token to force re-authentication. + $this->set_user_config([ + 'accesstoken' => null, + 'accesstokensecret' => null, + ]); + return array(); } $sets = array(); - foreach ($this->raw_sets['photoset'] as $set_data) { - $sets[$set_data['id']] = $set_data['title']; + foreach ($this->raw_sets->photosets->photoset as $set) { + $sets[$set->id] = $set->title->_content; } return $sets; }