Commit | Line | Data |
---|---|---|
0337d704 | 1 | <?php |
2 | /*************************************************************************** | |
5e1513f6 | 3 | * Copyright (C) 2003-2011 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( | |
5ae3e9a9 SJ |
27 | 'carnet' => $this->make_hook('index', AUTH_COOKIE, 'directory_private'), |
28 | 'carnet/panel' => $this->make_hook('panel', AUTH_COOKIE, 'directory_private'), | |
29 | 'carnet/notifs' => $this->make_hook('notifs', AUTH_COOKIE, 'directory_private'), | |
190efb3a | 30 | |
5ae3e9a9 SJ |
31 | 'carnet/contacts' => $this->make_hook('contacts', AUTH_COOKIE, 'directory_private'), |
32 | 'carnet/contacts/pdf' => $this->make_hook('pdf', AUTH_COOKIE, 'directory_private'), | |
33 | 'carnet/contacts/vcard' => $this->make_hook('vcard', AUTH_COOKIE, 'directory_private'), | |
34 | 'carnet/contacts/ical' => $this->make_token_hook('ical', AUTH_COOKIE, 'directory_private'), | |
35 | 'carnet/contacts/csv' => $this->make_token_hook('csv', AUTH_COOKIE, 'directory_private'), | |
190efb3a VZ |
36 | 'carnet/contacts/csv/birthday' => $this->make_token_hook('csv_birthday', AUTH_COOKIE, 'directory_private'), |
37 | ||
5ae3e9a9 | 38 | 'carnet/rss' => $this->make_token_hook('rss', AUTH_COOKIE, 'directory_private'), |
c9f82d49 | 39 | ); |
40 | } | |
0337d704 | 41 | |
26ba053e | 42 | function _add_rss_link($page) |
fc12cbd1 | 43 | { |
84fc72ff | 44 | if (!S::hasAuthToken()) { |
fc12cbd1 | 45 | return; |
fd8f77de | 46 | } |
162370e7 | 47 | $page->setRssLink('Polytechnique.org :: Carnet', |
2ab3486b | 48 | '/carnet/rss/' . S::v('hruid') . '/' . S::user()->token . '/rss.xml'); |
fc12cbd1 | 49 | } |
50 | ||
26ba053e | 51 | function handler_index($page) |
b48a0758 | 52 | { |
53 | $page->changeTpl('carnet/index.tpl'); | |
46f272fe | 54 | $page->setTitle('Mon carnet'); |
b48a0758 | 55 | $this->_add_rss_link($page); |
b48a0758 | 56 | } |
57 | ||
26ba053e | 58 | function handler_panel($page) |
fc12cbd1 | 59 | { |
60 | $page->changeTpl('carnet/panel.tpl'); | |
61 | ||
62 | if (Get::has('read')) { | |
009b8ab7 FB |
63 | XDB::execute('UPDATE watch |
64 | SET last = FROM_UNIXTIME({?}) | |
65 | WHERE uid = {?}', | |
66 | Get::i('read'), S::i('uid')); | |
069ddda8 | 67 | S::user()->invalidWatchCache(); |
ebfdf077 | 68 | Platal::session()->updateNbNotifs(); |
8b00e0e0 | 69 | pl_redirect('carnet/panel'); |
fc12cbd1 | 70 | } |
71 | ||
72 | require_once 'notifs.inc.php'; | |
009b8ab7 | 73 | $page->assign('now', time()); |
fc12cbd1 | 74 | |
009b8ab7 FB |
75 | $user = S::user(); |
76 | $notifs = Watch::getEvents($user, time() - (7 * 86400)); | |
fc12cbd1 | 77 | $page->assign('notifs', $notifs); |
78 | $page->assign('today', date('Y-m-d')); | |
79 | $this->_add_rss_link($page); | |
fc12cbd1 | 80 | } |
81 | ||
26ba053e | 82 | private function getSinglePromotion(PlPage $page, $promo) |
b48a0758 | 83 | { |
4aae4d2c | 84 | if (!(is_int($promo) || ctype_digit($promo)) || $promo < 1920 || $promo > date('Y')) { |
e46cf8c4 | 85 | $page->trigError('Promotion invalide : ' . $promo . '.'); |
4e7bf1e0 FB |
86 | return null; |
87 | } | |
88 | return (int)$promo; | |
89 | } | |
90 | ||
26ba053e | 91 | private function getPromo(PlPage $page, $promo) |
4e7bf1e0 FB |
92 | { |
93 | if (strpos($promo, '-') === false) { | |
94 | $promo = $this->getSinglePromotion($page, $promo); | |
95 | if (!$promo) { | |
96 | return null; | |
b48a0758 | 97 | } else { |
4e7bf1e0 | 98 | return array($promo); |
b48a0758 | 99 | } |
b48a0758 | 100 | } |
4e7bf1e0 FB |
101 | |
102 | list($promo1, $promo2) = explode('-', $promo); | |
103 | $promo1 = $this->getSinglePromotion($page, $promo1); | |
104 | if (!$promo1) { | |
105 | return null; | |
106 | } | |
107 | $promo2 = $this->getSinglePromotion($page, $promo2); | |
108 | if (!$promo2) { | |
109 | return null; | |
110 | } | |
111 | if ($promo1 > $promo2) { | |
e46cf8c4 | 112 | $page->trigError('Intervalle non valide : ' . $promo . '.'); |
4e7bf1e0 FB |
113 | return null; |
114 | } | |
115 | $array = array(); | |
116 | for ($i = $promo1 ; $i <= $promo2 ; ++$i) { | |
117 | $array[] = $i; | |
118 | } | |
119 | return $array; | |
b48a0758 | 120 | } |
121 | ||
26ba053e | 122 | private function addPromo(PlPage $page, $promo) |
b48a0758 | 123 | { |
4e7bf1e0 FB |
124 | $promos = $this->getPromo($page, $promo); |
125 | if (!$promos || count($promos) == 0) { | |
126 | return; | |
127 | } | |
128 | $to_add = array(); | |
129 | foreach ($promos as $promo) { | |
130 | $to_add[] = XDB::format('({?}, {?})', S::i('uid'), $promo); | |
131 | } | |
132 | XDB::execute('INSERT IGNORE INTO watch_promo (uid, promo) | |
133 | VALUES ' . implode(', ', $to_add)); | |
069ddda8 FB |
134 | S::user()->invalidWatchCache(); |
135 | Platal::session()->updateNbNotifs(); | |
4e7bf1e0 | 136 | } |
b48a0758 | 137 | |
26ba053e | 138 | private function delPromo(PlPage $page, $promo) |
4e7bf1e0 FB |
139 | { |
140 | $promos = $this->getPromo($page, $promo); | |
141 | if (!$promos || count($promos) == 0) { | |
142 | return; | |
143 | } | |
144 | $to_delete = array(); | |
145 | foreach ($promos as $promo) { | |
146 | $to_delete[] = XDB::format('{?}', $promo); | |
147 | } | |
148 | XDB::execute('DELETE FROM watch_promo | |
149 | WHERE ' . XDB::format('uid = {?}', S::i('uid')) . ' | |
150 | AND promo IN (' . implode(', ', $to_delete) . ')'); | |
069ddda8 FB |
151 | S::user()->invalidWatchCache(); |
152 | Platal::session()->updateNbNotifs(); | |
4e7bf1e0 FB |
153 | } |
154 | ||
26ba053e | 155 | public function addNonRegistered(PlPage $page, PlUser $user) |
4e7bf1e0 FB |
156 | { |
157 | XDB::execute('INSERT IGNORE INTO watch_nonins (uid, ni_id) | |
158 | VALUES ({?}, {?})', S::i('uid'), $user->id()); | |
069ddda8 FB |
159 | S::user()->invalidWatchCache(); |
160 | Platal::session()->updateNbNotifs(); | |
4e7bf1e0 | 161 | } |
b48a0758 | 162 | |
26ba053e | 163 | public function delNonRegistered(PlPage $page, PlUser $user) |
4e7bf1e0 FB |
164 | { |
165 | XDB::execute('DELETE FROM watch_nonins | |
166 | WHERE uid = {?} AND ni_id = {?}', | |
167 | S::i('uid'), $user->id()); | |
069ddda8 FB |
168 | S::user()->invalidWatchCache(); |
169 | Platal::session()->updateNbNotifs(); | |
4e7bf1e0 | 170 | } |
b48a0758 | 171 | |
26ba053e | 172 | public function handler_notifs($page, $action = null, $arg = null) |
4e7bf1e0 FB |
173 | { |
174 | $page->changeTpl('carnet/notifs.tpl'); | |
b48a0758 | 175 | |
40d428d8 VZ |
176 | if ($action) { |
177 | S::assert_xsrf_token(); | |
4e7bf1e0 FB |
178 | switch ($action) { |
179 | case 'add_promo': | |
180 | $this->addPromo($page, $arg); | |
181 | break; | |
b48a0758 | 182 | |
4e7bf1e0 FB |
183 | case 'del_promo': |
184 | $this->delPromo($page, $arg); | |
185 | break; | |
b48a0758 | 186 | |
4e7bf1e0 FB |
187 | case 'del_nonins': |
188 | $user = User::get($arg); | |
189 | if ($user) { | |
190 | $this->delNonRegistered($page, $user); | |
191 | } | |
192 | break; | |
193 | ||
194 | case 'add_nonins': | |
195 | $user = User::get($arg); | |
196 | if ($user) { | |
197 | $this->addNonRegistered($page, $user); | |
198 | } | |
199 | break; | |
200 | } | |
b48a0758 | 201 | } |
202 | ||
40d428d8 VZ |
203 | if (Env::has('subs')) { |
204 | S::assert_xsrf_token(); | |
4e7bf1e0 FB |
205 | $flags = new PlFlagSet(); |
206 | foreach (Env::v('sub') as $key=>$value) { | |
207 | $flags->addFlag($key, $value); | |
208 | } | |
209 | XDB::execute('UPDATE watch | |
210 | SET actions = {?} | |
211 | WHERE uid = {?}', $flags, S::i('uid')); | |
069ddda8 FB |
212 | S::user()->invalidWatchCache(); |
213 | Platal::session()->updateNbNotifs(); | |
e6bf9216 VZ |
214 | } |
215 | ||
40d428d8 VZ |
216 | if (Env::has('flags_contacts')) { |
217 | S::assert_xsrf_token(); | |
4e7bf1e0 FB |
218 | XDB::execute('UPDATE watch |
219 | SET ' . XDB::changeFlag('flags', 'contacts', Env::b('contacts')) . ' | |
7e735012 | 220 | WHERE uid = {?}', S::i('uid')); |
069ddda8 FB |
221 | S::user()->invalidWatchCache(); |
222 | Platal::session()->updateNbNotifs(); | |
b48a0758 | 223 | } |
e6bf9216 | 224 | |
40d428d8 VZ |
225 | if (Env::has('flags_mail')) { |
226 | S::assert_xsrf_token(); | |
4e7bf1e0 FB |
227 | XDB::execute('UPDATE watch |
228 | SET ' . XDB::changeFlag('flags', 'mail', Env::b('mail')) . ' | |
7e735012 | 229 | WHERE uid = {?}', S::i('uid')); |
069ddda8 FB |
230 | S::user()->invalidWatchCache(); |
231 | Platal::session()->updateNbNotifs(); | |
4e7bf1e0 FB |
232 | } |
233 | ||
234 | $user = S::user(); | |
235 | $nonins = new UserFilter(new UFC_WatchRegistration($user)); | |
236 | ||
237 | $promo = XDB::fetchColumn('SELECT promo | |
238 | FROM watch_promo | |
239 | WHERE uid = {?} | |
240 | ORDER BY promo', S::i('uid')); | |
241 | $page->assign('promo_count', count($promo)); | |
242 | $ranges = array(); | |
243 | $range_start = null; | |
244 | $range_end = null; | |
245 | foreach ($promo as $p) { | |
246 | if (is_null($range_start)) { | |
247 | $range_start = $range_end = $p; | |
248 | } else if ($p != $range_end + 1) { | |
249 | $ranges[] = array($range_start, $range_end); | |
250 | $range_start = $range_end = $p; | |
251 | } else { | |
252 | $range_end = $p; | |
253 | } | |
b48a0758 | 254 | } |
4e7bf1e0 FB |
255 | $ranges[] = array($range_start, $range_end); |
256 | $page->assign('promo_ranges', $ranges); | |
257 | $page->assign('nonins', $nonins->getUsers()); | |
b48a0758 | 258 | |
4e7bf1e0 FB |
259 | list($flags, $actions) = XDB::fetchOneRow('SELECT flags, actions |
260 | FROM watch | |
261 | WHERE uid = {?}', S::i('uid')); | |
262 | $flags = new PlFlagSet($flags); | |
263 | $actions = new PlFlagSet($actions); | |
264 | $page->assign('flags', $flags); | |
265 | $page->assign('actions', $actions); | |
b48a0758 | 266 | } |
267 | ||
26ba053e | 268 | function handler_contacts($page, $action = null, $subaction = null, $ssaction = null) |
b48a0758 | 269 | { |
46f272fe | 270 | $page->setTitle('Mes contacts'); |
59a61432 | 271 | $this->_add_rss_link($page); |
b48a0758 | 272 | |
76cbe885 | 273 | $uid = S::i('uid'); |
206cb50c | 274 | $user = S::user(); |
b48a0758 | 275 | |
e6bf9216 VZ |
276 | // For XSRF protection, checks both the normal xsrf token, and the special RSS token. |
277 | // It allows direct linking to contact adding in the RSS feed. | |
2ab3486b | 278 | if (Env::v('action') && Env::v('token') !== S::user()->token) { |
40d428d8 VZ |
279 | S::assert_xsrf_token(); |
280 | } | |
281 | switch (Env::v('action')) { | |
282 | case 'retirer': | |
ba4ddab6 | 283 | if (($contact = Profile::get(Env::v('user')))) { |
d6460651 | 284 | if (XDB::execute("DELETE FROM contacts |
c1540692 FB |
285 | WHERE uid = {?} AND contact = {?}", |
286 | $uid, $contact->id())) { | |
05d14860 | 287 | Platal::session()->updateNbNotifs(); |
6bb2f79a | 288 | $page->trigSuccess("Contact retiré !"); |
b48a0758 | 289 | } |
290 | } | |
291 | break; | |
292 | ||
40d428d8 | 293 | case 'ajouter': |
ba4ddab6 | 294 | if (($contact = Profile::get(Env::v('user')))) { |
00ba8a74 SJ |
295 | XDB::execute('INSERT IGNORE INTO contacts (uid, contact) |
296 | VALUES ({?}, {?})', | |
297 | $uid, $contact->id()); | |
298 | if (XDB::affectedRows() > 0) { | |
05d14860 | 299 | Platal::session()->updateNbNotifs(); |
6bb2f79a | 300 | $page->trigSuccess('Contact ajouté !'); |
b48a0758 | 301 | } else { |
6bb2f79a | 302 | $page->trigWarning('Contact déjà dans la liste !'); |
b48a0758 | 303 | } |
304 | } | |
d6460651 | 305 | break; |
b48a0758 | 306 | } |
307 | ||
355a5f15 | 308 | $search = false; |
355a5f15 RB |
309 | |
310 | require_once 'userset.inc.php'; | |
311 | ||
a2aa8436 | 312 | if ($action == 'search') { |
313 | $action = $subaction; | |
314 | $subaction = $ssaction; | |
315 | $search = true; | |
316 | } | |
317 | if ($search && trim(Env::v('quick'))) { | |
a2aa8436 | 318 | $base = 'carnet/contacts/search'; |
78a47eb4 | 319 | $view = new QuickSearchSet(new UFC_Contact($user)); |
a2aa8436 | 320 | } else { |
321 | $base = 'carnet/contacts'; | |
355a5f15 RB |
322 | $view = new ProfileSet(new UFC_Contact($user)); |
323 | } | |
76cbe885 | 324 | |
1fae7605 | 325 | $view->addMod('minifiche', 'Mini-fiches', true); |
8c4a0c30 | 326 | $view->addMod('trombi', 'Trombinoscope', false, array('with_admin' => false, 'with_promo' => true)); |
0a928a2f | 327 | $view->addMod('map', 'Planisphère'); |
76cbe885 | 328 | $view->apply('carnet/contacts', $page, $action, $subaction); |
76cbe885 | 329 | $page->changeTpl('carnet/mescontacts.tpl'); |
b48a0758 | 330 | } |
331 | ||
26ba053e | 332 | function handler_pdf($page, $arg0 = null, $arg1 = null) |
b48a0758 | 333 | { |
460d8f55 | 334 | $this->load('contacts.pdf.inc.php'); |
714f5437 | 335 | $user = S::user(); |
b48a0758 | 336 | |
732e5855 | 337 | Platal::session()->close(); |
b48a0758 | 338 | |
1a074f45 | 339 | $order = array(new UFO_Name()); |
b48a0758 | 340 | if ($arg0 == 'promo') { |
714f5437 | 341 | $order = array_unshift($order, new UFO_Promo()); |
b48a0758 | 342 | } else { |
714f5437 | 343 | $order[] = new UFO_Promo(); |
b48a0758 | 344 | } |
714f5437 | 345 | $filter = new UserFilter(new UFC_Contact($user), $order); |
b48a0758 | 346 | |
b48a0758 | 347 | $pdf = new ContactsPDF(); |
348 | ||
641f2115 FB |
349 | $it = $filter->iterProfiles(); |
350 | while ($p = $it->next()) { | |
714f5437 | 351 | $pdf = ContactsPDF::addContact($pdf, $p, $arg0 == 'photos' || $arg1 == 'photos'); |
b48a0758 | 352 | } |
353 | $pdf->Output(); | |
354 | ||
355 | exit; | |
356 | } | |
357 | ||
82af3fc3 | 358 | function handler_rss(PlPage $page, PlUser $user) |
c9f82d49 | 359 | { |
460d8f55 | 360 | $this->load('feed.inc.php'); |
4e9a7a6d | 361 | $feed = new CarnetFeed(); |
190efb3a | 362 | return $feed->run($page, $user); |
c9f82d49 | 363 | } |
fbfb06dc | 364 | |
ddaa62b8 FB |
365 | function buildBirthRef(Profile $profile) |
366 | { | |
367 | $date = strtotime($profile->birthdate); | |
368 | $tomorrow = $date + 86400; | |
369 | return array( | |
370 | 'timestamp' => $date, | |
371 | 'date' => date('Ymd', $date), | |
372 | 'tomorrow' => date('Ymd', $tomorrow), | |
f036c896 | 373 | 'email' => $profile->owner()->bestEmail(), |
ddaa62b8 FB |
374 | 'summary' => 'Anniversaire de ' . $profile->fullName(true) |
375 | ); | |
376 | } | |
377 | ||
82af3fc3 | 378 | function handler_csv_birthday(PlPage $page, PlUser $user) |
b3ad574d | 379 | { |
b3ad574d PC |
380 | $page->changeTpl('carnet/calendar.outlook.tpl', NO_SKIN); |
381 | $filter = new UserFilter(new UFC_Contact($user)); | |
382 | $profiles = $filter->iterProfiles(); | |
383 | $page->assign('events', PlIteratorUtils::map($profiles, array($this, 'buildBirthRef'))); | |
384 | $years = array(date("Y")); | |
385 | for ($i = 1; $i <= 10; ++$i) { | |
386 | $years[] = $years[0] + $i; | |
387 | } | |
388 | $page->assign('years', $years); | |
389 | $lang = 'fr'; | |
390 | if (preg_match('/([a-zA-Z]{2,8})($|[^a-zA-Z])/', $_SERVER['HTTP_ACCEPT_LANGUAGE'], $matches)) { | |
391 | $lang = strtolower($matches[1]); | |
392 | } | |
393 | $page->assign('lang', $lang); | |
394 | if ($lang == 'fr') { | |
395 | $encoding = 'iso8859-15'; | |
396 | } else { | |
397 | $encoding = 'utf-8'; | |
398 | } | |
399 | pl_content_headers("text/comma-separated-values;charset=".$encoding); | |
400 | } | |
ddaa62b8 | 401 | |
82af3fc3 | 402 | function handler_ical(PlPage $page, PlUser $user) |
fbfb06dc | 403 | { |
99544d53 | 404 | require_once 'ical.inc.php'; |
801fcad8 | 405 | $page->changeTpl('carnet/calendar.tpl', NO_SKIN); |
3585b0c8 | 406 | $page->register_function('display_ical', 'display_ical'); |
fbfb06dc | 407 | |
19550958 | 408 | $filter = new UserFilter(new UFC_Contact($user)); |
641f2115 | 409 | $profiles = $filter->iterProfiles(); |
ddaa62b8 | 410 | $page->assign('events', PlIteratorUtils::map($profiles, array($this, 'buildBirthRef'))); |
fbfb06dc | 411 | |
3cb500d5 | 412 | pl_content_headers("text/calendar"); |
fbfb06dc | 413 | } |
5e193297 | 414 | |
26ba053e | 415 | function handler_vcard($page, $photos = null) |
5e193297 | 416 | { |
f40e4ce5 | 417 | $pf = new ProfileFilter(new UFC_Contact(S::user())); |
5d42c993 | 418 | $vcard = new VCard($photos == 'photos'); |
801adeec | 419 | $vcard->addProfiles($pf->getProfiles(null, Profile::FETCH_ALL)); |
5d42c993 | 420 | $vcard->show(); |
5e193297 | 421 | } |
b3ad574d | 422 | |
82af3fc3 | 423 | function handler_csv(PlPage $page, PlUser $user) |
b3ad574d | 424 | { |
b3ad574d PC |
425 | $page->changeTpl('carnet/mescontacts.outlook.tpl', NO_SKIN); |
426 | $pf = new ProfileFilter(new UFC_Contact($user)); | |
427 | require_once 'carnet/outlook.inc.php'; | |
428 | Outlook::output_profiles($pf->getProfiles(), 'fr'); | |
429 | } | |
4da0b8d7 | 430 | } |
c9f82d49 | 431 | |
a7de4ef7 | 432 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
c9f82d49 | 433 | ?> |