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