Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mudrd8mz committed Sep 19, 2017
1 parent 34db9e9 commit f4d1aa9
Showing 1 changed file with 76 additions and 40 deletions.
116 changes: 76 additions & 40 deletions portfolio/flickr/lib.php
Expand Up @@ -26,12 +26,12 @@

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() {
Expand All @@ -52,25 +52,32 @@ public function send_package() {
$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');
}
}
}
Expand All @@ -81,7 +88,7 @@ public static function allows_multiple_instances() {
}

public function get_interactive_continue_url() {
return $this->flickr->urls_getUserPhotos();
return 'https://www.flickr.com/photos/organize';
}

public function expected_time($callertime) {
Expand Down Expand Up @@ -119,43 +126,57 @@ public function has_export_config() {
}

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) {
return false;
}

$token = $this->get_user_config('authtoken', $this->get('user')->id);
$nsid = $this->get_user_config('nsid', $this->get('user')->id);
$accesstoken = $this->get_user_config('accesstoken');
$accesstokensecret = $this->get_user_config('accesstokensecret');

$this->flickr = new phpFlickr($this->get_config('apikey'), $this->get_config('sharedsecret'), $token);
$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($token)) {
$this->token = $token;
$this->flickr = new phpFlickr($this->get_config('apikey'), $this->get_config('sharedsecret'), $token);
if (!empty($accesstoken) && !empty($accesstokensecret)) {
// The user has authenticated us already.
$this->flickr->set_access_token($accesstoken, $accesstokensecret);
return false;
}
return $this->flickr->auth('write');

$reqtoken = $this->flickr->request_token();
$this->flickr->set_request_token_secret(['caller' => 'portfolio_flickr'], $reqtoken['oauth_token_secret']);

$authurl = new moodle_url($reqtoken['authorize_url'], ['perms' => '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);

$secret = $this->flickr->get_request_token_secret(['caller' => 'portfolio_flickr']);

$auth_info = $this->flickr->auth_getToken($params['frob']);
// Exchange the request token for the access token.
$accesstoken = $this->flickr->get_access_token($params['oauth_token'], $secret, $params['oauth_verifier']);

$this->set_user_config(array('authtoken' => $auth_info['token'], 'nsid' => $auth_info['user']['nsid']), $this->get('user')->id);
// 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) {
Expand Down Expand Up @@ -198,14 +219,29 @@ public function export_config_form(&$mform) {
}
}

/**
* 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;
}
Expand Down

0 comments on commit f4d1aa9

Please sign in to comment.