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