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