-
Notifications
You must be signed in to change notification settings - Fork 2
/
common.php
128 lines (110 loc) · 3.14 KB
/
common.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
// functions that are common to multiple viewer modes
//XDDDDD
function bytesToGigabytes($bytes)
{
// 1073741824 = 1024 * 1024 * 1024
return $bytes / 1073741824;
}
function bytesToMegabytes($bytes)
{
// 1073741824 = 1024 * 1024
return $bytes / 1048576;
}
function getDriveUsed($driveinput)
{
$driveused = 0;
foreach ($driveinput['Partitions'] as $partition) {
$driveused += $partition['PartitionCapacity'] - $partition['PartitionFree'];
}
return $driveused;
}
function getDriveFree($driveinput)
{
$drivefree = $driveinput['DiskCapacity'] - getDriveUsed($driveinput);
return $drivefree;
}
function getDriveCapacity($driveinput)
{
$partitioncap = 0;
foreach ($driveinput['Partitions'] as $partition) {
$partitioncap += $partition['PartitionCapacity'];
}
return $partitioncap;
}
/**
* This is to prevent TypeErrors on malformed jsons where an array is null
*/
function safe_count($arr): int {
if (is_countable($arr)) {
return count($arr);
} else {
$bt = debug_backtrace();
trigger_error("safe_count called on null in {$bt['file']} on line {$bt['line']}", E_USER_NOTICE);
return 0;
}
}
function safe_implode(string $separator, $arr): string {
if (is_array($arr)) {
return implode($separator, $arr);
} else {
$bt = debug_backtrace()[0];
trigger_error("safe_implode called on null in {$bt['file']} on line {$bt['line']}", E_USER_NOTICE);
return '';
}
}
function timeConvert($time)
{
$timeString = "";
$days = floor($time / (60 * 60 * 24));
$hours = floor(($time % (60 * 60 * 24)) / (60 * 60));
$minutes = floor(($time % (60 * 60)) / 60);
$seconds = $time % 60;
// Initialize the string with the number of days
if ($days) {
$timeString = '<span';
if ($days > 3) $timeString .= ' style="color:#BF616A;"';
$timeString .= '>' . $days . ' day';
if ($days != 1) {
$timeString .= 's';
}
$timeString .= ', ';
}
// Add the number of hours to the string
if ($hours) {
$timeString .= $hours . ' hour';
if ($hours != 1) {
$timeString .= 's';
}
$timeString .= ', ';
}
// Add the number of minutes to the string
if ($minutes) {
$timeString .= $minutes . ' minute';
if ($minutes != 1) {
$timeString .= 's';
}
$timeString .= ', ';
}
// Add the number of seconds to the string A3BE8C
if ($seconds) {
if ($days || $hours || $minutes) $timeString .= 'and ';
$timeString .= $seconds . ' second';
if ($seconds != 1) {
$timeString .= 's</span>';
}
}
return $timeString;
}
// https://maxchadwick.xyz/blog/stripping-a-query-parameter-from-a-url-in-php
function http_strip_query_param($url, $param)
{
$pieces = parse_url($url);
$query = [];
if ($pieces['query']) {
parse_str($pieces['query'], $query);
unset($query[$param]);
$pieces['query'] = http_build_query($query);
}
return $pieces['path'] . ($pieces['query'] ? '?' : '') . $pieces['query'];
}