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