2 /***************************************************************************
3 * Copyright (C) 2003-2009 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
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. *
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. *
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 *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
22 class EventsModule
extends PLModule
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'),
34 'ajax/tips' => $this->make_hook('tips', AUTH_COOKIE
, 'user', NO_AUTH
),
35 'admin/tips' => $this->make_hook('admin_tips', AUTH_MDP
, 'admin'),
39 private function get_tips($exclude = null
)
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 = {?}
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>.',
62 $exclude = is_null($exclude) ?
'' : ' AND id != ' . intval($exclude) . ' ';
63 $priority = rand(0, 510);
65 $priority = (int)($priority/2);
66 $res = XDB
::query("SELECT *
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 >= {?})
72 AND (state = 'active')
76 S
::i('promo'), S
::i('promo'), $priority);
77 } while ($priority && !$res->numRows());
78 if (!$res->numRows()) {
81 return $res->fetchOneAssoc();
84 private function upload_image(PlPage
&$page, PlUpload
&$upload)
86 if (@!$_FILES['image']['tmp_name'] && !Env
::v('image_url')) {
89 if (!$upload->upload($_FILES['image']) && !$upload->download(Env
::v('image_url'))) {
90 $page->trigError('Impossible de télécharger l\'image');
92 } elseif (!$upload->isType('image')) {
93 $page->trigError('Le fichier n\'est pas une image valide au format JPEG, GIF ou PNG.');
96 } elseif (!$upload->resizeImage(200, 300, 100, 100, 32284)) {
97 $page->trigError('Impossible de retraiter l\'image');
103 function handler_ev(&$page, $action = 'list', $eid = null
, $pound = null
)
105 $page->changeTpl('events/index.tpl');
106 $page->addJsLink('ajax.js');
107 $page->assign('tips', $this->get_tips());
109 // Profile update (appears when profile is > 400d old), and birthday
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
116 WHERE user_id = {?}", S
::user()->id());
117 list($is_profile_old, $is_birthday, $profile_date, $age) = $res->fetchOneRow();
119 if ($is_profile_old) {
120 $page->assign('fiche_incitation', $profile_date);
123 $page->assign('birthday', $age);
127 $res = XDB
::query("SELECT COUNT(*) FROM photo WHERE uid = {?}", S
::user()->id());
128 $page->assign('photo_incitation', $res->fetchOneCell() == 0);
130 // Geo-location onebox.
131 require_once 'geoloc.inc.php';
132 $res = localize_addresses(S
::user()->id());
133 $page->assign('geoloc_incitation', count($res));
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');
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({?},{?})',
149 pl_redirect('events#'.$pound);
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 = {?}',
157 pl_redirect('events#newsid'.$eid);
160 require_once 'notifs.inc.php';
161 echo Watch
::getCount(S
::user());
163 function next_event(PlIterator
&$it)
166 while ($body = $it->next()) {
167 $uf = UserFilter
::getLegacy($body['promo_min'], $body['promo_max']);
168 if ($uf->checkUser($user)) {
175 // Fetch the events to display, along with their metadata.
177 $it = XDB
::iterator("SELECT e.id, e.titre, e.texte, e.post_id, e.user_id,
178 p.x, p.y, p.attach IS NOT NULL AS img, FIND_IN_SET('wiki', e.flags) AS wiki,
179 FIND_IN_SET('important', e.flags) AS important,
180 e.creation_date > DATE_SUB(CURDATE(), INTERVAL 2 DAY) AS news,
181 e.peremption < DATE_ADD(CURDATE(), INTERVAL 2 DAY) AS end,
182 ev.user_id IS NULL AS nonlu, e.promo_min, e.promo_max
184 LEFT JOIN evenements_photo AS p ON (e.id = p.eid)
185 LEFT JOIN evenements_vus AS ev ON (e.id = ev.evt_id AND ev.user_id = {?})
186 WHERE FIND_IN_SET('valide', e.flags) AND peremption >= NOW()
187 ORDER BY important DESC, news DESC, end DESC, e.peremption, e.creation_date DESC",
189 $cats = array('important', 'news', 'end', 'body');
190 $body = next_event($it);
191 foreach ($cats as $cat) {
197 if ($cat == 'body' ||
$body[$cat]) {
202 $body = next_event($it);
205 $array[$cat] = $data;
209 $page->assign_by_ref('events', $array);
212 function handler_photo(&$page, $eid = null
, $valid = null
)
214 if ($eid && $eid != 'valid') {
215 $res = XDB
::query("SELECT * FROM evenements_photo WHERE eid = {?}", $eid);
216 if ($res->numRows()) {
217 $photo = $res->fetchOneAssoc();
218 header('Content-Type: image/' . $photo['attachmime']);
219 echo $photo['attach'];
222 } elseif ($eid == 'valid') {
223 require_once 'validations.inc.php';
224 $valid = Validate
::get_request_by_id($valid);
225 if ($valid && $valid->img
) {
226 header('Content-Type: image/' . $valid->imgtype
);
231 $upload = new PlUpload(S
::user()->login(), 'event');
232 if ($upload->exists() && $upload->isType('image')) {
233 header('Content-Type: ' . $upload->contentType());
234 echo $upload->getContents();
239 header('Content-Type: image/png');
240 echo file_get_contents($globals->spoolroot
. '/htdocs/images/logo.png');
244 function handler_rss(&$page, $user = null
, $hash = null
)
246 $this->load('feed.inc.php');
247 $feed = new EventFeed();
248 return $feed->run($page, $user, $hash);
251 function handler_preview(&$page)
253 $page->changeTpl('events/preview.tpl', NO_SKIN
);
254 $texte = Get
::v('texte');
255 if (!is_utf8($texte)) {
256 $texte = utf8_encode($texte);
258 $titre = Get
::v('titre');
259 if (!is_utf8($titre)) {
260 $titre = utf8_encode($titre);
262 $page->assign('texte', $texte);
263 $page->assign('titre', $titre);
264 header('Content-Type: text/html; charset=utf-8');
267 function handler_ev_submit(&$page)
269 $page->changeTpl('events/submit.tpl');
270 $page->addJsLink('ajax.js');
272 $wp = new PlWikiPage('Xorg.Annonce');
275 $titre = Post
::v('titre');
276 $texte = Post
::v('texte');
277 $promo_min = Post
::i('promo_min');
278 $promo_max = Post
::i('promo_max');
279 $peremption = Post
::i('peremption');
280 $valid_mesg = Post
::v('valid_mesg');
281 $action = Post
::v('action');
282 $upload = new PlUpload(S
::user()->login(), 'event');
283 $this->upload_image($page, $upload);
285 if (($promo_min > $promo_max && $promo_max != 0)||
286 ($promo_min != 0 && ($promo_min <= 1900 ||
$promo_min >= 2020)) ||
287 ($promo_max != 0 && ($promo_max <= 1900 ||
$promo_max >= 2020)))
289 $page->trigError("L'intervalle de promotions n'est pas valide");
293 $page->assign('titre', $titre);
294 $page->assign('texte', $texte);
295 $page->assign('promo_min', $promo_min);
296 $page->assign('promo_max', $promo_max);
297 $page->assign('peremption', $peremption);
298 $page->assign('valid_mesg', $valid_mesg);
299 $page->assign('action', strtolower($action));
300 $page->assign_by_ref('upload', $upload);
302 if ($action == 'Supprimer l\'image') {
304 $page->assign('action', false
);
305 } elseif ($action && (!trim($texte) ||
!trim($titre))) {
306 $page->trigError("L'article doit avoir un titre et un contenu");
308 S
::assert_xsrf_token();
310 require_once 'validations.inc.php';
311 $evtreq = new EvtReq($titre, $texte, $promo_min, $promo_max,
312 $peremption, $valid_mesg, S
::user(), $upload);
314 $page->assign('ok', true
);
315 } elseif (!Env
::v('preview')) {
320 function handler_tips(&$page, $tips = null
)
322 header('Content-Type: text/html; charset="UTF-8"');
323 $page->changeTpl('include/tips.tpl', NO_SKIN
);
324 $page->assign('tips', $this->get_tips($tips));
327 function handler_admin_tips(&$page, $action = 'list', $id = null
)
329 $page->setTitle('Administration - Astuces');
330 $page->assign('title', 'Gestion des Astuces');
331 $table_editor = new PLTableEditor('admin/tips', 'tips', 'id');
332 $table_editor->describe('peremption', 'date de péremption', true
);
333 $table_editor->describe('promo_min', 'promo. min (0 aucune)', false
);
334 $table_editor->describe('promo_max', 'promo. max (0 aucune)', false
);
335 $table_editor->describe('titre', 'titre', true
);
336 $table_editor->describe('state', 'actif', true
);
337 $table_editor->describe('text', 'texte (html) de l\'astuce', false
);
338 $table_editor->describe('priorite', '0<=priorité<=255', true
);
339 $table_editor->list_on_edit(false
);
340 $table_editor->apply($page, $action, $id);
341 if (($action == 'edit' && !is_null($id)) ||
$action == 'update') {
342 $page->changeTpl('events/admin_tips.tpl');
346 function handler_admin_events(&$page, $action = 'list', $eid = null
)
348 $page->changeTpl('events/admin.tpl');
349 $page->addJsLink('ajax.js');
350 $page->setTitle('Administration - Evenements');
351 $page->register_modifier('hde', 'html_entity_decode');
353 $arch = $action == 'archives';
354 $page->assign('action', $action);
356 $upload = new PlUpload(S
::user()->login(), 'event');
357 if ((Env
::has('preview') || Post
::v('action') == "Proposer") && $eid) {
359 $this->upload_image($page, $upload);
362 if (Post
::v('action') == 'Pas d\'image' && $eid) {
363 S
::assert_xsrf_token();
365 XDB
::execute("DELETE FROM evenements_photo WHERE eid = {?}", $eid);
367 } elseif (Post
::v('action') == 'Supprimer l\'image' && $eid) {
368 S
::assert_xsrf_token();
371 } elseif (Post
::v('action') == "Proposer" && $eid) {
372 S
::assert_xsrf_token();
373 $promo_min = Post
::i('promo_min');
374 $promo_max = Post
::i('promo_max');
375 if (($promo_min != 0 && ($promo_min <= 1900 ||
$promo_min >= 2020)) ||
376 ($promo_max != 0 && ($promo_max <= 1900 ||
$promo_max >= 2020 ||
$promo_max < $promo_min)))
378 $page->trigError("L'intervalle de promotions $promo_min -> $promo_max n'est pas valide");
381 $res = XDB
::query('SELECT flags FROM evenements WHERE id = {?}', $eid);
382 $flags = new PlFlagSet($res->fetchOneCell());
383 $flags->addFlag('wiki');
384 if (Post
::v('important')) {
385 $flags->addFlag('important');
387 $flags->rmFlag('important');
390 XDB
::execute('UPDATE evenements
391 SET creation_date = creation_date,
392 titre={?}, texte={?}, peremption={?}, promo_min={?}, promo_max={?},
395 Post
::v('titre'), Post
::v('texte'), Post
::v('peremption'),
396 Post
::v('promo_min'), Post
::v('promo_max'),
398 if ($upload->exists() && list($x, $y, $type) = $upload->imageInfo()) {
399 XDB
::execute('REPLACE INTO evenements_photo
400 SET eid = {?}, attachmime = {?}, x = {?}, y = {?}, attach = {?}',
401 $eid, $type, $x, $y, $upload->getContents());
407 if ($action == 'edit') {
408 $res = XDB
::query('SELECT titre, texte, peremption, promo_min, promo_max, FIND_IN_SET(\'important\', flags),
411 LEFT JOIN evenements_photo AS p ON(e.id = p.eid)
412 WHERE id={?}', $eid);
413 list($titre, $texte, $peremption, $promo_min, $promo_max, $important, $img) = $res->fetchOneRow();
414 $page->assign('titre',$titre);
415 $page->assign('texte',$texte);
416 $page->assign('promo_min',$promo_min);
417 $page->assign('promo_max',$promo_max);
418 $page->assign('peremption',$peremption);
419 $page->assign('important', $important);
420 $page->assign('eid', $eid);
421 $page->assign('img', $img);
422 $page->assign_by_ref('upload', $upload);
425 for ($i = 1 ; $i < 30 ; $i++
) {
426 $p_stamp=date("Ymd",time()+
3600*24*$i);
427 $year=substr($p_stamp,0,4);
428 $month=substr($p_stamp,4,2);
429 $day=substr($p_stamp,6,2);
431 $select .= "<option value=\"$p_stamp\""
432 . (($p_stamp == strtr($peremption, array("-" => ""))) ?
" selected" : "")
433 . "> $day / $month / $year</option>\n";
435 $page->assign('select',$select);
439 S
::assert_xsrf_token();
440 XDB
::execute('DELETE from evenements
441 WHERE id = {?}', $eid);
445 S
::assert_xsrf_token();
446 XDB
::execute('UPDATE evenements
447 SET creation_date = creation_date, flags = CONCAT(flags,",archive")
448 WHERE id = {?}', $eid);
452 S
::assert_xsrf_token();
453 XDB
::execute('UPDATE evenements
454 SET creation_date = creation_date, flags = REPLACE(flags,"archive","")
455 WHERE id = {?}', $eid);
456 $action = 'archives';
461 S
::assert_xsrf_token();
462 XDB
::execute('UPDATE evenements
463 SET creation_date = creation_date, flags = CONCAT(flags,",valide")
464 WHERE id = {?}', $eid);
468 S
::assert_xsrf_token();
469 XDB
::execute('UPDATE evenements
470 SET creation_date = creation_date, flags = REPLACE(flags,"valide", "")
471 WHERE id = {?}', $eid);
475 $pid = ($eid && $action == 'preview') ?
$eid : -1;
476 $sql = "SELECT e.id, e.titre, e.texte,e.id = $pid AS preview,
477 DATE_FORMAT(e.creation_date,'%d/%m/%Y %T') AS creation_date,
478 DATE_FORMAT(e.peremption,'%d/%m/%Y') AS peremption,
479 e.promo_min, e.promo_max,
480 FIND_IN_SET('valide', e.flags) AS fvalide,
481 FIND_IN_SET('archive', e.flags) AS farch,
482 u.promo, u.nom, u.prenom, u.hruid,
483 FIND_IN_SET('wiki', e.flags) AS wiki
485 INNER JOIN auth_user_md5 AS u ON(e.user_id = u.user_id)
486 WHERE ".($arch ?
"" : "!")."FIND_IN_SET('archive',e.flags)
487 ORDER BY FIND_IN_SET('valide',e.flags), e.peremption DESC";
488 $page->assign('evs', XDB
::iterator($sql));
490 $page->assign('arch', $arch);
491 $page->assign('admin_evts', true
);
495 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: