2 /***************************************************************************
3 * Copyright (C) 2003-2011 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 'events/preview' => $this->make_hook('preview', AUTH_PUBLIC
, 'user', NO_AUTH
),
29 'events/photo' => $this->make_hook('photo', AUTH_PUBLIC
),
30 'events/submit' => $this->make_hook('ev_submit', AUTH_MDP
),
31 'admin/events' => $this->make_hook('admin_events', AUTH_MDP
, 'admin'),
32 'rss' => $this->make_token_hook('rss', AUTH_COOKIE
),
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
::user()->last_version
&& is_null($exclude)) {
44 XDB
::execute('UPDATE accounts
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 (expiration = '0000-00-00' OR expiration > 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');
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.
113 if ($user->checkPerms(User
::PERM_MAIL
)) {
114 $page->assign('tips', $this->get_tips());
118 // Adds a reminder onebox to the page.
119 require_once 'reminder.inc.php';
120 if (($reminder = Reminder
::GetCandidateReminder($user))) {
121 $reminder->Prepare($page);
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);
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');
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('INSERT IGNORE INTO announce_read (evt_id, uid)
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 announce_read
155 WHERE evt_id = {?} AND uid = {?}',
157 pl_redirect('events#newsid'.$eid);
160 // Fetch the events to display, along with their metadata.
162 $it = XDB
::iterator("SELECT e.id, e.titre, e.texte, e.post_id, e.uid,
163 p.x, p.y, p.attach IS NOT NULL AS img, FIND_IN_SET('wiki', e.flags) AS wiki,
164 FIND_IN_SET('important', e.flags) AS important,
165 e.creation_date > DATE_SUB(CURDATE(), INTERVAL 2 DAY) AS news,
166 e.expiration < DATE_ADD(CURDATE(), INTERVAL 2 DAY) AS end,
167 ev.uid IS NULL AS nonlu, e.promo_min, e.promo_max
169 LEFT JOIN announce_photos AS p ON (e.id = p.eid)
170 LEFT JOIN announce_read AS ev ON (e.id = ev.evt_id AND ev.uid = {?})
171 WHERE FIND_IN_SET('valide', e.flags) AND expiration >= NOW()
172 ORDER BY important DESC, news DESC, end DESC, e.expiration, e.creation_date DESC",
174 $cats = array('important', 'news', 'end', 'body');
176 $this->load('feed.inc.php');
178 $body = EventFeed
::nextEvent($it, $user);
179 foreach ($cats as $cat) {
185 if ($cat == 'body' ||
$body[$cat]) {
190 $body = EventFeed
::nextEvent($it, $user);
193 $array[$cat] = $data;
197 $page->assign_by_ref('events', $array);
200 function handler_photo($page, $eid = null
, $valid = null
)
202 if ($eid && $eid != 'valid') {
203 $res = XDB
::query("SELECT * FROM announce_photos WHERE eid = {?}", $eid);
204 if ($res->numRows()) {
205 $photo = $res->fetchOneAssoc();
206 pl_cached_dynamic_content_headers("image/" . $photo['attachmime']);
207 echo $photo['attach'];
210 } elseif ($eid == 'valid') {
211 $valid = Validate
::get_request_by_id($valid);
212 if ($valid && $valid->img
) {
213 pl_cached_dynamic_content_headers("image/" . $valid->imgtype
);
218 $upload = new PlUpload(S
::user()->login(), 'event');
219 if ($upload->exists() && $upload->isType('image')) {
220 pl_cached_dynamic_content_headers($upload->contentType());
221 echo $upload->getContents();
226 pl_cached_dynamic_content_headers("image/png");
227 echo file_get_contents($globals->spoolroot
. '/htdocs/images/logo.png');
231 function handler_rss(PlPage
$page, PlUser
$user)
233 $this->load('feed.inc.php');
234 $feed = new EventFeed();
235 return $feed->run($page, $user);
238 function handler_preview($page)
240 $page->changeTpl('events/preview.tpl', NO_SKIN
);
241 $texte = Get
::v('texte');
242 if (!is_utf8($texte)) {
243 $texte = utf8_encode($texte);
245 $titre = Get
::v('titre');
246 if (!is_utf8($titre)) {
247 $titre = utf8_encode($titre);
249 $page->assign('texte', $texte);
250 $page->assign('titre', $titre);
251 pl_content_headers("text/html");
254 function handler_ev_submit($page)
256 $page->changeTpl('events/submit.tpl');
258 $wp = new PlWikiPage('Xorg.Annonce');
261 $titre = Post
::v('titre');
262 $texte = Post
::v('texte');
263 $promo_min = Post
::i('promo_min');
264 $promo_max = Post
::i('promo_max');
265 $expiration = Post
::i('expiration');
266 $valid_mesg = Post
::v('valid_mesg');
267 $action = Post
::v('action');
268 $upload = new PlUpload(S
::user()->login(), 'event');
269 $this->upload_image($page, $upload);
271 if (($promo_min > $promo_max && $promo_max != 0)||
272 ($promo_min != 0 && ($promo_min <= 1900 ||
$promo_min >= 2020)) ||
273 ($promo_max != 0 && ($promo_max <= 1900 ||
$promo_max >= 2020)))
275 $page->trigError("L'intervalle de promotions n'est pas valide");
279 $page->assign('titre', $titre);
280 $page->assign('texte', $texte);
281 $page->assign('promo_min', $promo_min);
282 $page->assign('promo_max', $promo_max);
283 $page->assign('expiration', $expiration);
284 $page->assign('valid_mesg', $valid_mesg);
285 $page->assign('action', strtolower($action));
286 $page->assign_by_ref('upload', $upload);
288 if ($action == 'Supprimer l\'image') {
290 $page->assign('action', false
);
291 } elseif ($action && (!trim($texte) ||
!trim($titre))) {
292 $page->trigError("L'article doit avoir un titre et un contenu");
294 S
::assert_xsrf_token();
296 $evtreq = new EvtReq($titre, $texte, $promo_min, $promo_max,
297 $expiration, $valid_mesg, S
::user(), $upload);
299 $page->assign('ok', true
);
300 } elseif (!Env
::v('preview')) {
305 function handler_tips($page, $tips = null
)
307 pl_content_headers("text/html");
308 $page->changeTpl('include/tips.tpl', NO_SKIN
);
309 $page->assign('tips', $this->get_tips($tips));
312 function handler_admin_tips($page, $action = 'list', $id = null
)
314 $page->setTitle('Administration - Astuces');
315 $page->assign('title', 'Gestion des Astuces');
316 $table_editor = new PLTableEditor('admin/tips', 'reminder_tips', 'id');
317 $table_editor->describe('expiration', 'date de péremption', true
);
318 $table_editor->describe('promo_min', 'promo. min (0 aucune)', false
, true
);
319 $table_editor->describe('promo_max', 'promo. max (0 aucune)', false
, true
);
320 $table_editor->describe('title', 'titre', true
);
321 $table_editor->describe('state', 'actif', true
);
322 $table_editor->describe('text', 'texte (html) de l\'astuce', false
, true
);
323 $table_editor->describe('priority', '0<=priorité<=255', true
);
324 $table_editor->list_on_edit(false
);
325 $table_editor->apply($page, $action, $id);
326 if (($action == 'edit' && !is_null($id)) ||
$action == 'update') {
327 $page->changeTpl('events/admin_tips.tpl');
331 function handler_admin_events($page, $action = 'list', $eid = null
)
333 $page->changeTpl('events/admin.tpl');
334 $page->setTitle('Administration - Evenements');
335 $page->register_modifier('hde', 'html_entity_decode');
337 $arch = $action == 'archives';
338 $page->assign('action', $action);
340 $upload = new PlUpload(S
::user()->login(), 'event');
341 if ((Env
::has('preview') || Post
::v('action') == "Proposer") && $eid) {
343 $this->upload_image($page, $upload);
346 if (Post
::v('action') == 'Pas d\'image' && $eid) {
347 S
::assert_xsrf_token();
349 XDB
::execute("DELETE FROM announce_photos WHERE eid = {?}", $eid);
351 } elseif (Post
::v('action') == 'Supprimer l\'image' && $eid) {
352 S
::assert_xsrf_token();
355 } elseif (Post
::v('action') == "Proposer" && $eid) {
356 S
::assert_xsrf_token();
357 $promo_min = Post
::i('promo_min');
358 $promo_max = Post
::i('promo_max');
359 if (($promo_min != 0 && ($promo_min <= 1900 ||
$promo_min >= 2020)) ||
360 ($promo_max != 0 && ($promo_max <= 1900 ||
$promo_max >= 2020 ||
$promo_max < $promo_min)))
362 $page->trigError("L'intervalle de promotions $promo_min -> $promo_max n'est pas valide");
365 $res = XDB
::query('SELECT flags FROM announces WHERE id = {?}', $eid);
366 $flags = new PlFlagSet($res->fetchOneCell());
367 $flags->addFlag('wiki');
368 if (Post
::v('important')) {
369 $flags->addFlag('important');
371 $flags->rmFlag('important');
374 XDB
::execute('UPDATE announces
375 SET creation_date = creation_date,
376 titre={?}, texte={?}, expiration={?}, promo_min={?}, promo_max={?},
379 Post
::v('titre'), Post
::v('texte'), Post
::v('expiration'),
380 Post
::v('promo_min'), Post
::v('promo_max'),
382 if ($upload->exists() && list($x, $y, $type) = $upload->imageInfo()) {
383 XDB
::execute('INSERT INTO announce_photos (eid, attachmime, attach, x, y)
384 VALUES ({?}, {?}, {?}, {?}, {?})
385 ON DUPLICATE KEY UPDATE attachmime = VALUES(attachmime), attach = VALUES(attach), x = VALUES(x), y = VALUES(y)',
386 $eid, $type, $upload->getContents(), $x, $y);
392 if ($action == 'edit') {
393 $res = XDB
::query('SELECT titre, texte, expiration, promo_min, promo_max, FIND_IN_SET(\'important\', flags),
396 LEFT JOIN announce_photos AS p ON(e.id = p.eid)
397 WHERE id={?}', $eid);
398 list($titre, $texte, $expiration, $promo_min, $promo_max, $important, $img) = $res->fetchOneRow();
399 $page->assign('titre',$titre);
400 $page->assign('texte',$texte);
401 $page->assign('promo_min',$promo_min);
402 $page->assign('promo_max',$promo_max);
403 $page->assign('expiration',$expiration);
404 $page->assign('important', $important);
405 $page->assign('eid', $eid);
406 $page->assign('img', $img);
407 $page->assign_by_ref('upload', $upload);
410 for ($i = 1 ; $i < 30 ; $i++
) {
411 $p_stamp=date("Ymd",time()+
3600*24*$i);
412 $year=substr($p_stamp,0,4);
413 $month=substr($p_stamp,4,2);
414 $day=substr($p_stamp,6,2);
416 $select .= "<option value=\"$p_stamp\""
417 . (($p_stamp == strtr($expiration, array("-" => ""))) ?
" selected" : "")
418 . "> $day / $month / $year</option>\n";
420 $page->assign('select',$select);
424 S
::assert_xsrf_token();
425 XDB
::execute('DELETE from announces
426 WHERE id = {?}', $eid);
430 S
::assert_xsrf_token();
431 XDB
::execute('UPDATE announces
432 SET creation_date = creation_date, flags = CONCAT(flags,",archive")
433 WHERE id = {?}', $eid);
437 S
::assert_xsrf_token();
438 XDB
::execute('UPDATE announces
439 SET creation_date = creation_date, flags = REPLACE(flags,"archive","")
440 WHERE id = {?}', $eid);
441 $action = 'archives';
446 S
::assert_xsrf_token();
447 XDB
::execute('UPDATE announces
448 SET creation_date = creation_date, flags = CONCAT(flags,",valide")
449 WHERE id = {?}', $eid);
453 S
::assert_xsrf_token();
454 XDB
::execute('UPDATE announces
455 SET creation_date = creation_date, flags = REPLACE(flags,"valide", "")
456 WHERE id = {?}', $eid);
460 $pid = ($eid && $action == 'preview') ?
$eid : -1;
461 $sql = "SELECT e.id, e.titre, e.texte,e.id = $pid AS preview, e.uid,
462 DATE_FORMAT(e.creation_date,'%d/%m/%Y %T') AS creation_date,
463 DATE_FORMAT(e.expiration,'%d/%m/%Y') AS expiration,
464 e.promo_min, e.promo_max,
465 FIND_IN_SET('valide', e.flags) AS fvalide,
466 FIND_IN_SET('archive', e.flags) AS farch,
467 FIND_IN_SET('wiki', e.flags) AS wiki
469 WHERE ".($arch ?
"" : "!")."FIND_IN_SET('archive',e.flags)
470 ORDER BY FIND_IN_SET('valide',e.flags), e.expiration DESC";
471 $page->assign('evs', XDB
::iterator($sql));
473 $page->assign('arch', $arch);
474 $page->assign('admin_evts', true
);
478 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: