MDL-33468 - fix illegal whitespace
[moodle.git] / repository / flickr / lib.php
CommitLineData
36ea9a6c 1<?php
10d53fd3
DC
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.
13//
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/>.
16
36ea9a6c 17/**
67233725 18 * This plugin is used to access flickr pictures
36ea9a6c 19 *
10d53fd3 20 * @since 2.0
67233725
DC
21 * @package repository_flickr
22 * @copyright 2010 Dongsheng Cai {@link http://dongsheng.org}
d078f6d3 23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36ea9a6c 24 */
67233725 25require_once($CFG->dirroot . '/repository/lib.php');
539b3d13 26require_once($CFG->libdir.'/flickrlib.php');
6a391ebf 27
96297ca2 28/**
67233725 29 * This plugin is used to access user's private flickr repository
96297ca2 30 *
67233725
DC
31 * @since 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
96297ca2 35 */
dc0976b8 36class repository_flickr extends repository {
6a391ebf 37 private $flickr;
38 public $photos;
19add4c0 39
96297ca2 40 /**
41 *
6f2cd52a
DC
42 * @param int $repositoryid
43 * @param object $context
44 * @param array $options
96297ca2 45 */
447c7a19 46 public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array()) {
d68c527f 47 global $SESSION, $CFG;
48 $options['page'] = optional_param('p', 1, PARAM_INT);
49 parent::__construct($repositoryid, $context, $options);
50
51 $this->setting = 'flickr_';
52
53 $this->api_key = $this->get_option('api_key');
54 $this->secret = $this->get_option('secret');
55
56 $this->token = get_user_preferences($this->setting, '');
57 $this->nsid = get_user_preferences($this->setting.'_nsid', '');
58
59 $this->flickr = new phpFlickr($this->api_key, $this->secret, $this->token);
60
353d5cf3 61 $frob = optional_param('frob', '', PARAM_RAW);
62 if (empty($this->token) && !empty($frob)) {
63 $auth_info = $this->flickr->auth_getToken($frob);
64 $this->token = $auth_info['token'];
65 $this->nsid = $auth_info['user']['nsid'];
66 set_user_preference($this->setting, $auth_info['token']);
67 set_user_preference($this->setting.'_nsid', $auth_info['user']['nsid']);
d68c527f 68 }
69
70 }
96297ca2 71
72 /**
73 *
6f2cd52a 74 * @return bool
96297ca2 75 */
dc0976b8 76 public function check_login() {
d68c527f 77 return !empty($this->token);
78 }
96297ca2 79
80 /**
81 *
6f2cd52a 82 * @return mixed
96297ca2 83 */
dc0976b8 84 public function logout() {
d68c527f 85 set_user_preference($this->setting, '');
86 set_user_preference($this->setting.'_nsid', '');
87 $this->token = '';
88 $this->nsid = '';
89 return $this->print_login();
90 }
96297ca2 91
92 /**
93 *
6f2cd52a
DC
94 * @param array $options
95 * @return mixed
96297ca2 96 */
dc0976b8 97 public function set_option($options = array()) {
27051e43 98 if (!empty($options['api_key'])) {
70d91d90 99 set_config('api_key', trim($options['api_key']), 'flickr');
27051e43 100 }
7fab56b7 101 if (!empty($options['secret'])) {
102 set_config('secret', trim($options['secret']), 'flickr');
103 }
27051e43 104 unset($options['api_key']);
7fab56b7 105 unset($options['secret']);
27051e43 106 $ret = parent::set_option($options);
107 return $ret;
108 }
109
96297ca2 110 /**
111 *
6f2cd52a
DC
112 * @param string $config
113 * @return mixed
96297ca2 114 */
dc0976b8 115 public function get_option($config = '') {
116 if ($config==='api_key') {
70d91d90 117 return trim(get_config('flickr', 'api_key'));
7fab56b7 118 } elseif ($config ==='secret') {
119 return trim(get_config('flickr', 'secret'));
27051e43 120 } else {
70d91d90 121 $options['api_key'] = trim(get_config('flickr', 'api_key'));
7fab56b7 122 $options['secret'] = trim(get_config('flickr', 'secret'));
27051e43 123 }
124 $options = parent::get_option($config);
125 return $options;
126 }
127
96297ca2 128 /**
129 *
6f2cd52a 130 * @return bool
96297ca2 131 */
dc0976b8 132 public function global_search() {
8988c36b 133 if (empty($this->token)) {
455860ce 134 return false;
135 } else {
136 return true;
137 }
138 }
96297ca2 139
140 /**
141 *
6f2cd52a 142 * @return null
96297ca2 143 */
5e98ab96 144 public function print_login() {
145 if ($this->options['ajax']) {
d68c527f 146 $ret = array();
6bdfef5d 147 $popup_btn = new stdClass();
d68c527f 148 $popup_btn->type = 'popup';
149 $popup_btn->url = $this->flickr->auth();
150 $ret['login'] = array($popup_btn);
151 return $ret;
5e98ab96 152 } else {
362a728f 153 echo '<a target="_blank" href="'.$this->flickr->auth().'">'.get_string('login', 'repository').'</a>';
6a391ebf 154 }
36ea9a6c 155 }
96297ca2 156
157 /**
6819c428 158 * Converts result received from phpFlickr::photo_search to Filepicker/repository format
96297ca2 159 *
6f2cd52a 160 * @param mixed $photos
6f2cd52a 161 * @return array
96297ca2 162 */
6819c428 163 private function build_list($photos) {
353d5cf3 164 $photos_url = $this->flickr->urls_getUserPhotos($this->nsid);
d05ef4a3 165 $ret = array();
166 $ret['manage'] = $photos_url;
167 $ret['list'] = array();
168 $ret['pages'] = $photos['pages'];
009a97ce 169 $ret['total'] = $photos['total'];
170 $ret['perpage'] = $photos['perpage'];
6819c428 171 $ret['page'] = $photos['page'];
dc0976b8 172 if (!empty($photos['photo'])) {
8988c36b 173 foreach ($photos['photo'] as $p) {
353d5cf3 174 if(empty($p['title'])) {
8988c36b 175 $p['title'] = get_string('notitle', 'repository_flickr');
176 }
177 if (isset($p['originalformat'])) {
178 $format = $p['originalformat'];
179 } else {
180 $format = 'jpg';
181 }
0e51661b 182 $format = '.'.$format;
183 // append extensions to the files
184 if (substr($p['title'], strlen($p['title'])-strlen($format)) != $format) {
4317f92f 185 $p['title'] .= $format;
0e51661b 186 }
187 $ret['list'][] = array('title'=>$p['title'],'source'=>$p['id'],
353d5cf3 188 'id'=>$p['id'],'thumbnail'=>$this->flickr->buildPhotoURL($p, 'Square'),
6819c428 189 'thumbnail_width'=>75, 'thumbnail_height'=>75,
353d5cf3 190 'date'=>'', 'size'=>'unknown', 'url'=>$photos_url.$p['id']);
733cfd80 191 }
6a391ebf 192 }
353d5cf3 193 return $ret;
194 }
96297ca2 195
196 /**
197 *
6f2cd52a 198 * @param string $search_text
6819c428 199 * @param int $page
6f2cd52a 200 * @return array
96297ca2 201 */
68a7c9a6 202 public function search($search_text, $page = 0) {
353d5cf3 203 $photos = $this->flickr->photos_search(array(
204 'user_id'=>$this->nsid,
8a77c949 205 'per_page'=>24,
353d5cf3 206 'extras'=>'original_format',
6819c428 207 'page'=>$page,
353d5cf3 208 'text'=>$search_text
209 ));
c438f190 210 $ret = $this->build_list($photos);
6819c428 211 $ret['list'] = array_filter($ret['list'], array($this, 'filter')); // TODO this breaks pagination
c438f190 212 return $ret;
353d5cf3 213 }
96297ca2 214
215 /**
216 *
5fa358d1 217 * @param string $path
218 * @param int $page
6f2cd52a 219 * @return array
96297ca2 220 */
6819c428
MG
221 public function get_listing($path = '', $page = '') {
222 return $this->search('', $page);
36ea9a6c 223 }
96297ca2 224
414ce308
DP
225 public function get_link($photo_id) {
226 global $CFG;
227 $result = $this->flickr->photos_getSizes($photo_id);
99d52655
DC
228 $url = '';
229 if(!empty($result[4])) {
230 $url = $result[4]['source'];
231 } elseif(!empty($result[3])) {
232 $url = $result[3]['source'];
233 } elseif(!empty($result[2])) {
234 $url = $result[2]['source'];
235 }
236 return $url;
237 }
238
96297ca2 239 /**
240 *
414ce308 241 * @param string $photo_id
6f2cd52a
DC
242 * @param string $file
243 * @return string
96297ca2 244 */
414ce308
DP
245 public function get_file($photo_id, $file = '') {
246 global $CFG;
247 $result = $this->flickr->photos_getSizes($photo_id);
248 $url = '';
249 if(!empty($result[4])) {
250 $url = $result[4]['source'];
251 } elseif(!empty($result[3])) {
252 $url = $result[3]['source'];
253 } elseif(!empty($result[2])) {
254 $url = $result[2]['source'];
255 }
a53d4f45 256 $path = $this->prepare_file($file);
257 $fp = fopen($path, 'w');
6a391ebf 258 $c = new curl;
259 $c->download(array(
260 array('url'=>$url, 'file'=>$fp)
261 ));
1dce6261 262 return array('path'=>$path, 'url'=>$url);
6a391ebf 263 }
a6600395 264
96297ca2 265 /**
792ad3aa 266 * Add Plugin settings input to Moodle form
6f2cd52a 267 * @param object $mform
96297ca2 268 */
68a7c9a6 269 public static function type_config_form($mform, $classname = 'repository') {
7fab56b7 270 global $CFG;
336bb44b 271 $api_key = get_config('flickr', 'api_key');
7892948d 272 $secret = get_config('flickr', 'secret');
273
336bb44b 274 if (empty($api_key)) {
275 $api_key = '';
276 }
0868f619 277 if (empty($secret)) {
278 $secret = '';
279 }
7892948d 280
4a65c39a 281 $strrequired = get_string('required');
0868f619 282 $mform->addElement('text', 'api_key', get_string('apikey', 'repository_flickr'), array('value'=>$api_key,'size' => '40'));
283 $mform->addElement('text', 'secret', get_string('secret', 'repository_flickr'), array('value'=>$secret,'size' => '40'));
e49eaacb 284
285 //retrieve the flickr instances
6f2cd52a
DC
286 $params = array();
287 $params['context'] = array();
288 //$params['currentcontext'] = $this->context;
289 $params['onlyvisible'] = false;
290 $params['type'] = 'flickr';
291 $instances = repository::get_instances($params);
e49eaacb 292 if (empty($instances)) {
6f2cd52a
DC
293 $callbackurl = get_string('callbackwarning', 'repository_flickr');
294 $mform->addElement('static', null, '', $callbackurl);
295 } else {
b38b6132 296 $instance = array_shift($instances);
64f93798 297 $callbackurl = $CFG->wwwroot.'/repository/repository_callback.php?repo_id='.$instance->id;
6f2cd52a 298 $mform->addElement('static', 'callbackurl', '', get_string('callbackurltext', 'repository_flickr', $callbackurl));
e49eaacb 299 }
dbc01944 300
4a65c39a 301 $mform->addRule('api_key', $strrequired, 'required', null, 'client');
0868f619 302 $mform->addRule('secret', $strrequired, 'required', null, 'client');
4a65c39a 303 }
0868f619 304
96297ca2 305 /**
792ad3aa 306 * Names of the plugin settings
6f2cd52a 307 * @return array
96297ca2 308 */
1b79955a 309 public static function get_type_option_names() {
a5adfa26 310 return array('api_key', 'secret', 'pluginname');
4a65c39a 311 }
014c1ca0 312 public function supported_filetypes() {
313 return array('web_image');
314 }
41076c58
DC
315 public function supported_returntypes() {
316 return (FILE_INTERNAL | FILE_EXTERNAL);
317 }
8a77c949 318}