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