aboutsummaryrefslogtreecommitdiffhomepage
path: root/application/Utils.php
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/Utils.php
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/Utils.php')
-rw-r--r--application/Utils.php53
1 files changed, 53 insertions, 0 deletions
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}