1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740
|
<?php
/*
** Zabbix
** Copyright (C) 2001-2019 Zabbix SIA
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**/
/**
* Class containing methods for operations with scripts.
*/
class CScript extends CApiService {
protected $tableName = 'scripts';
protected $tableAlias = 's';
protected $sortColumns = ['scriptid', 'name'];
/**
* Get scripts data.
*
* @param array $options
* @param array $options['itemids']
* @param array $options['hostids'] deprecated (very slow)
* @param array $options['groupids']
* @param array $options['triggerids']
* @param array $options['scriptids']
* @param bool $options['status']
* @param bool $options['editable']
* @param bool $options['count']
* @param string $options['pattern']
* @param int $options['limit']
* @param string $options['order']
*
* @return array
*/
public function get(array $options) {
$result = [];
$sqlParts = [
'select' => ['scripts' => 's.scriptid'],
'from' => ['scripts s'],
'where' => [],
'order' => [],
'limit' => null
];
$defOptions = [
'groupids' => null,
'hostids' => null,
'scriptids' => null,
'usrgrpids' => null,
'editable' => false,
'nopermissions' => null,
// filter
'filter' => null,
'search' => null,
'searchByAny' => null,
'startSearch' => false,
'excludeSearch' => false,
'searchWildcardsEnabled'=> null,
// output
'output' => API_OUTPUT_EXTEND,
'selectGroups' => null,
'selectHosts' => null,
'countOutput' => false,
'preservekeys' => false,
'sortfield' => '',
'sortorder' => '',
'limit' => null
];
$options = zbx_array_merge($defOptions, $options);
// editable + permission check
if (self::$userData['type'] != USER_TYPE_SUPER_ADMIN) {
if ($options['editable']) {
return $result;
}
$userGroups = getUserGroupsByUserId(self::$userData['userid']);
$sqlParts['where'][] = '(s.usrgrpid IS NULL OR '.dbConditionInt('s.usrgrpid', $userGroups).')';
$sqlParts['where'][] = '(s.groupid IS NULL OR EXISTS ('.
'SELECT NULL'.
' FROM rights r'.
' WHERE s.groupid=r.id'.
' AND '.dbConditionInt('r.groupid', $userGroups).
' GROUP BY r.id'.
' HAVING MIN(r.permission)>'.PERM_DENY.
'))';
}
// groupids
if (!is_null($options['groupids'])) {
zbx_value2array($options['groupids']);
$sqlParts['where'][] = '(s.groupid IS NULL OR '.dbConditionInt('s.groupid', $options['groupids']).')';
}
// hostids
if (!is_null($options['hostids'])) {
zbx_value2array($options['hostids']);
// return scripts that are assigned to the hosts' groups or to no group
$hostGroups = API::HostGroup()->get([
'output' => ['groupid'],
'hostids' => $options['hostids']
]);
$hostGroupIds = zbx_objectValues($hostGroups, 'groupid');
$sqlParts['where'][] = '('.dbConditionInt('s.groupid', $hostGroupIds).' OR s.groupid IS NULL)';
}
// usrgrpids
if (!is_null($options['usrgrpids'])) {
zbx_value2array($options['usrgrpids']);
$sqlParts['where'][] = '(s.usrgrpid IS NULL OR '.dbConditionInt('s.usrgrpid', $options['usrgrpids']).')';
}
// scriptids
if (!is_null($options['scriptids'])) {
zbx_value2array($options['scriptids']);
$sqlParts['where'][] = dbConditionInt('s.scriptid', $options['scriptids']);
}
// search
if (is_array($options['search'])) {
zbx_db_search('scripts s', $options, $sqlParts);
}
// filter
if (is_array($options['filter'])) {
$this->dbFilter('scripts s', $options, $sqlParts);
}
// limit
if (zbx_ctype_digit($options['limit']) && $options['limit']) {
$sqlParts['limit'] = $options['limit'];
}
$sqlParts = $this->applyQueryOutputOptions($this->tableName(), $this->tableAlias(), $options, $sqlParts);
$sqlParts = $this->applyQuerySortOptions($this->tableName(), $this->tableAlias(), $options, $sqlParts);
$res = DBselect($this->createSelectQueryFromParts($sqlParts), $sqlParts['limit']);
while ($script = DBfetch($res)) {
if ($options['countOutput']) {
$result = $script['rowscount'];
}
else {
$result[$script['scriptid']] = $script;
}
}
if ($options['countOutput']) {
return $result;
}
if ($result) {
$result = $this->addRelatedObjects($options, $result);
$result = $this->unsetExtraFields($result, ['groupid', 'host_access'], $options['output']);
}
// removing keys (hash -> array)
if (!$options['preservekeys']) {
$result = zbx_cleanHashes($result);
}
return $result;
}
/**
* @param array $scripts
*
* @return array
*/
public function create(array $scripts) {
$this->validateCreate($scripts);
$scriptids = DB::insert('scripts', $scripts);
foreach ($scripts as $index => &$script) {
$script['scriptid'] = $scriptids[$index];
}
unset($script);
$this->addAuditBulk(AUDIT_ACTION_ADD, AUDIT_RESOURCE_SCRIPT, $scripts);
return ['scriptids' => $scriptids];
}
/**
* @param array $scripts
*
* @throws APIException if the input is invalid
*/
protected function validateCreate(array &$scripts) {
if (self::$userData['type'] != USER_TYPE_SUPER_ADMIN) {
self::exception(ZBX_API_ERROR_PERMISSIONS, _('You do not have permission to perform this operation.'));
}
$api_input_rules = ['type' => API_OBJECTS, 'flags' => API_NOT_EMPTY | API_NORMALIZE, 'uniq' => [['name']], 'fields' => [
'name' => ['type' => API_SCRIPT_NAME, 'flags' => API_REQUIRED, 'length' => DB::getFieldLength('scripts', 'name')],
'type' => ['type' => API_INT32, 'in' => implode(',', [ZBX_SCRIPT_TYPE_CUSTOM_SCRIPT, ZBX_SCRIPT_TYPE_IPMI])],
'execute_on' => ['type' => API_INT32, 'in' => implode(',', [ZBX_SCRIPT_EXECUTE_ON_AGENT, ZBX_SCRIPT_EXECUTE_ON_SERVER, ZBX_SCRIPT_EXECUTE_ON_PROXY])],
'command' => ['type' => API_STRING_UTF8, 'flags' => API_REQUIRED | API_NOT_EMPTY, 'length' => DB::getFieldLength('scripts', 'command')],
'description' => ['type' => API_STRING_UTF8, 'length' => DB::getFieldLength('scripts', 'description')],
'usrgrpid' => ['type' => API_ID],
'groupid' => ['type' => API_ID],
'host_access' => ['type' => API_INT32, 'in' => implode(',', [PERM_READ, PERM_READ_WRITE])],
'confirmation' => ['type' => API_STRING_UTF8, 'length' => DB::getFieldLength('scripts', 'confirmation')]
]];
if (!CApiInputValidator::validate($api_input_rules, $scripts, '/', $error)) {
self::exception(ZBX_API_ERROR_PARAMETERS, $error);
}
$scripts = $this->checkExecutionType($scripts);
$this->checkUserGroups($scripts);
$this->checkHostGroups($scripts);
$this->checkDuplicates($scripts);
}
/**
* @param array $scripts
*
* @return array
*/
public function update(array $scripts) {
$this->validateUpdate($scripts, $db_scripts);
$upd_scripts = [];
foreach ($scripts as $script) {
$db_script = $db_scripts[$script['scriptid']];
$upd_script = [];
// strings
foreach (['name', 'command', 'description', 'confirmation'] as $field_name) {
if (array_key_exists($field_name, $script) && $script[$field_name] !== $db_script[$field_name]) {
$upd_script[$field_name] = $script[$field_name];
}
}
// integers
foreach (['type', 'execute_on', 'usrgrpid', 'groupid', 'host_access'] as $field_name) {
if (array_key_exists($field_name, $script) && $script[$field_name] != $db_script[$field_name]) {
$upd_script[$field_name] = $script[$field_name];
}
}
if ($upd_script) {
$upd_scripts[] = [
'values' => $upd_script,
'where' => ['scriptid' => $script['scriptid']]
];
}
}
if ($upd_scripts) {
DB::update('scripts', $upd_scripts);
}
$this->addAuditBulk(AUDIT_ACTION_UPDATE, AUDIT_RESOURCE_SCRIPT, $scripts, $db_scripts);
return ['scriptids' => zbx_objectValues($scripts, 'scriptid')];
}
/**
* @param array $scripts
* @param array $db_scripts
*
* @throws APIException if the input is invalid
*/
protected function validateUpdate(array &$scripts, array &$db_scripts = null) {
if (self::$userData['type'] != USER_TYPE_SUPER_ADMIN) {
self::exception(ZBX_API_ERROR_PERMISSIONS, _('You do not have permission to perform this operation.'));
}
$api_input_rules = ['type' => API_OBJECTS, 'flags' => API_NOT_EMPTY | API_NORMALIZE, 'uniq' => [['scriptid'], ['name']], 'fields' => [
'scriptid' => ['type' => API_ID, 'flags' => API_REQUIRED],
'name' => ['type' => API_SCRIPT_NAME, 'length' => DB::getFieldLength('scripts', 'name')],
'type' => ['type' => API_INT32, 'in' => implode(',', [ZBX_SCRIPT_TYPE_CUSTOM_SCRIPT, ZBX_SCRIPT_TYPE_IPMI])],
'execute_on' => ['type' => API_INT32, 'in' => implode(',', [ZBX_SCRIPT_EXECUTE_ON_AGENT, ZBX_SCRIPT_EXECUTE_ON_SERVER, ZBX_SCRIPT_EXECUTE_ON_PROXY])],
'command' => ['type' => API_STRING_UTF8, 'flags' => API_NOT_EMPTY, 'length' => DB::getFieldLength('scripts', 'command')],
'description' => ['type' => API_STRING_UTF8, 'length' => DB::getFieldLength('scripts', 'description')],
'usrgrpid' => ['type' => API_ID],
'groupid' => ['type' => API_ID],
'host_access' => ['type' => API_INT32, 'in' => implode(',', [PERM_READ, PERM_READ_WRITE])],
'confirmation' => ['type' => API_STRING_UTF8, 'length' => DB::getFieldLength('scripts', 'confirmation')]
]];
if (!CApiInputValidator::validate($api_input_rules, $scripts, '/', $error)) {
self::exception(ZBX_API_ERROR_PARAMETERS, $error);
}
$db_scripts = DB::select('scripts', [
'output' => ['scriptid', 'name', 'type', 'execute_on', 'command', 'description', 'usrgrpid', 'groupid',
'host_access', 'confirmation'
],
'scriptids' => zbx_objectValues($scripts, 'scriptid'),
'preservekeys' => true
]);
$new_name_scripts = [];
foreach ($scripts as $script) {
if (!array_key_exists($script['scriptid'], $db_scripts)) {
self::exception(ZBX_API_ERROR_PERMISSIONS,
_('No permissions to referred object or it does not exist!')
);
}
$db_script = $db_scripts[$script['scriptid']];
if (array_key_exists('name', $script) && trimPath($script['name']) !== trimPath($db_script['name'])) {
$new_name_scripts[] = $script;
}
}
$scripts = $this->checkExecutionType($scripts);
$this->checkUserGroups($scripts);
$this->checkHostGroups($scripts);
if ($new_name_scripts) {
$this->checkDuplicates($new_name_scripts);
}
}
/**
* Validate incompatible options ZBX_SCRIPT_TYPE_IPMI and ZBX_SCRIPT_EXECUTE_ON_AGENT.
*
* @param array $scripts
*
* @return array
*/
private function checkExecutionType(array $scripts) {
foreach ($scripts as &$script) {
if (array_key_exists('type', $script) && $script['type'] == ZBX_SCRIPT_TYPE_IPMI) {
if (!array_key_exists('execute_on', $script)) {
$script['execute_on'] = ZBX_SCRIPT_EXECUTE_ON_SERVER;
}
elseif ($script['execute_on'] == ZBX_SCRIPT_EXECUTE_ON_AGENT) {
self::exception(ZBX_API_ERROR_PARAMETERS, _('IPMI scripts can be executed only by server.'));
}
}
}
unset($script);
return $scripts;
}
/**
* Check for valid user groups.
*
* @param array $scripts
* @param array $scripts[]['usrgrpid'] (optional)
*
* @throws APIException if user group is not exists.
*/
private function checkUserGroups(array $scripts) {
$usrgrpids = [];
foreach ($scripts as $script) {
if (array_key_exists('usrgrpid', $script) && $script['usrgrpid'] != 0) {
$usrgrpids[$script['usrgrpid']] = true;
}
}
if (!$usrgrpids) {
return;
}
$usrgrpids = array_keys($usrgrpids);
$db_usrgrps = DB::select('usrgrp', [
'output' => [],
'usrgrpids' => $usrgrpids,
'preservekeys' => true
]);
foreach ($usrgrpids as $usrgrpid) {
if (!array_key_exists($usrgrpid, $db_usrgrps)) {
self::exception(ZBX_API_ERROR_PARAMETERS, _s('User group with ID "%1$s" is not available.', $usrgrpid));
}
}
}
/**
* Check for valid host groups.
*
* @param array $scripts
* @param array $scripts[]['groupid'] (optional)
*
* @throws APIException if host group is not exists.
*/
private function checkHostGroups(array $scripts) {
$groupids = [];
foreach ($scripts as $script) {
if (array_key_exists('groupid', $script) && $script['groupid'] != 0) {
$groupids[$script['groupid']] = true;
}
}
if (!$groupids) {
return;
}
$groupids = array_keys($groupids);
$db_groups = DB::select('hstgrp', [
'output' => [],
'groupids' => $groupids,
'preservekeys' => true
]);
foreach ($groupids as $groupid) {
if (!array_key_exists($groupid, $db_groups)) {
self::exception(ZBX_API_ERROR_PARAMETERS, _s('Host group with ID "%1$s" is not available.', $groupid));
}
}
}
/**
* Auxiliary function for checkDuplicates().
*
* @param array $folders
* @param string $name
* @param array $db_folders
* @param string $db_name
*
* @throws APIException
*/
private static function checkScriptNames(array $folders, $name, array $db_folders, $db_name) {
if (array_slice($folders, 0, count($db_folders)) === $db_folders) {
self::exception(ZBX_API_ERROR_PARAMETERS,
_s('Script menu path "%1$s" already used in script name "%2$s".', $name, $db_name)
);
}
if (array_slice($db_folders, 0, count($folders)) === $folders) {
self::exception(ZBX_API_ERROR_PARAMETERS,
_s('Script name "%1$s" already used in menu path for script "%2$s".', $name, $db_name)
);
}
}
/**
* Check for duplicated scripts.
*
* @param array $scripts
* @param string $scripts['scriptid']
* @param string $scripts['name']
*
* @throws APIException if global script already exists.
*/
private function checkDuplicates(array $scripts) {
$db_scripts = DB::select('scripts', [
'output' => ['scriptid', 'name']
]);
$uniq_names = [];
foreach ($db_scripts as &$db_script) {
$db_script['folders'] = array_map('trim', splitPath($db_script['name']));
$uniq_names[implode('/', $db_script['folders'])] = true;
}
unset($db_script);
$ok_scripts = [];
foreach ($scripts as $script) {
$script['folders'] = array_map('trim', splitPath($script['name']));
$uniq_name = implode('/', $script['folders']);
if (array_key_exists($uniq_name, $uniq_names)) {
self::exception(ZBX_API_ERROR_PARAMETERS, _s('Script "%1$s" already exists.', $script['name']));
}
$uniq_names[$uniq_name] = true;
foreach ($ok_scripts as $ok_script) {
self::checkScriptNames($script['folders'], $script['name'], $ok_script['folders'], $ok_script['name']);
}
foreach ($db_scripts as $db_script) {
if (array_key_exists('scriptid', $script) && bccomp($script['scriptid'], $db_script['scriptid']) == 0) {
continue;
}
self::checkScriptNames($script['folders'], $script['name'], $db_script['folders'], $db_script['name']);
}
$ok_scripts[] = $script;
}
}
/**
* @param array $scriptids
*
* @return array
*/
public function delete(array $scriptids) {
$this->validateDelete($scriptids, $db_scripts);
DB::delete('scripts', ['scriptid' => $scriptids]);
$this->addAuditBulk(AUDIT_ACTION_DELETE, AUDIT_RESOURCE_SCRIPT, $db_scripts);
return ['scriptids' => $scriptids];
}
/**
* @param array $scriptids
* @param array $db_scripts
*
* @throws APIException if the input is invalid
*/
protected function validateDelete(array &$scriptids, array &$db_scripts = null) {
if (self::$userData['type'] != USER_TYPE_SUPER_ADMIN) {
self::exception(ZBX_API_ERROR_PERMISSIONS, _('You do not have permission to perform this operation.'));
}
$api_input_rules = ['type' => API_IDS, 'flags' => API_NOT_EMPTY, 'uniq' => true];
if (!CApiInputValidator::validate($api_input_rules, $scriptids, '/', $error)) {
self::exception(ZBX_API_ERROR_PARAMETERS, $error);
}
$db_scripts = DB::select('scripts', [
'output' => ['scriptid', 'name'],
'scriptids' => $scriptids,
'preservekeys' => true
]);
foreach ($scriptids as $scriptid) {
if (!array_key_exists($scriptid, $db_scripts)) {
self::exception(ZBX_API_ERROR_PERMISSIONS,
_('No permissions to referred object or it does not exist!')
);
}
}
// Check if deleted scripts used in actions.
$db_actions = DBselect(
'SELECT a.name,oc.scriptid'.
' FROM opcommand oc,operations o,actions a'.
' WHERE oc.operationid=o.operationid'.
' AND o.actionid=a.actionid'.
' AND '.dbConditionInt('oc.scriptid', $scriptids),
1
);
if ($db_action = DBfetch($db_actions)) {
self::exception(ZBX_API_ERROR_PARAMETERS,
_s('Cannot delete scripts. Script "%1$s" is used in action operation "%2$s".',
$db_scripts[$db_action['scriptid']]['name'], $db_action['name']
)
);
}
}
/**
* @param array $data
*
* @return array
*/
public function execute(array $data) {
global $ZBX_SERVER, $ZBX_SERVER_PORT;
$api_input_rules = ['type' => API_OBJECT, 'fields' => [
'hostid' => ['type' => API_ID, 'flags' => API_REQUIRED],
'scriptid' => ['type' => API_ID, 'flags' => API_REQUIRED]
]];
if (!CApiInputValidator::validate($api_input_rules, $data, '/', $error)) {
self::exception(ZBX_API_ERROR_PARAMETERS, $error);
}
$db_hosts = API::Host()->get([
'output' => [],
'hostids' => $data['hostid']
]);
if (!$db_hosts) {
self::exception(ZBX_API_ERROR_PERMISSIONS, _('No permissions to referred object or it does not exist!'));
}
$db_scripts = $this->get([
'output' => [],
'hostids' => $data['hostid'],
'scriptids' => $data['scriptid']
]);
if (!$db_scripts) {
self::exception(ZBX_API_ERROR_PERMISSIONS, _('No permissions to referred object or it does not exist!'));
}
// execute script
$zabbix_server = new CZabbixServer($ZBX_SERVER, $ZBX_SERVER_PORT, ZBX_SCRIPT_TIMEOUT, ZBX_SOCKET_BYTES_LIMIT);
$result = $zabbix_server->executeScript($data['scriptid'], $data['hostid'], self::$userData['sessionid']);
if ($result !== false) {
// return the result in a backwards-compatible format
return [
'response' => 'success',
'value' => $result
];
}
else {
self::exception(ZBX_API_ERROR_INTERNAL, $zabbix_server->getError());
}
}
/**
* Returns all the scripts that are available on each given host.
*
* @param $hostIds
*
* @return array
*/
public function getScriptsByHosts($hostIds) {
zbx_value2array($hostIds);
$scriptsByHost = [];
if (!$hostIds) {
return $scriptsByHost;
}
foreach ($hostIds as $hostId) {
$scriptsByHost[$hostId] = [];
}
$scripts = $this->get([
'output' => API_OUTPUT_EXTEND,
'selectHosts' => ['hostid'],
'hostids' => $hostIds,
'sortfield' => 'name',
'preservekeys' => true
]);
if ($scripts) {
// resolve macros
$macrosData = [];
foreach ($scripts as $scriptId => $script) {
if (!empty($script['confirmation'])) {
foreach ($script['hosts'] as $host) {
if (isset($scriptsByHost[$host['hostid']])) {
$macrosData[$host['hostid']][$scriptId] = $script['confirmation'];
}
}
}
}
if ($macrosData) {
$macrosData = CMacrosResolverHelper::resolve([
'config' => 'scriptConfirmation',
'data' => $macrosData
]);
}
foreach ($scripts as $scriptId => $script) {
$hosts = $script['hosts'];
unset($script['hosts']);
// set script to host
foreach ($hosts as $host) {
$hostId = $host['hostid'];
if (isset($scriptsByHost[$hostId])) {
$size = count($scriptsByHost[$hostId]);
$scriptsByHost[$hostId][$size] = $script;
// set confirmation text with resolved macros
if (isset($macrosData[$hostId][$scriptId]) && $script['confirmation']) {
$scriptsByHost[$hostId][$size]['confirmation'] = $macrosData[$hostId][$scriptId];
}
}
}
}
}
return $scriptsByHost;
}
protected function applyQueryOutputOptions($tableName, $tableAlias, array $options, array $sqlParts) {
$sqlParts = parent::applyQueryOutputOptions($tableName, $tableAlias, $options, $sqlParts);
if ($options['output'] != API_OUTPUT_COUNT) {
if ($options['selectGroups'] !== null || $options['selectHosts'] !== null) {
$sqlParts = $this->addQuerySelect($this->fieldId('groupid'), $sqlParts);
$sqlParts = $this->addQuerySelect($this->fieldId('host_access'), $sqlParts);
}
}
return $sqlParts;
}
protected function addRelatedObjects(array $options, array $result) {
$result = parent::addRelatedObjects($options, $result);
// adding groups
if ($options['selectGroups'] !== null && $options['selectGroups'] != API_OUTPUT_COUNT) {
foreach ($result as $scriptId => $script) {
$result[$scriptId]['groups'] = API::HostGroup()->get([
'output' => $options['selectGroups'],
'groupids' => $script['groupid'] ? $script['groupid'] : null,
'editable' => ($script['host_access'] == PERM_READ_WRITE)
]);
}
}
// adding hosts
if ($options['selectHosts'] !== null && $options['selectHosts'] != API_OUTPUT_COUNT) {
$processedGroups = [];
foreach ($result as $scriptId => $script) {
if (isset($processedGroups[$script['groupid'].'_'.$script['host_access']])) {
$result[$scriptId]['hosts'] = $result[$processedGroups[$script['groupid'].'_'.$script['host_access']]]['hosts'];
}
else {
$result[$scriptId]['hosts'] = API::Host()->get([
'output' => $options['selectHosts'],
'groupids' => $script['groupid'] ? $script['groupid'] : null,
'hostids' => $options['hostids'] ? $options['hostids'] : null,
'editable' => ($script['host_access'] == PERM_READ_WRITE)
]);
$processedGroups[$script['groupid'].'_'.$script['host_access']] = $scriptId;
}
}
}
return $result;
}
}
|