283ecc93585e6f3aab6bdf44647cf90557addb97
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'),
38 'carnet/rss' => $this->make_token_hook('rss', AUTH_COOKIE
, 'directory_private'),
42 function _add_rss_link($page)
44 if (!S
::hasAuthToken()) {
47 $page->setRssLink('Polytechnique.org :: Carnet',
48 '/carnet/rss/' . S
::v('hruid') . '/' . S
::user()->token
. '/rss.xml');
51 function handler_index($page)
53 $page->changeTpl('carnet/index.tpl');
54 $page->setTitle('Mon carnet');
55 $this->_add_rss_link($page);
58 function handler_panel($page)
60 $page->changeTpl('carnet/panel.tpl');
62 if (Get
::has('read')) {
63 XDB
::execute('UPDATE watch
64 SET last = FROM_UNIXTIME({?})
66 Get
::i('read'), S
::i('uid'));
67 S
::user()->invalidWatchCache();
68 Platal
::session()->updateNbNotifs();
69 pl_redirect('carnet/panel');
72 require_once 'notifs.inc.php';
73 $page->assign('now', time());
76 $notifs = Watch
::getEvents($user, time() - (7 * 86400));
77 $page->assign('notifs', $notifs);
78 $page->assign('today', date('Y-m-d'));
79 $this->_add_rss_link($page);
82 private function getSinglePromotion(PlPage
$page, $promo)
84 if (!(is_int($promo) ||
ctype_digit($promo)) ||
$promo < 1920 ||
$promo > date('Y')) {
85 $page->trigError('Promotion invalide : ' . $promo . '.');
91 private function getPromo(PlPage
$page, $promo)
93 if (strpos($promo, '-') === false
) {
94 $promo = $this->getSinglePromotion($page, $promo);
102 list($promo1, $promo2) = explode('-', $promo);
103 $promo1 = $this->getSinglePromotion($page, $promo1);
107 $promo2 = $this->getSinglePromotion($page, $promo2);
111 if ($promo1 > $promo2) {
112 $page->trigError('Intervalle non valide : ' . $promo . '.');
116 for ($i = $promo1 ; $i <= $promo2 ; ++
$i) {
122 private function addPromo(PlPage
$page, $promo)
124 $promos = $this->getPromo($page, $promo);
125 if (!$promos ||
count($promos) == 0) {
129 foreach ($promos as $promo) {
130 $to_add[] = XDB
::format('({?}, {?})', S
::i('uid'), $promo);
132 XDB
::execute('INSERT IGNORE INTO watch_promo (uid, promo)
133 VALUES ' . implode(', ', $to_add));
134 S
::user()->invalidWatchCache();
135 Platal
::session()->updateNbNotifs();
138 private function delPromo(PlPage
$page, $promo)
140 $promos = $this->getPromo($page, $promo);
141 if (!$promos ||
count($promos) == 0) {
144 $to_delete = array();
145 foreach ($promos as $promo) {
146 $to_delete[] = XDB
::format('{?}', $promo);
148 XDB
::execute('DELETE FROM watch_promo
149 WHERE ' . XDB
::format('uid = {?}', S
::i('uid')) . '
150 AND promo IN (' . implode(', ', $to_delete) . ')');
151 S
::user()->invalidWatchCache();
152 Platal
::session()->updateNbNotifs();
155 private function getGroup(PlPage
$page, $group)
157 $groupid = XDB
::fetchOneCell("SELECT id
159 WHERE (nom = {?} OR diminutif = {?}) AND NOT FIND_IN_SET('private', pub)",
161 if (is_null($groupid)) {
162 $search = XDB
::formatWildcards(XDB
::WILDCARD_CONTAINS
, $group);
163 $res = XDB
::query('SELECT id
165 WHERE (nom ' . $search . ' OR diminutif ' . $search . ") AND NOT FIND_IN_SET('private', pub)",
167 if ($res->numRows() == 1) {
168 $groupid = $res->fetchOneCell();
174 private function addGroup(PlPage
$page, $group)
176 $groupid = $this->getGroup($page, $group);
177 if (is_null($groupid)) {
180 XDB
::execute('INSERT IGNORE INTO watch_group (uid, groupid)
182 S
::i('uid'), $groupid);
183 S
::user()->invalidWatchCache();
184 Platal
::session()->updateNbNotifs();
187 private function delGroup(PlPage
$page, $group)
189 $groupid = $this->getGroup($page, $group);
190 if (is_null($groupid)) {
193 XDB
::execute('DELETE FROM watch_group
194 WHERE uid = {?} AND groupid = {?}',
195 S
::i('uid'), $groupid);
196 S
::user()->invalidWatchCache();
197 Platal
::session()->updateNbNotifs();
200 public function addNonRegistered(PlPage
$page, PlUser
$user)
202 XDB
::execute('INSERT IGNORE INTO watch_nonins (uid, ni_id)
203 VALUES ({?}, {?})', S
::i('uid'), $user->id());
204 S
::user()->invalidWatchCache();
205 Platal
::session()->updateNbNotifs();
208 public function delNonRegistered(PlPage
$page, PlUser
$user)
210 XDB
::execute('DELETE FROM watch_nonins
211 WHERE uid = {?} AND ni_id = {?}',
212 S
::i('uid'), $user->id());
213 S
::user()->invalidWatchCache();
214 Platal
::session()->updateNbNotifs();
217 public function handler_notifs($page, $action = null
, $arg = null
)
219 $page->changeTpl('carnet/notifs.tpl');
222 S
::assert_xsrf_token();
225 $this->addPromo($page, $arg);
229 $this->delPromo($page, $arg);
233 $this->addGroup($page, $arg);
237 $this->delGroup($page, $arg);
241 $user = User
::get($arg);
243 $this->delNonRegistered($page, $user);
248 $user = User
::get($arg);
250 $this->addNonRegistered($page, $user);
256 if (Env
::has('subs')) {
257 S
::assert_xsrf_token();
258 $flags = new PlFlagSet();
259 foreach (Env
::v('sub') as $key=>$value) {
260 $flags->addFlag($key, $value);
262 XDB
::execute('UPDATE watch
264 WHERE uid = {?}', $flags, S
::i('uid'));
265 S
::user()->invalidWatchCache();
266 Platal
::session()->updateNbNotifs();
269 if (Env
::has('flags_contacts')) {
270 S
::assert_xsrf_token();
271 XDB
::execute('UPDATE watch
272 SET ' . XDB
::changeFlag('flags', 'contacts', Env
::b('contacts')) . '
273 WHERE uid = {?}', S
::i('uid'));
274 S
::user()->invalidWatchCache();
275 Platal
::session()->updateNbNotifs();
278 if (Env
::has('flags_mail')) {
279 S
::assert_xsrf_token();
280 XDB
::execute('UPDATE watch
281 SET ' . XDB
::changeFlag('flags', 'mail', Env
::b('mail')) . '
282 WHERE uid = {?}', S
::i('uid'));
283 S
::user()->invalidWatchCache();
284 Platal
::session()->updateNbNotifs();
288 $nonins = new UserFilter(new UFC_WatchRegistration($user));
290 $promo = XDB
::fetchColumn('SELECT promo
293 ORDER BY promo', S
::i('uid'));
294 $page->assign('promo_count', count($promo));
298 foreach ($promo as $p) {
299 if (is_null($range_start)) {
300 $range_start = $range_end = $p;
301 } else if ($p != $range_end +
1) {
302 $ranges[] = array($range_start, $range_end);
303 $range_start = $range_end = $p;
308 $ranges[] = array($range_start, $range_end);
309 $page->assign('promo_ranges', $ranges);
310 $page->assign('nonins', $nonins->getUsers());
312 $groups = XDB
::fetchColumn('SELECT g.nom
313 FROM watch_group AS w
314 INNER JOIN groups AS g ON (g.id = w.groupid)
318 $page->assign('groups', $groups);
319 $page->assign('groups_count', count($groups));
320 list($flags, $actions) = XDB
::fetchOneRow('SELECT flags, actions
322 WHERE uid = {?}', S
::i('uid'));
323 $flags = new PlFlagSet($flags);
324 $actions = new PlFlagSet($actions);
325 $page->assign('flags', $flags);
326 $page->assign('actions', $actions);
329 function handler_contacts($page, $action = null
, $subaction = null
, $ssaction = null
)
331 $page->setTitle('Mes contacts');
332 $this->_add_rss_link($page);
337 // For XSRF protection, checks both the normal xsrf token, and the special RSS token.
338 // It allows direct linking to contact adding in the RSS feed.
339 if (Env
::v('action') && Env
::v('token') !== S
::user()->token
) {
340 S
::assert_xsrf_token();
342 switch (Env
::v('action')) {
344 if (($contact = Profile
::get(Env
::v('user')))) {
345 if (XDB
::execute("DELETE FROM contacts
346 WHERE uid = {?} AND contact = {?}",
347 $uid, $contact->id())) {
348 Platal
::session()->updateNbNotifs();
349 $page->trigSuccess("Contact retiré !");
355 if (($contact = Profile
::get(Env
::v('user')))) {
356 XDB
::execute('INSERT IGNORE INTO contacts (uid, contact)
358 $uid, $contact->id());
359 if (XDB
::affectedRows() > 0) {
360 Platal
::session()->updateNbNotifs();
361 $page->trigSuccess('Contact ajouté !');
363 $page->trigWarning('Contact déjà dans la liste !');
371 require_once 'userset.inc.php';
373 if ($action == 'search') {
374 $action = $subaction;
375 $subaction = $ssaction;
378 if ($search && trim(Env
::v('quick'))) {
379 $base = 'carnet/contacts/search';
380 $view = new QuickSearchSet(new UFC_Contact($user));
382 $base = 'carnet/contacts';
383 $view = new ProfileSet(new UFC_Contact($user));
386 $view->addMod('minifiche', 'Mini-fiches', true
);
387 $view->addMod('trombi', 'Trombinoscope', false
, array('with_admin' => false
, 'with_promo' => true
));
388 $view->addMod('map', 'Planisphère');
389 $view->apply('carnet/contacts', $page, $action, $subaction);
390 $page->changeTpl('carnet/mescontacts.tpl');
393 function handler_pdf($page, $arg0 = null
, $arg1 = null
)
395 $this->load('contacts.pdf.inc.php');
398 Platal
::session()->close();
400 $order = array(new UFO_Name());
401 if ($arg0 == 'promo') {
402 $order = array_unshift($order, new UFO_Promo());
404 $order[] = new UFO_Promo();
406 $filter = new UserFilter(new UFC_Contact($user), $order);
408 $pdf = new ContactsPDF();
410 $it = $filter->iterProfiles();
411 while ($p = $it->next()) {
412 $pdf = ContactsPDF
::addContact($pdf, $p, $arg0 == 'photos' ||
$arg1 == 'photos');
419 function handler_rss(PlPage
$page, PlUser
$user)
421 $this->load('feed.inc.php');
422 $feed = new CarnetFeed();
423 return $feed->run($page, $user);
426 function buildBirthRef(Profile
$profile)
428 $date = strtotime($profile->birthdate
);
429 $tomorrow = $date +
86400;
431 'timestamp' => $date,
432 'date' => date('Ymd', $date),
433 'tomorrow' => date('Ymd', $tomorrow),
434 'email' => $profile->owner()->bestEmail(),
435 'summary' => 'Anniversaire de ' . $profile->fullName(true
)
439 function handler_csv_birthday(PlPage
$page, PlUser
$user)
441 $page->changeTpl('carnet/calendar.outlook.tpl', NO_SKIN
);
442 $filter = new UserFilter(new UFC_Contact($user));
443 $profiles = $filter->iterProfiles();
444 $page->assign('events', PlIteratorUtils
::map($profiles, array($this, 'buildBirthRef')));
445 $years = array(date("Y"));
446 for ($i = 1; $i <= 10; ++
$i) {
447 $years[] = $years[0] +
$i;
449 $page->assign('years', $years);
451 if (preg_match('/([a-zA-Z]{2,8})($|[^a-zA-Z])/', $_SERVER['HTTP_ACCEPT_LANGUAGE'], $matches)) {
452 $lang = strtolower($matches[1]);
454 $page->assign('lang', $lang);
456 $encoding = 'iso8859-15';
460 pl_cached_content_headers('text/comma-separated-values; charset=' . $encoding, 1);
463 function handler_ical(PlPage
$page, PlUser
$user)
465 require_once 'ical.inc.php';
466 $page->changeTpl('carnet/calendar.tpl', NO_SKIN
);
467 $page->register_function('display_ical', 'display_ical');
469 $filter = new UserFilter(new UFC_Contact($user));
470 $profiles = $filter->iterProfiles();
471 $page->assign('events', PlIteratorUtils
::map($profiles, array($this, 'buildBirthRef')));
473 pl_cached_content_headers('text/calendar', 1);
476 function handler_vcard($page, $photos = null
)
478 $pf = new ProfileFilter(new UFC_Contact(S
::user()));
479 $vcard = new VCard($photos == 'photos');
480 $vcard->addProfiles($pf->getProfiles(null
, Profile
::FETCH_ALL
));
484 function handler_csv(PlPage
$page, PlUser
$user)
486 $page->changeTpl('carnet/mescontacts.outlook.tpl', NO_SKIN
);
487 $pf = new ProfileFilter(new UFC_Contact($user));
488 require_once 'carnet/outlook.inc.php';
489 Outlook
::output_profiles($pf->getProfiles(), 'fr');
493 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: