Passed
Push — master ( 442876...4ec1bc )
by Felipe
15:55 queued 10:33
created

ServersController::doTree()   C

Complexity

Conditions 8
Paths 8

Size

Total Lines 42
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 20
nc 8
nop 0
dl 0
loc 42
rs 5.3846
c 0
b 0
f 0
1
<?php
2
0 ignored issues
show
Coding Style introduced by
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
 */
5 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
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
Unused Code introduced by
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 = '')
1 ignored issue
show
Coding Style introduced by
Missing function doc comment
Loading history...
70
    {
71
        $lang = $this->lang;
72
73
        $this->printTabs('root', 'servers');
74
        $this->printMsg($msg);
75
        $group = isset($_GET['group']) ? $_GET['group'] : false;
76
77
        $groups  = $this->misc->getServersGroups(true, $group);
78
        $columns = [
79
            'group' => [
80
                'title' => $lang['strgroup'],
81
                'field' => Decorator::field('desc'),
82
                'url'   => 'servers.php?',
83
                'vars'  => ['group' => 'id'],
84
            ],
85
        ];
86
        $actions = [];
87
        if ((false !== $group) && (isset($this->conf['srv_groups'][$group])) && ($groups->recordCount() > 0)) {
88
            $this->printTitle(sprintf($lang['strgroupgroups'], htmlentities($this->conf['srv_groups'][$group]['desc'], ENT_QUOTES, 'UTF-8')));
89
        }
90
        $this->printTable($groups, $columns, $actions, $this->table_place);
91
        $servers = $this->misc->getServers(true, $group);
92
93
        $columns = [
94
            'server'   => [
95
                'title' => $lang['strserver'],
96
                'field' => Decorator::field('desc'),
97
                'url'   => \SUBFOLDER . '/redirect/server?',
98
                'vars'  => ['server' => 'id'],
99
            ],
100
            'host'     => [
101
                'title' => $lang['strhost'],
102
                'field' => Decorator::field('host'),
103
            ],
104
            'port'     => [
105
                'title' => $lang['strport'],
106
                'field' => Decorator::field('port'),
107
            ],
108
            'username' => [
109
                'title' => $lang['strusername'],
110
                'field' => Decorator::field('username'),
111
            ],
112
            'actions'  => [
113
                'title' => $lang['stractions'],
114
            ],
115
        ];
116
117
        $actions = [
118
            'logout' => [
119
                'content' => $lang['strlogout'],
120
                'attr'    => [
121
                    'href' => [
122
                        'url'     => 'servers.php',
123
                        'urlvars' => [
124
                            'action'       => 'logout',
125
                            'logoutServer' => Decorator::field('id'),
126
                        ],
127
                    ],
128
                ],
129
            ],
130
        ];
131
132
        $svPre = function (&$rowdata) use ($actions) {
133
            $actions['logout']['disable'] = empty($rowdata->fields['username']);
134
135
            return $actions;
136
        };
137
138
        if ((false !== $group) and isset($this->conf['srv_groups'][$group])) {
139
            $this->printTitle(sprintf($lang['strgroupservers'], htmlentities($this->conf['srv_groups'][$group]['desc'], ENT_QUOTES, 'UTF-8')), null);
140
            $actions['logout']['attr']['href']['urlvars']['group'] = $group;
141
        }
142
        echo $this->printTable($servers, $columns, $actions, $this->table_place, $lang['strnoobjects'], $svPre);
143
    }
144
145
    public function doTree()
1 ignored issue
show
Coding Style introduced by
Missing function doc comment
Loading history...
146
    {
147
        $nodes    = [];
148
        $group_id = isset($_GET['group']) ? $_GET['group'] : false;
149
150
        // root with srv_groups
151
        if (isset($this->conf['srv_groups']) and count($this->conf['srv_groups']) > 0
152
            and false === $group_id) {
1 ignored issue
show
Coding Style introduced by
Closing parenthesis of a multi-line IF statement must be on a new line
Loading history...
153
            $nodes = $this->misc->getServersGroups(true);
154
        } elseif (isset($this->conf['srv_groups']) and false !== $group_id) {
155
            // group subtree
156
            if ('all' !== $group_id) {
157
                $nodes = $this->misc->getServersGroups(false, $group_id);
158
            }
159
160
            $nodes = array_merge($nodes, $this->misc->getServers(false, $group_id));
161
            $nodes = new \PHPPgAdmin\ArrayRecordSet($nodes);
162
        } else {
163
            // no srv_group
164
            $nodes = $this->misc->getServers(true, false);
165
        }
166
167
        $reqvars = $this->misc->getRequestVars('server');
0 ignored issues
show
Unused Code introduced by
The assignment to $reqvars is dead and can be removed.
Loading history...
168
169
        //$this->prtrace($reqvars);
170
171
        $attrs = [
172
            'text'    => Decorator::field('desc'),
173
            // Show different icons for logged in/out
174
            'icon'    => Decorator::field('icon'),
175
            'toolTip' => Decorator::field('id'),
176
            'action'  => Decorator::field('action'),
177
            // Only create a branch url if the user has
178
            // logged into the server.
179
            'branch'  => Decorator::field('branch'),
180
        ];
181
        /*$this->prtrace([
182
        'nodes'   => $nodes,
183
        'attrs'   => $attrs,
184
        'section' => $this->section,
185
        ]);*/
186
        return $this->printTree($nodes, $attrs, $this->section);
187
    }
188
189
    public function doLogout()
1 ignored issue
show
Coding Style introduced by
Missing function doc comment
Loading history...
190
    {
191
        $plugin_manager = $this->plugin_manager;
192
        $lang           = $this->lang;
193
        $this->misc     = $this->misc;
194
        $conf           = $this->conf;
0 ignored issues
show
Unused Code introduced by
The assignment to $conf is dead and can be removed.
Loading history...
195
        $data           = $this->misc->getDatabaseAccessor();
0 ignored issues
show
Unused Code introduced by
The assignment to $data is dead and can be removed.
Loading history...
196
197
        $plugin_manager->do_hook('logout', $_REQUEST['logoutServer']);
198
199
        $server_info = $this->misc->getServerInfo($_REQUEST['logoutServer']);
200
        $this->misc->setServerInfo(null, null, $_REQUEST['logoutServer']);
201
202
        unset($_SESSION['sharedUsername'], $_SESSION['sharedPassword']);
203
204
        $this->misc->setReloadBrowser(true);
205
206
        echo sprintf($lang['strlogoutmsg'], $server_info['desc']);
207
    }
208
}
209