Skip to content

Commit 088092c

Browse files
committed
more twig work
1 parent b06495a commit 088092c

16 files changed

+267
-206
lines changed

auth.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ function create_session($user) {
3737

3838
if (isset($_GET['key']) &&
3939
password_verify('4X6EM' . $_GET['key'] . 'fgOGi', SUDO_HASH)) {
40-
$user = db_fetch_or_add_user('ist00000', 'Sudo', ROLE_SUDO, '', '', true);
40+
$user = db_fetch_or_add_user(
41+
'ist00000', 'Sudo', ROLE_SUDO, '[email protected]',
42+
'https://api.dicebear.com/9.x/notionists-neutral/svg?seed=Jameson&lips=variant17',
43+
true);
4144
create_session($user);
4245
}
4346

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"symfony/http-client": "*",
1111
"symfony/http-foundation": "*",
1212
"symfony/mailer": "*",
13-
"symfony/mailgun-mailer": "*",
13+
"symfony/messenger": "*",
1414
"symfony/translation": "*",
1515
"symfony/twig-bridge": "*",
1616
"twig/twig": "*"

cron.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,8 @@ function run_repository() {
228228
continue;
229229

230230
$group_repo = $group->getRepository();
231-
if (!$group_repo || !($is_valid = $group_repo->isValid())) {
232-
if (!$is_valid) {
231+
if (!$group_repo || !$group_repo->isValid()) {
232+
if ($group_repo) {
233233
email_group($group,
234234
"PIC1: repository no longer exists",
235235
"Repository no longer exists: " . $group_repo . "\n\n".

email.php

+39-15
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,61 @@
22
// Copyright (c) 2022-present Instituto Superior Técnico.
33
// Distributed under the MIT license that can be found in the LICENSE file.
44

5+
use Symfony\Component\EventDispatcher\EventDispatcher;
6+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
7+
use Symfony\Component\Mailer\Event\MessageEvent;
58
use Symfony\Component\Mailer\Mailer;
69
use Symfony\Component\Mailer\Transport;
10+
use Symfony\Component\Messenger\MessageBus;
711
use Symfony\Component\Mime\Address;
812
use Symfony\Component\Mime\Email;
913

14+
class MailLoggerSubscriber implements EventSubscriberInterface
15+
{
16+
public static function getSubscribedEvents() {
17+
return [ MessageEvent::class => 'onMessage' ];
18+
}
19+
20+
public function onMessage(MessageEvent $event): void {
21+
$message = $event->getMessage();
22+
$to = array_map(function($x) { return $x->toString(); }, $message->getTo());
23+
error_log(
24+
"Sent email to: " . implode(', ', $to) .
25+
"\nSubject: " . $message->getSubject() .
26+
"\n\n" . $message->getTextBody());
27+
}
28+
}
29+
1030
function send_email($dsts, $subject, $msg) {
1131
$email = (new Email())
1232
->from(new Address(EMAIL_FROM_ADDR, 'PIC1'))
1333
->subject($subject);
1434

15-
if (IN_PRODUCTION) {
16-
if (!is_array($dsts))
17-
$dsts = [$dsts];
35+
if (!is_array($dsts))
36+
$dsts = [$dsts];
1837

19-
$has_email = false;
20-
foreach ($dsts as $dst) {
21-
if ($dst) {
22-
$email->addTo($dst);
23-
$has_email = true;
24-
}
38+
$has_email = false;
39+
foreach ($dsts as $dst) {
40+
if ($dst) {
41+
$email->addTo($dst);
42+
$has_email = true;
2543
}
26-
if (!$has_email)
27-
return;
28-
} else {
29-
$email->to(DEBUG_EMAIL_DST);
30-
$msg = "PIC1 DEBUG MODE\nTo: " . implode(', ', $dsts) . "\n\n$msg";
3144
}
45+
if (!$has_email)
46+
return;
3247
$email->text($msg);
3348

49+
if (IN_PRODUCTION) {
50+
$dispatcher = null;
51+
$bus = null;
52+
} else {
53+
$dispatcher = new EventDispatcher();
54+
$dispatcher->addSubscriber(new MailLoggerSubscriber());
55+
$bus = new MessageBus();
56+
}
57+
3458
$transport = Transport::fromDsn(MAILER_DSN);
35-
$mailer = new Mailer($transport);
59+
$mailer = new Mailer($transport, $bus, $dispatcher);
3660
$mailer->send($email);
3761
}
3862

fenix.php

+2
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ function get_groups($course) {
131131
foreach ($group->members as $m) {
132132
$students[$m->username] = $m->name;
133133
}
134+
if (!$students)
135+
continue;
134136
assert(empty($groups[$group->groupNumber]));
135137
$groups[$group->groupNumber] = [trim($group->shift), $students];
136138
}

index.php

+12-2
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,14 @@
2424
$form = null;
2525
$select_form = null;
2626
$embed_file = null;
27+
$info_message = null;
2728
$success_message = null;
2829
$table = null;
30+
$lists = null;
2931
$deadline = null;
3032
$info_box = null;
33+
$monospace = null;
34+
$refresh_url = null;
3135

3236
$request = \Symfony\Component\HttpFoundation\Request::createFromGlobals();
3337

@@ -42,11 +46,13 @@
4246
} else {
4347
terminate(print_r($e, true));
4448
}
49+
} catch (ValidationException $ex) {
50+
terminate('Failed to validate all fields: ' . $ex->getMessage());
4551
}
4652

4753
function terminate($error_message = null) {
48-
global $page, $deadline, $table, $info_box, $form, $select_form, $embed_file,
49-
$success_message;
54+
global $page, $deadline, $table, $lists, $info_box, $form, $select_form,
55+
$embed_file, $info_message, $success_message, $monospace, $refresh_url;
5056

5157
$appvar = new \ReflectionClass('\Symfony\Bridge\Twig\AppVariable');
5258
$loader = new \Twig\Loader\FilesystemLoader([
@@ -108,11 +114,15 @@ function terminate($error_message = null) {
108114
'role' => get_role_string(),
109115
'photo' => $user->getPhoto(),
110116
'embed_file' => $embed_file,
117+
'info_message' => $info_message,
111118
'success_message' => $error_message ? '' : $success_message,
112119
'error_message' => $error_message,
113120
'table' => $table,
121+
'lists' => $lists,
114122
'info_box' => $info_box,
123+
'monospace' => $monospace,
115124
'deadline' => $deadline ? $deadline->format('c') : null,
125+
'refresh_url' => $refresh_url,
116126
];
117127

118128
if ($form)

logout.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@
1414

1515
setcookie('sessid', '', 1);
1616

17-
echo "<p>Logged out!</p>\n";
17+
header('Location: https://fenix.tecnico.ulisboa.pt/logout', true, 302);

pages/bugs.php

+55-79
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,16 @@
22
// Copyright (c) 2022-present Instituto Superior Técnico.
33
// Distributed under the MIT license that can be found in the LICENSE file.
44

5+
use Symfony\Component\Form\Extension\Core\Type\FormType;
6+
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
7+
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
8+
use Symfony\Component\Form\Extension\Core\Type\UrlType;
9+
510
$user = get_user();
611
$group = $user->getGroup();
712
$year = $group ? $group->year : get_current_year();
8-
$deadline = db_fetch_deadline($year);
9-
10-
if (isset($_POST['issue_url'])) {
11-
if ($user->role == ROLE_STUDENT && !$deadline->isBugSelectionActive())
12-
die('Deadline expired');
13-
14-
if (!$group)
15-
die("Student's group not found");
16-
17-
try {
18-
if ($bug = db_fetch_bug_user($year, $user)) {
19-
$bug->description = trim($_POST['description'] ?? '');
20-
$bug->set_issue_url($_POST['issue_url'] ?? '');
21-
$bug->set_repro_url($_POST['repro_url'] ?? '');
22-
} else {
23-
$bug = SelectedBug::factory($group, $user, $_POST['description'],
24-
$_POST['issue_url'], $_POST['repro_url']);
25-
db_save($bug);
26-
}
27-
} catch (ValidationException $ex) {
28-
echo "<p style=\"color: red\">Failed to validate all fields: ",
29-
nl2br(htmlspecialchars($ex->getMessage())), "</p>\n";
30-
}
31-
}
32-
33-
mk_box_left_begin();
13+
$deadlines = db_fetch_deadline($year);
14+
$deadline = $deadlines->bug_selection;
3415

3516
if (auth_at_least(ROLE_TA)) {
3617
$groups = filter_by(['group', 'year', 'shift', 'own_shifts', 'repo']);
@@ -44,69 +25,64 @@
4425
$video = $bug->getVideoHTML();
4526
if ($video) {
4627
$video = <<<HTML
47-
<button onclick="toggleVideo(this)">Show Video</button>
48-
<div style="display: none">$video</div>
28+
<button class="btn btn-primary" onclick="toggleVideo(this)">Show Video</button>
29+
<div style="display: none; margin-top: 10px">$video</div>
4930
HTML;
5031
}
5132
$repo = $group->getRepository();
5233
$table[] = [
53-
'id' => $bug->id,
54-
'Group' => '<a href="' . link_group($group) . "\">$group</a>",
55-
'Project' => "<a href=\"$repo\">{$repo->name()}</a>",
56-
'Student' => $bug->user->shortName(),
57-
'Issue' => '<a href="' . $bug->issue_url . '">link</a>',
58-
'Desciption' => nl2br(htmlspecialchars($bug->description)),
59-
'Video' => $video,
34+
'id' => $bug->id,
35+
'Group' => dolink_group($group, $group),
36+
'Project' => $repo ? dolink_ext($repo, $repo->name()) : '',
37+
'Student' => $bug->user->shortName(),
38+
'Issue' => dolink_ext($bug->issue_url, 'link'),
39+
'Description' => $bug->description,
40+
'Video' => ['html' => $video],
6041
];
6142
}
6243
}
6344

64-
echo <<<HTML
65-
<script>
66-
function toggleVideo(button) {
67-
let videoContainer = button.nextElementSibling;
68-
if (videoContainer.style.display === "none" ) {
69-
videoContainer.style.display = "block";
70-
button.textContent = "Hide Video";
45+
if ($user->role == ROLE_STUDENT && $deadlines->isBugSelectionActive()) {
46+
if ($bug = db_fetch_bug_user($year, $user)) {
47+
$issue_url = $bug->issue_url;
48+
$repro_url = $bug->repro_url;
49+
$description = $bug->description;
7150
} else {
72-
videoContainer.style.display = "none";
73-
button.textContent = "Show Video";
51+
$issue_url = $repro_url = $description = null;
7452
}
75-
}
76-
</script>
77-
HTML;
7853

79-
if ($bug = db_fetch_bug_user($year, $user)) {
80-
$issue_url = $bug->issue_url;
81-
$repro_url = $bug->repro_url;
82-
$description = format_text($bug->description);
83-
} else {
84-
$issue_url = $repro_url = $description = '';
85-
}
86-
87-
if ($user->role == ROLE_STUDENT && $deadline->isBugSelectionActive()) {
88-
echo <<<EOF
89-
<p>&nbsp;</p>
90-
<p>Submit/edit bug proposal:</p>
91-
<form action="index.php?page=bugs" method="post">
92-
93-
<label for="issue_url">Issue URL:</label>
94-
<input type="text" id="issue_url" name="issue_url" value="$issue_url" size="50">
95-
96-
<br>
97-
<label for="repro_url">URL of video reproducing the issue:</label>
98-
<input type="text" id="repro_url" name="repro_url" value="$repro_url" size="50">
99-
100-
<br>
101-
<label for="description">Description:</label>
102-
<textarea id="description" name="description" rows="5" cols="60">
103-
$description
104-
</textarea>
105-
106-
<p><input type="submit"></p>
107-
</form>
54+
$form = $formFactory->createBuilder(FormType::class)
55+
->add('issue_url', UrlType::class, [
56+
'label' => 'Issue URL',
57+
'data' => $issue_url,
58+
])
59+
->add('repro_url', UrlType::class, [
60+
'label' => 'URL of video reproducing the issue',
61+
'data' => $repro_url,
62+
])
63+
->add('description', TextareaType::class, [
64+
'label' => 'Description',
65+
'data' => $description,
66+
])
67+
->add('submit', SubmitType::class)
68+
->getForm();
69+
70+
$form->handleRequest($request);
71+
72+
if ($form->isSubmitted() && $form->isValid()) {
73+
if (!$group)
74+
terminate("Student is not in a group");
10875

109-
EOF;
76+
if ($bug = db_fetch_bug_user($year, $user)) {
77+
$bug->description = $form->get('description')->getData();
78+
$bug->set_issue_url($form->get('issue_url')->getData());
79+
$bug->set_repro_url($form->get('repro_url')->getData());
80+
} else {
81+
$bug = SelectedBug::factory(
82+
$group, $user, $form->get('description')->getData(),
83+
$form->get('issue_url')->getData(),
84+
$form->get('repro_url')->getData());
85+
db_save($bug);
86+
}
87+
}
11088
}
111-
112-
$deadline = $deadline->bug_selection;

pages/changerole.php

+21-22
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,34 @@
22
// Copyright (c) 2022-present Instituto Superior Técnico.
33
// Distributed under the MIT license that can be found in the LICENSE file.
44

5+
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
6+
use Symfony\Component\Form\Extension\Core\Type\FormType;
7+
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
8+
use Symfony\Component\Form\Extension\Core\Type\TextType;
9+
510
auth_require_at_least(ROLE_PROF);
611

7-
html_header("Change Users' Role");
12+
$form = $formFactory->createBuilder(FormType::class)
13+
->add('username', TextType::class, [
14+
'label' => 'Username',
15+
])
16+
->add('role', ChoiceType::class, [
17+
'label' => 'Role',
18+
'choices' => array_flip(get_all_roles(auth_at_least(ROLE_SUDO))),
19+
])
20+
->add('submit', SubmitType::class)
21+
->getForm();
22+
23+
$form->handleRequest($request);
824

9-
if (isset($_POST['username']) && isset($_POST['newrole'])) {
10-
$role = (int)$_POST['newrole'];
25+
if ($form->isSubmitted() && $form->isValid()) {
26+
$role = (int)$form->get('role')->getData();
1127
if (!validate_role($role, auth_at_least(ROLE_SUDO)))
1228
die('Unknown role');
13-
14-
$user = db_fetch_user($_POST['username']);
29+
$user = db_fetch_user($form->get('username')->getData());
1530
if (!$user)
1631
die('Unknown user');
1732
$user->role = $role;
1833
db_flush();
19-
echo "<p>Changed role successfully!</p>\n";
20-
}
21-
22-
echo <<<EOF
23-
<form action="index.php?page=changerole" method="post">
24-
<label for="username">Username:</label>
25-
<input type="text" id="username" name="username"><br>
26-
<label for="newrole">New role:</label>
27-
<select id="newrole" name="newrole">
28-
EOF;
29-
foreach (get_all_roles(auth_at_least(ROLE_SUDO)) as $id => $name) {
30-
echo "<option value=\"$id\">$name</option>\n";
34+
$success_message = 'Changed role successfully!';
3135
}
32-
echo <<<EOF
33-
</select>
34-
<input type="submit">
35-
</form>
36-
EOF;

0 commit comments

Comments
 (0)