PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/src/application/libraries/Zend/Service/Flickr.php

https://bitbucket.org/masnug/grc276-blog-laravel
PHP | 625 lines | 263 code | 90 blank | 272 comment | 24 complexity | 578a0787d4ce5d29cce8d9f7256a4280 MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Service
  17. * @subpackage Flickr
  18. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: Flickr.php 23775 2011-03-01 17:25:24Z ralph $
  21. */
  22. /**
  23. * @category Zend
  24. * @package Zend_Service
  25. * @subpackage Flickr
  26. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  27. * @license http://framework.zend.com/license/new-bsd New BSD License
  28. */
  29. class Zend_Service_Flickr
  30. {
  31. /**
  32. * Base URI for the REST client
  33. */
  34. const URI_BASE = 'http://www.flickr.com';
  35. /**
  36. * Your Flickr API key
  37. *
  38. * @var string
  39. */
  40. public $apiKey;
  41. /**
  42. * Reference to REST client object
  43. *
  44. * @var Zend_Rest_Client
  45. */
  46. protected $_restClient = null;
  47. /**
  48. * Performs object initializations
  49. *
  50. * # Sets up character encoding
  51. * # Saves the API key
  52. *
  53. * @param string $apiKey Your Flickr API key
  54. * @return void
  55. */
  56. public function __construct($apiKey)
  57. {
  58. $this->apiKey = (string) $apiKey;
  59. }
  60. /**
  61. * Find Flickr photos by tag.
  62. *
  63. * Query options include:
  64. *
  65. * # per_page: how many results to return per query
  66. * # page: the starting page offset. first result will be (page - 1) * per_page + 1
  67. * # tag_mode: Either 'any' for an OR combination of tags,
  68. * or 'all' for an AND combination. Default is 'any'.
  69. * # min_upload_date: Minimum upload date to search on. Date should be a unix timestamp.
  70. * # max_upload_date: Maximum upload date to search on. Date should be a unix timestamp.
  71. * # min_taken_date: Minimum upload date to search on. Date should be a MySQL datetime.
  72. * # max_taken_date: Maximum upload date to search on. Date should be a MySQL datetime.
  73. *
  74. * @param string|array $query A single tag or an array of tags.
  75. * @param array $options Additional parameters to refine your query.
  76. * @return Zend_Service_Flickr_ResultSet
  77. * @throws Zend_Service_Exception
  78. */
  79. public function tagSearch($query, array $options = array())
  80. {
  81. static $method = 'flickr.photos.search';
  82. static $defaultOptions = array('per_page' => 10,
  83. 'page' => 1,
  84. 'tag_mode' => 'or',
  85. 'extras' => 'license, date_upload, date_taken, owner_name, icon_server');
  86. $options['tags'] = is_array($query) ? implode(',', $query) : $query;
  87. $options = $this->_prepareOptions($method, $options, $defaultOptions);
  88. $this->_validateTagSearch($options);
  89. // now search for photos
  90. $restClient = $this->getRestClient();
  91. $restClient->getHttpClient()->resetParameters();
  92. $response = $restClient->restGet('/services/rest/', $options);
  93. if ($response->isError()) {
  94. /**
  95. * @see Zend_Service_Exception
  96. */
  97. require_once 'Zend/Service/Exception.php';
  98. throw new Zend_Service_Exception('An error occurred sending request. Status code: '
  99. . $response->getStatus());
  100. }
  101. $dom = new DOMDocument();
  102. $dom->loadXML($response->getBody());
  103. self::_checkErrors($dom);
  104. /**
  105. * @see Zend_Service_Flickr_ResultSet
  106. */
  107. require_once 'Zend/Service/Flickr/ResultSet.php';
  108. return new Zend_Service_Flickr_ResultSet($dom, $this);
  109. }
  110. /**
  111. * Finds photos by a user's username or email.
  112. *
  113. * Additional query options include:
  114. *
  115. * # per_page: how many results to return per query
  116. * # page: the starting page offset. first result will be (page - 1) * per_page + 1
  117. * # min_upload_date: Minimum upload date to search on. Date should be a unix timestamp.
  118. * # max_upload_date: Maximum upload date to search on. Date should be a unix timestamp.
  119. * # min_taken_date: Minimum upload date to search on. Date should be a MySQL datetime.
  120. * # max_taken_date: Maximum upload date to search on. Date should be a MySQL datetime.
  121. *
  122. * @param string $query username or email
  123. * @param array $options Additional parameters to refine your query.
  124. * @return Zend_Service_Flickr_ResultSet
  125. * @throws Zend_Service_Exception
  126. */
  127. public function userSearch($query, array $options = null)
  128. {
  129. static $method = 'flickr.people.getPublicPhotos';
  130. static $defaultOptions = array('per_page' => 10,
  131. 'page' => 1,
  132. 'extras' => 'license, date_upload, date_taken, owner_name, icon_server');
  133. // can't access by username, must get ID first
  134. if (strchr($query, '@')) {
  135. // optimistically hope this is an email
  136. $options['user_id'] = $this->getIdByEmail($query);
  137. } else {
  138. // we can safely ignore this exception here
  139. $options['user_id'] = $this->getIdByUsername($query);
  140. }
  141. $options = $this->_prepareOptions($method, $options, $defaultOptions);
  142. $this->_validateUserSearch($options);
  143. // now search for photos
  144. $restClient = $this->getRestClient();
  145. $restClient->getHttpClient()->resetParameters();
  146. $response = $restClient->restGet('/services/rest/', $options);
  147. if ($response->isError()) {
  148. /**
  149. * @see Zend_Service_Exception
  150. */
  151. require_once 'Zend/Service/Exception.php';
  152. throw new Zend_Service_Exception('An error occurred sending request. Status code: '
  153. . $response->getStatus());
  154. }
  155. $dom = new DOMDocument();
  156. $dom->loadXML($response->getBody());
  157. self::_checkErrors($dom);
  158. /**
  159. * @see Zend_Service_Flickr_ResultSet
  160. */
  161. require_once 'Zend/Service/Flickr/ResultSet.php';
  162. return new Zend_Service_Flickr_ResultSet($dom, $this);
  163. }
  164. /**
  165. * Finds photos in a group's pool.
  166. *
  167. * @param string $query group id
  168. * @param array $options Additional parameters to refine your query.
  169. * @return Zend_Service_Flickr_ResultSet
  170. * @throws Zend_Service_Exception
  171. */
  172. public function groupPoolGetPhotos($query, array $options = array())
  173. {
  174. static $method = 'flickr.groups.pools.getPhotos';
  175. static $defaultOptions = array('per_page' => 10,
  176. 'page' => 1,
  177. 'extras' => 'license, date_upload, date_taken, owner_name, icon_server');
  178. if (empty($query) || !is_string($query)) {
  179. /**
  180. * @see Zend_Service_Exception
  181. */
  182. require_once 'Zend/Service/Exception.php';
  183. throw new Zend_Service_Exception('You must supply a group id');
  184. }
  185. $options['group_id'] = $query;
  186. $options = $this->_prepareOptions($method, $options, $defaultOptions);
  187. $this->_validateGroupPoolGetPhotos($options);
  188. // now search for photos
  189. $restClient = $this->getRestClient();
  190. $restClient->getHttpClient()->resetParameters();
  191. $response = $restClient->restGet('/services/rest/', $options);
  192. if ($response->isError()) {
  193. /**
  194. * @see Zend_Service_Exception
  195. */
  196. require_once 'Zend/Service/Exception.php';
  197. throw new Zend_Service_Exception('An error occurred sending request. Status code: '
  198. . $response->getStatus());
  199. }
  200. $dom = new DOMDocument();
  201. $dom->loadXML($response->getBody());
  202. self::_checkErrors($dom);
  203. /**
  204. * @see Zend_Service_Flickr_ResultSet
  205. */
  206. require_once 'Zend/Service/Flickr/ResultSet.php';
  207. return new Zend_Service_Flickr_ResultSet($dom, $this);
  208. }
  209. /**
  210. * Utility function to find Flickr User IDs for usernames.
  211. *
  212. * (You can only find a user's photo with their NSID.)
  213. *
  214. * @param string $username the username
  215. * @return string the NSID (userid)
  216. * @throws Zend_Service_Exception
  217. */
  218. public function getIdByUsername($username)
  219. {
  220. static $method = 'flickr.people.findByUsername';
  221. $options = array('api_key' => $this->apiKey, 'method' => $method, 'username' => (string) $username);
  222. if (empty($username)) {
  223. /**
  224. * @see Zend_Service_Exception
  225. */
  226. require_once 'Zend/Service/Exception.php';
  227. throw new Zend_Service_Exception('You must supply a username');
  228. }
  229. $restClient = $this->getRestClient();
  230. $restClient->getHttpClient()->resetParameters();
  231. $response = $restClient->restGet('/services/rest/', $options);
  232. if ($response->isError()) {
  233. /**
  234. * @see Zend_Service_Exception
  235. */
  236. require_once 'Zend/Service/Exception.php';
  237. throw new Zend_Service_Exception('An error occurred sending request. Status code: '
  238. . $response->getStatus());
  239. }
  240. $dom = new DOMDocument();
  241. $dom->loadXML($response->getBody());
  242. self::_checkErrors($dom);
  243. $xpath = new DOMXPath($dom);
  244. return (string) $xpath->query('//user')->item(0)->getAttribute('id');
  245. }
  246. /**
  247. * Utility function to find Flickr User IDs for emails.
  248. *
  249. * (You can only find a user's photo with their NSID.)
  250. *
  251. * @param string $email the email
  252. * @return string the NSID (userid)
  253. * @throws Zend_Service_Exception
  254. */
  255. public function getIdByEmail($email)
  256. {
  257. static $method = 'flickr.people.findByEmail';
  258. if (empty($email)) {
  259. /**
  260. * @see Zend_Service_Exception
  261. */
  262. require_once 'Zend/Service/Exception.php';
  263. throw new Zend_Service_Exception('You must supply an e-mail address');
  264. }
  265. $options = array('api_key' => $this->apiKey, 'method' => $method, 'find_email' => (string) $email);
  266. $restClient = $this->getRestClient();
  267. $restClient->getHttpClient()->resetParameters();
  268. $response = $restClient->restGet('/services/rest/', $options);
  269. if ($response->isError()) {
  270. /**
  271. * @see Zend_Service_Exception
  272. */
  273. require_once 'Zend/Service/Exception.php';
  274. throw new Zend_Service_Exception('An error occurred sending request. Status code: '
  275. . $response->getStatus());
  276. }
  277. $dom = new DOMDocument();
  278. $dom->loadXML($response->getBody());
  279. self::_checkErrors($dom);
  280. $xpath = new DOMXPath($dom);
  281. return (string) $xpath->query('//user')->item(0)->getAttribute('id');
  282. }
  283. /**
  284. * Returns Flickr photo details by for the given photo ID
  285. *
  286. * @param string $id the NSID
  287. * @return array of Zend_Service_Flickr_Image, details for the specified image
  288. * @throws Zend_Service_Exception
  289. */
  290. public function getImageDetails($id)
  291. {
  292. static $method = 'flickr.photos.getSizes';
  293. if (empty($id)) {
  294. /**
  295. * @see Zend_Service_Exception
  296. */
  297. require_once 'Zend/Service/Exception.php';
  298. throw new Zend_Service_Exception('You must supply a photo ID');
  299. }
  300. $options = array('api_key' => $this->apiKey, 'method' => $method, 'photo_id' => $id);
  301. $restClient = $this->getRestClient();
  302. $restClient->getHttpClient()->resetParameters();
  303. $response = $restClient->restGet('/services/rest/', $options);
  304. $dom = new DOMDocument();
  305. $dom->loadXML($response->getBody());
  306. $xpath = new DOMXPath($dom);
  307. self::_checkErrors($dom);
  308. $retval = array();
  309. /**
  310. * @see Zend_Service_Flickr_Image
  311. */
  312. require_once 'Zend/Service/Flickr/Image.php';
  313. foreach ($xpath->query('//size') as $size) {
  314. $label = (string) $size->getAttribute('label');
  315. $retval[$label] = new Zend_Service_Flickr_Image($size);
  316. }
  317. return $retval;
  318. }
  319. /**
  320. * Returns a reference to the REST client, instantiating it if necessary
  321. *
  322. * @return Zend_Rest_Client
  323. */
  324. public function getRestClient()
  325. {
  326. if (null === $this->_restClient) {
  327. /**
  328. * @see Zend_Rest_Client
  329. */
  330. require_once 'Zend/Rest/Client.php';
  331. $this->_restClient = new Zend_Rest_Client(self::URI_BASE);
  332. }
  333. return $this->_restClient;
  334. }
  335. /**
  336. * Validate User Search Options
  337. *
  338. * @param array $options
  339. * @return void
  340. * @throws Zend_Service_Exception
  341. */
  342. protected function _validateUserSearch(array $options)
  343. {
  344. $validOptions = array('api_key', 'method', 'user_id', 'per_page', 'page', 'extras', 'min_upload_date',
  345. 'min_taken_date', 'max_upload_date', 'max_taken_date', 'safe_search');
  346. $this->_compareOptions($options, $validOptions);
  347. /**
  348. * @see Zend_Validate_Between
  349. */
  350. require_once 'Zend/Validate/Between.php';
  351. $between = new Zend_Validate_Between(1, 500, true);
  352. if (!$between->isValid($options['per_page'])) {
  353. /**
  354. * @see Zend_Service_Exception
  355. */
  356. require_once 'Zend/Service/Exception.php';
  357. throw new Zend_Service_Exception($options['per_page'] . ' is not valid for the "per_page" option');
  358. }
  359. /**
  360. * @see Zend_Validate_Int
  361. */
  362. require_once 'Zend/Validate/Int.php';
  363. $int = new Zend_Validate_Int();
  364. if (!$int->isValid($options['page'])) {
  365. /**
  366. * @see Zend_Service_Exception
  367. */
  368. require_once 'Zend/Service/Exception.php';
  369. throw new Zend_Service_Exception($options['page'] . ' is not valid for the "page" option');
  370. }
  371. // validate extras, which are delivered in csv format
  372. if ($options['extras']) {
  373. $extras = explode(',', $options['extras']);
  374. $validExtras = array('license', 'date_upload', 'date_taken', 'owner_name', 'icon_server');
  375. foreach($extras as $extra) {
  376. /**
  377. * @todo The following does not do anything [yet], so it is commented out.
  378. */
  379. //in_array(trim($extra), $validExtras);
  380. }
  381. }
  382. }
  383. /**
  384. * Validate Tag Search Options
  385. *
  386. * @param array $options
  387. * @return void
  388. * @throws Zend_Service_Exception
  389. */
  390. protected function _validateTagSearch(array $options)
  391. {
  392. $validOptions = array('method', 'api_key', 'user_id', 'tags', 'tag_mode', 'text', 'min_upload_date',
  393. 'max_upload_date', 'min_taken_date', 'max_taken_date', 'license', 'sort',
  394. 'privacy_filter', 'bbox', 'accuracy', 'safe_search', 'content_type', 'machine_tags',
  395. 'machine_tag_mode', 'group_id', 'contacts', 'woe_id', 'place_id', 'media', 'has_geo',
  396. 'geo_context', 'lat', 'lon', 'radius', 'radius_units', 'is_commons', 'is_gallery',
  397. 'extras', 'per_page', 'page');
  398. $this->_compareOptions($options, $validOptions);
  399. /**
  400. * @see Zend_Validate_Between
  401. */
  402. require_once 'Zend/Validate/Between.php';
  403. $between = new Zend_Validate_Between(1, 500, true);
  404. if (!$between->isValid($options['per_page'])) {
  405. /**
  406. * @see Zend_Service_Exception
  407. */
  408. require_once 'Zend/Service/Exception.php';
  409. throw new Zend_Service_Exception($options['per_page'] . ' is not valid for the "per_page" option');
  410. }
  411. /**
  412. * @see Zend_Validate_Int
  413. */
  414. require_once 'Zend/Validate/Int.php';
  415. $int = new Zend_Validate_Int();
  416. if (!$int->isValid($options['page'])) {
  417. /**
  418. * @see Zend_Service_Exception
  419. */
  420. require_once 'Zend/Service/Exception.php';
  421. throw new Zend_Service_Exception($options['page'] . ' is not valid for the "page" option');
  422. }
  423. // validate extras, which are delivered in csv format
  424. if ($options['extras']) {
  425. $extras = explode(',', $options['extras']);
  426. $validExtras = array('license', 'date_upload', 'date_taken', 'owner_name', 'icon_server');
  427. foreach($extras as $extra) {
  428. /**
  429. * @todo The following does not do anything [yet], so it is commented out.
  430. */
  431. //in_array(trim($extra), $validExtras);
  432. }
  433. }
  434. }
  435. /**
  436. * Validate Group Search Options
  437. *
  438. * @param array $options
  439. * @throws Zend_Service_Exception
  440. * @return void
  441. */
  442. protected function _validateGroupPoolGetPhotos(array $options)
  443. {
  444. $validOptions = array('api_key', 'tags', 'method', 'group_id', 'per_page', 'page', 'extras', 'user_id');
  445. $this->_compareOptions($options, $validOptions);
  446. /**
  447. * @see Zend_Validate_Between
  448. */
  449. require_once 'Zend/Validate/Between.php';
  450. $between = new Zend_Validate_Between(1, 500, true);
  451. if (!$between->isValid($options['per_page'])) {
  452. /**
  453. * @see Zend_Service_Exception
  454. */
  455. require_once 'Zend/Service/Exception.php';
  456. throw new Zend_Service_Exception($options['per_page'] . ' is not valid for the "per_page" option');
  457. }
  458. /**
  459. * @see Zend_Validate_Int
  460. */
  461. require_once 'Zend/Validate/Int.php';
  462. $int = new Zend_Validate_Int();
  463. if (!$int->isValid($options['page'])) {
  464. /**
  465. * @see Zend_Service_Exception
  466. */
  467. require_once 'Zend/Service/Exception.php';
  468. throw new Zend_Service_Exception($options['page'] . ' is not valid for the "page" option');
  469. }
  470. // validate extras, which are delivered in csv format
  471. if (isset($options['extras'])) {
  472. $extras = explode(',', $options['extras']);
  473. $validExtras = array('license', 'date_upload', 'date_taken', 'owner_name', 'icon_server');
  474. foreach($extras as $extra) {
  475. /**
  476. * @todo The following does not do anything [yet], so it is commented out.
  477. */
  478. //in_array(trim($extra), $validExtras);
  479. }
  480. }
  481. }
  482. /**
  483. * Throws an exception if and only if the response status indicates a failure
  484. *
  485. * @param DOMDocument $dom
  486. * @return void
  487. * @throws Zend_Service_Exception
  488. */
  489. protected static function _checkErrors(DOMDocument $dom)
  490. {
  491. if ($dom->documentElement->getAttribute('stat') === 'fail') {
  492. $xpath = new DOMXPath($dom);
  493. $err = $xpath->query('//err')->item(0);
  494. /**
  495. * @see Zend_Service_Exception
  496. */
  497. require_once 'Zend/Service/Exception.php';
  498. throw new Zend_Service_Exception('Search failed due to error: ' . $err->getAttribute('msg')
  499. . ' (error #' . $err->getAttribute('code') . ')');
  500. }
  501. }
  502. /**
  503. * Prepare options for the request
  504. *
  505. * @param string $method Flickr Method to call
  506. * @param array $options User Options
  507. * @param array $defaultOptions Default Options
  508. * @return array Merged array of user and default/required options
  509. */
  510. protected function _prepareOptions($method, array $options, array $defaultOptions)
  511. {
  512. $options['method'] = (string) $method;
  513. $options['api_key'] = $this->apiKey;
  514. return array_merge($defaultOptions, $options);
  515. }
  516. /**
  517. * Throws an exception if and only if any user options are invalid
  518. *
  519. * @param array $options User options
  520. * @param array $validOptions Valid options
  521. * @return void
  522. * @throws Zend_Service_Exception
  523. */
  524. protected function _compareOptions(array $options, array $validOptions)
  525. {
  526. $difference = array_diff(array_keys($options), $validOptions);
  527. if ($difference) {
  528. /**
  529. * @see Zend_Service_Exception
  530. */
  531. require_once 'Zend/Service/Exception.php';
  532. throw new Zend_Service_Exception('The following parameters are invalid: ' . implode(',', $difference));
  533. }
  534. }
  535. }