|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00027 require_once 'Zend/Gdata/HttpClient.php'; 00028 00032 require_once 'Zend/Version.php'; 00033 00045 class Zend_Gdata_ClientLogin 00046 { 00047 00052 const CLIENTLOGIN_URI = 'https://www.google.com/accounts/ClientLogin'; 00053 00058 const DEFAULT_SOURCE = 'Zend-ZendFramework'; 00059 00081 public static function getHttpClient($email, $password, $service = 'xapi', 00082 $client = null, 00083 $source = self::DEFAULT_SOURCE, 00084 $loginToken = null, 00085 $loginCaptcha = null, 00086 $loginUri = self::CLIENTLOGIN_URI, 00087 $accountType = 'HOSTED_OR_GOOGLE') 00088 { 00089 if (! ($email && $password)) { 00090 require_once 'Zend/Gdata/App/AuthException.php'; 00091 throw new Zend_Gdata_App_AuthException( 00092 'Please set your Google credentials before trying to ' . 00093 'authenticate'); 00094 } 00095 00096 if ($client == null) { 00097 $client = new Zend_Gdata_HttpClient(); 00098 } 00099 if (!$client instanceof Zend_Http_Client) { 00100 require_once 'Zend/Gdata/App/HttpException.php'; 00101 throw new Zend_Gdata_App_HttpException( 00102 'Client is not an instance of Zend_Http_Client.'); 00103 } 00104 00105 // Build the HTTP client for authentication 00106 $client->setUri($loginUri); 00107 $useragent = $source . ' Zend_Framework_Gdata/' . Zend_Version::VERSION; 00108 $client->setConfig(array( 00109 'maxredirects' => 0, 00110 'strictredirects' => true, 00111 'useragent' => $useragent 00112 ) 00113 ); 00114 $client->setParameterPost('accountType', $accountType); 00115 $client->setParameterPost('Email', (string) $email); 00116 $client->setParameterPost('Passwd', (string) $password); 00117 $client->setParameterPost('service', (string) $service); 00118 $client->setParameterPost('source', (string) $source); 00119 if ($loginToken || $loginCaptcha) { 00120 if($loginToken && $loginCaptcha) { 00121 $client->setParameterPost('logintoken', (string) $loginToken); 00122 $client->setParameterPost('logincaptcha', 00123 (string) $loginCaptcha); 00124 } 00125 else { 00126 require_once 'Zend/Gdata/App/AuthException.php'; 00127 throw new Zend_Gdata_App_AuthException( 00128 'Please provide both a token ID and a user\'s response ' . 00129 'to the CAPTCHA challenge.'); 00130 } 00131 } 00132 00133 // Send the authentication request 00134 // For some reason Google's server causes an SSL error. We use the 00135 // output buffer to supress an error from being shown. Ugly - but works! 00136 ob_start(); 00137 try { 00138 $response = $client->request('POST'); 00139 } catch (Zend_Http_Client_Exception $e) { 00140 require_once 'Zend/Gdata/App/HttpException.php'; 00141 throw new Zend_Gdata_App_HttpException($e->getMessage(), $e); 00142 } 00143 ob_end_clean(); 00144 00145 // Parse Google's response 00146 $goog_resp = array(); 00147 foreach (explode("\n", $response->getBody()) as $l) { 00148 $l = chop($l); 00149 if ($l) { 00150 list($key, $val) = explode('=', chop($l), 2); 00151 $goog_resp[$key] = $val; 00152 } 00153 } 00154 00155 if ($response->getStatus() == 200) { 00156 $client->setClientLoginToken($goog_resp['Auth']); 00157 $useragent = $source . ' Zend_Framework_Gdata/' . Zend_Version::VERSION; 00158 $client->setConfig(array( 00159 'strictredirects' => true, 00160 'useragent' => $useragent 00161 ) 00162 ); 00163 return $client; 00164 00165 } elseif ($response->getStatus() == 403) { 00166 // Check if the server asked for a CAPTCHA 00167 if (array_key_exists('Error', $goog_resp) && 00168 $goog_resp['Error'] == 'CaptchaRequired') { 00169 require_once 'Zend/Gdata/App/CaptchaRequiredException.php'; 00170 throw new Zend_Gdata_App_CaptchaRequiredException( 00171 $goog_resp['CaptchaToken'], $goog_resp['CaptchaUrl']); 00172 } 00173 else { 00174 require_once 'Zend/Gdata/App/AuthException.php'; 00175 throw new Zend_Gdata_App_AuthException('Authentication with Google failed. Reason: ' . 00176 (isset($goog_resp['Error']) ? $goog_resp['Error'] : 'Unspecified.')); 00177 } 00178 } 00179 } 00180 00181 } 00182