Implements a url-shortener (Closes #1042).
authorStéphane Jacob <sj@m4x.org>
Mon, 14 Mar 2011 15:27:56 +0000 (16:27 +0100)
committerStéphane Jacob <sj@m4x.org>
Mon, 14 Mar 2011 15:50:00 +0000 (16:50 +0100)
Signed-off-by: Stéphane Jacob <sj@m4x.org>
ChangeLog
classes/xorg.php
modules/urlshortener.php [new file with mode: 0644]
templates/admin/index.tpl
templates/urlshortener/admin.tpl [new file with mode: 0644]
upgrade/1.1.0/15_url_shortener.sql [new file with mode: 0644]

index 2c83e13..53e5758 100644 (file)
--- 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
index 2666803..bdd1e85 100644 (file)
@@ -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 (file)
index 0000000..0e7f3a1
--- /dev/null
@@ -0,0 +1,97 @@
+<?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                *
+ ***************************************************************************/
+
+class UrlShortenerModule extends PLModule
+{
+    function handlers()
+    {
+        return array(
+            'url'       => $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:
+?>
index 2af78b5..d937e5e 100644 (file)
       <a href="admin/nls">Liste des NLs groupes</a>
       &nbsp;&nbsp;|&nbsp;&nbsp;
       <a href="admin/newsletter/">NL de X.org</a>
+      &nbsp;&nbsp;|&nbsp;&nbsp;
+      <a href="admin/url">Raccourcisseur d'url</a>
     </td>
   </tr>
   <tr class="impair">
diff --git a/templates/urlshortener/admin.tpl b/templates/urlshortener/admin.tpl
new file mode 100644 (file)
index 0000000..e9d1dea
--- /dev/null
@@ -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               *}
+{*                                                                        *}
+{**************************************************************************}
+
+<h1>Raccourcisseur d'url</h1>
+
+<form action="admin/url" method="post">
+  {xsrf_token_field}
+  <table class="bicol">
+    <tr>
+      <th>Url&nbsp;:</th>
+      <td><input type="text" name="url" value="{if t($url)}{$url}{/if}" /></td>
+    </tr>
+    <tr>
+      <th>Alias (6 caractères, 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>
+      </td>
+    </tr>
+  </table>
+  <p class="center"><input type="submit" value="Raccourcir" /></p>
+</form>
+
+{* 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 (file)
index 0000000..042da53
--- /dev/null
@@ -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: