Passed
Push — develop ( 2969a7...df24bb )
by Felipe
05:41 queued 11s
created

PC::debug()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 1
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 1
rs 10
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', '&amp;');
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
// Check to see if the configuration file exists, if not, explain
19
if (file_exists(BASE_PATH . '/config.inc.php')) {
20
    $conf = [];
21
    include BASE_PATH . '/config.inc.php';
22
} else {
23
    die('Configuration error: Copy config.inc.php-dist to config.inc.php and edit appropriately.');
24
}
25
$setSession = (defined('PHP_SESSION_ACTIVE') ? session_status() != PHP_SESSION_ACTIVE : !session_id()) && !headers_sent() && !ini_get('session.auto_start');
26
27
if ($setSession) {
28
    session_set_cookie_params(0, '/', null, isset($_SERVER['HTTPS']));
29
    session_name('PPA_ID');
30
31
    session_start();
32
}
33
34
if (isset($conf['error_log'])) {
35
    ini_set('error_log', BASE_PATH . '/' . $conf['error_log']);
36
}
37
$debugmode = (!isset($conf['debugmode'])) ? false : boolval($conf['debugmode']);
38
define('DEBUGMODE', $debugmode);
39
40
if (!defined('ADODB_ERROR_HANDLER_TYPE')) {
41
    define('ADODB_ERROR_HANDLER_TYPE', E_USER_ERROR);
42
}
43
44
if (!defined('ADODB_ERROR_HANDLER')) {
45
    define('ADODB_ERROR_HANDLER', '\PHPPgAdmin\ADOdbException::adodb_throw');
46
}
47
if (!is_writable(session_save_path())) {
48
    echo 'Session path "' . session_save_path() . '" is not writable for PHP!';
49
}
50
51
ini_set('display_errors', intval(DEBUGMODE));
52
ini_set('display_startup_errors', intval(DEBUGMODE));
53
if (DEBUGMODE) {
54
    ini_set('opcache.revalidate_freq', 0);
55
    error_reporting(E_ALL);
56
    if (array_key_exists('register_debuggers', $conf) && is_callable($conf['register_debuggers'])) {
57
        $conf['register_debuggers']();
58
    }
59
}
60
61
// Fetch App and DI Container
62
list($container, $app) = \PHPPgAdmin\ContainerUtils::createContainer();
63
64
if ($container instanceof \Psr\Container\ContainerInterface) {
65
    if (PHP_SAPI == 'cli-server') {
66
        $subfolder = '/index.php';
67
    } elseif (isset($conf['subfolder']) && is_string($conf['subfolder'])) {
68
        $subfolder = $conf['subfolder'];
69
    } else {
70
        $normalized_php_self = str_replace('/src/views', '', $container->environment->get('PHP_SELF'));
71
        $subfolder           = str_replace('/' . basename($normalized_php_self), '', $normalized_php_self);
72
    }
73
    define('SUBFOLDER', $subfolder);
74
} else {
75
    trigger_error("App Container must be an instance of \Psr\Container\ContainerInterface", E_USER_ERROR);
76
}
77
78
$container['requestobj']  = $container['request'];
79
$container['responseobj'] = $container['response'];
80
81
// This should be deprecated once we're sure no php scripts are required directly
82
$container->offsetSet('server', isset($_REQUEST['server']) ? $_REQUEST['server'] : null);
83
$container->offsetSet('database', isset($_REQUEST['database']) ? $_REQUEST['database'] : null);
84
$container->offsetSet('schema', isset($_REQUEST['schema']) ? $_REQUEST['schema'] : null);
85
86
$container['flash'] = function () {
87
    return new \Slim\Flash\Messages();
88
};
89
90
// Complete missing conf keys
91
$container['conf'] = function ($c) use ($conf) {
92
93
    //\Kint::dump($conf);
94
    // Plugins are removed
95
    $conf['plugins'] = [];
96
    if (!isset($conf['theme'])) {
97
        $conf['theme'] = 'default';
98
    }
99
    foreach ($conf['servers'] as &$server) {
100
        if (!isset($server['port'])) {
101
            $server['port'] = 5432;
102
        }
103
        if (!isset($server['sslmode'])) {
104
            $server['sslmode'] = 'unspecified';
105
        }
106
    }
107
108
    return $conf;
109
};
110
111
$container['lang'] = function ($c) {
112
    $translations = new \PHPPgAdmin\Translations($c);
113
114
    return $translations->lang;
115
};
116
117
$container['plugin_manager'] = function ($c) {
118
    $plugin_manager = new \PHPPgAdmin\PluginManager($c);
119
120
    return $plugin_manager;
121
};
122
123
// Create Misc class references
124
$container['misc'] = function ($c) {
125
    $misc = new \PHPPgAdmin\Misc($c);
126
127
    $conf = $c->get('conf');
128
129
    // 4. Check for theme by server/db/user
130
    $_server_info = $misc->getServerInfo();
131
132
    /* starting with PostgreSQL 9.0, we can set the application name */
133
    if (isset($_server_info['pgVersion']) && $_server_info['pgVersion'] >= 9) {
134
        putenv('PGAPPNAME=' . $c->get('settings')['appName'] . '_' . $c->get('settings')['appVersion']);
135
    }
136
137
    $_theme = $c->utils->getTheme($conf, $_server_info);
138
    if (!is_null($_theme)) {
139
        /* save the selected theme in cookie for a year */
140
        setcookie('ppaTheme', $_theme, time() + 31536000, '/');
141
        $_SESSION['ppaTheme'] = $_theme;
142
        $misc->setConf('theme', $_theme);
143
    }
144
145
    return $misc;
146
};
147
148
// Register Twig View helper
149
$container['view'] = function ($c) {
150
    $conf = $c->get('conf');
0 ignored issues
show
Unused Code introduced by
The assignment to $conf is dead and can be removed.
Loading history...
151
    $misc = $c->misc;
152
153
    $view = new \Slim\Views\Twig(BASE_PATH . '/assets/templates', [
154
        'cache'       => BASE_PATH . '/temp/twigcache',
155
        'auto_reload' => $c->get('settings')['debug'],
156
        'debug'       => $c->get('settings')['debug'],
157
    ]);
158
    $environment              = $c->get('environment');
159
    $base_script_trailing_str = substr($environment['SCRIPT_NAME'], 1);
160
    $request_basepath         = $c['request']->getUri()->getBasePath();
161
    // Instantiate and add Slim specific extension
162
    $basePath = rtrim(str_ireplace($base_script_trailing_str, '', $request_basepath), '/');
163
164
    $view->addExtension(new Slim\Views\TwigExtension($c['router'], $basePath));
165
166
    $view->offsetSet('subfolder', SUBFOLDER);
167
    $view->offsetSet('theme', $c->misc->getConf('theme'));
168
    $view->offsetSet('Favicon', $c->misc->icon('Favicon'));
169
    $view->offsetSet('Introduction', $c->misc->icon('Introduction'));
170
    $view->offsetSet('lang', $c->lang);
171
172
    $view->offsetSet('applangdir', $c->lang['applangdir']);
173
174
    $view->offsetSet('appName', $c->get('settings')['appName']);
175
176
    $misc->setView($view);
177
178
    return $view;
179
};
180
181
$container['haltHandler'] = function ($c) {
182
    return function ($request, $response, $exits, $status = 500) use ($c) {
183
        $title = 'PHPPgAdmin Error';
184
185
        $html = '<p>The application could not run because of the following error:</p>';
186
187
        $output = sprintf(
188
            "<html><head><meta http-equiv='Content-Type' content='text/html; charset=utf-8'>" .
189
            '<title>%s</title><style>' .
190
            'body{margin:0;padding:30px;font:12px/1.5 Helvetica,Arial,Verdana,sans-serif;}' .
191
            'h3{margin:0;font-size:28px;font-weight:normal;line-height:30px;}' .
192
            'span{display:inline-block;font-size:16px;}' .
193
            '</style></head><body><h3>%s</h3><p>%s</p><span>%s</span></body></html>',
194
            $title,
195
            $title,
196
            $html,
197
            implode('<br>', $exits)
198
        );
199
200
        $body = $response->getBody(); //new \Slim\Http\Body(fopen('php://temp', 'r+'));
201
        $body->write($output);
202
203
        return $response
204
            ->withStatus($status)
205
            ->withHeader('Content-type', 'text/html')
206
            ->withBody($body);
207
    };
208
};
209
210
// Set the requestobj and responseobj properties of the container
211
// as the value of $request and $response, which already contain the route
212
$app->add(new \PHPPgAdmin\Middleware\PopulateRequestResponse($container));
213
214
$container['action'] = isset($_REQUEST['action']) ? $_REQUEST['action'] : '';
215
216
if (!isset($msg)) {
217
    $msg = '';
218
}
219
220
$container['msg'] = $msg;
221