Passed
Push — develop ( a518ac...108247 )
by Felipe
04:30
created

ServersController   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 269
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 30
dl 0
loc 269
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A doLogout() 0 18 1
C doTree() 0 42 8
B render() 0 39 4
C doDefault() 0 74 7
C getServersGroups() 0 66 10
1
<?php
2
3
/**
4
 * PHPPgAdmin v6.0.0-beta.33
5
 */
6
7
namespace PHPPgAdmin\Controller;
8
9
use PHPPgAdmin\Decorators\Decorator;
10
11
/**
12
 * Base controller class.
13
 *
14
 * @package PHPPgAdmin
15
 */
16
class ServersController extends BaseController
17
{
18
    public $controller_name = 'ServersController';
19
    public $table_place     = 'servers-servers';
20
    public $section         = 'servers';
21
    public $query           = '';
22
    public $subject         = '';
23
    public $start_time;
24
    public $duration;
25
26
    /**
27
     * Default method to render the controller according to the action parameter.
28
     */
29
    public function render()
30
    {
31
        $lang = $this->lang;
0 ignored issues
show
Unused Code introduced by
The assignment to $lang is dead and can be removed.
Loading history...
32
33
        $action = $this->action;
34
35
        if ('tree' == $action) {
36
            return $this->doTree();
37
        }
38
39
        $msg = $this->msg;
40
41
        $server_html = $this->printHeader($this->lang['strservers'], null, false);
42
        $server_html .= $this->printBody(false);
43
        $server_html .= $this->printTrail('root', false);
44
45
        ob_start();
46
        switch ($action) {
47
            case 'logout':
48
                $this->doLogout();
49
50
                break;
51
            default:
52
                $this->doDefault($msg);
53
54
                break;
55
        }
56
57
        $server_html .= ob_get_clean();
58
59
        $server_html .= $this->printFooter(false);
60
61
        if (null === $this->container->requestobj->getAttribute('route')) {
62
            echo $server_html;
63
        } else {
64
            $body = $this->container->responseobj->getBody();
65
            $body->write($server_html);
66
67
            return $this->container->responseobj;
68
        }
69
    }
70
71
    public function doDefault($msg = '')
72
    {
73
        $lang = $this->lang;
74
75
        $this->printTabs('root', 'servers');
76
        $this->printMsg($msg);
77
        $group = isset($_GET['group']) ? $_GET['group'] : false;
78
79
        $groups  = $this->getServersGroups(true, $group);
80
        $columns = [
81
            'group' => [
82
                'title' => $lang['strgroup'],
83
                'field' => Decorator::field('desc'),
84
                'url'   => 'servers?',
85
                'vars'  => ['group' => 'id'],
86
            ],
87
        ];
88
        $actions = [];
89
        if ((false !== $group) && (isset($this->conf['srv_groups'][$group])) && ($groups->recordCount() > 0)) {
90
            $this->printTitle(sprintf($lang['strgroupgroups'], htmlentities($this->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',
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($this->conf['srv_groups'][$group])) {
141
            $this->printTitle(sprintf($lang['strgroupservers'], htmlentities($this->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($this->conf['srv_groups']) and count($this->conf['srv_groups']) > 0
154
            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...
155
            $nodes = $this->getServersGroups(true);
156
        } elseif (isset($this->conf['srv_groups']) and false !== $group_id) {
157
            // group subtree
158
            if ('all' !== $group_id) {
159
                $nodes = $this->getServersGroups(false, $group_id);
160
            }
161
162
            $nodes = array_merge($nodes, $this->misc->getServers(false, $group_id));
1 ignored issue
show
Bug introduced by
It seems like $nodes can also be of type PHPPgAdmin\ArrayRecordSet; however, parameter $array1 of array_merge() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

162
            $nodes = array_merge(/** @scrutinizer ignore-type */ $nodes, $this->misc->getServers(false, $group_id));
Loading history...
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');
0 ignored issues
show
Unused Code introduced by
The assignment to $reqvars is dead and can be removed.
Loading history...
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;
0 ignored issues
show
Unused Code introduced by
The assignment to $conf is dead and can be removed.
Loading history...
197
        $data           = $this->misc->getDatabaseAccessor();
0 ignored issues
show
Unused Code introduced by
The assignment to $data is dead and can be removed.
Loading history...
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
    /**
212
     * Get list of server groups.
213
     *
214
     * @param bool  $recordset return as RecordSet suitable for HTMLTableController::printTable if true, otherwise just return an array
215
     * @param mixed $group_id  a group name to filter the returned servers using $this->conf[srv_groups]
216
     *
217
     * @return array|\PHPPgAdmin\ArrayRecordSet either an array or a Recordset suitable for HTMLTableController::printTable
218
     */
219
    private function getServersGroups($recordset = false, $group_id = false)
1 ignored issue
show
Coding Style introduced by
Private method name "ServersController::getServersGroups" must be prefixed with an underscore
Loading history...
220
    {
221
        $lang = $this->lang;
222
        $grps = [];
223
224
        if (isset($this->conf['srv_groups'])) {
225
            foreach ($this->conf['srv_groups'] as $i => $group) {
226
                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...
227
                    (($group_id === false) and (!isset($group['parents']))) /* root */
1 ignored issue
show
Coding Style introduced by
Each line in a multi-line IF statement must begin with a boolean operator
Loading history...
228
                    or (
229
                        ($group_id !== false)
2 ignored issues
show
Coding Style introduced by
Multi-line IF statement not indented correctly; expected 20 spaces but found 24
Loading history...
Coding Style introduced by
Each line in a multi-line IF statement must begin with a boolean operator
Loading history...
230
                        and isset($group['parents'])
1 ignored issue
show
Coding Style introduced by
Multi-line IF statement not indented correctly; expected 20 spaces but found 24
Loading history...
231
                        and in_array($group_id, explode(
2 ignored issues
show
Coding Style introduced by
Multi-line IF statement not indented correctly; expected 20 spaces but found 24
Loading history...
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
232
                            ',',
233
                            preg_replace('/\s/', '', $group['parents'])
234
                        ), true)
1 ignored issue
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
235
                    ) /* nested group */
1 ignored issue
show
Coding Style introduced by
Each line in a multi-line IF statement must begin with a boolean operator
Loading history...
236
                ) {
237
                    $grps[$i] = [
238
                        'id'     => $i,
239
                        'desc'   => $group['desc'],
240
                        'icon'   => 'Servers',
241
                        'action' => Decorator::url(
242
                            'servers',
243
                            [
244
                                'group' => Decorator::field('id'),
245
                            ]
246
                        ),
247
                        'branch' => Decorator::url(
248
                            'servers',
249
                            [
250
                                'action' => 'tree',
251
                                'group'  => $i,
252
                            ]
253
                        ),
254
                    ];
255
                }
256
            }
257
258
            if ($group_id === false) {
259
                $grps['all'] = [
260
                    'id'     => 'all',
261
                    'desc'   => $lang['strallservers'],
262
                    'icon'   => 'Servers',
263
                    'action' => Decorator::url(
264
                        'servers',
265
                        [
266
                            'group' => Decorator::field('id'),
267
                        ]
268
                    ),
269
                    'branch' => Decorator::url(
270
                        'servers',
271
                        [
272
                            'action' => 'tree',
273
                            'group'  => 'all',
274
                        ]
275
                    ),
276
                ];
277
            }
278
        }
279
280
        if ($recordset) {
281
            return new \PHPPgAdmin\ArrayRecordSet($grps);
282
        }
283
284
        return $grps;
285
    }
286
}
287