forked from emoncms/emoncms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
route.php
150 lines (125 loc) · 4.57 KB
/
route.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
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
<?php
/*
All Emoncms code is released under the GNU Affero General Public License.
See COPYRIGHT.txt and LICENSE.txt.
---------------------------------------------------------------------
Emoncms - open source energy visualisation
Part of the OpenEnergyMonitor project:
http://openenergymonitor.org
*/
// no direct access
defined('EMONCMS_EXEC') or die('Restricted access');
class Route
{
/**
* @var string
*/
public $controller = '';
/**
* @var string
*/
public $action = '';
/**
* @var string
*/
public $subaction = '';
/**
* @var string
*/
public $subaction2 = '';
/**
* @var string
*/
public $method = 'GET';
/**
* @var string
*/
public $format = 'html';
/**
* @var bool
*/
public $is_ajax = false;
/**
* @param string $q
* @param string $documentRoot
* @param string $requestMethod
*/
public function __construct($q, $documentRoot, $requestMethod)
{
$this->decode($q, $documentRoot, $requestMethod);
//this can be faked by the client. not to be trusted.
$this->is_ajax = !empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest';
}
/**
* @param string $q
* @param string $documentRoot
* @param string $requestMethod
*/
public function decode($q, $documentRoot, $requestMethod)
{
// filter out the applications relative root
// If we're running in a subdirectory "emoncms", $q would look like '/emoncms/user/view' instead or just 'user/view'
// for the example of viewing a users profile. We need to remove the first directory to get the "clean" routing path
// within the application no matter at which path it's hosted.
// First get the absolute physical path
// Example running at root: '/var/www' or subdirectory: '/var/www/emoncms'
$absolutePath = realpath(dirname(__FILE__));
// Next up, we need to find the relative path to the www root and remove everything except the part we will use to route
// for example this will perform the following:
// Running at root: str_replace('/var/www', '', '/var/www') => ''
// Running at subdirectory: str_replace('/var/www', '', '/var/www/emoncms') => '/emoncms'
$relativeApplicationPath = str_replace($documentRoot, '', $absolutePath);
// Next up we will need to remove the '/emoncms' from the route path '/emoncms/user/view'
// str_replace('/emoncms', '', '/emoncms/user/view') => '/user/view'
// running at root path it will just perform nothing: str_replace('', '', '/emoncms/user/view') so it can be skipped
if (!empty($relativeApplicationPath)) {
$q = str_replace($relativeApplicationPath, '', $q);
}
// trim slashes: '/user/view' => 'user/view'
$q = trim($q, '/');
// filter out all except alphanumerics and / . _ -
$q = preg_replace('/[^.\/_A-Za-z0-9-]/', '', $q);
// Split by /
$args = preg_split('/[\/]/', $q);
// get format (part of last argument after . i.e view.json)
$lastArgIndex = sizeof($args) - 1;
$lastArgSplit = preg_split('/[.]/', $args[$lastArgIndex]);
if (count($lastArgSplit) > 1) {
$this->format = $lastArgSplit[1];
}
$args[$lastArgIndex] = $lastArgSplit[0];
if (count($args) > 0) {
$this->controller = $args[0];
}
if (count($args) > 1) {
$this->action = $args[1];
}
if (count($args) > 2) {
$this->subaction = $args[2];
}
if (count($args) > 3) {
$this->subaction2 = $args[3];
}
// allow for method to be added as post variable
if(post('_method')=='DELETE') {
$this->method = 'DELETE';
} elseif(post('_method')=='PUT') {
$this->method = 'PUT';
} elseif(in_array($requestMethod, array('POST', 'DELETE', 'PUT'))) {
$this->method = $requestMethod;
} elseif($requestMethod === 'OPTIONS') {
// "CORS PREFLIGHT REQUESTS" EXPECT THESE HEADERS. no content required
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: Authorization');
header('Access-Control-Allow-Methods: GET');
exit();
}
}
/**
* @return bool
*/
public function isRouteNotDefined()
{
return empty($this->controller) && empty($this->action);
}
}