Commit | Line | Data |
---|---|---|
0337d704 | 1 | <?php |
2 | /*************************************************************************** | |
179afa7f | 3 | * Copyright (C) 2003-2008 Polytechnique.org * |
0337d704 | 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 | ||
c9f82d49 | 22 | class CarnetModule extends PLModule |
23 | { | |
24 | function handlers() | |
25 | { | |
26 | return array( | |
5e193297 | 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), | |
fc12cbd1 | 30 | |
5e193297 | 31 | 'carnet/contacts' => $this->make_hook('contacts', AUTH_COOKIE), |
8fc4efa3 | 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), | |
b48a0758 | 35 | |
8fc4efa3 | 36 | 'carnet/rss' => $this->make_hook('rss', AUTH_PUBLIC, 'user', NO_HTTPS), |
c9f82d49 | 37 | ); |
38 | } | |
0337d704 | 39 | |
8d8f7607 | 40 | function on_subscribe($forlife, $uid, $promo, $password) |
41 | { | |
42 | require_once 'notifs.inc.php'; | |
43 | register_watch_op($uid, WATCH_INSCR); | |
44 | inscription_notifs_base($uid); | |
45 | } | |
46 | ||
fc12cbd1 | 47 | function _add_rss_link(&$page) |
48 | { | |
cab08090 | 49 | if (!S::has('core_rss_hash')) { |
fc12cbd1 | 50 | return; |
fd8f77de | 51 | } |
162370e7 | 52 | $page->setRssLink('Polytechnique.org :: Carnet', |
53 | '/carnet/rss/'.S::v('forlife') .'/'.S::v('core_rss_hash').'/rss.xml'); | |
fc12cbd1 | 54 | } |
55 | ||
b48a0758 | 56 | function handler_index(&$page) |
57 | { | |
58 | $page->changeTpl('carnet/index.tpl'); | |
46f272fe | 59 | $page->setTitle('Mon carnet'); |
b48a0758 | 60 | $this->_add_rss_link($page); |
b48a0758 | 61 | } |
62 | ||
fc12cbd1 | 63 | function handler_panel(&$page) |
64 | { | |
65 | $page->changeTpl('carnet/panel.tpl'); | |
66 | ||
67 | if (Get::has('read')) { | |
ebfdf077 FB |
68 | S::set('watch_last', Get::v('read')); |
69 | Platal::session()->updateNbNotifs(); | |
8b00e0e0 | 70 | pl_redirect('carnet/panel'); |
fc12cbd1 | 71 | } |
72 | ||
73 | require_once 'notifs.inc.php'; | |
74 | ||
75 | $page->assign('now',date('YmdHis')); | |
cab08090 | 76 | $notifs = new Notifs(S::v('uid'), true); |
fc12cbd1 | 77 | |
78 | $page->assign('notifs', $notifs); | |
79 | $page->assign('today', date('Y-m-d')); | |
80 | $this->_add_rss_link($page); | |
fc12cbd1 | 81 | } |
82 | ||
b48a0758 | 83 | function _handler_notifs_promos(&$page, &$watch, $action, $arg) |
84 | { | |
85 | if(preg_match('!^ *(\d{4}) *$!', $arg, $matches)) { | |
86 | $p = intval($matches[1]); | |
87 | if($p<1900 || $p>2100) { | |
a7d35093 | 88 | $page->trigError("la promo entrée est invalide"); |
b48a0758 | 89 | } else { |
90 | if ($action == 'add_promo') { | |
91 | $watch->_promos->add($p); | |
92 | } else { | |
93 | $watch->_promos->del($p); | |
94 | } | |
95 | } | |
96 | } elseif (preg_match('!^ *(\d{4}) *- *(\d{4}) *$!', $arg, $matches)) { | |
97 | $p1 = intval($matches[1]); | |
98 | $p2 = intval($matches[2]); | |
99 | if($p1<1900 || $p1>2100) { | |
a7d35093 | 100 | $page->trigError('la première promo de la plage entrée est invalide'); |
b48a0758 | 101 | } elseif($p2<1900 || $p2>2100) { |
a7d35093 | 102 | $page->trigError('la seconde promo de la plage entrée est invalide'); |
b48a0758 | 103 | } else { |
104 | if ($action == 'add_promo') { | |
105 | $watch->_promos->addRange($p1, $p2); | |
106 | } else { | |
107 | $watch->_promos->delRange($p1, $p2); | |
108 | } | |
109 | } | |
110 | } else { | |
a7d35093 | 111 | $page->trigError("La promo (ou la plage de promo) entrée est dans un format incorrect."); |
b48a0758 | 112 | } |
113 | } | |
114 | ||
115 | function handler_notifs(&$page, $action = null, $arg = null) | |
116 | { | |
b48a0758 | 117 | $page->changeTpl('carnet/notifs.tpl'); |
118 | ||
119 | require_once 'notifs.inc.php'; | |
120 | ||
cab08090 | 121 | $watch = new Watch(S::v('uid')); |
b48a0758 | 122 | |
08cce2ff | 123 | $res = XDB::query("SELECT promo_sortie |
b48a0758 | 124 | FROM auth_user_md5 |
125 | WHERE user_id = {?}", | |
cab08090 | 126 | S::v('uid', -1)); |
b48a0758 | 127 | $promo_sortie = $res->fetchOneCell(); |
128 | $page->assign('promo_sortie', $promo_sortie); | |
129 | ||
40d428d8 VZ |
130 | if ($action) { |
131 | S::assert_xsrf_token(); | |
e6bf9216 | 132 | } |
b48a0758 | 133 | switch ($action) { |
134 | case 'add_promo': | |
135 | case 'del_promo': | |
136 | $this->_handler_notifs_promos($page, $watch, $action, $arg); | |
137 | break; | |
138 | ||
139 | case 'del_nonins': | |
140 | $watch->_nonins->del($arg); | |
141 | break; | |
142 | ||
143 | case 'add_nonins': | |
144 | $watch->_nonins->add($arg); | |
145 | break; | |
146 | } | |
147 | ||
40d428d8 VZ |
148 | if (Env::has('subs')) { |
149 | S::assert_xsrf_token(); | |
e6bf9216 | 150 | $watch->_subs->update('sub'); |
e6bf9216 VZ |
151 | } |
152 | ||
40d428d8 VZ |
153 | if (Env::has('flags_contacts')) { |
154 | S::assert_xsrf_token(); | |
5e2307dc | 155 | $watch->watch_contacts = Env::b('contacts'); |
b48a0758 | 156 | $watch->saveFlags(); |
157 | } | |
e6bf9216 | 158 | |
40d428d8 VZ |
159 | if (Env::has('flags_mail')) { |
160 | S::assert_xsrf_token(); | |
e6bf9216 | 161 | $watch->watch_mail = Env::b('mail'); |
b48a0758 | 162 | $watch->saveFlags(); |
163 | } | |
164 | ||
165 | $page->assign_by_ref('watch', $watch); | |
b48a0758 | 166 | } |
167 | ||
168 | function _get_list($offset, $limit) { | |
cab08090 | 169 | $uid = S::v('uid'); |
08cce2ff | 170 | $res = XDB::query("SELECT COUNT(*) FROM contacts WHERE uid = {?}", $uid); |
b48a0758 | 171 | $total = $res->fetchOneCell(); |
172 | ||
5e2307dc | 173 | $order = Get::v('order'); |
b48a0758 | 174 | $orders = Array( |
175 | 'nom' => 'nom DESC, u.prenom, u.promo', | |
176 | 'promo' => 'promo DESC, nom, u.prenom', | |
177 | 'last' => 'u.date DESC, nom, u.prenom, promo'); | |
178 | if ($order != 'promo' && $order != 'last') | |
179 | $order = 'nom'; | |
180 | $order = $orders[$order]; | |
5e2307dc | 181 | if (Get::v('inv') == '') |
b48a0758 | 182 | $order = str_replace(" DESC,", ",", $order); |
183 | ||
08cce2ff | 184 | $res = XDB::query(" |
b48a0758 | 185 | SELECT u.prenom, IF(u.nom_usage='',u.nom,u.nom_usage) AS nom, a.alias AS forlife, u.promo |
186 | FROM contacts AS c | |
187 | INNER JOIN auth_user_md5 AS u ON (u.user_id = c.contact) | |
188 | INNER JOIN aliases AS a ON (u.user_id = a.id AND a.type='a_vie') | |
189 | WHERE c.uid = {?} | |
190 | ORDER BY $order | |
191 | LIMIT {?}, {?}", $uid, $offset*$limit, $limit); | |
192 | $list = $res->fetchAllAssoc(); | |
193 | ||
194 | return Array($total, $list); | |
195 | } | |
196 | ||
3b2f9d11 | 197 | function searchErrorHandler($explain) { |
d7610c35 | 198 | $page =& Platal::page(); |
a7d35093 | 199 | $page->trigError($explain); |
3b2f9d11 | 200 | $this->handler_contacts($page); |
201 | } | |
202 | ||
a2aa8436 | 203 | function handler_contacts(&$page, $action = null, $subaction = null, $ssaction = null) |
b48a0758 | 204 | { |
46f272fe | 205 | $page->setTitle('Mes contacts'); |
59a61432 | 206 | $this->_add_rss_link($page); |
b48a0758 | 207 | |
cab08090 | 208 | $uid = S::v('uid'); |
5e2307dc | 209 | $user = Env::v('user'); |
b48a0758 | 210 | |
e6bf9216 VZ |
211 | // For XSRF protection, checks both the normal xsrf token, and the special RSS token. |
212 | // It allows direct linking to contact adding in the RSS feed. | |
40d428d8 VZ |
213 | if (Env::v('action') && Env::v('token') !== S::v('core_rss_hash')) { |
214 | S::assert_xsrf_token(); | |
215 | } | |
216 | switch (Env::v('action')) { | |
217 | case 'retirer': | |
d6460651 VZ |
218 | if (($user = User::get(Env::v('user')))) { |
219 | if (XDB::execute("DELETE FROM contacts | |
220 | WHERE uid = {?} AND contact = {?}", $uid, $user->id())) { | |
a7d35093 | 221 | $page->trigSuccess("Contact retiré !"); |
b48a0758 | 222 | } |
223 | } | |
224 | break; | |
225 | ||
40d428d8 | 226 | case 'ajouter': |
d6460651 VZ |
227 | if (($user = User::get(Env::v('user')))) { |
228 | if (XDB::execute("REPLACE INTO contacts (uid, contact) | |
229 | VALUES ({?}, {?})", $uid, $user->id())) { | |
a7d35093 | 230 | $page->trigSuccess('Contact ajouté !'); |
b48a0758 | 231 | } else { |
a7d35093 | 232 | $page->trigWarning('Contact déjà dans la liste !'); |
b48a0758 | 233 | } |
234 | } | |
d6460651 | 235 | break; |
b48a0758 | 236 | } |
237 | ||
a2aa8436 | 238 | $search = false; |
239 | if ($action == 'search') { | |
240 | $action = $subaction; | |
241 | $subaction = $ssaction; | |
242 | $search = true; | |
243 | } | |
244 | if ($search && trim(Env::v('quick'))) { | |
245 | require_once 'userset.inc.php'; | |
246 | $base = 'carnet/contacts/search'; | |
3b2f9d11 | 247 | |
adb07f6f | 248 | Platal::load('search', 'classes.inc.php'); |
3b2f9d11 | 249 | ThrowError::$throwHook = array($this, 'searchErrorHandler'); |
250 | $view = new SearchSet(true, false, "INNER JOIN contacts AS c2 ON (u.user_id = c2.contact)", "c2.uid = $uid"); | |
a2aa8436 | 251 | } else { |
252 | $base = 'carnet/contacts'; | |
253 | $view = new UserSet("INNER JOIN contacts AS c2 ON (u.user_id = c2.contact)", " c2.uid = $uid "); | |
254 | } | |
1fae7605 | 255 | $view->addMod('minifiche', 'Mini-fiches', true); |
8c4a0c30 | 256 | $view->addMod('trombi', 'Trombinoscope', false, array('with_admin' => false, 'with_promo' => true)); |
a2aa8436 | 257 | $view->addMod('geoloc', 'Planisphère', false, array('with_annu' => 'carnet/contacts/search')); |
258 | $view->apply($base, $page, $action, $subaction); | |
259 | if ($action != 'geoloc' || ($search && !$ssaction) || (!$search && !$subaction)) { | |
8c4a0c30 | 260 | $page->changeTpl('carnet/mescontacts.tpl'); |
b48a0758 | 261 | } |
b48a0758 | 262 | } |
263 | ||
264 | function handler_pdf(&$page, $arg0 = null, $arg1 = null) | |
265 | { | |
460d8f55 | 266 | $this->load('contacts.pdf.inc.php'); |
b48a0758 | 267 | require_once 'user.func.inc.php'; |
268 | ||
732e5855 | 269 | Platal::session()->close(); |
b48a0758 | 270 | |
271 | $sql = "SELECT a.alias | |
272 | FROM aliases AS a | |
273 | INNER JOIN auth_user_md5 AS u ON ( a.id = u.user_id ) | |
274 | INNER JOIN contacts AS c ON ( a.id = c.contact ) | |
275 | WHERE c.uid = {?} AND a.type='a_vie'"; | |
276 | if ($arg0 == 'promo') { | |
277 | $sql .= ' ORDER BY u.promo, u.nom, u.prenom'; | |
278 | } else { | |
279 | $sql .= ' ORDER BY u.nom, u.prenom, u.promo'; | |
280 | } | |
281 | ||
cab08090 | 282 | $citer = XDB::iterRow($sql, S::v('uid')); |
b48a0758 | 283 | $pdf = new ContactsPDF(); |
284 | ||
285 | while (list($alias) = $citer->next()) { | |
286 | $user = get_user_details($alias); | |
ac73e294 | 287 | foreach ($user as &$value) { |
288 | if (is_utf8($value)) { | |
289 | $value = utf8_decode($value); | |
290 | } | |
291 | } | |
93c099e1 | 292 | $pdf = ContactsPDF::addContact($pdf, $user, $arg0 == 'photos' || $arg1 == 'photos'); |
b48a0758 | 293 | } |
294 | $pdf->Output(); | |
295 | ||
296 | exit; | |
297 | } | |
298 | ||
c9f82d49 | 299 | function handler_rss(&$page, $user = null, $hash = null) |
300 | { | |
460d8f55 | 301 | $this->load('feed.inc.php'); |
4e9a7a6d FB |
302 | $feed = new CarnetFeed(); |
303 | return $feed->run($page, $user, $hash); | |
c9f82d49 | 304 | } |
fbfb06dc | 305 | |
963c0b2e | 306 | function handler_ical(&$page, $alias = null, $hash = null) |
fbfb06dc | 307 | { |
963c0b2e | 308 | require_once 'rss.inc.php'; |
309 | $uid = init_rss(null, $alias, $hash, false); | |
310 | if (S::logged()) { | |
311 | if (!$uid) { | |
312 | $uid = S::i('uid'); | |
313 | } else if ($uid != S::i('uid')) { | |
963c0b2e | 314 | send_warning_email("Récupération d\'un autre utilisateur ($uid)"); |
315 | } | |
316 | } else if (!$uid) { | |
317 | exit; | |
318 | } | |
99544d53 | 319 | require_once 'ical.inc.php'; |
801fcad8 | 320 | $page->changeTpl('carnet/calendar.tpl', NO_SKIN); |
3585b0c8 | 321 | $page->register_function('display_ical', 'display_ical'); |
fbfb06dc | 322 | |
f3b3f363 | 323 | $res = XDB::iterRow( |
324 | 'SELECT u.prenom, | |
325 | IF(u.nom_usage = \'\',u.nom,u.nom_usage) AS nom, | |
326 | u.promo, | |
327 | u.naissance, | |
328 | DATE_ADD(u.naissance, INTERVAL 1 DAY) AS end, | |
329 | u.date_ins, | |
330 | a.alias AS forlife | |
331 | FROM contacts AS c | |
332 | INNER JOIN auth_user_md5 AS u ON (u.user_id = c.contact) | |
333 | INNER JOIN aliases AS a ON (u.user_id = a.id AND a.type = \'a_vie\') | |
963c0b2e | 334 | WHERE c.uid = {?}', $uid); |
f3b3f363 | 335 | |
336 | $annivs = Array(); | |
337 | while (list($prenom, $nom, $promo, $naissance, $end, $ts, $forlife) = $res->next()) { | |
338 | $naissance = str_replace('-', '', $naissance); | |
339 | $end = str_replace('-', '', $end); | |
340 | $annivs[] = array( | |
341 | 'timestamp' => strtotime($ts), | |
342 | 'date' => $naissance, | |
343 | 'tomorrow' => $end, | |
344 | 'forlife' => $forlife, | |
345 | 'summary' => 'Anniversaire de '.$prenom | |
346 | .' '.$nom.' - x '.$promo, | |
347 | ); | |
fbfb06dc | 348 | } |
f3b3f363 | 349 | $page->assign('events', $annivs); |
fbfb06dc | 350 | |
351 | header('Content-Type: text/calendar; charset=utf-8'); | |
fbfb06dc | 352 | } |
5e193297 | 353 | |
917c4d11 | 354 | function handler_vcard(&$page, $photos = null) |
5e193297 | 355 | { |
356 | $res = XDB::query('SELECT contact | |
357 | FROM contacts | |
358 | WHERE uid = {?}', S::v('uid')); | |
917c4d11 | 359 | $vcard = new VCard($res->fetchColumn(), $photos == 'photos'); |
5e193297 | 360 | $vcard->do_page(&$page); |
361 | } | |
4da0b8d7 | 362 | } |
c9f82d49 | 363 | |
a7de4ef7 | 364 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
c9f82d49 | 365 | ?> |