-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.php
executable file
·154 lines (137 loc) · 5.29 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
<?php
require_once("config.php");
require_once("src/autoload.php");
require_once("vendor/autoload.php");
use BlueHerons\StatTracker\Agent;
use BlueHerons\StatTracker\StatTracker;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
$StatTracker = new StatTracker();
$StatTracker['controllers']->before(function(Request $request) use ($StatTracker) {
$StatTracker->setBaseUrl($request);
if (!is_dir(UPLOAD_DIR) || !is_writeable(UPLOAD_DIR)) {
throw new Exception(sprintf("UPLOAD_DIR (%s) is not writeable", UPLOAD_DIR));
}
if (!is_dir(LOG_DIR) || !is_writeable(LOG_DIR)) {
throw new Exception(sprintf("LOG_DIR (%s) is not writeable", LOG_DIR));
}
});
$StatTracker->error(function(Exception $e, $code) {
// Eventually, have a custom error page
});
// Default handler. Will match any alphnumeric string. If the page doesn't exist,
// 404
$StatTracker->get('/{page}', function ($page) use ($StatTracker) {
if ($page == "dashboard" ||
$page == "leaderboards" ||
$page == "submit-stats" ||
$page == "settings" ||
$page == "trends" ||
$page == "terms") {
$StatTracker['session']->set("page_after_login", $page);
return $StatTracker['twig']->render("index.twig", array(
"agent" => $StatTracker->getAgent(),
"constants" => array(
"ga_id" => StatTracker::getConstant("GOOGLE_ANALYTICS_ID"),
"admin_agent" => StatTracker::getConstant("ADMIN_AGENT"),
"contributors" => $StatTracker->getContributors(),
"debug" => StatTracker::getConstant("DEBUG", false),
"group_name" => StatTracker::getConstant("GROUP_NAME"),
"version" => StatTracker::getConstant("VERSION", "bleeding edge"),
),
"stats" => $StatTracker->getStats(),
"page" => $page
));
}
else if ($page == "logout") {
$StatTracker->getAuthenticationProvider()->logout($StatTracker);
return $StatTracker->redirect("./");
}
else if ($page == "authenticate") {
switch ($_REQUEST['action']) {
case "login":
$authResponse = $StatTracker->getAuthenticationProvider()->login($StatTracker);
if ($authResponse->status == "registration_required") {
$StatTracker->sendRegistrationEmail($authResponse->email);
}
return $StatTracker->json($authResponse);
break;
case "callback":
$StatTracker->getAuthenticationProvider()->callback($StatTracker);
$StatTracker->getAuthenticationProvider()->login($StatTracker);
$page = $StatTracker['session']->get("page_after_login");
$page = empty($page) ? "dashboard" : $page;
return $StatTracker->redirect("./{$page}");
break;
case "logout":
return $StatTracker->json($StatTracker->getAuthenticationProvider()->logout($StatTracker));
break;
default:
$StatTracker->abort(400, "Invalid authentication action");
}
}
else {
$StatTracker->abort(404);
}
})->assert('page', '[a-z-]+')
->value('page', 'dashboard');
$StatTracker->get('/page/{page}', function(Request $request, $page) use ($StatTracker) {
$page_parameters = array();
if ($page == "submit-stats") {
$date = $request->get("date");
$date = $StatTracker->isValidDate($date) ? $date : null;
if ($date == null || new DateTime() < new DateTime($date)) {
$StatTracker->getAgent()->getStats("latest", true);
$date = date("Y-m-d");
}
else {
$StatTracker->getAgent()->getStats($date, true);
}
$page_parameters['date'] = $date;
}
return $StatTracker['twig']->render($page.".twig", array(
"agent" => $StatTracker->getAgent(),
"constants" => array(
"admin_agent" => StatTracker::getConstant("ADMIN_AGENT"),
"email_submission" => StatTracker::getConstant("EMAIL_SUBMISSION")
),
"stats" => $StatTracker->getStats(),
"faction_class" => $StatTracker->getAgent()->faction == "R" ? "resistance-agent" : "enlightened-agent",
"faction_color" => $StatTracker->getAgent()->faction == "R" ? RES_BLUE : ENL_GREEN,
"parameters" => $page_parameters
));
});
$StatTracker->get("/resources/{resource_dir}/{resource}", function(Request $request, $resource) use ($StatTracker) {
switch ($resource) {
case "style.css":
$file = "./resources/css/style.less";
$lastModified = filemtime($file);
$css = new Symfony\Component\HttpFoundation\Response("", 200, array("Content-Type" => "text/css"));
$css->setLastModified(new \DateTime("@".filemtime($file)));
if ($css->isNotModified($request)) {
$css->setNotModified();
}
else {
$parser = new Less_Parser(array("compress" => true));
$parser->parseFile($file, $request->getBaseUrl());
$css->setLastModified(new \DateTime("@".filemtime($file)));
$css->setContent($parser->getCss());
}
return $css;
break;
case "stat-tracker.js":
$js = new Symfony\Component\HttpFoundation\Response();
if ($js->isNotModified($request)) {
$js->setNotModified();
}
else {
$content = $StatTracker['twig']->render("stat-tracker.js.twig");
$js->headers->set("Content-Type", "application/javascript");
$js->setContent($content);
}
return $js;
break;
}
});
$StatTracker->run();
?>