-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.php
More file actions
740 lines (657 loc) · 28.3 KB
/
auth.php
File metadata and controls
740 lines (657 loc) · 28.3 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
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
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
<?php
/**
* Course Community — Standalone Auth
*
* Handles enrollment via invite codes and passwordless magic-link login.
* Routed from index.php and (under Apache) directly via .htaccess.
*
* Routes:
* GET /join — enroll form (email + invite code)
* POST /join — process enrollment
* GET /login — magic-link request form
* POST /login — send magic link
* GET /auth/magic?token=x — redeem magic link
* GET /auth/course-picker — choose course (multi-course users)
* POST /auth/select-course — create session for chosen course
*/
require_once __DIR__ . '/config.php';
require_once __DIR__ . '/db.php';
// When included by another file (e.g. admin.php) skip routing — just load functions.
if (!defined('AUTH_FUNCTIONS_ONLY')) {
// ── Early request log — confirms auth.php was reached ────────────────────────
// Writes to data/auth.log; falls back to sys_get_temp_dir() if data/ isn't writable.
(function () {
$line = date('Y-m-d H:i:s') . ' REQUEST '
. ($_SERVER['REQUEST_METHOD'] ?? '?') . ' '
. ($_SERVER['REQUEST_URI'] ?? '?') . PHP_EOL;
$primary = __DIR__ . '/data/auth.log';
$fallback = sys_get_temp_dir() . '/cc-auth.log';
if (@file_put_contents($primary, $line, FILE_APPEND | LOCK_EX) === false) {
@file_put_contents($fallback, $line, FILE_APPEND | LOCK_EX);
// If neither works, surface it in the system error log
error_log('[auth] Could not write to ' . $primary . ' or ' . $fallback
. ' — check directory permissions. Request: ' . trim($line));
}
})();
// Determine the path relative to APP_URL
$_authUri = $_SERVER['REQUEST_URI'] ?? '/';
$_authUri = parse_url($_authUri, PHP_URL_PATH);
$_authUri = rtrim($_authUri, '/') ?: '/';
$_authBasePath = rtrim(parse_url(APP_URL, PHP_URL_PATH) ?? '', '/');
if ($_authBasePath && str_starts_with($_authUri, $_authBasePath)) {
$_authUri = substr($_authUri, strlen($_authBasePath)) ?: '/';
}
$_authMethod = $_SERVER['REQUEST_METHOD'];
// ── Route dispatch ─────────────────────────────────────────────────────────────
// Supports both clean URLs (via mod_rewrite through index.php) and direct
// ?action= parameters (when accessing auth.php directly without mod_rewrite).
//
// Clean URL ↔ Direct URL
// /join ↔ auth.php?action=join
// /login ↔ auth.php?action=login
// /auth/magic ↔ auth.php?action=magic
// /auth/course-picker↔ auth.php?action=picker
// /auth/select-course↔ auth.php?action=select (POST)
$_authAction = $_GET['action'] ?? '';
if ($_authUri === '/join' || $_authAction === 'join') { $_route = 'join'; }
elseif ($_authUri === '/login' || $_authAction === 'login') { $_route = 'login'; }
elseif ($_authUri === '/auth/magic' || $_authAction === 'magic') { $_route = 'magic'; }
elseif ($_authUri === '/auth/course-picker' || $_authAction === 'picker') { $_route = 'picker'; }
elseif ($_authUri === '/auth/select-course' || $_authAction === 'select') { $_route = 'select'; }
else { $_route = 'notfound'; }
match ($_route) {
'join' => $_authMethod === 'POST' ? authHandleJoin() : authShowJoin(),
'login' => $_authMethod === 'POST' ? authHandleLogin() : authShowLogin(),
'magic' => authHandleMagic(),
'picker' => authShowCoursePicker(),
'select' => authHandleSelectCourse(),
default => authPage('Not Found', '<p class="auth-error">Page not found.</p>'),
};
} // end if (!defined('AUTH_FUNCTIONS_ONLY'))
// ── Page wrapper ──────────────────────────────────────────────────────────────
function authPage(string $title, string $bodyHtml): never {
$appName = APP_NAME;
$appUrl = APP_URL;
echo <<<HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{$title} — {$appName}</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Fraunces:ital,opsz,wght@0,9..144,300..700;1,9..144,300..500&family=Plus+Jakarta+Sans:wght@400;500;600;700&display=swap" rel="stylesheet">
<style>
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
:root {
--bg: #F2F0EA;
--surface: #FFFFFF;
--sidebar: #1C2035;
--border: #E3DFD5;
--text: #1A1D2E;
--muted: #5E6278;
--accent: #C84B10;
--accent-t: #FDF1EC;
--error-t: #FDECEA;
--error: #C0392B;
--success-t:#EDF7F2;
--success: #2E7D52;
--r: 10px;
--font-body: 'Plus Jakarta Sans', system-ui, sans-serif;
--font-disp: 'Fraunces', Georgia, serif;
}
body {
font-family: var(--font-body);
background: var(--bg);
color: var(--text);
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
padding: 24px 16px;
}
.auth-card {
background: var(--surface);
border: 1px solid var(--border);
border-radius: var(--r);
padding: 40px 40px 36px;
width: 100%;
max-width: 420px;
box-shadow: 0 4px 24px rgba(28,32,53,0.08);
}
.auth-logo {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 28px;
color: var(--sidebar);
font-size: 13px;
font-weight: 600;
letter-spacing: 0.01em;
}
.auth-logo .mark {
font-size: 20px;
color: var(--accent);
}
.auth-title {
font-family: var(--font-disp);
font-size: 22px;
font-weight: 600;
color: var(--text);
margin-bottom: 6px;
line-height: 1.2;
}
.auth-subtitle {
font-size: 13px;
color: var(--muted);
margin-bottom: 28px;
line-height: 1.5;
}
.auth-field { margin-bottom: 18px; }
.auth-field label {
display: block;
font-size: 12px;
font-weight: 600;
color: var(--muted);
text-transform: uppercase;
letter-spacing: 0.07em;
margin-bottom: 6px;
}
.auth-field input {
width: 100%;
padding: 10px 14px;
border: 1.5px solid var(--border);
border-radius: 7px;
font-size: 15px;
font-family: var(--font-body);
color: var(--text);
background: #FAFAF8;
outline: none;
transition: border-color 0.15s;
}
.auth-field input:focus { border-color: var(--accent); background: #fff; }
.auth-btn {
width: 100%;
padding: 12px;
background: var(--accent);
color: #fff;
border: none;
border-radius: 7px;
font-size: 15px;
font-weight: 600;
font-family: var(--font-body);
cursor: pointer;
margin-top: 4px;
transition: opacity 0.15s;
}
.auth-btn:hover { opacity: 0.88; }
.auth-error {
background: var(--error-t);
border: 1px solid rgba(192,57,43,0.25);
color: var(--error);
padding: 10px 14px;
border-radius: 7px;
font-size: 13px;
margin-bottom: 18px;
line-height: 1.5;
}
.auth-success {
background: var(--success-t);
border: 1px solid rgba(46,125,82,0.25);
color: var(--success);
padding: 10px 14px;
border-radius: 7px;
font-size: 13px;
margin-bottom: 18px;
line-height: 1.5;
}
.auth-link {
display: block;
text-align: center;
font-size: 13px;
color: var(--muted);
margin-top: 20px;
}
.auth-link a { color: var(--accent); text-decoration: none; font-weight: 500; }
.auth-link a:hover { text-decoration: underline; }
.course-list { list-style: none; }
.course-list li { margin-bottom: 10px; }
.course-btn {
display: block;
width: 100%;
padding: 14px 16px;
background: var(--bg);
border: 1.5px solid var(--border);
border-radius: 8px;
text-align: left;
cursor: pointer;
font-family: var(--font-body);
font-size: 14px;
font-weight: 600;
color: var(--text);
transition: border-color 0.15s, background 0.15s;
}
.course-btn:hover { border-color: var(--accent); background: var(--accent-t); }
.course-btn small { display: block; font-size: 12px; font-weight: 400; color: var(--muted); margin-top: 2px; }
</style>
</head>
<body>
<div class="auth-card">
<div class="auth-logo">
<span class="mark">◈</span>
<span>{$appName}</span>
</div>
{$bodyHtml}
</div>
</body>
</html>
HTML;
exit;
}
// ── /join — Enrollment form ───────────────────────────────────────────────────
function authShowJoin(string $error = ''): never {
$errorHtml = $error ? '<div class="auth-error">' . htmlspecialchars($error) . '</div>' : '';
$action = htmlspecialchars($_SERVER['REQUEST_URI']);
$loginUrl = APP_URL . '/auth.php?action=login';
authPage('Join a Course', <<<HTML
<h1 class="auth-title">Join a Course</h1>
<p class="auth-subtitle">Enter your email address and the invite code shared by your instructor.</p>
{$errorHtml}
<form method="POST" action="{$action}">
<div class="auth-field">
<label for="email">Email Address</label>
<input type="email" id="email" name="email" required autofocus placeholder="you@example.com">
</div>
<div class="auth-field">
<label for="code">Invite Code</label>
<input type="text" id="code" name="code" required placeholder="e.g. ABCD1234"
maxlength="8" style="text-transform:uppercase;letter-spacing:0.1em;font-size:16px;">
</div>
<div class="auth-field">
<label for="name">Your Name</label>
<input type="text" id="name" name="name" required placeholder="First Last">
</div>
<button type="submit" class="auth-btn">Join Course</button>
</form>
<p class="auth-link">Already joined? <a href="{$loginUrl}">Sign in via email</a></p>
HTML);
}
function authHandleJoin(): never {
$email = strtolower(trim($_POST['email'] ?? ''));
$code = strtoupper(trim($_POST['code'] ?? ''));
$name = trim($_POST['name'] ?? '');
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
authShowJoin('Please enter a valid email address.');
}
if (strlen($code) !== 8) {
authShowJoin('Invite code must be 8 characters.');
}
if (!$name) {
authShowJoin('Please enter your name.');
}
// Look up invite code
$invite = dbOne(
"SELECT * FROM course_invite_codes
WHERE code = ? AND is_active = 1
AND (expires_at IS NULL OR expires_at > ?)
AND (max_uses IS NULL OR use_count < max_uses)",
[$code, time()]
);
if (!$invite) {
authShowJoin('That invite code is invalid, expired, or has reached its usage limit.');
}
$courseId = (int)$invite['course_id'];
$role = $invite['role'];
// Upsert user (issuer='standalone', sub=email)
$existing = dbOne("SELECT id FROM users WHERE sub = ? AND issuer = 'standalone'", [$email]);
if ($existing) {
$userId = (int)$existing['id'];
// Update name if blank
dbRun("UPDATE users SET name = CASE WHEN name = '' THEN ? ELSE name END WHERE id = ?", [$name, $userId]);
} else {
$parts = explode(' ', $name, 2);
$given = $parts[0];
$family = $parts[1] ?? '';
$userId = dbExec(
"INSERT INTO users (sub, issuer, name, given_name, family_name, email)
VALUES (?, 'standalone', ?, ?, ?, ?)",
[$email, $name, $given, $family, $email]
);
}
// Enroll (upsert)
$enrolled = dbOne('SELECT id, role FROM enrollments WHERE user_id = ? AND course_id = ?', [$userId, $courseId]);
if (!$enrolled) {
dbExec(
'INSERT INTO enrollments (user_id, course_id, role) VALUES (?, ?, ?)',
[$userId, $courseId, $role]
);
}
// Increment code use count
dbRun('UPDATE course_invite_codes SET use_count = use_count + 1 WHERE id = ?', [(int)$invite['id']]);
// Ensure default spaces exist
ensureDefaultSpaces($courseId);
// Create 30-day session
authCreateSession($userId, $courseId, $enrolled['role'] ?? $role);
header('Location: ' . APP_URL . '/');
exit;
}
// ── /login — Magic link request ───────────────────────────────────────────────
function authShowLogin(string $error = '', string $success = ''): never {
$errorHtml = $error ? '<div class="auth-error">' . htmlspecialchars($error) . '</div>' : '';
$successHtml = $success ? '<div class="auth-success">' . htmlspecialchars($success) . '</div>' : '';
$action = htmlspecialchars($_SERVER['REQUEST_URI']);
$joinUrl = APP_URL . '/auth.php?action=join';
authPage('Sign In', <<<HTML
<h1 class="auth-title">Sign In</h1>
<p class="auth-subtitle">Enter your email and we'll send you a sign-in link. No password needed.</p>
{$errorHtml}{$successHtml}
<form method="POST" action="{$action}">
<div class="auth-field">
<label for="email">Email Address</label>
<input type="email" id="email" name="email" required autofocus placeholder="you@example.com">
</div>
<button type="submit" class="auth-btn">Send Sign-In Link</button>
</form>
<p class="auth-link">New here? Get an <a href="{$joinUrl}">invite code</a> from your instructor.</p>
HTML);
}
function authHandleLogin(): never {
$email = strtolower(trim($_POST['email'] ?? ''));
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
authShowLogin('Please enter a valid email address.');
}
// Look up standalone user
$user = dbOne("SELECT * FROM users WHERE sub = ? AND issuer = 'standalone'", [$email]);
if (!$user) {
// Don't reveal whether the user exists — show the same success message
authShowLogin('', 'If that email is registered, a sign-in link has been sent. Check your inbox.');
}
$userId = (int)$user['id'];
// Find the user's enrolled standalone courses
$courses = dbAll(
"SELECT c.id FROM enrollments e
JOIN courses c ON c.id = e.course_id
WHERE e.user_id = ? AND c.course_type = 'standalone'",
[$userId]
);
$courseId = count($courses) === 1 ? (int)$courses[0]['id'] : null;
// Create a 15-minute magic token
$token = bin2hex(random_bytes(24)); // 48-char hex
dbExec(
'INSERT INTO local_auth_tokens (token, user_id, course_id, expires_at)
VALUES (?, ?, ?, ?)',
[$token, $userId, $courseId, time() + 900]
);
$link = APP_URL . '/auth.php?action=magic&token=' . urlencode($token);
authSendMagicLink($email, $user['name'], $link);
// In dev mode, show the link on-screen so testing doesn't require working email
if (DEV_MODE) {
$safeLink = htmlspecialchars($link);
authPage('Sign In — Dev Mode', <<<HTML
<h1 class="auth-title">Dev Mode: Magic Link</h1>
<p class="auth-subtitle">Email is not sent in dev mode. Click the link below to sign in:</p>
<div style="background:var(--bg);border:1.5px solid var(--border);border-radius:8px;padding:14px 16px;word-break:break-all;font-size:13px;margin-bottom:18px;">
<a href="{$safeLink}" style="color:var(--accent);text-decoration:none;">{$safeLink}</a>
</div>
<p class="auth-link"><a href="{$safeLink}" class="auth-btn" style="display:block;text-align:center;text-decoration:none;">Sign in now →</a></p>
HTML);
}
authShowLogin('', 'Sign-in link sent! Check your inbox (and spam folder). The link expires in 15 minutes.');
}
// ── /auth/magic — Redeem magic link ──────────────────────────────────────────
function authHandleMagic(): never {
// Token arrives via GET (link click) or POST (confirmation button).
// We intentionally do NOT consume the token on GET so that email security
// scanners (Outlook SafeLinks, Proofpoint, etc.) that pre-fetch URLs cannot
// burn the one-time token before the user actually clicks.
$token = trim($_GET['token'] ?? $_POST['token'] ?? '');
if (!$token) {
authPage('Invalid Link', '<div class="auth-error">Missing token. Please request a new sign-in link.</div>
<p class="auth-link"><a href="' . APP_URL . '/auth.php?action=login">Back to sign in</a></p>');
}
$row = dbOne(
'SELECT * FROM local_auth_tokens WHERE token = ? AND expires_at > ? AND used_at IS NULL',
[$token, time()]
);
if (!$row) {
authPage('Link Expired', '<div class="auth-error">This sign-in link has expired or already been used. Please request a new one.</div>
<p class="auth-link"><a href="' . APP_URL . '/auth.php?action=login">Request a new link</a></p>');
}
// GET: show a confirmation page. This prevents email scanners from consuming
// the token — they follow GET links but do not submit forms.
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
$safeToken = htmlspecialchars($token);
$action = htmlspecialchars(APP_URL . '/auth.php?action=magic');
$appName = htmlspecialchars(APP_NAME);
authPage('Sign In', <<<HTML
<h1 class="auth-title">Sign in to {$appName}</h1>
<p style="text-align:center;color:var(--text-muted);margin-bottom:1.5rem">
Click the button below to complete your sign-in.
</p>
<form method="POST" action="{$action}">
<input type="hidden" name="token" value="{$safeToken}">
<button type="submit" class="btn-auth-submit">Sign in →</button>
</form>
HTML);
}
// POST: consume token and create session
dbRun('UPDATE local_auth_tokens SET used_at = ? WHERE token = ?', [time(), $token]);
$userId = (int)$row['user_id'];
$courseId = $row['course_id'] ? (int)$row['course_id'] : null;
if ($courseId) {
// Single course — create session immediately
$enrollment = dbOne('SELECT role FROM enrollments WHERE user_id = ? AND course_id = ?', [$userId, $courseId]);
if (!$enrollment) {
authPage('Access Error', '<div class="auth-error">You are not enrolled in that course. Please use a valid invite code to join.</div>
<p class="auth-link"><a href="' . APP_URL . '/auth.php?action=join">Join with invite code</a></p>');
}
authCreateSession($userId, $courseId, $enrollment['role']);
header('Location: ' . APP_URL . '/');
exit;
}
// Multiple courses — show picker via session-less cookie
$pickerToken = bin2hex(random_bytes(24));
dbExec(
'INSERT INTO local_auth_tokens (token, user_id, course_id, expires_at)
VALUES (?, ?, NULL, ?)',
[$pickerToken, $userId, time() + 600] // 10-minute picker session
);
$cookiePath = rtrim(parse_url(APP_URL, PHP_URL_PATH) ?: '/', '/') . '/';
setcookie('cc_picker', $pickerToken, [
'expires' => time() + 600,
'path' => $cookiePath,
'secure' => !DEV_MODE,
'httponly' => true,
'samesite' => 'Lax',
]);
header('Location: ' . APP_URL . '/auth.php?action=picker');
exit;
}
// ── /auth/course-picker — Choose a course ────────────────────────────────────
function authShowCoursePicker(): never {
$pickerToken = $_COOKIE['cc_picker'] ?? '';
if (!$pickerToken) {
header('Location: ' . APP_URL . '/auth.php?action=login');
exit;
}
$row = dbOne(
'SELECT * FROM local_auth_tokens WHERE token = ? AND expires_at > ? AND used_at IS NULL',
[$pickerToken, time()]
);
if (!$row) {
header('Location: ' . APP_URL . '/auth.php?action=login');
exit;
}
$userId = (int)$row['user_id'];
$courses = dbAll(
"SELECT c.id, c.title, c.label, e.role
FROM enrollments e
JOIN courses c ON c.id = e.course_id
WHERE e.user_id = ? AND c.course_type = 'standalone'
ORDER BY c.title",
[$userId]
);
if (empty($courses)) {
authPage('No Courses', '<div class="auth-error">You are not enrolled in any courses.</div>
<p class="auth-link"><a href="' . APP_URL . '/join">Join with invite code</a></p>');
}
$items = '';
foreach ($courses as $c) {
$label = $c['label'] ? htmlspecialchars($c['label']) . ' — ' : '';
$role = ucfirst($c['role']);
$items .= '<li><form method="POST" action="' . APP_URL . '/auth/select-course">'
. '<input type="hidden" name="course_id" value="' . (int)$c['id'] . '">'
. '<button type="submit" class="course-btn">'
. $label . htmlspecialchars($c['title'])
. '<small>' . $role . '</small>'
. '</button></form></li>';
}
authPage('Choose Course', <<<HTML
<h1 class="auth-title">Choose a Course</h1>
<p class="auth-subtitle">You are enrolled in multiple courses. Which one would you like to open?</p>
<ul class="course-list">{$items}</ul>
HTML);
}
// ── /auth/select-course — POST: create session for chosen course ──────────────
function authHandleSelectCourse(): never {
$pickerToken = $_COOKIE['cc_picker'] ?? '';
$courseId = (int)($_POST['course_id'] ?? 0);
if (!$pickerToken || !$courseId) {
header('Location: ' . APP_URL . '/auth.php?action=login');
exit;
}
$row = dbOne(
'SELECT * FROM local_auth_tokens WHERE token = ? AND expires_at > ? AND used_at IS NULL',
[$pickerToken, time()]
);
if (!$row) {
header('Location: ' . APP_URL . '/auth.php?action=login');
exit;
}
$userId = (int)$row['user_id'];
$enrollment = dbOne(
'SELECT role FROM enrollments WHERE user_id = ? AND course_id = ?',
[$userId, $courseId]
);
if (!$enrollment) {
header('Location: ' . APP_URL . '/auth.php?action=picker');
exit;
}
// Mark picker token used
dbRun('UPDATE local_auth_tokens SET used_at = ? WHERE token = ?', [time(), $pickerToken]);
// Clear picker cookie
$cookiePath = rtrim(parse_url(APP_URL, PHP_URL_PATH) ?: '/', '/') . '/';
setcookie('cc_picker', '', ['expires' => time() - 3600, 'path' => $cookiePath]);
authCreateSession($userId, $courseId, $enrollment['role']);
header('Location: ' . APP_URL . '/');
exit;
}
// ── Logging ───────────────────────────────────────────────────────────────────
function authLog(string $message): void {
$line = date('Y-m-d H:i:s') . ' ' . $message . PHP_EOL;
// Write to a local file in data/ — reliable regardless of php.ini error_log setting
$logFile = dirname(DB_PATH) . '/auth.log';
@file_put_contents($logFile, $line, FILE_APPEND | LOCK_EX);
// Also send to the system error log as a secondary destination
error_log('[auth] ' . $message);
}
// ── Helpers ───────────────────────────────────────────────────────────────────
function authCreateSession(int $userId, int $courseId, string $role): void {
dbRun('DELETE FROM sessions WHERE expires_at < ?', [time()]);
$sid = bin2hex(random_bytes(32));
dbExec(
'INSERT INTO sessions (id, user_id, course_id, role, expires_at) VALUES (?, ?, ?, ?, ?)',
[$sid, $userId, $courseId, $role, time() + STANDALONE_SESSION_DURATION]
);
$cookiePath = rtrim(parse_url(APP_URL, PHP_URL_PATH) ?: '/', '/') . '/';
setcookie('cc_session', $sid, [
'expires' => time() + STANDALONE_SESSION_DURATION,
'path' => $cookiePath,
'secure' => !DEV_MODE,
'httponly' => true,
'samesite' => 'Lax', // Lax (not None) — standalone is first-party
]);
}
function authSendMagicLink(string $email, string $name, string $link): void {
$fromName = MAIL_FROM_NAME;
$from = MAIL_FROM;
$subject = APP_NAME . ': Your sign-in link';
$textBody = "Hi {$name},\n\nClick the link below to sign in to " . APP_NAME . ".\n\n{$link}\n\n"
. "This link expires in 15 minutes and can only be used once.\n\n"
. "If you didn't request this, you can safely ignore this email.\n\n— " . APP_NAME;
// Always log the magic link — visible in data/auth.log regardless of php.ini settings
authLog("Magic link for {$email}: {$link}");
if (SMTP_HOST) {
authSendSmtp($email, $from, $fromName, $subject, $textBody);
} else {
$headers = "From: {$fromName} <{$from}>\r\n";
$headers .= "Reply-To: {$from}\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/plain; charset=UTF-8\r\n";
$headers .= "X-Mailer: PHP/" . PHP_VERSION . "\r\n";
// MAIL_EXTRA_PARAMS is intentionally empty by default. The web server user
// (www-data/apache) is usually not trusted by sendmail to set the envelope
// sender with -f, which causes silent delivery failures. Set MAIL_EXTRA_PARAMS
// to '-f noreply@yourdomain.com' in config.php only if your host supports it.
$params = MAIL_EXTRA_PARAMS !== '' ? MAIL_EXTRA_PARAMS : null;
$sent = $params !== null
? @mail($email, $subject, $textBody, $headers, $params)
: @mail($email, $subject, $textBody, $headers);
if (!$sent) {
authLog("mail() returned false for {$email} — check sendmail_path in php.ini or set SMTP_HOST in config.php");
}
}
}
/**
* Minimal SMTP sender (no external dependencies).
* Only supports STARTTLS + AUTH PLAIN (covers most SMTP relays).
*/
function authSendSmtp(string $to, string $from, string $fromName, string $subject, string $body): void {
$host = SMTP_HOST;
$port = SMTP_PORT;
$sock = @fsockopen($host, $port, $errno, $errstr, 10);
if (!$sock) {
error_log("[auth] SMTP connect failed: $errstr ($errno)");
return;
}
$read = function() use ($sock): string {
$buf = '';
while ($line = fgets($sock, 515)) {
$buf .= $line;
if ($line[3] === ' ') break;
}
return $buf;
};
$cmd = function(string $c) use ($sock, $read): string {
fwrite($sock, $c . "\r\n");
return $read();
};
$read(); // banner
$cmd('EHLO ' . ($_SERVER['HTTP_HOST'] ?? 'localhost'));
$cmd('STARTTLS');
stream_socket_enable_crypto($sock, true, STREAM_CRYPTO_METHOD_TLS_CLIENT);
$cmd('EHLO ' . ($_SERVER['HTTP_HOST'] ?? 'localhost'));
if (SMTP_USER) {
$auth = base64_encode("\0" . SMTP_USER . "\0" . SMTP_PASS);
$cmd('AUTH PLAIN ' . $auth);
}
$cmd("MAIL FROM:<{$from}>");
$cmd("RCPT TO:<{$to}>");
$cmd('DATA');
$date = date('r');
$msgId = bin2hex(random_bytes(8)) . '@' . ($_SERVER['HTTP_HOST'] ?? 'localhost');
$payload = "Date: {$date}\r\n"
. "Message-ID: <{$msgId}>\r\n"
. "From: {$fromName} <{$from}>\r\n"
. "To: {$to}\r\n"
. "Subject: {$subject}\r\n"
. "MIME-Version: 1.0\r\n"
. "Content-Type: text/plain; charset=UTF-8\r\n"
. "\r\n"
. str_replace("\n.", "\n..", $body)
. "\r\n.";
$cmd($payload);
$cmd('QUIT');
fclose($sock);
}