From 5c11ce3d14becb0799434db3f1c6cc37eab3ca0f Mon Sep 17 00:00:00 2001 From: =?utf8?q?St=C3=A9phane=20Jacob?= Date: Mon, 14 Mar 2011 16:27:56 +0100 Subject: [PATCH] Implements a url-shortener (Closes #1042). MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Stéphane Jacob --- ChangeLog | 1 + classes/xorg.php | 2 +- modules/urlshortener.php | 97 ++++++++++++++++++++++++++++++++++++++ templates/admin/index.tpl | 2 + templates/urlshortener/admin.tpl | 43 +++++++++++++++++ upgrade/1.1.0/15_url_shortener.sql | 9 ++++ 6 files changed, 153 insertions(+), 1 deletion(-) create mode 100644 modules/urlshortener.php create mode 100644 templates/urlshortener/admin.tpl create mode 100644 upgrade/1.1.0/15_url_shortener.sql diff --git a/ChangeLog b/ChangeLog index 2c83e13..53e5758 100644 --- a/ChangeLog +++ b/ChangeLog @@ -4,6 +4,7 @@ VERSION 1.1.0 XX XX XXXX Bug/Wish: * Admin: + - #1042: Implements a url-shortener -JAC - #1315: Displays antispam level in users email admin pages -JAC - #1399: Displays all similar entreprises only on demand -JAC - #1403: Displays grade if any in medal validation process -JAC diff --git a/classes/xorg.php b/classes/xorg.php index 2666803..bdd1e85 100644 --- a/classes/xorg.php +++ b/classes/xorg.php @@ -28,7 +28,7 @@ class Xorg extends Platal 'profile', 'register', 'search', 'stats', 'admin', 'newsletter', 'axletter', 'epletter', 'bandeau', 'survey', 'fusionax', 'gadgets', 'googleapps', 'poison', - 'openid', 'reminder', 'api'); + 'openid', 'reminder', 'api', 'urlshortener'); } public function find_hook() diff --git a/modules/urlshortener.php b/modules/urlshortener.php new file mode 100644 index 0000000..0e7f3a1 --- /dev/null +++ b/modules/urlshortener.php @@ -0,0 +1,97 @@ + $this->make_hook('url', AUTH_COOKIE), + 'admin/url' => $this->make_hook('admin_url', AUTH_MDP, 'admin') + ); + } + + function handler_url($page, $alias) + { + $url = XDB::fetchOneCell('SELECT url + FROM url_shortener + WHERE alias = {?}', + $alias); + + if (is_null($url)) { + return PL_NOT_FOUND; + } + http_redirect($url); + } + + function handler_admin_url($page) + { + $page->changeTpl('urlshortener/admin.tpl'); + + if (!Post::has('url')) { + return; + } + + $url = Post::t('url'); + $alias = Post::t('alias'); + + $url_regex = '{^(https?|ftp)://[a-zA-Z0-9._%#+/?=&~-]+$}i'; + if (!preg_match($url_regex, $url)) { + $page->trigError("L'url donnée n'est pas valide."); + return; + } + $page->assign('url', $url); + + if ($alias != '') { + if (!preg_match('/^[a-zA-Z0-9\-]{6}$/i', $alias)) { + $page->trigError("L'alias proposé n'est pas valide."); + return; + } + $page->assign('alias', $alias); + + $used = XDB::fetchOneCell('SELECT COUNT(*) + FROM url_shortener + WHERE alias = {?}', + $alias); + if ($used != 0) { + $page->trigError("L'alias proposé est déjà utilisé."); + return; + } + } else { + do { + $alias = rand_token(6); + $used = XDB::fetchOneCell('SELECT COUNT(*) + FROM url_shortener + WHERE alias = {?}', + $alias); + } while ($used != 0); + $page->assign('alias', $alias); + } + + XDB::execute('INSERT INTO url_shortener (url, alias) + VALUES ({?}, {?})', + $url, $alias); + $page->trigSuccess("L'url « " . $url . ' » est maintenant accessible depuis « ' . Platal::globals()->baseurl . '/url/' . $alias . ' ».'); + } +} + +// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: +?> diff --git a/templates/admin/index.tpl b/templates/admin/index.tpl index 2af78b5..d937e5e 100644 --- a/templates/admin/index.tpl +++ b/templates/admin/index.tpl @@ -185,6 +185,8 @@ Liste des NLs groupes   |   NL de X.org +   |   + Raccourcisseur d'url diff --git a/templates/urlshortener/admin.tpl b/templates/urlshortener/admin.tpl new file mode 100644 index 0000000..e9d1dea --- /dev/null +++ b/templates/urlshortener/admin.tpl @@ -0,0 +1,43 @@ +{**************************************************************************} +{* *} +{* Copyright (C) 2003-2011 Polytechnique.org *} +{* http://opensource.polytechnique.org/ *} +{* *} +{* This program is free software; you can redistribute it and/or modify *} +{* it under the terms of the GNU General Public License as published by *} +{* the Free Software Foundation; either version 2 of the License, or *} +{* (at your option) any later version. *} +{* *} +{* This program is distributed in the hope that it will be useful, *} +{* but WITHOUT ANY WARRANTY; without even the implied warranty of *} +{* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *} +{* GNU General Public License for more details. *} +{* *} +{* You should have received a copy of the GNU General Public License *} +{* along with this program; if not, write to the Free Software *} +{* Foundation, Inc., *} +{* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *} +{* *} +{**************************************************************************} + +

Raccourcisseur d'url

+ +
+ {xsrf_token_field} + + + + + + + + + +
Url :
Alias (6 caractères, optionnel) : + + (peut contenir lettres, chiffres et tirets) +
+

+
+ +{* vim:set et sw=2 sts=2 sws=2 enc=utf-8: *} diff --git a/upgrade/1.1.0/15_url_shortener.sql b/upgrade/1.1.0/15_url_shortener.sql new file mode 100644 index 0000000..042da53 --- /dev/null +++ b/upgrade/1.1.0/15_url_shortener.sql @@ -0,0 +1,9 @@ +DROP TABLE IF EXISTS url_shortener; + +CREATE TABLE url_shortener ( + alias CHAR(6) NOT NULL DEFAULT '', + url TEXT NOT NULL, + PRIMARY KEY (alias) +) ENGINE=InnoDB, CHARSET=utf8; + +-- vim:set syntax=mysql: -- 2.1.4