-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.php
209 lines (179 loc) · 6.05 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
<?php
// Since we do lots of date/time stuff, set default timezone
date_default_timezone_set('America/New_York');
// Setup constants
// Wodk Web App constants
define('SITE_ROOT', __DIR__);
define('CACHE_DIR', SITE_ROOT . '/views/cache');
define('TEMPLATE_DIR', SITE_ROOT . '/views/templates');
define('SITE_NAME', 'Junkies');
define('WODK_LOG', SITE_ROOT . '/web_app.log');
define('WODK_BASE_URI', '/junkies/');
define('FORBIDDEN', 403);
// Football Challenge constants
define('FC_LOG', SITE_ROOT . '/football-challenge.log');
define('FC_COOKIE', 'football-challenge');
define('FC_YEAR', 2014);
define('FC_NUM_WEEKS', 15);
define('FC_DEFAULT_VALUE', 9999);
define('FC_NUM_CHALLENGES', 10);
// How much to truncate the Messages when a new challenge is created.
// 0 - No messages truncated
// 1 - Messages from last week (and older) are truncated
// 2 - Messages from two weeks ago (and older) are truncated
// ...
define('FC_MSGS_TRUNCATE', 1);
// Get the Wodk Library (DB, Logger and TwigExtensions)
// Get our templating engine Twig
// Get the micro-framework Limonade
require_once 'vendor/autoload.php';
// Autoload our controllers
require_once 'controllers/AppController.php';
AppController::register();
// Get our routes
require_once 'routes.php';
// Global helpers
function get_post($var) {
if (isset($_POST[$var])) {
return $_POST[$var];
} else if (isset($GLOBALS['_JSON']->$var)) {
return $GLOBALS['_JSON']->$var;
} else {
$json = json_decode(file_get_contents('php://input'));
if (isset($json)) {
if (isset($json->$var)) {
return $json->$var;
}
}
}
return NULL;
}
function get_flash_messages($all) {
$errs = array();
$msgs = array();
foreach ($all as $type => $msg) {
if (strpos($type, 'error') !== FALSE) {
array_push($errs, $msg);
} else if (strpos($type, 'message') !== FALSE) {
array_push($msgs, $msg);
}
}
option('have_flash_errors', count($errs) ? TRUE : FALSE);
option('flash_errors', $errs);
option('have_flash_messages', count($msgs) ? TRUE : FALSE);
option('flash_messages', $msgs);
return array('errors' => $errs, 'messages' => $msgs);
}
// Limonade
function configure() {
// Reset the signature
option('signature', 'Wodk Football Challenge');
// Setup logging
$log = new Wodk_Logger(WODK_LOG);
option('log', $log);
// Setup environment
$env = trim(file_get_contents('ENVIRONMENT.txt')) === 'prod' ? ENV_PRODUCTION : ENV_DEVELOPMENT;
option('env', $env);
option('base_uri', WODK_BASE_URI);
option('site_name', SITE_NAME);
// Setup database
$db_config = $env === ENV_PRODUCTION ? 'db-prod.php' : 'db-dev.php';
require_once($db_config);
$db = new Wodk_DB(DB_HOST, DB_USER, DB_PASS, DB_NAME, DB_PORT, DB_SOCK);
option('db', $db->setPrefix(DB_PFIX));
// Setup template engine
$cache = $env === ENV_PRODUCTION ? CACHE_DIR : FALSE;
$loader = new Twig_Loader_Filesystem(TEMPLATE_DIR);
$twig = new Twig_Environment($loader, array(
'cache' => $cache,
));
$twig->getExtension('core')->setTimezone('America/New_York');
$twig->addExtension(new Wodk_TwigExtensions());
option('twig', $twig);
/*
* Setup other application configurations
*/
// Challenge Weeks
$challenge_weeks = array();
if ($result = $db->qry('SELECT DISTINCT week FROM {{challenges}} WHERE year = %s', FC_YEAR)) {
while ($obj = $result->fetch_object()) {
$challenge_weeks[] = array(
'num' => $obj->week,
'path' => '/picks-week/' . $obj->week,
'name' => 'Week ' . $obj->week,
);
}
}
$challenge_weeks_len = count($challenge_weeks);
if ($challenge_weeks_len > 0) {
$challenge_weeks[$challenge_weeks_len - 1]['path'] = '/week/' . $challenge_weeks[$challenge_weeks_len - 1]['num'];
}
option('challenge_weeks', $challenge_weeks);
// Challenge Week
option('challenge_week', $challenge_weeks_len);
// Standings Week
$standings_week = 0;
if ($result = $db->qry('SELECT DISTINCT week FROM {{challenges}} WHERE year = %s AND winner_sid > 0 ORDER BY week DESC LIMIT 1', FC_YEAR)) {
while ($obj = $result->fetch_object()) {
$standings_week = $obj->week;
}
}
option('standings_week', $standings_week);
// User info
$empty_user = array(
'use' => FALSE,
'uid' => FALSE,
'name' => FALSE,
'perms' => FALSE,
);
option('empty_user', $empty_user);
$user_info = $empty_user;
if (isset($_COOKIE[FC_COOKIE])) {
$str = $_COOKIE[FC_COOKIE];
$str = explode('|', $str);
$user_info = array(
'use' => TRUE,
'uid' => $str[2],
'name' => $str[0],
'perms' => $str[1],
);
}
option('user_info', $user_info);
// Get all users simple info
$all_users = array();
if ($users = $db->qry('SELECT uid, username FROM {{users}}')) {
while ($user = $users->fetch_object()) {
$all_users[] = $user;
}
} else {
$log->log('error', 'Trying to select users while in configure()', $db->error);
}
option('all_users', $all_users);
option('build_version', file_get_contents('BUILD_VERSION.txt'));
option('app_version', file_get_contents('VERSION.txt'));
}
function before() {
// Load flash
get_flash_messages(flash_now());
/*
* Other application tasks
*/
// Footer
$db = option('db');
$footer = array();
if ($result = $db->qry('SELECT username FROM {{users}} WHERE active = 1 ORDER BY username')) {
while ($obj = $result->fetch_object()) {
$footer[] = array(
'name' => $obj->username,
'path' => '/picks/' . strtolower($obj->username),
);
}
}
option('footer', $footer);
}
function before_exit($exit) {
$db = option('db');
$db->close();
}
// Start app
run();