aboutsummaryrefslogtreecommitdiffhomepage
path: root/application
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2017-04-03 19:02:33 +0200
committerGitHub <noreply@github.com>2017-04-03 19:02:33 +0200
commit2109bb9d9a355787f8beea8cbfaed2eb1d53283f (patch)
tree6fd8c8319ece5e7e565f1793318ff63c665daf74 /application
parentd9d49b687afcb4c88a88390a7ff30b431ca6be12 (diff)
parentb68134ac1d6a287e3a5003ccde137dd0c7ecdc52 (diff)
downloadShaarli-2109bb9d9a355787f8beea8cbfaed2eb1d53283f.tar.gz
Shaarli-2109bb9d9a355787f8beea8cbfaed2eb1d53283f.tar.zst
Shaarli-2109bb9d9a355787f8beea8cbfaed2eb1d53283f.zip
Merge pull request #800 from ArthurHoaro/hotfix/get-bytes-warning
Fix a warning generated in return_bytes function and refactor it
Diffstat (limited to 'application')
-rw-r--r--application/Utils.php89
1 files changed, 89 insertions, 0 deletions
diff --git a/application/Utils.php b/application/Utils.php
index d6e06610..3ef2a7e2 100644
--- a/application/Utils.php
+++ b/application/Utils.php
@@ -345,3 +345,92 @@ function format_date($date, $time = true, $intl = true)
345 345
346 return $formatter->format($date); 346 return $formatter->format($date);
347} 347}
348
349/**
350 * Check if the input is an integer, no matter its real type.
351 *
352 * PHP is a bit messy regarding this:
353 * - is_int returns false if the input is a string
354 * - ctype_digit returns false if the input is an integer or negative
355 *
356 * @param mixed $input value
357 *
358 * @return bool true if the input is an integer, false otherwise
359 */
360function is_integer_mixed($input)
361{
362 if (is_array($input) || is_bool($input) || is_object($input)) {
363 return false;
364 }
365 $input = strval($input);
366 return ctype_digit($input) || (startsWith($input, '-') && ctype_digit(substr($input, 1)));
367}
368
369/**
370 * Convert post_max_size/upload_max_filesize (e.g. '16M') parameters to bytes.
371 *
372 * @param string $val Size expressed in string.
373 *
374 * @return int Size expressed in bytes.
375 */
376function return_bytes($val)
377{
378 if (is_integer_mixed($val) || $val === '0' || empty($val)) {
379 return $val;
380 }
381 $val = trim($val);
382 $last = strtolower($val[strlen($val)-1]);
383 $val = intval(substr($val, 0, -1));
384 switch($last) {
385 case 'g': $val *= 1024;
386 case 'm': $val *= 1024;
387 case 'k': $val *= 1024;
388 }
389 return $val;
390}
391
392/**
393 * Return a human readable size from bytes.
394 *
395 * @param int $bytes value
396 *
397 * @return string Human readable size
398 */
399function human_bytes($bytes)
400{
401 if ($bytes === '') {
402 return t('Setting not set');
403 }
404 if (! is_integer_mixed($bytes)) {
405 return $bytes;
406 }
407 $bytes = intval($bytes);
408 if ($bytes === 0) {
409 return t('Unlimited');
410 }
411
412 $units = [t('B'), t('kiB'), t('MiB'), t('GiB')];
413 for ($i = 0; $i < count($units) && $bytes >= 1024; ++$i) {
414 $bytes /= 1024;
415 }
416
417 return $bytes . $units[$i];
418}
419
420/**
421 * Try to determine max file size for uploads (POST).
422 * Returns an integer (in bytes)
423 *
424 * @param mixed $limitPost post_max_size PHP setting
425 * @param mixed $limitUpload upload_max_filesize PHP setting
426 *
427 * @return int max upload file size in bytes.
428 */
429function get_max_upload_size($limitPost, $limitUpload)
430{
431 $size1 = return_bytes($limitPost);
432 $size2 = return_bytes($limitUpload);
433 // Return the smaller of two:
434 $maxsize = min($size1, $size2);
435 return human_bytes($maxsize);
436}