This repository has been archived by the owner on Nov 10, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.php
429 lines (366 loc) · 10.5 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
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
<?php
define('MOEPRESS_VERSION', '1.1.4');
/**
* MoePress Mini
*
* A multi-level controller PHP framework
*
* @version 1.1.4
* @update 2017-09-19
* @since 2013-10-06
* @author Maiz
* @documentation https://maizify.com/work/moepress-mini
*
*/
class MoePress
{
static public $MP = NULL; // MoePress Instance
static public $config = array(); // global config (via config.php)
protected $base_path = '';
protected $model_path = '';
protected $view_path = '';
protected $library_path = '';
private $ob_level = null; // to be used when load views
private $loaded_files = array( // to ensure files not being loaded twice
'model' => array(),
'library' => array()
);
public $site_url = ''; // current site's URL
public $current_url = ''; // current URL
public $data = array(); // variables used in views
protected $argc = array(); // the limitation of the number of controller's arguments
/**
* Init Controller
* DO NOT call this method more than ONCE.
*
* @version 2014-10-12
*/
public function __init()
{
// init file path
$this->base_path = rtrim(getcwd(), '/') . '/';
$this->model_path = $this->base_path . 'model/';
$this->view_path = $this->base_path . 'view/';
$this->library_path = $this->base_path . 'library/';
// URLs
$s = empty($_SERVER['HTTPS']) ? '' : ($_SERVER['HTTPS'] == 'n') ? 's' : '';
$sp = strtolower($_SERVER['SERVER_PROTOCOL']);
$protocol = substr($sp, 0, strpos($sp, '/')) . $s;
$port = ':' . $_SERVER['SERVER_PORT'];
if ($port==':80' or $port==':443') { $port = ''; }
$url = $protocol . '://' . $_SERVER['HTTP_HOST'] . $port . '/';
$this->site_url = $url;
if (isset(MoePress::$config['base_uri']) and MoePress::$config['base_uri']) {
$this->site_url .= trim(MoePress::$config['base_uri'], '/') . '/';
}
$this->current_url = $url . ltrim($_SERVER['REQUEST_URI'], '/');
}
/**
* Load view
*
* @version 2013-10-06
* @usage
* // simply load a view
* $this->view('home_view.php');
*
* // pass specified variable(s) to the view
* $this->view('home_view.php', array('home_name'=>'My Home'));
*
* // or you can set variable(s) in $this->data, the following two lines is equal to the above one
* $this->data['home_name'] = 'My Home';
* $this->view('home_view.php');
*
* // get the view as a string(rather then output to browser right away) so that you can edit it later
* $view_html_string = $this->view('home_view.php', array(), true);
*
* @param string $file the file path
* @param array $data the variables used in view. Array key is the variable's name.
* @param boolean $return whether output the view to browser directly or return it as a string variable
* @return object|string if $return==false, then return $this, otherwise return view string
*/
public function view($file = '', $data = array(), $return = false)
{
if ($data) {
$data = (is_object($data)) ? get_object_vars($data) : $data;
$this->data = array_merge($this->data, $data);
}
extract($this->data);
if ($this->ob_level === null) {
$this->ob_level = ob_get_level();
}
ob_start();
if (file_exists($this->view_path.$file)) {
include($this->view_path.$file);
} else {
throw new Exception('View file "'.$file.'" does not exist.');
}
if ($return == true) {
$buffer = ob_get_contents();
@ob_end_clean();
return $buffer;
}
if (ob_get_level() > $this->ob_level + 1) {
ob_end_flush();
}
return $this;
}
/**
* Load model
*
* @version 2013-10-06
* @usage
* // load single model
* $this->model('user_model.php');
*
* // load multiple models
* $this->model(array('user_model.php', 'home_model.php'));
*
* @param string|array $files the file path(s) of model, multiple files are supported
* @return object $this
*/
public function model($files = array())
{
$this->load('model', $files);
return $this;
}
/**
* Load library
*
* @version 2013-10-06
* @usage see method model()
*
* @param string|array $files the file path(s) of library, multiple files are supported
* @return object $this
*/
public function library($files = array())
{
$this->load('library', $files);
return $this;
}
/**
* Load file (model / library)
*
* @version 2013-10-06
*
* @param string $type choose "model" or "library"
* @param string $files the file path(s), multiple files are supported
*/
private function load($type = '', $files = array())
{
switch ($type) {
case 'model':
$path = $this->model_path;
break;
case 'library':
$path = $this->library_path;
break;
default:
throw new Exception('Unknow type "'.$type.'" while loading files.');
break;
}
if (is_string($files)) {
$files = array($files);
}
foreach ($files as $file) {
if (isset($this->loaded_files[$type][$file]) and $this->loaded_files[$type][$file] == true) {
// this file has been loaded, so we do nothing here
} else if (file_exists($path.$file)) {
require_once($path.$file);
$this->loaded_files[$type][$file] = true;
} else {
throw new Exception(ucwords($type).' file "'.$file.'" does not exist.');
}
}
}
/**
* Helper function: Get site URL
*
* @version 2013-10-06
*
* @return string
*/
public function site_url($uri = '')
{
return $this->site_url . ($uri ? trim($uri, '/') : '');
}
/**
* Helper function: Get current URL
*
* @version 2013-10-09
*
* @return string
*/
public function current_url()
{
return $this->current_url;
}
/**
* Helper function: Redirect to other URL
*
* @version 2013-10-06
*
* @param string $url
* @param int $code http response code
*/
public function redirect($url = '', $code = 302)
{
if (!preg_match('/^http/', $url)) {
$url = $this->site_url($url);
}
header("Location: ".$url, true, $code);
exit;
}
/**
* Run MoePress
*
* @version 2014-09-06
*/
static public function run()
{
$base_path = rtrim(getcwd(), '/') . '/';
$config_file = $base_path.'config.php';
$controller_path = $base_path.'controller/';
// if the config file exists, then load it.
// note that this config file 'config.php' is not requisite
$_CONFIG = array();
if (file_exists($config_file)) {
require_once($config_file);
}
MoePress::$config = $_CONFIG;
// if $_CONFIG exists in config.php, then do something according to the configurations
if ($_CONFIG) {
// auto load files before running controller
if (is_array($_CONFIG['auto_load']) and count($_CONFIG['auto_load'])>0) {
foreach ($_CONFIG['auto_load'] as $file) {
if (!file_exists($base_path.$file)) {
throw new Exception('The auto-loaded file "'.$file.'" does not exists.');
}
require_once($base_path.$file);
}
}
}
// get the requrest URI for routing
$request_uri = preg_replace('/\?(.*)/', '', $_SERVER['REQUEST_URI']);
// if a base URI is setted, then remove the base URI from the requrest URI
if (isset($_CONFIG['base_uri']) and $_CONFIG['base_uri']) {
$request_uri = str_replace(trim($_CONFIG['base_uri'], '/'), '', $request_uri);
}
$request_uri = trim($request_uri, '/');
// routing
// note that the $request_uri can be matched and changed more than onece
if (isset($_CONFIG['routes']) and is_array($_CONFIG['routes']) and count($_CONFIG['routes']) > 0) {
foreach ($_CONFIG['routes'] as $pattern => $subject) {
if (preg_match($pattern, $request_uri)) {
$request_uri = preg_replace($pattern, $subject, $request_uri);
}
}
}
// find controller
$uris = explode('/', $request_uri);
$controller_args = array();
$controller_file = $controller_path.'main.php';
while (count($uris) > 0) {
$file = $controller_path.implode('/', $uris).'/main.php';
if (file_exists($file)) {
$controller_file = $file;
break;
} else {
// if the URI is not a controller, then use it as controller method & arguments
array_unshift($controller_args, array_pop($uris));
}
}
// load controller
if (!file_exists($controller_file)) {
throw new Exception('Controller "'.$controller_file.'" does not exist.');
}
require_once($controller_file);
// instantiate controller object
if (!class_exists('Controller')) {
throw new Exception('Controller class does not exist in file "'.$controller_file.'".');
}
$MP = new Controller();
$MP->__init();
MoePress::$MP = $MP;
// find controller method
$method = '__index';
// if the name of first args value exists in controller, use it as default method
if ($controller_args and method_exists($MP, $controller_args[0])) {
$method = array_shift($controller_args);
}
if (!method_exists($MP, $method)) {
throw new Exception('Controller method "'.$method.'" does not exist.');
}
// check the number of controller arguments
if (isset($MP->argc[$method])) {
$limit = $MP->argc[$method];
// the limitation should be [min_num, max_num]
if (is_numeric($limit)) {
$limit = array($limit, $limit);
}
if (!is_array($limit)) {
throw new Exception('The limitation of controller arguments is incorrect.');
}
$count = count($controller_args);
if ( !($count >= $limit[0] and $count <= $limit[1]) ) {
throw new Exception('The number of controller\'s arguments is incorrect.');
}
}
// pass the args to the controller method, and run it
call_user_func_array(array($MP, $method), $controller_args);
// finished.
}
} // END class MoePress
/**
* Dump formatted variable for debugging
*
* @version 2013-03-16
*
* @param mixed $value
*/
function v($value)
{
echo '<pre>';
if (func_num_args()==0) {
echo 'NULL';
} else {
if ($value) {
print_r($value);
} else {
if (is_bool($value)) {
echo 'false';
} else if (is_numeric($value)) {
echo '0';
} else if (is_array($value)) {
echo "Array\n(\n)";
} else if (is_string($value)) {
echo '""';
} else if (is_null($value)) {
echo 'null';
} else {
print_r($value);
}
}
}
echo '</pre>';
}
// here we go
try {
MoePress::run();
} catch (Exception $e) {
if (isset(MoePress::$config['debug']) and MoePress::$config['debug'] != false) {
$traces = $e->getTrace();
echo '<h1>Exception</h1>';
echo '<div>'.$e->getMessage().'</div>';
foreach ($traces as $trace) {
echo '<ul>';
echo '<li>File: '.$trace['file'].'</li>';
echo '<li>Line: '.$trace['line'].'</li>';
echo '<li>In function "'.$trace['function'].'" of class "'.$trace['class'].'"</li>';
echo '</ul>';
}
} else {
echo '<h1>Exception</h1>';
echo '<div>Try to contact the webmaster.</div>';
}
}
/* END file of Moepress-Mini */