Total Complexity | 246 |
Total Lines | 2019 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Misc often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Misc, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
27 | class Misc |
||
28 | { |
||
29 | use \PHPPgAdmin\Traits\HelperTrait; |
||
30 | |||
31 | private $_connection; |
||
32 | private $_no_db_connection = false; |
||
33 | private $_reload_browser = false; |
||
34 | private $_data; |
||
35 | private $_database; |
||
36 | private $_server_id; |
||
37 | private $_server_info; |
||
38 | private $_error_msg = ''; |
||
39 | |||
40 | public $appLangFiles = []; |
||
41 | public $appName = ''; |
||
42 | public $appVersion = ''; |
||
43 | public $form = ''; |
||
44 | public $href = ''; |
||
45 | public $controller_name = 'Misc'; |
||
46 | public $lang = []; |
||
47 | |||
48 | protected $container; |
||
49 | |||
50 | /** |
||
51 | * @param \Slim\Container $container The container |
||
52 | */ |
||
53 | public function __construct(\Slim\Container $container) |
||
54 | { |
||
55 | $this->container = $container; |
||
56 | |||
57 | $this->lang = $container->get('lang'); |
||
58 | $this->conf = $container->get('conf'); |
||
59 | |||
60 | //$this->view = $container->get('view'); |
||
61 | $this->plugin_manager = $container->get('plugin_manager'); |
||
62 | $this->appLangFiles = $container->get('appLangFiles'); |
||
63 | |||
64 | $this->appName = $container->get('settings')['appName']; |
||
65 | $this->appVersion = $container->get('settings')['appVersion']; |
||
66 | $this->postgresqlMinVer = $container->get('settings')['postgresqlMinVer']; |
||
67 | $this->phpMinVer = $container->get('settings')['phpMinVer']; |
||
68 | |||
69 | $base_version = $container->get('settings')['base_version']; |
||
70 | |||
71 | //$this->prtrace($base_version); |
||
72 | |||
73 | // Check for config file version mismatch |
||
74 | if (!isset($this->conf['version']) || $base_version > $this->conf['version']) { |
||
75 | $container->get('utils')->addError($this->lang['strbadconfig']); |
||
76 | } |
||
77 | |||
78 | // Check database support is properly compiled in |
||
79 | if (!function_exists('pg_connect')) { |
||
80 | $container->get('utils')->addError($this->lang['strnotloaded']); |
||
81 | } |
||
82 | |||
83 | // Check the version of PHP |
||
84 | if (version_compare(PHP_VERSION, $this->phpMinVer, '<')) { |
||
85 | $container->get('utils')->addError(sprintf('Version of PHP not supported. Please upgrade to version %s or later.', $this->phpMinVer)); |
||
86 | } |
||
87 | //$this->dumpAndDie($this); |
||
88 | |||
89 | $this->getServerId(); |
||
90 | } |
||
91 | |||
92 | public function serverToSha() |
||
93 | { |
||
94 | $request_server = $this->container->requestobj->getParam('server'); |
||
95 | if ($request_server === null) { |
||
96 | return null; |
||
97 | } |
||
98 | $srv_array = explode(':', $request_server); |
||
99 | if (count($srv_array) === 3) { |
||
100 | return sha1($request_server); |
||
101 | } |
||
102 | |||
103 | return $request_server; |
||
104 | } |
||
105 | |||
106 | public function getServerId() |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * Sets the view instance property of this class. |
||
129 | * |
||
130 | * @param \Slim\Views\Twig $view view instance |
||
131 | * |
||
132 | * @return \PHPPgAdmin\Misc this class instance |
||
133 | */ |
||
134 | public function setView(\Slim\Views\Twig $view) |
||
135 | { |
||
136 | $this->view = $view; |
||
137 | |||
138 | return $this; |
||
139 | } |
||
140 | |||
141 | /** |
||
142 | * Adds or modifies a key in the $conf instance property of this class. |
||
143 | * |
||
144 | * @param string $key name of the key to set |
||
145 | * @param mixed $value value of the key to set |
||
146 | * |
||
147 | * @return \PHPPgAdmin\Misc this class instance |
||
148 | */ |
||
149 | public function setConf($key, $value) |
||
150 | { |
||
151 | $this->conf[$key] = $value; |
||
152 | |||
153 | return $this; |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * Gets the value of a config property, or the array of all config properties. |
||
158 | * |
||
159 | * @param null|string $key value of the key to be retrieved. If null, the full array is returnes |
||
160 | * |
||
161 | * @return null|array|string the whole $conf array, the value of $conf[key] or null if said key does not exist |
||
162 | */ |
||
163 | public function getConf($key = null) |
||
164 | { |
||
165 | if ($key === null) { |
||
166 | return $this->conf; |
||
167 | } |
||
168 | if (array_key_exists($key, $this->conf)) { |
||
169 | return $this->conf[$key]; |
||
170 | } |
||
171 | |||
172 | return null; |
||
173 | } |
||
174 | |||
175 | /** |
||
176 | * Displays link to the context help. |
||
177 | * |
||
178 | * @param string $str the string that the context help is related to (already escaped) |
||
179 | * @param string $help help section identifier |
||
180 | * @param bool $do_print true to echo, false to return |
||
181 | */ |
||
182 | public function printHelp($str, $help = null, $do_print = true) |
||
183 | { |
||
184 | //\PC::debug(['str' => $str, 'help' => $help], 'printHelp'); |
||
185 | if ($help !== null) { |
||
186 | $helplink = $this->getHelpLink($help); |
||
187 | $str .= '<a class="help" href="'.$helplink.'" title="'.$this->lang['strhelp'].'" target="phppgadminhelp">'; |
||
188 | $str .= $this->lang['strhelpicon'].'</a>'; |
||
189 | } |
||
190 | if ($do_print) { |
||
191 | echo $str; |
||
192 | } else { |
||
193 | return $str; |
||
194 | } |
||
195 | } |
||
196 | |||
197 | /** |
||
198 | * Gets the help link. |
||
199 | * |
||
200 | * @param string $help The help subject |
||
201 | * |
||
202 | * @return string the help link |
||
203 | */ |
||
204 | public function getHelpLink($help) |
||
205 | { |
||
206 | return htmlspecialchars(SUBFOLDER.'/help?help='.urlencode($help).'&server='.urlencode($this->getServerId())); |
||
207 | } |
||
208 | |||
209 | /** |
||
210 | * Internally sets the reload browser property. |
||
211 | * |
||
212 | * @param bool $flag sets internal $_reload_browser var which will be passed to the footer methods |
||
213 | * |
||
214 | * @return \PHPPgAdmin\Misc this class instance |
||
215 | */ |
||
216 | public function setReloadBrowser($flag) |
||
217 | { |
||
218 | $this->_reload_browser = (bool) $flag; |
||
219 | |||
220 | return $this; |
||
221 | } |
||
222 | |||
223 | public function getReloadBrowser() |
||
224 | { |
||
225 | return $this->_reload_browser; |
||
226 | } |
||
227 | |||
228 | public function getContainer() |
||
229 | { |
||
230 | return $this->container; |
||
231 | } |
||
232 | |||
233 | /** |
||
234 | * Sets $_no_db_connection boolean value, allows to render scripts that do not need an active session. |
||
235 | * |
||
236 | * @param bool $flag true or false to allow unconnected clients to access the view |
||
237 | * |
||
238 | * @return \PHPPgAdmin\Misc this class instance |
||
239 | */ |
||
240 | public function setNoDBConnection($flag) |
||
241 | { |
||
242 | $this->_no_db_connection = (bool) $flag; |
||
243 | |||
244 | return $this; |
||
245 | } |
||
246 | |||
247 | /** |
||
248 | * Gets member variable $_no_db_connection. |
||
249 | * |
||
250 | * @return bool value of member variable $_no_db_connection |
||
251 | */ |
||
252 | public function getNoDBConnection() |
||
253 | { |
||
254 | return $this->_no_db_connection; |
||
255 | } |
||
256 | |||
257 | /** |
||
258 | * Sets the last error message to display afterwards instead of just dying with the error msg. |
||
259 | * |
||
260 | * @param string $msg error message string |
||
261 | * |
||
262 | * @return \PHPPgAdmin\Misc this class instance |
||
263 | */ |
||
264 | public function setErrorMsg($msg) |
||
269 | } |
||
270 | |||
271 | /** |
||
272 | * Returns the error messages stored in member variable $_error_msg. |
||
273 | * |
||
274 | * @return string the error message |
||
275 | */ |
||
276 | public function getErrorMsg() |
||
279 | } |
||
280 | |||
281 | /** |
||
282 | * Creates a database accessor. |
||
283 | * |
||
284 | * @param string $database the name of the database |
||
285 | * @param mixed $server_id the id of the server |
||
286 | * |
||
287 | * @internal mixed $plaform placeholder that will receive the value of the platform |
||
288 | */ |
||
289 | public function getDatabaseAccessor($database = '', $server_id = null) |
||
290 | { |
||
291 | $lang = $this->lang; |
||
292 | |||
293 | if ($server_id !== null) { |
||
294 | $this->_server_id = $server_id; |
||
295 | } |
||
296 | //$this->prtrace($this->_server_id); |
||
297 | |||
298 | $server_info = $this->getServerInfo($this->_server_id); |
||
299 | |||
300 | if ($this->_no_db_connection || !isset($server_info['username'])) { |
||
301 | return null; |
||
302 | } |
||
303 | |||
304 | if ($this->_data === null) { |
||
305 | try { |
||
306 | $_connection = $this->getConnection($database, $this->_server_id); |
||
307 | } catch (\Exception $e) { |
||
308 | $this->setServerInfo(null, null, $this->_server_id); |
||
309 | $this->setNoDBConnection(true); |
||
310 | $this->setErrorMsg($e->getMessage()); |
||
311 | |||
312 | return null; |
||
313 | } |
||
314 | |||
315 | //$this->prtrace('_connection', $_connection); |
||
316 | if (!$_connection) { |
||
317 | $this->container->utils->addError($lang['strloginfailed']); |
||
318 | $this->setErrorMsg($lang['strloginfailed']); |
||
319 | |||
320 | return null; |
||
321 | } |
||
322 | // Get the name of the database driver we need to use. |
||
323 | // The description of the server is returned in $platform. |
||
324 | $_type = $_connection->getDriver($platform); |
||
325 | |||
326 | //$this->prtrace(['type' => $_type, 'platform' => $platform, 'pgVersion' => $_connection->conn->pgVersion]); |
||
327 | |||
328 | if ($_type === null) { |
||
329 | $errormsg = sprintf($lang['strpostgresqlversionnotsupported'], $this->postgresqlMinVer); |
||
330 | $this->container->utils->addError($errormsg); |
||
331 | $this->setErrorMsg($errormsg); |
||
332 | |||
333 | return null; |
||
334 | } |
||
335 | $_type = '\PHPPgAdmin\Database\\'.$_type; |
||
336 | |||
337 | $this->setServerInfo('platform', $platform, $this->_server_id); |
||
338 | $this->setServerInfo('pgVersion', $_connection->conn->pgVersion, $this->_server_id); |
||
339 | |||
340 | // Create a database wrapper class for easy manipulation of the |
||
341 | // connection. |
||
342 | |||
343 | $this->_data = new $_type($_connection->conn, $this->container, $server_info); |
||
344 | $this->_data->platform = $_connection->platform; |
||
345 | |||
346 | //$this->_data->getHelpPages(); |
||
347 | |||
348 | //$this->prtrace('help_page has ' . count($this->_data->help_page) . ' items'); |
||
349 | |||
350 | /* we work on UTF-8 only encoding */ |
||
351 | $this->_data->execute("SET client_encoding TO 'UTF-8'"); |
||
352 | |||
353 | if ($this->_data->hasByteaHexDefault()) { |
||
354 | $this->_data->execute('SET bytea_output TO escape'); |
||
355 | } |
||
356 | } |
||
357 | |||
358 | if ($this->_no_db_connection === false && |
||
359 | $this->getDatabase() !== null && |
||
360 | isset($_REQUEST['schema']) |
||
361 | ) { |
||
362 | $status = $this->_data->setSchema($_REQUEST['schema']); |
||
363 | |||
364 | if ($status != 0) { |
||
365 | $this->container->utils->addError($this->lang['strbadschema']); |
||
366 | $this->setErrorMsg($this->lang['strbadschema']); |
||
367 | |||
368 | return null; |
||
369 | } |
||
370 | } |
||
371 | |||
372 | return $this->_data; |
||
373 | } |
||
374 | |||
375 | public function getConnection($database = '', $server_id = null) |
||
376 | { |
||
377 | $lang = $this->lang; |
||
378 | |||
379 | if ($this->_connection === null) { |
||
380 | if ($server_id !== null) { |
||
381 | $this->_server_id = $server_id; |
||
382 | } |
||
383 | $server_info = $this->getServerInfo($this->_server_id); |
||
384 | $database_to_use = $this->getDatabase($database); |
||
385 | |||
386 | // Perform extra security checks if this config option is set |
||
387 | if ($this->conf['extra_login_security']) { |
||
388 | // Disallowed logins if extra_login_security is enabled. |
||
389 | // These must be lowercase. |
||
390 | $bad_usernames = [ |
||
391 | 'pgsql' => 'pgsql', |
||
392 | 'postgres' => 'postgres', |
||
393 | 'root' => 'root', |
||
394 | 'administrator' => 'administrator', |
||
395 | ]; |
||
396 | |||
397 | if (isset($server_info['username']) && |
||
398 | array_key_exists(strtolower($server_info['username']), $bad_usernames) |
||
399 | ) { |
||
400 | $msg = $lang['strlogindisallowed']; |
||
401 | |||
402 | throw new \Exception($msg); |
||
403 | } |
||
404 | |||
405 | if (!isset($server_info['password']) || |
||
406 | $server_info['password'] == '' |
||
407 | ) { |
||
408 | $msg = $lang['strlogindisallowed']; |
||
409 | |||
410 | throw new \Exception($msg); |
||
411 | } |
||
412 | } |
||
413 | |||
414 | try { |
||
415 | // Create the connection object and make the connection |
||
416 | $this->_connection = new \PHPPgAdmin\Database\Connection( |
||
417 | $server_info, |
||
418 | $database_to_use, |
||
419 | $this->container |
||
420 | ); |
||
421 | } catch (\PHPPgAdmin\ADOdbException $e) { |
||
422 | throw new \Exception($lang['strloginfailed']); |
||
423 | } |
||
424 | } |
||
425 | |||
426 | return $this->_connection; |
||
427 | } |
||
428 | |||
429 | /** |
||
430 | * Validate and retrieve information on a server. |
||
431 | * If the parameter isn't supplied then the currently |
||
432 | * connected server is returned. |
||
433 | * |
||
434 | * @param string $server_id A server identifier (host:port) |
||
435 | * |
||
436 | * @return array An associative array of server properties |
||
437 | */ |
||
438 | public function getServerInfo($server_id = null) |
||
439 | { |
||
440 | //\PC::debug(['$server_id' => $server_id]); |
||
441 | |||
442 | if ($server_id !== null) { |
||
443 | $this->_server_id = $server_id; |
||
444 | } elseif ($this->_server_info !== null) { |
||
445 | return $this->_server_info; |
||
446 | } |
||
447 | |||
448 | // Check for the server in the logged-in list |
||
449 | if (isset($_SESSION['webdbLogin'][$this->_server_id])) { |
||
450 | $this->_server_info = $_SESSION['webdbLogin'][$this->_server_id]; |
||
451 | |||
452 | return $this->_server_info; |
||
453 | } |
||
454 | |||
455 | // Otherwise, look for it in the conf file |
||
456 | foreach ($this->conf['servers'] as $idx => $info) { |
||
457 | $server_string = $info['host'].':'.$info['port'].':'.$info['sslmode']; |
||
458 | $server_sha = sha1($server_string); |
||
459 | |||
460 | if ($this->_server_id === $server_string || |
||
461 | $this->_server_id === $server_sha |
||
462 | ) { |
||
463 | if (isset($info['username'])) { |
||
464 | $this->setServerInfo(null, $info, $this->_server_id); |
||
465 | } elseif (isset($_SESSION['sharedUsername'])) { |
||
466 | $info['username'] = $_SESSION['sharedUsername']; |
||
467 | $info['password'] = $_SESSION['sharedPassword']; |
||
468 | $this->setReloadBrowser(true); |
||
469 | $this->setServerInfo(null, $info, $this->_server_id); |
||
470 | } |
||
471 | $this->_server_info = $info; |
||
472 | |||
473 | return $this->_server_info; |
||
474 | } |
||
475 | } |
||
476 | |||
477 | if ($server_id === null) { |
||
478 | $this->_server_info = null; |
||
479 | |||
480 | return $this->_server_info; |
||
481 | } |
||
482 | |||
483 | $this->prtrace('Invalid server param'); |
||
484 | $this->_server_info = null; |
||
485 | // Unable to find a matching server, are we being hacked? |
||
486 | return $this->halt($this->lang['strinvalidserverparam']); |
||
487 | } |
||
488 | |||
489 | /** |
||
490 | * Set server information. |
||
491 | * |
||
492 | * @param null|string $key parameter name to set, or null to replace all |
||
493 | * params with the assoc-array in $value |
||
494 | * @param mixed $value the new value, or null to unset the parameter |
||
495 | * @param null|string $server_id the server identifier, or null for current server |
||
496 | */ |
||
497 | public function setServerInfo($key, $value, $server_id = null) |
||
517 | } |
||
518 | } |
||
519 | } |
||
520 | |||
521 | public function getDatabase($database = '') |
||
522 | { |
||
523 | if ($this->_server_id === null && !isset($_REQUEST['database'])) { |
||
524 | return null; |
||
525 | } |
||
526 | |||
527 | $server_info = $this->getServerInfo($this->_server_id); |
||
528 | |||
529 | if ($this->_server_id !== null && |
||
530 | isset($server_info['useonlydefaultdb']) && |
||
531 | $server_info['useonlydefaultdb'] === true && |
||
532 | isset($server_info['defaultdb']) |
||
533 | ) { |
||
534 | $this->_database = $server_info['defaultdb']; |
||
535 | } elseif ($database !== '') { |
||
536 | $this->_database = $database; |
||
537 | } elseif (isset($_REQUEST['database'])) { |
||
538 | // Connect to the current database |
||
539 | $this->_database = $_REQUEST['database']; |
||
540 | } elseif (isset($server_info['defaultdb'])) { |
||
541 | // or if one is not specified then connect to the default database. |
||
542 | $this->_database = $server_info['defaultdb']; |
||
543 | } else { |
||
544 | return null; |
||
545 | } |
||
546 | |||
547 | return $this->_database; |
||
548 | } |
||
549 | |||
550 | /** |
||
551 | * Set the current schema. |
||
552 | * |
||
553 | * @param string $schema The schema name |
||
554 | * |
||
555 | * @return int 0 on success |
||
556 | */ |
||
557 | public function setCurrentSchema($schema) |
||
558 | { |
||
559 | $data = $this->getDatabaseAccessor(); |
||
560 | |||
561 | $status = $data->setSchema($schema); |
||
562 | if ($status != 0) { |
||
563 | return $status; |
||
564 | } |
||
565 | |||
566 | $_REQUEST['schema'] = $schema; |
||
567 | $this->container->offsetSet('schema', $schema); |
||
568 | $this->setHREF(); |
||
569 | |||
570 | return 0; |
||
571 | } |
||
572 | |||
573 | /** |
||
574 | * Checks if dumps are properly set up. |
||
575 | * |
||
576 | * @param bool $all (optional) True to check pg_dumpall, false to just check pg_dump |
||
577 | * |
||
578 | * @return bool True, dumps are set up, false otherwise |
||
579 | */ |
||
580 | public function isDumpEnabled($all = false) |
||
581 | { |
||
582 | $info = $this->getServerInfo(); |
||
583 | |||
584 | return !empty($info[$all ? 'pg_dumpall_path' : 'pg_dump_path']); |
||
585 | } |
||
586 | |||
587 | /** |
||
588 | * Sets the href tracking variable. |
||
589 | * |
||
590 | * @return \PHPPgAdmin\Misc this class instance |
||
591 | */ |
||
592 | public function setHREF() |
||
597 | } |
||
598 | |||
599 | /** |
||
600 | * Get a href query string, excluding objects below the given object type (inclusive). |
||
601 | * |
||
602 | * @param null|string $exclude_from |
||
603 | * |
||
604 | * @return string |
||
605 | */ |
||
606 | public function getHREF($exclude_from = null) |
||
627 | } |
||
628 | |||
629 | public function getSubjectParams($subject) |
||
804 | } |
||
805 | |||
806 | /** |
||
807 | * Sets the form tracking variable. |
||
808 | */ |
||
809 | public function setForm() |
||
825 | //\PC::debug($this->form, 'Misc::form'); |
||
826 | } |
||
827 | |||
828 | /** |
||
829 | * Render a value into HTML using formatting rules specified |
||
830 | * by a type name and parameters. |
||
831 | * |
||
832 | * @param string $str The string to change |
||
833 | * @param string $type Field type (optional), this may be an internal PostgreSQL type, or: |
||
834 | * yesno - same as bool, but renders as 'Yes' or 'No'. |
||
835 | * pre - render in a <pre> block. |
||
836 | * nbsp - replace all spaces with 's |
||
837 | * verbatim - render exactly as supplied, no escaping what-so-ever. |
||
838 | * callback - render using a callback function supplied in the 'function' param. |
||
839 | * @param array $params Type parameters (optional), known parameters: |
||
840 | * null - string to display if $str is null, or set to TRUE to use a default 'NULL' string, |
||
841 | * otherwise nothing is rendered. |
||
842 | * clip - if true, clip the value to a fixed length, and append an ellipsis... |
||
843 | * cliplen - the maximum length when clip is enabled (defaults to $conf['max_chars']) |
||
844 | * ellipsis - the string to append to a clipped value (defaults to $lang['strellipsis']) |
||
845 | * tag - an HTML element name to surround the value. |
||
846 | * class - a class attribute to apply to any surrounding HTML element. |
||
847 | * align - an align attribute ('left','right','center' etc.) |
||
848 | * true - (type='bool') the representation of true. |
||
849 | * false - (type='bool') the representation of false. |
||
850 | * function - (type='callback') a function name, accepts args ($str, $params) and returns a rendering. |
||
851 | * lineno - prefix each line with a line number. |
||
852 | * map - an associative array. |
||
853 | * |
||
854 | * @return string The HTML rendered value |
||
855 | */ |
||
856 | public function printVal($str, $type = null, $params = []) |
||
857 | { |
||
858 | $lang = $this->lang; |
||
859 | $data = $this->getDatabaseAccessor(); |
||
860 | |||
861 | // Shortcircuit for a NULL value |
||
862 | if (!$str) { |
||
863 | return isset($params['null']) |
||
864 | ? ($params['null'] === true ? '<i>NULL</i>' : $params['null']) |
||
865 | : ''; |
||
866 | } |
||
867 | |||
868 | if (isset($params['map'], $params['map'][$str])) { |
||
869 | $str = $params['map'][$str]; |
||
870 | } |
||
871 | |||
872 | // Clip the value if the 'clip' parameter is true. |
||
873 | if (isset($params['clip']) && $params['clip'] === true) { |
||
874 | $maxlen = isset($params['cliplen']) && is_integer($params['cliplen']) ? $params['cliplen'] : $this->conf['max_chars']; |
||
875 | $ellipsis = isset($params['ellipsis']) ? $params['ellipsis'] : $lang['strellipsis']; |
||
876 | if (strlen($str) > $maxlen) { |
||
877 | $str = substr($str, 0, $maxlen - 1).$ellipsis; |
||
878 | } |
||
879 | } |
||
880 | |||
881 | $out = ''; |
||
882 | $class = ''; |
||
883 | |||
884 | switch ($type) { |
||
885 | case 'int2': |
||
886 | case 'int4': |
||
887 | case 'int8': |
||
888 | case 'float4': |
||
889 | case 'float8': |
||
890 | case 'money': |
||
891 | case 'numeric': |
||
892 | case 'oid': |
||
893 | case 'xid': |
||
894 | case 'cid': |
||
895 | case 'tid': |
||
896 | $align = 'right'; |
||
897 | $out = nl2br(htmlspecialchars(\PHPPgAdmin\Traits\HelperTrait::br2ln($str))); |
||
898 | |||
899 | break; |
||
900 | case 'yesno': |
||
901 | if (!isset($params['true'])) { |
||
902 | $params['true'] = $lang['stryes']; |
||
903 | } |
||
904 | |||
905 | if (!isset($params['false'])) { |
||
906 | $params['false'] = $lang['strno']; |
||
907 | } |
||
908 | |||
909 | // no break - fall through to boolean case. |
||
910 | case 'bool': |
||
911 | case 'boolean': |
||
912 | if (is_bool($str)) { |
||
913 | $str = $str ? 't' : 'f'; |
||
914 | } |
||
915 | |||
916 | switch ($str) { |
||
917 | case 't': |
||
918 | $out = (isset($params['true']) ? $params['true'] : $lang['strtrue']); |
||
919 | $align = 'center'; |
||
920 | |||
921 | break; |
||
922 | case 'f': |
||
923 | $out = (isset($params['false']) ? $params['false'] : $lang['strfalse']); |
||
924 | $align = 'center'; |
||
925 | |||
926 | break; |
||
927 | default: |
||
928 | $out = htmlspecialchars($str); |
||
929 | } |
||
930 | |||
931 | break; |
||
932 | case 'bytea': |
||
933 | $tag = 'div'; |
||
934 | $class = 'pre'; |
||
935 | $out = $data->escapeBytea($str); |
||
936 | |||
937 | break; |
||
938 | case 'errormsg': |
||
939 | $tag = 'pre'; |
||
940 | $class = 'error'; |
||
941 | $out = htmlspecialchars($str); |
||
942 | |||
943 | break; |
||
944 | case 'pre': |
||
945 | $tag = 'pre'; |
||
946 | $out = htmlspecialchars($str); |
||
947 | |||
948 | break; |
||
949 | case 'prenoescape': |
||
950 | $tag = 'pre'; |
||
951 | $out = $str; |
||
952 | |||
953 | break; |
||
954 | case 'nbsp': |
||
955 | $out = nl2br(str_replace(' ', ' ', \PHPPgAdmin\Traits\HelperTrait::br2ln($str))); |
||
956 | |||
957 | break; |
||
958 | case 'verbatim': |
||
959 | $out = $str; |
||
960 | |||
961 | break; |
||
962 | case 'callback': |
||
963 | $out = $params['function']($str, $params); |
||
964 | |||
965 | break; |
||
966 | case 'prettysize': |
||
967 | if ($str == -1) { |
||
968 | $out = $lang['strnoaccess']; |
||
969 | } else { |
||
970 | $out = \PHPPgAdmin\Traits\HelperTrait::formatSizeUnits($str, $lang); |
||
971 | } |
||
972 | |||
973 | break; |
||
974 | default: |
||
975 | // If the string contains at least one instance of >1 space in a row, a tab |
||
976 | // character, a space at the start of a line, or a space at the start of |
||
977 | // the whole string then render within a pre-formatted element (<pre>). |
||
978 | if (preg_match('/(^ | |\t|\n )/m', $str)) { |
||
979 | $tag = 'pre'; |
||
980 | $class = 'data'; |
||
981 | $out = htmlspecialchars($str); |
||
982 | } else { |
||
983 | $tag = 'span'; |
||
984 | $out = nl2br(htmlspecialchars(\PHPPgAdmin\Traits\HelperTrait::br2ln($str))); |
||
985 | } |
||
986 | } |
||
987 | |||
988 | if (isset($params['class'])) { |
||
989 | $class = $params['class']; |
||
990 | } |
||
991 | |||
992 | if (isset($params['align'])) { |
||
993 | $align = $params['align']; |
||
994 | } |
||
995 | |||
996 | if (!isset($tag) && (!empty($class) || isset($align))) { |
||
997 | $tag = 'div'; |
||
998 | } |
||
999 | |||
1000 | if (isset($tag)) { |
||
1001 | $alignattr = isset($align) ? " style=\"text-align: {$align}\"" : ''; |
||
1002 | $classattr = !empty($class) ? " class=\"{$class}\"" : ''; |
||
1003 | $out = "<{$tag}{$alignattr}{$classattr}>{$out}</{$tag}>"; |
||
1004 | } |
||
1005 | |||
1006 | // Add line numbers if 'lineno' param is true |
||
1007 | if (isset($params['lineno']) && $params['lineno'] === true) { |
||
1008 | $lines = explode("\n", $str); |
||
1009 | $num = count($lines); |
||
1010 | if ($num > 0) { |
||
1011 | $temp = "<table>\n<tr><td class=\"{$class}\" style=\"vertical-align: top; padding-right: 10px;\"><pre class=\"{$class}\">"; |
||
1012 | for ($i = 1; $i <= $num; ++$i) { |
||
1013 | $temp .= $i."\n"; |
||
1014 | } |
||
1015 | $temp .= "</pre></td><td class=\"{$class}\" style=\"vertical-align: top;\">{$out}</td></tr></table>\n"; |
||
1016 | $out = $temp; |
||
1017 | } |
||
1018 | unset($lines); |
||
1019 | } |
||
1020 | |||
1021 | return $out; |
||
1022 | } |
||
1023 | |||
1024 | /** |
||
1025 | * A function to recursively strip slashes. Used to |
||
1026 | * enforce magic_quotes_gpc being off. |
||
1027 | * |
||
1028 | * @param mixed $var The variable to strip (passed by reference) |
||
1029 | */ |
||
1030 | public function stripVar(&$var) |
||
1031 | { |
||
1032 | if (is_array($var)) { |
||
1033 | foreach ($var as $k => $v) { |
||
1034 | $this->stripVar($var[$k]); |
||
1035 | |||
1036 | /* magic_quotes_gpc escape keys as well ...*/ |
||
1037 | if (is_string($k)) { |
||
1038 | $ek = stripslashes($k); |
||
1039 | if ($ek !== $k) { |
||
1040 | $var[$ek] = $var[$k]; |
||
1041 | unset($var[$k]); |
||
1042 | } |
||
1043 | } |
||
1044 | } |
||
1045 | } else { |
||
1046 | $var = stripslashes($var); |
||
1047 | } |
||
1048 | } |
||
1049 | |||
1050 | /** |
||
1051 | * Retrieve the tab info for a specific tab bar. |
||
1052 | * |
||
1053 | * @param string $section the name of the tab bar |
||
1054 | * |
||
1055 | * @return array array of tabs |
||
1056 | */ |
||
1057 | public function getNavTabs($section) |
||
1750 | } |
||
1751 | |||
1752 | /** |
||
1753 | * Get the URL for the last active tab of a particular tab bar. |
||
1754 | * |
||
1755 | * @param string $section |
||
1756 | * |
||
1757 | * @return null|mixed |
||
1758 | */ |
||
1759 | public function getLastTabURL($section) |
||
1771 | } |
||
1772 | |||
1773 | /** |
||
1774 | * Converts a PHP.INI size variable to bytes. Taken from publically available |
||
1775 | * function by Chris DeRose, here: http://www.php.net/manual/en/configuration.directives.php#ini.file-uploads. |
||
1776 | * |
||
1777 | * @param mixed $strIniSize The PHP.INI variable |
||
1778 | * |
||
1779 | * @return bool|float|int size in bytes, false on failure |
||
1780 | */ |
||
1781 | public function inisizeToBytes($strIniSize) |
||
1782 | { |
||
1783 | // This function will take the string value of an ini 'size' parameter, |
||
1784 | // and return a double (64-bit float) representing the number of bytes |
||
1785 | // that the parameter represents. Or false if $strIniSize is unparseable. |
||
1786 | $a_IniParts = []; |
||
1787 | |||
1788 | if (!is_string($strIniSize)) { |
||
1789 | return false; |
||
1790 | } |
||
1791 | |||
1792 | if (!preg_match('/^(\d+)([bkm]*)$/i', $strIniSize, $a_IniParts)) { |
||
1793 | return false; |
||
1794 | } |
||
1795 | |||
1796 | $nSize = (float) $a_IniParts[1]; |
||
1797 | $strUnit = strtolower($a_IniParts[2]); |
||
1798 | |||
1799 | switch ($strUnit) { |
||
1800 | case 'm': |
||
1801 | return $nSize * (float) 1048576; |
||
1802 | case 'k': |
||
1803 | return $nSize * (float) 1024; |
||
1804 | case 'b': |
||
1805 | default: |
||
1806 | return $nSize; |
||
1807 | } |
||
1808 | } |
||
1809 | |||
1810 | public function getRequestVars($subject = '') |
||
1811 | { |
||
1812 | $v = []; |
||
1813 | if (!empty($subject)) { |
||
1814 | $v['subject'] = $subject; |
||
1815 | } |
||
1816 | |||
1817 | if ($this->_server_id !== null && $subject != 'root') { |
||
1818 | $v['server'] = $this->_server_id; |
||
1819 | if ($this->_database !== null && $subject != 'server') { |
||
1820 | $v['database'] = $this->_database; |
||
1821 | if (isset($_REQUEST['schema']) && $subject != 'database') { |
||
1822 | $v['schema'] = $_REQUEST['schema']; |
||
1823 | } |
||
1824 | } |
||
1825 | } |
||
1826 | //$this->prtrace($v); |
||
1827 | return $v; |
||
1828 | } |
||
1829 | |||
1830 | public function icon($icon) |
||
1831 | { |
||
1832 | if (is_string($icon)) { |
||
1833 | $path = "/assets/images/themes/{$this->conf['theme']}/{$icon}"; |
||
1834 | if (file_exists(\BASE_PATH.$path.'.png')) { |
||
1835 | return SUBFOLDER.$path.'.png'; |
||
1836 | } |
||
1837 | |||
1838 | if (file_exists(\BASE_PATH.$path.'.gif')) { |
||
1839 | return SUBFOLDER.$path.'.gif'; |
||
1840 | } |
||
1841 | |||
1842 | if (file_exists(\BASE_PATH.$path.'.ico')) { |
||
1843 | return SUBFOLDER.$path.'.ico'; |
||
1844 | } |
||
1845 | |||
1846 | $path = "/assets/images/themes/default/{$icon}"; |
||
1847 | if (file_exists(\BASE_PATH.$path.'.png')) { |
||
1848 | return SUBFOLDER.$path.'.png'; |
||
1849 | } |
||
1850 | |||
1851 | if (file_exists(\BASE_PATH.$path.'.gif')) { |
||
1852 | return SUBFOLDER.$path.'.gif'; |
||
1853 | } |
||
1854 | |||
1855 | if (file_exists(\BASE_PATH.$path.'.ico')) { |
||
1856 | return SUBFOLDER.$path.'.ico'; |
||
1857 | } |
||
1858 | } else { |
||
1859 | // Icon from plugins |
||
1860 | $path = "/plugins/{$icon[0]}/images/{$icon[1]}"; |
||
1861 | if (file_exists(\BASE_PATH.$path.'.png')) { |
||
1862 | return SUBFOLDER.$path.'.png'; |
||
1863 | } |
||
1864 | |||
1865 | if (file_exists(\BASE_PATH.$path.'.gif')) { |
||
1866 | return SUBFOLDER.$path.'.gif'; |
||
1867 | } |
||
1868 | |||
1869 | if (file_exists(\BASE_PATH.$path.'.ico')) { |
||
1870 | return SUBFOLDER.$path.'.ico'; |
||
1871 | } |
||
1872 | } |
||
1873 | |||
1874 | return ''; |
||
1875 | } |
||
1876 | |||
1877 | /** |
||
1878 | * Function to escape command line parameters. |
||
1879 | * |
||
1880 | * @param string $str The string to escape |
||
1881 | * |
||
1882 | * @return string The escaped string |
||
1883 | */ |
||
1884 | public function escapeShellArg($str) |
||
1885 | { |
||
1886 | //$data = $this->getDatabaseAccessor(); |
||
1887 | $lang = $this->lang; |
||
1888 | |||
1889 | if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { |
||
1890 | // Due to annoying PHP bugs, shell arguments cannot be escaped |
||
1891 | // (command simply fails), so we cannot allow complex objects |
||
1892 | // to be dumped. |
||
1893 | if (preg_match('/^[_.[:alnum:]]+$/', $str)) { |
||
1894 | return $str; |
||
1895 | } |
||
1896 | |||
1897 | return $this->halt($lang['strcannotdumponwindows']); |
||
1898 | } |
||
1899 | |||
1900 | return escapeshellarg($str); |
||
1901 | } |
||
1902 | |||
1903 | /** |
||
1904 | * Function to escape command line programs. |
||
1905 | * |
||
1906 | * @param string $str The string to escape |
||
1907 | * |
||
1908 | * @return string The escaped string |
||
1909 | */ |
||
1910 | public function escapeShellCmd($str) |
||
1921 | } |
||
1922 | |||
1923 | /** |
||
1924 | * Save the given SQL script in the history |
||
1925 | * of the database and server. |
||
1926 | * |
||
1927 | * @param string $script the SQL script to save |
||
1928 | */ |
||
1929 | public function saveScriptHistory($script) |
||
1930 | { |
||
1931 | list($usec, $sec) = explode(' ', microtime()); |
||
1932 | $time = ((float) $usec + (float) $sec); |
||
1933 | $_SESSION['history'][$_REQUEST['server']][$_REQUEST['database']]["${time}"] = [ |
||
1934 | 'query' => $script, |
||
1935 | 'paginate' => !isset($_REQUEST['paginate']) ? 'f' : 't', |
||
1936 | 'queryid' => $time, |
||
1937 | ]; |
||
1938 | } |
||
1939 | |||
1940 | /** |
||
1941 | * Returns an array representing FKs definition for a table, sorted by fields |
||
1942 | * or by constraint. |
||
1943 | * |
||
1944 | * @param string $table The table to retrieve FK contraints from |
||
1945 | * |
||
1946 | * @return array|bool the array of FK definition: |
||
1947 | * array( |
||
1948 | * 'byconstr' => array( |
||
1949 | * constrain id => array( |
||
1950 | * confrelid => foreign relation oid |
||
1951 | * f_schema => foreign schema name |
||
1952 | * f_table => foreign table name |
||
1953 | * pattnums => array of parent's fields nums |
||
1954 | * pattnames => array of parent's fields names |
||
1955 | * fattnames => array of foreign attributes names |
||
1956 | * ) |
||
1957 | * ), |
||
1958 | * 'byfield' => array( |
||
1959 | * attribute num => array (constraint id, ...) |
||
1960 | * ), |
||
1961 | * 'code' => HTML/js code to include in the page for auto-completion |
||
1962 | * ) |
||
1963 | */ |
||
1964 | public function getAutocompleteFKProperties($table) |
||
2046 | } |
||
2047 | } |
||
2048 |