X-Git-Url: https://git.p6c8.net/jirafeau_mojo42.git/blobdiff_plain/134fa87d899035f4ca5ebd2755f3ff4c425c2959..refs/merge-requests/36/head:/lib/functions.php diff --git a/lib/functions.php b/lib/functions.php index 592e841..e4bfbb5 100644 --- a/lib/functions.php +++ b/lib/functions.php @@ -23,12 +23,12 @@ * Transform a string in a path by seperating each letters by a '/'. * @return path finishing with a '/' */ -function -s2p ($s) +function s2p($s) { $p = ''; - for ($i = 0; $i < strlen ($s); $i++) + for ($i = 0; $i < strlen($s); $i++) { $p .= $s{$i} . '/'; + } return $p; } @@ -36,11 +36,10 @@ s2p ($s) * Convert base 16 to base 64 * @returns A string based on 64 characters (0-9, a-z, A-Z, "-" and "_") */ -function -base_16_to_64 ($num) +function base_16_to_64($num) { $m = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_'; - $hex2bin = array ('0000', # 0 + $hex2bin = array('0000', # 0 '0001', # 1 '0010', # 2 '0011', # 3 @@ -56,20 +55,23 @@ base_16_to_64 ($num) '1101', # d '1110', # e '1111'); # f - $o = ''; + $o = ''; $b = ''; $i = 0; # Convert long hex string to bin. - $size = strlen ($num); - for ($i = 0; $i < $size; $i++) - $b .= $hex2bin{hexdec ($num{$i})}; + $size = strlen($num); + for ($i = 0; $i < $size; $i++) { + $b .= $hex2bin{hexdec($num{$i})}; + } # Convert long bin to base 64. $size *= 4; - for ($i = $size - 6; $i >= 0; $i -= 6) - $o = $m{bindec (substr ($b, $i, 6))} . $o; + for ($i = $size - 6; $i >= 0; $i -= 6) { + $o = $m{bindec(substr($b, $i, 6))} . $o; + } # Some few bits remaining ? - if ($i < 0 && $i > -6) - $o = $m{bindec (substr ($b, 0, $i + 6))} . $o; + if ($i < 0 && $i > -6) { + $o = $m{bindec(substr($b, 0, $i + 6))} . $o; + } return $o; } @@ -78,80 +80,96 @@ base_16_to_64 ($num) * @param $l code length * @return random code. */ -function -jirafeau_gen_random ($l) +function jirafeau_gen_random($l) { - if ($l <= 0) + if ($l <= 0) { return 42; + } $code=""; - for ($i = 0; $i < $l; $i++) - $code .= dechex (rand (0, 15)); + for ($i = 0; $i < $l; $i++) { + $code .= dechex(rand(0, 15)); + } return $code; } -function -is_ssl() { - if ( isset($_SERVER['HTTPS']) ) { - if ( 'on' == strtolower($_SERVER['HTTPS']) || - '1' == $_SERVER['HTTPS'] ) +function is_ssl() +{ + if (isset($_SERVER['HTTPS'])) { + if ('on' == strtolower($_SERVER['HTTPS']) || + '1' == $_SERVER['HTTPS']) { return true; - } elseif ( isset($_SERVER['SERVER_PORT']) && ( '443' == $_SERVER['SERVER_PORT'] ) ) { + } + } elseif (isset($_SERVER['SERVER_PORT']) && ('443' == $_SERVER['SERVER_PORT'])) { return true; } elseif (isset($_SERVER['HTTP_X_FORWARDED_PROTO'])) { - if ($_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') + if ($_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') { return true; + } } return false; } -function -jirafeau_human_size ($octets) +function jirafeau_human_size($octets) +{ + $u = array('B', 'KB', 'MB', 'GB', 'TB'); + $o = max($octets, 0); + $p = min(floor(($o ? log($o) : 0) / log(1024)), count($u) - 1); + $o /= pow(1024, $p); + return round($o, 1) . $u[$p]; +} + +// Convert UTC timestamp to a datetime field +function jirafeau_get_datetimefield($timestamp) { - $u = array ('B', 'KB', 'MB', 'GB', 'TB'); - $o = max ($octets, 0); - $p = min (floor (($o ? log ($o) : 0) / log (1024)), count ($u) - 1); - $o /= pow (1024, $p); - return round ($o, 1) . $u[$p]; -} - -function -jirafeau_clean_rm_link ($link) + $content = '' + . strftime('%Y-%m-%d %H:%M', $timestamp) . ' (GMT)'; + return $content; +} + +function jirafeau_fatal_error($errorText, $cfg = array()) +{ + echo '
' . $errorText . '
' . $title. '
' . $description. '