ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
Request.php
Go to the documentation of this file.
1 <?php
49 
50  const METHOD_HEAD = 'HEAD';
51  const METHOD_GET = 'GET';
52  const METHOD_POST = 'POST';
53  const METHOD_PUT = 'PUT';
54  const METHOD_DELETE = 'DELETE';
55  const METHOD_OPTIONS = 'OPTIONS';
56  const METHOD_OVERRIDE = '_METHOD';
57 
61  protected $method;
62 
66  protected $headers;
67 
72  protected $additionalHeaders = array('content-type', 'content-length', 'php-auth-user', 'php-auth-pw', 'auth-type', 'x-requested-with');
73 
78  protected $cookies;
79 
83  protected $get;
84 
88  protected $post;
89 
93  protected $put;
94 
98  protected $body;
99 
103  protected $contentType;
104 
108  protected $resource;
109 
116  protected $root;
117 
121  public function __construct() {
122  $this->method = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : false;
123  $this->headers = $this->loadHttpHeaders();
124  $this->body = @file_get_contents('php://input');
125  $this->get = self::stripSlashesIfMagicQuotes($_GET);
126  $this->post = self::stripSlashesIfMagicQuotes($_POST);
127  $this->put = self::stripSlashesIfMagicQuotes($this->loadPutParameters());
128  $this->cookies = self::stripSlashesIfMagicQuotes($_COOKIE);
129  $this->root = Slim_Http_Uri::getBaseUri(true);
130  $this->resource = Slim_Http_Uri::getUri(true);
132  }
133 
138  public function isGet() {
139  return $this->method === self::METHOD_GET;
140  }
141 
146  public function isPost() {
147  return $this->method === self::METHOD_POST;
148  }
149 
154  public function isPut() {
155  return $this->method === self::METHOD_PUT;
156  }
157 
162  public function isDelete() {
163  return $this->method === self::METHOD_DELETE;
164  }
165 
170  public function isHead() {
171  return $this->method === self::METHOD_HEAD;
172  }
173 
178  public function isOptions() {
179  return $this->method === self::METHOD_OPTIONS;
180  }
181 
186  public function isAjax() {
187  return ( $this->params('isajax') || $this->headers('X_REQUESTED_WITH') === 'XMLHttpRequest' );
188  }
189 
199  public function params( $key ) {
200  foreach ( array('put', 'post', 'get') as $dataSource ) {
201  $source = $this->$dataSource;
202  if ( isset($source[(string)$key]) ) {
203  return $source[(string)$key];
204  }
205  }
206  return null;
207  }
208 
216  public function get( $key = null ) {
217  return $this->arrayOrArrayValue($this->get, $key);
218  }
219 
227  public function post( $key = null ) {
228  return $this->arrayOrArrayValue($this->post, $key);
229  }
230 
238  public function put( $key = null ) {
239  return $this->arrayOrArrayValue($this->put, $key);
240  }
241 
249  public function cookies( $key = null ) {
250  return $this->arrayOrArrayValue($this->cookies, $key);
251  }
252 
260  public function headers( $key = null ) {
261  return is_null($key) ? $this->headers : $this->arrayOrArrayValue($this->headers, $this->convertHttpHeaderName($key));
262  }
263 
268  public function getBody() {
269  return $this->body;
270  }
271 
276  public function getMethod() {
277  return $this->method;
278  }
279 
284  public function getContentType() {
285  if ( !isset($this->contentType) ) {
286  $contentType = 'application/x-www-form-urlencoded';
287  $header = $this->headers('CONTENT_TYPE');
288  if ( !is_null($header) ) {
289  $headerParts = preg_split('/\s*;\s*/', $header);
290  $contentType = $headerParts[0];
291  }
292  $this->contentType = $contentType;
293  }
294  return $this->contentType;
295  }
296 
301  public function getResourceUri() {
302  return $this->resource;
303  }
304 
309  public function getRootUri() {
310  return $this->root;
311  }
312 
319  protected function arrayOrArrayValue( array &$array, $key = null ) {
320  return is_null($key) ? $array : $this->arrayValueForKey($array, $key);
321  }
322 
327  protected function arrayValueForKey( array &$array, $key ) {
328  return isset($array[(string)$key]) ? $array[(string)$key] : null;
329  }
330 
336  public static function stripSlashesIfMagicQuotes( $rawData ) {
337  if ( get_magic_quotes_gpc() ) {
338  return is_array($rawData) ? array_map(array('self', 'stripSlashesIfMagicQuotes'), $rawData) : stripslashes($rawData);
339  } else {
340  return $rawData;
341  }
342  }
343 
348  protected function loadPutParameters() {
349  if ( $this->getContentType() === 'application/x-www-form-urlencoded' ) {
350  $input = is_string($this->body) ? $this->body : '';
351  if ( function_exists('mb_parse_str') ) {
352  mb_parse_str($input, $output);
353  } else {
354  parse_str($input, $output);
355  }
356  return $output;
357  } else {
358  return array();
359  }
360  }
361 
366  protected function loadHttpHeaders() {
367  $headers = array();
368  foreach ( $_SERVER as $key => $value ) {
369  $key = $this->convertHttpHeaderName($key);
370  if ( strpos($key, 'http-') === 0 || in_array($key, $this->additionalHeaders) ) {
371  $name = str_replace('http-', '', $key);
372  $headers[$name] = $value;
373  }
374  }
375  return $headers;
376  }
377 
382  protected function convertHttpHeaderName( $name ) {
383  return str_replace('_', '-', strtolower($name));
384  }
385 
395  protected function checkForHttpMethodOverride() {
396  if ( isset($this->post[self::METHOD_OVERRIDE]) ) {
397  $this->method = $this->post[self::METHOD_OVERRIDE];
398  unset($this->post[self::METHOD_OVERRIDE]);
399  if ( $this->isPut() ) {
400  $this->put = $this->post;
401  }
402  }
403  }
404 
405 }
cookies( $key=null)
Fetch COOKIE value(s)
Definition: Request.php:249
arrayValueForKey(array &$array, $key)
Fetch value from array.
Definition: Request.php:327
isAjax()
Is this a XHR request?
Definition: Request.php:186
isGet()
Is this a GET request?
Definition: Request.php:138
const METHOD_PUT
Definition: Request.php:53
const METHOD_OPTIONS
Definition: Request.php:55
if((!isset($_SERVER['DOCUMENT_ROOT'])) OR(empty($_SERVER['DOCUMENT_ROOT']))) $_SERVER['DOCUMENT_ROOT']
checkForHttpMethodOverride()
Check for HTTP request method override.
Definition: Request.php:395
$_GET["client_id"]
arrayOrArrayValue(array &$array, $key=null)
Fetch array or array value.
Definition: Request.php:319
convertHttpHeaderName( $name)
Convert HTTP header name.
Definition: Request.php:382
isOptions()
Is this a OPTIONS request?
Definition: Request.php:178
getContentType()
Get HTTP request content type.
Definition: Request.php:284
getMethod()
Get HTTP method.
Definition: Request.php:276
isHead()
Is this a HEAD request?
Definition: Request.php:170
isDelete()
Is this a DELETE request?
Definition: Request.php:162
Add rich text string
The name of the decorator.
const METHOD_DELETE
Definition: Request.php:54
getBody()
Get HTTP request body.
Definition: Request.php:268
post( $key=null)
Fetch POST parameter(s)
Definition: Request.php:227
if(!is_dir( $entity_dir)) exit("Fatal Error ([A-Za-z0-9]+)\+" &#(? foreach( $entity_files as $file) $output
getRootUri()
Get HTTP request root URI.
Definition: Request.php:309
isPost()
Is this a POST request?
Definition: Request.php:146
__construct()
Constructor.
Definition: Request.php:121
const METHOD_OVERRIDE
Definition: Request.php:56
const METHOD_POST
Definition: Request.php:52
$header
getResourceUri()
Get HTTP request resource URI.
Definition: Request.php:301
loadHttpHeaders()
Get HTTP request headers.
Definition: Request.php:366
static stripSlashesIfMagicQuotes( $rawData)
Strip slashes from string or array of strings.
Definition: Request.php:336
put( $key=null)
Fetch PUT parameter(s)
Definition: Request.php:238
params( $key)
Fetch a PUT|POST|GET parameter value.
Definition: Request.php:199
isPut()
Is this a PUT request?
Definition: Request.php:154
Create styles array
The data for the language used.
headers( $key=null)
Get HTTP request header.
Definition: Request.php:260
const METHOD_GET
Definition: Request.php:51
loadPutParameters()
Get PUT parameters.
Definition: Request.php:348
$_COOKIE['ilClientId']
Definition: BPMN2Parser.php:15
const METHOD_HEAD
Definition: Request.php:50
$_POST["username"]
static getBaseUri( $reload=false)
Get Base URI without trailing slash.
Definition: Uri.php:69
static getUri( $reload=false)
Get URI with leading slash.
Definition: Uri.php:85