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