| Total Complexity | 30 |
| Total Lines | 280 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 16 | class ServersController extends BaseController |
||
| 17 | { |
||
| 18 | |||
| 19 | use ServersTrait; |
||
|
1 ignored issue
–
show
|
|||
| 20 | |||
| 21 | public $controller_name = 'ServersController'; |
||
| 22 | public $table_place = 'servers-servers'; |
||
| 23 | public $section = 'servers'; |
||
| 24 | public $query = ''; |
||
| 25 | public $subject = ''; |
||
| 26 | public $start_time; |
||
| 27 | public $duration; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Default method to render the controller according to the action parameter. |
||
| 31 | */ |
||
| 32 | public function render() |
||
| 33 | { |
||
| 34 | $lang = $this->lang; |
||
| 35 | |||
| 36 | $action = $this->action; |
||
| 37 | |||
| 38 | if ('tree' == $action) { |
||
| 39 | return $this->doTree(); |
||
| 40 | } |
||
| 41 | |||
| 42 | $msg = $this->msg; |
||
| 43 | |||
| 44 | $server_html = $this->printHeader($this->lang['strservers'], null, false); |
||
| 45 | $server_html .= $this->printBody(false); |
||
| 46 | $server_html .= $this->printTrail('root', false); |
||
| 47 | |||
| 48 | ob_start(); |
||
| 49 | switch ($action) { |
||
| 50 | case 'logout': |
||
| 51 | $this->doLogout(); |
||
| 52 | |||
| 53 | break; |
||
| 54 | default: |
||
| 55 | $this->doDefault($msg); |
||
| 56 | |||
| 57 | break; |
||
| 58 | } |
||
| 59 | |||
| 60 | $server_html .= ob_get_clean(); |
||
| 61 | |||
| 62 | $server_html .= $this->printFooter(false); |
||
| 63 | |||
| 64 | if (null === $this->container->requestobj->getAttribute('route')) { |
||
| 65 | echo $server_html; |
||
| 66 | } else { |
||
| 67 | $body = $this->container->responseobj->getBody(); |
||
| 68 | $body->write($server_html); |
||
| 69 | |||
| 70 | return $this->container->responseobj; |
||
| 71 | } |
||
| 72 | } |
||
| 73 | |||
| 74 | public function doDefault($msg = '') |
||
| 75 | { |
||
| 76 | $lang = $this->lang; |
||
| 77 | |||
| 78 | $this->printTabs('root', 'servers'); |
||
| 79 | $this->printMsg($msg); |
||
| 80 | $group = isset($_GET['group']) ? $_GET['group'] : false; |
||
| 81 | |||
| 82 | $groups = $this->getServersGroups(true, $group); |
||
| 83 | $columns = [ |
||
| 84 | 'group' => [ |
||
| 85 | 'title' => $lang['strgroup'], |
||
| 86 | 'field' => Decorator::field('desc'), |
||
| 87 | 'url' => 'servers?', |
||
| 88 | 'vars' => ['group' => 'id'], |
||
| 89 | ], |
||
| 90 | ]; |
||
| 91 | $actions = []; |
||
| 92 | if ( |
||
| 93 | (false !== $group) && |
||
|
1 ignored issue
–
show
|
|||
| 94 | (isset($this->conf['srv_groups'][$group])) && |
||
|
1 ignored issue
–
show
|
|||
| 95 | ($groups->recordCount() > 0) |
||
|
1 ignored issue
–
show
|
|||
| 96 | ) { |
||
| 97 | $this->printTitle(sprintf($lang['strgroupgroups'], htmlentities($this->conf['srv_groups'][$group]['desc'], ENT_QUOTES, 'UTF-8'))); |
||
| 98 | echo $this->printTable($groups, $columns, $actions, $this->table_place); |
||
| 99 | } |
||
| 100 | |||
| 101 | $servers = $this->getServers(true, $group); |
||
| 102 | |||
| 103 | $columns = [ |
||
| 104 | 'server' => [ |
||
| 105 | 'title' => $lang['strserver'], |
||
| 106 | 'field' => Decorator::field('desc'), |
||
| 107 | 'url' => \SUBFOLDER . '/redirect/server?', |
||
| 108 | 'vars' => ['server' => 'sha'], |
||
| 109 | ], |
||
| 110 | 'host' => [ |
||
| 111 | 'title' => $lang['strhost'], |
||
| 112 | 'field' => Decorator::field('host'), |
||
| 113 | ], |
||
| 114 | 'port' => [ |
||
| 115 | 'title' => $lang['strport'], |
||
| 116 | 'field' => Decorator::field('port'), |
||
| 117 | ], |
||
| 118 | 'username' => [ |
||
| 119 | 'title' => $lang['strusername'], |
||
| 120 | 'field' => Decorator::field('username'), |
||
| 121 | ], |
||
| 122 | 'actions' => [ |
||
| 123 | 'title' => $lang['stractions'], |
||
| 124 | ], |
||
| 125 | ]; |
||
| 126 | |||
| 127 | $actions = [ |
||
| 128 | 'logout' => [ |
||
| 129 | 'content' => $lang['strlogout'], |
||
| 130 | 'attr' => [ |
||
| 131 | 'href' => [ |
||
| 132 | 'url' => 'servers', |
||
| 133 | 'urlvars' => [ |
||
| 134 | 'action' => 'logout', |
||
| 135 | 'logoutServer' => Decorator::field('id'), |
||
| 136 | ], |
||
| 137 | ], |
||
| 138 | ], |
||
| 139 | ], |
||
| 140 | ]; |
||
| 141 | |||
| 142 | $svPre = function (&$rowdata) use ($actions) { |
||
| 143 | $actions['logout']['disable'] = empty($rowdata->fields['username']); |
||
| 144 | |||
| 145 | return $actions; |
||
| 146 | }; |
||
| 147 | |||
| 148 | if ( |
||
| 149 | (false !== $group) && |
||
|
1 ignored issue
–
show
|
|||
| 150 | isset($this->conf['srv_groups'][$group]) |
||
|
1 ignored issue
–
show
|
|||
| 151 | ) { |
||
| 152 | $this->printTitle(sprintf($lang['strgroupservers'], htmlentities($this->conf['srv_groups'][$group]['desc'], ENT_QUOTES, 'UTF-8')), null); |
||
| 153 | $actions['logout']['attr']['href']['urlvars']['group'] = $group; |
||
| 154 | } |
||
| 155 | echo $this->printTable($servers, $columns, $actions, $this->table_place, $lang['strnoobjects'], $svPre); |
||
| 156 | } |
||
| 157 | |||
| 158 | public function doTree() |
||
| 159 | { |
||
| 160 | $nodes = []; |
||
| 161 | $group_id = isset($_GET['group']) ? $_GET['group'] : false; |
||
| 162 | |||
| 163 | // root with srv_groups |
||
| 164 | if (isset($this->conf['srv_groups']) and count($this->conf['srv_groups']) > 0 |
||
| 165 | and false === $group_id) { |
||
|
1 ignored issue
–
show
|
|||
| 166 | $nodes = $this->getServersGroups(true); |
||
| 167 | } elseif (isset($this->conf['srv_groups']) and false !== $group_id) { |
||
| 168 | // group subtree |
||
| 169 | if ('all' !== $group_id) { |
||
| 170 | $nodes = $this->getServersGroups(false, $group_id); |
||
| 171 | } |
||
| 172 | |||
| 173 | $nodes = array_merge($nodes, $this->getServers(false, $group_id)); |
||
|
2 ignored issues
–
show
|
|||
| 174 | $nodes = new \PHPPgAdmin\ArrayRecordSet($nodes); |
||
| 175 | } else { |
||
| 176 | // no srv_group |
||
| 177 | $nodes = $this->getServers(true, false); |
||
| 178 | } |
||
| 179 | |||
| 180 | $reqvars = $this->misc->getRequestVars('server'); |
||
| 181 | |||
| 182 | //$this->prtrace($reqvars); |
||
| 183 | |||
| 184 | $attrs = [ |
||
| 185 | 'text' => Decorator::field('desc'), |
||
| 186 | // Show different icons for logged in/out |
||
| 187 | 'icon' => Decorator::field('icon'), |
||
| 188 | 'toolTip' => Decorator::field('id'), |
||
| 189 | 'action' => Decorator::field('action'), |
||
| 190 | // Only create a branch url if the user has |
||
| 191 | // logged into the server. |
||
| 192 | 'branch' => Decorator::field('branch'), |
||
| 193 | ]; |
||
| 194 | /*$this->prtrace([ |
||
| 195 | 'nodes' => $nodes, |
||
| 196 | 'attrs' => $attrs, |
||
| 197 | 'section' => $this->section, |
||
| 198 | ]);*/ |
||
| 199 | return $this->printTree($nodes, $attrs, $this->section); |
||
| 200 | } |
||
| 201 | |||
| 202 | public function doLogout() |
||
| 220 | } |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Get list of server groups. |
||
| 224 | * |
||
| 225 | * @param bool $recordset return as RecordSet suitable for HTMLTableController::printTable if true, otherwise just return an array |
||
| 226 | * @param mixed $group_id a group name to filter the returned servers using $this->conf[srv_groups] |
||
| 227 | * |
||
| 228 | * @return array|\PHPPgAdmin\ArrayRecordSet either an array or a Recordset suitable for HTMLTableController::printTable |
||
| 229 | */ |
||
| 230 | private function getServersGroups($recordset = false, $group_id = false) |
||
| 298 |