RSS and email are auth_user free.
[platal.git] / modules / events.php
... / ...
CommitLineData
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
22class 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
161 $uf = new UserFilter(new UFC_And(new UFC_Group('Polytechnique.org'),
162 new UFC_Promo('=', UserFilter::GRADE_ING, 2000),
163 new UFC_Not(new UFC_Sex(User::GENDER_FEMALE))),
164 array(new UFO_Name(UserFilter::LASTNAME),
165 new UFO_Name(UserFilter::FIRSTNAME)));
166 $users = $uf->getUsers(3);
167 foreach ($users as $user) {
168 echo $user->fullName() . '<br />';
169 }
170 echo '... ' . ($uf->getTotalCount() - 3) . ' others<br />';
171
172 function next_event(PlIterator &$it)
173 {
174 $user = S::user();
175 while ($body = $it->next()) {
176 $uf = UserFilter::getLegacy($body['promo_min'], $body['promo_max']);
177 if ($uf->checkUser($user)) {
178 return $body;
179 }
180 }
181 return null;
182 }
183
184 // Fetch the events to display, along with their metadata.
185 $array = array();
186 $it = XDB::iterator("SELECT e.id, e.titre, e.texte, e.post_id, e.user_id,
187 p.x, p.y, p.attach IS NOT NULL AS img, FIND_IN_SET('wiki', e.flags) AS wiki,
188 FIND_IN_SET('important', e.flags) AS important,
189 e.creation_date > DATE_SUB(CURDATE(), INTERVAL 2 DAY) AS news,
190 e.peremption < DATE_ADD(CURDATE(), INTERVAL 2 DAY) AS end,
191 ev.user_id IS NULL AS nonlu, e.promo_min, e.promo_max
192 FROM evenements AS e
193 LEFT JOIN evenements_photo AS p ON (e.id = p.eid)
194 LEFT JOIN evenements_vus AS ev ON (e.id = ev.evt_id AND ev.user_id = {?})
195 WHERE FIND_IN_SET('valide', e.flags) AND peremption >= NOW()
196 ORDER BY important DESC, news DESC, end DESC, e.peremption, e.creation_date DESC",
197 S::i('uid'));
198 $cats = array('important', 'news', 'end', 'body');
199 $body = next_event($it);
200 foreach ($cats as $cat) {
201 $data = array();
202 if (!$body) {
203 continue;
204 }
205 do {
206 if ($cat == 'body' || $body[$cat]) {
207 $data[] = $body;
208 } else {
209 break;
210 }
211 $body = next_event($it);
212 } while ($body);
213 if (!empty($data)) {
214 $array[$cat] = $data;
215 }
216 }
217
218 $page->assign_by_ref('events', $array);
219 }
220
221 function handler_photo(&$page, $eid = null, $valid = null)
222 {
223 if ($eid && $eid != 'valid') {
224 $res = XDB::query("SELECT * FROM evenements_photo WHERE eid = {?}", $eid);
225 if ($res->numRows()) {
226 $photo = $res->fetchOneAssoc();
227 header('Content-Type: image/' . $photo['attachmime']);
228 echo $photo['attach'];
229 exit;
230 }
231 } elseif ($eid == 'valid') {
232 require_once 'validations.inc.php';
233 $valid = Validate::get_request_by_id($valid);
234 if ($valid && $valid->img) {
235 header('Content-Type: image/' . $valid->imgtype);
236 echo $valid->img;
237 exit;
238 }
239 } else {
240 $upload = new PlUpload(S::user()->login(), 'event');
241 if ($upload->exists() && $upload->isType('image')) {
242 header('Content-Type: ' . $upload->contentType());
243 echo $upload->getContents();
244 exit;
245 }
246 }
247 global $globals;
248 header('Content-Type: image/png');
249 echo file_get_contents($globals->spoolroot . '/htdocs/images/logo.png');
250 exit;
251 }
252
253 function handler_rss(&$page, $user = null, $hash = null)
254 {
255 $this->load('feed.inc.php');
256 $feed = new EventFeed();
257 return $feed->run($page, $user, $hash);
258 }
259
260 function handler_preview(&$page)
261 {
262 $page->changeTpl('events/preview.tpl', NO_SKIN);
263 $texte = Get::v('texte');
264 if (!is_utf8($texte)) {
265 $texte = utf8_encode($texte);
266 }
267 $titre = Get::v('titre');
268 if (!is_utf8($titre)) {
269 $titre = utf8_encode($titre);
270 }
271 $page->assign('texte', $texte);
272 $page->assign('titre', $titre);
273 header('Content-Type: text/html; charset=utf-8');
274 }
275
276 function handler_ev_submit(&$page)
277 {
278 $page->changeTpl('events/submit.tpl');
279 $page->addJsLink('ajax.js');
280
281 $wp = new PlWikiPage('Xorg.Annonce');
282 $wp->buildCache();
283
284 $titre = Post::v('titre');
285 $texte = Post::v('texte');
286 $promo_min = Post::i('promo_min');
287 $promo_max = Post::i('promo_max');
288 $peremption = Post::i('peremption');
289 $valid_mesg = Post::v('valid_mesg');
290 $action = Post::v('action');
291 $upload = new PlUpload(S::user()->login(), 'event');
292 $this->upload_image($page, $upload);
293
294 if (($promo_min > $promo_max && $promo_max != 0)||
295 ($promo_min != 0 && ($promo_min <= 1900 || $promo_min >= 2020)) ||
296 ($promo_max != 0 && ($promo_max <= 1900 || $promo_max >= 2020)))
297 {
298 $page->trigError("L'intervalle de promotions n'est pas valide");
299 $action = null;
300 }
301
302 $page->assign('titre', $titre);
303 $page->assign('texte', $texte);
304 $page->assign('promo_min', $promo_min);
305 $page->assign('promo_max', $promo_max);
306 $page->assign('peremption', $peremption);
307 $page->assign('valid_mesg', $valid_mesg);
308 $page->assign('action', strtolower($action));
309 $page->assign_by_ref('upload', $upload);
310
311 if ($action == 'Supprimer l\'image') {
312 $upload->rm();
313 $page->assign('action', false);
314 } elseif ($action && (!trim($texte) || !trim($titre))) {
315 $page->trigError("L'article doit avoir un titre et un contenu");
316 } elseif ($action) {
317 S::assert_xsrf_token();
318
319 require_once 'validations.inc.php';
320 $evtreq = new EvtReq($titre, $texte, $promo_min, $promo_max,
321 $peremption, $valid_mesg, S::user(), $upload);
322 $evtreq->submit();
323 $page->assign('ok', true);
324 } elseif (!Env::v('preview')) {
325 $upload->rm();
326 }
327 }
328
329 function handler_tips(&$page, $tips = null)
330 {
331 header('Content-Type: text/html; charset="UTF-8"');
332 $page->changeTpl('include/tips.tpl', NO_SKIN);
333 $page->assign('tips', $this->get_tips($tips));
334 }
335
336 function handler_admin_tips(&$page, $action = 'list', $id = null)
337 {
338 $page->setTitle('Administration - Astuces');
339 $page->assign('title', 'Gestion des Astuces');
340 $table_editor = new PLTableEditor('admin/tips', 'tips', 'id');
341 $table_editor->describe('peremption', 'date de péremption', true);
342 $table_editor->describe('promo_min', 'promo. min (0 aucune)', false);
343 $table_editor->describe('promo_max', 'promo. max (0 aucune)', false);
344 $table_editor->describe('titre', 'titre', true);
345 $table_editor->describe('state', 'actif', true);
346 $table_editor->describe('text', 'texte (html) de l\'astuce', false);
347 $table_editor->describe('priorite', '0<=priorité<=255', true);
348 $table_editor->list_on_edit(false);
349 $table_editor->apply($page, $action, $id);
350 if (($action == 'edit' && !is_null($id)) || $action == 'update') {
351 $page->changeTpl('events/admin_tips.tpl');
352 }
353 }
354
355 function handler_admin_events(&$page, $action = 'list', $eid = null)
356 {
357 $page->changeTpl('events/admin.tpl');
358 $page->addJsLink('ajax.js');
359 $page->setTitle('Administration - Evenements');
360 $page->register_modifier('hde', 'html_entity_decode');
361
362 $arch = $action == 'archives';
363 $page->assign('action', $action);
364
365 $upload = new PlUpload(S::user()->login(), 'event');
366 if ((Env::has('preview') || Post::v('action') == "Proposer") && $eid) {
367 $action = 'edit';
368 $this->upload_image($page, $upload);
369 }
370
371 if (Post::v('action') == 'Pas d\'image' && $eid) {
372 S::assert_xsrf_token();
373 $upload->rm();
374 XDB::execute("DELETE FROM evenements_photo WHERE eid = {?}", $eid);
375 $action = 'edit';
376 } elseif (Post::v('action') == 'Supprimer l\'image' && $eid) {
377 S::assert_xsrf_token();
378 $upload->rm();
379 $action = 'edit';
380 } elseif (Post::v('action') == "Proposer" && $eid) {
381 S::assert_xsrf_token();
382 $promo_min = Post::i('promo_min');
383 $promo_max = Post::i('promo_max');
384 if (($promo_min != 0 && ($promo_min <= 1900 || $promo_min >= 2020)) ||
385 ($promo_max != 0 && ($promo_max <= 1900 || $promo_max >= 2020 || $promo_max < $promo_min)))
386 {
387 $page->trigError("L'intervalle de promotions $promo_min -> $promo_max n'est pas valide");
388 $action = 'edit';
389 } else {
390 $res = XDB::query('SELECT flags FROM evenements WHERE id = {?}', $eid);
391 $flags = new PlFlagSet($res->fetchOneCell());
392 $flags->addFlag('wiki');
393 if (Post::v('important')) {
394 $flags->addFlag('important');
395 } else {
396 $flags->rmFlag('important');
397 }
398
399 XDB::execute('UPDATE evenements
400 SET creation_date = creation_date,
401 titre={?}, texte={?}, peremption={?}, promo_min={?}, promo_max={?},
402 flags = {?}
403 WHERE id = {?}',
404 Post::v('titre'), Post::v('texte'), Post::v('peremption'),
405 Post::v('promo_min'), Post::v('promo_max'),
406 $flags, $eid);
407 if ($upload->exists() && list($x, $y, $type) = $upload->imageInfo()) {
408 XDB::execute('REPLACE INTO evenements_photo
409 SET eid = {?}, attachmime = {?}, x = {?}, y = {?}, attach = {?}',
410 $eid, $type, $x, $y, $upload->getContents());
411 $upload->rm();
412 }
413 }
414 }
415
416 if ($action == 'edit') {
417 $res = XDB::query('SELECT titre, texte, peremption, promo_min, promo_max, FIND_IN_SET(\'important\', flags),
418 attach IS NOT NULL
419 FROM evenements AS e
420 LEFT JOIN evenements_photo AS p ON(e.id = p.eid)
421 WHERE id={?}', $eid);
422 list($titre, $texte, $peremption, $promo_min, $promo_max, $important, $img) = $res->fetchOneRow();
423 $page->assign('titre',$titre);
424 $page->assign('texte',$texte);
425 $page->assign('promo_min',$promo_min);
426 $page->assign('promo_max',$promo_max);
427 $page->assign('peremption',$peremption);
428 $page->assign('important', $important);
429 $page->assign('eid', $eid);
430 $page->assign('img', $img);
431 $page->assign_by_ref('upload', $upload);
432
433 $select = "";
434 for ($i = 1 ; $i < 30 ; $i++) {
435 $p_stamp=date("Ymd",time()+3600*24*$i);
436 $year=substr($p_stamp,0,4);
437 $month=substr($p_stamp,4,2);
438 $day=substr($p_stamp,6,2);
439
440 $select .= "<option value=\"$p_stamp\""
441 . (($p_stamp == strtr($peremption, array("-" => ""))) ? " selected" : "")
442 . "> $day / $month / $year</option>\n";
443 }
444 $page->assign('select',$select);
445 } else {
446 switch ($action) {
447 case 'delete':
448 S::assert_xsrf_token();
449 XDB::execute('DELETE from evenements
450 WHERE id = {?}', $eid);
451 break;
452
453 case "archive":
454 S::assert_xsrf_token();
455 XDB::execute('UPDATE evenements
456 SET creation_date = creation_date, flags = CONCAT(flags,",archive")
457 WHERE id = {?}', $eid);
458 break;
459
460 case "unarchive":
461 S::assert_xsrf_token();
462 XDB::execute('UPDATE evenements
463 SET creation_date = creation_date, flags = REPLACE(flags,"archive","")
464 WHERE id = {?}', $eid);
465 $action = 'archives';
466 $arch = true;
467 break;
468
469 case "valid":
470 S::assert_xsrf_token();
471 XDB::execute('UPDATE evenements
472 SET creation_date = creation_date, flags = CONCAT(flags,",valide")
473 WHERE id = {?}', $eid);
474 break;
475
476 case "unvalid":
477 S::assert_xsrf_token();
478 XDB::execute('UPDATE evenements
479 SET creation_date = creation_date, flags = REPLACE(flags,"valide", "")
480 WHERE id = {?}', $eid);
481 break;
482 }
483
484 $pid = ($eid && $action == 'preview') ? $eid : -1;
485 $sql = "SELECT e.id, e.titre, e.texte,e.id = $pid AS preview,
486 DATE_FORMAT(e.creation_date,'%d/%m/%Y %T') AS creation_date,
487 DATE_FORMAT(e.peremption,'%d/%m/%Y') AS peremption,
488 e.promo_min, e.promo_max,
489 FIND_IN_SET('valide', e.flags) AS fvalide,
490 FIND_IN_SET('archive', e.flags) AS farch,
491 u.promo, u.nom, u.prenom, u.hruid,
492 FIND_IN_SET('wiki', e.flags) AS wiki
493 FROM evenements AS e
494 INNER JOIN auth_user_md5 AS u ON(e.user_id = u.user_id)
495 WHERE ".($arch ? "" : "!")."FIND_IN_SET('archive',e.flags)
496 ORDER BY FIND_IN_SET('valide',e.flags), e.peremption DESC";
497 $page->assign('evs', XDB::iterator($sql));
498 }
499 $page->assign('arch', $arch);
500 $page->assign('admin_evts', true);
501 }
502}
503
504// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
505?>