PageRenderTime 60ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/DevApp/library/Zend/Gdata/Photos.php

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