]>
git.immae.eu Git - github/shaarli/Shaarli.git/blob - application/Utils.php
7 * Logs a message to a text file
9 * @param string $logFile where to write the logs
10 * @param string $clientIp the client's remote IPv4/IPv6 address
11 * @param string $message the message to log
13 function logm($logFile, $clientIp, $message)
15 $line = strval(date('Y/m/d_H:i:s')).' - '.$clientIp.' - '.strval($message).'\n';
16 file_put_contents($logFile, $line, FILE_APPEND
);
20 * Returns the small hash of a string, using RFC 4648 base64url format
23 * - are unique (well, as unique as crc32, at last)
24 * - are always 6 characters long.
25 * - only use the following characters: a-z A-Z 0-9 - _ @
26 * - are NOT cryptographically secure (they CAN be forged)
28 * In Shaarli, they are used as a tinyurl-like link to individual entries,
29 * e.g. smallHash('20111006_131924') --> yZH23w
31 function smallHash($text)
33 $t = rtrim(base64_encode(hash('crc32', $text, true)), '=');
34 return strtr($t, '+/', '-_');
38 * Tells if a string start with a substring
40 function startsWith($haystack, $needle, $case=true)
43 return (strcmp(substr($haystack, 0, strlen($needle)), $needle) === 0);
45 return (strcasecmp(substr($haystack, 0, strlen($needle)), $needle) === 0);
49 * Tells if a string ends with a substring
51 function endsWith($haystack, $needle, $case=true)
54 return (strcmp(substr($haystack, strlen($haystack) - strlen($needle)), $needle) === 0);
56 return (strcasecmp(substr($haystack, strlen($haystack) - strlen($needle)), $needle) === 0);
60 * Same as nl2br(), but escapes < and >
62 function nl2br_escaped($html)
64 return str_replace('>', '>', str_replace('<', '<', nl2br($html)));
68 * htmlspecialchars wrapper
72 return htmlspecialchars($str, ENT_COMPAT
, 'UTF-8', false);
76 * Link sanitization before templating
78 function sanitizeLink(&$link)
80 $link['url'] = escape($link['url']); // useful?
81 $link['title'] = escape($link['title']);
82 $link['description'] = escape($link['description']);
83 $link['tags'] = escape($link['tags']);
87 * Checks if a string represents a valid date
89 * @param string $format The expected DateTime format of the string
90 * @param string $string A string-formatted date
92 * @return bool whether the string is a valid date
94 * @see http://php.net/manual/en/class.datetime.php
95 * @see http://php.net/manual/en/datetime.createfromformat.php
97 function checkDateFormat($format, $string)
99 $date = DateTime
::createFromFormat($format, $string);
100 return $date && $date->format($string) == $string;
104 * Generate a header location from HTTP_REFERER.
105 * Make sure the referer is Shaarli itself and prevent redirection loop.
107 * @param string $referer - HTTP_REFERER.
108 * @param string $host - Server HOST.
109 * @param array $loopTerms - Contains list of term to prevent redirection loop.
111 * @return string $referer - final referer.
113 function generateLocation($referer, $host, $loopTerms = array())
117 // No referer if it contains any value in $loopCriteria.
118 foreach ($loopTerms as $value) {
119 if (strpos($referer, $value) !== false) {
120 return $finalReferer;
124 // Remove port from HTTP_HOST
125 if ($pos = strpos($host, ':')) {
126 $host = substr($host, 0, $pos);
129 $refererHost = parse_url($referer, PHP_URL_HOST
);
130 if (!empty($referer) && (strpos($refererHost, $host) !== false || startsWith('?', $refererHost))) {
131 $finalReferer = $referer;
134 return $finalReferer;
138 * Validate session ID to prevent Full Path Disclosure.
141 * The session ID's format depends on the hash algorithm set in PHP settings
143 * @param string $sessionId Session ID
145 * @return true if valid, false otherwise.
147 * @see http://php.net/manual/en/function.hash-algos.php
148 * @see http://php.net/manual/en/session.configuration.php
150 function is_session_id_valid($sessionId)
152 if (empty($sessionId)) {
160 if (!preg_match('/^[a-zA-Z0-9,-]{2,128}$/', $sessionId)) {
168 * In a string, converts URLs to clickable links.
170 * @param string $text input string.
171 * @param string $redirector if a redirector is set, use it to gerenate links.
173 * @return string returns $text with all links converted to HTML links.
175 * @see Function inspired from http://www.php.net/manual/en/function.preg-replace.php#85722
177 function text2clickable($text, $redirector)
179 $regex = '!(((?:https?|ftp|file)://|apt:|magnet:)\S+[[:alnum:]]/?)!si';
181 if (empty($redirector)) {
182 return preg_replace($regex, '<a href="$1">$1</a>', $text);
184 // Redirector is set, urlencode the final URL.
185 return preg_replace_callback(
187 function ($matches) use ($redirector) {
188 return '<a href="' . $redirector . urlencode($matches[1]) .'">'. $matches[1] .'</a>';
195 * This function inserts where relevant so that multiple spaces are properly displayed in HTML
196 * even in the absence of <pre> (This is used in description to keep text formatting).
198 * @param string $text input text.
200 * @return string formatted text.
202 function space2nbsp($text)
204 return preg_replace('/(^| ) /m', '$1 ', $text);
208 * Format Shaarli's description
209 * TODO: Move me to ApplicationUtils when it's ready.
211 * @param string $description shaare's description.
212 * @param string $redirector if a redirector is set, use it to gerenate links.
214 * @return string formatted description.
216 function format_description($description, $redirector) {
217 return nl2br(space2nbsp(text2clickable($description, $redirector)));