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