1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
|
<?php
/**
* PHPPgAdmin v6.0.0-RC7
*/
namespace PHPPgAdmin;
/**
* @file
* Class to hold various commonly used functions
*
* Id: Misc.php,v 1.171 2008/03/17 21:35:48 ioguix Exp $
*
* @package PHPPgAdmin
*/
/**
* Class to hold various commonly used functions.
*
* Release: Misc.php,v 1.171 2008/03/17 21:35:48 ioguix Exp $
*
* @package PHPPgAdmin
*/
class Misc
{
use \PHPPgAdmin\Traits\HelperTrait;
use \PHPPgAdmin\Traits\MiscTrait;
private $_connection;
private $_no_db_connection = false;
private $_reload_browser = false;
private $_data;
private $_database;
private $_server_id;
private $_server_info;
private $_error_msg = '';
public $appLangFiles = [];
public $appName = '';
public $appVersion = '';
public $form = '';
public $href = '';
public $controller_name = 'Misc';
public $lang = [];
protected $container;
/**
* @param \Slim\Container $container The container
*/
public function __construct(\Slim\Container $container)
{
$this->container = $container;
$this->lang = $container->get('lang');
$this->conf = $container->get('conf');
//$this->view = $container->get('view');
$this->plugin_manager = $container->get('plugin_manager');
$this->appLangFiles = $container->get('appLangFiles');
$this->appName = $container->get('settings')['appName'];
$this->appVersion = $container->get('settings')['appVersion'];
$this->postgresqlMinVer = $container->get('settings')['postgresqlMinVer'];
$this->phpMinVer = $container->get('settings')['phpMinVer'];
$base_version = $container->get('settings')['base_version'];
//$this->prtrace($base_version);
// Check for config file version mismatch
if (!isset($this->conf['version']) || $base_version > $this->conf['version']) {
$container->get('utils')->addError($this->lang['strbadconfig']);
}
// Check database support is properly compiled in
if (!function_exists('pg_connect')) {
$container->get('utils')->addError($this->lang['strnotloaded']);
}
// Check the version of PHP
if (version_compare(PHP_VERSION, $this->phpMinVer, '<')) {
$container->get('utils')->addError(sprintf('Version of PHP not supported. Please upgrade to version %s or later.', $this->phpMinVer));
}
//$this->dumpAndDie($this);
$this->getServerId();
}
public function serverToSha()
{
$request_server = $this->container->requestobj->getParam('server');
if ($request_server === null) {
return null;
}
$srv_array = explode(':', $request_server);
if (count($srv_array) === 3) {
return sha1($request_server);
}
return $request_server;
}
public function getServerId()
{
if ($this->_server_id) {
return $this->_server_id;
}
$request_server = $this->serverToSha();
if (count($this->conf['servers']) === 1) {
$info = $this->conf['servers'][0];
$this->_server_id = sha1($info['host'].':'.$info['port'].':'.$info['sslmode']);
} elseif ($request_server !== null) {
$this->_server_id = $request_server;
} elseif (isset($_SESSION['webdbLogin']) && count($_SESSION['webdbLogin']) > 0) {
//$this->prtrace('webdbLogin', $_SESSION['webdbLogin']);
$this->_server_id = array_keys($_SESSION['webdbLogin'])[0];
}
return $this->_server_id;
}
/**
* Sets the view instance property of this class.
*
* @param \Slim\Views\Twig $view view instance
*
* @return \PHPPgAdmin\Misc this class instance
*/
public function setView(\Slim\Views\Twig $view)
{
$this->view = $view;
return $this;
}
/**
* Adds or modifies a key in the $conf instance property of this class.
*
* @param string $key name of the key to set
* @param mixed $value value of the key to set
*
* @return \PHPPgAdmin\Misc this class instance
*/
public function setConf($key, $value)
{
$this->conf[$key] = $value;
return $this;
}
/**
* Gets the value of a config property, or the array of all config properties.
*
* @param null|string $key value of the key to be retrieved. If null, the full array is returnes
*
* @return null|array|string the whole $conf array, the value of $conf[key] or null if said key does not exist
*/
public function getConf($key = null)
{
if ($key === null) {
return $this->conf;
}
if (array_key_exists($key, $this->conf)) {
return $this->conf[$key];
}
return null;
}
/**
* Displays link to the context help.
*
* @param string $str the string that the context help is related to (already escaped)
* @param string $help help section identifier
* @param bool $do_print true to echo, false to return
*/
public function printHelp($str, $help = null, $do_print = true)
{
if ($help !== null) {
$helplink = $this->getHelpLink($help);
$str .= '<a class="help" href="'.$helplink.'" title="'.$this->lang['strhelp'].'" target="phppgadminhelp">';
$str .= $this->lang['strhelpicon'].'</a>';
}
if ($do_print) {
echo $str;
} else {
return $str;
}
}
/**
* Gets the help link.
*
* @param string $help The help subject
*
* @return string the help link
*/
public function getHelpLink($help)
{
return htmlspecialchars(SUBFOLDER.'/help?help='.urlencode($help).'&server='.urlencode($this->getServerId()));
}
/**
* Internally sets the reload browser property.
*
* @param bool $flag sets internal $_reload_browser var which will be passed to the footer methods
*
* @return \PHPPgAdmin\Misc this class instance
*/
public function setReloadBrowser($flag)
{
$this->_reload_browser = (bool) $flag;
return $this;
}
public function getReloadBrowser()
{
return $this->_reload_browser;
}
public function getContainer()
{
return $this->container;
}
/**
* Sets $_no_db_connection boolean value, allows to render scripts that do not need an active session.
*
* @param bool $flag true or false to allow unconnected clients to access the view
*
* @return \PHPPgAdmin\Misc this class instance
*/
public function setNoDBConnection($flag)
{
$this->_no_db_connection = (bool) $flag;
return $this;
}
/**
* Gets member variable $_no_db_connection.
*
* @return bool value of member variable $_no_db_connection
*/
public function getNoDBConnection()
{
return $this->_no_db_connection;
}
/**
* Sets the last error message to display afterwards instead of just dying with the error msg.
*
* @param string $msg error message string
*
* @return \PHPPgAdmin\Misc this class instance
*/
public function setErrorMsg($msg)
{
$this->_error_msg = $msg;
return $this;
}
/**
* Returns the error messages stored in member variable $_error_msg.
*
* @return string the error message
*/
public function getErrorMsg()
{
return $this->_error_msg;
}
/**
* Creates a database accessor.
*
* @param string $database the name of the database
* @param mixed $server_id the id of the server
*
* @internal mixed $plaform placeholder that will receive the value of the platform
*
* @return \PHPPgAdmin\Database\ADOdbBase the database accessor instance
*/
public function getDatabaseAccessor($database = '', $server_id = null)
{
$lang = $this->lang;
if ($server_id !== null) {
$this->_server_id = $server_id;
}
//$this->prtrace($this->_server_id);
$server_info = $this->getServerInfo($this->_server_id);
if ($this->_no_db_connection || !isset($server_info['username'])) {
return null;
}
if ($this->_data === null) {
try {
$_connection = $this->getConnection($database, $this->_server_id);
} catch (\Exception $e) {
$this->setServerInfo(null, null, $this->_server_id);
$this->setNoDBConnection(true);
$this->setErrorMsg($e->getMessage());
return null;
}
//$this->prtrace('_connection', $_connection);
if (!$_connection) {
$this->container->utils->addError($lang['strloginfailed']);
$this->setErrorMsg($lang['strloginfailed']);
return null;
}
// Get the name of the database driver we need to use.
// The description of the server is returned in $platform.
$_type = $_connection->getDriver($platform);
//$this->prtrace(['type' => $_type, 'platform' => $platform, 'pgVersion' => $_connection->conn->pgVersion]);
if ($_type === null) {
$errormsg = sprintf($lang['strpostgresqlversionnotsupported'], $this->postgresqlMinVer);
$this->container->utils->addError($errormsg);
$this->setErrorMsg($errormsg);
return null;
}
$_type = '\PHPPgAdmin\Database\\'.$_type;
//$this->prtrace('driver:', $_type);
$this->setServerInfo('platform', $platform, $this->_server_id);
$this->setServerInfo('pgVersion', $_connection->conn->pgVersion, $this->_server_id);
// Create a database wrapper class for easy manipulation of the
// connection.
$this->_data = new $_type($_connection->conn, $this->container, $server_info);
$this->_data->platform = $_connection->platform;
//$this->_data->getHelpPages();
//$this->prtrace('help_page has ' . count($this->_data->help_page) . ' items');
/* we work on UTF-8 only encoding */
$this->_data->execute("SET client_encoding TO 'UTF-8'");
if ($this->_data->hasByteaHexDefault()) {
$this->_data->execute('SET bytea_output TO escape');
}
}
if ($this->_no_db_connection === false &&
$this->getDatabase() !== null &&
isset($_REQUEST['schema'])
) {
$status = $this->_data->setSchema($_REQUEST['schema']);
if ($status != 0) {
$this->container->utils->addError($this->lang['strbadschema']);
$this->setErrorMsg($this->lang['strbadschema']);
return null;
}
}
return $this->_data;
}
public function getConnection($database = '', $server_id = null)
{
$lang = $this->lang;
if ($this->_connection === null) {
if ($server_id !== null) {
$this->_server_id = $server_id;
}
$server_info = $this->getServerInfo($this->_server_id);
$database_to_use = $this->getDatabase($database);
// Perform extra security checks if this config option is set
if ($this->conf['extra_login_security']) {
// Disallowed logins if extra_login_security is enabled.
// These must be lowercase.
$bad_usernames = [
'pgsql' => 'pgsql',
'postgres' => 'postgres',
'root' => 'root',
'administrator' => 'administrator',
];
if (isset($server_info['username']) &&
array_key_exists(strtolower($server_info['username']), $bad_usernames)
) {
$msg = $lang['strlogindisallowed'];
throw new \Exception($msg);
}
if (!isset($server_info['password']) ||
$server_info['password'] == ''
) {
$msg = $lang['strlogindisallowed'];
throw new \Exception($msg);
}
}
try {
// Create the connection object and make the connection
$this->_connection = new \PHPPgAdmin\Database\Connection(
$server_info,
$database_to_use,
$this->container
);
} catch (\PHPPgAdmin\ADOdbException $e) {
throw new \Exception($lang['strloginfailed']);
}
}
return $this->_connection;
}
/**
* Validate and retrieve information on a server.
* If the parameter isn't supplied then the currently
* connected server is returned.
*
* @param string $server_id A server identifier (host:port)
*
* @return array An associative array of server properties
*/
public function getServerInfo($server_id = null)
{
if ($server_id !== null) {
$this->_server_id = $server_id;
} elseif ($this->_server_info !== null) {
return $this->_server_info;
}
// Check for the server in the logged-in list
if (isset($_SESSION['webdbLogin'][$this->_server_id])) {
$this->_server_info = $_SESSION['webdbLogin'][$this->_server_id];
return $this->_server_info;
}
// Otherwise, look for it in the conf file
foreach ($this->conf['servers'] as $idx => $info) {
$server_string = $info['host'].':'.$info['port'].':'.$info['sslmode'];
$server_sha = sha1($server_string);
if ($this->_server_id === $server_string ||
$this->_server_id === $server_sha
) {
if (isset($info['username'])) {
$this->setServerInfo(null, $info, $this->_server_id);
} elseif (isset($_SESSION['sharedUsername'])) {
$info['username'] = $_SESSION['sharedUsername'];
$info['password'] = $_SESSION['sharedPassword'];
$this->setReloadBrowser(true);
$this->setServerInfo(null, $info, $this->_server_id);
}
$this->_server_info = $info;
return $this->_server_info;
}
}
if ($server_id === null) {
$this->_server_info = null;
return $this->_server_info;
}
// $this->prtrace('Invalid server param');
$this->_server_info = null;
// Unable to find a matching server, are we being hacked?
return $this->halt($this->lang['strinvalidserverparam']);
}
/**
* Set server information.
*
* @param null|string $key parameter name to set, or null to replace all
* params with the assoc-array in $value
* @param mixed $value the new value, or null to unset the parameter
* @param null|string $server_id the server identifier, or null for current server
*/
public function setServerInfo($key, $value, $server_id = null)
{
if ($server_id === null) {
$server_id = $this->container->requestobj->getParam('server');
}
if ($key === null) {
if ($value === null) {
unset($_SESSION['webdbLogin'][$server_id]);
} else {
$_SESSION['webdbLogin'][$server_id] = $value;
}
} else {
if ($value === null) {
unset($_SESSION['webdbLogin'][$server_id][$key]);
} else {
$_SESSION['webdbLogin'][$server_id][$key] = $value;
}
}
}
public function getDatabase($database = '')
{
if ($this->_server_id === null && !isset($_REQUEST['database'])) {
return null;
}
$server_info = $this->getServerInfo($this->_server_id);
if ($this->_server_id !== null &&
isset($server_info['useonlydefaultdb']) &&
$server_info['useonlydefaultdb'] === true &&
isset($server_info['defaultdb'])
) {
$this->_database = $server_info['defaultdb'];
} elseif ($database !== '') {
$this->_database = $database;
} elseif (isset($_REQUEST['database'])) {
// Connect to the current database
$this->_database = $_REQUEST['database'];
} elseif (isset($server_info['defaultdb'])) {
// or if one is not specified then connect to the default database.
$this->_database = $server_info['defaultdb'];
} else {
return null;
}
return $this->_database;
}
/**
* Set the current schema.
*
* @param string $schema The schema name
*
* @return int 0 on success
*/
public function setCurrentSchema($schema)
{
$data = $this->getDatabaseAccessor();
$status = $data->setSchema($schema);
if ($status != 0) {
return $status;
}
$_REQUEST['schema'] = $schema;
$this->container->offsetSet('schema', $schema);
$this->setHREF();
return 0;
}
/**
* Checks if dumps are properly set up.
*
* @param bool $all (optional) True to check pg_dumpall, false to just check pg_dump
*
* @return bool True, dumps are set up, false otherwise
*/
public function isDumpEnabled($all = false)
{
$info = $this->getServerInfo();
return !empty($info[$all ? 'pg_dumpall_path' : 'pg_dump_path']);
}
/**
* Sets the href tracking variable.
*
* @return \PHPPgAdmin\Misc this class instance
*/
public function setHREF()
{
$this->href = $this->getHREF();
return $this;
}
/**
* Get a href query string, excluding objects below the given object type (inclusive).
*
* @param null|string $exclude_from
*
* @return string
*/
public function getHREF($exclude_from = null)
{
$href = [];
$server = $this->container->server || isset($_REQUEST['server']) ? $_REQUEST['server'] : null;
$database = $this->container->database || isset($_REQUEST['database']) ? $_REQUEST['database'] : null;
$schema = $this->container->schema || isset($_REQUEST['schema']) ? $_REQUEST['schema'] : null;
if ($server && $exclude_from !== 'server') {
$href[] = 'server='.urlencode($server);
}
if ($database && $exclude_from !== 'database') {
$href[] = 'database='.urlencode($database);
}
if ($schema && $exclude_from !== 'schema') {
$href[] = 'schema='.urlencode($schema);
}
$this->href = htmlentities(implode('&', $href));
return $this->href;
}
/**
* Sets the form tracking variable.
*/
public function setForm()
{
$form = [];
if ($this->container->server) {
$form[] = '<input type="hidden" name="server" value="'.htmlspecialchars($this->container->server).'" />';
}
if ($this->container->database) {
$form[] = '<input type="hidden" name="database" value="'.htmlspecialchars($this->container->database).'" />';
}
if ($this->container->schema) {
$form[] = '<input type="hidden" name="schema" value="'.htmlspecialchars($this->container->schema).'" />';
}
$this->form = implode("\n", $form);
return $this->form;
}
/**
* A function to recursively strip slashes. Used to
* enforce magic_quotes_gpc being off.
*
* @param mixed $var The variable to strip (passed by reference)
*/
public function stripVar(&$var)
{
if (is_array($var)) {
foreach ($var as $k => $v) {
$this->stripVar($var[$k]);
/* magic_quotes_gpc escape keys as well ...*/
if (is_string($k)) {
$ek = stripslashes($k);
if ($ek !== $k) {
$var[$ek] = $var[$k];
unset($var[$k]);
}
}
}
} else {
$var = stripslashes($var);
}
}
/**
* Converts a PHP.INI size variable to bytes. Taken from publically available
* function by Chris DeRose, here: http://www.php.net/manual/en/configuration.directives.php#ini.file-uploads.
*
* @param mixed $strIniSize The PHP.INI variable
*
* @return bool|float|int size in bytes, false on failure
*/
public function inisizeToBytes($strIniSize)
{
// This function will take the string value of an ini 'size' parameter,
// and return a double (64-bit float) representing the number of bytes
// that the parameter represents. Or false if $strIniSize is unparseable.
$a_IniParts = [];
if (!is_string($strIniSize)) {
return false;
}
if (!preg_match('/^(\d+)([bkm]*)$/i', $strIniSize, $a_IniParts)) {
return false;
}
$nSize = (float) $a_IniParts[1];
$strUnit = strtolower($a_IniParts[2]);
switch ($strUnit) {
case 'm':
return $nSize * (float) 1048576;
case 'k':
return $nSize * (float) 1024;
case 'b':
default:
return $nSize;
}
}
public function getRequestVars($subject = '')
{
$v = [];
if (!empty($subject)) {
$v['subject'] = $subject;
}
if ($this->_server_id !== null && $subject != 'root') {
$v['server'] = $this->_server_id;
if ($this->_database !== null && $subject != 'server') {
$v['database'] = $this->_database;
if (isset($_REQUEST['schema']) && $subject != 'database') {
$v['schema'] = $_REQUEST['schema'];
}
}
}
//$this->prtrace($v);
return $v;
}
public function icon($icon)
{
if (is_string($icon)) {
$path = "/assets/images/themes/{$this->conf['theme']}/{$icon}";
if (file_exists(\BASE_PATH.$path.'.png')) {
return SUBFOLDER.$path.'.png';
}
if (file_exists(\BASE_PATH.$path.'.gif')) {
return SUBFOLDER.$path.'.gif';
}
if (file_exists(\BASE_PATH.$path.'.ico')) {
return SUBFOLDER.$path.'.ico';
}
$path = "/assets/images/themes/default/{$icon}";
if (file_exists(\BASE_PATH.$path.'.png')) {
return SUBFOLDER.$path.'.png';
}
if (file_exists(\BASE_PATH.$path.'.gif')) {
return SUBFOLDER.$path.'.gif';
}
if (file_exists(\BASE_PATH.$path.'.ico')) {
return SUBFOLDER.$path.'.ico';
}
} else {
// Icon from plugins
$path = "/plugins/{$icon[0]}/images/{$icon[1]}";
if (file_exists(\BASE_PATH.$path.'.png')) {
return SUBFOLDER.$path.'.png';
}
if (file_exists(\BASE_PATH.$path.'.gif')) {
return SUBFOLDER.$path.'.gif';
}
if (file_exists(\BASE_PATH.$path.'.ico')) {
return SUBFOLDER.$path.'.ico';
}
}
return '';
}
/**
* Function to escape command line parameters.
*
* @param string $str The string to escape
*
* @return string The escaped string
*/
public function escapeShellArg($str)
{
//$data = $this->getDatabaseAccessor();
$lang = $this->lang;
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
// Due to annoying PHP bugs, shell arguments cannot be escaped
// (command simply fails), so we cannot allow complex objects
// to be dumped.
if (preg_match('/^[_.[:alnum:]]+$/', $str)) {
return $str;
}
return $this->halt($lang['strcannotdumponwindows']);
}
return escapeshellarg($str);
}
/**
* Function to escape command line programs.
*
* @param string $str The string to escape
*
* @return string The escaped string
*/
public function escapeShellCmd($str)
{
$data = $this->getDatabaseAccessor();
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
$data->fieldClean($str);
return '"'.$str.'"';
}
return escapeshellcmd($str);
}
/**
* Save the given SQL script in the history
* of the database and server.
*
* @param string $script the SQL script to save
*/
public function saveScriptHistory($script)
{
list($usec, $sec) = explode(' ', microtime());
$time = ((float) $usec + (float) $sec);
$server = $this->container->server ? $this->container->server : $_REQUEST['server'];
$database = $this->container->database ? $this->container->database : $_REQUEST['database'];
$_SESSION['history'][$server][$database]["${time}"] = [
'query' => $script,
'paginate' => !isset($_REQUEST['paginate']) ? 'f' : 't',
'queryid' => $time,
];
}
}
|