2 /***************************************************************************
3 * Copyright (C) 2003-2011 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
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. *
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. *
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 *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
22 class UrlShortenerModule
extends PLModule
27 'url' => $this->make_hook('url', AUTH_PUBLIC
),
28 'admin/url' => $this->make_hook('admin_url', AUTH_PASSWD
, 'admin')
32 function handler_url($page, $alias)
34 http_redirect(Platal
::globals()->core
->base_url_shortener
. $alias);
37 function handler_admin_url($page)
39 $page->changeTpl('urlshortener/admin.tpl');
41 if (!Post
::has('url')) {
45 $url = Post
::t('url');
46 $alias = Post
::t('alias');
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.");
53 $page->assign('url', $url);
56 if (!preg_match('/^[a-zA-Z0-9\-\/]+$/i', $alias)) {
57 $page->trigError("L'alias proposé n'est pas valide.");
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é.");
64 $page->assign('alias', $alias);
66 $used = XDB
::fetchOneCell('SELECT COUNT(*)
71 $page->trigError("L'alias proposé est déjà utilisé.");
76 $alias = 'a/' . rand_token(6);
77 $used = XDB
::fetchOneCell('SELECT COUNT(*)
82 $page->assign('alias', $alias);
85 XDB
::execute('INSERT INTO url_shortener (url, alias)
88 $page->trigSuccess("L'url « " . $url . ' » est maintenant accessible depuis « http://u.w4x.org/' . $alias . ' ».');
92 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: