├── LICENSE.md ├── README.md ├── changelog.md └── src ├── .htaccess ├── EasyRouter.php └── nginx conf /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Andre Sieverding 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # EasyRouter 2 | 3 |

4 | 5 |

EasyRouter is a simple php routing-system.

6 |

7 | 8 | ------------- 9 | 10 | ![](http://i.imgur.com/EIIWanz.png) 11 | 12 | ### Benefits 13 | 14 | - Beautiful URL for every page on your website 15 | - Search Engine Optimization 16 | - Easy to use in new and existing projects 17 | 18 | ------------- 19 | 20 | ### Installation 21 | 22 | Move .htaccess from src into the root directory of your website. After that you have to move the EasyRouter.php from src into one directory of your website. 23 | 24 | **Important:** The [`.htaccess`](https://github.com/Teddy95/EasyRouter/blob/master/src/.htaccess) file redirects to the `index.php` file, so the code should be written into the `index.php` file! - _Alternatively you can rewrite the [`.htaccess`](https://github.com/Teddy95/EasyRouter/blob/master/src/.htaccess) file!_ 25 | 26 | Now you have to start routing on your website: 27 | 28 | ```php 29 | 33 | ``` 34 | or: 35 | 36 | ```php 37 | 41 | ``` 42 | 43 | The parameters from your URL are now in the ```$_GET``` array. 44 | 45 | ------------- 46 | 47 | ### Documentaion 48 | 49 | [https://github.com/Teddy95/EasyRouter/wiki](https://github.com/Teddy95/EasyRouter/wiki) 50 | 51 | ------------- 52 | 53 | ### Download 54 | 55 | - [Releases on Github](https://github.com/Teddy95/EasyRouter/releases) 56 | - **[Download latest version from Github](https://github.com/Teddy95/EasyRouter/archive/0.7.2.zip)** 57 | - [Download master from Github](https://github.com/Teddy95/EasyRouter/archive/master.zip) 58 | 59 | ------------- 60 | 61 | ### Contributors 62 | 63 | - [Teddy95](https://github.com/Teddy95) 64 | 65 | ------------- 66 | 67 | ### License 68 | 69 | The MIT License (MIT) - [View LICENSE.md](https://github.com/Teddy95/EasyRouter/blob/master/LICENSE.md) 70 | -------------------------------------------------------------------------------- /changelog.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | ### Version 0.7.2 - 26.09.2014 4 | 5 | - Fixed bug in .htaccess (added RewriteBase /) 6 | 7 | ### Version 0.7.1 - 08.09.2014 8 | 9 | - Fixed bug in `error()` function 10 | 11 | ### Version 0.7.0 - 01.08.2014 12 | 13 | - Add [url function](https://github.com/Teddy95/EasyRouter/wiki/url-function) 14 | 15 | ### Version 0.6.3 - 13.06.2014 16 | 17 | - Fixed bug in `get_active_path()` function 18 | 19 | ### Version 0.6.2 - 13.06.2014 20 | 21 | - Fixed bug in `get_active_path()` function 22 | 23 | ### Version 0.6.1 - 12.06.2014 24 | 25 | - Fixed bug in `get_active_path()` function 26 | 27 | ### Version 0.6.0 - 12.06.2014 28 | 29 | - Change class `route` to `main` 30 | - Change namespace `Teddy95\EasyRouter` to `EasyRouter` 31 | 32 | ### Version 0.5.2 - 09.06.2014 33 | 34 | ### Version 0.5.1 - 07.05.2014 35 | 36 | - Add options for absolute and relative paths [get active path function](https://github.com/Teddy95/EasyRouter/wiki/Get-active-path-function) 37 | 38 | ### Version 0.5.0 - 03.05.2014 39 | 40 | - Add [get active path function](https://github.com/Teddy95/EasyRouter/wiki/Get-active-path-function) 41 | - Delete [info function](https://github.com/Teddy95/EasyRouter/wiki/Info-function) 42 | - Now you can create and use JavaScript plugins for EasyRouter 43 | 44 | ### Version 0.4.1 - 02.03.2014 45 | 46 | - Fixed notices 47 | 48 | ### Version 0.4.0 - 02.02.2014 49 | 50 | - Add [launch function](https://github.com/Teddy95/EasyRouter/wiki/Launch-function) 51 | - Add [set basedir function](https://github.com/Teddy95/EasyRouter/wiki/Set-basedir-function) 52 | - Add [unset basedir function](https://github.com/Teddy95/EasyRouter/wiki/Unset-basedir-function) 53 | - Add [set params function](https://github.com/Teddy95/EasyRouter/wiki/Set-params-function) 54 | - Add [unset params function](https://github.com/Teddy95/EasyRouter/wiki/Unset-params-function) 55 | - Add [set exceptions function](https://github.com/Teddy95/EasyRouter/wiki/Set-exceptions-function) 56 | - Add [unset exceptions function](https://github.com/Teddy95/EasyRouter/wiki/Unset-exceptions-function) 57 | 58 | ### Version 0.3.0 - 02.02.2014 59 | 60 | - Add [execute function](https://github.com/Teddy95/EasyRouter/wiki/Execute-function) 61 | - Add [prepare function](https://github.com/Teddy95/EasyRouter/wiki/Prepare-function) 62 | - Add [error function](https://github.com/Teddy95/EasyRouter/wiki/Error-function) 63 | - Add [get_true_params function](https://github.com/Teddy95/EasyRouter/wiki/Get-true-params-function) 64 | - Update [start function](https://github.com/Teddy95/EasyRouter/wiki/start-function) 65 | 66 | ### Version 0.2.1 - 19.01.2014 67 | 68 | - Fixed HTTPS bug (HTTPS wasn't considered) 69 | -------------------------------------------------------------------------------- /src/.htaccess: -------------------------------------------------------------------------------- 1 | Options +FollowSymLinks 2 | 3 | 4 | RewriteEngine on 5 | # RewriteBase / 6 | RewriteRule (.*)\ /index.php [L] 7 | RewriteCond %{REQUEST_FILENAME} -f [OR] 8 | RewriteCond %{REQUEST_FILENAME} -d 9 | RewriteRule .+ - [L] 10 | RewriteRule .* index.php [L] 11 | 12 | -------------------------------------------------------------------------------- /src/EasyRouter.php: -------------------------------------------------------------------------------- 1 | prepareString = $basedir_prepareString; 72 | 73 | return; 74 | } 75 | } 76 | 77 | self::$basedir = $basedir_prepareString; 78 | self::$params = $params; 79 | self::$exceptions = $exceptions; 80 | 81 | return; 82 | 83 | } 84 | 85 | /** 86 | * @param string $basedir 87 | * 88 | * @access public 89 | * 90 | * @uses $basedir 91 | * 92 | * @return bool Returns FALSE on failure 93 | */ 94 | public static function set_basedir ($basedir) 95 | { 96 | 97 | if (isset($basedir)) { 98 | self::$basedir = $basedir; 99 | } else { 100 | return false; 101 | } 102 | 103 | } 104 | 105 | /** 106 | * @param string $basedir 107 | * 108 | * @access public 109 | * 110 | * @uses $basedir 111 | * 112 | * @return void 113 | */ 114 | public static function unset_basedir () 115 | { 116 | 117 | self::$basedir = false; 118 | 119 | return; 120 | 121 | } 122 | 123 | /** 124 | * @param array $params 125 | * 126 | * @access public 127 | * 128 | * @uses $params 129 | * 130 | * @return bool Returns FALSE on failure 131 | */ 132 | public static function set_params ($params) 133 | { 134 | 135 | if (isset($params)) { 136 | self::$params = $params; 137 | } else { 138 | return false; 139 | } 140 | 141 | } 142 | 143 | /** 144 | * @param string $params 145 | * 146 | * @access public 147 | * 148 | * @uses $params 149 | * 150 | * @return void 151 | */ 152 | public static function unset_params () 153 | { 154 | 155 | self::$params = false; 156 | 157 | return; 158 | 159 | } 160 | 161 | /** 162 | * @param array $exceptions 163 | * 164 | * @access public 165 | * 166 | * @uses $exceptions 167 | * 168 | * @return bool Returns FALSE on failure 169 | */ 170 | public static function set_exceptions ($exceptions) 171 | { 172 | 173 | if (isset($exceptions)) { 174 | self::$exceptions = $exceptions; 175 | } else { 176 | return false; 177 | } 178 | 179 | } 180 | 181 | /** 182 | * @param string $exceptions 183 | * 184 | * @access public 185 | * 186 | * @uses $exceptions 187 | * 188 | * @return void 189 | */ 190 | public static function unset_exceptions () 191 | { 192 | 193 | self::$exceptions = false; 194 | 195 | return; 196 | 197 | } 198 | 199 | /** 200 | * @param string $basedir 201 | * @param array $params 202 | * @param array $exceptions 203 | * @param bool $load_GET 204 | * 205 | * @access public 206 | * 207 | * @uses $prepareString 208 | * @uses execute() 209 | * @uses start() 210 | * 211 | * @return array Returns an array with the uri-params on success or FALSE on failure 212 | */ 213 | public static function launch ($basedir = null, $params = null, $exceptions = null, $load_GET = true) 214 | { 215 | 216 | $callback = false; 217 | 218 | if ($load_GET === true) { 219 | if (self::$prepareString !== false) { 220 | self::execute($basedir, true); 221 | } else { 222 | self::start($basedir, $params, $exceptions, true); 223 | } 224 | } else { 225 | if (self::$prepareString !== false) { 226 | $callback = self::execute($basedir, false); 227 | } else { 228 | $callback = self::start($basedir, $params, $exceptions, false); 229 | } 230 | } 231 | 232 | if (!$callback) { 233 | return; 234 | } else { 235 | return $callback; 236 | } 237 | 238 | } 239 | 240 | /** 241 | * @param string $basedir 242 | * @param array $params 243 | * @param array $exceptions 244 | * @param bool $load_GET 245 | * 246 | * @access public 247 | * 248 | * @uses $basedir 249 | * @uses $params 250 | * @uses $exceptions 251 | * @uses $paramsCount 252 | * @uses $routed 253 | * @uses $allParams 254 | * 255 | * @return array Returns an array with the uri-params on success or FALSE on failure 256 | */ 257 | public static function start ($basedir = null, $params = null, $exceptions = null, $load_GET = true) 258 | { 259 | 260 | /** 261 | * Check params. 262 | */ 263 | if (is_null($basedir) && self::$basedir == true) { 264 | $basedir = self::$basedir; 265 | } 266 | 267 | if (is_null($params) && self::$params == true) { 268 | $params = self::$params; 269 | } 270 | 271 | if (is_null($exceptions) && self::$exceptions == true) { 272 | $exceptions = self::$exceptions; 273 | } 274 | 275 | /** 276 | * Current path. 277 | */ 278 | if (is_null($basedir)) { 279 | if (!isset($_SERVER['HTTPS'])) { 280 | $uri = "http://" . $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"]; 281 | } else { 282 | $uri = "https://" . $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"]; 283 | } 284 | } else { 285 | if (substr($basedir, 0, 7) == "http://") { 286 | $uri = "http://" . $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"]; 287 | } elseif (substr($basedir, 0, 8) == "https://") { 288 | $uri = "https://" . $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"]; 289 | } else { 290 | return false; 291 | } 292 | } 293 | 294 | /** 295 | * Generate basedir. 296 | */ 297 | if (is_null($basedir)) { 298 | if (!isset($_SERVER['HTTPS'])) { 299 | $scheme = 'http://'; 300 | } else { 301 | $scheme = 'https://'; 302 | } 303 | $basedir = $scheme . $_SERVER['SERVER_NAME'] . $_SERVER['SCRIPT_NAME']; 304 | $basedir = str_replace('/' . basename($scheme . $_SERVER['SERVER_NAME'] . $_SERVER['SCRIPT_NAME']), '', $basedir); 305 | } 306 | 307 | /** 308 | * Delete last slash from the root directory. 309 | */ 310 | if (substr($basedir, -1, 1) == "/") { 311 | $basedir = substr($basedir, 0, strlen($basedir) - 1); 312 | } 313 | 314 | /** 315 | * Is there a slash at the end of the path? (only if there are no parameters!) 316 | */ 317 | $slash = ""; 318 | if ($uri == $basedir) { 319 | $slash = "/"; 320 | } elseif ($uri == $basedir . "/") { 321 | $slash = ""; 322 | } elseif (substr($uri, -1, 1) == "/") { 323 | $uri = substr($uri, 0, strlen($uri) - 1); 324 | } 325 | 326 | /** 327 | * Filter out the index file and the base directory. 328 | */ 329 | $uri = str_replace($basedir . "/", "", $uri . $slash); 330 | 331 | if (is_null($params) === true) { 332 | 333 | $paramsCount = 0; 334 | 335 | /** 336 | * Load the individual parameters in the $_GET array. 337 | */ 338 | $__GET = explode("/", urldecode($uri)); 339 | $getCount = count($__GET) - 1; 340 | 341 | if (strpos($__GET[$getCount], '?') == true) { # a question mark must be present in the parameter 342 | if (substr($__GET[$getCount], -1, 1) == '?') { # is the last character a question mark? 343 | $__GET[$getCount] = substr($__GET[$getCount], 0, strlen($__GET[$getCount]) - 1); 344 | $glue = "?"; 345 | $glueGetParam = false; 346 | } else { 347 | $glue = ""; 348 | $glueGetParam = false; 349 | } 350 | if (strpos($__GET[$getCount], '?') == true) { 351 | $glueGetParam = true; 352 | $elements = explode('?', $__GET[$getCount], 2); 353 | $__GET[$getCount] = $elements[0]; 354 | $elements[1] .= $glue; 355 | $getParams = explode('&', $elements[1]); 356 | $trueGetParams = array(); 357 | foreach ($getParams as $getParam) { 358 | if (strpos($getParam, '=') == true) { 359 | $paramArray = explode('=', $getParam, 2); 360 | $trueGetParams[$paramArray[0]] = $paramArray[1]; 361 | } 362 | } 363 | } 364 | $__GET[$getCount] .= $glueGetParam == false ? $glue : ''; 365 | } 366 | 367 | if (isset($trueGetParams) && count($trueGetParams) > 0) { 368 | foreach ($trueGetParams as $key => $value) { 369 | $__GET[$key] = $value; 370 | } 371 | } 372 | 373 | } else { 374 | 375 | $paramsCount = count($params); 376 | 377 | /** 378 | * Load the individual parameters in the $route array. 379 | */ 380 | $route = explode("/", urldecode($uri)); 381 | 382 | /** 383 | * Count $route. 384 | */ 385 | $j = count($route); 386 | $k = 0; 387 | $l = 0; 388 | 389 | /** 390 | * While $i is smaller than $j, load parameters in the $_GET array. 391 | */ 392 | for ($i = 0; $i < $j; $i++) { 393 | 394 | /** 395 | * Load the individual parameters in the $route array. 396 | */ 397 | if ($i == $j - 1) { 398 | if (strpos($route[$i], '?') == true) { # a question mark must be present in the parameter 399 | if (substr($route[$i], -1, 1) == '?') { # is the last character a question mark? 400 | $route[$i] = substr($route[$i], 0, strlen($route[$i]) - 1); 401 | $glue = "?"; 402 | $glueGetParam = false; 403 | } else { 404 | $glue = ""; 405 | $glueGetParam = false; 406 | } 407 | if (strpos($route[$i], '?') == true) { 408 | $glueGetParam = true; 409 | $elements = explode('?', $route[$i], 2); 410 | $route[$i] = $elements[0]; 411 | $elements[1] .= $glue; 412 | $getParams = explode('&', $elements[1]); 413 | $trueGetParams = array(); 414 | foreach ($getParams as $getParam) { 415 | if (strpos($getParam, '=') == true) { 416 | $paramArray = explode('=', $getParam, 2); 417 | $trueGetParams[$paramArray[0]] = $paramArray[1]; # watch paramEnd! 418 | } 419 | } 420 | } 421 | $route[$i] .= $glueGetParam == false ? $glue : ''; 422 | } 423 | } 424 | 425 | /** 426 | * If the current parameter is the exception parameter, then the parameter must be treated specially. 427 | */ 428 | if (!is_null($exceptions)) { 429 | foreach ($exceptions as $exception) { 430 | $route_i = $route[$i]; 431 | 432 | /** 433 | * Check options. 434 | */ 435 | if (isset($exception["options"]) && !is_null($exception["options"])) { 436 | if (isset($exception["revertOptions"]) && !is_null($exception["revertOptions"])) { 437 | if ($exception["revertOptions"] === true) { 438 | $routeParamOriginal = $route_i; 439 | } 440 | } 441 | 442 | if (isset($exception["options"]["strtolower"]) && $exception["options"]["strtolower"] === true) { 443 | $route_i = strtolower($route_i); 444 | } 445 | if (isset($exception["options"]["strtoupper"]) && $exception["options"]["strtoupper"] === true) { 446 | $route_i = strtoupper($route_i); 447 | } 448 | if (isset($exception["options"]["strtotime"]) && $exception["options"]["strtotime"] === true) { 449 | $route_i = strtotime($route_i); 450 | } 451 | if (isset($exception["options"]["strtoint"]) && $exception["options"]["strtoint"] === true) { 452 | $route_i = intval($route_i); 453 | } 454 | if (isset($exception["options"]["inttobinary"]) && $exception["options"]["inttobinary"] === true) { 455 | $route_i = decbin($route_i); 456 | } 457 | if (isset($exception["options"]["addition"])) { 458 | $route_i = $route_i . $exception["options"]["addition"]; 459 | } 460 | if (isset($exception["options"]["additionBevor"])) { 461 | $route_i = $exception["options"]["additionBefore"] . $route_i; 462 | } 463 | if (isset($exception["options"]["replace"])) { 464 | if (isset($exception["options"]["replace"]["ireplace"]) && $exception["options"]["replace"]["ireplace"] === true) { 465 | $route_i = str_ireplace($exception["options"]["replace"]["search"], $exception["options"]["replace"]["replace"], $route_i); 466 | } else { 467 | $route_i = str_replace($exception["options"]["replace"]["search"], $exception["options"]["replace"]["replace"], $route_i); 468 | } 469 | } 470 | if (isset($exception["options"]["pregreplace"])) { 471 | if (isset($exception["options"]["pregreplace"]["pattern"]) || isset($exception["options"]["pregreplace"]["replace"])) { 472 | if (!isset($exception["options"]["pregreplace"]["pattern"])) { 473 | $exception["options"]["pregreplace"]["pattern"] = "/.*/"; 474 | } 475 | if (!isset($exception["options"]["pregreplace"]["replace"])) { 476 | $exception["options"]["pregreplace"]["replace"] = ""; 477 | } 478 | $route_i = preg_replace($exception["options"]["pregreplace"]["pattern"], $exception["options"]["pregreplace"]["replace"], $route_i); 479 | } 480 | } 481 | } 482 | 483 | /** 484 | * Load params. 485 | */ 486 | if (isset($params[$i + $k]) && $params[$i + $k] == $exception["param"]) { 487 | foreach ($exception["exceptions"] as $excep) { 488 | if ($excep == $route_i) { 489 | $l++; 490 | } 491 | } 492 | 493 | if (isset($exception["revertOptions"]) && !is_null($exception["revertOptions"])) { 494 | if ($exception["revertOptions"] === true) { 495 | $route_i = $routeParamOriginal; 496 | } 497 | } 498 | 499 | if ($l > 0) { 500 | $__GET[$params[$i + $k]] = $route_i; 501 | $l = 0; 502 | goto paramEnd; 503 | } else { 504 | $k++; 505 | $paramsCount--; 506 | } 507 | } 508 | } 509 | } 510 | 511 | /** 512 | * If there are more values ​​as parameters or empty parameters, natural numbers must be followed. 513 | */ 514 | if (!isset($params[$i + $k])) { 515 | $params[$i + $k] = $i; 516 | } 517 | 518 | /** 519 | * The parameter should be ignored, if the value of it is null. 520 | */ 521 | if (is_null($route[$i]) || empty($route[$i]) || !isset($route[$i])) { 522 | # 523 | } else { 524 | $__GET[$params[$i + $k]] = $route[$i]; 525 | } 526 | 527 | paramEnd: 528 | 529 | if (isset($trueGetParams) && count($trueGetParams) > 0) { 530 | foreach ($trueGetParams as $key => $value) { 531 | $__GET[$key] = $value; 532 | } 533 | } 534 | } 535 | } 536 | 537 | self::$paramsCount = $paramsCount; 538 | self::$routed = true; 539 | self::$allParams = isset($__GET) ? $__GET : false; 540 | self::$getCount = isset($__GET) ? count($__GET) : 0; 541 | 542 | if ($load_GET === true) { 543 | if (isset($__GET)) { 544 | $GLOBALS['_GET'] = $__GET; 545 | 546 | return; 547 | } 548 | } else { 549 | /** 550 | * Return array. 551 | */ 552 | if (isset($__GET)) { 553 | return $__GET; 554 | } 555 | } 556 | 557 | } 558 | 559 | /** 560 | * @param string $basedir 561 | * @param bool $load_GET 562 | * 563 | * @access public 564 | * 565 | * @uses $basedir 566 | * @uses $prepareString 567 | * @uses $prepareCount 568 | * @uses $paramsCount 569 | * @uses $routed 570 | * @uses $allParams 571 | * 572 | * @return array Returns an array with the uri-params on success or FALSE on failure 573 | */ 574 | public static function execute ($basedir = null, $load_GET = true) 575 | { 576 | 577 | /** 578 | * Check params. 579 | */ 580 | if (is_null($basedir) && self::$basedir == true) { 581 | $basedir = self::$basedir; 582 | } 583 | 584 | if (self::$prepareString === false) { 585 | return false; 586 | } 587 | 588 | /** 589 | * Current path. 590 | */ 591 | if (is_null($basedir)) { 592 | if (!isset($_SERVER['HTTPS'])) { 593 | $uri = "http://" . $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"]; 594 | } else { 595 | $uri = "https://" . $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"]; 596 | } 597 | } else { 598 | if (substr($basedir, 0, 7) == "http://") { 599 | $uri = "http://" . $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"]; 600 | } elseif (substr($basedir, 0, 8) == "https://") { 601 | $uri = "https://" . $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"]; 602 | } else { 603 | return false; 604 | } 605 | } 606 | 607 | /** 608 | * Generate basedir. 609 | */ 610 | if (is_null($basedir)) { 611 | if (!isset($_SERVER['HTTPS'])) { 612 | $scheme = 'http://'; 613 | } else { 614 | $scheme = 'https://'; 615 | } 616 | $basedir = $scheme . $_SERVER['SERVER_NAME'] . $_SERVER['SCRIPT_NAME']; 617 | $basedir = str_replace('/' . basename($scheme . $_SERVER['SERVER_NAME'] . $_SERVER['SCRIPT_NAME']), '', $basedir); 618 | } 619 | 620 | /** 621 | * Delete last slash from the root directory. 622 | */ 623 | if (substr($basedir, -1, 1) == "/") { 624 | $basedir = substr($basedir, 0, strlen($basedir) - 1); 625 | } 626 | 627 | /** 628 | * Is there a slash at the end of the path? (only if there are no parameters!) 629 | */ 630 | $slash = ""; 631 | if ($uri == $basedir) { 632 | $slash = "/"; 633 | } elseif ($uri == $basedir . "/") { 634 | $slash = ""; 635 | } elseif (substr($uri, -1, 1) == "/") { 636 | $uri = substr($uri, 0, strlen($uri) - 1); 637 | } 638 | 639 | /** 640 | * Filter out the index file and the base directory. 641 | */ 642 | $uri = str_replace($basedir . "/", "", $uri . $slash); 643 | 644 | $paramsCount = 0; 645 | 646 | /** 647 | * Parse prepare string and load the individual parameters in the $_GET array. 648 | */ 649 | $prepareString = self::$prepareString; 650 | $parts = explode('/', $prepareString); 651 | $partsCount = count($parts); 652 | $prepareParams = array(); 653 | $uriParams = array(); 654 | $uriParts = explode('/', $uri); 655 | 656 | for ($i = 0; $partsCount > $i; $i++) { 657 | if (strpos($parts[$i], '-') == true) { 658 | $prepareParams = explode('-', $parts[$i]); 659 | $uriParams = explode('-', $uriParts[$i], count($prepareParams)); 660 | 661 | for ($j = 0; count($prepareParams) > $j; $j++) { 662 | if (isset($uriParams[$j]) && !is_null($uriParams[$j]) && !empty($uriParams[$j])) { 663 | $__GET[substr($prepareParams[$j], 1, strlen($prepareParams[$j]) - 2)] = $uriParams[$j]; 664 | $latestKey = substr($prepareParams[$j], 1, strlen($prepareParams[$j]) - 2); 665 | } 666 | } 667 | 668 | if (isset($parts[$i]) && isset($uriParts[$i])) { 669 | $prepareParams = $parts[$i]; 670 | $uriParams = $uriParts[$i]; 671 | } 672 | 673 | if (isset($uriParams) && !is_null($uriParams) && !empty($uriParams)) { 674 | $paramsCount++; 675 | } 676 | } else { 677 | if (isset($parts[$i]) && isset($uriParts[$i])) { 678 | $prepareParams = $parts[$i]; 679 | $uriParams = $uriParts[$i]; 680 | } 681 | 682 | if (isset($uriParams) && !is_null($uriParams) && !empty($uriParams)) { 683 | $__GET[substr($prepareParams, 1, strlen($prepareParams) - 2)] = $uriParams; 684 | $latestKey = substr($prepareParams, 1, strlen($prepareParams) - 2); 685 | $paramsCount++; 686 | } 687 | } 688 | } 689 | 690 | if (count($uriParts) - 1 >= $i) { 691 | $remains = count($uriParts) - 1 - $i; 692 | for ($j = 0; $remains >= $j; $j++) { 693 | $__GET[$j + $i + 1] = $uriParts[$j + $i]; 694 | } 695 | } 696 | 697 | if (isset($__GET) && isset($latestKey) && strpos($__GET[$latestKey], '?') == true) { # a question mark must be present in the parameter 698 | if (substr($__GET[$latestKey], -1, 1) == '?') { # is the last character a question mark? 699 | $__GET[$latestKey] = substr($__GET[$latestKey], 0, strlen($__GET[$latestKey]) - 1); 700 | $glue = "?"; 701 | $glueGetParam = false; 702 | } else { 703 | $glue = ""; 704 | $glueGetParam = false; 705 | } 706 | if (strpos($__GET[$latestKey], '?') == true) { 707 | $glueGetParam = true; 708 | $elements = explode('?', $__GET[$latestKey], 2); 709 | $__GET[$latestKey] = $elements[0]; 710 | $elements[1] .= $glue; 711 | $getParams = explode('&', $elements[1]); 712 | $trueGetParams = array(); 713 | foreach ($getParams as $getParam) { 714 | if (strpos($getParam, '=') == true) { 715 | $paramArray = explode('=', $getParam, 2); 716 | $trueGetParams[$paramArray[0]] = $paramArray[1]; 717 | } 718 | } 719 | } 720 | $__GET[$latestKey] .= $glueGetParam == false ? $glue : ''; 721 | } 722 | 723 | if (isset($trueGetParams) && count($trueGetParams) > 0) { 724 | foreach ($trueGetParams as $key => $value) { 725 | $__GET[$key] = $value; 726 | } 727 | } 728 | 729 | self::$paramsCount = $paramsCount; 730 | self::$routed = true; 731 | self::$allParams = isset($__GET) ? $__GET : false; 732 | self::$getCount = isset($__GET) ? count($__GET) : 0; 733 | 734 | if ($load_GET === true) { 735 | if (isset($__GET)) { 736 | $GLOBALS['_GET'] = $__GET; 737 | 738 | return; 739 | } 740 | } else { 741 | /** 742 | * Return array. 743 | */ 744 | if (isset($__GET)) { 745 | return $__GET; 746 | } 747 | } 748 | 749 | } 750 | 751 | /** 752 | * @param string $prepareString 753 | * 754 | * @access public 755 | * 756 | * @uses $prepareString 757 | * 758 | * @return bool Returns FALSE on failure 759 | */ 760 | public static function prepare ($prepareString) 761 | { 762 | 763 | if (!isset($prepareString)) { 764 | return false; 765 | } 766 | 767 | if (substr($prepareString, -1, 1) == '/') { 768 | $prepareString = substr($prepareString, 0, strlen($prepareString)-1); 769 | } 770 | 771 | if (substr($prepareString, 0, 1) == '/') { 772 | $prepareString = substr($prepareString, 1, strlen($prepareString)-1); 773 | } 774 | 775 | $prepareString = str_replace('}-{', '}#{', $prepareString); 776 | $prepareString = str_replace(array(' ', '-'), array('_', '_'), $prepareString); 777 | $prepareString = str_replace('}#{', '}-{', $prepareString); 778 | self::$prepareString = $prepareString; 779 | 780 | } 781 | 782 | /** 783 | * @access public 784 | * 785 | * @return array Returns an array with normal GET params 786 | */ 787 | public static function get_true_params () 788 | { 789 | 790 | $trueGetParams = array(); 791 | 792 | if (isset($_SERVER['QUERY_STRING'])) { 793 | if (!empty($_SERVER['QUERY_STRING'])) { 794 | $params = explode('&', $_SERVER['QUERY_STRING']); 795 | foreach ($params as $param) { 796 | $paramArray = explode('=', $param, 2); 797 | $trueGetParams[$paramArray[0]] = $paramArray[1]; 798 | } 799 | } else { 800 | return false; 801 | } 802 | } else { 803 | return false; 804 | } 805 | 806 | return $trueGetParams; 807 | 808 | } 809 | 810 | /** 811 | * @param string $href 812 | * @param int $params 813 | * @param bool $staticRedirect 814 | * 815 | * @access public 816 | * 817 | * @uses $routed 818 | * @uses $paramsCount 819 | * @uses $prepareCount 820 | * @uses get_true_params() 821 | * 822 | * @return bool Returns FALSE on failure 823 | */ 824 | public static function error ($href, $params = null, $staticRedirect = false) 825 | { 826 | 827 | if ($staticRedirect === false) { 828 | if (isset(self::$routed) && self::$routed === true) { 829 | if (!isset($params) || is_null($params)) { 830 | if (!self::get_true_params()) { 831 | $trueGetParams = 0; 832 | } else { 833 | $trueGetParams = count(self::get_true_params()); 834 | } 835 | 836 | $realCount = self::$getCount - $trueGetParams; 837 | 838 | if (self::$paramsCount < $realCount && self::$paramsCount > 0) { 839 | header('Location: ' . $href); 840 | exit(); 841 | } 842 | } else { 843 | if (!self::get_true_params()) { 844 | $trueGetParams = 0; 845 | } else { 846 | $trueGetParams = count(self::get_true_params()); 847 | } 848 | 849 | $realCount = self::$getCount - $trueGetParams; 850 | 851 | if ($params < $realCount) { 852 | header('Location: ' . $href); 853 | exit(); 854 | } 855 | } 856 | } 857 | } else { 858 | header('Location: ' . $href); 859 | exit(); 860 | ob_end_flush(); 861 | } 862 | 863 | return false; 864 | 865 | } 866 | 867 | /** 868 | * @param bool $relative 869 | * 870 | * @access public 871 | * 872 | * @uses $allParams 873 | * @uses start() 874 | * @uses get_true_params() 875 | * 876 | * @return string Returns the active path (url) on success or FALSE on failure 877 | */ 878 | public static function get_active_path ($relative = false) 879 | { 880 | 881 | if ($relative === false) { 882 | if (self::$allParams === false) { 883 | $allParams = self::start(null, null, null, false); 884 | } else { 885 | $allParams = explode('/', implode('/', self::$allParams)); 886 | } 887 | 888 | $trueGetParams = self::get_true_params(); 889 | 890 | if ($trueGetParams === false) { 891 | $paramCount = count($allParams) - 0; 892 | } else { 893 | $paramCount = count($allParams) - count($trueGetParams); 894 | } 895 | 896 | if ($paramCount > 0) { 897 | if (!isset($_SERVER['HTTPS'])) { 898 | $uri = "http://" . $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"]; 899 | } else { 900 | $uri = "https://" . $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"]; 901 | } 902 | 903 | if (empty($allParams[0]) === true) { 904 | return $uri; 905 | } 906 | 907 | $uriParts = explode($allParams[0], $uri); 908 | $uri = $uriParts[0]; 909 | 910 | return $uri; // Return the absolute path -> http://www.example.com/directory/to/active/file/ 911 | } else { 912 | return false; 913 | } 914 | } else { 915 | if (self::$allParams === false) { 916 | $allParams = self::start(null, null, null, false); 917 | } else { 918 | $allParams = explode('/', implode('/', self::$allParams)); 919 | } 920 | 921 | $trueGetParams = self::get_true_params(); 922 | 923 | if ($trueGetParams == FALSE) { 924 | $paramCount = count($allParams) - 0; 925 | } else { 926 | $paramCount = count($allParams) - count($trueGetParams); 927 | } 928 | 929 | $uri = ""; 930 | 931 | if (empty($allParams[0]) === false) { 932 | for ($i = 0; $i < $paramCount; $i++) { 933 | $uri .= "../"; 934 | } 935 | } 936 | 937 | if (substr($_SERVER["REQUEST_URI"], -1, 1) != '/') { 938 | if (strlen($uri) >= 3) { 939 | $uri = substr($uri, 0, strlen($uri)-3); 940 | } 941 | } 942 | 943 | if (strlen($uri) == 0) { 944 | $uri = './'; 945 | } 946 | 947 | return $uri; // Return the relative path -> ../../../../ 948 | } 949 | 950 | } 951 | 952 | /** 953 | * @param string $url 954 | * @param bool $relative 955 | * @param bool $print 956 | * 957 | * @access public 958 | * 959 | * @uses get_active_path() 960 | * 961 | * @return string Returns the url 962 | */ 963 | public static function url ($url, $relative = true, $print = true) 964 | { 965 | 966 | if (substr($url, 0, 1) == '/') { 967 | $url = substr($url, 1, strlen($url)-1); 968 | } 969 | 970 | $url = self::get_active_path($relative) . $url; 971 | 972 | if ($print === true) { 973 | echo $url; 974 | 975 | return; 976 | } else { 977 | return $url; 978 | } 979 | 980 | } 981 | 982 | } 983 | ?> 984 | -------------------------------------------------------------------------------- /src/nginx conf: -------------------------------------------------------------------------------- 1 | # nginx configuration 2 | location / { 3 | rewrite (.*)\ /index.php break; 4 | if (-e $request_filename){ 5 | rewrite ^(.*)$ /index.php break; 6 | } 7 | } 8 | --------------------------------------------------------------------------------