aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2017-11-07 20:23:58 +0100
committerArthurHoaro <arthur@hoa.ro>2017-11-07 20:23:58 +0100
commitfd08b50a80c3aed25f9e2a19cbfe9fb3ad35cf1f (patch)
treeac75653241dec9fbdc0c66dfa14181b13d4a0dba
parentd12b2a08c886a652d1edbacb287788814a8ef7f1 (diff)
downloadShaarli-fd08b50a80c3aed25f9e2a19cbfe9fb3ad35cf1f.tar.gz
Shaarli-fd08b50a80c3aed25f9e2a19cbfe9fb3ad35cf1f.tar.zst
Shaarli-fd08b50a80c3aed25f9e2a19cbfe9fb3ad35cf1f.zip
Don't URL encode description links if parameter 'redirector.encode_url' is set to false
-rw-r--r--application/FeedBuilder.php2
-rw-r--r--application/LinkUtils.php15
-rw-r--r--index.php18
-rw-r--r--tests/LinkUtilsTest.php15
4 files changed, 40 insertions, 10 deletions
diff --git a/application/FeedBuilder.php b/application/FeedBuilder.php
index 3cfaafb4..ebae18b4 100644
--- a/application/FeedBuilder.php
+++ b/application/FeedBuilder.php
@@ -152,7 +152,7 @@ class FeedBuilder
152 } else { 152 } else {
153 $permalink = '<a href="'. $link['guid'] .'" title="'. t('Permalink') .'">'. t('Permalink') .'</a>'; 153 $permalink = '<a href="'. $link['guid'] .'" title="'. t('Permalink') .'">'. t('Permalink') .'</a>';
154 } 154 }
155 $link['description'] = format_description($link['description'], '', $pageaddr); 155 $link['description'] = format_description($link['description'], '', false, $pageaddr);
156 $link['description'] .= PHP_EOL .'<br>&#8212; '. $permalink; 156 $link['description'] .= PHP_EOL .'<br>&#8212; '. $permalink;
157 157
158 $pubDate = $link['created']; 158 $pubDate = $link['created'];
diff --git a/application/LinkUtils.php b/application/LinkUtils.php
index 267e62cd..e3d95d08 100644
--- a/application/LinkUtils.php
+++ b/application/LinkUtils.php
@@ -102,12 +102,13 @@ function count_private($links)
102 * 102 *
103 * @param string $text input string. 103 * @param string $text input string.
104 * @param string $redirector if a redirector is set, use it to gerenate links. 104 * @param string $redirector if a redirector is set, use it to gerenate links.
105 * @param bool $urlEncode Use `urlencode()` on the URL after the redirector or not.
105 * 106 *
106 * @return string returns $text with all links converted to HTML links. 107 * @return string returns $text with all links converted to HTML links.
107 * 108 *
108 * @see Function inspired from http://www.php.net/manual/en/function.preg-replace.php#85722 109 * @see Function inspired from http://www.php.net/manual/en/function.preg-replace.php#85722
109 */ 110 */
110function text2clickable($text, $redirector = '') 111function text2clickable($text, $redirector = '', $urlEncode = true)
111{ 112{
112 $regex = '!(((?:https?|ftp|file)://|apt:|magnet:)\S+[a-z0-9\(\)]/?)!si'; 113 $regex = '!(((?:https?|ftp|file)://|apt:|magnet:)\S+[a-z0-9\(\)]/?)!si';
113 114
@@ -117,8 +118,9 @@ function text2clickable($text, $redirector = '')
117 // Redirector is set, urlencode the final URL. 118 // Redirector is set, urlencode the final URL.
118 return preg_replace_callback( 119 return preg_replace_callback(
119 $regex, 120 $regex,
120 function ($matches) use ($redirector) { 121 function ($matches) use ($redirector, $urlEncode) {
121 return '<a href="' . $redirector . urlencode($matches[1]) .'">'. $matches[1] .'</a>'; 122 $url = $urlEncode ? urlencode($matches[1]) : $matches[1];
123 return '<a href="' . $redirector . $url .'">'. $matches[1] .'</a>';
122 }, 124 },
123 $text 125 $text
124 ); 126 );
@@ -164,12 +166,13 @@ function space2nbsp($text)
164 * 166 *
165 * @param string $description shaare's description. 167 * @param string $description shaare's description.
166 * @param string $redirector if a redirector is set, use it to gerenate links. 168 * @param string $redirector if a redirector is set, use it to gerenate links.
169 * @param bool $urlEncode Use `urlencode()` on the URL after the redirector or not.
167 * @param string $indexUrl URL to Shaarli's index. 170 * @param string $indexUrl URL to Shaarli's index.
168 * 171
169 * @return string formatted description. 172 * @return string formatted description.
170 */ 173 */
171function format_description($description, $redirector = '', $indexUrl = '') { 174function format_description($description, $redirector = '', $urlEncode = true, $indexUrl = '') {
172 return nl2br(space2nbsp(hashtag_autolink(text2clickable($description, $redirector), $indexUrl))); 175 return nl2br(space2nbsp(hashtag_autolink(text2clickable($description, $redirector, $urlEncode), $indexUrl)));
173} 176}
174 177
175/** 178/**
diff --git a/index.php b/index.php
index e1516d37..9d5f25ea 100644
--- a/index.php
+++ b/index.php
@@ -526,7 +526,11 @@ function showDailyRSS($conf) {
526 526
527 // We pre-format some fields for proper output. 527 // We pre-format some fields for proper output.
528 foreach ($links as &$link) { 528 foreach ($links as &$link) {
529 $link['formatedDescription'] = format_description($link['description'], $conf->get('redirector.url')); 529 $link['formatedDescription'] = format_description(
530 $link['description'],
531 $conf->get('redirector.url'),
532 $conf->get('redirector.encode_url')
533 );
530 $link['thumbnail'] = thumbnail($conf, $link['url']); 534 $link['thumbnail'] = thumbnail($conf, $link['url']);
531 $link['timestamp'] = $link['created']->getTimestamp(); 535 $link['timestamp'] = $link['created']->getTimestamp();
532 if (startsWith($link['url'], '?')) { 536 if (startsWith($link['url'], '?')) {
@@ -598,7 +602,11 @@ function showDaily($pageBuilder, $LINKSDB, $conf, $pluginManager)
598 $taglist = explode(' ',$link['tags']); 602 $taglist = explode(' ',$link['tags']);
599 uasort($taglist, 'strcasecmp'); 603 uasort($taglist, 'strcasecmp');
600 $linksToDisplay[$key]['taglist']=$taglist; 604 $linksToDisplay[$key]['taglist']=$taglist;
601 $linksToDisplay[$key]['formatedDescription'] = format_description($link['description'], $conf->get('redirector.url')); 605 $linksToDisplay[$key]['formatedDescription'] = format_description(
606 $link['description'],
607 $conf->get('redirector.url'),
608 $conf->get('redirector.encode_url')
609 );
602 $linksToDisplay[$key]['thumbnail'] = thumbnail($conf, $link['url']); 610 $linksToDisplay[$key]['thumbnail'] = thumbnail($conf, $link['url']);
603 $linksToDisplay[$key]['timestamp'] = $link['created']->getTimestamp(); 611 $linksToDisplay[$key]['timestamp'] = $link['created']->getTimestamp();
604 } 612 }
@@ -1688,7 +1696,11 @@ function buildLinkList($PAGE,$LINKSDB, $conf, $pluginManager)
1688 while ($i<$end && $i<count($keys)) 1696 while ($i<$end && $i<count($keys))
1689 { 1697 {
1690 $link = $linksToDisplay[$keys[$i]]; 1698 $link = $linksToDisplay[$keys[$i]];
1691 $link['description'] = format_description($link['description'], $conf->get('redirector.url')); 1699 $link['description'] = format_description(
1700 $link['description'],
1701 $conf->get('redirector.url'),
1702 $conf->get('redirector.encode_url')
1703 );
1692 $classLi = ($i % 2) != 0 ? '' : 'publicLinkHightLight'; 1704 $classLi = ($i % 2) != 0 ? '' : 'publicLinkHightLight';
1693 $link['class'] = $link['private'] == 0 ? $classLi : 'private'; 1705 $link['class'] = $link['private'] == 0 ? $classLi : 'private';
1694 $link['timestamp'] = $link['created']->getTimestamp(); 1706 $link['timestamp'] = $link['created']->getTimestamp();
diff --git a/tests/LinkUtilsTest.php b/tests/LinkUtilsTest.php
index c77922ec..99679320 100644
--- a/tests/LinkUtilsTest.php
+++ b/tests/LinkUtilsTest.php
@@ -131,6 +131,21 @@ class LinkUtilsTest extends PHPUnit_Framework_TestCase
131 } 131 }
132 132
133 /** 133 /**
134 * Test text2clickable a redirector set and without URL encode.
135 */
136 public function testText2clickableWithRedirectorDontEncode()
137 {
138 $text = 'stuff http://hello.there/?is=someone&or=something#here otherstuff';
139 $redirector = 'http://redirector.to';
140 $expectedText = 'stuff <a href="'.
141 $redirector .
142 'http://hello.there/?is=someone&or=something#here' .
143 '">http://hello.there/?is=someone&or=something#here</a> otherstuff';
144 $processedText = text2clickable($text, $redirector, false);
145 $this->assertEquals($expectedText, $processedText);
146 }
147
148 /**
134 * Test testSpace2nbsp. 149 * Test testSpace2nbsp.
135 */ 150 */
136 public function testSpace2nbsp() 151 public function testSpace2nbsp()