]>
git.immae.eu Git - github/shaarli/Shaarli.git/blob - application/Utils.php
7 * Logs a message to a text file
9 * The log format is compatible with fail2ban.
11 * @param string $logFile where to write the logs
12 * @param string $clientIp the client's remote IPv4/IPv6 address
13 * @param string $message the message to log
15 function logm($logFile, $clientIp, $message)
19 date('Y/m/d H:i:s').' - '.$clientIp.' - '.strval($message).PHP_EOL
,
25 * Returns the small hash of a string, using RFC 4648 base64url format
28 * - are unique (well, as unique as crc32, at last)
29 * - are always 6 characters long.
30 * - only use the following characters: a-z A-Z 0-9 - _ @
31 * - are NOT cryptographically secure (they CAN be forged)
33 * In Shaarli, they are used as a tinyurl-like link to individual entries,
34 * e.g. smallHash('20111006_131924') --> yZH23w
36 function smallHash($text)
38 $t = rtrim(base64_encode(hash('crc32', $text, true)), '=');
39 return strtr($t, '+/', '-_');
43 * Tells if a string start with a substring
45 function startsWith($haystack, $needle, $case=true)
48 return (strcmp(substr($haystack, 0, strlen($needle)), $needle) === 0);
50 return (strcasecmp(substr($haystack, 0, strlen($needle)), $needle) === 0);
54 * Tells if a string ends with a substring
56 function endsWith($haystack, $needle, $case=true)
59 return (strcmp(substr($haystack, strlen($haystack) - strlen($needle)), $needle) === 0);
61 return (strcasecmp(substr($haystack, strlen($haystack) - strlen($needle)), $needle) === 0);
65 * htmlspecialchars wrapper
69 return htmlspecialchars($str, ENT_COMPAT
, 'UTF-8', false);
73 * Link sanitization before templating
75 function sanitizeLink(&$link)
77 $link['url'] = escape($link['url']); // useful?
78 $link['title'] = escape($link['title']);
79 $link['description'] = escape($link['description']);
80 $link['tags'] = escape($link['tags']);
84 * Checks if a string represents a valid date
86 * @param string $format The expected DateTime format of the string
87 * @param string $string A string-formatted date
89 * @return bool whether the string is a valid date
91 * @see http://php.net/manual/en/class.datetime.php
92 * @see http://php.net/manual/en/datetime.createfromformat.php
94 function checkDateFormat($format, $string)
96 $date = DateTime
::createFromFormat($format, $string);
97 return $date && $date->format($string) == $string;
101 * Generate a header location from HTTP_REFERER.
102 * Make sure the referer is Shaarli itself and prevent redirection loop.
104 * @param string $referer - HTTP_REFERER.
105 * @param string $host - Server HOST.
106 * @param array $loopTerms - Contains list of term to prevent redirection loop.
108 * @return string $referer - final referer.
110 function generateLocation($referer, $host, $loopTerms = array())
114 // No referer if it contains any value in $loopCriteria.
115 foreach ($loopTerms as $value) {
116 if (strpos($referer, $value) !== false) {
117 return $finalReferer;
121 // Remove port from HTTP_HOST
122 if ($pos = strpos($host, ':')) {
123 $host = substr($host, 0, $pos);
126 $refererHost = parse_url($referer, PHP_URL_HOST
);
127 if (!empty($referer) && (strpos($refererHost, $host) !== false || startsWith('?', $refererHost))) {
128 $finalReferer = $referer;
131 return $finalReferer;
135 * Validate session ID to prevent Full Path Disclosure.
138 * The session ID's format depends on the hash algorithm set in PHP settings
140 * @param string $sessionId Session ID
142 * @return true if valid, false otherwise.
144 * @see http://php.net/manual/en/function.hash-algos.php
145 * @see http://php.net/manual/en/session.configuration.php
147 function is_session_id_valid($sessionId)
149 if (empty($sessionId)) {
157 if (!preg_match('/^[a-zA-Z0-9,-]{2,128}$/', $sessionId)) {
165 * In a string, converts URLs to clickable links.
167 * @param string $text input string.
168 * @param string $redirector if a redirector is set, use it to gerenate links.
170 * @return string returns $text with all links converted to HTML links.
172 * @see Function inspired from http://www.php.net/manual/en/function.preg-replace.php#85722
174 function text2clickable($text, $redirector)
176 $regex = '!(((?:https?|ftp|file)://|apt:|magnet:)\S+[[:alnum:]]/?)!si';
178 if (empty($redirector)) {
179 return preg_replace($regex, '<a href="$1">$1</a>', $text);
181 // Redirector is set, urlencode the final URL.
182 return preg_replace_callback(
184 function ($matches) use ($redirector) {
185 return '<a href="' . $redirector . urlencode($matches[1]) .'">'. $matches[1] .'</a>';
192 * This function inserts where relevant so that multiple spaces are properly displayed in HTML
193 * even in the absence of <pre> (This is used in description to keep text formatting).
195 * @param string $text input text.
197 * @return string formatted text.
199 function space2nbsp($text)
201 return preg_replace('/(^| ) /m', '$1 ', $text);
205 * Format Shaarli's description
206 * TODO: Move me to ApplicationUtils when it's ready.
208 * @param string $description shaare's description.
209 * @param string $redirector if a redirector is set, use it to gerenate links.
211 * @return string formatted description.
213 function format_description($description, $redirector) {
214 return nl2br(space2nbsp(text2clickable($description, $redirector)));