]> git.immae.eu Git - github/shaarli/Shaarli.git/commitdiff
Remove the redirector setting 1269/head
authorArthurHoaro <arthur@hoa.ro>
Sat, 9 Feb 2019 12:52:12 +0000 (13:52 +0100)
committerArthurHoaro <arthur@hoa.ro>
Sat, 9 Feb 2019 12:55:11 +0000 (13:55 +0100)
Fixes #1239

12 files changed:
application/api/ApiMiddleware.php
application/bookmark/LinkDB.php
application/bookmark/LinkUtils.php
application/config/ConfigManager.php
application/feed/FeedBuilder.php
application/updater/Updater.php
doc/md/Shaarli-configuration.md
index.php
tests/bookmark/LinkDBTest.php
tests/bookmark/LinkUtilsTest.php
tests/plugins/PluginMarkdownTest.php
tests/updater/UpdaterTest.php

index 5ffb8c6d06653fc527df1caca85d58004d41def8..2d55bda65eb2e24fd7348abaafac4f2f12567a8e 100644 (file)
@@ -129,9 +129,7 @@ class ApiMiddleware
         $linkDb = new \Shaarli\Bookmark\LinkDB(
             $conf->get('resource.datastore'),
             true,
-            $conf->get('privacy.hide_public_links'),
-            $conf->get('redirector.url'),
-            $conf->get('redirector.encode_url')
+            $conf->get('privacy.hide_public_links')
         );
         $this->container['db'] = $linkDb;
     }
index c13a11417b5fb66af9a7df5597ad2f2a09298936..266632e30da3ed45a0015dbcfc278878809e80e1 100644 (file)
@@ -29,10 +29,10 @@ use Shaarli\FileUtils;
  *  - private:  Is this link private? 0=no, other value=yes
  *  - tags:     tags attached to this entry (separated by spaces)
  *  - title     Title of the link
- *  - url       URL of the link. Used for displayable links (no redirector, relative, etc.).
- *              Can be absolute or relative.
- *              Relative URLs are permalinks (e.g.'?m-ukcw')
- *  - real_url  Absolute processed URL.
+ *  - url       URL of the link. Used for displayable links.
+ *              Can be absolute or relative in the database but the relative links
+ *              will be converted to absolute ones in templates.
+ *  - real_url  Raw URL in stored in the DB (absolute or relative).
  *  - shorturl  Permalink smallhash
  *
  * Implements 3 interfaces:
@@ -88,19 +88,6 @@ class LinkDB implements Iterator, Countable, ArrayAccess
     // Hide public links
     private $hidePublicLinks;
 
-    // link redirector set in user settings.
-    private $redirector;
-
-    /**
-     * Set this to `true` to urlencode link behind redirector link, `false` to leave it untouched.
-     *
-     * Example:
-     *   anonym.to needs clean URL while dereferer.org needs urlencoded URL.
-     *
-     * @var boolean $redirectorEncode parameter: true or false
-     */
-    private $redirectorEncode;
-
     /**
      * Creates a new LinkDB
      *
@@ -109,22 +96,16 @@ class LinkDB implements Iterator, Countable, ArrayAccess
      * @param string  $datastore        datastore file path.
      * @param boolean $isLoggedIn       is the user logged in?
      * @param boolean $hidePublicLinks  if true all links are private.
-     * @param string  $redirector       link redirector set in user settings.
-     * @param boolean $redirectorEncode Enable urlencode on redirected urls (default: true).
      */
     public function __construct(
         $datastore,
         $isLoggedIn,
-        $hidePublicLinks,
-        $redirector = '',
-        $redirectorEncode = true
+        $hidePublicLinks
     ) {
     
         $this->datastore = $datastore;
         $this->loggedIn = $isLoggedIn;
         $this->hidePublicLinks = $hidePublicLinks;
-        $this->redirector = $redirector;
-        $this->redirectorEncode = $redirectorEncode === true;
         $this->check();
         $this->read();
     }
@@ -323,17 +304,7 @@ You use the community supported version of the original Shaarli project, by Seba
                 $link['tags'] = preg_replace('/(^|\s+)\.[^($|\s)]+\s*/', ' ', $link['tags']);
             }
 
