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

/repository/flickr/lib.php

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