Passed
Push — master ( 5b06fe...edcd19 )
by Felipe
03:43
created

index.php (1 issue)

1
<?php
2
3
/**
4
 * Single entrypoint of the app
5
 */
6
require_once './src/lib.inc.php';
7
8
$app->post('/redirect[/{subject}]', function ($request, $response, $args) {
9
10
    $body         = $response->getBody();
11
    $query_string = $request->getUri()->getQuery();
12
    $misc         = $this->misc;
13
14
    $loginShared   = $request->getParsedBodyParam('loginShared');
15
    $loginServer   = $request->getParsedBodyParam('loginServer');
16
    $loginUsername = $request->getParsedBodyParam('loginUsername');
17
    $loginPassword = $request->getParsedBodyParam('loginPassword_' . md5($loginServer));
18
19
    // If login action is set, then set session variables
20
    if (boolval($loginServer) && boolval($loginUsername) && $loginPassword !== null) {
21
22
        $_server_info = $this->misc->getServerInfo($loginServer);
23
24
        $_server_info['username'] = $loginUsername;
25
        $_server_info['password'] = $loginPassword;
26
27
        $this->misc->setServerInfo(null, $_server_info, $loginServer);
28
29
        $data = $misc->getDatabaseAccessor();
30
31
        if ($data === null) {
32
            $login_controller = new \PHPPgAdmin\Controller\LoginController($this, true);
33
            $body->write($login_controller->doLoginForm($misc->getErrorMsg()));
34
            return $response;
35
        }
36
        // Check for shared credentials
37
        if ($loginShared !== null) {
38
            $_SESSION['sharedUsername'] = $loginUsername;
39
            $_SESSION['sharedPassword'] = $loginPassword;
40
        }
41
42
        $misc->setReloadBrowser(true);
43
        $all_db_controller = new \PHPPgAdmin\Controller\AlldbController($this);
44
        return $all_db_controller->render();
45
46
    } else {
47
48
        $_server_info = $this->misc->getServerInfo();
49
50
        if (!isset($_server_info['username'])) {
51
            $destinationurl = $this->utils->getDestinationWithLastTab($subject);
52
            return $response->withStatus(302)->withHeader('Location', $destinationurl);
53
        }
54
    }
55
56
});
57
58
$app->get('/redirect[/{subject}]', function ($request, $response, $args) {
59
60
    $subject        = (isset($args['subject'])) ? $args['subject'] : 'root';
61
    $destinationurl = $this->utils->getDestinationWithLastTab($subject);
62
    return $response->withStatus(302)->withHeader('Location', $destinationurl);
63
64
});
65
66
$app->get('/src/views/browser', function ($request, $response, $args) {
67
68
    $controller = new \PHPPgAdmin\Controller\BrowserController($this, true);
69
    return $controller->render();
70
});
71
72
$app->get('/src/views/login', function ($request, $response, $args) {
73
74
    $controller = new \PHPPgAdmin\Controller\LoginController($this, true);
75
    return $controller->render();
76
});
77
78
$app->get('/src/views/servers', function ($request, $response, $args) {
79
80
    $controller = new \PHPPgAdmin\Controller\ServersController($this, true);
81
    return $controller->render();
82
});
83
84
$app->get('/src/views/intro', function ($request, $response, $args) {
85
86
    $controller = new \PHPPgAdmin\Controller\IntroController($this, true);
87
    return $controller->render();
88
});
89
90
$app->map(['GET', 'POST'], '/src/views/{subject}', function ($request, $response, $args) {
91
92
    $subject = $args['subject'];
93
94
    $className  = '\PHPPgAdmin\Controller\\' . ucfirst($subject) . 'Controller';
95
    $controller = new $className($this);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $this seems to be never defined.
Loading history...
96
    return $controller->render();
97
});
98
99
$app->get('/[{subject}]', function ($request, $response, $args) {
100
101
    $subject      = (isset($args['subject'])) ? $args['subject'] : 'intro';
102
    $_server_info = $this->misc->getServerInfo();
103
    $query_string = $request->getUri()->getQuery();
104
    $server_id    = $request->getQueryParam('server');
105
106
    if (!isset($_server_info['username']) && ($subject === 'server' || $subject === 'root')) {
107
        $subject = 'login';
108
    }
109
110
    if ($subject === 'login' && $server_id === null) {
111
        $subject = 'servers';
112
    }
113
114
    $viewVars = [
115
        'url'            => '/src/views/' . $subject . ($query_string ? '?' . $query_string : ''),
116
        'headertemplate' => 'header.twig',
117
    ];
118
119
    return $this->view->render($response, 'iframe_view.twig', $viewVars);
120
});
121
122
// Run app
123
$app->run();
124