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