PageRenderTime 54ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/moodle/repository/flickr/lib.php

#
PHP | 317 lines | 194 code | 28 blank | 95 comment | 22 complexity | 6860f9708f6366ebf523090f89629c20 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1, BSD-3-Clause, AGPL-3.0, MPL-2.0-no-copyleft-exception, LGPL-3.0, Apache-2.0
  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.
  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. /**
  17. * repository_flickr class
  18. * This plugin is used to access user's private flickr repository
  19. *
  20. * @since 2.0
  21. * @package repository
  22. * @subpackage flickr
  23. * @copyright 2009 Dongsheng Cai
  24. * @author Dongsheng Cai <dongsheng@moodle.com>
  25. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26. */
  27. require_once($CFG->libdir.'/flickrlib.php');
  28. /**
  29. *
  30. */
  31. class repository_flickr extends repository {
  32. private $flickr;
  33. public $photos;
  34. /**
  35. *
  36. * @param int $repositoryid
  37. * @param object $context
  38. * @param array $options
  39. */
  40. public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array()) {
  41. global $SESSION, $CFG;
  42. $options['page'] = optional_param('p', 1, PARAM_INT);
  43. parent::__construct($repositoryid, $context, $options);
  44. $this->setting = 'flickr_';
  45. $this->api_key = $this->get_option('api_key');
  46. $this->secret = $this->get_option('secret');
  47. $this->token = get_user_preferences($this->setting, '');
  48. $this->nsid = get_user_preferences($this->setting.'_nsid', '');
  49. $this->flickr = new phpFlickr($this->api_key, $this->secret, $this->token);
  50. $frob = optional_param('frob', '', PARAM_RAW);
  51. if (empty($this->token) && !empty($frob)) {
  52. $auth_info = $this->flickr->auth_getToken($frob);
  53. $this->token = $auth_info['token'];
  54. $this->nsid = $auth_info['user']['nsid'];
  55. set_user_preference($this->setting, $auth_info['token']);
  56. set_user_preference($this->setting.'_nsid', $auth_info['user']['nsid']);
  57. }
  58. }
  59. /**
  60. *
  61. * @return bool
  62. */
  63. public function check_login() {
  64. return !empty($this->token);
  65. }
  66. /**
  67. *
  68. * @return mixed
  69. */
  70. public function logout() {
  71. set_user_preference($this->setting, '');
  72. set_user_preference($this->setting.'_nsid', '');
  73. $this->token = '';
  74. $this->nsid = '';
  75. return $this->print_login();
  76. }
  77. /**
  78. *
  79. * @param array $options
  80. * @return mixed
  81. */
  82. public function set_option($options = array()) {
  83. if (!empty($options['api_key'])) {
  84. set_config('api_key', trim($options['api_key']), 'flickr');
  85. }
  86. if (!empty($options['secret'])) {
  87. set_config('secret', trim($options['secret']), 'flickr');
  88. }
  89. unset($options['api_key']);
  90. unset($options['secret']);
  91. $ret = parent::set_option($options);
  92. return $ret;
  93. }
  94. /**
  95. *
  96. * @param string $config
  97. * @return mixed
  98. */
  99. public function get_option($config = '') {
  100. if ($config==='api_key') {
  101. return trim(get_config('flickr', 'api_key'));
  102. } elseif ($config ==='secret') {
  103. return trim(get_config('flickr', 'secret'));
  104. } else {
  105. $options['api_key'] = trim(get_config('flickr', 'api_key'));
  106. $options['secret'] = trim(get_config('flickr', 'secret'));
  107. }
  108. $options = parent::get_option($config);
  109. return $options;
  110. }
  111. /**
  112. *
  113. * @return bool
  114. */
  115. public function global_search() {
  116. if (empty($this->token)) {
  117. return false;
  118. } else {
  119. return true;
  120. }
  121. }
  122. /**
  123. *
  124. * @return null
  125. */
  126. public function print_login() {
  127. if ($this->options['ajax']) {
  128. $ret = array();
  129. $popup_btn = new stdClass();
  130. $popup_btn->type = 'popup';
  131. $popup_btn->url = $this->flickr->auth();
  132. $ret['login'] = array($popup_btn);
  133. return $ret;
  134. } else {
  135. echo '<a target="_blank" href="'.$this->flickr->auth().'">'.get_string('login', 'repository').'</a>';
  136. }
  137. }
  138. /**
  139. * Converts result received from phpFlickr::photo_search to Filepicker/repository format
  140. *
  141. * @param mixed $photos
  142. * @return array
  143. */
  144. private function build_list($photos) {
  145. $photos_url = $this->flickr->urls_getUserPhotos($this->nsid);
  146. $ret = array();
  147. $ret['manage'] = $photos_url;
  148. $ret['list'] = array();
  149. $ret['pages'] = $photos['pages'];
  150. $ret['total'] = $photos['total'];
  151. $ret['perpage'] = $photos['perpage'];
  152. $ret['page'] = $photos['page'];
  153. if (!empty($photos['photo'])) {
  154. foreach ($photos['photo'] as $p) {
  155. if(empty($p['title'])) {
  156. $p['title'] = get_string('notitle', 'repository_flickr');
  157. }
  158. if (isset($p['originalformat'])) {
  159. $format = $p['originalformat'];
  160. } else {
  161. $format = 'jpg';
  162. }
  163. $format = '.'.$format;
  164. // append extensions to the files
  165. if (substr($p['title'], strlen($p['title'])-strlen($format)) != $format) {
  166. $p['title'] .= $format;
  167. }
  168. $ret['list'][] = array('title'=>$p['title'],'source'=>$p['id'],
  169. 'id'=>$p['id'],'thumbnail'=>$this->flickr->buildPhotoURL($p, 'Square'),
  170. 'thumbnail_width'=>75, 'thumbnail_height'=>75,
  171. 'date'=>'', 'size'=>'unknown', 'url'=>$photos_url.$p['id']);
  172. }
  173. }
  174. return $ret;
  175. }
  176. /**
  177. *
  178. * @param string $search_text
  179. * @param int $page
  180. * @return array
  181. */
  182. public function search($search_text, $page = 0) {
  183. $photos = $this->flickr->photos_search(array(
  184. 'user_id'=>$this->nsid,
  185. 'per_page'=>24,
  186. 'extras'=>'original_format',
  187. 'page'=>$page,
  188. 'text'=>$search_text
  189. ));
  190. $ret = $this->build_list($photos);
  191. $ret['list'] = array_filter($ret['list'], array($this, 'filter')); // TODO this breaks pagination
  192. return $ret;
  193. }
  194. /**
  195. *
  196. * @param string $path
  197. * @param int $page
  198. * @return array
  199. */
  200. public function get_listing($path = '', $page = '') {
  201. return $this->search('', $page);
  202. }
  203. public function get_link($photo_id) {
  204. global $CFG;
  205. $result = $this->flickr->photos_getSizes($photo_id);
  206. $url = '';
  207. if(!empty($result[4])) {
  208. $url = $result[4]['source'];
  209. } elseif(!empty($result[3])) {
  210. $url = $result[3]['source'];
  211. } elseif(!empty($result[2])) {
  212. $url = $result[2]['source'];
  213. }
  214. return $url;
  215. }
  216. /**
  217. *
  218. * @param string $photo_id
  219. * @param string $file
  220. * @return string
  221. */
  222. public function get_file($photo_id, $file = '') {
  223. global $CFG;
  224. $result = $this->flickr->photos_getSizes($photo_id);
  225. $url = '';
  226. if(!empty($result[4])) {
  227. $url = $result[4]['source'];
  228. } elseif(!empty($result[3])) {
  229. $url = $result[3]['source'];
  230. } elseif(!empty($result[2])) {
  231. $url = $result[2]['source'];
  232. }
  233. $path = $this->prepare_file($file);
  234. $fp = fopen($path, 'w');
  235. $c = new curl;
  236. $c->download(array(
  237. array('url'=>$url, 'file'=>$fp)
  238. ));
  239. return array('path'=>$path, 'url'=>$url);
  240. }
  241. /**
  242. * Add Plugin settings input to Moodle form
  243. * @param object $mform
  244. */
  245. public function type_config_form($mform) {
  246. global $CFG;
  247. $api_key = get_config('flickr', 'api_key');
  248. $secret = get_config('flickr', 'secret');
  249. if (empty($api_key)) {
  250. $api_key = '';
  251. }
  252. if (empty($secret)) {
  253. $secret = '';
  254. }
  255. $strrequired = get_string('required');
  256. $mform->addElement('text', 'api_key', get_string('apikey', 'repository_flickr'), array('value'=>$api_key,'size' => '40'));
  257. $mform->addElement('text', 'secret', get_string('secret', 'repository_flickr'), array('value'=>$secret,'size' => '40'));
  258. //retrieve the flickr instances
  259. $params = array();
  260. $params['context'] = array();
  261. //$params['currentcontext'] = $this->context;
  262. $params['onlyvisible'] = false;
  263. $params['type'] = 'flickr';
  264. $instances = repository::get_instances($params);
  265. if (empty($instances)) {
  266. $callbackurl = get_string('callbackwarning', 'repository_flickr');
  267. $mform->addElement('static', null, '', $callbackurl);
  268. } else {
  269. $instance = array_shift($instances);
  270. $callbackurl = $CFG->wwwroot.'/repository/repository_callback.php?repo_id='.$instance->id;
  271. $mform->addElement('static', 'callbackurl', '', get_string('callbackurltext', 'repository_flickr', $callbackurl));
  272. }
  273. $mform->addRule('api_key', $strrequired, 'required', null, 'client');
  274. $mform->addRule('secret', $strrequired, 'required', null, 'client');
  275. }
  276. /**
  277. * Names of the plugin settings
  278. * @return array
  279. */
  280. public static function get_type_option_names() {
  281. return array('api_key', 'secret', 'pluginname');
  282. }
  283. public function supported_filetypes() {
  284. return array('web_image');
  285. }
  286. public function supported_returntypes() {
  287. return (FILE_INTERNAL | FILE_EXTERNAL);
  288. }
  289. }