[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/lib/zend/Zend/Gdata/ -> ClientLogin.php (source)

   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 [email protected] 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$
  22   */
  23  
  24  /**
  25   * Zend_Gdata_HttpClient
  26   */
  27  require_once 'Zend/Gdata/HttpClient.php';
  28  
  29  /**
  30   * Zend_Version
  31   */
  32  require_once 'Zend/Version.php';
  33  
  34  /**
  35   * Class to facilitate Google's "Account Authentication
  36   * for Installed Applications" also known as "ClientLogin".
  37   * @see http://code.google.com/apis/accounts/AuthForInstalledApps.html
  38   *
  39   * @category   Zend
  40   * @package    Zend_Gdata
  41   * @subpackage Gdata
  42   * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  43   * @license    http://framework.zend.com/license/new-bsd     New BSD License
  44   */
  45  class Zend_Gdata_ClientLogin
  46  {
  47  
  48      /**
  49       * The Google client login URI
  50       *
  51       */
  52      const CLIENTLOGIN_URI = 'https://www.google.com/accounts/ClientLogin';
  53  
  54      /**
  55       * The default 'source' parameter to send to Google
  56       *
  57       */
  58      const DEFAULT_SOURCE = 'Zend-ZendFramework';
  59  
  60      /**
  61       * Set Google authentication credentials.
  62       * Must be done before trying to do any Google Data operations that
  63       * require authentication.
  64       * For example, viewing private data, or posting or deleting entries.
  65       *
  66       * @param string $email
  67       * @param string $password
  68       * @param string $service
  69       * @param Zend_Gdata_HttpClient $client
  70       * @param string $source
  71       * @param string $loginToken The token identifier as provided by the server.
  72       * @param string $loginCaptcha The user's response to the CAPTCHA challenge.
  73       * @param string $accountType An optional string to identify whether the
  74       * account to be authenticated is a google or a hosted account. Defaults to
  75       * 'HOSTED_OR_GOOGLE'. See: http://code.google.com/apis/accounts/docs/AuthForInstalledApps.html#Request
  76       * @throws Zend_Gdata_App_AuthException
  77       * @throws Zend_Gdata_App_HttpException
  78       * @throws Zend_Gdata_App_CaptchaRequiredException
  79       * @return Zend_Gdata_HttpClient
  80       */
  81      public static function getHttpClient($email, $password, $service = 'xapi',
  82          $client = null,
  83          $source = self::DEFAULT_SOURCE,
  84          $loginToken = null,
  85          $loginCaptcha = null,
  86          $loginUri = self::CLIENTLOGIN_URI,
  87          $accountType = 'HOSTED_OR_GOOGLE')
  88      {
  89          if (! ($email && $password)) {
  90              require_once 'Zend/Gdata/App/AuthException.php';
  91              throw new Zend_Gdata_App_AuthException(
  92                     'Please set your Google credentials before trying to ' .
  93                     'authenticate');
  94          }
  95  
  96          if ($client == null) {
  97              $client = new Zend_Gdata_HttpClient();
  98          }
  99          if (!$client instanceof Zend_Http_Client) {
 100              require_once 'Zend/Gdata/App/HttpException.php';
 101              throw new Zend_Gdata_App_HttpException(
 102                      'Client is not an instance of Zend_Http_Client.');
 103          }
 104  
 105          // Build the HTTP client for authentication
 106          $client->setUri($loginUri);
 107          $useragent = $source . ' Zend_Framework_Gdata/' . Zend_Version::VERSION;
 108          $client->setConfig(array(
 109                  'maxredirects'    => 0,
 110                  'strictredirects' => true,
 111                  'useragent' => $useragent
 112              )
 113          );
 114          $client->setParameterPost('accountType', $accountType);
 115          $client->setParameterPost('Email', (string) $email);
 116          $client->setParameterPost('Passwd', (string) $password);
 117          $client->setParameterPost('service', (string) $service);
 118          $client->setParameterPost('source', (string) $source);
 119          if ($loginToken || $loginCaptcha) {
 120              if($loginToken && $loginCaptcha) {
 121                  $client->setParameterPost('logintoken', (string) $loginToken);
 122                  $client->setParameterPost('logincaptcha',
 123                          (string) $loginCaptcha);
 124              }
 125              else {
 126                  require_once 'Zend/Gdata/App/AuthException.php';
 127                  throw new Zend_Gdata_App_AuthException(
 128                      'Please provide both a token ID and a user\'s response ' .
 129                      'to the CAPTCHA challenge.');
 130              }
 131          }
 132  
 133          // Send the authentication request
 134          // For some reason Google's server causes an SSL error. We use the
 135          // output buffer to supress an error from being shown. Ugly - but works!
 136          ob_start();
 137          try {
 138              $response = $client->request('POST');
 139          } catch (Zend_Http_Client_Exception $e) {
 140              require_once 'Zend/Gdata/App/HttpException.php';
 141              throw new Zend_Gdata_App_HttpException($e->getMessage(), $e);
 142          }
 143          ob_end_clean();
 144  
 145          // Parse Google's response
 146          $goog_resp = array();
 147          foreach (explode("\n", $response->getBody()) as $l) {
 148              $l = chop($l);
 149              if ($l) {
 150                  list($key, $val) = explode('=', chop($l), 2);
 151                  $goog_resp[$key] = $val;
 152              }
 153          }
 154  
 155          if ($response->getStatus() == 200) {
 156              $client->setClientLoginToken($goog_resp['Auth']);
 157              $useragent = $source . ' Zend_Framework_Gdata/' . Zend_Version::VERSION;
 158              $client->setConfig(array(
 159                      'strictredirects' => true,
 160                      'useragent' => $useragent
 161                  )
 162              );
 163              return $client;
 164  
 165          } elseif ($response->getStatus() == 403) {
 166              // Check if the server asked for a CAPTCHA
 167              if (array_key_exists('Error', $goog_resp) &&
 168                  $goog_resp['Error'] == 'CaptchaRequired') {
 169                  require_once 'Zend/Gdata/App/CaptchaRequiredException.php';
 170                  throw new Zend_Gdata_App_CaptchaRequiredException(
 171                      $goog_resp['CaptchaToken'], $goog_resp['CaptchaUrl']);
 172              }
 173              else {
 174                  require_once 'Zend/Gdata/App/AuthException.php';
 175                  throw new Zend_Gdata_App_AuthException('Authentication with Google failed. Reason: ' .
 176                      (isset($goog_resp['Error']) ? $goog_resp['Error'] : 'Unspecified.'));
 177              }
 178          }
 179      }
 180  
 181  }
 182  


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1