1 | <?php |
||
2 | |||
3 | /** |
||
4 | * PHPPgAdmin 6.1.3 |
||
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 | use \PHPPgAdmin\Traits\ServersTrait; |
||
17 | |||
18 | public $table_place = 'servers-servers'; |
||
19 | |||
20 | public $section = 'servers'; |
||
21 | |||
22 | public $query = ''; |
||
23 | |||
24 | public $subject = ''; |
||
25 | |||
26 | public $start_time; |
||
27 | |||
28 | public $duration; |
||
29 | |||
30 | public $controller_title = 'strservers'; |
||
31 | |||
32 | protected $no_db_connection = true; |
||
33 | |||
34 | /** |
||
35 | * Default method to render the controller according to the action parameter. |
||
36 | */ |
||
37 | public function render() |
||
38 | { |
||
39 | if ('tree' === $this->action) { |
||
40 | return $this->doTree(); |
||
41 | } |
||
42 | |||
43 | $msg = $this->msg; |
||
44 | |||
45 | $server_html = $this->printHeader($this->headerTitle(), null, false); |
||
46 | $server_html .= $this->printBody(false); |
||
47 | $server_html .= $this->printTrail('root', false); |
||
48 | |||
49 | \ob_start(); |
||
50 | |||
51 | switch ($this->action) { |
||
52 | case 'logout': |
||
53 | $this->doLogout(); |
||
54 | |||
55 | break; |
||
56 | |||
57 | default: |
||
58 | $this->doDefault($msg); |
||
59 | |||
60 | break; |
||
61 | } |
||
62 | |||
63 | $server_html .= \ob_get_clean(); |
||
64 | |||
65 | $server_html .= $this->printFooter(false); |
||
66 | |||
67 | if (null === \requestInstance()->getAttribute('route')) { |
||
68 | echo $server_html; |
||
69 | } else { |
||
70 | $body = \responseInstance()->getBody(); |
||
71 | $body->write($server_html); |
||
72 | |||
73 | return \responseInstance(); |
||
74 | } |
||
75 | } |
||
76 | |||
77 | public function doDefault($msg = ''): void |
||
78 | { |
||
79 | $this->printTabs('root', 'servers'); |
||
80 | $this->printMsg($msg); |
||
81 | $group = $_GET['group'] ?? false; |
||
82 | |||
83 | $groups = $this->getServersGroups(true, $group); |
||
84 | $columns = [ |
||
85 | 'group' => [ |
||
86 | 'title' => $this->lang['strgroup'], |
||
87 | 'field' => Decorator::field('desc'), |
||
88 | 'url' => 'servers?', |
||
89 | 'vars' => ['group' => 'id'], |
||
90 | ], |
||
91 | ]; |
||
92 | $actions = []; |
||
93 | |||
94 | if ((false !== $group) && |
||
95 | (isset($this->conf['srv_groups'][$group])) && |
||
96 | (0 < $groups->recordCount()) |
||
97 | ) { |
||
98 | $this->printTitle(\sprintf($this->lang['strgroupgroups'], \htmlentities($this->conf['srv_groups'][$group]['desc'], \ENT_QUOTES, 'UTF-8'))); |
||
99 | echo $this->printTable($groups, $columns, $actions, $this->table_place); |
||
100 | } |
||
101 | |||
102 | $servers = $this->getServers(true, $group); |
||
103 | |||
104 | $columns = [ |
||
105 | 'server' => [ |
||
106 | 'title' => $this->lang['strserver'], |
||
107 | 'field' => Decorator::field('desc'), |
||
108 | 'url' => \containerInstance()->subFolder . '/redirect/server?', |
||
109 | 'vars' => ['server' => 'sha'], |
||
110 | ], |
||
111 | 'host' => [ |
||
112 | 'title' => $this->lang['strhost'], |
||
113 | 'field' => Decorator::field('host'), |
||
114 | ], |
||
115 | 'port' => [ |
||
116 | 'title' => $this->lang['strport'], |
||
117 | 'field' => Decorator::field('port'), |
||
118 | ], |
||
119 | 'username' => [ |
||
120 | 'title' => $this->lang['strusername'], |
||
121 | 'field' => Decorator::field('username'), |
||
122 | ], |
||
123 | 'actions' => [ |
||
124 | 'title' => $this->lang['stractions'], |
||
125 | ], |
||
126 | ]; |
||
127 | |||
128 | $actions = [ |
||
129 | 'logout' => [ |
||
130 | 'content' => $this->lang['strlogout'], |
||
131 | 'attr' => [ |
||
132 | 'href' => [ |
||
133 | 'url' => 'servers', |
||
134 | 'urlvars' => [ |
||
135 | 'action' => 'logout', |
||
136 | 'logoutServer' => Decorator::field('sha'), |
||
137 | ], |
||
138 | ], |
||
139 | ], |
||
140 | ], |
||
141 | ]; |
||
142 | //\sha1("{$server_info['host']}:{$server_info['port']}:{$server_info['sslmode']}") |
||
143 | $svPre = static function (&$rowdata) use ($actions) { |
||
144 | $actions['logout']['disable'] = empty($rowdata->fields['username']); |
||
145 | |||
146 | return $actions; |
||
147 | }; |
||
148 | |||
149 | if ((false !== $group) && |
||
150 | isset($this->conf['srv_groups'][$group]) |
||
151 | ) { |
||
152 | $this->printTitle(\sprintf($this->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, $this->lang['strnoobjects'], $svPre); |
||
156 | } |
||
157 | |||
158 | public function doTree() |
||
159 | { |
||
160 | $nodes = []; |
||
161 | $group_id = $_GET['group'] ?? false; |
||
162 | |||
163 | // root with srv_groups |
||
164 | if (isset($this->conf['srv_groups']) && 0 < \count($this->conf['srv_groups']) |
||
165 | && false === $group_id) { |
||
166 | $nodes = $this->getServersGroups(true); |
||
167 | } elseif (isset($this->conf['srv_groups']) && 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)); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
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 | $attrs = [ |
||
183 | 'text' => Decorator::field('desc'), |
||
184 | // Show different icons for logged in/out |
||
185 | 'icon' => Decorator::field('icon'), |
||
186 | 'toolTip' => Decorator::field('id'), |
||
187 | 'action' => Decorator::field('action'), |
||
188 | // Only create a branch url if the user has |
||
189 | // logged into the server. |
||
190 | 'branch' => Decorator::field('branch'), |
||
191 | ]; |
||
192 | |||
193 | return $this->printTree($nodes, $attrs, $this->section); |
||
194 | } |
||
195 | |||
196 | public function doLogout(): void |
||
197 | { |
||
198 | $server_info = $this->misc->getServerInfo($_REQUEST['logoutServer']); |
||
199 | $this->misc->setServerInfo(null, null, $_REQUEST['logoutServer']); |
||
200 | |||
201 | unset($_SESSION['sharedUsername'], $_SESSION['sharedPassword']); |
||
202 | |||
203 | $this->view->setReloadBrowser(true); |
||
204 | |||
205 | echo \sprintf($this->lang['strlogoutmsg'], $server_info['desc']); |
||
206 | } |
||
207 | |||
208 | /** |
||
209 | * Get list of server groups. |
||
210 | * |
||
211 | * @param bool $recordset return as RecordSet suitable for HTMLTableController::printTable if true, otherwise just return an array |
||
212 | * @param mixed $group_id a group name to filter the returned servers using $this->conf[srv_groups] |
||
213 | * |
||
214 | * @return array|\PHPPgAdmin\ArrayRecordSet either an array or a Recordset suitable for HTMLTableController::printTable |
||
215 | */ |
||
216 | private function getServersGroups($recordset = false, $group_id = false) |
||
217 | { |
||
218 | $grps = []; |
||
219 | |||
220 | if (isset($this->conf['srv_groups'])) { |
||
221 | foreach ($this->conf['srv_groups'] as $i => $group) { |
||
222 | if (((false === $group_id) && (!isset($group['parents']))) || |
||
223 | (false !== $group_id) && |
||
224 | isset($group['parents']) && |
||
225 | \in_array( |
||
226 | $group_id, |
||
227 | \explode( |
||
228 | ',', |
||
229 | \preg_replace('/\s/', '', $group['parents']) |
||
230 | ), |
||
231 | true |
||
232 | ) |
||
233 | ) { |
||
234 | $grps[$i] = [ |
||
235 | 'id' => $i, |
||
236 | 'desc' => $group['desc'], |
||
237 | 'icon' => 'Servers', |
||
238 | 'action' => Decorator::url( |
||
239 | 'servers', |
||
240 | [ |
||
241 | 'group' => Decorator::field('id'), |
||
242 | ] |
||
243 | ), |
||
244 | 'branch' => Decorator::url( |
||
245 | 'servers', |
||
246 | [ |
||
247 | 'action' => 'tree', |
||
248 | 'group' => $i, |
||
249 | ] |
||
250 | ), |
||
251 | ]; |
||
252 | } |
||
253 | } |
||
254 | |||
255 | if (false === $group_id) { |
||
256 | $grps['all'] = [ |
||
257 | 'id' => 'all', |
||
258 | 'desc' => $this->lang['strallservers'], |
||
259 | 'icon' => 'Servers', |
||
260 | 'action' => Decorator::url( |
||
261 | 'servers', |
||
262 | [ |
||
263 | 'group' => Decorator::field('id'), |
||
264 | ] |
||
265 | ), |
||
266 | 'branch' => Decorator::url( |
||
267 | 'servers', |
||
268 | [ |
||
269 | 'action' => 'tree', |
||
270 | 'group' => 'all', |
||
271 | ] |
||
272 | ), |
||
273 | ]; |
||
274 | } |
||
275 | } |
||
276 | |||
277 | if ($recordset) { |
||
278 | return new \PHPPgAdmin\ArrayRecordSet($grps); |
||
279 | } |
||
280 | |||
281 | return $grps; |
||
282 | } |
||
283 | } |
||
284 |