Deletes lost addresses, medals and phones.
[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
SJ
822 $user = S::user();
823 $uid = S::user()->id();
824 if (empty($uid)) {
30032578 825 return PL_NOT_FOUND;
ef7c8560 826 }
30032578 827 $page->assign('self', true);
035de9c1 828 $page->assign('user', $uid);
ef7c8560 829
30032578 830 if (!Post::has('confirm')) {
831 return;
e7fdf9dd
VZ
832 } else {
833 S::assert_xsrf_token();
30032578 834 }
835
836 if ($this->unsubscribe($user)) {
a7d35093 837 $page->trigSuccess('Vous avez été désinscrit du groupe avec succès.');
30032578 838 } else {
9ec2213d 839 $page->trigWarning('Vous avez été désinscrit du groupe, mais des erreurs se sont produites lors des désinscriptions des alias et des listes de diffusion.');
30032578 840 }
c1863ee9 841 $page->assign('is_member', is_member(true));
30032578 842 }
843
26ba053e 844 function handler_admin_member_del($page, $user = null)
30032578 845 {
1490093c 846 $page->changeTpl('xnetgrp/membres-del.tpl');
45dcd6dd 847 $user = User::getSilent($user);
30032578 848 if (empty($user)) {
849 return PL_NOT_FOUND;
850 }
851 $page->assign('user', $user);
852
853 if (!Post::has('confirm')) {
854 return;
e7fdf9dd
VZ
855 } else {
856 S::assert_xsrf_token();
30032578 857 }
858
859 if ($this->unsubscribe($user)) {
e46cf8c4 860 $page->trigSuccess("{$user->fullName()} a été désinscrit du groupe&nbsp;!");
30032578 861 } else {
e46cf8c4 862 $page->trigWarning("{$user->fullName()} a été désinscrit du groupe, mais des erreurs subsistent&nbsp;!");
30032578 863 }
ef7c8560 864 }
865
26ba053e 866 private function changeLogin(PlPage $page, PlUser $user, MMList $mmlist, $login)
631565e1 867 {
33a4f3f9 868 // Search the user's uid.
bb88d138
PC
869 $xuser = User::getSilent($login);
870 if (!$xuser) {
61a7d279
SJ
871 $accounts = User::getPendingAccounts($login);
872 if (!$accounts) {
873 $page->trigError("L'identifiant $login ne correspond à aucun X.");
631565e1 874 return false;
61a7d279
SJ
875 } else if (count($accounts) > 1) {
876 $page->trigError("L'identifiant $login correspond à plusieurs camarades.");
631565e1
FB
877 return false;
878 }
bb88d138 879 $xuser = User::getSilent($accounts[0]['uid']);
631565e1
FB
880 }
881
bb88d138
PC
882 if (!$xuser) {
883 return false;
631565e1
FB
884 }
885
33a4f3f9
SJ
886 if ($user->mergeIn($xuser)) {
887 return $xuser->login();
888 }
889 return $user->login();
631565e1
FB
890 }
891
26ba053e 892 function handler_admin_member($page, $user)
ef7c8560 893 {
894 global $globals;
895
1490093c 896 $page->changeTpl('xnetgrp/membres-edit.tpl');
ef7c8560 897
45dcd6dd 898 $user = User::getSilent($user);
ef7c8560 899 if (empty($user)) {
900 return PL_NOT_FOUND;
901 }
902
dc81a0b2 903 $mmlist = new MMList(S::user(), $globals->asso('mail_domain'));
ef7c8560 904
905 if (Post::has('change')) {
e7fdf9dd
VZ
906 S::assert_xsrf_token();
907
631565e1 908 // Convert user status to X
bb88d138 909 if (!Post::blank('login_X')) {
45dcd6dd 910 $forlife = $this->changeLogin($page, $user, $mmlist, Post::t('login_X'));
631565e1
FB
911 if ($forlife) {
912 pl_redirect('member/' . $forlife);
913 }
914 }
915
916 // Update user info
bb88d138
PC
917 $email_changed = (!$user->profile() && strtolower($user->forlifeEmail()) != strtolower(Post::v('email')));
918 $from_email = $user->forlifeEmail();
b42417f4 919 if ($user->type == 'virtual' || $user->type == 'xnet') {
bb88d138 920 XDB::query('UPDATE accounts
33a4f3f9
SJ
921 SET full_name = {?}, directory_name = {?}, display_name = {?},
922 sex = {?}, email = {?}, type = {?}
bb88d138 923 WHERE uid = {?}',
33a4f3f9
SJ
924 Post::t('full_name'), Post::t('directory_name'), Post::t('display_name'),
925 (Post::t('sex') == 'male') ? 'male' : 'female', Post::t('email'),
926 (Post::t('type') == 'xnet') ? 'xnet' : 'virtual', $user->id());
927 } else if (!$user->perms) {
928 XDB::query('UPDATE accounts
929 SET email = {?}
930 WHERE uid = {?}',
931 Post::t('email'), $user->id());
932 }
933 if (XDB::affectedRows()) {
934 $page->trigSuccess('Données de l\'utilisateur mise à jour.');
ef7c8560 935 }
936
bb88d138
PC
937 // Update group params for user
938 $perms = Post::v('group_perms');
a6761ca9 939 $comm = Post::t('comm');
33fcb12c
SJ
940 $position = (Post::t('group_position') == '') ? null : Post::v('group_position');
941 if ($user->group_perms != $perms || $user->group_comm != $comm || $user->group_position != $position) {
eb41eda9 942 XDB::query('UPDATE group_members
33fcb12c 943 SET perms = {?}, comm = {?}, position = {?}
00112b2e 944 WHERE uid = {?} AND asso_id = {?}',
33fcb12c 945 ($perms == 'admin') ? 'admin' : 'membre', $comm, $position,
bb88d138 946 $user->id(), $globals->asso('id'));
33a4f3f9
SJ
947 if (XDB::affectedRows()) {
948 if ($perms != $user->group_perms) {
949 $page->trigSuccess('Permissions modifiées&nbsp;!');
950 }
951 if ($comm != $user->group_comm) {
952 $page->trigSuccess('Commentaire mis à jour.');
953 }
58149849
SJ
954 if ($position != $user->group_position) {
955 $page->trigSuccess('Poste mis à jour.');
956 }
54b24ba2 957 }
ef7c8560 958 }
959
bb88d138
PC
960 // Gets user info again as they might have change
961 $user = User::getSilent($user->id());
962
631565e1 963 // Update ML subscriptions
5e2307dc 964 foreach (Env::v('ml1', array()) as $ml => $state) {
ef7c8560 965 $ask = empty($_REQUEST['ml2'][$ml]) ? 0 : 2;
c4d57bd8 966 if ($ask == $state) {
06db561e 967 if ($state && $email_changed) {
bb88d138
PC
968 $mmlist->replace_email($ml, $from_email, $user->forlifeEmail());
969 $page->trigSuccess("L'abonnement de {$user->fullName()} à $ml@ a été mis à jour.");
c4d57bd8 970 }
971 continue;
972 }
ef7c8560 973 if ($state == '1') {
bb88d138 974 $page->trigWarning("{$user->fullName()} a "
ef7c8560 975 ."actuellement une demande d'inscription en "
976 ."cours sur <strong>$ml@</strong> !!!");
977 } elseif ($ask) {
bb88d138
PC
978 $mmlist->mass_subscribe($ml, Array($user->forlifeEmail()));
979 $page->trigSuccess("{$user->fullName()} a été abonné à $ml@.");
ef7c8560 980 } else {
c4d57bd8 981 if ($email_changed) {
982 $mmlist->mass_unsubscribe($ml, Array($from_email));
983 } else {
bb88d138 984 $mmlist->mass_unsubscribe($ml, Array($user->forlifeEmail()));
c4d57bd8 985 }
bb88d138 986 $page->trigSuccess("{$user->fullName()} a été désabonné de $ml@.");
ef7c8560 987 }
988 }
989
631565e1 990 // Change subscriptioin to aliases
5e2307dc 991 foreach (Env::v('ml3', array()) as $ml => $state) {
831aa467 992 require_once 'emails.inc.php';
ef7c8560 993 $ask = !empty($_REQUEST['ml4'][$ml]);
bb88d138
PC
994 if($state == $ask) {
995 if ($state && $email_changed) {
831aa467 996 update_list_alias($user, $from_email, $ml, $globals->asso('mail_domain'));
bb88d138
PC
997 $page->trigSuccess("L'abonnement de {$user->fullName()} à $ml a été mis à jour.");
998 }
999 } else if($ask) {
831aa467 1000 add_to_list_alias($user, $ml, $globals->asso('mail_domain'));
bb88d138 1001 $page->trigSuccess("{$user->fullName()} a été abonné à $ml.");
ef7c8560 1002 } else {
831aa467 1003 delete_from_list_alias($user, $ml, $globals->asso('mail_domain'));
bb88d138 1004 $page->trigSuccess("{$user->fullName()} a été désabonné de $ml.");
ef7c8560 1005 }
1006 }
1007 }
1008
33fcb12c
SJ
1009 $res = XDB::rawFetchAllAssoc('SHOW COLUMNS FROM group_members LIKE \'position\'');
1010 $positions = str_replace(array('enum(', ')', '\''), '', $res[0]['Type']);
1011
ef7c8560 1012 $page->assign('user', $user);
a6761ca9 1013 $page->assign('listes', $mmlist->get_lists($user->forlifeEmail()));
06a548e5 1014 $page->assign('alias', $user->emailGroupAliases($globals->asso('mail_domain')));
33fcb12c 1015 $page->assign('positions', explode(',', $positions));
ef7c8560 1016 }
24bcf50c 1017
82af3fc3 1018 function handler_rss(PlPage $page, PlUser $user)
24bcf50c 1019 {
1020 global $globals;
24bcf50c 1021 $page->assign('asso', $globals->asso());
4f18bb11 1022
460d8f55 1023 $this->load('feed.inc.php');
4f18bb11 1024 $feed = new XnetGrpEventFeed();
190efb3a 1025 return $feed->run($page, $user, false);
24bcf50c 1026 }
1027
26ba053e 1028 private function upload_image(PlPage $page, PlUpload $upload)
ff95a302
FB
1029 {
1030 if (@!$_FILES['image']['tmp_name'] && !Env::v('image_url')) {
1031 return true;
1032 }
1033 if (!$upload->upload($_FILES['image']) && !$upload->download(Env::v('image_url'))) {
a7d35093 1034 $page->trigError('Impossible de télécharger l\'image');
ff95a302
FB
1035 return false;
1036 } elseif (!$upload->isType('image')) {
a7d35093 1037 $page->trigError('Le fichier n\'est pas une image valide au format JPEG, GIF ou PNG.');
ff95a302
FB
1038 $upload->rm();
1039 return false;
1040 } elseif (!$upload->resizeImage(200, 300, 100, 100, 32284)) {
a7d35093 1041 $page->trigError('Impossible de retraiter l\'image');
ff95a302
FB
1042 return false;
1043 }
1044 return true;
1045 }
1046
26ba053e 1047 function handler_photo_announce($page, $eid = null) {
ff95a302 1048 if ($eid) {
14bc135e
SJ
1049 $res = XDB::query('SELECT *
1050 FROM group_announces_photo
1051 WHERE eid = {?}', $eid);
ff95a302
FB
1052 if ($res->numRows()) {
1053 $photo = $res->fetchOneAssoc();
3cb500d5 1054 pl_cached_dynamic_content_headers("image/" . $photo['attachmime']);
ff95a302
FB
1055 echo $photo['attach'];
1056 exit;
1057 }
1058 } else {
f3df6d38 1059 $upload = new PlUpload(S::user()->login(), 'xnetannounce');
ff95a302 1060 if ($upload->exists() && $upload->isType('image')) {
3cb500d5 1061 pl_cached_dynamic_content_headers($upload->contentType());
ff95a302
FB
1062 echo $upload->getContents();
1063 exit;
1064 }
1065 }
1066 global $globals;
3cb500d5 1067 pl_cached_dynamic_content_headers("image/png");
ff95a302
FB
1068 echo file_get_contents($globals->spoolroot . '/htdocs/images/logo.png');
1069 exit;
1070 }
1071
26ba053e 1072 function handler_edit_announce($page, $aid = null)
24bcf50c 1073 {
1074 global $globals, $platal;
1490093c 1075 $page->changeTpl('xnetgrp/announce-edit.tpl');
24bcf50c 1076 $page->assign('new', is_null($aid));
1077 $art = array();
1078
ff95a302 1079 if (Post::v('valid') == 'Visualiser' || Post::v('valid') == 'Enregistrer'
2f8677c2 1080 || Post::v('valid') == 'Supprimer l\'image' || Post::v('valid') == 'Pas d\'image') {
e7fdf9dd
VZ
1081 S::assert_xsrf_token();
1082
24bcf50c 1083 if (!is_null($aid)) {
1084 $art['id'] = $aid;
1085 }
1086 $art['titre'] = Post::v('titre');
1087 $art['texte'] = Post::v('texte');
1088 $art['contacts'] = Post::v('contacts');
1089 $art['promo_min'] = Post::i('promo_min');
1090 $art['promo_max'] = Post::i('promo_max');
1091 $art['nom'] = S::v('nom');
1092 $art['prenom'] = S::v('prenom');
1093 $art['promo'] = S::v('promo');
0fab5209 1094 $art['hruid'] = S::user()->login();
79e610a9 1095 $art['uid'] = S::user()->id();
b58e5ac2 1096 $art['expiration'] = Post::v('expiration');
24bcf50c 1097 $art['public'] = Post::has('public');
1098 $art['xorg'] = Post::has('xorg');
1099 $art['nl'] = Post::has('nl');
1100 $art['event'] = Post::v('event');
f3df6d38 1101 $upload = new PlUpload(S::user()->login(), 'xnetannounce');
ff95a302 1102 $this->upload_image($page, $upload);
24bcf50c 1103
1104 $art['contact_html'] = $art['contacts'];
b9a1ba64 1105 if ($art['event']) {
1106 $art['contact_html'] .= "\n{$globals->baseurl}/{$platal->ns}events/sub/{$art['event']}";
24bcf50c 1107 }
2a557a09 1108
1109 if (!$art['public'] &&
d1c97e42 1110 (($art['promo_min'] > $art['promo_max'] && $art['promo_max'] != 0) ||
2a557a09 1111 ($art['promo_min'] != 0 && ($art['promo_min'] <= 1900 || $art['promo_min'] >= 2020)) ||
1112 ($art['promo_max'] != 0 && ($art['promo_max'] <= 1900 || $art['promo_max'] >= 2020))))
1113 {
a7d35093 1114 $page->trigError("L'intervalle de promotions est invalide.");
2a557a09 1115 Post::kill('valid');
1116 }
1970c12b 1117
1118 if (!trim($art['titre']) || !trim($art['texte'])) {
a7d35093 1119 $page->trigError("L'article doit avoir un titre et un contenu.");
1970c12b 1120 Post::kill('valid');
1121 }
ff95a302
FB
1122
1123 if (Post::v('valid') == 'Supprimer l\'image') {
1124 $upload->rm();
1125 Post::kill('valid');
1126 }
2f8677c2
FB
1127 $art['photo'] = $upload->exists() || Post::i('photo');
1128 if (Post::v('valid') == 'Pas d\'image' && !is_null($aid)) {
14bc135e
SJ
1129 XDB::query('DELETE FROM group_announces_photo
1130 WHERE eid = {?}', $aid);
2f8677c2
FB
1131 $upload->rm();
1132 Post::kill('valid');
1133 $art['photo'] = false;
1134 }
24bcf50c 1135 }
1136
1137 if (Post::v('valid') == 'Enregistrer') {
2a557a09 1138 $promo_min = ($art['public'] ? 0 : $art['promo_min']);
1139 $promo_max = ($art['public'] ? 0 : $art['promo_max']);
113f6de8 1140 $flags = new PlFlagSet();
ff95a302 1141 if ($art['public']) {
77e786e1 1142 $flags->addFlag('public');
ff95a302
FB
1143 }
1144 if ($art['photo']) {
77e786e1 1145 $flags->addFlag('photo');
ff95a302 1146 }
24bcf50c 1147 if (is_null($aid)) {
eaf30d86
PH
1148 $fulltext = $art['texte'];
1149 if (!empty($art['contact_html'])) {
1150 $fulltext .= "\n\n'''Contacts :'''\\\\\n" . $art['contact_html'];
e219710a 1151 }
ff95a302 1152 $post = null;/*
e219710a 1153 if ($globals->asso('forum')) {
1154 require_once 'banana/forum.inc.php';
e3a55098 1155 $banana = new ForumsBanana(S::user());
e219710a 1156 $post = $banana->post($globals->asso('forum'), null,
1157 $art['titre'], MiniWiki::wikiToText($fulltext, false, 0, 80));
ff95a302 1158 }*/
14bc135e 1159 XDB::query('INSERT INTO group_announces (uid, asso_id, create_date, titre, texte, contacts,
b58e5ac2 1160 expiration, promo_min, promo_max, flags, post_id)
14bc135e 1161 VALUES ({?}, {?}, NOW(), {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?})',
24bcf50c 1162 S::i('uid'), $globals->asso('id'), $art['titre'], $art['texte'], $art['contact_html'],
b58e5ac2 1163 $art['expiration'], $promo_min, $promo_max, $flags, $post);
8b83a166 1164 $aid = XDB::insertId();
ff95a302
FB
1165 if ($art['photo']) {
1166 list($imgx, $imgy, $imgtype) = $upload->imageInfo();
14bc135e
SJ
1167 XDB::execute('INSERT INTO group_announces_photo
1168 SET eid = {?}, attachmime = {?}, x = {?}, y = {?}, attach = {?}',
ff95a302
FB
1169 $aid, $imgtype, $imgx, $imgy, $upload->getContents());
1170 }
24bcf50c 1171 if ($art['xorg']) {
794feea7 1172 $article = new EvtReq("[{$globals->asso('nom')}] " . $art['titre'], $fulltext,
b58e5ac2 1173 $art['promo_min'], $art['promo_max'], $art['expiration'], "", S::user(),
ff95a302 1174 $upload);
24bcf50c 1175 $article->submit();
a7d35093 1176 $page->trigWarning("L'affichage sur la page d'accueil de Polytechnique.org est en attente de validation.");
ff95a302
FB
1177 } else if ($upload && $upload->exists()) {
1178 $upload->rm();
24bcf50c 1179 }
1180 if ($art['nl']) {
5daf68f6 1181 $article = new NLReq(S::user(), $globals->asso('nom') . " : " .$art['titre'],
2a557a09 1182 $art['texte'], $art['contact_html']);
24bcf50c 1183 $article->submit();
a7d35093 1184 $page->trigWarning("La parution dans la Lettre Mensuelle est en attente de validation.");
24bcf50c 1185 }
1186 } else {
14bc135e 1187 XDB::query('UPDATE group_announces
b58e5ac2 1188 SET titre = {?}, texte = {?}, contacts = {?}, expiration = {?},
14bc135e
SJ
1189 promo_min = {?}, promo_max = {?}, flags = {?}
1190 WHERE id = {?} AND asso_id = {?}',
b58e5ac2 1191 $art['titre'], $art['texte'], $art['contacts'], $art['expiration'],
ff95a302 1192 $promo_min, $promo_max, $flags,
24bcf50c 1193 $art['id'], $globals->asso('id'));
2f8677c2
FB
1194 if ($art['photo'] && $upload->exists()) {
1195 list($imgx, $imgy, $imgtype) = $upload->imageInfo();
00ba8a74
SJ
1196 XDB::execute('INSERT INTO group_announces_photo (eid, attachmime, attach, x, y)
1197 VALUES ({?}, {?}, {?}, {?}, {?})
1198 ON DUPLICATE KEY UPDATE attachmime = VALUES(attachmime), attach = VALUES(attach), x = VALUES(x), y = VALUES(y)',
1199 $aid, $imgtype, $upload->getContents(), $imgx, $imgy);
2f8677c2
FB
1200 $upload->rm();
1201 }
24bcf50c 1202 }
2a557a09 1203 }
1204 if (Post::v('valid') == 'Enregistrer' || Post::v('valid') == 'Annuler') {
24bcf50c 1205 pl_redirect("");
eaf30d86 1206 }
24bcf50c 1207
1208 if (empty($art) && !is_null($aid)) {
14bc135e
SJ
1209 $res = XDB::query("SELECT *, FIND_IN_SET('public', flags) AS public,
1210 FIND_IN_SET('photo', flags) AS photo
1211 FROM group_announces
1212 WHERE asso_id = {?} AND id = {?}",
24bcf50c 1213 $globals->asso('id'), $aid);
1214 if ($res->numRows()) {
1215 $art = $res->fetchOneAssoc();
1216 $art['contact_html'] = $art['contacts'];
1217 } else {
567553ac 1218 $page->kill("Aucun article correspond à l'identifiant indiqué.");
24bcf50c 1219 }
1220 }
1221
24bcf50c 1222 if (is_null($aid)) {
1223 $events = XDB::iterator("SELECT *
eb41eda9 1224 FROM group_events
24bcf50c 1225 WHERE asso_id = {?} AND archive = 0",
1226 $globals->asso('id'));
1227 if ($events->total()) {
1228 $page->assign('events', $events);
1229 }
eaf30d86 1230 }
24bcf50c 1231
72b2f8bb 1232 $art['contact_html'] = @MiniWiki::WikiToHTML($art['contact_html']);
24bcf50c 1233 $page->assign('art', $art);
ff95a302 1234 $page->assign_by_ref('upload', $upload);
24bcf50c 1235 }
1236
26ba053e 1237 function handler_admin_announce($page)
24bcf50c 1238 {
1239 global $globals;
1490093c 1240 $page->changeTpl('xnetgrp/announce-admin.tpl');
24bcf50c 1241
1242 if (Env::has('del')) {
e7fdf9dd 1243 S::assert_xsrf_token();
14bc135e
SJ
1244 XDB::execute('DELETE FROM group_announces
1245 WHERE id = {?} AND asso_id = {?}',
24bcf50c 1246 Env::i('del'), $globals->asso('id'));
1247 }
b58e5ac2 1248 $res = XDB::iterator('SELECT id, titre, expiration, expiration < CURRENT_DATE() AS perime
14bc135e
SJ
1249 FROM group_announces
1250 WHERE asso_id = {?}
b58e5ac2 1251 ORDER BY expiration DESC',
24bcf50c 1252 $globals->asso('id'));
1253 $page->assign('articles', $res);
1254 }
ae7bb180 1255}
1256
a7de4ef7 1257// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
ae7bb180 1258?>