anvil_php/app/core/Request.php

181 lines
4.8 KiB
PHP
Raw Normal View History

2014-08-19 14:10:22 +00:00
<?php
/* The URI Library is necessary for the framework
* to succesfully route
*/
class Request {
public $type;
public $parameters;
public $cookie;
public $files;
public $server;
public $uri_array;
2015-10-22 16:02:50 +00:00
public $original_uri_array;
2014-08-19 14:10:22 +00:00
private $_anvil;
private static $request_params = null;
public function __construct($a) {
$this->_anvil = $a;
$this->createFromGlobals();
2015-10-22 16:02:50 +00:00
$this->clearFlashdata();
2014-08-19 14:10:22 +00:00
}
public function createFromGlobals() {
$this->type = strtoupper($_SERVER['REQUEST_METHOD']);
if($this->type == 'GET') {
$this->parameters = $_GET;
} else {
$this->parameters = $this->process_data();
}
$this->cookie = $_COOKIE;
$this->files = $_FILES;
$this->server = $_SERVER;
$this->uri_array = $this->uriToArray();
2015-10-22 16:02:50 +00:00
$this->original_uri_array = $this->uri_array;
2014-08-19 14:10:22 +00:00
}
/** Is this an HTTPS request? */
public function isSecure() {
return !(!isset($this->server['HTTPS'])
|| empty($this->server['HTTPS'])
|| strtolower($this->server['HTTPS']) === 'off');
}
/** Pull the JSON Payload **/
public function json() {
if(!self::$request_params) {
$payload = file_get_contents('php://input');
if(is_array($payload)) {
self::$request_params = $payload;
} else if((substr($payload, 0, 1) == "{")
&& (substr($payload, (strlen($payload)-1), 1) == "}")) {
self::$request_params = json_decode($payload);
} else {
parse_str($payload, self::$request_params);
}
}
return (object)self::$request_params;
}
public function post($index=NULL, $xss_clean=FALSE) {
return $this->process_data($index, $xss_clean);
}
public function put($index=NULL, $xss_clean=FALSE) {
return $this->process_data($index, $xss_clean);
}
public function patch($index=NULL, $xss_clean=FALSE) {
return $this->process_data($index, $xss_clean);
}
public function delete($index=NULL, $xss_clean=FALSE) {
return $this->process_data($index, $xss_clean);
}
public function process_data($index=NULL, $xss_clean=FALSE) {
2015-10-22 16:02:50 +00:00
if(count($_FILES) > 0) {
$request_vars = $_POST;
} else {
$request_vars = (array)$this->json();
}
2014-08-19 14:10:22 +00:00
if($index==NULL && !empty($request_vars)) {
$post = array();
foreach(array_keys($request_vars) as $key) {
$post[$key] = $this->_fetch_from_array($request_vars, $key, $xss_clean);
}
return $post;
}
return $this->_fetch_from_array($request_vars, $index, $xss_clean);
}
private function _fetch_from_array(&$array, $index = '', $xss_clean = FALSE) {
if(!isset($array[$index])) {
return FALSE;
}
if($xss_clean === TRUE) {
}
return $array[$index];
}
public function setCookie($key, $val, $expire=0, $path='/', $domain=NULL, $secure=0) {
if($expire == 'never') {
// We can't do never... Set it to 10 years, should be good enough.
$expire = time()+60*60*24*365*10;
}
if(!isset($path) && ($this->_anvil->config->item('cookie_path') !== FALSE)) {
$path = $this->_anvil->config->item('cookie_path');
}
if(!isset($domain)) {
if($this->_anvil->config->item('cookie_domain') !== FALSE) {
$domain = $this->_anvil->config->item('cookie_domain');
} else {
$domain = $this->_anvil->request->server['HTTP_HOST'];
}
}
setcookie($key, $val, $expire, $path, $domain, $secure);
}
public function clearCookie($key) {
$this->setCookie($key, '', time()-3600*24*365);
}
2015-10-22 16:02:50 +00:00
public function setFlashdata($key, $val) {
return $this->setCookie('flashdata_'.$key, $val);
}
public function getFlashdata($key) {
if(!isset($this->cookie['flashdata_'.$key])) { return false; }
return $this->cookie['flashdata_'.$key];
}
public function clearFlashdata() {
foreach($this->cookie as $k => $v) {
if(strpos($k, 'flashdata_') !== FALSE) {
$this->clearCookie($k);
}
}
}
2014-08-19 14:10:22 +00:00
/**
* URI Parsing Functions
*/
public function uriToArray($st = 0, $uri=NULL) {
$uri = (isset($uri)?$uri:$this->server['REQUEST_URI']);
if(substr($uri,0,10)=='/index.php') {
$uri = substr($uri,10);
}
$uri=substr($uri,1);
$uri_array = preg_split('^[\/\?]^', $uri);
$uri_array = array_slice($uri_array, $st);
return $uri_array;
}
/**
* Parse URI into key=>value pairs
*/
public function uriToPairs($st = 0, $uri=NULL) {
$uri = (isset($uri)?$uri:$this->server['REQUEST_URI']);
$uri_array = $this->uriToArray($st, $uri);
$pair_array = array();
$key = '';
foreach($uri_array as $a_val) {
if(empty($key)) {
$key = $a_val;
} else {
$pair_array[$key] = $a_val;
$key = '';
}
}
if(!empty($key)) {
$pair_array[$key] = '';
}
return $pair_array;
}
}
?>