2
0

Gdata.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 Gdata
  18. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /**
  22. * Zend_Gdata_App
  23. */
  24. require_once 'Zend/Gdata/App.php';
  25. /**
  26. * Provides functionality to interact with Google data APIs
  27. * Subclasses exist to implement service-specific features
  28. *
  29. * As the Google data API protocol is based upon the Atom Publishing Protocol
  30. * (APP), Gdata functionality extends the appropriate Zend_Gdata_App classes
  31. *
  32. * @link http://code.google.com/apis/gdata/overview.html
  33. *
  34. * @category Zend
  35. * @package Zend_Gdata
  36. * @subpackage Gdata
  37. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  38. * @license http://framework.zend.com/license/new-bsd New BSD License
  39. */
  40. class Zend_Gdata extends Zend_Gdata_App
  41. {
  42. /**
  43. * Service name for use with Google's authentication mechanisms
  44. *
  45. * @var string
  46. */
  47. const AUTH_SERVICE_NAME = 'xapi';
  48. /**
  49. * Default URI to which to POST.
  50. *
  51. * @var string
  52. */
  53. protected $_defaultPostUri = null;
  54. /**
  55. * Packages to search for classes when using magic __call method, in order.
  56. *
  57. * @var array
  58. */
  59. protected $_registeredPackages = array(
  60. 'Zend_Gdata_Kind',
  61. 'Zend_Gdata_Extension',
  62. 'Zend_Gdata',
  63. 'Zend_Gdata_App_Extension',
  64. 'Zend_Gdata_App');
  65. /**
  66. * Namespaces used for Gdata data
  67. *
  68. * @var array
  69. */
  70. public static $namespaces = array(
  71. array('gd', 'http://schemas.google.com/g/2005', 1, 0),
  72. array('openSearch', 'http://a9.com/-/spec/opensearchrss/1.0/', 1, 0),
  73. array('openSearch', 'http://a9.com/-/spec/opensearch/1.1/', 2, 0),
  74. array('rss', 'http://blogs.law.harvard.edu/tech/rss', 1, 0)
  75. );
  76. /**
  77. * Client object used to communicate
  78. *
  79. * @var Zend_Gdata_HttpClient
  80. */
  81. protected $_httpClient;
  82. /**
  83. * Client object used to communicate in static context
  84. *
  85. * @var Zend_Gdata_HttpClient
  86. */
  87. protected static $_staticHttpClient = null;
  88. /**
  89. * Create Gdata object
  90. *
  91. * @param Zend_Http_Client $client
  92. * @param string $applicationId The identity of the app in the form of
  93. * Company-AppName-Version
  94. */
  95. public function __construct($client = null, $applicationId = 'MyCompany-MyApp-1.0')
  96. {
  97. parent::__construct($client, $applicationId);
  98. }
  99. /**
  100. * Imports a feed located at $uri.
  101. *
  102. * @param string $uri
  103. * @param Zend_Http_Client $client The client used for communication
  104. * @param string $className The class which is used as the return type
  105. * @throws Zend_Gdata_App_Exception
  106. * @return string|Zend_Gdata_App_Feed Returns string only if the object
  107. * mapping has been disabled explicitly
  108. * by passing false to the
  109. * useObjectMapping() function.
  110. */
  111. public static function import($uri, $client = null,
  112. $className='Zend_Gdata_Feed')
  113. {
  114. $app = new Zend_Gdata($client);
  115. $requestData = $app->decodeRequest('GET', $uri);
  116. $response = $app->performHttpRequest($requestData['method'], $requestData['url']);
  117. $feedContent = $response->getBody();
  118. $feed = self::importString($feedContent, $className);
  119. if ($client != null) {
  120. $feed->setHttpClient($client);
  121. }
  122. return $feed;
  123. }
  124. /**
  125. * Retrieve feed as string or object
  126. *
  127. * @param mixed $location The location as string or Zend_Gdata_Query
  128. * @param string $className The class type to use for returning the feed
  129. * @throws Zend_Gdata_App_InvalidArgumentException
  130. * @return string|Zend_Gdata_App_Feed Returns string only if the object
  131. * mapping has been disabled explicitly
  132. * by passing false to the
  133. * useObjectMapping() function.
  134. */
  135. public function getFeed($location, $className='Zend_Gdata_Feed')
  136. {
  137. if (is_string($location)) {
  138. $uri = $location;
  139. } elseif ($location instanceof Zend_Gdata_Query) {
  140. $uri = $location->getQueryUrl();
  141. } else {
  142. require_once 'Zend/Gdata/App/InvalidArgumentException.php';
  143. throw new Zend_Gdata_App_InvalidArgumentException(
  144. 'You must specify the location as either a string URI ' .
  145. 'or a child of Zend_Gdata_Query');
  146. }
  147. return parent::getFeed($uri, $className);
  148. }
  149. /**
  150. * Retrieve entry as string or object
  151. *
  152. * @param mixed $location The location as string or Zend_Gdata_Query
  153. * @throws Zend_Gdata_App_InvalidArgumentException
  154. * @return string|Zend_Gdata_App_Entry Returns string only if the object
  155. * mapping has been disabled explicitly
  156. * by passing false to the
  157. * useObjectMapping() function.
  158. */
  159. public function getEntry($location, $className='Zend_Gdata_Entry')
  160. {
  161. if (is_string($location)) {
  162. $uri = $location;
  163. } elseif ($location instanceof Zend_Gdata_Query) {
  164. $uri = $location->getQueryUrl();
  165. } else {
  166. require_once 'Zend/Gdata/App/InvalidArgumentException.php';
  167. throw new Zend_Gdata_App_InvalidArgumentException(
  168. 'You must specify the location as either a string URI ' .
  169. 'or a child of Zend_Gdata_Query');
  170. }
  171. return parent::getEntry($uri, $className);
  172. }
  173. /**
  174. * Performs a HTTP request using the specified method.
  175. *
  176. * Overrides the definition in the parent (Zend_Gdata_App)
  177. * and uses the Zend_Gdata_HttpClient functionality
  178. * to filter the HTTP requests and responses.
  179. *
  180. * @param string $method The HTTP method for the request -
  181. * 'GET', 'POST', 'PUT', 'DELETE'
  182. * @param string $url The URL to which this request is being performed,
  183. * or null if found in $data
  184. * @param array $headers An associative array of HTTP headers
  185. * for this request
  186. * @param string $body The body of the HTTP request
  187. * @param string $contentType The value for the content type of the
  188. * request body
  189. * @param int $remainingRedirects Number of redirects to follow
  190. * if requests results in one
  191. * @return Zend_Http_Response The response object
  192. */
  193. public function performHttpRequest($method, $url, $headers = array(), $body = null, $contentType = null, $remainingRedirects = null)
  194. {
  195. if ($this->_httpClient instanceof Zend_Gdata_HttpClient) {
  196. $filterResult = $this->_httpClient->filterHttpRequest($method, $url, $headers, $body, $contentType);
  197. $method = $filterResult['method'];
  198. $url = $filterResult['url'];
  199. $body = $filterResult['body'];
  200. $headers = $filterResult['headers'];
  201. $contentType = $filterResult['contentType'];
  202. return $this->_httpClient->filterHttpResponse(parent::performHttpRequest($method, $url, $headers, $body, $contentType, $remainingRedirects));
  203. } else {
  204. return parent::performHttpRequest($method, $url, $headers, $body, $contentType, $remainingRedirects);
  205. }
  206. }
  207. /**
  208. * Determines whether service object is authenticated.
  209. *
  210. * @return boolean True if service object is authenticated, false otherwise.
  211. */
  212. public function isAuthenticated()
  213. {
  214. $client = parent::getHttpClient();
  215. if ($client->getClientLoginToken() ||
  216. $client->getAuthSubToken()) {
  217. return true;
  218. }
  219. return false;
  220. }
  221. }