-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.php
executable file
·582 lines (477 loc) · 15.4 KB
/
init.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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
<?php
use jmvc\Controller;
use jmvc\View;
class JMVC {
public static $traces = array();
/**
* Main bootstrapping function. Initialize framework, perform routing, hand things off to template controller.
* @return void
*/
public static function init()
{
self::trace('init');
// Load classes that will always be used; faster than autoloader
include(CONFIG_FILE);
if (!defined('IS_PRODUCTION')) {
throw new \Exception('IS_PRODUCTION not set! Please check '.CONFIG_FILE.'.');
}
include(JMVC_DIR.'view.php');
include(JMVC_DIR.'controller.php');
if (file_exists(APP_DIR.'routes.php')) {
include(APP_DIR.'routes.php');
}
if (file_exists(APP_DIR.'constants.php')) {
include(APP_DIR.'constants.php');
}
date_default_timezone_set('America/New_York'); // TODO: make this configurable
spl_autoload_register(array('JMVC', 'autoloader'));
// set error handling
error_reporting(E_ERROR | E_WARNING | E_PARSE);
set_exception_handler(array('JMVC', 'handle_exception'));
set_error_handler(array('JMVC', 'handle_error'), E_ERROR | E_WARNING);
register_shutdown_function(array('JMVC', 'fatal_error_checker'));
if (defined('TRACE_THRESHOLD')) register_shutdown_function(array('JMVC', 'check_trace'));
self::trace('bootstrap complete');
if (!isset($_SERVER['REQUEST_URI'])) { //don't do routing if we're not running as a web server process
return;
}
// define some helper constants
$uri = htmlspecialchars(urldecode($_SERVER['REQUEST_URI'])); // defend against XSS
if ($qPos = strpos($uri, '?')) {
define('CURRENT_URL', substr($uri, 0, $qPos));
define('QUERY_STRING', substr($uri, $qPos));
} else {
define('CURRENT_URL', $uri);
define('QUERY_STRING', '');
}
define('IS_AJAX', isset($_SERVER['HTTP_X_REQUESTED_WITH']));
// check if we need to do a URL redirect (301)
if (isset($REDIRECTS)) {
foreach ($REDIRECTS as $in=>$out) {
$routed_url = preg_replace('%'.$in.'%', $out, CURRENT_URL, 1, $count);
if ($count) {
\jmvc\Controller::forward($routed_url.QUERY_STRING, true);
break;
}
}
}
self::trace('redirects complete');
$app_url = CURRENT_URL;
// Check for any internal URL mapping
if (isset($ROUTES)) {
foreach ($ROUTES as $in=>$out) {
$routed_url = preg_replace('%'.$in.'%', $out, $app_url, 1, $count);
if ($count) {
$routed_parts = explode('?', $routed_url);
$app_url = $routed_parts[0];
if (isset($routed_parts[1]) && !empty($routed_parts[1])) {
foreach (explode('&', $routed_parts[1]) as $pair) {
list($key, $val) = explode('=', $pair);
$_GET[$key] = $val;
$_REQUEST[$key] = $val;
}
}
break;
}
}
}
self::trace('routes complete');
// Set default context
if (defined('DEFAULT_SITE')) \jmvc\View::$CONTEXT_DEFAULTS['site'] = DEFAULT_SITE;
if (defined('DEFAULT_TEMPLATE')) \jmvc\View::$CONTEXT_DEFAULTS['template'] = DEFAULT_TEMPLATE;
if (defined('DEFAULT_CONTROLLER')) \jmvc\View::$CONTEXT_DEFAULTS['controller'] = DEFAULT_CONTROLLER;
if (defined('DEFAULT_VIEW')) \jmvc\View::$CONTEXT_DEFAULTS['view'] = DEFAULT_VIEW;
// routing
if ($app_url == '/') {
$context = \jmvc\View::$CONTEXT_DEFAULTS;
$url_parts = array();
} else if (substr($app_url, 0, 5) == '/css/') {
self::css();
} else {
// Parse each segment of the URL, left to right
$url_parts = explode('/', trim($app_url, '/'));
// Set site
if (Controller::exists($url_parts[0], 'template')) $context['site'] = array_shift($url_parts);
if (isset($context['site'])) {
if ($context['site'] == \jmvc\View::$CONTEXT_DEFAULTS['site']) { // block direct url for default site
\jmvc::do404();
}
} else {
$context['site'] = \jmvc\View::$CONTEXT_DEFAULTS['site'];
}
// Do not allow access to base class methods
if (method_exists('jmvc\\controller', $url_parts[0])) {
\jmvc::do404();
}
// Set template
if (method_exists('controllers\\'.$context['site'].'\Template', $url_parts[0])) $context['template'] = array_shift($url_parts);
if (isset($context['template'])) {
if ($context['template'] == \jmvc\View::$CONTEXT_DEFAULTS['template']) { // block direct url for default template
\jmvc::do404();
}
} else {
$context['template'] = \jmvc\View::$CONTEXT_DEFAULTS['template'];
}
// Get controller
$possible_controller = str_replace('-', '_', $url_parts[0]);
if ($possible_controller == \jmvc\View::$CONTEXT_DEFAULTS['controller'] || $possible_controller == 'template') { // block direct url for default controller
\jmvc::do404();
}
if (Controller::exists($context['site'], $possible_controller)) {
$context['controller'] = $possible_controller;
array_shift($url_parts);
} if (!isset($context['controller'])) {
$context['controller'] = \jmvc\View::$CONTEXT_DEFAULTS['controller'];
}
// Get view
$possible_view = empty($url_parts) ? null : str_replace('-', '_', $url_parts[0]);
if ($possible_view == \jmvc\View::$CONTEXT_DEFAULTS['view']) { // block direct url for default view
\jmvc::do404();
}
if (count($url_parts) && View::exists($context+array('view'=>$possible_view), true)) {
$context['view'] = $possible_view;
array_shift($url_parts);
} if (!isset($context['view'])) {
$context['view'] = \jmvc\View::$CONTEXT_DEFAULTS['view'];
}
}
self::trace('routing complete');
self::hook('post_routing', $context);
self::trace('post_routing hooks complete');
// Hand things off to the template controller
ob_start();
echo \jmvc\View::render(array_merge($context, array('controller'=>'template', 'view'=>$context['template'])),
array_merge($url_parts, array('context'=>$context)));
self::hook('post_render', $context);
}
/**
* Display a 404 error to the user. Stops execution of the script.
* @param bool $template
* @return void
*/
public static function do404()
{
// clear the output buffer
while(ob_get_length()) { ob_end_clean(); }
header("HTTP/1.0 404 Not Found");
echo \jmvc\View::render(array('controller'=>'template', 'view'=>\jmvc\View::$CONTEXT_DEFAULTS['template'],
'site'=>\jmvc\View::$CONTEXT_DEFAULTS['site'], 'template'=>\jmvc\View::$CONTEXT_DEFAULTS['template']),
array('context'=>array('controller'=>'template', 'view'=>'do404')));
exit;
}
/**
* Compile and concatinate LESS and CSS
* @return void
*/
protected static function css()
{
// for CDN compatibility, args are base64 encoded in the URL
parse_str(base64_decode(substr(CURRENT_URL, 5, -1)), $args);
if (!empty($args['files'])) $files = explode(',', $args['files']);
if (!is_array($files)) {
exit;
}
$last_change = 0;
foreach ($files as $file) {
$file = APP_DIR.'../www'.$file;
if (!file_exists($file)) {
\jmvc::do404();
}
$last_change = max($last_change, filemtime($file));
}
// see if we can serve from cache
$r = \jmvc::redis();
$key = 'JMVC:css:'.md5(serialize($files).$last_change);
$css_out = $r->get($key);
if (!$css_out || $args['nocache']) {
foreach ($files as $file) {
if (substr($file, -4) == 'less') {
$lc = new \jmvc\classes\Lessc(APP_DIR.'../www'.$file);
$css = $lc->parse();
} else {
$css = file_get_contents(APP_DIR.'../www'.$file);
}
$out[] = "\n\n\n/*** ".$file." ***/\n\n".$css;
}
$css_out = implode(' ', $out);
$r->setex($key, 3600, $css_out);
}
header('Content-type: text/css');
header('Cache-Control: max-age=31556926, public');
header('Expires: '.date('r', time()+31536000));
echo $css_out;
exit;
}
/**
* Place a job in the JMVC job queue. Requires job-worker.php to be running
* @param $class The model name to call or instantiate
* @param $method The model class method to call
* @param $obj_id Optional; if provided, the object with that ID will be instantiated, otherwise $method will be called statically.
* @param $args Arguments to be passed to $method
* @param $priority Can be either 'high' or 'low'
*/
public static function defer($class, $method, $obj_id=null, $args=array(), $priority='low')
{
if (!in_array($priority, array('high', 'low'))) {
throw new \Exception('Invalid priority type: '.$priority);
}
$r = \jmvc::redis();
$job = array('class'=>$class, 'method'=>$method, 'obj_id'=>$obj_id, 'args'=>$args, 'created'=>time());
$r->rpush('JMVC:jobs:'.$priority, json_encode($job));
}
/**
* Call a hook during a certain part of the app lifecycle. Hooks are defined in a global $HOOKS array
* with keys matching hook names. Only 'post_routing' is supported at this time.
* @param string $hook Hook to call. Only 'post_routing' is supported at this time
* @param mixed &$args Arbitrary argument to be passed to hook function. Passed by reference.
* @return void
*/
public static function hook($hook, &$args)
{
if (is_callable($GLOBALS['HOOKS'][$hook])) {
return $GLOBALS['HOOKS'][$hook]($args);
}
}
/**
* Class autoloaded. Translates namespaceing into file paths
* @param string $classname
* @return void
*/
public static function autoloader($classname)
{
$classname = strtolower($classname);
$parts = explode('\\', $classname);
switch ($parts[0]) {
case 'jmvc':
switch ($parts[1]) {
case 'classes':
$filename = JMVC_DIR.'classes/'.$parts[2].'.php';
break;
case 'models':
$filename = JMVC_DIR.'models/'.$parts[2].'.php';
break;
default:
$filename = JMVC_DIR.$parts[1].'.php';
break;
}
break;
case 'models':
$filename = APP_DIR.'models/'.$parts[1].'.php';
break;
case 'controllers':
$filename = APP_DIR.'sites/'.$parts[1].'/'.$parts[2].'.php';
break;
default:
$filename = APP_DIR.$classname.'.php';
}
if (file_exists($filename)) {
include($filename);
} else {
throw new \ErrorException('Couldn\'t load '.$classname.'; was looking at '.$filename);
}
}
/**
* Return cleaned data from superglobals
* @param string $name Key
* @param string $global Superglobal to access
* @param bool $raw Only trim the value, don't clean. Defaults to false
* @return string
*/
public static function input($name, $global=null, $raw=false)
{
switch (strtolower($global)) {
case 'get':
if (isset($_GET[$name])) $inp = $_GET[$name];
break;
case 'post':
if (isset($_POST[$name])) $inp = $_POST[$name];
break;
case 'cookie':
if (isset($_COOKIE[$name])) $inp = $_COOKIE[$name];
break;
case 'server':
if (isset($_SERVER[$name])) $inp = $_SERVER[$name];
break;
default:
if (isset($_REQUEST[$name])) $inp = $_REQUEST[$name];
break;
}
if (isset($inp)) {
if ($raw) {
return trim($inp);
} else {
return htmlspecialchars(strip_tags(trim($inp)), ENT_COMPAT, 'ISO-8859-1', false);
}
}
}
/**
* Append data to a file.
* @param string $data Data to be recorded
* @param string $logname Optional
* @return void
*/
public static function log($data, $logname=false)
{
$host = $_SERVER['HTTP_HOST'] ?: 'cmd';
if ($logname) $host .= '.';
$log = @fopen(LOG_DIR.'/'.$host.$logname.'.log', 'a');
if ($log) {
fwrite($log, date('g:i:sa M j, Y')."----------------\n".$data."\n\n");
fclose($log);
}
}
/**
* Set a trace point. Points are measured in milliseconds from script start time
* @param string $message The trace message
* @return void
*/
public static function trace($message)
{
static $start_time;
if (!$start_time) {
$start_time = microtime(true);
}
$trace = array('time'=>(int)((microtime(true)-$start_time)*1000), // round to nearest integer
'message'=>$message);
self::$traces[] = $trace;
return $trace;
}
public static function check_trace()
{
if (defined('NO_TRACE_CHECK')) return;
$last_trace = array_pop(self::$traces);
if ($last_trace['time'] > TRACE_THRESHOLD) {
$message = '';
foreach (self::$traces as $trace) {
$message .= $trace['time']."\t".$trace['message']."\n";
}
$message .= $last_trace['time']."\t".$last_trace['message'];
self::log($message, 'traces');
}
}
/**
* Retrieve an connection to the cache
* @return \jmvc\classes\Cache_Interface
*/
public static function cache()
{
static $driver_instance = false;
if (!$driver_instance) {
if (isset($GLOBALS['_CONFIG']['cache_driver'])) {
$driver = $GLOBALS['_CONFIG']['cache_driver'];
$driver_instance = $driver::instance();
} else {
throw new \Exception('cache_driver not set!');
}
}
return $driver_instance;
}
/**
* Retrieve an connection to Redis
* @return \Redis
*/
public static function redis()
{
static $redis_instance = false;
if (!$redis_instance) {
if (isset($GLOBALS['_CONFIG']['redis'])) {
$redis_instance = new \Redis();
$redis_instance->connect($GLOBALS['_CONFIG']['redis']['host'], $GLOBALS['_CONFIG']['redis']['port']);
} else {
throw new \Exception('redis config not set!');
}
}
return $redis_instance;
}
public static function handle_exception($ex)
{
// clear the output buffer
while(ob_get_length()) { ob_end_clean(); }
if (!IS_PRODUCTION) {
include(JMVC_DIR.'exception_html.php');
die();
}
self::notify_admin($ex);
// clear the output buffer
header('HTTP/1.1 500 Internal Server Error');
die;
}
public static function handle_error($errno, $errstr, $errfile, $errline)
{
if (error_reporting() == 0) return;
throw new \ErrorException($errstr, 0, $errno, $errfile, $errline);
}
public static function fatal_error_checker()
{
if ($error = error_get_last()) {
if ($error['type'] != 8 && (!IS_PRODUCTION || $error['type'] <= 2)) {
self::handle_exception(new \ErrorException($error['message'], 0, $error['type'], $error['file'], $error['line']));
}
}
}
public static function notify_admin($e)
{
if (defined(SENTRY_ENDPOINT)) {
require_once(JMVC_DIR.'Raven/Client.php');
require_once(JMVC_DIR.'Raven/Compat.php');
require_once(JMVC_DIR.'Raven/Stacktrace.php');
$client = new Raven_Client(SENTRY_ENDPOINT);
$client->captureException($e);
exit();
}
$file = $e->getFile();
$message = self::make_error_report($e->getFile(), $e->getLine(), $e->getMessage());
self::log(date('r')."\n".$message, 'php_errors');
$lockfile = LOG_DIR.'/error_state';
$last_notification = (file_exists($lockfile)) ? filemtime($lockfile) : 0;
if ($last_notification == 0 || (DEFINED('ADMIN_ALERT_FREQ') && time() - $last_notification > ADMIN_ALERT_FREQ)) {
touch($lockfile);
mail(ADMIN_EMAIL, 'Error in '.$file, $message);
if (defined('ADMIN_ALERT')) {
mail(ADMIN_ALERT, 'Error on '.$_SERVER['HTTP_HOST'], 'check email');
}
}
}
private static function make_error_report($file, $line, $message)
{
$out = 'An error occurred on line '.$line.' of '.$file.'
'.$message.'
REQUEST URI: http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']."\n\n";
if (!empty($_GET)) {
$out .= 'GET: '.print_r($_GET, true)."\n\n";
}
if (!empty($_POST)) {
$out .= 'POST: '.print_r($_POST, true)."\n\n";
}
if (!empty(jmvc\classes\Session::$d)) {
$out .= 'SESSION: '.print_r(jmvc\classes\Session::$d, true)."\n\n";
}
if (!empty($_SERVER)) {
$out .= 'SERVER: '.print_r($_SERVER, true)."\n\n";
}
return $out;
}
}
/**
* Dump the contents of a variable with some pretty formatting
* @param mixed $data
* @return void
*/
function pp($data)
{
if (!IS_PRODUCTION) {
echo '<pre>'.htmlspecialchars(print_r($data,true)).'</pre>';
}
}
/**
* Dump the contents of a variable and exit(). Similar to pp()
* @param mixed $data
* @return void
*/
function pd($data)
{
if (!IS_PRODUCTION) {
pp($data);
die();
}
}