php-censor/src/Http/Request.php

148 lines
3.2 KiB
PHP
Raw Permalink Normal View History

2016-04-12 19:31:39 +02:00
<?php
2018-03-04 11:22:14 +01:00
namespace PHPCensor\Http;
2016-04-12 19:31:39 +02:00
class Request
{
/**
2016-04-20 17:39:48 +02:00
* @var array
*/
protected $params = [];
2016-04-12 19:31:39 +02:00
/**
2016-04-20 17:39:48 +02:00
* Request data.
*/
protected $data = [];
2016-04-12 19:31:39 +02:00
/**
2016-04-20 17:39:48 +02:00
* Set up the request.
*/
2016-04-12 19:31:39 +02:00
public function __construct()
{
$this->parseInput();
2016-04-21 19:05:32 +02:00
$this->data['path'] = $this->getRequestPath();
2016-04-12 19:31:39 +02:00
$this->data['parts'] = array_values(array_filter(explode('/', $this->data['path'])));
}
protected function getRequestPath()
{
$path = '';
// Start out with the REQUEST_URI:
if (!empty($_SERVER['REQUEST_URI'])) {
$path = $_SERVER['REQUEST_URI'];
}
if ($_SERVER['SCRIPT_NAME'] != $_SERVER['REQUEST_URI']) {
$scriptPath = str_replace('/index.php', '', $_SERVER['SCRIPT_NAME']);
$path = str_replace($scriptPath, '', $path);
}
// Remove index.php from the URL if it is present:
2016-04-20 17:39:48 +02:00
$path = str_replace(['/index.php', 'index.php'], '', $path);
2016-04-12 19:31:39 +02:00
// Also cut out the query string:
$path = explode('?', $path);
$path = array_shift($path);
return $path;
}
/**
2016-04-20 17:39:48 +02:00
* Parse incoming variables, incl. $_GET, $_POST and also reads php://input for PUT/DELETE.
*/
2016-04-12 19:31:39 +02:00
protected function parseInput()
{
$params = $_REQUEST;
2016-04-20 17:39:48 +02:00
if (!isset($_SERVER['REQUEST_METHOD']) || in_array($_SERVER['REQUEST_METHOD'], ['PUT', 'DELETE'])) {
2016-04-12 19:31:39 +02:00
$vars = file_get_contents('php://input');
2016-04-20 17:39:48 +02:00
if (!is_string($vars) || strlen(trim($vars)) === 0) {
2016-04-12 19:31:39 +02:00
$vars = '';
}
2016-04-20 17:39:48 +02:00
$inputData = [];
2016-04-12 19:31:39 +02:00
parse_str($vars, $inputData);
$params = array_merge($params, $inputData);
}
$this->setParams($params);
}
/**
2016-04-20 17:39:48 +02:00
* Returns all request parameters.
* @return array
*/
2016-04-12 19:31:39 +02:00
public function getParams()
{
return $this->params;
}
/**
2016-04-20 17:39:48 +02:00
* Return a specific request parameter, or a default value if not set.
*/
2016-04-12 19:31:39 +02:00
public function getParam($key, $default = null)
{
if (isset($this->params[$key])) {
return $this->params[$key];
} else {
return $default;
}
}
/**
2016-04-20 17:39:48 +02:00
* Set or override a request parameter.
*/
2016-04-12 19:31:39 +02:00
public function setParam($key, $value = null)
{
$this->params[$key] = $value;
}
/**
2016-04-20 17:39:48 +02:00
* Set an array of request parameters.
*/
2016-04-12 19:31:39 +02:00
public function setParams(array $params)
{
$this->params = array_merge($this->params, $params);
}
/**
2016-04-20 17:39:48 +02:00
* Un-set a specific parameter.
*/
2016-04-12 19:31:39 +02:00
public function unsetParam($key)
{
unset($this->params[$key]);
}
public function getMethod()
{
return strtoupper($_SERVER['REQUEST_METHOD']);
}
public function getPath()
{
return $this->data['path'];
}
public function getPathParts()
{
return $this->data['parts'];
}
public function isAjax()
{
if (!isset($_SERVER['HTTP_X_REQUESTED_WITH'])) {
return false;
}
if (strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
return true;
}
return false;
}
}