]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
Add internal URL shortener
authorEvan Prodromou <evan@status.net>
Sun, 23 Jan 2011 21:49:12 +0000 (16:49 -0500)
committerEvan Prodromou <evan@status.net>
Sun, 23 Jan 2011 21:49:12 +0000 (16:49 -0500)
actions/redirecturl.php [new file with mode: 0644]
actions/urlsettings.php
lib/default.php
lib/router.php
lib/util.php

diff --git a/actions/redirecturl.php b/actions/redirecturl.php
new file mode 100644 (file)
index 0000000..0a959b0
--- /dev/null
@@ -0,0 +1,144 @@
+<?php
+/**
+ * StatusNet - the distributed open-source microblogging tool
+ * Copyright (C) 2010, StatusNet, Inc.
+ *
+ * Redirect to the given URL
+ * 
+ * PHP version 5
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 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 Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * @category  URL
+ * @package   StatusNet
+ * @author    Evan Prodromou <evan@status.net>
+ * @copyright 2010 StatusNet, Inc.
+ * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
+ * @link      http://status.net/
+ */
+
+if (!defined('STATUSNET')) {
+    // This check helps protect against security problems;
+    // your code file can't be executed directly from the web.
+    exit(1);
+}
+
+/**
+ * Redirect to a given URL
+ *
+ * This is our internal low-budget URL-shortener
+ *
+ * @category  Action
+ * @package   StatusNet
+ * @author    Evan Prodromou <evan@status.net>
+ * @copyright 2010 StatusNet, Inc.
+ * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
+ * @link      http://status.net/
+ */
+
+class RedirecturlAction extends Action
+{
+    protected $id   = null;
+    protected $file = null;
+
+    /**
+     * For initializing members of the class.
+     *
+     * @param array $argarray misc. arguments
+     *
+     * @return boolean true
+     */
+
+    function prepare($argarray)
+    {
+        parent::prepare($argarray);
+
+        $this->id = $this->trimmed('id');
+        
+        if (empty($this->id)) {
+            throw new ClientException(_('No id parameter'));
+        }
+
+        $this->file = File::staticGet('id', $this->id);
+        
+        if (empty($this->file)) {
+            throw new ClientException(sprintf(_('No such file "%d"'),
+                                              $this->id),
+                                      404);
+        }
+
+        return true;
+    }
+
+    /**
+     * Handler method
+     *
+     * @param array $argarray is ignored since it's now passed in in prepare()
+     *
+     * @return void
+     */
+
+    function handle($argarray=null)
+    {
+        common_redirect($this->file->url, 307);
+        return;
+    }
+
+    /**
+     * Return true if read only.
+     *
+     * MAY override
+     *
+     * @param array $args other arguments
+     *
+     * @return boolean is read only action?
+     */
+
+    function isReadOnly($args)
+    {
+        return true;
+    }
+
+    /**
+     * Return last modified, if applicable.
+     *
+     * MAY override
+     *
+     * @return string last modified http header
+     */
+
+    function lastModified()
+    {
+        // For comparison with If-Last-Modified
+        // If not applicable, return null
+
+        return strtotime($this->file->modified);
+    }
+
+    /**
+     * Return etag, if applicable.
+     *
+     * MAY override
+     *
+     * @return string etag http header
+     */
+
+    function etag()
+    {
+        return 'W/"' . implode(':', array($this->arg('action'),
+                                          common_user_cache_hash(),
+                                          common_language(),
+                                          $this->file->id)) . '"';
+    }
+}
index f8780a7e86bef10c9d6b595e77fdd10d01c9b789..140e28c999c72a1733778356c9daffaef0934d4f 100644 (file)
@@ -99,23 +99,28 @@ class UrlsettingsAction extends SettingsAction
         $this->hidden('token', common_session_token());
         $this->elementStart('ul', 'form_data');
 
-        $shorteners = array(_('[none]') => array('freeService' => false));
-
         Event::handle('GetUrlShorteners', array(&$shorteners));
 
         $services = array();
