1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* PHPPgAdmin v6.0.0-RC9-3-gd93ec300 |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace PHPPgAdmin\Traits; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @file |
11
|
|
|
* A trait with helpers methods to debug, halt the app and format text to html |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* A trait with helpers methods to debug, halt the app and format text to html. |
16
|
|
|
*/ |
17
|
|
|
trait HelperTrait |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* static reference to subfolder in which the app is running. |
21
|
|
|
* |
22
|
|
|
* @var null|string |
23
|
|
|
*/ |
24
|
|
|
public static $subFolder = null; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Gets the subfolder. |
28
|
|
|
* |
29
|
|
|
* @param string $path The path |
30
|
|
|
* |
31
|
|
|
* @return string the subfolder |
32
|
|
|
*/ |
33
|
|
|
public function getSubfolder(string $path = ''): string |
34
|
|
|
{ |
35
|
|
|
if (null === self::$subFolder) { |
36
|
|
|
self::$subFolder = $this->container->subfolder; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
return \implode(\DIRECTORY_SEPARATOR, [self::$subFolder, $path]); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Halts the execution of the program. It's like calling exit() but using builtin Slim Exceptions. |
44
|
|
|
* |
45
|
|
|
* @param string $msg The message to show to the user |
46
|
|
|
* |
47
|
|
|
* @throws \Slim\Exception\SlimException (description) |
48
|
|
|
*/ |
49
|
|
|
public function halt($msg = 'An error has happened'): void |
50
|
|
|
{ |
51
|
|
|
$body = $this->container->responseobj->getBody(); |
52
|
|
|
$body->write($msg); |
53
|
|
|
|
54
|
|
|
throw new \Slim\Exception\SlimException($this->container->requestobj, $this->container->responseobj); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public static function getBackTrace($offset = 0) |
58
|
|
|
{ |
59
|
|
|
$i0 = $offset; |
60
|
|
|
$i1 = $offset + 1; |
61
|
|
|
$backtrace = \debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS, $offset + 3); |
62
|
|
|
|
63
|
|
|
return [ |
64
|
|
|
'class' => 'Closure' === $backtrace[$i1]['class'] ? |
65
|
|
|
$backtrace[$i0]['file'] : |
66
|
|
|
$backtrace[$i1]['class'], |
67
|
|
|
|
68
|
|
|
'type' => $backtrace[$i1]['type'], |
69
|
|
|
|
70
|
|
|
'function' => '{closure}' === $backtrace[$i1]['function'] |
71
|
|
|
? $backtrace[$i0]['function'] : |
72
|
|
|
$backtrace[$i1]['function'], |
73
|
|
|
|
74
|
|
|
'spacer4' => ' ', |
75
|
|
|
'line' => $backtrace[$i0]['line'], |
76
|
|
|
]; |
77
|
|
|
//dump($backtrace); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Converts an ADORecordSet to an array. |
82
|
|
|
* |
83
|
|
|
* @param \ADORecordSet $set The set |
84
|
|
|
* @param string $field optionally the field to query for |
85
|
|
|
* |
86
|
|
|
* @return array the parsed array |
87
|
|
|
*/ |
88
|
|
|
public static function recordSetToArray($set, $field = '') |
89
|
|
|
{ |
90
|
|
|
$result = []; |
91
|
|
|
|
92
|
|
|
if (0 >= $set->recordCount()) { |
93
|
|
|
return $result; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
while (!$set->EOF) { |
97
|
|
|
$result[] = $field ? $set->fields[$field] : $set; |
98
|
|
|
$set->moveNext(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $result; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Checks if a variable is defined, in which case assign its value to $var1 |
106
|
|
|
* If it isn't and $set is true, assign the default value. Otherwise don't |
107
|
|
|
* assign anything to $var1. |
108
|
|
|
* |
109
|
|
|
* @param mixed $var1 The variable to manipulate if $set if true |
110
|
|
|
* @param mixed $var2 The value to assign to $var1 if it's defined |
111
|
|
|
* @param mixed $default The default value to set, it $set is true |
112
|
|
|
* @param bool $set True to set the default value if $var2 isn't defined |
113
|
|
|
* |
114
|
|
|
* @return mixed the value of $var2 is $var2 is set, or $default otherwise |
115
|
|
|
*/ |
116
|
|
|
public function setIfIsset(&$var1, $var2, $default = null, $set = true) |
117
|
|
|
{ |
118
|
|
|
if (isset($var2)) { |
119
|
|
|
$var1 = $var2; |
120
|
|
|
|
121
|
|
|
return $var1; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
if (true === $set) { |
125
|
|
|
$var1 = $default; |
126
|
|
|
|
127
|
|
|
return $var1; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return $default; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Checks if the $key of an $array is set. If it isn't, optionally set it to |
135
|
|
|
* the default parameter. |
136
|
|
|
* |
137
|
|
|
* @param array $array The array to check |
138
|
|
|
* @param int|string $key The key to check |
139
|
|
|
* @param mixed $default The default value to set, it $set is true |
140
|
|
|
* @param bool $set True to set the default value if $key isn't |
141
|
|
|
* set |
142
|
|
|
* |
143
|
|
|
* @return array the original array |
144
|
|
|
*/ |
145
|
|
|
public function coalesceArr(&$array, $key, $default = null, $set = true) |
146
|
|
|
{ |
147
|
|
|
if (!isset($array[$key]) && true === $set) { |
148
|
|
|
$array[$key] = $default; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return $array; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public static function formatSizeUnits($bytes, $lang) |
155
|
|
|
{ |
156
|
|
|
if (-1 === $bytes) { |
157
|
|
|
$bytes = $lang['strnoaccess']; |
158
|
|
|
} elseif (1099511627776 <= $bytes) { |
159
|
|
|
$bytes = \sprintf('%s %s', \number_format($bytes / 1099511627776, 0), $lang['strtb']); |
160
|
|
|
} elseif (1073741824 <= $bytes) { |
161
|
|
|
$bytes = \sprintf('%s %s', \number_format($bytes / 1073741824, 0), $lang['strgb']); |
162
|
|
|
} elseif (1048576 <= $bytes) { |
163
|
|
|
$bytes = \sprintf('%s %s', \number_format($bytes / 1048576, 0), $lang['strmb']); |
164
|
|
|
} elseif (1024 <= $bytes) { |
165
|
|
|
$bytes = \sprintf('%s %s', \number_format($bytes / 1024, 0), $lang['strkb']); |
166
|
|
|
} else { |
167
|
|
|
$bytes = \sprintf('%s %s', $bytes, $lang['strbytes']); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
return $bytes; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Returns a string with html <br> variant replaced with a new line. |
175
|
|
|
* |
176
|
|
|
* @param string $msg message to parse (<br> separated) |
177
|
|
|
* |
178
|
|
|
* @return string parsed message (linebreak separated) |
179
|
|
|
*/ |
180
|
|
|
public static function br2ln($msg) |
181
|
|
|
{ |
182
|
|
|
return \str_replace(['<br>', '<br/>', '<br />'], \PHP_EOL, $msg); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Receives N parameters and sends them to the console adding where was it called from. |
187
|
|
|
* |
188
|
|
|
* @param mixed ...$args |
189
|
|
|
*/ |
190
|
|
|
public function prtrace(...$args): void |
191
|
|
|
{ |
192
|
|
|
self::staticTrace(...$args); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Just a proxy for prtrace. |
197
|
|
|
* |
198
|
|
|
* @param mixed ...$args The arguments |
199
|
|
|
*/ |
200
|
|
|
public function dump(...$args): void |
201
|
|
|
{ |
202
|
|
|
self::staticTrace(...$args); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/**Claveunica.,219 |
|
|
|
|
206
|
|
|
* Dumps and die. |
207
|
|
|
* |
208
|
|
|
* @param mixed ...$args The arguments |
209
|
|
|
*/ |
210
|
|
|
public function dumpAndDie(...$args): void |
211
|
|
|
{ |
212
|
|
|
self::staticTrace(...$args); |
213
|
|
|
exit(); |
|
|
|
|
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Receives N parameters and sends them to the console adding where was it |
218
|
|
|
* called from. |
219
|
|
|
* |
220
|
|
|
* @param mixed $variablesToDump |
|
|
|
|
221
|
|
|
*/ |
222
|
|
|
private static function staticTrace( |
223
|
|
|
...$variablesToDump |
224
|
|
|
): void { |
225
|
|
|
if (!$variablesToDump) { |
|
|
|
|
226
|
|
|
$variablesToDump = \func_get_args(); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
$calledFrom = \str_replace( |
230
|
|
|
dirname( |
231
|
|
|
dirname(__DIR__) |
232
|
|
|
), |
233
|
|
|
'', |
234
|
|
|
implode( |
235
|
|
|
'', |
236
|
|
|
self::getBackTrace(2) |
237
|
|
|
) |
238
|
|
|
); |
239
|
|
|
|
240
|
|
|
if (\function_exists('dump')) { |
241
|
|
|
dump([ |
|
|
|
|
242
|
|
|
'args' => $variablesToDump, |
243
|
|
|
'from' => $calledFrom, |
244
|
|
|
]); |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
} |
248
|
|
|
|