-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.php
197 lines (142 loc) · 5.27 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
193
194
195
196
197
<?php
/**
* INDEX BOOTSTRAP
*
* This is the starting point (index) for the system. Here we check cached files
* and then precede to load the system and finally run the controller.
*
* @package MicroMVC
* @author David Pennington
* @copyright Copyright (c) 2009 MicroMVC
* @license http://www.gnu.org/licenses/gpl-3.0.html
* @link http://micromvc.com
* @version 1.1.0 <7/7/2009>
********************************** 80 Columns *********************************
*/
//Log current time so we can tell how long it takes to run this script
define('START_TIME', microtime(true));
//Log starting memory useage
define('START_MEMORY_USAGE', memory_get_usage());
//Set the unique name of the current page (for the cache)
$var = preg_replace("/([^a-z0-9_\-\.]+)/i", '_', $_SERVER["REQUEST_URI"]);
define('PAGE_NAME', ($var ? $var : 'index'));
//Define the OS file path separator
define('DS', DIRECTORY_SEPARATOR);
//Define the base file system path to MicroMVC
define('SYSTEM_PATH', realpath(dirname(__FILE__)). DS);
//Define the base file system path to libraries
define('LIBRARY_PATH', SYSTEM_PATH. 'libraries'. DS);
//Define the base file system path to functions
define('FUNCTION_PATH', SYSTEM_PATH. 'functions'. DS);
//Define the base file system path to modules
define('MODULE_PATH', SYSTEM_PATH. 'modules'. DS);
//Define the base file system path to logs
define('LOG_PATH', SYSTEM_PATH. 'logs'. DS);
//Include the common file to continue loading
require_once(FUNCTION_PATH. 'common.php');
//Discover whether this is an AJAX request or not
define('AJAX_REQUEST', is_ajax_request());
//Discover the current domain for the whole script
define('DOMAIN', current_domain());
//Define the file system path to the current site
define('SITE_PATH', SYSTEM_PATH. DOMAIN. DS);
//The file system path of the site's cache folder
define('CACHE_PATH', SITE_PATH. 'cache'. DS);
//The file system path of the site's config folder
define('CONFIG_PATH', SITE_PATH. 'config'. DS);
//The file system path of the site's models folder
define('MODEL_PATH', SITE_PATH. 'models'. DS);
//The file system path of the site's uploads folder
define('UPLOAD_PATH', SITE_PATH. 'uploads'. DS);
//The file system path of the site's views folder
define('VIEW_PATH', SITE_PATH. 'views'. DS);
//Override the PHP error handler
set_error_handler('_error_handler');
//Require the config file for this site name
require(SITE_PATH. 'config/config.php');
//Require the config file for the hooks
require(SITE_PATH. 'config/hooks.php');
//Load the caching class
$cache = load_class('cache', LIBRARY_PATH);
//Load the hooks class
$hooks = load_class('hooks', LIBRARY_PATH, $hooks);
//Call first hook
$hooks->call('system_startup');
/**
* Check for cached version -die if found
*/
if($output = $cache->fetch(md5(PAGE_NAME. AJAX_REQUEST), null, null)) {
print $output;
//If debuging is enabled
if(DEBUG_MODE) {
$time = round((microtime(true) - START_TIME), 5);
$memory = round((memory_get_usage() - START_MEMORY_USAGE) / 1024);
die('<!-- Rendered in '. $time. ' seconds using '. $memory. ' kb of memory -->');
}
}
/**
* strip the slashes that have been added to our POST/GET data!
*/
if (ini_get('magic_quotes_gpc')) {
function array_clean(&$value) {
$value = stripslashes($value);
}
//php 5+ only
array_walk_recursive($_GET, 'array_clean');
array_walk_recursive($_POST, 'array_clean');
array_walk_recursive($_COOKIE, 'array_clean');
}
//Include the controller class file
load_class('controller', LIBRARY_PATH, NULL, FALSE);
//Include the base class file
load_class('base', LIBRARY_PATH, NULL, FALSE);
/**
* Get the controller from the URI
*/
$routes = load_class('routes', LIBRARY_PATH);
//Set default controller/method if none is set in URL
$routes->set_defaults(
$config['default_controller'],
$config['default_method'],
$config['permitted_uri_chars']
);
//Parse the URI
$routes->parse();
//Fetch the controller/method
$controller = $routes->fetch(0);
$method = $routes->fetch(1);
/**
* START-UP THE SYSTEM!
*/
//Try to load the controller from the site directory first
if(file_exists(SITE_PATH. 'controllers'. DS. $controller. '.php')) {
$path = SITE_PATH. 'controllers'. DS;
//Then try to see if there is a module
} elseif(file_exists(MODULE_PATH. $controller. DS. 'controllers'. DS. $controller. '.php')) {
$path = MODULE_PATH. $controller. DS. 'controllers'. DS;
} else {
//Show a 404 error and exit the script
request_error();
}
//Load the controller and pass the $config
$controller = load_class($controller, $path, $config);
//Make sure someone isn't trying to access core or private functions
if($method != 'request_error' && $controller != 'core') {
//Make sure this method exists (and is public)
if(method_exists('core', $method) OR ! in_array($method, get_class_methods($controller))) {
//Show a 404 error and exit the script
request_error();
}
}
//Call the startup hook
$controller->hooks->call('post_constructor');
// Call the requested method.
// Any URI segments present (besides the class/function)
// will be passed to the method for convenience
call_user_func_array(array(&$controller, $method), array_slice($routes->fetch(true), 2));
//Call the post-controller hook
$controller->hooks->call('post_method');
// And we're done!
$controller->render();
//Call the finish hook
$controller->hooks->call('system_shutdown');