PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/DevApp/library/Zend/Gdata/HttpClient.php

http://firephp.googlecode.com/
PHP | 245 lines | 78 code | 21 blank | 146 comment | 11 complexity | ddc74f63cd8d9270422f5023326be313 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.0, MIT, Apache-2.0
  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. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. */
  20. /**
  21. * Zend_Exception
  22. */
  23. require_once 'Zend/Exception.php';
  24. /**
  25. * Zend_Http_Client
  26. */
  27. require_once 'Zend/Http/Client.php';
  28. /**
  29. * Gdata Http Client object.
  30. *
  31. * Class to extend the generic Zend Http Client with the ability to perform
  32. * secure AuthSub requests
  33. *
  34. * @category Zend
  35. * @package Zend_Gdata
  36. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  37. * @license http://framework.zend.com/license/new-bsd New BSD License
  38. */
  39. class Zend_Gdata_HttpClient extends Zend_Http_Client
  40. {
  41. /**
  42. * OpenSSL private key resource id
  43. * This key is used for AuthSub authentication. If this value is set,
  44. * it is assuemd that secure AuthSub is desired.
  45. *
  46. * @var resource
  47. */
  48. private $_authSubPrivateKeyId = null;
  49. /**
  50. * Token for AuthSub authentication.
  51. * If this token is set, AuthSub authentication is used.
  52. *
  53. * @var string
  54. */
  55. private $_authSubToken = null;
  56. /**
  57. * Token for ClientLogin authentication.
  58. * If only this token is set, ClientLogin authentication is used.
  59. *
  60. * @var string
  61. */
  62. private $_clientLoginToken = null;
  63. /**
  64. * Token for ClientLogin authentication.
  65. * If this token is set, and the AuthSub key is not set,
  66. * ClientLogin authentication is used
  67. *
  68. * @var string
  69. */
  70. private $_clientLoginKey = null;
  71. /**
  72. * Sets the PEM formatted private key, as read from a file.
  73. *
  74. * This method reads the file and then calls setAuthSubPrivateKey()
  75. * with the file contents.
  76. *
  77. * @param string $file The location of the file containing the PEM key
  78. * @param string $passphrase The optional private key passphrase
  79. * @param bool $useIncludePath Whether to search the include_path
  80. * for the file
  81. * @return void
  82. */
  83. public function setAuthSubPrivateKeyFile($file, $passphrase = null,
  84. $useIncludePath = false) {
  85. $fp = fopen($file, "r", $useIncludePath);
  86. $key = '';
  87. while (!feof($fp)) {
  88. $key .= fread($fp, 8192);
  89. }
  90. $this->setAuthSubPrivateKey($key, $passphrase);
  91. fclose($fp);
  92. }
  93. /**
  94. * Sets the PEM formatted private key to be used for secure AuthSub auth.
  95. *
  96. * In order to call this method, openssl must be enabled in your PHP
  97. * installation. Otherwise, a Zend_Gdata_App_InvalidArgumentException
  98. * will be thrown.
  99. *
  100. * @param string $key The private key
  101. * @param string $passphrase The optional private key passphrase
  102. * @throws Zend_Gdata_App_InvalidArgumentException
  103. * @return Zend_Gdata_HttpClient Provides a fluent interface
  104. */
  105. public function setAuthSubPrivateKey($key, $passphrase = null) {
  106. if ($key != null && !function_exists('openssl_pkey_get_private')) {
  107. require_once 'Zend/Gdata/App/InvalidArgumentException.php';
  108. throw new Zend_Gdata_App_InvalidArgumentException(
  109. 'You cannot enable secure AuthSub if the openssl module ' .
  110. 'is not enabled in your PHP installation.');
  111. }
  112. $this->_authSubPrivateKeyId = openssl_pkey_get_private(
  113. $key, $passphrase);
  114. return $this;
  115. }
  116. /**
  117. * Gets the openssl private key id
  118. *
  119. * @return string The private key
  120. */
  121. public function getAuthSubPrivateKeyId() {
  122. return $this->_authSubPrivateKeyId;
  123. }
  124. /**
  125. * Gets the AuthSub token used for authentication
  126. *
  127. * @return string The token
  128. */
  129. public function getAuthSubToken() {
  130. return $this->_authSubToken;
  131. }
  132. /**
  133. * Sets the AuthSub token used for authentication
  134. *
  135. * @param string $token The token
  136. * @return Zend_Gdata_HttpClient Provides a fluent interface
  137. */
  138. public function setAuthSubToken($token) {
  139. $this->_authSubToken = $token;
  140. return $this;
  141. }
  142. /**
  143. * Gets the ClientLogin token used for authentication
  144. *
  145. * @return string The token
  146. */
  147. public function getClientLoginToken() {
  148. return $this->_clientLoginToken;
  149. }
  150. /**
  151. * Sets the ClientLogin token used for authentication
  152. *
  153. * @param string $token The token
  154. * @return Zend_Gdata_HttpClient Provides a fluent interface
  155. */
  156. public function setClientLoginToken($token) {
  157. $this->_clientLoginToken = $token;
  158. return $this;
  159. }
  160. /**
  161. * Filters the HTTP requests being sent to add the Authorization header.
  162. *
  163. * If both AuthSub and ClientLogin tokens are set,
  164. * AuthSub takes precedence. If an AuthSub key is set, then
  165. * secure AuthSub authentication is used, and the request is signed.
  166. * Requests must be signed only with the private key corresponding to the
  167. * public key registered with Google. If an AuthSub key is set, but
  168. * openssl support is not enabled in the PHP installation, an exception is
  169. * thrown.
  170. *
  171. * @param string $method The HTTP method
  172. * @param string $url The URL
  173. * @param array $headers An associate array of headers to be
  174. * sent with the request or null
  175. * @param string $body The body of the request or null
  176. * @param string $contentType The MIME content type of the body or null
  177. * @throws Zend_Gdata_App_Exception if there was a signing failure
  178. * @return array The processed values in an associative array,
  179. * using the same names as the params
  180. */
  181. public function filterHttpRequest($method, $url, $headers = array(), $body = null, $contentType = null) {
  182. if ($this->getAuthSubToken() != null) {
  183. // AuthSub authentication
  184. if ($this->getAuthSubPrivateKeyId() != null) {
  185. // secure AuthSub
  186. $time = time();
  187. $nonce = mt_rand(0, 999999999);
  188. $dataToSign = $method . ' ' . $url . ' ' . $time . ' ' . $nonce;
  189. // compute signature
  190. $pKeyId = $this->getAuthSubPrivateKeyId();
  191. $signSuccess = openssl_sign($dataToSign, $signature, $pKeyId,
  192. OPENSSL_ALGO_SHA1);
  193. if (!$signSuccess) {
  194. require_once 'Zend/Gdata/App/Exception.php';
  195. throw new Zend_Gdata_App_Exception(
  196. 'openssl_signing failure - returned false');
  197. }
  198. // encode signature
  199. $encodedSignature = base64_encode($signature);
  200. // final header
  201. $headers['authorization'] = 'AuthSub token="' . $this->getAuthSubToken() . '" ' .
  202. 'data="' . $dataToSign . '" ' .
  203. 'sig="' . $encodedSignature . '" ' .
  204. 'sigalg="rsa-sha1"';
  205. } else {
  206. // AuthSub without secure tokens
  207. $headers['authorization'] = 'AuthSub token="' . $this->getAuthSubToken() . '"';
  208. }
  209. } elseif ($this->getClientLoginToken() != null) {
  210. $headers['authorization'] = 'GoogleLogin auth=' . $this->getClientLoginToken();
  211. }
  212. return array('method' => $method, 'url' => $url, 'body' => $body, 'headers' => $headers, 'contentType' => $contentType);
  213. }
  214. /**
  215. * Method for filtering the HTTP response, though no filtering is
  216. * currently done.
  217. *
  218. * @param Zend_Http_Response $response The response object to filter
  219. * @return Zend_Http_Response The filterd response object
  220. */
  221. public function filterHttpResponse($response) {
  222. return $response;
  223. }
  224. }