]>
Commit | Line | Data |
---|---|---|
ca74886f V |
1 | <?php |
2 | /** | |
3 | * Shaarli utilities | |
4 | */ | |
5 | ||
1abe6555 V |
6 | /** |
7 | * Logs a message to a text file | |
8 | * | |
478ce8af V |
9 | * The log format is compatible with fail2ban. |
10 | * | |
1abe6555 V |
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 | |
14 | */ | |
15 | function logm($logFile, $clientIp, $message) | |
16 | { | |
478ce8af V |
17 | file_put_contents( |
18 | $logFile, | |
aa7f7b3e | 19 | date('Y/m/d H:i:s').' - '.$clientIp.' - '.strval($message).PHP_EOL, |
478ce8af V |
20 | FILE_APPEND |
21 | ); | |
1abe6555 V |
22 | } |
23 | ||
ca74886f V |
24 | /** |
25 | * Returns the small hash of a string, using RFC 4648 base64url format | |
26 | * | |
27 | * Small hashes: | |
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) | |
32 | * | |
33 | * In Shaarli, they are used as a tinyurl-like link to individual entries, | |
34 | * e.g. smallHash('20111006_131924') --> yZH23w | |
35 | */ | |
36 | function smallHash($text) | |
37 | { | |
38 | $t = rtrim(base64_encode(hash('crc32', $text, true)), '='); | |
39 | return strtr($t, '+/', '-_'); | |
40 | } | |
41 | ||
42 | /** | |
43 | * Tells if a string start with a substring | |
44 | */ | |
45 | function startsWith($haystack, $needle, $case=true) | |
46 | { | |
47 | if ($case) { | |
48 | return (strcmp(substr($haystack, 0, strlen($needle)), $needle) === 0); | |
49 | } | |
50 | return (strcasecmp(substr($haystack, 0, strlen($needle)), $needle) === 0); | |
51 | } | |
52 | ||
53 | /** | |
54 | * Tells if a string ends with a substring | |
55 | */ | |
56 | function endsWith($haystack, $needle, $case=true) | |
57 | { | |
58 | if ($case) { | |
59 | return (strcmp(substr($haystack, strlen($haystack) - strlen($needle)), $needle) === 0); | |
60 | } | |
61 | return (strcasecmp(substr($haystack, strlen($haystack) - strlen($needle)), $needle) === 0); | |
62 | } | |
64bc92e3 | 63 | |
64bc92e3 | 64 | /** |
2925687e A |
65 | * Htmlspecialchars wrapper |
66 | * | |
67 | * @param string $str the string to escape. | |
68 | * | |
69 | * @return string escaped. | |
64bc92e3 | 70 | */ |
71 | function escape($str) | |
72 | { | |
73 | return htmlspecialchars($str, ENT_COMPAT, 'UTF-8', false); | |
74 | } | |
75 | ||
2925687e A |
76 | /** |
77 | * Reverse the escape function. | |
78 | * | |
79 | * @param string $str the string to unescape. | |
80 | * | |
81 | * @return string unescaped string. | |
82 | */ | |
83 | function unescape($str) | |
84 | { | |
85 | return htmlspecialchars_decode($str); | |
86 | } | |
87 | ||
64bc92e3 | 88 | /** |
89 | * Link sanitization before templating | |
90 | */ | |
91 | function sanitizeLink(&$link) | |
92 | { | |
93 | $link['url'] = escape($link['url']); // useful? | |
94 | $link['title'] = escape($link['title']); | |
95 | $link['description'] = escape($link['description']); | |
96 | $link['tags'] = escape($link['tags']); | |
97 | } | |
9186ab95 V |
98 | |
99 | /** | |
100 | * Checks if a string represents a valid date | |
822bffce A |
101 | |
102 | * @param string $format The expected DateTime format of the string | |
103 | * @param string $string A string-formatted date | |
104 | * | |
105 | * @return bool whether the string is a valid date | |
9186ab95 | 106 | * |
822bffce A |
107 | * @see http://php.net/manual/en/class.datetime.php |
108 | * @see http://php.net/manual/en/datetime.createfromformat.php | |
9186ab95 V |
109 | */ |
110 | function checkDateFormat($format, $string) | |
111 | { | |
112 | $date = DateTime::createFromFormat($format, $string); | |
113 | return $date && $date->format($string) == $string; | |
114 | } | |
775803a0 A |
115 | |
116 | /** | |
117 | * Generate a header location from HTTP_REFERER. | |
118 | * Make sure the referer is Shaarli itself and prevent redirection loop. | |
119 | * | |
120 | * @param string $referer - HTTP_REFERER. | |
121 | * @param string $host - Server HOST. | |
122 | * @param array $loopTerms - Contains list of term to prevent redirection loop. | |
123 | * | |
124 | * @return string $referer - final referer. | |
125 | */ | |
126 | function generateLocation($referer, $host, $loopTerms = array()) | |
127 | { | |
d01c2342 | 128 | $finalReferer = '?'; |
775803a0 A |
129 | |
130 | // No referer if it contains any value in $loopCriteria. | |
131 | foreach ($loopTerms as $value) { | |
132 | if (strpos($referer, $value) !== false) { | |
d01c2342 | 133 | return $finalReferer; |
775803a0 A |
134 | } |
135 | } | |
136 | ||
137 | // Remove port from HTTP_HOST | |
138 | if ($pos = strpos($host, ':')) { | |
139 | $host = substr($host, 0, $pos); | |
140 | } | |
141 | ||
d01c2342 A |
142 | $refererHost = parse_url($referer, PHP_URL_HOST); |
143 | if (!empty($referer) && (strpos($refererHost, $host) !== false || startsWith('?', $refererHost))) { | |
144 | $finalReferer = $referer; | |
775803a0 A |
145 | } |
146 | ||
d01c2342 | 147 | return $finalReferer; |
775803a0 | 148 | } |
d1e2f8e5 | 149 | |
06b6660a A |
150 | /** |
151 | * Validate session ID to prevent Full Path Disclosure. | |
68bc2135 | 152 | * |
06b6660a | 153 | * See #298. |
68bc2135 | 154 | * The session ID's format depends on the hash algorithm set in PHP settings |
06b6660a A |
155 | * |
156 | * @param string $sessionId Session ID | |
157 | * | |
158 | * @return true if valid, false otherwise. | |
68bc2135 V |
159 | * |
160 | * @see http://php.net/manual/en/function.hash-algos.php | |
161 | * @see http://php.net/manual/en/session.configuration.php | |
06b6660a A |
162 | */ |
163 | function is_session_id_valid($sessionId) | |
164 | { | |
165 | if (empty($sessionId)) { | |
166 | return false; | |
167 | } | |
168 | ||
169 | if (!$sessionId) { | |
170 | return false; | |
171 | } | |
172 | ||
68bc2135 | 173 | if (!preg_match('/^[a-zA-Z0-9,-]{2,128}$/', $sessionId)) { |
06b6660a A |
174 | return false; |
175 | } | |
176 | ||
177 | return true; | |
178 | } | |
90e5bd65 A |
179 | |
180 | /** | |
181 | * In a string, converts URLs to clickable links. | |
182 | * | |
183 | * @param string $text input string. | |
184 | * @param string $redirector if a redirector is set, use it to gerenate links. | |
185 | * | |
186 | * @return string returns $text with all links converted to HTML links. | |
187 | * | |
188 | * @see Function inspired from http://www.php.net/manual/en/function.preg-replace.php#85722 | |
189 | */ | |
190 | function text2clickable($text, $redirector) | |
191 | { | |
192 | $regex = '!(((?:https?|ftp|file)://|apt:|magnet:)\S+[[:alnum:]]/?)!si'; | |
193 | ||
194 | if (empty($redirector)) { | |
195 | return preg_replace($regex, '<a href="$1">$1</a>', $text); | |
196 | } | |
197 | // Redirector is set, urlencode the final URL. | |
198 | return preg_replace_callback( | |
199 | $regex, | |
200 | function ($matches) use ($redirector) { | |
201 | return '<a href="' . $redirector . urlencode($matches[1]) .'">'. $matches[1] .'</a>'; | |
202 | }, | |
203 | $text | |
204 | ); | |
205 | } | |
206 | ||
207 | /** | |
208 | * This function inserts where relevant so that multiple spaces are properly displayed in HTML | |
209 | * even in the absence of <pre> (This is used in description to keep text formatting). | |
210 | * | |
211 | * @param string $text input text. | |
212 | * | |
213 | * @return string formatted text. | |
214 | */ | |
215 | function space2nbsp($text) | |
216 | { | |
217 | return preg_replace('/(^| ) /m', '$1 ', $text); | |
218 | } | |
219 | ||
220 | /** | |
221 | * Format Shaarli's description | |
222 | * TODO: Move me to ApplicationUtils when it's ready. | |
223 | * | |
224 | * @param string $description shaare's description. | |
225 | * @param string $redirector if a redirector is set, use it to gerenate links. | |
226 | * | |
227 | * @return string formatted description. | |
228 | */ | |
229 | function format_description($description, $redirector) { | |
230 | return nl2br(space2nbsp(text2clickable($description, $redirector))); | |
231 | } | |
7b63e4ca A |
232 | |
233 | /** | |
234 | * Sniff browser language to set the locale automatically. | |
235 | * Note that is may not work on your server if the corresponding locale is not installed. | |
236 | * | |
237 | * @param string $headerLocale Locale send in HTTP headers (e.g. "fr,fr-fr;q=0.8,en;q=0.5,en-us;q=0.3"). | |
238 | **/ | |
239 | function autoLocale($headerLocale) | |
240 | { | |
241 | // Default if browser does not send HTTP_ACCEPT_LANGUAGE | |
242 | $attempts = array('en_US'); | |
243 | if (isset($headerLocale)) { | |
244 | // (It's a bit crude, but it works very well. Preferred language is always presented first.) | |
245 | if (preg_match('/([a-z]{2})-?([a-z]{2})?/i', $headerLocale, $matches)) { | |
246 | $loc = $matches[1] . (!empty($matches[2]) ? '_' . strtoupper($matches[2]) : ''); | |
247 | $attempts = array( | |
248 | $loc.'.UTF-8', $loc, str_replace('_', '-', $loc).'.UTF-8', str_replace('_', '-', $loc), | |
249 | $loc . '_' . strtoupper($loc).'.UTF-8', $loc . '_' . strtoupper($loc), | |
250 | $loc . '_' . $loc.'.UTF-8', $loc . '_' . $loc, $loc . '-' . strtoupper($loc).'.UTF-8', | |
251 | $loc . '-' . strtoupper($loc), $loc . '-' . $loc.'.UTF-8', $loc . '-' . $loc | |
252 | ); | |
253 | } | |
254 | } | |
255 | setlocale(LC_ALL, $attempts); | |
256 | } |