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