File indexing completed on 2024-05-26 06:03:14

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Oauth
0017  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0018  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0019  * @version    $Id$
0020  */
0021 
0022 /** Zend_Oauth_Http */
0023 // require_once 'Zend/Oauth/Http.php';
0024 
0025 /** Zend_Oauth_Token_Request */
0026 // require_once 'Zend/Oauth/Token/Request.php';
0027 
0028 /**
0029  * @category   Zend
0030  * @package    Zend_Oauth
0031  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0032  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0033  */
0034 class Zend_Oauth_Http_RequestToken extends Zend_Oauth_Http
0035 {
0036     /**
0037      * Singleton instance if required of the HTTP client
0038      *
0039      * @var Zend_Http_Client
0040      */
0041     protected $_httpClient = null;
0042 
0043     /**
0044      * Initiate a HTTP request to retrieve a Request Token.
0045      *
0046      * @return Zend_Oauth_Token_Request
0047      */
0048     public function execute()
0049     {
0050         $params   = $this->assembleParams();
0051         $response = $this->startRequestCycle($params);
0052         $return   = new Zend_Oauth_Token_Request($response);
0053         return $return;
0054     }
0055 
0056     /**
0057      * Assemble all parameters for an OAuth Request Token request.
0058      *
0059      * @return array
0060      */
0061     public function assembleParams()
0062     {
0063         $params = array(
0064             'oauth_consumer_key'     => $this->_consumer->getConsumerKey(),
0065             'oauth_nonce'            => $this->_httpUtility->generateNonce(),
0066             'oauth_timestamp'        => $this->_httpUtility->generateTimestamp(),
0067             'oauth_signature_method' => $this->_consumer->getSignatureMethod(),
0068             'oauth_version'          => $this->_consumer->getVersion(),
0069         );
0070 
0071         // indicates we support 1.0a
0072         if ($this->_consumer->getCallbackUrl()) {
0073             $params['oauth_callback'] = $this->_consumer->getCallbackUrl();
0074         } else {
0075             $params['oauth_callback'] = 'oob';
0076         }
0077 
0078         if (!empty($this->_parameters)) {
0079             $params = array_merge($params, $this->_parameters);
0080         }
0081 
0082         $params['oauth_signature'] = $this->_httpUtility->sign(
0083             $params,
0084             $this->_consumer->getSignatureMethod(),
0085             $this->_consumer->getConsumerSecret(),
0086             null,
0087             $this->_preferredRequestMethod,
0088             $this->_consumer->getRequestTokenUrl()
0089         );
0090 
0091         return $params;
0092     }
0093 
0094     /**
0095      * Generate and return a HTTP Client configured for the Header Request Scheme
0096      * specified by OAuth, for use in requesting a Request Token.
0097      *
0098      * @param array $params
0099      * @return Zend_Http_Client
0100      */
0101     public function getRequestSchemeHeaderClient(array $params)
0102     {
0103         $headerValue = $this->_httpUtility->toAuthorizationHeader(
0104             $params
0105         );
0106         $client = Zend_Oauth::getHttpClient();
0107         $client->setUri($this->_consumer->getRequestTokenUrl());
0108         $client->setHeaders('Authorization', $headerValue);
0109         $rawdata = $this->_httpUtility->toEncodedQueryString($params, true);
0110         if (!empty($rawdata)) {
0111             $client->setRawData($rawdata, 'application/x-www-form-urlencoded');
0112         }
0113         $client->setMethod($this->_preferredRequestMethod);
0114         return $client;
0115     }
0116 
0117     /**
0118      * Generate and return a HTTP Client configured for the POST Body Request
0119      * Scheme specified by OAuth, for use in requesting a Request Token.
0120      *
0121      * @param  array $params
0122      * @return Zend_Http_Client
0123      */
0124     public function getRequestSchemePostBodyClient(array $params)
0125     {
0126         $client = Zend_Oauth::getHttpClient();
0127         $client->setUri($this->_consumer->getRequestTokenUrl());
0128         $client->setMethod($this->_preferredRequestMethod);
0129         $client->setRawData(
0130             $this->_httpUtility->toEncodedQueryString($params)
0131         );
0132         $client->setHeaders(
0133             Zend_Http_Client::CONTENT_TYPE,
0134             Zend_Http_Client::ENC_URLENCODED
0135         );
0136         return $client;
0137     }
0138 
0139     /**
0140      * Attempt a request based on the current configured OAuth Request Scheme and
0141      * return the resulting HTTP Response.
0142      *
0143      * @param  array $params
0144      * @return Zend_Http_Response
0145      */
0146     protected function _attemptRequest(array $params)
0147     {
0148         switch ($this->_preferredRequestScheme) {
0149             case Zend_Oauth::REQUEST_SCHEME_HEADER:
0150                 $httpClient = $this->getRequestSchemeHeaderClient($params);
0151                 break;
0152             case Zend_Oauth::REQUEST_SCHEME_POSTBODY:
0153                 $httpClient = $this->getRequestSchemePostBodyClient($params);
0154                 break;
0155             case Zend_Oauth::REQUEST_SCHEME_QUERYSTRING:
0156                 $httpClient = $this->getRequestSchemeQueryStringClient($params,
0157                     $this->_consumer->getRequestTokenUrl());
0158                 break;
0159         }
0160         return $httpClient->request();
0161     }
0162 }