|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00026 require_once 'Zend/Http/Client.php'; 00027 00040 class Zend_Gdata_HttpClient extends Zend_Http_Client 00041 { 00042 00050 private $_authSubPrivateKeyId = null; 00051 00058 private $_authSubToken = null; 00059 00066 private $_clientLoginToken = null; 00067 00075 private $_clientLoginKey = null; 00076 00083 protected $_streamingRequest = null; 00084 00097 public function setAuthSubPrivateKeyFile($file, $passphrase = null, 00098 $useIncludePath = false) { 00099 $fp = @fopen($file, "r", $useIncludePath); 00100 if (!$fp) { 00101 require_once 'Zend/Gdata/App/InvalidArgumentException.php'; 00102 throw new Zend_Gdata_App_InvalidArgumentException('Failed to open private key file for AuthSub.'); 00103 } 00104 00105 $key = ''; 00106 while (!feof($fp)) { 00107 $key .= fread($fp, 8192); 00108 } 00109 $this->setAuthSubPrivateKey($key, $passphrase); 00110 fclose($fp); 00111 } 00112 00125 public function setAuthSubPrivateKey($key, $passphrase = null) { 00126 if ($key != null && !function_exists('openssl_pkey_get_private')) { 00127 require_once 'Zend/Gdata/App/InvalidArgumentException.php'; 00128 throw new Zend_Gdata_App_InvalidArgumentException( 00129 'You cannot enable secure AuthSub if the openssl module ' . 00130 'is not enabled in your PHP installation.'); 00131 } 00132 $this->_authSubPrivateKeyId = openssl_pkey_get_private( 00133 $key, $passphrase); 00134 return $this; 00135 } 00136 00142 public function getAuthSubPrivateKeyId() { 00143 return $this->_authSubPrivateKeyId; 00144 } 00145 00151 public function getAuthSubToken() { 00152 return $this->_authSubToken; 00153 } 00154 00161 public function setAuthSubToken($token) { 00162 $this->_authSubToken = $token; 00163 return $this; 00164 } 00165 00171 public function getClientLoginToken() { 00172 return $this->_clientLoginToken; 00173 } 00174 00181 public function setClientLoginToken($token) { 00182 $this->_clientLoginToken = $token; 00183 return $this; 00184 } 00185 00207 public function filterHttpRequest($method, $url, $headers = array(), $body = null, $contentType = null) { 00208 if ($this->getAuthSubToken() != null) { 00209 // AuthSub authentication 00210 if ($this->getAuthSubPrivateKeyId() != null) { 00211 // secure AuthSub 00212 $time = time(); 00213 $nonce = mt_rand(0, 999999999); 00214 $dataToSign = $method . ' ' . $url . ' ' . $time . ' ' . $nonce; 00215 00216 // compute signature 00217 $pKeyId = $this->getAuthSubPrivateKeyId(); 00218 $signSuccess = openssl_sign($dataToSign, $signature, $pKeyId, 00219 OPENSSL_ALGO_SHA1); 00220 if (!$signSuccess) { 00221 require_once 'Zend/Gdata/App/Exception.php'; 00222 throw new Zend_Gdata_App_Exception( 00223 'openssl_signing failure - returned false'); 00224 } 00225 // encode signature 00226 $encodedSignature = base64_encode($signature); 00227 00228 // final header 00229 $headers['authorization'] = 'AuthSub token="' . $this->getAuthSubToken() . '" ' . 00230 'data="' . $dataToSign . '" ' . 00231 'sig="' . $encodedSignature . '" ' . 00232 'sigalg="rsa-sha1"'; 00233 } else { 00234 // AuthSub without secure tokens 00235 $headers['authorization'] = 'AuthSub token="' . $this->getAuthSubToken() . '"'; 00236 } 00237 } elseif ($this->getClientLoginToken() != null) { 00238 $headers['authorization'] = 'GoogleLogin auth=' . $this->getClientLoginToken(); 00239 } 00240 return array('method' => $method, 'url' => $url, 'body' => $body, 'headers' => $headers, 'contentType' => $contentType); 00241 } 00242 00250 public function filterHttpResponse($response) { 00251 return $response; 00252 } 00253 00259 public function getAdapter() 00260 { 00261 return $this->adapter; 00262 } 00263 00270 public function setAdapter($adapter) 00271 { 00272 if ($adapter == null) { 00273 $this->adapter = $adapter; 00274 } else { 00275 parent::setAdapter($adapter); 00276 } 00277 } 00278 00286 public function setStreamingRequest($value) 00287 { 00288 $this->_streamingRequest = $value; 00289 } 00290 00296 public function getStreamingRequest() 00297 { 00298 if ($this->_streamingRequest()) { 00299 return true; 00300 } else { 00301 return false; 00302 } 00303 } 00304 00311 protected function _prepareBody() 00312 { 00313 if($this->_streamingRequest) { 00314 $this->setHeaders(self::CONTENT_LENGTH, 00315 $this->raw_post_data->getTotalSize()); 00316 return $this->raw_post_data; 00317 } 00318 else { 00319 return parent::_prepareBody(); 00320 } 00321 } 00322 00328 public function resetParameters($clearAll = false) 00329 { 00330 $this->_streamingRequest = false; 00331 00332 return parent::resetParameters($clearAll); 00333 } 00334 00346 public function setRawDataStream($data, $enctype = null) 00347 { 00348 $this->_streamingRequest = true; 00349 return $this->setRawData($data, $enctype); 00350 } 00351 00352 }