81b5b746 |
1 | <?php |
2 | /*************************************************************************** |
3 | * Copyright (C) 2003-2006 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 EmailModule extends PLModule |
23 | { |
81b5b746 |
24 | function handlers() |
25 | { |
26 | return array( |
27 | 'emails' => $this->make_hook('emails', AUTH_COOKIE), |
28 | 'emails/alias' => $this->make_hook('alias', AUTH_MDP), |
29 | 'emails/antispam' => $this->make_hook('antispam', AUTH_MDP), |
30 | 'emails/broken' => $this->make_hook('broken', AUTH_COOKIE), |
31 | 'emails/redirect' => $this->make_hook('redirect', AUTH_MDP), |
32 | 'emails/send' => $this->make_hook('send', AUTH_MDP), |
9e570fe0 |
33 | |
34 | 'admin/emails/duplicated' => $this->make_hook('duplicated', AUTH_MDP, 'admin') |
81b5b746 |
35 | ); |
36 | } |
37 | |
38 | function handler_emails(&$page) |
39 | { |
40 | global $globals; |
41 | |
42 | $page->changeTpl('emails/index.tpl'); |
43 | $page->assign('xorg_title','Polytechnique.org - Mes emails'); |
44 | |
cab08090 |
45 | $uid = S::v('uid'); |
81b5b746 |
46 | |
47 | if (Post::has('best')) { |
48 | // bestalias is the first bit : 1 |
49 | // there will be maximum 8 bits in flags : 255 |
08cce2ff |
50 | XDB::execute("UPDATE aliases SET flags=flags & (255 - 1) WHERE id={?}", $uid); |
51 | XDB::execute("UPDATE aliases SET flags=flags | 1 WHERE id={?} AND alias={?}", |
5e2307dc |
52 | $uid, Post::v('best')); |
81b5b746 |
53 | } |
54 | |
55 | // on regarde si on a affaire à un homonyme |
56 | $sql = "SELECT alias, (type='a_vie') AS a_vie, |
57 | (alias REGEXP '\\\\.[0-9]{2}$') AS cent_ans, |
58 | FIND_IN_SET('bestalias',flags) AS best, expire |
59 | FROM aliases |
60 | WHERE id = {?} AND type!='homonyme' |
61 | ORDER BY LENGTH(alias)"; |
08cce2ff |
62 | $page->assign('aliases', XDB::iterator($sql, $uid)); |
81b5b746 |
63 | |
64 | $sql = "SELECT email |
65 | FROM emails |
66 | WHERE uid = {?} AND FIND_IN_SET('active', flags)"; |
08cce2ff |
67 | $page->assign('mails', XDB::iterator($sql, $uid)); |
81b5b746 |
68 | |
69 | |
70 | // on regarde si l'utilisateur a un alias et si oui on l'affiche ! |
cab08090 |
71 | $forlife = S::v('forlife'); |
08cce2ff |
72 | $res = XDB::query( |
81b5b746 |
73 | "SELECT alias |
74 | FROM virtual AS v |
75 | INNER JOIN virtual_redirect AS vr USING(vid) |
76 | WHERE (redirect={?} OR redirect={?}) |
77 | AND alias LIKE '%@{$globals->mail->alias_dom}'", |
78 | $forlife.'@'.$globals->mail->domain, $forlife.'@'.$globals->mail->domain2); |
79 | $page->assign('melix', $res->fetchOneCell()); |
81b5b746 |
80 | } |
81 | |
82 | function handler_alias(&$page, $action = null, $value = null) |
83 | { |
84 | require_once 'validations.inc.php'; |
85 | |
86 | global $globals; |
87 | |
88 | $page->changeTpl('emails/alias.tpl'); |
89 | $page->assign('xorg_title','Polytechnique.org - Alias melix.net'); |
90 | |
cab08090 |
91 | $uid = S::v('uid'); |
92 | $forlife = S::v('forlife'); |
81b5b746 |
93 | |
94 | $page->assign('demande', AliasReq::get_request($uid)); |
95 | |
96 | if ($action == 'suppr' && $value) { |
97 | //Suppression d'un alias |
08cce2ff |
98 | XDB::execute( |
81b5b746 |
99 | 'DELETE virtual, virtual_redirect |
100 | FROM virtual |
101 | INNER JOIN virtual_redirect USING (vid) |
102 | WHERE alias = {?} AND (redirect = {?} OR redirect = {?})', $value, |
103 | $forlife.'@'.$globals->mail->domain, $forlife.'@'.$globals->mail->domain2); |
104 | } |
105 | |
106 | //Récupération des alias éventuellement existants |
08cce2ff |
107 | $res = XDB::query( |
81b5b746 |
108 | "SELECT alias, emails_alias_pub |
109 | FROM auth_user_quick, virtual |
110 | INNER JOIN virtual_redirect USING(vid) |
111 | WHERE ( redirect={?} OR redirect= {?} ) |
112 | AND alias LIKE '%@{$globals->mail->alias_dom}' AND user_id = {?}", |
113 | $forlife.'@'.$globals->mail->domain, |
cab08090 |
114 | $forlife.'@'.$globals->mail->domain2, S::v('uid')); |
81b5b746 |
115 | list($alias, $visibility) = $res->fetchOneRow(); |
116 | $page->assign('actuel', $alias); |
117 | |
118 | if ($action == 'ask' && Env::has('alias') and Env::has('raison')) { |
119 | //Si l'utilisateur vient de faire une damande |
120 | |
5e2307dc |
121 | $alias = Env::v('alias'); |
122 | $raison = Env::v('raison'); |
123 | $public = (Env::v('public', 'off') == 'on')?"public":"private"; |
81b5b746 |
124 | |
125 | $page->assign('r_alias', $alias); |
126 | $page->assign('r_raison', $raison); |
127 | if ($public == 'public') { |
128 | $page->assign('r_public', true); |
129 | } |
130 | |
131 | //Quelques vérifications sur l'alias (caractères spéciaux) |
132 | if (!preg_match( "/^[a-zA-Z0-9\-.]{3,20}$/", $alias)) { |
133 | $page->trig("L'adresse demandée n'est pas valide. |
134 | Vérifie qu'elle comporte entre 3 et 20 caractères |
135 | et qu'elle ne contient que des lettres non accentuées, |
136 | des chiffres ou les caractères - et ."); |
fd8f77de |
137 | return; |
81b5b746 |
138 | } else { |
139 | //vérifier que l'alias n'est pas déja pris |
08cce2ff |
140 | $res = XDB::query('SELECT COUNT(*) FROM virtual WHERE alias={?}', |
81b5b746 |
141 | $alias.'@'.$globals->mail->alias_dom); |
142 | if ($res->fetchOneCell() > 0) { |
143 | $page->trig("L'alias $alias@{$globals->mail->alias_dom} a déja été attribué. |
144 | Tu ne peux donc pas l'obtenir."); |
fd8f77de |
145 | return; |
81b5b746 |
146 | } |
147 | |
148 | //vérifier que l'alias n'est pas déja en demande |
149 | $it = new ValidateIterator (); |
150 | while($req = $it->next()) { |
151 | if ($req->type == "alias" and $req->alias == $alias) { |
152 | $page->trig("L'alias $alias@{$globals->mail->alias_dom} a déja été demandé. |
153 | Tu ne peux donc pas l'obtenir pour l'instant."); |
fd8f77de |
154 | return ; |
81b5b746 |
155 | } |
156 | } |
157 | |
158 | //Insertion de la demande dans la base, écrase les requêtes précédente |
159 | $myalias = new AliasReq($uid, $alias, $raison, $public); |
160 | $myalias->submit(); |
161 | $page->assign('success',$alias); |
fd8f77de |
162 | return; |
81b5b746 |
163 | } |
164 | } |
165 | elseif ($action == 'set' |
166 | && ($value == 'public' || $value == 'private')) |
167 | { |
168 | if ($value == 'public') { |
08cce2ff |
169 | XDB::execute("UPDATE auth_user_quick SET emails_alias_pub = 'public' |
cab08090 |
170 | WHERE user_id = {?}", S::v('uid')); |
81b5b746 |
171 | } else { |
08cce2ff |
172 | XDB::execute("UPDATE auth_user_quick SET emails_alias_pub = 'private' |
cab08090 |
173 | WHERE user_id = {?}", S::v('uid')); |
81b5b746 |
174 | } |
175 | |
176 | $visibility = $value; |
177 | } |
178 | |
179 | $page->assign('mail_public', ($visibility == 'public')); |
81b5b746 |
180 | } |
181 | |
182 | function handler_redirect(&$page, $action = null, $email = null) |
183 | { |
184 | global $globals; |
185 | |
186 | require_once 'emails.inc.php'; |
187 | |
188 | $page->changeTpl('emails/redirect.tpl'); |
189 | |
cab08090 |
190 | $uid = S::v('uid'); |
191 | $forlife = S::v('forlife'); |
81b5b746 |
192 | |
cab08090 |
193 | $redirect = new Redirect(S::v('uid')); |
81b5b746 |
194 | |
195 | if ($action == 'remove' && $email) { |
196 | $page->assign('retour', $redirect->delete_email($email)); |
197 | } |
198 | |
199 | if (Env::has('emailop')) { |
5e2307dc |
200 | $actifs = Env::v('emails_actifs', Array()); |
201 | if (Env::v('emailop') == "ajouter" && Env::has('email')) { |
202 | $page->assign('retour', $redirect->add_email(Env::v('email'))); |
81b5b746 |
203 | } elseif (empty($actifs)) { |
204 | $page->assign('retour', ERROR_INACTIVE_REDIRECTION); |
205 | } elseif (is_array($actifs)) { |
206 | $page->assign('retour', $redirect->modify_email($actifs, |
5e2307dc |
207 | Env::v('emails_rewrite',Array()))); |
81b5b746 |
208 | } |
209 | } |
210 | |
08cce2ff |
211 | $res = XDB::query( |
81b5b746 |
212 | "SELECT alias |
213 | FROM virtual |
214 | INNER JOIN virtual_redirect USING(vid) |
215 | WHERE (redirect={?} OR redirect={?}) |
216 | AND alias LIKE '%@{$globals->mail->alias_dom}'", |
217 | $forlife.'@'.$globals->mail->domain, $forlife.'@'.$globals->mail->domain2); |
218 | $melix = $res->fetchOneCell(); |
219 | if ($melix) { |
220 | list($melix) = explode('@', $melix); |
221 | $page->assign('melix',$melix); |
222 | } |
223 | |
08cce2ff |
224 | $res = XDB::query( |
81b5b746 |
225 | "SELECT alias,expire |
226 | FROM aliases |
227 | WHERE id={?} AND (type='a_vie' OR type='alias') |
228 | ORDER BY !FIND_IN_SET('usage',flags), LENGTH(alias)", $uid); |
229 | $page->assign('alias', $res->fetchAllAssoc()); |
230 | $page->assign('emails',$redirect->emails); |
81b5b746 |
231 | } |
232 | |
233 | function handler_antispam(&$page) |
234 | { |
235 | require_once 'emails.inc.php'; |
236 | |
237 | $page->changeTpl('emails/antispam.tpl'); |
238 | |
cab08090 |
239 | $bogo = new Bogo(S::v('uid')); |
81b5b746 |
240 | if (Env::has('statut_filtre')) { |
5e2307dc |
241 | $bogo->change(S::v('uid'), Env::i('statut_filtre')); |
81b5b746 |
242 | } |
243 | $page->assign('filtre',$bogo->level()); |
81b5b746 |
244 | } |
245 | |
246 | function handler_send(&$page) |
247 | { |
248 | global $globals; |
249 | |
250 | $page->changeTpl('emails/send.tpl'); |
251 | |
252 | $page->assign('xorg_title','Polytechnique.org - Envoyer un email'); |
253 | |
254 | // action si on recoit un formulaire |
5e2307dc |
255 | if (Env::v('submit') == 'Envoyer') |
81b5b746 |
256 | { |
5e2307dc |
257 | $to2 = join(', ', Env::v('contacts', Array())); |
258 | $txt = str_replace('^M', '', Env::v('contenu')); |
259 | $to = Env::v('to'); |
260 | $subj = Env::v('sujet'); |
261 | $from = Env::v('from'); |
262 | $cc = Env::v('cc'); |
263 | $bcc = Env::v('bcc'); |
81b5b746 |
264 | |
265 | if (empty($to) && empty($cc) && empty($to2)) { |
266 | $page->trig("Indique au moins un destinataire."); |
267 | } else { |
268 | require_once("diogenes/diogenes.hermes.inc.php"); |
269 | |
270 | $mymail = new HermesMailer(); |
271 | $mymail->setFrom($from); |
272 | $mymail->setSubject($subj); |
273 | if (!empty($to)) { $mymail->addTo($to); } |
274 | if (!empty($cc)) { $mymail->addCc($cc); } |
275 | if (!empty($bcc)) { $mymail->addBcc($bcc); } |
276 | if (!empty($to2)) { $mymail->addTo($to2); } |
ea3930d0 |
277 | $mymail->addHeader('X-XOrg-Login', S::v('bestalias') . '@' . $globals->mail->domain); |
81b5b746 |
278 | $mymail->setTxtBody(wordwrap($txt,72,"\n")); |
279 | if ($mymail->send()) { |
280 | $page->trig("Ton mail a bien été envoyé."); |
cab08090 |
281 | $_REQUEST = array('bcc' => S::v('bestalias').'@'.$globals->mail->domain); |
81b5b746 |
282 | } else { |
283 | $page->trig("Erreur lors de l'envoi du courriel, réessaye."); |
284 | } |
285 | } |
286 | } else { |
cab08090 |
287 | $_REQUEST['bcc'] = S::v('bestalias').'@'.$globals->mail->domain; |
81b5b746 |
288 | } |
289 | |
08cce2ff |
290 | $res = XDB::query( |
81b5b746 |
291 | "SELECT u.prenom, u.nom, u.promo, a.alias as forlife |
292 | FROM auth_user_md5 AS u |
293 | INNER JOIN contacts AS c ON (u.user_id = c.contact) |
294 | INNER JOIN aliases AS a ON (u.user_id=a.id AND FIND_IN_SET('bestalias',a.flags)) |
295 | WHERE c.uid = {?} |
cab08090 |
296 | ORDER BY u.nom, u.prenom", S::v('uid')); |
81b5b746 |
297 | $page->assign('contacts', $res->fetchAllAssoc()); |
81b5b746 |
298 | } |
299 | |
300 | function handler_broken(&$page, $warn = null, $email = null) |
301 | { |
302 | require_once 'emails.inc.php'; |
303 | |
304 | global $globals; |
305 | |
306 | $page->changeTpl('emails/broken.tpl'); |
307 | |
308 | if ($warn == 'warn' && $email) { |
309 | $email = valide_email($email); |
310 | // vérifications d'usage |
08cce2ff |
311 | $sel = XDB::query( |
81b5b746 |
312 | "SELECT e.uid, a.alias |
313 | FROM emails AS e |
314 | INNER JOIN auth_user_md5 AS u ON e.uid = u.user_id |
315 | INNER JOIN aliases AS a ON (e.uid = a.id AND type!='homonyme' |
316 | AND FIND_IN_SET('bestalias',a.flags)) |
317 | WHERE e.email={?}", $email); |
318 | |
319 | if (list($uid, $dest) = $sel->fetchOneRow()) { |
320 | // envoi du mail |
321 | $message = "Bonjour ! |
322 | |
323 | Ce mail a été généré automatiquement par le service de patte cassée de |
cab08090 |
324 | Polytechnique.org car un autre utilisateur, ".S::v('prenom').' '.S::v('nom').", |
81b5b746 |
325 | nous a signalé qu'en t'envoyant un mail, il avait reçu un message d'erreur |
326 | indiquant que ton adresse de redirection $email |
327 | ne fonctionnait plus ! |
328 | |
329 | Nous te suggérons de vérifier cette adresse, et le cas échéant de mettre |
2c388954 |
330 | à jour sur le site <{$globals->baseurl}/emails> tes adresses |
81b5b746 |
331 | de redirection... |
332 | |
333 | Pour plus de rensignements sur le service de patte cassée, n'hésites pas à |
2c388954 |
334 | consulter la page <{$globals->baseurl}/emails/broken>. |
81b5b746 |
335 | |
336 | |
337 | A bientôt sur Polytechnique.org ! |
338 | L'équipe d'administration <support@polytechnique.org>"; |
339 | |
340 | require_once("diogenes/diogenes.hermes.inc.php"); |
341 | $mail = new HermesMailer(); |
342 | $mail->setFrom('"Polytechnique.org" <support@polytechnique.org>'); |
343 | $mail->addTo("$dest@polytechnique.org"); |
344 | $mail->setSubject("Une de tes adresse de redirection Polytechnique.org ne marche plus !!"); |
345 | $mail->setTxtBody($message); |
346 | $mail->send(); |
347 | $page->trig("Mail envoyé ! :o)"); |
348 | } |
349 | } elseif (Post::has('email')) { |
5e2307dc |
350 | $email = valide_email(Post::v('email')); |
81b5b746 |
351 | |
352 | list(,$fqdn) = explode('@', $email); |
353 | $fqdn = strtolower($fqdn); |
354 | if ($fqdn == 'polytechnique.org' || $fqdn == 'melix.org' |
355 | || $fqdn == 'm4x.org' || $fqdn == 'melix.net') |
356 | { |
357 | $page->assign('neuneu', true); |
358 | } else { |
359 | $page->assign('email',$email); |
08cce2ff |
360 | $sel = XDB::query( |
81b5b746 |
361 | "SELECT e1.uid, e1.panne != 0 AS panne, count(e2.uid) AS nb_mails, |
362 | u.nom, u.prenom, u.promo |
363 | FROM emails as e1 |
364 | LEFT JOIN emails as e2 ON(e1.uid = e2.uid |
365 | AND FIND_IN_SET('active', e2.flags) |
366 | AND e1.email != e2.email) |
367 | INNER JOIN auth_user_md5 as u ON(e1.uid = u.user_id) |
368 | WHERE e1.email = {?} |
369 | GROUP BY e1.uid", $email); |
370 | if ($x = $sel->fetchOneAssoc()) { |
371 | // on écrit dans la base que l'adresse est cassée |
372 | if (!$x['panne']) { |
74938c82 |
373 | XDB::execute("UPDATE emails |
374 | SET panne=NOW(), |
375 | last=NOW(), |
376 | panne_level = 1 |
377 | WHERE email = {?}", $email); |
378 | } else { |
379 | XDB::execute("UPDATE emails |
380 | SET panne_level = 1 |
381 | WHERE email = {?} AND panne_level = 0"); |
81b5b746 |
382 | } |
383 | $page->assign_by_ref('x', $x); |
384 | } |
385 | } |
386 | } |
387 | } |
9e570fe0 |
388 | |
389 | function handler_duplicated(&$page, $action = 'list', $email = null) |
390 | { |
391 | $page->changeTpl('emails/duplicated.tpl'); |
392 | |
393 | $states = array('pending' => 'En attente...', |
394 | 'safe' => 'Pas d\'inquiétude', |
395 | 'unsafe' => 'Recherches en cours', |
396 | 'dangerous' => 'Usurpations par cette adresse'); |
397 | $page->assign('states', $states); |
398 | |
399 | switch (Post::v('action')) { |
400 | case 'create': |
401 | if (trim(Post::v('emailN')) != '') { |
402 | Xdb::execute('INSERT IGNORE INTO emails_watch (email, state, detection, last, uid, description) |
403 | VALUES ({?}, {?}, CURDATE(), NOW(), {?}, {?})', |
404 | trim(Post::v('emailN')), Post::v('stateN'), S::i('uid'), Post::v('descriptionN')); |
405 | }; |
406 | break; |
407 | |
408 | case 'edit': |
409 | Xdb::execute('UPDATE emails_watch |
410 | SET state = {?}, last = NOW(), uid = {?}, description = {?} |
411 | WHERE email = {?}', Post::v('stateN'), S::i('uid'), Post::v('descriptionN'), Post::v('emailN')); |
412 | break; |
413 | |
414 | default: |
415 | if ($action == 'delete' && !is_null($email)) { |
416 | Xdb::execute('DELETE FROM emails_watch WHERE email = {?}', $email); |
417 | } |
418 | } |
419 | if ($action != 'create' && $action != 'edit') { |
420 | $action = 'list'; |
421 | } |
422 | $page->assign('action', $action); |
423 | |
424 | if ($action == 'list') { |
425 | $sql = "SELECT w.email, w.detection, w.state, a.alias AS forlife |
426 | FROM emails_watch AS w |
ca6d07f4 |
427 | LEFT JOIN emails AS e USING(email) |
428 | LEFT JOIN aliases AS a ON (a.id = e.uid AND a.type = 'a_vie') |
9e570fe0 |
429 | ORDER BY w.state, w.email, a.alias"; |
430 | $it = Xdb::iterRow($sql); |
431 | |
432 | $table = array(); |
433 | $props = array(); |
434 | while (list($email, $date, $state, $forlife) = $it->next()) { |
435 | if (count($props) == 0 || $props['mail'] != $email) { |
436 | if (count($props) > 0) { |
437 | $table[] = $props; |
438 | } |
439 | $props = array('mail' => $email, |
440 | 'detection' => $date, |
441 | 'state' => $state, |
442 | 'users' => array($forlife)); |
443 | } else { |
444 | $props['users'][] = $forlife; |
445 | } |
446 | } |
447 | if (count($props) > 0) { |
448 | $table[] = $props; |
449 | } |
450 | $page->assign('table', $table); |
451 | } elseif ($action == 'edit') { |
452 | $sql = "SELECT w.detection, w.state, w.last, w.description, |
453 | a1.alias AS edit, a2.alias AS forlife |
454 | FROM emails_watch AS w |
ca6d07f4 |
455 | LEFT JOIN emails AS e USING(email) |
456 | INNER JOIN aliases AS a1 ON (a1.id = w.uid AND a1.type = 'a_vie') |
457 | LEFT JOIN aliases AS a2 ON (a2.id = e.uid AND a2.type = 'a_vie') |
9e570fe0 |
458 | WHERE w.email = {?} |
459 | ORDER BY a2.alias"; |
460 | $it = Xdb::iterRow($sql, $email); |
461 | |
462 | $props = array(); |
463 | while (list($detection, $state, $last, $description, $edit, $forlife) = $it->next()) { |
464 | if (count($props) == 0) { |
465 | $props = array('mail' => $email, |
466 | 'detection' => $detection, |
467 | 'state' => $state, |
468 | 'last' => $last, |
469 | 'description' => $description, |
470 | 'edit' => $edit, |
471 | 'users' => array($forlife)); |
472 | } else { |
473 | $props['users'][] = $forlife; |
474 | } |
475 | } |
476 | $page->assign('doublon', $props); |
477 | } |
478 | } |
81b5b746 |
479 | } |
480 | |
481 | ?> |