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