mirror of
https://github.com/moodle/moodle.git
synced 2025-01-18 05:58:34 +01:00
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.
This commit is contained in:
parent
34db9e9342
commit
f4d1aa9573
@ -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;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user