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