Current file: /Users/bill/git/HTTP_OAuth/HTTP/OAuth/Provider/Response.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% 12 / 12 CRAP
100.00%100.00%
100.00% 44 / 44
 
HTTP_OAuth_Provider_Response
100.00%100.00%
100.00% 1 / 1
100.00%100.00%
100.00% 12 / 12
100.00%100.00%
100.00% 44 / 44
 setRealm($realm)
100.00%100.00%
100.00% 1 / 1 1
100.00%100.00%
100.00% 3 / 3
 setHeader($name, $value)
100.00%100.00%
100.00% 1 / 1 1
100.00%100.00%
100.00% 2 / 2
 getHeader($name)
100.00%100.00%
100.00% 1 / 1 2
100.00%100.00%
100.00% 3 / 3
 getHeaders()
100.00%100.00%
100.00% 1 / 1 1
100.00%100.00%
100.00% 1 / 1
 setHeaders(array $headers)
100.00%100.00%
100.00% 1 / 1 1
100.00%100.00%
100.00% 2 / 2
 setStatus($status)
100.00%100.00%
100.00% 1 / 1 5
100.00%100.00%
100.00% 13 / 13
 headersSent()
100.00%100.00%
100.00% 1 / 1 1
100.00%100.00%
100.00% 1 / 1
 header($header)
100.00%100.00%
100.00% 1 / 1 1
100.00%100.00%
100.00% 1 / 1
 prepareBody()
100.00%100.00%
100.00% 1 / 1 3
100.00%100.00%
100.00% 5 / 5
 setBody($body)
100.00%100.00%
100.00% 1 / 1 1
100.00%100.00%
100.00% 2 / 2
 getBody()
100.00%100.00%
100.00% 1 / 1 1
100.00%100.00%
100.00% 1 / 1
 send()
