HttpClient.php 11 KB

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