-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
192 lines (160 loc) · 5.6 KB
/
index.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
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL ^ E_NOTICE);
session_start();
### AUTO CONFIG ##############################
$sSiteRoot = dirname(__FILE__).'/';
##############################################
##############################################
include($sSiteRoot.'app/config/config.php');
// Set timezone
putenv('TZ='.$aConfig['options']['timezone']);
date_default_timezone_set($aConfig['options']['timezone']);
ini_set('include_path', ini_get('include_path').':'.$sSiteRoot.'app/core/libraries/:'.$sSiteRoot.'app/views/');
### URL VARIABLES ############################
// Remove _GET parameters from url
$sURL = array_shift(explode('?', $_SERVER['REQUEST_URI']));
// Force ending slash
if(substr($sURL, -1) != '/' && substr($sURL,-4,1) != '.' && substr($sURL,-3,1) != '.')
{
// Save _GET parameters
if(!empty($_SERVER['QUERY_STRING']))
$sQueryString .= '?'.$_SERVER['QUERY_STRING'];
// Permanently redirect page
header('HTTP/1.1 301 Moved Permanently');
header('Location: '.$sURL.'/'.$sQueryString);
exit;
}
// Break URL into peices
$aURL = explode('/', $sURL);
array_shift($aURL); // Remove first array item, always empty
array_pop($aURL); // Remove last array item, always empty
$sController = strtolower(preg_replace('/([^a-z0-9_-]+)/i', '', $aURL[0]));
$sAction = strtolower(preg_replace('/([^a-z0-9_-]+)/i', '', $aURL[1]));
if(empty($sController)) {
$sController = 'app';
}
if(empty($sAction)) {
$sAction = 'index';
}
##############################################
### PREPARE URL PATTERN ######################
require($sSiteRoot.'app/config/routes.php');
// Split patterns into chunks to not choke the server
$aPatternGroups = array_chunk($aRoutePatterns, 80, TRUE);
// Run just created pattern chunks
foreach($aPatternGroups as $aGroupChunk) {
$aPatterns = array();
$aKeys = array();
/* Prepare patterns for matching */
$i = 0;
foreach($aGroupChunk as $sIndex => $sValue) {
$aKeys[$i] = $sIndex;
$sIndex = preg_replace('/<([a-z]+):(.+?)>/i', '($2)', $sIndex);
$aPatterns[] = '(?P<url'.$i.'>^'.$sIndex.'$)';
$i++;
}
/* Run pattern chunk */
preg_match('/'.str_replace('/','\/',implode('|',$aPatterns)).'/i', $sURL, $aMatches);
/* See if one of the patterns stuck */
foreach(array_reverse($aMatches) as $sIndex => $sValue) {
if(!is_numeric($sIndex) && !empty($sValue)) {
// Pattern is found
$sPattern = str_replace('url',null,$sIndex);
$sPattern = $aKeys[$sPattern];
break;
}
}
// If pattern is found, don't try anymore chunks
if(!empty($sPattern)) {
break;
}
}
##############################################
### APPLICATION ##############################
require($sSiteRoot.'app/core/gw.php');
require($sSiteRoot.'app/core/application.php');
require($sSiteRoot.'app/core/controller.php');
require($sSiteRoot.'app/core/model.php');
require($sSiteRoot.'app/core/load.php');
require($sSiteRoot.'app/core/error.php');
$oApp = Application::getInstance(true, 'index');
$oLoad = new Load();
$oError = new Error();
$oLoad->reloadInstance(true);
$oError->reloadInstance(true);
##############################################
### DATABASE #################################
include($sSiteRoot.'app/core/libraries/stratum/database.php');
$oDatabase = new Stratum();
$oApp->setDatabase($oDatabase);
if($aConfig['database']['connect'] == true) {
$oDatabase->connect(
$aConfig['database']['username'],
$aConfig['database']['password'],
$aConfig['database']['database'],
$aConfig['database']['host']
);
}
##############################################
foreach($aConfig['autoLoad'] as $sType => $aValues) {
if(in_array($sType, array('model', 'helper'))) {
foreach($aValues as $sValue) {
if(is_array($sValue)) {
call_user_func_array(array($oApp->load, $sType), $sValue);
} else {
$oapp->load->$sType($sValue);
}
}
}
}
ob_start();
ob_implicit_flush(false);
if(count($aRoutePatterns[$sPattern]) > 0 && is_file($sSiteRoot.'app/controllers/'.$aRoutePatterns[$sPattern]['controller'].'.php')) {
$aRoutePattern = $aRoutePatterns[$sPattern];
$sController = $aRoutePattern['controller'];
$sAction = $aRoutePattern['action'];
include($sSiteRoot.'app/controllers/'.$sController.'.php');
if(class_exists($sController)) {
if(method_exists($sController, $sAction)) {
// Pull dynamic variables from url
$sPatternRGXP = preg_replace('/<([a-z]+):(.+?)>/i', '(?P<$1>$2)', $sPattern);
preg_match('/'.str_replace('/', '\/', $sPatternRGXP).'/i', $sURL, $aParamMatches);
// Put dynamic variables into usable array
$urlParams = array();
foreach($aParamMatches as $sKey => $sValue) {
if(!is_numeric($sKey) && !empty($sValue)) {
$urlParams[$sKey] = $sValue;
}
}
if(is_array($aRoutePattern['param'])) {
// Combine dynamic and manual url variables to be loaded into the Controller
$aURLVars = array_merge($urlParams, $aRoutePattern['param']);
} else {
$aURLVars = $urlParams;
}
$oController = $oApp->load->controller($sController);
$oController->$sAction();
} else {
$oApp->error->send('Page not found.', '404');
}
} else {
$oApp->error->send('Page not found.', '404');
}
} elseif(is_file($sSiteRoot.'app/controllers/'.$sController.'.php')) {
include($sSiteRoot.'app/controllers/'.$sController.'.php');
if(class_exists($sController)) {
if(method_exists($sController, $sAction)) {
$oController = new $sController($sController, $aURL);
call_user_func_array(array($oController, $sAction), array_slice($aURL, 2));
} else {
$oApp->error->send('Page not found.', '404');
}
} else {
$oApp->error->send('Page not found.', '404');
}
} else {
$oApp->error->send('Page not found.', '404');
}
echo ob_get_contents();
ob_end_clean();