]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - application/Utils.php
Merge pull request #928 from virtualtam/documentation/v0.9.1
[github/shaarli/Shaarli.git] / application / Utils.php
1 <?php
2 /**
3 * Shaarli utilities
4 */
5
6 /**
7 * Logs a message to a text file
8 *
9 * The log format is compatible with fail2ban.
10 *
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 {
17 file_put_contents(
18 $logFile,
19 date('Y/m/d H:i:s').' - '.$clientIp.' - '.strval($message).PHP_EOL,
20 FILE_APPEND
21 );
22 }
23
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 * 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.
43 */
44 function smallHash($text)
45 {
46 $t = rtrim(base64_encode(hash('crc32', $text, true)), '=');
47 return strtr($t, '+/', '-_');
48 }
49
50 /**
51 * Tells if a string start with a substring
52 *
53 * @param string $haystack Given string.
54 * @param string $needle String to search at the beginning of $haystack.
55 * @param bool $case Case sensitive.
56 *
57 * @return bool True if $haystack starts with $needle.
58 */
59 function startsWith($haystack, $needle, $case = true)
60 {
61 if ($case) {
62 return (strcmp(substr($haystack, 0, strlen($needle)), $needle) === 0);
63 }
64 return (strcasecmp(substr($haystack, 0, strlen($needle)), $needle) === 0);
65 }
66
67 /**
68 * Tells if a string ends with a substring
69 *
70 * @param string $haystack Given string.
71 * @param string $needle String to search at the end of $haystack.
72 * @param bool $case Case sensitive.
73 *
74 * @return bool True if $haystack ends with $needle.
75 */
76 function endsWith($haystack, $needle, $case = true)
77 {
78 if ($case) {
79 return (strcmp(substr($haystack, strlen($haystack) - strlen($needle)), $needle) === 0);
80 }
81 return (strcasecmp(substr($haystack, strlen($haystack) - strlen($needle)), $needle) === 0);
82 }
83
84 /**
85 * Htmlspecialchars wrapper
86 * Support multidimensional array of strings.
87 *
88 * @param mixed $input Data to escape: a single string or an array of strings.
89 *
90 * @return string escaped.
91 */
92 function escape($input)
93 {
94 if (is_bool($input)) {
95 return $input;
96 }
97
98 if (is_array($input)) {
99 $out = array();
100 foreach($input as $key => $value) {
101 $out[$key] = escape($value);
102 }
103 return $out;
104 }
105 return htmlspecialchars($input, ENT_COMPAT, 'UTF-8', false);
106 }
107
108 /**
109 * Reverse the escape function.
110 *
111 * @param string $str the string to unescape.
112 *
113 * @return string unescaped string.
114 */
115 function unescape($str)
116 {
117 return htmlspecialchars_decode($str);
118 }
119
120 /**
121 * Sanitize link before rendering.
122 *
123 * @param array $link Link to escape.
124 */
125 function sanitizeLink(&$link)
126 {
127 $link['url'] = escape($link['url']); // useful?
128 $link['title'] = escape($link['title']);
129 $link['description'] = escape($link['description']);
130 $link['tags'] = escape($link['tags']);
131 }
132
133 /**
134 * Checks if a string represents a valid date
135
136 * @param string $format The expected DateTime format of the string
137 * @param string $string A string-formatted date
138 *
139 * @return bool whether the string is a valid date
140 *
141 * @see http://php.net/manual/en/class.datetime.php
142 * @see http://php.net/manual/en/datetime.createfromformat.php
143 */
144 function checkDateFormat($format, $string)
145 {
146 $date = DateTime::createFromFormat($format, $string);
147 return $date && $date->format($string) == $string;
148 }
149
150 /**
151 * Generate a header location from HTTP_REFERER.
152 * Make sure the referer is Shaarli itself and prevent redirection loop.
153 *
154 * @param string $referer - HTTP_REFERER.
155 * @param string $host - Server HOST.
156 * @param array $loopTerms - Contains list of term to prevent redirection loop.
157 *
158 * @return string $referer - final referer.
159 */
160 function generateLocation($referer, $host, $loopTerms = array())
161 {
162 $finalReferer = '?';
163
164 // No referer if it contains any value in $loopCriteria.
165 foreach ($loopTerms as $value) {
166 if (strpos($referer, $value) !== false) {
167 return $finalReferer;
168 }
169 }
170
171 // Remove port from HTTP_HOST
172 if ($pos = strpos($host, ':')) {
173 $host = substr($host, 0, $pos);
174 }
175
176 $refererHost = parse_url($referer, PHP_URL_HOST);
177 if (!empty($referer) && (strpos($refererHost, $host) !== false || startsWith('?', $refererHost))) {
178 $finalReferer = $referer;
179 }
180
181 return $finalReferer;
182 }
183
184 /**
185 * Validate session ID to prevent Full Path Disclosure.
186 *
187 * See #298.
188 * The session ID's format depends on the hash algorithm set in PHP settings
189 *
190 * @param string $sessionId Session ID
191 *
192 * @return true if valid, false otherwise.
193 *
194 * @see http://php.net/manual/en/function.hash-algos.php
195 * @see http://php.net/manual/en/session.configuration.php
196 */
197 function is_session_id_valid($sessionId)
198 {
199 if (empty($sessionId)) {
200 return false;
201 }
202
203 if (!$sessionId) {
204 return false;
205 }
206
207 if (!preg_match('/^[a-zA-Z0-9,-]{2,128}$/', $sessionId)) {
208 return false;
209 }
210
211 return true;
212 }
213
214 /**
215 * Sniff browser language to set the locale automatically.
216 * Note that is may not work on your server if the corresponding locale is not installed.
217 *
218 * @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").
219 **/
220 function autoLocale($headerLocale)
221 {
222 // Default if browser does not send HTTP_ACCEPT_LANGUAGE
223 $locales = array('en_US', 'en_US.utf8', 'en_US.UTF-8');
224 if (! empty($headerLocale)) {
225 if (preg_match_all('/([a-z]{2,3})[-_]?([a-z]{2})?,?/i', $headerLocale, $matches, PREG_SET_ORDER)) {
226 $attempts = [];
227 foreach ($matches as $match) {
228 $first = [strtolower($match[1]), strtoupper($match[1])];
229 $separators = ['_', '-'];
230 $encodings = ['utf8', 'UTF-8'];
231 if (!empty($match[2])) {
232 $second = [strtoupper($match[2]), strtolower($match[2])];
233 $items = [$first, $separators, $second, ['.'], $encodings];
234 } else {
235 $items = [$first, $separators, $first, ['.'], $encodings];
236 }
237 $attempts = array_merge($attempts, iterator_to_array(cartesian_product_generator($items)));
238 }
239
240 if (! empty($attempts)) {
241 $locales = array_merge(array_map('implode', $attempts), $locales);
242 }
243 }
244 }
245
246 setlocale(LC_ALL, $locales);
247 }
248
249 /**
250 * Build a Generator object representing the cartesian product from given $items.
251 *
252 * Example:
253 * [['a'], ['b', 'c']]
254 * will generate:
255 * [
256 * ['a', 'b'],
257 * ['a', 'c'],
258 * ]
259 *
260 * @param array $items array of array of string
261 *
262 * @return Generator representing the cartesian product of given array.
263 *
264 * @see https://en.wikipedia.org/wiki/Cartesian_product
265 */
266 function cartesian_product_generator($items)
267 {
268 if (empty($items)) {
269 yield [];
270 }
271 $subArray = array_pop($items);
272 if (empty($subArray)) {
273 return;
274 }
275 foreach (cartesian_product_generator($items) as $item) {
276 foreach ($subArray as $value) {
277 yield $item + [count($item) => $value];
278 }
279 }
280 }
281
282 /**
283 * Generates a default API secret.
284 *
285 * Note that the random-ish methods used in this function are predictable,
286 * which makes them NOT suitable for crypto.
287 * BUT the random string is salted with the salt and hashed with the username.
288 * It makes the generated API secret secured enough for Shaarli.
289 *
290 * PHP 7 provides random_int(), designed for cryptography.
291 * More info: http://stackoverflow.com/questions/4356289/php-random-string-generator
292
293 * @param string $username Shaarli login username
294 * @param string $salt Shaarli password hash salt
295 *
296 * @return string|bool Generated API secret, 12 char length.
297 * Or false if invalid parameters are provided (which will make the API unusable).
298 */
299 function generate_api_secret($username, $salt)
300 {
301 if (empty($username) || empty($salt)) {
302 return false;
303 }
304
305 return str_shuffle(substr(hash_hmac('sha512', uniqid($salt), $username), 10, 12));
306 }
307
308 /**
309 * Trim string, replace sequences of whitespaces by a single space.
310 * PHP equivalent to `normalize-space` XSLT function.
311 *
312 * @param string $string Input string.
313 *
314 * @return mixed Normalized string.
315 */
316 function normalize_spaces($string)
317 {
318 return preg_replace('/\s{2,}/', ' ', trim($string));
319 }
320
321 /**
322 * Format the date according to the locale.
323 *
324 * Requires php-intl to display international datetimes,
325 * otherwise default format '%c' will be returned.
326 *
327 * @param DateTime $date to format.
328 * @param bool $time Displays time if true.
329 * @param bool $intl Use international format if true.
330 *
331 * @return bool|string Formatted date, or false if the input is invalid.
332 */
333 function format_date($date, $time = true, $intl = true)
334 {
335 if (! $date instanceof DateTime) {
336 return false;
337 }
338
339 if (! $intl || ! class_exists('IntlDateFormatter')) {
340 $format = $time ? '%c' : '%x';
341 return strftime($format, $date->getTimestamp());
342 }
343
344 $formatter = new IntlDateFormatter(
345 setlocale(LC_TIME, 0),
346 IntlDateFormatter::LONG,
347 $time ? IntlDateFormatter::LONG : IntlDateFormatter::NONE
348 );
349
350 return $formatter->format($date);
351 }
352
353 /**
354 * Check if the input is an integer, no matter its real type.
355 *
356 * PHP is a bit messy regarding this:
357 * - is_int returns false if the input is a string
358 * - ctype_digit returns false if the input is an integer or negative
359 *
360 * @param mixed $input value
361 *
362 * @return bool true if the input is an integer, false otherwise
363 */
364 function is_integer_mixed($input)
365 {
366 if (is_array($input) || is_bool($input) || is_object($input)) {
367 return false;
368 }
369 $input = strval($input);
370 return ctype_digit($input) || (startsWith($input, '-') && ctype_digit(substr($input, 1)));
371 }
372
373 /**
374 * Convert post_max_size/upload_max_filesize (e.g. '16M') parameters to bytes.
375 *
376 * @param string $val Size expressed in string.
377 *
378 * @return int Size expressed in bytes.
379 */
380 function return_bytes($val)
381 {
382 if (is_integer_mixed($val) || $val === '0' || empty($val)) {
383 return $val;
384 }
385 $val = trim($val);
386 $last = strtolower($val[strlen($val)-1]);
387 $val = intval(substr($val, 0, -1));
388 switch($last) {
389 case 'g': $val *= 1024;
390 case 'm': $val *= 1024;
391 case 'k': $val *= 1024;
392 }
393 return $val;
394 }
395
396 /**
397 * Return a human readable size from bytes.
398 *
399 * @param int $bytes value
400 *
401 * @return string Human readable size
402 */
403 function human_bytes($bytes)
404 {
405 if ($bytes === '') {
406 return t('Setting not set');
407 }
408 if (! is_integer_mixed($bytes)) {
409 return $bytes;
410 }
411 $bytes = intval($bytes);
412 if ($bytes === 0) {
413 return t('Unlimited');
414 }
415
416 $units = [t('B'), t('kiB'), t('MiB'), t('GiB')];
417 for ($i = 0; $i < count($units) && $bytes >= 1024; ++$i) {
418 $bytes /= 1024;
419 }
420
421 return round($bytes) . $units[$i];
422 }
423
424 /**
425 * Try to determine max file size for uploads (POST).
426 * Returns an integer (in bytes) or formatted depending on $format.
427 *
428 * @param mixed $limitPost post_max_size PHP setting
429 * @param mixed $limitUpload upload_max_filesize PHP setting
430 * @param bool $format Format max upload size to human readable size
431 *
432 * @return int|string max upload file size
433 */
434 function get_max_upload_size($limitPost, $limitUpload, $format = true)
435 {
436 $size1 = return_bytes($limitPost);
437 $size2 = return_bytes($limitUpload);
438 // Return the smaller of two:
439 $maxsize = min($size1, $size2);
440 return $format ? human_bytes($maxsize) : $maxsize;
441 }
442
443 /**
444 * Sort the given array alphabetically using php-intl if available.
445 * Case sensitive.
446 *
447 * Note: doesn't support multidimensional arrays
448 *
449 * @param array $data Input array, passed by reference
450 * @param bool $reverse Reverse sort if set to true
451 * @param bool $byKeys Sort the array by keys if set to true, by value otherwise.
452 */
453 function alphabetical_sort(&$data, $reverse = false, $byKeys = false)
454 {
455 $callback = function($a, $b) use ($reverse) {
456 // Collator is part of PHP intl.
457 if (class_exists('Collator')) {
458 $collator = new Collator(setlocale(LC_COLLATE, 0));
459 if (!intl_is_failure(intl_get_error_code())) {
460 return $collator->compare($a, $b) * ($reverse ? -1 : 1);
461 }
462 }
463
464 return strcasecmp($a, $b) * ($reverse ? -1 : 1);
465 };
466
467 if ($byKeys) {
468 uksort($data, $callback);
469 } else {
470 usort($data, $callback);
471 }
472 }