Created
September 14, 2011 15:58
-
-
Save larscwallin/1216951 to your computer and use it in GitHub Desktop.
simplx.request static class which wrapps the HTTP Request
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
IModGenericMessage | |
A VERY abstract representation of a system/client request/response message. | |
The class is meant to normalize all types of inter-system communication for all protocols used. | |
*/ | |
interface IModGenericMessage{ | |
public function getId(); | |
public function setId($value); | |
public function getExpires(); | |
public function setExpires($value); | |
public function getOrigin(); | |
public function setOrigin($value); | |
public function getDestination(); | |
public function setDestination($value); | |
public function getHeaders(); | |
public function getHeaderField($name); | |
public function setHeaderField($name,$value); | |
public function addHeaderField($name,$value); | |
public function deleteHeaderField($name); | |
public function getSection($name); | |
public function getSectionField($section,$name); | |
public function setSection($name,$value); | |
public function setSectionField($section,$name,$value); | |
public function addSection($name); | |
public function addSectionField($section,$name); | |
public function deleteSection($name); | |
public function deleteSectionField($section,$name); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
The SimplxRequest Class is a static Class which wraps the HTTP Request and adds some nice utility features. | |
Most importantly it is meant to abstract the Request so that it can be used as an internal message format, | |
independant of PHP's HTTP variables. As of now though the built in PHP HTTP variables are used to init the | |
Class members. | |
It does not at this stage include any methods for executing a Request, it's simply a message wrapper. | |
*/ | |
require_once($modx->getOption('core_path').'components/simplx/common/IModGenericMessage.php'); | |
class SimplxRequest implements IModGenericMessage{ | |
protected static $requestData = false; | |
public static $fieldDelimiter = '.'; | |
public static $valueDelimiter = '='; | |
public static $patternDelimiter = '?'; | |
public function getField($name){ | |
if(!self::$requestData){self::init();} | |
if(array_key_exists($name,self::$requestData)){ | |
return self::$requestData[$name]; | |
}else{ | |
return null; | |
} | |
} | |
public function setField($name,$value){ | |
if(!self::$requestData){self::init();} | |
if(array_key_exists($name,self::$requestData) && ( isset($name) && isset($value) ) ){ | |
self::$requestData[$name] = $value; | |
}else{ | |
return null; | |
} | |
} | |
/* IModGenericMessage */ | |
public function getId(){ | |
return $this->getField('id'); | |
} | |
public function setId($value){ | |
return $this->setField('id',$value); | |
} | |
public function getExpires(){ | |
return $this->getField('id'); | |
} | |
public function setExpires($value){ | |
return $this->getField('id'); | |
return $this->setField('id',$value); | |
} | |
public function getOrigin(){ | |
return $this->getField('origin'); | |
} | |
public function setOrigin($value){ | |
return $this->setField('origin',$value); | |
} | |
public function getDestination(){ | |
return $this->getField('destination'); | |
} | |
public function setDestination($value){ | |
return $this->setField('destination',$value); | |
} | |
public function getHeaders(){ | |
if(!self::$requestData){self::init();} | |
return self::$requestData['headers']; | |
} | |
public function getHeaderField($name){ | |
if(!self::$requestData){self::init();} | |
return self::getHeaderItem($name); | |
} | |
public function setHeaderField($name,$value){ | |
if(!self::$requestData){self::init();} | |
return self::$requestData['headers'][$name] = $value; | |
} | |
public function addHeaderField($name,$value){ | |
if(!self::$requestData){self::init();} | |
if(!isset($value)){$value = '';} | |
if(!array_key_exists($name,self::$requestData['headers'])){ | |
return self::$requestData['headers'][$name] = $value; | |
}else{ | |
return false; | |
} | |
} | |
public function deleteHeaderField($name){ | |
if(!self::$requestData){self::init();} | |
if(array_key_exists($name,self::$requestData['headers'])){ | |
return self::$requestData['headers'][$name] = null; | |
} | |
} | |
public function getSection($name){ | |
if(!self::$requestData){self::init();} | |
if(array_key_exists($name,self::$requestData)){ | |
return self::$requestData[$name]; | |
}else{ | |
return null; | |
} | |
} | |
public function setSection($name,$value){ | |
if(!self::$requestData){self::init();} | |
if(!isset($value)){$value = array();} | |
if(array_key_exists($name,self::$requestData)){ | |
return self::$requestData[$name] = $value; | |
}else{ | |
return false; | |
} | |
} | |
public function addSection($name){ | |
if(!self::$requestData){self::init();} | |
if(!array_key_exists($name,self::$requestData)){ | |
return self::$requestData[$name] = array(); | |
}else{ | |
return false; | |
} | |
} | |
public function deleteSection($name){ | |
if(!self::$requestData){self::init();} | |
if(array_key_exists($name,self::$requestData)){ | |
return self::$requestData[$name] = null; | |
} | |
} | |
public function addSectionField($section,$name){ | |
if(!self::$requestData){self::init();} | |
if(!array_key_exists($name,self::$requestData[$section])){ | |
return self::$requestData[$section][$name] = ''; | |
}else{ | |
return false; | |
} | |
} | |
public function getSectionField($section,$name){ | |
if(!self::$requestData){self::init();} | |
if(array_key_exists($name,self::$requestData[$section])){ | |
return self::$requestData[$section][$name]; | |
}else{ | |
return null; | |
} | |
} | |
public function setSectionField($section,$name,$value){ | |
if(!self::$requestData){self::init();} | |
if(!isset($value)){$value = '';} | |
if(array_key_exists($name,self::$requestData[$section])){ | |
return self::$requestData[$section][$name] = $value; | |
}else{ | |
return false; | |
} | |
} | |
public function deleteSectionField($section,$name){ | |
if(!self::$requestData){self::init();} | |
if(array_key_exists($name,self::$requestData[$section])){ | |
return self::$requestData[$section][$name] = null; | |
}else{ | |
return false; | |
} | |
} | |
/* IModGenericMessage */ | |
private static function get_request_headers() { | |
$headers = array(); | |
foreach($_SERVER as $key => $value) { | |
if(strpos($key, 'HTTP_') === 0) { | |
$headers[strtolower(str_replace(' ', '-', ucwords(str_replace('_', ' ', substr($key, 5)))))] = $value; | |
} | |
} | |
return $headers; | |
} | |
public static function init(){ | |
global $modx; | |
$queryStartPos = false; | |
// for the moment we only hande http and https | |
$protocol = array_key_exists('HTTPS',$_SERVER) ? 'http' : 'https'; | |
// Temporary variable to hold the uri parameter. | |
$uri = $_SERVER['REQUEST_URI']; | |
// Create the urn field from the uri. This allows us to identify resources which have no file type extension. | |
$urn = substr($uri,(strrpos($uri,'/')+1)); | |
$queryStartPos = strpos($urn,'?'); | |
// Build the url variable from the SERVER variables. | |
// This will be changed in next version to use the original url parameter which was used for the request. | |
$url = $protocol."://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; | |
if($queryStartPos !== false){ | |
if($queryStartPos==0){ | |
$urn = ''; | |
}else{ | |
$urn = substr($urn,0,$queryStartPos); | |
} | |
}else{ | |
$urn = $urn; | |
} | |
// Remove the urn from the path as this is redundant. | |
$path = substr($uri,0,(strrpos($uri,'/')+1)); | |
// Parse the url and split all portions into an array. | |
self::$requestData = parse_url($url); | |
if(self::$requestData){ | |
self::$requestData['url'] = $url; | |
self::$requestData['method'] = $_SERVER['REQUEST_METHOD']; | |
self::$requestData['body'] = file_get_contents('php://input'); | |
self::$requestData['uri'] = $uri; | |
self::$requestData['urn'] = $urn; | |
self::$requestData['headers'] = self::get_request_headers(); | |
self::$requestData['query'] = $_GET; | |
self::$requestData['form'] = $_POST; | |
self::$requestData['cookies'] = $_COOKIE; | |
self::$requestData['path'] = $path; | |
}else{ | |
return false; | |
} | |
} | |
/* | |
Below follow accsessor functions which extract and return requested values from the | |
property array. | |
*/ | |
public static function getRequest(){ | |
if(!self::$requestData){self::init();} | |
return self::$requestData; | |
} | |
public static function getFormItem($name){ | |
if(!self::$requestData){self::init();} | |
if(array_key_exists($name,self::$requestData['form'])){ | |
return self::$requestData['form'][$name]; | |
}else{ | |
return null; | |
} | |
} | |
public static function getQueryItem($name){ | |
if(!self::$requestData){self::init();} | |
if(array_key_exists($name,self::$requestData['query'])){ | |
return self::$requestData['query'][$name]; | |
}else{ | |
return null; | |
} | |
} | |
public static function getHeaderItem($name){ | |
if(!self::$requestData){self::init();} | |
if(array_key_exists($name,self::$requestData['headers'])){ | |
return self::$requestData['headers'][$name]; | |
}else{ | |
return null; | |
} | |
} | |
public static function getCookieItem($name){ | |
if(!self::$requestData){self::init();} | |
if(array_key_exists($name,self::$requestData['cookies'])){ | |
return self::$requestData['cookies'][$name]; | |
}else{ | |
return null; | |
} | |
} | |
public static function match($pattern,$string){ | |
//global $modx; | |
if(!$pattern){ | |
return null; | |
} | |
$result = ((preg_match($pattern,$string)>0) ? true : false); | |
return ($result ? $string : null); | |
} | |
/* | |
Neat function which allows us to search through request using namespaces ("header:content-type" or "cookie:mycookie"). | |
If no namespace was provided it defaults to the first dimension of the requestData array, which excludes cookies and headers. | |
*/ | |
public static function lookup($name) { | |
//global $modx; | |
if(!self::$requestData){self::init();} | |
$result; | |
$query; | |
$ns; | |
$name; | |
$value; | |
$pattern = false; | |
/* | |
Start parsing the query | |
*/ | |
$name = strtolower($name); | |
$query = explode(self::$fieldDelimiter,$name); | |
/* | |
If the query is an array, this means that we have a namespace plus a field to search for | |
*/ | |
if(is_array($query) && count($query)>1){ | |
// The Request items "namespace". | |
$ns = $query[0]; | |
// The Request items name/key. | |
$name = $query[1]; | |
// First see if we should match using regular expressions. | |
$result = explode(self::$patternDelimiter,$name); | |
if(isset($result[1])){ | |
// Ok we have a pattern to match. | |
$name = $result[0]; | |
$pattern = $result[1]; | |
// Set the $value variable to null to signal that we should use "match" instead of "equals" comparison. | |
$value = null; | |
}else{ | |
// See if this call is a value comparison check. This is indicated by the presence of a "value part". | |
$result = explode(self::$valueDelimiter,$name); | |
$name = $result[0]; | |
$value = (isset($result[1]) ? $result[1] : null); | |
} | |
if(!$name){ | |
/* | |
No use going if we have no $name parameter. Notice that we return null instead of false. | |
This is not confuse the calling code, as false is of course a valid Request item value. | |
*/ | |
return null; | |
} | |
switch($ns){ | |
case 'header': | |
$result = self::getHeaderItem($name); | |
if($pattern){ | |
return (self::match($pattern,$result) ? $result : null); | |
} | |
if($value){ | |
return (($result == $value) ? $value : null); | |
} | |
if(isset($result)){ | |
return $result; | |
}else{ | |
return null; | |
} | |
break; | |
case 'path': | |
if(array_key_exists('path',self::$requestData)){ | |
if($pattern){ | |
$value = self::$requestData['path']; | |
return (self::match($pattern,$value) ? $value : null); | |
} | |
if($value){ | |
return (self::$requestData['path'] == $value) ? $value : null; | |
} | |
return self::$requestData['path']; | |
}else{ | |
return null; | |
} | |
break; | |
case 'uri': | |
if(array_key_exists('uri',self::$requestData)){ | |
if($pattern){ | |
$value = self::$requestData['uri']; | |
return (self::match($pattern,$value) ? $value : null); | |
} | |
if($value){ | |
return (self::$requestData['uri'] == $value) ? $value : null; | |
} | |
return self::$requestData['uri']; | |
}else{ | |
return null; | |
} | |
break; | |
case 'urn': | |
if(array_key_exists('urn',self::$requestData)){ | |
if($pattern){ | |
$value = self::$requestData['urn']; | |
return (self::match($pattern,$value) ? $value : null); | |
} | |
if($value){ | |
return (self::$requestData['urn'] == $value) ? $value : null; | |
} | |
return self::$requestData['urn']; | |
}else{ | |
return null; | |
} | |
break; | |
case 'get': | |
$result = self::getQueryItem($name); | |
if($pattern){ | |
$value = $result; | |
return (self::match($pattern,$value) ? $value : null); | |
} | |
if($value){ | |
return (($result == $value) ? $value : null); | |
} | |
if(isset($result)){ | |
return $result; | |
}else{ | |
return null; | |
} | |
break; | |
case 'query': | |
$result = self::getQueryItem($name); | |
if($pattern){ | |
$value = $result; | |
return (self::match($pattern,$value) ? $value : null); | |
} | |
if($value){ | |
return (($result == $value) ? $value : null); | |
} | |
return $result; | |
break; | |
case 'post': | |
$result = self::getFormItem($name); | |
if($pattern){ | |
$value = $result; | |
return (self::match($pattern,$value) ? $value : null); | |
} | |
if($value){ | |
return (($result == $value) ? $value : null); | |
} | |
if(isset($result)){ | |
return $result; | |
}else{ | |
return null; | |
} | |
break; | |
case 'form': | |
$result = self::getFormItem($name); | |
if($pattern){ | |
$value = $result; | |
return (self::match($pattern,$value) ? $value : null); | |
} | |
if($value){ | |
return (($result == $value) ? $value : null); | |
} | |
if(isset($result)){ | |
return $result; | |
}else{ | |
return null; | |
} | |
break; | |
case 'cookie': | |
$result = self::getCookieItem($name); | |
if($pattern){ | |
$value = $result; | |
return (self::match($pattern,$value) ? $value : null); | |
} | |
if($value){ | |
return (($result == $value) ? $value : null); | |
} | |
if(isset($result)){ | |
return $result; | |
}else{ | |
return null; | |
} | |
break; | |
default: | |
/* | |
If the namespace key was not defined in the in any of the cases above whe see if it has been | |
defined ad-hoc during runtime. | |
*/ | |
if(array_key_exists($ns,self::$requestData)){ | |
if($pattern){ | |
$value = self::$requestData[$ns][$name]; | |
return (self::match($pattern,$value) ? $value : null); | |
} | |
if($value){ | |
return (self::$requestData[$ns][$name] == $value) ? $value : null; | |
} | |
return self::$requestData[$ns][$name]; | |
}else{ | |
return null; | |
} | |
break; | |
} | |
}else{ | |
// First see if we should match using regular expressions. | |
$result = explode(self::$patternDelimiter,$name); | |
if(isset($result[1])){ | |
// Ok we have a pattern to match. | |
$name = $result[0]; | |
$pattern = $result[1]; | |
// Set the $value variable to null to signal that we should use "match" instead of "equals" comparison. | |
$value = null; | |
}else{ | |
// See if this call is a value comparison check. This is indicated by the presence of a "value part". | |
$result = explode(self::$valueDelimiter,$name); | |
$name = $result[0]; | |
$value = (isset($result[1]) ? $result[1] : null); | |
} | |
// See if this call is a value comparison check. This is indicated by the presence of a "value part". | |
/* | |
$result = explode(self::$valueDelimiter,$name); | |
if(is_array($result)){ | |
$value = (isset($result[1]) ? $result[1] : null); | |
$name= $result[0]; | |
} | |
*/ | |
/* | |
If the name parameter was not formatted as a key/val (ns:key) pair we try to find it in the root | |
or the properties array. | |
*/ | |
if(array_key_exists($name,self::$requestData)){ | |
$result = strtolower(self::$requestData[$name]); | |
if($pattern){ | |
$result = self::match($pattern,$result); | |
if($result){ | |
return $result; | |
}else{ | |
return null; | |
} | |
} | |
if($value){ | |
if($result == $value){ | |
return $value; | |
}else{ | |
return null; | |
} | |
}else{ | |
return self::$requestData[$name]; | |
} | |
}else{ | |
return null; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment