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
::has('core_rss_hash')) {
45 $page->setRssLink('Polytechnique.org :: Carnet',
46 '/carnet/rss/'.S
::v('hruid').'/'.S
::v('core_rss_hash').'/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 function _handler_notifs_promos(&$page, &$watch, $action, $arg)
78 if(preg_match('!^ *(\d{4}) *$!', $arg, $matches)) {
79 $p = intval($matches[1]);
80 if($p<1900 ||
$p>2100) {
81 $page->trigError("la promo entrée est invalide");
83 if ($action == 'add_promo') {
84 $watch->_promos
->add($p);
86 $watch->_promos
->del($p);
89 } elseif (preg_match('!^ *(\d{4}) *- *(\d{4}) *$!', $arg, $matches)) {
90 $p1 = intval($matches[1]);
91 $p2 = intval($matches[2]);
92 if($p1<1900 ||
$p1>2100) {
93 $page->trigError('la première promo de la plage entrée est invalide');
94 } elseif($p2<1900 ||
$p2>2100) {
95 $page->trigError('la seconde promo de la plage entrée est invalide');
97 if ($action == 'add_promo') {
98 $watch->_promos
->addRange($p1, $p2);
100 $watch->_promos
->delRange($p1, $p2);
104 $page->trigError("La promo (ou la plage de promo) entrée est dans un format incorrect.");
108 function handler_notifs(&$page, $action = null
, $arg = null
)
110 $page->changeTpl('carnet/notifs.tpl');
112 require_once 'notifs.inc.php';
114 $watch = new Watch(S
::v('uid'));
116 $res = XDB
::query("SELECT promo_sortie
118 WHERE user_id = {?}",
120 $promo_sortie = $res->fetchOneCell();
121 $page->assign('promo_sortie', $promo_sortie);
124 S
::assert_xsrf_token();
129 $this->_handler_notifs_promos($page, $watch, $action, $arg);
133 $watch->_nonins
->del($arg);
137 $watch->_nonins
->add($arg);
141 if (Env
::has('subs')) {
142 S
::assert_xsrf_token();
143 $watch->_subs
->update('sub');
146 if (Env
::has('flags_contacts')) {
147 S
::assert_xsrf_token();
148 $watch->watch_contacts
= Env
::b('contacts');
152 if (Env
::has('flags_mail')) {
153 S
::assert_xsrf_token();
154 $watch->watch_mail
= Env
::b('mail');
158 $page->assign_by_ref('watch', $watch);
161 function handler_contacts(&$page, $action = null
, $subaction = null
, $ssaction = null
)
163 $page->setTitle('Mes contacts');
164 $this->_add_rss_link($page);
167 $user = Env
::v('user');
169 // For XSRF protection, checks both the normal xsrf token, and the special RSS token.
170 // It allows direct linking to contact adding in the RSS feed.
171 if (Env
::v('action') && Env
::v('token') !== S
::v('core_rss_hash')) {
172 S
::assert_xsrf_token();
174 switch (Env
::v('action')) {
176 if (($user = User
::get(Env
::v('user')))) {
177 if (XDB
::execute("DELETE FROM contacts
178 WHERE uid = {?} AND contact = {?}", $uid, $user->id())) {
179 $page->trigSuccess("Contact retiré !");
185 if (($user = User
::get(Env
::v('user')))) {
186 if (XDB
::execute("REPLACE INTO contacts (uid, contact)
187 VALUES ({?}, {?})", $uid, $user->id())) {
188 $page->trigSuccess('Contact ajouté !');
190 $page->trigWarning('Contact déjà dans la liste !');
197 if ($action == 'search') {
198 $action = $subaction;
199 $subaction = $ssaction;
202 if ($search && trim(Env
::v('quick'))) {
203 require_once 'userset.inc.php';
204 $base = 'carnet/contacts/search';
206 Platal
::load('search', 'classes.inc.php');
207 ThrowError
::$throwHook = array($this, 'searchErrorHandler');
208 $view = new SearchSet(true
, false
, "INNER JOIN contacts AS c2 ON (u.user_id = c2.contact)", "c2.uid = $uid");
210 $base = 'carnet/contacts';
211 $view = new UserSet("INNER JOIN contacts AS c2 ON (u.user_id = c2.contact)", " c2.uid = $uid ");
213 $view->addMod('minifiche', 'Mini-fiches', true
);
214 $view->addMod('trombi', 'Trombinoscope', false
, array('with_admin' => false
, 'with_promo' => true
));
215 $view->addMod('geoloc', 'Planisphère', false
, array('with_annu' => 'carnet/contacts/search'));
216 $view->apply($base, $page, $action, $subaction);
217 if ($action != 'geoloc' ||
($search && !$ssaction) ||
(!$search && !$subaction)) {
218 $page->changeTpl('carnet/mescontacts.tpl');
222 function handler_pdf(&$page, $arg0 = null
, $arg1 = null
)
224 $this->load('contacts.pdf.inc.php');
225 require_once 'user.func.inc.php';
227 Platal
::session()->close();
229 $sql = "SELECT a.alias
231 INNER JOIN auth_user_md5 AS u ON ( a.id = u.user_id )
232 INNER JOIN contacts AS c ON ( a.id = c.contact )
233 WHERE c.uid = {?} AND a.type='a_vie'";
234 if ($arg0 == 'promo') {
235 $sql .= ' ORDER BY u.promo, u.nom, u.prenom';
237 $sql .= ' ORDER BY u.nom, u.prenom, u.promo';
240 $citer = XDB
::iterRow($sql, S
::v('uid'));
241 $pdf = new ContactsPDF();
243 while (list($alias) = $citer->next()) {
244 $user = get_user_details($alias);
245 foreach ($user as &$value) {
246 if (is_utf8($value)) {
247 $value = utf8_decode($value);
250 $pdf = ContactsPDF
::addContact($pdf, $user, $arg0 == 'photos' ||
$arg1 == 'photos');
257 function handler_rss(&$page, $user = null
, $hash = null
)
259 $this->load('feed.inc.php');
260 $feed = new CarnetFeed();
261 return $feed->run($page, $user, $hash);
264 function handler_ical(&$page, $alias = null
, $hash = null
)
266 require_once 'rss.inc.php';
267 $uid = init_rss(null
, $alias, $hash, false
);
271 } else if ($uid != S
::i('uid')) {
272 send_warning_email("Récupération d\'un autre utilisateur ($uid)");
278 require_once 'ical.inc.php';
279 $page->changeTpl('carnet/calendar.tpl', NO_SKIN
);
280 $page->register_function('display_ical', 'display_ical');
284 IF(u.nom_usage = \'\',u.nom,u.nom_usage) AS nom,
287 DATE_ADD(u.naissance, INTERVAL 1 DAY) AS end,
291 INNER JOIN auth_user_md5 AS u ON (u.user_id = c.contact)
292 INNER JOIN aliases AS a ON (u.user_id = a.id AND a.type = \'a_vie\')
293 WHERE c.uid = {?}', $uid);
296 while (list($prenom, $nom, $promo, $naissance, $end, $ts, $hruid) = $res->next()) {
297 $naissance = str_replace('-', '', $naissance);
298 $end = str_replace('-', '', $end);
300 'timestamp' => strtotime($ts),
301 'date' => $naissance,
304 'summary' => 'Anniversaire de '.$prenom
305 .' '.$nom.' - x '.$promo,
308 $page->assign('events', $annivs);
310 header('Content-Type: text/calendar; charset=utf-8');
313 function handler_vcard(&$page, $photos = null
)
315 $res = XDB
::query('SELECT contact
317 WHERE uid = {?}', S
::v('uid'));
318 $vcard = new VCard($photos == 'photos');
319 $vcard->addUsers($res->fetchColumn());
324 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: