2 /***************************************************************************
3 * Copyright (C) 2003-2007 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
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. *
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. *
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 *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
22 class AXLetterModule
extends PLModule
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'),
37 function handler_out(&$page, $hash = null
)
43 return $this->handler_index($page, 'out');
46 require_once dirname(__FILE__
) . '/axletter/axletter.inc.php';
47 $page->changeTpl('axletter/unsubscribe.tpl');
48 $page->assign('success', AXLetter
::unsubscribe($hash, true
));
51 function handler_index(&$page, $action = null
)
53 require_once dirname(__FILE__
) . '/axletter/axletter.inc.php';
55 $page->changeTpl('axletter/index.tpl');
56 $page->assign('xorg_title','Polytechnique.org - Envois de l\'AX');
59 case 'in': AXLetter
::subscribe(); break;
60 case 'out': AXLetter
::unsubscribe(); break;
63 $perm = AXLetter
::hasPerms();
65 $res = XDB
::query("SELECT * FROM axletter_ins");
66 $page->assign('count', $res->numRows());
67 $page->assign('new', AXLetter
::awaiting());
69 $page->assign('axs', AXLetter
::subscriptionState());
70 $page->assign('ax_list', AXLetter
::listSent());
71 $page->assign('ax_rights', $perm);
74 function handler_submit(&$page, $action = null
)
76 require_once dirname(__FILE__
) . '/axletter/axletter.inc.php';
77 if (!AXLetter
::hasPerms()) {
81 $page->changeTpl('axletter/edit.tpl');
83 $saved = Post
::i('saved');
86 $shortname = trim(Post
::v('shortname'));
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') ? Post
::v('echeance_date') . ' ' . Post
::v('echeance_time')
94 : Post
::v('echeance');
95 $echeance_date = Post
::v('echeance_date');
96 $echeance_time = Post
::v('echeance_time');
99 $res = XDB
::query("SELECT * FROM axletter WHERE FIND_IN_SET('new', bits)");
100 if ($res->numRows()) {
101 extract($res->fetchOneAssoc(), EXTR_OVERWRITE
);
104 XDB
::execute("INSERT INTO axletter SET id = NULL");
105 $id = XDB
::insertId();
107 if (!$echeance ||
$echeance == '0000-00-00 00:00:00') {
111 } elseif (Post
::has('valid')) {
112 if (!$subject && $title) {
115 if (!$title && $subject) {
118 if (!$subject ||
!$title ||
!$body) {
119 $page->trig("L'article doit avoir un sujet et un contenu");
122 if (($promo_min > $promo_max && $promo_max != 0)||
123 ($promo_min != 0 && ($promo_min <= 1900 ||
$promo_min >= 2020)) ||
124 ($promo_max != 0 && ($promo_max <= 1900 ||
$promo_max >= 2020)))
126 $page->trig("L'intervalle de promotions n'est pas valide");
129 if (empty($shortname)) {
130 $page->trig("L'annonce doit avoir un nom raccourci pour simplifier la navigation dans les archives");
132 } elseif (!preg_match('/^[a-z][-a-z0-9]*[a-z0-9]$/', $shortname)) {
133 $page->trig("Le nom raccourci n'est pas valide, il doit comporter au moins 2 caractères et n'être composé "
134 . "que de chiffres, lettres et tirets");
136 } elseif ($shortname != Post
::v('old_shortname')) {
137 $res = XDB
::query("SELECT id FROM axletter WHERE shortname = {?}", $shortname);
138 if ($res->numRows() && $res->fetchOneCell() != $id) {
139 $page->trig("Le nom $shortname est déjà utilisé, merci d'en choisir un autre");
140 $shortname = Post
::v('old_shortname');
141 if (empty($shortname)) {
147 switch (@Post
::v('valid')) {
149 require_once dirname(__FILE__
) . '/axletter/axletter.inc.php';
150 $al = new AXLetter(array($id, $shortname, $subject, $title, $body, $signature,
151 $promo_min, $promo_max, $echeance, 0, 'new'));
152 $al->toHtml($page, S
::v('prenom'), S
::v('nom'), S
::v('femme'));
156 XDB
::execute("REPLACE INTO axletter
157 SET id = {?}, shortname = {?}, subject = {?}, title = {?}, body = {?},
158 signature = {?}, promo_min = {?}, promo_max = {?}, echeance = {?}",
159 $id, $shortname, $subject, $title, $body, $signature, $promo_min, $promo_max, $echeance);
161 $mailer = new PlMailer();
162 $mailer->setFrom("support@polytechnique.org");
163 $mailer->setSubject("Un nouveau projet de mail de l'AX vient d'être proposé");
164 $mailer->setTxtBody("Un nouveau mail vient d'être rédigé en prévision d'un envoi prochain. Vous pouvez "
165 . "le modifier jusqu'à ce qu'il soit verrouillé pour l'envoi\n\n"
166 . "Le sujet du mail : $subject\n"
167 . "L'échéance d'envoi est fixée à $echeance.\n"
168 . "Le mail pourra néanmoins partir avant cette échéance si un administrateur de "
169 . "Polytechnique.org le valide.\n\n"
170 . "Pour modifier, valider ou annuler le mail :\n"
171 . "https://www.polytechnique.org/ax/edit\n"
173 . "Association Polytechnique.org\n");
174 $res = XDB
::iterRow("SELECT IF(u.nom_usage != '', u.nom_usage, u.nom) AS nom,
175 u.prenom, a.alias AS bestalias
176 FROM axletter_rights AS ar
177 INNER JOIN auth_user_md5 AS u USING(user_id)
178 INNER JOIN aliases AS a ON (u.user_id = a.id
179 AND FIND_IN_SET('bestalias', a.flags))");
181 while (list($nom, $prenom, $alias) = $res->next()) {
182 $mailer->addTo("$nom $prenom <$alias@{$globals->mail->domain}>");
187 $echeance_date = null
;
188 $echeance_time = null
;
193 $page->assign('id', $id);
194 $page->assign('shortname', $shortname);
195 $page->assign('subject', $subject);
196 $page->assign('title', $title);
197 $page->assign('body', $body);
198 $page->assign('signature', $signature);
199 $page->assign('promo_min', $promo_min);
200 $page->assign('promo_max', $promo_max);
201 $page->assign('echeance', $echeance);
202 $page->assign('echeance_date', $echeance_date);
203 $page->assign('echeance_time', $echeance_time);
204 $page->assign('saved', $saved);
205 $page->assign('new', $new);
206 $page->assign('is_xorg', S
::has_perms());
210 $time = time() +
3600 * 24 * 2;
211 for ($i = 0 ; $i < 15 ; $i++
) {
213 $p_stamp = date('Ymd', $time);
214 $year = date('Y', $time);
215 $month = date('m', $time);
216 $day = date('d', $time);
218 if ($p_stamp == $echeance_date) {
219 $sel = ' selected="selected"';
223 $select .= "<option value=\"$p_stamp\"$sel> $day / $month / $year</option>\n";
225 $page->assign('echeance_date', $select);
227 for ($i = 0 ; $i < 24 ; $i++
) {
228 $stamp = sprintf('%02d:00:00', $i);
229 if ($stamp == $echeance_time) {
230 $sel = ' selected="selected"';
234 $select .= "<option value=\"$stamp\"$sel>{$i}h</option>\n";
236 $page->assign('echeance_time', $select);
240 function handler_cancel(&$page, $force = null
)
242 require_once dirname(__FILE__
) . '/axletter/axletter.inc.php';
243 if (!AXLetter
::hasPerms()) {
247 $url = parse_url($_SERVER['HTTP_REFERER']);
248 if ($force != 'force' && trim($url['path'], '/') != 'ax/edit') {
252 $al = AXLetter
::awaiting();
254 $page->kill("Aucune lettre en attente");
257 if (!$al->invalid()) {
258 $page->kill("Une erreur est survenue lors de l'annulation de l'envoi");
262 $page->kill("L'envoi de l'annonce {$al->title()} est annulé");
265 function handler_valid(&$page, $force = null
)
267 require_once dirname(__FILE__
) . '/axletter/axletter.inc.php';
268 if (!AXLetter
::hasPerms()) {
272 $url = parse_url($_SERVER['HTTP_REFERER']);
273 if ($force != 'force' && trim($url['path'], '/') != 'ax/edit') {
277 $al = AXLetter
::awaiting();
279 $page->kill("Aucune lettre en attente");
283 $page->kill("Une erreur est survenue lors de la validation de l'envoi");
287 $page->kill("L'envoi de l'annonce aura lieu dans l'heure qui vient.");
290 function handler_show(&$page, $nid = 'last')
292 require_once dirname(__FILE__
) . '/axletter/axletter.inc.php';
293 $page->changeTpl('axletter/show.tpl');
295 $nl = new AXLetter($nid);
296 if (Get
::has('text')) {
297 $nl->toText($page, S
::v('prenom'), S
::v('nom'), S
::v('femme'));
299 $nl->toHtml($page, S
::v('prenom'), S
::v('nom'), S
::v('femme'));
301 if (Post
::has('send')) {
302 $nl->sendTo(S
::v('prenom'), S
::v('nom'),
303 S
::v('bestalias'), S
::v('femme'),
304 S
::v('mail_fmt') != 'texte');
308 function handler_admin(&$page, $action = null
, $uid = null
)
310 require_once dirname(__FILE__
) . '/axletter/axletter.inc.php';
311 if (Post
::has('action')) {
312 $action = Post
::v('action');
313 $uid = Post
::v('uid');
316 $uids = preg_split('/ *[,;\: ] */', $uid);
317 foreach ($uids as $uid) {
320 $res = AXLetter
::grantPerms($uid);
323 $res = AXLetter
::revokePerms($uid);
327 $page->trig("Personne ne oorrespond à l'identifiant '$uid'");
332 $page->changeTpl('axletter/admin.tpl');
333 $res = XDB
::iterator("SELECT IF(u.nom_usage != '', u.nom_usage, u.nom) AS nom,
334 u.prenom, u.promo, a.alias AS forlife
335 FROM axletter_rights AS ar
336 INNER JOIN auth_user_md5 AS u USING(user_id)
337 INNER JOIN aliases AS a ON (u.user_id = a.id AND a.type = 'a_vie')");
338 $page->assign('admins', $res);
340 $importer = new CSVImporter('axletter_ins');
341 $importer->registerFunction('user_id', 'email vers Id X.org', array($this, 'idFromMail'));
342 $importer->forceValue('hash', array($this, 'createHash'));
343 $importer->apply($page, "admin/axletter", array('user_id', 'email', 'prenom', 'nom', 'promo', 'flag', 'hash'));
346 function idFromMail($line, $key)
350 if (!isset($field)) {
351 $field = array('email', 'mail', 'login', 'bestalias', 'forlife', 'flag');
352 foreach ($field as $fld) {
353 if (isset($line[$fld])) {
359 $email = $line[$field];
360 if (strpos($email, '@') === false
) {
362 $domain = $globals->mail
->domain2
;
364 list($user, $domain) = explode('@', $email);
366 if ($domain != $globals->mail
->domain
&& $domain != $globals->mail
->domain2
367 && $domain != $globals->mail
->alias_dom
&& $domain != $globals->mail
->alias_dom2
) {
368 $res = XDB
::query("SELECT uid FROM emails WHERE email = {?}", $email);
369 if ($res->numRows() == 1) {
370 return $res->fetchOneCell();
374 list($user) = explode('+', $user);
375 list($user) = explode('_', $user);
376 if ($domain == $globals->mail
->alias_dom ||
$domain == $globals->mail
->alias_dom2
) {
377 $res = XDB
::query("SELECT a.id
379 INNER JOIN virtual_redirect AS r USING(vid)
380 INNER JOIN aliases AS a ON (a.type = 'a_vie'
381 AND r.redirect = CONCAT(a.alias, '@{$globals->mail->domain2}'))
382 WHERE v.alias = CONCAT({?}, '@{$globals->mail->alias_dom}')", $user);
383 $id = $res->fetchOneCell();
384 return $id ?
$id : '0';
386 $res = XDB
::query("SELECT id FROM aliases WHERE alias = {?}", $user);
387 $id = $res->fetchOneCell();
388 return $id ?
$id : '0';
391 function createHash($line, $key)
393 $hash = implode(time(), $line) . rand();
399 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: