1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Function library read in upon startup. |
5
|
|
|
* |
6
|
|
|
* Release: lib.inc.php,v 1.123 2008/04/06 01:10:35 xzilla Exp $ |
7
|
|
|
*/ |
8
|
|
|
defined('BASE_PATH') or define('BASE_PATH', dirname(__DIR__)); |
9
|
|
|
|
10
|
|
|
define('THEME_PATH', BASE_PATH . '/assets/themes'); |
11
|
|
|
// Enforce PHP environment |
12
|
|
|
ini_set('arg_separator.output', '&'); |
13
|
|
|
|
14
|
|
|
if (!is_writable(BASE_PATH . '/temp')) { |
15
|
|
|
die('Your temp folder must have write permissions (use chmod 777 temp -R on linux)'); |
16
|
|
|
} |
17
|
|
|
require_once BASE_PATH . '/vendor/autoload.php'; |
18
|
|
|
|
19
|
|
|
// base value for PHPConsole handler to avoid undefined variable |
20
|
|
|
$phpConsoleHandler = null; |
21
|
|
|
// Check to see if the configuration file exists, if not, explain |
22
|
|
|
if (file_exists(BASE_PATH . '/config.inc.php')) { |
23
|
|
|
$conf = []; |
24
|
|
|
include BASE_PATH . '/config.inc.php'; |
25
|
|
|
} else { |
26
|
|
|
die('Configuration error: Copy config.inc.php-dist to config.inc.php and edit appropriately.'); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
if (isset($conf['error_log'])) { |
30
|
|
|
ini_set('error_log', BASE_PATH . '/' . $conf['error_log']); |
31
|
|
|
} |
32
|
|
|
$debugmode = (!isset($conf['debugmode'])) ? false : boolval($conf['debugmode']); |
33
|
|
|
define('DEBUGMODE', $debugmode); |
34
|
|
|
|
35
|
|
|
if (!defined('ADODB_ERROR_HANDLER_TYPE')) { |
36
|
|
|
define('ADODB_ERROR_HANDLER_TYPE', E_USER_ERROR); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
if (!defined('ADODB_ERROR_HANDLER')) { |
40
|
|
|
define('ADODB_ERROR_HANDLER', '\PHPPgAdmin\ADOdbException::adodb_throw'); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
// Start session (if not auto-started) |
44
|
|
|
if (!ini_get('session.auto_start')) { |
45
|
|
|
session_name('PPA_ID'); |
46
|
|
|
|
47
|
|
|
$use_ssl = isset($_SERVER['HTTPS']) && |
48
|
|
|
isset($conf['HTTPS_COOKIE']) && |
49
|
|
|
boolval($conf['HTTPS_COOKIE']) !== false; |
50
|
|
|
session_set_cookie_params(0, null, null, $use_ssl, true); |
51
|
|
|
|
52
|
|
|
session_start(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
// Dumb Polyfill to avoid errors with Kint |
56
|
|
|
if ( |
|
|
|
|
57
|
|
|
class_exists('Kint')) { |
58
|
|
|
\Kint::$enabled_mode = DEBUGMODE; |
59
|
|
|
Kint\Renderer\RichRenderer::$folder = false; |
60
|
|
|
} else { |
61
|
|
|
class Kint |
62
|
|
|
{ |
63
|
|
|
public static $enabled_mode = false; |
64
|
|
|
public static $aliases = []; |
65
|
|
|
|
66
|
|
|
public static function dump() {} |
|
|
|
|
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
\Kint::$enabled_mode = DEBUGMODE; |
71
|
|
|
function ddd(...$v) |
72
|
|
|
{ |
73
|
|
|
\Kint::dump(...$v); |
|
|
|
|
74
|
|
|
exit; |
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
\Kint::$aliases[] = 'ddd'; |
78
|
|
|
// Polyfill for PHPConsole |
79
|
|
|
if (isset($conf['php_console']) && |
80
|
|
|
class_exists('\PhpConsole\Helper') && |
81
|
|
|
$conf['php_console'] === true) { |
82
|
|
|
\PhpConsole\Helper::register(); // it will register global PC class |
83
|
|
|
if (!is_null($phpConsoleHandler)) { |
|
|
|
|
84
|
|
|
$phpConsoleHandler->start(); // initialize phpConsoleHandler*/ |
85
|
|
|
} |
86
|
|
|
} else { |
87
|
|
|
class PC |
88
|
|
|
{ |
89
|
|
|
public static function debug() {} |
|
|
|
|
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
//\PC::dump($phpConsoleHandler); |
93
|
|
|
|
94
|
|
|
ini_set('display_errors', intval(DEBUGMODE)); |
95
|
|
|
ini_set('display_startup_errors', intval(DEBUGMODE)); |
96
|
|
|
if (DEBUGMODE) { |
97
|
|
|
ini_set('opcache.revalidate_freq', 0); |
98
|
|
|
error_reporting(E_ALL); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
// Fetch App and DI Container |
102
|
|
|
list($container, $app) = \PHPPgAdmin\ContainerUtils::createContainer(); |
103
|
|
|
|
104
|
|
|
if ($container instanceof \Psr\Container\ContainerInterface) { |
105
|
|
|
if (PHP_SAPI == 'cli-server') { |
106
|
|
|
$subfolder = '/index.php'; |
107
|
|
|
} elseif (isset($conf['subfolder']) && is_string($conf['subfolder'])) { |
108
|
|
|
$subfolder = $conf['subfolder']; |
109
|
|
|
} else { |
110
|
|
|
$normalized_php_self = str_replace('/src/views', '', $container->environment->get('PHP_SELF')); |
111
|
|
|
$subfolder = str_replace('/' . basename($normalized_php_self), '', $normalized_php_self); |
112
|
|
|
} |
113
|
|
|
define('SUBFOLDER', $subfolder); |
114
|
|
|
} else { |
115
|
|
|
trigger_error("App Container must be an instance of \Psr\Container\ContainerInterface", E_USER_ERROR); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
$container['requestobj'] = $container['request']; |
119
|
|
|
$container['responseobj'] = $container['response']; |
120
|
|
|
|
121
|
|
|
// This should be deprecated once we're sure no php scripts are required directly |
122
|
|
|
$container->offsetSet('server', isset($_REQUEST['server']) ? $_REQUEST['server'] : null); |
123
|
|
|
$container->offsetSet('database', isset($_REQUEST['database']) ? $_REQUEST['database'] : null); |
124
|
|
|
$container->offsetSet('schema', isset($_REQUEST['schema']) ? $_REQUEST['schema'] : null); |
125
|
|
|
|
126
|
|
|
$container['flash'] = function () { |
127
|
|
|
return new \Slim\Flash\Messages(); |
128
|
|
|
}; |
129
|
|
|
|
130
|
|
|
// Complete missing conf keys |
131
|
|
|
$container['conf'] = function ($c) use ($conf) { |
132
|
|
|
|
133
|
|
|
//\Kint::dump($conf); |
134
|
|
|
// Plugins are removed |
135
|
|
|
$conf['plugins'] = []; |
136
|
|
|
if (!isset($conf['theme'])) { |
137
|
|
|
$conf['theme'] = 'default'; |
138
|
|
|
} |
139
|
|
|
foreach ($conf['servers'] as &$server) { |
140
|
|
|
if (!isset($server['port'])) { |
141
|
|
|
$server['port'] = 5432; |
142
|
|
|
} |
143
|
|
|
if (!isset($server['sslmode'])) { |
144
|
|
|
$server['sslmode'] = 'unspecified'; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return $conf; |
149
|
|
|
}; |
150
|
|
|
|
151
|
|
|
$container['lang'] = function ($c) { |
152
|
|
|
$translations = new \PHPPgAdmin\Translations($c); |
153
|
|
|
|
154
|
|
|
return $translations->lang; |
155
|
|
|
}; |
156
|
|
|
|
157
|
|
|
$container['plugin_manager'] = function ($c) { |
158
|
|
|
$plugin_manager = new \PHPPgAdmin\PluginManager($c); |
159
|
|
|
|
160
|
|
|
return $plugin_manager; |
161
|
|
|
}; |
162
|
|
|
|
163
|
|
|
// Create Misc class references |
164
|
|
|
$container['misc'] = function ($c) { |
165
|
|
|
$misc = new \PHPPgAdmin\Misc($c); |
166
|
|
|
|
167
|
|
|
$conf = $c->get('conf'); |
168
|
|
|
|
169
|
|
|
// 4. Check for theme by server/db/user |
170
|
|
|
$_server_info = $misc->getServerInfo(); |
171
|
|
|
|
172
|
|
|
/* starting with PostgreSQL 9.0, we can set the application name */ |
173
|
|
|
if (isset($_server_info['pgVersion']) && $_server_info['pgVersion'] >= 9) { |
174
|
|
|
putenv('PGAPPNAME=' . $c->get('settings')['appName'] . '_' . $c->get('settings')['appVersion']); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
$_theme = $c->utils->getTheme($conf, $_server_info); |
178
|
|
|
if (!is_null($_theme)) { |
179
|
|
|
/* save the selected theme in cookie for a year */ |
180
|
|
|
setcookie('ppaTheme', $_theme, time() + 31536000, '/'); |
181
|
|
|
$_SESSION['ppaTheme'] = $_theme; |
182
|
|
|
$misc->setConf('theme', $_theme); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
return $misc; |
186
|
|
|
}; |
187
|
|
|
|
188
|
|
|
// Register Twig View helper |
189
|
|
|
$container['view'] = function ($c) { |
190
|
|
|
$conf = $c->get('conf'); |
|
|
|
|
191
|
|
|
$misc = $c->misc; |
192
|
|
|
|
193
|
|
|
$view = new \Slim\Views\Twig(BASE_PATH . '/assets/templates', [ |
194
|
|
|
'cache' => BASE_PATH . '/temp/twigcache', |
195
|
|
|
'auto_reload' => $c->get('settings')['debug'], |
196
|
|
|
'debug' => $c->get('settings')['debug'], |
197
|
|
|
]); |
198
|
|
|
$environment = $c->get('environment'); |
199
|
|
|
$base_script_trailing_str = substr($environment['SCRIPT_NAME'], 1); |
200
|
|
|
$request_basepath = $c['request']->getUri()->getBasePath(); |
201
|
|
|
// Instantiate and add Slim specific extension |
202
|
|
|
$basePath = rtrim(str_ireplace($base_script_trailing_str, '', $request_basepath), '/'); |
203
|
|
|
|
204
|
|
|
$view->addExtension(new Slim\Views\TwigExtension($c['router'], $basePath)); |
205
|
|
|
|
206
|
|
|
$view->offsetSet('subfolder', SUBFOLDER); |
207
|
|
|
$view->offsetSet('theme', $c->misc->getConf('theme')); |
208
|
|
|
$view->offsetSet('Favicon', $c->misc->icon('Favicon')); |
209
|
|
|
$view->offsetSet('Introduction', $c->misc->icon('Introduction')); |
210
|
|
|
$view->offsetSet('lang', $c->lang); |
211
|
|
|
|
212
|
|
|
$view->offsetSet('applangdir', $c->lang['applangdir']); |
213
|
|
|
|
214
|
|
|
$view->offsetSet('appName', $c->get('settings')['appName']); |
215
|
|
|
|
216
|
|
|
$misc->setView($view); |
217
|
|
|
|
218
|
|
|
return $view; |
219
|
|
|
}; |
220
|
|
|
|
221
|
|
|
$container['haltHandler'] = function ($c) { |
222
|
|
|
return function ($request, $response, $exits, $status = 500) use ($c) { |
223
|
|
|
$title = 'PHPPgAdmin Error'; |
224
|
|
|
|
225
|
|
|
$html = '<p>The application could not run because of the following error:</p>'; |
226
|
|
|
|
227
|
|
|
$output = sprintf( |
228
|
|
|
"<html><head><meta http-equiv='Content-Type' content='text/html; charset=utf-8'>" . |
229
|
|
|
'<title>%s</title><style>' . |
230
|
|
|
'body{margin:0;padding:30px;font:12px/1.5 Helvetica,Arial,Verdana,sans-serif;}' . |
231
|
|
|
'h3{margin:0;font-size:28px;font-weight:normal;line-height:30px;}' . |
232
|
|
|
'span{display:inline-block;font-size:16px;}' . |
233
|
|
|
'</style></head><body><h3>%s</h3><p>%s</p><span>%s</span></body></html>', |
234
|
|
|
$title, |
235
|
|
|
$title, |
236
|
|
|
$html, |
237
|
|
|
implode('<br>', $exits) |
238
|
|
|
); |
239
|
|
|
|
240
|
|
|
$body = $response->getBody(); //new \Slim\Http\Body(fopen('php://temp', 'r+')); |
241
|
|
|
$body->write($output); |
242
|
|
|
|
243
|
|
|
return $response |
244
|
|
|
->withStatus($status) |
245
|
|
|
->withHeader('Content-type', 'text/html') |
246
|
|
|
->withBody($body); |
247
|
|
|
}; |
248
|
|
|
}; |
249
|
|
|
|
250
|
|
|
// Set the requestobj and responseobj properties of the container |
251
|
|
|
// as the value of $request and $response, which already contain the route |
252
|
|
|
$app->add(new \PHPPgAdmin\Middleware\PopulateRequestResponse($container)); |
253
|
|
|
|
254
|
|
|
$container['action'] = isset($_REQUEST['action']) ? $_REQUEST['action'] : ''; |
255
|
|
|
|
256
|
|
|
if (!isset($msg)) { |
257
|
|
|
$msg = ''; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
$container['msg'] = $msg; |
261
|
|
|
|