Test Failed
Push — main ( 6ea9a3...5960f6 )
by Rafael
04:57
created

Functions::getThemes()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 16
rs 9.9
c 0
b 0
f 0
cc 4
nc 4
nop 0
1
<?php
2
3
/* Copyright (C) 2024       Rafael San José         <[email protected]>
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17
 * or see https://www.gnu.org/
18
 */
19
20
namespace Alxarafe\Lib;
21
22
abstract class Functions
23
{
24
    /**
25
     * Obtains the main url
26
     *
27
     * @return string
28
     */
29
    public static function getUrl()
30
    {
31
        $ssl = !empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on';
32
        $proto = strtolower($_SERVER['SERVER_PROTOCOL']);
33
        $proto = substr($proto, 0, strpos($proto, '/')) . ($ssl ? 's' : '');
34
        if (isset($_SERVER['HTTP_HOST'])) {
35
            $host = $_SERVER['HTTP_HOST'];
36
        } else {
37
            $port = $_SERVER['SERVER_PORT'];
38
            $port = ((!$ssl && $port == '80') || ($ssl && $port == '443')) ? '' : ':' . $port;
39
            $host = $_SERVER['SERVER_NAME'] . $port;
40
        }
41
42
        $script = $_SERVER['SCRIPT_NAME'];
43
44
        $script = substr($script, 0, strlen($script) - strlen('/index.php'));
45
        return $proto . '://' . $host . $script;
46
    }
47
48
    /**
49
     * This function is used to obtain the value of a POST variable, and if it does not exist
50
     * (for example, the first time the form is loaded), take a default value.
51
     *
52
     * @param $postVar
53
     * @param $defaultValue
54
     * @return mixed
55
     */
56
    public static function getIfIsset($postVar, $defaultValue)
57
    {
58
        $return = filter_input(INPUT_POST, $postVar);
59
        if ($return === null || $return === false) {
60
            return $defaultValue;
61
        }
62
        return $return;
63
    }
64
65
    /**
66
     * Defines the constant $name, if it is not already defined.
67
     *
68
     * @param string $name
69
     * @param        $value
70
     */
71
    public static function defineIfNotDefined(string $name, $value)
72
    {
73
        if (!defined($name)) {
74
            define($name, $value);
75
        }
76
    }
77
78
    /**
79
     * Convert an array of attributes to a string.
80
     *
81
     * @param array $attributes
82
     * @return string
83
     */
84
    public static function htmlAttributes(array $attributes): string
85
    {
86
        $_attributes = '';
87
        foreach ($attributes as $key => $value) {
88
            $_attributes .= $key . '="' . htmlspecialchars($value) . '" ';
89
        }
90
        return trim($_attributes);
91
    }
92
93
    public static function getThemes()
94
    {
95
        $result = [];
96
        $pattern = realpath(constant('BASE_PATH') . '/../vendor/rsanjoseo/alxarafe/Templates/theme');
97
        if ($pattern === false) {
98
            return $result;
99
        }
100
        $files = glob($pattern . '/*');
101
        foreach ($files as $file) {
102
            $theme = substr($file, 1 + strlen($pattern));
103
            if (in_array($theme, ['.', '..'])) {
104
                continue;
105
            }
106
            $result[$theme] = $theme;
107
        }
108
        return $result;
109
    }
110
}
111