aboutsummaryrefslogtreecommitdiffhomepage
path: root/application/Utils.php
diff options
context:
space:
mode:
authorVirtualTam <virtualtam@flibidi.net>2015-11-26 23:05:58 +0100
committerVirtualTam <virtualtam@flibidi.net>2015-11-26 23:05:58 +0100
commit61873e3ded8dfba397b39aebd2322d0939c82caa (patch)
treea0cb54fb7223f23faa67bd90349a0981b9ef04e6 /application/Utils.php
parent657f0e25ba2c6f39775a8386b62d7c662ae709f7 (diff)
parent90e5bd65c9d4a5d3d5cedfeaa1314f2a15df5227 (diff)
downloadShaarli-61873e3ded8dfba397b39aebd2322d0939c82caa.tar.gz
Shaarli-61873e3ded8dfba397b39aebd2322d0939c82caa.tar.zst
Shaarli-61873e3ded8dfba397b39aebd2322d0939c82caa.zip
Merge pull request #355 from ArthurHoaro/redirector-url
URL encode links when a redirector is set
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}