]> git.immae.eu Git - github/shaarli/Shaarli.git/commitdiff
Fixes #480: add an option to urlencode redirector URL 524/head
authorArthurHoaro <arthur@hoa.ro>
Thu, 24 Mar 2016 18:40:12 +0000 (19:40 +0100)
committerArthurHoaro <arthur@hoa.ro>
Sat, 9 Apr 2016 14:52:32 +0000 (16:52 +0200)
  * New config: `$GLOBALS['config']['REDIRECTOR_URLENCODE']` (default `true`).
  * Parameter added to LinkDB constructor.
  * Fixes a bug with urlencode and escaped url.
  * In `index.php`, LinkDB is now instanciate once for `importFile()` and `showDaily()`.
  * TU

application/LinkDB.php
index.php
tests/LinkDBTest.php

index a62341fc638e2c078e401bdedb58e6aa635e990c..1cb70de02344b07aad4b1ca79f21fb1a95f6ed73 100644 (file)
@@ -65,22 +65,40 @@ class LinkDB implements Iterator, Countable, ArrayAccess
     // 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
      *
      * Checks if the datastore exists; else, attempts to create a dummy one.
      *
-     * @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 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).
      */
-    function __construct($datastore, $isLoggedIn, $hidePublicLinks, $redirector = '')
+    function __construct(
+        $datastore,
+        $isLoggedIn,
+        $hidePublicLinks,
+        $redirector = '',
+        $redirectorEncode = true
+    )
     {
         $this->_datastore = $datastore;
         $this->_loggedIn = $isLoggedIn;
         $this->_hidePublicLinks = $hidePublicLinks;
         $this->_redirector = $redirector;
+        $this->redirectorEncode = $redirectorEncode === true;
         $this->_checkDB();
         $this->_readDB();
     }
@@ -278,7 +296,12 @@ You use the community supported version of the original Shaarli project, by Seba
 
             // 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 . urlencode($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'];
index 735615ff7b084326aef7dc8dfee611021b73fe3a..d3369a2dcd58335772dc4dbe4c21f6735451c0c6 100644 (file)
--- a/index.php
+++ b/index.php
@@ -100,6 +100,7 @@ $GLOBALS['config']['ENABLE_LOCALCACHE'] = true;
 $GLOBALS['config']['UPDATECHECK_BRANCH'] = 'stable';
 $GLOBALS['config']['UPDATECHECK_INTERVAL'] = 86400;
 
+$GLOBALS['config']['REDIRECTOR_URLENCODE'] = true;
 
 /*
  * Plugin configuration
@@ -706,7 +707,8 @@ function showDailyRSS() {
         $GLOBALS['config']['DATASTORE'],
         isLoggedIn(),
         $GLOBALS['config']['HIDE_PUBLIC_LINKS'],
-        $GLOBALS['redirector']
+        $GLOBALS['redirector'],
+        $GLOBALS['config']['REDIRECTOR_URLENCODE']
     );
 
     /* Some Shaarlies may have very few links, so we need to look
@@ -791,16 +793,10 @@ function showDailyRSS() {
  * Show the 'Daily' page.
  *
  * @param PageBuilder $pageBuilder Template engine wrapper.
+ * @param LinkDB $LINKSDB LinkDB instance.
  */
-function showDaily($pageBuilder)
+function showDaily($pageBuilder, $LINKSDB)
 {
-    $LINKSDB = new LinkDB(
-        $GLOBALS['config']['DATASTORE'],
-        isLoggedIn(),
-        $GLOBALS['config']['HIDE_PUBLIC_LINKS'],
-        $GLOBALS['redirector']
-    );
-
     $day=Date('Ymd',strtotime('-1 day')); // Yesterday, in format YYYYMMDD.
     if (isset($_GET['day'])) $day=$_GET['day'];
 
@@ -892,7 +888,8 @@ function renderPage()
         $GLOBALS['config']['DATASTORE'],
         isLoggedIn(),
         $GLOBALS['config']['HIDE_PUBLIC_LINKS'],
-        $GLOBALS['redirector']
+        $GLOBALS['redirector'],
+        $GLOBALS['config']['REDIRECTOR_URLENCODE']
     );
 
     $updater = new Updater(
@@ -1043,7 +1040,7 @@ function renderPage()
 
     // Daily page.
     if ($targetPage == Router::$PAGE_DAILY) {
-        showDaily($PAGE);
+        showDaily($PAGE, $LINKSDB);
     }
 
     // ATOM and RSS feed.
@@ -1638,7 +1635,7 @@ HTML;
             exit;
         }
         if (!tokenOk($_POST['token'])) die('Wrong token.');
-        importFile();
+        importFile($LINKSDB);
         exit;
     }
 
@@ -1707,15 +1704,10 @@ HTML;
 
 // -----------------------------------------------------------------------------------------------
 // Process the import file form.
-function importFile()
+function importFile($LINKSDB)
 {
     if (!isLoggedIn()) { die('Not allowed.'); }
-    $LINKSDB = new LinkDB(
-        $GLOBALS['config']['DATASTORE'],
-        isLoggedIn(),
-        $GLOBALS['config']['HIDE_PUBLIC_LINKS'],
-        $GLOBALS['redirector']
-    );
+
     $filename=$_FILES['filetoupload']['name'];
     $filesize=$_FILES['filetoupload']['size'];
     $data=file_get_contents($_FILES['filetoupload']['tmp_name']);
index 52d31400b5a0716ebce5c2ab51ec3ab008af4c38..b055fe9169e2a57377c3ed08ef9b27a9af546ae0 100644 (file)
@@ -338,6 +338,13 @@ class LinkDBTest extends PHPUnit_Framework_TestCase
         $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('://')));
         }
     }