1
|
|
|
<? |
|
|
|
|
2
|
|
|
|
3
|
|
|
Loader::load('utility', array( |
4
|
|
|
'Request', |
5
|
|
|
'URLDecode')); |
6
|
|
|
|
7
|
|
|
abstract class Router |
8
|
|
|
{ |
9
|
|
|
|
10
|
|
|
public function __construct() {} |
11
|
|
|
|
12
|
|
|
public static function instance() |
13
|
|
|
{ |
14
|
|
|
$router_name = self::get_router_name(); |
15
|
|
|
$router = Loader::loadNew('router', $router_name); |
16
|
|
|
$router->route(); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
private static function get_router_name() |
20
|
|
|
{ |
21
|
|
|
if(Request::isAJAX()) |
22
|
|
|
return 'AJAXRouter'; |
23
|
|
|
|
24
|
|
|
switch(URLDecode::getSite()) |
25
|
|
|
{ |
26
|
|
|
case 'ajax' : |
27
|
|
|
return 'AjaxRouter'; |
28
|
|
|
break; |
|
|
|
|
29
|
|
|
case 'blog' : |
30
|
|
|
return 'BlogRouter'; |
31
|
|
|
break; |
|
|
|
|
32
|
|
|
case 'home' : |
33
|
|
|
return 'HomeRouter'; |
34
|
|
|
break; |
|
|
|
|
35
|
|
|
case 'lifestream' : |
36
|
|
|
return 'LifestreamRouter'; |
37
|
|
|
break; |
|
|
|
|
38
|
|
|
case 'portfolio' : |
39
|
|
|
return 'PortfolioRouter'; |
40
|
|
|
break; |
|
|
|
|
41
|
|
|
case 'site' : |
42
|
|
|
return 'SiteRouter'; |
43
|
|
|
break; |
|
|
|
|
44
|
|
|
case 'waterfalls' : |
45
|
|
|
return 'WaterfallRouter'; |
46
|
|
|
break; |
|
|
|
|
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
Loader::loadNew('controller', '/Error404Controller')->activate(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
protected function route() |
53
|
|
|
{ |
54
|
|
|
$uri = URLDecode::getURI(); |
55
|
|
|
|
56
|
|
|
$this->check_for_redirect($uri); |
57
|
|
|
|
58
|
|
|
$controller = $this->get_controller($uri); |
59
|
|
|
Loader::loadNew('controller', $controller) |
60
|
|
|
->activate(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
abstract protected function get_redirect_array(); |
64
|
|
|
abstract protected function get_direct_array(); |
65
|
|
|
|
66
|
|
|
final protected function check_for_redirect($redirect_uri) |
67
|
|
|
{ |
68
|
|
|
foreach($this->get_redirect_array() as $check) |
69
|
|
|
{ |
70
|
|
|
$redirect_uri = preg_replace($check->pattern, $check->replace, $redirect_uri); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$redirect_uri = $this->check_for_special_redirect($redirect_uri); |
74
|
|
|
|
75
|
|
|
if($this->requires_trailing_slash() && substr($redirect_uri, -1) != '/') |
76
|
|
|
$redirect_uri .= '/'; |
77
|
|
|
|
78
|
|
|
if (URLDecode::getHost() == 'waterfalls.jacobemerick.com') { |
79
|
|
|
$redirect_uri = 'http://' . (!Loader::isLive() ? 'dev' : 'www') . '.waterfallsofthekeweenaw.com' . $redirect_uri; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
if($redirect_uri == URLDecode::getURI()) |
83
|
|
|
return; |
84
|
|
|
|
85
|
|
|
$controller_check = $redirect_uri; |
86
|
|
|
if(substr($redirect_uri, 0, 4) == 'http') |
87
|
|
|
$controller_check = preg_replace('@^http://([a-z\.]+)@', '', $redirect_uri); |
88
|
|
|
|
89
|
|
|
$controller = $this->get_controller($controller_check); |
90
|
|
|
if($controller == '/Error404Controller') |
91
|
|
|
{ |
92
|
|
|
Loader::loadNew('controller', '/Error404Controller') |
93
|
|
|
->activate(); |
94
|
|
|
exit; |
|
|
|
|
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
if(substr($redirect_uri, 0, 4) != 'http') |
98
|
|
|
{ |
99
|
|
|
$redirect_uri = substr($redirect_uri, 1); |
100
|
|
|
$redirect_uri = URLDecode::getBase() . $redirect_uri; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
Loader::loadNew('controller', '/Error301Controller', (array) $redirect_uri) |
104
|
|
|
->activate(); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
protected function check_for_special_redirect($uri) |
108
|
|
|
{ |
109
|
|
|
return $uri; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
final private function get_controller($uri) |
113
|
|
|
{ |
114
|
|
|
foreach($this->get_direct_array() as $check) |
115
|
|
|
{ |
116
|
|
|
if($uri == $check->match) |
117
|
|
|
return "{$this->get_primary_folder()}/{$check->controller}"; |
118
|
|
|
|
119
|
|
|
if(preg_match("@^{$check->match}$@", $uri)) |
120
|
|
|
return "{$this->get_primary_folder()}/{$check->controller}"; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return '/Error404Controller'; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
final private function get_primary_folder() |
127
|
|
|
{ |
128
|
|
|
if(Request::isAjax()) |
129
|
|
|
return 'ajax'; |
130
|
|
|
|
131
|
|
|
return URLDecode::getSite(); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
private function requires_trailing_slash() |
135
|
|
|
{ |
136
|
|
|
return ( |
137
|
|
|
URLDecode::getExtension() != 'json' && |
138
|
|
|
strstr(URLDecode::getURI(), '#') === false); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
} |
142
|
|
|
|
Short opening tags are disabled in PHP’s default configuration. In such a case, all content of this file is output verbatim to the browser without being parsed, or executed.
As a precaution to avoid these problems better use the long opening tag
<?php
.