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_COOKIE
),
28 'admin/url' => $this->make_hook('admin_url', AUTH_MDP
, 'admin')
32 function handler_url($page, $alias)
34 $url = XDB
::fetchOneCell('SELECT url
45 function handler_admin_url($page)
47 $page->changeTpl('urlshortener/admin.tpl');
49 if (!Post
::has('url')) {
53 $url = Post
::t('url');
54 $alias = Post
::t('alias');
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.");
61 $page->assign('url', $url);
64 if (!preg_match('/^[a-zA-Z0-9\-]{6}$/i', $alias)) {
65 $page->trigError("L'alias proposé n'est pas valide.");
68 $page->assign('alias', $alias);
70 $used = XDB
::fetchOneCell('SELECT COUNT(*)
75 $page->trigError("L'alias proposé est déjà utilisé.");
80 $alias = rand_token(6);
81 $used = XDB
::fetchOneCell('SELECT COUNT(*)
86 $page->assign('alias', $alias);
89 XDB
::execute('INSERT INTO url_shortener (url, alias)
92 $page->trigSuccess("L'url « " . $url . ' » est maintenant accessible depuis « ' . Platal
::globals()->baseurl
. '/url/' . $alias . ' ».');
96 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: