aboutsummaryrefslogtreecommitdiffhomepage
path: root/application/Utils.php
diff options
context:
space:
mode:
Diffstat (limited to 'application/Utils.php')
-rw-r--r--application/Utils.php69
1 files changed, 13 insertions, 56 deletions
diff --git a/application/Utils.php b/application/Utils.php
index da521cce..0a5b476e 100644
--- a/application/Utils.php
+++ b/application/Utils.php
@@ -31,7 +31,15 @@ function logm($logFile, $clientIp, $message)
31 * - are NOT cryptographically secure (they CAN be forged) 31 * - are NOT cryptographically secure (they CAN be forged)
32 * 32 *
33 * In Shaarli, they are used as a tinyurl-like link to individual entries, 33 * In Shaarli, they are used as a tinyurl-like link to individual entries,
34 * e.g. smallHash('20111006_131924') --> yZH23w 34 * built once with the combination of the date and item ID.
35 * e.g. smallHash('20111006_131924' . 142) --> eaWxtQ
36 *
37 * @warning before v0.8.1, smallhashes were built only with the date,
38 * and their value has been preserved.
39 *
40 * @param string $text Create a hash from this text.
41 *
42 * @return string generated small hash.
35 */ 43 */
36function smallHash($text) 44function smallHash($text)
37{ 45{
@@ -106,7 +114,9 @@ function unescape($str)
106} 114}
107 115
108/** 116/**
109 * Link sanitization before templating 117 * Sanitize link before rendering.
118 *
119 * @param array $link Link to escape.
110 */ 120 */
111function sanitizeLink(&$link) 121function sanitizeLink(&$link)
112{ 122{
@@ -198,59 +208,6 @@ function is_session_id_valid($sessionId)
198} 208}
199 209
200/** 210/**
201 * In a string, converts URLs to clickable links.
202 *
203 * @param string $text input string.
204 * @param string $redirector if a redirector is set, use it to gerenate links.
205 *
206 * @return string returns $text with all links converted to HTML links.
207 *
208 * @see Function inspired from http://www.php.net/manual/en/function.preg-replace.php#85722
209 */
210function text2clickable($text, $redirector)
211{
212 $regex = '!(((?:https?|ftp|file)://|apt:|magnet:)\S+[[:alnum:]]/?)!si';
213
214 if (empty($redirector)) {
215 return preg_replace($regex, '<a href="$1">$1</a>', $text);
216 }
217 // Redirector is set, urlencode the final URL.
218 return preg_replace_callback(
219 $regex,
220 function ($matches) use ($redirector) {
221 return '<a href="' . $redirector . urlencode($matches[1]) .'">'. $matches[1] .'</a>';
222 },
223 $text
224 );
225}
226
227/**
228 * This function inserts &nbsp; where relevant so that multiple spaces are properly displayed in HTML
229 * even in the absence of <pre> (This is used in description to keep text formatting).
230 *
231 * @param string $text input text.
232 *
233 * @return string formatted text.
234 */
235function space2nbsp($text)
236{
237 return preg_replace('/(^| ) /m', '$1&nbsp;', $text);
238}
239
240/**
241 * Format Shaarli's description
242 * TODO: Move me to ApplicationUtils when it's ready.
243 *
244 * @param string $description shaare's description.
245 * @param string $redirector if a redirector is set, use it to gerenate links.
246 *
247 * @return string formatted description.
248 */
249function format_description($description, $redirector = false) {
250 return nl2br(space2nbsp(text2clickable($description, $redirector)));
251}
252
253/**
254 * Sniff browser language to set the locale automatically. 211 * Sniff browser language to set the locale automatically.
255 * Note that is may not work on your server if the corresponding locale is not installed. 212 * Note that is may not work on your server if the corresponding locale is not installed.
256 * 213 *
@@ -273,4 +230,4 @@ function autoLocale($headerLocale)
273 } 230 }
274 } 231 }
275 setlocale(LC_ALL, $attempts); 232 setlocale(LC_ALL, $attempts);
276} \ No newline at end of file 233}