Commit | Line | Data |
---|---|---|
0337d704 | 1 | <?php |
2 | /*************************************************************************** | |
8d84c630 | 3 | * Copyright (C) 2003-2009 Polytechnique.org * |
0337d704 | 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 | ||
c9f82d49 | 22 | class CarnetModule extends PLModule |
23 | { | |
24 | function handlers() | |
25 | { | |
26 | return array( | |
5e193297 | 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), | |
fc12cbd1 | 30 | |
5e193297 | 31 | 'carnet/contacts' => $this->make_hook('contacts', AUTH_COOKIE), |
8fc4efa3 | 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), | |
b48a0758 | 35 | |
8fc4efa3 | 36 | 'carnet/rss' => $this->make_hook('rss', AUTH_PUBLIC, 'user', NO_HTTPS), |
c9f82d49 | 37 | ); |
38 | } | |
0337d704 | 39 | |
fc12cbd1 | 40 | function _add_rss_link(&$page) |
41 | { | |
84fc72ff | 42 | if (!S::hasAuthToken()) { |
fc12cbd1 | 43 | return; |
fd8f77de | 44 | } |
162370e7 | 45 | $page->setRssLink('Polytechnique.org :: Carnet', |
84fc72ff | 46 | '/carnet/rss/'.S::v('hruid').'/'.S::v('token').'/rss.xml'); |
fc12cbd1 | 47 | } |
48 | ||
b48a0758 | 49 | function handler_index(&$page) |
50 | { | |
51 | $page->changeTpl('carnet/index.tpl'); | |
46f272fe | 52 | $page->setTitle('Mon carnet'); |
b48a0758 | 53 | $this->_add_rss_link($page); |
b48a0758 | 54 | } |
55 | ||
fc12cbd1 | 56 | function handler_panel(&$page) |
57 | { | |
58 | $page->changeTpl('carnet/panel.tpl'); | |
59 | ||
60 | if (Get::has('read')) { | |
009b8ab7 FB |
61 | XDB::execute('UPDATE watch |
62 | SET last = FROM_UNIXTIME({?}) | |
63 | WHERE uid = {?}', | |
64 | Get::i('read'), S::i('uid')); | |
65 | S::set('watch_last', Get::i('read')); | |
ebfdf077 | 66 | Platal::session()->updateNbNotifs(); |
8b00e0e0 | 67 | pl_redirect('carnet/panel'); |
fc12cbd1 | 68 | } |
69 | ||
70 | require_once 'notifs.inc.php'; | |
009b8ab7 | 71 | $page->assign('now', time()); |
fc12cbd1 | 72 | |
009b8ab7 FB |
73 | $user = S::user(); |
74 | $notifs = Watch::getEvents($user, time() - (7 * 86400)); | |
fc12cbd1 | 75 | $page->assign('notifs', $notifs); |
76 | $page->assign('today', date('Y-m-d')); | |
77 | $this->_add_rss_link($page); | |
fc12cbd1 | 78 | } |
79 | ||
4e7bf1e0 | 80 | private function getSinglePromotion(PlPage &$page, $promo) |
b48a0758 | 81 | { |
4e7bf1e0 FB |
82 | if (!ctype_digit($promo) || $promo < 1920 || $promo > date('Y')) { |
83 | $page->trigError('Promotion invalide : ' . $promo); | |
84 | return null; | |
85 | } | |
86 | return (int)$promo; | |
87 | } | |
88 | ||
89 | private function getPromo(PlPage &$page, $promo) | |
90 | { | |
91 | if (strpos($promo, '-') === false) { | |
92 | $promo = $this->getSinglePromotion($page, $promo); | |
93 | if (!$promo) { | |
94 | return null; | |
b48a0758 | 95 | } else { |
4e7bf1e0 | 96 | return array($promo); |
b48a0758 | 97 | } |
b48a0758 | 98 | } |
4e7bf1e0 FB |
99 | |
100 | list($promo1, $promo2) = explode('-', $promo); | |
101 | $promo1 = $this->getSinglePromotion($page, $promo1); | |
102 | if (!$promo1) { | |
103 | return null; | |
104 | } | |
105 | $promo2 = $this->getSinglePromotion($page, $promo2); | |
106 | if (!$promo2) { | |
107 | return null; | |
108 | } | |
109 | if ($promo1 > $promo2) { | |
110 | $page->trigError("Intervale non valide : " . $promo); | |
111 | return null; | |
112 | } | |
113 | $array = array(); | |
114 | for ($i = $promo1 ; $i <= $promo2 ; ++$i) { | |
115 | $array[] = $i; | |
116 | } | |
117 | return $array; | |
b48a0758 | 118 | } |
119 | ||
4e7bf1e0 | 120 | private function addPromo(PlPage &$page, $promo) |
b48a0758 | 121 | { |
4e7bf1e0 FB |
122 | $promos = $this->getPromo($page, $promo); |
123 | if (!$promos || count($promos) == 0) { | |
124 | return; | |
125 | } | |
126 | $to_add = array(); | |
127 | foreach ($promos as $promo) { | |
128 | $to_add[] = XDB::format('({?}, {?})', S::i('uid'), $promo); | |
129 | } | |
130 | XDB::execute('INSERT IGNORE INTO watch_promo (uid, promo) | |
131 | VALUES ' . implode(', ', $to_add)); | |
132 | } | |
b48a0758 | 133 | |
4e7bf1e0 FB |
134 | private function delPromo(PlPage &$page, $promo) |
135 | { | |
136 | $promos = $this->getPromo($page, $promo); | |
137 | if (!$promos || count($promos) == 0) { | |
138 | return; | |
139 | } | |
140 | $to_delete = array(); | |
141 | foreach ($promos as $promo) { | |
142 | $to_delete[] = XDB::format('{?}', $promo); | |
143 | } | |
144 | XDB::execute('DELETE FROM watch_promo | |
145 | WHERE ' . XDB::format('uid = {?}', S::i('uid')) . ' | |
146 | AND promo IN (' . implode(', ', $to_delete) . ')'); | |
147 | } | |
148 | ||
149 | public function addNonRegistered(PlPage &$page, PlUser &$user) | |
150 | { | |
151 | XDB::execute('INSERT IGNORE INTO watch_nonins (uid, ni_id) | |
152 | VALUES ({?}, {?})', S::i('uid'), $user->id()); | |
153 | } | |
b48a0758 | 154 | |
4e7bf1e0 FB |
155 | public function delNonRegistered(PlPage &$page, PlUser &$user) |
156 | { | |
157 | XDB::execute('DELETE FROM watch_nonins | |
158 | WHERE uid = {?} AND ni_id = {?}', | |
159 | S::i('uid'), $user->id()); | |
160 | } | |
b48a0758 | 161 | |
4e7bf1e0 FB |
162 | public function handler_notifs(&$page, $action = null, $arg = null) |
163 | { | |
164 | $page->changeTpl('carnet/notifs.tpl'); | |
b48a0758 | 165 | |
40d428d8 VZ |
166 | if ($action) { |
167 | S::assert_xsrf_token(); | |
4e7bf1e0 FB |
168 | switch ($action) { |
169 | case 'add_promo': | |
170 | $this->addPromo($page, $arg); | |
171 | break; | |
b48a0758 | 172 | |
4e7bf1e0 FB |
173 | case 'del_promo': |
174 | $this->delPromo($page, $arg); | |
175 | break; | |
b48a0758 | 176 | |
4e7bf1e0 FB |
177 | case 'del_nonins': |
178 | $user = User::get($arg); | |
179 | if ($user) { | |
180 | $this->delNonRegistered($page, $user); | |
181 | } | |
182 | break; | |
183 | ||
184 | case 'add_nonins': | |
185 | $user = User::get($arg); | |
186 | if ($user) { | |
187 | $this->addNonRegistered($page, $user); | |
188 | } | |
189 | break; | |
190 | } | |
b48a0758 | 191 | } |
192 | ||
40d428d8 VZ |
193 | if (Env::has('subs')) { |
194 | S::assert_xsrf_token(); | |
4e7bf1e0 FB |
195 | $flags = new PlFlagSet(); |
196 | foreach (Env::v('sub') as $key=>$value) { | |
197 | $flags->addFlag($key, $value); | |
198 | } | |
199 | XDB::execute('UPDATE watch | |
200 | SET actions = {?} | |
201 | WHERE uid = {?}', $flags, S::i('uid')); | |
e6bf9216 VZ |
202 | } |
203 | ||
40d428d8 VZ |
204 | if (Env::has('flags_contacts')) { |
205 | S::assert_xsrf_token(); | |
4e7bf1e0 FB |
206 | XDB::execute('UPDATE watch |
207 | SET ' . XDB::changeFlag('flags', 'contacts', Env::b('contacts')) . ' | |
7e735012 | 208 | WHERE uid = {?}', S::i('uid')); |
b48a0758 | 209 | } |
e6bf9216 | 210 | |
40d428d8 VZ |
211 | if (Env::has('flags_mail')) { |
212 | S::assert_xsrf_token(); | |
4e7bf1e0 FB |
213 | XDB::execute('UPDATE watch |
214 | SET ' . XDB::changeFlag('flags', 'mail', Env::b('mail')) . ' | |
7e735012 | 215 | WHERE uid = {?}', S::i('uid')); |
4e7bf1e0 FB |
216 | } |
217 | ||
218 | $user = S::user(); | |
219 | $nonins = new UserFilter(new UFC_WatchRegistration($user)); | |
220 | ||
221 | $promo = XDB::fetchColumn('SELECT promo | |
222 | FROM watch_promo | |
223 | WHERE uid = {?} | |
224 | ORDER BY promo', S::i('uid')); | |
225 | $page->assign('promo_count', count($promo)); | |
226 | $ranges = array(); | |
227 | $range_start = null; | |
228 | $range_end = null; | |
229 | foreach ($promo as $p) { | |
230 | if (is_null($range_start)) { | |
231 | $range_start = $range_end = $p; | |
232 | } else if ($p != $range_end + 1) { | |
233 | $ranges[] = array($range_start, $range_end); | |
234 | $range_start = $range_end = $p; | |
235 | } else { | |
236 | $range_end = $p; | |
237 | } | |
b48a0758 | 238 | } |
4e7bf1e0 FB |
239 | $ranges[] = array($range_start, $range_end); |
240 | $page->assign('promo_ranges', $ranges); | |
241 | $page->assign('nonins', $nonins->getUsers()); | |
b48a0758 | 242 | |
4e7bf1e0 FB |
243 | list($flags, $actions) = XDB::fetchOneRow('SELECT flags, actions |
244 | FROM watch | |
245 | WHERE uid = {?}', S::i('uid')); | |
246 | $flags = new PlFlagSet($flags); | |
247 | $actions = new PlFlagSet($actions); | |
248 | $page->assign('flags', $flags); | |
249 | $page->assign('actions', $actions); | |
b48a0758 | 250 | } |
251 | ||
a2aa8436 | 252 | function handler_contacts(&$page, $action = null, $subaction = null, $ssaction = null) |
b48a0758 | 253 | { |
46f272fe | 254 | $page->setTitle('Mes contacts'); |
59a61432 | 255 | $this->_add_rss_link($page); |
b48a0758 | 256 | |
76cbe885 | 257 | $uid = S::i('uid'); |
5e2307dc | 258 | $user = Env::v('user'); |
b48a0758 | 259 | |
e6bf9216 VZ |
260 | // For XSRF protection, checks both the normal xsrf token, and the special RSS token. |
261 | // It allows direct linking to contact adding in the RSS feed. | |
84fc72ff | 262 | if (Env::v('action') && Env::v('token') !== S::v('token')) { |
40d428d8 VZ |
263 | S::assert_xsrf_token(); |
264 | } | |
265 | switch (Env::v('action')) { | |
266 | case 'retirer': | |
d6460651 VZ |
267 | if (($user = User::get(Env::v('user')))) { |
268 | if (XDB::execute("DELETE FROM contacts | |
269 | WHERE uid = {?} AND contact = {?}", $uid, $user->id())) { | |
a7d35093 | 270 | $page->trigSuccess("Contact retiré !"); |
b48a0758 | 271 | } |
272 | } | |
273 | break; | |
274 | ||
40d428d8 | 275 | case 'ajouter': |
d6460651 VZ |
276 | if (($user = User::get(Env::v('user')))) { |
277 | if (XDB::execute("REPLACE INTO contacts (uid, contact) | |
278 | VALUES ({?}, {?})", $uid, $user->id())) { | |
a7d35093 | 279 | $page->trigSuccess('Contact ajouté !'); |
b48a0758 | 280 | } else { |
a7d35093 | 281 | $page->trigWarning('Contact déjà dans la liste !'); |
b48a0758 | 282 | } |
283 | } | |
d6460651 | 284 | break; |
b48a0758 | 285 | } |
286 | ||
76cbe885 | 287 | /* $search = false; |
a2aa8436 | 288 | if ($action == 'search') { |
289 | $action = $subaction; | |
290 | $subaction = $ssaction; | |
291 | $search = true; | |
292 | } | |
293 | if ($search && trim(Env::v('quick'))) { | |
a2aa8436 | 294 | $base = 'carnet/contacts/search'; |
3b2f9d11 | 295 | |
adb07f6f | 296 | Platal::load('search', 'classes.inc.php'); |
3b2f9d11 | 297 | ThrowError::$throwHook = array($this, 'searchErrorHandler'); |
298 | $view = new SearchSet(true, false, "INNER JOIN contacts AS c2 ON (u.user_id = c2.contact)", "c2.uid = $uid"); | |
a2aa8436 | 299 | } else { |
300 | $base = 'carnet/contacts'; | |
301 | $view = new UserSet("INNER JOIN contacts AS c2 ON (u.user_id = c2.contact)", " c2.uid = $uid "); | |
76cbe885 FB |
302 | }*/ |
303 | ||
304 | require_once 'userset.inc.php'; | |
305 | $user = S::user(); | |
306 | $view = new UserSet(new UFC_Contact($user)); | |
1fae7605 | 307 | $view->addMod('minifiche', 'Mini-fiches', true); |
8c4a0c30 | 308 | $view->addMod('trombi', 'Trombinoscope', false, array('with_admin' => false, 'with_promo' => true)); |
a2aa8436 | 309 | $view->addMod('geoloc', 'Planisphère', false, array('with_annu' => 'carnet/contacts/search')); |
76cbe885 FB |
310 | $view->apply('carnet/contacts', $page, $action, $subaction); |
311 | //if ($action != 'geoloc' || ($search && !$ssaction) || (!$search && !$subaction)) { | |
312 | $page->changeTpl('carnet/mescontacts.tpl'); | |
313 | //} | |
b48a0758 | 314 | } |
315 | ||
316 | function handler_pdf(&$page, $arg0 = null, $arg1 = null) | |
317 | { | |
460d8f55 | 318 | $this->load('contacts.pdf.inc.php'); |
b48a0758 | 319 | require_once 'user.func.inc.php'; |
320 | ||
732e5855 | 321 | Platal::session()->close(); |
b48a0758 | 322 | |
323 | $sql = "SELECT a.alias | |
324 | FROM aliases AS a | |
325 | INNER JOIN auth_user_md5 AS u ON ( a.id = u.user_id ) | |
326 | INNER JOIN contacts AS c ON ( a.id = c.contact ) | |
327 | WHERE c.uid = {?} AND a.type='a_vie'"; | |
328 | if ($arg0 == 'promo') { | |
329 | $sql .= ' ORDER BY u.promo, u.nom, u.prenom'; | |
330 | } else { | |
331 | $sql .= ' ORDER BY u.nom, u.prenom, u.promo'; | |
332 | } | |
333 | ||
cab08090 | 334 | $citer = XDB::iterRow($sql, S::v('uid')); |
b48a0758 | 335 | $pdf = new ContactsPDF(); |
336 | ||
337 | while (list($alias) = $citer->next()) { | |
338 | $user = get_user_details($alias); | |
ac73e294 | 339 | foreach ($user as &$value) { |
340 | if (is_utf8($value)) { | |
341 | $value = utf8_decode($value); | |
342 | } | |
343 | } | |
93c099e1 | 344 | $pdf = ContactsPDF::addContact($pdf, $user, $arg0 == 'photos' || $arg1 == 'photos'); |
b48a0758 | 345 | } |
346 | $pdf->Output(); | |
347 | ||
348 | exit; | |
349 | } | |
350 | ||
c9f82d49 | 351 | function handler_rss(&$page, $user = null, $hash = null) |
352 | { | |
460d8f55 | 353 | $this->load('feed.inc.php'); |
4e9a7a6d FB |
354 | $feed = new CarnetFeed(); |
355 | return $feed->run($page, $user, $hash); | |
c9f82d49 | 356 | } |
fbfb06dc | 357 | |
963c0b2e | 358 | function handler_ical(&$page, $alias = null, $hash = null) |
fbfb06dc | 359 | { |
f3e66b56 FB |
360 | $user = Platal::session()->tokenAuth($alias, $hash); |
361 | if (is_null($user)) { | |
362 | if (S::logged()) { | |
363 | $user == S::user(); | |
364 | } else { | |
365 | return PL_FORBIDDEN; | |
963c0b2e | 366 | } |
963c0b2e | 367 | } |
5406bb88 | 368 | |
99544d53 | 369 | require_once 'ical.inc.php'; |
801fcad8 | 370 | $page->changeTpl('carnet/calendar.tpl', NO_SKIN); |
3585b0c8 | 371 | $page->register_function('display_ical', 'display_ical'); |
fbfb06dc | 372 | |
f3b3f363 | 373 | $res = XDB::iterRow( |
374 | 'SELECT u.prenom, | |
375 | IF(u.nom_usage = \'\',u.nom,u.nom_usage) AS nom, | |
376 | u.promo, | |
377 | u.naissance, | |
378 | DATE_ADD(u.naissance, INTERVAL 1 DAY) AS end, | |
379 | u.date_ins, | |
5406bb88 | 380 | u.hruid |
f3b3f363 | 381 | FROM contacts AS c |
382 | INNER JOIN auth_user_md5 AS u ON (u.user_id = c.contact) | |
383 | INNER JOIN aliases AS a ON (u.user_id = a.id AND a.type = \'a_vie\') | |
f3e66b56 | 384 | WHERE c.uid = {?}', $user->id()); |
f3b3f363 | 385 | |
386 | $annivs = Array(); | |
5406bb88 | 387 | while (list($prenom, $nom, $promo, $naissance, $end, $ts, $hruid) = $res->next()) { |
f3b3f363 | 388 | $naissance = str_replace('-', '', $naissance); |
389 | $end = str_replace('-', '', $end); | |
390 | $annivs[] = array( | |
391 | 'timestamp' => strtotime($ts), | |
392 | 'date' => $naissance, | |
393 | 'tomorrow' => $end, | |
5406bb88 | 394 | 'hruid' => $hruid, |
f3b3f363 | 395 | 'summary' => 'Anniversaire de '.$prenom |
396 | .' '.$nom.' - x '.$promo, | |
397 | ); | |
fbfb06dc | 398 | } |
f3b3f363 | 399 | $page->assign('events', $annivs); |
fbfb06dc | 400 | |
401 | header('Content-Type: text/calendar; charset=utf-8'); | |
fbfb06dc | 402 | } |
5e193297 | 403 | |
917c4d11 | 404 | function handler_vcard(&$page, $photos = null) |
5e193297 | 405 | { |
406 | $res = XDB::query('SELECT contact | |
407 | FROM contacts | |
408 | WHERE uid = {?}', S::v('uid')); | |
5d42c993 FB |
409 | $vcard = new VCard($photos == 'photos'); |
410 | $vcard->addUsers($res->fetchColumn()); | |
411 | $vcard->show(); | |
5e193297 | 412 | } |
4da0b8d7 | 413 | } |
c9f82d49 | 414 | |
a7de4ef7 | 415 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
c9f82d49 | 416 | ?> |