-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathparticipants.php
222 lines (189 loc) · 7.6 KB
/
participants.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
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php
declare(strict_types=1);
require_once 'config.php';
require_once 'utils.php';
require_once 'event_type.php';
require_once 'i18n/i18n.php';
class ParticipantListMailer {
private const MAX_TIME_BETWEEN_EMAILS = 2 * 60 * 60; // 2 hours
private string $logPath;
public function __construct(string $logPath) {
$this->logPath = $logPath;
}
public function sendParticipantList(Event $event): string {
$emailAddresses = $event->metas;
if (!$this->canSendEmail($event) || empty($emailAddresses)) {
return "Der letzte Mailversand liegt unter der Minimalzeit, bitte versuch es später erneut!";
}
if (!$this->validateEmailAddresses($emailAddresses)) {
return "Eine der hinterlegten Mailadressen ist keine gültige Mailadresse!";
}
$participants = $this->getParticipants($event);
$emailContent = $this->buildEmailContent($event, $participants);
if (!$this->sendEmails($emailAddresses, $emailContent['subject'], $emailContent['body'])) {
return "Der Mailversand hat nicht funktioniert!";
}
$this->logEmailSent($event, $emailAddresses);
return "Die Teilnehmerliste wurde erfolgreich versendet.";
}
private function getParticipants(Event $event): array {
if (!file_exists($event->csvPath)) {
return [];
}
$participants = [];
if (($handle = fopen($event->csvPath, 'r')) !== false) {
// Skip header row
fgetcsv($handle);
while (($data = fgetcsv($handle)) !== false) {
$participants[] = [
'name' => $data[0] ?? '',
'mail' => $data[1] ?? '',
'misc' => implode(', ', array_slice($data, 2))
];
}
fclose($handle);
}
return $participants;
}
private function buildEmailContent(Event $event, array $participants): array {
$subject = "Teilnehmerliste für {$event->name} am " . $event->getEventDateString();
$body = "$subject:<br><br>";
foreach ($participants as $participant) {
$body .= sprintf(
'%s (<a href="mailto:%s">%s</a>) %s<br>',
htmlspecialchars($participant['name']),
$participant['mail'],
$participant['mail'],
htmlspecialchars($participant['misc'])
);
}
$emailList = implode(',', array_column($participants, 'mail'));
$body .= "<br><br>Mail-Liste:<br><br>$emailList";
return [
'subject' => $subject,
'body' => $body
];
}
private function canSendEmail(Event $event): bool {
if (!file_exists($this->logPath)) {
return true;
}
$lastSentTime = 0;
if (($handle = fopen($this->logPath, 'r')) !== false) {
while (($line = fgetcsv($handle)) !== false) {
if ($line[0] === $event->link) {
$lastSentTime = strtotime($line[1] ?? '');
}
}
fclose($handle);
}
return (time() - $lastSentTime) > self::MAX_TIME_BETWEEN_EMAILS;
}
private function logEmailSent(Event $event, array $emailAddresses): void {
$data = [
$event->link,
date('Y-m-d H:i:s'),
implode(', ', $emailAddresses)
];
if (($handle = fopen($this->logPath, 'a')) !== false) {
fputcsv($handle, $data);
fclose($handle);
}
}
private function validateEmailAddresses(array $emails): bool {
return array_reduce($emails, fn($valid, $email) =>
$valid && filter_var($email, FILTER_VALIDATE_EMAIL), true);
}
private function sendEmails(array $recipients, string $subject, string $body): bool {
foreach ($recipients as $recipient) {
if (!sendMailViaPHPMailer($recipient, $subject, $body)) {
return false;
}
}
return true;
}
}
final class SecurityToken {
private const TOKEN_LENGTH = 32;
public static function generate(): string {
try {
return bin2hex(random_bytes(self::TOKEN_LENGTH));
} catch (Exception) {
return '';
}
}
}
// Start the session and handle the request
session_start();
$mailer = new ParticipantListMailer($GLOBALS['fp'] . 'logs.csv');
$filtered_events = array_filter($GLOBALS['events'], fn(Event $event) => $event->isUpcoming());
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
$_SESSION['token'] = SecurityToken::generate();
$_SESSION['token_field'] = SecurityToken::generate();
}
?>
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="css/style.css<?= $FILE_REVISION ?>">
<title><?= $i18n["title"] ?></title>
</head>
<body>
<div id="center">
<div class="container">
<?php if ($_SERVER['REQUEST_METHOD'] === 'GET'): ?>
<form action="participants.php" method="post">
<label for="event">Event:</label>
<select name="event" id="event">
<?php foreach ($filtered_events as $event): ?>
<option value="<?= htmlspecialchars($event->link) ?>">
<?= htmlspecialchars($event->name) ?> -
<?= htmlspecialchars($event->getEventDateString()) ?> -
<?= htmlspecialchars($event->link) ?>
</option>
<?php endforeach; ?>
</select>
<br>
<input type="hidden" name="send" value="true">
<input type="hidden" name="<?= htmlspecialchars($_SESSION['token_field']) ?>"
value="<?= htmlspecialchars($_SESSION['token']) ?>">
<br>
<input type="submit" value="Liste senden">
</form>
<div class="container">
<a href="index.php?lang=<?= $i18n->getLanguage() ?>">
<div class="link"><?= $i18n['back'] ?></div>
</a>
</div>
<?php elseif ($_SERVER['REQUEST_METHOD'] === 'POST'): ?>
<div class="container">
<?php
if (isset($_POST["send"], $_POST["event"],
$_POST[$_SESSION["token_field"]],
$_SESSION["token"]) &&
$_POST[$_SESSION["token_field"]] === $_SESSION["token"]) {
$link = filter_input(INPUT_POST, 'event', FILTER_SANITIZE_ENCODED);
$event = $GLOBALS['events'][$link];
$result = $mailer->sendParticipantList($event);
$isSuccess = $result === "Die Teilnehmerliste wurde erfolgreich versendet.";
?>
<div class="text-block <?= $isSuccess ? '' : 'error' ?>">
<?= htmlspecialchars($result) ?>
</div>
<?php } else { ?>
<div class="text-block error">Ungültiger Vorgang.</div>
<?php } ?>
<div class="container">
<a href="participants.php?lang=<?= $i18n->getLanguage() ?>">
<div class="link"><?= $i18n['back'] ?></div>
</a>
</div>
</div>
<?php endif; ?>
</div>
</div>
</body>
</html>