rpicms

A CMS for the Raspberry Pi
git clone git://archive.git.mtrnord.blog/RpicmsTeam/rpicms.git
Log | Files | Refs | README | LICENSE

Environment.php (7333B)


      1 <?php
      2 /**
      3  * Slim - a micro PHP 5 framework
      4  *
      5  * @author      Josh Lockhart <info@slimframework.com>
      6  * @copyright   2011 Josh Lockhart
      7  * @link        http://www.slimframework.com
      8  * @license     http://www.slimframework.com/license
      9  * @version     2.4.2
     10  * @package     Slim
     11  *
     12  * MIT LICENSE
     13  *
     14  * Permission is hereby granted, free of charge, to any person obtaining
     15  * a copy of this software and associated documentation files (the
     16  * "Software"), to deal in the Software without restriction, including
     17  * without limitation the rights to use, copy, modify, merge, publish,
     18  * distribute, sublicense, and/or sell copies of the Software, and to
     19  * permit persons to whom the Software is furnished to do so, subject to
     20  * the following conditions:
     21  *
     22  * The above copyright notice and this permission notice shall be
     23  * included in all copies or substantial portions of the Software.
     24  *
     25  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
     26  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
     27  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
     28  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
     29  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
     30  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
     31  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
     32  */
     33 namespace Slim;
     34 
     35 /**
     36  * Environment
     37  *
     38  * This class creates and returns a key/value array of common
     39  * environment variables for the current HTTP request.
     40  *
     41  * This is a singleton class; derived environment variables will
     42  * be common across multiple Slim applications.
     43  *
     44  * This class matches the Rack (Ruby) specification as closely
     45  * as possible. More information available below.
     46  *
     47  * @package Slim
     48  * @author  Josh Lockhart
     49  * @since   1.6.0
     50  */
     51 class Environment implements \ArrayAccess, \IteratorAggregate
     52 {
     53     /**
     54      * @var array
     55      */
     56     protected $properties;
     57 
     58     /**
     59      * @var \Slim\Environment
     60      */
     61     protected static $environment;
     62 
     63     /**
     64      * Get environment instance (singleton)
     65      *
     66      * This creates and/or returns an environment instance (singleton)
     67      * derived from $_SERVER variables. You may override the global server
     68      * variables by using `\Slim\Environment::mock()` instead.
     69      *
     70      * @param  bool             $refresh Refresh properties using global server variables?
     71      * @return \Slim\Environment
     72      */
     73     public static function getInstance($refresh = false)
     74     {
     75         if (is_null(self::$environment) || $refresh) {
     76             self::$environment = new self();
     77         }
     78 
     79         return self::$environment;
     80     }
     81 
     82     /**
     83      * Get mock environment instance
     84      *
     85      * @param  array       $userSettings
     86      * @return \Slim\Environment
     87      */
     88     public static function mock($userSettings = array())
     89     {
     90         $defaults = array(
     91             'REQUEST_METHOD' => 'GET',
     92             'SCRIPT_NAME' => '',
     93             'PATH_INFO' => '',
     94             'QUERY_STRING' => '',
     95             'SERVER_NAME' => 'localhost',
     96             'SERVER_PORT' => 80,
     97             'ACCEPT' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
     98             'ACCEPT_LANGUAGE' => 'en-US,en;q=0.8',
     99             'ACCEPT_CHARSET' => 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
    100             'USER_AGENT' => 'Slim Framework',
    101             'REMOTE_ADDR' => '127.0.0.1',
    102             'slim.url_scheme' => 'http',
    103             'slim.input' => '',
    104             'slim.errors' => @fopen('php://stderr', 'w')
    105         );
    106         self::$environment = new self(array_merge($defaults, $userSettings));
    107 
    108         return self::$environment;
    109     }
    110 
    111     /**
    112      * Constructor (private access)
    113      *
    114      * @param  array|null $settings If present, these are used instead of global server variables
    115      */
    116     private function __construct($settings = null)
    117     {
    118         if ($settings) {
    119             $this->properties = $settings;
    120         } else {
    121             $env = array();
    122 
    123             //The HTTP request method
    124             $env['REQUEST_METHOD'] = $_SERVER['REQUEST_METHOD'];
    125 
    126             //The IP
    127             $env['REMOTE_ADDR'] = $_SERVER['REMOTE_ADDR'];
    128 
    129             // Server params
    130             $scriptName = $_SERVER['SCRIPT_NAME']; // <-- "/foo/index.php"
    131             $requestUri = $_SERVER['REQUEST_URI']; // <-- "/foo/bar?test=abc" or "/foo/index.php/bar?test=abc"
    132             $queryString = isset($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : ''; // <-- "test=abc" or ""
    133 
    134             // Physical path
    135             if (strpos($requestUri, $scriptName) !== false) {
    136                 $physicalPath = $scriptName; // <-- Without rewriting
    137             } else {
    138                 $physicalPath = str_replace('\\', '', dirname($scriptName)); // <-- With rewriting
    139             }
    140             $env['SCRIPT_NAME'] = rtrim($physicalPath, '/'); // <-- Remove trailing slashes
    141 
    142             // Virtual path
    143             $env['PATH_INFO'] = substr_replace($requestUri, '', 0, strlen($physicalPath)); // <-- Remove physical path
    144             $env['PATH_INFO'] = str_replace('?' . $queryString, '', $env['PATH_INFO']); // <-- Remove query string
    145             $env['PATH_INFO'] = '/' . ltrim($env['PATH_INFO'], '/'); // <-- Ensure leading slash
    146 
    147             // Query string (without leading "?")
    148             $env['QUERY_STRING'] = $queryString;
    149 
    150             //Name of server host that is running the script
    151             $env['SERVER_NAME'] = $_SERVER['SERVER_NAME'];
    152 
    153             //Number of server port that is running the script
    154             $env['SERVER_PORT'] = $_SERVER['SERVER_PORT'];
    155 
    156             //HTTP request headers (retains HTTP_ prefix to match $_SERVER)
    157             $headers = \Slim\Http\Headers::extract($_SERVER);
    158             foreach ($headers as $key => $value) {
    159                 $env[$key] = $value;
    160             }
    161 
    162             //Is the application running under HTTPS or HTTP protocol?
    163             $env['slim.url_scheme'] = empty($_SERVER['HTTPS']) || $_SERVER['HTTPS'] === 'off' ? 'http' : 'https';
    164 
    165             //Input stream (readable one time only; not available for multipart/form-data requests)
    166             $rawInput = @file_get_contents('php://input');
    167             if (!$rawInput) {
    168                 $rawInput = '';
    169             }
    170             $env['slim.input'] = $rawInput;
    171 
    172             //Error stream
    173             $env['slim.errors'] = @fopen('php://stderr', 'w');
    174 
    175             $this->properties = $env;
    176         }
    177     }
    178 
    179     /**
    180      * Array Access: Offset Exists
    181      */
    182     public function offsetExists($offset)
    183     {
    184         return isset($this->properties[$offset]);
    185     }
    186 
    187     /**
    188      * Array Access: Offset Get
    189      */
    190     public function offsetGet($offset)
    191     {
    192         if (isset($this->properties[$offset])) {
    193             return $this->properties[$offset];
    194         } else {
    195             return null;
    196         }
    197     }
    198 
    199     /**
    200      * Array Access: Offset Set
    201      */
    202     public function offsetSet($offset, $value)
    203     {
    204         $this->properties[$offset] = $value;
    205     }
    206 
    207     /**
    208      * Array Access: Offset Unset
    209      */
    210     public function offsetUnset($offset)
    211     {
    212         unset($this->properties[$offset]);
    213     }
    214 
    215     /**
    216      * IteratorAggregate
    217      *
    218      * @return \ArrayIterator
    219      */
    220     public function getIterator()
    221     {
    222         return new \ArrayIterator($this->properties);
    223     }
    224 }