Apache Zeta Components Manual :: File Source for http.php

Source for file http.php

Documentation is available at http.php

  1. <?php
  2. /**
  3.  * File containing the ezcMvcHttpRequestParser class
  4.  *
  5.  * Licensed to the Apache Software Foundation (ASF) under one
  6.  * or more contributor license agreements.  See the NOTICE file
  7.  * distributed with this work for additional information
  8.  * regarding copyright ownership.  The ASF licenses this file
  9.  * to you under the Apache License, Version 2.0 (the
  10.  * "License"); you may not use this file except in compliance
  11.  * with the License.  You may obtain a copy of the License at
  12.  * 
  13.  *   http://www.apache.org/licenses/LICENSE-2.0
  14.  * 
  15.  * Unless required by applicable law or agreed to in writing,
  16.  * software distributed under the License is distributed on an
  17.  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  18.  * KIND, either express or implied.  See the License for the
  19.  * specific language governing permissions and limitations
  20.  * under the License.
  21.  *
  22.  * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
  23.  * @version //autogentag//
  24.  * @filesource
  25.  * @package MvcTools
  26.  */
  27.  
  28. /**
  29.  * Request parser that uses HTTP headers to populate an ezcMvcRequest object.
  30.  *
  31.  * @package MvcTools
  32.  * @version //autogentag//
  33.  * @mainclass
  34.  */
  35. {
  36.     /**
  37.      * Uses the data from the superglobals.
  38.      *
  39.      * @return ezcMvcRequest 
  40.      */
  41.     public function createRequest()
  42.     {
  43.         $this->request = $this->createRequestObject();
  44.         $this->processStandardHeaders();
  45.         $this->processAcceptHeaders();
  46.         $this->processUserAgentHeaders();
  47.         $this->processFiles();
  48.         $this->processAuthVars();
  49.         $this->processCookies();
  50.  
  51.         $this->request->raw &$_SERVER;
  52.  
  53.         return $this->request;
  54.     }
  55.  
  56.     /**
  57.      * Creates and returns an ezcMvcRequest object.
  58.      *
  59.      * @return ezcMvcRequest 
  60.      */
  61.     protected function createRequestObject()
  62.     {
  63.         return new ezcMvcRequest();
  64.     }
  65.  
  66.     /**
  67.      * Processes the basic HTTP auth variables is set
  68.      */
  69.     protected function processAuthVars()
  70.     {
  71.         $req $this->request;
  72.         if isset$_SERVER['PHP_AUTH_USER'&& isset$_SERVER['PHP_AUTH_PW') )
  73.         {
  74.             $req->authentication new ezcMvcRequestAuthentication$_SERVER['PHP_AUTH_USER']$_SERVER['PHP_AUTH_PW');
  75.         }
  76.     }
  77.  
  78.     /**
  79.      * Processes the standard headers that are not subdivided into other structs.
  80.      */
  81.     protected function processStandardHeaders()
  82.     {
  83.         $this->processProtocol();
  84.         $this->processHost();
  85.         $this->processDate();
  86.         $this->processVariables();
  87.         $this->processReferrer();
  88.         $this->processUri();
  89.         $this->processBody();
  90.         $this->processRequestId();
  91.     }
  92.  
  93.     /**
  94.      * Processes the request protocol.
  95.      */
  96.     protected function processProtocol()
  97.     {
  98.         $req $this->request;
  99.  
  100.         if isset$_SERVER['REQUEST_METHOD') )
  101.         {
  102.             switch $_SERVER['REQUEST_METHOD')
  103.             {
  104.                 case 'POST':
  105.                     $req->protocol 'http-post';
  106.                     break;
  107.                 case 'PUT':
  108.                     $req->protocol 'http-put';
  109.                     break;
  110.                 case 'DELETE':
  111.                     $req->protocol 'http-delete';
  112.                     break;
  113.                 default:
  114.                     $req->protocol 'http-get';
  115.             }
  116.         }
  117.     }
  118.  
  119.     /**
  120.      * Processes the request host.
  121.      */
  122.     protected function processHost()
  123.     {
  124.         $this->request->host = isset$_SERVER['HTTP_HOST')
  125.             ? $_SERVER['HTTP_HOST']
  126.             : (
  127.                 isset$_SERVER['SERVER_NAME')
  128.                     ? $_SERVER['SERVER_NAME']
  129.                     : 'localhost.localdomain'
  130.             );
  131.     }
  132.  
  133.     /**
  134.      * Processes the request date.
  135.      */
  136.     protected function processDate()
  137.     {
  138.         $this->request->date = isset$_SERVER['REQUEST_TIME')
  139.             ? new DateTime"@{$_SERVER['REQUEST_TIME']})
  140.             : new DateTime();
  141.     }
  142.  
  143.     /**
  144.      * Processes the request variables.
  145.      */
  146.     protected function processVariables()
  147.     {
  148.         $this->request->variables =$_REQUEST;
  149.     }
  150.  
  151.     /**
  152.      * Processes the referrer.
  153.      */
  154.     protected function processReferrer()
  155.     {
  156.         $this->request->referrer = isset$_SERVER['HTTP_REFERER')
  157.             ? $_SERVER['HTTP_REFERER']
  158.             : null;
  159.     }
  160.  
  161.     /**
  162.      * Processes the request URI.
  163.      */
  164.     protected function processUri()
  165.     {
  166.         $req $this->request;
  167.  
  168.         $req->uri = isset$_SERVER['REQUEST_URI')
  169.             ? $_SERVER['REQUEST_URI']
  170.             : '';
  171.         // remove the query string from the URI
  172.         $req->uri preg_replace'@\?.*$@'''$req->uri );
  173.         // url decode the uri
  174.         $req->uri urldecode$req->uri );
  175.         // remove the prefix from the URI
  176.         $req->uri preg_replace'@^' preg_quote$this->properties['prefix''@'''$req->uri );
  177.     }
  178.  
  179.     /**
  180.      * Processes the request ID from host and URI.
  181.      */
  182.     protected function processRequestId()
  183.     {
  184.         $this->request->requestId $this->request->host $this->request->uri;
  185.     }
  186.  
  187.     /**
  188.      * Processes the request body for PUT requests.
  189.      */
  190.     protected function processBody()
  191.     {
  192.         $req $this->request;
  193.  
  194.         if $req->protocol == 'http-put' )
  195.         {
  196.             $req->body file_get_contents"php://input" );
  197.         }
  198.     }
  199.  
  200.     /**
  201.      * Proccesses the HTTP Accept headers into the ezcMvcRequestAccept struct.
  202.      */
  203.     protected function processAcceptHeaders()
  204.     {
  205.         $this->request->accept new ezcMvcRequestAccept;
  206.         $accept $this->request->accept;
  207.  
  208.         $map array(
  209.             'HTTP_ACCEPT' => 'types',
  210.             'HTTP_ACCEPT_CHARSET' => 'charsets',
  211.             'HTTP_ACCEPT_ENCODING' => 'encodings',
  212.             'HTTP_ACCEPT_LANGUAGE' => 'languages',
  213.         );
  214.  
  215.         foreach $map as $var => $property )
  216.         {
  217.             if !isset$_SERVER[$var) )
  218.             {
  219.                 $accept->$property array();
  220.                 continue;
  221.             }
  222.             $parts explode','$_SERVER[$var);
  223.             $tmpPriorities array();
  224.             foreach $parts as $part )
  225.             {
  226.                 $priPart explode';q='$part );
  227.                 if count$priPart == )
  228.                 {
  229.                     $tmpPriorities[$priPart[0]] $priPart[1];
  230.                 }
  231.                 else
  232.                 {
  233.                     $tmpPriorities[$part1;
  234.                 }
  235.             }
  236.             asort$tmpPriorities );
  237.             $accept->$property array_keysarray_reverse$tmpPriorities ) );
  238.         }
  239.     }
  240.  
  241.     /**
  242.      * Proccesses the User Agent header into the ezcMvcRequestUserAgent struct.
  243.      */
  244.     protected function processUserAgentHeaders()
  245.     {
  246.         $this->request->agent new ezcMvcRequestUserAgent;
  247.         $agent $this->request->agent;
  248.  
  249.         $agent->agent = isset$_SERVER['HTTP_USER_AGENT')
  250.             ? $_SERVER['HTTP_USER_AGENT']
  251.             : null;
  252.     }
  253.  
  254.     /**
  255.      * Processes uploaded files.
  256.      */
  257.     protected function processFiles()
  258.     {
  259.         foreach $_FILES as $name => $info )
  260.         {
  261.             $file new ezcMvcRequestFile;
  262.             $file->mimeType $info['type'];
  263.             $file->name $info['name'];
  264.             $file->size $info['size'];
  265.             $file->status $info['error'];
  266.             $file->tmpPath $info['tmp_name'];
  267.  
  268.             $this->request->files[$file;
  269.         }
  270.     }
  271.  
  272.     /**
  273.      * Process cookies
  274.      */
  275.     protected function processCookies()
  276.     {
  277.         foreach $_COOKIE as $name => $value )
  278.         {
  279.             $cookie new ezcMvcRequestCookie$name$value );
  280.             $this->request->cookies[$cookie;
  281.         }
  282.     }
  283. }
  284. ?>
Documentation generated by phpDocumentor 1.4.3