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