forked from emoncms/emoncms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroute.php
More file actions
124 lines (101 loc) · 3.61 KB
/
route.php
File metadata and controls
124 lines (101 loc) · 3.61 KB
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
<?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 $method = 'GET';
/**
* @var string
*/
public $format = 'html';
/**
* @param string $q
* @param string $documentRoot
* @param string $requestMethod
*/
public function __construct($q, $documentRoot, $requestMethod)
{
$this->decode($q, $documentRoot, $requestMethod);
}
/**
* @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 a-z 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 (in_array($requestMethod, ['POST', 'DELETE', 'PUT'])) {
$this->method = $requestMethod;
}
}
/**
* @return bool
*/
public function isRouteNotDefined()
{
return empty($this->controller) && empty($this->action);
}
}