1
|
|
|
<? |
|
|
|
|
2
|
|
|
|
3
|
|
|
Loader::load('utility', array( |
4
|
|
|
'ImageOld', |
5
|
|
|
'Request', |
6
|
|
|
'URLDecode')); |
7
|
|
|
|
8
|
|
|
abstract class Router |
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
public function __construct() {} |
12
|
|
|
|
13
|
|
|
public static function instance() |
14
|
|
|
{ |
15
|
|
|
$router_name = self::get_router_name(); |
16
|
|
|
$router = Loader::loadNew('router', $router_name); |
17
|
|
|
$router->route(); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
private static function get_router_name() |
21
|
|
|
{ |
22
|
|
|
if(Request::isAJAX()) |
23
|
|
|
return 'AJAXRouter'; |
24
|
|
|
if(URLDecode::getURI() == '/sitemap.xml') |
25
|
|
|
return 'SitemapRouter'; |
26
|
|
|
if(URLDecode::getURI() == '/rss/') |
27
|
|
|
return 'RSSRouter'; |
28
|
|
|
if(URLDecode::getExtension() == 'jpg') |
29
|
|
|
return 'ImageRouter'; |
30
|
|
|
if(URLDecode::getExtension() == 'png') |
31
|
|
|
return 'ImageRouter'; |
32
|
|
|
|
33
|
|
|
switch(URLDecode::getSite()) |
34
|
|
|
{ |
35
|
|
|
case 'ajax' : |
36
|
|
|
return 'AjaxRouter'; |
37
|
|
|
break; |
|
|
|
|
38
|
|
|
case 'blog' : |
39
|
|
|
return 'BlogRouter'; |
40
|
|
|
break; |
|
|
|
|
41
|
|
|
case 'home' : |
42
|
|
|
return 'HomeRouter'; |
43
|
|
|
break; |
|
|
|
|
44
|
|
|
case 'images' : |
45
|
|
|
return 'ImageRouter'; |
46
|
|
|
break; |
|
|
|
|
47
|
|
|
case 'lifestream' : |
48
|
|
|
return 'LifestreamRouter'; |
49
|
|
|
break; |
|
|
|
|
50
|
|
|
case 'portfolio' : |
51
|
|
|
return 'PortfolioRouter'; |
52
|
|
|
break; |
|
|
|
|
53
|
|
|
case 'site' : |
54
|
|
|
return 'SiteRouter'; |
55
|
|
|
break; |
|
|
|
|
56
|
|
|
case 'waterfalls' : |
57
|
|
|
return 'WaterfallRouter'; |
58
|
|
|
break; |
|
|
|
|
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
Debugger::logMessage("The router for " . URLDecode::getSite() . " was not loaded."); |
62
|
|
|
Loader::loadNew('controller', '/Error404Controller')->activate(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
protected function route() |
66
|
|
|
{ |
67
|
|
|
$uri = URLDecode::getURI(); |
68
|
|
|
|
69
|
|
|
$this->check_for_redirect($uri); |
70
|
|
|
|
71
|
|
|
$controller = $this->get_controller($uri); |
72
|
|
|
Loader::loadNew('controller', $controller) |
73
|
|
|
->activate(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
abstract protected function get_redirect_array(); |
77
|
|
|
abstract protected function get_direct_array(); |
78
|
|
|
|
79
|
|
|
final protected function check_for_redirect($redirect_uri) |
80
|
|
|
{ |
81
|
|
|
foreach($this->get_redirect_array() as $check) |
82
|
|
|
{ |
83
|
|
|
$redirect_uri = preg_replace($check->pattern, $check->replace, $redirect_uri); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$redirect_uri = $this->check_for_special_redirect($redirect_uri); |
87
|
|
|
|
88
|
|
|
if($this->requires_trailing_slash() && substr($redirect_uri, -1) != '/') |
89
|
|
|
$redirect_uri .= '/'; |
90
|
|
|
|
91
|
|
|
if (URLDecode::getHost() == 'waterfalls.jacobemerick.com') { |
92
|
|
|
$redirect_uri = 'http://' . (!Loader::isLive() ? 'dev' : 'www') . '.waterfallsofthekeweenaw.com' . $redirect_uri; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
if($redirect_uri == URLDecode::getURI()) |
96
|
|
|
return; |
97
|
|
|
|
98
|
|
|
$controller_check = $redirect_uri; |
99
|
|
|
if(substr($redirect_uri, 0, 4) == 'http') |
100
|
|
|
$controller_check = preg_replace('@^http://([a-z\.]+)@', '', $redirect_uri); |
101
|
|
|
|
102
|
|
|
$controller = $this->get_controller($controller_check); |
103
|
|
|
if($controller == '/Error404Controller') |
104
|
|
|
{ |
105
|
|
|
Loader::loadNew('controller', '/Error404Controller') |
106
|
|
|
->activate(); |
107
|
|
|
exit; |
|
|
|
|
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
if($this->get_primary_folder() == 'images') { |
111
|
|
|
$file = $controller_check;//URLDecode::getURI(); |
112
|
|
|
|
113
|
|
View Code Duplication |
if ( |
|
|
|
|
114
|
|
|
URLDecode::getSite() == 'images' || |
115
|
|
|
URLDecode::getExtension() == 'ico') { |
116
|
|
|
$file = "/css/{$file}"; |
117
|
|
|
} else if (URLDecode::getSite() == 'portfolio') { |
118
|
|
|
$file = "/portfolio/{$file}"; |
119
|
|
|
} else if (substr($file, 0, 7) == '/photo/') { |
120
|
|
|
$file = '/photo/processed/' . substr($file, 7); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
$image = new ImageOld($file); |
124
|
|
|
if(!$image->isValid()) { |
125
|
|
|
Loader::loadNew('controller', '/Error404Controller')->activate(); |
126
|
|
|
exit(); |
|
|
|
|
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
if(substr($redirect_uri, 0, 4) != 'http') |
131
|
|
|
{ |
132
|
|
|
$redirect_uri = substr($redirect_uri, 1); |
133
|
|
|
$redirect_uri = URLDecode::getBase() . $redirect_uri; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
Loader::loadNew('controller', '/Error301Controller', (array) $redirect_uri) |
137
|
|
|
->activate(); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
protected function check_for_special_redirect($uri) |
141
|
|
|
{ |
142
|
|
|
return $uri; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
final private function get_controller($uri) |
146
|
|
|
{ |
147
|
|
|
foreach($this->get_direct_array() as $check) |
148
|
|
|
{ |
149
|
|
|
if($uri == $check->match) |
150
|
|
|
return "{$this->get_primary_folder()}/{$check->controller}"; |
151
|
|
|
|
152
|
|
|
if(preg_match("@^{$check->match}$@", $uri)) |
153
|
|
|
return "{$this->get_primary_folder()}/{$check->controller}"; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
return '/Error404Controller'; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
final private function get_primary_folder() |
160
|
|
|
{ |
161
|
|
|
if(Request::isAjax()) |
162
|
|
|
return 'ajax'; |
163
|
|
|
if(URLDecode::getURI() == '/sitemap.xml') |
164
|
|
|
return 'sitemap'; |
165
|
|
|
if(URLDecode::getURI() == '/rss/') |
166
|
|
|
return 'rss'; |
167
|
|
|
if( |
168
|
|
|
URLDecode::getExtension() == 'jpg' || |
169
|
|
|
URLDecode::getExtension() == 'png') |
170
|
|
|
return 'images'; |
171
|
|
|
|
172
|
|
|
return URLDecode::getSite(); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
private function requires_trailing_slash() |
176
|
|
|
{ |
177
|
|
|
return ( |
178
|
|
|
URLDecode::getURI() != '/sitemap.xml' && |
179
|
|
|
URLDecode::getExtension() != 'json' && |
180
|
|
|
URLDecode::getExtension() != 'jpg' && |
181
|
|
|
URLDecode::getExtension() != 'png' && |
182
|
|
|
strstr(URLDecode::getURI(), '#') === false); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
} |
186
|
|
|
|
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
.