1<?php 2/* 3** Zabbix 4** Copyright (C) 2001-2021 Zabbix SIA 5** 6** This program is free software; you can redistribute it and/or modify 7** it under the terms of the GNU General Public License as published by 8** the Free Software Foundation; either version 2 of the License, or 9** (at your option) any later version. 10** 11** This program is distributed in the hope that it will be useful, 12** but WITHOUT ANY WARRANTY; without even the implied warranty of 13** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14** GNU General Public License for more details. 15** 16** You should have received a copy of the GNU General Public License 17** along with this program; if not, write to the Free Software 18** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 19**/ 20 21 22require_once dirname(__FILE__).'/js/configuration.host.massupdate.js.php'; 23 24$hostWidget = (new CWidget())->setTitle(_('Hosts')); 25 26// create form 27$hostView = (new CForm()) 28 ->setName('hostForm') 29 ->addVar('action', 'host.massupdate') 30 ->addVar('tls_accept', $data['tls_accept']) 31 ->setAttribute('id', 'hostForm'); 32foreach ($data['hosts'] as $hostid) { 33 $hostView->addVar('hosts['.$hostid.']', $hostid); 34} 35 36// create form list 37$hostFormList = new CFormList('hostFormList'); 38 39// replace host groups 40$hostGroupsToReplace = null; 41if (isset($_REQUEST['groups'])) { 42 $getHostGroups = API::HostGroup()->get([ 43 'groupids' => $_REQUEST['groups'], 44 'output' => ['groupid', 'name'], 45 'editable' => true 46 ]); 47 foreach ($getHostGroups as $getHostGroup) { 48 $hostGroupsToReplace[] = [ 49 'id' => $getHostGroup['groupid'], 50 'name' => $getHostGroup['name'] 51 ]; 52 } 53} 54 55$replaceGroups = (new CDiv( 56 (new CMultiSelect([ 57 'name' => 'groups[]', 58 'objectName' => 'hostGroup', 59 'objectOptions' => ['editable' => true], 60 'data' => $hostGroupsToReplace, 61 'popup' => [ 62 'parameters' => 'srctbl=host_groups&dstfrm='.$hostView->getName().'&dstfld1=groups_&srcfld1=groupid'. 63 '&writeonly=1&multiselect=1' 64 ] 65 ]))->setWidth(ZBX_TEXTAREA_STANDARD_WIDTH) 66))->setId('replaceGroups'); 67 68$hostFormList->addRow( 69 [ 70 _('Replace host groups'), 71 SPACE, 72 (new CVisibilityBox('visible[groups]', 'replaceGroups', _('Original'))) 73 ->setChecked(isset($data['visible']['groups'])) 74 ], 75 $replaceGroups 76); 77 78// add new or existing host groups 79$hostGroupsToAdd = null; 80if (isset($_REQUEST['new_groups'])) { 81 foreach ($_REQUEST['new_groups'] as $newHostGroup) { 82 if (is_array($newHostGroup) && isset($newHostGroup['new'])) { 83 $hostGroupsToAdd[] = [ 84 'id' => $newHostGroup['new'], 85 'name' => $newHostGroup['new'].' ('._x('new', 'new element in multiselect').')', 86 'isNew' => true 87 ]; 88 } 89 else { 90 $hostGroupIds[] = $newHostGroup; 91 } 92 } 93 94 if (isset($hostGroupIds)) { 95 $getHostGroups = API::HostGroup()->get([ 96 'groupids' => $hostGroupIds, 97 'output' => ['groupid', 'name'] 98 ]); 99 foreach ($getHostGroups as $getHostGroup) { 100 $hostGroupsToAdd[] = [ 101 'id' => $getHostGroup['groupid'], 102 'name' => $getHostGroup['name'] 103 ]; 104 } 105 } 106} 107if (CWebUser::getType() == USER_TYPE_SUPER_ADMIN) { 108 $hostFormList->addRow( 109 [ 110 _('Add new or existing host groups'), 111 SPACE, 112 (new CVisibilityBox('visible[new_groups]', 'newGroups', _('Original'))) 113 ->setChecked(isset($data['visible']['new_groups'])) 114 ], 115 (new CDiv( 116 (new CMultiSelect([ 117 'name' => 'new_groups[]', 118 'objectName' => 'hostGroup', 119 'objectOptions' => ['editable' => true], 120 'data' => $hostGroupsToAdd, 121 'addNew' => true, 122 'popup' => [ 123 'parameters' => 'srctbl=host_groups&dstfrm='.$hostView->getName().'&dstfld1=new_groups_'. 124 '&srcfld1=groupid&writeonly=1&multiselect=1' 125 ] 126 ]))->setWidth(ZBX_TEXTAREA_STANDARD_WIDTH) 127 ))->setId('newGroups') 128 ); 129} 130else { 131 $hostFormList->addRow( 132 [ 133 _('New host group'), 134 SPACE, 135 (new CVisibilityBox('visible[new_groups]', 'newGroups', _('Original'))) 136 ->setChecked(isset($data['visible']['new_groups'])) 137 ], 138 (new CDiv( 139 (new CMultiSelect([ 140 'name' => 'new_groups[]', 141 'objectName' => 'hostGroup', 142 'objectOptions' => ['editable' => true], 143 'data' => $hostGroupsToAdd, 144 'popup' => [ 145 'parameters' => 'srctbl=host_groups&dstfrm='.$hostView->getName().'&dstfld1=new_groups_'. 146 '&srcfld1=groupid&writeonly=1&multiselect=1' 147 ] 148 ]))->setWidth(ZBX_TEXTAREA_STANDARD_WIDTH) 149 ))->setId('newGroups') 150 ); 151} 152 153// append description to form list 154$hostFormList->addRow( 155 [ 156 _('Description'), 157 SPACE, 158 (new CVisibilityBox('visible[description]', 'description', _('Original'))) 159 ->setChecked(isset($data['visible']['description'])) 160 ], 161 (new CTextArea('description', $data['description']))->setWidth(ZBX_TEXTAREA_STANDARD_WIDTH) 162); 163 164// append proxy to form list 165$proxyComboBox = new CComboBox('proxy_hostid', $data['proxy_hostid']); 166$proxyComboBox->addItem(0, _('(no proxy)')); 167foreach ($data['proxies'] as $proxie) { 168 $proxyComboBox->addItem($proxie['hostid'], $proxie['host']); 169} 170$hostFormList->addRow( 171 [ 172 _('Monitored by proxy'), 173 SPACE, 174 (new CVisibilityBox('visible[proxy_hostid]', 'proxy_hostid', _('Original'))) 175 ->setChecked(isset($data['visible']['proxy_hostid'])) 176 ], 177 $proxyComboBox 178); 179 180// append status to form list 181$hostFormList->addRow( 182 [ 183 _('Status'), 184 SPACE, 185 (new CVisibilityBox('visible[status]', 'status', _('Original'))) 186 ->setChecked(isset($data['visible']['status'])) 187 ], 188 new CComboBox('status', $data['status'], null, [ 189 HOST_STATUS_MONITORED => _('Enabled'), 190 HOST_STATUS_NOT_MONITORED => _('Disabled') 191 ]) 192); 193 194$templatesFormList = new CFormList('templatesFormList'); 195 196// append templates table to from list 197$newTemplateTable = (new CTable()) 198 ->addRow([ 199 (new CMultiSelect([ 200 'name' => 'templates[]', 201 'objectName' => 'templates', 202 'data' => $data['linkedTemplates'], 203 'popup' => [ 204 'parameters' => 'srctbl=templates&srcfld1=hostid&srcfld2=host&dstfrm='.$hostView->getName(). 205 '&dstfld1=templates_&templated_hosts=1&multiselect=1' 206 ] 207 ]))->setWidth(ZBX_TEXTAREA_STANDARD_WIDTH) 208 ]) 209 ->addRow([ 210 (new CDiv([ 211 new CLabel( 212 [(new CCheckBox('mass_replace_tpls'))->setChecked($data['mass_replace_tpls'] == 1), _('Replace')], 213 'mass_replace_tpls' 214 ), 215 BR(), 216 new CLabel( 217 [(new CCheckBox('mass_clear_tpls'))->setChecked($data['mass_clear_tpls'] == 1), _('Clear when unlinking')], 218 'mass_clear_tpls' 219 ) 220 ]))->addClass('floatleft') 221 ]); 222 223$templatesFormList->addRow( 224 [ 225 _('Link templates'), 226 SPACE, 227 (new CVisibilityBox('visible[templates]', 'templateDiv', _('Original'))) 228 ->setChecked(isset($data['visible']['templates'])) 229 ], 230 (new CDiv($newTemplateTable)) 231 ->setId('templateDiv') 232 ->addClass(ZBX_STYLE_TABLE_FORMS_SEPARATOR) 233 ->setAttribute('style', 'min-width: '.ZBX_TEXTAREA_BIG_WIDTH.'px;') 234); 235 236$ipmiFormList = new CFormList('ipmiFormList'); 237 238// append ipmi to form list 239$ipmiFormList->addRow( 240 [ 241 _('IPMI authentication algorithm'), 242 SPACE, 243 (new CVisibilityBox('visible[ipmi_authtype]', 'ipmi_authtype', _('Original'))) 244 ->setChecked(isset($data['visible']['ipmi_authtype'])) 245 ], 246 new CComboBox('ipmi_authtype', $data['ipmi_authtype'], null, ipmiAuthTypes()) 247); 248 249$ipmiFormList->addRow( 250 [ 251 _('IPMI privilege level'), 252 SPACE, 253 (new CVisibilityBox('visible[ipmi_privilege]', 'ipmi_privilege', _('Original'))) 254 ->setChecked(isset($data['visible']['ipmi_privilege'])) 255 ], 256 new CComboBox('ipmi_privilege', $data['ipmi_privilege'], null, ipmiPrivileges()) 257); 258 259$ipmiFormList->addRow( 260 [ 261 _('IPMI username'), 262 SPACE, 263 (new CVisibilityBox('visible[ipmi_username]', 'ipmi_username', _('Original'))) 264 ->setChecked(isset($data['visible']['ipmi_username'])) 265 ], 266 (new CTextBox('ipmi_username', $data['ipmi_username']))->setWidth(ZBX_TEXTAREA_SMALL_WIDTH) 267); 268 269$ipmiFormList->addRow( 270 [ 271 _('IPMI password'), 272 SPACE, 273 (new CVisibilityBox('visible[ipmi_password]', 'ipmi_password', _('Original'))) 274 ->setChecked(isset($data['visible']['ipmi_password'])) 275 ], 276 (new CTextBox('ipmi_password', $data['ipmi_password']))->setWidth(ZBX_TEXTAREA_SMALL_WIDTH) 277); 278 279$inventoryFormList = new CFormList('inventoryFormList'); 280 281// append inventories to form list 282$inventoryFormList->addRow( 283 [ 284 _('Inventory mode'), 285 SPACE, 286 (new CVisibilityBox('visible[inventory_mode]', 'inventory_mode_div', _('Original'))) 287 ->setChecked(isset($data['visible']['inventory_mode'])) 288 ], 289 (new CDiv( 290 (new CRadioButtonList('inventory_mode', (int) $data['inventory_mode'])) 291 ->addValue(_('Disabled'), HOST_INVENTORY_DISABLED) 292 ->addValue(_('Manual'), HOST_INVENTORY_MANUAL) 293 ->addValue(_('Automatic'), HOST_INVENTORY_AUTOMATIC) 294 ->setModern(true) 295 ))->setId('inventory_mode_div') 296); 297 298$hostInventoryTable = DB::getSchema('host_inventory'); 299foreach ($data['inventories'] as $field => $fieldInfo) { 300 if (!isset($data['host_inventory'][$field])) { 301 $data['host_inventory'][$field] = ''; 302 } 303 304 if ($hostInventoryTable['fields'][$field]['type'] == DB::FIELD_TYPE_TEXT) { 305 $fieldInput = (new CTextArea('host_inventory['.$field.']', $data['host_inventory'][$field])) 306 ->setWidth(ZBX_TEXTAREA_BIG_WIDTH); 307 } 308 else { 309 $field_length = $hostInventoryTable['fields'][$field]['length']; 310 311 if ($field_length < 39) { 312 $width = ZBX_TEXTAREA_SMALL_WIDTH; 313 } 314 elseif ($field_length < 64) { 315 $width = ZBX_TEXTAREA_STANDARD_WIDTH; 316 } 317 else { 318 $width = ZBX_TEXTAREA_BIG_WIDTH; 319 } 320 321 $fieldInput = (new CTextBox('host_inventory['.$field.']', $data['host_inventory'][$field])) 322 ->setWidth($width) 323 ->setAttribute('maxlength', $field_length); 324 } 325 326 $inventoryFormList->addRow( 327 [ 328 $fieldInfo['title'], 329 SPACE, 330 (new CVisibilityBox( 331 'visible['.$field.']', 332 'host_inventory['.$field.']', 333 _('Original') 334 ))->setChecked(isset($data['visible'][$field])) 335 ], 336 $fieldInput, null, 'formrow-inventory' 337 ); 338} 339 340// Encryption 341$encryption_form_list = new CFormList('encryption'); 342 343$encryption_table = (new CTable()) 344 ->addRow([_('Connections to host'), 345 (new CRadioButtonList('tls_connect', (int) $data['tls_connect'])) 346 ->addValue(_('No encryption'), HOST_ENCRYPTION_NONE) 347 ->addValue(_('PSK'), HOST_ENCRYPTION_PSK) 348 ->addValue(_('Certificate'), HOST_ENCRYPTION_CERTIFICATE) 349 ->setModern(true) 350 ]) 351 ->addRow([_('Connections from host'), [ 352 new CLabel([new CCheckBox('tls_in_none'), _('No encryption')]), 353 BR(), 354 new CLabel([new CCheckBox('tls_in_psk'), _('PSK')]), 355 BR(), 356 new CLabel([new CCheckBox('tls_in_cert'), _('Certificate')]) 357 ]]) 358 ->addRow([_('PSK identity'), 359 (new CTextBox('tls_psk_identity', $data['tls_psk_identity'], false, 128))->setWidth(ZBX_TEXTAREA_BIG_WIDTH) 360 ]) 361 ->addRow([_('PSK'), 362 (new CTextBox('tls_psk', $data['tls_psk'], false, 512))->setWidth(ZBX_TEXTAREA_BIG_WIDTH) 363 ]) 364 ->addRow([_('Issuer'), 365 (new CTextBox('tls_issuer', $data['tls_issuer'], false, 1024))->setWidth(ZBX_TEXTAREA_BIG_WIDTH) 366 ]) 367 ->addRow([_x('Subject', 'encryption certificate'), 368 (new CTextBox('tls_subject', $data['tls_subject'], false, 1024))->setWidth(ZBX_TEXTAREA_BIG_WIDTH) 369 ]); 370 371$encryption_form_list->addRow([ 372 _('Connections'), 373 SPACE, 374 (new CVisibilityBox('visible[encryption]', 'encryption_div', _('Original'))) 375 ->setChecked(isset($data['visible']['encryption'])) 376 ], 377 (new CDiv($encryption_table)) 378 ->setId('encryption_div') 379 ->addClass(ZBX_STYLE_TABLE_FORMS_SEPARATOR) 380 ->setAttribute('style', 'min-width: '.ZBX_TEXTAREA_BIG_WIDTH.'px;') 381); 382 383// append tabs to form 384$hostTab = (new CTabView()) 385 ->addTab('hostTab', _('Host'), $hostFormList) 386 ->addTab('templatesTab', _('Templates'), $templatesFormList) 387 ->addTab('ipmiTab', _('IPMI'), $ipmiFormList) 388 ->addTab('inventoryTab', _('Inventory'), $inventoryFormList); 389// reset the tab when opening the form for the first time 390if (!hasRequest('masssave') && !hasRequest('inventory_mode')) { 391 $hostTab->setSelected(0); 392} 393$hostTab->addTab('encryptionTab', _('Encryption'), $encryption_form_list); 394 395// append buttons to form 396$hostTab->setFooter(makeFormFooter( 397 new CSubmit('masssave', _('Update')), 398 [new CButtonCancel(url_param('groupid'))] 399)); 400 401$hostView->addItem($hostTab); 402 403$hostWidget->addItem($hostView); 404 405return $hostWidget; 406