Current file: /Users/bill/git/HTTP_OAuth/HTTP/OAuth/Provider/Request.php
Legend: executed not executed dead code

  Coverage
  Classes Functions / Methods Lines
Total
100.00%100.00%
100.00% 1 / 1
100.00%100.00%
100.00% 14 / 14 CRAP
100.00%100.00%
100.00% 116 / 116
 
HTTP_OAuth_Provider_Request
100.00%100.00%
100.00% 1 / 1
100.00%100.00%
100.00% 14 / 14
100.00%100.00%
100.00% 116 / 116
 __construct()
100.00%100.00%
100.00% 1 / 1 1
100.00%100.00%
100.00% 3 / 3
 setHeaders(array $headers = array()
100.00%100.00%
100.00% 1 / 1 6
100.00%100.00%
100.00% 20 / 20
 apacheRequestHeaders()
100.00%100.00%
100.00% 1 / 1 2
100.00%100.00%
100.00% 3 / 3
 peclHttpHeaders()
100.00%100.00%
100.00% 1 / 1 3
100.00%100.00%
100.00% 4 / 4
 setParametersFromRequest()
100.00%100.00%
100.00% 1 / 1 9
100.00%100.00%
100.00% 37 / 37
 isValidSignature($consumerSecret, $tokenSecret = '')
100.00%100.00%
100.00% 1 / 1 3
100.00%100.00%
100.00% 13 / 13
 getQueryString()
100.00%100.00%
100.00% 1 / 1 2
100.00%100.00%
100.00% 3 / 3
 getRequestMethod()
100.00%100.00%
100.00% 1 / 1 2
100.00%100.00%
100.00% 3 / 3
 getUrl()
100.00%100.00%
100.00% 1 / 1 3
100.00%100.00%
100.00% 6 / 6
 getRequestUri()
100.00%100.00%
100.00% 1 / 1 3
100.00%100.00%
100.00% 7 / 7
 getHeader($header)
100.00%100.00%
100.00% 1 / 1 3
100.00%100.00%
100.00% 5 / 5
 getHeaders()
100.00%100.00%
100.00% 1 / 1 1
100.00%100.00%
100.00% 1 / 1
 getPostData()
100.00%100.00%
100.00% 1 / 1 1
100.00%100.00%
100.00% 1 / 1
 parseQueryString($string)
100.00%100.00%
100.00% 1 / 1 4
100.00%100.00%
100.00% 10 / 10


       1                 : <?php                                                                               
       2                 : /**                                                                                 
       3                 :  * HTTP_OAuth                                                                       
       4                 :  *                                                                                  
       5                 :  * PHP version 5.2.0+                                                               
       6                 :  *                                                                                  
       7                 :  * LICENSE: This source file is subject to the New BSD license that is              
       8                 :  * available through the world-wide-web at the following URI:                       
       9                 :  * http://www.opensource.org/licenses/bsd-license.php. If you did not receive       
      10                 :  * a copy of the New BSD License and are unable to obtain it through the web,       
      11                 :  * please send a note to license@php.net so we can mail you a copy immediately.     
      12                 :  *                                                                                  
      13                 :  * @category  HTTP                                                                  
      14                 :  * @package   HTTP_OAuth                                                            
      15                 :  * @author    Jeff Hodsdon <jeffhodsdon@gmail.com>                                  
      16                 :  * @copyright 2009 Jeff Hodsdon <jeffhodsdon@gmail.com>                             
      17                 :  * @license   http://www.opensource.org/licenses/bsd-license.php New BSD License    
      18                 :  * @link      http://pear.php.net/package/HTTP_OAuth                                
      19                 :  * @link      http://github.com/jeffhodsdon/HTTP_OAuth                              
      20                 :  */                                                                                 
      21                 :                                                                                     
      22                 : require_once 'HTTP/OAuth/Message.php';                                              
      23                 : require_once 'HTTP/OAuth/Signature.php';                                            
      24                 : require_once 'HTTP/OAuth/Provider/Exception/InvalidRequest.php';                    
      25                 :                                                                                     
      26                 : /**                                                                                 
      27                 :  * HTTP_OAuth_Provider_Request                                                      
      28                 :  *                                                                                  
      29                 :  * NOTE:                                                                            
      30                 :  * Currently, it's up to the developer to implement the provider side of            
      31                 :  * timestamp and nonce checking.                                                    
      32                 :  *                                                                                  
      33                 :  * @category  HTTP                                                                  
      34                 :  * @package   HTTP_OAuth                                                            
      35                 :  * @author    Jeff Hodsdon <jeffhodsdon@gmail.com>                                  
      36                 :  * @copyright 2009 Jeff Hodsdon <jeffhodsdon@gmail.com>                             
      37                 :  * @license   http://www.opensource.org/licenses/bsd-license.php New BSD License    
      38                 :  * @link      http://pear.php.net/package/HTTP_OAuth                                
      39                 :  * @link      http://github.com/jeffhodsdon/HTTP_OAuth                              
      40                 :  */                                                                                 
      41                 : class HTTP_OAuth_Provider_Request extends HTTP_OAuth_Message                        
      42                 : {                                                                                   
      43                 :                                                                                     
      44                 :     /**                                                                             
      45                 :      * Headers from the incoming request                                            
      46                 :      *                                                                              
      47                 :      * @var array $headers Headers from the incoming request                        
      48                 :      */                                                                             
      49                 :     protected $headers = array();                                                   
      50                 :                                                                                     
      51                 :     /**                                                                             
      52                 :      * Method used in the incoming request                                          
      53                 :      *                                                                               
      54                 :      * @var string Method used in the incoming request                              
      55                 :      */                                                                             
      56                 :     protected $method = '';                                                         
      57                 :                                                                                     
      58                 :     /**                                                                             
      59                 :      * Construct                                                                    
      60                 :      *                                                                              
      61                 :      * @return void                                                                 
      62                 :      */                                                                             
      63                 :     public function __construct()                                                   
      64                 :     {                                                                               
      65              14 :         $this->setHeaders();                                                        
      66              14 :         $this->setParametersFromRequest();                                          
      67              13 :     }                                                                               
      68                 :                                                                                     
      69                 :     /**                                                                             
      70                 :      * Set incoming request headers                                                 
      71                 :      *                                                                              
      72                 :      * @param array $headers Optional headers to set                                
      73                 :      *                                                                              
      74                 :      * @return void                                                                 
      75                 :      */                                                                             
      76                 :     public function setHeaders(array $headers = array())                            
      77                 :     {                                                                               
      78              14 :         if (count($headers)) {                                                      
      79               4 :             $this->headers = $headers;                                              
      80              14 :         } else if (is_array($this->apacheRequestHeaders())) {                       
      81               1 :             $this->debug('Using apache_request_headers() to get request headers');  
      82               1 :             $this->headers = $this->apacheRequestHeaders();                         
      83              14 :         } else if (is_array($this->peclHttpHeaders())) {                            
      84               1 :             $this->debug('Using pecl_http to get request headers');                 
      85               1 :             $this->headers = $this->peclHttpHeaders();                              
      86               1 :         } else {                                                                     
      87              13 :             $this->debug('Using $_SERVER to get request headers');                  
      88              13 :             foreach ($_SERVER as $name => $value) {                                 
      89              13 :                 if (substr($name, 0, 5) == 'HTTP_') {                               
      90              13 :                     $name = str_replace(                                            
      91              13 :                         ' ', '-',                                                   
      92              13 :                         ucwords(strtolower(str_replace('_', ' ', substr($name, 5))))
      93              13 :                     );                                                              
      94              13 :                     $this->headers[$name] = $value;                                 
      95              13 :                 }                                                                   
      96              13 :             }                                                                       
      97                 :         }                                                                           
      98              14 :     }                                                                               
      99                 :                                                                                     
     100                 :     /**                                                                             
     101                 :      * Apache request headers                                                       
     102                 :      *                                                                              
     103                 :      * If the function exists to get the request headers from apache                
     104                 :      * use it to get them, otherwise return null. Abstracted for                    
     105                 :      * testing purposes.                                                            
     106                 :      *                                                                              
     107                 :      * @return array|null Headers or null if no function                            
     108                 :      */                                                                             
     109                 :     protected function apacheRequestHeaders()                                       
     110                 :     {                                                                               
     111               1 :         if (function_exists('apache_request_headers')) {                            
     112                 :             // @codeCoverageIgnoreStart                                             
     113                 :             return apache_request_headers();                                        
     114                 :             // @codeCoverageIgnoreEnd                                               
     115                 :         }                                                                           
     116                 :                                                                                     
     117               1 :         return null;                                                                
     118                 :     }                                                                               
     119                 :                                                                                     
     120                 :     // @codeCoverageIgnoreStart                                                     
     121                 :     /**                                                                             
     122                 :      * Pecl HTTP request headers                                                    
     123                 :      *                                                                              
     124                 :      * If the pecl_http extension is loaded use it to get the incoming              
     125                 :      * request headers, otherwise return null. Abstracted for testing               
     126                 :      * purposes.                                                                    
     127                 :      *                                                                              
     128                 :      * @return array|null Headers or null if no extension                           
     129                 :      */                                                                             
     130                 :     protected function peclHttpHeaders()                                            
     131                 :     {                                                                               
     132                 :         if (extension_loaded('http') && class_exists('HttpMessage')) {              
     133                 :             $message = HttpMessage::fromEnv(HttpMessage::TYPE_REQUEST);             
     134                 :             return $message->getHeaders();                                          
     135                 :         }                                                                           
     136                 :                                                                                     
     137                 :         return null;                                                                
     138                 :     }                                                                               
     139                 :     // @codeCoverageIgnoreEnd                                                       
     140                 :                                                                                     
     141                 :     /**                                                                             
     142                 :      * Set parameters from the incoming request                                      
     143                 :      *                                                                               
     144                 :      * @return void                                                                 
     145                 :      */                                                                             
     146                 :     public function setParametersFromRequest()                                      
     147                 :     {                                                                               
     148              14 :         $params = array();                                                          
     149              14 :         $auth   = $this->getHeader('Authorization');                                
     150              14 :         if ($auth !== null) {                                                       
     151              13 :             $this->debug('Using OAuth data from header');                           
     152              13 :             $parts = explode(',', $auth);                                           
     153              13 :             foreach ($parts as $part) {                                             
     154              13 :                 list($key, $value) = explode('=', trim($part));                     
     155              13 :                 if (strstr(strtolower($key), 'oauth ')                              
     156              13 :                     || strstr(strtolower($key), 'uth re')                           
     157              13 :                     || substr(strtolower($key), 0, 6) != 'oauth_'                   
     158              13 :                 ) {                                                                 
     159              13 :                     continue;                                                       
     160                 :                 }                                                                   
     161                 :                                                                                     
     162              13 :                 $value = trim($value);                                              
     163              13 :                 $value = str_replace('"', '', $value);                              
     164                 :                                                                                     
     165              13 :                 $params[$key] = $value;                                             
     166              13 :             }                                                                       
     167              13 :         }                                                                           
     168                 :                                                                                     
     169              14 :         if ($this->getRequestMethod() == 'POST') {                                  
     170               2 :             $this->debug('getting data from POST');                                 
     171               2 :             $contentType = substr($this->getHeader('Content-Type'), 0, 33);         
     172               2 :             if ($contentType !== 'application/x-www-form-urlencoded') {             
     173               1 :                 throw new HTTP_OAuth_Provider_Exception_InvalidRequest('Invalid ' . 
     174               1 :                     'content type for POST request');                               
     175                 :             }                                                                       
     176                 :                                                                                     
     177               1 :             $params = array_merge(                                                  
     178               1 :                 $params,                                                            
     179               1 :                 $this->parseQueryString($this->getPostData())                       
     180               1 :             );                                                                      
     181               1 :         }                                                                           
     182                 :                                                                                     
     183              14 :         $params = array_merge(                                                      
     184              14 :             $params,                                                                
     185              14 :             $this->parseQueryString($this->getQueryString())                        
     186              14 :         );                                                                          
     187                 :                                                                                     
     188              14 :         if (empty($params)) {                                                       
     189               1 :             throw new HTTP_OAuth_Provider_Exception_InvalidRequest('No oauth ' .    
     190               1 :                 'data found from request');                                         
     191                 :         }                                                                           
     192                 :                                                                                     
     193              13 :         $this->setParameters(HTTP_OAuth::urldecode($params));                       
     194              13 :     }                                                                               
     195                 :                                                                                     
     196                 :     /**                                                                             
     197                 :      * Is valid signature                                                           
     198                 :      *                                                                              
     199                 :      * @param string $consumerSecret Consumer secret value                          
     200                 :      * @param string $tokenSecret    Token secret value (if exists)                 
     201                 :      *                                                                              
     202                 :      * @return bool Valid or not                                                    
     203                 :      */                                                                             
     204                 :     public function isValidSignature($consumerSecret, $tokenSecret = '')            
     205                 :     {                                                                               
     206               2 :         if (!$this->oauth_signature_method) {                                       
     207               1 :             throw new HTTP_OAuth_Provider_Exception_InvalidRequest(                 
     208                 :                 'Missing oauth_signature_method in request'                         
     209               1 :             );                                                                      
     210                 :         }                                                                           
     211                 :                                                                                     
     212               1 :         $sign  = HTTP_OAuth_Signature::factory($this->oauth_signature_method);      
     213               1 :         $check = $sign->build(                                                      
     214               1 :             $this->getRequestMethod(), $this->getUrl(),                             
     215               1 :             $this->getParameters(), $consumerSecret, $tokenSecret                   
     216               1 :         );                                                                          
     217                 :                                                                                     
     218               1 :         if ($this->oauth_signature === $check) {                                    
     219               1 :             $this->info('Valid signature');                                         
     220               1 :             return true;                                                            
     221                 :         }                                                                           
     222                 :                                                                                     
     223               1 :         $this->err('Invalid signature');                                            
     224               1 :         return false;                                                               
     225                 :                                                                                     
     226                 :     }                                                                               
     227                 :                                                                                     
     228                 :     /**                                                                             
     229                 :      * Gets incoming request query string                                           
     230                 :      *                                                                              
     231                 :      * @return string|null Query string                                             
     232                 :      */                                                                             
     233                 :     public function getQueryString()                                                
     234                 :     {                                                                               
     235              14 :         if (!empty($_SERVER['QUERY_STRING'])) {                                     
     236               1 :             return $_SERVER['QUERY_STRING'];                                        
     237                 :         }                                                                           
     238                 :                                                                                     
     239              14 :         return null;                                                                
     240                 :     }                                                                               
     241                 :                                                                                     
     242                 :     /**                                                                             
     243                 :      * Get request method                                                           
     244                 :      *                                                                              
     245                 :      * @return string Request method                                                
     246                 :      */                                                                             
     247                 :     public function getRequestMethod()                                              
     248                 :     {                                                                               
     249              12 :         if (!array_key_exists('REQUEST_METHOD', $_SERVER)) {                        
     250              12 :             return 'HEAD';                                                          
     251                 :         }                                                                           
     252                 :                                                                                     
     253               1 :         return $_SERVER['REQUEST_METHOD'];                                          
     254                 :     }                                                                               
     255                 :                                                                                     
     256                 :     /**                                                                             
     257                 :      * Get url                                                                      
     258                 :      *                                                                              
     259                 :      * @return string URL of the request                                            
     260                 :      */                                                                             
     261                 :     public function getUrl()                                                        
     262                 :     {                                                                               
     263               1 :         $schema = 'http';                                                           
     264               1 :         if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') {                
     265               1 :             $schema .= 's';                                                         
     266               1 :         }                                                                           
     267                 :                                                                                     
     268               1 :         return $schema . '://' . $this->getHeader('Host')                           
     269               1 :             . $this->getRequestUri();                                               
     270                 :     }                                                                               
     271                 :                                                                                     
     272                 :     /**                                                                             
     273                 :      * Gets incoming request URI                                                    
     274                 :      *                                                                              
     275                 :      * Checks if the schema/host is included and strips it.                         
     276                 :      * Thanks Naosumi! Bug #16800                                                   
     277                 :      *                                                                              
     278                 :      * @return string|null Request URI, null if doesn't exist                       
     279                 :      */                                                                             
     280                 :     public function getRequestUri()                                                 
     281                 :     {                                                                               
     282               3 :         if (!array_key_exists('REQUEST_URI', $_SERVER)) {                           
     283               2 :             return null;                                                            
     284                 :         }                                                                           
     285                 :                                                                                     
     286               2 :         $uri = $_SERVER['REQUEST_URI'];                                             
     287               2 :         $pos = stripos($uri, '://');                                                
     288               2 :         if (!$pos) {                                                                
     289               2 :             return $uri;                                                            
     290                 :         }                                                                           
     291                 :                                                                                     
     292               1 :         return substr($uri, strpos($uri, '/', $pos + 3));                           
     293                 :     }                                                                               
     294                 :                                                                                     
     295                 :     /**                                                                             
     296                 :      * Gets a header                                                                
     297                 :      *                                                                              
     298                 :      * @param string $header Which header to fetch                                  
     299                 :      *                                                                              
     300                 :      * @return string|null Header if exists, null if not                            
     301                 :      */                                                                             
     302                 :     public function getHeader($header)                                              
     303                 :     {                                                                               
     304              14 :         foreach ($this->headers as $name => $value) {                               
     305              13 :             if (strtolower($header) == strtolower($name)) {                         
     306              13 :                 return $value;                                                      
     307                 :             }                                                                       
     308               5 :         }                                                                           
     309                 :                                                                                     
     310               4 :         return null;                                                                
     311                 :     }                                                                               
     312                 :                                                                                     
     313                 :     /**                                                                             
     314                 :      * getHeaders                                                                    
     315                 :      *                                                                               
     316                 :      * @access public                                                               
     317                 :      * @return void                                                                 
     318                 :      */                                                                             
     319                 :     public function getHeaders()                                                    
     320                 :     {                                                                               
     321               3 :         return $this->headers;                                                      
     322                 :     }                                                                               
     323                 :                                                                                     
     324                 :     // @codeCoverageIgnoreStart                                                     
     325                 :     /**                                                                             
     326                 :      * Gets POST data                                                               
     327                 :      *                                                                              
     328                 :      * @return string Post data                                                     
     329                 :      */                                                                             
     330                 :     protected function getPostData()                                                
     331                 :     {                                                                               
     332                 :         return file_get_contents('php://input');                                    
     333                 :     }                                                                               
     334                 :     // @codeCoverageIgnoreEnd                                                       
     335                 :                                                                                     
     336                 :     /**                                                                             
     337                 :      * Parses a query string                                                        
     338                 :      *                                                                              
     339                 :      * Does not urldecode the name or values like $_GET and $_POST                  
     340                 :      *                                                                              
     341                 :      * @param string $string Query string                                           
     342                 :      *                                                                              
     343                 :      * @return array Data from the query string                                     
     344                 :      */                                                                             
     345                 :     protected function parseQueryString($string)                                    
     346                 :     {                                                                               
     347              14 :         $data = array();                                                            
     348              14 :         if (empty($string)) {                                                       
     349              14 :             return $data;                                                           
     350                 :         }                                                                           
     351                 :                                                                                     
     352               1 :         foreach (explode('&', $string) as $part) {                                  
     353               1 :             if (!strstr($part, '=')) {                                              
     354               1 :                 continue;                                                           
     355                 :             }                                                                       
     356                 :                                                                                     
     357               1 :             list($key, $value) = explode('=', $part);                               
     358               1 :             $data[$key] = self::urldecode($value);                                  
     359               1 :         }                                                                           
     360                 :                                                                                     
     361               1 :         return $data;                                                               
     362                 :     }                                                                               
     363                 :                                                                                     
     364                 : }                                                                                   
     365                 :                                                                                     
     366                 : ?>                                                                                  

Generated by PHP_CodeCoverage 1.0.2 using PHP 5.3.3 and PHPUnit 3.5.6 at Tue Dec 28 21:42:13 PST 2010.