Commit | Line | Data |
---|---|---|
8da0d3c1 | 1 | <?php |
2 | /*************************************************************************** | |
8d84c630 | 3 | * Copyright (C) 2003-2009 Polytechnique.org * |
8da0d3c1 | 4 | * http://opensource.polytechnique.org/ * |
5 | * * | |
6 | * This program is free software; you can redistribute it and/or modify * | |
7 | * it under the terms of the GNU General Public License as published by * | |
8 | * the Free Software Foundation; either version 2 of the License, or * | |
9 | * (at your option) any later version. * | |
10 | * * | |
11 | * This program is distributed in the hope that it will be useful, * | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | |
14 | * GNU General Public License for more details. * | |
15 | * * | |
16 | * You should have received a copy of the GNU General Public License * | |
17 | * along with this program; if not, write to the Free Software * | |
18 | * Foundation, Inc., * | |
19 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * | |
20 | ***************************************************************************/ | |
21 | ||
22 | class AXLetterModule extends PLModule | |
23 | { | |
24 | function handlers() | |
25 | { | |
26 | return array( | |
27 | 'ax' => $this->make_hook('index', AUTH_COOKIE), | |
a0f05027 | 28 | 'ax/out' => $this->make_hook('out', AUTH_PUBLIC), |
8da0d3c1 | 29 | 'ax/show' => $this->make_hook('show', AUTH_COOKIE), |
30 | 'ax/edit' => $this->make_hook('submit', AUTH_MDP), | |
31 | 'ax/edit/cancel' => $this->make_hook('cancel', AUTH_MDP), | |
32 | 'ax/edit/valid' => $this->make_hook('valid', AUTH_MDP), | |
0d75939a | 33 | 'admin/axletter' => $this->make_hook('admin', AUTH_MDP, 'admin'), |
8da0d3c1 | 34 | ); |
35 | } | |
36 | ||
030038cd | 37 | function handler_out(&$page, $hash = null) |
a0f05027 | 38 | { |
39 | if (!$hash) { | |
40 | if (!S::logged()) { | |
41 | return PL_DO_AUTH; | |
42 | } else { | |
43 | return $this->handler_index($page, 'out'); | |
44 | } | |
45 | } | |
460d8f55 | 46 | $this->load('axletter.inc.php'); |
a0f05027 | 47 | $page->changeTpl('axletter/unsubscribe.tpl'); |
48 | $page->assign('success', AXLetter::unsubscribe($hash, true)); | |
49 | } | |
50 | ||
51 | function handler_index(&$page, $action = null) | |
8da0d3c1 | 52 | { |
460d8f55 | 53 | $this->load('axletter.inc.php'); |
8da0d3c1 | 54 | |
55 | $page->changeTpl('axletter/index.tpl'); | |
46f272fe | 56 | $page->setTitle('Envois de l\'AX'); |
8da0d3c1 | 57 | |
58 | switch ($action) { | |
8da0d3c1 | 59 | case 'in': AXLetter::subscribe(); break; |
a0f05027 | 60 | case 'out': AXLetter::unsubscribe(); break; |
8da0d3c1 | 61 | } |
62 | ||
63 | $perm = AXLetter::hasPerms(); | |
64 | if ($perm) { | |
9b242f17 | 65 | $res = XDB::query("SELECT * FROM axletter_ins"); |
66 | $page->assign('count', $res->numRows()); | |
a0f05027 | 67 | $page->assign('new', AXLetter::awaiting()); |
8da0d3c1 | 68 | } |
69 | $page->assign('axs', AXLetter::subscriptionState()); | |
70 | $page->assign('ax_list', AXLetter::listSent()); | |
71 | $page->assign('ax_rights', $perm); | |
72 | } | |
73 | ||
74 | function handler_submit(&$page, $action = null) | |
75 | { | |
460d8f55 | 76 | $this->load('axletter.inc.php'); |
8da0d3c1 | 77 | if (!AXLetter::hasPerms()) { |
78 | return PL_FORBIDDEN; | |
79 | } | |
80 | ||
81 | $page->changeTpl('axletter/edit.tpl'); | |
82 | ||
4ea58d4b VZ |
83 | $saved = Post::i('saved'); |
84 | $new = false; | |
85 | $id = Post::i('id'); | |
86 | $short_name = trim(Post::v('short_name')); | |
87 | $subject = trim(Post::v('subject')); | |
88 | $title = trim(Post::v('title')); | |
89 | $body = rtrim(Post::v('body')); | |
90 | $signature = trim(Post::v('signature')); | |
91 | $promo_min = Post::i('promo_min'); | |
92 | $promo_max = Post::i('promo_max'); | |
93 | $echeance = Post::has('echeance_date') ? | |
94 | preg_replace('/^(\d\d\d\d)(\d\d)(\d\d)$/', '\1-\2-\3', Post::v('echeance_date')) . ' ' . Post::v('echeance_time') | |
95 | : Post::v('echeance'); | |
8da0d3c1 | 96 | $echeance_date = Post::v('echeance_date'); |
97 | $echeance_time = Post::v('echeance_time'); | |
98 | ||
99 | if (!$id) { | |
100 | $res = XDB::query("SELECT * FROM axletter WHERE FIND_IN_SET('new', bits)"); | |
101 | if ($res->numRows()) { | |
102 | extract($res->fetchOneAssoc(), EXTR_OVERWRITE); | |
103 | $saved = true; | |
104 | } else { | |
105 | XDB::execute("INSERT INTO axletter SET id = NULL"); | |
106 | $id = XDB::insertId(); | |
107 | } | |
108 | if (!$echeance || $echeance == '0000-00-00 00:00:00') { | |
109 | $saved = false; | |
110 | $new = true; | |
111 | } | |
40d428d8 VZ |
112 | } elseif (Post::has('valid')) { |
113 | S::assert_xsrf_token(); | |
114 | ||
8da0d3c1 | 115 | if (!$subject && $title) { |
116 | $subject = $title; | |
117 | } | |
118 | if (!$title && $subject) { | |
119 | $title = $subject; | |
120 | } | |
121 | if (!$subject || !$title || !$body) { | |
a7d35093 | 122 | $page->trigError("L'article doit avoir un sujet et un contenu"); |
8da0d3c1 | 123 | Post::kill('valid'); |
124 | } | |
125 | if (($promo_min > $promo_max && $promo_max != 0)|| | |
126 | ($promo_min != 0 && ($promo_min <= 1900 || $promo_min >= 2020)) || | |
127 | ($promo_max != 0 && ($promo_max <= 1900 || $promo_max >= 2020))) | |
128 | { | |
a7d35093 | 129 | $page->trigError("L'intervalle de promotions n'est pas valide"); |
8da0d3c1 | 130 | Post::kill('valid'); |
131 | } | |
4ea58d4b | 132 | if (empty($short_name)) { |
a7d35093 | 133 | $page->trigError("L'annonce doit avoir un nom raccourci pour simplifier la navigation dans les archives"); |
8da0d3c1 | 134 | Post::kill('valid'); |
4ea58d4b | 135 | } elseif (!preg_match('/^[a-z][-a-z0-9]*[a-z0-9]$/', $short_name)) { |
a7d35093 | 136 | $page->trigError("Le nom raccourci n'est pas valide, il doit comporter au moins 2 caractères et n'être composé " |
8da0d3c1 | 137 | . "que de chiffres, lettres et tirets"); |
138 | Post::kill('valid'); | |
4ea58d4b VZ |
139 | } elseif ($short_name != Post::v('old_short_name')) { |
140 | $res = XDB::query("SELECT id FROM axletter WHERE short_name = {?}", $short_name); | |
8da0d3c1 | 141 | if ($res->numRows() && $res->fetchOneCell() != $id) { |
a7d35093 | 142 | $page->trigError("Le nom $short_name est déjà utilisé, merci d'en choisir un autre"); |
4ea58d4b VZ |
143 | $short_name = Post::v('old_short_name'); |
144 | if (empty($short_name)) { | |
8da0d3c1 | 145 | Post::kill('valid'); |
146 | } | |
147 | } | |
148 | } | |
149 | ||
150 | switch (@Post::v('valid')) { | |
a7de4ef7 | 151 | case 'Aperçu': |
460d8f55 | 152 | $this->load('axletter.inc.php'); |
4ea58d4b | 153 | $al = new AXLetter(array($id, $short_name, $subject, $title, $body, $signature, |
8da0d3c1 | 154 | $promo_min, $promo_max, $echeance, 0, 'new')); |
155 | $al->toHtml($page, S::v('prenom'), S::v('nom'), S::v('femme')); | |
156 | break; | |
157 | ||
158 | case 'Confirmer': | |
159 | XDB::execute("REPLACE INTO axletter | |
c6f2cafe | 160 | SET id = {?}, short_name = {?}, subject = {?}, title = {?}, body = {?}, |
8da0d3c1 | 161 | signature = {?}, promo_min = {?}, promo_max = {?}, echeance = {?}", |
4ea58d4b | 162 | $id, $short_name, $subject, $title, $body, $signature, $promo_min, $promo_max, $echeance); |
9b242f17 | 163 | if (!$saved) { |
115c90db | 164 | global $globals; |
9b242f17 | 165 | $mailer = new PlMailer(); |
1d55fe45 | 166 | $mailer->setFrom("support@" . $globals->mail->domain); |
faefdbb7 SJ |
167 | $mailer->setSubject("Un nouveau projet d'email de l'AX vient d'être proposé"); |
168 | $mailer->setTxtBody("Un nouvel email vient d'être rédigé en prévision d'un envoi prochain. Vous pouvez " | |
9b242f17 | 169 | . "le modifier jusqu'à ce qu'il soit verrouillé pour l'envoi\n\n" |
faefdbb7 | 170 | . "Le sujet de l'email : $subject\n" |
9b242f17 | 171 | . "L'échéance d'envoi est fixée à $echeance.\n" |
faefdbb7 | 172 | . "L'email pourra néanmoins partir avant cette échéance si un administrateur de " |
9b242f17 | 173 | . "Polytechnique.org le valide.\n\n" |
faefdbb7 | 174 | . "Pour modifier, valider ou annuler l'email :\n" |
9b242f17 | 175 | . "https://www.polytechnique.org/ax/edit\n" |
176 | . "-- \n" | |
177 | . "Association Polytechnique.org\n"); | |
178 | $res = XDB::iterRow("SELECT IF(u.nom_usage != '', u.nom_usage, u.nom) AS nom, | |
179 | u.prenom, a.alias AS bestalias | |
180 | FROM axletter_rights AS ar | |
181 | INNER JOIN auth_user_md5 AS u USING(user_id) | |
182 | INNER JOIN aliases AS a ON (u.user_id = a.id | |
183 | AND FIND_IN_SET('bestalias', a.flags))"); | |
9b242f17 | 184 | while (list($nom, $prenom, $alias) = $res->next()) { |
185 | $mailer->addTo("$nom $prenom <$alias@{$globals->mail->domain}>"); | |
186 | } | |
187 | $mailer->send(); | |
188 | } | |
8da0d3c1 | 189 | $saved = true; |
190 | $echeance_date = null; | |
191 | $echeance_time = null; | |
192 | pl_redirect('ax'); | |
193 | break; | |
194 | } | |
195 | } | |
196 | $page->assign('id', $id); | |
4ea58d4b | 197 | $page->assign('short_name', $short_name); |
8da0d3c1 | 198 | $page->assign('subject', $subject); |
199 | $page->assign('title', $title); | |
200 | $page->assign('body', $body); | |
201 | $page->assign('signature', $signature); | |
202 | $page->assign('promo_min', $promo_min); | |
203 | $page->assign('promo_max', $promo_max); | |
204 | $page->assign('echeance', $echeance); | |
205 | $page->assign('echeance_date', $echeance_date); | |
206 | $page->assign('echeance_time', $echeance_time); | |
207 | $page->assign('saved', $saved); | |
208 | $page->assign('new', $new); | |
209 | $page->assign('is_xorg', S::has_perms()); | |
210 | ||
211 | if (!$saved) { | |
212 | $select = ''; | |
8da0d3c1 | 213 | for ($i = 0 ; $i < 24 ; $i++) { |
214 | $stamp = sprintf('%02d:00:00', $i); | |
215 | if ($stamp == $echeance_time) { | |
216 | $sel = ' selected="selected"'; | |
217 | } else { | |
218 | $sel = ''; | |
219 | } | |
220 | $select .= "<option value=\"$stamp\"$sel>{$i}h</option>\n"; | |
221 | } | |
222 | $page->assign('echeance_time', $select); | |
eaf30d86 | 223 | } |
8da0d3c1 | 224 | } |
225 | ||
226 | function handler_cancel(&$page, $force = null) | |
227 | { | |
460d8f55 | 228 | $this->load('axletter.inc.php'); |
fde3e90e | 229 | if (!AXLetter::hasPerms() || !S::has_xsrf_token()) { |
8da0d3c1 | 230 | return PL_FORBIDDEN; |
231 | } | |
232 | ||
a0f05027 | 233 | $al = AXLetter::awaiting(); |
fde3e90e | 234 | if (!$al) { |
8da0d3c1 | 235 | $page->kill("Aucune lettre en attente"); |
236 | return; | |
237 | } | |
8da0d3c1 | 238 | if (!$al->invalid()) { |
239 | $page->kill("Une erreur est survenue lors de l'annulation de l'envoi"); | |
240 | return; | |
241 | } | |
242 | ||
8f794f88 | 243 | $page->killSuccess("L'envoi de l'annonce {$al->title()} est annulé."); |
8da0d3c1 | 244 | } |
245 | ||
246 | function handler_valid(&$page, $force = null) | |
247 | { | |
460d8f55 | 248 | $this->load('axletter.inc.php'); |
fde3e90e | 249 | if (!AXLetter::hasPerms() || !S::has_xsrf_token()) { |
8da0d3c1 | 250 | return PL_FORBIDDEN; |
251 | } | |
252 | ||
a0f05027 | 253 | $al = AXLetter::awaiting(); |
254 | if (!$al) { | |
8da0d3c1 | 255 | $page->kill("Aucune lettre en attente"); |
256 | return; | |
257 | } | |
8da0d3c1 | 258 | if (!$al->valid()) { |
259 | $page->kill("Une erreur est survenue lors de la validation de l'envoi"); | |
260 | return; | |
261 | } | |
262 | ||
8f794f88 | 263 | $page->killSuccess("L'envoi de l'annonce aura lieu dans l'heure qui vient."); |
8da0d3c1 | 264 | } |
265 | ||
266 | function handler_show(&$page, $nid = 'last') | |
267 | { | |
460d8f55 | 268 | $this->load('axletter.inc.php'); |
8da0d3c1 | 269 | $page->changeTpl('axletter/show.tpl'); |
270 | ||
4a71cf67 SJ |
271 | try { |
272 | $nl = new AXLetter($nid); | |
273 | if (Get::has('text')) { | |
274 | $nl->toText($page, S::v('prenom'), S::v('nom'), S::v('femme')); | |
275 | } else { | |
276 | $nl->toHtml($page, S::v('prenom'), S::v('nom'), S::v('femme')); | |
277 | } | |
278 | if (Post::has('send')) { | |
279 | $nl->sendTo(S::user()->login(), S::user()->bestEmail(), | |
280 | S::v('prenom'), S::v('nom'), | |
281 | S::v('femme'), S::v('mail_fmt') != 'texte'); | |
282 | } | |
283 | } catch (MailNotFound $e) { | |
284 | return PL_NOT_FOUND; | |
8da0d3c1 | 285 | } |
286 | } | |
0d75939a | 287 | |
288 | function handler_admin(&$page, $action = null, $uid = null) | |
289 | { | |
460d8f55 | 290 | $this->load('axletter.inc.php'); |
0d75939a | 291 | if (Post::has('action')) { |
292 | $action = Post::v('action'); | |
293 | $uid = Post::v('uid'); | |
294 | } | |
40d428d8 VZ |
295 | if ($uid) { |
296 | S::assert_xsrf_token(); | |
297 | ||
0d75939a | 298 | $uids = preg_split('/ *[,;\: ] */', $uid); |
299 | foreach ($uids as $uid) { | |
300 | switch ($action) { | |
301 | case 'add': | |
302 | $res = AXLetter::grantPerms($uid); | |
303 | break; | |
304 | case 'del'; | |
305 | $res = AXLetter::revokePerms($uid); | |
306 | break; | |
307 | } | |
308 | if (!$res) { | |
a7d35093 | 309 | $page->trigError("Personne ne correspond à l'identifiant '$uid'"); |
0d75939a | 310 | } |
311 | } | |
312 | } | |
313 | ||
314 | $page->changeTpl('axletter/admin.tpl'); | |
315 | $res = XDB::iterator("SELECT IF(u.nom_usage != '', u.nom_usage, u.nom) AS nom, | |
90c614cd | 316 | u.prenom, u.promo, u.hruid |
0d75939a | 317 | FROM axletter_rights AS ar |
90c614cd | 318 | INNER JOIN auth_user_md5 AS u USING(user_id)"); |
0d75939a | 319 | $page->assign('admins', $res); |
eaf30d86 | 320 | |
0d75939a | 321 | $importer = new CSVImporter('axletter_ins'); |
322 | $importer->registerFunction('user_id', 'email vers Id X.org', array($this, 'idFromMail')); | |
323 | $importer->forceValue('hash', array($this, 'createHash')); | |
2f95cd8f | 324 | $importer->apply($page, "admin/axletter", array('user_id', 'email', 'prenom', 'nom', 'promo', 'flag', 'hash')); |
0d75939a | 325 | } |
326 | ||
327 | function idFromMail($line, $key) | |
328 | { | |
329 | static $field; | |
cbce77b7 | 330 | global $globals; |
0d75939a | 331 | if (!isset($field)) { |
332 | $field = array('email', 'mail', 'login', 'bestalias', 'forlife', 'flag'); | |
333 | foreach ($field as $fld) { | |
334 | if (isset($line[$fld])) { | |
335 | $field = $fld; | |
336 | break; | |
337 | } | |
338 | } | |
339 | } | |
340 | $email = $line[$field]; | |
341 | if (strpos($email, '@') === false) { | |
342 | $user = $email; | |
cbce77b7 | 343 | $domain = $globals->mail->domain2; |
0d75939a | 344 | } else { |
eaf30d86 | 345 | list($user, $domain) = explode('@', $email); |
cbce77b7 | 346 | } |
347 | if ($domain != $globals->mail->domain && $domain != $globals->mail->domain2 | |
0d75939a | 348 | && $domain != $globals->mail->alias_dom && $domain != $globals->mail->alias_dom2) { |
cbce77b7 | 349 | $res = XDB::query("SELECT uid FROM emails WHERE email = {?}", $email); |
350 | if ($res->numRows() == 1) { | |
351 | return $res->fetchOneCell(); | |
0d75939a | 352 | } |
cbce77b7 | 353 | return '0'; |
354 | } | |
355 | list($user) = explode('+', $user); | |
356 | list($user) = explode('_', $user); | |
357 | if ($domain == $globals->mail->alias_dom || $domain == $globals->mail->alias_dom2) { | |
358 | $res = XDB::query("SELECT a.id | |
359 | FROM virtual AS v | |
360 | INNER JOIN virtual_redirect AS r USING(vid) | |
361 | INNER JOIN aliases AS a ON (a.type = 'a_vie' | |
362 | AND r.redirect = CONCAT(a.alias, '@{$globals->mail->domain2}')) | |
363 | WHERE v.alias = CONCAT({?}, '@{$globals->mail->alias_dom}')", $user); | |
364 | $id = $res->fetchOneCell(); | |
365 | return $id ? $id : '0'; | |
0d75939a | 366 | } |
367 | $res = XDB::query("SELECT id FROM aliases WHERE alias = {?}", $user); | |
368 | $id = $res->fetchOneCell(); | |
369 | return $id ? $id : '0'; | |
370 | } | |
371 | ||
372 | function createHash($line, $key) | |
373 | { | |
5480a216 | 374 | $hash = implode(time(), $line) . rand(); |
0d75939a | 375 | $hash = md5($hash); |
376 | return $hash; | |
377 | } | |
8da0d3c1 | 378 | } |
379 | ||
a7de4ef7 | 380 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
8da0d3c1 | 381 | ?> |