-
-
Notifications
You must be signed in to change notification settings - Fork 668
/
Copy pathfunctions.inc.php
106 lines (80 loc) · 2.83 KB
/
functions.inc.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
function format_html($str) {
global $server;
if (isset($server['charset']) && $server['charset']) {
$res = mb_convert_encoding($str, 'utf-8', $server['charset']);
} else {
$res = $str;
}
$res = htmlentities($res, defined('ENT_SUBSTITUTE') ? (ENT_QUOTES | ENT_SUBSTITUTE) : ENT_QUOTES, 'utf-8');
return ($res || !$str) ? $res : '(' . strlen($str) . ' bytes)';
}
function input_convert($str) {
global $server;
if (isset($server['charset']) && $server['charset']) {
return mb_convert_encoding($str, $server['charset'], 'utf-8');
} else {
return $str;
}
}
function format_time($time) {
$minute = 60;
$hour = $minute * 60;
$day = $hour * 24;
$when = $time;
if ($when > $day) {
$tmpday = floor($when / $day);
$tmphour = floor(($when / $hour) - (24*$tmpday));
$tmpminute = floor(($when / $minute) - (24*60*$tmpday) - ($tmphour * 60));
$tmpsec = floor($when - (24*60*60*$tmpday) - ($tmphour * 60 * 60) - ($tmpminute * 60));
return sprintf("%d day%s %d hour%s %d min%s %d sec%s",$tmpday,($tmpday != 1) ? 's' : '',$tmphour,($tmphour != 1) ? 's' : '',$tmpminute,($tmpminute != 1) ? 's' : '',$tmpsec,($tmpsec != 1) ? 's' : '');
} else if ($when > $hour) {
$tmphour = floor($when / $hour);
$tmpminute = floor(($when / $minute) - ($tmphour * 60));
$tmpsec = floor($when - ($tmphour * 60 * 60) - ($tmpminute * 60));
return sprintf("%d hour%s %d min%s %d sec%s",$tmphour,($tmphour != 1) ? 's' : '',$tmpminute,($tmpminute != 1) ? 's' : '',$tmpsec,($tmpsec != 1) ? 's' : '');
} else if ($when > $minute) {
$tmpminute = floor($when / $minute);
$tmpsec = floor($when - ($tmpminute * 60));
return sprintf("%d min%s %d sec%s",$tmpminute,($tmpminute != 1) ? 's' : '',$tmpsec,($tmpsec != 1) ? 's' : '');
} else {
return sprintf("%d sec%s",$when,($when != 1) ? 's' : '');
}
}
function format_size($size) {
$sizes = array('B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
if ($size == 0) {
return '0 B';
} else {
return round($size / pow(1024, ($i = floor(log($size, 1024)))), 1).' '.$sizes[$i];
}
}
function format_ttl($seconds) {
if ($seconds > 60) {
return sprintf('%d (%s)', $seconds, format_time($seconds));
} else {
return $seconds;
}
}
function str_rand($length) {
$r = '';
for (; $length > 0; --$length) {
$r .= chr(rand(32, 126)); // 32 - 126 is the printable ascii range
}
return $r;
}
function encodeOrDecode($action, $key, $data) {
global $server;
if (isset($_GET['raw']) || !isset($server['serialization'])) {
return $data;
}
foreach ($server['serialization'] as $pattern => $closures) {
if (fnmatch($pattern, $key)) {
return $closures[$action]($data);
}
}
return $data;
}
function getRelativePath($base) {
return substr($_SERVER['REQUEST_URI'], strpos($_SERVER['REQUEST_URI'], $base));
}