aboutsummaryrefslogtreecommitdiffhomepage
path: root/application/Utils.php
diff options
context:
space:
mode:
Diffstat (limited to 'application/Utils.php')
-rw-r--r--application/Utils.php78
1 files changed, 57 insertions, 21 deletions
diff --git a/application/Utils.php b/application/Utils.php
index bcfda65c..952378ab 100644
--- a/application/Utils.php
+++ b/application/Utils.php
@@ -1,24 +1,27 @@
1<?php 1<?php
2
2/** 3/**
3 * Shaarli utilities 4 * Shaarli utilities
4 */ 5 */
5 6
6/** 7/**
7 * Logs a message to a text file 8 * Format log using provided data.
8 * 9 *
9 * The log format is compatible with fail2ban. 10 * @param string $message the message to log
11 * @param string|null $clientIp the client's remote IPv4/IPv6 address
10 * 12 *
11 * @param string $logFile where to write the logs 13 * @return string Formatted message to log
12 * @param string $clientIp the client's remote IPv4/IPv6 address
13 * @param string $message the message to log
14 */ 14 */
15function logm($logFile, $clientIp, $message) 15function format_log(string $message, string $clientIp = null): string
16{ 16{
17 file_put_contents( 17 $out = $message;
18 $logFile, 18
19 date('Y/m/d H:i:s').' - '.$clientIp.' - '.strval($message).PHP_EOL, 19 if (!empty($clientIp)) {
20 FILE_APPEND 20 // Note: we keep the first dash to avoid breaking fail2ban configs
21 ); 21 $out = '- ' . $clientIp . ' - ' . $out;
22 }
23
24 return $out;
22} 25}
23 26
24/** 27/**
@@ -100,7 +103,7 @@ function escape($input)
100 } 103 }
101 104
102 if (is_array($input)) { 105 if (is_array($input)) {
103 $out = array(); 106 $out = [];
104 foreach ($input as $key => $value) { 107 foreach ($input as $key => $value) {
105 $out[escape($key)] = escape($value); 108 $out[escape($key)] = escape($value);
106 } 109 }
@@ -161,7 +164,7 @@ function checkDateFormat($format, $string)
161 * 164 *
162 * @return string $referer - final referer. 165 * @return string $referer - final referer.
163 */ 166 */
164function generateLocation($referer, $host, $loopTerms = array()) 167function generateLocation($referer, $host, $loopTerms = [])
165{ 168{
166 $finalReferer = './?'; 169 $finalReferer = './?';
167 170
@@ -194,7 +197,7 @@ function generateLocation($referer, $host, $loopTerms = array())
194function autoLocale($headerLocale) 197function autoLocale($headerLocale)
195{ 198{
196 // Default if browser does not send HTTP_ACCEPT_LANGUAGE 199 // Default if browser does not send HTTP_ACCEPT_LANGUAGE
197 $locales = array('en_US', 'en_US.utf8', 'en_US.UTF-8'); 200 $locales = ['en_US', 'en_US.utf8', 'en_US.UTF-8'];
198 if (! empty($headerLocale)) { 201 if (! empty($headerLocale)) {
199 if (preg_match_all('/([a-z]{2,3})[-_]?([a-z]{2})?,?/i', $headerLocale, $matches, PREG_SET_ORDER)) { 202 if (preg_match_all('/([a-z]{2,3})[-_]?([a-z]{2})?,?/i', $headerLocale, $matches, PREG_SET_ORDER)) {
200 $attempts = []; 203 $attempts = [];
@@ -325,6 +328,23 @@ function format_date($date, $time = true, $intl = true)
325} 328}
326 329
327/** 330/**
331 * Format the date month according to the locale.
332 *
333 * @param DateTimeInterface $date to format.
334 *
335 * @return bool|string Formatted date, or false if the input is invalid.
336 */
337function format_month(DateTimeInterface $date)
338{
339 if (! $date instanceof DateTimeInterface) {
340 return false;
341 }
342
343 return strftime('%B', $date->getTimestamp());
344}
345
346
347/**
328 * Check if the input is an integer, no matter its real type. 348 * Check if the input is an integer, no matter its real type.
329 * 349 *
330 * PHP is a bit messy regarding this: 350 * PHP is a bit messy regarding this:
@@ -357,13 +377,15 @@ function return_bytes($val)
357 return $val; 377 return $val;
358 } 378 }
359 $val = trim($val); 379 $val = trim($val);
360 $last = strtolower($val[strlen($val)-1]); 380 $last = strtolower($val[strlen($val) - 1]);
361 $val = intval(substr($val, 0, -1)); 381 $val = intval(substr($val, 0, -1));
362 switch ($last) { 382 switch ($last) {
363 case 'g': 383 case 'g':
364 $val *= 1024; 384 $val *= 1024;
385 // do no break in order 1024^2 for each unit
365 case 'm': 386 case 'm':
366 $val *= 1024; 387 $val *= 1024;
388 // do no break in order 1024^2 for each unit
367 case 'k': 389 case 'k':
368 $val *= 1024; 390 $val *= 1024;
369 } 391 }
@@ -452,14 +474,28 @@ function alphabetical_sort(&$data, $reverse = false, $byKeys = false)
452 * Wrapper function for translation which match the API 474 * Wrapper function for translation which match the API
453 * of gettext()/_() and ngettext(). 475 * of gettext()/_() and ngettext().
454 * 476 *
455 * @param string $text Text to translate. 477 * @param string $text Text to translate.
456 * @param string $nText The plural message ID. 478 * @param string $nText The plural message ID.
457 * @param int $nb The number of items for plural forms. 479 * @param int $nb The number of items for plural forms.
458 * @param string $domain The domain where the translation is stored (default: shaarli). 480 * @param string $domain The domain where the translation is stored (default: shaarli).
481 * @param array $variables Associative array of variables to replace in translated text.
482 * @param bool $fixCase Apply `ucfirst` on the translated string, might be useful for strings with variables.
459 * 483 *
460 * @return string Text translated. 484 * @return string Text translated.
461 */ 485 */
462function t($text, $nText = '', $nb = 1, $domain = 'shaarli') 486function t($text, $nText = '', $nb = 1, $domain = 'shaarli', $variables = [], $fixCase = false)
487{
488 $postFunction = $fixCase ? 'ucfirst' : function ($input) {
489 return $input;
490 };
491
492 return $postFunction(dn__($domain, $text, $nText, $nb, $variables));
493}
494
495/**
496 * Converts an exception into a printable stack trace string.
497 */
498function exception2text(Throwable $e): string
463{ 499{
464 return dn__($domain, $text, $nText, $nb); 500 return $e->getMessage() . PHP_EOL . $e->getFile() . $e->getLine() . PHP_EOL . $e->getTraceAsString();
465} 501}