PageRenderTime 57ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/library/Zend/Gdata/Photos.php

http://github.com/michael-romer/zf-boilerplate
PHP | 576 lines | 290 code | 36 blank | 250 comment | 75 complexity | 298a91d22fe1a64adad7d251267789a0 MD5 | raw file
Possible License(s): Unlicense, Apache-2.0
  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_Gdata
  17. * @subpackage Photos
  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: Photos.php 23805 2011-03-16 00:55:40Z tjohns $
  21. */
  22. /**
  23. * @see Zend_Gdata
  24. */
  25. require_once 'Zend/Gdata.php';
  26. /**
  27. * @see Zend_Gdata_Photos_UserFeed
  28. */
  29. require_once 'Zend/Gdata/Photos/UserFeed.php';
  30. /**
  31. * @see Zend_Gdata_Photos_AlbumFeed
  32. */
  33. require_once 'Zend/Gdata/Photos/AlbumFeed.php';
  34. /**
  35. * @see Zend_Gdata_Photos_PhotoFeed
  36. */
  37. require_once 'Zend/Gdata/Photos/PhotoFeed.php';
  38. /**
  39. * Service class for interacting with the Google Photos Data API.
  40. *
  41. * Like other service classes in this module, this class provides access via
  42. * an HTTP client to Google servers for working with entries and feeds.
  43. *
  44. * @link http://code.google.com/apis/picasaweb/gdata.html
  45. *
  46. * @category Zend
  47. * @package Zend_Gdata
  48. * @subpackage Photos
  49. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  50. * @license http://framework.zend.com/license/new-bsd New BSD License
  51. */
  52. class Zend_Gdata_Photos extends Zend_Gdata
  53. {
  54. const PICASA_BASE_URI = 'https://picasaweb.google.com/data';
  55. const PICASA_BASE_FEED_URI = 'https://picasaweb.google.com/data/feed';
  56. const AUTH_SERVICE_NAME = 'lh2';
  57. /**
  58. * Default projection when interacting with the Picasa server.
  59. */
  60. const DEFAULT_PROJECTION = 'api';
  61. /**
  62. * The default visibility to filter events by.
  63. */
  64. const DEFAULT_VISIBILITY = 'all';
  65. /**
  66. * The default user to retrieve feeds for.
  67. */
  68. const DEFAULT_USER = 'default';
  69. /**
  70. * Path to the user feed on the Picasa server.
  71. */
  72. const USER_PATH = 'user';
  73. /**
  74. * Path to album feeds on the Picasa server.
  75. */
  76. const ALBUM_PATH = 'albumid';
  77. /**
  78. * Path to photo feeds on the Picasa server.
  79. */
  80. const PHOTO_PATH = 'photoid';
  81. /**
  82. * The path to the community search feed on the Picasa server.
  83. */
  84. const COMMUNITY_SEARCH_PATH = 'all';
  85. /**
  86. * The path to use for finding links to feeds within entries
  87. */
  88. const FEED_LINK_PATH = 'http://schemas.google.com/g/2005#feed';
  89. /**
  90. * The path to use for the determining type of an entry
  91. */
  92. const KIND_PATH = 'http://schemas.google.com/g/2005#kind';
  93. /**
  94. * Namespaces used for Zend_Gdata_Photos
  95. *
  96. * @var array
  97. */
  98. public static $namespaces = array(
  99. array('gphoto', 'http://schemas.google.com/photos/2007', 1, 0),
  100. array('photo', 'http://www.pheed.com/pheed/', 1, 0),
  101. array('exif', 'http://schemas.google.com/photos/exif/2007', 1, 0),
  102. array('georss', 'http://www.georss.org/georss', 1, 0),
  103. array('gml', 'http://www.opengis.net/gml', 1, 0),
  104. array('media', 'http://search.yahoo.com/mrss/', 1, 0)
  105. );
  106. /**
  107. * Create Zend_Gdata_Photos object
  108. *
  109. * @param Zend_Http_Client $client (optional) The HTTP client to use when
  110. * when communicating with the servers.
  111. * @param string $applicationId The identity of the app in the form of Company-AppName-Version
  112. */
  113. public function __construct($client = null, $applicationId = 'MyCompany-MyApp-1.0')
  114. {
  115. $this->registerPackage('Zend_Gdata_Photos');
  116. $this->registerPackage('Zend_Gdata_Photos_Extension');
  117. parent::__construct($client, $applicationId);
  118. $this->_httpClient->setParameterPost('service', self::AUTH_SERVICE_NAME);
  119. }
  120. /**
  121. * Retrieve a UserFeed containing AlbumEntries, PhotoEntries and
  122. * TagEntries associated with a given user.
  123. *
  124. * @param string $userName The userName of interest
  125. * @param mixed $location (optional) The location for the feed, as a URL
  126. * or Query. If not provided, a default URL will be used instead.
  127. * @return Zend_Gdata_Photos_UserFeed
  128. * @throws Zend_Gdata_App_Exception
  129. * @throws Zend_Gdata_App_HttpException
  130. */
  131. public function getUserFeed($userName = null, $location = null)
  132. {
  133. if ($location instanceof Zend_Gdata_Photos_UserQuery) {
  134. $location->setType('feed');
  135. if ($userName !== null) {
  136. $location->setUser($userName);
  137. }
  138. $uri = $location->getQueryUrl();
  139. } else if ($location instanceof Zend_Gdata_Query) {
  140. if ($userName !== null) {
  141. $location->setUser($userName);
  142. }
  143. $uri = $location->getQueryUrl();
  144. } else if ($location !== null) {
  145. $uri = $location;
  146. } else if ($userName !== null) {
  147. $uri = self::PICASA_BASE_FEED_URI . '/' .
  148. self::DEFAULT_PROJECTION . '/' . self::USER_PATH . '/' .
  149. $userName;
  150. } else {
  151. $uri = self::PICASA_BASE_FEED_URI . '/' .
  152. self::DEFAULT_PROJECTION . '/' . self::USER_PATH . '/' .
  153. self::DEFAULT_USER;
  154. }
  155. return parent::getFeed($uri, 'Zend_Gdata_Photos_UserFeed');
  156. }
  157. /**
  158. * Retreive AlbumFeed object containing multiple PhotoEntry or TagEntry
  159. * objects.
  160. *
  161. * @param mixed $location (optional) The location for the feed, as a URL or Query.
  162. * @return Zend_Gdata_Photos_AlbumFeed
  163. * @throws Zend_Gdata_App_Exception
  164. * @throws Zend_Gdata_App_HttpException
  165. */
  166. public function getAlbumFeed($location = null)
  167. {
  168. if ($location === null) {
  169. require_once 'Zend/Gdata/App/InvalidArgumentException.php';
  170. throw new Zend_Gdata_App_InvalidArgumentException(
  171. 'Location must not be null');
  172. } else if ($location instanceof Zend_Gdata_Photos_UserQuery) {
  173. $location->setType('feed');
  174. $uri = $location->getQueryUrl();
  175. } else if ($location instanceof Zend_Gdata_Query) {
  176. $uri = $location->getQueryUrl();
  177. } else {
  178. $uri = $location;
  179. }
  180. return parent::getFeed($uri, 'Zend_Gdata_Photos_AlbumFeed');
  181. }
  182. /**
  183. * Retreive PhotoFeed object containing comments and tags associated
  184. * with a given photo.
  185. *
  186. * @param mixed $location (optional) The location for the feed, as a URL
  187. * or Query. If not specified, the community search feed will
  188. * be returned instead.
  189. * @return Zend_Gdata_Photos_PhotoFeed
  190. * @throws Zend_Gdata_App_Exception
  191. * @throws Zend_Gdata_App_HttpException
  192. */
  193. public function getPhotoFeed($location = null)
  194. {
  195. if ($location === null) {
  196. $uri = self::PICASA_BASE_FEED_URI . '/' .
  197. self::DEFAULT_PROJECTION . '/' .
  198. self::COMMUNITY_SEARCH_PATH;
  199. } else if ($location instanceof Zend_Gdata_Photos_UserQuery) {
  200. $location->setType('feed');
  201. $uri = $location->getQueryUrl();
  202. } else if ($location instanceof Zend_Gdata_Query) {
  203. $uri = $location->getQueryUrl();
  204. } else {
  205. $uri = $location;
  206. }
  207. return parent::getFeed($uri, 'Zend_Gdata_Photos_PhotoFeed');
  208. }
  209. /**
  210. * Retreive a single UserEntry object.
  211. *
  212. * @param mixed $location The location for the feed, as a URL or Query.
  213. * @return Zend_Gdata_Photos_UserEntry
  214. * @throws Zend_Gdata_App_Exception
  215. * @throws Zend_Gdata_App_HttpException
  216. */
  217. public function getUserEntry($location)
  218. {
  219. if ($location === null) {
  220. require_once 'Zend/Gdata/App/InvalidArgumentException.php';
  221. throw new Zend_Gdata_App_InvalidArgumentException(
  222. 'Location must not be null');
  223. } else if ($location instanceof Zend_Gdata_Photos_UserQuery) {
  224. $location->setType('entry');
  225. $uri = $location->getQueryUrl();
  226. } else if ($location instanceof Zend_Gdata_Query) {
  227. $uri = $location->getQueryUrl();
  228. } else {
  229. $uri = $location;
  230. }
  231. return parent::getEntry($uri, 'Zend_Gdata_Photos_UserEntry');
  232. }
  233. /**
  234. * Retreive a single AlbumEntry object.
  235. *
  236. * @param mixed $location The location for the feed, as a URL or Query.
  237. * @return Zend_Gdata_Photos_AlbumEntry
  238. * @throws Zend_Gdata_App_Exception
  239. * @throws Zend_Gdata_App_HttpException
  240. */
  241. public function getAlbumEntry($location)
  242. {
  243. if ($location === null) {
  244. require_once 'Zend/Gdata/App/InvalidArgumentException.php';
  245. throw new Zend_Gdata_App_InvalidArgumentException(
  246. 'Location must not be null');
  247. } else if ($location instanceof Zend_Gdata_Photos_UserQuery) {
  248. $location->setType('entry');
  249. $uri = $location->getQueryUrl();
  250. } else if ($location instanceof Zend_Gdata_Query) {
  251. $uri = $location->getQueryUrl();
  252. } else {
  253. $uri = $location;
  254. }
  255. return parent::getEntry($uri, 'Zend_Gdata_Photos_AlbumEntry');
  256. }
  257. /**
  258. * Retreive a single PhotoEntry object.
  259. *
  260. * @param mixed $location The location for the feed, as a URL or Query.
  261. * @return Zend_Gdata_Photos_PhotoEntry
  262. * @throws Zend_Gdata_App_Exception
  263. * @throws Zend_Gdata_App_HttpException
  264. */
  265. public function getPhotoEntry($location)
  266. {
  267. if ($location === null) {
  268. require_once 'Zend/Gdata/App/InvalidArgumentException.php';
  269. throw new Zend_Gdata_App_InvalidArgumentException(
  270. 'Location must not be null');
  271. } else if ($location instanceof Zend_Gdata_Photos_UserQuery) {
  272. $location->setType('entry');
  273. $uri = $location->getQueryUrl();
  274. } else if ($location instanceof Zend_Gdata_Query) {
  275. $uri = $location->getQueryUrl();
  276. } else {
  277. $uri = $location;
  278. }
  279. return parent::getEntry($uri, 'Zend_Gdata_Photos_PhotoEntry');
  280. }
  281. /**
  282. * Retreive a single TagEntry object.
  283. *
  284. * @param mixed $location The location for the feed, as a URL or Query.
  285. * @return Zend_Gdata_Photos_TagEntry
  286. * @throws Zend_Gdata_App_Exception
  287. * @throws Zend_Gdata_App_HttpException
  288. */
  289. public function getTagEntry($location)
  290. {
  291. if ($location === null) {
  292. require_once 'Zend/Gdata/App/InvalidArgumentException.php';
  293. throw new Zend_Gdata_App_InvalidArgumentException(
  294. 'Location must not be null');
  295. } else if ($location instanceof Zend_Gdata_Photos_UserQuery) {
  296. $location->setType('entry');
  297. $uri = $location->getQueryUrl();
  298. } else if ($location instanceof Zend_Gdata_Query) {
  299. $uri = $location->getQueryUrl();
  300. } else {
  301. $uri = $location;
  302. }
  303. return parent::getEntry($uri, 'Zend_Gdata_Photos_TagEntry');
  304. }
  305. /**
  306. * Retreive a single CommentEntry object.
  307. *
  308. * @param mixed $location The location for the feed, as a URL or Query.
  309. * @return Zend_Gdata_Photos_CommentEntry
  310. * @throws Zend_Gdata_App_Exception
  311. * @throws Zend_Gdata_App_HttpException
  312. */
  313. public function getCommentEntry($location)
  314. {
  315. if ($location === null) {
  316. require_once 'Zend/Gdata/App/InvalidArgumentException.php';
  317. throw new Zend_Gdata_App_InvalidArgumentException(
  318. 'Location must not be null');
  319. } else if ($location instanceof Zend_Gdata_Photos_UserQuery) {
  320. $location->setType('entry');
  321. $uri = $location->getQueryUrl();
  322. } else if ($location instanceof Zend_Gdata_Query) {
  323. $uri = $location->getQueryUrl();
  324. } else {
  325. $uri = $location;
  326. }
  327. return parent::getEntry($uri, 'Zend_Gdata_Photos_CommentEntry');
  328. }
  329. /**
  330. * Create a new album from a AlbumEntry.
  331. *
  332. * @param Zend_Gdata_Photos_AlbumEntry $album The album entry to
  333. * insert.
  334. * @param string $url (optional) The URI that the album should be
  335. * uploaded to. If null, the default album creation URI for
  336. * this domain will be used.
  337. * @return Zend_Gdata_Photos_AlbumEntry The inserted album entry as
  338. * returned by the server.
  339. * @throws Zend_Gdata_App_Exception
  340. * @throws Zend_Gdata_App_HttpException
  341. */
  342. public function insertAlbumEntry($album, $uri = null)
  343. {
  344. if ($uri === null) {
  345. $uri = self::PICASA_BASE_FEED_URI . '/' .
  346. self::DEFAULT_PROJECTION . '/' . self::USER_PATH . '/' .
  347. self::DEFAULT_USER;
  348. }
  349. $newEntry = $this->insertEntry($album, $uri, 'Zend_Gdata_Photos_AlbumEntry');
  350. return $newEntry;
  351. }
  352. /**
  353. * Create a new photo from a PhotoEntry.
  354. *
  355. * @param Zend_Gdata_Photos_PhotoEntry $photo The photo to insert.
  356. * @param string $url The URI that the photo should be uploaded
  357. * to. Alternatively, an AlbumEntry can be provided and the
  358. * photo will be added to that album.
  359. * @return Zend_Gdata_Photos_PhotoEntry The inserted photo entry
  360. * as returned by the server.
  361. * @throws Zend_Gdata_App_Exception
  362. * @throws Zend_Gdata_App_HttpException
  363. */
  364. public function insertPhotoEntry($photo, $uri = null)
  365. {
  366. if ($uri instanceof Zend_Gdata_Photos_AlbumEntry) {
  367. $uri = $uri->getLink(self::FEED_LINK_PATH)->href;
  368. }
  369. if ($uri === null) {
  370. require_once 'Zend/Gdata/App/InvalidArgumentException.php';
  371. throw new Zend_Gdata_App_InvalidArgumentException(
  372. 'URI must not be null');
  373. }
  374. $newEntry = $this->insertEntry($photo, $uri, 'Zend_Gdata_Photos_PhotoEntry');
  375. return $newEntry;
  376. }
  377. /**
  378. * Create a new tag from a TagEntry.
  379. *
  380. * @param Zend_Gdata_Photos_TagEntry $tag The tag entry to insert.
  381. * @param string $url The URI where the tag should be
  382. * uploaded to. Alternatively, a PhotoEntry can be provided and
  383. * the tag will be added to that photo.
  384. * @return Zend_Gdata_Photos_TagEntry The inserted tag entry as returned
  385. * by the server.
  386. * @throws Zend_Gdata_App_Exception
  387. * @throws Zend_Gdata_App_HttpException
  388. */
  389. public function insertTagEntry($tag, $uri = null)
  390. {
  391. if ($uri instanceof Zend_Gdata_Photos_PhotoEntry) {
  392. $uri = $uri->getLink(self::FEED_LINK_PATH)->href;
  393. }
  394. if ($uri === null) {
  395. require_once 'Zend/Gdata/App/InvalidArgumentException.php';
  396. throw new Zend_Gdata_App_InvalidArgumentException(
  397. 'URI must not be null');
  398. }
  399. $newEntry = $this->insertEntry($tag, $uri, 'Zend_Gdata_Photos_TagEntry');
  400. return $newEntry;
  401. }
  402. /**
  403. * Create a new comment from a CommentEntry.
  404. *
  405. * @param Zend_Gdata_Photos_CommentEntry $comment The comment entry to
  406. * insert.
  407. * @param string $url The URI where the comment should be uploaded to.
  408. * Alternatively, a PhotoEntry can be provided and
  409. * the comment will be added to that photo.
  410. * @return Zend_Gdata_Photos_CommentEntry The inserted comment entry
  411. * as returned by the server.
  412. * @throws Zend_Gdata_App_Exception
  413. * @throws Zend_Gdata_App_HttpException
  414. */
  415. public function insertCommentEntry($comment, $uri = null)
  416. {
  417. if ($uri instanceof Zend_Gdata_Photos_PhotoEntry) {
  418. $uri = $uri->getLink(self::FEED_LINK_PATH)->href;
  419. }
  420. if ($uri === null) {
  421. require_once 'Zend/Gdata/App/InvalidArgumentException.php';
  422. throw new Zend_Gdata_App_InvalidArgumentException(
  423. 'URI must not be null');
  424. }
  425. $newEntry = $this->insertEntry($comment, $uri, 'Zend_Gdata_Photos_CommentEntry');
  426. return $newEntry;
  427. }
  428. /**
  429. * Delete an AlbumEntry.
  430. *
  431. * @param Zend_Gdata_Photos_AlbumEntry $album The album entry to
  432. * delete.
  433. * @param boolean $catch Whether to catch an exception when
  434. * modified and re-delete or throw
  435. * @return void.
  436. * @throws Zend_Gdata_App_Exception
  437. * @throws Zend_Gdata_App_HttpException
  438. */
  439. public function deleteAlbumEntry($album, $catch)
  440. {
  441. if ($catch) {
  442. try {
  443. $this->delete($album);
  444. } catch (Zend_Gdata_App_HttpException $e) {
  445. if ($e->getResponse()->getStatus() === 409) {
  446. $entry = new Zend_Gdata_Photos_AlbumEntry($e->getResponse()->getBody());
  447. $this->delete($entry->getLink('edit')->href);
  448. } else {
  449. throw $e;
  450. }
  451. }
  452. } else {
  453. $this->delete($album);
  454. }
  455. }
  456. /**
  457. * Delete a PhotoEntry.
  458. *
  459. * @param Zend_Gdata_Photos_PhotoEntry $photo The photo entry to
  460. * delete.
  461. * @param boolean $catch Whether to catch an exception when
  462. * modified and re-delete or throw
  463. * @return void.
  464. * @throws Zend_Gdata_App_Exception
  465. * @throws Zend_Gdata_App_HttpException
  466. */
  467. public function deletePhotoEntry($photo, $catch)
  468. {
  469. if ($catch) {
  470. try {
  471. $this->delete($photo);
  472. } catch (Zend_Gdata_App_HttpException $e) {
  473. if ($e->getResponse()->getStatus() === 409) {
  474. $entry = new Zend_Gdata_Photos_PhotoEntry($e->getResponse()->getBody());
  475. $this->delete($entry->getLink('edit')->href);
  476. } else {
  477. throw $e;
  478. }
  479. }
  480. } else {
  481. $this->delete($photo);
  482. }
  483. }
  484. /**
  485. * Delete a CommentEntry.
  486. *
  487. * @param Zend_Gdata_Photos_CommentEntry $comment The comment entry to
  488. * delete.
  489. * @param boolean $catch Whether to catch an exception when
  490. * modified and re-delete or throw
  491. * @return void.
  492. * @throws Zend_Gdata_App_Exception
  493. * @throws Zend_Gdata_App_HttpException
  494. */
  495. public function deleteCommentEntry($comment, $catch)
  496. {
  497. if ($catch) {
  498. try {
  499. $this->delete($comment);
  500. } catch (Zend_Gdata_App_HttpException $e) {
  501. if ($e->getResponse()->getStatus() === 409) {
  502. $entry = new Zend_Gdata_Photos_CommentEntry($e->getResponse()->getBody());
  503. $this->delete($entry->getLink('edit')->href);
  504. } else {
  505. throw $e;
  506. }
  507. }
  508. } else {
  509. $this->delete($comment);
  510. }
  511. }
  512. /**
  513. * Delete a TagEntry.
  514. *
  515. * @param Zend_Gdata_Photos_TagEntry $tag The tag entry to
  516. * delete.
  517. * @param boolean $catch Whether to catch an exception when
  518. * modified and re-delete or throw
  519. * @return void.
  520. * @throws Zend_Gdata_App_Exception
  521. * @throws Zend_Gdata_App_HttpException
  522. */
  523. public function deleteTagEntry($tag, $catch)
  524. {
  525. if ($catch) {
  526. try {
  527. $this->delete($tag);
  528. } catch (Zend_Gdata_App_HttpException $e) {
  529. if ($e->getResponse()->getStatus() === 409) {
  530. $entry = new Zend_Gdata_Photos_TagEntry($e->getResponse()->getBody());
  531. $this->delete($entry->getLink('edit')->href);
  532. } else {
  533. throw $e;
  534. }
  535. }
  536. } else {
  537. $this->delete($tag);
  538. }
  539. }
  540. }