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