ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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');
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}
$_GET["client_id"]
cookies( $key=null)
Fetch COOKIE value(s)
Definition: Request.php:249
getResourceUri()
Get HTTP request resource URI.
Definition: Request.php:301
headers( $key=null)
Get HTTP request header.
Definition: Request.php:260
put( $key=null)
Fetch PUT parameter(s)
Definition: Request.php:238
convertHttpHeaderName( $name)
Convert HTTP header name.
Definition: Request.php:382
const METHOD_OPTIONS
Definition: Request.php:55
checkForHttpMethodOverride()
Check for HTTP request method override.
Definition: Request.php:395
isHead()
Is this a HEAD request?
Definition: Request.php:170
static stripSlashesIfMagicQuotes( $rawData)
Strip slashes from string or array of strings.
Definition: Request.php:336
loadPutParameters()
Get PUT parameters.
Definition: Request.php:348
const METHOD_HEAD
Definition: Request.php:50
isOptions()
Is this a OPTIONS request?
Definition: Request.php:178
getBody()
Get HTTP request body.
Definition: Request.php:268
const METHOD_DELETE
Definition: Request.php:54
isAjax()
Is this a XHR request?
Definition: Request.php:186
__construct()
Constructor.
Definition: Request.php:121
const METHOD_OVERRIDE
Definition: Request.php:56
const METHOD_PUT
Definition: Request.php:53
arrayOrArrayValue(array &$array, $key=null)
Fetch array or array value.
Definition: Request.php:319
post( $key=null)
Fetch POST parameter(s)
Definition: Request.php:227
isPost()
Is this a POST request?
Definition: Request.php:146
getContentType()
Get HTTP request content type.
Definition: Request.php:284
arrayValueForKey(array &$array, $key)
Fetch value from array.
Definition: Request.php:327
const METHOD_POST
Definition: Request.php:52
isDelete()
Is this a DELETE request?
Definition: Request.php:162
isPut()
Is this a PUT request?
Definition: Request.php:154
isGet()
Is this a GET request?
Definition: Request.php:138
const METHOD_GET
Definition: Request.php:51
params( $key)
Fetch a PUT|POST|GET parameter value.
Definition: Request.php:199
getRootUri()
Get HTTP request root URI.
Definition: Request.php:309
loadHttpHeaders()
Get HTTP request headers.
Definition: Request.php:366
getMethod()
Get HTTP method.
Definition: Request.php:276
static getUri( $reload=false)
Get URI with leading slash.
Definition: Uri.php:85
static getBaseUri( $reload=false)
Get Base URI without trailing slash.
Definition: Uri.php:69
$_POST['username']
Definition: cron.php:12
$_COOKIE["ilClientId"]
Definition: cron.php:11
$header
if((!isset($_SERVER['DOCUMENT_ROOT'])) OR(empty($_SERVER['DOCUMENT_ROOT']))) $_SERVER['DOCUMENT_ROOT']