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.
18 * @copyright Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
19 * @license http://framework.zend.com/license/new-bsd New BSD License
25 require_once 'Zend/Http/Client.php';
30 require_once 'Zend/Version.php';
33 * Class to facilitate Google's "Account Authentication
34 * for Installed Applications" also known as "ClientLogin".
35 * @see http://code.google.com/apis/accounts/AuthForInstalledApps.html
39 * @copyright Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
40 * @license http://framework.zend.com/license/new-bsd New BSD License
42 class Zend_Gdata_ClientLogin
46 * The Google client login URI
49 const CLIENTLOGIN_URI
= 'https://www.google.com/accounts/ClientLogin';
52 * The default 'source' parameter to send to Google
55 const DEFAULT_SOURCE
= 'Zend-ZendFramework';
58 * Set Google authentication credentials.
59 * Must be done before trying to do any Google Data operations that
60 * require authentication.
61 * For example, viewing private data, or posting or deleting entries.
63 * @param string $email
64 * @param string $password
65 * @param string $service
66 * @param Zend_Http_Client $client
67 * @param string $source
68 * @param string $loginToken The token identifier as provided by the server.
69 * @param string $loginCaptcha The user's response to the CAPTCHA challenge.
70 * @return Zend_Http_Client
71 * @throws Zend_Gdata_App_AuthException
72 * @throws Zend_Gdata_App_HttpException
73 * @throws Zend_Gdata_App_CaptchaRequiredException
75 public static function getHttpClient($email, $password, $service = 'xapi',
77 $source = self
::DEFAULT_SOURCE
,
81 if (! ($email && $password)) {
82 require_once 'Zend/Gdata/App/AuthException.php';
83 throw new Zend_Gdata_App_AuthException('Please set your Google credentials before trying to authenticate');
86 if ($client == null) {
87 $client = new Zend_Http_Client();
89 if (!$client instanceof Zend_Http_Client
) {
90 require_once 'Zend/Gdata/App/HttpException.php';
91 throw new Zend_Gdata_App_HttpException('Client is not an instance of Zend_Http_Client.');
94 // Build the HTTP client for authentication
95 $client->setUri(self
::CLIENTLOGIN_URI
);
96 $useragent = $source . ' Zend_Framework_Gdata/' . Zend_Version
::VERSION
;
97 $client->setConfig(array(
99 'strictredirects' => true,
100 'useragent' => $useragent
103 $client->setParameterPost('accountType', 'HOSTED_OR_GOOGLE');
104 $client->setParameterPost('Email', (string) $email);
105 $client->setParameterPost('Passwd', (string) $password);
106 $client->setParameterPost('service', (string) $service);
107 $client->setParameterPost('source', (string) $source);
108 if ($loginToken ||
$loginCaptcha) {
109 if($loginToken && $loginCaptcha) {
110 $client->setParameterPost('logintoken', (string) $loginToken);
111 $client->setParameterPost('logincaptcha', (string) $loginCaptcha);
114 require_once 'Zend/Gdata/App/AuthException.php';
115 throw new Zend_Gdata_App_AuthException(
116 'Please provide both a token ID and a user\'s response to the CAPTCHA challenge.');
120 // Send the authentication request
121 // For some reason Google's server causes an SSL error. We use the
122 // output buffer to supress an error from being shown. Ugly - but works!
125 $response = $client->request('POST');
126 } catch (Zend_Http_Client_Exception
$e) {
127 require_once 'Zend/Gdata/App/HttpException.php';
128 throw new Zend_Gdata_App_HttpException($e->getMessage(), $e);
132 // Parse Google's response
133 $goog_resp = array();
134 foreach (explode("\n", $response->getBody()) as $l) {
137 list($key, $val) = explode('=', chop($l), 2);
138 $goog_resp[$key] = $val;
142 if ($response->getStatus() == 200) {
143 $headers['authorization'] = 'GoogleLogin auth=' . $goog_resp['Auth'];
144 $client = new Zend_Http_Client();
145 $useragent = $source . ' Zend_Framework_Gdata/' . Zend_Version
::VERSION
;
146 $client->setConfig(array(
147 'strictredirects' => true,
148 'useragent' => $useragent
151 $client->setHeaders($headers);
154 } elseif ($response->getStatus() == 403) {
155 // Check if the server asked for a CAPTCHA
156 if (array_key_exists('Error', $goog_resp) &&
157 $goog_resp['Error'] == 'CaptchaRequired') {
158 require_once 'Zend/Gdata/App/CaptchaRequiredException.php';
159 throw new Zend_Gdata_App_CaptchaRequiredException(
160 $goog_resp['CaptchaToken'], $goog_resp['CaptchaUrl']);
163 require_once 'Zend/Gdata/App/AuthException.php';
164 throw new Zend_Gdata_App_AuthException('Authentication with Google failed. Reason: ' .
165 (isset($goog_resp['Error']) ?
$goog_resp['Error'] : 'Unspecified.'));