-
Notifications
You must be signed in to change notification settings - Fork 2
/
Router.php
139 lines (110 loc) · 3.95 KB
/
Router.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
<?php
class Wave_Router {
private static $root;
public $request_method;
public $request_uri;
public $response_method;
public function __construct($host){
self::$root = self::loadRoutesCache($host);
}
public static function init($host = null){
if($host === null){
$host = $_SERVER['HTTP_HOST'];
}
return new self($host);
}
public function route($url = null, $method = null){
if($url === null){
if(isset($_SERVER['PATH_INFO']))
$this->request_uri = substr($_SERVER['PATH_INFO'], strpos($_SERVER['PATH_INFO'], '.php/'));
else
$this->request_uri = $_SERVER['REQUEST_URI'];
}
else
$this->request_uri = $url;
// trim off any query string parameters etc
$qs = strpos($this->request_uri, '?');
if($qs !== false){
$this->request_uri = substr($this->request_uri, 0, $qs);
}
// remove the trailing slash and replace with the default response method if required
if(substr($this->request_uri, -1, 1) == '/'){
$trimmed = substr($this->request_uri, 0, -1);
$this->request_uri = $trimmed . '.' . Wave_Config::get('wave')->controller->default_response;
}
// deduce the response method
$path = pathinfo($this->request_uri);
if(isset($path['extension']) && in_array($path['extension'], Wave_Response::$ALL)){
$this->response_method = $path['extension'];
// remove the response method from the url, we dont need it here
$this->request_uri = substr($this->request_uri, 0, -(strlen($this->response_method)+1));
}
else $this->response_method = Wave_Config::get('wave')->controller->default_response;
// put the response method onto the controller class in case we need it for exceptions
Wave_Controller::_setResponseMethod($this->response_method);
if($method === null)
$this->request_method = $_SERVER['REQUEST_METHOD'];
else
$this->request_method = $method;
$this->findRoute($this->request_method.$this->request_uri);
}
public function findRoute($url){
$parts = explode('/', $url);
$parts_count = count($parts);
$pos = 0;
$node = self::$root;
$var_stack = array();
while($pos < $parts_count){
$child = $node->getChild($parts[$pos]);
if($child === null)
break;
if($child->isVarNode()){
$var_stack[$child->getVarName()] = $parts[$pos];
}
$node = $child;
$pos++;
}
if($pos == $parts_count && $node !== null && $node->isLeaf()){
$destination = $node->getDestination();
$auth_obj = Wave_Auth::getIdentity();
if(!in_array($this->response_method, $destination['respondswith'])){
throw new Wave_Exception(
'The requested action '.$destination['action'].
' can not respond with '.$this->response_method.
'. (Accepts: '.implode(', ', $destination['respondswith']).')');
}
else if($destination['requireslevel'] !== null && Wave_Auth::$_is_loaded){
if(!($auth_obj instanceof Wave_IAuthable) || !$auth_obj->hasAccess($destination['requireslevel'], $var_stack)){
$auth_class = Wave_Auth::getHandlerClass();
if(!$auth_class::noAuthAction(array(
'destination' => $destination,
'auth_obj' => $auth_obj,
'args' => $var_stack
)))
throw new Wave_Exception(
'The current user does not have the required level to access this page', 403);
}
}
Wave_Controller::invoke($destination['action'], $var_stack, $this);
}
else {
throw new Wave_Exception('The requested URL '.$url.' does not exist', 404);
}
}
public static function loadRoutesCache($host){
$cache_name = self::getCacheName($host);
$routes = Wave_Cache::load($cache_name);
if($routes == null){
$defaultdomain = Wave_Config::get('deploy')->baseurl;
$routes = Wave_Cache::load(self::getCacheName($defaultdomain));
}
if($routes == null)
throw new Wave_Exception('Could not load routes for domain: '.$host.' nor default domain: '.$defaultdomain);
else
return $routes;
}
public static function getCacheName($host){
return 'routes/'.md5($host);
}
}
?>