Merge commit 'origin/fusionax' into account
[platal.git] / modules / axletter.php
CommitLineData
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
22class 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");
bd84f1ae
FB
178 $users = User::getBulkUsersWithUIDs(XDB::fetchColumn('SELECT user_id
179 FROM axletter_rights'));
180 foreach ($users as $user) {
181 $mailer->addTo($user);
9b242f17 182 }
183 $mailer->send();
184 }
8da0d3c1 185 $saved = true;
186 $echeance_date = null;
187 $echeance_time = null;
188 pl_redirect('ax');
189 break;
190 }
191 }
192 $page->assign('id', $id);
4ea58d4b 193 $page->assign('short_name', $short_name);
8da0d3c1 194 $page->assign('subject', $subject);
195 $page->assign('title', $title);
196 $page->assign('body', $body);
197 $page->assign('signature', $signature);
198 $page->assign('promo_min', $promo_min);
199 $page->assign('promo_max', $promo_max);
200 $page->assign('echeance', $echeance);
201 $page->assign('echeance_date', $echeance_date);
202 $page->assign('echeance_time', $echeance_time);
203 $page->assign('saved', $saved);
204 $page->assign('new', $new);
205 $page->assign('is_xorg', S::has_perms());
206
207 if (!$saved) {
208 $select = '';
8da0d3c1 209 for ($i = 0 ; $i < 24 ; $i++) {
210 $stamp = sprintf('%02d:00:00', $i);
211 if ($stamp == $echeance_time) {
212 $sel = ' selected="selected"';
213 } else {
214 $sel = '';
215 }
216 $select .= "<option value=\"$stamp\"$sel>{$i}h</option>\n";
217 }
218 $page->assign('echeance_time', $select);
eaf30d86 219 }
8da0d3c1 220 }
221
222 function handler_cancel(&$page, $force = null)
223 {
460d8f55 224 $this->load('axletter.inc.php');
fde3e90e 225 if (!AXLetter::hasPerms() || !S::has_xsrf_token()) {
8da0d3c1 226 return PL_FORBIDDEN;
227 }
228
a0f05027 229 $al = AXLetter::awaiting();
fde3e90e 230 if (!$al) {
8da0d3c1 231 $page->kill("Aucune lettre en attente");
232 return;
233 }
8da0d3c1 234 if (!$al->invalid()) {
235 $page->kill("Une erreur est survenue lors de l'annulation de l'envoi");
236 return;
237 }
238
8f794f88 239 $page->killSuccess("L'envoi de l'annonce {$al->title()} est annulé.");
8da0d3c1 240 }
241
242 function handler_valid(&$page, $force = null)
243 {
460d8f55 244 $this->load('axletter.inc.php');
fde3e90e 245 if (!AXLetter::hasPerms() || !S::has_xsrf_token()) {
8da0d3c1 246 return PL_FORBIDDEN;
247 }
248
a0f05027 249 $al = AXLetter::awaiting();
250 if (!$al) {
8da0d3c1 251 $page->kill("Aucune lettre en attente");
252 return;
253 }
8da0d3c1 254 if (!$al->valid()) {
255 $page->kill("Une erreur est survenue lors de la validation de l'envoi");
256 return;
257 }
258
8f794f88 259 $page->killSuccess("L'envoi de l'annonce aura lieu dans l'heure qui vient.");
8da0d3c1 260 }
261
262 function handler_show(&$page, $nid = 'last')
263 {
460d8f55 264 $this->load('axletter.inc.php');
8da0d3c1 265 $page->changeTpl('axletter/show.tpl');
266
4a71cf67
SJ
267 try {
268 $nl = new AXLetter($nid);
6d1747b3 269 $user =& S::user();
4a71cf67 270 if (Get::has('text')) {
6d1747b3 271 $nl->toText($page, $user);
4a71cf67 272 } else {
6d1747b3 273 $nl->toHtml($page, $user);
4a71cf67
SJ
274 }
275 if (Post::has('send')) {
6d1747b3 276 $nl->sendTo($user);
4a71cf67
SJ
277 }
278 } catch (MailNotFound $e) {
279 return PL_NOT_FOUND;
8da0d3c1 280 }
281 }
0d75939a 282
283 function handler_admin(&$page, $action = null, $uid = null)
284 {
460d8f55 285 $this->load('axletter.inc.php');
0d75939a 286 if (Post::has('action')) {
287 $action = Post::v('action');
288 $uid = Post::v('uid');
289 }
40d428d8
VZ
290 if ($uid) {
291 S::assert_xsrf_token();
292
0d75939a 293 $uids = preg_split('/ *[,;\: ] */', $uid);
294 foreach ($uids as $uid) {
295 switch ($action) {
296 case 'add':
297 $res = AXLetter::grantPerms($uid);
298 break;
299 case 'del';
300 $res = AXLetter::revokePerms($uid);
301 break;
302 }
303 if (!$res) {
a7d35093 304 $page->trigError("Personne ne correspond à l'identifiant '$uid'");
0d75939a 305 }
306 }
307 }
308
309 $page->changeTpl('axletter/admin.tpl');
bd84f1ae
FB
310 $page->assign('admins', User::getBulkUsersWithUIDs(XDB::fetchColumn('SELECT user_id
311 FROM axletter_rights')));
eaf30d86 312
0d75939a 313 $importer = new CSVImporter('axletter_ins');
314 $importer->registerFunction('user_id', 'email vers Id X.org', array($this, 'idFromMail'));
315 $importer->forceValue('hash', array($this, 'createHash'));
2f95cd8f 316 $importer->apply($page, "admin/axletter", array('user_id', 'email', 'prenom', 'nom', 'promo', 'flag', 'hash'));
0d75939a 317 }
318
319 function idFromMail($line, $key)
320 {
321 static $field;
cbce77b7 322 global $globals;
0d75939a 323 if (!isset($field)) {
324 $field = array('email', 'mail', 'login', 'bestalias', 'forlife', 'flag');
325 foreach ($field as $fld) {
326 if (isset($line[$fld])) {
327 $field = $fld;
328 break;
329 }
330 }
331 }
bd84f1ae
FB
332 $uf = new UserFilter(new UFC_Email($line[$field]));
333 $id = $uf->getUIDs();
334 return count($id) == 1 ? $id[0] : 0;
0d75939a 335 }
336
337 function createHash($line, $key)
338 {
5480a216 339 $hash = implode(time(), $line) . rand();
0d75939a 340 $hash = md5($hash);
341 return $hash;
342 }
8da0d3c1 343}
344
a7de4ef7 345// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
8da0d3c1 346?>