$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;
}
* - 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:
// 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
*
* @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();
}
$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'])) {
* 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);
}
/**
* 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)));
}
/**
'general.title',
'general.header_link',
'privacy.default_private_links',
- 'redirector.url',
);
// Only logged in user can alter config.
// 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');
} 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>— ' . $permalink;
$pubDate = $link['created'];
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());
return true;
}
+
+ /**
+ * Remove redirector settings.
+ */
+ public function updateMethodRemoveRedirector()
+ {
+ $this->conf->remove('redirector');
+ $this->conf->write(true);
+ return true;
+ }
}
- **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
"hide_public_links": false,
"hide_timestamps": false,
"open_shaarli": false,
- "redirector": "http://anonym.to/?",
- "redirector_encode_url": false
},
"general": {
"header_link": "?",
"enable_thumbnails": true,
"enable_localcache": true
},
- "redirector": {
- "url": "http://anonym.to/?",
- "encode_url": false
- },
"plugins": {
"WALLABAG_URL": "http://demo.wallabag.org",
"WALLABAG_VERSION": "1"
$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
// 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
$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();
}
$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();
'search_term' => $searchterm,
'search_tags' => $searchtags,
'visibility' => ! empty($_SESSION['visibility']) ? $_SESSION['visibility'] : '',
- 'redirector' => $conf->get('redirector.url'), // Optional redirector URL.
'links' => $linkDisp,
);
$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();
);
}
- /**
- * 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.
*/
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));
}
/**
- * 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);
}
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);
}
$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');
}
}
/**
-<<<<<<< HEAD
* Test updateMethodWebThumbnailer with thumbnails enabled.
*/
public function testUpdateMethodWebThumbnailerEnabled()
$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'));
+ }
}