diff options
author | ArthurHoaro <arthur@hoa.ro> | 2016-03-24 19:40:12 +0100 |
---|---|---|
committer | ArthurHoaro <arthur@hoa.ro> | 2016-04-09 16:52:32 +0200 |
commit | 043eae70c4c57b0447b56a58b64ce9d102895396 (patch) | |
tree | 83a40e1f0096059bd9c7c13efbcfdeaa0ea06b5e | |
parent | 9486a2e92911c726673fe50674a0a408be3c774f (diff) | |
download | Shaarli-043eae70c4c57b0447b56a58b64ce9d102895396.tar.gz Shaarli-043eae70c4c57b0447b56a58b64ce9d102895396.tar.zst Shaarli-043eae70c4c57b0447b56a58b64ce9d102895396.zip |
Fixes #480: add an option to urlencode redirector URL
* 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
-rw-r--r-- | application/LinkDB.php | 35 | ||||
-rw-r--r-- | index.php | 30 | ||||
-rw-r--r-- | tests/LinkDBTest.php | 7 |
3 files changed, 47 insertions, 25 deletions
diff --git a/application/LinkDB.php b/application/LinkDB.php index a62341fc..1cb70de0 100644 --- a/application/LinkDB.php +++ b/application/LinkDB.php | |||
@@ -66,21 +66,39 @@ class LinkDB implements Iterator, Countable, ArrayAccess | |||
66 | private $_redirector; | 66 | private $_redirector; |
67 | 67 | ||
68 | /** | 68 | /** |
69 | * Set this to `true` to urlencode link behind redirector link, `false` to leave it untouched. | ||
70 | * | ||
71 | * Example: | ||
72 | * anonym.to needs clean URL while dereferer.org needs urlencoded URL. | ||
73 | * | ||
74 | * @var boolean $redirectorEncode parameter: true or false | ||
75 | */ | ||
76 | private $redirectorEncode; | ||
77 | |||
78 | /** | ||
69 | * Creates a new LinkDB | 79 | * Creates a new LinkDB |
70 | * | 80 | * |
71 | * Checks if the datastore exists; else, attempts to create a dummy one. | 81 | * Checks if the datastore exists; else, attempts to create a dummy one. |
72 | * | 82 | * |
73 | * @param string $datastore datastore file path. | 83 | * @param string $datastore datastore file path. |
74 | * @param boolean $isLoggedIn is the user logged in? | 84 | * @param boolean $isLoggedIn is the user logged in? |
75 | * @param boolean $hidePublicLinks if true all links are private. | 85 | * @param boolean $hidePublicLinks if true all links are private. |
76 | * @param string $redirector link redirector set in user settings. | 86 | * @param string $redirector link redirector set in user settings. |
87 | * @param boolean $redirectorEncode Enable urlencode on redirected urls (default: true). | ||
77 | */ | 88 | */ |
78 | function __construct($datastore, $isLoggedIn, $hidePublicLinks, $redirector = '') | 89 | function __construct( |
90 | $datastore, | ||
91 | $isLoggedIn, | ||
92 | $hidePublicLinks, | ||
93 | $redirector = '', | ||
94 | $redirectorEncode = true | ||
95 | ) | ||
79 | { | 96 | { |
80 | $this->_datastore = $datastore; | 97 | $this->_datastore = $datastore; |
81 | $this->_loggedIn = $isLoggedIn; | 98 | $this->_loggedIn = $isLoggedIn; |
82 | $this->_hidePublicLinks = $hidePublicLinks; | 99 | $this->_hidePublicLinks = $hidePublicLinks; |
83 | $this->_redirector = $redirector; | 100 | $this->_redirector = $redirector; |
101 | $this->redirectorEncode = $redirectorEncode === true; | ||
84 | $this->_checkDB(); | 102 | $this->_checkDB(); |
85 | $this->_readDB(); | 103 | $this->_readDB(); |
86 | } | 104 | } |
@@ -278,7 +296,12 @@ You use the community supported version of the original Shaarli project, by Seba | |||
278 | 296 | ||
279 | // Do not use the redirector for internal links (Shaarli note URL starting with a '?'). | 297 | // Do not use the redirector for internal links (Shaarli note URL starting with a '?'). |
280 | if (!empty($this->_redirector) && !startsWith($link['url'], '?')) { | 298 | if (!empty($this->_redirector) && !startsWith($link['url'], '?')) { |
281 | $link['real_url'] = $this->_redirector . urlencode($link['url']); | 299 | $link['real_url'] = $this->_redirector; |
300 | if ($this->redirectorEncode) { | ||
301 | $link['real_url'] .= urlencode(unescape($link['url'])); | ||
302 | } else { | ||
303 | $link['real_url'] .= $link['url']; | ||
304 | } | ||
282 | } | 305 | } |
283 | else { | 306 | else { |
284 | $link['real_url'] = $link['url']; | 307 | $link['real_url'] = $link['url']; |
@@ -100,6 +100,7 @@ $GLOBALS['config']['ENABLE_LOCALCACHE'] = true; | |||
100 | $GLOBALS['config']['UPDATECHECK_BRANCH'] = 'stable'; | 100 | $GLOBALS['config']['UPDATECHECK_BRANCH'] = 'stable'; |
101 | $GLOBALS['config']['UPDATECHECK_INTERVAL'] = 86400; | 101 | $GLOBALS['config']['UPDATECHECK_INTERVAL'] = 86400; |
102 | 102 | ||
103 | $GLOBALS['config']['REDIRECTOR_URLENCODE'] = true; | ||
103 | 104 | ||
104 | /* | 105 | /* |
105 | * Plugin configuration | 106 | * Plugin configuration |
@@ -706,7 +707,8 @@ function showDailyRSS() { | |||
706 | $GLOBALS['config']['DATASTORE'], | 707 | $GLOBALS['config']['DATASTORE'], |
707 | isLoggedIn(), | 708 | isLoggedIn(), |
708 | $GLOBALS['config']['HIDE_PUBLIC_LINKS'], | 709 | $GLOBALS['config']['HIDE_PUBLIC_LINKS'], |
709 | $GLOBALS['redirector'] | 710 | $GLOBALS['redirector'], |
711 | $GLOBALS['config']['REDIRECTOR_URLENCODE'] | ||
710 | ); | 712 | ); |
711 | 713 | ||
712 | /* Some Shaarlies may have very few links, so we need to look | 714 | /* Some Shaarlies may have very few links, so we need to look |
@@ -791,16 +793,10 @@ function showDailyRSS() { | |||
791 | * Show the 'Daily' page. | 793 | * Show the 'Daily' page. |
792 | * | 794 | * |
793 | * @param PageBuilder $pageBuilder Template engine wrapper. | 795 | * @param PageBuilder $pageBuilder Template engine wrapper. |
796 | * @param LinkDB $LINKSDB LinkDB instance. | ||
794 | */ | 797 | */ |
795 | function showDaily($pageBuilder) | 798 | function showDaily($pageBuilder, $LINKSDB) |
796 | { | 799 | { |
797 | $LINKSDB = new LinkDB( | ||
798 | $GLOBALS['config']['DATASTORE'], | ||
799 | isLoggedIn(), | ||
800 | $GLOBALS['config']['HIDE_PUBLIC_LINKS'], | ||
801 | $GLOBALS['redirector'] | ||
802 | ); | ||
803 | |||
804 | $day=Date('Ymd',strtotime('-1 day')); // Yesterday, in format YYYYMMDD. | 800 | $day=Date('Ymd',strtotime('-1 day')); // Yesterday, in format YYYYMMDD. |
805 | if (isset($_GET['day'])) $day=$_GET['day']; | 801 | if (isset($_GET['day'])) $day=$_GET['day']; |
806 | 802 | ||
@@ -892,7 +888,8 @@ function renderPage() | |||
892 | $GLOBALS['config']['DATASTORE'], | 888 | $GLOBALS['config']['DATASTORE'], |
893 | isLoggedIn(), | 889 | isLoggedIn(), |
894 | $GLOBALS['config']['HIDE_PUBLIC_LINKS'], | 890 | $GLOBALS['config']['HIDE_PUBLIC_LINKS'], |
895 | $GLOBALS['redirector'] | 891 | $GLOBALS['redirector'], |
892 | $GLOBALS['config']['REDIRECTOR_URLENCODE'] | ||
896 | ); | 893 | ); |
897 | 894 | ||
898 | $updater = new Updater( | 895 | $updater = new Updater( |
@@ -1043,7 +1040,7 @@ function renderPage() | |||
1043 | 1040 | ||
1044 | // Daily page. | 1041 | // Daily page. |
1045 | if ($targetPage == Router::$PAGE_DAILY) { | 1042 | if ($targetPage == Router::$PAGE_DAILY) { |
1046 | showDaily($PAGE); | 1043 | showDaily($PAGE, $LINKSDB); |
1047 | } | 1044 | } |
1048 | 1045 | ||
1049 | // ATOM and RSS feed. | 1046 | // ATOM and RSS feed. |
@@ -1638,7 +1635,7 @@ HTML; | |||
1638 | exit; | 1635 | exit; |
1639 | } | 1636 | } |
1640 | if (!tokenOk($_POST['token'])) die('Wrong token.'); | 1637 | if (!tokenOk($_POST['token'])) die('Wrong token.'); |
1641 | importFile(); | 1638 | importFile($LINKSDB); |
1642 | exit; | 1639 | exit; |
1643 | } | 1640 | } |
1644 | 1641 | ||
@@ -1707,15 +1704,10 @@ HTML; | |||
1707 | 1704 | ||
1708 | // ----------------------------------------------------------------------------------------------- | 1705 | // ----------------------------------------------------------------------------------------------- |
1709 | // Process the import file form. | 1706 | // Process the import file form. |
1710 | function importFile() | 1707 | function importFile($LINKSDB) |
1711 | { | 1708 | { |
1712 | if (!isLoggedIn()) { die('Not allowed.'); } | 1709 | if (!isLoggedIn()) { die('Not allowed.'); } |
1713 | $LINKSDB = new LinkDB( | 1710 | |
1714 | $GLOBALS['config']['DATASTORE'], | ||
1715 | isLoggedIn(), | ||
1716 | $GLOBALS['config']['HIDE_PUBLIC_LINKS'], | ||
1717 | $GLOBALS['redirector'] | ||
1718 | ); | ||
1719 | $filename=$_FILES['filetoupload']['name']; | 1711 | $filename=$_FILES['filetoupload']['name']; |
1720 | $filesize=$_FILES['filetoupload']['size']; | 1712 | $filesize=$_FILES['filetoupload']['size']; |
1721 | $data=file_get_contents($_FILES['filetoupload']['tmp_name']); | 1713 | $data=file_get_contents($_FILES['filetoupload']['tmp_name']); |
diff --git a/tests/LinkDBTest.php b/tests/LinkDBTest.php index 52d31400..b055fe91 100644 --- a/tests/LinkDBTest.php +++ b/tests/LinkDBTest.php | |||
@@ -338,6 +338,13 @@ class LinkDBTest extends PHPUnit_Framework_TestCase | |||
338 | $db = new LinkDB(self::$testDatastore, false, false, $redirector); | 338 | $db = new LinkDB(self::$testDatastore, false, false, $redirector); |
339 | foreach($db as $link) { | 339 | foreach($db as $link) { |
340 | $this->assertStringStartsWith($redirector, $link['real_url']); | 340 | $this->assertStringStartsWith($redirector, $link['real_url']); |
341 | $this->assertNotFalse(strpos($link['real_url'], urlencode('://'))); | ||
342 | } | ||
343 | |||
344 | $db = new LinkDB(self::$testDatastore, false, false, $redirector, false); | ||
345 | foreach($db as $link) { | ||
346 | $this->assertStringStartsWith($redirector, $link['real_url']); | ||
347 | $this->assertFalse(strpos($link['real_url'], urlencode('://'))); | ||
341 | } | 348 | } |
342 | } | 349 | } |
343 | 350 | ||