src/Request.php
53e0de50
 <?php
48eb99eb
 
 namespace elanpl\L3;
 
 class Request{
c6173b1c
     public $path;
     public $method;
     public $accept;
     public $acceptTypes;
     public $acceptLanguage;
     public $userAgent;
     public $headers;
     public $body;
48eb99eb
     public function __construct(){
         if(isset($_GET['path']))
c6173b1c
             $this->path = $_GET['path'];
         $this->method = $_SERVER['REQUEST_METHOD'];
         $this->accept = $_SERVER['HTTP_ACCEPT'];
         $this->acceptTypes = $this->parseAccept($this->accept);
         $this->acceptLanguage = isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])?$_SERVER['HTTP_ACCEPT_LANGUAGE']:"";
         $this->userAgent = $_SERVER['HTTP_USER_AGENT'];
48eb99eb
         if(function_exists('apache_request_headers')){
c6173b1c
             $this->headers = apache_request_headers();
48eb99eb
         }
         else{
c6173b1c
             $this->headers = array();
48eb99eb
             foreach($_SERVER as $key => $value) {
                 if (substr($key, 0, 5) <> 'HTTP_') {
                     continue;
                 }
                 $header = str_replace(' ', '-', ucwords(str_replace('_', ' ', strtolower(substr($key, 5)))));
c6173b1c
                 $this->headers[$header] = $value;
48eb99eb
             }
         }
a2bd90a3
         if (php_sapi_name() != "cli") {
c6173b1c
             $this->body = file_get_contents('php://input');
a2bd90a3
         }
48eb99eb
     }
 
c6173b1c
     public function parseAccept($accept){
48eb99eb
         //cut out the spaces
         $accept = str_replace(" ", "", $accept);
 
         $result = array();
         $quality_factors = array();
 
         // find content type and corresponding quality factor value
c6173b1c
         foreach(explode(",", $accept) as $acceptParts){
             $type = explode(";", $acceptParts);
48eb99eb
             $result[] = $type[0];
             if(count($type)>1){
b0b06371
                 if(isset($type[1]) && $type[1][0].$type[1][1] == "q="){
48eb99eb
                     $quality_factors[] = substr($type[1],2);
                 }
b0b06371
                 else if(isset($type[2]) && $type[2][0].$type[2][1] == "q="){
48eb99eb
                     $quality_factors[] = substr($type[2],2);
                 }
                 else{
                     $quality_factors[] = 1;
                 }
             }
             else{
                 $quality_factors[] = 1;
             }
         }
 
         // sort the types according to quality factors
         array_multisort($quality_factors, SORT_DESC, SORT_NUMERIC, $result);
 
         return $result;
     }
 
 }