File: /www/exchange0old/exchange/Bin/Request.php
<?php
/**
* dev-rc - Request.php
*
* Initial version by: MisterX
* Initial version created on: 22.02.2016
*/
class Bin_Request
{
public static $baseUrl = '/exchange/';
/**
* @var null
*/
protected static $instance = null;
protected $protocol='http';
protected $uri='';
protected $isAjax=false;
protected $method='get';
protected $domain;
protected $referer;
/**
* @var array query parameters
*/
protected $_get = array();
/**
* @var array post parameters
*/
protected $_post = array();
/**
* @return boolean
*/
public function isAjax()
{
return $this->isAjax;
}
/**
* @param boolean $isAjax
*/
protected function setIsAjax($isAjax)
{
$this->isAjax = $isAjax;
}
/**
* @return string
*/
public function getProtocol()
{
return $this->protocol;
}
/**
* @param string $protocol
*/
protected function setProtocol($protocol)
{
$this->protocol = $protocol;
return $this;
}
/**
* @return string
*/
public function getUri()
{
return $this->uri;
}
/**
* @param string $uri
*/
protected function setUri($uri)
{
$this->uri = $uri;
return $this;
}
/**
* @return string
*/
public function getMethod()
{
return $this->method;
}
/**
* @param string $method
*/
protected function setMethod($method)
{
$this->method = $method;
return $this;
}
/**
* @return $this
*/
public static function getInstance()
{
return (!self::$instance)?self::createFromGlobals():self::$instance;
}
/**
* @return mixed
*/
public function getDomain()
{
return $this->domain;
}
/**
* @param mixed $domain
*/
protected function setDomain($domain)
{
$this->domain = $domain;
}
/**
* Gets the base URL to the application.
* @return string
*/
public function baseUrl(){
return $this->protocol.'://'.$this->domain.self::$baseUrl;
}
public function siteUrl($uri=''){
$path = preg_replace('~^[-a-z0-9+.]++://[^/]++/?~', '', trim($uri, '/'));
return $this->baseUrl().$path;
}
public function oguiUrl($do=null){
return $this->oguiBaseUrl().'index.php?do='.$do;
}
public function oguiBaseUrl(){
return $this->siteUrl('/');
}
public function boguiBaseUrl(){
return $this->siteUrl('/admin/');
}
public function boguiUrl($do=null){
if($do){
return $this->boguiBaseUrl().'/adminindex.php?do='.$do;
}else{
return $this->boguiBaseUrl();
}
}
/**
* @return mixed
*/
public function getReferer()
{
return $this->referer;
}
protected function __construct(){
}
public static function createFromGlobals(){
if (isset($_SERVER['REQUEST_METHOD']))
{
// Use the server request method
$method = $_SERVER['REQUEST_METHOD'];
}
else
{
// Default to GET requests
$method = 'get';
}
if ( ! empty($_SERVER['HTTPS']) AND filter_var($_SERVER['HTTPS'], FILTER_VALIDATE_BOOLEAN))
{
// This request is secure
$protocol = 'https';
}else{
$protocol = 'http';
}
if (isset($_SERVER['HTTP_X_REQUESTED_WITH']))
{
// Typically used to denote AJAX requests
$requested_with = strtolower($_SERVER['HTTP_X_REQUESTED_WITH']);
}
$request = self::$instance = new self();
$request->referer = $_SERVER['HTTP_REFERER'];
$request->setUri(self::detectUri());
if (isset($requested_with))
{
// Apply the requested with variable
$request->setIsAjax($requested_with=='xmlhttprequest');
}
$request->setProtocol($protocol);
$request->setMethod($method);
$request->setDomain(isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME']);
$request->query($_GET);
$request->post($_POST);
return $request;
}
public static function detectUri()
{
if ( ! empty($_SERVER['PATH_INFO']))
{
// PATH_INFO does not contain the docroot or index
$uri = $_SERVER['PATH_INFO'];
}
else
{
// REQUEST_URI and PHP_SELF include the docroot and index
if (isset($_SERVER['REQUEST_URI']))
{
$uri = $_SERVER['REQUEST_URI'];
if ($request_uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH))
{
// Valid URL path found, set it.
$uri = $request_uri;
}
// Decode the request URI
$uri = rawurldecode($uri);
}
elseif (isset($_SERVER['PHP_SELF']))
{
$uri = $_SERVER['PHP_SELF'];
}
elseif (isset($_SERVER['REDIRECT_URL']))
{
$uri = $_SERVER['REDIRECT_URL'];
}
else
{
// If you ever see this error, please report an issue at http://dev.kohanaphp.com/projects/kohana3/issues
// along with any relevant information about your web server setup. Thanks!
throw new Exception('Unable to detect the URI using PATH_INFO, REQUEST_URI, PHP_SELF or REDIRECT_URL');
}
// Get the path from the base URL, including the index file
$base_url = parse_url(self::$baseUrl, PHP_URL_PATH);
if (strpos($uri, $base_url) === 0)
{
// Remove the base URL from the URI
$uri = (string) substr($uri, strlen($base_url));
}
}
return $uri;
}
/**
* Gets or sets HTTP query string.
*
* @param mixed $key Key or key value pairs to set
* @param string $value Value to set to a key
* @return mixed
*/
public function query($key = NULL, $value = NULL)
{
if (is_array($key))
{
// Act as a setter, replace all query strings
$this->_get = $key;
return $this;
}
if ($key === NULL)
{
// Act as a getter, all query strings
return $this->_get;
}
elseif ($value === NULL)
{
// Act as a getter, single query string
return Bin_Array::get($this->_get, $key);
}
// Act as a setter, single query string
$this->_get[$key] = $value;
return $this;
}
/**
* Gets or sets HTTP POST parameters to the request.
*
* @param mixed $key Key or key value pairs to set
* @param string $value Value to set to a key
* @return mixed
* @uses Arr::path
*/
public function post($key = NULL, $value = NULL)
{
if (is_array($key))
{
// Act as a setter, replace all fields
$this->_post = $key;
return $this;
}
if ($key === NULL)
{
// Act as a getter, all fields
return $this->_post;
}
elseif ($value === NULL)
{
// Act as a getter, single field
return Bin_Array::get($this->_post, $key);
}
// Act as a setter, single field
$this->_post[$key] = $value;
return $this;
}
public function getReturnUrl(){
($return = $this->query('_return')) || ($return = $this->post('_return')) || ($return = $this->referer) || ($return = $this->baseUrl());
return $return;
}
public function responseReturn(){
$this->responseRedirect($this->getReturnUrl());
}
public function responseRedirect($url){
header('Location: '.$url);
exit;
}
}