Improves url shortener.
authorStéphane Jacob <sj@m4x.org>
Thu, 7 Apr 2011 22:36:58 +0000 (00:36 +0200)
committerStéphane Jacob <sj@m4x.org>
Sat, 9 Apr 2011 12:29:42 +0000 (14:29 +0200)
Signed-off-by: Stéphane Jacob <sj@m4x.org>
configs/platal.ini
htdocs/url_redirect.php [new file with mode: 0644]
modules/urlshortener.php
templates/urlshortener/admin.tpl
upgrade/1.1.1/03_url.sql [new file with mode: 0644]
upgrade/1.1.1/README [new file with mode: 0644]

index c7e159e..a057415 100644 (file)
@@ -104,6 +104,9 @@ secure_domain = ""
 ; 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
 ;
diff --git a/htdocs/url_redirect.php b/htdocs/url_redirect.php
new file mode 100644 (file)
index 0000000..f4e25bd
--- /dev/null
@@ -0,0 +1,55 @@
+<?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:
+?>
index 0e7f3a1..33b5153 100644 (file)
@@ -24,22 +24,14 @@ class UrlShortenerModule extends PLModule
     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)
@@ -54,17 +46,21 @@ class UrlShortenerModule extends PLModule
         $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(*)
@@ -77,7 +73,7 @@ class UrlShortenerModule extends PLModule
             }
         } else {
             do {
-                $alias = rand_token(6);
+                $alias = 'a/' . rand_token(6);
                 $used = XDB::fetchOneCell('SELECT  COUNT(*)
                                              FROM  url_shortener
                                             WHERE  alias = {?}',
@@ -89,7 +85,7 @@ class UrlShortenerModule extends PLModule
         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 . ' ».');
     }
 }
 
index e9d1dea..ec11de8 100644 (file)
       <td><input type="text" name="url" value="{if t($url)}{$url}{/if}" /></td>
     </tr>
     <tr>
-      <th>Alias (6 caractères, optionnel)&nbsp;:</th>
+      <th>Alias (optionnel)&nbsp;:</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 «&nbsp;a/&nbsp;» (par exemple&nbsp;: « a/azerty ». Ce
+  préfixe «&nbsp;a/&nbsp;» 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: *}
diff --git a/upgrade/1.1.1/03_url.sql b/upgrade/1.1.1/03_url.sql
new file mode 100644 (file)
index 0000000..14810a1
--- /dev/null
@@ -0,0 +1,15 @@
+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:
diff --git a/upgrade/1.1.1/README b/upgrade/1.1.1/README
new file mode 100644 (file)
index 0000000..936e205
--- /dev/null
@@ -0,0 +1,3 @@
+The following variable should be set:
+[Core]
+baseurl_shortener = "http://u.w4x.org/"