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