100.00%100.00%
100.00% 1 / 1 3
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                 :                                                                                 
      24                 : /**                                                                             
      25                 :  * HTTP_OAuth_Provider_Response                                                 
      26                 :  *                                                                              
      27                 :  * @category  HTTP                                                              
      28                 :  * @package   HTTP_OAuth                                                        
      29                 :  * @author    Jeff Hodsdon <jeffhodsdon@gmail.com>                              
      30                 :  * @copyright 2009 Jeff Hodsdon <jeffhodsdon@gmail.com>                         
      31                 :  * @license   http://www.opensource.org/licenses/bsd-license.php New BSD License
      32                 :  * @link      http://pear.php.net/package/HTTP_OAuth                            
      33                 :  * @link      http://github.com/jeffhodsdon/HTTP_OAuth                          
      34                 :  */                                                                             
      35                 : class HTTP_OAuth_Provider_Response extends HTTP_OAuth_Message                   
      36                 : {                                                                               
      37                 :                                                                                 
      38                 :     const STATUS_UNSUPPORTED_PARAMETER        = 0;                              
      39                 :     const STATUS_UNSUPPORTED_SIGNATURE_METHOD = 1;                              
      40                 :     const STATUS_MISSING_REQUIRED_PARAMETER   = 2;                              
      41                 :     const STATUS_DUPLICATED_OAUTH_PARAMETER   = 3;                              
      42                 :                                                                                 
      43                 :     const STATUS_INVALID_CONSUMER_KEY = 4;                                      
      44                 :     const STATUS_INVALID_TOKEN        = 5;                                      
      45                 :     const STATUS_INVALID_SIGNATURE    = 6;                                      
      46                 :     const STATUS_INVALID_NONCE        = 7;                                      
      47                 :     const STATUS_INVALID_VERIFIER     = 8;                                      
      48                 :     const STATUS_INVALID_TIMESTAMP    = 9;                                      
      49                 :                                                                                 
      50                 :     /**                                                                         
      51                 :      * Status map                                                               
      52                 :      *                                                                          
      53                 :      * Map of what statuses have codes and body text                            
      54                 :      *                                                                          
      55                 :      * @var array $statusMap Map of status to code and text                     
      56                 :      */                                                                         
      57                 :     static protected $statusMap = array(                                        
      58                 :         self::STATUS_UNSUPPORTED_PARAMETER => array(                            
      59                 :             400, 'Unsupported parameter'                                        
      60                 :         ),                                                                      
      61                 :         self::STATUS_UNSUPPORTED_SIGNATURE_METHOD => array(                     
      62                 :             400, 'Unsupported signature method'                                 
      63                 :         ),                                                                      
      64                 :         self::STATUS_MISSING_REQUIRED_PARAMETER => array(                       
      65                 :             400, 'Missing required parameter'                                   
      66                 :         ),                                                                      
      67                 :         self::STATUS_DUPLICATED_OAUTH_PARAMETER => array(                       
      68                 :             400, 'Duplicated OAuth Protocol Parameter'                          
      69                 :         ),                                                                      
      70                 :         self::STATUS_INVALID_CONSUMER_KEY => array(                             
      71                 :             401, 'Invalid Consumer Key'                                         
      72                 :         ),                                                                      
      73                 :         self::STATUS_INVALID_TOKEN => array(                                    
      74                 :             401, 'Invalid / expired Token'                                      
      75                 :         ),                                                                      
      76                 :         self::STATUS_INVALID_SIGNATURE => array(                                
      77                 :             401, 'Invalid signature'                                            
      78                 :         ),                                                                      
      79                 :         self::STATUS_INVALID_NONCE => array(                                    
      80                 :             401, 'Invalid / used nonce'                                         
      81                 :         ),                                                                      
      82                 :         self::STATUS_INVALID_VERIFIER => array(                                 
      83                 :             401, 'Invalid verifier'                                             
      84                 :         ),                                                                      
      85                 :         self::STATUS_INVALID_TIMESTAMP => array(                                
      86                 :             401, 'Invalid timestamp'                                            
      87                 :         ),                                                                      
      88                 :     );                                                                          
      89                 :                                                                                 
      90                 :     /**                                                                         
      91                 :      * Headers to be sent the OAuth response                                    
      92                 :      *                                                                          
      93                 :      * @var array $headers Headers to send as an OAuth response                 
      94                 :      */                                                                         
      95                 :     protected $headers = array();                                               
      96                 :                                                                                 
      97                 :     /**                                                                         
      98                 :      * Body of the response                                                     
      99                 :      *                                                                          
     100                 :      * @var string $body Body of the response                                   
     101                 :      */                                                                         
     102                 :     protected $body = '';                                                       
     103                 :                                                                                 
     104                 :     /**                                                                         
     105                 :      * Set realm                                                                
     106                 :      *                                                                          
     107                 :      * @param string $realm Realm for the WWW-Authenticate header               
     108                 :      *                                                                          
     109                 :      * @return void                                                             
     110                 :      */                                                                         
     111                 :     public function setRealm($realm)                                            
     112                 :     {                                                                           
     113               1 :         $header = 'OAuth realm="' . $realm . '"';                               
     114               1 :         $this->setHeader('WWW-Authenticate', $header);                          
     115               1 :     }                                                                           
     116                 :                                                                                 
     117                 :     /**                                                                         
     118                 :      * Set header                                                               
     119                 :      *                                                                          
     120                 :      * @param string $name  Name of the header                                  
     121                 :      * @param string $value Value of the header                                 
     122                 :      *                                                                          
     123                 :      * @return void                                                             
     124                 :      */                                                                         
     125                 :     public function setHeader($name, $value)                                    
     126                 :     {                                                                           
     127               2 :         $this->headers[$name] = $value;                                         
     128               2 :     }                                                                           
     129                 :                                                                                 
     130                 :     /**                                                                         
     131                 :      * Get header                                                               
     132                 :      *                                                                          
     133                 :      * @param string $name Name of header                                       
     134                 :      *                                                                          
     135                 :      * @return string|null Header if exists, null if not                        
     136                 :      */                                                                         
     137                 :     public function getHeader($name)                                            
     138                 :     {                                                                           
     139               1 :         if (array_key_exists($name, $this->headers)) {                          
     140               1 :             return $this->headers[$name];                                       
     141                 :         }                                                                       
     142                 :                                                                                 
     143               1 :         return null;                                                            
     144                 :     }                                                                           
     145                 :                                                                                 
     146                 :     /**                                                                         
     147                 :      * Get all headers                                                          
     148                 :      *                                                                          
     149                 :      * @return array Current headers to send                                    
     150                 :      */                                                                         
     151                 :     public function getHeaders()                                                
     152                 :     {                                                                           
     153               3 :         return $this->headers;                                                  
     154                 :     }                                                                           
     155                 :                                                                                 
     156                 :     /**                                                                         
     157                 :      * Set all headers                                                          
     158                 :      *                                                                          
     159                 :      * @param array $headers Sets all headers to this name/value array          
     160                 :      *                                                                          
     161                 :      * @return void                                                             
     162                 :      */                                                                         
     163                 :     public function setHeaders(array $headers)                                  
     164                 :     {                                                                           
     165               1 :         $this->headers = $headers;                                              
     166               1 :     }                                                                           
     167                 :                                                                                 
     168                 :     /**                                                                         
     169                 :      * Set status                                                               
     170                 :      *                                                                          
     171                 :      * @param int $status Status constant                                       
     172                 :      *                                                                          
     173                 :      * @return void                                                             
     174                 :      */                                                                         
     175                 :     public function setStatus($status)                                          
     176                 :     {                                                                           
     177               3 :         if (!array_key_exists($status, self::$statusMap)) {                     
     178               1 :             throw new HTTP_OAuth_Exception('Invalid status');                   
     179                 :         }                                                                       
     180                 :                                                                                 
     181               2 :         list($code, $text) = self::$statusMap[$status];                         
     182               2 :         $this->setBody($text);                                                  
     183                 :                                                                                 
     184               2 :         if ($this->headersSent()) {                                             
     185               1 :             throw new HTTP_OAuth_Exception('Status already sent');              
     186                 :         }                                                                       
     187                 :                                                                                 
     188                 :         switch ($code) {                                                        
     189               1 :         case 400:                                                               
     190               1 :             $this->header('HTTP/1.1 400 Bad Request');                          
     191               1 :             break;                                                              
     192               1 :         case 401:                                                               
     193               1 :             $this->header('HTTP/1.1 401 Unauthorized');                         
     194               1 :             break;                                                              
     195                 :         }                                                                       
     196               1 :     }                                                                           
     197                 :                                                                                 
     198                 :     // @codeCoverageIgnoreStart                                                 
     199                 :     /**                                                                         
     200                 :      * Headers sent                                                             
     201                 :      *                                                                          
     202                 :      * @return bool If the headers have been sent                               
     203                 :      */                                                                         
     204                 :     protected function headersSent()                                            
     205                 :     {                                                                           
     206                 :         return headers_sent();                                                  
     207                 :     }                                                                           
     208                 :                                                                                 
     209                 :     /**                                                                         
     210                 :      * Header                                                                   
     211                 :      *                                                                          
     212                 :      * @param string $header Header to add                                      
     213                 :      *                                                                          
     214                 :      * @return void                                                             
     215                 :      */                                                                         
     216                 :     protected function header($header)                                          
     217                 :     {                                                                           
     218                 :         return header($header);                                                 
     219                 :     }                                                                           
     220                 :     // @codeCoverageIgnoreEnd                                                   
     221                 :                                                                                 
     222                 :     /**                                                                         
     223                 :      * Prepare body                                                             
     224                 :      *                                                                          
     225                 :      * Sets the body if nesscary                                                
     226                 :      *                                                                          
     227                 :      * @return void                                                             
     228                 :      */                                                                         
     229                 :     protected function prepareBody()                                            
     230                 :     {                                                                           
     231               1 :         if ($this->headersSent() && $this->getBody() !== '') {                  
     232               1 :             $this->err('Body already sent, not setting');                       
     233               1 :         } else {                                                                
     234               1 :             $this->setBody(HTTP_OAuth::buildHTTPQuery($this->getParameters())); 
     235                 :         }                                                                       
     236               1 :     }                                                                           
     237                 :                                                                                 
     238                 :     /**                                                                         
     239                 :      * Set body                                                                 
     240                 :      *                                                                          
     241                 :      * @param string $body Sets the body to send                                
     242                 :      *                                                                          
     243                 :      * @return void                                                             
     244                 :      */                                                                         
     245                 :     public function setBody($body)                                              
     246                 :     {                                                                           
     247               3 :         $this->body = $body;                                                    
     248               3 :     }                                                                           
     249                 :                                                                                 
     250                 :     /**                                                                         
     251                 :      * Get body                                                                 
     252                 :      *                                                                          
     253                 :      * @return string Body that will be sent                                    
     254                 :      */                                                                         
     255                 :     public function getBody()                                                   
     256                 :     {                                                                           
     257               2 :         return $this->body;                                                     
     258                 :     }                                                                           
     259                 :                                                                                 
     260                 :     /**                                                                         
     261                 :      * Send response                                                            
     262                 :      *                                                                          
     263                 :      * Does a check whether or not headers have been sent in order              
     264                 :      * to determine if it can send them.                                        
     265                 :      *                                                                          
     266                 :      * @return void                                                             
     267                 :      */                                                                         
     268                 :     public function send()                                                      
     269                 :     {                                                                           
     270               1 :         $this->prepareBody();                                                   
     271               1 :         if (!$this->headersSent()) {                                            
     272               1 :             $this->header('HTTP/1.1 200 OK');                                   
     273               1 :             foreach ($this->getHeaders() as $name => $value) {                  
     274                 :                 // @codeCoverageIgnoreStart                                     
     275                 :                 $this->header($name . ': ' . $value);                           
     276                 :                 // @codeCoverageIgnoreEnd                                       
     277               1 :             }                                                                   
     278               1 :         } else {                                                                
     279               1 :             $this->err('Headers already sent, can not send headers');           
     280                 :         }                                                                       
     281                 :                                                                                 
     282               1 :         echo $this->getBody();                                                  
     283               1 :     }                                                                           
     284                 :                                                                                 
     285                 : }                                                                               
     286                 :                                                                                 
     287                 : ?>                                                                              

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.