Fix choice of user bestalias
[platal.git] / modules / events.php
CommitLineData
74e0093f 1<?php
2/***************************************************************************
5e1513f6 3 * Copyright (C) 2003-2011 Polytechnique.org *
74e0093f 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
22class EventsModule extends PLModule
23{
24 function handlers()
25 {
26 return array(
eb5a266d 27 'events' => $this->make_hook('ev', AUTH_COOKIE),
eb5a266d
SJ
28 'events/preview' => $this->make_hook('preview', AUTH_PUBLIC, 'user', NO_AUTH),
29 'events/photo' => $this->make_hook('photo', AUTH_PUBLIC),
30 'events/submit' => $this->make_hook('ev_submit', AUTH_MDP),
31 'admin/events' => $this->make_hook('admin_events', AUTH_MDP, 'admin'),
190efb3a 32 'rss' => $this->make_token_hook('rss', AUTH_COOKIE),
b829e258 33
eb5a266d
SJ
34 'ajax/tips' => $this->make_hook('tips', AUTH_COOKIE, 'user', NO_AUTH),
35 'admin/tips' => $this->make_hook('admin_tips', AUTH_MDP, 'admin'),
74e0093f 36 );
37 }
38
02838718 39 private function get_tips($exclude = null)
dc767839 40 {
6ce5dee4 41 global $globals;
42 // Add a new special tip when changing plat/al version
2ab3486b 43 if ($globals->version != S::user()->last_version && is_null($exclude)) {
bc910323 44 XDB::execute('UPDATE accounts
6ce5dee4 45 SET last_version = {?}
bc910323 46 WHERE uid = {?}',
6ce5dee4 47 $globals->version, S::i('uid'));
48 return array('id' => 0,
49 'titre' => 'Bienvenue sur la nouvelle version du site !',
50 'text' => 'Le site a été mis à jour depuis ta dernière visite vers la version ' . $globals->version
44837f02
FB
51 . '.<br /> Nous t\'invitons à <a href="review">faire un tour d\'horizon des '
52 . 'nouveautés</a>.<br /><br />'
53 . 'Tu peux également retrouver ces informations sur <a href="banana/xorg.m4x.innovation">'
661e78f7 54 . 'les forums</a>, ou sur <a href="changelog">la liste exhaustive des modifications</a>.',
6ce5dee4 55 'priorite' => 255,
56 'promo_min' => 0,
57 'promo_max' => 0,
58 'state' => 'active',
59 'special' => true);
908db125 60 }
6ce5dee4 61
73279cf8 62 $exclude = is_null($exclude) ? '' : ' AND id != ' . intval($exclude) . ' ';
dc767839 63 $priority = rand(0, 510);
64 do {
65 $priority = (int)($priority/2);
66 $res = XDB::query("SELECT *
da431170 67 FROM reminder_tips
b58e5ac2 68 WHERE (expiration = '0000-00-00' OR expiration > CURDATE())
dc767839 69 AND (promo_min = 0 OR promo_min <= {?})
70 AND (promo_max = 0 OR promo_max >= {?})
da431170 71 AND (priority >= {?})
5d617d8d 72 AND (state = 'active')
dc767839 73 $exclude
74 ORDER BY RAND()
75 LIMIT 1",
76 S::i('promo'), S::i('promo'), $priority);
77 } while ($priority && !$res->numRows());
78 if (!$res->numRows()) {
79 return null;
eaf30d86 80 }
dc767839 81 return $res->fetchOneAssoc();
82 }
83
26ba053e 84 private function upload_image(PlPage $page, PlUpload $upload)
02838718 85 {
86 if (@!$_FILES['image']['tmp_name'] && !Env::v('image_url')) {
87 return true;
88 }
89 if (!$upload->upload($_FILES['image']) && !$upload->download(Env::v('image_url'))) {
a7d35093 90 $page->trigError('Impossible de télécharger l\'image');
02838718 91 return false;
92 } elseif (!$upload->isType('image')) {
a7d35093 93 $page->trigError('Le fichier n\'est pas une image valide au format JPEG, GIF ou PNG.');
02838718 94 $upload->rm();
95 return false;
96 } elseif (!$upload->resizeImage(200, 300, 100, 100, 32284)) {
a7d35093 97 $page->trigError('Impossible de retraiter l\'image');
02838718 98 return false;
99 }
100 return true;
101 }
102
26ba053e 103 function handler_ev($page, $action = 'list', $eid = null, $pound = null)
1a828cd4 104 {
8b1f8e12 105 $page->changeTpl('events/index.tpl');
dc767839 106
bc910323 107 $user = S::user();
1a828cd4 108
52858d0e 109 /** XXX: Tips and reminder only for user with 'email' permission.
1f64f1a7
FB
110 * We can do better in the future by storing a userfilter
111 * with the tip/reminder.
52858d0e
FB
112 */
113 if ($user->checkPerms(User::PERM_MAIL)) {
114 $page->assign('tips', $this->get_tips());
115
1f64f1a7
FB
116 }
117
118 // Adds a reminder onebox to the page.
119 require_once 'reminder.inc.php';
120 if (($reminder = Reminder::GetCandidateReminder($user))) {
121 $reminder->Prepare($page);
122 }
52858d0e 123
1f64f1a7
FB
124 // Wishes "Happy birthday" when required
125 $profile = $user->profile();
126 if (!is_null($profile)) {
127 if ($profile->next_birthday == date('Y-m-d')) {
128 $birthyear = (int)date('Y', strtotime($profile->birthdate));
129 $curyear = (int)date('Y');
130 $page->assign('birthday', $curyear - $birthyear);
bc910323 131 }
1a828cd4 132 }
133
c52838d6 134 // Direct link to the RSS feed, when available.
84fc72ff 135 if (S::hasAuthToken()) {
162370e7 136 $page->setRssLink('Polytechnique.org :: News',
2ab3486b 137 '/rss/' . S::v('hruid') . '/' . S::user()->token . '/rss.xml');
1a828cd4 138 }
139
c52838d6 140 // Hide the read event, and reload the page to get to the next event.
4e61caea 141 if ($action == 'read' && $eid) {
12500b65 142 XDB::execute('DELETE ev.*
06f4daf9
FB
143 FROM announce_read AS ev
144 INNER JOIN announces AS e ON e.id = ev.evt_id
b58e5ac2 145 WHERE expiration < NOW()');
00ba8a74
SJ
146 XDB::execute('INSERT IGNORE INTO announce_read (evt_id, uid)
147 VALUES ({?}, {?})',
148 $eid, S::v('uid'));
096f0dca 149 pl_redirect('events#'.$pound);
1a828cd4 150 }
151
c52838d6 152 // Unhide the requested event, and reload the page to display it.
4e61caea 153 if ($action == 'unread' && $eid) {
06f4daf9 154 XDB::execute('DELETE FROM announce_read
f280f651 155 WHERE evt_id = {?} AND uid = {?}',
4e61caea 156 $eid, S::v('uid'));
096f0dca 157 pl_redirect('events#newsid'.$eid);
1a828cd4 158 }
159
c52838d6 160 // Fetch the events to display, along with their metadata.
d0e45ced 161 $array = array();
f280f651 162 $it = XDB::iterator("SELECT e.id, e.titre, e.texte, e.post_id, e.uid,
c321aa99
FB
163 p.x, p.y, p.attach IS NOT NULL AS img, FIND_IN_SET('wiki', e.flags) AS wiki,
164 FIND_IN_SET('important', e.flags) AS important,
165 e.creation_date > DATE_SUB(CURDATE(), INTERVAL 2 DAY) AS news,
b58e5ac2 166 e.expiration < DATE_ADD(CURDATE(), INTERVAL 2 DAY) AS end,
f280f651 167 ev.uid IS NULL AS nonlu, e.promo_min, e.promo_max
06f4daf9
FB
168 FROM announces AS e
169 LEFT JOIN announce_photos AS p ON (e.id = p.eid)
f280f651 170 LEFT JOIN announce_read AS ev ON (e.id = ev.evt_id AND ev.uid = {?})
b58e5ac2
SJ
171 WHERE FIND_IN_SET('valide', e.flags) AND expiration >= NOW()
172 ORDER BY important DESC, news DESC, end DESC, e.expiration, e.creation_date DESC",
a087cc8d 173 S::i('uid'));
c321aa99 174 $cats = array('important', 'news', 'end', 'body');
a0b45322
FB
175
176 $this->load('feed.inc.php');
177 $user = S::user();
178 $body = EventFeed::nextEvent($it, $user);
c321aa99
FB
179 foreach ($cats as $cat) {
180 $data = array();
181 if (!$body) {
182 continue;
183 }
184 do {
185 if ($cat == 'body' || $body[$cat]) {
186 $data[] = $body;
187 } else {
188 break;
189 }
b774ddab 190 $body = EventFeed::nextEvent($it, $user);
c321aa99
FB
191 } while ($body);
192 if (!empty($data)) {
193 $array[$cat] = $data;
194 }
195 }
a087cc8d 196
d0e45ced 197 $page->assign_by_ref('events', $array);
1a828cd4 198 }
199
26ba053e 200 function handler_photo($page, $eid = null, $valid = null)
02838718 201 {
202 if ($eid && $eid != 'valid') {
06f4daf9 203 $res = XDB::query("SELECT * FROM announce_photos WHERE eid = {?}", $eid);
02838718 204 if ($res->numRows()) {
205 $photo = $res->fetchOneAssoc();
3cb500d5 206 pl_cached_dynamic_content_headers("image/" . $photo['attachmime']);
02838718 207 echo $photo['attach'];
208 exit;
209 }
210 } elseif ($eid == 'valid') {
02838718 211 $valid = Validate::get_request_by_id($valid);
212 if ($valid && $valid->img) {
3cb500d5 213 pl_cached_dynamic_content_headers("image/" . $valid->imgtype);
02838718 214 echo $valid->img;
215 exit;
216 }
217 } else {
f3df6d38 218 $upload = new PlUpload(S::user()->login(), 'event');
02838718 219 if ($upload->exists() && $upload->isType('image')) {
3cb500d5 220 pl_cached_dynamic_content_headers($upload->contentType());
02838718 221 echo $upload->getContents();
222 exit;
223 }
224 }
225 global $globals;
3cb500d5 226 pl_cached_dynamic_content_headers("image/png");
02838718 227 echo file_get_contents($globals->spoolroot . '/htdocs/images/logo.png');
228 exit;
229 }
230
f1449415 231 function handler_rss(PlPage $page, PlUser $user)
eaf30d86 232 {
460d8f55 233 $this->load('feed.inc.php');
61fa44d9 234 $feed = new EventFeed();
190efb3a 235 return $feed->run($page, $user);
db3bd146 236 }
237
26ba053e 238 function handler_preview($page)
db3bd146 239 {
db3bd146 240 $page->changeTpl('events/preview.tpl', NO_SKIN);
241 $texte = Get::v('texte');
242 if (!is_utf8($texte)) {
243 $texte = utf8_encode($texte);
244 }
db3bd146 245 $titre = Get::v('titre');
246 if (!is_utf8($titre)) {
247 $titre = utf8_encode($titre);
248 }
794feea7 249 $page->assign('texte', $texte);
db3bd146 250 $page->assign('titre', $titre);
3cb500d5 251 pl_content_headers("text/html");
db3bd146 252 }
d1c97e42 253
26ba053e 254 function handler_ev_submit($page)
74e0093f 255 {
8b1f8e12 256 $page->changeTpl('events/submit.tpl');
eaf30d86 257
8f201b69
FB
258 $wp = new PlWikiPage('Xorg.Annonce');
259 $wp->buildCache();
74e0093f 260
5e2307dc 261 $titre = Post::v('titre');
262 $texte = Post::v('texte');
263 $promo_min = Post::i('promo_min');
264 $promo_max = Post::i('promo_max');
b58e5ac2 265 $expiration = Post::i('expiration');
5e2307dc 266 $valid_mesg = Post::v('valid_mesg');
267 $action = Post::v('action');
f3df6d38 268 $upload = new PlUpload(S::user()->login(), 'event');
02838718 269 $this->upload_image($page, $upload);
74e0093f 270
d1c97e42 271 if (($promo_min > $promo_max && $promo_max != 0)||
2086ab7f 272 ($promo_min != 0 && ($promo_min <= 1900 || $promo_min >= 2020)) ||
273 ($promo_max != 0 && ($promo_max <= 1900 || $promo_max >= 2020)))
274 {
a7d35093 275 $page->trigError("L'intervalle de promotions n'est pas valide");
2086ab7f 276 $action = null;
277 }
278
74e0093f 279 $page->assign('titre', $titre);
280 $page->assign('texte', $texte);
281 $page->assign('promo_min', $promo_min);
282 $page->assign('promo_max', $promo_max);
b58e5ac2 283 $page->assign('expiration', $expiration);
74e0093f 284 $page->assign('valid_mesg', $valid_mesg);
285 $page->assign('action', strtolower($action));
02838718 286 $page->assign_by_ref('upload', $upload);
74e0093f 287
02838718 288 if ($action == 'Supprimer l\'image') {
289 $upload->rm();
290 $page->assign('action', false);
291 } elseif ($action && (!trim($texte) || !trim($titre))) {
a7d35093 292 $page->trigError("L'article doit avoir un titre et un contenu");
db3bd146 293 } elseif ($action) {
7b642046
VZ
294 S::assert_xsrf_token();
295
74e0093f 296 $evtreq = new EvtReq($titre, $texte, $promo_min, $promo_max,
b58e5ac2 297 $expiration, $valid_mesg, S::user(), $upload);
74e0093f 298 $evtreq->submit();
299 $page->assign('ok', true);
02838718 300 } elseif (!Env::v('preview')) {
301 $upload->rm();
74e0093f 302 }
74e0093f 303 }
b829e258 304
26ba053e 305 function handler_tips($page, $tips = null)
dc767839 306 {
3cb500d5 307 pl_content_headers("text/html");
dc767839 308 $page->changeTpl('include/tips.tpl', NO_SKIN);
309 $page->assign('tips', $this->get_tips($tips));
310 }
311
26ba053e 312 function handler_admin_tips($page, $action = 'list', $id = null)
dc767839 313 {
46f272fe 314 $page->setTitle('Administration - Astuces');
dc767839 315 $page->assign('title', 'Gestion des Astuces');
da431170 316 $table_editor = new PLTableEditor('admin/tips', 'reminder_tips', 'id');
b58e5ac2 317 $table_editor->describe('expiration', 'date de péremption', true);
dc767839 318 $table_editor->describe('promo_min', 'promo. min (0 aucune)', false);
319 $table_editor->describe('promo_max', 'promo. max (0 aucune)', false);
054c3ffe 320 $table_editor->describe('title', 'titre', true);
5d617d8d 321 $table_editor->describe('state', 'actif', true);
dc767839 322 $table_editor->describe('text', 'texte (html) de l\'astuce', false);
054c3ffe 323 $table_editor->describe('priority', '0<=priorité<=255', true);
3851b0a6 324 $table_editor->list_on_edit(false);
dc767839 325 $table_editor->apply($page, $action, $id);
3851b0a6 326 if (($action == 'edit' && !is_null($id)) || $action == 'update') {
3032cbd8 327 $page->changeTpl('events/admin_tips.tpl');
328 }
dc767839 329 }
330
26ba053e 331 function handler_admin_events($page, $action = 'list', $eid = null)
506a46ed 332 {
e2efba7d 333 $page->changeTpl('events/admin.tpl');
46f272fe 334 $page->setTitle('Administration - Evenements');
506a46ed 335 $page->register_modifier('hde', 'html_entity_decode');
336
337 $arch = $action == 'archives';
338 $page->assign('action', $action);
02838718 339
f3df6d38 340 $upload = new PlUpload(S::user()->login(), 'event');
02838718 341 if ((Env::has('preview') || Post::v('action') == "Proposer") && $eid) {
342 $action = 'edit';
343 $this->upload_image($page, $upload);
344 }
345
346 if (Post::v('action') == 'Pas d\'image' && $eid) {
7b642046 347 S::assert_xsrf_token();
02838718 348 $upload->rm();
06f4daf9 349 XDB::execute("DELETE FROM announce_photos WHERE eid = {?}", $eid);
02838718 350 $action = 'edit';
351 } elseif (Post::v('action') == 'Supprimer l\'image' && $eid) {
7b642046 352 S::assert_xsrf_token();
02838718 353 $upload->rm();
354 $action = 'edit';
355 } elseif (Post::v('action') == "Proposer" && $eid) {
7b642046 356 S::assert_xsrf_token();
2086ab7f 357 $promo_min = Post::i('promo_min');
358 $promo_max = Post::i('promo_max');
1b602af5 359 if (($promo_min != 0 && ($promo_min <= 1900 || $promo_min >= 2020)) ||
360 ($promo_max != 0 && ($promo_max <= 1900 || $promo_max >= 2020 || $promo_max < $promo_min)))
2086ab7f 361 {
a7d35093 362 $page->trigError("L'intervalle de promotions $promo_min -> $promo_max n'est pas valide");
2086ab7f 363 $action = 'edit';
364 } else {
06f4daf9 365 $res = XDB::query('SELECT flags FROM announces WHERE id = {?}', $eid);
113f6de8 366 $flags = new PlFlagSet($res->fetchOneCell());
794feea7 367 $flags->addFlag('wiki');
9ed396c0 368 if (Post::v('important')) {
369 $flags->addFlag('important');
370 } else {
371 $flags->rmFlag('important');
eaf30d86 372 }
794feea7 373
06f4daf9 374 XDB::execute('UPDATE announces
eaf30d86 375 SET creation_date = creation_date,
b58e5ac2 376 titre={?}, texte={?}, expiration={?}, promo_min={?}, promo_max={?},
9ed396c0 377 flags = {?}
eaf30d86 378 WHERE id = {?}',
b58e5ac2 379 Post::v('titre'), Post::v('texte'), Post::v('expiration'),
9ed396c0 380 Post::v('promo_min'), Post::v('promo_max'),
77e786e1 381 $flags, $eid);
02838718 382 if ($upload->exists() && list($x, $y, $type) = $upload->imageInfo()) {
00ba8a74
SJ
383 XDB::execute('INSERT INTO announce_photos (eid, attachmime, attach, x, y)
384 VALUES ({?}, {?}, {?}, {?}, {?})
385 ON DUPLICATE KEY UPDATE attachmime = VALUES(attachmime), attach = VALUES(attach), x = VALUES(x), y = VALUES(y)',
386 $eid, $type, $upload->getContents(), $x, $y);
02838718 387 $upload->rm();
388 }
eaf30d86 389 }
92423144 390 }
506a46ed 391
392 if ($action == 'edit') {
b58e5ac2 393 $res = XDB::query('SELECT titre, texte, expiration, promo_min, promo_max, FIND_IN_SET(\'important\', flags),
02838718 394 attach IS NOT NULL
06f4daf9
FB
395 FROM announces AS e
396 LEFT JOIN announce_photos AS p ON(e.id = p.eid)
506a46ed 397 WHERE id={?}', $eid);
b58e5ac2 398 list($titre, $texte, $expiration, $promo_min, $promo_max, $important, $img) = $res->fetchOneRow();
506a46ed 399 $page->assign('titre',$titre);
400 $page->assign('texte',$texte);
401 $page->assign('promo_min',$promo_min);
402 $page->assign('promo_max',$promo_max);
b58e5ac2 403 $page->assign('expiration',$expiration);
9ed396c0 404 $page->assign('important', $important);
02838718 405 $page->assign('eid', $eid);
406 $page->assign('img', $img);
407 $page->assign_by_ref('upload', $upload);
506a46ed 408
409 $select = "";
410 for ($i = 1 ; $i < 30 ; $i++) {
411 $p_stamp=date("Ymd",time()+3600*24*$i);
412 $year=substr($p_stamp,0,4);
413 $month=substr($p_stamp,4,2);
414 $day=substr($p_stamp,6,2);
415
eaf30d86 416 $select .= "<option value=\"$p_stamp\""
b58e5ac2 417 . (($p_stamp == strtr($expiration, array("-" => ""))) ? " selected" : "")
506a46ed 418 . "> $day / $month / $year</option>\n";
419 }
420 $page->assign('select',$select);
421 } else {
422 switch ($action) {
423 case 'delete':
7b642046 424 S::assert_xsrf_token();
06f4daf9 425 XDB::execute('DELETE from announces
506a46ed 426 WHERE id = {?}', $eid);
427 break;
428
429 case "archive":
7b642046 430 S::assert_xsrf_token();
06f4daf9 431 XDB::execute('UPDATE announces
506a46ed 432 SET creation_date = creation_date, flags = CONCAT(flags,",archive")
433 WHERE id = {?}', $eid);
434 break;
435
436 case "unarchive":
7b642046 437 S::assert_xsrf_token();
06f4daf9 438 XDB::execute('UPDATE announces
506a46ed 439 SET creation_date = creation_date, flags = REPLACE(flags,"archive","")
440 WHERE id = {?}', $eid);
441 $action = 'archives';
442 $arch = true;
443 break;
444
445 case "valid":
7b642046 446 S::assert_xsrf_token();
06f4daf9 447 XDB::execute('UPDATE announces
506a46ed 448 SET creation_date = creation_date, flags = CONCAT(flags,",valide")
449 WHERE id = {?}', $eid);
450 break;
451
452 case "unvalid":
7b642046 453 S::assert_xsrf_token();
06f4daf9 454 XDB::execute('UPDATE announces
506a46ed 455 SET creation_date = creation_date, flags = REPLACE(flags,"valide", "")
456 WHERE id = {?}', $eid);
457 break;
458 }
459
460 $pid = ($eid && $action == 'preview') ? $eid : -1;
f280f651 461 $sql = "SELECT e.id, e.titre, e.texte,e.id = $pid AS preview, e.uid,
92423144 462 DATE_FORMAT(e.creation_date,'%d/%m/%Y %T') AS creation_date,
b58e5ac2 463 DATE_FORMAT(e.expiration,'%d/%m/%Y') AS expiration,
92423144 464 e.promo_min, e.promo_max,
465 FIND_IN_SET('valide', e.flags) AS fvalide,
466 FIND_IN_SET('archive', e.flags) AS farch,
c321aa99 467 FIND_IN_SET('wiki', e.flags) AS wiki
06f4daf9 468 FROM announces AS e
92423144 469 WHERE ".($arch ? "" : "!")."FIND_IN_SET('archive',e.flags)
b58e5ac2 470 ORDER BY FIND_IN_SET('valide',e.flags), e.expiration DESC";
92423144 471 $page->assign('evs', XDB::iterator($sql));
472 }
506a46ed 473 $page->assign('arch', $arch);
9ed396c0 474 $page->assign('admin_evts', true);
eaf30d86 475 }
74e0093f 476}
477
a7de4ef7 478// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
74e0093f 479?>