|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* PHPPgAdmin v6.0.0-beta.43 |
|
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
|
|
|
* @package PHPPgAdmin |
|
18
|
|
|
*/ |
|
19
|
|
|
trait HelperTrait |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* Halts the execution of the program. It's like calling exit() but using builtin Slim Exceptions. |
|
23
|
|
|
* |
|
24
|
|
|
* @param string $msg The message to show to the user |
|
25
|
|
|
* |
|
26
|
|
|
* @throws \Slim\Exception\SlimException (description) |
|
27
|
|
|
*/ |
|
28
|
|
|
public function halt($msg = 'An error has happened') |
|
29
|
|
|
{ |
|
30
|
|
|
$body = $this->container->responseobj->getBody(); |
|
31
|
|
|
$body->write($msg); |
|
32
|
|
|
|
|
33
|
|
|
throw new \Slim\Exception\SlimException($this->container->requestobj, $this->container->responseobj); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function addFlash($key, $content) |
|
37
|
|
|
{ |
|
38
|
|
|
$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2); |
|
39
|
|
|
|
|
40
|
|
|
$btarray0 = ([ |
|
41
|
|
|
'class2' => $backtrace[1]['class'], |
|
42
|
|
|
'type2' => $backtrace[1]['type'], |
|
43
|
|
|
'function2' => $backtrace[1]['function'], |
|
44
|
|
|
'spacer4' => ' ', |
|
45
|
|
|
'line2' => $backtrace[0]['line'], |
|
46
|
|
|
]); |
|
47
|
|
|
|
|
48
|
|
|
$tag = implode('', $btarray0); |
|
49
|
|
|
|
|
50
|
|
|
$this->container->flash->addMessage($tag . ' ' . $key, $content); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Receives N parameters and sends them to the console adding where was it called from. |
|
55
|
|
|
*/ |
|
56
|
|
|
public function prtrace() |
|
57
|
|
|
{ |
|
58
|
|
|
$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2); |
|
59
|
|
|
|
|
60
|
|
|
$btarray0 = ([ |
|
61
|
|
|
/*'class0' => $backtrace[3]['class'], |
|
62
|
|
|
'type0' => $backtrace[3]['type'], |
|
63
|
|
|
'function0' => $backtrace[3]['function'], |
|
64
|
|
|
'spacer0' => ' ', |
|
65
|
|
|
'line0' => $backtrace[2]['line'], |
|
66
|
|
|
|
|
67
|
|
|
'spacer1' => "\n", |
|
68
|
|
|
|
|
69
|
|
|
'class1' => $backtrace[2]['class'], |
|
70
|
|
|
'type1' => $backtrace[2]['type'], |
|
71
|
|
|
'function1' => $backtrace[2]['function'], |
|
72
|
|
|
'spacer2' => ' ', |
|
73
|
|
|
'line1' => $backtrace[1]['line'], |
|
74
|
|
|
|
|
75
|
|
|
'spacer3' => "\n",*/ |
|
76
|
|
|
|
|
77
|
|
|
'class2' => $backtrace[1]['class'], |
|
78
|
|
|
'type2' => $backtrace[1]['type'], |
|
79
|
|
|
'function2' => $backtrace[1]['function'], |
|
80
|
|
|
'spacer4' => ' ', |
|
81
|
|
|
'line2' => $backtrace[0]['line'], |
|
82
|
|
|
]); |
|
83
|
|
|
|
|
84
|
|
|
$tag = implode('', $btarray0); |
|
85
|
|
|
|
|
86
|
|
|
\PC::debug(func_get_args(), $tag); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Converts an ADORecordSet to an array |
|
91
|
|
|
* |
|
92
|
|
|
* @param \PHPPgAdmin\ADORecordSet $set The set |
|
93
|
|
|
* @param string $field optionally the field to query for |
|
94
|
|
|
* |
|
95
|
|
|
* @return array the parsed array |
|
96
|
|
|
*/ |
|
97
|
|
|
public static function recordSetToArray($set, $field = '') |
|
98
|
|
|
{ |
|
99
|
|
|
$result = []; |
|
100
|
|
|
|
|
101
|
|
|
if ($set->recordCount() <= 0) { |
|
102
|
|
|
return $result; |
|
103
|
|
|
} |
|
104
|
|
|
while (!$set->EOF) { |
|
105
|
|
|
$result[] = $field ? $set->fields[$field] : $set; |
|
106
|
|
|
$set->moveNext(); |
|
107
|
|
|
} |
|
108
|
|
|
return $result; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* checks if a variable is defined, in which case assign its value to $var1 |
|
|
|
|
|
|
113
|
|
|
* If it isn't and $set is true, assign the default value. Otherwise don't |
|
114
|
|
|
* assign anything to $var1 |
|
115
|
|
|
* |
|
116
|
|
|
* @param mixed $var1 The variable to manipulate if $set if true |
|
117
|
|
|
* @param mixed $var2 The value to assign to $var1 if it's defined |
|
118
|
|
|
* @param mixed $default The default value to set, it $set is true |
|
119
|
|
|
* @param bool $set True to set the default value if $var2 isn't defined |
|
120
|
|
|
* |
|
121
|
|
|
* @return mixed the value of $var2 is $var2 is set, or $default otherwise |
|
122
|
|
|
*/ |
|
123
|
|
|
public function setIfIsset(&$var1, $var2, $default = null, $set = true) |
|
|
|
|
|
|
124
|
|
|
{ |
|
125
|
|
|
if (isset($var2)) { |
|
126
|
|
|
$var1 = $var2; |
|
127
|
|
|
return $var1; |
|
128
|
|
|
} else if ($set = true) { |
|
|
|
|
|
|
129
|
|
|
$var1 = $default; |
|
130
|
|
|
return $var1; |
|
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 string|int $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]) && $set === true) { |
|
150
|
|
|
$array[$key] = $default; |
|
151
|
|
|
} |
|
152
|
|
|
return $array; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Receives N parameters and sends them to the console adding where was it |
|
157
|
|
|
* called from. |
|
158
|
|
|
*/ |
|
159
|
|
|
public static function statictrace() |
|
160
|
|
|
{ |
|
161
|
|
|
$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2); |
|
162
|
|
|
|
|
163
|
|
|
$btarray0 = [ |
|
164
|
|
|
'class' => $backtrace[1]['class'], |
|
165
|
|
|
'type' => $backtrace[1]['type'], |
|
166
|
|
|
'function' => $backtrace[1]['function'], |
|
167
|
|
|
'spacer' => ' ', |
|
168
|
|
|
'line' => $backtrace[0]['line'], |
|
169
|
|
|
]; |
|
170
|
|
|
|
|
171
|
|
|
$tag = implode('', $btarray0); |
|
172
|
|
|
|
|
173
|
|
|
\PC::debug(func_get_args(), $tag); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Returns a string with html <br> variant replaced with a new line. |
|
178
|
|
|
* |
|
179
|
|
|
* @param string $msg message to parse (<br> separated) |
|
180
|
|
|
* |
|
181
|
|
|
* @return string parsed message (linebreak separated) |
|
182
|
|
|
*/ |
|
183
|
|
|
public static function br2ln($msg) |
|
184
|
|
|
{ |
|
185
|
|
|
return str_replace(['<br>', '<br/>', '<br />'], "\n", $msg); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
public function dump() |
|
189
|
|
|
{ |
|
190
|
|
|
call_user_func_array('\Kint::dump', func_get_args()); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
public function dumpAndDie() |
|
194
|
|
|
{ |
|
195
|
|
|
$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1); |
|
196
|
|
|
|
|
197
|
|
|
$folder = dirname(dirname(__DIR__)); |
|
198
|
|
|
$file = str_replace($folder, '', $backtrace[0]['file']); |
|
199
|
|
|
$line = $backtrace[0]['line']; |
|
200
|
|
|
|
|
201
|
|
|
call_user_func_array('\Kint::dump', func_get_args()); |
|
202
|
|
|
$this->halt('stopped by user at ' . $file . ' line ' . $line); |
|
203
|
|
|
} |
|
204
|
|
|
} |
|
205
|
|
|
|