| 1 | <?php |
| 2 | /*************************************************************************** |
| 3 | * Copyright (C) 2003-2010 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_hook('ical', AUTH_PUBLIC, 'directory_private', NO_HTTPS), |
| 35 | 'carnet/contacts/csv' => $this->make_hook('csv', AUTH_PUBLIC, 'directory_private', NO_HTTPS), |
| 36 | 'carnet/contacts/csv/birthday' => $this->make_hook('csv_birthday', AUTH_PUBLIC, 'directory_private', NO_HTTPS), |
| 37 | |
| 38 | 'carnet/rss' => $this->make_hook('rss', AUTH_PUBLIC, 'directory_private', NO_HTTPS), |
| 39 | ); |
| 40 | } |
| 41 | |
| 42 | function _add_rss_link(&$page) |
| 43 | { |
| 44 | if (!S::hasAuthToken()) { |
| 45 | return; |
| 46 | } |
| 47 | $page->setRssLink('Polytechnique.org :: Carnet', |
| 48 | '/carnet/rss/' . S::v('hruid') . '/' . S::user()->token . '/rss.xml'); |
| 49 | } |
| 50 | |
| 51 | function handler_index(&$page) |
| 52 | { |
| 53 | $page->changeTpl('carnet/index.tpl'); |
| 54 | $page->setTitle('Mon carnet'); |
| 55 | $this->_add_rss_link($page); |
| 56 | } |
| 57 | |
| 58 | function handler_panel(&$page) |
| 59 | { |
| 60 | $page->changeTpl('carnet/panel.tpl'); |
| 61 | |
| 62 | if (Get::has('read')) { |
| 63 | XDB::execute('UPDATE watch |
| 64 | SET last = FROM_UNIXTIME({?}) |
| 65 | WHERE uid = {?}', |
| 66 | Get::i('read'), S::i('uid')); |
| 67 | S::user()->invalidWatchCache(); |
| 68 | Platal::session()->updateNbNotifs(); |
| 69 | pl_redirect('carnet/panel'); |
| 70 | } |
| 71 | |
| 72 | require_once 'notifs.inc.php'; |
| 73 | $page->assign('now', time()); |
| 74 | |
| 75 | $user = S::user(); |
| 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); |
| 80 | } |
| 81 | |
| 82 | private function getSinglePromotion(PlPage &$page, $promo) |
| 83 | { |
| 84 | if (!(is_int($promo) || ctype_digit($promo)) || $promo < 1920 || $promo > date('Y')) { |
| 85 | $page->trigError('Promotion invalide : ' . $promo . '.'); |
| 86 | return null; |
| 87 | } |
| 88 | return (int)$promo; |
| 89 | } |
| 90 | |
| 91 | private function getPromo(PlPage &$page, $promo) |
| 92 | { |
| 93 | if (strpos($promo, '-') === false) { |
| 94 | $promo = $this->getSinglePromotion($page, $promo); |
| 95 | if (!$promo) { |
| 96 | return null; |
| 97 | } else { |
| 98 | return array($promo); |
| 99 | } |
| 100 | } |
| 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) { |
| 112 | $page->trigError('Intervalle non valide : ' . $promo . '.'); |
| 113 | return null; |
| 114 | } |
| 115 | $array = array(); |
| 116 | for ($i = $promo1 ; $i <= $promo2 ; ++$i) { |
| 117 | $array[] = $i; |
| 118 | } |
| 119 | return $array; |
| 120 | } |
| 121 | |
| 122 | private function addPromo(PlPage &$page, $promo) |
| 123 | { |
| 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)); |
| 134 | S::user()->invalidWatchCache(); |
| 135 | Platal::session()->updateNbNotifs(); |
| 136 | } |
| 137 | |
| 138 | private function delPromo(PlPage &$page, $promo) |
| 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) . ')'); |
| 151 | S::user()->invalidWatchCache(); |
| 152 | Platal::session()->updateNbNotifs(); |
| 153 | } |
| 154 | |
| 155 | public function addNonRegistered(PlPage &$page, PlUser &$user) |
| 156 | { |
| 157 | XDB::execute('INSERT IGNORE INTO watch_nonins (uid, ni_id) |
| 158 | VALUES ({?}, {?})', S::i('uid'), $user->id()); |
| 159 | S::user()->invalidWatchCache(); |
| 160 | Platal::session()->updateNbNotifs(); |
| 161 | } |
| 162 | |
| 163 | public function delNonRegistered(PlPage &$page, PlUser &$user) |
| 164 | { |
| 165 | XDB::execute('DELETE FROM watch_nonins |
| 166 | WHERE uid = {?} AND ni_id = {?}', |
| 167 | S::i('uid'), $user->id()); |
| 168 | S::user()->invalidWatchCache(); |
| 169 | Platal::session()->updateNbNotifs(); |
| 170 | } |
| 171 | |
| 172 | public function handler_notifs(&$page, $action = null, $arg = null) |
| 173 | { |
| 174 | $page->changeTpl('carnet/notifs.tpl'); |
| 175 | |
| 176 | if ($action) { |
| 177 | S::assert_xsrf_token(); |
| 178 | switch ($action) { |
| 179 | case 'add_promo': |
| 180 | $this->addPromo($page, $arg); |
| 181 | break; |
| 182 | |
| 183 | case 'del_promo': |
| 184 | $this->delPromo($page, $arg); |
| 185 | break; |
| 186 | |
| 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 | } |
| 201 | } |
| 202 | |
| 203 | if (Env::has('subs')) { |
| 204 | S::assert_xsrf_token(); |
| 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')); |
| 212 | S::user()->invalidWatchCache(); |
| 213 | Platal::session()->updateNbNotifs(); |
| 214 | } |
| 215 | |
| 216 | if (Env::has('flags_contacts')) { |
| 217 | S::assert_xsrf_token(); |
| 218 | XDB::execute('UPDATE watch |
| 219 | SET ' . XDB::changeFlag('flags', 'contacts', Env::b('contacts')) . ' |
| 220 | WHERE uid = {?}', S::i('uid')); |
| 221 | S::user()->invalidWatchCache(); |
| 222 | Platal::session()->updateNbNotifs(); |
| 223 | } |
| 224 | |
| 225 | if (Env::has('flags_mail')) { |
| 226 | S::assert_xsrf_token(); |
| 227 | XDB::execute('UPDATE watch |
| 228 | SET ' . XDB::changeFlag('flags', 'mail', Env::b('mail')) . ' |
| 229 | WHERE uid = {?}', S::i('uid')); |
| 230 | S::user()->invalidWatchCache(); |
| 231 | Platal::session()->updateNbNotifs(); |
| 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 | } |
| 254 | } |
| 255 | $ranges[] = array($range_start, $range_end); |
| 256 | $page->assign('promo_ranges', $ranges); |
| 257 | $page->assign('nonins', $nonins->getUsers()); |
| 258 | |
| 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); |
| 266 | } |
| 267 | |
| 268 | function handler_contacts(&$page, $action = null, $subaction = null, $ssaction = null) |
| 269 | { |
| 270 | $page->setTitle('Mes contacts'); |
| 271 | $this->_add_rss_link($page); |
| 272 | |
| 273 | $uid = S::i('uid'); |
| 274 | $user = S::user(); |
| 275 | |
| 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. |
| 278 | if (Env::v('action') && Env::v('token') !== S::user()->token) { |
| 279 | S::assert_xsrf_token(); |
| 280 | } |
| 281 | switch (Env::v('action')) { |
| 282 | case 'retirer': |
| 283 | if (($contact = User::get(Env::v('user')))) { |
| 284 | if (XDB::execute("DELETE FROM contacts |
| 285 | WHERE uid = {?} AND contact = {?}", |
| 286 | $uid, $contact->id())) { |
| 287 | Platal::session()->updateNbNotifs(); |
| 288 | $page->trigSuccess("Contact retiré !"); |
| 289 | } |
| 290 | } |
| 291 | break; |
| 292 | |
| 293 | case 'ajouter': |
| 294 | if (($contact = User::get(Env::v('user')))) { |
| 295 | if (XDB::execute("REPLACE INTO contacts (uid, contact) |
| 296 | VALUES ({?}, {?})", |
| 297 | $uid, $contact->id())) { |
| 298 | Platal::session()->updateNbNotifs(); |
| 299 | $page->trigSuccess('Contact ajouté !'); |
| 300 | } else { |
| 301 | $page->trigWarning('Contact déjà dans la liste !'); |
| 302 | } |
| 303 | } |
| 304 | break; |
| 305 | } |
| 306 | |
| 307 | $search = false; |
| 308 | |
| 309 | require_once 'userset.inc.php'; |
| 310 | |
| 311 | if ($action == 'search') { |
| 312 | $action = $subaction; |
| 313 | $subaction = $ssaction; |
| 314 | $search = true; |
| 315 | } |
| 316 | if ($search && trim(Env::v('quick'))) { |
| 317 | $base = 'carnet/contacts/search'; |
| 318 | $view = new SearchSet(true, false, new UFC_Contact($user)); |
| 319 | } else { |
| 320 | $base = 'carnet/contacts'; |
| 321 | $view = new ProfileSet(new UFC_Contact($user)); |
| 322 | } |
| 323 | |
| 324 | $view->addMod('minifiche', 'Mini-fiches', true); |
| 325 | $view->addMod('trombi', 'Trombinoscope', false, array('with_admin' => false, 'with_promo' => true)); |
| 326 | // TODO: Reactivate when the new map is completed. |
| 327 | // $view->addMod('geoloc', 'Planisphère', false, array('with_annu' => 'carnet/contacts/search')); |
| 328 | $view->apply('carnet/contacts', $page, $action, $subaction); |
| 329 | //if ($action != 'geoloc' || ($search && !$ssaction) || (!$search && !$subaction)) { |
| 330 | $page->changeTpl('carnet/mescontacts.tpl'); |
| 331 | //} |
| 332 | } |
| 333 | |
| 334 | function handler_pdf(&$page, $arg0 = null, $arg1 = null) |
| 335 | { |
| 336 | $this->load('contacts.pdf.inc.php'); |
| 337 | $user = S::user(); |
| 338 | |
| 339 | Platal::session()->close(); |
| 340 | |
| 341 | $order = array(new UFO_Name(Profile::LASTNAME), new UFO_Name(Profile::FIRSTNAME)); |
| 342 | if ($arg0 == 'promo') { |
| 343 | $order = array_unshift($order, new UFO_Promo()); |
| 344 | } else { |
| 345 | $order[] = new UFO_Promo(); |
| 346 | } |
| 347 | $filter = new UserFilter(new UFC_Contact($user), $order); |
| 348 | |
| 349 | $pdf = new ContactsPDF(); |
| 350 | |
| 351 | $it = $filter->iterProfiles(); |
| 352 | while ($p = $it->next()) { |
| 353 | $pdf = ContactsPDF::addContact($pdf, $p, $arg0 == 'photos' || $arg1 == 'photos'); |
| 354 | } |
| 355 | $pdf->Output(); |
| 356 | |
| 357 | exit; |
| 358 | } |
| 359 | |
| 360 | function handler_rss(&$page, $user = null, $hash = null) |
| 361 | { |
| 362 | $this->load('feed.inc.php'); |
| 363 | $feed = new CarnetFeed(); |
| 364 | return $feed->run($page, $user, $hash); |
| 365 | } |
| 366 | |
| 367 | function buildBirthRef(Profile $profile) |
| 368 | { |
| 369 | $date = strtotime($profile->birthdate); |
| 370 | $tomorrow = $date + 86400; |
| 371 | return array( |
| 372 | 'timestamp' => $date, |
| 373 | 'date' => date('Ymd', $date), |
| 374 | 'tomorrow' => date('Ymd', $tomorrow), |
| 375 | 'hruid' => $profile->hrid(), |
| 376 | 'summary' => 'Anniversaire de ' . $profile->fullName(true) |
| 377 | ); |
| 378 | } |
| 379 | |
| 380 | function handler_csv_birthday(&$page, $alias = null, $hash = null) |
| 381 | { |
| 382 | $user = Platal::session()->tokenAuth($alias, $hash); |
| 383 | if (is_null($user)) { |
| 384 | if (S::logged()) { |
| 385 | $user == S::user(); |
| 386 | } else { |
| 387 | return PL_FORBIDDEN; |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | $page->changeTpl('carnet/calendar.outlook.tpl', NO_SKIN); |
| 392 | $filter = new UserFilter(new UFC_Contact($user)); |
| 393 | $profiles = $filter->iterProfiles(); |
| 394 | $page->assign('events', PlIteratorUtils::map($profiles, array($this, 'buildBirthRef'))); |
| 395 | $years = array(date("Y")); |
| 396 | for ($i = 1; $i <= 10; ++$i) { |
| 397 | $years[] = $years[0] + $i; |
| 398 | } |
| 399 | $page->assign('years', $years); |
| 400 | $lang = 'fr'; |
| 401 | if (preg_match('/([a-zA-Z]{2,8})($|[^a-zA-Z])/', $_SERVER['HTTP_ACCEPT_LANGUAGE'], $matches)) { |
| 402 | $lang = strtolower($matches[1]); |
| 403 | } |
| 404 | $page->assign('lang', $lang); |
| 405 | if ($lang == 'fr') { |
| 406 | $encoding = 'iso8859-15'; |
| 407 | } else { |
| 408 | $encoding = 'utf-8'; |
| 409 | } |
| 410 | pl_content_headers("text/comma-separated-values;charset=".$encoding); |
| 411 | } |
| 412 | |
| 413 | function handler_ical(&$page, $alias = null, $hash = null) |
| 414 | { |
| 415 | $user = Platal::session()->tokenAuth($alias, $hash); |
| 416 | if (is_null($user)) { |
| 417 | if (S::logged()) { |
| 418 | $user == S::user(); |
| 419 | } else { |
| 420 | return PL_FORBIDDEN; |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | require_once 'ical.inc.php'; |
| 425 | $page->changeTpl('carnet/calendar.tpl', NO_SKIN); |
| 426 | $page->register_function('display_ical', 'display_ical'); |
| 427 | |
| 428 | $filter = new UserFilter(new UFC_Contact($user)); |
| 429 | $profiles = $filter->iterProfiles(); |
| 430 | $page->assign('events', PlIteratorUtils::map($profiles, array($this, 'buildBirthRef'))); |
| 431 | |
| 432 | pl_content_headers("text/calendar"); |
| 433 | } |
| 434 | |
| 435 | function handler_vcard(&$page, $photos = null) |
| 436 | { |
| 437 | $pf = new ProfileFilter(new UFC_Contact(S::user())); |
| 438 | $vcard = new VCard($photos == 'photos'); |
| 439 | $vcard->addProfiles($pf->getProfiles()); |
| 440 | $vcard->show(); |
| 441 | } |
| 442 | |
| 443 | function handler_csv(&$page, $alias = null, $hash = null) |
| 444 | { |
| 445 | $user = Platal::session()->tokenAuth($alias, $hash); |
| 446 | if (is_null($user)) { |
| 447 | if (S::logged()) { |
| 448 | $user == S::user(); |
| 449 | } else { |
| 450 | return PL_FORBIDDEN; |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | $page->changeTpl('carnet/mescontacts.outlook.tpl', NO_SKIN); |
| 455 | $pf = new ProfileFilter(new UFC_Contact($user)); |
| 456 | require_once 'carnet/outlook.inc.php'; |
| 457 | Outlook::output_profiles($pf->getProfiles(), 'fr'); |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
| 462 | ?> |