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