PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/src/application/libraries/Zend/Oauth/Http/RequestToken.php

https://bitbucket.org/masnug/grc276-blog-laravel
PHP | 162 lines | 86 code | 12 blank | 64 comment | 5 complexity | e6de7b828ba1cc49769a03768cf0bdc0 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_Oauth
  17. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: RequestToken.php 23775 2011-03-01 17:25:24Z ralph $
  20. */
  21. /** Zend_Oauth_Http */
  22. require_once 'Zend/Oauth/Http.php';
  23. /** Zend_Oauth_Token_Request */
  24. require_once 'Zend/Oauth/Token/Request.php';
  25. /**
  26. * @category Zend
  27. * @package Zend_Oauth
  28. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. */
  31. class Zend_Oauth_Http_RequestToken extends Zend_Oauth_Http
  32. {
  33. /**
  34. * Singleton instance if required of the HTTP client
  35. *
  36. * @var Zend_Http_Client
  37. */
  38. protected $_httpClient = null;
  39. /**
  40. * Initiate a HTTP request to retrieve a Request Token.
  41. *
  42. * @return Zend_Oauth_Token_Request
  43. */
  44. public function execute()
  45. {
  46. $params = $this->assembleParams();
  47. $response = $this->startRequestCycle($params);
  48. $return = new Zend_Oauth_Token_Request($response);
  49. return $return;
  50. }
  51. /**
  52. * Assemble all parameters for an OAuth Request Token request.
  53. *
  54. * @return array
  55. */
  56. public function assembleParams()
  57. {
  58. $params = array(
  59. 'oauth_consumer_key' => $this->_consumer->getConsumerKey(),
  60. 'oauth_nonce' => $this->_httpUtility->generateNonce(),
  61. 'oauth_timestamp' => $this->_httpUtility->generateTimestamp(),
  62. 'oauth_signature_method' => $this->_consumer->getSignatureMethod(),
  63. 'oauth_version' => $this->_consumer->getVersion(),
  64. );
  65. // indicates we support 1.0a
  66. if ($this->_consumer->getCallbackUrl()) {
  67. $params['oauth_callback'] = $this->_consumer->getCallbackUrl();
  68. } else {
  69. $params['oauth_callback'] = 'oob';
  70. }
  71. if (!empty($this->_parameters)) {
  72. $params = array_merge($params, $this->_parameters);
  73. }
  74. $params['oauth_signature'] = $this->_httpUtility->sign(
  75. $params,
  76. $this->_consumer->getSignatureMethod(),
  77. $this->_consumer->getConsumerSecret(),
  78. null,
  79. $this->_preferredRequestMethod,
  80. $this->_consumer->getRequestTokenUrl()
  81. );
  82. return $params;
  83. }
  84. /**
  85. * Generate and return a HTTP Client configured for the Header Request Scheme
  86. * specified by OAuth, for use in requesting a Request Token.
  87. *
  88. * @param array $params
  89. * @return Zend_Http_Client
  90. */
  91. public function getRequestSchemeHeaderClient(array $params)
  92. {
  93. $headerValue = $this->_httpUtility->toAuthorizationHeader(
  94. $params
  95. );
  96. $client = Zend_Oauth::getHttpClient();
  97. $client->setUri($this->_consumer->getRequestTokenUrl());
  98. $client->setHeaders('Authorization', $headerValue);
  99. $rawdata = $this->_httpUtility->toEncodedQueryString($params, true);
  100. if (!empty($rawdata)) {
  101. $client->setRawData($rawdata, 'application/x-www-form-urlencoded');
  102. }
  103. $client->setMethod($this->_preferredRequestMethod);
  104. return $client;
  105. }
  106. /**
  107. * Generate and return a HTTP Client configured for the POST Body Request
  108. * Scheme specified by OAuth, for use in requesting a Request Token.
  109. *
  110. * @param array $params
  111. * @return Zend_Http_Client
  112. */
  113. public function getRequestSchemePostBodyClient(array $params)
  114. {
  115. $client = Zend_Oauth::getHttpClient();
  116. $client->setUri($this->_consumer->getRequestTokenUrl());
  117. $client->setMethod($this->_preferredRequestMethod);
  118. $client->setRawData(
  119. $this->_httpUtility->toEncodedQueryString($params)
  120. );
  121. $client->setHeaders(
  122. Zend_Http_Client::CONTENT_TYPE,
  123. Zend_Http_Client::ENC_URLENCODED
  124. );
  125. return $client;
  126. }
  127. /**
  128. * Attempt a request based on the current configured OAuth Request Scheme and
  129. * return the resulting HTTP Response.
  130. *
  131. * @param array $params
  132. * @return Zend_Http_Response
  133. */
  134. protected function _attemptRequest(array $params)
  135. {
  136. switch ($this->_preferredRequestScheme) {
  137. case Zend_Oauth::REQUEST_SCHEME_HEADER:
  138. $httpClient = $this->getRequestSchemeHeaderClient($params);
  139. break;
  140. case Zend_Oauth::REQUEST_SCHEME_POSTBODY:
  141. $httpClient = $this->getRequestSchemePostBodyClient($params);
  142. break;
  143. case Zend_Oauth::REQUEST_SCHEME_QUERYSTRING:
  144. $httpClient = $this->getRequestSchemeQueryStringClient($params,
  145. $this->_consumer->getRequestTokenUrl());
  146. break;
  147. }
  148. return $httpClient->request();
  149. }
  150. }