|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Lagdo\DbAdmin\Admin\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use Lagdo\DbAdmin\Driver\Entity\TableFieldEntity; |
|
6
|
|
|
|
|
7
|
|
|
use function file_get_contents; |
|
8
|
|
|
use function substr; |
|
9
|
|
|
use function function_exists; |
|
10
|
|
|
use function iconv; |
|
11
|
|
|
use function json_decode; |
|
12
|
|
|
use function is_array; |
|
13
|
|
|
use function preg_match; |
|
14
|
|
|
use function is_string; |
|
15
|
|
|
use function array_sum; |
|
16
|
|
|
|
|
17
|
|
|
trait QueryInputTrait |
|
18
|
|
|
{ |
|
19
|
|
|
use InputFieldTrait; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Get INI boolean value |
|
23
|
|
|
* |
|
24
|
|
|
* @param string $ini |
|
25
|
|
|
* |
|
26
|
|
|
* @return bool |
|
27
|
|
|
*/ |
|
28
|
|
|
abstract public function iniBool(string $ini): bool; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param array $file |
|
32
|
|
|
* @param string $key |
|
33
|
|
|
* @param bool $decompress |
|
34
|
|
|
* |
|
35
|
|
|
* @return string |
|
36
|
|
|
*/ |
|
37
|
|
|
private function readFileContent(array $file, string $key, bool $decompress): string |
|
38
|
|
|
{ |
|
39
|
|
|
$name = $file['name'][$key]; |
|
40
|
|
|
$tmpName = $file['tmp_name'][$key]; |
|
41
|
|
|
$content = file_get_contents($decompress && preg_match('~\.gz$~', $name) ? |
|
42
|
|
|
"compress.zlib://$tmpName" : $tmpName); //! may not be reachable because of open_basedir |
|
43
|
|
|
if (!$decompress) { |
|
44
|
|
|
return $content; |
|
45
|
|
|
} |
|
46
|
|
|
$start = substr($content, 0, 3); |
|
47
|
|
|
if (function_exists('iconv') && preg_match("~^\xFE\xFF|^\xFF\xFE~", $start, $regs)) { |
|
48
|
|
|
// not ternary operator to save memory |
|
49
|
|
|
return iconv('utf-16', 'utf-8', $content) . "\n\n"; |
|
50
|
|
|
} |
|
51
|
|
|
if ($start == "\xEF\xBB\xBF") { // UTF-8 BOM |
|
52
|
|
|
return substr($content, 3) . "\n\n"; |
|
53
|
|
|
} |
|
54
|
|
|
return $content; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Get file contents from $_FILES |
|
59
|
|
|
* |
|
60
|
|
|
* @param string $key |
|
61
|
|
|
* @param bool $decompress |
|
62
|
|
|
* |
|
63
|
|
|
* @return string|null |
|
64
|
|
|
*/ |
|
65
|
|
|
private function getFileContents(string $key, bool $decompress = false) |
|
66
|
|
|
{ |
|
67
|
|
|
$file = $_FILES[$key]; |
|
68
|
|
|
if (!$file) { |
|
69
|
|
|
return null; |
|
70
|
|
|
} |
|
71
|
|
|
foreach ($file as $key => $val) { |
|
72
|
|
|
$file[$key] = (array) $val; |
|
73
|
|
|
} |
|
74
|
|
|
$queries = ''; |
|
75
|
|
|
foreach ($file['error'] as $key => $error) { |
|
76
|
|
|
if (($error)) { |
|
77
|
|
|
return $error; |
|
78
|
|
|
} |
|
79
|
|
|
$queries .= $this->readFileContent($file, $key, $decompress); |
|
80
|
|
|
} |
|
81
|
|
|
//! Support SQL files not ending with semicolon |
|
82
|
|
|
return $queries; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @param mixed $value |
|
87
|
|
|
* |
|
88
|
|
|
* @return false|int|string |
|
89
|
|
|
*/ |
|
90
|
|
|
private function getEnumFieldValue($value) |
|
91
|
|
|
{ |
|
92
|
|
|
if ($value === -1) { |
|
93
|
|
|
return false; |
|
94
|
|
|
} |
|
95
|
|
|
if ($value === '') { |
|
96
|
|
|
return 'NULL'; |
|
97
|
|
|
} |
|
98
|
|
|
return +$value; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @param TableFieldEntity $field |
|
103
|
|
|
* |
|
104
|
|
|
* @return string|false |
|
105
|
|
|
*/ |
|
106
|
|
|
private function getOrigFieldValue(TableFieldEntity $field) |
|
107
|
|
|
{ |
|
108
|
|
|
if (preg_match('~^CURRENT_TIMESTAMP~i', $field->onUpdate) === false) { |
|
109
|
|
|
return false; |
|
110
|
|
|
} |
|
111
|
|
|
return $this->driver->escapeId($field->name); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @param mixed $value |
|
116
|
|
|
* |
|
117
|
|
|
* @return array|false |
|
118
|
|
|
*/ |
|
119
|
|
|
private function getJsonFieldValue($value) |
|
120
|
|
|
{ |
|
121
|
|
|
if (!is_array($value = json_decode($value, true))) { |
|
122
|
|
|
return false; //! Report errors |
|
123
|
|
|
} |
|
124
|
|
|
return $value; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @param TableFieldEntity $field |
|
129
|
|
|
* |
|
130
|
|
|
* @return string|false |
|
131
|
|
|
*/ |
|
132
|
|
|
private function getBinaryFieldValue(TableFieldEntity $field) |
|
133
|
|
|
{ |
|
134
|
|
|
if (!$this->iniBool('file_uploads')) { |
|
135
|
|
|
return false; |
|
136
|
|
|
} |
|
137
|
|
|
$idf = $this->driver->bracketEscape($field->name); |
|
138
|
|
|
$file = $this->getFileContents("fields-$idf"); |
|
139
|
|
|
if (!is_string($file)) { |
|
140
|
|
|
return false; //! report errors |
|
141
|
|
|
} |
|
142
|
|
|
return $this->driver->quoteBinary($file); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Process edit input field |
|
147
|
|
|
* |
|
148
|
|
|
* @param TableFieldEntity $field |
|
149
|
|
|
* @param array $inputs The user inputs |
|
150
|
|
|
* |
|
151
|
|
|
* @return array|false|float|int|string|null |
|
152
|
|
|
*/ |
|
153
|
|
|
public function processInput(TableFieldEntity $field, array $inputs) |
|
154
|
|
|
{ |
|
155
|
|
|
$idf = $this->driver->bracketEscape($field->name); |
|
156
|
|
|
$function = $inputs['function'][$idf] ?? ''; |
|
157
|
|
|
$value = $inputs['fields'][$idf]; |
|
158
|
|
|
if ($field->autoIncrement && $value === '') { |
|
159
|
|
|
return null; |
|
160
|
|
|
} |
|
161
|
|
|
if ($function === 'NULL') { |
|
162
|
|
|
return 'NULL'; |
|
163
|
|
|
} |
|
164
|
|
|
if ($field->type === 'enum') { |
|
165
|
|
|
return $this->getEnumFieldValue($value); |
|
166
|
|
|
} |
|
167
|
|
|
if ($function === 'orig') { |
|
168
|
|
|
return $this->getOrigFieldValue($field); |
|
169
|
|
|
} |
|
170
|
|
|
if ($field->type === 'set') { |
|
171
|
|
|
return array_sum((array) $value); |
|
172
|
|
|
} |
|
173
|
|
|
if ($function == 'json') { |
|
174
|
|
|
return $this->getJsonFieldValue($value); |
|
175
|
|
|
} |
|
176
|
|
|
if (preg_match('~blob|bytea|raw|file~', $field->type)) { |
|
177
|
|
|
return $this->getBinaryFieldValue($field); |
|
178
|
|
|
} |
|
179
|
|
|
return $this->getUnconvertedFieldValue($field, $value, $function); |
|
180
|
|
|
} |
|
181
|
|
|
} |
|
182
|
|
|
|