aboutsummaryrefslogtreecommitdiffhomepage
path: root/application
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2015-09-18 13:26:36 +0200
committerArthurHoaro <arthur@hoa.ro>2015-11-26 20:14:38 +0100
commit90e5bd65c9d4a5d3d5cedfeaa1314f2a15df5227 (patch)
tree7a085b9aecaa92aa5076df01df78c39edd2acff5 /application
parent986afb752bc57271e76935da9ed2df6ef8713cb7 (diff)
downloadShaarli-90e5bd65c9d4a5d3d5cedfeaa1314f2a15df5227.tar.gz
Shaarli-90e5bd65c9d4a5d3d5cedfeaa1314f2a15df5227.tar.zst
Shaarli-90e5bd65c9d4a5d3d5cedfeaa1314f2a15df5227.zip
URL encode links when a redirector is set.
Fixes #328 - URL encode links when a redirector is set * WARNING - template edit - new variable available : "real_url" Contains the final real url (redirected or any other change on original URL) * Don't redirect shaares link in RSS/Atom. * Affects links shaared in description. * Move text2clickable and keepMultipleSpaces to Utils.php + unit test UPDATE: * keepMultipleSpaces renamed to space2nbsp * space2nbsp improved to handle single space at line beginning * links in text description aren't 'nofollow' anymore
Diffstat (limited to 'application')
-rw-r--r--application/LinkDB.php20
-rw-r--r--application/Utils.php53
2 files changed, 70 insertions, 3 deletions
diff --git a/application/LinkDB.php b/application/LinkDB.php
index 15fadbc3..f771ac8b 100644
--- a/application/LinkDB.php
+++ b/application/LinkDB.php
@@ -57,18 +57,25 @@ class LinkDB implements Iterator, Countable, ArrayAccess
57 // Hide public links 57 // Hide public links
58 private $_hidePublicLinks; 58 private $_hidePublicLinks;
59 59
60 // link redirector set in user settings.
61 private $_redirector;
62
60 /** 63 /**
61 * Creates a new LinkDB 64 * Creates a new LinkDB
62 * 65 *
63 * Checks if the datastore exists; else, attempts to create a dummy one. 66 * Checks if the datastore exists; else, attempts to create a dummy one.
64 * 67 *
65 * @param $isLoggedIn is the user logged in? 68 * @param string $datastore datastore file path.
69 * @param boolean $isLoggedIn is the user logged in?
70 * @param boolean $hidePublicLinks if true all links are private.
71 * @param string $redirector link redirector set in user settings.
66 */ 72 */
67 function __construct($datastore, $isLoggedIn, $hidePublicLinks) 73 function __construct($datastore, $isLoggedIn, $hidePublicLinks, $redirector = '')
68 { 74 {
69 $this->_datastore = $datastore; 75 $this->_datastore = $datastore;
70 $this->_loggedIn = $isLoggedIn; 76 $this->_loggedIn = $isLoggedIn;
71 $this->_hidePublicLinks = $hidePublicLinks; 77 $this->_hidePublicLinks = $hidePublicLinks;
78 $this->_redirector = $redirector;
72 $this->_checkDB(); 79 $this->_checkDB();
73 $this->_readDB(); 80 $this->_readDB();
74 } 81 }
@@ -259,7 +266,14 @@ You use the community supported version of the original Shaarli project, by Seba
259 266
260 // Escape links data 267 // Escape links data
261 foreach($this->_links as &$link) { 268 foreach($this->_links as &$link) {
262 sanitizeLink($link); 269 sanitizeLink($link);
270 // Do not use the redirector for internal links (Shaarli note URL starting with a '?').
271 if (!empty($this->_redirector) && !startsWith($link['url'], '?')) {
272 $link['real_url'] = $this->_redirector . urlencode($link['url']);
273 }
274 else {
275 $link['real_url'] = $link['url'];
276 }
263 } 277 }
264 } 278 }
265 279
diff --git a/application/Utils.php b/application/Utils.php
index b8579b48..f84f70e4 100644
--- a/application/Utils.php
+++ b/application/Utils.php
@@ -148,3 +148,56 @@ function is_session_id_valid($sessionId)
148 148
149 return true; 149 return true;
150} 150}
151
152/**
153 * In a string, converts URLs to clickable links.
154 *
155 * @param string $text input string.
156 * @param string $redirector if a redirector is set, use it to gerenate links.
157 *
158 * @return string returns $text with all links converted to HTML links.
159 *
160 * @see Function inspired from http://www.php.net/manual/en/function.preg-replace.php#85722
161 */
162function text2clickable($text, $redirector)
163{
164 $regex = '!(((?:https?|ftp|file)://|apt:|magnet:)\S+[[:alnum:]]/?)!si';
165
166 if (empty($redirector)) {
167 return preg_replace($regex, '<a href="$1">$1</a>', $text);
168 }
169 // Redirector is set, urlencode the final URL.
170 return preg_replace_callback(
171 $regex,
172 function ($matches) use ($redirector) {
173 return '<a href="' . $redirector . urlencode($matches[1]) .'">'. $matches[1] .'</a>';
174 },
175 $text
176 );
177}
178
179/**
180 * This function inserts &nbsp; where relevant so that multiple spaces are properly displayed in HTML
181 * even in the absence of <pre> (This is used in description to keep text formatting).
182 *
183 * @param string $text input text.
184 *
185 * @return string formatted text.
186 */
187function space2nbsp($text)
188{
189 return preg_replace('/(^| ) /m', '$1&nbsp;', $text);
190}
191
192/**
193 * Format Shaarli's description
194 * TODO: Move me to ApplicationUtils when it's ready.
195 *
196 * @param string $description shaare's description.
197 * @param string $redirector if a redirector is set, use it to gerenate links.
198 *
199 * @return string formatted description.
200 */
201function format_description($description, $redirector) {
202 return nl2br(space2nbsp(text2clickable($description, $redirector)));
203}