2 /***************************************************************************
3 * Copyright (C) 2003-2011 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
, 'directory_private'),
28 'carnet/panel' => $this->make_hook('panel', AUTH_COOKIE
, 'directory_private'),
29 'carnet/notifs' => $this->make_hook('notifs', AUTH_COOKIE
, 'directory_private'),
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'),
39 'carnet/rss' => $this->make_token_hook('rss', AUTH_COOKIE
, 'directory_private'),
43 function _add_rss_link($page)
45 if (!S
::hasAuthToken()) {
48 $page->setRssLink('Polytechnique.org :: Carnet',
49 '/carnet/rss/' . S
::v('hruid') . '/' . S
::user()->token
. '/rss.xml');
52 function handler_index($page)
54 $page->changeTpl('carnet/index.tpl');
55 $page->setTitle('Mon carnet');
56 $this->_add_rss_link($page);
59 function handler_panel($page)
61 $page->changeTpl('carnet/panel.tpl');
63 if (Get
::has('read')) {
64 XDB
::execute('UPDATE watch
65 SET last = FROM_UNIXTIME({?})
67 Get
::i('read'), S
::i('uid'));
68 S
::user()->invalidWatchCache();
69 Platal
::session()->updateNbNotifs();
70 pl_redirect('carnet/panel');
73 require_once 'notifs.inc.php';
74 $page->assign('now', time());
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);
83 private function getSinglePromotion(PlPage
$page, $promo)
85 if (!(is_int($promo) ||
ctype_digit($promo)) ||
$promo < 1920 ||
$promo > date('Y')) {
86 $page->trigError('Promotion invalide : ' . $promo . '.');
92 private function getPromo(PlPage
$page, $promo)
94 if (strpos($promo, '-') === false
) {
95 $promo = $this->getSinglePromotion($page, $promo);
103 list($promo1, $promo2) = explode('-', $promo);
104 $promo1 = $this->getSinglePromotion($page, $promo1);
108 $promo2 = $this->getSinglePromotion($page, $promo2);
112 if ($promo1 > $promo2) {
113 $page->trigError('Intervalle non valide : ' . $promo . '.');
117 for ($i = $promo1 ; $i <= $promo2 ; ++
$i) {
123 private function addPromo(PlPage
$page, $promo)
125 $promos = $this->getPromo($page, $promo);
126 if (!$promos ||
count($promos) == 0) {
130 foreach ($promos as $promo) {
131 $to_add[] = XDB
::format('({?}, {?})', S
::i('uid'), $promo);
133 XDB
::execute('INSERT IGNORE INTO watch_promo (uid, promo)
134 VALUES ' . implode(', ', $to_add));
135 S
::user()->invalidWatchCache();
136 Platal
::session()->updateNbNotifs();
139 private function delPromo(PlPage
$page, $promo)
141 $promos = $this->getPromo($page, $promo);
142 if (!$promos ||
count($promos) == 0) {
145 $to_delete = array();
146 foreach ($promos as $promo) {
147 $to_delete[] = XDB
::format('{?}', $promo);
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();
156 private function getGroup(PlPage
$page, $group)
158 $groupid = XDB
::fetchOneCell("SELECT id
160 WHERE (nom = {?} OR diminutif = {?}) AND NOT FIND_IN_SET('private', pub)",
162 if (is_null($groupid)) {
163 $search = XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, $group);
164 $res = XDB
::query('SELECT id
166 WHERE (nom ' . $search . ' OR diminutif ' . $search . ") AND NOT FIND_IN_SET('private', pub)",
168 if ($res->numRows() == 1) {
169 $groupid = $res->fetchOneCell();
175 private function addGroup(PlPage
$page, $group)
177 $groupid = $this->getGroup($page, $group);
178 if (is_null($groupid)) {
181 XDB
::execute('INSERT IGNORE INTO watch_group (uid, groupid)
183 S
::i('uid'), $groupid);
184 S
::user()->invalidWatchCache();
185 Platal
::session()->updateNbNotifs();
188 private function delGroup(PlPage
$page, $group)
190 $groupid = $this->getGroup($page, $group);
191 if (is_null($groupid)) {
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();
201 public function addNonRegistered(PlPage
$page, PlUser
$user)
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é : ' . $user->fullName(true
));
210 $page->trigWarning('Contact déjà dans la liste : ' . $user->fullName(true
));
214 public function delNonRegistered(PlPage
$page, PlUser
$user)
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();
223 public function addRegistered(PlPage
$page, Profile
$profile)
225 XDB
::execute('INSERT IGNORE INTO contacts (uid, contact)
227 S
::i('uid'), $profile->id());
228 if (XDB
::affectedRows() > 0) {
229 S
::user()->invalidWatchCache();
230 Platal
::session()->updateNbNotifs();
231 $page->trigSuccess('Contact ajouté : ' . $profile->fullName(true
));
233 $page->trigWarning('Contact déjà dans la liste : ' . $profile->fullName(true
));
237 public function delRegistered(PlPage
$page, Profile
$profile)
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é !");
250 public function handler_notifs($page, $action = null
, $arg = null
)
252 $page->changeTpl('carnet/notifs.tpl');
255 S
::assert_xsrf_token();
258 $this->addPromo($page, $arg);
262 $this->delPromo($page, $arg);
266 $this->addGroup($page, $arg);
270 $this->delGroup($page, $arg);
274 $user = User
::get($arg);
276 $this->delNonRegistered($page, $user);
281 $user = User
::get($arg);
283 $this->addNonRegistered($page, $user);
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);
295 XDB
::execute('UPDATE watch
297 WHERE uid = {?}', $flags, S
::i('uid'));
298 S
::user()->invalidWatchCache();
299 Platal
::session()->updateNbNotifs();
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();
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();
321 $nonins = new UserFilter(new UFC_WatchRegistration($user));
323 $promo = XDB
::fetchColumn('SELECT promo
326 ORDER BY promo', S
::i('uid'));
327 $page->assign('promo_count', count($promo));
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;
341 $ranges[] = array($range_start, $range_end);
342 $page->assign('promo_ranges', $ranges);
343 $page->assign('nonins', $nonins->getUsers());
345 $groups = XDB
::fetchColumn('SELECT g.nom
346 FROM watch_group AS w
347 INNER JOIN groups AS g ON (g.id = w.groupid)
351 $page->assign('groups', $groups);
352 $page->assign('groups_count', count($groups));
353 list($flags, $actions) = XDB
::fetchOneRow('SELECT flags, actions
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);
362 function handler_contacts($page, $action = null
, $subaction = null
, $ssaction = null
)
364 $page->setTitle('Mes contacts');
365 $this->_add_rss_link($page);
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();
372 switch (Env
::v('action')) {
374 if (($contact = Profile
::get(Env
::v('user')))) {
375 $this->delRegistered($page, $contact);
380 if (($contact = Profile
::get(Env
::v('user')))) {
381 $this->addRegistered($page, $contact);
389 require_once 'userset.inc.php';
391 if ($action == 'search') {
392 $action = $subaction;
393 $subaction = $ssaction;
396 if ($search && trim(Env
::v('quick'))) {
397 $base = 'carnet/contacts/search';
398 $view = new QuickSearchSet(new UFC_Contact($user));
400 $base = 'carnet/contacts';
401 $view = new ProfileSet(new UFC_Contact($user));
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');
411 function handler_pdf($page, $arg0 = null
, $arg1 = null
)
413 $this->load('contacts.pdf.inc.php');
416 Platal
::session()->close();
418 $order = array(new UFO_Name());
419 if ($arg0 == 'promo') {
420 $order = array_unshift($order, new UFO_Promo());
422 $order[] = new UFO_Promo();
424 $filter = new UserFilter(new UFC_Contact($user), $order);
426 $pdf = new ContactsPDF();
428 $it = $filter->iterProfiles();
429 while ($p = $it->next()) {
430 $pdf = ContactsPDF
::addContact($pdf, $p, $arg0 == 'photos' ||
$arg1 == 'photos');
437 function handler_rss(PlPage
$page, PlUser
$user)
439 $this->load('feed.inc.php');
440 $feed = new CarnetFeed();
441 return $feed->run($page, $user);
444 function buildBirthRef(Profile
$profile)
446 $date = strtotime($profile->birthdate
);
447 $tomorrow = $date +
86400;
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
)
457 function handler_csv_birthday(PlPage
$page, PlUser
$user)
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;
467 $page->assign('years', $years);
469 if (preg_match('/([a-zA-Z]{2,8})($|[^a-zA-Z])/', $_SERVER['HTTP_ACCEPT_LANGUAGE'], $matches)) {
470 $lang = strtolower($matches[1]);
472 $page->assign('lang', $lang);
474 $encoding = 'iso8859-15';
478 pl_cached_content_headers('text/comma-separated-values; charset=' . $encoding, 1);
481 function handler_ical(PlPage
$page, PlUser
$user)
483 require_once 'ical.inc.php';
484 $page->changeTpl('carnet/calendar.tpl', NO_SKIN
);
485 $page->register_function('display_ical', 'display_ical');
487 $filter = new UserFilter(new UFC_Contact($user));
488 $profiles = $filter->iterProfiles();
489 $page->assign('events', PlIteratorUtils
::map($profiles, array($this, 'buildBirthRef')));
491 pl_cached_content_headers('text/calendar', 1);
494 function handler_vcard($page, $photos = null
)
496 $pf = new ProfileFilter(new UFC_Contact(S
::user()));
497 $vcard = new VCard($photos == 'photos');
498 $vcard->addProfiles($pf->getProfiles(null
, Profile
::FETCH_ALL
));
502 function handler_csv(PlPage
$page, PlUser
$user)
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');
510 function handler_batch($page)
512 $page->changeTpl('carnet/batch.tpl');
514 $incomplete = array();
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';
522 $list = explode("\n", Post
::v('list'));
523 $origin = Post
::v('origin');
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 : ' . $email);
531 $incomplete[] = $item;
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));
544 $cond->addChild(new UFC_NameTokens($promo));
546 foreach ($elements as $element) {
547 $cond->addChild(new UFC_NameTokens($element));
549 $uf = new UserFilter($cond);
550 $count = $uf->getTotalCount();
552 $page->trigError('Les informations : « ' . $item . ' » ne correspondent à aucun camarade.');
553 $incomplete[] = $item;
556 } elseif ($count > 1) {
557 $page->trigError('Les informations : « ' . $item . ' » sont ambigues et correspondent à plusieurs camarades.');
558 $incomplete[] = $item;
562 $user = $uf->getUser();
566 if ($user->state
== 'active') {
567 $this->addRegistered($page, $user->profile());
569 if (!User
::isForeignEmailAddress($email)) {
570 $page->trigError('Email pas encore attribué : ' . $email);
571 $incomplete[] = $item;
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
);
585 $page->assign('errors', $errors);
586 $page->assign('incomplete', $incomplete);
590 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: