Advertisement
Danack

HTTPRequest

Nov 1st, 2013
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.51 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4. namespace Intahwebz\Router;
  5.  
  6.  
  7. class HTTPRequest extends AbstractRequest {
  8.  
  9.     private $referedParams = array();
  10.    
  11.     private $files = array();
  12.    
  13.     private $server;
  14.    
  15.     function __construct(
  16.         $server,// $_SERVER,
  17.         $get,//$_GET,
  18.         $post,//$_POST,
  19.         $files,//$_FILES,
  20.         $cookie//$_COOKIE
  21.     ) {
  22.         $this->determineHostName($server);
  23.         $this->determineScheme($server);
  24.         $this->determineMethod($server);
  25.         $this->determineFormat();
  26.         $this->determineRequestParams($get, $post);
  27.         $this->determineRefererParams($server);
  28.         $this->determinePath($server);
  29.         $this->determinePort($server);
  30.         $this->determineClientIP($server);
  31.  
  32.         $this->getNormalizedFILES($files);
  33.         $this->server = $server;
  34.     }
  35.  
  36.     function    determineScheme(){
  37.         $this->scheme = 'http';
  38.     }
  39.  
  40.     function    determineRefererParams($server){
  41.         $this->referedParams = array();
  42.  
  43.         if(array_key_exists('HTTP_REFERER', $server) == true){
  44.             $queryString = parse_url($_SERVER['HTTP_REFERER'], PHP_URL_QUERY);
  45.             $urlInfo = parse_url($queryString, PHP_URL_QUERY);
  46.             parse_str($urlInfo, $this->referedParams);
  47.         }
  48.     }
  49.  
  50.  
  51.     function    determineMethod($server){
  52.  
  53.         $this->method = 'GET';
  54.  
  55.         if(array_key_exists('REQUEST_METHOD', $server) == true){
  56.             $this->method = mb_strtoupper($server['REQUEST_METHOD']);
  57.         }
  58.  
  59. //      if ('POST' === $this->method) {
  60. //          'X-HTTP-METHOD-OVERRIDE',
  61. //          '_method',
  62. //          $this->query->get('_method', 'POST'))));
  63. //        }
  64.     }
  65.  
  66.     function    determineFormat(){
  67.         $this->requestedFormat = 'default';
  68.     }
  69.  
  70.     function    determineHostName($server){
  71.  
  72.         $this->hostName = 'localhost';
  73.  
  74.         if(array_key_exists('HTTP_HOST', $server)){
  75.             $this->hostName = $server['HTTP_HOST'];
  76.         }
  77.     }
  78.  
  79.  
  80.     function    determineRequestParams($get, $post){
  81.         $this->requestParams = array_merge($get, $post);
  82.     }
  83.  
  84.     function    determinePort($server){
  85.  
  86.         $this->port = 80;
  87.  
  88.         if(array_key_exists('SERVER_PORT', $server)){
  89.             $this->port = intval($server['SERVER_PORT']);
  90.         }
  91.  
  92. //      if ($this->trustProxy && $this->headers->has('X-Forwarded-Port')) {
  93. //          return $this->headers->get('X-Forwarded-Port');
  94. //      }
  95.     }
  96.  
  97.  
  98.     function    determinePath($server){
  99.         $this->path = '/';
  100.  
  101.         if(array_key_exists('REQUEST_URI', $server)){
  102.             $uriInfo = parse_url($server['REQUEST_URI']);
  103.  
  104.             $this->path = $uriInfo['path'];
  105. //          $this->query =  $uriInfo['query'];
  106.         }
  107.     }
  108.  
  109.     function    determineClientIP($server){
  110.  
  111.         $headerArray = array(
  112.             'HTTP_CLIENT_IP',
  113.             'HTTP_X_FORWARDED_FOR',
  114.             'HTTP_X_FORWARDED',
  115.             'HTTP_X_CLUSTER_CLIENT_IP',
  116.             'HTTP_FORWARDED_FOR',
  117.             'HTTP_FORWARDED',
  118.             'REMOTE_ADDR'
  119.         );
  120.  
  121.         foreach ($headerArray as $key){
  122.             if (array_key_exists($key, $server) === true){
  123.                 foreach (explode(',', $server[$key]) as $ip){
  124.                     $ip = trim($ip); // just to be safe
  125.  
  126.                     if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) !== false){
  127.  
  128.                         $this->clientIP = $ip;
  129.                         return;
  130.                     }
  131.                 }
  132.             }
  133.         }
  134.     }
  135.  
  136.  
  137.  
  138.     function getNormalizedFILES($files){
  139.         foreach($files as $fieldName => $fieldValue){
  140.             foreach($fieldValue as $paramName => $paramValue){
  141.                 foreach((array)$paramValue as $value){
  142.                     $this->files[$fieldName][$paramName] = $value;
  143.                 }
  144.             }
  145.         }
  146.     }
  147.  
  148.     function getFileUploadErrorMeaning($errorCode){
  149.  
  150.         switch($errorCode){
  151.             case (UPLOAD_ERR_OK):{ //no error; possible file attack!
  152.                 return "There was a problem with your upload.";
  153.             }
  154.             case (UPLOAD_ERR_INI_SIZE): {//uploaded file exceeds the upload_max_filesize directive in php.ini
  155.                 return "The file you are trying to upload is too big.";
  156.             }
  157.             case (UPLOAD_ERR_FORM_SIZE):{ //uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the html form
  158.                 return "The file you are trying to upload is too big.";
  159.             }
  160.             case UPLOAD_ERR_PARTIAL: {//uploaded file was only partially uploaded
  161.                 //Todo - allow partial uploads
  162.                 return  "The file you are trying upload was only partially uploaded.";
  163.             }
  164.             case (UPLOAD_ERR_NO_FILE): {//no file was uploaded
  165.                 return  "You must select a file for upload.";
  166.             }
  167.  
  168.             //TODO - handle these
  169. //          UPLOAD_ERR_NO_TMP_DIR
  170. //          UPLOAD_ERR_CANT_WRITE
  171. //          UPLOAD_ERR_EXTENSION
  172.  
  173.             default: {  //a default error, just in case!  :)
  174.             return  "There was a problem with your upload, error code is ".$errorCode;
  175.             }
  176.         }
  177.     }
  178.  
  179.  
  180.     /**
  181.      * @param $formFileName
  182.      * @return \Intahwebz\Utils\UploadedFile
  183.      * @throws \Intahwebz\Utils\FileUploadException
  184.      * @throws \Exception
  185.      */
  186.     function getUploadedFile($formFileName){
  187.  
  188.         if(isset($this->files[$formFileName]) == FALSE){
  189.             return false;
  190.         }
  191.         else{
  192.             if($this->files[$formFileName]['error'] == UPLOAD_ERR_OK) {
  193.                 if(is_uploaded_file($this->files[$formFileName]['tmp_name']) ){
  194.                     return new \Intahwebz\Utils\UploadedFile(
  195.                         $this->files[$formFileName]['name'],
  196.                         $this->files[$formFileName]['tmp_name'],
  197.                         $this->files[$formFileName]['size']
  198.                     );
  199.                 }
  200.                 else{
  201.                     throw new \Intahwebz\Utils\FileUploadException("File not uploaded. Status [".$this->files[$formFileName]['error']."] indicated error.");
  202.                 }
  203.             }
  204.             else{
  205.                 throw new \Intahwebz\Utils\FileUploadException("Error detected in upload: ".$this->getFileUploadErrorMeaning($this->files[$formFileName]['error']));
  206.             }
  207.         }
  208.     }
  209.  
  210.     function getReferrer() {
  211.         if(array_key_exists("HTTP_REFERER", $this->server)) {
  212.             return $this->server["HTTP_REFERER"];
  213.         }
  214.         return null;
  215.     }
  216. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement