Passed
Push — main ( 7a7778...6a2887 )
by Thierry
12:57 queued 05:00
created

UtilTrait::iniBool()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 5
rs 10
1
<?php
2
3
namespace Lagdo\DbAdmin\Db\Traits;
4
5
use Lagdo\DbAdmin\Driver\Entity\TableFieldEntity;
6
7
use function is_string;
8
use function preg_match;
9
use function ini_get;
10
use function strtolower;
11
use function substr;
12
use function intval;
13
use function str_replace;
14
use function preg_quote;
15
use function str_repeat;
16
use function strtr;
17
18
trait UtilTrait
19
{
20
    /**
21
     * Escape for HTML
22
     *
23
     * @param string|null $string
24
     *
25
     * @return string
26
     */
27
    abstract public function html($string): string;
28
29
    /**
30
     * Get escaped error message
31
     *
32
     * @return string
33
     */
34
    public function error(): string
35
    {
36
        return $this->html($this->driver->error());
37
    }
38
39
    /**
40
     * Check if the string is e-mail address
41
     *
42
     * @param mixed $email
43
     *
44
     * @return bool
45
     */
46
    public function isMail($email): bool
47
    {
48
        if (!is_string($email)) {
49
            return false;
50
        }
51
        $atom = '[-a-z0-9!#$%&\'*+/=?^_`{|}~]'; // characters of local-name
52
        $domain = '[a-z0-9]([-a-z0-9]{0,61}[a-z0-9])'; // one domain component
53
        $pattern = "$atom+(\\.$atom+)*@($domain?\\.)+$domain";
54
        return preg_match("(^$pattern(,\\s*$pattern)*\$)i", $email) > 0;
55
    }
56
57
    /**
58
     * Check if the string is URL address
59
     *
60
     * @param mixed $string
61
     *
62
     * @return bool
63
     */
64
    public function isUrl($string): bool
65
    {
66
        $domain = '[a-z0-9]([-a-z0-9]{0,61}[a-z0-9])'; // one domain component //! IDN
67
        //! restrict path, query and fragment characters
68
        return preg_match("~^(https?)://($domain?\\.)+$domain(:\\d+)?(/.*)?(\\?.*)?(#.*)?\$~i", $string) > 0;
69
    }
70
71
    /**
72
     * Check if field should be shortened
73
     *
74
     * @param TableFieldEntity $field
75
     *
76
     * @return bool
77
     */
78
    public function isShortable(TableFieldEntity $field): bool
79
    {
80
        return preg_match('~char|text|json|lob|geometry|point|linestring|polygon|string|bytea~', $field->type) > 0;
81
    }
82
83
    /**
84
     * Get INI boolean value
85
     *
86
     * @param string $ini
87
     *
88
     * @return bool
89
     */
90
    public function iniBool(string $ini): bool
91
    {
92
        $value = ini_get($ini);
93
        // boolean values set by php_value are strings
94
        return (preg_match('~^(on|true|yes)$~i', $value) || (int) $value);
95
    }
96
97
    /**
98
     * Get INI bytes value
99
     *
100
     * @param string
101
     *
102
     * @return int
103
     */
104
    public function iniBytes(string $ini): int
105
    {
106
        $value = ini_get($ini);
107
        $unit = strtolower(substr($value, -1)); // Get the last char
108
        $ival = intval(substr($value, 0, -1)); // Remove the last char
109
        switch ($unit) {
110
            case 'g': $value = $ival * 1024 * 1024 * 1024; break;
111
            case 'm': $value = $ival * 1024 * 1024; break;
112
            case 'k': $value = $ival * 1024; break;
113
        }
114
        return intval($value);
115
    }
116
117
    /**
118
     * Escape column key used in where()
119
     *
120
     * @param string
121
     *
122
     * @return string
123
     */
124
    public function escapeKey(string $key): string
125
    {
126
        if (preg_match('(^([\w(]+)(' . str_replace('_', '.*',
127
                preg_quote($this->driver->escapeId('_'))) . ')([ \w)]+)$)', $key, $match)) {
128
            //! columns looking like functions
129
            return $match[1] . $this->driver->escapeId($this->driver->unescapeId($match[2])) . $match[3]; //! SQL injection
130
        }
131
        return $this->driver->escapeId($key);
132
    }
133
134
    /**
135
     * Escape or unescape string to use inside form []
136
     *
137
     * @param string $idf
138
     * @param bool $back
139
     *
140
     * @return string
141
     */
142
    public function bracketEscape(string $idf, bool $back = false): string
143
    {
144
        // escape brackets inside name='x[]'
145
        static $trans = [':' => ':1', ']' => ':2', '[' => ':3', '"' => ':4'];
146
        return strtr($idf, ($back ? array_flip($trans) : $trans));
147
    }
148
}
149