Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/lib/zend/Zend/Gdata/AuthSub.php
Go to the documentation of this file.
00001 <?php
00002 
00027 require_once 'Zend/Gdata/HttpClient.php';
00028 
00032 require_once 'Zend/Version.php';
00033 
00046 class Zend_Gdata_AuthSub
00047 {
00048 
00049     const AUTHSUB_REQUEST_URI      = 'https://www.google.com/accounts/AuthSubRequest';
00050 
00051     const AUTHSUB_SESSION_TOKEN_URI = 'https://www.google.com/accounts/AuthSubSessionToken';
00052 
00053     const AUTHSUB_REVOKE_TOKEN_URI  = 'https://www.google.com/accounts/AuthSubRevokeToken';
00054 
00055     const AUTHSUB_TOKEN_INFO_URI    = 'https://www.google.com/accounts/AuthSubTokenInfo';
00056 
00079      public static function getAuthSubTokenUri($next, $scope, $secure=0, $session=0,
00080                                                $request_uri = self::AUTHSUB_REQUEST_URI)
00081      {
00082          $querystring = '?next=' . urlencode($next)
00083              . '&scope=' . urldecode($scope)
00084              . '&secure=' . urlencode($secure)
00085              . '&session=' . urlencode($session);
00086          return $request_uri . $querystring;
00087      }
00088 
00089 
00102     public static function getAuthSubSessionToken(
00103             $token, $client = null,
00104             $request_uri = self::AUTHSUB_SESSION_TOKEN_URI)
00105     {
00106         $client = self::getHttpClient($token, $client);
00107 
00108         if ($client instanceof Zend_Gdata_HttpClient) {
00109             $filterResult = $client->filterHttpRequest('GET', $request_uri);
00110             $url = $filterResult['url'];
00111             $headers = $filterResult['headers'];
00112             $client->setHeaders($headers);
00113             $client->setUri($url);
00114         } else {
00115             $client->setUri($request_uri);
00116         }
00117 
00118         try {
00119             $response = $client->request('GET');
00120         } catch (Zend_Http_Client_Exception $e) {
00121             require_once 'Zend/Gdata/App/HttpException.php';
00122             throw new Zend_Gdata_App_HttpException($e->getMessage(), $e);
00123         }
00124 
00125         // Parse Google's response
00126         if ($response->isSuccessful()) {
00127             $goog_resp = array();
00128             foreach (explode("\n", $response->getBody()) as $l) {
00129                 $l = chop($l);
00130                 if ($l) {
00131                     list($key, $val) = explode('=', chop($l), 2);
00132                     $goog_resp[$key] = $val;
00133                 }
00134             }
00135             return $goog_resp['Token'];
00136         } else {
00137             require_once 'Zend/Gdata/App/AuthException.php';
00138             throw new Zend_Gdata_App_AuthException(
00139                     'Token upgrade failed. Reason: ' . $response->getBody());
00140         }
00141     }
00142 
00152     public static function AuthSubRevokeToken($token, $client = null,
00153                                               $request_uri = self::AUTHSUB_REVOKE_TOKEN_URI)
00154     {
00155         $client = self::getHttpClient($token, $client);
00156 
00157         if ($client instanceof Zend_Gdata_HttpClient) {
00158             $filterResult = $client->filterHttpRequest('GET', $request_uri);
00159             $url = $filterResult['url'];
00160             $headers = $filterResult['headers'];
00161             $client->setHeaders($headers);
00162             $client->setUri($url);
00163             $client->resetParameters();
00164         } else {
00165             $client->setUri($request_uri);
00166         }
00167 
00168         ob_start();
00169         try {
00170             $response = $client->request('GET');
00171         } catch (Zend_Http_Client_Exception $e) {
00172             require_once 'Zend/Gdata/App/HttpException.php';
00173             throw new Zend_Gdata_App_HttpException($e->getMessage(), $e);
00174         }
00175         ob_end_clean();
00176         // Parse Google's response
00177         if ($response->isSuccessful()) {
00178             return true;
00179         } else {
00180             return false;
00181         }
00182     }
00183 
00184 
00194     public static function getAuthSubTokenInfo(
00195             $token, $client = null, $request_uri = self::AUTHSUB_TOKEN_INFO_URI)
00196     {
00197         $client = self::getHttpClient($token, $client);
00198 
00199         if ($client instanceof Zend_Gdata_HttpClient) {
00200             $filterResult = $client->filterHttpRequest('GET', $request_uri);
00201             $url = $filterResult['url'];
00202             $headers = $filterResult['headers'];
00203             $client->setHeaders($headers);
00204             $client->setUri($url);
00205         } else {
00206             $client->setUri($request_uri);
00207         }
00208 
00209         ob_start();
00210         try {
00211             $response = $client->request('GET');
00212         } catch (Zend_Http_Client_Exception $e) {
00213             require_once 'Zend/Gdata/App/HttpException.php';
00214             throw new Zend_Gdata_App_HttpException($e->getMessage(), $e);
00215         }
00216         ob_end_clean();
00217         return $response->getBody();
00218     }
00219 
00227     public static function getHttpClient($token, $client = null)
00228     {
00229         if ($client == null) {
00230             $client = new Zend_Gdata_HttpClient();
00231         }
00232         if (!$client instanceof Zend_Http_Client) {
00233             require_once 'Zend/Gdata/App/HttpException.php';
00234             throw new Zend_Gdata_App_HttpException('Client is not an instance of Zend_Http_Client.');
00235         }
00236         $useragent = 'Zend_Framework_Gdata/' . Zend_Version::VERSION;
00237         $client->setConfig(array(
00238                 'strictredirects' => true,
00239                 'useragent' => $useragent
00240             )
00241         );
00242         $client->setAuthSubToken($token);
00243         return $client;
00244     }
00245 
00246 }
 All Data Structures Namespaces Files Functions Variables Enumerations