| 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_PUBLIC), |
| 28 | 'admin/url' => $this->make_hook('admin_url', AUTH_MDP, 'admin') |
| 29 | ); |
| 30 | } |
| 31 | |
| 32 | function handler_url($page, $alias) |
| 33 | { |
| 34 | http_redirect(Platal::globals()->core->base_url_shortener . $alias); |
| 35 | } |
| 36 | |
| 37 | function handler_admin_url($page) |
| 38 | { |
| 39 | $page->changeTpl('urlshortener/admin.tpl'); |
| 40 | |
| 41 | if (!Post::has('url')) { |
| 42 | return; |
| 43 | } |
| 44 | |
| 45 | $url = Post::t('url'); |
| 46 | $alias = Post::t('alias'); |
| 47 | |
| 48 | $url_regex = '{^(https?|ftp)://[a-zA-Z0-9._%#+/?=&~-]+$}i'; |
| 49 | if (strlen($url) > 255 || !preg_match($url_regex, $url)) { |
| 50 | $page->trigError("L'url donnée n'est pas valide."); |
| 51 | return; |
| 52 | } |
| 53 | $page->assign('url', $url); |
| 54 | |
| 55 | if ($alias != '') { |
| 56 | if (!preg_match('/^[a-zA-Z0-9\-\/]+$/i', $alias)) { |
| 57 | $page->trigError("L'alias proposé n'est pas valide."); |
| 58 | return; |
| 59 | } |
| 60 | if (preg_match('/^a\//i', $alias)) { |
| 61 | $page->trigError("L'alias commence par le préfixe 'a/' qui est réservé et donc non autorisé."); |
| 62 | return; |
| 63 | } |
| 64 | $page->assign('alias', $alias); |
| 65 | |
| 66 | $used = XDB::fetchOneCell('SELECT COUNT(*) |
| 67 | FROM url_shortener |
| 68 | WHERE alias = {?}', |
| 69 | $alias); |
| 70 | if ($used != 0) { |
| 71 | $page->trigError("L'alias proposé est déjà utilisé."); |
| 72 | return; |
| 73 | } |
| 74 | } else { |
| 75 | do { |
| 76 | $alias = 'a/' . rand_token(6); |
| 77 | $used = XDB::fetchOneCell('SELECT COUNT(*) |
| 78 | FROM url_shortener |
| 79 | WHERE alias = {?}', |
| 80 | $alias); |
| 81 | } while ($used != 0); |
| 82 | $page->assign('alias', $alias); |
| 83 | } |
| 84 | |
| 85 | XDB::execute('INSERT INTO url_shortener (url, alias) |
| 86 | VALUES ({?}, {?})', |
| 87 | $url, $alias); |
| 88 | $page->trigSuccess("L'url « " . $url . ' » est maintenant accessible depuis « http://u.w4x.org/' . $alias . ' ».'); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
| 93 | ?> |