Passed
Push — develop ( 1026fb...945d40 )
by Felipe
04:55
created

PC   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 3
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
wmc 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A debug() 0 1 1
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
9
defined('BASE_PATH') or define('BASE_PATH', dirname(__DIR__));
10
11
define('THEME_PATH', BASE_PATH . '/assets/themes');
12
// Enforce PHP environment
13
ini_set('arg_separator.output', '&amp;');
14
15
if (!is_writable(BASE_PATH . '/temp')) {
16
    die('Your temp folder must have write permissions (use chmod 777 temp -R on linux)');
17
}
18
require_once BASE_PATH . '/vendor/autoload.php';
19
20
// base value for PHPConsole handler to avoid undefined variable
21
$phpConsoleHandler = null;
22
// Check to see if the configuration file exists, if not, explain
23
if (file_exists(BASE_PATH . '/config.inc.php')) {
24
    $conf = [];
25
    include BASE_PATH . '/config.inc.php';
26
} else {
27
    die('Configuration error: Copy config.inc.php-dist to config.inc.php and edit appropriately.');
28
}
29
30
if (isset($conf['error_log'])) {
31
    ini_set('error_log', BASE_PATH . '/' . $conf['error_log']);
32
}
33
$debugmode = (!isset($conf['debugmode'])) ? false : boolval($conf['debugmode']);
34
define('DEBUGMODE', $debugmode);
35
36
if (!defined('ADODB_ERROR_HANDLER_TYPE')) {
37
    define('ADODB_ERROR_HANDLER_TYPE', E_USER_ERROR);
38
}
39
40
if (!defined('ADODB_ERROR_HANDLER')) {
41
    define('ADODB_ERROR_HANDLER', '\PHPPgAdmin\ADOdbException::adodb_throw');
42
}
43
44
// Start session (if not auto-started)
45
if (!ini_get('session.auto_start')) {
46
    session_name('PPA_ID');
47
48
    $use_ssl = isset($_SERVER['HTTPS']) &&
49
    isset($conf['HTTPS_COOKIE']) &&
50
    boolval($conf['HTTPS_COOKIE']) !== false;
51
    session_set_cookie_params(0, null, null, $use_ssl, true);
52
53
    session_start();
54
}
55
56
// Global functions and polyfills
57
function maybeRenderIframes($c, $response, $subject, $query_string)
58
{
59
    $in_test = $c->view->offsetGet('in_test');
60
61
    if ($in_test === '1') {
62
        $className  = '\PHPPgAdmin\Controller\\' . ucfirst($subject) . 'Controller';
63
        $controller = new $className($c);
64
        return $controller->render();
65
    }
66
67
    $viewVars = [
68
        'url'            => '/src/views/' . $subject . ($query_string ? '?' . $query_string : ''),
69
        'headertemplate' => 'header.twig',
70
    ];
71
72
    return $c->view->render($response, 'iframe_view.twig', $viewVars);
73
};
74
// Dumb Polyfill to avoid errors with Kint
75
if (
0 ignored issues
show
Coding Style introduced by
First condition of a multi-line IF statement must directly follow the opening parenthesis
Loading history...
76
    class_exists('Kint')) {
77
    \Kint::$enabled_mode                = DEBUGMODE;
78
    Kint\Renderer\RichRenderer::$folder = false;
79
80
} else {
81
    class Kint
82
    {
83
        static $enabled_mode = false;
84
        static $aliases      = [];
85
        public static function dump() {}
0 ignored issues
show
Coding Style introduced by
Opening brace should be on a new line
Loading history...
Coding Style introduced by
Closing brace must be on a line by itself
Loading history...
86
    }
87
}
88
89
\Kint::$enabled_mode = DEBUGMODE;
90
function ddd(...$v)
91
{
92
    \Kint::dump(...$v);
0 ignored issues
show
Unused Code introduced by
The call to Kint::dump() has too many arguments starting with $v. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

92
    \Kint::/** @scrutinizer ignore-call */ 
93
           dump(...$v);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
93
    exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
94
}
95
96
\Kint::$aliases[] = 'ddd';
97
// Polyfill for PHPConsole
98
if (isset($conf['php_console']) &&
99
    class_exists('PhpConsole\Helper') &&
100
    $conf['php_console'] === true) {
101
    \PhpConsole\Helper::register(); // it will register global PC class
102
    if (!is_null($phpConsoleHandler)) {
0 ignored issues
show
introduced by
The condition is_null($phpConsoleHandler) is always true.
Loading history...
103
        $phpConsoleHandler->start(); // initialize phpConsoleHandler*/
104
    }
105
106
} else {
107
    class PC
108
    {
109
        public static function debug() {}
0 ignored issues
show
Coding Style introduced by
Opening brace should be on a new line
Loading history...
Coding Style introduced by
Closing brace must be on a line by itself
Loading history...
110
    }
111
}
112
113
ini_set('display_errors', intval(DEBUGMODE));
114
ini_set('display_startup_errors', intval(DEBUGMODE));
115
if (DEBUGMODE) {
116
    ini_set('opcache.revalidate_freq', 0);
117
    error_reporting(E_ALL);
118
}
119
120
// Fetch App and DI Container
121
list($container, $app) = \PHPPgAdmin\ContainerUtils::createContainer();
122
123
if ($container instanceof \Psr\Container\ContainerInterface) {
124
125
    if (PHP_SAPI == 'cli-server') {
126
        $subfolder = '/index.php';
127
    } elseif (isset($conf['subfolder']) && is_string($conf['subfolder'])) {
128
        $subfolder = $conf['subfolder'];
129
    } else {
130
        $normalized_php_self = str_replace('/src/views', '', $container->environment->get('PHP_SELF'));
131
        $subfolder           = str_replace('/' . basename($normalized_php_self), '', $normalized_php_self);
132
    }
133
    define('SUBFOLDER', $subfolder);
134
} else {
135
    trigger_error("App Container must be an instance of \Psr\Container\ContainerInterface", E_USER_ERROR);
136
}
137
138
$container['requestobj']  = $container['request'];
139
$container['responseobj'] = $container['response'];
140
141
// This should be deprecated once we're sure no php scripts are required directly
142
$container->offsetSet('server', isset($_REQUEST['server']) ? $_REQUEST['server'] : null);
143
$container->offsetSet('database', isset($_REQUEST['database']) ? $_REQUEST['database'] : null);
144
$container->offsetSet('schema', isset($_REQUEST['schema']) ? $_REQUEST['schema'] : null);
145
146
$container['flash'] = function () {
147
    return new \Slim\Flash\Messages();
148
};
149
150
// Complete missing conf keys
151
$container['conf'] = function ($c) use ($conf) {
152
153
    //\Kint::dump($conf);
154
    // Plugins are removed
155
    $conf['plugins'] = [];
156
    if (!isset($conf['theme'])) {
157
        $conf['theme'] = 'default';
158
    }
159
    foreach ($conf['servers'] as &$server) {
160
        if (!isset($server['port'])) {
161
            $server['port'] = 5432;
162
        }
163
        if (!isset($server['sslmode'])) {
164
            $server['sslmode'] = 'unspecified';
165
        }
166
    }
167
    return $conf;
168
};
169
170
$container['lang'] = function ($c) {
171
    $translations = new \PHPPgAdmin\Translations($c);
172
173
    return $translations->lang;
174
};
175
176
$container['plugin_manager'] = function ($c) {
177
    $plugin_manager = new \PHPPgAdmin\PluginManager($c);
178
    return $plugin_manager;
179
};
180
181
// Create Misc class references
182
$container['misc'] = function ($c) {
183
    $misc = new \PHPPgAdmin\Misc($c);
184
185
    $conf = $c->get('conf');
186
187
    // 4. Check for theme by server/db/user
188
    $_server_info = $misc->getServerInfo();
189
190
    /* starting with PostgreSQL 9.0, we can set the application name */
191
    if (isset($_server_info['pgVersion']) && $_server_info['pgVersion'] >= 9) {
192
        putenv('PGAPPNAME=' . $c->get('settings')['appName'] . '_' . $c->get('settings')['appVersion']);
193
    }
194
195
    $_theme = $c->utils->getTheme($conf, $_server_info);
196
    if (!is_null($_theme)) {
197
        /* save the selected theme in cookie for a year */
198
        setcookie('ppaTheme', $_theme, time() + 31536000, '/');
199
        $_SESSION['ppaTheme'] = $_theme;
200
        $misc->setConf('theme', $_theme);
201
    }
202
    return $misc;
203
};
204
205
// Register Twig View helper
206
$container['view'] = function ($c) {
207
    $conf = $c->get('conf');
0 ignored issues
show
Unused Code introduced by
The assignment to $conf is dead and can be removed.
Loading history...
208
    $misc = $c->misc;
209
210
    $view = new \Slim\Views\Twig(BASE_PATH . '/assets/templates', [
211
        'cache'       => BASE_PATH . '/temp/twigcache',
212
        'auto_reload' => $c->get('settings')['debug'],
213
        'debug'       => $c->get('settings')['debug'],
214
    ]);
215
    $environment              = $c->get('environment');
216
    $base_script_trailing_str = substr($environment['SCRIPT_NAME'], 1);
217
    $request_basepath         = $c['request']->getUri()->getBasePath();
218
    // Instantiate and add Slim specific extension
219
    $basePath = rtrim(str_ireplace($base_script_trailing_str, '', $request_basepath), '/');
220
221
    $view->addExtension(new Slim\Views\TwigExtension($c['router'], $basePath));
222
223
    $view->offsetSet('subfolder', SUBFOLDER);
224
    $view->offsetSet('theme', $c->misc->getConf('theme'));
225
    $view->offsetSet('Favicon', $c->misc->icon('Favicon'));
226
    $view->offsetSet('Introduction', $c->misc->icon('Introduction'));
227
    $view->offsetSet('lang', $c->lang);
228
229
    $view->offsetSet('applangdir', $c->lang['applangdir']);
230
231
    $view->offsetSet('appName', $c->get('settings')['appName']);
232
233
    $misc->setView($view);
234
    return $view;
235
};
236
237
$container['haltHandler'] = function ($c) {
238
    return function ($request, $response, $exits, $status = 500) use ($c) {
239
        $title = 'PHPPgAdmin Error';
240
241
        $html = '<p>The application could not run because of the following error:</p>';
242
243
        $output = sprintf(
244
            "<html><head><meta http-equiv='Content-Type' content='text/html; charset=utf-8'>" .
245
            '<title>%s</title><style>' .
246
            'body{margin:0;padding:30px;font:12px/1.5 Helvetica,Arial,Verdana,sans-serif;}' .
247
            'h3{margin:0;font-size:28px;font-weight:normal;line-height:30px;}' .
248
            'span{display:inline-block;font-size:16px;}' .
249
            '</style></head><body><h3>%s</h3><p>%s</p><span>%s</span></body></html>',
250
            $title,
251
            $title,
252
            $html,
253
            implode('<br>', $exits)
254
        );
255
256
        $body = $response->getBody(); //new \Slim\Http\Body(fopen('php://temp', 'r+'));
257
        $body->write($output);
258
259
        return $response
260
            ->withStatus($status)
261
            ->withHeader('Content-type', 'text/html')
262
            ->withBody($body);
263
    };
264
};
265
266
// Set the requestobj and responseobj properties of the container
267
// as the value of $request and $response, which already contain the route
268
$app->add(new \PHPPgAdmin\Middleware\PopulateRequestResponse($container));
269
270
$container['action'] = isset($_REQUEST['action']) ? $_REQUEST['action'] : '';
271
272
if (!isset($msg)) {
273
    $msg = '';
274
}
275
276
$container['msg'] = $msg;
277