Fixes display of non-users in aliases.
[platal.git] / modules / xnetgrp.php
CommitLineData
ae7bb180 1<?php
2/***************************************************************************
5e1513f6 3 * Copyright (C) 2003-2011 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 22
ae7bb180 23class XnetGrpModule extends PLModule
24{
25 function handlers()
26 {
27 return array(
020c8ed0
SJ
28 '%grp' => $this->make_hook('index', AUTH_PUBLIC),
29 '%grp/asso.php' => $this->make_hook('index', AUTH_PUBLIC),
30 '%grp/logo' => $this->make_hook('logo', AUTH_PUBLIC),
31 '%grp/site' => $this->make_hook('site', AUTH_PUBLIC),
5ae3e9a9
SJ
32 '%grp/edit' => $this->make_hook('edit', AUTH_MDP, 'groupadmin'),
33 '%grp/mail' => $this->make_hook('mail', AUTH_MDP, 'groupadmin'),
34 '%grp/forum' => $this->make_hook('forum', AUTH_MDP, 'groupmember'),
35 '%grp/annuaire' => $this->make_hook('annuaire', AUTH_MDP, 'groupannu'),
36 '%grp/annuaire/vcard' => $this->make_hook('vcard', AUTH_MDP, 'groupmember:groupannu'),
37 '%grp/annuaire/csv' => $this->make_hook('csv', AUTH_MDP, 'groupmember:groupannu'),
38 '%grp/trombi' => $this->make_hook('trombi', AUTH_MDP, 'groupannu'),
39 '%grp/geoloc' => $this->make_hook('geoloc', AUTH_MDP, 'groupannu'),
020c8ed0 40 '%grp/subscribe' => $this->make_hook('subscribe', AUTH_MDP),
5ae3e9a9
SJ
41 '%grp/subscribe/valid' => $this->make_hook('subscribe_valid', AUTH_MDP, 'groupadmin'),
42 '%grp/unsubscribe' => $this->make_hook('unsubscribe', AUTH_MDP, 'groupmember'),
4e310c61 43
020c8ed0 44 '%grp/change_rights' => $this->make_hook('change_rights', AUTH_MDP),
5ae3e9a9
SJ
45 '%grp/admin/annuaire' => $this->make_hook('admin_annuaire', AUTH_MDP, 'groupadmin'),
46 '%grp/member' => $this->make_hook('admin_member', AUTH_MDP, 'groupadmin'),
47 '%grp/member/new' => $this->make_hook('admin_member_new', AUTH_MDP, 'groupadmin'),
48 '%grp/member/new/ajax' => $this->make_hook('admin_member_new_ajax', AUTH_MDP, 'user', NO_AUTH),
49 '%grp/member/del' => $this->make_hook('admin_member_del', AUTH_MDP, 'groupadmin'),
50 '%grp/member/suggest' => $this->make_hook('admin_member_suggest', AUTH_MDP, 'groupadmin'),
b8e265bf 51
190efb3a 52 '%grp/rss' => $this->make_token_hook('rss', AUTH_PUBLIC),
5ae3e9a9
SJ
53 '%grp/announce/new' => $this->make_hook('edit_announce', AUTH_MDP, 'groupadmin'),
54 '%grp/announce/edit' => $this->make_hook('edit_announce', AUTH_MDP, 'groupadmin'),
020c8ed0 55 '%grp/announce/photo' => $this->make_hook('photo_announce', AUTH_PUBLIC),
5ae3e9a9 56 '%grp/admin/announces' => $this->make_hook('admin_announce', AUTH_MDP, 'groupadmin'),
ae7bb180 57 );
58 }
59
26ba053e 60 function handler_index($page, $arg = null)
ae7bb180 61 {
24bcf50c 62 global $globals, $platal;
2e94d2b8 63 if (!is_null($arg)) {
ae7bb180 64 return PL_NOT_FOUND;
65 }
1490093c 66 $page->changeTpl('xnetgrp/asso.tpl');
ae7bb180 67
24bcf50c 68 if (S::logged()) {
69 if (Env::has('read')) {
14bc135e
SJ
70 XDB::query('DELETE r.*
71 FROM group_announces_read AS r
72 INNER JOIN group_announces AS a ON (a.id = r.announce_id)
b58e5ac2 73 WHERE expiration < CURRENT_DATE()');
14bc135e
SJ
74 XDB::query('INSERT INTO group_announces_read
75 VALUES ({?}, {?})',
24bcf50c 76 Env::i('read'), S::i('uid'));
77 pl_redirect("");
78 }
79 if (Env::has('unread')) {
14bc135e
SJ
80 XDB::query('DELETE FROM group_announces_read
81 WHERE announce_id = {?} AND uid = {?}',
24bcf50c 82 Env::i('unread'), S::i('uid'));
83 pl_redirect("#art" . Env::i('unread'));
84 }
5a9ad4b0
SJ
85
86 /* TODO: refines this filter on promotions by using userfilter. */
87 $user = S::user();
88 if ($user->hasProfile()) {
89 $promo = XDB::format('{?}', $user->profile()->entry_year);
90 $minCondition = ' OR promo_min <= ' . $promo;
91 $maxCondition = ' OR promo_max >= ' . $promo;
92 } else {
93 $minCondition = '';
94 $maxCondition = '';
95 }
96 $arts = XDB::iterator('SELECT a.*, FIND_IN_SET(\'photo\', a.flags) AS photo
14bc135e
SJ
97 FROM group_announces AS a
98 LEFT JOIN group_announces_read AS r ON (r.uid = {?} AND r.announce_id = a.id)
b58e5ac2 99 WHERE asso_id = {?} AND expiration >= CURRENT_DATE()
5a9ad4b0
SJ
100 AND (promo_min = 0' . $minCondition . ')
101 AND (promo_max = 0' . $maxCondition . ')
5660032a 102 AND r.announce_id IS NULL
5a9ad4b0
SJ
103 ORDER BY a.expiration',
104 S::i('uid'), $globals->asso('id'));
105 $index = XDB::iterator('SELECT a.id, a.titre, r.uid IS NULL AS nonlu
14bc135e
SJ
106 FROM group_announces AS a
107 LEFT JOIN group_announces_read AS r ON (a.id = r.announce_id AND r.uid = {?})
b58e5ac2 108 WHERE asso_id = {?} AND expiration >= CURRENT_DATE()
5a9ad4b0
SJ
109 AND (promo_min = 0' . $minCondition . ')
110 AND (promo_max = 0' . $maxCondition . ')
111 ORDER BY a.expiration',
112 S::i('uid'), $globals->asso('id'));
24bcf50c 113 $page->assign('article_index', $index);
114 } else {
14bc135e
SJ
115 $arts = XDB::iterator("SELECT *, FIND_IN_SET('photo', flags) AS photo
116 FROM group_announces
b58e5ac2 117 WHERE asso_id = {?} AND expiration >= CURRENT_DATE()
14bc135e 118 AND FIND_IN_SET('public', flags)",
24bcf50c 119 $globals->asso('id'));
120 }
4af7fd22
FB
121 if (may_update()) {
122 $subs_valid = XDB::query("SELECT uid
eb41eda9 123 FROM group_member_sub_requests
4af7fd22
FB
124 WHERE asso_id = {?}",
125 $globals->asso('id'));
126 $page->assign('requests', $subs_valid->numRows());
127 }
24bcf50c 128
84fc72ff 129 if (!S::hasAuthToken()) {
24bcf50c 130 $page->setRssLink("Polytechnique.net :: {$globals->asso("nom")} :: News publiques",
5700a250 131 $platal->ns . "rss/rss.xml");
24bcf50c 132 } else {
133 $page->setRssLink("Polytechnique.net :: {$globals->asso("nom")} :: News",
2ab3486b 134 $platal->ns . 'rss/' . S::v('hruid') . '/' . S::user()->token . '/rss.xml');
24bcf50c 135 }
eaf30d86 136
24bcf50c 137 $page->assign('articles', $arts);
ae7bb180 138 }
dd798f38 139
26ba053e 140 function handler_logo($page)
dd798f38 141 {
142 global $globals;
833a6e86 143 $globals->asso()->getLogo()->send();
dd798f38 144 }
2e94d2b8 145
26ba053e 146 function handler_site($page)
d430f4d8 147 {
148 global $globals;
149 $site = $globals->asso('site');
150 if (!$site) {
a7d35093 151 $page->trigError('Le groupe n\'a pas de site web.');
d430f4d8 152 return $this->handler_index($page);
153 }
02838718 154 http_redirect($site);
d430f4d8 155 exit;
156 }
157
26ba053e 158 function handler_edit($page)
2e94d2b8 159 {
160 global $globals;
1490093c 161 $page->changeTpl('xnetgrp/edit.tpl');
2e94d2b8 162
40d428d8
VZ
163 if (Post::has('submit')) {
164 S::assert_xsrf_token();
165
113f6de8 166 $flags = new PlFlagSet('wiki_desc');
a6761ca9
FB
167 $flags->addFlag('notif_unsub', Post::i('notif_unsub') == 1);
168 $site = Post::t('site');
ffe73cf7
SJ
169 if ($site && ($site != "http://")) {
170 $scheme = parse_url($site, PHP_URL_SCHEME);
171 if (!$scheme) {
172 $site = "http://" . $site;
173 }
174 } else {
175 $site = "";
176 }
23ff6dd6 177 if (S::admin()) {
c3d959ce
SJ
178 $page->assign('super', true);
179
5e2307dc 180 if (Post::v('mail_domain') && (strstr(Post::v('mail_domain'), '.') === false)) {
6bb2f79a 181 $page->trigError('Le domaine doit être un FQDN (aucune modification effectuée)&nbsp;!!!');
c9110c6c 182 return;
2e94d2b8 183 }
c3d959ce
SJ
184 if (Post::t('nom') == '' || Post::t('diminutif') == '') {
185 $page->trigError('Ni le nom ni le diminutif du groupe ne peuvent être vide.');
186 return;
187 }
004ff79e
SJ
188 $axDate = make_datetime(Post::v('axDate'));
189 if (Post::t('axDate') != '') {
190 $axDate = make_datetime(Post::v('axDate'))->format('Y-m-d');
191 } else {
192 $axDate = null;
193 }
08cce2ff 194 XDB::execute(
eb41eda9 195 "UPDATE groups
2e94d2b8 196 SET nom={?}, diminutif={?}, cat={?}, dom={?},
197 descr={?}, site={?}, mail={?}, resp={?},
004ff79e 198 forum={?}, mail_domain={?}, ax={?}, axDate = {?}, pub={?},
794feea7 199 sub_url={?}, inscriptible={?}, unsub_url={?},
73ac0916 200 flags = {?}, welcome_msg = {?}
2e94d2b8 201 WHERE id={?}",
5e2307dc 202 Post::v('nom'), Post::v('diminutif'),
ca46cc12 203 Post::v('cat'), (Post::i('dom') == 0) ? null : Post::i('dom'),
ffe73cf7 204 Post::v('descr'), $site,
5e2307dc 205 Post::v('mail'), Post::v('resp'),
206 Post::v('forum'), Post::v('mail_domain'),
004ff79e 207 Post::has('ax'), $axDate, Post::v('pub'),
5e2307dc 208 Post::v('sub_url'), Post::v('inscriptible'),
73ac0916
SJ
209 Post::v('unsub_url'), $flags, Post::t('welcome_msg'),
210 $globals->asso('id'));
5e2307dc 211 if (Post::v('mail_domain')) {
831aa467
SJ
212 XDB::execute('INSERT IGNORE INTO email_virtual_domains (name)
213 VALUES ({?})',
214 Post::t('mail_domain'));
215 XDB::execute('UPDATE email_virtual_domains
216 SET aliasing = id
217 WHERE name = {?}',
218 Post::t('mail_domain'));
2e94d2b8 219 }
220 } else {
08cce2ff 221 XDB::execute(
eb41eda9 222 "UPDATE groups
2e94d2b8 223 SET descr={?}, site={?}, mail={?}, resp={?},
c2f19630 224 forum={?}, pub= {?}, sub_url={?},
73ac0916 225 unsub_url = {?}, flags = {?}, welcome_msg = {?}
2e94d2b8 226 WHERE id={?}",
ffe73cf7 227 Post::v('descr'), $site,
5e2307dc 228 Post::v('mail'), Post::v('resp'),
c2f19630 229 Post::v('forum'), Post::v('pub'),
30032578 230 Post::v('sub_url'), Post::v('unsub_url'),
73ac0916
SJ
231 $flags, Post::t('welcome_msg'),
232 $globals->asso('id'));
2e94d2b8 233 }
234
a174a707
SJ
235 Phone::deletePhones(0, Phone::LINK_GROUP, $globals->asso('id'));
236 $phone = new Phone(array('link_type' => 'group', 'link_id' => $globals->asso('id'), 'id' => 0,
237 'type' => 'fixed', 'display' => Post::v('phone'), 'pub' => 'public'));
238 $fax = new Phone(array('link_type' => 'group', 'link_id' => $globals->asso('id'), 'id' => 1,
239 'type' => 'fax', 'display' => Post::v('fax'), 'pub' => 'public'));
240 $phone->save();
241 $fax->save();
baab50c7
SJ
242 Address::deleteAddresses(null, Address::LINK_GROUP, null, $globals->asso('id'));
243 $address = new Address(array('groupid' => $globals->asso('id'), 'type' => Address::LINK_GROUP, 'text' => Post::v('address')));
244 $address->save();
a6761ca9 245
2e94d2b8 246 if ($_FILES['logo']['name']) {
a6761ca9
FB
247 $upload = PlUpload::get($_FILES['logo'], $globals->asso('id'), 'asso.logo', true);
248 if (!$upload) {
5660032a 249 $page->trigError("Impossible de télécharger le logo.");
a6761ca9 250 } else {
eb41eda9 251 XDB::execute('UPDATE groups
5660032a
SJ
252 SET logo = {?}, logo_mime = {?}
253 WHERE id = {?}', $upload->getContents(), $upload->contentType(),
a6761ca9
FB
254 $globals->asso('id'));
255 $upload->rm();
256 }
2e94d2b8 257 }
258
fc4714d6
SJ
259 pl_redirect('../' . Post::v('diminutif', $globals->asso('diminutif')) . '/edit');
260 }
261
262 if (S::admin()) {
263 $dom = XDB::iterator('SELECT *
264 FROM group_dom
265 ORDER BY nom');
266 $page->assign('dom', $dom);
267 $page->assign('super', true);
2e94d2b8 268 }
2e94d2b8 269 }
270
26ba053e 271 function handler_mail($page)
479d2787 272 {
273 global $globals;
274
1490093c 275 $page->changeTpl('xnetgrp/mail.tpl');
5c36f345 276 $mmlist = new MMList(S::user(), $globals->asso('mail_domain'));
9bb8bf21 277 $page->assign('listes', $mmlist->get_lists());
7912097e 278 $page->assign('user', S::user());
479d2787 279
280 if (Post::has('send')) {
e7fdf9dd 281 S::assert_xsrf_token();
5e2307dc 282 $from = Post::v('from');
283 $sujet = Post::v('sujet');
284 $body = Post::v('body');
479d2787 285
5e2307dc 286 $mls = array_keys(Env::v('ml', array()));
06db561e 287 $mbr = array_keys(Env::v('membres', array()));
479d2787 288
460d8f55 289 $this->load('mail.inc.php');
c974e61c 290 set_time_limit(120);
06db561e 291 $tos = get_all_redirects($mbr, $mls, $mmlist);
eab2f6f5 292
f3df6d38 293 $upload = PlUpload::get($_FILES['uploaded'], S::user()->login(), 'xnet.emails', true);
eab2f6f5
VZ
294 if (!$upload && @$_FILES['uploaded']['name'] && PlUpload::$lastError != null) {
295 $page->trigError(PlUpload::$lastError);
296 return;
297 }
298
5b21237d 299 send_xnet_mails($from, $sujet, $body, Env::v('wiki'), $tos, Post::v('replyto'), $upload, @$_FILES['uploaded']['name']);
13627bef 300 if ($upload) {
301 $upload->rm();
302 }
6bb2f79a 303 $page->killSuccess("Email envoyé&nbsp;!");
479d2787 304 $page->assign('sent', true);
305 }
306 }
307
26ba053e 308 function handler_forum($page, $group = null, $artid = null)
4f355064 309 {
310 global $globals;
1490093c 311 $page->changeTpl('xnetgrp/forum.tpl');
4f355064 312 if (!$globals->asso('forum')) {
313 return PL_NOT_FOUND;
314 }
315 require_once 'banana/forum.inc.php';
316 $get = array();
317 get_banana_params($get, $globals->asso('forum'), $group, $artid);
318 run_banana($page, 'ForumsBanana', $get);
319 }
320
26ba053e 321 function handler_annuaire($page, $action = null, $subaction = null)
2e94d2b8 322 {
323 global $globals;
a91fb584 324
316120eb 325 if ($action == 'trombi') {
8ae9dd2f
FB
326 __autoload('userset');
327 if ($action == 'trombi') {
328 $view = new ProfileSet(new UFC_Group($globals->asso('id')));
329 } else {
330 $view = new UserSet(new UFC_Group($globals->asso('id')));
331 }
a91fb584 332 $view->addMod('trombi', 'Trombinoscope');
a91fb584 333 $view->apply('annuaire', $page, $action, $subaction);
316120eb 334 $page->changeTpl('xnetgrp/annuaire.tpl');
71db1043
SJ
335 $count = XDB::fetchOneCell('SELECT COUNT(*)
336 FROM group_members
337 WHERE asso_id = {?}',
338 $globals->asso('id'));
339 $page->assign('nb_tot', $count);
316120eb 340 return;
a91fb584 341 }
2e94d2b8 342
316120eb 343 $page->changeTpl('xnetgrp/annuaire.tpl');
f6d7a2df 344 $sort = Env::s('order', 'directory_name');
ce814052 345 $ofs = Env::i('offset');
34ade5a6
FB
346 if ($ofs < 0) {
347 $ofs = 0;
348 }
2e94d2b8 349
d865c296
FB
350 $sdesc = $sort{0} == '-';
351 $sf = $sdesc ? substr($sort, 1) : $sort;
352 if ($sf == 'promo') {
353 $se = new UFO_Promo(null, $sdesc);
354 } else {
355 $se = new UFO_Name($sf, null, null, $sdesc);
356 }
357
f6d7a2df 358 if (Env::b('admin')) {
184b012d 359 $uf = $globals->asso()->getAdminsFilter(null, $se);
f6d7a2df 360 } else {
184b012d 361 $uf = $globals->asso()->getMembersFilter(null, $se);
2e94d2b8 362 }
e1406965 363 $users = $uf->getUsers(new PlLimit(NB_PER_PAGE, $ofs * NB_PER_PAGE));
d865c296
FB
364 $count = $uf->getTotalCount();
365
71db1043 366 $page->assign('nb_tot', $count);
a087cc8d 367 $page->assign('pages', floor(($count + NB_PER_PAGE - 1) / NB_PER_PAGE));
ce814052 368 $page->assign('current', $ofs);
f6d7a2df
FB
369 $page->assign('order', $sort);
370 $page->assign('users', $users);
371 $page->assign('only_admin', Env::b('admin'));
2e94d2b8 372 }
4e310c61 373
26ba053e 374 function handler_trombi($page)
a91fb584 375 {
376 pl_redirect('annuaire/trombi');
377 }
378
26ba053e 379 function handler_geoloc($page)
28929cf0 380 {
a91fb584 381 pl_redirect('annuaire/geoloc');
28929cf0 382 }
eaf30d86 383
26ba053e 384 function handler_vcard($page, $photos = null)
5e193297 385 {
386 global $globals;
5d42c993 387 $vcard = new VCard($photos == 'photos', 'Membre du groupe ' . $globals->asso('nom'));
801adeec 388 $vcard->addProfiles($globals->asso()->getMembersFilter()->getProfiles(null, Profile::FETCH_ALL));
5d42c993 389 $vcard->show();
5e193297 390 }
391
26ba053e 392 function handler_csv($page, $filename = null)
102f4e64
FB
393 {
394 global $globals;
395 if (is_null($filename)) {
396 $filename = $globals->asso('diminutif') . '.csv';
397 }
184b012d 398 $users = $globals->asso()->getMembersFilter(null, new UFO_Name('directory_name'))->getUsers();
023c46fb 399 pl_cached_content_headers('text/x-csv', 1);
102f4e64 400 $page->changeTpl('xnetgrp/annuaire-csv.tpl', NO_SKIN);
a6761ca9 401 $page->assign('users', $users);
102f4e64
FB
402 }
403
4af7fd22
FB
404 private function removeSubscriptionRequest($uid)
405 {
406 global $globals;
eb41eda9 407 XDB::execute("DELETE FROM group_member_sub_requests
4af7fd22
FB
408 WHERE asso_id = {?} AND uid = {?}",
409 $globals->asso('id'), $uid);
410 }
411
26ba053e 412 private function validSubscription(User $user)
4af7fd22
FB
413 {
414 global $globals;
0fab5209 415 $this->removeSubscriptionRequest($user->id());
eb41eda9 416 XDB::execute("INSERT IGNORE INTO group_members (asso_id, uid)
cac7dc37 417 VALUES ({?}, {?})",
0fab5209 418 $globals->asso('id'), $user->id());
cac7dc37
FB
419 if (XDB::affectedRows() == 1) {
420 $mailer = new PlMailer();
421 $mailer->addTo($user->forlifeEmail());
422 $mailer->setFrom('"' . S::user()->fullName() . '" <' . S::user()->forlifeEmail() . '>');
423 $mailer->setSubject('[' . $globals->asso('nom') . '] Demande d\'inscription');
424 $message = ($user->isFemale() ? 'Chère' : 'Cher') . " Camarade,\n"
425 . "\n"
f41e70e9
SJ
426 . " Suite à ta demande d'adhésion à " . $globals->asso('nom')
427 . ", j'ai le plaisir de t'annoncer que ton inscription a été validée !\n"
73ac0916 428 . (is_null($globals->asso('welcome_msg')) ? '' : "\n" . $globals->asso('welcome_msg') . "\n")
cac7dc37
FB
429 . "\n"
430 . "Bien cordialement,\n"
431 . "-- \n"
432 . S::user()->fullName() . '.';
f41e70e9 433 $mailer->setTxtBody(wordwrap($message, 72));
cac7dc37
FB
434 $mailer->send();
435 }
4af7fd22
FB
436 }
437
26ba053e 438 function handler_subscribe($page, $u = null)
fdf0200a 439 {
c8faf82a 440 global $globals;
1490093c 441 $page->changeTpl('xnetgrp/inscrire.tpl');
fdf0200a 442
443 if (!$globals->asso('inscriptible'))
a7de4ef7 444 $page->kill("Il n'est pas possible de s'inscire en ligne à ce "
445 ."groupe. Essaie de joindre le contact indiqué "
446 ."sur la page de présentation.");
fdf0200a 447
448 if (!is_null($u) && may_update()) {
0fab5209
VZ
449 $user = User::get($u);
450 if (!$user) {
451 return PL_NOT_FOUND;
452 } else {
453 $page->assign('user', $user);
fdf0200a 454 }
0fab5209
VZ
455
456 // Retrieves the subscription status, and the reason.
457 $res = XDB::query("SELECT reason
eb41eda9 458 FROM group_member_sub_requests
0fab5209
VZ
459 WHERE asso_id = {?} AND uid = {?}",
460 $globals->asso('id'), $user->id());
461 $reason = ($res->numRows() ? $res->fetchOneCell() : null);
462
463 $res = XDB::query("SELECT COUNT(*)
eb41eda9 464 FROM group_members
0fab5209
VZ
465 WHERE asso_id = {?} AND uid = {?}",
466 $globals->asso('id'), $user->id());
467 $already_member = ($res->fetchOneCell() > 0);
468
469 // Handles the membership request.
470 if ($already_member) {
471 $this->removeSubscriptionRequest($user->id());
6bb2f79a 472 $page->kill($user->fullName() . ' est déjà membre du groupe&nbsp;!');
0fab5209
VZ
473 } elseif (Env::has('accept')) {
474 S::assert_xsrf_token();
475
476 $this->validSubscription($user);
477 pl_redirect("member/" . $user->login());
478 } elseif (Env::has('refuse')) {
479 S::assert_xsrf_token();
480
481 $this->removeSubscriptionRequest($user->id());
482 $mailer = new PlMailer();
483 $mailer->addTo($user->forlifeEmail());
484 $mailer->setFrom('"' . S::user()->fullName() . '" <' . S::user()->forlifeEmail() . '>');
485 $mailer->setSubject('['.$globals->asso('nom').'] Demande d\'inscription annulée');
486 $mailer->setTxtBody(Env::v('motif'));
487 $mailer->send();
354adb18 488 $page->killSuccess("La demande de {$user->fullName()} a bien été refusée.");
0fab5209
VZ
489 } else {
490 $page->assign('show_form', true);
491 $page->assign('reason', $reason);
492 }
493 return;
fdf0200a 494 }
495
496 if (is_member()) {
6bb2f79a 497 $page->kill("Tu es déjà membre&nbsp;!");
fdf0200a 498 return;
499 }
500
4af7fd22 501 $res = XDB::query("SELECT uid
eb41eda9 502 FROM group_member_sub_requests
4af7fd22
FB
503 WHERE uid = {?} AND asso_id = {?}",
504 S::i('uid'), $globals->asso('id'));
505 if ($res->numRows() != 0) {
506 $page->kill("Tu as déjà demandé ton inscription à ce groupe. Cette demande est actuellement en attente de validation.");
507 return;
508 }
509
fdf0200a 510 if (Post::has('inscrire')) {
e7fdf9dd
VZ
511 S::assert_xsrf_token();
512
eb41eda9 513 XDB::execute("INSERT INTO group_member_sub_requests (asso_id, uid, ts, reason)
4af7fd22
FB
514 VALUES ({?}, {?}, NOW(), {?})",
515 $globals->asso('id'), S::i('uid'), Post::v('message'));
ae775de9
SJ
516 $uf = New UserFilter(New UFC_Group($globals->asso('id'), true));
517 $admins = $uf->iterUsers();
518 $admin = $admins->next();
11d8c704 519 $to = $admin->bestEmail();
ae775de9 520 while ($admin = $admins->next()) {
11d8c704 521 $to .= ', ' . $admin->bestEmail();
ae775de9 522 }
fdf0200a 523
524 $append = "\n"
525 . "-- \n"
a7de4ef7 526 . "Ce message a été envoyé suite à la demande d'inscription de\n"
0fab5209 527 . S::user()->fullName() . ' (X' . S::v('promo') . ")\n"
fdf0200a 528 . "Via le site www.polytechnique.net. Tu peux choisir de valider ou\n"
529 . "de refuser sa demande d'inscription depuis la page :\n"
0fab5209 530 . "http://www.polytechnique.net/" . $globals->asso("diminutif") . "/subscribe/" . S::user()->login() . "\n"
fdf0200a 531 . "\n"
a7de4ef7 532 . "En cas de problème, contacter l'équipe de Polytechnique.org\n"
533 . "à l'adresse : support@polytechnique.org\n";
fdf0200a 534
535 if (!$to) {
6f2b23a4
SJ
536 $to = ($globals->asso('mail') != '') ? $globals->asso('mail') . ', ' : '';
537 $to .= 'support@polytechnique.org';
fdf0200a 538 $append = "\n-- \nLe groupe ".$globals->asso("nom")
a7de4ef7 539 ." n'a pas d'administrateur, l'équipe de"
540 ." Polytechnique.org a été prévenue et va rapidement"
541 ." résoudre ce problème.\n";
fdf0200a 542 }
543
1e33266a 544 $mailer = new PlMailer();
fdf0200a 545 $mailer->addTo($to);
0fab5209 546 $mailer->setFrom('"' . S::user()->fullName() . '" <' . S::user()->forlifeEmail() . '>');
fdf0200a 547 $mailer->setSubject('['.$globals->asso('nom').'] Demande d\'inscription');
5e2307dc 548 $mailer->setTxtBody(Post::v('message').$append);
fdf0200a 549 $mailer->send();
550 }
551 }
552
26ba053e 553 function handler_subscribe_valid($page)
4af7fd22
FB
554 {
555 global $globals;
556
557 if (Post::has('valid')) {
558 S::assert_xsrf_token();
559 $subs = Post::v('subs');
560 if (is_array($subs)) {
561 $users = array();
0fab5209 562 foreach ($subs as $hruid => $val) {
4af7fd22 563 if ($val == '1') {
0fab5209
VZ
564 $user = User::get($hruid);
565 if ($user) {
566 $this->validSubscription($user);
4af7fd22
FB
567 }
568 }
569 }
570 }
571 }
572
aa21c568 573 $it = XDB::iterator('SELECT s.uid, a.hruid, s.ts AS date
eb41eda9 574 FROM group_member_sub_requests AS s
c8d75c58 575 INNER JOIN accounts AS a ON(s.uid = a.uid)
aa21c568
FB
576 WHERE s.asso_id = {?}
577 ORDER BY s.ts', $globals->asso('id'));
4af7fd22
FB
578 $page->changeTpl('xnetgrp/subscribe-valid.tpl');
579 $page->assign('valid', $it);
580 }
581
26ba053e 582 function handler_change_rights($page)
b8e265bf 583 {
0c02607e 584 if (Env::has('right') && (may_update() || S::suid())) {
b8e265bf 585 switch (Env::v('right')) {
eaf30d86 586 case 'admin':
ab694eb5 587 Platal::session()->stopSUID();
b8e265bf 588 break;
589 case 'anim':
ab694eb5 590 Platal::session()->doSelfSuid();
b8e265bf 591 may_update(true);
592 is_member(true);
593 break;
594 case 'member':
ab694eb5 595 Platal::session()->doSelfSuid();
b8e265bf 596 may_update(false, true);
597 is_member(true);
598 break;
599 case 'logged':
ab694eb5 600 Platal::session()->doSelfSuid();
b8e265bf 601 may_update(false, true);
602 is_member(false, true);
603 break;
604 }
605 }
5603d2ce 606 http_redirect($_SERVER['HTTP_REFERER']);
b8e265bf 607 }
608
26ba053e 609 function handler_admin_annuaire($page)
4e310c61 610 {
611 global $globals;
612
460d8f55 613 $this->load('mail.inc.php');
1490093c 614 $page->changeTpl('xnetgrp/annuaire-admin.tpl');
aa21c568
FB
615 $user = S::user();
616 $mmlist = new MMList($user, $globals->asso('mail_domain'));
9bb8bf21 617 $lists = $mmlist->get_lists();
4e310c61 618 if (!$lists) $lists = array();
619 $listes = array_map(create_function('$arr', 'return $arr["list"];'), $lists);
620
621 $subscribers = array();
622
623 foreach ($listes as $list) {
9bb8bf21 624 list(,$members) = $mmlist->get_members($list);
4e310c61 625 $mails = array_map(create_function('$arr', 'return $arr[1];'), $members);
626 $subscribers = array_unique(array_merge($subscribers, $mails));
627 }
628
629 $not_in_group_x = array();
630 $not_in_group_ext = array();
631
632 foreach ($subscribers as $mail) {
c8763ca3 633 $uf = new UserFilter(new PFC_And(new UFC_Group($globals->asso('id')),
aa21c568
FB
634 new UFC_Email($mail)));
635 if ($uf->getTotalCount() == 0) {
636 if (User::isForeignEmailAddress($mail)) {
4e310c61 637 $not_in_group_ext[] = $mail;
638 } else {
639 $not_in_group_x[] = $mail;
640 }
641 }
642 }
643
644 $page->assign('not_in_group_ext', $not_in_group_ext);
645 $page->assign('not_in_group_x', $not_in_group_x);
646 $page->assign('lists', $lists);
647 }
ef7c8560 648
26ba053e 649 function handler_admin_member_new($page, $email = null)
ef7c8560 650 {
651 global $globals;
652
1490093c 653 $page->changeTpl('xnetgrp/membres-add.tpl');
ef7c8560 654
655 if (is_null($email)) {
656 return;
657 }
658
bb88d138 659 S::assert_xsrf_token();
bc6e8005 660 $suggest_account_activation = false;
bb88d138 661
41905adf
RB
662 // FS#703 : $_GET is urldecoded twice, hence
663 // + (the data) => %2B (in the url) => + (first decoding) => ' ' (second decoding)
664 // Since there can be no spaces in emails, we can fix this with :
665 $email = str_replace(' ', '+', $email);
666
33a4f3f9 667 // Finds or creates account: first cases are for users with an account.
a1642859 668 if (!User::isForeignEmailAddress($email)) {
33a4f3f9 669 // Standard account
a1642859 670 $user = User::get($email);
bb88d138
PC
671 } else if (!isvalid_email($email)) {
672 // email might not be a regular email but an alias or a hruid
673 $user = User::get($email);
674 if (!$user) {
675 // need a valid email address
33a4f3f9 676 $page->trigError('«&nbsp;<strong>' . $email . '</strong>&nbsp;» n\'est pas une adresse email valide.');
bb88d138 677 return;
ef7c8560 678 }
bb88d138 679 } else if (Env::v('x') && Env::i('userid')) {
bb88d138
PC
680 $user = User::getWithUID(Env::i('userid'));
681 if (!$user) {
33a4f3f9 682 $page->trigError('Utilisateur invalide.');
bb88d138
PC
683 return;
684 }
33a4f3f9
SJ
685
686 // User has an account but is not yet registered.
687 if ($user->state == 'pending') {
688 // Add email in account table.
689 XDB::query('UPDATE accounts
690 SET email = {?}
691 WHERE uid = {?} AND email IS NULL',
692 Post::t('email'), $user->id());
693 // Add email for marketing if required.
694 if (Env::v('market')) {
695 $market = Marketing::get($user->uid, $email);
696 if (!$market) {
697 $market = new Marketing($user->uid, $email, 'group', $globals->asso('nom'),
698 Env::v('market_from'), S::v('uid'));
699 $market->add();
700 }
dc2073c3 701 }
bb88d138
PC
702 }
703 } else {
bc6e8005
SJ
704 // User is of type xnet. There are 3 possible cases:
705 // * the email is not known yet: we create a new account and
706 // propose to send an email to the user so he can activate
707 // his account,
708 // * the email is known but the user was not contacted in order to
709 // activate yet: we propose to send an email to the user so he
710 // can activate his account,
711 // * the email is known and the user was already contacted or has
712 // an active account: nothing to be done.
445d244c
RB
713 list($mbox, $domain) = explode('@', strtolower($email));
714 $hruid = User::makeHrid($mbox, $domain, 'ext');
33a4f3f9 715 // User might already have an account (in another group for example).
bb88d138 716 $user = User::get($hruid);
33a4f3f9
SJ
717
718 // If the user has no account yet, creates new account: build names from email address.
bb88d138 719 if (empty($user)) {
445d244c
RB
720 $parts = explode('.', $mbox);
721 if (count($parts) == 1) {
722 $display_name = $full_name = $directory_name = ucfirst($mbox);
723 } else {
724 $firstname = ucfirst($parts[0]);
725 $lastname = ucwords(implode(' ', array_slice($parts, 1)));
726 $display_name = $firstname;
727 $full_name = "$firstname $lastname";
728 $directory_name = strtoupper($lastname) . " " . $firstname;
729 }
730 XDB::execute('INSERT INTO accounts (hruid, display_name, full_name, directory_name,
731 email, type)
732 VALUES ({?}, {?}, {?}, {?}, {?}, \'xnet\')',
733 $hruid, $display_name, $full_name, $directory_name, $email);
bb88d138 734 $user = User::get($hruid);
ef7c8560 735 }
bc6e8005 736
fb422cab 737 $suggest_account_activation = $this->suggest($user);
ef7c8560 738 }
33a4f3f9 739
bb88d138 740 if ($user) {
00ba8a74
SJ
741 XDB::execute('INSERT IGNORE INTO group_members (uid, asso_id)
742 VALUES ({?}, {?})',
bb88d138
PC
743 $user->id(), $globals->asso('id'));
744 $this->removeSubscriptionRequest($user->id());
bc6e8005
SJ
745 if ($suggest_account_activation) {
746 pl_redirect('member/suggest/' . $user->login() . '/' . $email . '/' . $globals->asso('nom'));
747 } else {
748 pl_redirect('member/' . $user->login());
749 }
750 }
751 }
752
fb422cab
SJ
753 // Check if the user has a pending or active account, and thus if we should her account's activation.
754 private function suggest(PlUser $user)
755 {
756 $active = XDB::fetchOneCell('SELECT state = \'active\'
757 FROM accounts
758 WHERE uid = {?}',
759 $user->id());
760 $pending = XDB::fetchOneCell('SELECT uid
761 FROM register_pending_xnet
762 WHERE uid = {?}',
763 $user->id());
764 $requested = AccountReq::isPending($user->id());
765
766 if ($active || $pending || $requested) {
767 return false;
768 }
769 return true;
770 }
771
bc6e8005
SJ
772 function handler_admin_member_suggest($page, $hruid, $email)
773 {
774 $page->changeTpl('xnetgrp/membres-suggest.tpl');
41905adf
RB
775
776 // FS#703 : $_GET is urldecoded twice, hence
777 // + (the data) => %2B (in the url) => + (first decoding) => ' ' (second decoding)
778 // Since there can be no spaces in emails, we can fix this with :
779 $email = str_replace(' ', '+', $email);
bc6e8005
SJ
780
781 if (Post::has('suggest')) {
782 if (Post::t('suggest') == 'yes') {
783 $user = S::user();
784 $group = Platal::globals()->asso('nom');
785 $request = new AccountReq($user, $hruid, $email, $group);
786 $request->submit();
787 $page->trigSuccessRedirect('Un email va bien être envoyé à ' . $email . ' pour l\'activation de son compte.',
788 $group . '/member/' . $hruid);
789 } else {
790 pl_redirect('member/' . $hruid);
791 }
bb88d138 792 }
bc6e8005
SJ
793 $page->assign('email', $email);
794 $page->assign('hruid', $hruid);
ef7c8560 795 }
796
26ba053e 797 function handler_admin_member_new_ajax($page)
dc2073c3 798 {
3cb500d5 799 pl_content_headers("text/html");
8b1f8e12 800 $page->changeTpl('xnetgrp/membres-new-search.tpl', NO_SKIN);
aa21c568 801 $users = array();
0baf0741 802 if (Env::has('login')) {
aa21c568
FB
803 $user = User::getSilent(Env::t('login'));
804 if ($user && $user->state != 'pending') {
805 $users = array($user);
806 }
e2b0a45c 807 }
aa21c568 808 if (empty($users)) {
33a4f3f9 809 list($lastname, $firstname) = str_replace(array('-', ' ', "'"), '%', array(Env::t('nom'), Env::t('prenom')));
c8763ca3 810 $cond = new PFC_And(new PFC_Not(new UFC_Registered()));
33a4f3f9
SJ
811 if (!empty($lastname)) {
812 $cond->addChild(new UFC_Name(Profile::LASTNAME, $lastname, UFC_Name::CONTAINS));
0baf0741 813 }
33a4f3f9
SJ
814 if (!empty($firstname)) {
815 $cond->addChild(new UFC_Name(Profile::FIRSTNAME, $firstname, UFC_Name::CONTAINS));
0baf0741 816 }
33a4f3f9
SJ
817 if (Env::t('promo')) {
818 $cond->addChild(new UFC_Promo('=', UserFilter::DISPLAY, Env::t('promo')));
aa21c568
FB
819 }
820 $uf = new UserFilter($cond);
e1406965 821 $users = $uf->getUsers(new PlLimit(30));
aa21c568
FB
822 if ($uf->getTotalCount() > 30) {
823 $page->assign('too_many', true);
824 $users = array();
0baf0741 825 }
dc2073c3 826 }
aa21c568 827 $page->assign('users', $users);
dc2073c3 828 }
829
26ba053e 830 function unsubscribe(PlUser $user)
ef7c8560 831 {
d7610c35 832 global $globals;
eb41eda9 833 XDB::execute("DELETE FROM group_members
45dcd6dd
FB
834 WHERE uid = {?} AND asso_id = {?}",
835 $user->id(), $globals->asso('id'));
ef7c8560 836
fa9493fe
FB
837 if ($globals->asso('notif_unsub')) {
838 $mailer = new PlMailer('xnetgrp/unsubscription-notif.mail.tpl');
57c1f1d7
SJ
839 $admins = $globals->asso()->iterAdmins();
840 while ($admin = $admins->next()) {
841 $mailer->addTo($admin);
fa9493fe
FB
842 }
843 $mailer->assign('group', $globals->asso('nom'));
45dcd6dd
FB
844 $mailer->assign('user', $user);
845 $mailer->assign('selfdone', $user->id() == S::i('uid'));
fa9493fe
FB
846 $mailer->send();
847 }
eca5d69f 848
30032578 849 $domain = $globals->asso('mail_domain');
45dcd6dd 850 if (!$domain) {
30032578 851 return true;
852 }
ef7c8560 853
dc81a0b2 854 $mmlist = new MMList(S::user(), $domain);
45dcd6dd 855 $listes = $mmlist->get_lists($user->forlifeEmail());
ef7c8560 856
30032578 857 $may_update = may_update();
858 $warning = false;
23ba40c4
PC
859 if (is_array($listes)) {
860 foreach ($listes as $liste) {
861 if ($liste['sub'] == 2) {
862 if ($may_update) {
863 $mmlist->mass_unsubscribe($liste['list'], Array($user->forlifeEmail()));
864 } else {
865 $mmlist->unsubscribe($liste['list']);
866 }
867 } elseif ($liste['sub']) {
868 Platal::page()->trigWarning($user->fullName() . " a une"
869 ." demande d'inscription en cours sur la"
870 ." liste {$liste['list']}@ !");
871 $warning = true;
ef7c8560 872 }
873 }
30032578 874 }
ef7c8560 875
831aa467
SJ
876 XDB::execute('DELETE v
877 FROM email_virtual AS v
878 INNER JOIN email_virtual_domains AS d ON (v.domain = d.id)
879 WHERE v.redirect = {?} AND d.name = {?}',
880 $user->forlifeEmail(), $domain);
30032578 881 return !$warning;
882 }
883
26ba053e 884 function handler_unsubscribe($page)
30032578 885 {
1490093c 886 $page->changeTpl('xnetgrp/membres-del.tpl');
035de9c1 887 $user = S::user();
cb1f5408 888 if (empty($user)) {
30032578 889 return PL_NOT_FOUND;
ef7c8560 890 }
30032578 891 $page->assign('self', true);
cb1f5408 892 $page->assign('user', $user);
ef7c8560 893
30032578 894 if (!Post::has('confirm')) {
895 return;
e7fdf9dd
VZ
896 } else {
897 S::assert_xsrf_token();
30032578 898 }
899
9a7f3d8e 900 $hasSingleGroup = ($user->groupCount() == 1);
cb1f5408 901
30032578 902 if ($this->unsubscribe($user)) {
cb1f5408 903 $page->trigSuccess('Tu as été désinscrit du groupe avec succès.');
30032578 904 } else {
cb1f5408
SJ
905 $page->trigWarning('Tu as été désinscrit du groupe, mais des erreurs se sont produites lors des désinscriptions des alias et des listes de diffusion.');
906 }
8d014576
SJ
907
908 // If user is of type xnet account and this was her last group, disable the account.
909 if ($user->type == 'xnet' && $hasSingleGroup) {
910 $user->clear(true);
30032578 911 }
c1863ee9 912 $page->assign('is_member', is_member(true));
30032578 913 }
914
26ba053e 915 function handler_admin_member_del($page, $user = null)
30032578 916 {
1490093c 917 $page->changeTpl('xnetgrp/membres-del.tpl');
45dcd6dd 918 $user = User::getSilent($user);
30032578 919 if (empty($user)) {
920 return PL_NOT_FOUND;
921 }
4a648e80
SJ
922
923 global $globals;
924
925 if (!$user->inGroup($globals->asso('id'))) {
926 pl_redirect('annuaire');
927 }
928
cb1f5408 929 $page->assign('self', false);
30032578 930 $page->assign('user', $user);
931
932 if (!Post::has('confirm')) {
933 return;
e7fdf9dd
VZ
934 } else {
935 S::assert_xsrf_token();
30032578 936 }
937
9a7f3d8e 938 $hasSingleGroup = ($user->groupCount() == 1);
91d2135c 939
30032578 940 if ($this->unsubscribe($user)) {
e46cf8c4 941 $page->trigSuccess("{$user->fullName()} a été désinscrit du groupe&nbsp;!");
30032578 942 } else {
e46cf8c4 943 $page->trigWarning("{$user->fullName()} a été désinscrit du groupe, mais des erreurs subsistent&nbsp;!");
30032578 944 }
91d2135c 945
8d014576 946 // If user is of type xnet account and this was her last group, disable the account.
91d2135c 947 if ($user->type == 'xnet' && $hasSingleGroup) {
8d014576 948 $user->clear(true);
91d2135c 949 }
ef7c8560 950 }
951
2a1cd4ab 952 private function changeLogin(PlPage $page, PlUser $user, $login)
631565e1 953 {
33a4f3f9 954 // Search the user's uid.
bb88d138
PC
955 $xuser = User::getSilent($login);
956 if (!$xuser) {
61a7d279
SJ
957 $accounts = User::getPendingAccounts($login);
958 if (!$accounts) {
959 $page->trigError("L'identifiant $login ne correspond à aucun X.");
631565e1 960 return false;
61a7d279
SJ
961 } else if (count($accounts) > 1) {
962 $page->trigError("L'identifiant $login correspond à plusieurs camarades.");
631565e1
FB
963 return false;
964 }
bb88d138 965 $xuser = User::getSilent($accounts[0]['uid']);
631565e1
FB
966 }
967
bb88d138
PC
968 if (!$xuser) {
969 return false;
631565e1
FB
970 }
971
33a4f3f9
SJ
972 if ($user->mergeIn($xuser)) {
973 return $xuser->login();
974 }
975 return $user->login();
631565e1
FB
976 }
977
26ba053e 978 function handler_admin_member($page, $user)
ef7c8560 979 {
980 global $globals;
981
45dcd6dd 982 $user = User::getSilent($user);
ef7c8560 983 if (empty($user)) {
984 return PL_NOT_FOUND;
985 }
986
7ae5c545
SJ
987 if (!$user->inGroup($globals->asso('id'))) {
988 pl_redirect('annuaire');
989 }
990
991 $page->changeTpl('xnetgrp/membres-edit.tpl');
992
dc81a0b2 993 $mmlist = new MMList(S::user(), $globals->asso('mail_domain'));
ef7c8560 994
995 if (Post::has('change')) {
e7fdf9dd
VZ
996 S::assert_xsrf_token();
997
631565e1 998 // Convert user status to X
bb88d138 999 if (!Post::blank('login_X')) {
bb94f7b6 1000 $forlife = $this->changeLogin($page, $user, Post::t('login_X'));
631565e1
FB
1001 if ($forlife) {
1002 pl_redirect('member/' . $forlife);
1003 }
1004 }
1005
1006 // Update user info
bb88d138
PC
1007 $email_changed = (!$user->profile() && strtolower($user->forlifeEmail()) != strtolower(Post::v('email')));
1008 $from_email = $user->forlifeEmail();
1d6870e1 1009 if ($user->type == 'virtual' || ($user->type == 'xnet' && !$user->perms)) {
bb88d138 1010 XDB::query('UPDATE accounts
33a4f3f9
SJ
1011 SET full_name = {?}, directory_name = {?}, display_name = {?},
1012 sex = {?}, email = {?}, type = {?}
bb88d138 1013 WHERE uid = {?}',
33a4f3f9
SJ
1014 Post::t('full_name'), Post::t('directory_name'), Post::t('display_name'),
1015 (Post::t('sex') == 'male') ? 'male' : 'female', Post::t('email'),
1016 (Post::t('type') == 'xnet') ? 'xnet' : 'virtual', $user->id());
1017 } else if (!$user->perms) {
1018 XDB::query('UPDATE accounts
1019 SET email = {?}
1020 WHERE uid = {?}',
1021 Post::t('email'), $user->id());
1022 }
1023 if (XDB::affectedRows()) {
94bf736e 1024 $page->trigSuccess('Données de l\'utilisateur mises à jour.');
ef7c8560 1025 }
1026
fb422cab
SJ
1027 if (($user->type == 'xnet' && !$user->perms) && Post::b('suggest')) {
1028 $request = new AccountReq(S::user(), $user->hruid, Post::t('email'), $globals->asso('nom'));
1029 $request->submit();
1030 $page->trigSuccess('Le compte va bientôt être activé.');
1031 }
1032
bb88d138
PC
1033 // Update group params for user
1034 $perms = Post::v('group_perms');
a6761ca9 1035 $comm = Post::t('comm');
33fcb12c
SJ
1036 $position = (Post::t('group_position') == '') ? null : Post::v('group_position');
1037 if ($user->group_perms != $perms || $user->group_comm != $comm || $user->group_position != $position) {
eb41eda9 1038 XDB::query('UPDATE group_members
33fcb12c 1039 SET perms = {?}, comm = {?}, position = {?}
00112b2e 1040 WHERE uid = {?} AND asso_id = {?}',
33fcb12c 1041 ($perms == 'admin') ? 'admin' : 'membre', $comm, $position,
bb88d138 1042 $user->id(), $globals->asso('id'));
33a4f3f9
SJ
1043 if (XDB::affectedRows()) {
1044 if ($perms != $user->group_perms) {
1045 $page->trigSuccess('Permissions modifiées&nbsp;!');
1046 }
1047 if ($comm != $user->group_comm) {
1048 $page->trigSuccess('Commentaire mis à jour.');
1049 }
58149849
SJ
1050 if ($position != $user->group_position) {
1051 $page->trigSuccess('Poste mis à jour.');
1052 }
54b24ba2 1053 }
ef7c8560 1054 }
1055
bb88d138
PC
1056 // Gets user info again as they might have change
1057 $user = User::getSilent($user->id());
1058
631565e1 1059 // Update ML subscriptions
5e2307dc 1060 foreach (Env::v('ml1', array()) as $ml => $state) {
ef7c8560 1061 $ask = empty($_REQUEST['ml2'][$ml]) ? 0 : 2;
c4d57bd8 1062 if ($ask == $state) {
06db561e 1063 if ($state && $email_changed) {
bb88d138
PC
1064 $mmlist->replace_email($ml, $from_email, $user->forlifeEmail());
1065 $page->trigSuccess("L'abonnement de {$user->fullName()} à $ml@ a été mis à jour.");
c4d57bd8 1066 }
1067 continue;
1068 }
ef7c8560 1069 if ($state == '1') {
bb88d138 1070 $page->trigWarning("{$user->fullName()} a "
ef7c8560 1071 ."actuellement une demande d'inscription en "
1072 ."cours sur <strong>$ml@</strong> !!!");
1073 } elseif ($ask) {
bb88d138
PC
1074 $mmlist->mass_subscribe($ml, Array($user->forlifeEmail()));
1075 $page->trigSuccess("{$user->fullName()} a été abonné à $ml@.");
ef7c8560 1076 } else {
c4d57bd8 1077 if ($email_changed) {
1078 $mmlist->mass_unsubscribe($ml, Array($from_email));
1079 } else {
bb88d138 1080 $mmlist->mass_unsubscribe($ml, Array($user->forlifeEmail()));
c4d57bd8 1081 }
bb88d138 1082 $page->trigSuccess("{$user->fullName()} a été désabonné de $ml@.");
ef7c8560 1083 }
1084 }
1085
631565e1 1086 // Change subscriptioin to aliases
5e2307dc 1087 foreach (Env::v('ml3', array()) as $ml => $state) {
831aa467 1088 require_once 'emails.inc.php';
ef7c8560 1089 $ask = !empty($_REQUEST['ml4'][$ml]);
bb88d138
PC
1090 if($state == $ask) {
1091 if ($state && $email_changed) {
831aa467 1092 update_list_alias($user, $from_email, $ml, $globals->asso('mail_domain'));
bb88d138
PC
1093 $page->trigSuccess("L'abonnement de {$user->fullName()} à $ml a été mis à jour.");
1094 }
1095 } else if($ask) {
831aa467 1096 add_to_list_alias($user, $ml, $globals->asso('mail_domain'));
bb88d138 1097 $page->trigSuccess("{$user->fullName()} a été abonné à $ml.");
ef7c8560 1098 } else {
831aa467 1099 delete_from_list_alias($user, $ml, $globals->asso('mail_domain'));
bb88d138 1100 $page->trigSuccess("{$user->fullName()} a été désabonné de $ml.");
ef7c8560 1101 }
1102 }
1103 }
1104
33fcb12c
SJ
1105 $res = XDB::rawFetchAllAssoc('SHOW COLUMNS FROM group_members LIKE \'position\'');
1106 $positions = str_replace(array('enum(', ')', '\''), '', $res[0]['Type']);
1107
ef7c8560 1108 $page->assign('user', $user);
fb422cab 1109 $page->assign('suggest', $this->suggest($user));
a6761ca9 1110 $page->assign('listes', $mmlist->get_lists($user->forlifeEmail()));
06a548e5 1111 $page->assign('alias', $user->emailGroupAliases($globals->asso('mail_domain')));
33fcb12c 1112 $page->assign('positions', explode(',', $positions));
ef7c8560 1113 }
24bcf50c 1114
82af3fc3 1115 function handler_rss(PlPage $page, PlUser $user)
24bcf50c 1116 {
1117 global $globals;
24bcf50c 1118 $page->assign('asso', $globals->asso());
4f18bb11 1119
460d8f55 1120 $this->load('feed.inc.php');
4f18bb11 1121 $feed = new XnetGrpEventFeed();
190efb3a 1122 return $feed->run($page, $user, false);
24bcf50c 1123 }
1124
26ba053e 1125 private function upload_image(PlPage $page, PlUpload $upload)
ff95a302
FB
1126 {
1127 if (@!$_FILES['image']['tmp_name'] && !Env::v('image_url')) {
1128 return true;
1129 }
1130 if (!$upload->upload($_FILES['image']) && !$upload->download(Env::v('image_url'))) {
a7d35093 1131 $page->trigError('Impossible de télécharger l\'image');
ff95a302
FB
1132 return false;
1133 } elseif (!$upload->isType('image')) {
a7d35093 1134 $page->trigError('Le fichier n\'est pas une image valide au format JPEG, GIF ou PNG.');
ff95a302
FB
1135 $upload->rm();
1136 return false;
1137 } elseif (!$upload->resizeImage(200, 300, 100, 100, 32284)) {
a7d35093 1138 $page->trigError('Impossible de retraiter l\'image');
ff95a302
FB
1139 return false;
1140 }
1141 return true;
1142 }
1143
26ba053e 1144 function handler_photo_announce($page, $eid = null) {
ff95a302 1145 if ($eid) {
14bc135e
SJ
1146 $res = XDB::query('SELECT *
1147 FROM group_announces_photo
1148 WHERE eid = {?}', $eid);
ff95a302
FB
1149 if ($res->numRows()) {
1150 $photo = $res->fetchOneAssoc();
3cb500d5 1151 pl_cached_dynamic_content_headers("image/" . $photo['attachmime']);
ff95a302
FB
1152 echo $photo['attach'];
1153 exit;
1154 }
1155 } else {
f3df6d38 1156 $upload = new PlUpload(S::user()->login(), 'xnetannounce');
ff95a302 1157 if ($upload->exists() && $upload->isType('image')) {
3cb500d5 1158 pl_cached_dynamic_content_headers($upload->contentType());
ff95a302
FB
1159 echo $upload->getContents();
1160 exit;
1161 }
1162 }
1163 global $globals;
3cb500d5 1164 pl_cached_dynamic_content_headers("image/png");
ff95a302
FB
1165 echo file_get_contents($globals->spoolroot . '/htdocs/images/logo.png');
1166 exit;
1167 }
1168
26ba053e 1169 function handler_edit_announce($page, $aid = null)
24bcf50c 1170 {
1171 global $globals, $platal;
1490093c 1172 $page->changeTpl('xnetgrp/announce-edit.tpl');
24bcf50c 1173 $page->assign('new', is_null($aid));
1174 $art = array();
1175
ff95a302 1176 if (Post::v('valid') == 'Visualiser' || Post::v('valid') == 'Enregistrer'
2f8677c2 1177 || Post::v('valid') == 'Supprimer l\'image' || Post::v('valid') == 'Pas d\'image') {
e7fdf9dd
VZ
1178 S::assert_xsrf_token();
1179
24bcf50c 1180 if (!is_null($aid)) {
1181 $art['id'] = $aid;
1182 }
1183 $art['titre'] = Post::v('titre');
1184 $art['texte'] = Post::v('texte');
1185 $art['contacts'] = Post::v('contacts');
1186 $art['promo_min'] = Post::i('promo_min');
1187 $art['promo_max'] = Post::i('promo_max');
1188 $art['nom'] = S::v('nom');
1189 $art['prenom'] = S::v('prenom');
1190 $art['promo'] = S::v('promo');
0fab5209 1191 $art['hruid'] = S::user()->login();
79e610a9 1192 $art['uid'] = S::user()->id();
b58e5ac2 1193 $art['expiration'] = Post::v('expiration');
24bcf50c 1194 $art['public'] = Post::has('public');
1195 $art['xorg'] = Post::has('xorg');
1196 $art['nl'] = Post::has('nl');
1197 $art['event'] = Post::v('event');
f3df6d38 1198 $upload = new PlUpload(S::user()->login(), 'xnetannounce');
ff95a302 1199 $this->upload_image($page, $upload);
24bcf50c 1200
1201 $art['contact_html'] = $art['contacts'];
b9a1ba64 1202 if ($art['event']) {
1203 $art['contact_html'] .= "\n{$globals->baseurl}/{$platal->ns}events/sub/{$art['event']}";
24bcf50c 1204 }
2a557a09 1205
1206 if (!$art['public'] &&
d1c97e42 1207 (($art['promo_min'] > $art['promo_max'] && $art['promo_max'] != 0) ||
2a557a09 1208 ($art['promo_min'] != 0 && ($art['promo_min'] <= 1900 || $art['promo_min'] >= 2020)) ||
1209 ($art['promo_max'] != 0 && ($art['promo_max'] <= 1900 || $art['promo_max'] >= 2020))))
1210 {
a7d35093 1211 $page->trigError("L'intervalle de promotions est invalide.");
2a557a09 1212 Post::kill('valid');
1213 }
1970c12b 1214
1215 if (!trim($art['titre']) || !trim($art['texte'])) {
a7d35093 1216 $page->trigError("L'article doit avoir un titre et un contenu.");
1970c12b 1217 Post::kill('valid');
1218 }
ff95a302
FB
1219
1220 if (Post::v('valid') == 'Supprimer l\'image') {
1221 $upload->rm();
1222 Post::kill('valid');
1223 }
2f8677c2
FB
1224 $art['photo'] = $upload->exists() || Post::i('photo');
1225 if (Post::v('valid') == 'Pas d\'image' && !is_null($aid)) {
14bc135e
SJ
1226 XDB::query('DELETE FROM group_announces_photo
1227 WHERE eid = {?}', $aid);
2f8677c2
FB
1228 $upload->rm();
1229 Post::kill('valid');
1230 $art['photo'] = false;
1231 }
24bcf50c 1232 }
1233
1234 if (Post::v('valid') == 'Enregistrer') {
2a557a09 1235 $promo_min = ($art['public'] ? 0 : $art['promo_min']);
1236 $promo_max = ($art['public'] ? 0 : $art['promo_max']);
113f6de8 1237 $flags = new PlFlagSet();
ff95a302 1238 if ($art['public']) {
77e786e1 1239 $flags->addFlag('public');
ff95a302
FB
1240 }
1241 if ($art['photo']) {
77e786e1 1242 $flags->addFlag('photo');
ff95a302 1243 }
24bcf50c 1244 if (is_null($aid)) {
eaf30d86
PH
1245 $fulltext = $art['texte'];
1246 if (!empty($art['contact_html'])) {
1247 $fulltext .= "\n\n'''Contacts :'''\\\\\n" . $art['contact_html'];
e219710a 1248 }
ff95a302 1249 $post = null;/*
e219710a 1250 if ($globals->asso('forum')) {
1251 require_once 'banana/forum.inc.php';
e3a55098 1252 $banana = new ForumsBanana(S::user());
e219710a 1253 $post = $banana->post($globals->asso('forum'), null,
1254 $art['titre'], MiniWiki::wikiToText($fulltext, false, 0, 80));
ff95a302 1255 }*/
14bc135e 1256 XDB::query('INSERT INTO group_announces (uid, asso_id, create_date, titre, texte, contacts,
b58e5ac2 1257 expiration, promo_min, promo_max, flags, post_id)
14bc135e 1258 VALUES ({?}, {?}, NOW(), {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?})',
24bcf50c 1259 S::i('uid'), $globals->asso('id'), $art['titre'], $art['texte'], $art['contact_html'],
b58e5ac2 1260 $art['expiration'], $promo_min, $promo_max, $flags, $post);
8b83a166 1261 $aid = XDB::insertId();
ff95a302
FB
1262 if ($art['photo']) {
1263 list($imgx, $imgy, $imgtype) = $upload->imageInfo();
14bc135e
SJ
1264 XDB::execute('INSERT INTO group_announces_photo
1265 SET eid = {?}, attachmime = {?}, x = {?}, y = {?}, attach = {?}',
ff95a302
FB
1266 $aid, $imgtype, $imgx, $imgy, $upload->getContents());
1267 }
24bcf50c 1268 if ($art['xorg']) {
794feea7 1269 $article = new EvtReq("[{$globals->asso('nom')}] " . $art['titre'], $fulltext,
b58e5ac2 1270 $art['promo_min'], $art['promo_max'], $art['expiration'], "", S::user(),
ff95a302 1271 $upload);
24bcf50c 1272 $article->submit();
a7d35093 1273 $page->trigWarning("L'affichage sur la page d'accueil de Polytechnique.org est en attente de validation.");
ff95a302
FB
1274 } else if ($upload && $upload->exists()) {
1275 $upload->rm();
24bcf50c 1276 }
1277 if ($art['nl']) {
5daf68f6 1278 $article = new NLReq(S::user(), $globals->asso('nom') . " : " .$art['titre'],
2a557a09 1279 $art['texte'], $art['contact_html']);
24bcf50c 1280 $article->submit();
a7d35093 1281 $page->trigWarning("La parution dans la Lettre Mensuelle est en attente de validation.");
24bcf50c 1282 }
1283 } else {
14bc135e 1284 XDB::query('UPDATE group_announces
b58e5ac2 1285 SET titre = {?}, texte = {?}, contacts = {?}, expiration = {?},
14bc135e
SJ
1286 promo_min = {?}, promo_max = {?}, flags = {?}
1287 WHERE id = {?} AND asso_id = {?}',
b58e5ac2 1288 $art['titre'], $art['texte'], $art['contacts'], $art['expiration'],
ff95a302 1289 $promo_min, $promo_max, $flags,
24bcf50c 1290 $art['id'], $globals->asso('id'));
2f8677c2
FB
1291 if ($art['photo'] && $upload->exists()) {
1292 list($imgx, $imgy, $imgtype) = $upload->imageInfo();
00ba8a74
SJ
1293 XDB::execute('INSERT INTO group_announces_photo (eid, attachmime, attach, x, y)
1294 VALUES ({?}, {?}, {?}, {?}, {?})
1295 ON DUPLICATE KEY UPDATE attachmime = VALUES(attachmime), attach = VALUES(attach), x = VALUES(x), y = VALUES(y)',
1296 $aid, $imgtype, $upload->getContents(), $imgx, $imgy);
2f8677c2
FB
1297 $upload->rm();
1298 }
24bcf50c 1299 }
2a557a09 1300 }
1301 if (Post::v('valid') == 'Enregistrer' || Post::v('valid') == 'Annuler') {
24bcf50c 1302 pl_redirect("");
eaf30d86 1303 }
24bcf50c 1304
1305 if (empty($art) && !is_null($aid)) {
14bc135e
SJ
1306 $res = XDB::query("SELECT *, FIND_IN_SET('public', flags) AS public,
1307 FIND_IN_SET('photo', flags) AS photo
1308 FROM group_announces
1309 WHERE asso_id = {?} AND id = {?}",
24bcf50c 1310 $globals->asso('id'), $aid);
1311 if ($res->numRows()) {
1312 $art = $res->fetchOneAssoc();
1313 $art['contact_html'] = $art['contacts'];
1314 } else {
567553ac 1315 $page->kill("Aucun article correspond à l'identifiant indiqué.");
24bcf50c 1316 }
1317 }
1318
24bcf50c 1319 if (is_null($aid)) {
1320 $events = XDB::iterator("SELECT *
eb41eda9 1321 FROM group_events
24bcf50c 1322 WHERE asso_id = {?} AND archive = 0",
1323 $globals->asso('id'));
1324 if ($events->total()) {
1325 $page->assign('events', $events);
1326 }
eaf30d86 1327 }
24bcf50c 1328
72b2f8bb 1329 $art['contact_html'] = @MiniWiki::WikiToHTML($art['contact_html']);
24bcf50c 1330 $page->assign('art', $art);
ff95a302 1331 $page->assign_by_ref('upload', $upload);
24bcf50c 1332 }
1333
26ba053e 1334 function handler_admin_announce($page)
24bcf50c 1335 {
1336 global $globals;
1490093c 1337 $page->changeTpl('xnetgrp/announce-admin.tpl');
24bcf50c 1338
1339 if (Env::has('del')) {
e7fdf9dd 1340 S::assert_xsrf_token();
14bc135e
SJ
1341 XDB::execute('DELETE FROM group_announces
1342 WHERE id = {?} AND asso_id = {?}',
24bcf50c 1343 Env::i('del'), $globals->asso('id'));
1344 }
b58e5ac2 1345 $res = XDB::iterator('SELECT id, titre, expiration, expiration < CURRENT_DATE() AS perime
14bc135e
SJ
1346 FROM group_announces
1347 WHERE asso_id = {?}
b58e5ac2 1348 ORDER BY expiration DESC',
24bcf50c 1349 $globals->asso('id'));
1350 $page->assign('articles', $res);
1351 }
ae7bb180 1352}
1353
a7de4ef7 1354// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
ae7bb180 1355?>