-        foreach($shorteners as $name=>$value)
+
+        foreach ($shorteners as $name => $value)
         {
-            $services[$name]=$name;
-            if($value['freeService']){
+            $services[$name] = $name;
+            if ($value['freeService']) {
                 // TRANS: Used as a suffix for free URL shorteners in a dropdown list in the tab "Other" of a
                 // TRANS: user's profile settings. This message has one space at the beginning. Use your
                 // TRANS: language's word separator here if it has one (most likely a single space).
-                $services[$name].=_(' (free service)');
+                $services[$name] .= _(' (free service)');
             }
         }
-        if($services)
-        {
+
+        // Include default values
+
+        $services['none']     = _('[none]');
+        $services['internal'] = _('[internal]');
+
+        if ($services) {
+
             asort($services);
 
             $this->elementStart('li');
index 9528d9cf8be30ebd39919a9275145554e9a97474..186ac263c94fc511d9115f0f56c8d1e2043d428a 100644 (file)
@@ -295,17 +295,7 @@ $default =
         'logincommand' =>
         array('disabled' => true),
         'plugins' =>
-        array('default' => array('LilUrl' => array('shortenerName'=>'ur1.ca',
-                                                   'freeService' => true,
-                                                   'serviceUrl'=>'http://ur1.ca/'),
-                                 'PtitUrl' => array('shortenerName' => 'ptiturl.com',
-                                                    'serviceUrl' => 'http://ptiturl.com/?creer=oui&action=Reduire&url=%1$s'),
-                                 'SimpleUrl' => array(array('shortenerName' => 'is.gd', 'serviceUrl' => 'http://is.gd/api.php?longurl=%1$s'),
-                                                      array('shortenerName' => 'snipr.com', 'serviceUrl' => 'http://snipr.com/site/snip?r=simple&link=%1$s'),
-                                                      array('shortenerName' => 'metamark.net', 'serviceUrl' => 'http://metamark.net/api/rest/simple?long_url=%1$s'),
-                                                      array('shortenerName' => 'tinyurl.com', 'serviceUrl' => 'http://tinyurl.com/api-create.php?url=%1$s')),
-                                 'TightUrl' => array('shortenerName' => '2tu.us', 'freeService' => true,'serviceUrl'=>'http://2tu.us/?save=y&url=%1$s'),
-                                 'Geonames' => null,
+        array('default' => array('Geonames' => null,
                                  'Mapstraction' => null,
                                  'OStatus' => null,
                                  'WikiHashtags' => null,
index 99d8aeb043925afa9caa60d0a8f534256954bc72..10e5291588a793c28650b0607da616b13ca87e3e 100644 (file)
@@ -974,6 +974,12 @@ class Router
                         array('action' => 'AtomPubMembershipFeed'),
                         array('profile' => '[0-9]+'));
 
+            // URL shortening
+
+            $m->connect('url/:id',
+                        array('action' => 'redirecturl',
+                              'id' => '[0-9]+'));
+
             // user stuff
 
             Event::handle('RouterInitialized', array($m));
index 3d4adcf4bf876a00df205ae5547fa06db55769d2..4aec05275c525fd2325ac81eb36bb5e3a4cce02a 100644 (file)
@@ -2073,6 +2073,7 @@ function common_shorten_url($long_url, User $user=null, $force = false)
     common_debug("maxUrlLength = $maxUrlLength");
 
     // $force forces shortening even if it's not strictly needed
+    // I doubt URL shortening is ever 'strictly' needed. - ESP
 
     if (mb_strlen($long_url) < $maxUrlLength && !$force) {
         common_debug("Skipped shortening URL.");
@@ -2083,9 +2084,20 @@ function common_shorten_url($long_url, User $user=null, $force = false)
 
     common_debug("Shortener name = '$shortenerName'");
 
-    if (Event::handle('StartShortenUrl', array($long_url, $shortenerName, &$shortenedUrl))) {
-        //URL wasn't shortened, so return the long url
-        return $long_url;
+    if (Event::handle('StartShortenUrl', 
+                      array($long_url, $shortenerName, &$shortenedUrl))) {
+        if ($shortenerName == 'internal') {
+            $f = File::processNew($long_url);
+            if (empty($f)) {
+                return $long_url;
+            } else {
+                $shortenedUrl = common_local_url('redirecturl',
+                                                 array('id' => $f->id));
+                return $shortenedUrl;
+            }
+        } else {
+            return $long_url;
+        }
     } else {
         //URL was shortened, so return the result
         return trim($shortenedUrl);