aboutsummaryrefslogtreecommitdiffstats
path: root/lib/lib.php
blob: 1258bfa0f778b005ebfbd9925028f24205312214 (plain)
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
<?php
/**
 * Various imported functions, under: CC-By-SA, CC-By.
 * See each function comments for details.
*/

/**
 * Convert size in bytes in human readable format.
 *
 * Taken from http://stackoverflow.com/a/2510459/204910
 * licensed / CC-By-SA 3.0 (http://creativecommons.org/licenses/by-sa/3.0/),
 * (c) Mef and various contributors,
 *
 * itself sourced from http://php.net/manual/en/function.filesize.php
 * so licensed under CC-By 3.0 (http://creativecommons.org/licenses/by/3.0/),
 * (c) the PHP Documentation Group
 *
 * @param integer $bytes size in bytes
 * @param integer $precision
 *
 * @return string
*/
function formatBytes($bytes, $precision = 2) { 
    $units = array('B', 'KB', 'MB', 'GB', 'TB'); 

    $bytes = max($bytes, 0); 
    $pow = floor(($bytes ? log($bytes) : 0) / log(1024)); 
    $pow = min($pow, count($units) - 1); 

    // Uncomment one of the following alternatives
    // $bytes /= pow(1024, $pow);
    $bytes /= (1 << (10 * $pow)); 

    return round($bytes, $precision) . ' ' . $units[$pow]; 
}


/**
 * Properly dump $assoc_arr into a .ini file, with or without sections.
 *
 * Taken from http://php.net/manual/en/function.parse-ini-file.php
 * so licensed under CC-By 3.0 (http://creativecommons.org/licenses/by/3.0/),
 * (c) the PHP Documentation Group
 *
 * @param array    $assoc_arr data to dump
 * @param string   $path where to write the data
 * @param boolean  $has_sections
 *
 * @return boolean
*/
function write_ini_file($assoc_arr, $path, $has_sections = FALSE)
{
    $content = '';
    if ($has_sections) {
        foreach ($assoc_arr as $key => $elem) {
            $content .= "\n;\n[" . $key . "]\n";
            foreach ($elem as $key2 => $elem2) {
                if (is_array($elem2)) {
                    for ($i = 0; $i < count($elem2); $i++)
                        $content .= $key2 . '[] = "' . $elem2[$i] . "\"\n";
                }
                elseif ( $elem2 == '')
                    $content .= $key2 ." = \n";
                else
                    $content .= $key2 . ' = "' . $elem2 . "\"\n";
            }
        }
    } else {
        foreach ($assoc_arr as $key => $elem) {
            if (is_array($elem)) {
                for ($i = 0; $i < count($elem); $i++)
                    $content .= $key2."[] = \"".$elem[$i]."\"\n";
            }
            elseif ($elem == '')
                $content .= $key2." = \n";
            else
                $content .= $key2." = \"".$elem."\"\n";
        }
    }

    if (!$handle = fopen($path, 'w')) {
        return false;
    }
    if (!fwrite($handle, $content)) {
        return false;
    }
    fclose($handle);

    return true;
}