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