aboutsummaryrefslogtreecommitdiffhomepage
path: root/application/Utils.php
diff options
context:
space:
mode:
Diffstat (limited to 'application/Utils.php')
-rw-r--r--application/Utils.php84
1 files changed, 60 insertions, 24 deletions
diff --git a/application/Utils.php b/application/Utils.php
index 4b7fc546..db046893 100644
--- a/application/Utils.php
+++ b/application/Utils.php
@@ -4,21 +4,23 @@
4 */ 4 */
5 5
6/** 6/**
7 * Logs a message to a text file 7 * Format log using provided data.
8 * 8 *
9 * The log format is compatible with fail2ban. 9 * @param string $message the message to log
10 * @param string|null $clientIp the client's remote IPv4/IPv6 address
10 * 11 *
11 * @param string $logFile where to write the logs 12 * @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 */ 13 */
15function logm($logFile, $clientIp, $message) 14function format_log(string $message, string $clientIp = null): string
16{ 15{
17 file_put_contents( 16 $out = $message;
18 $logFile, 17
19 date('Y/m/d H:i:s').' - '.$clientIp.' - '.strval($message).PHP_EOL, 18 if (!empty($clientIp)) {
20 FILE_APPEND 19 // Note: we keep the first dash to avoid breaking fail2ban configs
21 ); 20 $out = '- ' . $clientIp . ' - ' . $out;
21 }
22
23 return $out;
22} 24}
23 25
24/** 26/**
@@ -87,18 +89,22 @@ function endsWith($haystack, $needle, $case = true)
87 * 89 *
88 * @param mixed $input Data to escape: a single string or an array of strings. 90 * @param mixed $input Data to escape: a single string or an array of strings.
89 * 91 *
90 * @return string escaped. 92 * @return string|array escaped.
91 */ 93 */
92function escape($input) 94function escape($input)
93{ 95{
94 if (is_bool($input)) { 96 if (null === $input) {
97 return null;
98 }
99
100 if (is_bool($input) || is_int($input) || is_float($input) || $input instanceof DateTimeInterface) {
95 return $input; 101 return $input;
96 } 102 }
97 103
98 if (is_array($input)) { 104 if (is_array($input)) {
99 $out = array(); 105 $out = array();
100 foreach ($input as $key => $value) { 106 foreach ($input as $key => $value) {
101 $out[$key] = escape($value); 107 $out[escape($key)] = escape($value);
102 } 108 }
103 return $out; 109 return $out;
104 } 110 }
@@ -294,15 +300,15 @@ function normalize_spaces($string)
294 * Requires php-intl to display international datetimes, 300 * Requires php-intl to display international datetimes,
295 * otherwise default format '%c' will be returned. 301 * otherwise default format '%c' will be returned.
296 * 302 *
297 * @param DateTime $date to format. 303 * @param DateTimeInterface $date to format.
298 * @param bool $time Displays time if true. 304 * @param bool $time Displays time if true.
299 * @param bool $intl Use international format if true. 305 * @param bool $intl Use international format if true.
300 * 306 *
301 * @return bool|string Formatted date, or false if the input is invalid. 307 * @return bool|string Formatted date, or false if the input is invalid.
302 */ 308 */
303function format_date($date, $time = true, $intl = true) 309function format_date($date, $time = true, $intl = true)
304{ 310{
305 if (! $date instanceof DateTime) { 311 if (! $date instanceof DateTimeInterface) {
306 return false; 312 return false;
307 } 313 }
308 314
@@ -321,6 +327,23 @@ function format_date($date, $time = true, $intl = true)
321} 327}
322 328
323/** 329/**
330 * Format the date month according to the locale.
331 *
332 * @param DateTimeInterface $date to format.
333 *
334 * @return bool|string Formatted date, or false if the input is invalid.
335 */
336function format_month(DateTimeInterface $date)
337{
338 if (! $date instanceof DateTimeInterface) {
339 return false;
340 }
341
342 return strftime('%B', $date->getTimestamp());
343}
344
345
346/**
324 * Check if the input is an integer, no matter its real type. 347 * Check if the input is an integer, no matter its real type.
325 * 348 *
326 * PHP is a bit messy regarding this: 349 * PHP is a bit messy regarding this:
@@ -448,14 +471,27 @@ function alphabetical_sort(&$data, $reverse = false, $byKeys = false)
448 * Wrapper function for translation which match the API 471 * Wrapper function for translation which match the API
449 * of gettext()/_() and ngettext(). 472 * of gettext()/_() and ngettext().
450 * 473 *
451 * @param string $text Text to translate. 474 * @param string $text Text to translate.
452 * @param string $nText The plural message ID. 475 * @param string $nText The plural message ID.
453 * @param int $nb The number of items for plural forms. 476 * @param int $nb The number of items for plural forms.
454 * @param string $domain The domain where the translation is stored (default: shaarli). 477 * @param string $domain The domain where the translation is stored (default: shaarli).
478 * @param array $variables Associative array of variables to replace in translated text.
479 * @param bool $fixCase Apply `ucfirst` on the translated string, might be useful for strings with variables.
455 * 480 *
456 * @return string Text translated. 481 * @return string Text translated.
457 */ 482 */
458function t($text, $nText = '', $nb = 1, $domain = 'shaarli') 483function t($text, $nText = '', $nb = 1, $domain = 'shaarli', $variables = [], $fixCase = false)
484{
485 $postFunction = $fixCase ? 'ucfirst' : function ($input) { return $input; };
486
487 return $postFunction(dn__($domain, $text, $nText, $nb, $variables));
488}
489
490/**
491 * Converts an exception into a printable stack trace string.
492 */
493function exception2text(Throwable $e): string
459{ 494{
460 return dn__($domain, $text, $nText, $nb); 495 return $e->getMessage() . PHP_EOL . $e->getFile() . $e->getLine() . PHP_EOL . $e->getTraceAsString();
461} 496}
497