Overview

Namespaces

  • api
  • config
  • database
  • PHP
  • Slim
    • Exception
    • Http
    • Middleware
  • utiliy

Classes

  • Headers
  • Request
  • Response
  • Util
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * Slim - a micro PHP 5 framework
  4:  *
  5:  * @author      Josh Lockhart <info@slimframework.com>
  6:  * @copyright   2011 Josh Lockhart
  7:  * @link        http://www.slimframework.com
  8:  * @license     http://www.slimframework.com/license
  9:  * @version     2.2.0
 10:  * @package     Slim
 11:  *
 12:  * MIT LICENSE
 13:  *
 14:  * Permission is hereby granted, free of charge, to any person obtaining
 15:  * a copy of this software and associated documentation files (the
 16:  * "Software"), to deal in the Software without restriction, including
 17:  * without limitation the rights to use, copy, modify, merge, publish,
 18:  * distribute, sublicense, and/or sell copies of the Software, and to
 19:  * permit persons to whom the Software is furnished to do so, subject to
 20:  * the following conditions:
 21:  *
 22:  * The above copyright notice and this permission notice shall be
 23:  * included in all copies or substantial portions of the Software.
 24:  *
 25:  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 26:  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 27:  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 28:  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 29:  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 30:  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 31:  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 32:  */
 33: namespace Slim\Http;
 34: 
 35: /**
 36:  * Slim HTTP Request
 37:  *
 38:  * This class provides a human-friendly interface to the Slim environment variables;
 39:  * environment variables are passed by reference and will be modified directly.
 40:  *
 41:  * @package Slim
 42:  * @author  Josh Lockhart
 43:  * @since   1.0.0
 44:  */
 45: class Request
 46: {
 47:     const METHOD_HEAD = 'HEAD';
 48:     const METHOD_GET = 'GET';
 49:     const METHOD_POST = 'POST';
 50:     const METHOD_PUT = 'PUT';
 51:     const METHOD_DELETE = 'DELETE';
 52:     const METHOD_OPTIONS = 'OPTIONS';
 53:     const METHOD_OVERRIDE = '_METHOD';
 54: 
 55:     /**
 56:      * @var array
 57:      */
 58:     protected static $formDataMediaTypes = array('application/x-www-form-urlencoded');
 59: 
 60:     /**
 61:      * @var array
 62:      */
 63:     protected $env;
 64: 
 65:     /**
 66:      * Constructor
 67:      * @param array $env
 68:      * @see   \Slim\Environment
 69:      */
 70:     public function __construct($env)
 71:     {
 72:         $this->env = $env;
 73:     }
 74: 
 75:     /**
 76:      * Get HTTP method
 77:      * @return string
 78:      */
 79:     public function getMethod()
 80:     {
 81:         return $this->env['REQUEST_METHOD'];
 82:     }
 83: 
 84:     /**
 85:      * Is this a GET request?
 86:      * @return bool
 87:      */
 88:     public function isGet()
 89:     {
 90:         return $this->getMethod() === self::METHOD_GET;
 91:     }
 92: 
 93:     /**
 94:      * Is this a POST request?
 95:      * @return bool
 96:      */
 97:     public function isPost()
 98:     {
 99:         return $this->getMethod() === self::METHOD_POST;
100:     }
101: 
102:     /**
103:      * Is this a PUT request?
104:      * @return bool
105:      */
106:     public function isPut()
107:     {
108:         return $this->getMethod() === self::METHOD_PUT;
109:     }
110: 
111:     /**
112:      * Is this a DELETE request?
113:      * @return bool
114:      */
115:     public function isDelete()
116:     {
117:         return $this->getMethod() === self::METHOD_DELETE;
118:     }
119: 
120:     /**
121:      * Is this a HEAD request?
122:      * @return bool
123:      */
124:     public function isHead()
125:     {
126:         return $this->getMethod() === self::METHOD_HEAD;
127:     }
128: 
129:     /**
130:      * Is this a OPTIONS request?
131:      * @return bool
132:      */
133:     public function isOptions()
134:     {
135:         return $this->getMethod() === self::METHOD_OPTIONS;
136:     }
137: 
138:     /**
139:      * Is this an AJAX request?
140:      * @return bool
141:      */
142:     public function isAjax()
143:     {
144:         if ($this->params('isajax')) {
145:             return true;
146:         } elseif (isset($this->env['X_REQUESTED_WITH']) && $this->env['X_REQUESTED_WITH'] === 'XMLHttpRequest') {
147:             return true;
148:         } else {
149:             return false;
150:         }
151:     }
152: 
153:     /**
154:      * Is this an XHR request? (alias of Slim_Http_Request::isAjax)
155:      * @return bool
156:      */
157:     public function isXhr()
158:     {
159:         return $this->isAjax();
160:     }
161: 
162:     /**
163:      * Fetch GET and POST data
164:      *
165:      * This method returns a union of GET and POST data as a key-value array, or the value
166:      * of the array key if requested; if the array key does not exist, NULL is returned.
167:      *
168:      * @param  string           $key
169:      * @return array|mixed|null
170:      */
171:     public function params($key = null)
172:     {
173:         $union = array_merge($this->get(), $this->post());
174:         if ($key) {
175:             if (isset($union[$key])) {
176:                 return $union[$key];
177:             } else {
178:                 return null;
179:             }
180:         } else {
181:             return $union;
182:         }
183:     }
184: 
185:     /**
186:      * Fetch GET data
187:      *
188:      * This method returns a key-value array of data sent in the HTTP request query string, or
189:      * the value of the array key if requested; if the array key does not exist, NULL is returned.
190:      *
191:      * @param  string           $key
192:      * @return array|mixed|null
193:      */
194:     public function get($key = null)
195:     {
196:         if (!isset($this->env['slim.request.query_hash'])) {
197:             $output = array();
198:             if (function_exists('mb_parse_str') && !isset($this->env['slim.tests.ignore_multibyte'])) {
199:                 mb_parse_str($this->env['QUERY_STRING'], $output);
200:             } else {
201:                 parse_str($this->env['QUERY_STRING'], $output);
202:             }
203:             $this->env['slim.request.query_hash'] = Util::stripSlashesIfMagicQuotes($output);
204:         }
205:         if ($key) {
206:             if (isset($this->env['slim.request.query_hash'][$key])) {
207:                 return $this->env['slim.request.query_hash'][$key];
208:             } else {
209:                 return null;
210:             }
211:         } else {
212:             return $this->env['slim.request.query_hash'];
213:         }
214:     }
215: 
216:     /**
217:      * Fetch POST data
218:      *
219:      * This method returns a key-value array of data sent in the HTTP request body, or
220:      * the value of a hash key if requested; if the array key does not exist, NULL is returned.
221:      *
222:      * @param  string           $key
223:      * @return array|mixed|null
224:      * @throws \RuntimeException If environment input is not available
225:      */
226:     public function post($key = null)
227:     {
228:         if (!isset($this->env['slim.input'])) {
229:             throw new \RuntimeException('Missing slim.input in environment variables');
230:         }
231:         if (!isset($this->env['slim.request.form_hash'])) {
232:             $this->env['slim.request.form_hash'] = array();
233:             if ($this->isFormData() && is_string($this->env['slim.input'])) {
234:                 $output = array();
235:                 if (function_exists('mb_parse_str') && !isset($this->env['slim.tests.ignore_multibyte'])) {
236:                     mb_parse_str($this->env['slim.input'], $output);
237:                 } else {
238:                     parse_str($this->env['slim.input'], $output);
239:                 }
240:                 $this->env['slim.request.form_hash'] = Util::stripSlashesIfMagicQuotes($output);
241:             } else {
242:                 $this->env['slim.request.form_hash'] = Util::stripSlashesIfMagicQuotes($_POST);
243:             }
244:         }
245:         if ($key) {
246:             if (isset($this->env['slim.request.form_hash'][$key])) {
247:                 return $this->env['slim.request.form_hash'][$key];
248:             } else {
249:                 return null;
250:             }
251:         } else {
252:             return $this->env['slim.request.form_hash'];
253:         }
254:     }
255: 
256:     /**
257:      * Fetch PUT data (alias for \Slim\Http\Request::post)
258:      * @param  string           $key
259:      * @return array|mixed|null
260:      */
261:     public function put($key = null)
262:     {
263:         return $this->post($key);
264:     }
265: 
266:     /**
267:      * Fetch DELETE data (alias for \Slim\Http\Request::post)
268:      * @param  string           $key
269:      * @return array|mixed|null
270:      */
271:     public function delete($key = null)
272:     {
273:         return $this->post($key);
274:     }
275: 
276:     /**
277:      * Fetch COOKIE data
278:      *
279:      * This method returns a key-value array of Cookie data sent in the HTTP request, or
280:      * the value of a array key if requested; if the array key does not exist, NULL is returned.
281:      *
282:      * @param  string            $key
283:      * @return array|string|null
284:      */
285:     public function cookies($key = null)
286:     {
287:         if (!isset($this->env['slim.request.cookie_hash'])) {
288:             $cookieHeader = isset($this->env['COOKIE']) ? $this->env['COOKIE'] : '';
289:             $this->env['slim.request.cookie_hash'] = Util::parseCookieHeader($cookieHeader);
290:         }
291:         if ($key) {
292:             if (isset($this->env['slim.request.cookie_hash'][$key])) {
293:                 return $this->env['slim.request.cookie_hash'][$key];
294:             } else {
295:                 return null;
296:             }
297:         } else {
298:             return $this->env['slim.request.cookie_hash'];
299:         }
300:     }
301: 
302:     /**
303:      * Does the Request body contain parseable form data?
304:      * @return bool
305:      */
306:     public function isFormData()
307:     {
308:         $method = isset($this->env['slim.method_override.original_method']) ? $this->env['slim.method_override.original_method'] : $this->getMethod();
309: 
310:         return ($method === self::METHOD_POST && is_null($this->getContentType())) || in_array($this->getMediaType(), self::$formDataMediaTypes);
311:     }
312: 
313:     /**
314:      * Get Headers
315:      *
316:      * This method returns a key-value array of headers sent in the HTTP request, or
317:      * the value of a hash key if requested; if the array key does not exist, NULL is returned.
318:      *
319:      * @param  string $key
320:      * @param  mixed  $default The default value returned if the requested header is not available
321:      * @return mixed
322:      */
323:     public function headers($key = null, $default = null)
324:     {
325:         if ($key) {
326:             $key = strtoupper($key);
327:             $key = str_replace('-', '_', $key);
328:             $key = preg_replace('@^HTTP_@', '', $key);
329:             if (isset($this->env[$key])) {
330:                 return $this->env[$key];
331:             } else {
332:                 return $default;
333:             }
334:         } else {
335:             $headers = array();
336:             foreach ($this->env as $key => $value) {
337:                 if (strpos($key, 'slim.') !== 0) {
338:                     $headers[$key] = $value;
339:                 }
340:             }
341: 
342:             return $headers;
343:         }
344:     }
345: 
346:     /**
347:      * Get Body
348:      * @return string
349:      */
350:     public function getBody()
351:     {
352:         return $this->env['slim.input'];
353:     }
354: 
355:     /**
356:      * Get Content Type
357:      * @return string
358:      */
359:     public function getContentType()
360:     {
361:         if (isset($this->env['CONTENT_TYPE'])) {
362:             return $this->env['CONTENT_TYPE'];
363:         } else {
364:             return null;
365:         }
366:     }
367: 
368:     /**
369:      * Get Media Type (type/subtype within Content Type header)
370:      * @return string|null
371:      */
372:     public function getMediaType()
373:     {
374:         $contentType = $this->getContentType();
375:         if ($contentType) {
376:             $contentTypeParts = preg_split('/\s*[;,]\s*/', $contentType);
377: 
378:             return strtolower($contentTypeParts[0]);
379:         } else {
380:             return null;
381:         }
382:     }
383: 
384:     /**
385:      * Get Media Type Params
386:      * @return array
387:      */
388:     public function getMediaTypeParams()
389:     {
390:         $contentType = $this->getContentType();
391:         $contentTypeParams = array();
392:         if ($contentType) {
393:             $contentTypeParts = preg_split('/\s*[;,]\s*/', $contentType);
394:             $contentTypePartsLength = count($contentTypeParts);
395:             for ($i = 1; $i < $contentTypePartsLength; $i++) {
396:                 $paramParts = explode('=', $contentTypeParts[$i]);
397:                 $contentTypeParams[strtolower($paramParts[0])] = $paramParts[1];
398:             }
399:         }
400: 
401:         return $contentTypeParams;
402:     }
403: 
404:     /**
405:      * Get Content Charset
406:      * @return string|null
407:      */
408:     public function getContentCharset()
409:     {
410:         $mediaTypeParams = $this->getMediaTypeParams();
411:         if (isset($mediaTypeParams['charset'])) {
412:             return $mediaTypeParams['charset'];
413:         } else {
414:             return null;
415:         }
416:     }
417: 
418:     /**
419:      * Get Content-Length
420:      * @return int
421:      */
422:     public function getContentLength()
423:     {
424:         if (isset($this->env['CONTENT_LENGTH'])) {
425:             return (int) $this->env['CONTENT_LENGTH'];
426:         } else {
427:             return 0;
428:         }
429:     }
430: 
431:     /**
432:      * Get Host
433:      * @return string
434:      */
435:     public function getHost()
436:     {
437:         if (isset($this->env['HOST'])) {
438:             if (strpos($this->env['HOST'], ':') !== false) {
439:                 $hostParts = explode(':', $this->env['HOST']);
440: 
441:                 return $hostParts[0];
442:             }
443: 
444:             return $this->env['HOST'];
445:         } else {
446:             return $this->env['SERVER_NAME'];
447:         }
448:     }
449: 
450:     /**
451:      * Get Host with Port
452:      * @return string
453:      */
454:     public function getHostWithPort()
455:     {
456:         return sprintf('%s:%s', $this->getHost(), $this->getPort());
457:     }
458: 
459:     /**
460:      * Get Port
461:      * @return int
462:      */
463:     public function getPort()
464:     {
465:         return (int) $this->env['SERVER_PORT'];
466:     }
467: 
468:     /**
469:      * Get Scheme (https or http)
470:      * @return string
471:      */
472:     public function getScheme()
473:     {
474:         return $this->env['slim.url_scheme'];
475:     }
476: 
477:     /**
478:      * Get Script Name (physical path)
479:      * @return string
480:      */
481:     public function getScriptName()
482:     {
483:         return $this->env['SCRIPT_NAME'];
484:     }
485: 
486:     /**
487:      * LEGACY: Get Root URI (alias for Slim_Http_Request::getScriptName)
488:      * @return string
489:      */
490:     public function getRootUri()
491:     {
492:         return $this->getScriptName();
493:     }
494: 
495:     /**
496:      * Get Path (physical path + virtual path)
497:      * @return string
498:      */
499:     public function getPath()
500:     {
501:         return $this->getScriptName() . $this->getPathInfo();
502:     }
503: 
504:     /**
505:      * Get Path Info (virtual path)
506:      * @return string
507:      */
508:     public function getPathInfo()
509:     {
510:         return $this->env['PATH_INFO'];
511:     }
512: 
513:     /**
514:      * LEGACY: Get Resource URI (alias for Slim_Http_Request::getPathInfo)
515:      * @return string
516:      */
517:     public function getResourceUri()
518:     {
519:         return $this->getPathInfo();
520:     }
521: 
522:     /**
523:      * Get URL (scheme + host [ + port if non-standard ])
524:      * @return string
525:      */
526:     public function getUrl()
527:     {
528:         $url = $this->getScheme() . '://' . $this->getHost();
529:         if (($this->getScheme() === 'https' && $this->getPort() !== 443) || ($this->getScheme() === 'http' && $this->getPort() !== 80)) {
530:             $url .= sprintf(':%s', $this->getPort());
531:         }
532: 
533:         return $url;
534:     }
535: 
536:     /**
537:      * Get IP
538:      * @return string
539:      */
540:     public function getIp()
541:     {
542:         if (isset($this->env['X_FORWARDED_FOR'])) {
543:             return $this->env['X_FORWARDED_FOR'];
544:         } elseif (isset($this->env['CLIENT_IP'])) {
545:             return $this->env['CLIENT_IP'];
546:         }
547: 
548:         return $this->env['REMOTE_ADDR'];
549:     }
550: 
551:     /**
552:      * Get Referrer
553:      * @return string|null
554:      */
555:     public function getReferrer()
556:     {
557:         if (isset($this->env['REFERER'])) {
558:             return $this->env['REFERER'];
559:         } else {
560:             return null;
561:         }
562:     }
563: 
564:     /**
565:      * Get Referer (for those who can't spell)
566:      * @return string|null
567:      */
568:     public function getReferer()
569:     {
570:         return $this->getReferrer();
571:     }
572: 
573:     /**
574:      * Get User Agent
575:      * @return string|null
576:      */
577:     public function getUserAgent()
578:     {
579:         if (isset($this->env['USER_AGENT'])) {
580:             return $this->env['USER_AGENT'];
581:         } else {
582:             return null;
583:         }
584:     }
585: }
586: 
GeoApi API documentation generated by ApiGen 2.8.0