aboutsummaryrefslogtreecommitdiffhomepage
path: root/application/Utils.php
diff options
context:
space:
mode:
Diffstat (limited to 'application/Utils.php')
-rw-r--r--application/Utils.php129
1 files changed, 126 insertions, 3 deletions
diff --git a/application/Utils.php b/application/Utils.php
index 87e5cc8f..4a2f5561 100644
--- a/application/Utils.php
+++ b/application/Utils.php
@@ -325,25 +325,148 @@ function normalize_spaces($string)
325 * otherwise default format '%c' will be returned. 325 * otherwise default format '%c' will be returned.
326 * 326 *
327 * @param DateTime $date to format. 327 * @param DateTime $date to format.
328 * @param bool $time Displays time if true.
328 * @param bool $intl Use international format if true. 329 * @param bool $intl Use international format if true.
329 * 330 *
330 * @return bool|string Formatted date, or false if the input is invalid. 331 * @return bool|string Formatted date, or false if the input is invalid.
331 */ 332 */
332function format_date($date, $intl = true) 333function format_date($date, $time = true, $intl = true)
333{ 334{
334 if (! $date instanceof DateTime) { 335 if (! $date instanceof DateTime) {
335 return false; 336 return false;
336 } 337 }
337 338
338 if (! $intl || ! class_exists('IntlDateFormatter')) { 339 if (! $intl || ! class_exists('IntlDateFormatter')) {
339 return strftime('%c', $date->getTimestamp()); 340 $format = $time ? '%c' : '%x';
341 return strftime($format, $date->getTimestamp());
340 } 342 }
341 343
342 $formatter = new IntlDateFormatter( 344 $formatter = new IntlDateFormatter(
343 setlocale(LC_TIME, 0), 345 setlocale(LC_TIME, 0),
344 IntlDateFormatter::LONG, 346 IntlDateFormatter::LONG,
345 IntlDateFormatter::LONG 347 $time ? IntlDateFormatter::LONG : IntlDateFormatter::NONE
346 ); 348 );
347 349
348 return $formatter->format($date); 350 return $formatter->format($date);
349} 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 */
364function 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 */
380function 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 */
403function 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 */
434function 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 */
453function 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}