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