; command line. The value is computed automatically when php serves a web page.
baseurl = "https://www.example.org/"
+; The base url of the url shortener.
+baseurl_shortener = ""
+
; $globals->sitename
; The name of the site
;
--- /dev/null
+<?php
+/***************************************************************************
+ * 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 *
+ ***************************************************************************/
+
+require_once 'xorg.inc.php';
+
+$platal = new Xorg('core');
+
+global $globals;
+$alias = ltrim($platal->pl_self(), '/');
+
+if (preg_match('/^[a-zA-Z0-9\-\/]+$/i', $alias)) {
+ $url = XDB::fetchOneCell('SELECT url
+ FROM url_shortener
+ WHERE alias = {?}',
+ $alias);
+ if ($url) {
+ http_redirect($url);
+ }
+}
+
+header($_SERVER['SERVER_PROTOCOL'] . ' 404 Not Found');
+?>
+<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
+<html>
+ <head>
+ <title>404 Not Found</title>
+ </head>
+ <body>
+ <h1>Not Found</h1>
+ The requested URL <?php echo $_SERVER['REQUEST_URI'] ?> was not found on this server.<p>
+ <hr>
+ <address><?php echo $_SERVER['SERVER_SIGNATURE'] ?></address>
+ </body>
+</html>
+<?php
+// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
+?>
function handlers()
{
return array(
- 'url' => $this->make_hook('url', AUTH_COOKIE),
+ 'url' => $this->make_hook('url', AUTH_PUBLIC),
'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);
+ http_redirect(Platal::globals()->core->base_url_shortener . $alias);
}
function handler_admin_url($page)
$alias = Post::t('alias');
$url_regex = '{^(https?|ftp)://[a-zA-Z0-9._%#+/?=&~-]+$}i';
- if (!preg_match($url_regex, $url)) {
+ if (strlen($url) > 255 || !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)) {
+ if (!preg_match('/^[a-zA-Z0-9\-\/]+$/i', $alias)) {
$page->trigError("L'alias proposé n'est pas valide.");
return;
}
+ if (preg_match('/^a\//i', $alias)) {
+ $page->trigError("L'alias commence par le préfixe 'a/' qui est réservé et donc non autorisé.");
+ return;
+ }
$page->assign('alias', $alias);
$used = XDB::fetchOneCell('SELECT COUNT(*)
}
} else {
do {
- $alias = rand_token(6);
+ $alias = 'a/' . rand_token(6);
$used = XDB::fetchOneCell('SELECT COUNT(*)
FROM url_shortener
WHERE 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 . ' ».');
+ $page->trigSuccess("L'url « " . $url . ' » est maintenant accessible depuis « http://u.w4x.org/' . $alias . ' ».');
}
}
<td><input type="text" name="url" value="{if t($url)}{$url}{/if}" /></td>
</tr>
<tr>
- <th>Alias (6 caractères, optionnel) :</th>
+ <th>Alias (optionnel) :</th>
<td>
- <input type="text" name="alias" size="6" maxlength="6" value="{if t($alias)}{$alias}{/if}" />
- <small>(peut contenir lettres, chiffres et tirets)</small>
+ <input type="text" name="alias" size="42" maxlength="255" value="{if t($alias)}{$alias}{/if}" />
+ <small>(peut contenir lettres, chiffres, tirets et /)</small>
</td>
</tr>
</table>
<p class="center"><input type="submit" value="Raccourcir" /></p>
</form>
+<h3>Explications</h3>
+<p>
+ L'alias peut être demandé. Dans ce cas, sa longueur maximal autorisée est
+ de 255 lettres, chiffres, tirets ou /. Ce dernier permet de définir des
+ domaines pour regrouper des raccourcis liés. Par exemple, « nl-04-04/ »
+ pourrait être utilisé comme base pour les urls de la lettre mensuelle d'avril
+ 2004.<br />
+ Si aucun alias n'est fournit, le site en génère un de 6 caractères aléatoires
+ accolés à la la base « a/ » (par exemple : « a/azerty ». Ce
+ préfixe « a/ » et réservé à cet usage et ne peut être utilisé pour
+ former une url choisie.
+</p>
+
{* vim:set et sw=2 sts=2 sws=2 enc=utf-8: *}
--- /dev/null
+DROP TABLE IF EXISTS tmp_url_shortener;
+CREATE TEMPORARY TABLE tmp_url_shortener LIKE url_shortener;
+INSERT INTO tmp_url_shortener SELECT * FROM url_shortener;
+DROP TABLE url_shortener;
+CREATE TABLE url_shortener (
+ alias VARCHAR(255) NOT NULL DEFAULT '',
+ url TEXT NOT NULL,
+ PRIMARY KEY (alias)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8;
+INSERT INTO url_shortener (alias, url)
+ SELECT alias, url
+ FROM tmp_url_shortener;
+DROP TABLE IF EXISTS tmp_url_shortener;
+
+-- vim:set syntax=mysql:
--- /dev/null
+The following variable should be set:
+[Core]
+baseurl_shortener = "http://u.w4x.org/"