forked from forkcms/forkcms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
routing.php
117 lines (99 loc) · 2.64 KB
/
routing.php
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
<?php
/*
* This file is part of Fork CMS.
*
* For the full copyright and license information, please view the license
* file that was distributed with this source code.
*/
/**
* Application routing
*
* @author Tijs Verkoyen <[email protected]>
* @author Davy Hellemans <[email protected]>
* @author Dieter Vanden Eynde <[email protected]>
*/
class ApplicationRouting
{
const DEFAULT_APPLICATION = 'frontend';
/**
* Virtual folders mappings
*
* @var array
*/
private static $routes = array(
'' => self::DEFAULT_APPLICATION,
'private' => 'backend',
'backend' => 'backend',
'api' => 'api'
);
public function __construct()
{
// spoof querystring on lighttpd
$this->spoofQueryString();
// process querystring
$this->processQueryString();
// require correct app
require_once APPLICATION . '/index.php';
}
/**
* Get the possible routes
*
* @return array
*/
public static function getRoutes()
{
return self::$routes;
}
/**
* Process the querystring to define the application
*/
private function processQueryString()
{
// get querystring
$queryString = trim($_SERVER['REQUEST_URI'], '/');
// split into chunks
$chunks = explode('/', $queryString);
// is there a application specified
if(isset($chunks[0]))
{
// cleanup
$proposedApplication = (string) $chunks[0];
// set real application
$application = (isset(self::$routes[$proposedApplication])) ? self::$routes[$proposedApplication] : self::DEFAULT_APPLICATION;
}
// no application
else $application = self::DEFAULT_APPLICATION;
// define APP
if(!defined('APPLICATION')) define('APPLICATION', $application);
if(!defined('NAMED_APPLICATION')) define('NAMED_APPLICATION', $proposedApplication);
}
/**
* Spoof QUERY_STRING when on a lighttp webserver
*
* Lighttp does not fill up the QUERY_STRING var when using rewrites or the error handler.
* This function fakes the QUERY_STRING.
*
* PHP uses QUERY_STRING to fill up the GET array. Without this fix GET would be empty
*/
public static function spoofQueryString()
{
// its a lighttp server
if(strpos($_SERVER['SERVER_SOFTWARE'], 'lighttpd') !== false)
{
// build current url (we need a valid url to use parse_url)
$url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
// get querystring
$queryString = @parse_url($url, PHP_URL_QUERY);
// successfuly parsed
if($queryString !== false)
{
// spoof the query string
$_SERVER['QUERY_STRING'] = (string) $queryString;
// parse querystring to array
parse_str($queryString, $get);
// spoof get
foreach($get as $key => $val) $_GET[$key] = $val;
}
}
}
}