| 1 | <?php |
| 2 | /*************************************************************************** |
| 3 | * Copyright (C) 2003-2009 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), |
| 28 | 'carnet/panel' => $this->make_hook('panel', AUTH_COOKIE), |
| 29 | 'carnet/notifs' => $this->make_hook('notifs', AUTH_COOKIE), |
| 30 | |
| 31 | 'carnet/contacts' => $this->make_hook('contacts', AUTH_COOKIE), |
| 32 | 'carnet/contacts/pdf' => $this->make_hook('pdf', AUTH_COOKIE, 'user', NO_HTTPS), |
| 33 | 'carnet/contacts/ical' => $this->make_hook('ical', AUTH_PUBLIC, 'user', NO_HTTPS), |
| 34 | 'carnet/contacts/vcard' => $this->make_hook('vcard', AUTH_COOKIE, 'user', NO_HTTPS), |
| 35 | |
| 36 | 'carnet/rss' => $this->make_hook('rss', AUTH_PUBLIC, 'user', NO_HTTPS), |
| 37 | ); |
| 38 | } |
| 39 | |
| 40 | function _add_rss_link(&$page) |
| 41 | { |
| 42 | if (!S::hasAuthToken()) { |
| 43 | return; |
| 44 | } |
| 45 | $page->setRssLink('Polytechnique.org :: Carnet', |
| 46 | '/carnet/rss/'.S::v('hruid').'/'.S::v('token').'/rss.xml'); |
| 47 | } |
| 48 | |
| 49 | function handler_index(&$page) |
| 50 | { |
| 51 | $page->changeTpl('carnet/index.tpl'); |
| 52 | $page->setTitle('Mon carnet'); |
| 53 | $this->_add_rss_link($page); |
| 54 | } |
| 55 | |
| 56 | function handler_panel(&$page) |
| 57 | { |
| 58 | $page->changeTpl('carnet/panel.tpl'); |
| 59 | |
| 60 | if (Get::has('read')) { |
| 61 | S::set('watch_last', Get::v('read')); |
| 62 | Platal::session()->updateNbNotifs(); |
| 63 | pl_redirect('carnet/panel'); |
| 64 | } |
| 65 | |
| 66 | require_once 'notifs.inc.php'; |
| 67 | |
| 68 | $page->assign('now',date('YmdHis')); |
| 69 | $notifs = new Notifs(S::v('uid'), true); |
| 70 | |
| 71 | $page->assign('notifs', $notifs); |
| 72 | $page->assign('today', date('Y-m-d')); |
| 73 | $this->_add_rss_link($page); |
| 74 | } |
| 75 | |
| 76 | private function getSinglePromotion(PlPage &$page, $promo) |
| 77 | { |
| 78 | if (!ctype_digit($promo) || $promo < 1920 || $promo > date('Y')) { |
| 79 | $page->trigError('Promotion invalide : ' . $promo); |
| 80 | return null; |
| 81 | } |
| 82 | return (int)$promo; |
| 83 | } |
| 84 | |
| 85 | private function getPromo(PlPage &$page, $promo) |
| 86 | { |
| 87 | if (strpos($promo, '-') === false) { |
| 88 | $promo = $this->getSinglePromotion($page, $promo); |
| 89 | if (!$promo) { |
| 90 | return null; |
| 91 | } else { |
| 92 | return array($promo); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | list($promo1, $promo2) = explode('-', $promo); |
| 97 | $promo1 = $this->getSinglePromotion($page, $promo1); |
| 98 | if (!$promo1) { |
| 99 | return null; |
| 100 | } |
| 101 | $promo2 = $this->getSinglePromotion($page, $promo2); |
| 102 | if (!$promo2) { |
| 103 | return null; |
| 104 | } |
| 105 | if ($promo1 > $promo2) { |
| 106 | $page->trigError("Intervale non valide : " . $promo); |
| 107 | return null; |
| 108 | } |
| 109 | $array = array(); |
| 110 | for ($i = $promo1 ; $i <= $promo2 ; ++$i) { |
| 111 | $array[] = $i; |
| 112 | } |
| 113 | return $array; |
| 114 | } |
| 115 | |
| 116 | private function addPromo(PlPage &$page, $promo) |
| 117 | { |
| 118 | $promos = $this->getPromo($page, $promo); |
| 119 | if (!$promos || count($promos) == 0) { |
| 120 | return; |
| 121 | } |
| 122 | $to_add = array(); |
| 123 | foreach ($promos as $promo) { |
| 124 | $to_add[] = XDB::format('({?}, {?})', S::i('uid'), $promo); |
| 125 | } |
| 126 | XDB::execute('INSERT IGNORE INTO watch_promo (uid, promo) |
| 127 | VALUES ' . implode(', ', $to_add)); |
| 128 | } |
| 129 | |
| 130 | private function delPromo(PlPage &$page, $promo) |
| 131 | { |
| 132 | $promos = $this->getPromo($page, $promo); |
| 133 | if (!$promos || count($promos) == 0) { |
| 134 | return; |
| 135 | } |
| 136 | $to_delete = array(); |
| 137 | foreach ($promos as $promo) { |
| 138 | $to_delete[] = XDB::format('{?}', $promo); |
| 139 | } |
| 140 | XDB::execute('DELETE FROM watch_promo |
| 141 | WHERE ' . XDB::format('uid = {?}', S::i('uid')) . ' |
| 142 | AND promo IN (' . implode(', ', $to_delete) . ')'); |
| 143 | } |
| 144 | |
| 145 | public function addNonRegistered(PlPage &$page, PlUser &$user) |
| 146 | { |
| 147 | XDB::execute('INSERT IGNORE INTO watch_nonins (uid, ni_id) |
| 148 | VALUES ({?}, {?})', S::i('uid'), $user->id()); |
| 149 | } |
| 150 | |
| 151 | public function delNonRegistered(PlPage &$page, PlUser &$user) |
| 152 | { |
| 153 | XDB::execute('DELETE FROM watch_nonins |
| 154 | WHERE uid = {?} AND ni_id = {?}', |
| 155 | S::i('uid'), $user->id()); |
| 156 | } |
| 157 | |
| 158 | public function handler_notifs(&$page, $action = null, $arg = null) |
| 159 | { |
| 160 | $page->changeTpl('carnet/notifs.tpl'); |
| 161 | |
| 162 | if ($action) { |
| 163 | S::assert_xsrf_token(); |
| 164 | switch ($action) { |
| 165 | case 'add_promo': |
| 166 | $this->addPromo($page, $arg); |
| 167 | break; |
| 168 | |
| 169 | case 'del_promo': |
| 170 | $this->delPromo($page, $arg); |
| 171 | break; |
| 172 | |
| 173 | case 'del_nonins': |
| 174 | $user = User::get($arg); |
| 175 | if ($user) { |
| 176 | $this->delNonRegistered($page, $user); |
| 177 | } |
| 178 | break; |
| 179 | |
| 180 | case 'add_nonins': |
| 181 | $user = User::get($arg); |
| 182 | if ($user) { |
| 183 | $this->addNonRegistered($page, $user); |
| 184 | } |
| 185 | break; |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | if (Env::has('subs')) { |
| 190 | S::assert_xsrf_token(); |
| 191 | $flags = new PlFlagSet(); |
| 192 | foreach (Env::v('sub') as $key=>$value) { |
| 193 | $flags->addFlag($key, $value); |
| 194 | } |
| 195 | XDB::execute('UPDATE watch |
| 196 | SET actions = {?} |
| 197 | WHERE uid = {?}', $flags, S::i('uid')); |
| 198 | } |
| 199 | |
| 200 | if (Env::has('flags_contacts')) { |
| 201 | S::assert_xsrf_token(); |
| 202 | XDB::execute('UPDATE watch |
| 203 | SET ' . XDB::changeFlag('flags', 'contacts', Env::b('contacts')) . ' |
| 204 | WHERE uid = {?}', S::i('uid')); |
| 205 | } |
| 206 | |
| 207 | if (Env::has('flags_mail')) { |
| 208 | S::assert_xsrf_token(); |
| 209 | XDB::execute('UPDATE watch |
| 210 | SET ' . XDB::changeFlag('flags', 'mail', Env::b('mail')) . ' |
| 211 | WHERE uid = {?}', S::i('uid')); |
| 212 | } |
| 213 | |
| 214 | $user = S::user(); |
| 215 | $nonins = new UserFilter(new UFC_WatchRegistration($user)); |
| 216 | |
| 217 | $promo = XDB::fetchColumn('SELECT promo |
| 218 | FROM watch_promo |
| 219 | WHERE uid = {?} |
| 220 | ORDER BY promo', S::i('uid')); |
| 221 | $page->assign('promo_count', count($promo)); |
| 222 | $ranges = array(); |
| 223 | $range_start = null; |
| 224 | $range_end = null; |
| 225 | foreach ($promo as $p) { |
| 226 | if (is_null($range_start)) { |
| 227 | $range_start = $range_end = $p; |
| 228 | } else if ($p != $range_end + 1) { |
| 229 | $ranges[] = array($range_start, $range_end); |
| 230 | $range_start = $range_end = $p; |
| 231 | } else { |
| 232 | $range_end = $p; |
| 233 | } |
| 234 | } |
| 235 | $ranges[] = array($range_start, $range_end); |
| 236 | $page->assign('promo_ranges', $ranges); |
| 237 | $page->assign('nonins', $nonins->getUsers()); |
| 238 | |
| 239 | list($flags, $actions) = XDB::fetchOneRow('SELECT flags, actions |
| 240 | FROM watch |
| 241 | WHERE uid = {?}', S::i('uid')); |
| 242 | $flags = new PlFlagSet($flags); |
| 243 | $actions = new PlFlagSet($actions); |
| 244 | $page->assign('flags', $flags); |
| 245 | $page->assign('actions', $actions); |
| 246 | } |
| 247 | |
| 248 | function handler_contacts(&$page, $action = null, $subaction = null, $ssaction = null) |
| 249 | { |
| 250 | $page->setTitle('Mes contacts'); |
| 251 | $this->_add_rss_link($page); |
| 252 | |
| 253 | $uid = S::v('uid'); |
| 254 | $user = Env::v('user'); |
| 255 | |
| 256 | // For XSRF protection, checks both the normal xsrf token, and the special RSS token. |
| 257 | // It allows direct linking to contact adding in the RSS feed. |
| 258 | if (Env::v('action') && Env::v('token') !== S::v('token')) { |
| 259 | S::assert_xsrf_token(); |
| 260 | } |
| 261 | switch (Env::v('action')) { |
| 262 | case 'retirer': |
| 263 | if (($user = User::get(Env::v('user')))) { |
| 264 | if (XDB::execute("DELETE FROM contacts |
| 265 | WHERE uid = {?} AND contact = {?}", $uid, $user->id())) { |
| 266 | $page->trigSuccess("Contact retiré !"); |
| 267 | } |
| 268 | } |
| 269 | break; |
| 270 | |
| 271 | case 'ajouter': |
| 272 | if (($user = User::get(Env::v('user')))) { |
| 273 | if (XDB::execute("REPLACE INTO contacts (uid, contact) |
| 274 | VALUES ({?}, {?})", $uid, $user->id())) { |
| 275 | $page->trigSuccess('Contact ajouté !'); |
| 276 | } else { |
| 277 | $page->trigWarning('Contact déjà dans la liste !'); |
| 278 | } |
| 279 | } |
| 280 | break; |
| 281 | } |
| 282 | |
| 283 | $search = false; |
| 284 | if ($action == 'search') { |
| 285 | $action = $subaction; |
| 286 | $subaction = $ssaction; |
| 287 | $search = true; |
| 288 | } |
| 289 | if ($search && trim(Env::v('quick'))) { |
| 290 | require_once 'userset.inc.php'; |
| 291 | $base = 'carnet/contacts/search'; |
| 292 | |
| 293 | Platal::load('search', 'classes.inc.php'); |
| 294 | ThrowError::$throwHook = array($this, 'searchErrorHandler'); |
| 295 | $view = new SearchSet(true, false, "INNER JOIN contacts AS c2 ON (u.user_id = c2.contact)", "c2.uid = $uid"); |
| 296 | } else { |
| 297 | $base = 'carnet/contacts'; |
| 298 | $view = new UserSet("INNER JOIN contacts AS c2 ON (u.user_id = c2.contact)", " c2.uid = $uid "); |
| 299 | } |
| 300 | $view->addMod('minifiche', 'Mini-fiches', true); |
| 301 | $view->addMod('trombi', 'Trombinoscope', false, array('with_admin' => false, 'with_promo' => true)); |
| 302 | $view->addMod('geoloc', 'Planisphère', false, array('with_annu' => 'carnet/contacts/search')); |
| 303 | $view->apply($base, $page, $action, $subaction); |
| 304 | if ($action != 'geoloc' || ($search && !$ssaction) || (!$search && !$subaction)) { |
| 305 | $page->changeTpl('carnet/mescontacts.tpl'); |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | function handler_pdf(&$page, $arg0 = null, $arg1 = null) |
| 310 | { |
| 311 | $this->load('contacts.pdf.inc.php'); |
| 312 | require_once 'user.func.inc.php'; |
| 313 | |
| 314 | Platal::session()->close(); |
| 315 | |
| 316 | $sql = "SELECT a.alias |
| 317 | FROM aliases AS a |
| 318 | INNER JOIN auth_user_md5 AS u ON ( a.id = u.user_id ) |
| 319 | INNER JOIN contacts AS c ON ( a.id = c.contact ) |
| 320 | WHERE c.uid = {?} AND a.type='a_vie'"; |
| 321 | if ($arg0 == 'promo') { |
| 322 | $sql .= ' ORDER BY u.promo, u.nom, u.prenom'; |
| 323 | } else { |
| 324 | $sql .= ' ORDER BY u.nom, u.prenom, u.promo'; |
| 325 | } |
| 326 | |
| 327 | $citer = XDB::iterRow($sql, S::v('uid')); |
| 328 | $pdf = new ContactsPDF(); |
| 329 | |
| 330 | while (list($alias) = $citer->next()) { |
| 331 | $user = get_user_details($alias); |
| 332 | foreach ($user as &$value) { |
| 333 | if (is_utf8($value)) { |
| 334 | $value = utf8_decode($value); |
| 335 | } |
| 336 | } |
| 337 | $pdf = ContactsPDF::addContact($pdf, $user, $arg0 == 'photos' || $arg1 == 'photos'); |
| 338 | } |
| 339 | $pdf->Output(); |
| 340 | |
| 341 | exit; |
| 342 | } |
| 343 | |
| 344 | function handler_rss(&$page, $user = null, $hash = null) |
| 345 | { |
| 346 | $this->load('feed.inc.php'); |
| 347 | $feed = new CarnetFeed(); |
| 348 | return $feed->run($page, $user, $hash); |
| 349 | } |
| 350 | |
| 351 | function handler_ical(&$page, $alias = null, $hash = null) |
| 352 | { |
| 353 | $user = Platal::session()->tokenAuth($alias, $hash); |
| 354 | if (is_null($user)) { |
| 355 | if (S::logged()) { |
| 356 | $user == S::user(); |
| 357 | } else { |
| 358 | return PL_FORBIDDEN; |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | require_once 'ical.inc.php'; |
| 363 | $page->changeTpl('carnet/calendar.tpl', NO_SKIN); |
| 364 | $page->register_function('display_ical', 'display_ical'); |
| 365 | |
| 366 | $res = XDB::iterRow( |
| 367 | 'SELECT u.prenom, |
| 368 | IF(u.nom_usage = \'\',u.nom,u.nom_usage) AS nom, |
| 369 | u.promo, |
| 370 | u.naissance, |
| 371 | DATE_ADD(u.naissance, INTERVAL 1 DAY) AS end, |
| 372 | u.date_ins, |
| 373 | u.hruid |
| 374 | FROM contacts AS c |
| 375 | INNER JOIN auth_user_md5 AS u ON (u.user_id = c.contact) |
| 376 | INNER JOIN aliases AS a ON (u.user_id = a.id AND a.type = \'a_vie\') |
| 377 | WHERE c.uid = {?}', $user->id()); |
| 378 | |
| 379 | $annivs = Array(); |
| 380 | while (list($prenom, $nom, $promo, $naissance, $end, $ts, $hruid) = $res->next()) { |
| 381 | $naissance = str_replace('-', '', $naissance); |
| 382 | $end = str_replace('-', '', $end); |
| 383 | $annivs[] = array( |
| 384 | 'timestamp' => strtotime($ts), |
| 385 | 'date' => $naissance, |
| 386 | 'tomorrow' => $end, |
| 387 | 'hruid' => $hruid, |
| 388 | 'summary' => 'Anniversaire de '.$prenom |
| 389 | .' '.$nom.' - x '.$promo, |
| 390 | ); |
| 391 | } |
| 392 | $page->assign('events', $annivs); |
| 393 | |
| 394 | header('Content-Type: text/calendar; charset=utf-8'); |
| 395 | } |
| 396 | |
| 397 | function handler_vcard(&$page, $photos = null) |
| 398 | { |
| 399 | $res = XDB::query('SELECT contact |
| 400 | FROM contacts |
| 401 | WHERE uid = {?}', S::v('uid')); |
| 402 | $vcard = new VCard($photos == 'photos'); |
| 403 | $vcard->addUsers($res->fetchColumn()); |
| 404 | $vcard->show(); |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
| 409 | ?> |