Passed
Pull Request — develop (#92)
by Felipe
04:25
created

src/controllers/ServersController.php (4 issues)

1
<?php
2
0 ignored issues
show
You must use "/**" style comments for a file comment
Loading history...
3
/*
4
 * PHPPgAdmin v6.0.0-beta.30
5
 */
6
7
namespace PHPPgAdmin\Controller;
8
9
use PHPPgAdmin\Decorators\Decorator;
10
11
/**
12
 * Base controller class.
13
 */
14
class ServersController extends BaseController
15
{
16
    public $controller_name = 'ServersController';
17
    public $table_place     = 'servers-servers';
18
    public $section         = 'servers';
19
    public $query           = '';
20
    public $subject         = '';
21
    public $start_time;
22
    public $duration;
23
24
    /**
25
     * Default method to render the controller according to the action parameter.
26
     */
27
    public function render()
28
    {
29
        $lang = $this->lang;
0 ignored issues
show
The assignment to $lang is dead and can be removed.
Loading history...
30
31
        $action = $this->action;
32
33
        if ('tree' == $action) {
34
            return $this->doTree();
35
        }
36
37
        $msg = $this->msg;
38
39
        $server_html = $this->printHeader($this->lang['strservers'], null, false);
40
        $server_html .= $this->printBody(false);
41
        $server_html .= $this->printTrail('root', false);
42
43
        ob_start();
44
        switch ($action) {
45
            case 'logout':
46
                $this->doLogout();
47
48
                break;
49
            default:
50
                $this->doDefault($msg);
51
52
                break;
53
        }
54
55
        $server_html .= ob_get_clean();
56
57
        $server_html .= $this->printFooter(false);
58
59
        if (null === $this->container->requestobj->getAttribute('route')) {
60
            echo $server_html;
61
        } else {
62
            $body = $this->container->responseobj->getBody();
63
            $body->write($server_html);
64
65
            return $this->container->responseobj;
66
        }
67
    }
68
69
    public function doDefault($msg = '')
70
    {
71
        $lang = $this->lang;
72
73
        $data = $this->misc->getDatabaseAccessor();
74
75
        $this->printTabs('root', 'servers');
76
        $this->printMsg($msg);
77
        $group = isset($_GET['group']) ? $_GET['group'] : false;
78
79
        $groups  = $this->misc->getServersGroups(true, $group);
80
        $columns = [
81
            'group' => [
82
                'title' => $lang['strgroup'],
83
                'field' => Decorator::field('desc'),
84
                'url'   => 'servers.php?',
85
                'vars'  => ['group' => 'id'],
86
            ],
87
        ];
88
        $actions = [];
89
        if ((false !== $group) && (isset($conf['srv_groups'][$group])) && ($groups->recordCount() > 0)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $conf seems to never exist and therefore isset should always be false.
Loading history...
90
            $this->printTitle(sprintf($lang['strgroupgroups'], htmlentities($conf['srv_groups'][$group]['desc'], ENT_QUOTES, 'UTF-8')));
91
        }
92
        $this->printTable($groups, $columns, $actions, $this->table_place);
93
        $servers = $this->misc->getServers(true, $group);
94
95
        $columns = [
96
            'server'   => [
97
                'title' => $lang['strserver'],
98
                'field' => Decorator::field('desc'),
99
                'url'   => \SUBFOLDER . '/redirect/server?',
100
                'vars'  => ['server' => 'id'],
101
            ],
102
            'host'     => [
103
                'title' => $lang['strhost'],
104
                'field' => Decorator::field('host'),
105
            ],
106
            'port'     => [
107
                'title' => $lang['strport'],
108
                'field' => Decorator::field('port'),
109
            ],
110
            'username' => [
111
                'title' => $lang['strusername'],
112
                'field' => Decorator::field('username'),
113
            ],
114
            'actions'  => [
115
                'title' => $lang['stractions'],
116
            ],
117
        ];
118
119
        $actions = [
120
            'logout' => [
121
                'content' => $lang['strlogout'],
122
                'attr'    => [
123
                    'href' => [
124
                        'url'     => 'servers.php',
125
                        'urlvars' => [
126
                            'action'       => 'logout',
127
                            'logoutServer' => Decorator::field('id'),
128
                        ],
129
                    ],
130
                ],
131
            ],
132
        ];
133
134
        $svPre = function (&$rowdata) use ($actions) {
135
            $actions['logout']['disable'] = empty($rowdata->fields['username']);
136
137
            return $actions;
138
        };
139
140
        if ((false !== $group) and isset($conf['srv_groups'][$group])) {
141
            $this->printTitle(sprintf($lang['strgroupservers'], htmlentities($conf['srv_groups'][$group]['desc'], ENT_QUOTES, 'UTF-8')), null);
142
            $actions['logout']['attr']['href']['urlvars']['group'] = $group;
143
        }
144
        echo $this->printTable($servers, $columns, $actions, $this->table_place, $lang['strnoobjects'], $svPre);
145
    }
146
147
    public function doTree()
148
    {
149
        $nodes    = [];
150
        $group_id = isset($_GET['group']) ? $_GET['group'] : false;
151
152
        // root with srv_groups
153
        if (isset($conf['srv_groups']) and count($conf['srv_groups']) > 0
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $conf seems to never exist and therefore isset should always be false.
Loading history...
154
            and false === $group_id) {
155
            $nodes = $this->misc->getServersGroups(true);
156
        } elseif (isset($conf['srv_groups']) and false !== $group_id) {
157
            // group subtree
158
            if ('all' !== $group_id) {
159
                $nodes = $this->misc->getServersGroups(false, $group_id);
160
            }
161
162
            $nodes = array_merge($nodes, $this->misc->getServers(false, $group_id));
163
            $nodes = new \PHPPgAdmin\ArrayRecordSet($nodes);
164
        } else {
165
            // no srv_group
166
            $nodes = $this->misc->getServers(true, false);
167
        }
168
169
        $reqvars = $this->misc->getRequestVars('server');
170
171
        //$this->prtrace($reqvars);
172
173
        $attrs = [
174
            'text'    => Decorator::field('desc'),
175
            // Show different icons for logged in/out
176
            'icon'    => Decorator::field('icon'),
177
            'toolTip' => Decorator::field('id'),
178
            'action'  => Decorator::field('action'),
179
            // Only create a branch url if the user has
180
            // logged into the server.
181
            'branch'  => Decorator::field('branch'),
182
        ];
183
        /*$this->prtrace([
184
        'nodes'   => $nodes,
185
        'attrs'   => $attrs,
186
        'section' => $this->section,
187
        ]);*/
188
        return $this->printTree($nodes, $attrs, $this->section);
189
    }
190
191
    public function doLogout()
192
    {
193
        $plugin_manager = $this->plugin_manager;
194
        $lang           = $this->lang;
195
        $this->misc     = $this->misc;
196
        $conf           = $this->conf;
197
        $data           = $this->misc->getDatabaseAccessor();
198
199
        $plugin_manager->do_hook('logout', $_REQUEST['logoutServer']);
200
201
        $server_info = $this->misc->getServerInfo($_REQUEST['logoutServer']);
202
        $this->misc->setServerInfo(null, null, $_REQUEST['logoutServer']);
203
204
        unset($_SESSION['sharedUsername'], $_SESSION['sharedPassword']);
205
206
        $this->misc->setReloadBrowser(true);
207
208
        echo sprintf($lang['strlogoutmsg'], $server_info['desc']);
209
    }
210
}
211