Adds content caching for static content (css, images, javascript).
[platal.git] / modules / events.php
CommitLineData
74e0093f 1<?php
2/***************************************************************************
8d84c630 3 * Copyright (C) 2003-2009 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
SJ
27 'events' => $this->make_hook('ev', AUTH_COOKIE),
28 'rss' => $this->make_hook('rss', AUTH_PUBLIC, 'user', NO_HTTPS),
29 'events/preview' => $this->make_hook('preview', AUTH_PUBLIC, 'user', NO_AUTH),
30 'events/photo' => $this->make_hook('photo', AUTH_PUBLIC),
31 'events/submit' => $this->make_hook('ev_submit', AUTH_MDP),
32 'admin/events' => $this->make_hook('admin_events', AUTH_MDP, 'admin'),
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
908db125 43 if ($globals->version != S::v('last_version') && is_null($exclude)) {
6ce5dee4 44 XDB::execute('UPDATE auth_user_quick
45 SET last_version = {?}
46 WHERE user_id = {?}',
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 *
67 FROM tips
68 WHERE (peremption = '0000-00-00' OR peremption > CURDATE())
69 AND (promo_min = 0 OR promo_min <= {?})
70 AND (promo_max = 0 OR promo_max >= {?})
71 AND (priorite >= {?})
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
04334c61 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
096f0dca 103 function handler_ev(&$page, $action = 'list', $eid = null, $pound = null)
1a828cd4 104 {
8b1f8e12 105 $page->changeTpl('events/index.tpl');
dc767839 106 $page->addJsLink('ajax.js');
107 $page->assign('tips', $this->get_tips());
108
4b0cb388 109 // Adds a reminder onebox to the page.
fb0ee2e8 110 $user = S::user();
4b0cb388
VZ
111 require_once 'reminder.inc.php';
112 if (($reminder = Reminder::GetCandidateReminder($user))) {
113 $reminder->Prepare($page);
fb0ee2e8
SJ
114 }
115
894c18b2 116 // Wishes "Happy birthday" when required
c52838d6 117 $res = XDB::query(
894c18b2
SJ
118 'SELECT MONTH(naissance) = MONTH(NOW())
119 AND DAYOFMONTH(naissance) = DAYOFMONTH(NOW()) AS is_birthday
c52838d6 120 FROM auth_user_md5
894c18b2
SJ
121 WHERE user_id = {?}',
122 $user->id());
123 $page->assign('birthday', $res->fetchOneCell());
1a828cd4 124
c52838d6 125 // Direct link to the RSS feed, when available.
0279e18d 126 if (S::rssActivated()) {
162370e7 127 $page->setRssLink('Polytechnique.org :: News',
c52838d6 128 '/rss/'.S::v('hruid') .'/'.S::v('core_rss_hash').'/rss.xml');
1a828cd4 129 }
130
c52838d6 131 // Hide the read event, and reload the page to get to the next event.
4e61caea 132 if ($action == 'read' && $eid) {
12500b65 133 XDB::execute('DELETE ev.*
eaf30d86 134 FROM evenements_vus AS ev
eadff9f9 135 INNER JOIN evenements AS e ON e.id = ev.evt_id
136 WHERE peremption < NOW()');
08cce2ff 137 XDB::execute('REPLACE INTO evenements_vus VALUES({?},{?})',
096f0dca 138 $eid, S::v('uid'));
139 pl_redirect('events#'.$pound);
1a828cd4 140 }
141
c52838d6 142 // Unhide the requested event, and reload the page to display it.
4e61caea 143 if ($action == 'unread' && $eid) {
08cce2ff 144 XDB::execute('DELETE FROM evenements_vus
eadff9f9 145 WHERE evt_id = {?} AND user_id = {?}',
4e61caea 146 $eid, S::v('uid'));
096f0dca 147 pl_redirect('events#newsid'.$eid);
1a828cd4 148 }
149
c52838d6 150 // Fetch the events to display, along with their metadata.
d0e45ced 151 $array = array();
c52838d6 152 $it = XDB::iterator("SELECT e.id,e.titre,e.texte,e.post_id,a.user_id,a.nom,a.prenom,a.promo,a.hruid,
c321aa99
FB
153 p.x, p.y, p.attach IS NOT NULL AS img, FIND_IN_SET('wiki', e.flags) AS wiki,
154 FIND_IN_SET('important', e.flags) AS important,
155 e.creation_date > DATE_SUB(CURDATE(), INTERVAL 2 DAY) AS news,
156 e.peremption < DATE_ADD(CURDATE(), INTERVAL 2 DAY) AS end,
157 ev.user_id IS NULL AS nonlu
158 FROM evenements AS e
159 LEFT JOIN evenements_photo AS p ON (e.id = p.eid)
160 INNER JOIN auth_user_md5 AS a ON e.user_id=a.user_id
c321aa99
FB
161 LEFT JOIN evenements_vus AS ev ON (e.id = ev.evt_id AND ev.user_id = {?})
162 WHERE FIND_IN_SET('valide', e.flags) AND peremption >= NOW()
163 AND (e.promo_min = 0 || e.promo_min <= {?})
164 AND (e.promo_max = 0 || e.promo_max >= {?})
165 ORDER BY important DESC, news DESC, end DESC, e.peremption, e.creation_date DESC",
166 S::i('uid'), S::i('promo'), S::i('promo'));
167 $cats = array('important', 'news', 'end', 'body');
168 $body = $it->next();
169 foreach ($cats as $cat) {
170 $data = array();
171 if (!$body) {
172 continue;
173 }
174 do {
175 if ($cat == 'body' || $body[$cat]) {
176 $data[] = $body;
177 } else {
178 break;
179 }
180 $body = $it->next();
181 } while ($body);
182 if (!empty($data)) {
183 $array[$cat] = $data;
184 }
185 }
d0e45ced 186 $page->assign_by_ref('events', $array);
1a828cd4 187 }
188
02838718 189 function handler_photo(&$page, $eid = null, $valid = null)
190 {
191 if ($eid && $eid != 'valid') {
192 $res = XDB::query("SELECT * FROM evenements_photo WHERE eid = {?}", $eid);
193 if ($res->numRows()) {
194 $photo = $res->fetchOneAssoc();
195 header('Content-Type: image/' . $photo['attachmime']);
196 echo $photo['attach'];
197 exit;
198 }
199 } elseif ($eid == 'valid') {
200 require_once 'validations.inc.php';
201 $valid = Validate::get_request_by_id($valid);
202 if ($valid && $valid->img) {
203 header('Content-Type: image/' . $valid->imgtype);
204 echo $valid->img;
205 exit;
206 }
207 } else {
f3df6d38 208 $upload = new PlUpload(S::user()->login(), 'event');
02838718 209 if ($upload->exists() && $upload->isType('image')) {
210 header('Content-Type: ' . $upload->contentType());
211 echo $upload->getContents();
212 exit;
213 }
214 }
215 global $globals;
216 header('Content-Type: image/png');
217 echo file_get_contents($globals->spoolroot . '/htdocs/images/logo.png');
218 exit;
219 }
220
d1c97e42 221 function handler_rss(&$page, $user = null, $hash = null)
eaf30d86 222 {
460d8f55 223 $this->load('feed.inc.php');
61fa44d9 224 $feed = new EventFeed();
8fe7768e 225 return $feed->run($page, $user, $hash);
db3bd146 226 }
227
228 function handler_preview(&$page)
229 {
db3bd146 230 $page->changeTpl('events/preview.tpl', NO_SKIN);
231 $texte = Get::v('texte');
232 if (!is_utf8($texte)) {
233 $texte = utf8_encode($texte);
234 }
db3bd146 235 $titre = Get::v('titre');
236 if (!is_utf8($titre)) {
237 $titre = utf8_encode($titre);
238 }
794feea7 239 $page->assign('texte', $texte);
db3bd146 240 $page->assign('titre', $titre);
241 header('Content-Type: text/html; charset=utf-8');
242 }
d1c97e42 243
1a828cd4 244 function handler_ev_submit(&$page)
74e0093f 245 {
8b1f8e12 246 $page->changeTpl('events/submit.tpl');
db3bd146 247 $page->addJsLink('ajax.js');
eaf30d86 248
8f201b69
FB
249 $wp = new PlWikiPage('Xorg.Annonce');
250 $wp->buildCache();
74e0093f 251
5e2307dc 252 $titre = Post::v('titre');
253 $texte = Post::v('texte');
254 $promo_min = Post::i('promo_min');
255 $promo_max = Post::i('promo_max');
256 $peremption = Post::i('peremption');
257 $valid_mesg = Post::v('valid_mesg');
258 $action = Post::v('action');
f3df6d38 259 $upload = new PlUpload(S::user()->login(), 'event');
02838718 260 $this->upload_image($page, $upload);
74e0093f 261
d1c97e42 262 if (($promo_min > $promo_max && $promo_max != 0)||
2086ab7f 263 ($promo_min != 0 && ($promo_min <= 1900 || $promo_min >= 2020)) ||
264 ($promo_max != 0 && ($promo_max <= 1900 || $promo_max >= 2020)))
265 {
a7d35093 266 $page->trigError("L'intervalle de promotions n'est pas valide");
2086ab7f 267 $action = null;
268 }
269
74e0093f 270 $page->assign('titre', $titre);
271 $page->assign('texte', $texte);
272 $page->assign('promo_min', $promo_min);
273 $page->assign('promo_max', $promo_max);
274 $page->assign('peremption', $peremption);
275 $page->assign('valid_mesg', $valid_mesg);
276 $page->assign('action', strtolower($action));
02838718 277 $page->assign_by_ref('upload', $upload);
74e0093f 278
02838718 279 if ($action == 'Supprimer l\'image') {
280 $upload->rm();
281 $page->assign('action', false);
282 } elseif ($action && (!trim($texte) || !trim($titre))) {
a7d35093 283 $page->trigError("L'article doit avoir un titre et un contenu");
db3bd146 284 } elseif ($action) {
7b642046
VZ
285 S::assert_xsrf_token();
286
74e0093f 287 require_once 'validations.inc.php';
288 $evtreq = new EvtReq($titre, $texte, $promo_min, $promo_max,
5daf68f6 289 $peremption, $valid_mesg, S::user(), $upload);
74e0093f 290 $evtreq->submit();
291 $page->assign('ok', true);
02838718 292 } elseif (!Env::v('preview')) {
293 $upload->rm();
74e0093f 294 }
74e0093f 295 }
b829e258 296
dc767839 297 function handler_tips(&$page, $tips = null)
298 {
493b6abe 299 header('Content-Type: text/html; charset="UTF-8"');
dc767839 300 $page->changeTpl('include/tips.tpl', NO_SKIN);
301 $page->assign('tips', $this->get_tips($tips));
302 }
303
304 function handler_admin_tips(&$page, $action = 'list', $id = null)
305 {
46f272fe 306 $page->setTitle('Administration - Astuces');
dc767839 307 $page->assign('title', 'Gestion des Astuces');
308 $table_editor = new PLTableEditor('admin/tips', 'tips', 'id');
a7de4ef7 309 $table_editor->describe('peremption', 'date de péremption', true);
dc767839 310 $table_editor->describe('promo_min', 'promo. min (0 aucune)', false);
311 $table_editor->describe('promo_max', 'promo. max (0 aucune)', false);
312 $table_editor->describe('titre', 'titre', true);
5d617d8d 313 $table_editor->describe('state', 'actif', true);
dc767839 314 $table_editor->describe('text', 'texte (html) de l\'astuce', false);
a7de4ef7 315 $table_editor->describe('priorite', '0<=priorité<=255', true);
3851b0a6 316 $table_editor->list_on_edit(false);
dc767839 317 $table_editor->apply($page, $action, $id);
3851b0a6 318 if (($action == 'edit' && !is_null($id)) || $action == 'update') {
3032cbd8 319 $page->changeTpl('events/admin_tips.tpl');
320 }
dc767839 321 }
322
eaf30d86 323 function handler_admin_events(&$page, $action = 'list', $eid = null)
506a46ed 324 {
e2efba7d 325 $page->changeTpl('events/admin.tpl');
db3bd146 326 $page->addJsLink('ajax.js');
46f272fe 327 $page->setTitle('Administration - Evenements');
506a46ed 328 $page->register_modifier('hde', 'html_entity_decode');
329
330 $arch = $action == 'archives';
331 $page->assign('action', $action);
02838718 332
f3df6d38 333 $upload = new PlUpload(S::user()->login(), 'event');
02838718 334 if ((Env::has('preview') || Post::v('action') == "Proposer") && $eid) {
335 $action = 'edit';
336 $this->upload_image($page, $upload);
337 }
338
339 if (Post::v('action') == 'Pas d\'image' && $eid) {
7b642046 340 S::assert_xsrf_token();
02838718 341 $upload->rm();
342 XDB::execute("DELETE FROM evenements_photo WHERE eid = {?}", $eid);
343 $action = 'edit';
344 } elseif (Post::v('action') == 'Supprimer l\'image' && $eid) {
7b642046 345 S::assert_xsrf_token();
02838718 346 $upload->rm();
347 $action = 'edit';
348 } elseif (Post::v('action') == "Proposer" && $eid) {
7b642046 349 S::assert_xsrf_token();
2086ab7f 350 $promo_min = Post::i('promo_min');
351 $promo_max = Post::i('promo_max');
1b602af5 352 if (($promo_min != 0 && ($promo_min <= 1900 || $promo_min >= 2020)) ||
353 ($promo_max != 0 && ($promo_max <= 1900 || $promo_max >= 2020 || $promo_max < $promo_min)))
2086ab7f 354 {
a7d35093 355 $page->trigError("L'intervalle de promotions $promo_min -> $promo_max n'est pas valide");
2086ab7f 356 $action = 'edit';
357 } else {
9ed396c0 358 $res = XDB::query('SELECT flags FROM evenements WHERE id = {?}', $eid);
113f6de8 359 $flags = new PlFlagSet($res->fetchOneCell());
794feea7 360 $flags->addFlag('wiki');
9ed396c0 361 if (Post::v('important')) {
362 $flags->addFlag('important');
363 } else {
364 $flags->rmFlag('important');
eaf30d86 365 }
794feea7 366
2086ab7f 367 XDB::execute('UPDATE evenements
eaf30d86 368 SET creation_date = creation_date,
9ed396c0 369 titre={?}, texte={?}, peremption={?}, promo_min={?}, promo_max={?},
370 flags = {?}
eaf30d86 371 WHERE id = {?}',
2086ab7f 372 Post::v('titre'), Post::v('texte'), Post::v('peremption'),
9ed396c0 373 Post::v('promo_min'), Post::v('promo_max'),
77e786e1 374 $flags, $eid);
02838718 375 if ($upload->exists() && list($x, $y, $type) = $upload->imageInfo()) {
376 XDB::execute('REPLACE INTO evenements_photo
377 SET eid = {?}, attachmime = {?}, x = {?}, y = {?}, attach = {?}',
378 $eid, $type, $x, $y, $upload->getContents());
379 $upload->rm();
380 }
eaf30d86 381 }
92423144 382 }
506a46ed 383
384 if ($action == 'edit') {
02838718 385 $res = XDB::query('SELECT titre, texte, peremption, promo_min, promo_max, FIND_IN_SET(\'important\', flags),
386 attach IS NOT NULL
387 FROM evenements AS e
388 LEFT JOIN evenements_photo AS p ON(e.id = p.eid)
506a46ed 389 WHERE id={?}', $eid);
02838718 390 list($titre, $texte, $peremption, $promo_min, $promo_max, $important, $img) = $res->fetchOneRow();
506a46ed 391 $page->assign('titre',$titre);
392 $page->assign('texte',$texte);
393 $page->assign('promo_min',$promo_min);
394 $page->assign('promo_max',$promo_max);
395 $page->assign('peremption',$peremption);
9ed396c0 396 $page->assign('important', $important);
02838718 397 $page->assign('eid', $eid);
398 $page->assign('img', $img);
399 $page->assign_by_ref('upload', $upload);
506a46ed 400
401 $select = "";
402 for ($i = 1 ; $i < 30 ; $i++) {
403 $p_stamp=date("Ymd",time()+3600*24*$i);
404 $year=substr($p_stamp,0,4);
405 $month=substr($p_stamp,4,2);
406 $day=substr($p_stamp,6,2);
407
eaf30d86 408 $select .= "<option value=\"$p_stamp\""
506a46ed 409 . (($p_stamp == strtr($peremption, array("-" => ""))) ? " selected" : "")
410 . "> $day / $month / $year</option>\n";
411 }
412 $page->assign('select',$select);
413 } else {
414 switch ($action) {
415 case 'delete':
7b642046 416 S::assert_xsrf_token();
506a46ed 417 XDB::execute('DELETE from evenements
418 WHERE id = {?}', $eid);
419 break;
420
421 case "archive":
7b642046 422 S::assert_xsrf_token();
506a46ed 423 XDB::execute('UPDATE evenements
424 SET creation_date = creation_date, flags = CONCAT(flags,",archive")
425 WHERE id = {?}', $eid);
426 break;
427
428 case "unarchive":
7b642046 429 S::assert_xsrf_token();
506a46ed 430 XDB::execute('UPDATE evenements
431 SET creation_date = creation_date, flags = REPLACE(flags,"archive","")
432 WHERE id = {?}', $eid);
433 $action = 'archives';
434 $arch = true;
435 break;
436
437 case "valid":
7b642046 438 S::assert_xsrf_token();
506a46ed 439 XDB::execute('UPDATE evenements
440 SET creation_date = creation_date, flags = CONCAT(flags,",valide")
441 WHERE id = {?}', $eid);
442 break;
443
444 case "unvalid":
7b642046 445 S::assert_xsrf_token();
506a46ed 446 XDB::execute('UPDATE evenements
447 SET creation_date = creation_date, flags = REPLACE(flags,"valide", "")
448 WHERE id = {?}', $eid);
449 break;
450 }
451
452 $pid = ($eid && $action == 'preview') ? $eid : -1;
453 $sql = "SELECT e.id, e.titre, e.texte,e.id = $pid AS preview,
92423144 454 DATE_FORMAT(e.creation_date,'%d/%m/%Y %T') AS creation_date,
455 DATE_FORMAT(e.peremption,'%d/%m/%Y') AS peremption,
456 e.promo_min, e.promo_max,
457 FIND_IN_SET('valide', e.flags) AS fvalide,
458 FIND_IN_SET('archive', e.flags) AS farch,
c52838d6 459 u.promo, u.nom, u.prenom, u.hruid,
c321aa99 460 FIND_IN_SET('wiki', e.flags) AS wiki
92423144 461 FROM evenements AS e
462 INNER JOIN auth_user_md5 AS u ON(e.user_id = u.user_id)
92423144 463 WHERE ".($arch ? "" : "!")."FIND_IN_SET('archive',e.flags)
506a46ed 464 ORDER BY FIND_IN_SET('valide',e.flags), e.peremption DESC";
92423144 465 $page->assign('evs', XDB::iterator($sql));
466 }
506a46ed 467 $page->assign('arch', $arch);
9ed396c0 468 $page->assign('admin_evts', true);
eaf30d86 469 }
74e0093f 470}
471
a7de4ef7 472// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
74e0093f 473?>