Merge branch 'MDL-54742-master' of git://github.com/mihailges/moodle
[moodle.git] / repository / flickr / lib.php
blob84d683ae90c8b042a48c81b531820996ffd3831b
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * This plugin is used to access flickr pictures
20 * @since Moodle 2.0
21 * @package repository_flickr
22 * @copyright 2010 Dongsheng Cai {@link http://dongsheng.org}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 require_once($CFG->dirroot . '/repository/lib.php');
26 require_once($CFG->libdir.'/flickrclient.php');
28 /**
29 * This plugin is used to access user's private flickr repository
31 * @since Moodle 2.0
32 * @package repository_flickr
33 * @copyright 2009 Dongsheng Cai {@link http://dongsheng.org}
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 class repository_flickr extends repository {
38 /** @var flickr_client */
39 protected $flickr;
41 /** @var string oauth consumer key */
42 protected $api_key;
44 /** @var string oauth consumer secret */
45 protected $secret;
47 /** @var string oauth access token */
48 protected $accesstoken;
50 /** @var string oauth access token secret */
51 protected $accesstokensecret;
53 public $photos;
55 /**
56 * Stores sizes of images to prevent multiple API call
58 static private $sizes = array();
60 /**
62 * @param int $repositoryid
63 * @param object $context
64 * @param array $options
66 public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array()) {
67 global $SESSION, $CFG;
68 $options['page'] = optional_param('p', 1, PARAM_INT);
69 parent::__construct($repositoryid, $context, $options);
71 $this->api_key = $this->get_option('api_key');
72 $this->secret = $this->get_option('secret');
74 $this->accesstoken = get_user_preferences('repository_flickr_access_token');
75 $this->accesstokensecret = get_user_preferences('repository_flickr_access_token_secret');
77 $callbackurl = new moodle_url('/repository/repository_callback.php', ['repo_id' => $repositoryid]);
78 $this->flickr = new flickr_client($this->api_key, $this->secret, $callbackurl);
79 $this->flickr->set_access_token($this->accesstoken, $this->accesstokensecret);
82 /**
83 * Check if the user has authorized us to make requests to Flickr API.
85 * @return bool
87 public function check_login() {
89 if (empty($this->accesstoken) || empty($this->accesstokensecret)) {
90 return false;
92 } else {
93 return true;
97 /**
98 * Purge the stored access token and related user data.
100 * @return string
102 public function logout() {
104 set_user_preference('repository_flickr_access_token', null);
105 set_user_preference('repository_flickr_access_token_secret', null);
107 $this->accesstoken = null;
108 $this->accesstokensecret = null;
110 return $this->print_login();
115 * @param array $options
116 * @return mixed
118 public function set_option($options = array()) {
119 if (!empty($options['api_key'])) {
120 set_config('api_key', trim($options['api_key']), 'flickr');
122 if (!empty($options['secret'])) {
123 set_config('secret', trim($options['secret']), 'flickr');
125 unset($options['api_key']);
126 unset($options['secret']);
127 $ret = parent::set_option($options);
128 return $ret;
133 * @param string $config
134 * @return mixed
136 public function get_option($config = '') {
137 if ($config==='api_key') {
138 return trim(get_config('flickr', 'api_key'));
139 } elseif ($config ==='secret') {
140 return trim(get_config('flickr', 'secret'));
141 } else {
142 $options['api_key'] = trim(get_config('flickr', 'api_key'));
143 $options['secret'] = trim(get_config('flickr', 'secret'));
145 $options = parent::get_option($config);
146 return $options;
151 * @return bool
153 public function global_search() {
154 if (empty($this->token)) {
155 return false;
156 } else {
157 return true;
162 * Show the interface to log in to Flickr..
164 * @return string|array
166 public function print_login() {
168 $reqtoken = $this->flickr->request_token();
169 $this->flickr->set_request_token_secret(['caller' => 'repository_flickr'], $reqtoken['oauth_token_secret']);
171 // Even when the Flick auth docs states the "perms" argument is
172 // optional, it does not work without it.
173 $authurl = new moodle_url($reqtoken['authorize_url'], array('perms' => 'read'));
175 if ($this->options['ajax']) {
176 return [
177 'login' => [
179 'type' => 'popup',
180 'url' => $authurl->out(false),
185 } else {
186 echo '<a target="_blank" href="'.$authurl->out().'">'.get_string('login', 'repository').'</a>';
191 * Search for the user's photos at Flickr
193 * @param string $searchtext Photos with title, description or tags containing the text will be returned
194 * @param int $page Page number to load
195 * @return array
197 public function search($searchtext, $page = 0) {
199 $response = $this->flickr->call('photos.search', [
200 'user_id' => 'me',
201 'per_page' => 24,
202 'extras' => 'original_format,url_sq,url_o,date_upload,owner_name',
203 'page' => $page,
204 'text' => $searchtext,
207 if ($response === false) {
208 $this->logout();
209 return [];
212 // Convert the response to the format expected by the filepicker.
214 $ret = [
215 'manage' => 'https://www.flickr.com/photos/organize',
216 'list' => [],
217 'pages' => $response->photos->pages,
218 'total' => $response->photos->total,
219 'perpage' => $response->photos->perpage,
220 'page' => $response->photos->page,
223 if (!empty($response->photos->photo)) {
224 foreach ($response->photos->photo as $p) {
225 if (empty($p->title)) {
226 $p->title = get_string('notitle', 'repository_flickr');
229 if (isset($p->originalformat)) {
230 $format = $p->originalformat;
231 } else {
232 $format = 'jpg';
234 $format = '.'.$format;
236 // Append extension to the file name.
237 if (substr($p->title, strlen($p->title) - strlen($format)) != $format) {
238 $p->title .= $format;
241 $ret['list'][] = [
242 'title' => $p->title,
243 'source' => $p->id,
244 'id' => $p->id,
245 'thumbnail' => $p->url_sq,
246 'thumbnail_width' => $p->width_sq,
247 'thumbnail_height' => $p->height_sq,
248 'date' => $p->dateupload,
249 'url' => $p->url_o,
250 'author' => $p->ownername,
251 'size' => null,
252 'license' => '',
257 // Filter file listing to display specific types only.
258 $ret['list'] = array_filter($ret['list'], array($this, 'filter'));
260 return $ret;
265 * @param string $path
266 * @param int $page
267 * @return array
269 public function get_listing($path = '', $page = '') {
270 return $this->search('', $page);
273 public function get_link($photoid) {
274 return $this->flickr->get_photo_url($photoid);
279 * @param string $photoid
280 * @param string $file
281 * @return string
283 public function get_file($photoid, $file = '') {
284 return parent::get_file($this->flickr->get_photo_url($photoid), $file);
288 * Add Plugin settings input to Moodle form
289 * @param object $mform
291 public static function type_config_form($mform, $classname = 'repository') {
292 global $CFG;
293 $api_key = get_config('flickr', 'api_key');
294 $secret = get_config('flickr', 'secret');
296 if (empty($api_key)) {
297 $api_key = '';
299 if (empty($secret)) {
300 $secret = '';
303 parent::type_config_form($mform);
305 $strrequired = get_string('required');
306 $mform->addElement('text', 'api_key', get_string('apikey', 'repository_flickr'), array('value'=>$api_key,'size' => '40'));
307 $mform->setType('api_key', PARAM_RAW_TRIMMED);
308 $mform->addElement('text', 'secret', get_string('secret', 'repository_flickr'), array('value'=>$secret,'size' => '40'));
309 $mform->setType('secret', PARAM_RAW_TRIMMED);
311 //retrieve the flickr instances
312 $params = array();
313 $params['context'] = array();
314 //$params['currentcontext'] = $this->context;
315 $params['onlyvisible'] = false;
316 $params['type'] = 'flickr';
317 $instances = repository::get_instances($params);
318 if (empty($instances)) {
319 $callbackurl = get_string('callbackwarning', 'repository_flickr');
320 $mform->addElement('static', null, '', $callbackurl);
321 } else {
322 $instance = array_shift($instances);
323 $callbackurl = $CFG->wwwroot.'/repository/repository_callback.php?repo_id='.$instance->id;
324 $mform->addElement('static', 'callbackurl', '', get_string('callbackurltext', 'repository_flickr', $callbackurl));
327 $mform->addRule('api_key', $strrequired, 'required', null, 'client');
328 $mform->addRule('secret', $strrequired, 'required', null, 'client');
332 * Names of the plugin settings
333 * @return array
335 public static function get_type_option_names() {
336 return array('api_key', 'secret', 'pluginname');
338 public function supported_filetypes() {
339 return array('web_image');
341 public function supported_returntypes() {
342 return (FILE_INTERNAL | FILE_EXTERNAL);
346 * Return the source information
348 * @param string $photoid
349 * @return string|null
351 public function get_file_source_info($photoid) {
352 return $this->flickr->get_photo_url($photoid);
356 * Handle the oauth authorize callback
358 * This is to exchange the approved request token for an access token.
360 public function callback() {
362 $token = required_param('oauth_token', PARAM_RAW);
363 $verifier = required_param('oauth_verifier', PARAM_RAW);
364 $secret = $this->flickr->get_request_token_secret(['caller' => 'repository_flickr']);
366 // Exchange the request token for the access token.
367 $accesstoken = $this->flickr->get_access_token($token, $secret, $verifier);
369 // Store the access token and the access token secret in the user preferences.
370 set_user_preference('repository_flickr_access_token', $accesstoken['oauth_token']);
371 set_user_preference('repository_flickr_access_token_secret', $accesstoken['oauth_token_secret']);