forked from getchabooks/getchabooks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
102 lines (81 loc) · 2.84 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
<?php
// Initialize flags, config, models, cache, etc.
require_once 'init.php';
require_once BASE_DIR . '/vendor/Slim/Slim/Slim.php';
require_once BASE_DIR . '/vendor/Slim-Extras/Log Writers/TimestampLogFileWriter.php';
$app = new Slim(array(
'mode' => defined('PRODUCTION') ? 'production' : 'development',
'debug' => false,
'log.enabled' => true,
'log.writer' => new TimestampLogFileWriter(array(
'path' => BASE_DIR,
'name_format' => '\s\l\i\m\_\l\o\g'
)),
));
$app->configureMode('development', function () use ($app) {
$app->config(array(
'debug' => true,
));
});
$app->configureMode('production', function () use ($app) {
error_reporting(0);
$app->notFound(function () use ($app) {
$page = new ErrorController(404);
$page->render();
});
$app->error(function (Exception $e) use ($app) {
$app->response()->status(500);
if (!$app->request()->isAjax()) {
$page = new ErrorController(500);
$page->render();
}
$app->stop();
if (file_exists(BASE_DIR . '/.gbemail')) {
foreach (explode('\n', file_get_contents(BASE_DIR . '/.gbemail')) as $email) {
mail(trim($email), "GetchaBooks Error", get_error_message($e));
}
}
});
});
$app->hook('slim.before', function () use ($app) {
global $referrers;
$request = $app->request();
define('BASE_URL', $request->getUrl() . $request->getRootUri() . '/');
define('CURRENT_URL', $request->getUrl() . $request->getPath());
define('MOBILE_DEVICE', strpos(strtolower($request->getUserAgent()), 'mobile') !== false);
// remove extra slashes
$path = $request->getPath();
$newPath = preg_replace("#/{2,}#", '/', $path);
if ($path != $newPath) {
$app->redirect($request->getUrl() . $newPath, 301);
}
// process referrer tag
if (isset($_GET['ref']) && isset($referrers[$_GET['ref']])) {
$_SESSION['ref'] = $_GET['ref'];
$_SESSION['tag'] = $referrers[$_GET['ref']];
} else {
$_SESSION['ref'] = null;
}
});
$routes = include 'routes.php';
// Use Slim with Class#method style routes
foreach ($routes as $name => $details) {
$fn = function () use ($details) {
list($class, $method) = explode('.', $details[1]);
$class = "{$class}Controller";
call_user_func_array(array(new $class, $method), func_get_args());
};
$route = $app->map($details[0], $fn)->name($name);
if (isset($details['method'])) {
if (!is_array($details['method'])) {
$details['method'] = array($details['method']);
}
call_user_func_array(array($route, 'via'), $details['method']);
} else {
$route->via('GET');
}
if (isset($details['conditions'])) {
$route->conditions($details['conditions']);
}
}
$app->run();