source: trunk/www.guidonia.net/wp/wp-content/plugins/webtv/Drivers/Zend/Gdata/ClientLogin.php@ 44

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