-            // Do not use the redirector for internal links (Shaarli note URL starting with a '?').
-            if (!empty($this->redirector) && !startsWith($link['url'], '?')) {
-                $link['real_url'] = $this->redirector;
-                if ($this->redirectorEncode) {
-                    $link['real_url'] .= urlencode(unescape($link['url']));
-                } else {
-                    $link['real_url'] .= $link['url'];
-                }
-            } else {
-                $link['real_url'] = $link['url'];
-            }
+            $link['real_url'] = $link['url'];
 
             // To be able to load links before running the update, and prepare the update
             if (!isset($link['created'])) {
index de5b61cbcaf6e8467d57eb9c49aa009d0a832754..988970bdf924854ba06fa352a61a1d3c62da94bd 100644 (file)
@@ -133,29 +133,15 @@ function count_private($links)
  * In a string, converts URLs to clickable links.
  *
  * @param string $text       input string.
- * @param string $redirector if a redirector is set, use it to gerenate links.
- * @param bool   $urlEncode  Use `urlencode()` on the URL after the redirector or not.
  *
  * @return string returns $text with all links converted to HTML links.
  *
  * @see Function inspired from http://www.php.net/manual/en/function.preg-replace.php#85722
  */
-function text2clickable($text, $redirector = '', $urlEncode = true)
+function text2clickable($text)
 {
     $regex = '!(((?:https?|ftp|file)://|apt:|magnet:)\S+[a-z0-9\(\)]/?)!si';
-
-    if (empty($redirector)) {
-        return preg_replace($regex, '<a href="$1">$1</a>', $text);
-    }
-    // Redirector is set, urlencode the final URL.
-    return preg_replace_callback(
-        $regex,
-        function ($matches) use ($redirector, $urlEncode) {
-            $url = $urlEncode ? urlencode($matches[1]) : $matches[1];
-            return '<a href="' . $redirector . $url .'">'. $matches[1] .'</a>';
-        },
-        $text
-    );
+    return preg_replace($regex, '<a href="$1">$1</a>', $text);
 }
 
 /**
@@ -197,15 +183,13 @@ function space2nbsp($text)
  * Format Shaarli's description
  *
  * @param string $description shaare's description.
- * @param string $redirector  if a redirector is set, use it to gerenate links.
- * @param bool   $urlEncode   Use `urlencode()` on the URL after the redirector or not.
  * @param string $indexUrl    URL to Shaarli's index.
 
  * @return string formatted description.
  */
-function format_description($description, $redirector = '', $urlEncode = true, $indexUrl = '')
+function format_description($description, $indexUrl = '')
 {
-    return nl2br(space2nbsp(hashtag_autolink(text2clickable($description, $redirector, $urlEncode), $indexUrl)));
+    return nl2br(space2nbsp(hashtag_autolink(text2clickable($description), $indexUrl)));
 }
 
 /**
index e6c3507329dc7e39a162c6fc4ec1103da8fadff2..3099392807bd41d0d48146be66a40461607f24ae 100644 (file)
@@ -221,7 +221,6 @@ class ConfigManager
             'general.title',
             'general.header_link',
             'privacy.default_private_links',
-            'redirector.url',
         );
 
         // Only logged in user can alter config.
@@ -381,9 +380,6 @@ class ConfigManager
         // default state of the 'remember me' checkbox of the login form
         $this->setEmpty('privacy.remember_user_default', true);
 
-        $this->setEmpty('redirector.url', '');
-        $this->setEmpty('redirector.encode_url', true);
-
         $this->setEmpty('thumbnails.width', '125');
         $this->setEmpty('thumbnails.height', '90');
 
index b66f2f918edd494f1d11090892fbcfda7d073e59..e23b34525b02f6e0920777e51e8cc42ee5d0dca4 100644 (file)
@@ -156,7 +156,7 @@ class FeedBuilder
         } else {
             $permalink = '<a href="' . $link['guid'] . '" title="' . t('Permalink') . '">' . t('Permalink') . '</a>';
         }
-        $link['description'] = format_description($link['description'], '', false, $pageaddr);
+        $link['description'] = format_description($link['description'], $pageaddr);
         $link['description'] .= PHP_EOL . '<br>&#8212; ' . $permalink;
 
         $pubDate = $link['created'];
index f12e351673640ddff963981d1232543a4f17979a..beb9ea9b7517613cd9c2ca01c884ba239f250355 100644 (file)
@@ -218,7 +218,6 @@ class Updater
         try {
             $this->conf->set('general.title', escape($this->conf->get('general.title')));
             $this->conf->set('general.header_link', escape($this->conf->get('general.header_link')));
-            $this->conf->set('redirector.url', escape($this->conf->get('redirector.url')));
             $this->conf->write($this->isLoggedIn);
         } catch (Exception $e) {
             error_log($e->getMessage());
@@ -550,4 +549,14 @@ class Updater
 
         return true;
     }
+
+    /**
+     * Remove redirector settings.
+     */
+    public function updateMethodRemoveRedirector()
+    {
+        $this->conf->remove('redirector');
+        $this->conf->write(true);
+        return true;
+    }
 }
