PageRenderTime 41ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Gdata/ClientLogin.php

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