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.
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
25 * Zend_Gdata_HttpClient
27 require_once 'Zend/Gdata/HttpClient.php';
32 require_once 'Zend/Version.php';
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
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
45 class Zend_Gdata_ClientLogin
49 * The Google client login URI
52 const CLIENTLOGIN_URI
= 'https://www.google.com/accounts/ClientLogin';
55 * The default 'source' parameter to send to Google
58 const DEFAULT_SOURCE
= 'Zend-ZendFramework';
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.
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
81 public static function getHttpClient($email, $password, $service = 'xapi',
83 $source = self
::DEFAULT_SOURCE
,
86 $loginUri = self
::CLIENTLOGIN_URI
,
87 $accountType = 'HOSTED_OR_GOOGLE')
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 ' .
96 if ($client == null) {
97 $client = new Zend_Gdata_HttpClient();
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.');
105 // Build the HTTP client for authentication
106 $client->setUri($loginUri);
107 $useragent = $source . ' Zend_Framework_Gdata/' . Zend_Version
::VERSION
;
108 $client->setConfig(array(
110 'strictredirects' => true,
111 'useragent' => $useragent
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);
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.');
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!
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);
145 // Parse Google's response
146 $goog_resp = array();
147 foreach (explode("\n", $response->getBody()) as $l) {
150 list($key, $val) = explode('=', chop($l), 2);
151 $goog_resp[$key] = $val;
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
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']);
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.'));