o2system /
kernel
| 1 | <?php |
||||
| 2 | /** |
||||
| 3 | * This file is part of the O2System Framework package. |
||||
| 4 | * |
||||
| 5 | * For the full copyright and license information, please view the LICENSE |
||||
| 6 | * file that was distributed with this source code. |
||||
| 7 | * |
||||
| 8 | * @author Steeve Andrian Salim |
||||
| 9 | * @copyright Copyright (c) Steeve Andrian Salim |
||||
| 10 | */ |
||||
| 11 | |||||
| 12 | // ------------------------------------------------------------------------ |
||||
| 13 | |||||
| 14 | namespace O2System\Kernel\Http\Message\Uri; |
||||
| 15 | |||||
| 16 | // ------------------------------------------------------------------------ |
||||
| 17 | |||||
| 18 | use O2System\Spl\DataStructures\SplArrayObject; |
||||
| 19 | use O2System\Spl\Exceptions\RuntimeException; |
||||
| 20 | use O2System\Spl\Iterators\ArrayIterator; |
||||
| 21 | |||||
| 22 | /** |
||||
| 23 | * Class Segments |
||||
| 24 | * |
||||
| 25 | * @package O2System\Kernel\Http\Message\Uri |
||||
| 26 | */ |
||||
| 27 | class Segments extends ArrayIterator |
||||
| 28 | { |
||||
| 29 | /** |
||||
| 30 | * Segments::__construct |
||||
| 31 | * |
||||
| 32 | * @param string|null $segments |
||||
| 33 | * |
||||
| 34 | * @throws \O2System\Spl\Exceptions\RuntimeException |
||||
| 35 | */ |
||||
| 36 | public function __construct($segments = null) |
||||
| 37 | { |
||||
| 38 | parent::__construct([]); |
||||
| 39 | |||||
| 40 | if (is_null($segments)) { |
||||
| 41 | if (kernel()->services->has('config')) { |
||||
| 42 | if (config()->offsetExists('uri')) { |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 43 | $protocol = strtoupper(config('uri')->offsetGet('protocol')); |
||||
| 44 | } |
||||
| 45 | } |
||||
| 46 | |||||
| 47 | empty($protocol) && $protocol = 'REQUEST_URI'; |
||||
| 48 | |||||
| 49 | switch ($protocol) { |
||||
| 50 | case 'AUTO': |
||||
| 51 | case 'REQUEST_URI': |
||||
| 52 | $segments = $this->parseRequestUri(); |
||||
| 53 | break; |
||||
| 54 | case 'QUERY_STRING': |
||||
| 55 | $segments = $this->parseQueryString(); |
||||
| 56 | break; |
||||
| 57 | case 'PATH_INFO': |
||||
| 58 | default: |
||||
| 59 | $segments = isset($_SERVER[ $protocol ]) |
||||
| 60 | ? $_SERVER[ $protocol ] |
||||
| 61 | : $this->parseRequestUri(); |
||||
| 62 | break; |
||||
| 63 | } |
||||
| 64 | |||||
| 65 | } elseif (is_array($segments)) { |
||||
|
0 ignored issues
–
show
|
|||||
| 66 | $segments = implode('/', $segments); |
||||
| 67 | } |
||||
| 68 | |||||
| 69 | $segments = str_replace(['\\', '_'], ['/', '-'], $segments); |
||||
| 70 | $segments = trim(remove_invisible_characters($segments, false), '/'); |
||||
| 71 | |||||
| 72 | $this->setParts(explode('/', $segments)); |
||||
| 73 | } |
||||
| 74 | |||||
| 75 | // ------------------------------------------------------------------------ |
||||
| 76 | |||||
| 77 | /** |
||||
| 78 | * Segments::parseRequestUri |
||||
| 79 | * |
||||
| 80 | * Parse REQUEST_URI |
||||
| 81 | * |
||||
| 82 | * Will parse REQUEST_URI and automatically detect the URI from it, |
||||
| 83 | * while fixing the query string if necessary. |
||||
| 84 | * |
||||
| 85 | * @access protected |
||||
| 86 | * @return string |
||||
| 87 | */ |
||||
| 88 | protected function parseRequestUri() |
||||
| 89 | { |
||||
| 90 | if ( ! isset($_SERVER[ 'REQUEST_URI' ], $_SERVER[ 'SCRIPT_NAME' ])) { |
||||
| 91 | return ''; |
||||
| 92 | } |
||||
| 93 | |||||
| 94 | $uri = parse_url($_SERVER[ 'REQUEST_URI' ]); |
||||
| 95 | $query = isset($uri[ 'query' ]) |
||||
| 96 | ? $uri[ 'query' ] |
||||
| 97 | : ''; |
||||
| 98 | $uri = isset($uri[ 'path' ]) |
||||
| 99 | ? $uri[ 'path' ] |
||||
| 100 | : ''; |
||||
| 101 | |||||
| 102 | if (isset($_SERVER[ 'SCRIPT_NAME' ][ 0 ])) { |
||||
| 103 | if (strpos($uri, $_SERVER[ 'SCRIPT_NAME' ]) === 0) { |
||||
| 104 | $uri = (string)substr($uri, strlen($_SERVER[ 'SCRIPT_NAME' ])); |
||||
| 105 | } elseif (strpos($uri, dirname($_SERVER[ 'SCRIPT_NAME' ])) === 0) { |
||||
| 106 | $uri = (string)substr($uri, strlen(dirname($_SERVER[ 'SCRIPT_NAME' ]))); |
||||
| 107 | } |
||||
| 108 | } |
||||
| 109 | |||||
| 110 | // This section ensures that even on servers that require the URI to be in the query string (Nginx) a correct |
||||
| 111 | // URI is found, and also fixes the QUERY_STRING server var and $_GET array. |
||||
| 112 | if (trim($uri, '/') === '' AND strncmp($query, '/', 1) === 0) { |
||||
| 113 | $query = explode('?', $query, 2); |
||||
| 114 | $uri = $query[ 0 ]; |
||||
| 115 | |||||
| 116 | $_SERVER[ 'QUERY_STRING' ] = isset($query[ 1 ]) |
||||
| 117 | ? $query[ 1 ] |
||||
| 118 | : ''; |
||||
| 119 | } else { |
||||
| 120 | $_SERVER[ 'QUERY_STRING' ] = $query; |
||||
| 121 | } |
||||
| 122 | |||||
| 123 | if (isset($_GET[ 'SEGMENTS_STRING' ])) { |
||||
| 124 | $uri = $_GET[ 'SEGMENTS_STRING' ]; |
||||
| 125 | unset($_GET[ 'SEGMENTS_STRING' ]); |
||||
| 126 | |||||
| 127 | $_SERVER[ 'QUERY_STRING' ] = str_replace([ |
||||
| 128 | 'SEGMENTS_STRING=' . $uri . '&', |
||||
| 129 | 'SEGMENTS_STRING=' . $uri, |
||||
| 130 | ], '', $_SERVER[ 'QUERY_STRING' ]); |
||||
| 131 | } |
||||
| 132 | |||||
| 133 | parse_str($_SERVER[ 'QUERY_STRING' ], $_GET); |
||||
| 134 | |||||
| 135 | if ($uri === '/' || $uri === '') { |
||||
| 136 | return '/'; |
||||
| 137 | } |
||||
| 138 | |||||
| 139 | return $uri; |
||||
| 140 | } |
||||
| 141 | |||||
| 142 | // ------------------------------------------------------------------------ |
||||
| 143 | |||||
| 144 | /** |
||||
| 145 | * Segments::parseQueryString |
||||
| 146 | * |
||||
| 147 | * Parse QUERY_STRING |
||||
| 148 | * |
||||
| 149 | * Will parse QUERY_STRING and automatically detect the URI from it. |
||||
| 150 | * |
||||
| 151 | * @access protected |
||||
| 152 | * @return string |
||||
| 153 | */ |
||||
| 154 | protected function parseQueryString() |
||||
| 155 | { |
||||
| 156 | $uri = isset($_SERVER[ 'QUERY_STRING' ]) |
||||
| 157 | ? $_SERVER[ 'QUERY_STRING' ] |
||||
| 158 | : @getenv('QUERY_STRING'); |
||||
| 159 | |||||
| 160 | if (trim($uri, '/') === '') { |
||||
| 161 | return ''; |
||||
| 162 | } elseif (strncmp($uri, '/', 1) === 0) { |
||||
| 163 | $uri = explode('?', $uri, 2); |
||||
| 164 | $_SERVER[ 'QUERY_STRING' ] = isset($uri[ 1 ]) |
||||
| 165 | ? $uri[ 1 ] |
||||
| 166 | : ''; |
||||
| 167 | $uri = rawurldecode($uri[ 0 ]); |
||||
| 168 | } |
||||
| 169 | |||||
| 170 | parse_str($_SERVER[ 'QUERY_STRING' ], $_GET); |
||||
| 171 | |||||
| 172 | return $uri; |
||||
| 173 | } |
||||
| 174 | |||||
| 175 | // -------------------------------------------------------------------- |
||||
| 176 | |||||
| 177 | /** |
||||
| 178 | * Segments::addString |
||||
| 179 | * |
||||
| 180 | * @param string $string |
||||
| 181 | * |
||||
| 182 | * @return \O2System\Kernel\Http\Message\Uri\Segments |
||||
| 183 | * @throws \O2System\Spl\Exceptions\RuntimeException |
||||
| 184 | */ |
||||
| 185 | public function addString($string) |
||||
| 186 | { |
||||
| 187 | $string = $this->__toString() . '/' . trim($string, '/'); |
||||
| 188 | |||||
| 189 | return $this->withString($string); |
||||
| 190 | } |
||||
| 191 | |||||
| 192 | // ------------------------------------------------------------------------ |
||||
| 193 | |||||
| 194 | /** |
||||
| 195 | * Segments::withString |
||||
| 196 | * |
||||
| 197 | * @param string $string |
||||
| 198 | * |
||||
| 199 | * @return \O2System\Kernel\Http\Message\Uri\Segments |
||||
| 200 | * @throws \O2System\Spl\Exceptions\RuntimeException |
||||
| 201 | */ |
||||
| 202 | public function withString($string) |
||||
| 203 | { |
||||
| 204 | $string = trim(remove_invisible_characters($string, false), '/'); |
||||
| 205 | |||||
| 206 | return $this->withParts(explode('/', $string)); |
||||
| 207 | } |
||||
| 208 | |||||
| 209 | // ------------------------------------------------------------------------ |
||||
| 210 | |||||
| 211 | /** |
||||
| 212 | * Segments::withParts |
||||
| 213 | * |
||||
| 214 | * @param array $parts |
||||
| 215 | * |
||||
| 216 | * @return \O2System\Kernel\Http\Message\Uri\Segments |
||||
| 217 | * @throws \O2System\Spl\Exceptions\RuntimeException |
||||
| 218 | */ |
||||
| 219 | public function withParts(array $parts) |
||||
| 220 | { |
||||
| 221 | $uri = clone $this; |
||||
| 222 | $uri->setParts($parts); |
||||
| 223 | |||||
| 224 | return $uri; |
||||
| 225 | } |
||||
| 226 | |||||
| 227 | // ------------------------------------------------------------------------ |
||||
| 228 | |||||
| 229 | /** |
||||
| 230 | * Segments::addParts |
||||
| 231 | * |
||||
| 232 | * @param array $parts |
||||
| 233 | * |
||||
| 234 | * @return \O2System\Kernel\Http\Message\Uri\Segments |
||||
| 235 | * @throws \O2System\Spl\Exceptions\RuntimeException |
||||
| 236 | */ |
||||
| 237 | public function addParts(array $parts) |
||||
| 238 | { |
||||
| 239 | return $this->withParts($parts); |
||||
| 240 | } |
||||
| 241 | |||||
| 242 | // ------------------------------------------------------------------------ |
||||
| 243 | |||||
| 244 | /** |
||||
| 245 | * Segments::getPart |
||||
| 246 | * |
||||
| 247 | * Get Segment |
||||
| 248 | * |
||||
| 249 | * @param int $index (n) of Uri Segments |
||||
| 250 | * |
||||
| 251 | * @return mixed |
||||
| 252 | */ |
||||
| 253 | public function getPart($index) |
||||
| 254 | { |
||||
| 255 | if($this->offsetExists($index)) { |
||||
| 256 | return $this->offsetGet($index); |
||||
| 257 | } |
||||
| 258 | |||||
| 259 | return false; |
||||
| 260 | } |
||||
| 261 | |||||
| 262 | // ------------------------------------------------------------------------ |
||||
| 263 | |||||
| 264 | /** |
||||
| 265 | * Segments::setParts |
||||
| 266 | * |
||||
| 267 | * @param array $parts |
||||
| 268 | * |
||||
| 269 | * @throws \O2System\Spl\Exceptions\RuntimeException |
||||
| 270 | */ |
||||
| 271 | protected function setParts(array $parts) |
||||
| 272 | { |
||||
| 273 | if (count($parts)) { |
||||
| 274 | $validSegments = []; |
||||
| 275 | |||||
| 276 | if (count($parts)) { |
||||
| 277 | foreach ($parts as $part) { |
||||
| 278 | // Filter segments for security |
||||
| 279 | if ($part = trim($this->filterPart($part))) { |
||||
| 280 | if (class_exists('O2System\Framework', false)) { |
||||
| 281 | if (false !== ($language = language()->registered($part))) { |
||||
|
0 ignored issues
–
show
The method
registered() does not exist on O2System\Kernel\Services\Language.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||
| 282 | language()->setDefault($part); |
||||
| 283 | |||||
| 284 | continue; |
||||
| 285 | } |
||||
| 286 | } |
||||
| 287 | |||||
| 288 | if ( ! in_array($part, $validSegments)) { |
||||
| 289 | $validSegments[] = $part; |
||||
| 290 | } |
||||
| 291 | } |
||||
| 292 | } |
||||
| 293 | } |
||||
| 294 | |||||
| 295 | $validSegments = array_filter($validSegments); |
||||
| 296 | array_unshift($validSegments, null); |
||||
| 297 | |||||
| 298 | unset($validSegments[ 0 ]); |
||||
| 299 | |||||
| 300 | $this->merge($validSegments); |
||||
| 301 | } |
||||
| 302 | } |
||||
| 303 | |||||
| 304 | // ------------------------------------------------------------------------ |
||||
| 305 | |||||
| 306 | /** |
||||
| 307 | * Segments::__toString |
||||
| 308 | * |
||||
| 309 | * @return string |
||||
| 310 | */ |
||||
| 311 | public function __toString() |
||||
| 312 | { |
||||
| 313 | if ($this->count()) { |
||||
| 314 | return implode('/', $this->getArrayCopy()); |
||||
| 315 | } |
||||
| 316 | |||||
| 317 | return ''; |
||||
| 318 | } |
||||
| 319 | |||||
| 320 | // ------------------------------------------------------------------------ |
||||
| 321 | |||||
| 322 | /** |
||||
| 323 | * Segments::filterPart |
||||
| 324 | * |
||||
| 325 | * Filters segments for malicious characters. |
||||
| 326 | * |
||||
| 327 | * @param string $string URI String |
||||
| 328 | * |
||||
| 329 | * @return mixed |
||||
| 330 | * @throws RuntimeException |
||||
| 331 | */ |
||||
| 332 | protected function filterPart($string) |
||||
| 333 | { |
||||
| 334 | if (function_exists('config')) { |
||||
| 335 | $config = config('uri'); |
||||
| 336 | } |
||||
| 337 | |||||
| 338 | if (empty($config)) { |
||||
| 339 | $config = new SplArrayObject([ |
||||
| 340 | 'permittedChars' => 'a-z 0-9~%.:_\-@#', |
||||
| 341 | 'suffix' => null, |
||||
| 342 | ]); |
||||
| 343 | } |
||||
| 344 | |||||
| 345 | if ( ! empty($string) AND |
||||
| 346 | ! empty($config->offsetGet('permittedChars')) AND |
||||
| 347 | ! preg_match('/^[' . $config->offsetGet('permittedChars') . ']+$/i', $string) AND |
||||
| 348 | ! is_cli() |
||||
| 349 | ) { |
||||
| 350 | throw new RuntimeException('E_URI_HAS_DISALLOWED_CHARACTERS', 105); |
||||
| 351 | } |
||||
| 352 | |||||
| 353 | $regex = ['$', '(', ')', '%28', '%29', 'index', '.php', '.phtml']; // Bad |
||||
| 354 | $replace = ['$', '(', ')', '(', ')']; // Good |
||||
| 355 | |||||
| 356 | if ( ! empty($config)) { |
||||
| 357 | array_push($regex, $config->offsetGet('suffix')); |
||||
| 358 | array_push($replace, ''); |
||||
| 359 | } |
||||
| 360 | |||||
| 361 | // Convert programatic characters to entities and return |
||||
| 362 | return str_replace($regex, $replace, $string); |
||||
| 363 | } |
||||
| 364 | } |