Fix wiki text in RSS
[platal.git] / modules / events.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2007 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 fora</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 != ' . $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 get_events($where, $order, array &$array, $name)
85 {
86 // affichage des evenements
87 // annonces promos triées par présence d'une limite sur les promos
88 // puis par dates croissantes d'expiration
89 $promo = S::v('promo');
90 $uid = S::i('uid');
91 $sql = "SELECT e.id,e.titre, ev.user_id IS NULL AS nonlu
92 FROM evenements AS e
93 LEFT JOIN evenements_vus AS ev ON (e.id = ev.evt_id AND ev.user_id = {?})
94 WHERE FIND_IN_SET('valide', e.flags) AND peremption >= NOW()
95 AND (e.promo_min = 0 || e.promo_min <= {?})
96 AND (e.promo_max = 0 || e.promo_max >= {?})
97 AND $where
98 ORDER BY $order";
99 $sum = XDB::iterator($sql, $uid, $promo, $promo);
100 if (!$sum->total()) {
101 return false;
102 }
103 $sql = "SELECT e.id,e.titre,e.texte,e.post_id,a.user_id,a.nom,a.prenom,a.promo,l.alias AS forlife,
104 p.x, p.y, p.attach IS NOT NULL AS img, FIND_IN_SET('wiki', e.flags) AS wiki
105 FROM evenements AS e
106 LEFT JOIN evenements_photo AS p ON (e.id = p.eid)
107 INNER JOIN auth_user_md5 AS a ON e.user_id=a.user_id
108 INNER JOIN aliases AS l ON ( a.user_id=l.id AND l.type='a_vie' )
109 LEFT JOIN evenements_vus AS ev ON (e.id = ev.evt_id AND ev.user_id = {?})
110 WHERE FIND_IN_SET('valide', e.flags) AND peremption >= NOW()
111 AND (e.promo_min = 0 || e.promo_min <= {?})
112 AND (e.promo_max = 0 || e.promo_max >= {?})
113 AND ev.user_id IS NULL
114 AND $where
115 ORDER BY $order";
116 $evt = XDB::iterator($sql, $uid, $promo, $promo);
117 $array[$name] = array('events' => $evt, 'summary' => $sum);
118 return true;
119 }
120
121 private function upload_image(PlatalPage &$page, PlUpload &$upload)
122 {
123 if (@!$_FILES['image']['tmp_name'] && !Env::v('image_url')) {
124 return true;
125 }
126 if (!$upload->upload($_FILES['image']) && !$upload->download(Env::v('image_url'))) {
127 $page->trig('Impossible de télécharger l\'image');
128 return false;
129 } elseif (!$upload->isType('image')) {
130 $page->trig('Le fichier n\'est pas une image valide au format JPEG, GIF ou PNG.');
131 $upload->rm();
132 return false;
133 } elseif (!$upload->resizeImage(200, 300, 100, 100, 32284)) {
134 $page->trig('Impossible de retraiter l\'image');
135 return false;
136 }
137 return true;
138 }
139
140 function handler_ev(&$page, $action = 'list', $eid = null, $pound = null)
141 {
142 $page->changeTpl('events/index.tpl');
143 $page->addJsLink('ajax.js');
144 $page->assign('tips', $this->get_tips());
145
146 $res = XDB::query('SELECT date, naissance FROM auth_user_md5
147 WHERE user_id={?}', S::v('uid'));
148 list($date, $naissance) = $res->fetchOneRow();
149
150 // incitation à mettre à jour la fiche
151
152 $d2 = mktime(0, 0, 0, substr($date, 5, 2), substr($date, 8, 2),
153 substr($date, 0, 4));
154 if( (time() - $d2) > 60 * 60 * 24 * 400 ) {
155 // si fiche date de + de 400j;
156 $page->assign('fiche_incitation', $date);
157 }
158
159 // Souhaite bon anniversaire
160
161 if (substr($naissance, 5) == date('m-d')) {
162 $page->assign('birthday', date('Y') - substr($naissance, 0, 4));
163 }
164
165 // incitation à mettre une photo
166
167 $res = XDB::query('SELECT COUNT(*) FROM photo
168 WHERE uid={?}', S::v('uid'));
169 $page->assign('photo_incitation', $res->fetchOneCell() == 0);
170
171 // Incitation à se géolocaliser
172 require_once 'geoloc.inc.php';
173 $res = localize_addresses(S::v('uid', -1));
174 $page->assign('geoloc_incitation', count($res));
175
176 // ajout du lien RSS
177 if (S::has('core_rss_hash')) {
178 $page->setRssLink('Polytechnique.org :: News',
179 '/rss/'.S::v('forlife') .'/'.S::v('core_rss_hash').'/rss.xml');
180 }
181
182 // cache les evenements lus et raffiche les evenements a relire
183 if ($action == 'read' && $eid) {
184 XDB::execute('DELETE evenements_vus.*
185 FROM evenements_vus AS ev
186 INNER JOIN evenements AS e ON e.id = ev.evt_id
187 WHERE peremption < NOW()');
188 XDB::execute('REPLACE INTO evenements_vus VALUES({?},{?})',
189 $eid, S::v('uid'));
190 pl_redirect('events#'.$pound);
191 }
192
193 if ($action == 'unread' && $eid) {
194 XDB::execute('DELETE FROM evenements_vus
195 WHERE evt_id = {?} AND user_id = {?}',
196 $eid, S::v('uid'));
197 pl_redirect('events#newsid'.$eid);
198 }
199
200 $array = array();
201 $this->get_events('FIND_IN_SET(\'important\', e.flags)', 'e.creation_date DESC', $array, 'important');
202 $this->get_events('e.creation_date > DATE_SUB(CURDATE(), INTERVAL 2 DAY)
203 AND NOT FIND_IN_SET(\'important\', e.flags)',
204 'e.creation_date DESC', $array, 'news');
205 $this->get_events('e.peremption < DATE_ADD(CURDATE(), INTERVAL 2 DAY)
206 AND e.creation_date <= DATE_SUB(CURDATE(), INTERVAL 2 DAY)
207 AND NOT FIND_IN_SET(\'important\', e.flags)',
208 'e.peremption, e.creation_date DESC', $array, 'end');
209 $this->get_events('e.peremption >= DATE_ADD(CURDATE(), INTERVAL 2 DAY)
210 AND e.creation_date <= DATE_SUB(CURDATE(), INTERVAL 2 DAY)
211 AND NOT FIND_IN_SET(\'important\', e.flags)',
212 'e.peremption, e.creation_date DESC', $array, 'body');
213 $page->assign_by_ref('events', $array);
214 }
215
216 function handler_photo(&$page, $eid = null, $valid = null)
217 {
218 if ($eid && $eid != 'valid') {
219 $res = XDB::query("SELECT * FROM evenements_photo WHERE eid = {?}", $eid);
220 if ($res->numRows()) {
221 $photo = $res->fetchOneAssoc();
222 header('Content-Type: image/' . $photo['attachmime']);
223 echo $photo['attach'];
224 exit;
225 }
226 } elseif ($eid == 'valid') {
227 require_once 'validations.inc.php';
228 $valid = Validate::get_request_by_id($valid);
229 if ($valid && $valid->img) {
230 header('Content-Type: image/' . $valid->imgtype);
231 echo $valid->img;
232 exit;
233 }
234 } else {
235 $upload = new PlUpload(S::v('forlife'), 'event');
236 if ($upload->exists() && $upload->isType('image')) {
237 header('Content-Type: ' . $upload->contentType());
238 echo $upload->getContents();
239 exit;
240 }
241 }
242 global $globals;
243 header('Content-Type: image/png');
244 echo file_get_contents($globals->spoolroot . '/htdocs/images/logo.png');
245 exit;
246 }
247
248 function handler_rss(&$page, $user = null, $hash = null)
249 {
250 require_once 'rss.inc.php';
251
252 $uid = init_rss('events/rss.tpl', $user, $hash);
253
254 $rss = XDB::iterator(
255 'SELECT e.id, e.titre, e.texte, e.creation_date, e.post_id, p.attachmime IS NOT NULL AS photo,
256 IF(u2.nom_usage = "", u2.nom, u2.nom_usage) AS nom, u2.prenom, u2.promo,
257 FIND_IN_SET(\'wiki\', e.flags) AS wiki
258 FROM auth_user_md5 AS u
259 INNER JOIN evenements AS e ON ( (e.promo_min = 0 || e.promo_min <= u.promo)
260 AND (e.promo_max = 0 || e.promo_max >= u.promo) )
261 LEFT JOIN evenements_photo AS p ON (p.eid = e.id)
262 INNER JOIN auth_user_md5 AS u2 ON (u2.user_id = e.user_id)
263 WHERE u.user_id = {?} AND FIND_IN_SET("valide", e.flags)
264 AND peremption >= NOW()', $uid);
265 $page->assign('rss', $rss);
266 }
267
268 function handler_preview(&$page)
269 {
270 $page->changeTpl('events/preview.tpl', NO_SKIN);
271 $texte = Get::v('texte');
272 if (!is_utf8($texte)) {
273 $texte = utf8_encode($texte);
274 }
275 $titre = Get::v('titre');
276 if (!is_utf8($titre)) {
277 $titre = utf8_encode($titre);
278 }
279 $page->assign('texte', $texte);
280 $page->assign('titre', $titre);
281 header('Content-Type: text/html; charset=utf-8');
282 }
283
284 function handler_ev_submit(&$page)
285 {
286 $page->changeTpl('events/submit.tpl');
287 $page->addJsLink('ajax.js');
288
289 require_once('wiki.inc.php');
290 wiki_require_page('Xorg.Annonce');
291
292 $titre = Post::v('titre');
293 $texte = Post::v('texte');
294 $promo_min = Post::i('promo_min');
295 $promo_max = Post::i('promo_max');
296 $peremption = Post::i('peremption');
297 $valid_mesg = Post::v('valid_mesg');
298 $action = Post::v('action');
299 $upload = new PlUpload(S::v('forlife'), 'event');
300 $this->upload_image($page, $upload);
301
302 if (($promo_min > $promo_max && $promo_max != 0)||
303 ($promo_min != 0 && ($promo_min <= 1900 || $promo_min >= 2020)) ||
304 ($promo_max != 0 && ($promo_max <= 1900 || $promo_max >= 2020)))
305 {
306 $page->trig("L'intervalle de promotions n'est pas valide");
307 $action = null;
308 }
309
310 $page->assign('titre', $titre);
311 $page->assign('texte', $texte);
312 $page->assign('promo_min', $promo_min);
313 $page->assign('promo_max', $promo_max);
314 $page->assign('peremption', $peremption);
315 $page->assign('valid_mesg', $valid_mesg);
316 $page->assign('action', strtolower($action));
317 $page->assign_by_ref('upload', $upload);
318
319 if ($action == 'Supprimer l\'image') {
320 $upload->rm();
321 $page->assign('action', false);
322 } elseif ($action && (!trim($texte) || !trim($titre))) {
323 $page->trig("L'article doit avoir un titre et un contenu");
324 } elseif ($action) {
325 require_once 'validations.inc.php';
326 $evtreq = new EvtReq($titre, $texte, $promo_min, $promo_max,
327 $peremption, $valid_mesg, S::v('uid'), $upload);
328 $evtreq->submit();
329 $page->assign('ok', true);
330 } elseif (!Env::v('preview')) {
331 $upload->rm();
332 }
333 }
334
335 function handler_tips(&$page, $tips = null)
336 {
337 header('Content-Type: text/html; charset="UTF-8"');
338 $page->changeTpl('include/tips.tpl', NO_SKIN);
339 $page->assign('tips', $this->get_tips($tips));
340 }
341
342 function handler_admin_tips(&$page, $action = 'list', $id = null)
343 {
344 $page->assign('xorg_title', 'Polytechnique.org - Administration - Astuces');
345 $page->assign('title', 'Gestion des Astuces');
346 $table_editor = new PLTableEditor('admin/tips', 'tips', 'id');
347 $table_editor->describe('peremption', 'date de péremption', true);
348 $table_editor->describe('promo_min', 'promo. min (0 aucune)', false);
349 $table_editor->describe('promo_max', 'promo. max (0 aucune)', false);
350 $table_editor->describe('titre', 'titre', true);
351 $table_editor->describe('state', 'actif', true);
352 $table_editor->describe('text', 'texte (html) de l\'astuce', false);
353 $table_editor->describe('priorite', '0<=priorité<=255', true);
354 $table_editor->list_on_edit(false);
355 $table_editor->apply($page, $action, $id);
356 if (($action == 'edit' && !is_null($id)) || $action == 'update') {
357 $page->changeTpl('events/admin_tips.tpl');
358 }
359 }
360
361 function handler_admin_events(&$page, $action = 'list', $eid = null)
362 {
363 $page->changeTpl('events/admin.tpl');
364 $page->addJsLink('ajax.js');
365 $page->assign('xorg_title','Polytechnique.org - Administration - Evenements');
366 $page->register_modifier('hde', 'html_entity_decode');
367
368 $arch = $action == 'archives';
369 $page->assign('action', $action);
370
371 $upload = new PlUpload(S::v('forlife'), 'event');
372 if ((Env::has('preview') || Post::v('action') == "Proposer") && $eid) {
373 $action = 'edit';
374 $this->upload_image($page, $upload);
375 }
376
377 if (Post::v('action') == 'Pas d\'image' && $eid) {
378 $upload->rm();
379 XDB::execute("DELETE FROM evenements_photo WHERE eid = {?}", $eid);
380 $action = 'edit';
381 } elseif (Post::v('action') == 'Supprimer l\'image' && $eid) {
382 $upload->rm();
383 $action = 'edit';
384 } elseif (Post::v('action') == "Proposer" && $eid) {
385 $promo_min = Post::i('promo_min');
386 $promo_max = Post::i('promo_max');
387 if (($promo_min != 0 && ($promo_min <= 1900 || $promo_min >= 2020)) ||
388 ($promo_max != 0 && ($promo_max <= 1900 || $promo_max >= 2020 || $promo_max < $promo_min)))
389 {
390 $page->trig("L'intervalle de promotions $promo_min -> $promo_max n'est pas valide");
391 $action = 'edit';
392 } else {
393 $res = XDB::query('SELECT flags FROM evenements WHERE id = {?}', $eid);
394 $flags = new FlagSet($res->fetchOneCell());
395 $flags->addFlag('wiki');
396 if (Post::v('important')) {
397 $flags->addFlag('important');
398 } else {
399 $flags->rmFlag('important');
400 }
401
402 XDB::execute('UPDATE evenements
403 SET creation_date = creation_date,
404 titre={?}, texte={?}, peremption={?}, promo_min={?}, promo_max={?},
405 flags = {?}
406 WHERE id = {?}',
407 Post::v('titre'), Post::v('texte'), Post::v('peremption'),
408 Post::v('promo_min'), Post::v('promo_max'),
409 $flags->flags(), $eid);
410 if ($upload->exists() && list($x, $y, $type) = $upload->imageInfo()) {
411 XDB::execute('REPLACE INTO evenements_photo
412 SET eid = {?}, attachmime = {?}, x = {?}, y = {?}, attach = {?}',
413 $eid, $type, $x, $y, $upload->getContents());
414 $upload->rm();
415 }
416 }
417 }
418
419 if ($action == 'edit') {
420 $res = XDB::query('SELECT titre, texte, peremption, promo_min, promo_max, FIND_IN_SET(\'important\', flags),
421 attach IS NOT NULL
422 FROM evenements AS e
423 LEFT JOIN evenements_photo AS p ON(e.id = p.eid)
424 WHERE id={?}', $eid);
425 list($titre, $texte, $peremption, $promo_min, $promo_max, $important, $img) = $res->fetchOneRow();
426 $page->assign('titre',$titre);
427 $page->assign('texte',$texte);
428 $page->assign('promo_min',$promo_min);
429 $page->assign('promo_max',$promo_max);
430 $page->assign('peremption',$peremption);
431 $page->assign('important', $important);
432 $page->assign('eid', $eid);
433 $page->assign('img', $img);
434 $page->assign_by_ref('upload', $upload);
435
436 $select = "";
437 for ($i = 1 ; $i < 30 ; $i++) {
438 $p_stamp=date("Ymd",time()+3600*24*$i);
439 $year=substr($p_stamp,0,4);
440 $month=substr($p_stamp,4,2);
441 $day=substr($p_stamp,6,2);
442
443 $select .= "<option value=\"$p_stamp\""
444 . (($p_stamp == strtr($peremption, array("-" => ""))) ? " selected" : "")
445 . "> $day / $month / $year</option>\n";
446 }
447 $page->assign('select',$select);
448 } else {
449 switch ($action) {
450 case 'delete':
451 XDB::execute('DELETE from evenements
452 WHERE id = {?}', $eid);
453 break;
454
455 case "archive":
456 XDB::execute('UPDATE evenements
457 SET creation_date = creation_date, flags = CONCAT(flags,",archive")
458 WHERE id = {?}', $eid);
459 break;
460
461 case "unarchive":
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 XDB::execute('UPDATE evenements
471 SET creation_date = creation_date, flags = CONCAT(flags,",valide")
472 WHERE id = {?}', $eid);
473 break;
474
475 case "unvalid":
476 XDB::execute('UPDATE evenements
477 SET creation_date = creation_date, flags = REPLACE(flags,"valide", "")
478 WHERE id = {?}', $eid);
479 break;
480 }
481
482 $pid = ($eid && $action == 'preview') ? $eid : -1;
483 $sql = "SELECT e.id, e.titre, e.texte,e.id = $pid AS preview,
484 DATE_FORMAT(e.creation_date,'%d/%m/%Y %T') AS creation_date,
485 DATE_FORMAT(e.peremption,'%d/%m/%Y') AS peremption,
486 e.promo_min, e.promo_max,
487 FIND_IN_SET('valide', e.flags) AS fvalide,
488 FIND_IN_SET('archive', e.flags) AS farch,
489 u.promo, u.nom, u.prenom, a.alias AS forlife,
490 FIND_IN_SET('wiki', flags) AS wiki
491 FROM evenements AS e
492 INNER JOIN auth_user_md5 AS u ON(e.user_id = u.user_id)
493 INNER JOIN aliases AS a ON (u.user_id = a.id AND a.type='a_vie')
494 WHERE ".($arch ? "" : "!")."FIND_IN_SET('archive',e.flags)
495 ORDER BY FIND_IN_SET('valide',e.flags), e.peremption DESC";
496 $page->assign('evs', XDB::iterator($sql));
497 }
498 $page->assign('arch', $arch);
499 $page->assign('admin_evts', true);
500 }
501 }
502
503 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
504 ?>