PageRenderTime 26ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Gdata/HttpClient.php

https://bitbucket.org/lanoversolutions/afterschool
PHP | 352 lines | 127 code | 28 blank | 197 comment | 19 complexity | 6f91d6cdbd25c20cf175623ed14513fd MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Gdata
  17. * @subpackage Gdata
  18. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: HttpClient.php 20096 2010-01-06 02:05:09Z bkarwin $
  21. */
  22. /**
  23. * Zend_Http_Client
  24. */
  25. require_once 'Zend/Http/Client.php';
  26. /**
  27. * Gdata Http Client object.
  28. *
  29. * Class to extend the generic Zend Http Client with the ability to perform
  30. * secure AuthSub requests
  31. *
  32. * @category Zend
  33. * @package Zend_Gdata
  34. * @subpackage Gdata
  35. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  36. * @license http://framework.zend.com/license/new-bsd New BSD License
  37. */
  38. class Zend_Gdata_HttpClient extends Zend_Http_Client
  39. {
  40. /**
  41. * OpenSSL private key resource id
  42. * This key is used for AuthSub authentication. If this value is set,
  43. * it is assuemd that secure AuthSub is desired.
  44. *
  45. * @var resource
  46. */
  47. private $_authSubPrivateKeyId = null;
  48. /**
  49. * Token for AuthSub authentication.
  50. * If this token is set, AuthSub authentication is used.
  51. *
  52. * @var string
  53. */
  54. private $_authSubToken = null;
  55. /**
  56. * Token for ClientLogin authentication.
  57. * If only this token is set, ClientLogin authentication is used.
  58. *
  59. * @var string
  60. */
  61. private $_clientLoginToken = null;
  62. /**
  63. * Token for ClientLogin authentication.
  64. * If this token is set, and the AuthSub key is not set,
  65. * ClientLogin authentication is used
  66. *
  67. * @var string
  68. */
  69. private $_clientLoginKey = null;
  70. /**
  71. * True if this request is being made with data supplied by
  72. * a stream object instead of a raw encoded string.
  73. *
  74. * @var bool
  75. */
  76. protected $_streamingRequest = null;
  77. /**
  78. * Sets the PEM formatted private key, as read from a file.
  79. *
  80. * This method reads the file and then calls setAuthSubPrivateKey()
  81. * with the file contents.
  82. *
  83. * @param string $file The location of the file containing the PEM key
  84. * @param string $passphrase The optional private key passphrase
  85. * @param bool $useIncludePath Whether to search the include_path
  86. * for the file
  87. * @return void
  88. */
  89. public function setAuthSubPrivateKeyFile($file, $passphrase = null,
  90. $useIncludePath = false) {
  91. $fp = @fopen($file, "r", $useIncludePath);
  92. if (!$fp) {
  93. require_once 'Zend/Gdata/App/InvalidArgumentException.php';
  94. throw new Zend_Gdata_App_InvalidArgumentException('Failed to open private key file for AuthSub.');
  95. }
  96. $key = '';
  97. while (!feof($fp)) {
  98. $key .= fread($fp, 8192);
  99. }
  100. $this->setAuthSubPrivateKey($key, $passphrase);
  101. fclose($fp);
  102. }
  103. /**
  104. * Sets the PEM formatted private key to be used for secure AuthSub auth.
  105. *
  106. * In order to call this method, openssl must be enabled in your PHP
  107. * installation. Otherwise, a Zend_Gdata_App_InvalidArgumentException
  108. * will be thrown.
  109. *
  110. * @param string $key The private key
  111. * @param string $passphrase The optional private key passphrase
  112. * @throws Zend_Gdata_App_InvalidArgumentException
  113. * @return Zend_Gdata_HttpClient Provides a fluent interface
  114. */
  115. public function setAuthSubPrivateKey($key, $passphrase = null) {
  116. if ($key != null && !function_exists('openssl_pkey_get_private')) {
  117. require_once 'Zend/Gdata/App/InvalidArgumentException.php';
  118. throw new Zend_Gdata_App_InvalidArgumentException(
  119. 'You cannot enable secure AuthSub if the openssl module ' .
  120. 'is not enabled in your PHP installation.');
  121. }
  122. $this->_authSubPrivateKeyId = openssl_pkey_get_private(
  123. $key, $passphrase);
  124. return $this;
  125. }
  126. /**
  127. * Gets the openssl private key id
  128. *
  129. * @return string The private key
  130. */
  131. public function getAuthSubPrivateKeyId() {
  132. return $this->_authSubPrivateKeyId;
  133. }
  134. /**
  135. * Gets the AuthSub token used for authentication
  136. *
  137. * @return string The token
  138. */
  139. public function getAuthSubToken() {
  140. return $this->_authSubToken;
  141. }
  142. /**
  143. * Sets the AuthSub token used for authentication
  144. *
  145. * @param string $token The token
  146. * @return Zend_Gdata_HttpClient Provides a fluent interface
  147. */
  148. public function setAuthSubToken($token) {
  149. $this->_authSubToken = $token;
  150. return $this;
  151. }
  152. /**
  153. * Gets the ClientLogin token used for authentication
  154. *
  155. * @return string The token
  156. */
  157. public function getClientLoginToken() {
  158. return $this->_clientLoginToken;
  159. }
  160. /**
  161. * Sets the ClientLogin token used for authentication
  162. *
  163. * @param string $token The token
  164. * @return Zend_Gdata_HttpClient Provides a fluent interface
  165. */
  166. public function setClientLoginToken($token) {
  167. $this->_clientLoginToken = $token;
  168. return $this;
  169. }
  170. /**
  171. * Filters the HTTP requests being sent to add the Authorization header.
  172. *
  173. * If both AuthSub and ClientLogin tokens are set,
  174. * AuthSub takes precedence. If an AuthSub key is set, then
  175. * secure AuthSub authentication is used, and the request is signed.
  176. * Requests must be signed only with the private key corresponding to the
  177. * public key registered with Google. If an AuthSub key is set, but
  178. * openssl support is not enabled in the PHP installation, an exception is
  179. * thrown.
  180. *
  181. * @param string $method The HTTP method
  182. * @param string $url The URL
  183. * @param array $headers An associate array of headers to be
  184. * sent with the request or null
  185. * @param string $body The body of the request or null
  186. * @param string $contentType The MIME content type of the body or null
  187. * @throws Zend_Gdata_App_Exception if there was a signing failure
  188. * @return array The processed values in an associative array,
  189. * using the same names as the params
  190. */
  191. public function filterHttpRequest($method, $url, $headers = array(), $body = null, $contentType = null) {
  192. if ($this->getAuthSubToken() != null) {
  193. // AuthSub authentication
  194. if ($this->getAuthSubPrivateKeyId() != null) {
  195. // secure AuthSub
  196. $time = time();
  197. $nonce = mt_rand(0, 999999999);
  198. $dataToSign = $method . ' ' . $url . ' ' . $time . ' ' . $nonce;
  199. // compute signature
  200. $pKeyId = $this->getAuthSubPrivateKeyId();
  201. $signSuccess = openssl_sign($dataToSign, $signature, $pKeyId,
  202. OPENSSL_ALGO_SHA1);
  203. if (!$signSuccess) {
  204. require_once 'Zend/Gdata/App/Exception.php';
  205. throw new Zend_Gdata_App_Exception(
  206. 'openssl_signing failure - returned false');
  207. }
  208. // encode signature
  209. $encodedSignature = base64_encode($signature);
  210. // final header
  211. $headers['authorization'] = 'AuthSub token="' . $this->getAuthSubToken() . '" ' .
  212. 'data="' . $dataToSign . '" ' .
  213. 'sig="' . $encodedSignature . '" ' .
  214. 'sigalg="rsa-sha1"';
  215. } else {
  216. // AuthSub without secure tokens
  217. $headers['authorization'] = 'AuthSub token="' . $this->getAuthSubToken() . '"';
  218. }
  219. } elseif ($this->getClientLoginToken() != null) {
  220. $headers['authorization'] = 'GoogleLogin auth=' . $this->getClientLoginToken();
  221. }
  222. return array('method' => $method, 'url' => $url, 'body' => $body, 'headers' => $headers, 'contentType' => $contentType);
  223. }
  224. /**
  225. * Method for filtering the HTTP response, though no filtering is
  226. * currently done.
  227. *
  228. * @param Zend_Http_Response $response The response object to filter
  229. * @return Zend_Http_Response The filterd response object
  230. */
  231. public function filterHttpResponse($response) {
  232. return $response;
  233. }
  234. /**
  235. * Return the current connection adapter
  236. *
  237. * @return Zend_Http_Client_Adapter_Interface|string $adapter
  238. */
  239. public function getAdapter()
  240. {
  241. return $this->adapter;
  242. }
  243. /**
  244. * Load the connection adapter
  245. *
  246. * @param Zend_Http_Client_Adapter_Interface $adapter
  247. * @return void
  248. */
  249. public function setAdapter($adapter)
  250. {
  251. if ($adapter == null) {
  252. $this->adapter = $adapter;
  253. } else {
  254. parent::setAdapter($adapter);
  255. }
  256. }
  257. /**
  258. * Set the streamingRequest variable which controls whether we are
  259. * sending the raw (already encoded) POST data from a stream source.
  260. *
  261. * @param boolean $value The value to set.
  262. * @return void
  263. */
  264. public function setStreamingRequest($value)
  265. {
  266. $this->_streamingRequest = $value;
  267. }
  268. /**
  269. * Check whether the client is set to perform streaming requests.
  270. *
  271. * @return boolean True if yes, false otherwise.
  272. */
  273. public function getStreamingRequest()
  274. {
  275. if ($this->_streamingRequest()) {
  276. return true;
  277. } else {
  278. return false;
  279. }
  280. }
  281. /**
  282. * Prepare the request body (for POST and PUT requests)
  283. *
  284. * @return string
  285. * @throws Zend_Http_Client_Exception
  286. */
  287. protected function _prepareBody()
  288. {
  289. if($this->_streamingRequest) {
  290. $this->setHeaders(self::CONTENT_LENGTH,
  291. $this->raw_post_data->getTotalSize());
  292. return $this->raw_post_data;
  293. }
  294. else {
  295. return parent::_prepareBody();
  296. }
  297. }
  298. /**
  299. * Clear all custom parameters we set.
  300. *
  301. * @return Zend_Http_Client
  302. */
  303. public function resetParameters($clearAll = false)
  304. {
  305. $this->_streamingRequest = false;
  306. return parent::resetParameters($clearAll);
  307. }
  308. /**
  309. * Set the raw (already encoded) POST data from a stream source.
  310. *
  311. * This is used to support POSTing from open file handles without
  312. * caching the entire body into memory. It is a wrapper around
  313. * Zend_Http_Client::setRawData().
  314. *
  315. * @param string $data The request data
  316. * @param string $enctype The encoding type
  317. * @return Zend_Http_Client
  318. */
  319. public function setRawDataStream($data, $enctype = null)
  320. {
  321. $this->_streamingRequest = true;
  322. return $this->setRawData($data, $enctype);
  323. }
  324. }