carnet/panel is back.
[platal.git] / modules / events.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2009 Polytechnique.org *
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
22 class EventsModule extends PLModule
23 {
24 function handlers()
25 {
26 return array(
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'),
33
34 'ajax/tips' => $this->make_hook('tips', AUTH_COOKIE, 'user', NO_AUTH),
35 'admin/tips' => $this->make_hook('admin_tips', AUTH_MDP, 'admin'),
36 );
37 }
38
39 private function get_tips($exclude = null)
40 {
41 global $globals;
42 // Add a new special tip when changing plat/al version
43 if ($globals->version != S::v('last_version') && is_null($exclude)) {
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
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">'
54 . 'les forums</a>, ou sur <a href="changelog">la liste exhaustive des modifications</a>.',
55 'priorite' => 255,
56 'promo_min' => 0,
57 'promo_max' => 0,
58 'state' => 'active',
59 'special' => true);
60 }
61
62 $exclude = is_null($exclude) ? '' : ' AND id != ' . intval($exclude) . ' ';
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 >= {?})
72 AND (state = 'active')
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;
80 }
81 return $res->fetchOneAssoc();
82 }
83
84 private function upload_image(PlPage &$page, PlUpload &$upload)
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'))) {
90 $page->trigError('Impossible de télécharger l\'image');
91 return false;
92 } elseif (!$upload->isType('image')) {
93 $page->trigError('Le fichier n\'est pas une image valide au format JPEG, GIF ou PNG.');
94 $upload->rm();
95 return false;
96 } elseif (!$upload->resizeImage(200, 300, 100, 100, 32284)) {
97 $page->trigError('Impossible de retraiter l\'image');
98 return false;
99 }
100 return true;
101 }
102
103 function handler_ev(&$page, $action = 'list', $eid = null, $pound = null)
104 {
105 $page->changeTpl('events/index.tpl');
106 $page->addJsLink('ajax.js');
107 $page->assign('tips', $this->get_tips());
108
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);
121 }
122 if ($is_birthday) {
123 $page->assign('birthday', $age);
124 }
125
126 // No-photo onebox.
127 $res = XDB::query("SELECT COUNT(*) FROM photo WHERE uid = {?}", S::user()->id());
128 $page->assign('photo_incitation', $res->fetchOneCell() == 0);
129
130 // Geo-location onebox.
131 require_once 'geoloc.inc.php';
132 $res = localize_addresses(S::user()->id());
133 $page->assign('geoloc_incitation', count($res));
134
135 // Direct link to the RSS feed, when available.
136 if (S::hasAuthToken()) {
137 $page->setRssLink('Polytechnique.org :: News',
138 '/rss/'.S::v('hruid') .'/'.S::v('token').'/rss.xml');
139 }
140
141 // Hide the read event, and reload the page to get to the next event.
142 if ($action == 'read' && $eid) {
143 XDB::execute('DELETE ev.*
144 FROM evenements_vus AS ev
145 INNER JOIN evenements AS e ON e.id = ev.evt_id
146 WHERE peremption < NOW()');
147 XDB::execute('REPLACE INTO evenements_vus VALUES({?},{?})',
148 $eid, S::v('uid'));
149 pl_redirect('events#'.$pound);
150 }
151
152 // Unhide the requested event, and reload the page to display it.
153 if ($action == 'unread' && $eid) {
154 XDB::execute('DELETE FROM evenements_vus
155 WHERE evt_id = {?} AND user_id = {?}',
156 $eid, S::v('uid'));
157 pl_redirect('events#newsid'.$eid);
158 }
159
160 function next_event(PlIterator &$it)
161 {
162 $user = S::user();
163 while ($body = $it->next()) {
164 $uf = UserFilter::getLegacy($body['promo_min'], $body['promo_max']);
165 if ($uf->checkUser($user)) {
166 return $body;
167 }
168 }
169 return null;
170 }
171
172 // Fetch the events to display, along with their metadata.
173 $array = array();
174 $it = XDB::iterator("SELECT e.id, e.titre, e.texte, e.post_id, e.user_id,
175 p.x, p.y, p.attach IS NOT NULL AS img, FIND_IN_SET('wiki', e.flags) AS wiki,
176 FIND_IN_SET('important', e.flags) AS important,
177 e.creation_date > DATE_SUB(CURDATE(), INTERVAL 2 DAY) AS news,
178 e.peremption < DATE_ADD(CURDATE(), INTERVAL 2 DAY) AS end,
179 ev.user_id IS NULL AS nonlu, e.promo_min, e.promo_max
180 FROM evenements AS e
181 LEFT JOIN evenements_photo AS p ON (e.id = p.eid)
182 LEFT JOIN evenements_vus AS ev ON (e.id = ev.evt_id AND ev.user_id = {?})
183 WHERE FIND_IN_SET('valide', e.flags) AND peremption >= NOW()
184 ORDER BY important DESC, news DESC, end DESC, e.peremption, e.creation_date DESC",
185 S::i('uid'));
186 $cats = array('important', 'news', 'end', 'body');
187 $body = next_event($it);
188 foreach ($cats as $cat) {
189 $data = array();
190 if (!$body) {
191 continue;
192 }
193 do {
194 if ($cat == 'body' || $body[$cat]) {
195 $data[] = $body;
196 } else {
197 break;
198 }
199 $body = next_event($it);
200 } while ($body);
201 if (!empty($data)) {
202 $array[$cat] = $data;
203 }
204 }
205
206 $page->assign_by_ref('events', $array);
207 }
208
209 function handler_photo(&$page, $eid = null, $valid = null)
210 {
211 if ($eid && $eid != 'valid') {
212 $res = XDB::query("SELECT * FROM evenements_photo WHERE eid = {?}", $eid);
213 if ($res->numRows()) {
214 $photo = $res->fetchOneAssoc();
215 header('Content-Type: image/' . $photo['attachmime']);
216 echo $photo['attach'];
217 exit;
218 }
219 } elseif ($eid == 'valid') {
220 require_once 'validations.inc.php';
221 $valid = Validate::get_request_by_id($valid);
222 if ($valid && $valid->img) {
223 header('Content-Type: image/' . $valid->imgtype);
224 echo $valid->img;
225 exit;
226 }
227 } else {
228 $upload = new PlUpload(S::user()->login(), 'event');
229 if ($upload->exists() && $upload->isType('image')) {
230 header('Content-Type: ' . $upload->contentType());
231 echo $upload->getContents();
232 exit;
233 }
234 }
235 global $globals;
236 header('Content-Type: image/png');
237 echo file_get_contents($globals->spoolroot . '/htdocs/images/logo.png');
238 exit;
239 }
240
241 function handler_rss(&$page, $user = null, $hash = null)
242 {
243 $this->load('feed.inc.php');
244 $feed = new EventFeed();
245 return $feed->run($page, $user, $hash);
246 }
247
248 function handler_preview(&$page)
249 {
250 $page->changeTpl('events/preview.tpl', NO_SKIN);
251 $texte = Get::v('texte');
252 if (!is_utf8($texte)) {
253 $texte = utf8_encode($texte);
254 }
255 $titre = Get::v('titre');
256 if (!is_utf8($titre)) {
257 $titre = utf8_encode($titre);
258 }
259 $page->assign('texte', $texte);
260 $page->assign('titre', $titre);
261 header('Content-Type: text/html; charset=utf-8');
262 }
263
264 function handler_ev_submit(&$page)
265 {
266 $page->changeTpl('events/submit.tpl');
267 $page->addJsLink('ajax.js');
268
269 $wp = new PlWikiPage('Xorg.Annonce');
270 $wp->buildCache();
271
272 $titre = Post::v('titre');
273 $texte = Post::v('texte');
274 $promo_min = Post::i('promo_min');
275 $promo_max = Post::i('promo_max');
276 $peremption = Post::i('peremption');
277 $valid_mesg = Post::v('valid_mesg');
278 $action = Post::v('action');
279 $upload = new PlUpload(S::user()->login(), 'event');
280 $this->upload_image($page, $upload);
281
282 if (($promo_min > $promo_max && $promo_max != 0)||
283 ($promo_min != 0 && ($promo_min <= 1900 || $promo_min >= 2020)) ||
284 ($promo_max != 0 && ($promo_max <= 1900 || $promo_max >= 2020)))
285 {
286 $page->trigError("L'intervalle de promotions n'est pas valide");
287 $action = null;
288 }
289
290 $page->assign('titre', $titre);
291 $page->assign('texte', $texte);
292 $page->assign('promo_min', $promo_min);
293 $page->assign('promo_max', $promo_max);
294 $page->assign('peremption', $peremption);
295 $page->assign('valid_mesg', $valid_mesg);
296 $page->assign('action', strtolower($action));
297 $page->assign_by_ref('upload', $upload);
298
299 if ($action == 'Supprimer l\'image') {
300 $upload->rm();
301 $page->assign('action', false);
302 } elseif ($action && (!trim($texte) || !trim($titre))) {
303 $page->trigError("L'article doit avoir un titre et un contenu");
304 } elseif ($action) {
305 S::assert_xsrf_token();
306
307 require_once 'validations.inc.php';
308 $evtreq = new EvtReq($titre, $texte, $promo_min, $promo_max,
309 $peremption, $valid_mesg, S::user(), $upload);
310 $evtreq->submit();
311 $page->assign('ok', true);
312 } elseif (!Env::v('preview')) {
313 $upload->rm();
314 }
315 }
316
317 function handler_tips(&$page, $tips = null)
318 {
319 header('Content-Type: text/html; charset="UTF-8"');
320 $page->changeTpl('include/tips.tpl', NO_SKIN);
321 $page->assign('tips', $this->get_tips($tips));
322 }
323
324 function handler_admin_tips(&$page, $action = 'list', $id = null)
325 {
326 $page->setTitle('Administration - Astuces');
327 $page->assign('title', 'Gestion des Astuces');
328 $table_editor = new PLTableEditor('admin/tips', 'tips', 'id');
329 $table_editor->describe('peremption', 'date de péremption', true);
330 $table_editor->describe('promo_min', 'promo. min (0 aucune)', false);
331 $table_editor->describe('promo_max', 'promo. max (0 aucune)', false);
332 $table_editor->describe('titre', 'titre', true);
333 $table_editor->describe('state', 'actif', true);
334 $table_editor->describe('text', 'texte (html) de l\'astuce', false);
335 $table_editor->describe('priorite', '0<=priorité<=255', true);
336 $table_editor->list_on_edit(false);
337 $table_editor->apply($page, $action, $id);
338 if (($action == 'edit' && !is_null($id)) || $action == 'update') {
339 $page->changeTpl('events/admin_tips.tpl');
340 }
341 }
342
343 function handler_admin_events(&$page, $action = 'list', $eid = null)
344 {
345 $page->changeTpl('events/admin.tpl');
346 $page->addJsLink('ajax.js');
347 $page->setTitle('Administration - Evenements');
348 $page->register_modifier('hde', 'html_entity_decode');
349
350 $arch = $action == 'archives';
351 $page->assign('action', $action);
352
353 $upload = new PlUpload(S::user()->login(), 'event');
354 if ((Env::has('preview') || Post::v('action') == "Proposer") && $eid) {
355 $action = 'edit';
356 $this->upload_image($page, $upload);
357 }
358
359 if (Post::v('action') == 'Pas d\'image' && $eid) {
360 S::assert_xsrf_token();
361 $upload->rm();
362 XDB::execute("DELETE FROM evenements_photo WHERE eid = {?}", $eid);
363 $action = 'edit';
364 } elseif (Post::v('action') == 'Supprimer l\'image' && $eid) {
365 S::assert_xsrf_token();
366 $upload->rm();
367 $action = 'edit';
368 } elseif (Post::v('action') == "Proposer" && $eid) {
369 S::assert_xsrf_token();
370 $promo_min = Post::i('promo_min');
371 $promo_max = Post::i('promo_max');
372 if (($promo_min != 0 && ($promo_min <= 1900 || $promo_min >= 2020)) ||
373 ($promo_max != 0 && ($promo_max <= 1900 || $promo_max >= 2020 || $promo_max < $promo_min)))
374 {
375 $page->trigError("L'intervalle de promotions $promo_min -> $promo_max n'est pas valide");
376 $action = 'edit';
377 } else {
378 $res = XDB::query('SELECT flags FROM evenements WHERE id = {?}', $eid);
379 $flags = new PlFlagSet($res->fetchOneCell());
380 $flags->addFlag('wiki');
381 if (Post::v('important')) {
382 $flags->addFlag('important');
383 } else {
384 $flags->rmFlag('important');
385 }
386
387 XDB::execute('UPDATE evenements
388 SET creation_date = creation_date,
389 titre={?}, texte={?}, peremption={?}, promo_min={?}, promo_max={?},
390 flags = {?}
391 WHERE id = {?}',
392 Post::v('titre'), Post::v('texte'), Post::v('peremption'),
393 Post::v('promo_min'), Post::v('promo_max'),
394 $flags, $eid);
395 if ($upload->exists() && list($x, $y, $type) = $upload->imageInfo()) {
396 XDB::execute('REPLACE INTO evenements_photo
397 SET eid = {?}, attachmime = {?}, x = {?}, y = {?}, attach = {?}',
398 $eid, $type, $x, $y, $upload->getContents());
399 $upload->rm();
400 }
401 }
402 }
403
404 if ($action == 'edit') {
405 $res = XDB::query('SELECT titre, texte, peremption, promo_min, promo_max, FIND_IN_SET(\'important\', flags),
406 attach IS NOT NULL
407 FROM evenements AS e
408 LEFT JOIN evenements_photo AS p ON(e.id = p.eid)
409 WHERE id={?}', $eid);
410 list($titre, $texte, $peremption, $promo_min, $promo_max, $important, $img) = $res->fetchOneRow();
411 $page->assign('titre',$titre);
412 $page->assign('texte',$texte);
413 $page->assign('promo_min',$promo_min);
414 $page->assign('promo_max',$promo_max);
415 $page->assign('peremption',$peremption);
416 $page->assign('important', $important);
417 $page->assign('eid', $eid);
418 $page->assign('img', $img);
419 $page->assign_by_ref('upload', $upload);
420
421 $select = "";
422 for ($i = 1 ; $i < 30 ; $i++) {
423 $p_stamp=date("Ymd",time()+3600*24*$i);
424 $year=substr($p_stamp,0,4);
425 $month=substr($p_stamp,4,2);
426 $day=substr($p_stamp,6,2);
427
428 $select .= "<option value=\"$p_stamp\""
429 . (($p_stamp == strtr($peremption, array("-" => ""))) ? " selected" : "")
430 . "> $day / $month / $year</option>\n";
431 }
432 $page->assign('select',$select);
433 } else {
434 switch ($action) {
435 case 'delete':
436 S::assert_xsrf_token();
437 XDB::execute('DELETE from evenements
438 WHERE id = {?}', $eid);
439 break;
440
441 case "archive":
442 S::assert_xsrf_token();
443 XDB::execute('UPDATE evenements
444 SET creation_date = creation_date, flags = CONCAT(flags,",archive")
445 WHERE id = {?}', $eid);
446 break;
447
448 case "unarchive":
449 S::assert_xsrf_token();
450 XDB::execute('UPDATE evenements
451 SET creation_date = creation_date, flags = REPLACE(flags,"archive","")
452 WHERE id = {?}', $eid);
453 $action = 'archives';
454 $arch = true;
455 break;
456
457 case "valid":
458 S::assert_xsrf_token();
459 XDB::execute('UPDATE evenements
460 SET creation_date = creation_date, flags = CONCAT(flags,",valide")
461 WHERE id = {?}', $eid);
462 break;
463
464 case "unvalid":
465 S::assert_xsrf_token();
466 XDB::execute('UPDATE evenements
467 SET creation_date = creation_date, flags = REPLACE(flags,"valide", "")
468 WHERE id = {?}', $eid);
469 break;
470 }
471
472 $pid = ($eid && $action == 'preview') ? $eid : -1;
473 $sql = "SELECT e.id, e.titre, e.texte,e.id = $pid AS preview,
474 DATE_FORMAT(e.creation_date,'%d/%m/%Y %T') AS creation_date,
475 DATE_FORMAT(e.peremption,'%d/%m/%Y') AS peremption,
476 e.promo_min, e.promo_max,
477 FIND_IN_SET('valide', e.flags) AS fvalide,
478 FIND_IN_SET('archive', e.flags) AS farch,
479 u.promo, u.nom, u.prenom, u.hruid,
480 FIND_IN_SET('wiki', e.flags) AS wiki
481 FROM evenements AS e
482 INNER JOIN auth_user_md5 AS u ON(e.user_id = u.user_id)
483 WHERE ".($arch ? "" : "!")."FIND_IN_SET('archive',e.flags)
484 ORDER BY FIND_IN_SET('valide',e.flags), e.peremption DESC";
485 $page->assign('evs', XDB::iterator($sql));
486 }
487 $page->assign('arch', $arch);
488 $page->assign('admin_evts', true);
489 }
490 }
491
492 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
493 ?>