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