Merge pull request #524 from ArthurHoaro/hotfix/redirectorurl

Fixes #480: add an option to urlencode redirector URL
This commit is contained in:
Arthur 2016-04-09 16:55:39 +02:00
commit 9ad2950deb
3 changed files with 47 additions and 25 deletions

View 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'];

View file

@ -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']);

View 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('://')));
}
}