-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.php
More file actions
283 lines (238 loc) · 9.21 KB
/
Copy pathindex.php
File metadata and controls
283 lines (238 loc) · 9.21 KB
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
<?php
// load packages
require_once('./vendor/autoload.php');
// bring in Fat Free
$f3 = \Base::instance();
// load phpdotenv
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();
if ( ( $_ENV['ENV'] ?? '' ) === 'MAINTENANCE') {
$custom_maintenance_template = __DIR__ . '/data/maintenance.html';
if ( is_readable( $custom_maintenance_template ) ) {
readfile( $custom_maintenance_template );
} else {
echo \Template::instance()->render( 'templates/maintenance.html' );
}
return;
}
// require POSTMARK_SECRET
$dotenv->required( 'POSTMARK_SECRET' );
// set default debug level
$debug_level = $_ENV['DEBUG'] ?? '';
$debug_level = $debug_level ? $debug_level : 0;
$f3->set( 'DEBUG', $debug_level );
$f3->set( 'AUTOLOAD', 'classes/models/; classes/controllers/; classes/utils/' );
$db = new \DB\SQL( 'sqlite:data/db.sqlite3', null, null, [
\PDO::ATTR_TIMEOUT => 5,
] );
$db->exec( 'PRAGMA busy_timeout=5000' );
$db->exec( 'PRAGMA synchronous=NORMAL' );
$db->exec( 'PRAGMA wal_autocheckpoint=1000' );
$f3->set( 'DB', $db );
$f3->set( 'CACHE', 'folder=tmp/cache/' );
// Allow only admins to access the site?
$f3->set( 'admin_only', ($_ENV['ADMIN_ONLY'] ?? null) === '1' );
// allow signups only if ALLOW_SIGNUPS and not ADMIN_ONLY
$f3->set( 'allow_signups', ($_ENV['ALLOW_SIGNUPS'] ?? null) === '1' && !$f3->get( 'admin_only' ));
// set the font awesome kit URL from env for icon loading
$f3->set( 'font_awesome_kit_url', $_ENV['FONT_AWESOME_KIT_URL'] ?? '' );
// homepage
$f3->route( 'GET /', function( $f3 ) {
$f3->set( 'lightbox', true );
echo \Template::instance()->render( 'templates/home.html' );
} );
// dashboard
$f3->route( 'GET /dashboard', 'Dashboard->sheet_list' );
$f3->route( 'GET|POST /add-sheet', 'Dashboard->add_sheet' );
$f3->route( 'POST /make-public/@sheet_slug', 'Dashboard->make_sheet_public' );
$f3->route( 'POST /import-sheet', 'Dashboard->import_sheet' );
// sheet view
$f3->route( 'GET /sheet/@sheet_slug', 'Dashboard->sheet_single' );
$f3->route( 'POST /sheet/@sheet_slug', 'Dashboard->save_sheet' );
$f3->route( 'DELETE /sheet/@sheet_slug', 'Dashboard->delete_sheet' );
// read-only public sheet refresh. This endpoint receives a request for a sheet
// and provides an updated date if it exists. The endpoint returns sheet data
// only if the sheet has been updated.
$f3->route( 'GET /sheet-data/@sheet_slug', 'Dashboard->get_sheet_data' );
$f3->route( 'GET /print/@sheet_slug', 'Dashboard->print_sheet' );
// random.org proxy (keeps API key server-side)
$f3->route( 'POST /api/random', function( $f3 ) {
header( 'Content-Type: application/json' );
// initialize DB-backed session so we can check auth
new \DB\SQL\Session( $f3->get( 'DB' ), 'sessions', true, function( $sess ) { return true; } );
// auth check
$auth = new Authentication( $f3 );
$email = $auth->get_logged_in_email();
if ( $email === '' ) {
$f3->status( 401 );
echo json_encode([ 'success' => false, 'reason' => 'unauthorized' ]);
return;
}
// rate limit: 10 requests per 60 seconds per IP
$cache = \Cache::instance();
$ip = $f3->get( 'IP' );
$cacheKey = 'ratelimit_random_' . md5( $ip );
$limit = 10;
$window = 60;
if ( $cache->exists( $cacheKey, $count ) ) {
if ( $count >= $limit ) {
$f3->status( 429 );
echo json_encode([ 'success' => false, 'reason' => 'rate_limited' ]);
return;
}
$cache->set( $cacheKey, $count + 1, $window );
} else {
$cache->set( $cacheKey, 1, $window );
}
// check API key is configured
$apiKey = $_ENV['RANDOM_ORG_API_KEY'] ?? '';
if ( ! $apiKey ) {
$f3->status( 503 );
echo json_encode([ 'success' => false, 'reason' => 'not_configured' ]);
return;
}
// parse and validate request body
$body = json_decode( file_get_contents( 'php://input' ), true );
if ( ! $body ) {
$f3->status( 400 );
echo json_encode([ 'success' => false, 'reason' => 'invalid_json' ]);
return;
}
$n = $body['n'] ?? null;
$min = $body['min'] ?? null;
$max = $body['max'] ?? null;
$allowedMax = [ 4, 6, 8, 10, 12, 20, 100 ];
if ( ! is_int( $n ) || $n < 1 || $n > 100 ) {
$f3->status( 400 );
echo json_encode([ 'success' => false, 'reason' => 'invalid_n' ]);
return;
}
if ( ! is_int( $min ) || $min !== 1 ) {
$f3->status( 400 );
echo json_encode([ 'success' => false, 'reason' => 'invalid_min' ]);
return;
}
if ( ! is_int( $max ) || ! in_array( $max, $allowedMax, true ) ) {
$f3->status( 400 );
echo json_encode([ 'success' => false, 'reason' => 'invalid_max' ]);
return;
}
// proxy to RANDOM.ORG v4 API
$payload = json_encode([
'jsonrpc' => '2.0',
'method' => 'generateIntegers',
'params' => [
'apiKey' => $apiKey,
'n' => $n,
'min' => $min,
'max' => $max,
'replacement' => true,
'base' => 10,
],
'id' => 1,
]);
$ch = curl_init( 'https://api.random.org/json-rpc/4/invoke' );
curl_setopt_array( $ch, [
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $payload,
CURLOPT_HTTPHEADER => [ 'Content-Type: application/json' ],
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 10,
]);
$response = curl_exec( $ch );
$httpCode = curl_getinfo( $ch, CURLINFO_HTTP_CODE );
curl_close( $ch );
if ( $httpCode !== 200 || ! $response ) {
$f3->status( 502 );
echo json_encode([ 'success' => false, 'reason' => 'upstream_error' ]);
return;
}
$result = json_decode( $response, true );
if ( ! isset( $result['result']['random']['data'] ) ) {
$f3->status( 502 );
echo json_encode([ 'success' => false, 'reason' => 'unexpected_response' ]);
return;
}
echo json_encode([
'success' => true,
'data' => $result['result']['random']['data'],
]);
});
// login/logout
$f3->route( 'GET /login', 'Authentication->login_form' );
$f3->route( 'POST /login', 'Authentication->login' );
$f3->route( 'GET /logout', 'Authentication->logout' );
// registration
if ($f3->get('allow_signups')) {
$f3->route( 'GET /register/confirm/@email/@clear_token', 'Authentication->confirm' );
$f3->route( 'POST /register/confirm/@email/@clear_token', 'Authentication->confirm' );
$f3->route( 'GET /register/confirm/send', 'Authentication->resend_confirmation_form' );
$f3->route( 'POST /register/confirm/send', 'Authentication->resend_confirmation' );
$f3->route( 'GET /register', 'Authentication->registration_form' );
$f3->route( 'POST /register', 'Authentication->register' );
}
// password reset
$f3->route( 'GET /request-password-reset', 'Authentication->request_password_reset_form' );
$f3->route( 'POST /request-password-reset', 'Authentication->request_password_reset' );
$f3->route( 'GET /password-reset/@email/@clear_token', 'Authentication->password_reset_form' );
$f3->route( 'POST /password-reset', 'Authentication->password_reset' );
// admin dashboard
$f3->route( 'GET /admin', 'Admin->dashboard' );
$f3->route( 'GET /admin/posts', 'Admin->posts' );
$f3->route( 'GET|POST /admin/posts/create', 'Admin->post_create' );
$f3->route( 'GET|POST /admin/posts/edit/@id', 'Admin->post_edit' );
$f3->route( 'POST /admin/posts/delete/@id', 'Admin->post_delete' );
$f3->route( 'GET /admin/banners', 'Admin->banners' );
$f3->route( 'GET|POST /admin/banners/create', 'Admin->banner_create' );
$f3->route( 'GET|POST /admin/banners/edit/@id', 'Admin->banner_edit' );
$f3->route( 'POST /admin/banners/delete/@id', 'Admin->banner_delete' );
$f3->route( 'GET /admin/users', 'Admin->users' );
$f3->route( 'GET /admin/stats', 'Admin->stats' );
$f3->route( 'GET|POST /admin/restore-sheet', 'Admin->restore_sheet' );
// set up custom error page
$f3->set( 'ONERROR', function( $f3, $params ) {
echo \Template::instance()->render( 'templates/error.html' );
} );
/**
* Helper functions
* Move these to a separate helpers.php file if the list grows.
*/
/**
* Returns a versioned asset path using Vite's manifest file.
* Used in templates for cache busting: {{ vite('js/app.js') }}
*/
function vite($entry) {
static $manifest;
if (!$manifest) {
$manifestPath = __DIR__ . '/dist/.vite/manifest.json';
if (file_exists($manifestPath)) {
$manifest = json_decode(file_get_contents($manifestPath), true);
} else {
$manifest = [];
}
}
if (isset($manifest[$entry])) {
return '/dist/' . $manifest[$entry]['file'];
}
// Fallback: return the path as-is (for non-manifest assets)
return '/dist/' . $entry;
}
/**
* Returns companion CSS paths for a JS entry from the Vite manifest.
*/
function viteCssFromJs($entry) {
static $manifest;
if (!$manifest) {
$manifestPath = __DIR__ . '/dist/.vite/manifest.json';
if (file_exists($manifestPath)) {
$manifest = json_decode(file_get_contents($manifestPath), true);
} else {
$manifest = [];
}
}
if (isset($manifest[$entry]['css'])) {
return array_map(fn($path) => '/dist/' . $path, $manifest[$entry]['css']);
}
return [];
}
$f3->run();