Maslosoft Staple API
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
<?php
/**
* This software package is licensed under AGPL or Commercial license.
*
* @package maslosoft/staple
* @licence AGPL or Commercial
* @copyright Copyright (c) Piotr Masełkowski <pmaselkowski@gmail.com>
* @copyright Copyright (c) Maslosoft
* @link http://maslosoft.com/staple/
*/
namespace Maslosoft\Staple\Request;
use Maslosoft\Staple\Helpers\RequestHandler;
use Maslosoft\Staple\Interfaces\RequestAwareInterface;
use Maslosoft\Staple\Interfaces\RequestInterface;
class HttpRequest implements RequestInterface
{
private $handler = null;
/**
* Request path
* @var string
*/
private $path = null;
/**
* Return true if is secure connection
* @return bool
*/
public function isSecure()
{
return isset($_SERVER['HTTPS']) && (strcasecmp($_SERVER['HTTPS'], 'on') === 0 || $_SERVER['HTTPS'] == 1) || isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && strcasecmp($_SERVER['HTTP_X_FORWARDED_PROTO'], 'https') === 0;
}
/**
* Get current website host name
* @return boolean|string
*/
public function getHost()
{
if (isset($_SERVER['HTTP_HOST']))
{
return $_SERVER['HTTP_HOST'];
}
if (isset($_SERVER['SERVER_NAME']))
{
return $_SERVER['SERVER_NAME'];
}
return false;
}
/**
* Enforce secure connection
*/
public function forceSecure()
{
if (!$this->isSecure())
{
$url = sprintf('https://%s%s', $this->getHost(), $_SERVER['REQUEST_URI']);
header("Location: $url", true, 301);
}
}
public function dispatch(RequestAwareInterface $owner)
{
$path = null;
$view = null;
$urlPath = $this->getPath();
$basePath = sprintf('%s/%s/%s', $owner->getRootPath(), $owner->getContentPath(), $this->_sanitizeUrl($urlPath));
$this->resolve($owner, $path, $view);
$this->handler = new RequestHandler;
return $this->handler->handle($owner, $basePath, $path, $view);
}
public function canHandle(RequestAwareInterface $owner)
{
$path = null;
$view = null;
$this->resolve($owner, $path, $view);
return $view || $path;
}
public function getFileName(RequestAwareInterface $owner)
{
$path = null;
$view = null;
$this->resolve($owner, $path, $view);
return $path;
}
public function getData()
{
return $this->handler ? $this->handler->getData() : [];
}
/**
* Request path, will auto detect if not set.
* @return string
*/
public function getPath()
{
if (null === $this->path)
{
$this->path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
}
return $this->path;
}
/**
* Set request path. If not set, it will take from $_SERVER vars
* @param string $path
* @return static
*/
public function setPath($path)
{
$this->path = $path;
return $this;
}
/**
* Get filename
* @param string $path
* @param string $ext
* @return boolean|string
*/
private function _getFilename($path, $ext)
{
$extRegexp = preg_quote($ext);
if (preg_match("~$extRegexp$~i", $path))
{
return $path;
}
return false;
}
/**
* Sanitize path. Remove double slashes and trailing slash.
* @param RequestAwareInterface $owner
* @param string $urlPath
* @return string
*/
private function _sanitizeUrl($urlPath)
{
$patterns = [
'~/+~' => '/',
'~/$~' => '',
'~^/~' => ''
];
return preg_replace(array_keys($patterns), array_values($patterns), $urlPath);
}
private function resolve($owner, &$path, &$view)
{
$urlPath = $this->getPath();
$basePath = sprintf('%s/%s/%s', $owner->getRootPath(), $owner->getContentPath(), $this->_sanitizeUrl($urlPath));
$path = null;
$view = null;
foreach ($owner->getExtensions() as $ext)
{
// Check for file
$path = $this->_getFilename($basePath, $ext);
$extRegexp = preg_quote($ext);
if (false !== $path)
{
$view = $this->_sanitizeUrl(preg_replace("~\.$extRegexp$~i", '', $urlPath));
break;
}
// Check for index if folder like url
if (is_dir($basePath))
{
$path = $this->_getFilename(sprintf('%s/index.%s', $basePath, $ext), $ext);
if (false !== $path && file_exists($path))
{
$view = $this->_sanitizeUrl(sprintf('%s/%s', $urlPath, preg_replace("~\.$extRegexp$~i", '', basename($path))));
return;
}
}
}
}
}
API documentation generated by ApiGen