3 // @title Request class
4 // @author Matt Todd <matt@matttoddphoto.com>
6 // @desc A class to handle Request variables
7 // @requires stdexception.php (StdException class)
8 // @refer_to http://www.php.net/manual/en/function.parse-str.php for parsing
9 // the URL parameters following ? before the # (e.g.: x?param=val#z)
11 include_once('stdexception.php');
18 // primary public variables
19 public $route; // the route requested (array of parameters passed in)
20 public $controller; // the Controller requested
21 public $action; // the Controller's requested Action
22 public $id; // the Id to be acted upon by the Controller's Action
25 public $protocol; // e.g.: 'http' or 'https'
26 public $location; // e.g.: 'http://anything.clayton.edu/omnia/files/index' // does not add params on to end
27 public $host; // e.g.: 'http://anything.clayton.edu/'
28 public $directory; // e.g.: 'omnia/'
29 public $request; // e.g.: 'files/list'
30 public $params_string; // e.g.: '?extra=foo' -> array of values
31 public $params; // array parsing of $_params
32 public $redirect_url; // e.g.: '/mvc/admin/'
33 public $referrer; // the 'sender'
36 public $continue_to; // e.g.: 'http://anything.clayton.edu/omnia/files/show/124'
39 public $request_type; // e.g.: POST, GET, or XHR (XMLHTTPRequest)
40 public $xmlhttprequest; // a remote call
41 public $xhr; // alias of above
42 public $remote; // alias of above
44 // protected variables
45 public $request_url; // the request (ie: the URL)
46 public $get; // PHP magic $_GET variable
47 public $post; // PHP magic $_POST variable
48 public $cookie; // PHP magic $_COOKIE variable
49 public $files; // PHP magic $_FILES variable
52 public $error; // error object
55 public function __construct($request_url) {
57 $this->request_url
= $request_url;
58 $this->process_request($this->request_url
);
61 $this->post
= new RequestIn($_POST);
62 $this->get
= new RequestIn($_GET);
63 $this->cookie
= new RequestIn($_COOKIE);
64 $this->files
= new RequestIn($_FILES);
65 $this->server
= new RequestIn($_SERVER);
66 $this->error
= null; // why?
69 $this->protocol
= (empty($_SERVER['HTTPS']) ||
$_SERVER['HTTPS'] == 'off') ?
"http" : "https";
70 $this->host
= $_SERVER['HTTP_HOST'];
71 $this->directory
= substr(str_replace("dispatcher.php", "", $_SERVER['SCRIPT_NAME']), 1);
72 $this->request
= substr($_SERVER['PATH_INFO'], 1);
73 $this->params_string
= $_SERVER['QUERY_STRING']; // http://www.php.net/manual/en/function.parse-str.php
74 parse_str($this->params_string
, $params); // creates an associative array of the params
75 $this->params
= $params;
76 $this->location
= "{$this->protocol}://{$this->host}/{$this->directory}";
77 $this->redirect_url
= $_SERVER['REDIRECT_URL'];
78 $this->referrer
= $_SERVER['HTTP_REFERER'];
80 // if there was a continue_to parameter passed in after redirection, set the property
81 $session = Session
::retreive();
82 $this->continue_to
= $session->continue_to
;
83 $session->continue_to
= null;
85 // set properties of a remote call if applicable
86 if(!empty($_REQUEST['remote']) ||
!empty($_REQUEST['xhr']) ||
!empty($_REQUEST['xmlhttprequest'])) {
87 $this->remote
= $this->xhr
= $this->xmlhttprequest
= true;
91 $params = empty($this->params_string
) ?
"" : "?{$this->params_string}";
92 $request_url = substr($request_url, 1);
93 $this->url
= "{$this->protocol}://{$this->host}/{$this->directory}{$request_url}{$params}";
95 // possibly put some serialization code in here?
99 private function process_request($request) {
101 $route = Router2
::route($request);
103 // set routing variables
104 $this->route
= $route;
105 foreach($route->mapping() as $routing_symbol) {
106 $this->$routing_symbol = $route->$routing_symbol;
110 public function __set($name, $value) {
111 return false; // cannot set Request variables, it's just the nature of the Request object/variables
113 public function __get($name) {
114 if(isset($this->post
->$name)) return $this->post
->$name;
115 if(isset($this->get
->$name)) return $this->get
->$name;
116 if(isset($this->cookie
->$name)) return $this->cookie
->$name;
119 // return all params (from the route and from the ?... params)
120 public function params() {
123 // get params from _GET
124 foreach($_GET as $get_param => $value) {
125 $params[$get_param] = $value;
128 // getting routing values
129 foreach($this->route
->mapping() as $routing_symbol) {
130 $params[$routing_symbol] = $this->route
->$routing_symbol;
137 public function __destruct() {
138 // possibly put some serialization code in here?
143 // specific request variables (POST, GET, etc)
145 private $values = array();
148 public function __construct($values) {
149 foreach($values as $key=>$value) $this->values
[$key] = $value;
153 public function __get($name) {
154 return $this->values
[$name];
156 public function __set($name, $value) {
157 return $this->values
[$name] = $value;
161 class RequestException
extends StdException
{}