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