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