Moving to GitHub.
[platal.git] / modules / carnet.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2014 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, 'directory_private'),
28 'carnet/panel' => $this->make_hook('panel', AUTH_COOKIE, 'directory_private'),
29 'carnet/notifs' => $this->make_hook('notifs', AUTH_COOKIE, 'directory_private'),
30
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'),
36 'carnet/contacts/csv/birthday' => $this->make_token_hook('csv_birthday', AUTH_COOKIE, 'directory_private'),
37 'carnet/batch' => $this->make_hook('batch', AUTH_COOKIE, 'directory_private'),
38
39 'carnet/rss' => $this->make_token_hook('rss', AUTH_COOKIE, 'directory_private'),
40 );
41 }
42
43 function _add_rss_link($page)
44 {
45 if (!S::hasAuthToken()) {
46 return;
47 }
48 $page->setRssLink('Polytechnique.org :: Carnet',
49 '/carnet/rss/' . S::v('hruid') . '/' . S::user()->token . '/rss.xml');
50 }
51
52 function handler_index($page)
53 {
54 $page->changeTpl('carnet/index.tpl');
55 $page->setTitle('Mon carnet');
56 $this->_add_rss_link($page);
57 }
58
59 function handler_panel($page)
60 {
61 $page->changeTpl('carnet/panel.tpl');
62
63 if (Get::has('read')) {
64 XDB::execute('UPDATE watch
65 SET last = FROM_UNIXTIME({?})
66 WHERE uid = {?}',
67 Get::i('read'), S::i('uid'));
68 S::user()->invalidWatchCache();
69 Platal::session()->updateNbNotifs();
70 pl_redirect('carnet/panel');
71 }
72
73 require_once 'notifs.inc.php';
74 $page->assign('now', time());
75
76 $user = S::user();
77 $notifs = Watch::getEvents($user, time() - (7 * 86400));
78 $page->assign('notifs', $notifs);
79 $page->assign('today', date('Y-m-d'));
80 $this->_add_rss_link($page);
81 }
82
83 private function getSinglePromotion(PlPage $page, $promo)
84 {
85 if (!(is_int($promo) || ctype_digit($promo)) || $promo < 1920 || $promo > date('Y')) {
86 $page->trigError('Promotion invalide&nbsp;: ' . $promo . '.');
87 return null;
88 }
89 return (int)$promo;
90 }
91
92 private function getPromo(PlPage $page, $promo)
93 {
94 if (strpos($promo, '-') === false) {
95 $promo = $this->getSinglePromotion($page, $promo);
96 if (!$promo) {
97 return null;
98 } else {
99 return array($promo);
100 }
101 }
102
103 list($promo1, $promo2) = explode('-', $promo);
104 $promo1 = $this->getSinglePromotion($page, $promo1);
105 if (!$promo1) {
106 return null;
107 }
108 $promo2 = $this->getSinglePromotion($page, $promo2);
109 if (!$promo2) {
110 return null;
111 }
112 if ($promo1 > $promo2) {
113 $page->trigError('Intervalle non valide :&nbsp;' . $promo . '.');
114 return null;
115 }
116 $array = array();
117 for ($i = $promo1 ; $i <= $promo2 ; ++$i) {
118 $array[] = $i;
119 }
120 return $array;
121 }
122
123 private function addPromo(PlPage $page, $promo)
124 {
125 $promos = $this->getPromo($page, $promo);
126 if (!$promos || count($promos) == 0) {
127 return;
128 }
129 $to_add = array();
130 foreach ($promos as $promo) {
131 $to_add[] = XDB::format('({?}, {?})', S::i('uid'), $promo);
132 }
133 XDB::execute('INSERT IGNORE INTO watch_promo (uid, promo)
134 VALUES ' . implode(', ', $to_add));
135 S::user()->invalidWatchCache();
136 Platal::session()->updateNbNotifs();
137 }
138
139 private function delPromo(PlPage $page, $promo)
140 {
141 $promos = $this->getPromo($page, $promo);
142 if (!$promos || count($promos) == 0) {
143 return;
144 }
145 $to_delete = array();
146 foreach ($promos as $promo) {
147 $to_delete[] = XDB::format('{?}', $promo);
148 }
149 XDB::execute('DELETE FROM watch_promo
150 WHERE ' . XDB::format('uid = {?}', S::i('uid')) . '
151 AND promo IN (' . implode(', ', $to_delete) . ')');
152 S::user()->invalidWatchCache();
153 Platal::session()->updateNbNotifs();
154 }
155
156 private function getGroup(PlPage $page, $group)
157 {
158 $groupid = XDB::fetchOneCell("SELECT id
159 FROM groups
160 WHERE (nom = {?} OR diminutif = {?}) AND NOT FIND_IN_SET('private', pub)",
161 $group, $group);
162 if (is_null($groupid)) {
163 $search = XDB::formatWildcards(XDB::WILDCARD_CONTAINS, $group);
164 $res = XDB::query('SELECT id
165 FROM groups
166 WHERE (nom ' . $search . ' OR diminutif ' . $search . ") AND NOT FIND_IN_SET('private', pub)",
167 $search, $search);
168 if ($res->numRows() == 1) {
169 $groupid = $res->fetchOneCell();
170 }
171 }
172 return $groupid;
173 }
174
175 private function addGroup(PlPage $page, $group)
176 {
177 $groupid = $this->getGroup($page, $group);
178 if (is_null($groupid)) {
179 return;
180 }
181 XDB::execute('INSERT IGNORE INTO watch_group (uid, groupid)
182 VALUES ({?}, {?})',
183 S::i('uid'), $groupid);
184 S::user()->invalidWatchCache();
185 Platal::session()->updateNbNotifs();
186 }
187
188 private function delGroup(PlPage $page, $group)
189 {
190 $groupid = $this->getGroup($page, $group);
191 if (is_null($groupid)) {
192 return;
193 }
194 XDB::execute('DELETE FROM watch_group
195 WHERE uid = {?} AND groupid = {?}',
196 S::i('uid'), $groupid);
197 S::user()->invalidWatchCache();
198 Platal::session()->updateNbNotifs();
199 }
200
201 public function addNonRegistered(PlPage $page, PlUser $user)
202 {
203 XDB::execute('INSERT IGNORE INTO watch_nonins (uid, ni_id)
204 VALUES ({?}, {?})', S::i('uid'), $user->id());
205 if (XDB::affectedRows() > 0) {
206 S::user()->invalidWatchCache();
207 Platal::session()->updateNbNotifs();
208 $page->trigSuccess('Contact ajouté&nbsp;: ' . $user->fullName(true));
209 } else {
210 $page->trigWarning('Contact déjà dans la liste&nbsp;: ' . $user->fullName(true));
211 }
212 }
213
214 public function delNonRegistered(PlPage $page, PlUser $user)
215 {
216 XDB::execute('DELETE FROM watch_nonins
217 WHERE uid = {?} AND ni_id = {?}',
218 S::i('uid'), $user->id());
219 S::user()->invalidWatchCache();
220 Platal::session()->updateNbNotifs();
221 }
222
223 public function addRegistered(PlPage $page, Profile $profile)
224 {
225 XDB::execute('INSERT IGNORE INTO contacts (uid, contact)
226 VALUES ({?}, {?})',
227 S::i('uid'), $profile->id());
228 if (XDB::affectedRows() > 0) {
229 S::user()->invalidWatchCache();
230 Platal::session()->updateNbNotifs();
231 $page->trigSuccess('Contact ajouté&nbsp;: ' . $profile->fullName(true));
232 } else {
233 $page->trigWarning('Contact déjà dans la liste&nbsp;: ' . $profile->fullName(true));
234 }
235 }
236
237 public function delRegistered(PlPage $page, Profile $profile)
238 {
239 XDB::execute('DELETE FROM contacts
240 WHERE uid = {?} AND contact = {?}',
241 S::i('uid'), $profile->id());
242 if (XDB::affectedRows() > 0) {
243 S::user()->invalidWatchCache();
244 Platal::session()->updateNbNotifs();
245 $page->trigSuccess("Contact retiré&nbsp;!");
246 }
247
248 }
249
250 public function handler_notifs($page, $action = null, $arg = null)
251 {
252 $page->changeTpl('carnet/notifs.tpl');
253
254 if ($action) {
255 S::assert_xsrf_token();
256 switch ($action) {
257 case 'add_promo':
258 $this->addPromo($page, $arg);
259 break;
260
261 case 'del_promo':
262 $this->delPromo($page, $arg);
263 break;
264
265 case 'add_group':
266 $this->addGroup($page, $arg);
267 break;
268
269 case 'del_group':
270 $this->delGroup($page, $arg);
271 break;
272
273 case 'del_nonins':
274 $user = User::get($arg);
275 if ($user) {
276 $this->delNonRegistered($page, $user);
277 }
278 break;
279
280 case 'add_nonins':
281 $user = User::get($arg);
282 if ($user) {
283 $this->addNonRegistered($page, $user);
284 }
285 break;
286 }
287 }
288
289 if (Env::has('subs')) {
290 S::assert_xsrf_token();
291 $flags = new PlFlagSet();
292 foreach (Env::v('sub') as $key=>$value) {
293 $flags->addFlag($key, $value);
294 }
295 XDB::execute('UPDATE watch
296 SET actions = {?}
297 WHERE uid = {?}', $flags, S::i('uid'));
298 S::user()->invalidWatchCache();
299 Platal::session()->updateNbNotifs();
300 }
301
302 if (Env::has('flags_contacts')) {
303 S::assert_xsrf_token();
304 XDB::execute('UPDATE watch
305 SET ' . XDB::changeFlag('flags', 'contacts', Env::b('contacts')) . '
306 WHERE uid = {?}', S::i('uid'));
307 S::user()->invalidWatchCache();
308 Platal::session()->updateNbNotifs();
309 }
310
311 if (Env::has('flags_mail')) {
312 S::assert_xsrf_token();
313 XDB::execute('UPDATE watch
314 SET ' . XDB::changeFlag('flags', 'mail', Env::b('mail')) . '
315 WHERE uid = {?}', S::i('uid'));
316 S::user()->invalidWatchCache();
317 Platal::session()->updateNbNotifs();
318 }
319
320 $user = S::user();
321 $nonins = new UserFilter(new UFC_WatchRegistration($user));
322
323 $promo = XDB::fetchColumn('SELECT promo
324 FROM watch_promo
325 WHERE uid = {?}
326 ORDER BY promo', S::i('uid'));
327 $page->assign('promo_count', count($promo));
328 $ranges = array();
329 $range_start = null;
330 $range_end = null;
331 foreach ($promo as $p) {
332 if (is_null($range_start)) {
333 $range_start = $range_end = $p;
334 } else if ($p != $range_end + 1) {
335 $ranges[] = array($range_start, $range_end);
336 $range_start = $range_end = $p;
337 } else {
338 $range_end = $p;
339 }
340 }
341 $ranges[] = array($range_start, $range_end);
342 $page->assign('promo_ranges', $ranges);
343 $page->assign('nonins', $nonins->getUsers());
344
345 $groups = XDB::fetchColumn('SELECT g.nom
346 FROM watch_group AS w
347 INNER JOIN groups AS g ON (g.id = w.groupid)
348 WHERE w.uid = {?}
349 ORDER BY g.nom',
350 S::i('uid'));
351 $page->assign('groups', $groups);
352 $page->assign('groups_count', count($groups));
353 list($flags, $actions) = XDB::fetchOneRow('SELECT flags, actions
354 FROM watch
355 WHERE uid = {?}', S::i('uid'));
356 $flags = new PlFlagSet($flags);
357 $actions = new PlFlagSet($actions);
358 $page->assign('flags', $flags);
359 $page->assign('actions', $actions);
360 }
361
362 function handler_contacts($page, $action = null, $subaction = null, $ssaction = null)
363 {
364 $page->setTitle('Mes contacts');
365 $this->_add_rss_link($page);
366
367 // For XSRF protection, checks both the normal xsrf token, and the special RSS token.
368 // It allows direct linking to contact adding in the RSS feed.
369 if (Env::v('action') && Env::v('token') !== S::user()->token) {
370 S::assert_xsrf_token();
371 }
372 switch (Env::v('action')) {
373 case 'retirer':
374 if (($contact = Profile::get(Env::v('user')))) {
375 $this->delRegistered($page, $contact);
376 }
377 break;
378
379 case 'ajouter':
380 if (($contact = Profile::get(Env::v('user')))) {
381 $this->addRegistered($page, $contact);
382 }
383 break;
384 }
385
386 $search = false;
387 $user = S::user();
388
389 require_once 'userset.inc.php';
390
391 if ($action == 'search') {
392 $action = $subaction;
393 $subaction = $ssaction;
394 $search = true;
395 }
396 if ($search && trim(Env::v('quick'))) {
397 $base = 'carnet/contacts/search';
398 $view = new QuickSearchSet(new UFC_Contact($user));
399 } else {
400 $base = 'carnet/contacts';
401 $view = new ProfileSet(new UFC_Contact($user));
402 }
403
404 $view->addMod('minifiche', 'Mini-fiches', true);
405 $view->addMod('trombi', 'Trombinoscope', false, array('with_admin' => false, 'with_promo' => true));
406 $view->addMod('map', 'Planisphère');
407 $view->apply('carnet/contacts', $page, $action, $subaction);
408 $page->changeTpl('carnet/mescontacts.tpl');
409 }
410
411 function handler_pdf($page, $arg0 = null, $arg1 = null)
412 {
413 $this->load('contacts.pdf.inc.php');
414 $user = S::user();
415
416 Platal::session()->close();
417
418 $order = array(new UFO_Name());
419 if ($arg0 == 'promo') {
420 $order = array_unshift($order, new UFO_Promo());
421 } else {
422 $order[] = new UFO_Promo();
423 }
424 $filter = new UserFilter(new UFC_Contact($user), $order);
425
426 $pdf = new ContactsPDF();
427
428 $it = $filter->iterProfiles();
429 while ($p = $it->next()) {
430 $pdf = ContactsPDF::addContact($pdf, $p, $arg0 == 'photos' || $arg1 == 'photos');
431 }
432 $pdf->Output();
433
434 exit;
435 }
436
437 function handler_rss(PlPage $page, PlUser $user)
438 {
439 $this->load('feed.inc.php');
440 $feed = new CarnetFeed();
441 return $feed->run($page, $user);
442 }
443
444 function buildBirthRef(Profile $profile)
445 {
446 $date = strtotime($profile->birthdate);
447 $tomorrow = $date + 86400;
448 return array(
449 'timestamp' => $date,
450 'date' => date('Ymd', $date),
451 'tomorrow' => date('Ymd', $tomorrow),
452 'email' => $profile->owner()->bestEmail(),
453 'summary' => 'Anniversaire de ' . $profile->fullName(true)
454 );
455 }
456
457 function handler_csv_birthday(PlPage $page, PlUser $user)
458 {
459 $page->changeTpl('carnet/calendar.outlook.tpl', NO_SKIN);
460 $filter = new UserFilter(new UFC_Contact($user));
461 $profiles = $filter->iterProfiles();
462 $page->assign('events', PlIteratorUtils::map($profiles, array($this, 'buildBirthRef')));
463 $years = array(date("Y"));
464 for ($i = 1; $i <= 10; ++$i) {
465 $years[] = $years[0] + $i;
466 }
467 $page->assign('years', $years);
468 $lang = 'fr';
469 if (preg_match('/([a-zA-Z]{2,8})($|[^a-zA-Z])/', $_SERVER['HTTP_ACCEPT_LANGUAGE'], $matches)) {
470 $lang = strtolower($matches[1]);
471 }
472 $page->assign('lang', $lang);
473 if ($lang == 'fr') {
474 $encoding = 'iso8859-15';
475 } else {
476 $encoding = 'utf-8';
477 }
478 pl_cached_content_headers('text/comma-separated-values; charset=' . $encoding, 1);
479 }
480
481 function handler_ical(PlPage $page, PlUser $user)
482 {
483 require_once 'ical.inc.php';
484 $page->changeTpl('carnet/calendar.tpl', NO_SKIN);
485 $page->register_function('display_ical', 'display_ical');
486
487 $filter = new UserFilter(new UFC_Contact($user));
488 $profiles = $filter->iterProfiles();
489 $page->assign('events', PlIteratorUtils::map($profiles, array($this, 'buildBirthRef')));
490
491 pl_cached_content_headers('text/calendar', 1);
492 }
493
494 function handler_vcard($page, $photos = null)
495 {
496 $pf = new ProfileFilter(new UFC_Contact(S::user()));
497 $vcard = new VCard($photos == 'photos');
498 $vcard->addProfiles($pf->getProfiles(null, Profile::FETCH_ALL));
499 $vcard->show();
500 }
501
502 function handler_csv(PlPage $page, PlUser $user)
503 {
504 $page->changeTpl('carnet/mescontacts.outlook.tpl', NO_SKIN);
505 $pf = new ProfileFilter(new UFC_Contact($user));
506 require_once 'carnet/outlook.inc.php';
507 Outlook::output_profiles($pf->getProfiles(), 'fr');
508 }
509
510 function handler_batch($page)
511 {
512 $page->changeTpl('carnet/batch.tpl');
513 $errors = false;
514 $incomplete = array();
515
516 if (Post::has('add')) {
517 S::assert_xsrf_token();
518 require_once 'userset.inc.php';
519 require_once 'emails.inc.php';
520 require_once 'marketing.inc.php';
521
522 $list = explode("\n", Post::v('list'));
523 $origin = Post::v('origin');
524
525 foreach ($list as $item) {
526 if ($item = trim($item)) {
527 $elements = preg_split("/\s/", $item);
528 $email = array_pop($elements);
529 if (!isvalid_email($email)) {
530 $page->trigError('Email invalide&nbsp;: ' . $email);
531 $incomplete[] = $item;
532 $errors = true;
533 continue;
534 }
535
536 $user = User::getSilent($email);
537 if (is_null($user)) {
538 $details = implode(' ', $elements);
539 $promo = trim(array_pop($elements));
540 $cond = new PFC_And();
541 if (preg_match('/^[MDX]\d{4}$/', $promo)) {
542 $cond->addChild(new UFC_Promo('=', UserFilter::DISPLAY, $promo));
543 } else {
544 $cond->addChild(new UFC_NameTokens($promo));
545 }
546 foreach ($elements as $element) {
547 $cond->addChild(new UFC_NameTokens($element));
548 }
549 $uf = new UserFilter($cond);
550 $count = $uf->getTotalCount();
551 if ($count == 0) {
552 $page->trigError('Les informations : « ' . $item . ' » ne correspondent à aucun camarade.');
553 $incomplete[] = $item;
554 $errors = true;
555 continue;
556 } elseif ($count > 1) {
557 $page->trigError('Les informations : « ' . $item . ' » sont ambigues et correspondent à plusieurs camarades.');
558 $incomplete[] = $item;
559 $errors = true;
560 continue;
561 } else {
562 $user = $uf->getUser();
563 }
564 }
565
566 if ($user->state == 'active') {
567 $this->addRegistered($page, $user->profile());
568 } else {
569 if (!User::isForeignEmailAddress($email)) {
570 $page->trigError('Email pas encore attribué&nbsp;: ' . $email);
571 $incomplete[] = $item;
572 $errors = true;
573 } else {
574 $this->addNonRegistered($page, $user);
575 if (!Marketing::get($user->id(), $email, true)) {
576 check_email($email, "Une adresse surveillée est proposée au marketing par " . S::user()->login());
577 $market = new Marketing($user->id(), $email, 'default', null, $origin, S::v('uid'), null);
578 $market->add();
579 }
580 }
581 }
582 }
583 }
584 }
585 $page->assign('errors', $errors);
586 $page->assign('incomplete', $incomplete);
587 }
588 }
589
590 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker fenc=utf-8:
591 ?>