tu -> vous for xnet accounts.
[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),
eb5a266d
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),
eb5a266d
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),
eb5a266d
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'),
bc6e8005 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),
eb5a266d
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),
eb5a266d 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
33a4f3f9 662 // Finds or creates account: first cases are for users with an account.
a1642859 663 if (!User::isForeignEmailAddress($email)) {
33a4f3f9 664 // Standard account
a1642859 665 $user = User::get($email);
bb88d138
PC
666 } else if (!isvalid_email($email)) {
667 // email might not be a regular email but an alias or a hruid
668 $user = User::get($email);
669 if (!$user) {
670 // need a valid email address
33a4f3f9 671 $page->trigError('«&nbsp;<strong>' . $email . '</strong>&nbsp;» n\'est pas une adresse email valide.');
bb88d138 672 return;
ef7c8560 673 }
bb88d138 674 } else if (Env::v('x') && Env::i('userid')) {
bb88d138
PC
675 $user = User::getWithUID(Env::i('userid'));
676 if (!$user) {
33a4f3f9 677 $page->trigError('Utilisateur invalide.');
bb88d138
PC
678 return;
679 }
33a4f3f9
SJ
680
681 // User has an account but is not yet registered.
682 if ($user->state == 'pending') {
683 // Add email in account table.
684 XDB::query('UPDATE accounts
685 SET email = {?}
686 WHERE uid = {?} AND email IS NULL',
687 Post::t('email'), $user->id());
688 // Add email for marketing if required.
689 if (Env::v('market')) {
690 $market = Marketing::get($user->uid, $email);
691 if (!$market) {
692 $market = new Marketing($user->uid, $email, 'group', $globals->asso('nom'),
693 Env::v('market_from'), S::v('uid'));
694 $market->add();
695 }
dc2073c3 696 }
bb88d138
PC
697 }
698 } else {
bc6e8005
SJ
699 // User is of type xnet. There are 3 possible cases:
700 // * the email is not known yet: we create a new account and
701 // propose to send an email to the user so he can activate
702 // his account,
703 // * the email is known but the user was not contacted in order to
704 // activate yet: we propose to send an email to the user so he
705 // can activate his account,
706 // * the email is known and the user was already contacted or has
707 // an active account: nothing to be done.
445d244c
RB
708 list($mbox, $domain) = explode('@', strtolower($email));
709 $hruid = User::makeHrid($mbox, $domain, 'ext');
33a4f3f9 710 // User might already have an account (in another group for example).
bb88d138 711 $user = User::get($hruid);
33a4f3f9
SJ
712
713 // If the user has no account yet, creates new account: build names from email address.
bb88d138 714 if (empty($user)) {
445d244c
RB
715 $parts = explode('.', $mbox);
716 if (count($parts) == 1) {
717 $display_name = $full_name = $directory_name = ucfirst($mbox);
718 } else {
719 $firstname = ucfirst($parts[0]);
720 $lastname = ucwords(implode(' ', array_slice($parts, 1)));
721 $display_name = $firstname;
722 $full_name = "$firstname $lastname";
723 $directory_name = strtoupper($lastname) . " " . $firstname;
724 }
725 XDB::execute('INSERT INTO accounts (hruid, display_name, full_name, directory_name,
726 email, type)
727 VALUES ({?}, {?}, {?}, {?}, {?}, \'xnet\')',
728 $hruid, $display_name, $full_name, $directory_name, $email);
bb88d138 729 $user = User::get($hruid);
ef7c8560 730 }
bc6e8005
SJ
731
732 // Check if the user is has a pending or active account.
733 $active = XDB::fetchOneCell('SELECT state = \'active\'
734 FROM accounts
735 WHERE uid = {?}',
736 $user->id());
737 $pending = XDB::fetchOneCell('SELECT uid
738 FROM register_pending_xnet
739 WHERE uid = {?}',
740 $user->id());
741 $requested = AccountReq::isPending($user->id());
742
743 if (!($active || $pending || $requested)) {
744 $suggest_account_activation = true;
745 }
ef7c8560 746 }
33a4f3f9 747
bb88d138 748 if ($user) {
00ba8a74
SJ
749 XDB::execute('INSERT IGNORE INTO group_members (uid, asso_id)
750 VALUES ({?}, {?})',
bb88d138
PC
751 $user->id(), $globals->asso('id'));
752 $this->removeSubscriptionRequest($user->id());
bc6e8005
SJ
753 if ($suggest_account_activation) {
754 pl_redirect('member/suggest/' . $user->login() . '/' . $email . '/' . $globals->asso('nom'));
755 } else {
756 pl_redirect('member/' . $user->login());
757 }
758 }
759 }
760
761 function handler_admin_member_suggest($page, $hruid, $email)
762 {
763 $page->changeTpl('xnetgrp/membres-suggest.tpl');
764
765 if (Post::has('suggest')) {
766 if (Post::t('suggest') == 'yes') {
767 $user = S::user();
768 $group = Platal::globals()->asso('nom');
769 $request = new AccountReq($user, $hruid, $email, $group);
770 $request->submit();
771 $page->trigSuccessRedirect('Un email va bien être envoyé à ' . $email . ' pour l\'activation de son compte.',
772 $group . '/member/' . $hruid);
773 } else {
774 pl_redirect('member/' . $hruid);
775 }
bb88d138 776 }
bc6e8005
SJ
777 $page->assign('email', $email);
778 $page->assign('hruid', $hruid);
ef7c8560 779 }
780
26ba053e 781 function handler_admin_member_new_ajax($page)
dc2073c3 782 {
3cb500d5 783 pl_content_headers("text/html");
8b1f8e12 784 $page->changeTpl('xnetgrp/membres-new-search.tpl', NO_SKIN);
aa21c568 785 $users = array();
0baf0741 786 if (Env::has('login')) {
aa21c568
FB
787 $user = User::getSilent(Env::t('login'));
788 if ($user && $user->state != 'pending') {
789 $users = array($user);
790 }
e2b0a45c 791 }
aa21c568 792 if (empty($users)) {
33a4f3f9 793 list($lastname, $firstname) = str_replace(array('-', ' ', "'"), '%', array(Env::t('nom'), Env::t('prenom')));
c8763ca3 794 $cond = new PFC_And(new PFC_Not(new UFC_Registered()));
33a4f3f9
SJ
795 if (!empty($lastname)) {
796 $cond->addChild(new UFC_Name(Profile::LASTNAME, $lastname, UFC_Name::CONTAINS));
0baf0741 797 }
33a4f3f9
SJ
798 if (!empty($firstname)) {
799 $cond->addChild(new UFC_Name(Profile::FIRSTNAME, $firstname, UFC_Name::CONTAINS));
0baf0741 800 }
33a4f3f9
SJ
801 if (Env::t('promo')) {
802 $cond->addChild(new UFC_Promo('=', UserFilter::DISPLAY, Env::t('promo')));
aa21c568
FB
803 }
804 $uf = new UserFilter($cond);
e1406965 805 $users = $uf->getUsers(new PlLimit(30));
aa21c568
FB
806 if ($uf->getTotalCount() > 30) {
807 $page->assign('too_many', true);
808 $users = array();
0baf0741 809 }
dc2073c3 810 }
aa21c568 811 $page->assign('users', $users);
dc2073c3 812 }
813
26ba053e 814 function unsubscribe(PlUser $user)
ef7c8560 815 {
d7610c35 816 global $globals;
eb41eda9 817 XDB::execute("DELETE FROM group_members
45dcd6dd
FB
818 WHERE uid = {?} AND asso_id = {?}",
819 $user->id(), $globals->asso('id'));
ef7c8560 820
fa9493fe
FB
821 if ($globals->asso('notif_unsub')) {
822 $mailer = new PlMailer('xnetgrp/unsubscription-notif.mail.tpl');
57c1f1d7
SJ
823 $admins = $globals->asso()->iterAdmins();
824 while ($admin = $admins->next()) {
825 $mailer->addTo($admin);
fa9493fe
FB
826 }
827 $mailer->assign('group', $globals->asso('nom'));
45dcd6dd
FB
828 $mailer->assign('user', $user);
829 $mailer->assign('selfdone', $user->id() == S::i('uid'));
fa9493fe
FB
830 $mailer->send();
831 }
eca5d69f 832
30032578 833 $domain = $globals->asso('mail_domain');
45dcd6dd 834 if (!$domain) {
30032578 835 return true;
836 }
ef7c8560 837
dc81a0b2 838 $mmlist = new MMList(S::user(), $domain);
45dcd6dd 839 $listes = $mmlist->get_lists($user->forlifeEmail());
ef7c8560 840
30032578 841 $may_update = may_update();
842 $warning = false;
23ba40c4
PC
843 if (is_array($listes)) {
844 foreach ($listes as $liste) {
845 if ($liste['sub'] == 2) {
846 if ($may_update) {
847 $mmlist->mass_unsubscribe($liste['list'], Array($user->forlifeEmail()));
848 } else {
849 $mmlist->unsubscribe($liste['list']);
850 }
851 } elseif ($liste['sub']) {
852 Platal::page()->trigWarning($user->fullName() . " a une"
853 ." demande d'inscription en cours sur la"
854 ." liste {$liste['list']}@ !");
855 $warning = true;
ef7c8560 856 }
857 }
30032578 858 }
ef7c8560 859
831aa467
SJ
860 XDB::execute('DELETE v
861 FROM email_virtual AS v
862 INNER JOIN email_virtual_domains AS d ON (v.domain = d.id)
863 WHERE v.redirect = {?} AND d.name = {?}',
864 $user->forlifeEmail(), $domain);
30032578 865 return !$warning;
866 }
867
26ba053e 868 function handler_unsubscribe($page)
30032578 869 {
1490093c 870 $page->changeTpl('xnetgrp/membres-del.tpl');
035de9c1 871 $user = S::user();
cb1f5408 872 if (empty($user)) {
30032578 873 return PL_NOT_FOUND;
ef7c8560 874 }
30032578 875 $page->assign('self', true);
cb1f5408 876 $page->assign('user', $user);
ef7c8560 877
30032578 878 if (!Post::has('confirm')) {
879 return;
e7fdf9dd
VZ
880 } else {
881 S::assert_xsrf_token();
30032578 882 }
883
9a7f3d8e 884 $hasSingleGroup = ($user->groupCount() == 1);
cb1f5408 885
30032578 886 if ($this->unsubscribe($user)) {
cb1f5408 887 $page->trigSuccess('Tu as été désinscrit du groupe avec succès.');
30032578 888 } else {
cb1f5408
SJ
889 $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.');
890 }
8d014576
SJ
891
892 // If user is of type xnet account and this was her last group, disable the account.
893 if ($user->type == 'xnet' && $hasSingleGroup) {
894 $user->clear(true);
30032578 895 }
c1863ee9 896 $page->assign('is_member', is_member(true));
30032578 897 }
898
26ba053e 899 function handler_admin_member_del($page, $user = null)
30032578 900 {
1490093c 901 $page->changeTpl('xnetgrp/membres-del.tpl');
45dcd6dd 902 $user = User::getSilent($user);
30032578 903 if (empty($user)) {
904 return PL_NOT_FOUND;
905 }
4a648e80
SJ
906
907 global $globals;
908
909 if (!$user->inGroup($globals->asso('id'))) {
910 pl_redirect('annuaire');
911 }
912
cb1f5408 913 $page->assign('self', false);
30032578 914 $page->assign('user', $user);
915
916 if (!Post::has('confirm')) {
917 return;
e7fdf9dd
VZ
918 } else {
919 S::assert_xsrf_token();
30032578 920 }
921
9a7f3d8e 922 $hasSingleGroup = ($user->groupCount() == 1);
91d2135c 923
30032578 924 if ($this->unsubscribe($user)) {
e46cf8c4 925 $page->trigSuccess("{$user->fullName()} a été désinscrit du groupe&nbsp;!");
30032578 926 } else {
e46cf8c4 927 $page->trigWarning("{$user->fullName()} a été désinscrit du groupe, mais des erreurs subsistent&nbsp;!");
30032578 928 }
91d2135c 929
8d014576 930 // If user is of type xnet account and this was her last group, disable the account.
91d2135c 931 if ($user->type == 'xnet' && $hasSingleGroup) {
8d014576 932 $user->clear(true);
91d2135c 933 }
ef7c8560 934 }
935
2a1cd4ab 936 private function changeLogin(PlPage $page, PlUser $user, $login)
631565e1 937 {
33a4f3f9 938 // Search the user's uid.
bb88d138
PC
939 $xuser = User::getSilent($login);
940 if (!$xuser) {
61a7d279
SJ
941 $accounts = User::getPendingAccounts($login);
942 if (!$accounts) {
943 $page->trigError("L'identifiant $login ne correspond à aucun X.");
631565e1 944 return false;
61a7d279
SJ
945 } else if (count($accounts) > 1) {
946 $page->trigError("L'identifiant $login correspond à plusieurs camarades.");
631565e1
FB
947 return false;
948 }
bb88d138 949 $xuser = User::getSilent($accounts[0]['uid']);
631565e1
FB
950 }
951
bb88d138
PC
952 if (!$xuser) {
953 return false;
631565e1
FB
954 }
955
33a4f3f9
SJ
956 if ($user->mergeIn($xuser)) {
957 return $xuser->login();
958 }
959 return $user->login();
631565e1
FB
960 }
961
26ba053e 962 function handler_admin_member($page, $user)
ef7c8560 963 {
964 global $globals;
965
45dcd6dd 966 $user = User::getSilent($user);
ef7c8560 967 if (empty($user)) {
968 return PL_NOT_FOUND;
969 }
970
7ae5c545
SJ
971 if (!$user->inGroup($globals->asso('id'))) {
972 pl_redirect('annuaire');
973 }
974
975 $page->changeTpl('xnetgrp/membres-edit.tpl');
976
dc81a0b2 977 $mmlist = new MMList(S::user(), $globals->asso('mail_domain'));
ef7c8560 978
979 if (Post::has('change')) {
e7fdf9dd
VZ
980 S::assert_xsrf_token();
981
631565e1 982 // Convert user status to X
bb88d138 983 if (!Post::blank('login_X')) {
bb94f7b6 984 $forlife = $this->changeLogin($page, $user, Post::t('login_X'));
631565e1
FB
985 if ($forlife) {
986 pl_redirect('member/' . $forlife);
987 }
988 }
989
990 // Update user info
bb88d138
PC
991 $email_changed = (!$user->profile() && strtolower($user->forlifeEmail()) != strtolower(Post::v('email')));
992 $from_email = $user->forlifeEmail();
1d6870e1 993 if ($user->type == 'virtual' || ($user->type == 'xnet' && !$user->perms)) {
bb88d138 994 XDB::query('UPDATE accounts
33a4f3f9
SJ
995 SET full_name = {?}, directory_name = {?}, display_name = {?},
996 sex = {?}, email = {?}, type = {?}
bb88d138 997 WHERE uid = {?}',
33a4f3f9
SJ
998 Post::t('full_name'), Post::t('directory_name'), Post::t('display_name'),
999 (Post::t('sex') == 'male') ? 'male' : 'female', Post::t('email'),
1000 (Post::t('type') == 'xnet') ? 'xnet' : 'virtual', $user->id());
1001 } else if (!$user->perms) {
1002 XDB::query('UPDATE accounts
1003 SET email = {?}
1004 WHERE uid = {?}',
1005 Post::t('email'), $user->id());
1006 }
1007 if (XDB::affectedRows()) {
94bf736e 1008 $page->trigSuccess('Données de l\'utilisateur mises à jour.');
ef7c8560 1009 }
1010
bb88d138
PC
1011 // Update group params for user
1012 $perms = Post::v('group_perms');
a6761ca9 1013 $comm = Post::t('comm');
33fcb12c
SJ
1014 $position = (Post::t('group_position') == '') ? null : Post::v('group_position');
1015 if ($user->group_perms != $perms || $user->group_comm != $comm || $user->group_position != $position) {
eb41eda9 1016 XDB::query('UPDATE group_members
33fcb12c 1017 SET perms = {?}, comm = {?}, position = {?}
00112b2e 1018 WHERE uid = {?} AND asso_id = {?}',
33fcb12c 1019 ($perms == 'admin') ? 'admin' : 'membre', $comm, $position,
bb88d138 1020 $user->id(), $globals->asso('id'));
33a4f3f9
SJ
1021 if (XDB::affectedRows()) {
1022 if ($perms != $user->group_perms) {
1023 $page->trigSuccess('Permissions modifiées&nbsp;!');
1024 }
1025 if ($comm != $user->group_comm) {
1026 $page->trigSuccess('Commentaire mis à jour.');
1027 }
58149849
SJ
1028 if ($position != $user->group_position) {
1029 $page->trigSuccess('Poste mis à jour.');
1030 }
54b24ba2 1031 }
ef7c8560 1032 }
1033
bb88d138
PC
1034 // Gets user info again as they might have change
1035 $user = User::getSilent($user->id());
1036
631565e1 1037 // Update ML subscriptions
5e2307dc 1038 foreach (Env::v('ml1', array()) as $ml => $state) {
ef7c8560 1039 $ask = empty($_REQUEST['ml2'][$ml]) ? 0 : 2;
c4d57bd8 1040 if ($ask == $state) {
06db561e 1041 if ($state && $email_changed) {
bb88d138
PC
1042 $mmlist->replace_email($ml, $from_email, $user->forlifeEmail());
1043 $page->trigSuccess("L'abonnement de {$user->fullName()} à $ml@ a été mis à jour.");
c4d57bd8 1044 }
1045 continue;
1046 }
ef7c8560 1047 if ($state == '1') {
bb88d138 1048 $page->trigWarning("{$user->fullName()} a "
ef7c8560 1049 ."actuellement une demande d'inscription en "
1050 ."cours sur <strong>$ml@</strong> !!!");
1051 } elseif ($ask) {
bb88d138
PC
1052 $mmlist->mass_subscribe($ml, Array($user->forlifeEmail()));
1053 $page->trigSuccess("{$user->fullName()} a été abonné à $ml@.");
ef7c8560 1054 } else {
c4d57bd8 1055 if ($email_changed) {
1056 $mmlist->mass_unsubscribe($ml, Array($from_email));
1057 } else {
bb88d138 1058 $mmlist->mass_unsubscribe($ml, Array($user->forlifeEmail()));
c4d57bd8 1059 }
bb88d138 1060 $page->trigSuccess("{$user->fullName()} a été désabonné de $ml@.");
ef7c8560 1061 }
1062 }
1063
631565e1 1064 // Change subscriptioin to aliases
5e2307dc 1065 foreach (Env::v('ml3', array()) as $ml => $state) {
831aa467 1066 require_once 'emails.inc.php';
ef7c8560 1067 $ask = !empty($_REQUEST['ml4'][$ml]);
bb88d138
PC
1068 if($state == $ask) {
1069 if ($state && $email_changed) {
831aa467 1070 update_list_alias($user, $from_email, $ml, $globals->asso('mail_domain'));
bb88d138
PC
1071 $page->trigSuccess("L'abonnement de {$user->fullName()} à $ml a été mis à jour.");
1072 }
1073 } else if($ask) {
831aa467 1074 add_to_list_alias($user, $ml, $globals->asso('mail_domain'));
bb88d138 1075 $page->trigSuccess("{$user->fullName()} a été abonné à $ml.");
ef7c8560 1076 } else {
831aa467 1077 delete_from_list_alias($user, $ml, $globals->asso('mail_domain'));
bb88d138 1078 $page->trigSuccess("{$user->fullName()} a été désabonné de $ml.");
ef7c8560 1079 }
1080 }
1081 }
1082
33fcb12c
SJ
1083 $res = XDB::rawFetchAllAssoc('SHOW COLUMNS FROM group_members LIKE \'position\'');
1084 $positions = str_replace(array('enum(', ')', '\''), '', $res[0]['Type']);
1085
ef7c8560 1086 $page->assign('user', $user);
a6761ca9 1087 $page->assign('listes', $mmlist->get_lists($user->forlifeEmail()));
06a548e5 1088 $page->assign('alias', $user->emailGroupAliases($globals->asso('mail_domain')));
33fcb12c 1089 $page->assign('positions', explode(',', $positions));
ef7c8560 1090 }
24bcf50c 1091
82af3fc3 1092 function handler_rss(PlPage $page, PlUser $user)
24bcf50c 1093 {
1094 global $globals;
24bcf50c 1095 $page->assign('asso', $globals->asso());
4f18bb11 1096
460d8f55 1097 $this->load('feed.inc.php');
4f18bb11 1098 $feed = new XnetGrpEventFeed();
190efb3a 1099 return $feed->run($page, $user, false);
24bcf50c 1100 }
1101
26ba053e 1102 private function upload_image(PlPage $page, PlUpload $upload)
ff95a302
FB
1103 {
1104 if (@!$_FILES['image']['tmp_name'] && !Env::v('image_url')) {
1105 return true;
1106 }
1107 if (!$upload->upload($_FILES['image']) && !$upload->download(Env::v('image_url'))) {
a7d35093 1108 $page->trigError('Impossible de télécharger l\'image');
ff95a302
FB
1109 return false;
1110 } elseif (!$upload->isType('image')) {
a7d35093 1111 $page->trigError('Le fichier n\'est pas une image valide au format JPEG, GIF ou PNG.');
ff95a302
FB
1112 $upload->rm();
1113 return false;
1114 } elseif (!$upload->resizeImage(200, 300, 100, 100, 32284)) {
a7d35093 1115 $page->trigError('Impossible de retraiter l\'image');
ff95a302
FB
1116 return false;
1117 }
1118 return true;
1119 }
1120
26ba053e 1121 function handler_photo_announce($page, $eid = null) {
ff95a302 1122 if ($eid) {
14bc135e
SJ
1123 $res = XDB::query('SELECT *
1124 FROM group_announces_photo
1125 WHERE eid = {?}', $eid);
ff95a302
FB
1126 if ($res->numRows()) {
1127 $photo = $res->fetchOneAssoc();
3cb500d5 1128 pl_cached_dynamic_content_headers("image/" . $photo['attachmime']);
ff95a302
FB
1129 echo $photo['attach'];
1130 exit;
1131 }
1132 } else {
f3df6d38 1133 $upload = new PlUpload(S::user()->login(), 'xnetannounce');
ff95a302 1134 if ($upload->exists() && $upload->isType('image')) {
3cb500d5 1135 pl_cached_dynamic_content_headers($upload->contentType());
ff95a302
FB
1136 echo $upload->getContents();
1137 exit;
1138 }
1139 }
1140 global $globals;
3cb500d5 1141 pl_cached_dynamic_content_headers("image/png");
ff95a302
FB
1142 echo file_get_contents($globals->spoolroot . '/htdocs/images/logo.png');
1143 exit;
1144 }
1145
26ba053e 1146 function handler_edit_announce($page, $aid = null)
24bcf50c 1147 {
1148 global $globals, $platal;
1490093c 1149 $page->changeTpl('xnetgrp/announce-edit.tpl');
24bcf50c 1150 $page->assign('new', is_null($aid));
1151 $art = array();
1152
ff95a302 1153 if (Post::v('valid') == 'Visualiser' || Post::v('valid') == 'Enregistrer'
2f8677c2 1154 || Post::v('valid') == 'Supprimer l\'image' || Post::v('valid') == 'Pas d\'image') {
e7fdf9dd
VZ
1155 S::assert_xsrf_token();
1156
24bcf50c 1157 if (!is_null($aid)) {
1158 $art['id'] = $aid;
1159 }
1160 $art['titre'] = Post::v('titre');
1161 $art['texte'] = Post::v('texte');
1162 $art['contacts'] = Post::v('contacts');
1163 $art['promo_min'] = Post::i('promo_min');
1164 $art['promo_max'] = Post::i('promo_max');
1165 $art['nom'] = S::v('nom');
1166 $art['prenom'] = S::v('prenom');
1167 $art['promo'] = S::v('promo');
0fab5209 1168 $art['hruid'] = S::user()->login();
79e610a9 1169 $art['uid'] = S::user()->id();
b58e5ac2 1170 $art['expiration'] = Post::v('expiration');
24bcf50c 1171 $art['public'] = Post::has('public');
1172 $art['xorg'] = Post::has('xorg');
1173 $art['nl'] = Post::has('nl');
1174 $art['event'] = Post::v('event');
f3df6d38 1175 $upload = new PlUpload(S::user()->login(), 'xnetannounce');
ff95a302 1176 $this->upload_image($page, $upload);
24bcf50c 1177
1178 $art['contact_html'] = $art['contacts'];
b9a1ba64 1179 if ($art['event']) {
1180 $art['contact_html'] .= "\n{$globals->baseurl}/{$platal->ns}events/sub/{$art['event']}";
24bcf50c 1181 }
2a557a09 1182
1183 if (!$art['public'] &&
d1c97e42 1184 (($art['promo_min'] > $art['promo_max'] && $art['promo_max'] != 0) ||
2a557a09 1185 ($art['promo_min'] != 0 && ($art['promo_min'] <= 1900 || $art['promo_min'] >= 2020)) ||
1186 ($art['promo_max'] != 0 && ($art['promo_max'] <= 1900 || $art['promo_max'] >= 2020))))
1187 {
a7d35093 1188 $page->trigError("L'intervalle de promotions est invalide.");
2a557a09 1189 Post::kill('valid');
1190 }
1970c12b 1191
1192 if (!trim($art['titre']) || !trim($art['texte'])) {
a7d35093 1193 $page->trigError("L'article doit avoir un titre et un contenu.");
1970c12b 1194 Post::kill('valid');
1195 }
ff95a302
FB
1196
1197 if (Post::v('valid') == 'Supprimer l\'image') {
1198 $upload->rm();
1199 Post::kill('valid');
1200 }
2f8677c2
FB
1201 $art['photo'] = $upload->exists() || Post::i('photo');
1202 if (Post::v('valid') == 'Pas d\'image' && !is_null($aid)) {
14bc135e
SJ
1203 XDB::query('DELETE FROM group_announces_photo
1204 WHERE eid = {?}', $aid);
2f8677c2
FB
1205 $upload->rm();
1206 Post::kill('valid');
1207 $art['photo'] = false;
1208 }
24bcf50c 1209 }
1210
1211 if (Post::v('valid') == 'Enregistrer') {
2a557a09 1212 $promo_min = ($art['public'] ? 0 : $art['promo_min']);
1213 $promo_max = ($art['public'] ? 0 : $art['promo_max']);
113f6de8 1214 $flags = new PlFlagSet();
ff95a302 1215 if ($art['public']) {
77e786e1 1216 $flags->addFlag('public');
ff95a302
FB
1217 }
1218 if ($art['photo']) {
77e786e1 1219 $flags->addFlag('photo');
ff95a302 1220 }
24bcf50c 1221 if (is_null($aid)) {
eaf30d86
PH
1222 $fulltext = $art['texte'];
1223 if (!empty($art['contact_html'])) {
1224 $fulltext .= "\n\n'''Contacts :'''\\\\\n" . $art['contact_html'];
e219710a 1225 }
ff95a302 1226 $post = null;/*
e219710a 1227 if ($globals->asso('forum')) {
1228 require_once 'banana/forum.inc.php';
e3a55098 1229 $banana = new ForumsBanana(S::user());
e219710a 1230 $post = $banana->post($globals->asso('forum'), null,
1231 $art['titre'], MiniWiki::wikiToText($fulltext, false, 0, 80));
ff95a302 1232 }*/
14bc135e 1233 XDB::query('INSERT INTO group_announces (uid, asso_id, create_date, titre, texte, contacts,
b58e5ac2 1234 expiration, promo_min, promo_max, flags, post_id)
14bc135e 1235 VALUES ({?}, {?}, NOW(), {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?})',
24bcf50c 1236 S::i('uid'), $globals->asso('id'), $art['titre'], $art['texte'], $art['contact_html'],
b58e5ac2 1237 $art['expiration'], $promo_min, $promo_max, $flags, $post);
8b83a166 1238 $aid = XDB::insertId();
ff95a302
FB
1239 if ($art['photo']) {
1240 list($imgx, $imgy, $imgtype) = $upload->imageInfo();
14bc135e
SJ
1241 XDB::execute('INSERT INTO group_announces_photo
1242 SET eid = {?}, attachmime = {?}, x = {?}, y = {?}, attach = {?}',
ff95a302
FB
1243 $aid, $imgtype, $imgx, $imgy, $upload->getContents());
1244 }
24bcf50c 1245 if ($art['xorg']) {
794feea7 1246 $article = new EvtReq("[{$globals->asso('nom')}] " . $art['titre'], $fulltext,
b58e5ac2 1247 $art['promo_min'], $art['promo_max'], $art['expiration'], "", S::user(),
ff95a302 1248 $upload);
24bcf50c 1249 $article->submit();
a7d35093 1250 $page->trigWarning("L'affichage sur la page d'accueil de Polytechnique.org est en attente de validation.");
ff95a302
FB
1251 } else if ($upload && $upload->exists()) {
1252 $upload->rm();
24bcf50c 1253 }
1254 if ($art['nl']) {
5daf68f6 1255 $article = new NLReq(S::user(), $globals->asso('nom') . " : " .$art['titre'],
2a557a09 1256 $art['texte'], $art['contact_html']);
24bcf50c 1257 $article->submit();
a7d35093 1258 $page->trigWarning("La parution dans la Lettre Mensuelle est en attente de validation.");
24bcf50c 1259 }
1260 } else {
14bc135e 1261 XDB::query('UPDATE group_announces
b58e5ac2 1262 SET titre = {?}, texte = {?}, contacts = {?}, expiration = {?},
14bc135e
SJ
1263 promo_min = {?}, promo_max = {?}, flags = {?}
1264 WHERE id = {?} AND asso_id = {?}',
b58e5ac2 1265 $art['titre'], $art['texte'], $art['contacts'], $art['expiration'],
ff95a302 1266 $promo_min, $promo_max, $flags,
24bcf50c 1267 $art['id'], $globals->asso('id'));
2f8677c2
FB
1268 if ($art['photo'] && $upload->exists()) {
1269 list($imgx, $imgy, $imgtype) = $upload->imageInfo();
00ba8a74
SJ
1270 XDB::execute('INSERT INTO group_announces_photo (eid, attachmime, attach, x, y)
1271 VALUES ({?}, {?}, {?}, {?}, {?})
1272 ON DUPLICATE KEY UPDATE attachmime = VALUES(attachmime), attach = VALUES(attach), x = VALUES(x), y = VALUES(y)',
1273 $aid, $imgtype, $upload->getContents(), $imgx, $imgy);
2f8677c2
FB
1274 $upload->rm();
1275 }
24bcf50c 1276 }
2a557a09 1277 }
1278 if (Post::v('valid') == 'Enregistrer' || Post::v('valid') == 'Annuler') {
24bcf50c 1279 pl_redirect("");
eaf30d86 1280 }
24bcf50c 1281
1282 if (empty($art) && !is_null($aid)) {
14bc135e
SJ
1283 $res = XDB::query("SELECT *, FIND_IN_SET('public', flags) AS public,
1284 FIND_IN_SET('photo', flags) AS photo
1285 FROM group_announces
1286 WHERE asso_id = {?} AND id = {?}",
24bcf50c 1287 $globals->asso('id'), $aid);
1288 if ($res->numRows()) {
1289 $art = $res->fetchOneAssoc();
1290 $art['contact_html'] = $art['contacts'];
1291 } else {
567553ac 1292 $page->kill("Aucun article correspond à l'identifiant indiqué.");
24bcf50c 1293 }
1294 }
1295
24bcf50c 1296 if (is_null($aid)) {
1297 $events = XDB::iterator("SELECT *
eb41eda9 1298 FROM group_events
24bcf50c 1299 WHERE asso_id = {?} AND archive = 0",
1300 $globals->asso('id'));
1301 if ($events->total()) {
1302 $page->assign('events', $events);
1303 }
eaf30d86 1304 }
24bcf50c 1305
72b2f8bb 1306 $art['contact_html'] = @MiniWiki::WikiToHTML($art['contact_html']);
24bcf50c 1307 $page->assign('art', $art);
ff95a302 1308 $page->assign_by_ref('upload', $upload);
24bcf50c 1309 }
1310
26ba053e 1311 function handler_admin_announce($page)
24bcf50c 1312 {
1313 global $globals;
1490093c 1314 $page->changeTpl('xnetgrp/announce-admin.tpl');
24bcf50c 1315
1316 if (Env::has('del')) {
e7fdf9dd 1317 S::assert_xsrf_token();
14bc135e
SJ
1318 XDB::execute('DELETE FROM group_announces
1319 WHERE id = {?} AND asso_id = {?}',
24bcf50c 1320 Env::i('del'), $globals->asso('id'));
1321 }
b58e5ac2 1322 $res = XDB::iterator('SELECT id, titre, expiration, expiration < CURRENT_DATE() AS perime
14bc135e
SJ
1323 FROM group_announces
1324 WHERE asso_id = {?}
b58e5ac2 1325 ORDER BY expiration DESC',
24bcf50c 1326 $globals->asso('id'));
1327 $page->assign('articles', $res);
1328 }
ae7bb180 1329}
1330
a7de4ef7 1331// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
ae7bb180 1332?>