PageRenderTime 52ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/portfolio/flickr/lib.php

https://bitbucket.org/moodle/moodle
PHP | 313 lines | 216 code | 54 blank | 43 comment | 20 complexity | b222f1762bb400b956f4269a3226d32b MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, BSD-3-Clause, MIT, GPL-3.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. * @package portfolio
  18. * @subpackage flickr
  19. * @copyright 2008 Nicolas Connault
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. */
  22. defined('MOODLE_INTERNAL') || die();
  23. require_once($CFG->libdir.'/portfolio/plugin.php');
  24. require_once($CFG->libdir.'/filelib.php');
  25. require_once($CFG->libdir.'/flickrclient.php');
  26. class portfolio_plugin_flickr extends portfolio_plugin_push_base {
  27. /** @var flickr_client */
  28. private $flickr;
  29. private $raw_sets;
  30. public function supported_formats() {
  31. return array(PORTFOLIO_FORMAT_IMAGE);
  32. }
  33. public static function get_name() {
  34. return get_string('pluginname', 'portfolio_flickr');
  35. }
  36. public function prepare_package() {
  37. }
  38. public function send_package() {
  39. foreach ($this->exporter->get_tempfiles() as $file) {
  40. // @TODO get max size from flickr people_getUploadStatus
  41. $filesize = $file->get_filesize();
  42. if ($this->is_valid_image($file)) {
  43. $photoid = $this->flickr->upload($file, [
  44. 'title' => $this->get_export_config('title'),
  45. 'description' => $this->get_export_config('description'),
  46. 'tags' => $this->get_export_config('tags'),
  47. 'is_public' => $this->get_export_config('is_public'),
  48. 'is_friend' => $this->get_export_config('is_friend'),
  49. 'is_family' => $this->get_export_config('is_family'),
  50. 'safety_level' => $this->get_export_config('safety_level'),
  51. 'content_type' => $this->get_export_config('content_type'),
  52. 'hidden' => $this->get_export_config('hidden'),
  53. ]);
  54. if ($photoid === false) {
  55. $this->set_user_config([
  56. 'accesstoken' => null,
  57. 'accesstokensecret' => null,
  58. ]);
  59. throw new portfolio_plugin_exception('uploadfailed', 'portfolio_flickr', '', 'Authentication failed');
  60. }
  61. // Attach photo to a set if requested.
  62. if ($this->get_export_config('set')) {
  63. $result = $this->flickr->call('photosets.addPhoto', [
  64. 'photoset_id' => $this->get_export_config('set'),
  65. 'photo_id' => $photoid,
  66. ], 'POST');
  67. }
  68. }
  69. }
  70. }
  71. public static function allows_multiple_instances() {
  72. return false;
  73. }
  74. public function get_interactive_continue_url() {
  75. return 'https://www.flickr.com/photos/organize';
  76. }
  77. public function expected_time($callertime) {
  78. return $callertime;
  79. }
  80. public static function get_allowed_config() {
  81. return array('apikey', 'sharedsecret');
  82. }
  83. public static function has_admin_config() {
  84. return true;
  85. }
  86. public static function admin_config_form(&$mform) {
  87. global $CFG;
  88. $strrequired = get_string('required');
  89. $mform->addElement('text', 'apikey', get_string('apikey', 'portfolio_flickr'), array('size' => 30));
  90. $mform->addRule('apikey', $strrequired, 'required', null, 'client');
  91. $mform->setType('apikey', PARAM_RAW_TRIMMED);
  92. $mform->addElement('text', 'sharedsecret', get_string('sharedsecret', 'portfolio_flickr'));
  93. $mform->addRule('sharedsecret', $strrequired, 'required', null, 'client');
  94. $mform->setType('sharedsecret', PARAM_RAW_TRIMMED);
  95. $a = new stdClass();
  96. $a->applyurl = 'http://www.flickr.com/services/api/keys/apply/';
  97. $a->keysurl = 'http://www.flickr.com/services/api/keys/';
  98. $a->callbackurl = $CFG->wwwroot . '/portfolio/add.php?postcontrol=1&type=flickr';
  99. $mform->addElement('static', 'setupinfo', get_string('setupinfo', 'portfolio_flickr'),
  100. get_string('setupinfodetails', 'portfolio_flickr', $a));
  101. }
  102. public function has_export_config() {
  103. return true;
  104. }
  105. public function get_allowed_user_config() {
  106. return array('accesstoken', 'accesstokensecret');
  107. }
  108. public function steal_control($stage) {
  109. if ($stage != PORTFOLIO_STAGE_CONFIG) {
  110. return false;
  111. }
  112. $accesstoken = $this->get_user_config('accesstoken');
  113. $accesstokensecret = $this->get_user_config('accesstokensecret');
  114. $callbackurl = new moodle_url('/portfolio/add.php', ['postcontrol' => 1, 'type' => 'flickr']);
  115. $this->flickr = new flickr_client($this->get_config('apikey'), $this->get_config('sharedsecret'), $callbackurl);
  116. if (!empty($accesstoken) && !empty($accesstokensecret)) {
  117. // The user has authenticated us already.
  118. $this->flickr->set_access_token($accesstoken, $accesstokensecret);
  119. return false;
  120. }
  121. $reqtoken = $this->flickr->request_token();
  122. $this->flickr->set_request_token_secret(['caller' => 'portfolio_flickr'], $reqtoken['oauth_token_secret']);
  123. $authurl = new moodle_url($reqtoken['authorize_url'], ['perms' => 'write']);
  124. return $authurl->out(false);
  125. }
  126. public function post_control($stage, $params) {
  127. if ($stage != PORTFOLIO_STAGE_CONFIG) {
  128. return;
  129. }
  130. if (empty($params['oauth_token']) || empty($params['oauth_verifier'])) {
  131. throw new portfolio_plugin_exception('noauthtoken', 'portfolio_flickr');
  132. }
  133. $callbackurl = new moodle_url('/portfolio/add.php', ['postcontrol' => 1, 'type' => 'flickr']);
  134. $this->flickr = new flickr_client($this->get_config('apikey'), $this->get_config('sharedsecret'), $callbackurl);
  135. $secret = $this->flickr->get_request_token_secret(['caller' => 'portfolio_flickr']);
  136. // Exchange the request token for the access token.
  137. $accesstoken = $this->flickr->get_access_token($params['oauth_token'], $secret, $params['oauth_verifier']);
  138. // Store the access token and the access token secret as the user
  139. // config so that we can use it on behalf of the user in next exports.
  140. $this->set_user_config([
  141. 'accesstoken' => $accesstoken['oauth_token'],
  142. 'accesstokensecret' => $accesstoken['oauth_token_secret'],
  143. ]);
  144. }
  145. public function export_config_form(&$mform) {
  146. $mform->addElement('text', 'plugin_title', get_string('title', 'portfolio_flickr'));
  147. $mform->setType('plugin_title', PARAM_TEXT);
  148. $mform->addElement('textarea', 'plugin_description', get_string('description'));
  149. $mform->setType('plugin_description', PARAM_CLEANHTML);
  150. $mform->addElement('text', 'plugin_tags', get_string('tags'));
  151. $mform->setType('plugin_tags', PARAM_TAGLIST);
  152. $mform->addElement('checkbox', 'plugin_is_public', get_string('ispublic', 'portfolio_flickr'));
  153. $mform->addElement('checkbox', 'plugin_is_family', get_string('isfamily', 'portfolio_flickr'));
  154. $mform->addElement('checkbox', 'plugin_is_friend', get_string('isfriend', 'portfolio_flickr'));
  155. $mform->disabledIf('plugin_is_friend', 'plugin_is_public', 'checked');
  156. $mform->disabledIf('plugin_is_family', 'plugin_is_public', 'checked');
  157. $safety_levels = array(1 => $this->get_export_value_name('safety_level', 1),
  158. 2 => $this->get_export_value_name('safety_level', 2),
  159. 3 => $this->get_export_value_name('safety_level', 3));
  160. $content_types = array(1 => $this->get_export_value_name('content_type', 1),
  161. 2 => $this->get_export_value_name('content_type', 2),
  162. 3 => $this->get_export_value_name('content_type', 3));
  163. $hidden_values = array(1,2);
  164. $mform->addElement('select', 'plugin_safety_level', get_string('safetylevel', 'portfolio_flickr'), $safety_levels);
  165. $mform->addElement('select', 'plugin_content_type', get_string('contenttype', 'portfolio_flickr'), $content_types);
  166. $mform->addElement('advcheckbox', 'plugin_hidden', get_string('hidefrompublicsearches', 'portfolio_flickr'), get_string('yes'), null, $hidden_values);
  167. $mform->setDefaults(array('plugin_is_public' => true));
  168. $rawsets = $this->get_sets();
  169. if (!empty($rawsets)) {
  170. $sets = array('0' => '----');
  171. foreach ($rawsets as $key => $value) {
  172. $sets[$key] = $value;
  173. }
  174. $mform->addElement('select', 'plugin_set', get_string('set', 'portfolio_flickr'), $sets);
  175. }
  176. }
  177. /**
  178. * Fetches a list of current user's photosets (albums) on flickr.
  179. *
  180. * @return array (int)id => (string)title
  181. */
  182. private function get_sets() {
  183. if (empty($this->raw_sets)) {
  184. $this->raw_sets = $this->flickr->call('photosets.getList');
  185. }
  186. if ($this->raw_sets === false) {
  187. // Authentication failed, drop the locally stored token to force re-authentication.
  188. $this->set_user_config([
  189. 'accesstoken' => null,
  190. 'accesstokensecret' => null,
  191. ]);
  192. return array();
  193. }
  194. $sets = array();
  195. foreach ($this->raw_sets->photosets->photoset as $set) {
  196. $sets[$set->id] = $set->title->_content;
  197. }
  198. return $sets;
  199. }
  200. public function get_allowed_export_config() {
  201. return array('set', 'title', 'description', 'tags', 'is_public', 'is_family', 'is_friend', 'safety_level', 'content_type', 'hidden');
  202. }
  203. public function get_export_summary() {
  204. return array(get_string('set', 'portfolio_flickr') => $this->get_export_value_name('set', $this->get_export_config('set')),
  205. get_string('title', 'portfolio_flickr') => $this->get_export_config('title'),
  206. get_string('description') => $this->get_export_config('description'),
  207. get_string('tags') => $this->get_export_config('tags'),
  208. get_string('ispublic', 'portfolio_flickr') => $this->get_export_value_name('is_public', $this->get_export_config('is_public')),
  209. get_string('isfamily', 'portfolio_flickr') => $this->get_export_value_name('is_family', $this->get_export_config('is_family')),
  210. get_string('isfriend', 'portfolio_flickr') => $this->get_export_value_name('is_friend', $this->get_export_config('is_friend')),
  211. get_string('safetylevel', 'portfolio_flickr') => $this->get_export_value_name('safety_level', $this->get_export_config('safety_level')),
  212. get_string('contenttype', 'portfolio_flickr') => $this->get_export_value_name('content_type', $this->get_export_config('content_type')),
  213. get_string('hidefrompublicsearches', 'portfolio_flickr') => $this->get_export_value_name('hidden', $this->get_export_config('hidden')));
  214. }
  215. private function get_export_value_name($param, $value) {
  216. $params = array('set' => $this->get_sets(),
  217. 'is_public' => array(0 => get_string('no'), 1 => get_string('yes')),
  218. 'is_family' => array(0 => get_string('no'), 1 => get_string('yes')),
  219. 'is_friend' => array(0 => get_string('no'), 1 => get_string('yes')),
  220. 'safety_level' => array(1 => get_string('safe', 'portfolio_flickr'),
  221. 2 => get_string('moderate', 'portfolio_flickr'),
  222. 3 => get_string('restricted', 'portfolio_flickr')),
  223. 'content_type' => array(1 => get_string('photo', 'portfolio_flickr'),
  224. 2 => get_string('screenshot', 'portfolio_flickr'),
  225. 3 => get_string('other', 'portfolio_flickr')),
  226. 'hidden' => array(1 => get_string('no'), 2 => get_string('yes')));
  227. if (isset($params[$param][$value])) {
  228. return $params[$param][$value];
  229. } else {
  230. return '-';
  231. }
  232. }
  233. /**
  234. * For now, flickr doesn't support this because we can't dynamically construct callbackurl
  235. */
  236. public static function allows_multiple_exports() {
  237. return false;
  238. }
  239. /**
  240. * Verifies the file is a valid optimised image - gif, png and jpeg only.
  241. * Currently, Flickr only supports these file types.
  242. *
  243. * @param stored_file $file
  244. * @return bool true if the file is ok
  245. */
  246. private function is_valid_image(stored_file $file): bool {
  247. $mimetype = $file->get_mimetype();
  248. if (!file_mimetype_in_typegroup($mimetype, 'optimised_image')) {
  249. return false;
  250. }
  251. if (!$info = $file->get_imageinfo()) {
  252. return false;
  253. }
  254. if ($info['mimetype'] !== $mimetype) {
  255. return false;
  256. }
  257. return true;
  258. }
  259. }