Skip to content

Commit

Permalink
Merge pull request #1269 from ArthurHoaro/feature/remove-redirector
Browse files Browse the repository at this point in the history
Remove the redirector setting
  • Loading branch information
ArthurHoaro committed Feb 24, 2019
2 parents 0ee11e9 + 520d295 commit 015314f
Show file tree
Hide file tree
Showing 12 changed files with 50 additions and 168 deletions.
4 changes: 1 addition & 3 deletions application/api/ApiMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,7 @@ protected function setLinkDb($conf)
$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;
}
Expand Down
41 changes: 6 additions & 35 deletions application/bookmark/LinkDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@
* - 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:
Expand Down Expand Up @@ -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
*
Expand All @@ -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();
}
Expand Down Expand Up @@ -323,17 +304,7 @@ private function read()
$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'])) {
Expand Down
24 changes: 4 additions & 20 deletions application/bookmark/LinkUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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)));
}

/**
Expand Down
4 changes: 0 additions & 4 deletions application/config/ConfigManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,6 @@ public function write($isLoggedIn)
'general.title',
'general.header_link',
'privacy.default_private_links',
'redirector.url',
);

// Only logged in user can alter config.
Expand Down Expand Up @@ -381,9 +380,6 @@ protected function setDefaultValues()
// 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');

Expand Down
2 changes: 1 addition & 1 deletion application/feed/FeedBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ protected function buildItem($link, $pageaddr)
} 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'];
Expand Down
11 changes: 10 additions & 1 deletion application/updater/Updater.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,6 @@ public function updateMethodEscapeUnescapedConfig()
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());
Expand Down Expand Up @@ -550,4 +549,14 @@ public function updateMethodSetSticky()

return true;
}

/**
* Remove redirector settings.
*/
public function updateMethodRemoveRedirector()
{
$this->conf->remove('redirector');
$this->conf->write(true);
return true;
}
}
11 changes: 0 additions & 11 deletions doc/md/Shaarli-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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": "?",
Expand Down Expand Up @@ -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"
Expand Down
27 changes: 5 additions & 22 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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,
);

Expand Down Expand Up @@ -1877,9 +1862,7 @@ function install($conf, $sessionManager, $loginManager)
$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();
Expand Down
32 changes: 1 addition & 31 deletions tests/bookmark/LinkDBTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -361,36 +361,6 @@ public function testAllTags()
);
}

/**
* 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.
*/
Expand Down Expand Up @@ -516,7 +486,7 @@ public function testRenameTagMultiple()
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));
Expand Down
41 changes: 6 additions & 35 deletions tests/bookmark/LinkUtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,56 +216,27 @@ public function testCountPrivateLinks()
}

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

Expand Down

0 comments on commit 015314f

Please sign in to comment.