Gdata.php 8.5 KB

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