MDL-33468 - fix illegal whitespace
[moodle.git] / repository / flickr_public / lib.php
CommitLineData
539b3d13 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
67233725
DC
17/**
18 * This plugin is used to access flickr pictures
19 *
20 * @since 2.0
21 * @package repository_flickr_public
22 * @copyright 2010 Dongsheng Cai {@link http://dongsheng.org}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25require_once($CFG->dirroot . '/repository/lib.php');
26require_once($CFG->libdir.'/flickrlib.php');
27require_once(dirname(__FILE__) . '/image.php');
28
539b3d13 29/**
30 * repository_flickr_public class
31 * This one is used to create public repository
49d20def
DC
32 * You can set up a public account in admin page, so everyone can access
33 * flickr photos from this plugin
539b3d13 34 *
10d53fd3 35 * @since 2.0
67233725
DC
36 * @package repository_flickr_public
37 * @copyright 2009 Dongsheng Cai {@link http://dongsheng.org}
d078f6d3 38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
539b3d13 39 */
dc0976b8 40class repository_flickr_public extends repository {
539b3d13 41 private $flickr;
42 public $photos;
43
49d20def
DC
44 /**
45 * constructor method
46 *
47 * @global object $CFG
48 * @global object $SESSION
49 * @param int $repositoryid
50 * @param int $context
51 * @param array $options
52 * @param boolean $readonly
53 */
54 public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array(), $readonly=0) {
55 global $CFG, $SESSION;
56 parent::__construct($repositoryid, $context, $options,$readonly);
57 $this->api_key = $this->get_option('api_key');
58 $this->flickr = new phpFlickr($this->api_key);
59 $this->flickr_account = $this->get_option('email_address');
2c40911a 60 $this->usewatermarks = $this->get_option('usewatermarks');
49d20def
DC
61
62 $account = optional_param('flickr_account', '', PARAM_RAW);
63 $fulltext = optional_param('flickr_fulltext', '', PARAM_RAW);
64 if (empty($fulltext)) {
65 $fulltext = optional_param('s', '', PARAM_RAW);
66 }
67 $tag = optional_param('flickr_tag', '', PARAM_RAW);
68 $license = optional_param('flickr_license', '', PARAM_RAW);
69
70 $this->sess_account = 'flickr_public_'.$this->id.'_account';
71 $this->sess_tag = 'flickr_public_'.$this->id.'_tag';
72 $this->sess_text = 'flickr_public_'.$this->id.'_text';
73
74 if (!empty($account) or !empty($fulltext) or !empty($tag) or !empty($license)) {
75 $SESSION->{$this->sess_tag} = $tag;
76 $SESSION->{$this->sess_text} = $fulltext;
77 $SESSION->{$this->sess_account} = $account;
78 }
79 }
80
96297ca2 81 /**
6b2a6c90 82 * save api_key in config table
83 * @param array $options
84 * @return boolean
96297ca2 85 */
dc0976b8 86 public function set_option($options = array()) {
539b3d13 87 if (!empty($options['api_key'])) {
88 set_config('api_key', trim($options['api_key']), 'flickr_public');
89 }
90 unset($options['api_key']);
99eaca9d 91 return parent::set_option($options);
539b3d13 92 }
93
96297ca2 94 /**
6b2a6c90 95 * get api_key from config table
99eaca9d 96 *
6b2a6c90 97 * @param string $config
98 * @return mixed
96297ca2 99 */
dc0976b8 100 public function get_option($config = '') {
101 if ($config==='api_key') {
539b3d13 102 return trim(get_config('flickr_public', 'api_key'));
103 } else {
104 $options['api_key'] = trim(get_config('flickr_public', 'api_key'));
105 }
99eaca9d 106 return parent::get_option($config);
539b3d13 107 }
108
96297ca2 109 /**
6b2a6c90 110 * is global_search available?
99eaca9d 111 *
6b2a6c90 112 * @return boolean
96297ca2 113 */
dc0976b8 114 public function global_search() {
539b3d13 115 if (empty($this->flickr_account)) {
116 return false;
117 } else {
118 return true;
119 }
120 }
121
96297ca2 122 /**
49d20def 123 * check if flickr account
6b2a6c90 124 * @return boolean
96297ca2 125 */
dc0976b8 126 public function check_login() {
d68c527f 127 return !empty($this->flickr_account);
128 }
96297ca2 129
130 /**
99eaca9d 131 * construct login form
96297ca2 132 *
6b2a6c90 133 * @param boolean $ajax
134 * @return array
96297ca2 135 */
78ff2983 136 public function print_login() {
137 if ($this->options['ajax']) {
d68c527f 138 $ret = array();
6bdfef5d 139 $fulltext = new stdClass();
6b2a6c90 140 $fulltext->label = get_string('fulltext', 'repository_flickr_public').': ';
141 $fulltext->id = 'el_fulltext';
142 $fulltext->type = 'text';
143 $fulltext->name = 'flickr_fulltext';
144
6bdfef5d 145 $tag = new stdClass();
6b2a6c90 146 $tag->label = get_string('tag', 'repository_flickr_public').': ';
147 $tag->id = 'el_tag';
148 $tag->type = 'text';
149 $tag->name = 'flickr_tag';
150
6bdfef5d 151 $email_field = new stdClass();
ca0174cc 152 $email_field->label = get_string('username', 'repository_flickr_public').': ';
153 $email_field->id = 'account';
154 $email_field->type = 'text';
155 $email_field->name = 'flickr_account';
45983072 156
6bdfef5d 157 $commercial = new stdClass();
16e9dccd 158 $commercial->label = get_string('commercialuse', 'repository_flickr_public').': ';
159 $commercial->id = 'flickr_commercial_id';
640bb8c1 160 $commercial->type = 'checkbox';
16e9dccd 161 $commercial->name = 'flickr_commercial';
640bb8c1 162 $commercial->value = 'yes';
9f50fb5f 163
6bdfef5d 164 $modification = new stdClass();
16e9dccd 165 $modification->label = get_string('modification', 'repository_flickr_public').': ';
166 $modification->id = 'flickr_modification_id';
640bb8c1 167 $modification->type = 'checkbox';
16e9dccd 168 $modification->name = 'flickr_modification';
640bb8c1 169 $modification->value = 'yes';
16e9dccd 170
171 $ret['login'] = array($fulltext, $tag, $email_field, $commercial, $modification);
45983072 172 $ret['login_btn_label'] = get_string('search');
da2946c9 173 $ret['login_btn_action'] = 'search';
d68c527f 174 return $ret;
78ff2983 175 } else {
176 echo '<table>';
177 echo '<tr><td><label>'.get_string('fulltext', 'repository_flickr_public').'</label></td>';
178 echo '<td><input type="text" name="flickr_fulltext" /></td></tr>';
179 echo '<tr><td><label>'.get_string('tag', 'repository_flickr_public').'</label></td>';
180 echo '<td><input type="text" name="flickr_tag" /></td></tr>';
181 echo '<tr><td><label>'.get_string('username', 'repository_flickr_public').'</label></td>';
182 echo '<td><input type="text" name="flickr_account" /></td></tr>';
183
16e9dccd 184 echo '<tr><td><label>'.get_string('commercialuse', 'repository_flickr_public').'</label></td>';
78ff2983 185 echo '<td>';
640bb8c1 186 echo '<input type="checkbox" name="flickr_commercial" value="yes" />';
16e9dccd 187 echo '</td></tr>';
188
189 echo '<tr><td><label>'.get_string('modification', 'repository_flickr_public').'</label></td>';
190 echo '<td>';
640bb8c1 191 echo '<input type="checkbox" name="flickr_modification" value="yes" />';
78ff2983 192 echo '</td></tr>';
193
194 echo '</table>';
195
196 echo '<input type="hidden" name="action" value="search" />';
362a728f 197 echo '<input type="submit" value="'.get_string('search', 'repository').'" />';
539b3d13 198 }
199 }
96297ca2 200
aa48f05d 201 /**
99eaca9d 202 * destroy session
aa48f05d 203 *
99eaca9d 204 * @return object
aa48f05d 205 */
206 public function logout() {
6b2a6c90 207 global $SESSION;
208 unset($SESSION->{$this->sess_tag});
209 unset($SESSION->{$this->sess_text});
210 unset($SESSION->{$this->sess_account});
aa48f05d 211 return $this->print_login();
212 }
213
1dce6261
DC
214 public function license4moodle ($license_id) {
215 $license = array(
216 '1' => 'cc-nc-sa',
217 '2' => 'cc-nc',
218 '3' => 'cc-nc-nd',
219 '4' => 'cc',
220 '5' => 'cc-sa',
221 '6' => 'cc-nd',
222 '7' => 'allrightsreserved'
223 );
224 return $license[$license_id];
225 }
226
96297ca2 227 /**
99eaca9d 228 * search images on flickr
96297ca2 229 *
99eaca9d
DC
230 * @param string $search_text
231 * @return array
96297ca2 232 */
68a7c9a6 233 public function search($search_text, $page = 0) {
86e68c2d 234 global $SESSION;
d1bfc05e 235 $ret = array();
99eaca9d
DC
236 if (empty($page)) {
237 $page = 1;
238 }
239
6b2a6c90 240 if (!empty($this->flickr_account)) {
241 $people = $this->flickr->people_findByEmail($this->flickr_account);
242 $this->nsid = $people['nsid'];
243 }
244 if (!empty($SESSION->{$this->sess_account})) {
245 $people = $this->flickr->people_findByEmail($SESSION->{$this->sess_account});
246 $this->nsid = $people['nsid'];
247 }
248 if (empty($this->nsid)) {
249 $this->nsid = null;
d1bfc05e 250 // user specify a flickr account, but it is not valid
251 if (!empty($this->flickr_account) or !empty($SESSION->{$this->sess_account})) {
252 $ret['e'] = get_string('invalidemail', 'repository_flickr_public');
253 return $ret;
254 }
6b2a6c90 255 }
99eaca9d 256
16e9dccd 257 // including all licenses by default
258 $licenses = array(1=>1, 2, 3, 4, 5, 6, 7);
259
260 $commercial = optional_param('flickr_commercial', '', PARAM_RAW);
261 $modification = optional_param('flickr_modification', '', PARAM_RAW);
262
263 if ($commercial == 'yes') {
264 // including
265 // 4: Attribution License
266 // 5: Attribution ShareAlike
267 // 6: Attribution NoDerives
268 // 7: unknown license
269 unset($licenses[1], $licenses[2], $licenses[3]);
270 }
271 if ($modification == 'yes') {
272 // including
273 // 1: Attribution NonCommercial ShareAlike
274 // 2: Attribution NonCommercial
275 // 4: Attribution License
276 // 5: Attribution ShareAlike
277 // 7: unknown license
278 unset($licenses[3], $licenses[6]);
279 }
640bb8c1 280 //if ($modification == 'sharealike') {
16e9dccd 281 // including
282 // 1: Attribution NonCommercial ShareAlike
283 // 5: Attribution ShareAlike
640bb8c1
MD
284 //unset($licenses[2], $licenses[3], $licenses[4], $licenses[6], $licenses[7]);
285 //}
16e9dccd 286
287 $licenses = implode(',', $licenses);
288
da78b10c
DC
289 $tag = !empty($SESSION->{$this->sess_tag}) ? $SESSION->{$this->sess_tag} : null;
290 $text = !empty($SESSION->{$this->sess_text}) ? $SESSION->{$this->sess_text} : null;
291 $nsid = !empty($this->nsid) ? $this->nsid : null;
292
293 $photos = $this->flickr->photos_search(array(
294 'tags'=>$tag,
295 'page'=>$page,
296 'per_page'=>24,
297 'user_id'=>$nsid,
298 'license'=>$licenses,
299 'text'=>$text
300 )
301 );
009a97ce 302 $ret['total'] = $photos['total'];
303 $ret['perpage'] = $photos['perpage'];
9f50fb5f 304 if (empty($photos)) {
305 $ret['list'] = array();
306 return $ret;
307 }
c438f190 308 $ret = $this->build_list($photos, $page, $ret);
309 $ret['list'] = array_filter($ret['list'], array($this, 'filter'));
310 return $ret;
fbd508b4 311 }
96297ca2 312
313 /**
99eaca9d 314 * return an image list
96297ca2 315 *
5fa358d1 316 * @param string $path
317 * @param int $page
99eaca9d 318 * @return array
96297ca2 319 */
5fa358d1 320 public function get_listing($path = '', $page = 1) {
539b3d13 321 $people = $this->flickr->people_findByEmail($this->flickr_account);
353d5cf3 322 $this->nsid = $people['nsid'];
5fa358d1 323 $photos = $this->flickr->people_getPublicPhotos($people['nsid'], 'original_format', 24, $page);
86e68c2d 324 $ret = array();
539b3d13 325
5fa358d1 326 return $this->build_list($photos, $page, $ret);
353d5cf3 327 }
96297ca2 328
329 /**
99eaca9d 330 * build an image list
96297ca2 331 *
99eaca9d
DC
332 * @param array $photos
333 * @param int $page
334 * @return array
96297ca2 335 */
5fa358d1 336 private function build_list($photos, $page = 1, &$ret) {
6b2a6c90 337 if (!empty($this->nsid)) {
338 $photos_url = $this->flickr->urls_getUserPhotos($this->nsid);
339 $ret['manage'] = $photos_url;
340 }
539b3d13 341 $ret['list'] = array();
fdfb9cbe
DC
342 $ret['nosearch'] = true;
343 $ret['norefresh'] = true;
344 $ret['logouttext'] = get_string('backtosearch', 'repository_flickr_public');
539b3d13 345 $ret['pages'] = $photos['pages'];
5fa358d1 346 if (is_int($page) && $page <= $ret['pages']) {
347 $ret['page'] = $page;
539b3d13 348 } else {
349 $ret['page'] = 1;
350 }
353d5cf3 351 if (!empty($photos['photo'])) {
352 foreach ($photos['photo'] as $p) {
353 if(empty($p['title'])) {
354 $p['title'] = get_string('notitle', 'repository_flickr');
355 }
356 if (isset($p['originalformat'])) {
357 $format = $p['originalformat'];
358 } else {
359 $format = 'jpg';
360 }
0e51661b 361 $format = '.'.$format;
0e51661b 362 if (substr($p['title'], strlen($p['title'])-strlen($format)) != $format) {
d1bfc05e 363 // append author id
5fdc39c7 364 // $p['title'] .= '-'.$p['owner'];
d1bfc05e 365 // append file extension
4317f92f 366 $p['title'] .= $format;
0e51661b 367 }
1dce6261
DC
368 $ret['list'][] = array(
369 'title'=>$p['title'],
370 'source'=>$p['id'],
371 'id'=>$p['id'],
372 'thumbnail'=>$this->flickr->buildPhotoURL($p, 'Square'),
373 'date'=>'',
374 'size'=>'unknown',
375 'url'=>'http://www.flickr.com/photos/'.$p['owner'].'/'.$p['id'],
376 'haslicense'=>true,
377 'hasauthor'=>true
378 );
539b3d13 379 }
539b3d13 380 }
353d5cf3 381 return $ret;
539b3d13 382 }
dc0976b8 383
96297ca2 384 /**
99d52655 385 * Print a search form
96297ca2 386 *
99d52655 387 * @return string
96297ca2 388 */
dc0976b8 389 public function print_search() {
b763c2d9 390 $str = '';
391 $str .= '<input type="hidden" name="repo_id" value="'.$this->id.'" />';
392 $str .= '<input type="hidden" name="ctx_id" value="'.$this->context->id.'" />';
393 $str .= '<input type="hidden" name="seekey" value="'.sesskey().'" />';
394 $str .= '<label>'.get_string('fulltext', 'repository_flickr_public').': </label><br/><input name="s" value="" /><br/>';
1238246b 395 $str .= '<label>'.get_string('tag', 'repository_flickr_public').'</label><br /><input type="text" name="flickr_tag" /><br />';
b763c2d9 396 return $str;
539b3d13 397 }
dc0976b8 398
414ce308
DP
399 public function get_link($photo_id) {
400 global $CFG;
401 $result = $this->flickr->photos_getSizes($photo_id);
99d52655
DC
402 $url = '';
403 if(!empty($result[4])) {
404 $url = $result[4]['source'];
405 } elseif(!empty($result[3])) {
406 $url = $result[3]['source'];
407 } elseif(!empty($result[2])) {
408 $url = $result[2]['source'];
409 }
410 return $url;
411 }
412
96297ca2 413 /**
414 *
009a97ce 415 * @global object $CFG
414ce308 416 * @param string $photo_id
009a97ce 417 * @param string $file
418 * @return string
96297ca2 419 */
414ce308 420 public function get_file($photo_id, $file = '') {
539b3d13 421 global $CFG;
414ce308 422 $info = $this->flickr->photos_getInfo($photo_id);
2c40911a
DC
423 if ($info['owner']['realname']) {
424 $author = $info['owner']['realname'];
425 } else {
426 $author = $info['owner']['username'];
427 }
428 $copyright = get_string('author', 'repository') . ': ' . $author;
414ce308 429 $result = $this->flickr->photos_getSizes($photo_id);
1dce6261
DC
430 // download link
431 $source = '';
432 // flickr photo page
539b3d13 433 $url = '';
dc0976b8 434 if (!empty($result[4])) {
1dce6261
DC
435 $source = $result[4]['source'];
436 $url = $result[4]['url'];
353d5cf3 437 } elseif(!empty($result[3])) {
1dce6261
DC
438 $source = $result[3]['source'];
439 $url = $result[3]['url'];
353d5cf3 440 } elseif(!empty($result[2])) {
1dce6261
DC
441 $source = $result[2]['source'];
442 $url = $result[2]['url'];
539b3d13 443 }
a53d4f45 444 $path = $this->prepare_file($file);
445 $fp = fopen($path, 'w');
539b3d13 446 $c = new curl;
1dce6261 447 $c->download(array(array('url'=>$source, 'file'=>$fp)));
2c40911a
DC
448 // must close file handler, otherwise gd lib will fail to process it
449 fclose($fp);
450 if (!empty($this->usewatermarks)) {
451 $img = new moodle_image($path);
452 $img->watermark($copyright, array(10,10), array('ttf'=>true, 'fontsize'=>12))->saveas($path);
453 }
1dce6261
DC
454
455 return array('path'=>$path, 'url'=>$url, 'author'=>$info['owner']['realname'], 'license'=>$this->license4moodle($info['license']));
539b3d13 456 }
539b3d13 457
96297ca2 458 /**
792ad3aa 459 * Add Instance settings input to Moodle form
99eaca9d 460 * @param object $mform
96297ca2 461 */
4a126f17 462 public static function instance_config_form($mform) {
b8f46e17 463 $mform->addElement('text', 'email_address', get_string('emailaddress', 'repository_flickr_public'));
2c40911a 464 $mform->addElement('checkbox', 'usewatermarks', get_string('watermark', 'repository_flickr_public'));
d55846d8 465 $mform->setDefault('usewatermarks', 0);
b8f46e17 466 }
467
96297ca2 468 /**
792ad3aa 469 * Names of the instance settings
99eaca9d 470 * @return array
96297ca2 471 */
b8f46e17 472 public static function get_instance_option_names() {
2c40911a 473 return array('email_address', 'usewatermarks');
539b3d13 474 }
475
96297ca2 476 /**
792ad3aa 477 * Add Plugin settings input to Moodle form
99eaca9d 478 * @param object $mform
96297ca2 479 */
68a7c9a6 480 public static function type_config_form($mform, $classname = 'repository') {
539b3d13 481 $api_key = get_config('flickr_public', 'api_key');
482 if (empty($api_key)) {
483 $api_key = '';
484 }
485 $strrequired = get_string('required');
d1bfc05e 486
539b3d13 487 $mform->addElement('text', 'api_key', get_string('apikey', 'repository_flickr_public'), array('value'=>$api_key,'size' => '40'));
539b3d13 488 $mform->addRule('api_key', $strrequired, 'required', null, 'client');
d1bfc05e 489
e49eaacb 490 $mform->addElement('static', null, '', get_string('information','repository_flickr_public'));
539b3d13 491 }
b8f46e17 492
96297ca2 493 /**
792ad3aa 494 * Names of the plugin settings
49d20def 495 * @return array
96297ca2 496 */
1b79955a 497 public static function get_type_option_names() {
a5adfa26 498 return array('api_key', 'pluginname');
b8f46e17 499 }
500
96297ca2 501 /**
83a018ed 502 * is run when moodle administrator add the plugin
96297ca2 503 */
948c2860 504 public static function plugin_init() {
e49eaacb 505 //here we create a default instance for this type
539b3d13 506
d55846d8 507 $id = repository::static_function('flickr_public','create', 'flickr_public', 0, get_system_context(), array('name'=>'', 'email_address' => null, 'usewatermarks' => false), 0);
60c366e8 508 if (empty($id)) {
509 return false;
510 } else {
511 return true;
512 }
dbc01944 513 }
014c1ca0 514 public function supported_filetypes() {
515 return array('web_image');
516 }
41076c58
DC
517 public function supported_returntypes() {
518 return (FILE_INTERNAL | FILE_EXTERNAL);
519 }
539b3d13 520}