Subversion Repositories ALCASAR

Rev

Go to most recent revision | Details | Last modification | View Log

Rev Author Line No. Line
2809 rexy 1
<?php
2
 
3
/*
4
 * Copyright (C) 2019 Alexander Marston (alexander.marston@gmail.com)
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
 */
19
 
20
// Get the largest value in an array
21
function getLargestValue($array) {
22
    return $max = array_reduce($array, function ($a, $b) {
23
        return $a > $b['total'] ? $a : $b['total'];
24
    });
25
}
26
 
27
function formatSize($bytes, $vnstatJsonVersion) {
28
    // json version 1 = convert from KiB
29
    // json version 2 = convert from bytes
30
    if ($vnstatJsonVersion == 1) {
31
        $bytes *= 1024;  // convert from kibibytes to bytes
32
    }
33
 
34
    return formatBytes($bytes);
35
}
36
 
37
function formatBytes($bytes, $decimals = 2) {
38
    $base = log(floatval($bytes), 1024);
39
    $suffixes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
40
 
41
    return round(pow(1024, $base - floor($base)), $decimals) .' '. $suffixes[floor($base)];
42
}
43
 
44
function formatBytesTo($bytes, $delimiter, $decimals = 2) {
45
    if ($bytes == 0) {
46
        return '0';
47
    }
48
 
49
    $k = 1024;
50
    $dm = $decimals < 0 ? 0 : $decimals;
51
    $sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
52
 
53
    $i = array_search($delimiter, $sizes);
54
 
55
    return number_format(($bytes / pow($k, $i)), $decimals);
56
}
57
 
58
function kibibytesToBytes($kibibytes, $vnstatJsonVersion) {
59
    if ($vnstatJsonVersion == 1) {
60
        return $kibibytes *= 1024;
61
    } else {
62
        return $kibibytes;
63
    }
64
}
65
 
66
function getLargestPrefix($kb)
67
{
68
    $units = ['TB', 'GB', 'MB', 'KB', 'B'];
69
    $scale = 1024 * 1024 * 1024 * 1024;
70
    $ui = 0;
71
 
72
    while ((($kb < $scale) && ($scale > 1))) {
73
        $ui++;
74
        $scale = $scale / 1024;
75
    }
76
 
77
    return $units[$ui];
78
}
79
 
80
function sortingFunction($item1, $item2) {
81
    if ($item1['time'] == $item2['time']) {
82
        return 0;
83
    } else {
84
        return $item1['time'] > $item2['time'] ? -1 : 1;
85
    }
86
};
87
 
88
?>