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 CarnetModule
extends PLModule
27 'carnet' => $this->make_hook('index', AUTH_COOKIE
),
28 'carnet/panel' => $this->make_hook('panel', AUTH_COOKIE
),
29 'carnet/notifs' => $this->make_hook('notifs', AUTH_COOKIE
),
31 'carnet/contacts' => $this->make_hook('contacts', AUTH_COOKIE
),
32 'carnet/contacts/pdf' => $this->make_hook('pdf', AUTH_COOKIE
, 'user', NO_HTTPS
),
33 'carnet/contacts/ical' => $this->make_hook('ical', AUTH_PUBLIC
, 'user', NO_HTTPS
),
34 'carnet/contacts/vcard' => $this->make_hook('vcard', AUTH_COOKIE
, 'user', NO_HTTPS
),
36 'carnet/rss' => $this->make_hook('rss', AUTH_PUBLIC
, 'user', NO_HTTPS
),
40 function _add_rss_link(&$page)
42 if (!S
::hasAuthToken()) {
45 $page->setRssLink('Polytechnique.org :: Carnet',
46 '/carnet/rss/'.S
::v('hruid').'/'.S
::v('token').'/rss.xml');
49 function handler_index(&$page)
51 $page->changeTpl('carnet/index.tpl');
52 $page->setTitle('Mon carnet');
53 $this->_add_rss_link($page);
56 function handler_panel(&$page)
58 $page->changeTpl('carnet/panel.tpl');
60 if (Get
::has('read')) {
61 S
::set('watch_last', Get
::v('read'));
62 Platal
::session()->updateNbNotifs();
63 pl_redirect('carnet/panel');
66 require_once 'notifs.inc.php';
68 $page->assign('now',date('YmdHis'));
69 $notifs = new Notifs(S
::v('uid'), true
);
71 $page->assign('notifs', $notifs);
72 $page->assign('today', date('Y-m-d'));
73 $this->_add_rss_link($page);
76 private function getSinglePromotion(PlPage
&$page, $promo)
78 if (!ctype_digit($promo) ||
$promo < 1920 ||
$promo > date('Y')) {
79 $page->trigError('Promotion invalide : ' . $promo);
85 private function getPromo(PlPage
&$page, $promo)
87 if (strpos($promo, '-') === false
) {
88 $promo = $this->getSinglePromotion($page, $promo);
96 list($promo1, $promo2) = explode('-', $promo);
97 $promo1 = $this->getSinglePromotion($page, $promo1);
101 $promo2 = $this->getSinglePromotion($page, $promo2);
105 if ($promo1 > $promo2) {
106 $page->trigError("Intervale non valide : " . $promo);
110 for ($i = $promo1 ; $i <= $promo2 ; ++
$i) {
116 private function addPromo(PlPage
&$page, $promo)
118 $promos = $this->getPromo($page, $promo);
119 if (!$promos ||
count($promos) == 0) {
123 foreach ($promos as $promo) {
124 $to_add[] = XDB
::format('({?}, {?})', S
::i('uid'), $promo);
126 XDB
::execute('INSERT IGNORE INTO watch_promo (uid, promo)
127 VALUES ' . implode(', ', $to_add));
130 private function delPromo(PlPage
&$page, $promo)
132 $promos = $this->getPromo($page, $promo);
133 if (!$promos ||
count($promos) == 0) {
136 $to_delete = array();
137 foreach ($promos as $promo) {
138 $to_delete[] = XDB
::format('{?}', $promo);
140 XDB
::execute('DELETE FROM watch_promo
141 WHERE ' . XDB
::format('uid = {?}', S
::i('uid')) . '
142 AND promo IN (' . implode(', ', $to_delete) . ')');
145 public function addNonRegistered(PlPage
&$page, PlUser
&$user)
147 XDB
::execute('INSERT IGNORE INTO watch_nonins (uid, ni_id)
148 VALUES ({?}, {?})', S
::i('uid'), $user->id());
151 public function delNonRegistered(PlPage
&$page, PlUser
&$user)
153 XDB
::execute('DELETE FROM watch_nonins
154 WHERE uid = {?} AND ni_id = {?}',
155 S
::i('uid'), $user->id());
158 public function handler_notifs(&$page, $action = null
, $arg = null
)
160 $page->changeTpl('carnet/notifs.tpl');
163 S
::assert_xsrf_token();
166 $this->addPromo($page, $arg);
170 $this->delPromo($page, $arg);
174 $user = User
::get($arg);
176 $this->delNonRegistered($page, $user);
181 $user = User
::get($arg);
183 $this->addNonRegistered($page, $user);
189 if (Env
::has('subs')) {
190 S
::assert_xsrf_token();
191 $flags = new PlFlagSet();
192 foreach (Env
::v('sub') as $key=>$value) {
193 $flags->addFlag($key, $value);
195 XDB
::execute('UPDATE watch
197 WHERE uid = {?}', $flags, S
::i('uid'));
200 if (Env
::has('flags_contacts')) {
201 S
::assert_xsrf_token();
202 XDB
::execute('UPDATE watch
203 SET ' . XDB
::changeFlag('flags', 'contacts', Env
::b('contacts')) . '
204 WHERE uid = {?}', S
::i('uid'));
207 if (Env
::has('flags_mail')) {
208 S
::assert_xsrf_token();
209 XDB
::execute('UPDATE watch
210 SET ' . XDB
::changeFlag('flags', 'mail', Env
::b('mail')) . '
211 WHERE uid = {?}', S
::i('uid'));
215 $nonins = new UserFilter(new UFC_WatchRegistration($user));
217 $promo = XDB
::fetchColumn('SELECT promo
220 ORDER BY promo', S
::i('uid'));
221 $page->assign('promo_count', count($promo));
225 foreach ($promo as $p) {
226 if (is_null($range_start)) {
227 $range_start = $range_end = $p;
228 } else if ($p != $range_end +
1) {
229 $ranges[] = array($range_start, $range_end);
230 $range_start = $range_end = $p;
235 $ranges[] = array($range_start, $range_end);
236 $page->assign('promo_ranges', $ranges);
237 $page->assign('nonins', $nonins->getUsers());
239 list($flags, $actions) = XDB
::fetchOneRow('SELECT flags, actions
241 WHERE uid = {?}', S
::i('uid'));
242 $flags = new PlFlagSet($flags);
243 $actions = new PlFlagSet($actions);
244 $page->assign('flags', $flags);
245 $page->assign('actions', $actions);
248 function handler_contacts(&$page, $action = null
, $subaction = null
, $ssaction = null
)
250 $page->setTitle('Mes contacts');
251 $this->_add_rss_link($page);
254 $user = Env
::v('user');
256 // For XSRF protection, checks both the normal xsrf token, and the special RSS token.
257 // It allows direct linking to contact adding in the RSS feed.
258 if (Env
::v('action') && Env
::v('token') !== S
::v('token')) {
259 S
::assert_xsrf_token();
261 switch (Env
::v('action')) {
263 if (($user = User
::get(Env
::v('user')))) {
264 if (XDB
::execute("DELETE FROM contacts
265 WHERE uid = {?} AND contact = {?}", $uid, $user->id())) {
266 $page->trigSuccess("Contact retiré !");
272 if (($user = User
::get(Env
::v('user')))) {
273 if (XDB
::execute("REPLACE INTO contacts (uid, contact)
274 VALUES ({?}, {?})", $uid, $user->id())) {
275 $page->trigSuccess('Contact ajouté !');
277 $page->trigWarning('Contact déjà dans la liste !');
284 if ($action == 'search') {
285 $action = $subaction;
286 $subaction = $ssaction;
289 if ($search && trim(Env
::v('quick'))) {
290 require_once 'userset.inc.php';
291 $base = 'carnet/contacts/search';
293 Platal
::load('search', 'classes.inc.php');
294 ThrowError
::$throwHook = array($this, 'searchErrorHandler');
295 $view = new SearchSet(true
, false
, "INNER JOIN contacts AS c2 ON (u.user_id = c2.contact)", "c2.uid = $uid");
297 $base = 'carnet/contacts';
298 $view = new UserSet("INNER JOIN contacts AS c2 ON (u.user_id = c2.contact)", " c2.uid = $uid ");
300 $view->addMod('minifiche', 'Mini-fiches', true
);
301 $view->addMod('trombi', 'Trombinoscope', false
, array('with_admin' => false
, 'with_promo' => true
));
302 $view->addMod('geoloc', 'Planisphère', false
, array('with_annu' => 'carnet/contacts/search'));
303 $view->apply($base, $page, $action, $subaction);
304 if ($action != 'geoloc' ||
($search && !$ssaction) ||
(!$search && !$subaction)) {
305 $page->changeTpl('carnet/mescontacts.tpl');
309 function handler_pdf(&$page, $arg0 = null
, $arg1 = null
)
311 $this->load('contacts.pdf.inc.php');
312 require_once 'user.func.inc.php';
314 Platal
::session()->close();
316 $sql = "SELECT a.alias
318 INNER JOIN auth_user_md5 AS u ON ( a.id = u.user_id )
319 INNER JOIN contacts AS c ON ( a.id = c.contact )
320 WHERE c.uid = {?} AND a.type='a_vie'";
321 if ($arg0 == 'promo') {
322 $sql .= ' ORDER BY u.promo, u.nom, u.prenom';
324 $sql .= ' ORDER BY u.nom, u.prenom, u.promo';
327 $citer = XDB
::iterRow($sql, S
::v('uid'));
328 $pdf = new ContactsPDF();
330 while (list($alias) = $citer->next()) {
331 $user = get_user_details($alias);
332 foreach ($user as &$value) {
333 if (is_utf8($value)) {
334 $value = utf8_decode($value);
337 $pdf = ContactsPDF
::addContact($pdf, $user, $arg0 == 'photos' ||
$arg1 == 'photos');
344 function handler_rss(&$page, $user = null
, $hash = null
)
346 $this->load('feed.inc.php');
347 $feed = new CarnetFeed();
348 return $feed->run($page, $user, $hash);
351 function handler_ical(&$page, $alias = null
, $hash = null
)
353 $user = Platal
::session()->tokenAuth($alias, $hash);
354 if (is_null($user)) {
362 require_once 'ical.inc.php';
363 $page->changeTpl('carnet/calendar.tpl', NO_SKIN
);
364 $page->register_function('display_ical', 'display_ical');
368 IF(u.nom_usage = \'\',u.nom,u.nom_usage) AS nom,
371 DATE_ADD(u.naissance, INTERVAL 1 DAY) AS end,
375 INNER JOIN auth_user_md5 AS u ON (u.user_id = c.contact)
376 INNER JOIN aliases AS a ON (u.user_id = a.id AND a.type = \'a_vie\')
377 WHERE c.uid = {?}', $user->id());
380 while (list($prenom, $nom, $promo, $naissance, $end, $ts, $hruid) = $res->next()) {
381 $naissance = str_replace('-', '', $naissance);
382 $end = str_replace('-', '', $end);
384 'timestamp' => strtotime($ts),
385 'date' => $naissance,
388 'summary' => 'Anniversaire de '.$prenom
389 .' '.$nom.' - x '.$promo,
392 $page->assign('events', $annivs);
394 header('Content-Type: text/calendar; charset=utf-8');
397 function handler_vcard(&$page, $photos = null
)
399 $res = XDB
::query('SELECT contact
401 WHERE uid = {?}', S
::v('uid'));
402 $vcard = new VCard($photos == 'photos');
403 $vcard->addUsers($res->fetchColumn());
408 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: