/Main_Page

::You must have ninja focus to complete your mission::NinjaFocus::

PHP HTTP Request

Views:


<?php
/**
 * @package ninja
 * @copyright Kieran Whitbread 2006-2010
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
 */

/*
 * NinjaRequest
 *
 * This class abstracts the incoming HTTP request in order to make the 
 * framework more portable, extensible and testable.
 *
 * This class should be used by all components of the framework or 
 * application that need to query data about the current request
 *
 */

class NinjaRequest
{
    protected $arguments = null;
    protected $argumentString = null;
    protected $ifModifiedSince = null;
    protected $requestTime = null;
    protected $url = null;
    protected $post = false;
    
    public function __construct(NinjaConfig $config)
    {
        $this->requestTime = time();
        if (get_magic_quotes_gpc())
        {
            $_GET = $this->stripSlashesArray($_GET);
            $_POST = $this->stripSlashesArray($_POST);
            $_COOKIE = $this->stripSlashesArray($_COOKIE);
        }
    }

    public function getAccept()
    {
        if (isset($_SERVER['HTTP_ACCEPT']) && $_SERVER['HTTP_ACCEPT'])
        {
            return $_SERVER['HTTP_ACCEPT'];
        }
        else
        {
            return null;
        }
    }
    public function getAcceptCharset()
    {
        if (isset($_SERVER['HTTP_ACCEPT_CHARSET']) && $_SERVER['HTTP_ACCEPT_CHARSET'])
        {
            return $_SERVER['HTTP_ACCEPT_CHARSET'];
        }
        else
        {
            return null;
        }
    }
    public function getAcceptEncoding()
    {
        if (isset($_SERVER['HTTP_ACCEPT_ENCODING']) && $_SERVER['HTTP_ACCEPT_ENCODING'])
        {
            return $_SERVER['HTTP_ACCEPT_ENCODING'];
        }
        else
        {
            return null;
        }
    }
    public function getAcceptLanguage()
    {
        if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) && $_SERVER['HTTP_ACCEPT_LANGUAGE'])
        {
            return $_SERVER['HTTP_ACCEPT_LANGUAGE'];
        }
        else
        {
            return null;
        }
    }
    public function getArgumentString()
    {
        // It's just no use repeating yourself
        if (isset($this->argumentString) && strlen($this->argumentString)) return $this->argumentString;
        
        // Get the uri used for this request
        $uri = $this->getUri();
        
        // ignore the base uri of the application and get rid of 
        // the initial slash
        $paths = $this->config->get('paths');
        $script = basename($_SERVER['SCRIPT_NAME']);
        if (strstr($uri, $script) !== false)
        {
            $uri = str_replace('/'.$script, '', $uri);
        }
        $baseUriLen = strlen($paths['base_uri']);
        $args = substr($uri, $baseUriLen + 1);
        
        // Ignore any query string
        if (strstr($args, '?') !== false)
        {
            $args = substr($args, 0, strpos($args, '?'));
        }
        
        // If there are no arguments, look for a default
        if (!strlen($args))
        {
            $appConfig = $this->config->get('application');
            if (isset($appConfig['default_args']) && strlen($appConfig['default_args']) > 0)
            {
                $args = $appConfig['default_args'];
            }
        }
        
        // Store $args to save having to work it out more than once
        // next time we're called, we can just return the stored arguments
        $this->argumentString = $args;
        
        return $args;
    }
    
    /*
     * Return an array of arguments passed in the request uri
     * e.g. when the address used to access the application is
     * http://www.example.com/index.php/arg_one/arg_two/arg_three 
     * we return the following (ignoring any query string):
     * Array
     *  (
     *      [0] => arg_one
     *      [1] => arg_two
     *      [2] => arg_three
     *  )
     */ 
    public function getArguments()
    {
        // It's just no use repeating yourself
        if (is_array($this->arguments)) return $this->arguments;
        
        $argString = $this->getArgumentString();
        
        // Now convert the argument string in to an indexed array
        $args = explode('/', $argString);
        
        // Store $args to save having to work it out more than once
        // next time we're called, we can just return the stored arguments
        $this->arguments = $args;
        
        return $args;
    }
    public function getUrl()
    {
        if (!is_null($this->url)) return $this->url;
        $paths = $this->config->get('paths');
        $serverUrl = $paths['server_url'];
        $relUri = $this->getUri();
        $url = "{$serverUrl}{$relUri}";
        $this->url = $url;
        return $url;
    }
    public function getGet()
    {
        return $_GET;
    }
    public function getHost()
    {
        if (isset($_SERVER['HTTP_HOST']) && $_SERVER['HTTP_HOST'])
        {
            return $_SERVER['HTTP_HOST'];
        }
        else
        {
            return null;
        }
    }
    /**
     * Return the Time stamp of any If-Modified-Since request
     *
     * @return int Time stamp of IMS request or null
     * @author Kieran Whitbread
     **/
    public function getIMS()
    {
        if ($this->ifModifiedSince) return $this->ifModifiedSince;
        if ($this->isIMSRequest())
        {
            $ims = $_SERVER['HTTP_IF_MODIFIED_SINCE'];
            if(strstr($ims, ';'))
            {
                //Bah! Internet Explorer. Who'd have thought it would break standards
                $ims = substr($ims, 0, strpos($ims, ';'));
            }
            $this->ifModfiedSince = strtotime($ims);
            return $this->ifModfiedSince;
        }
        return null;
    }
    public function getMethod()
    {
        if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'])
        {
            return $_SERVER['REQUEST_METHOD'];
        }
        else
        {
            return null;
        }
    }   
    public function getPassword()
    {
        if (isset($_SERVER['PHP_AUTH_PW']) && $_SERVER['PHP_AUTH_PW'])
        {
            return $_SERVER['PHP_AUTH_PW'];
        }
        else
        {
            return null;
        }
    }   
    public function getPost()
    {
        if ($this->post !== false)
        {
            return $this->post;
        }
        else return $_POST;
    }
    public function getQueryString()
    {
        if (isset($_SERVER['QUERY_STRING']) && $_SERVER['QUERY_STRING'])
        {
            return $_SERVER['QUERY_STRING'];
        }
        else
        {
            return null;
        }
    }
    public function getReferer()
    {
        if (isset($_SERVER['HTTP_REFERER']) && $_SERVER['HTTP_REFERER'])
        {
            return $_SERVER['HTTP_REFERER'];
        }
        else
        {
            return null;
        }
    }
    public function getRemoteAddr()
    {
        if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'])
        {
            return $_SERVER['REMOTE_ADDR'];
        }
        else
        {
            return null;
        }
    }
    public function getRemoteHost()
    {
        if (isset($_SERVER['REMOTE_HOST']) && $_SERVER['REMOTE_HOST'])
        {
            return $_SERVER['REMOTE_HOST'];
        }
        elseif ($this->getRemoteAddr())
        {
            return gethostbyaddr($this->getRemoteAddr());
        }
        else
        {
            return null;
        }
    }
    public function getRemoteUser()
    {
        if (isset($_SERVER['REMOTE_USER']) && $_SERVER['REMOTE_USER'])
        {
            return $_SERVER['REMOTE_USER'];
        }
        elseif (isset($_SERVER['PHP_AUTH_USER']) && $_SERVER['PHP_AUTH_USER'])
        {
            return $_SERVER['PHP_AUTH_USER'];
        }
        else
        {
            return null;
        }
    }
    public function getUri()
    {
        if (isset($_SERVER['REQUEST_URI']) && $_SERVER['REQUEST_URI'])
        {
            return $_SERVER['REQUEST_URI'];
        }
        else
        {
            return null;
        }
    }
    public function getUserAgent()
    {
        if (isset($_SERVER['HTTP_USER_AGENT']) && $_SERVER['HTTP_USER_AGENT'])
        {
            return $_SERVER['USER_AGENT'];
        }
        else
        {
            return null;
        }
    }
    public function getTime()
    {
        if (isset($_SERVER['REQUEST_TIME']) && $_SERVER['REQUEST_TIME'])
        {
            return $_SERVER['REQUEST_TIME'];
        }
        else
        {
            return $this->requestTime;
        }
    }
    /**
     * Test if the client sent an If-Modified-Since header
     *
     * @return bool
     * @author Kieran Whitbread
     **/
    public function isIMSRequest()
    {
        return isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && strlen($_SERVER['HTTP_IF_MODIFIED_SINCE']);
    }
    /**
     * setArgumentString
     *
     * Override the argument string, trick controllers in to thinking the current request is some
     * thing else. Use for mad frantic hacking purposes.
     *
     * @throws InvalidArgumentException
     * @param string $string The argument string, arg1/arg2/arg3 etc
     * @return void
     * @author Kieran Whitbread
     **/
    public function setArgumentString($string)
    {
        if (!is_string($string)) throw New InvalidArgumentException();
        $this->argumentString = $string;
        // Arguments can be returned as an array, the array is cached the first time it's 
        // created. Best make sure there to over write any cached value.
        $this->arguments = null;
    }
    public function setPost($post)
    {
        $this->post = $post;
    }
    protected function stripSlashesArray($arr)
    { 
        foreach ($arr as $key => $value)
        { 
            if (is_array($value))
            {
                $arr[$key] = $this->stripSlashesArray($value);
            }
            else
            {
                $arr[$key] = stripslashes($value);
            }
        } 
        return $arr;
    }
}


Main Menu

Personal tools

Toolbox