Merge pull request #1269 from ArthurHoaro/feature/remove-redirector

Remove the redirector setting
This commit is contained in:
ArthurHoaro 2019-02-24 11:29:56 +01:00 committed by GitHub
commit 015314f3c6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 50 additions and 168 deletions

View 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;
}

View 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'])) {

View 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)));
}
/**

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

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

View 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;
}
}

View 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"

View file

@ -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();

View 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));

View 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);
}

View 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);
}

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