index 920c7e27e151a3cf1a870a46ee0b2e8c1ced9505..8787f59daa0593983de0d16cf40bdc86be7621c8 100644 (file)
@@ -120,11 +120,6 @@ Must be an associative array: `translation domain => translation path`.
 - **enable_thumbnails**: Enable or disable thumbnail display.  
 - **enable_localcache**: Enable or disable local cache.
 
-### Redirector
-
-- **url**: Redirector URL, such as `anonym.to`.  
-- **encode_url**: Enable this if the redirector needs encoded URL to work properly.
-
 ## Configuration file example
 
 ```json
@@ -185,8 +180,6 @@ Must be an associative array: `translation domain => translation path`.
         "hide_public_links": false,
         "hide_timestamps": false,
         "open_shaarli": false,
-        "redirector": "http://anonym.to/?",
-        "redirector_encode_url": false
     },
     "general": {
         "header_link": "?",
@@ -218,10 +211,6 @@ Must be an associative array: `translation domain => translation path`.
         "enable_thumbnails": true,
         "enable_localcache": true
     },
-    "redirector": {
-        "url": "http://anonym.to/?",
-        "encode_url": false
-    },
     "plugins": {
         "WALLABAG_URL": "http://demo.wallabag.org",
         "WALLABAG_VERSION": "1"
index 633ab89e6386e90eaa02f76d5a1880cb9366dccb..9ea26c7c5f67a09d8d2ab0d115863d44bd324de6 100644 (file)
--- a/index.php
+++ b/index.php
@@ -312,9 +312,7 @@ function showDailyRSS($conf, $loginManager)
     $LINKSDB = new LinkDB(
         $conf->get('resource.datastore'),
         $loginManager->isLoggedIn(),
-        $conf->get('privacy.hide_public_links'),
-        $conf->get('redirector.url'),
-        $conf->get('redirector.encode_url')
+        $conf->get('privacy.hide_public_links')
     );
 
     /* Some Shaarlies may have very few links, so we need to look
@@ -356,11 +354,7 @@ function showDailyRSS($conf, $loginManager)
 
         // We pre-format some fields for proper output.
         foreach ($links as &$link) {
-            $link['formatedDescription'] = format_description(
-                $link['description'],
-                $conf->get('redirector.url'),
-                $conf->get('redirector.encode_url')
-            );
+            $link['formatedDescription'] = format_description($link['description']);
             $link['timestamp'] = $link['created']->getTimestamp();
             if (startsWith($link['url'], '?')) {
                 $link['url'] = index_url($_SERVER) . $link['url'];  // make permalink URL absolute
@@ -433,11 +427,7 @@ function showDaily($pageBuilder, $LINKSDB, $conf, $pluginManager, $loginManager)
         $taglist = explode(' ', $link['tags']);
         uasort($taglist, 'strcasecmp');
         $linksToDisplay[$key]['taglist']=$taglist;
-        $linksToDisplay[$key]['formatedDescription'] = format_description(
-            $link['description'],
-            $conf->get('redirector.url'),
-            $conf->get('redirector.encode_url')
-        );
+        $linksToDisplay[$key]['formatedDescription'] = format_description($link['description']);
         $linksToDisplay[$key]['timestamp'] =  $link['created']->getTimestamp();
     }
 
@@ -1662,11 +1652,7 @@ function buildLinkList($PAGE, $LINKSDB, $conf, $pluginManager, $loginManager)
     $linkDisp = array();
     while ($i<$end && $i<count($keys)) {
         $link = $linksToDisplay[$keys[$i]];
-        $link['description'] = format_description(
-            $link['description'],
-            $conf->get('redirector.url'),
-            $conf->get('redirector.encode_url')
-        );
+        $link['description'] = format_description($link['description']);
         $classLi =  ($i % 2) != 0 ? '' : 'publicLinkHightLight';
         $link['class'] = $link['private'] == 0 ? $classLi : 'private';
         $link['timestamp'] = $link['created']->getTimestamp();
@@ -1727,7 +1713,6 @@ function buildLinkList($PAGE, $LINKSDB, $conf, $pluginManager, $loginManager)
         'search_term' => $searchterm,
         'search_tags' => $searchtags,
         'visibility' => ! empty($_SESSION['visibility']) ? $_SESSION['visibility'] : '',
-        'redirector' => $conf->get('redirector.url'),  // Optional redirector URL.
         'links' => $linkDisp,
     );
 
@@ -1877,9 +1862,7 @@ try {
 $linkDb = new LinkDB(
     $conf->get('resource.datastore'),
     $loginManager->isLoggedIn(),
-    $conf->get('privacy.hide_public_links'),
-    $conf->get('redirector.url'),
-    $conf->get('redirector.encode_url')
+    $conf->get('privacy.hide_public_links')
 );
 
 $container = new \Slim\Container();
index ff5c0b970af1e8283d71e68606c3337e3d4c5936..2990a6b56a0360756b3b8ee157fc7125e4372a21 100644 (file)
@@ -361,36 +361,6 @@ class LinkDBTest extends \PHPUnit\Framework\TestCase
         );
     }
 
-    /**
-     * Test real_url without redirector.
-     */
-    public function testLinkRealUrlWithoutRedirector()
-    {
-        $db = new LinkDB(self::$testDatastore, false, false);
-        foreach ($db as $link) {
-            $this->assertEquals($link['url'], $link['real_url']);
-        }
-    }
-
-    /**
-     * Test real_url with redirector.
-     */
-    public function testLinkRealUrlWithRedirector()
-    {
-        $redirector = 'http://redirector.to?';
-        $db = new LinkDB(self::$testDatastore, false, false, $redirector);
-        foreach ($db as $link) {
-            $this->assertStringStartsWith($redirector, $link['real_url']);
-            $this->assertNotFalse(strpos($link['real_url'], urlencode('://')));
-        }
-
-        $db = new LinkDB(self::$testDatastore, false, false, $redirector, false);
-        foreach ($db as $link) {
-            $this->assertStringStartsWith($redirector, $link['real_url']);
-            $this->assertFalse(strpos($link['real_url'], urlencode('://')));
-        }
-    }
-
     /**
      * Test filter with string.
      */
@@ -516,7 +486,7 @@ class LinkDBTest extends \PHPUnit\Framework\TestCase
     public function testRenameTagCaseSensitive()
     {
         self::$refDB->write(self::$testDatastore);
-        $linkDB = new LinkDB(self::$testDatastore, true, false, '');
+        $linkDB = new LinkDB(self::$testDatastore, true, false);
 
         $res = $linkDB->renameTag('sTuff', 'Taz');
         $this->assertEquals(1, count($res));
index 1b8688e6d398fc1381e6b2acc586ae2562c282f2..bd08f03670a68cba16bea8895a906b35c5b953ec 100644 (file)
@@ -216,56 +216,27 @@ class LinkUtilsTest extends \PHPUnit\Framework\TestCase
     }
 
     /**
-     * Test text2clickable without a redirector being set.
+     * Test text2clickable.
      */
-    public function testText2clickableWithoutRedirector()
+    public function testText2clickable()
     {
         $text = 'stuff http://hello.there/is=someone#here otherstuff';
         $expectedText = 'stuff <a href="http://hello.there/is=someone#here">'
             . 'http://hello.there/is=someone#here</a> otherstuff';
-        $processedText = text2clickable($text, '');
+        $processedText = text2clickable($text);
         $this->assertEquals($expectedText, $processedText);
 
         $text = 'stuff http://hello.there/is=someone#here(please) otherstuff';
         $expectedText = 'stuff <a href="http://hello.there/is=someone#here(please)">'
             . 'http://hello.there/is=someone#here(please)</a> otherstuff';
-        $processedText = text2clickable($text, '');
+        $processedText = text2clickable($text);
         $this->assertEquals($expectedText, $processedText);
 
+        $text = 'stuff http://hello.there/is=someone#here(please)&no otherstuff';
         $text = 'stuff http://hello.there/is=someone#here(please)&no otherstuff';
         $expectedText = 'stuff <a href="http://hello.there/is=someone#here(please)&no">'
             . 'http://hello.there/is=someone#here(please)&no</a> otherstuff';
-        $processedText = text2clickable($text, '');
-        $this->assertEquals($expectedText, $processedText);
-    }
-
-    /**
-     * Test text2clickable with a redirector set.
-     */
-    public function testText2clickableWithRedirector()
-    {
-        $text = 'stuff http://hello.there/is=someone#here otherstuff';
-        $redirector = 'http://redirector.to';
-        $expectedText = 'stuff <a href="' .
-            $redirector .
-            urlencode('http://hello.there/is=someone#here') .
-            '">http://hello.there/is=someone#here</a> otherstuff';
-        $processedText = text2clickable($text, $redirector);
-        $this->assertEquals($expectedText, $processedText);
-    }
-
-    /**
-     * Test text2clickable a redirector set and without URL encode.
-     */
-    public function testText2clickableWithRedirectorDontEncode()
-    {
-        $text = 'stuff http://hello.there/?is=someone&or=something#here otherstuff';
-        $redirector = 'http://redirector.to';
-        $expectedText = 'stuff <a href="' .
-            $redirector .
-            'http://hello.there/?is=someone&or=something#here' .
-            '">http://hello.there/?is=someone&or=something#here</a> otherstuff';
-        $processedText = text2clickable($text, $redirector, false);
+        $processedText = text2clickable($text);
         $this->assertEquals($expectedText, $processedText);
     }
 
index 5e7c02b0c8f5b95407733d73cf01f95b26ccf3e8..9ddbc558f3d50294fe286322877d099c30ce0b4a 100644 (file)
@@ -107,7 +107,7 @@ class PluginMarkdownTest extends \PHPUnit\Framework\TestCase
     public function testReverseText2clickable()
     {
         $text = 'stuff http://hello.there/is=someone#here otherstuff';
-        $clickableText = text2clickable($text, '');
+        $clickableText = text2clickable($text);
         $reversedText = reverse_text2clickable($clickableText);
         $this->assertEquals($text, $reversedText);
     }
index d7df59637395ddf9a7d4760eecefa5db4a32a97e..93bc86c1b72a1ae5457fbd89847bbcf2f4730803 100644 (file)
@@ -287,17 +287,14 @@ $GLOBALS[\'privateLinkByDefault\'] = true;';
         $this->conf = new ConfigManager($sandbox);
         $title = '<script>alert("title");</script>';
         $headerLink = '<script>alert("header_link");</script>';
-        $redirectorUrl = '<script>alert("redirector");</script>';
         $this->conf->set('general.title', $title);
         $this->conf->set('general.header_link', $headerLink);
-        $this->conf->set('redirector.url', $redirectorUrl);
         $updater = new Updater(array(), array(), $this->conf, true);
         $done = $updater->updateMethodEscapeUnescapedConfig();
         $this->assertTrue($done);
         $this->conf->reload();
         $this->assertEquals(escape($title), $this->conf->get('general.title'));
         $this->assertEquals(escape($headerLink), $this->conf->get('general.header_link'));
-        $this->assertEquals(escape($redirectorUrl), $this->conf->get('redirector.url'));
         unlink($sandbox . '.json.php');
     }
 
@@ -707,7 +704,6 @@ $GLOBALS[\'privateLinkByDefault\'] = true;';
     }
 
     /**
-<<<<<<< HEAD
      * Test updateMethodWebThumbnailer with thumbnails enabled.
      */
     public function testUpdateMethodWebThumbnailerEnabled()
@@ -812,4 +808,19 @@ $GLOBALS[\'privateLinkByDefault\'] = true;';
         $linkDB = new LinkDB(self::$testDatastore, true, false);
         $this->assertTrue($linkDB[1]['sticky']);
     }
+
+    /**
+     * Test updateMethodRemoveRedirector().
+     */
+    public function testUpdateRemoveRedirector()
+    {
+        $sandboxConf = 'sandbox/config';
+        copy(self::$configFile . '.json.php', $sandboxConf . '.json.php');
+        $this->conf = new ConfigManager($sandboxConf);
+        $updater = new Updater([], null, $this->conf, true);
+        $this->assertTrue($updater->updateMethodRemoveRedirector());
+        $this->assertFalse($this->conf->exists('redirector'));
+        $this->conf = new ConfigManager($sandboxConf);
+        $this->assertFalse($this->conf->exists('redirector'));
+    }
 }