aboutsummaryrefslogtreecommitdiff
path: root/ratatoeskr/sys/utils.php
blob: 8a848ebc6a714194e17aa675650c76f842a33a07 (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
<?php
/*
 * File: ratatoeskr/sys/utils.php
 *
 * Various useful helper functions.
 *
 * License:
 * This file is part of Ratatöskr.
 * Ratatöskr is licensed unter the MIT / X11 License.
 * See "ratatoeskr/licenses/ratatoeskr" for more information.
 */

/*
 * Function: array_repeat
 *
 * Parameters:
 *
 *  $val -
 *  $n   -
 *
 * Returns:
 *
 *  An array with $val $n-times repeated.
 */

use r7r\cms\sys\Esc;

function array_repeat($val, $n)
{
    $rv = [];
    for ($i = 0; $i < $n; ++$i) {
        array_push($rv, $val);
    }
    return $rv;
}

/*
 * Function: intcmp
 * Compare integers (equavilent to strcmp)
 */
function intcmp($a, $b)
{
    return ($a == $b) ? 0 : (($a < $b) ? -1 : 1);
}

/**
 * Escape HTML (shorter than htmlspecialchars)
 *
 * @param mixed $text Input text
 * @return string HTML
 * @deprecated Use {@see Esc::esc()} instead.
 */
function htmlesc($text): string
{
    return Esc::esc($text);
}

/*
 * Function: delete_directory
 * Delete a directory and all of its content.
 */
function delete_directory($dir)
{
    $dir_content = scandir($dir);
    foreach ($dir_content as $f) {
        if (($f == "..") or ($f == ".")) {
            continue;
        }

        $f = "$dir/$f";

        if (is_dir($f)) {
            delete_directory($f);
        } else {
            unlink($f);
        }
    }
    rmdir($dir);
}

/*
 * Constant: SITE_BASE_PATH
 * The Base path of this ratatoeskr site.
 */
define("SITE_BASE_PATH", dirname(dirname(dirname(__FILE__))));