194d866de756027df1bbdc75a983ac53f414ab33
[platal.git] / modules / carnet.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2008 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 CarnetModule extends PLModule
23 {
24 function handlers()
25 {
26 return array(
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),
30
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),
35
36 'carnet/rss' => $this->make_hook('rss', AUTH_PUBLIC, 'user', NO_HTTPS),
37 );
38 }
39
40 function on_subscribe($forlife, $uid, $promo, $password)
41 {
42 require_once 'notifs.inc.php';
43 register_watch_op($uid, WATCH_INSCR);
44 inscription_notifs_base($uid);
45 }
46
47 function _add_rss_link(&$page)
48 {
49 if (!S::has('core_rss_hash')) {
50 return;
51 }
52 $page->setRssLink('Polytechnique.org :: Carnet',
53 '/carnet/rss/'.S::v('forlife') .'/'.S::v('core_rss_hash').'/rss.xml');
54 }
55
56 function handler_index(&$page)
57 {
58 $page->changeTpl('carnet/index.tpl');
59 $page->assign('xorg_title','Polytechnique.org - Mon carnet');
60 $this->_add_rss_link($page);
61 }
62
63 function handler_panel(&$page)
64 {
65 $page->changeTpl('carnet/panel.tpl');
66
67 if (Get::has('read')) {
68 $_SESSION['watch_last'] = Get::v('read');
69 update_NbNotifs();
70 pl_redirect('carnet/panel');
71 }
72
73 require_once 'notifs.inc.php';
74
75 $page->assign('now',date('YmdHis'));
76 $notifs = new Notifs(S::v('uid'), true);
77
78 $page->assign('notifs', $notifs);
79 $page->assign('today', date('Y-m-d'));
80 $this->_add_rss_link($page);
81 }
82
83 function _handler_notifs_promos(&$page, &$watch, $action, $arg)
84 {
85 if(preg_match('!^ *(\d{4}) *$!', $arg, $matches)) {
86 $p = intval($matches[1]);
87 if($p<1900 || $p>2100) {
88 $page->trig("la promo entrée est invalide");
89 } else {
90 if ($action == 'add_promo') {
91 $watch->_promos->add($p);
92 } else {
93 $watch->_promos->del($p);
94 }
95 }
96 } elseif (preg_match('!^ *(\d{4}) *- *(\d{4}) *$!', $arg, $matches)) {
97 $p1 = intval($matches[1]);
98 $p2 = intval($matches[2]);
99 if($p1<1900 || $p1>2100) {
100 $page->trig('la première promo de la plage entrée est invalide');
101 } elseif($p2<1900 || $p2>2100) {
102 $page->trig('la seconde promo de la plage entrée est invalide');
103 } else {
104 if ($action == 'add_promo') {
105 $watch->_promos->addRange($p1, $p2);
106 } else {
107 $watch->_promos->delRange($p1, $p2);
108 }
109 }
110 } else {
111 $page->trig("La promo (ou la plage de promo) entrée est dans un format incorrect.");
112 }
113 }
114
115 function handler_notifs(&$page, $action = null, $arg = null)
116 {
117 $page->changeTpl('carnet/notifs.tpl');
118
119 require_once 'notifs.inc.php';
120
121 $watch = new Watch(S::v('uid'));
122
123 $res = XDB::query("SELECT promo_sortie
124 FROM auth_user_md5
125 WHERE user_id = {?}",
126 S::v('uid', -1));
127 $promo_sortie = $res->fetchOneCell();
128 $page->assign('promo_sortie', $promo_sortie);
129
130 if ($action && !S::has_xsrf_token()) {
131 $page->trig("La mise à jour des notifications a échouée, merci de réessayer.");
132 $action = false;
133 }
134 switch ($action) {
135 case 'add_promo':
136 case 'del_promo':
137 $this->_handler_notifs_promos($page, $watch, $action, $arg);
138 break;
139
140 case 'del_nonins':
141 $watch->_nonins->del($arg);
142 break;
143
144 case 'add_nonins':
145 $watch->_nonins->add($arg);
146 break;
147 }
148
149 if (Env::has('subs') && S::has_xsrf_token()) {
150 $watch->_subs->update('sub');
151 } elseif (Env::has('subs')) {
152 $page->trig("La mise à jour des notifications a échouée, merci de réessayer.");
153 }
154
155 if (Env::has('flags_contacts') && S::has_xsrf_token()) {
156 $watch->watch_contacts = Env::b('contacts');
157 $watch->saveFlags();
158 } elseif (Env::has('flags_contacts')) {
159 $page->trig("La mise à jour des notifications a échouée, merci de réessayer.");
160 }
161
162 if (Env::has('flags_mail') && S::has_xsrf_token()) {
163 $watch->watch_mail = Env::b('mail');
164 $watch->saveFlags();
165 } elseif (Env::has('flags_mail')) {
166 $page->trig("La mise à jour des notifications a échouée, merci de réessayer.");
167 }
168
169 $page->assign_by_ref('watch', $watch);
170 }
171
172 function _get_list($offset, $limit) {
173 $uid = S::v('uid');
174 $res = XDB::query("SELECT COUNT(*) FROM contacts WHERE uid = {?}", $uid);
175 $total = $res->fetchOneCell();
176
177 $order = Get::v('order');
178 $orders = Array(
179 'nom' => 'nom DESC, u.prenom, u.promo',
180 'promo' => 'promo DESC, nom, u.prenom',
181 'last' => 'u.date DESC, nom, u.prenom, promo');
182 if ($order != 'promo' && $order != 'last')
183 $order = 'nom';
184 $order = $orders[$order];
185 if (Get::v('inv') == '')
186 $order = str_replace(" DESC,", ",", $order);
187
188 $res = XDB::query("
189 SELECT u.prenom, IF(u.nom_usage='',u.nom,u.nom_usage) AS nom, a.alias AS forlife, u.promo
190 FROM contacts AS c
191 INNER JOIN auth_user_md5 AS u ON (u.user_id = c.contact)
192 INNER JOIN aliases AS a ON (u.user_id = a.id AND a.type='a_vie')
193 WHERE c.uid = {?}
194 ORDER BY $order
195 LIMIT {?}, {?}", $uid, $offset*$limit, $limit);
196 $list = $res->fetchAllAssoc();
197
198 return Array($total, $list);
199 }
200
201 function searchErrorHandler($explain) {
202 global $page;
203 $page->trig($explain);
204 $this->handler_contacts($page);
205 }
206
207 function handler_contacts(&$page, $action = null, $subaction = null, $ssaction = null)
208 {
209 $page->assign('xorg_title','Polytechnique.org - Mes contacts');
210 $this->_add_rss_link($page);
211
212 $uid = S::v('uid');
213 $user = Env::v('user');
214
215 // For XSRF protection, checks both the normal xsrf token, and the special RSS token.
216 // It allows direct linking to contact adding in the RSS feed.
217 if (Env::v('action') && (S::has_xsrf_token() || Env::v('token') === S::v('core_rss_hash'))) {
218 switch (Env::v('action')) {
219 case 'retirer':
220 if (is_numeric($user)) {
221 if (XDB::execute('DELETE FROM contacts
222 WHERE uid = {?} AND contact = {?}',
223 $uid, $user))
224 {
225 $page->trig("Contact retiré !");
226 }
227 } else {
228 if (XDB::execute(
229 'DELETE FROM c
230 USING contacts AS c
231 INNER JOIN aliases AS a ON (c.contact=a.id and a.type!="homonyme")
232 WHERE c.uid = {?} AND a.alias={?}', $uid, $user))
233 {
234 $page->trig("Contact retiré !");
235 }
236 }
237 break;
238
239 case 'ajouter':
240 require_once('user.func.inc.php');
241 if (($login = get_user_login($user)) !== false) {
242 if (XDB::execute(
243 'REPLACE INTO contacts (uid, contact)
244 SELECT {?}, id
245 FROM aliases
246 WHERE alias = {?}', $uid, $login))
247 {
248 $page->trig('Contact ajouté !');
249 } else {
250 $page->trig('Contact déjà dans la liste !');
251 }
252 }
253 }
254 } elseif (Env::v('action')) {
255 $page->trig("La modification du contact a échouée, merci de réessayer.");
256 }
257
258 $search = false;
259 if ($action == 'search') {
260 $action = $subaction;
261 $subaction = $ssaction;
262 $search = true;
263 }
264 if ($search && trim(Env::v('quick'))) {
265 require_once 'userset.inc.php';
266 $base = 'carnet/contacts/search';
267
268 require_once(dirname(__FILE__) . '/search/classes.inc.php');
269 ThrowError::$throwHook = array($this, 'searchErrorHandler');
270 $view = new SearchSet(true, false, "INNER JOIN contacts AS c2 ON (u.user_id = c2.contact)", "c2.uid = $uid");
271 } else {
272 $base = 'carnet/contacts';
273 $view = new UserSet("INNER JOIN contacts AS c2 ON (u.user_id = c2.contact)", " c2.uid = $uid ");
274 }
275 $view->addMod('minifiche', 'Mini-Fiches', true);
276 $view->addMod('trombi', 'Trombinoscope', false, array('with_admin' => false, 'with_promo' => true));
277 $view->addMod('geoloc', 'Planisphère', false, array('with_annu' => 'carnet/contacts/search'));
278 $view->apply($base, $page, $action, $subaction);
279 if ($action != 'geoloc' || ($search && !$ssaction) || (!$search && !$subaction)) {
280 $page->changeTpl('carnet/mescontacts.tpl');
281 }
282 }
283
284 function handler_pdf(&$page, $arg0 = null, $arg1 = null)
285 {
286 require_once dirname(__FILE__).'/carnet/contacts.pdf.inc.php';
287 require_once 'user.func.inc.php';
288
289 session_write_close();
290
291 $sql = "SELECT a.alias
292 FROM aliases AS a
293 INNER JOIN auth_user_md5 AS u ON ( a.id = u.user_id )
294 INNER JOIN contacts AS c ON ( a.id = c.contact )
295 WHERE c.uid = {?} AND a.type='a_vie'";
296 if ($arg0 == 'promo') {
297 $sql .= ' ORDER BY u.promo, u.nom, u.prenom';
298 } else {
299 $sql .= ' ORDER BY u.nom, u.prenom, u.promo';
300 }
301
302 $citer = XDB::iterRow($sql, S::v('uid'));
303 $pdf = new ContactsPDF();
304
305 while (list($alias) = $citer->next()) {
306 $user = get_user_details($alias);
307 foreach ($user as &$value) {
308 if (is_utf8($value)) {
309 $value = utf8_decode($value);
310 }
311 }
312 $pdf = ContactsPDF::addContact($pdf, $user, $arg0 == 'photos' || $arg1 == 'photos');
313 }
314 $pdf->Output();
315
316 exit;
317 }
318
319 function handler_rss(&$page, $user = null, $hash = null)
320 {
321 require_once 'rss.inc.php';
322 require_once 'notifs.inc.php';
323
324 $uid = init_rss('carnet/rss.tpl', $user, $hash);
325 $notifs = new Notifs($uid, false);
326 $page->assign('notifs', $notifs);
327 }
328
329 function handler_ical(&$page, $alias = null, $hash = null)
330 {
331 require_once 'rss.inc.php';
332 $uid = init_rss(null, $alias, $hash, false);
333 if (S::logged()) {
334 if (!$uid) {
335 $uid = S::i('uid');
336 } else if ($uid != S::i('uid')) {
337 require_once 'xorg.misc.inc.php';
338 send_warning_email("Récupération d\'un autre utilisateur ($uid)");
339 }
340 } else if (!$uid) {
341 exit;
342 }
343 require_once 'ical.inc.php';
344 $page->changeTpl('carnet/calendar.tpl', NO_SKIN);
345 $page->register_function('display_ical', 'display_ical');
346
347 $res = XDB::iterRow(
348 'SELECT u.prenom,
349 IF(u.nom_usage = \'\',u.nom,u.nom_usage) AS nom,
350 u.promo,
351 u.naissance,
352 DATE_ADD(u.naissance, INTERVAL 1 DAY) AS end,
353 u.date_ins,
354 a.alias AS forlife
355 FROM contacts AS c
356 INNER JOIN auth_user_md5 AS u ON (u.user_id = c.contact)
357 INNER JOIN aliases AS a ON (u.user_id = a.id AND a.type = \'a_vie\')
358 WHERE c.uid = {?}', $uid);
359
360 $annivs = Array();
361 while (list($prenom, $nom, $promo, $naissance, $end, $ts, $forlife) = $res->next()) {
362 $naissance = str_replace('-', '', $naissance);
363 $end = str_replace('-', '', $end);
364 $annivs[] = array(
365 'timestamp' => strtotime($ts),
366 'date' => $naissance,
367 'tomorrow' => $end,
368 'forlife' => $forlife,
369 'summary' => 'Anniversaire de '.$prenom
370 .' '.$nom.' - x '.$promo,
371 );
372 }
373 $page->assign('events', $annivs);
374
375 header('Content-Type: text/calendar; charset=utf-8');
376 }
377
378 function handler_vcard(&$page, $photos = null)
379 {
380 $res = XDB::query('SELECT contact
381 FROM contacts
382 WHERE uid = {?}', S::v('uid'));
383 $vcard = new VCard($res->fetchColumn(), $photos == 'photos');
384 $vcard->do_page(&$page);
385 }
386 }
387
388 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
389 ?>