PageRenderTime 45ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Zend/Gdata/HttpClient.php

https://github.com/lboaretto/magento-pt_br
PHP | 240 lines | 74 code | 18 blank | 148 comment | 11 complexity | 4778b2abd38c286c2432a8e1bc49aa66 MD5 | raw file
Possible License(s): GPL-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. * @subpackage Gdata
  18. * @copyright Copyright (c) 2005-2008 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-2008 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. * Sets the PEM formatted private key, as read from a file.
  71. *
  72. * This method reads the file and then calls setAuthSubPrivateKey()
  73. * with the file contents.
  74. *
  75. * @param string $file The location of the file containing the PEM key
  76. * @param string $passphrase The optional private key passphrase
  77. * @param bool $useIncludePath Whether to search the include_path
  78. * for the file
  79. * @return void
  80. */
  81. public function setAuthSubPrivateKeyFile($file, $passphrase = null,
  82. $useIncludePath = false) {
  83. $fp = fopen($file, "r", $useIncludePath);
  84. $key = '';
  85. while (!feof($fp)) {
  86. $key .= fread($fp, 8192);
  87. }
  88. $this->setAuthSubPrivateKey($key, $passphrase);
  89. fclose($fp);
  90. }
  91. /**
  92. * Sets the PEM formatted private key to be used for secure AuthSub auth.
  93. *
  94. * In order to call this method, openssl must be enabled in your PHP
  95. * installation. Otherwise, a Zend_Gdata_App_InvalidArgumentException
  96. * will be thrown.
  97. *
  98. * @param string $key The private key
  99. * @param string $passphrase The optional private key passphrase
  100. * @throws Zend_Gdata_App_InvalidArgumentException
  101. * @return Zend_Gdata_HttpClient Provides a fluent interface
  102. */
  103. public function setAuthSubPrivateKey($key, $passphrase = null) {
  104. if ($key != null && !function_exists('openssl_pkey_get_private')) {
  105. #require_once 'Zend/Gdata/App/InvalidArgumentException.php';
  106. throw new Zend_Gdata_App_InvalidArgumentException(
  107. 'You cannot enable secure AuthSub if the openssl module ' .
  108. 'is not enabled in your PHP installation.');
  109. }
  110. $this->_authSubPrivateKeyId = openssl_pkey_get_private(
  111. $key, $passphrase);
  112. return $this;
  113. }
  114. /**
  115. * Gets the openssl private key id
  116. *
  117. * @return string The private key
  118. */
  119. public function getAuthSubPrivateKeyId() {
  120. return $this->_authSubPrivateKeyId;
  121. }
  122. /**
  123. * Gets the AuthSub token used for authentication
  124. *
  125. * @return string The token
  126. */
  127. public function getAuthSubToken() {
  128. return $this->_authSubToken;
  129. }
  130. /**
  131. * Sets the AuthSub token used for authentication
  132. *
  133. * @param string $token The token
  134. * @return Zend_Gdata_HttpClient Provides a fluent interface
  135. */
  136. public function setAuthSubToken($token) {
  137. $this->_authSubToken = $token;
  138. return $this;
  139. }
  140. /**
  141. * Gets the ClientLogin token used for authentication
  142. *
  143. * @return string The token
  144. */
  145. public function getClientLoginToken() {
  146. return $this->_clientLoginToken;
  147. }
  148. /**
  149. * Sets the ClientLogin token used for authentication
  150. *
  151. * @param string $token The token
  152. * @return Zend_Gdata_HttpClient Provides a fluent interface
  153. */
  154. public function setClientLoginToken($token) {
  155. $this->_clientLoginToken = $token;
  156. return $this;
  157. }
  158. /**
  159. * Filters the HTTP requests being sent to add the Authorization header.
  160. *
  161. * If both AuthSub and ClientLogin tokens are set,
  162. * AuthSub takes precedence. If an AuthSub key is set, then
  163. * secure AuthSub authentication is used, and the request is signed.
  164. * Requests must be signed only with the private key corresponding to the
  165. * public key registered with Google. If an AuthSub key is set, but
  166. * openssl support is not enabled in the PHP installation, an exception is
  167. * thrown.
  168. *
  169. * @param string $method The HTTP method
  170. * @param string $url The URL
  171. * @param array $headers An associate array of headers to be
  172. * sent with the request or null
  173. * @param string $body The body of the request or null
  174. * @param string $contentType The MIME content type of the body or null
  175. * @throws Zend_Gdata_App_Exception if there was a signing failure
  176. * @return array The processed values in an associative array,
  177. * using the same names as the params
  178. */
  179. public function filterHttpRequest($method, $url, $headers = array(), $body = null, $contentType = null) {
  180. if ($this->getAuthSubToken() != null) {
  181. // AuthSub authentication
  182. if ($this->getAuthSubPrivateKeyId() != null) {
  183. // secure AuthSub
  184. $time = time();
  185. $nonce = mt_rand(0, 999999999);
  186. $dataToSign = $method . ' ' . $url . ' ' . $time . ' ' . $nonce;
  187. // compute signature
  188. $pKeyId = $this->getAuthSubPrivateKeyId();
  189. $signSuccess = openssl_sign($dataToSign, $signature, $pKeyId,
  190. OPENSSL_ALGO_SHA1);
  191. if (!$signSuccess) {
  192. #require_once 'Zend/Gdata/App/Exception.php';
  193. throw new Zend_Gdata_App_Exception(
  194. 'openssl_signing failure - returned false');
  195. }
  196. // encode signature
  197. $encodedSignature = base64_encode($signature);
  198. // final header
  199. $headers['authorization'] = 'AuthSub token="' . $this->getAuthSubToken() . '" ' .
  200. 'data="' . $dataToSign . '" ' .
  201. 'sig="' . $encodedSignature . '" ' .
  202. 'sigalg="rsa-sha1"';
  203. } else {
  204. // AuthSub without secure tokens
  205. $headers['authorization'] = 'AuthSub token="' . $this->getAuthSubToken() . '"';
  206. }
  207. } elseif ($this->getClientLoginToken() != null) {
  208. $headers['authorization'] = 'GoogleLogin auth=' . $this->getClientLoginToken();
  209. }
  210. return array('method' => $method, 'url' => $url, 'body' => $body, 'headers' => $headers, 'contentType' => $contentType);
  211. }
  212. /**
  213. * Method for filtering the HTTP response, though no filtering is
  214. * currently done.
  215. *
  216. * @param Zend_Http_Response $response The response object to filter
  217. * @return Zend_Http_Response The filterd response object
  218. */
  219. public function filterHttpResponse($response) {
  220. return $response;
  221. }
  222. }