-
Notifications
You must be signed in to change notification settings - Fork 0
/
Router.php
356 lines (280 loc) · 10.9 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
<?php (defined('BASEPATH')) OR exit('No direct script access allowed');
/* load the MX core module class */
require dirname(__FILE__).'/Modules.php';
use Illuminate\Database\Capsule\Manager as Capsule;
/**
* Modular Extensions - HMVC
*
* Adapted from the CodeIgniter Core Classes
* @link http://codeigniter.com
*
* Description:
* This library extends the CodeIgniter router class.
*
* Install this file as application/third_party/MX/Router.php
*
* @copyright Copyright (c) 2011 Wiredesignz
* @version 5.4
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
**/
class MX_Router extends CI_Router
{
private $module;
protected static $DB = null;
public function fetch_module()
{
return $this->module;
}
public function _validate_request($segments)
{
if (count($segments) == 0) return $segments;
/* locate module controller */
if ($located = $this->locate($segments)) {
return $located;
}
/* Routes module */
if ($routes_segments = $this->routes($segments))
{
if ($located = $this->locate($routes_segments)) return $located;
}
/* use a default 404_override controller */
if (isset($this->routes['404_override']) and $this->routes['404_override']) {
$segments = explode('/', $this->routes['404_override']);
if ($located = $this->locate($segments)) return $located;
}
/* no controller found */
show_404();
}
/** Locate the controller **/
public function locate($segments)
{
/**
* Load the site ref for multi-site support if the "sites" module exists
* and the multi-site constants haven't been defined already (hmvc request)
*/
if ($path = self::is_multisite() and ! defined('SITE_REF'))
{
$site = self::$DB
->table('core_sites')
->select('core_sites.name', 'core_sites.ref', 'core_sites.domain', 'core_sites.is_activated', 'core_domains.domain as alias_domain', 'core_domains.type as alias_type')
->where('core_sites.domain', '=', SITE_DOMAIN)
->orWhere('core_domains.domain', '=', SITE_DOMAIN)
->leftJoin('core_domains', 'core_domains.site_id', '=', 'core_sites.id')
->first();
// If the site is disabled we set the message in a constant for MY_Controller to display
if (isset($site->is_activated) and ! $site->is_activated) {
$status = $DB->where('slug', 'status_message')
->get('core_settings')
->row();
define('STATUS', $status ? $status->value : 'This site has been disabled by a super-administrator');
}
// If this domain is an alias and it is a redirect
if ($site and $site->alias_domain !== null and $site->alias_type === 'redirect' and str_replace(array('http://', 'https://'), '', trim(strtolower(BASE_URL), '/')) !== $site->domain) {
$protocol = ( ! empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off')
? 'https' : 'http';
// Send them off to the original domain
header("Location: {$protocol}://{$site->domain}{$_SERVER['REQUEST_URI']}");
exit;
}
$locations = array();
// Check to see if the site retrieval was successful. If not then
// we will let MY_Controller handle the errors.
if (isset($site->ref)) {
// Set the session config to the correct table using the config name (but removing 'default_')
$this->config->set_item('sess_table_name', $site->ref.'_'.str_replace('default_', '', config_item('sess_table_name')));
// The site ref. Used for building site specific paths
define('SITE_REF', $site->ref);
// Path to uploaded files for this site
define('UPLOAD_PATH', 'uploads/'.SITE_REF.'/');
// Path to the addon folder for this site
define('ADDONPATH', ADDON_FOLDER.SITE_REF.'/');
// the path to the MSM module
define('MSMPATH', str_replace('__SITE_REF__', SITE_REF, $path));
}
}
// we aren't running the Multi-Site Manager so define the defaults
if ( ! defined('SITE_REF'))
{
// The site ref. Used for building site specific paths
define('SITE_REF', 'default');
// Path to uploaded files for this site
define('UPLOAD_PATH', 'uploads/'.SITE_REF.'/');
// Path to the addon folder for this site
define('ADDONPATH', ADDON_FOLDER.SITE_REF.'/');
}
// update the config paths with the site specific paths
self::update_module_locations(SITE_REF);
$this->module = '';
$this->directory = '';
$ext = $this->config->item('controller_suffix').'.php';
/* use module route if available */
if (isset($segments[0]) and $routes = Modules::parse_routes($segments[0], implode('/', $segments))) {
$segments = $routes;
}
/* get the segments array elements */
list($module, $directory, $controller) = array_pad($segments, 3, null);
/* check modules */
foreach (Modules::$locations as $location => $offset) {
/* module exists? */
if (is_dir($source = $location.$module.'/controllers/')) {
$this->module = $module;
$this->directory = $offset.$module.'/controllers/';
/* module sub-controller exists? */
if ($directory and is_file($source.$directory.$ext)) {
return array_slice($segments, 1);
}
/* module sub-directory exists? */
if ($directory and is_dir($source.$directory.'/')) {
$source = $source.$directory.'/';
$this->directory .= $directory.'/';
/* module sub-directory controller exists? */
if (is_file($source.$directory.$ext)) {
return array_slice($segments, 1);
}
/* module sub-directory sub-controller exists? */
if ($controller and is_file($source.$controller.$ext)) {
return array_slice($segments, 2);
}
}
/* module controller exists? */
if (is_file($source.$module.$ext)) {
return $segments;
}
}
}
/* application controller exists? */
if (is_file(APPPATH.'controllers/'.$module.$ext)) {
return $segments;
}
/* application sub-directory controller exists? */
if ($directory and is_file(APPPATH.'controllers/'.$module.'/'.$directory.$ext)) {
$this->directory = $module.'/';
return array_slice($segments, 1);
}
/* application sub-directory default controller exists? */
if (is_file(APPPATH.'controllers/'.$module.'/'.$this->default_controller.$ext)) {
$this->directory = $module.'/';
return array($this->default_controller);
}
}
public function set_class($class)
{
$this->class = $class.$this->config->item('controller_suffix');
}
private function is_multisite()
{
foreach (Modules::$locations as $location => $offset)
{
if (is_dir($location.'sites'))
{
return $location.'sites/';
}
}
// one last check, the default site's folder
if (is_dir(ADDON_FOLDER.'default/modules/sites'))
{
return ADDON_FOLDER.'default/modules/sites/';
}
return false;
}
private function update_module_locations($site_ref)
{
$locations = array();
foreach (config_item('modules_locations') AS $location => $offset)
{
$locations[str_replace('__SITE_REF__', $site_ref, $location)] = str_replace('__SITE_REF__', $site_ref, $offset);
}
Modules::$locations = $locations;
}
/**
* Check if a route from the routes module has been used
* @param array $segments
* @return array [description]
*/
public function routes($segments)
{
// Connect and save the connection
self::$DB = self::connect();
//TODO Caching Here
$routes = self::$DB->table(SITE_REF.'_routes')->select('route_key', 'route_value')->get();
$uri = implode('/', $segments);
foreach ($routes as $route)
{
if ($uri == $route->route_key) return explode('/', $route->route_value);
$key = str_replace(':any', '.+', str_replace(':num', '[0-9]+', $route->route_key));
// Does the RegEx match?
if (preg_match('#^'.$key.'$#', $uri))
{
// Do we have a back-reference?
if (strpos($route->route_value, '$') !== FALSE AND strpos($key, '(') !== FALSE)
{
$val = preg_replace('#^'.$key.'$#', $route->route_value, $uri);
}
return explode('/', $route->route_value);
}
}
}
private function connect() {
include APPPATH.'config/database.php';
$db = $db[ENVIRONMENT];
// Is this a PDO connection?
if ($db['dbdriver']) {
preg_match('/(mysql|pgsql|sqlite)+:.+dbname=(\w+)/', $db['dsn'], $matches);
$subdriver = $matches[1];
$database = $matches[2];
unset($matches);
$drivers = array(
'mysql' => '\Illuminate\Database\MySqlConnection',
'pgsql' => '\Illuminate\Database\PostgresConnection',
'sqlite' => '\Illuminate\Database\SQLiteConnection',
);
$pdo = new PDO($db['dsn'], $db['username'], $db['password'], array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
// Make a connection instance with the existing PDO connection
$conn = new $drivers[$subdriver]($pdo, $database);
$resolver = new Illuminate\Database\ConnectionResolver;
$resolver->addConnection('default', $conn);
$resolver->setDefaultConnection('default');
Illuminate\Database\Eloquent\Model::setConnectionResolver($resolver);
// Not using the new PDO driver
} else {
$capsule = new Capsule;
$capsule->addConnection(array(
'driver' => $dbdb['dbdriver'],
'host' => $db["hostname"],
'database' => $db["database"],
'username' => $db["username"],
'prefix' => $prefix,
'password' => $db["password"],
'charset' => $db["char_set"],
'collation' => $db["dbcollat"],
));
// Set the fetch mode FETCH_CLASS so we
// get objects back by default.
$capsule->setFetchMode(PDO::FETCH_CLASS);
// Setup the Eloquent ORM
$capsule->bootEloquent();
// Make this Capsule instance available globally via static methods... (optional)
$capsule->setAsGlobal();
$conn = $capsule->connection();
}
$conn->setFetchMode(PDO::FETCH_OBJ);
return $conn;
}
}