Commit | Line | Data |
---|---|---|
e23d21c1 SJ |
1 | <?php |
2 | /*************************************************************************** | |
3 | * Copyright (C) 2003-2011 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 UrlShortenerModule extends PLModule | |
23 | { | |
24 | function handlers() | |
25 | { | |
26 | return array( | |
27 | 'url' => $this->make_hook('url', AUTH_COOKIE), | |
28 | 'admin/url' => $this->make_hook('admin_url', AUTH_MDP, 'admin') | |
29 | ); | |
30 | } | |
31 | ||
32 | function handler_url($page, $alias) | |
33 | { | |
34 | $url = XDB::fetchOneCell('SELECT url | |
35 | FROM url_shortener | |
36 | WHERE alias = {?}', | |
37 | $alias); | |
38 | ||
39 | if (is_null($url)) { | |
40 | return PL_NOT_FOUND; | |
41 | } | |
42 | http_redirect($url); | |
43 | } | |
44 | ||
45 | function handler_admin_url($page) | |
46 | { | |
47 | $page->changeTpl('urlshortener/admin.tpl'); | |
48 | ||
49 | if (!Post::has('url')) { | |
50 | return; | |
51 | } | |
52 | ||
53 | $url = Post::t('url'); | |
54 | $alias = Post::t('alias'); | |
55 | ||
56 | $url_regex = '{^(https?|ftp)://[a-zA-Z0-9._%#+/?=&~-]+$}i'; | |
57 | if (!preg_match($url_regex, $url)) { | |
58 | $page->trigError("L'url donnée n'est pas valide."); | |
59 | return; | |
60 | } | |
61 | $page->assign('url', $url); | |
62 | ||
63 | if ($alias != '') { | |
64 | if (!preg_match('/^[a-zA-Z0-9\-]{6}$/i', $alias)) { | |
65 | $page->trigError("L'alias proposé n'est pas valide."); | |
66 | return; | |
67 | } | |
68 | $page->assign('alias', $alias); | |
69 | ||
70 | $used = XDB::fetchOneCell('SELECT COUNT(*) | |
71 | FROM url_shortener | |
72 | WHERE alias = {?}', | |
73 | $alias); | |
74 | if ($used != 0) { | |
75 | $page->trigError("L'alias proposé est déjà utilisé."); | |
76 | return; | |
77 | } | |
78 | } else { | |
79 | do { | |
80 | $alias = rand_token(6); | |
81 | $used = XDB::fetchOneCell('SELECT COUNT(*) | |
82 | FROM url_shortener | |
83 | WHERE alias = {?}', | |
84 | $alias); | |
85 | } while ($used != 0); | |
86 | $page->assign('alias', $alias); | |
87 | } | |
88 | ||
89 | XDB::execute('INSERT INTO url_shortener (url, alias) | |
90 | VALUES ({?}, {?})', | |
91 | $url, $alias); | |
92 | $page->trigSuccess("L'url « " . $url . ' » est maintenant accessible depuis « ' . Platal::globals()->baseurl . '/url/' . $alias . ' ».'); | |
93 | } | |
94 | } | |
95 | ||
96 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: | |
97 | ?> |