Properly unsubscribe from group MLs when leaving the group (Closes #806)
[platal.git] / modules / xnetgrp.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2008 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 function get_infos($email)
23 {
24 global $globals;
25 // look for uid instead of email if numeric
26 $field = is_numeric($email) ? 'uid' : 'email';
27
28 if ($field == 'email') {
29 $email = strtolower($email);
30 if (strpos($email, '@') === false) {
31 $email .= '@m4x.org';
32 }
33 list($mbox,$dom) = explode('@', $email);
34 }
35
36 $res = XDB::query(
37 "SELECT uid, nom, prenom, email, email AS email2, perms='admin', origine, sexe
38 FROM groupex.membres
39 WHERE $field = {?} AND asso_id = {?}", $email, $globals->asso('id'));
40
41 if ($res->numRows()) {
42 $user = $res->fetchOneAssoc();
43 if ($user['origine'] == 'X') {
44 $res = XDB::query("SELECT nom, prenom, promo, FIND_IN_SET('femme', flags) AS sexe
45 FROM auth_user_md5
46 WHERE user_id = {?}", $user['uid']);
47 $user = array_merge($user, $res->fetchOneAssoc());
48 }
49 return $user;
50 } elseif ($dom == 'polytechnique.org' || $dom == 'm4x.org') {
51 $res = XDB::query(
52 "SELECT user_id AS uid, u.promo,
53 IF(u.nom_usage<>'', u.nom_usage, u.nom) AS nom,
54 u.prenom, b.alias,
55 CONCAT(b.alias, '@m4x.org') AS email,
56 CONCAT(b.alias, '@polytechnique.org') AS email2,
57 m.perms = 'admin' AS perms, m.origine,
58 FIND_IN_SET('femme', u.flags) AS sexe
59 FROM auth_user_md5 AS u
60 INNER JOIN aliases AS a ON ( u.user_id = a.id AND a.type != 'homonyme' )
61 INNER JOIN aliases AS b ON ( u.user_id = b.id AND b.type = 'a_vie' )
62 LEFT JOIN groupex.membres AS m ON ( m.uid = u.user_id AND asso_id={?})
63 WHERE a.alias = {?} AND u.user_id < 50000", $globals->asso('id'), $mbox);
64 return $res->fetchOneAssoc();
65 }
66
67 return null;
68 }
69
70
71 class XnetGrpModule extends PLModule
72 {
73 function handlers()
74 {
75 return array(
76 '%grp' => $this->make_hook('index', AUTH_PUBLIC),
77 '%grp/asso.php' => $this->make_hook('index', AUTH_PUBLIC),
78 '%grp/logo' => $this->make_hook('logo', AUTH_PUBLIC),
79 '%grp/site' => $this->make_hook('site', AUTH_PUBLIC),
80 '%grp/edit' => $this->make_hook('edit', AUTH_MDP, 'groupadmin'),
81 '%grp/mail' => $this->make_hook('mail', AUTH_MDP, 'groupadmin'),
82 '%grp/forum' => $this->make_hook('forum', AUTH_MDP, 'groupmember'),
83 '%grp/annuaire' => $this->make_hook('annuaire', AUTH_MDP, 'groupannu'),
84 '%grp/annuaire/vcard' => $this->make_hook('vcard', AUTH_MDP, 'groupmember:groupannu'),
85 '%grp/trombi' => $this->make_hook('trombi', AUTH_MDP, 'groupannu'),
86 '%grp/geoloc' => $this->make_hook('geoloc', AUTH_MDP, 'groupannu'),
87 '%grp/subscribe' => $this->make_hook('subscribe', AUTH_MDP),
88 '%grp/unsubscribe' => $this->make_hook('unsubscribe', AUTH_MDP, 'groupmember'),
89
90 '%grp/change_rights' => $this->make_hook('change_rights', AUTH_MDP),
91
92 '%grp/admin/annuaire'
93 => $this->make_hook('admin_annuaire', AUTH_MDP, 'groupadmin'),
94
95 '%grp/member'
96 => $this->make_hook('admin_member', AUTH_MDP, 'groupadmin'),
97 '%grp/member/new'
98 => $this->make_hook('admin_member_new', AUTH_MDP, 'groupadmin'),
99 '%grp/member/new/ajax'
100 => $this->make_hook('admin_member_new_ajax', AUTH_MDP, 'user', NO_AUTH),
101 '%grp/member/del'
102 => $this->make_hook('admin_member_del', AUTH_MDP, 'groupadmin'),
103
104 '%grp/rss' => $this->make_hook('rss', AUTH_PUBLIC, 'user', NO_HTTPS),
105 '%grp/announce/new' => $this->make_hook('edit_announce', AUTH_MDP, 'groupadmin'),
106 '%grp/announce/edit' => $this->make_hook('edit_announce', AUTH_MDP, 'groupadmin'),
107 '%grp/announce/photo' => $this->make_hook('photo_announce', AUTH_PUBLIC),
108 '%grp/admin/announces' => $this->make_hook('admin_announce', AUTH_MDP, 'groupadmin'),
109 );
110 }
111
112 function handler_index(&$page, $arg = null)
113 {
114 global $globals, $platal;
115
116 if (!is_null($arg)) {
117 return PL_NOT_FOUND;
118 }
119 $page->changeTpl('xnetgrp/asso.tpl');
120
121 if (S::logged()) {
122 if (Env::has('read')) {
123 XDB::query('DELETE r.*
124 FROM groupex.announces_read AS r
125 INNER JOIN groupex.announces AS a ON a.id = r.announce_id
126 WHERE peremption < CURRENT_DATE()');
127 XDB::query('INSERT INTO groupex.announces_read
128 VALUES ({?}, {?})',
129 Env::i('read'), S::i('uid'));
130 pl_redirect("");
131 }
132 if (Env::has('unread')) {
133 XDB::query('DELETE FROM groupex.announces_read
134 WHERE announce_id={?} AND user_id={?}',
135 Env::i('unread'), S::i('uid'));
136 pl_redirect("#art" . Env::i('unread'));
137 }
138 $arts = XDB::iterator("SELECT a.*, u.nom, u.prenom, u.promo, l.alias AS forlife,
139 FIND_IN_SET('photo', a.flags) AS photo
140 FROM groupex.announces AS a
141 INNER JOIN auth_user_md5 AS u USING(user_id)
142 INNER JOIN aliases AS l ON (u.user_id = l.id AND l.type = 'a_vie')
143 LEFT JOIN groupex.announces_read AS r ON (r.user_id = {?} AND r.announce_id = a.id)
144 WHERE asso_id = {?} AND peremption >= CURRENT_DATE()
145 AND (promo_min = 0 OR promo_min <= {?})
146 AND (promo_max = 0 OR promo_max >= {?})
147 AND r.announce_id IS NULL
148 ORDER BY a.peremption",
149 S::i('uid'), $globals->asso('id'), S::i('promo'), S::i('promo'));
150 $index = XDB::iterator("SELECT a.id, a.titre, r.user_id IS NULL AS nonlu
151 FROM groupex.announces AS a
152 LEFT JOIN groupex.announces_read AS r ON (a.id = r.announce_id AND r.user_id = {?})
153 WHERE asso_id = {?} AND peremption >= CURRENT_DATE()
154 AND (promo_min = 0 OR promo_min <= {?})
155 AND (promo_max = 0 OR promo_max >= {?})
156 ORDER BY a.peremption",
157 S::i('uid'), $globals->asso('id'), S::i('promo'), S::i('promo'));
158 $page->assign('article_index', $index);
159 } else {
160 $arts = XDB::iterator("SELECT a.*, u.nom, u.prenom, u.promo, FIND_IN_SET('photo', a.flags) AS photo
161 FROM groupex.announces AS a
162 INNER JOIN auth_user_md5 AS u USING(user_id)
163 WHERE asso_id = {?} AND peremption >= CURRENT_DATE()
164 AND FIND_IN_SET('public', u.flags)",
165 $globals->asso('id'));
166 }
167
168 if (!S::has('core_rss_hash')) {
169 $page->setRssLink("Polytechnique.net :: {$globals->asso("nom")} :: News publiques",
170 $platal->ns . "rss/rss.xml");
171 } else {
172 $page->setRssLink("Polytechnique.net :: {$globals->asso("nom")} :: News",
173 $platal->ns . 'rss/'.S::v('forlife') .'/'.S::v('core_rss_hash').'/rss.xml');
174 }
175
176 $page->assign('articles', $arts);
177 }
178
179 function handler_logo(&$page)
180 {
181 global $globals;
182
183 $res = XDB::query("SELECT logo, logo_mime
184 FROM groupex.asso WHERE id = {?}",
185 $globals->asso('id'));
186 list($logo, $logo_mime) = $res->fetchOneRow();
187
188 if (!empty($logo)) {
189 header("Content-type: $mime");
190 header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
191 header('Last-Modified:' . gmdate('D, d M Y H:i:s') . ' GMT');
192 header('Cache-Control: no-cache, must-revalidate');
193 header('Pragma: no-cache');
194 echo $logo;
195 } else {
196 header('Content-type: image/jpeg');
197 header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
198 header('Last-Modified:' . gmdate('D, d M Y H:i:s') . ' GMT');
199 header('Cache-Control: no-cache, must-revalidate');
200 header('Pragma: no-cache');
201 readfile(dirname(__FILE__).'/../htdocs/images/dflt_carre.jpg');
202 }
203
204 exit;
205 }
206
207 function handler_site(&$page)
208 {
209 global $globals;
210 $site = $globals->asso('site');
211 if (!$site) {
212 $page->trig('Le groupe n\'a pas de site web.');
213 return $this->handler_index($page);
214 }
215 http_redirect($site);
216 exit;
217 }
218
219 function handler_edit(&$page)
220 {
221 global $globals;
222 $page->changeTpl('xnetgrp/edit.tpl');
223
224 if (Post::has('submit')) {
225 S::assert_xsrf_token();
226
227 if (S::has_perms()) {
228 if (Post::v('mail_domain') && (strstr(Post::v('mail_domain'), '.') === false)) {
229 $page->trig("le domaine doit être un FQDN (aucune modif effectuée) !!!");
230 return;
231 }
232 XDB::execute(
233 "UPDATE groupex.asso
234 SET nom={?}, diminutif={?}, cat={?}, dom={?},
235 descr={?}, site={?}, mail={?}, resp={?},
236 forum={?}, mail_domain={?}, ax={?}, pub={?},
237 sub_url={?}, inscriptible={?}, unsub_url={?},
238 flags='wiki_desc'
239 WHERE id={?}",
240 Post::v('nom'), Post::v('diminutif'),
241 Post::v('cat'), Post::i('dom'),
242 Post::v('descr'), Post::v('site'),
243 Post::v('mail'), Post::v('resp'),
244 Post::v('forum'), Post::v('mail_domain'),
245 Post::has('ax'), Post::v('pub'),
246 Post::v('sub_url'), Post::v('inscriptible'),
247 Post::v('unsub_url'),$globals->asso('id'));
248 if (Post::v('mail_domain')) {
249 XDB::execute('INSERT INTO virtual_domains (domain) VALUES({?})',
250 Post::v('mail_domain'));
251 }
252 } else {
253 XDB::execute(
254 "UPDATE groupex.asso
255 SET descr={?}, site={?}, mail={?}, resp={?},
256 forum={?}, ax={?}, pub= {?}, sub_url={?},
257 unsub_url={?},flags='wiki_desc'
258 WHERE id={?}",
259 Post::v('descr'), Post::v('site'),
260 Post::v('mail'), Post::v('resp'),
261 Post::v('forum'), Post::has('ax'),
262 Post::has('pub'),
263 Post::v('sub_url'), Post::v('unsub_url'),
264 $globals->asso('id'));
265 }
266
267 if ($_FILES['logo']['name']) {
268 $logo = file_get_contents($_FILES['logo']['tmp_name']);
269 $mime = $_FILES['logo']['type'];
270 XDB::execute('UPDATE groupex.asso
271 SET logo={?}, logo_mime={?}
272 WHERE id={?}', $logo, $mime,
273 $globals->asso('id'));
274 }
275
276 pl_redirect('../'.Post::v('diminutif', $globals->asso('diminutif')).'/edit');
277 }
278
279 if (S::has_perms()) {
280 $dom = XDB::iterator('SELECT * FROM groupex.dom ORDER BY nom');
281 $page->assign('dom', $dom);
282 $page->assign('super', true);
283 }
284 if (!$globals->asso('wiki_desc') && $globals->asso('descr')) {
285 $page->trig("Attention, le format de la description a changé et utilise désormais la syntaxe wiki "
286 . "intégrée au site. Il te faudra probablement adapter le formatage du texte actuel pour "
287 . "qu'il s'affiche correctement avec cette nouvelle syntaxe.");
288 }
289 }
290
291 function handler_mail(&$page)
292 {
293 global $globals;
294
295 $page->changeTpl('xnetgrp/mail.tpl');
296 $mmlist = new MMList(S::v('uid'), S::v('password'),
297 $globals->asso('mail_domain'));
298 $page->assign('listes', $mmlist->get_lists());
299 $page->addJsLink('ajax.js');
300
301 if (Post::has('send')) {
302 $from = Post::v('from');
303 $sujet = Post::v('sujet');
304 $body = Post::v('body');
305
306 $mls = array_keys(Env::v('ml', array()));
307 $mbr = array_keys(Env::v('membres', array()));
308
309 require_once dirname(__FILE__) . '/xnetgrp/mail.inc.php';
310 set_time_limit(120);
311 $tos = get_all_redirects($mbr, $mls, $mmlist);
312 $upload = PlUpload::get($_FILES['uploaded'], S::v('forlife'), 'xnet.emails', true);
313 send_xnet_mails($from, $sujet, $body, Env::v('wiki'), $tos, Post::v('replyto'), $upload, @$_FILES['uploaded']['name']);
314 if ($upload) {
315 $upload->rm();
316 }
317 $page->kill("Mail envoyé !");
318 $page->assign('sent', true);
319 }
320 }
321
322 function handler_forum(&$page, $group = null, $artid = null)
323 {
324 global $globals;
325 $page->changeTpl('xnetgrp/forum.tpl');
326 if (!$globals->asso('forum')) {
327 return PL_NOT_FOUND;
328 }
329 require_once 'banana/forum.inc.php';
330 $get = array();
331 get_banana_params($get, $globals->asso('forum'), $group, $artid);
332 run_banana($page, 'ForumsBanana', $get);
333 }
334
335 function handler_annuaire(&$page, $action = null, $subaction = null)
336 {
337 global $globals;
338
339 if ($action == 'search') {
340 http_redirect("https://www.polytechnique.org/search/adv?rechercher=Chercher&groupex={$globals->asso('id')}"
341 . "&cityid=" . Env::v('cityid') . "&mapid=" . Env::v('mapid'));
342 } else if ($action == 'geoloc' || $action == 'trombi') {
343 $view = new UserSet();
344 $view->addMod('trombi', 'Trombinoscope');
345 $view->addMod('geoloc', 'Planisphère', false, array('with_annu' => 'annuaire/search'));
346 $view->apply('annuaire', $page, $action, $subaction);
347 if ($action == 'geoloc' && $subaction) {
348 return;
349 }
350 }
351 $page->changeTpl('xnetgrp/annuaire.tpl');
352
353 $sort = Env::v('order');
354 switch (Env::v('order')) {
355 case 'promo' : $group = 'promo'; $tri = 'promo_o DESC, nom, prenom'; break;
356 case 'promo_inv': $group = 'promo'; $tri = 'promo_o, nom, prenom'; break;
357 case 'alpha_inv': $group = 'initiale'; $tri = 'nom DESC, prenom DESC, promo'; break;
358 default : $group = 'initiale'; $tri = 'nom, prenom, promo'; $sort = 'alpha';
359 }
360 $page->assign('sort', $sort);
361
362 if ($group == 'initiale') {
363 $res = XDB::iterRow(
364 'SELECT UPPER(SUBSTRING(
365 IF(m.origine="X", IF(u.nom_usage<>"", u.nom_usage, u.nom),m.nom),
366 1, 1)) as letter, COUNT(*)
367 FROM groupex.membres AS m
368 LEFT JOIN auth_user_md5 AS u ON ( u.user_id = m.uid)
369 WHERE asso_id = {?} and (u.perms != \'pending\' OR m.email IS NOT NULL)
370 GROUP BY letter
371 ORDER BY letter', $globals->asso('id'));
372 } else {
373 $res = XDB::iterRow(
374 'SELECT IF(m.origine="X",u.promo,
375 IF(m.origine="ext", "extérieur", "personne morale")) AS promo,
376 COUNT(*), IF(m.origine="X",u.promo,"") AS promo_o
377 FROM groupex.membres AS m
378 LEFT JOIN auth_user_md5 AS u ON ( u.user_id = m.uid )
379 WHERE asso_id = {?}
380 GROUP BY promo
381 ORDER BY promo_o DESC', $globals->asso('id'));
382 }
383 $alphabet = array();
384 $nb_tot = 0;
385 while (list($char, $nb) = $res->next()) {
386 $alphabet[] = $char;
387 $nb_tot += $nb;
388 if (Env::has($group) && $char == strtoupper(Env::v($group))) {
389 $tot = $nb;
390 }
391 }
392 $page->assign('group', $group);
393 $page->assign('request_group', Env::v($group));
394 $page->assign('only_admin', Env::has('admin'));
395 $page->assign('alphabet', $alphabet);
396 $page->assign('nb_tot', $nb_tot);
397
398 $ofs = Env::i('offset');
399 $tot = Env::v($group) ? $tot : $nb_tot;
400 $nbp = intval(($tot-1)/NB_PER_PAGE);
401 $links = array();
402 if ($ofs) {
403 $links['précédent'] = $ofs-1;
404 }
405 for ($i = 0; $i <= $nbp; $i++) {
406 $links[(string)($i+1)] = $i;
407 }
408 if ($ofs < $nbp) {
409 $links['suivant'] = $ofs+1;
410 }
411 if (count($links)>1) {
412 $page->assign('links', $links);
413 }
414
415 $ini = '';
416 if (Env::has('initiale')) {
417 $ini = 'AND IF(m.origine="X",
418 IF(u.nom_usage<>"", u.nom_usage, u.nom),
419 m.nom) LIKE "'.addslashes(Env::v('initiale')).'%"';
420 } elseif (Env::has('promo')) {
421 $ini = 'AND IF(m.origine="X", u.promo, IF(m.origine="ext", "extérieur", "personne morale")) = "'
422 .addslashes(Env::v('promo')).'"';
423 } elseif (Env::has('admin')) {
424 $ini = 'AND m.perms = "admin"';
425 }
426
427 $ann = XDB::iterator(
428 "SELECT IF(m.origine='X',IF(u.nom_usage<>'', u.nom_usage, u.nom) ,m.nom) AS nom,
429 IF(m.origine='X',u.prenom,m.prenom) AS prenom,
430 IF(m.origine='X', u.promo, IF(m.origine='ext', 'extérieur', 'personne morale')) AS promo,
431 IF(m.origine='X',u.promo,'') AS promo_o,
432 IF(m.origine='X' AND u.perms != 'pending',a.alias,m.email) AS email,
433 IF(m.origine='X',FIND_IN_SET('femme', u.flags), m.sexe) AS femme,
434 m.perms='admin' AS admin,
435 m.origine='X' AS x,
436 u.perms!='pending' AS inscrit,
437 m.uid, IF(e.email IS NULL AND FIND_IN_SET('googleapps', u.mail_storage) = 0, NULL, 1) AS actif
438 FROM groupex.membres AS m
439 LEFT JOIN auth_user_md5 AS u ON ( u.user_id = m.uid )
440 LEFT JOIN aliases AS a ON ( a.id = m.uid AND a.type='a_vie' )
441 LEFT JOIN emails AS e ON ( e.flags = 'active' AND e.uid = m.uid)
442 WHERE m.asso_id = {?} $ini
443 AND (m.origine != 'X' OR u.perms != 'pending' OR m.email IS NOT NULL)
444 GROUP BY m.uid
445 ORDER BY $tri
446 LIMIT {?},{?}", $globals->asso('id'), $ofs*NB_PER_PAGE, NB_PER_PAGE);
447 $page->assign('ann', $ann);
448 $page->jsonAssign('ann', $ann);
449 }
450
451 function handler_trombi(&$page)
452 {
453 pl_redirect('annuaire/trombi');
454 }
455
456 function handler_geoloc(&$page)
457 {
458 pl_redirect('annuaire/geoloc');
459 }
460
461 function handler_vcard(&$page, $photos = null)
462 {
463 global $globals;
464 $res = XDB::query('SELECT uid
465 FROM groupex.membres
466 WHERE asso_id = {?}', $globals->asso('id'));
467 $vcard = new VCard($res->fetchColumn(), $photos == 'photos', 'Membre du groupe ' . $globals->asso('nom'));
468 $vcard->do_page($page);
469 }
470
471 function handler_subscribe(&$page, $u = null)
472 {
473 global $globals;
474 $page->changeTpl('xnetgrp/inscrire.tpl');
475
476 if (!$globals->asso('inscriptible'))
477 $page->kill("Il n'est pas possible de s'inscire en ligne à ce "
478 ."groupe. Essaie de joindre le contact indiqué "
479 ."sur la page de présentation.");
480
481 if (!is_null($u) && may_update()) {
482 $page->assign('u', $u);
483 $res = XDB::query("SELECT u.nom, u.prenom, u.promo, u.user_id, FIND_IN_SET('femme', u.flags)
484 FROM auth_user_md5 AS u
485 INNER JOIN aliases AS al ON (al.id = u.user_id AND al.type != 'liste')
486 WHERE al.alias = {?}", $u);
487
488 if (list($nom, $prenom, $promo, $uid, $sexe) = $res->fetchOneRow()) {
489 $res = XDB::query("SELECT COUNT(*)
490 FROM groupex.membres AS m
491 INNER JOIN aliases AS a ON (m.uid = a.id AND a.type != 'homonyme')
492 WHERE a.alias = {?} AND m.asso_id = {?}",
493 $u, $globals->asso('id'));
494 $n = $res->fetchOneCell();
495 if ($n) {
496 $page->kill("$prenom $nom est déjà membre du groupe !");
497 return;
498 }
499 elseif (Env::has('accept'))
500 {
501 XDB::execute("INSERT INTO groupex.membres (asso_id, uid)
502 VALUES ({?}, {?})",
503 $globals->asso('id'), $uid);
504 $mailer = new PlMailer();
505 $mailer->addTo("$u@polytechnique.org");
506 $mailer->setFrom('"'.S::v('prenom').' '.S::v('nom')
507 .'" <'.S::v('forlife').'@polytechnique.org>');
508 $mailer->setSubject('['.$globals->asso('nom').'] Demande d\'inscription');
509 $message = ($sexe ? 'Chère' : 'Cher') . " Camarade,\n"
510 . "\n"
511 . " Suite à ta demande d'adhésion à ".$globals->asso('nom').",\n"
512 . "j'ai le plaisir de t'annoncer que ton inscription a été validée !\n"
513 . "\n"
514 . "Bien cordialement,\n"
515 . "-- \n"
516 . "{$_SESSION["prenom"]} {$_SESSION["nom"]}.";
517 $mailer->setTxtBody($message);
518 $mailer->send();
519 pl_redirect("member/$u");
520 }
521 elseif (Env::has('refuse'))
522 {
523 $mailer = new PlMailer();
524 $mailer->addTo("$u@polytechnique.org");
525 $mailer->setFrom('"'.S::v('prenom').' '.S::v('nom')
526 .'" <'.S::v('forlife').'@polytechnique.org>');
527 $mailer->setSubject('['.$globals->asso('nom').'] Demande d\'inscription annulée');
528 $mailer->setTxtBody(Env::v('motif'));
529 $mailer->send();
530 $page->kill("La demande de $prenom $nom a bien été refusée.");
531 } else {
532 $page->assign('show_form', true);
533 $page->assign('prenom', $prenom);
534 $page->assign('nom', $nom);
535 $page->assign('promo', $promo);
536 $page->assign('uid', $uid);
537 }
538 return;
539 }
540 return PL_NOT_FOUND;
541 }
542
543 if (is_member()) {
544 $page->kill("Tu es déjà membre !");
545 return;
546 }
547
548 if (Post::has('inscrire')) {
549 $res = XDB::query('SELECT IF(m.email IS NULL,
550 CONCAT(al.alias,"@polytechnique.org"),
551 m.email)
552 FROM groupex.membres AS m
553 INNER JOIN aliases AS al ON (al.type = "a_vie"
554 AND al.id = m.uid)
555 WHERE perms="admin" AND m.asso_id = {?}',
556 $globals->asso('id'));
557 $emails = $res->fetchColumn();
558 $to = implode(',', $emails);
559
560 $append = "\n"
561 . "-- \n"
562 . "Ce message a été envoyé suite à la demande d'inscription de\n"
563 . S::v('prenom').' '.S::v('nom').' (X'.S::v('promo').")\n"
564 . "Via le site www.polytechnique.net. Tu peux choisir de valider ou\n"
565 . "de refuser sa demande d'inscription depuis la page :\n"
566 .
567 "http://www.polytechnique.net/".$globals->asso("diminutif")."/subscribe/"
568 .S::v('forlife')."\n"
569 . "\n"
570 . "En cas de problème, contacter l'équipe de Polytechnique.org\n"
571 . "à l'adresse : support@polytechnique.org\n";
572
573 if (!$to) {
574 $to = $globals->asso("mail").", support@polytechnique.org";
575 $append = "\n-- \nLe groupe ".$globals->asso("nom")
576 ." n'a pas d'administrateur, l'équipe de"
577 ." Polytechnique.org a été prévenue et va rapidement"
578 ." résoudre ce problème.\n";
579 }
580
581 $mailer = new PlMailer();
582 $mailer->addTo($to);
583 $mailer->setFrom('"'.S::v('prenom').' '.S::v('nom')
584 .'" <'.S::v('forlife').'@polytechnique.org>');
585 $mailer->setSubject('['.$globals->asso('nom').'] Demande d\'inscription');
586 $mailer->setTxtBody(Post::v('message').$append);
587 $mailer->send();
588 }
589 }
590
591 function handler_change_rights(&$page)
592 {
593 if (Env::has('right') && (may_update() || S::has('suid'))) {
594 switch (Env::v('right')) {
595 case 'admin':
596 XnetSession::killSuid();
597 break;
598 case 'anim':
599 XnetSession::doSelfSuid();
600 may_update(true);
601 is_member(true);
602 break;
603 case 'member':
604 XnetSession::doSelfSuid();
605 may_update(false, true);
606 is_member(true);
607 break;
608 case 'logged':
609 XnetSession::doSelfSuid();
610 may_update(false, true);
611 is_member(false, true);
612 break;
613 }
614 }
615 http_redirect($_SERVER['HTTP_REFERER']);
616 }
617
618 function handler_admin_annuaire(&$page)
619 {
620 global $globals;
621
622 require_once dirname(__FILE__) . '/xnetgrp/mail.inc.php';
623 $page->changeTpl('xnetgrp/annuaire-admin.tpl');
624 $mmlist = new MMList(S::v('uid'), S::v('password'),
625 $globals->asso('mail_domain'));
626 $lists = $mmlist->get_lists();
627 if (!$lists) $lists = array();
628 $listes = array_map(create_function('$arr', 'return $arr["list"];'), $lists);
629
630 $subscribers = array();
631
632 foreach ($listes as $list) {
633 list(,$members) = $mmlist->get_members($list);
634 $mails = array_map(create_function('$arr', 'return $arr[1];'), $members);
635 $subscribers = array_unique(array_merge($subscribers, $mails));
636 }
637
638 $not_in_group_x = array();
639 $not_in_group_ext = array();
640
641 foreach ($subscribers as $mail) {
642 $res = XDB::query(
643 'SELECT COUNT(*)
644 FROM groupex.membres AS m
645 LEFT JOIN auth_user_md5 AS u ON (m.uid=u.user_id AND m.uid<50000)
646 LEFT JOIN aliases AS a ON (a.id=u.user_id and a.type="a_vie")
647 WHERE asso_id = {?} AND
648 (m.email = {?} OR CONCAT(a.alias, "@polytechnique.org") = {?})',
649 $globals->asso('id'), $mail, $mail);
650 if ($res->fetchOneCell() == 0) {
651 if (strstr($mail, '@polytechnique.org') === false) {
652 $not_in_group_ext[] = $mail;
653 } else {
654 $not_in_group_x[] = $mail;
655 }
656 }
657 }
658
659 $page->assign('not_in_group_ext', $not_in_group_ext);
660 $page->assign('not_in_group_x', $not_in_group_x);
661 $page->assign('lists', $lists);
662 }
663
664 function handler_admin_member_new(&$page, $email = null)
665 {
666 global $globals;
667
668 $page->changeTpl('xnetgrp/membres-add.tpl');
669 $page->addJsLink('ajax.js');
670
671 if (is_null($email)) {
672 return;
673 }
674
675 if (strpos($email, '@') === false) {
676 $x = true;
677 } else {
678 list(,$fqdn) = explode('@', $email, 2);
679 $fqdn = strtolower($fqdn);
680 $x = ($fqdn == 'polytechnique.org' || $fqdn == 'melix.org' ||
681 $fqdn == 'm4x.org' || $fqdn == 'melix.net');
682 }
683 if ($x) {
684 require_once 'user.func.inc.php';
685 if ($forlife = get_user_forlife($email)) {
686 XDB::execute(
687 'INSERT INTO groupex.membres (uid,asso_id,origine)
688 SELECT user_id,{?},"X"
689 FROM auth_user_md5 AS u
690 INNER JOIN aliases AS a ON (u.user_id = a.id)
691 WHERE a.alias={?}', $globals->asso('id'), $forlife);
692 pl_redirect("member/$forlife");
693 } else {
694 $page->trig($email." n'est pas un alias polytechnique.org valide.");
695 }
696 } else {
697 require_once 'xorg.misc.inc.php';
698 if (isvalid_email($email)) {
699 if (Env::v('x') && Env::has('userid') && Env::i('userid')) {
700 $uid = Env::i('userid');
701 $res = XDB::query("SELECT *
702 FROM auth_user_md5
703 WHERE user_id = {?} AND perms = 'pending'", $uid);
704 if ($res->numRows() == 1) {
705 if (Env::v('market')) {
706 $market = Marketing::get($uid, $email);
707 if (!$market) {
708 $market = new Marketing($uid, $email, 'group', $globals->asso('nom'),
709 Env::v('market_from'), S::v('uid'));
710 $market->add();
711 }
712 }
713 XDB::execute('INSERT INTO groupex.membres (uid, asso_id, origine, email)
714 VALUES ({?}, {?}, "X", {?})',
715 $uid, $globals->asso('id'), $email);
716 pl_redirect("member/$email");
717 }
718 $page->trig("Utilisateur invalide");
719 } else {
720 $res = XDB::query('SELECT MAX(uid)+1 FROM groupex.membres');
721 $uid = max(intval($res->fetchOneCell()), 50001);
722 XDB::execute('INSERT INTO groupex.membres (uid,asso_id,origine,email)
723 VALUES({?},{?},"ext",{?})', $uid,
724 $globals->asso('id'), $email);
725 pl_redirect("member/$email");
726 }
727 } else {
728 $page->trig("« <strong>$email</strong> » n'est pas une adresse mail valide.");
729 }
730 }
731 }
732
733 function handler_admin_member_new_ajax(&$page)
734 {
735 header('Content-Type: text/html; charset="UTF-8"');
736 $page->changeTpl('xnetgrp/membres-new-search.tpl', NO_SKIN);
737 $res = null;
738 if (Env::has('login')) {
739 require_once 'user.func.inc.php';
740 $res = get_not_registered_user(Env::v('login'), true);
741 }
742 if (is_null($res)) {
743 list($nom, $prenom) = str_replace(array('-', ' ', "'"), '%', array(Env::v('nom'), Env::v('prenom')));
744 $where = "perms = 'pending'";
745 if (!empty($nom)) {
746 $where .= " AND nom LIKE '%$nom%'";
747 }
748 if (!empty($prenom)) {
749 $where .= " AND prenom LIKE '%$prenom%'";
750 }
751 if (preg_match('/^[0-9]{4}$/', Env::v('promo'))) {
752 $where .= " AND promo = " . Env::i('promo');
753 } elseif (preg_match('/^[0-9]{2}$/', Env::v('promo'))) {
754 $where .= " AND MOD(promo, 100) = " . Env::i('promo');
755 } elseif (Env::has('promo')) {
756 return;
757 }
758 $res = XDB::iterator("SELECT user_id, nom, prenom, promo
759 FROM auth_user_md5
760 WHERE $where");
761 }
762 if ($res && $res->total() < 30) {
763 $page->assign("choix", $res);
764 }
765 }
766
767 function unsubscribe(&$user)
768 {
769 global $globals, $page;
770 XDB::execute(
771 "DELETE FROM groupex.membres WHERE uid={?} AND asso_id={?}",
772 $user['uid'], $globals->asso('id'));
773
774 $user_same_email = get_infos($user['email']);
775 $domain = $globals->asso('mail_domain');
776
777 if (!$domain || (!empty($user_same_email) && $user_same_email['uid'] != $user['uid'])) {
778 return true;
779 }
780
781 $mmlist = new MMList(S::v('uid'), S::v('password'), $domain);
782 $listes = $mmlist->get_lists($user['email2']);
783
784 $may_update = may_update();
785 $warning = false;
786 foreach ($listes as $liste) {
787 if ($liste['sub'] == 2) {
788 if ($may_update) {
789 $mmlist->mass_unsubscribe($liste['list'], Array($user['email2']));
790 } else {
791 $mmlist->unsubscribe($liste['list']);
792 }
793 } elseif ($liste['sub']) {
794 $page->trig("{$user['prenom']} {$user['nom']} a une"
795 ." demande d'inscription en cours sur la"
796 ." liste {$liste['list']}@ !");
797 $warning = true;
798 }
799 }
800
801 XDB::execute(
802 "DELETE FROM virtual_redirect
803 USING virtual_redirect
804 INNER JOIN virtual USING(vid)
805 WHERE redirect={?} AND alias LIKE {?}", $user['email'], '%@'.$domain);
806 return !$warning;
807 }
808
809 function handler_unsubscribe(&$page)
810 {
811 $page->changeTpl('xnetgrp/membres-del.tpl');
812 $user = get_infos(S::v('forlife'));
813 if (empty($user)) {
814 return PL_NOT_FOUND;
815 }
816 $page->assign('self', true);
817 $page->assign('user', $user);
818
819 if (!Post::has('confirm')) {
820 return;
821 }
822
823 if ($this->unsubscribe($user)) {
824 $page->trig('Vous avez été désinscrit du groupe avec succès.');
825 } else {
826 $page->trig('Vous avez été désinscrit du groupe, mais des erreurs se sont produites lors des désinscriptions des alias et des mailing-lists.');
827 }
828 $page->assign('is_member', is_member(true));
829 }
830
831 function handler_admin_member_del(&$page, $user = null)
832 {
833 $page->changeTpl('xnetgrp/membres-del.tpl');
834 $user = get_infos($user);
835 if (empty($user)) {
836 return PL_NOT_FOUND;
837 }
838 $page->assign('user', $user);
839
840 if (!Post::has('confirm')) {
841 return;
842 }
843
844 if ($this->unsubscribe($user)) {
845 $page->trig("{$user['prenom']} {$user['nom']} a été désabonné du groupe !");
846 } else {
847 $page->trig("{$user['prenom']} {$user['nom']} a été désabonné du groupe, mais des erreurs subsistent !");
848 }
849 }
850
851 private function changeLogin(PlatalPage &$page, array &$user, MMList &$mmlist, $login)
852 {
853 require_once 'user.func.inc.php';
854 // Search the uid of the user...
855 $res = XDB::query("SELECT f.id, f.alias
856 FROM aliases AS a
857 INNER JOIN aliases AS f ON (f.id = a.id AND f.type = 'a_vie')
858 WHERE a.alias = {?}",
859 $login);
860 if ($res->numRows() == 0) {
861 $x = get_not_registered_user($login);
862 if (!$x) {
863 $page->trig("Le login $login ne correspond à aucun X.");
864 return false;
865 } else if (count($x) > 1) {
866 $page->trig("Le login $login correspond a plusieurs camarades.");
867 return false;
868 }
869 $uid = $x[0]['user_id'];
870 $sub = false;
871 } else {
872 list($uid, $login) = $res->fetchOneRow();
873 $sub = true;
874 }
875
876 // Check if the user is already in the group
877 global $globals;
878 $res = XDB::query("SELECT uid, email
879 FROM groupex.membres
880 WHERE uid = {?} AND asso_id = {?}",
881 $uid, $globals->asso('id'));
882 if ($res->numRows()) {
883 list($uid, $email) = $res->fetchOneRow();
884 XDB::execute("DELETE FROM groupex.membres
885 WHERE uid = {?}",
886 $user['uid']);
887 } else {
888 $email = $user['email'];
889 XDB::execute("UPDATE groupex.membres
890 SET uid = {?}, origine = 'X'
891 WHERE uid = {?} AND asso_id = {?}",
892 $uid, $user['uid'], $globals->asso('id'));
893 }
894 if ($sub) {
895 $email = $login . '@' . $globals->mail->domain;
896 }
897
898 // Update subscription to aliases
899 if ($email != $user['email']) {
900 XDB::execute("UPDATE IGNORE virtual_redirect AS vr
901 INNER JOIN virtual AS v ON(vr.vid = v.vid AND SUBSTRING_INDEX(alias, '@', 2) = {?})
902 SET vr.redirect = {?}
903 WHERE vr.redirect = {?}",
904 $globals->asso('mail_domain'), $email, $user['email']);
905 XDB::execute("DELETE vr.*
906 FROM virtual_redirect AS vr
907 INNER JOIN virtual AS v ON(vr.vid = v.vid AND SUBSTRING_INDEX(alias, '@', 2) = {?})
908 WHERE vr.redirect = {?}",
909 $globals->asso('mail_domain'), $user['email']);
910 foreach (Env::v('ml1', array()) as $ml => $state) {
911 $mmlist->replace_email($ml, $user['email'], $email);
912 }
913 }
914 return $login;
915 }
916
917 function handler_admin_member(&$page, $user)
918 {
919 global $globals;
920
921 $page->changeTpl('xnetgrp/membres-edit.tpl');
922
923 $user = get_infos($user);
924 if (empty($user)) {
925 return PL_NOT_FOUND;
926 }
927
928 $mmlist = new MMList(S::v('uid'), S::v('password'),
929 $globals->asso('mail_domain'));
930
931 if (Post::has('change')) {
932 // Convert user status to X
933 if ($user['origine'] == 'ext' && trim(Post::v('login_X'))) {
934 $forlife = $this->changeLogin($page, $user, $mmlist, trim(Post::v('login_X')));
935 if ($forlife) {
936 pl_redirect('member/' . $forlife);
937 }
938 }
939
940 // Update user info
941 $email_changed = ($user['origine'] != 'X' && strtolower($user['email']) != strtolower(Post::v('email')));
942 $from_email = $user['email'];
943 if ($user['origine'] != 'X') {
944 $user['nom'] = Post::v('nom');
945 $user['prenom'] = (Post::v('origine') == 'ext') ? Post::v('prenom') : '';
946 $user['sexe'] = (Post::v('origine') == 'ext') ? Post::v('sexe') : 0;
947 $user['origine'] = Post::v('origine');
948 XDB::query('UPDATE groupex.membres
949 SET prenom={?}, nom={?}, email={?}, sexe={?}, origine={?}
950 WHERE uid={?} AND asso_id={?}',
951 $user['prenom'], $user['nom'], Post::v('email'),
952 $user['sexe'], $user['origine'], $user['uid'],
953 $globals->asso('id'));
954 $user['email'] = Post::v('email');
955 $user['email2'] = Post::v('email');
956 }
957
958 $perms = Post::i('is_admin');
959 if ($user['perms'] != $perms) {
960 XDB::query('UPDATE groupex.membres SET perms={?}
961 WHERE uid={?} AND asso_id={?}',
962 $perms ? 'admin' : 'membre',
963 $user['uid'], $globals->asso('id'));
964 $user['perms'] = $perms;
965 $page->trig('Permissions modifiées !');
966 }
967
968 // Update ML subscriptions
969 foreach (Env::v('ml1', array()) as $ml => $state) {
970 $ask = empty($_REQUEST['ml2'][$ml]) ? 0 : 2;
971 if ($ask == $state) {
972 if ($state && $email_changed) {
973 $mmlist->replace_email($ml, $from_email, $user['email2']);
974 $page->trig("L'abonnement de {$user['prenom']} {$user['nom']} à $ml@ a été mis à jour.");
975 }
976 continue;
977 }
978 if ($state == '1') {
979 $page->trig("{$user['prenom']} {$user['nom']} a "
980 ."actuellement une demande d'inscription en "
981 ."cours sur <strong>$ml@</strong> !!!");
982 } elseif ($ask) {
983 $mmlist->mass_subscribe($ml, Array($user['email2']));
984 $page->trig("{$user['prenom']} {$user['nom']} a été abonné à $ml@.");
985 } else {
986 if ($email_changed) {
987 $mmlist->mass_unsubscribe($ml, Array($from_email));
988 } else {
989 $mmlist->mass_unsubscribe($ml, Array($user['email2']));
990 }
991 $page->trig("{$user['prenom']} {$user['nom']} a été désabonné de $ml@.");
992 }
993 }
994
995 // Change subscriptioin to aliases
996 foreach (Env::v('ml3', array()) as $ml => $state) {
997 $ask = !empty($_REQUEST['ml4'][$ml]);
998 if($state == $ask) continue;
999 if($ask) {
1000 XDB::query("INSERT INTO virtual_redirect (vid,redirect)
1001 SELECT vid,{?} FROM virtual WHERE alias={?}",
1002 $user['email'], $ml);
1003 $page->trig("{$user['prenom']} {$user['nom']} a été abonné à $ml.");
1004 } else {
1005 XDB::query("DELETE FROM virtual_redirect
1006 USING virtual_redirect
1007 INNER JOIN virtual USING(vid)
1008 WHERE redirect={?} AND alias={?}",
1009 $user['email'], $ml);
1010 $page->trig("{$user['prenom']} {$user['nom']} a été désabonné de $ml.");
1011 }
1012 }
1013 }
1014
1015 $page->assign('user', $user);
1016 $listes = $mmlist->get_lists($user['email2']);
1017 $page->assign('listes', $listes);
1018
1019 $res = XDB::query(
1020 'SELECT alias, redirect IS NOT NULL as sub
1021 FROM virtual AS v
1022 LEFT JOIN virtual_redirect AS vr ON(v.vid=vr.vid AND redirect={?})
1023 WHERE alias LIKE {?} AND type="user"',
1024 $user['email'], '%@'.$globals->asso('mail_domain'));
1025 $page->assign('alias', $res->fetchAllAssoc());
1026 }
1027
1028 function handler_rss(&$page, $user = null, $hash = null)
1029 {
1030 global $globals;
1031 require_once('rss.inc.php');
1032 $uid = init_rss('xnetgrp/announce-rss.tpl', $user, $hash, false);
1033
1034 if ($uid) {
1035 $rss = XDB::iterator("SELECT a.id, a.titre, a.texte, a.contacts, a.create_date,
1036 IF(u2.nom_usage != '', u2.nom_usage, u2.nom) AS nom, u2.prenom, u2.promo,
1037 FIND_IN_SET('photo', a.flags) AS photo
1038 FROM auth_user_md5 AS u
1039 INNER JOIN groupex.announces AS a ON ( (a.promo_min = 0 OR a.promo_min <= u.promo)
1040 AND (a.promo_max = 0 OR a.promo_max <= u.promo))
1041 INNER JOIN auth_user_md5 AS u2 ON (u2.user_id = a.user_id)
1042 WHERE u.user_id = {?} AND peremption >= NOW() AND a.asso_id = {?}",
1043 $uid, $globals->asso('id'));
1044 } else {
1045 $rss = XDB::iterator("SELECT a.id, a.titre, a.texte, a.create_date,
1046 IF(u.nom_usage != '', u.nom_usage, u.nom) AS nom, u.prenom, u.promo
1047 FROM groupex.announces AS a
1048 INNER JOIN auth_user_md5 AS u USING(user_id)
1049 WHERE FIND_IN_SET('public', a.flags) AND peremption >= NOW() AND a.asso_id = {?}",
1050 $globals->asso('id'));
1051 }
1052 $page->assign('asso', $globals->asso());
1053 $page->assign('rss', $rss);
1054 }
1055
1056 private function upload_image(PlatalPage &$page, PlUpload &$upload)
1057 {
1058 if (@!$_FILES['image']['tmp_name'] && !Env::v('image_url')) {
1059 return true;
1060 }
1061 if (!$upload->upload($_FILES['image']) && !$upload->download(Env::v('image_url'))) {
1062 $page->trig('Impossible de télécharger l\'image');
1063 return false;
1064 } elseif (!$upload->isType('image')) {
1065 $page->trig('Le fichier n\'est pas une image valide au format JPEG, GIF ou PNG.');
1066 $upload->rm();
1067 return false;
1068 } elseif (!$upload->resizeImage(200, 300, 100, 100, 32284)) {
1069 $page->trig('Impossible de retraiter l\'image');
1070 return false;
1071 }
1072 return true;
1073 }
1074
1075 function handler_photo_announce(&$page, $eid = null) {
1076 if ($eid) {
1077 $res = XDB::query("SELECT * FROM groupex.announces_photo WHERE eid = {?}", $eid);
1078 if ($res->numRows()) {
1079 $photo = $res->fetchOneAssoc();
1080 header('Content-Type: image/' . $photo['attachmime']);
1081 echo $photo['attach'];
1082 exit;
1083 }
1084 } else {
1085 $upload = new PlUpload(S::v('forlife'), 'xnetannounce');
1086 if ($upload->exists() && $upload->isType('image')) {
1087 header('Content-Type: ' . $upload->contentType());
1088 echo $upload->getContents();
1089 exit;
1090 }
1091 }
1092 global $globals;
1093 header('Content-Type: image/png');
1094 echo file_get_contents($globals->spoolroot . '/htdocs/images/logo.png');
1095 exit;
1096 }
1097
1098 function handler_edit_announce(&$page, $aid = null)
1099 {
1100 global $globals, $platal;
1101 $page->changeTpl('xnetgrp/announce-edit.tpl');
1102 $page->assign('new', is_null($aid));
1103 $art = array();
1104
1105 if (Post::v('valid') == 'Visualiser' || Post::v('valid') == 'Enregistrer'
1106 || Post::v('valid') == 'Supprimer l\'image' || Post::v('valid') == 'Pas d\'image') {
1107 if (!is_null($aid)) {
1108 $art['id'] = $aid;
1109 }
1110 $art['titre'] = Post::v('titre');
1111 $art['texte'] = Post::v('texte');
1112 $art['contacts'] = Post::v('contacts');
1113 $art['promo_min'] = Post::i('promo_min');
1114 $art['promo_max'] = Post::i('promo_max');
1115 $art['nom'] = S::v('nom');
1116 $art['prenom'] = S::v('prenom');
1117 $art['promo'] = S::v('promo');
1118 $art['forlife'] = S::v('forlife');
1119 $art['peremption'] = Post::v('peremption');
1120 $art['public'] = Post::has('public');
1121 $art['xorg'] = Post::has('xorg');
1122 $art['nl'] = Post::has('nl');
1123 $art['event'] = Post::v('event');
1124 $upload = new PlUpload(S::v('forlife'), 'xnetannounce');
1125 $this->upload_image($page, $upload);
1126
1127 $art['contact_html'] = $art['contacts'];
1128 if ($art['event']) {
1129 $art['contact_html'] .= "\n{$globals->baseurl}/{$platal->ns}events/sub/{$art['event']}";
1130 }
1131
1132 if (!$art['public'] &&
1133 (($art['promo_min'] > $art['promo_max'] && $art['promo_max'] != 0) ||
1134 ($art['promo_min'] != 0 && ($art['promo_min'] <= 1900 || $art['promo_min'] >= 2020)) ||
1135 ($art['promo_max'] != 0 && ($art['promo_max'] <= 1900 || $art['promo_max'] >= 2020))))
1136 {
1137 $page->trig("L'intervalle de promotions est invalide.");
1138 Post::kill('valid');
1139 }
1140
1141 if (!trim($art['titre']) || !trim($art['texte'])) {
1142 $page->trig("L'article doit avoir un titre et un contenu.");
1143 Post::kill('valid');
1144 }
1145
1146 if (Post::v('valid') == 'Supprimer l\'image') {
1147 $upload->rm();
1148 Post::kill('valid');
1149 }
1150 $art['photo'] = $upload->exists() || Post::i('photo');
1151 if (Post::v('valid') == 'Pas d\'image' && !is_null($aid)) {
1152 XDB::query("DELETE FROM groupex.announces_photo WHERE eid = {?}", $aid);
1153 $upload->rm();
1154 Post::kill('valid');
1155 $art['photo'] = false;
1156 }
1157 }
1158
1159 if (Post::v('valid') == 'Enregistrer') {
1160 $promo_min = ($art['public'] ? 0 : $art['promo_min']);
1161 $promo_max = ($art['public'] ? 0 : $art['promo_max']);
1162 $flags = array();
1163 if ($art['public']) {
1164 $flags[] = 'public';
1165 }
1166 if ($art['photo']) {
1167 $flags[] = 'photo';
1168 }
1169 $flags = implode(',', $flags);
1170 if (is_null($aid)) {
1171 $fulltext = $art['texte'];
1172 if (!empty($art['contact_html'])) {
1173 $fulltext .= "\n\n'''Contacts :'''\\\\\n" . $art['contact_html'];
1174 }
1175 $post = null;/*
1176 if ($globals->asso('forum')) {
1177 require_once 'banana/forum.inc.php';
1178 $banana = new ForumsBanana(S::v('forlife'));
1179 $post = $banana->post($globals->asso('forum'), null,
1180 $art['titre'], MiniWiki::wikiToText($fulltext, false, 0, 80));
1181 }*/
1182 XDB::query("INSERT INTO groupex.announces
1183 (user_id, asso_id, create_date, titre, texte, contacts,
1184 peremption, promo_min, promo_max, flags, post_id)
1185 VALUES ({?}, {?}, NOW(), {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?})",
1186 S::i('uid'), $globals->asso('id'), $art['titre'], $art['texte'], $art['contact_html'],
1187 $art['peremption'], $promo_min, $promo_max, $flags, $post);
1188 $aid = XDB::insertId();
1189 if ($art['photo']) {
1190 list($imgx, $imgy, $imgtype) = $upload->imageInfo();
1191 XDB::execute("INSERT INTO groupex.announces_photo
1192 SET eid = {?}, attachmime = {?}, x = {?}, y = {?}, attach = {?}",
1193 $aid, $imgtype, $imgx, $imgy, $upload->getContents());
1194 }
1195 if ($art['xorg']) {
1196 require_once('validations.inc.php');
1197 $article = new EvtReq("[{$globals->asso('nom')}] " . $art['titre'], $fulltext,
1198 $art['promo_min'], $art['promo_max'], $art['peremption'], "", S::v('uid'),
1199 $upload);
1200 $article->submit();
1201 $page->trig("L'affichage sur la page d'accueil de Polytechnique.org est en attente de validation.");
1202 } else if ($upload && $upload->exists()) {
1203 $upload->rm();
1204 }
1205 if ($art['nl']) {
1206 require_once('validations.inc.php');
1207 $article = new NLReq(S::v('uid'), $globals->asso('nom') . " : " .$art['titre'],
1208 $art['texte'], $art['contact_html']);
1209 $article->submit();
1210 $page->trig("La parution dans la Lettre Mensuelle est en attente de validation.");
1211 }
1212 } else {
1213 XDB::query("UPDATE groupex.announces
1214 SET titre={?}, texte={?}, contacts={?}, peremption={?},
1215 promo_min={?}, promo_max={?}, flags={?}
1216 WHERE id={?} AND asso_id={?}",
1217 $art['titre'], $art['texte'], $art['contacts'], $art['peremption'],
1218 $promo_min, $promo_max, $flags,
1219 $art['id'], $globals->asso('id'));
1220 if ($art['photo'] && $upload->exists()) {
1221 list($imgx, $imgy, $imgtype) = $upload->imageInfo();
1222 XDB::execute("REPLACE INTO groupex.announces_photo
1223 SET eid = {?}, attachmime = {?}, x = {?}, y = {?}, attach = {?}",
1224 $aid, $imgtype, $imgx, $imgy, $upload->getContents());
1225 $upload->rm();
1226 }
1227 }
1228 }
1229 if (Post::v('valid') == 'Enregistrer' || Post::v('valid') == 'Annuler') {
1230 pl_redirect("");
1231 }
1232
1233 if (empty($art) && !is_null($aid)) {
1234 $res = XDB::query("SELECT a.*, u.nom, u.prenom, u.promo, l.alias AS forlife,
1235 FIND_IN_SET('public', a.flags) AS public,
1236 FIND_IN_SET('photo', a.flags) AS photo
1237 FROM groupex.announces AS a
1238 INNER JOIN auth_user_md5 AS u USING(user_id)
1239 INNER JOIN aliases AS l ON (l.id = u.user_id AND l.type = 'a_vie')
1240 WHERE asso_id = {?} AND a.id = {?}",
1241 $globals->asso('id'), $aid);
1242 if ($res->numRows()) {
1243 $art = $res->fetchOneAssoc();
1244 $art['contact_html'] = $art['contacts'];
1245 } else {
1246 $page->kill("Aucun article correspond à l'identifiant indiqué.");
1247 }
1248 }
1249
1250 if (is_null($aid)) {
1251 $events = XDB::iterator("SELECT *
1252 FROM groupex.evenements
1253 WHERE asso_id = {?} AND archive = 0",
1254 $globals->asso('id'));
1255 if ($events->total()) {
1256 $page->assign('events', $events);
1257 }
1258 }
1259
1260 $art['contact_html'] = @MiniWiki::WikiToHTML($art['contact_html']);
1261 $page->assign('art', $art);
1262 $page->assign_by_ref('upload', $upload);
1263 }
1264
1265 function handler_admin_announce(&$page)
1266 {
1267 global $globals;
1268 $page->changeTpl('xnetgrp/announce-admin.tpl');
1269
1270 if (Env::has('del')) {
1271 XDB::execute("DELETE FROM groupex.announces
1272 WHERE id = {?} AND asso_id = {?}",
1273 Env::i('del'), $globals->asso('id'));
1274 }
1275 $res = XDB::iterator("SELECT a.id, a.titre, a.peremption, a.peremption < CURRENT_DATE() AS perime
1276 FROM groupex.announces AS a
1277 WHERE a.asso_id = {?}
1278 ORDER BY a.peremption DESC",
1279 $globals->asso('id'));
1280 $page->assign('articles', $res);
1281 }
1282 }
1283
1284 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
1285 ?>