Merge branch 'fusionax' into account
[platal.git] / modules / axletter.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2009 Polytechnique.org *
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),
28 'ax/out' => $this->make_hook('out', AUTH_PUBLIC),
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),
33 'admin/axletter' => $this->make_hook('admin', AUTH_MDP, 'admin'),
34 );
35 }
36
37 function handler_out(&$page, $hash = null)
38 {
39 if (!$hash) {
40 if (!S::logged()) {
41 return PL_DO_AUTH;
42 } else {
43 return $this->handler_index($page, 'out');
44 }
45 }
46 $this->load('axletter.inc.php');
47 $page->changeTpl('axletter/unsubscribe.tpl');
48 $page->assign('success', AXLetter::unsubscribe($hash, true));
49 }
50
51 function handler_index(&$page, $action = null)
52 {
53 $this->load('axletter.inc.php');
54
55 $page->changeTpl('axletter/index.tpl');
56 $page->setTitle('Envois de l\'AX');
57
58 switch ($action) {
59 case 'in': AXLetter::subscribe(); break;
60 case 'out': AXLetter::unsubscribe(); break;
61 }
62
63 $perm = AXLetter::hasPerms();
64 if ($perm) {
65 $res = XDB::query("SELECT * FROM axletter_ins");
66 $page->assign('count', $res->numRows());
67 $page->assign('new', AXLetter::awaiting());
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 {
76 $this->load('axletter.inc.php');
77 if (!AXLetter::hasPerms()) {
78 return PL_FORBIDDEN;
79 }
80
81 $page->changeTpl('axletter/edit.tpl');
82
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 $subset_to = preg_split("/[ ,;\:\n\r]+/", Post::v('subset_to'), -1, PREG_SPLIT_NO_EMPTY);
94 $subset = (count($subset_to) > 0);
95 $echeance = Post::has('echeance_date') ?
96 preg_replace('/^(\d\d\d\d)(\d\d)(\d\d)$/', '\1-\2-\3', Post::v('echeance_date')) . ' ' . Post::v('echeance_time')
97 : Post::v('echeance');
98 $echeance_date = Post::v('echeance_date');
99 $echeance_time = Post::v('echeance_time');
100
101 if (!$id) {
102 $res = XDB::query("SELECT * FROM axletter WHERE FIND_IN_SET('new', bits)");
103 if ($res->numRows()) {
104 extract($res->fetchOneAssoc(), EXTR_OVERWRITE);
105 $subset_to = ($subset ? explode("\n", $subset) : null);
106 $subset = (count($subset_to) > 0);
107 $saved = true;
108 } else {
109 XDB::execute("INSERT INTO axletter SET id = NULL");
110 $id = XDB::insertId();
111 }
112 if (!$echeance || $echeance == '0000-00-00 00:00:00') {
113 $saved = false;
114 $new = true;
115 }
116 } elseif (Post::has('valid')) {
117 S::assert_xsrf_token();
118
119 if (!$subject && $title) {
120 $subject = $title;
121 }
122 if (!$title && $subject) {
123 $title = $subject;
124 }
125 if (!$subject || !$title || !$body) {
126 $page->trigError("L'article doit avoir un sujet et un contenu");
127 Post::kill('valid');
128 }
129 if (($promo_min > $promo_max && $promo_max != 0)||
130 ($promo_min != 0 && ($promo_min <= 1900 || $promo_min >= 2020)) ||
131 ($promo_max != 0 && ($promo_max <= 1900 || $promo_max >= 2020)))
132 {
133 $page->trigError("L'intervalle de promotions n'est pas valide");
134 Post::kill('valid');
135 }
136 if (empty($short_name)) {
137 $page->trigError("L'annonce doit avoir un nom raccourci pour simplifier la navigation dans les archives");
138 Post::kill('valid');
139 } elseif (!preg_match('/^[a-z][-a-z0-9]*[a-z0-9]$/', $short_name)) {
140 $page->trigError("Le nom raccourci n'est pas valide, il doit comporter au moins 2 caractères et n'être composé "
141 . "que de chiffres, lettres et tirets");
142 Post::kill('valid');
143 } elseif ($short_name != Post::v('old_short_name')) {
144 $res = XDB::query("SELECT id FROM axletter WHERE short_name = {?}", $short_name);
145 if ($res->numRows() && $res->fetchOneCell() != $id) {
146 $page->trigError("Le nom $short_name est déjà utilisé, merci d'en choisir un autre");
147 $short_name = Post::v('old_short_name');
148 if (empty($short_name)) {
149 Post::kill('valid');
150 }
151 }
152 }
153
154 switch (@Post::v('valid')) {
155 case 'Aperçu':
156 $this->load('axletter.inc.php');
157 $al = new AXLetter(array($id, $short_name, $subject, $title, $body, $signature,
158 $promo_min, $promo_max, $subset, $echeance, 0, 'new'));
159 $al->toHtml($page, S::v('prenom'), S::v('nom'), S::v('femme'));
160 break;
161
162 case 'Confirmer':
163 XDB::execute("REPLACE INTO axletter
164 SET id = {?}, short_name = {?}, subject = {?}, title = {?}, body = {?},
165 signature = {?}, promo_min = {?}, promo_max = {?}, echeance = {?}, subset = {?}",
166 $id, $short_name, $subject, $title, $body, $signature, $promo_min, $promo_max, $echeance, $subset ? implode("\n", $subset_to) : null);
167 if (!$saved) {
168 global $globals;
169 $mailer = new PlMailer();
170 $mailer->setFrom("support@" . $globals->mail->domain);
171 $mailer->setSubject("Un nouveau projet d'email de l'AX vient d'être proposé");
172 $mailer->setTxtBody("Un nouvel email vient d'être rédigé en prévision d'un envoi prochain. Vous pouvez "
173 . "le modifier jusqu'à ce qu'il soit verrouillé pour l'envoi\n\n"
174 . "Le sujet de l'email : $subject\n"
175 . "L'échéance d'envoi est fixée à $echeance.\n"
176 . "L'email pourra néanmoins partir avant cette échéance si un administrateur de "
177 . "Polytechnique.org le valide.\n\n"
178 . "Pour modifier, valider ou annuler l'email :\n"
179 . "https://www.polytechnique.org/ax/edit\n"
180 . "-- \n"
181 . "Association Polytechnique.org\n");
182 $users = User::getBulkUsersWithUIDs(XDB::fetchColumn('SELECT user_id
183 FROM axletter_rights'));
184 foreach ($users as $user) {
185 $mailer->addTo($user);
186 }
187 $mailer->send();
188 }
189 $saved = true;
190 $echeance_date = null;
191 $echeance_time = null;
192 pl_redirect('ax');
193 break;
194 }
195 }
196 $page->assign('id', $id);
197 $page->assign('short_name', $short_name);
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('subset_to', implode("\n", $subset_to));
205 $page->assign('subset', $subset);
206 $page->assign('echeance', $echeance);
207 $page->assign('echeance_date', $echeance_date);
208 $page->assign('echeance_time', $echeance_time);
209 $page->assign('saved', $saved);
210 $page->assign('new', $new);
211 $page->assign('is_xorg', S::admin());
212
213 if (!$saved) {
214 $select = '';
215 for ($i = 0 ; $i < 24 ; $i++) {
216 $stamp = sprintf('%02d:00:00', $i);
217 if ($stamp == $echeance_time) {
218 $sel = ' selected="selected"';
219 } else {
220 $sel = '';
221 }
222 $select .= "<option value=\"$stamp\"$sel>{$i}h</option>\n";
223 }
224 $page->assign('echeance_time', $select);
225 }
226 }
227
228 function handler_cancel(&$page, $force = null)
229 {
230 $this->load('axletter.inc.php');
231 if (!AXLetter::hasPerms() || !S::has_xsrf_token()) {
232 return PL_FORBIDDEN;
233 }
234
235 $al = AXLetter::awaiting();
236 if (!$al) {
237 $page->kill("Aucune lettre en attente");
238 return;
239 }
240 if (!$al->invalid()) {
241 $page->kill("Une erreur est survenue lors de l'annulation de l'envoi");
242 return;
243 }
244
245 $page->killSuccess("L'envoi de l'annonce {$al->title()} est annulé.");
246 }
247
248 function handler_valid(&$page, $force = null)
249 {
250 $this->load('axletter.inc.php');
251 if (!AXLetter::hasPerms() || !S::has_xsrf_token()) {
252 return PL_FORBIDDEN;
253 }
254
255 $al = AXLetter::awaiting();
256 if (!$al) {
257 $page->kill("Aucune lettre en attente");
258 return;
259 }
260 if (!$al->valid()) {
261 $page->kill("Une erreur est survenue lors de la validation de l'envoi");
262 return;
263 }
264
265 $page->killSuccess("L'envoi de l'annonce aura lieu dans l'heure qui vient.");
266 }
267
268 function handler_show(&$page, $nid = 'last')
269 {
270 $this->load('axletter.inc.php');
271 $page->changeTpl('axletter/show.tpl');
272
273 try {
274 $nl = new AXLetter($nid);
275 $user =& S::user();
276 if (Get::has('text')) {
277 $nl->toText($page, $user);
278 } else {
279 $nl->toHtml($page, $user);
280 }
281 if (Post::has('send')) {
282 $nl->sendTo($user);
283 }
284 } catch (MailNotFound $e) {
285 return PL_NOT_FOUND;
286 }
287 }
288
289 function handler_admin(&$page, $action = null, $uid = null)
290 {
291 $this->load('axletter.inc.php');
292 if (Post::has('action')) {
293 $action = Post::v('action');
294 $uid = Post::v('uid');
295 }
296 if ($uid) {
297 S::assert_xsrf_token();
298
299 $uids = preg_split('/ *[,;\: ] */', $uid);
300 foreach ($uids as $uid) {
301 switch ($action) {
302 case 'add':
303 $res = AXLetter::grantPerms($uid);
304 break;
305 case 'del';
306 $res = AXLetter::revokePerms($uid);
307 break;
308 }
309 if (!$res) {
310 $page->trigError("Personne ne correspond à l'identifiant '$uid'");
311 }
312 }
313 }
314
315 $page->changeTpl('axletter/admin.tpl');
316 $page->assign('admins', User::getBulkUsersWithUIDs(XDB::fetchColumn('SELECT user_id
317 FROM axletter_rights')));
318
319 $importer = new CSVImporter('axletter_ins');
320 $importer->registerFunction('user_id', 'email vers Id X.org', array($this, 'idFromMail'));
321 $importer->forceValue('hash', array($this, 'createHash'));
322 $importer->apply($page, "admin/axletter", array('user_id', 'email', 'prenom', 'nom', 'promo', 'flag', 'hash'));
323 }
324
325 function idFromMail($line, $key, $relation = null)
326 {
327 static $field;
328 global $globals;
329 if (!isset($field)) {
330 $field = array('email', 'mail', 'login', 'bestalias', 'forlife', 'flag');
331 foreach ($field as $fld) {
332 if (isset($line[$fld])) {
333 $field = $fld;
334 break;
335 }
336 }
337 }
338 $uf = new UserFilter(new UFC_Email($line[$field]));
339 $id = $uf->getUIDs();
340 return count($id) == 1 ? $id[0] : 0;
341 }
342
343 function createHash($line, $key, $relation)
344 {
345 $hash = implode(time(), $line) . rand();
346 $hash = md5($hash);
347 return $hash;
348 }
349 }
350
351 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
352 ?>