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 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909
|
<?php
/*
** Zabbix
** Copyright (C) 2001-2014 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 user macro.
*
* @package API
*/
class CUserMacro extends CZBXAPI {
protected $tableName = 'hostmacro';
protected $tableAlias = 'hm';
protected $sortColumns = array('macro');
/**
* Get UserMacros data.
*
* @param array $options
* @param array $options['nodeids'] node ids
* @param array $options['groupids'] usermacrosgroup ids
* @param array $options['hostids'] host ids
* @param array $options['hostmacroids'] host macros ids
* @param array $options['globalmacroids'] global macros ids
* @param array $options['templateids'] template ids
* @param boolean $options['globalmacro'] only global macros
* @param boolean $options['selectGroups'] select groups
* @param boolean $options['selectHosts'] select hosts
* @param boolean $options['selectTemplates'] select templates
*
* @return array|boolean UserMacros data as array or false if error
*/
public function get($options = array()) {
$result = array();
$userType = self::$userData['type'];
$userid = self::$userData['userid'];
$sqlParts = array(
'select' => array('macros' => 'hm.hostmacroid'),
'from' => array('hostmacro hm'),
'where' => array(),
'order' => array(),
'limit' => null
);
$sqlPartsGlobal = array(
'select' => array('macros' => 'gm.globalmacroid'),
'from' => array('globalmacro gm'),
'where' => array(),
'order' => array(),
'limit' => null
);
$defOptions = array(
'nodeids' => null,
'groupids' => null,
'hostids' => null,
'hostmacroids' => null,
'globalmacroids' => null,
'templateids' => null,
'globalmacro' => null,
'editable' => null,
'nopermissions' => null,
// filter
'filter' => null,
'search' => null,
'searchByAny' => null,
'startSearch' => null,
'excludeSearch' => null,
'searchWildcardsEnabled' => null,
// output
'output' => API_OUTPUT_REFER,
'selectGroups' => null,
'selectHosts' => null,
'selectTemplates' => null,
'countOutput' => null,
'preservekeys' => null,
'sortfield' => '',
'sortorder' => '',
'limit' => null
);
$options = zbx_array_merge($defOptions, $options);
// editable + PERMISSION CHECK
if ($userType != USER_TYPE_SUPER_ADMIN && !$options['nopermissions']) {
if (!is_null($options['editable']) && !is_null($options['globalmacro'])) {
return array();
}
else {
$permission = $options['editable'] ? PERM_READ_WRITE : PERM_READ;
$userGroups = getUserGroupsByUserId($userid);
$sqlParts['where'][] = 'EXISTS ('.
'SELECT NULL'.
' FROM hosts_groups hgg'.
' JOIN rights r'.
' ON r.id=hgg.groupid'.
' AND '.dbConditionInt('r.groupid', $userGroups).
' WHERE hm.hostid=hgg.hostid'.
' GROUP BY hgg.hostid'.
' HAVING MIN(r.permission)>'.PERM_DENY.
' AND MAX(r.permission)>='.$permission.
')';
}
}
// nodeids
$nodeids = !is_null($options['nodeids']) ? $options['nodeids'] : get_current_nodeid();
// global macro
if (!is_null($options['globalmacro'])) {
$sqlPartsGlobal['where'] = sqlPartDbNode($sqlPartsGlobal['where'], 'gm.globalmacroid', $nodeids);
$options['groupids'] = null;
$options['hostmacroids'] = null;
$options['triggerids'] = null;
$options['hostids'] = null;
$options['itemids'] = null;
$options['selectGroups'] = null;
$options['selectTemplates'] = null;
$options['selectHosts'] = null;
}
else {
$sqlParts['where'] = sqlPartDbNode($sqlParts['where'], 'hm.hostmacroid', $nodeids);
}
// globalmacroids
if (!is_null($options['globalmacroids'])) {
zbx_value2array($options['globalmacroids']);
$sqlPartsGlobal['where'][] = dbConditionInt('gm.globalmacroid', $options['globalmacroids']);
}
// hostmacroids
if (!is_null($options['hostmacroids'])) {
zbx_value2array($options['hostmacroids']);
$sqlParts['where'][] = dbConditionInt('hm.hostmacroid', $options['hostmacroids']);
}
// groupids
if (!is_null($options['groupids'])) {
zbx_value2array($options['groupids']);
$sqlParts['select']['groupid'] = 'hg.groupid';
$sqlParts['from']['hosts_groups'] = 'hosts_groups hg';
$sqlParts['where'][] = dbConditionInt('hg.groupid', $options['groupids']);
$sqlParts['where']['hgh'] = 'hg.hostid=hm.hostid';
}
// hostids
if (!is_null($options['hostids'])) {
zbx_value2array($options['hostids']);
$sqlParts['select']['hostid'] = 'hm.hostid';
$sqlParts['where'][] = dbConditionInt('hm.hostid', $options['hostids']);
}
// templateids
if (!is_null($options['templateids'])) {
zbx_value2array($options['templateids']);
$sqlParts['select']['templateid'] = 'ht.templateid';
$sqlParts['from']['macros_templates'] = 'hosts_templates ht';
$sqlParts['where'][] = dbConditionInt('ht.templateid', $options['templateids']);
$sqlParts['where']['hht'] = 'hm.hostid=ht.hostid';
}
// search
if (is_array($options['search'])) {
zbx_db_search('hostmacro hm', $options, $sqlParts);
zbx_db_search('globalmacro gm', $options, $sqlPartsGlobal);
}
// filter
if (is_array($options['filter'])) {
if (isset($options['filter']['macro'])) {
zbx_value2array($options['filter']['macro']);
$sqlParts['where'][] = dbConditionString('hm.macro', $options['filter']['macro']);
$sqlPartsGlobal['where'][] = dbConditionString('gm.macro', $options['filter']['macro']);
}
}
// sorting
$sqlParts = $this->applyQuerySortOptions('hostmacro', 'hm', $options, $sqlParts);
$sqlPartsGlobal = $this->applyQuerySortOptions('globalmacro', 'gm', $options, $sqlPartsGlobal);
// limit
if (zbx_ctype_digit($options['limit']) && $options['limit']) {
$sqlParts['limit'] = $options['limit'];
$sqlPartsGlobal['limit'] = $options['limit'];
}
// init GLOBALS
if (!is_null($options['globalmacro'])) {
$sqlPartsGlobal = $this->applyQueryOutputOptions('globalmacro', 'gm', $options, $sqlPartsGlobal);
$res = DBselect($this->createSelectQueryFromParts($sqlPartsGlobal), $sqlPartsGlobal['limit']);
while ($macro = DBfetch($res)) {
if ($options['countOutput']) {
$result = $macro['rowscount'];
}
else {
if (!isset($result[$macro['globalmacroid']])) {
$result[$macro['globalmacroid']] = array();
}
$result[$macro['globalmacroid']] += $macro;
}
}
}
// init HOSTS
else {
$sqlParts = $this->applyQueryOutputOptions('hostmacro', 'hm', $options, $sqlParts);
$res = DBselect($this->createSelectQueryFromParts($sqlParts), $sqlParts['limit']);
while ($macro = DBfetch($res)) {
if ($options['countOutput']) {
$result = $macro['rowscount'];
}
else {
if (!isset($result[$macro['hostmacroid']])) {
$result[$macro['hostmacroid']] = array();
}
// groupids
if (isset($macro['groupid'])) {
if (!isset($result[$macro['hostmacroid']]['groups'])) {
$result[$macro['hostmacroid']]['groups'] = array();
}
$result[$macro['hostmacroid']]['groups'][] = array('groupid' => $macro['groupid']);
unset($macro['groupid']);
}
// templateids
if (isset($macro['templateid'])) {
if (!isset($result[$macro['hostmacroid']]['templates'])) {
$result[$macro['hostmacroid']]['templates'] = array();
}
$result[$macro['hostmacroid']]['templates'][] = array('templateid' => $macro['templateid']);
unset($macro['templateid']);
}
// hostids
if (isset($macro['hostid'])) {
if (!isset($result[$macro['hostmacroid']]['hosts'])) {
$result[$macro['hostmacroid']]['hosts'] = array();
}
$result[$macro['hostmacroid']]['hosts'][] = array('hostid' => $macro['hostid']);
}
$result[$macro['hostmacroid']] += $macro;
}
}
}
if (!is_null($options['countOutput'])) {
return $result;
}
if ($result) {
$result = $this->addRelatedObjects($options, $result);
$result = $this->unsetExtraFields($result, array('hostid'), $options['output']);
}
// removing keys (hash -> array)
if (is_null($options['preservekeys'])) {
$result = zbx_cleanHashes($result);
}
return $result;
}
/**
* Validates the input parameters for the createGlobal() method.
*
* @param array $globalMacros
*
* @throws APIException if the input is invalid
*/
protected function validateCreateGlobal(array $globalMacros) {
$this->checkGlobalMacrosPermissions(_('Only Super Admins can create global macros.'));
foreach ($globalMacros as $globalMacro) {
$this->checkMacro($globalMacro);
$this->checkValue($globalMacro);
$this->checkUnsupportedFields('globalmacro', $globalMacro,
_s('Wrong fields for macro "%1$s".', $globalMacro['macro']));
}
$this->checkDuplicateMacros($globalMacros);
$this->checkIfGlobalMacrosDontRepeat($globalMacros);
}
/**
* Add global macros.
*
* @param array $globalMacros
*
* @return array
*/
public function createGlobal(array $globalMacros) {
$globalMacros = zbx_toArray($globalMacros);
$this->validateCreateGlobal($globalMacros);
$globalmacroids = DB::insert('globalmacro', $globalMacros);
return array('globalmacroids' => $globalmacroids);
}
/**
* Validates the input parameters for the updateGlobal() method.
*
* @param array $globalMacros
*
* @throws APIException if the input is invalid
*/
protected function validateUpdateGlobal(array $globalMacros) {
$this->checkGlobalMacrosPermissions(_('Only Super Admins can update global macros.'));
foreach ($globalMacros as $globalMacro) {
if (!isset($globalMacro['globalmacroid']) || zbx_empty($globalMacro['globalmacroid'])) {
self::exception(ZBX_API_ERROR_PARAMETERS, _('Invalid method parameters.'));
}
}
$globalMacros = $this->extendObjects('globalmacro', $globalMacros, array('macro'));
foreach ($globalMacros as $globalMacro) {
$this->checkMacro($globalMacro);
$this->checkValue($globalMacro);
$this->checkUnsupportedFields('globalmacro', $globalMacro,
_s('Wrong fields for macro "%1$s".', $globalMacro['macro']));
}
$this->checkDuplicateMacros($globalMacros);
$this->checkIfGlobalMacrosExist(zbx_objectValues($globalMacros, 'globalmacroid'));
$this->checkIfGlobalMacrosDontRepeat($globalMacros);
}
/**
* Updates global macros.
*
* @param array $globalMacros
*
* @return array
*/
public function updateGlobal(array $globalMacros) {
$globalMacros = zbx_toArray($globalMacros);
$this->validateUpdateGlobal($globalMacros);
// update macros
$data = array();
foreach ($globalMacros as $gmacro) {
$globalMacroId = $gmacro['globalmacroid'];
unset($gmacro['globalmacroid']);
$data[] = array(
'values'=> $gmacro,
'where'=> array('globalmacroid' => $globalMacroId)
);
}
DB::update('globalmacro', $data);
return array('globalmacroids' => zbx_objectValues($globalMacros, 'globalmacroid'));
}
/**
* Validates the input parameters for the deleteGlobal() method.
*
* @param array $globalMacroIds
*
* @throws APIException if the input is invalid
*/
protected function validateDeleteGlobal(array $globalMacroIds) {
if (empty($globalMacroIds)) {
self::exception(ZBX_API_ERROR_PARAMETERS, _('Empty input parameter.'));
}
$this->checkGlobalMacrosPermissions(_('Only Super Admins can delete global macros.'));
$this->checkIfGlobalMacrosExist($globalMacroIds);
}
/**
* Delete global macros.
*
* @param mixed $globalMacroIds
*
* @return array
*/
public function deleteGlobal($globalMacroIds) {
$globalMacroIds = zbx_toArray($globalMacroIds);
$this->validateDeleteGlobal($globalMacroIds);
// delete macros
DB::delete('globalmacro', array('globalmacroid' => $globalMacroIds));
return array('globalmacroids' => $globalMacroIds);
}
/**
* Validates the input parameters for the create() method.
*
* @param array $hostMacros
*
* @throws APIException if the input is invalid
*/
protected function validateCreate(array $hostMacros) {
// check the data required for authorization first
foreach ($hostMacros as $hostMacro) {
$this->checkHostId($hostMacro);
}
$this->checkHostPermissions(array_unique(zbx_objectValues($hostMacros, 'hostid')));
foreach ($hostMacros as $hostMacro) {
$this->checkMacro($hostMacro);
$this->checkValue($hostMacro);
$this->checkUnsupportedFields('hostmacro', $hostMacro,
_s('Wrong fields for macro "%1$s".', $hostMacro['macro']));
}
$this->checkDuplicateMacros($hostMacros);
$this->checkIfHostMacrosDontRepeat($hostMacros);
}
/**
* Add new host macros.
*
* @param array $hostMacros an array of host macros
*
* @return array
*/
public function create(array $hostMacros) {
$hostMacros = zbx_toArray($hostMacros);
$this->validateCreate($hostMacros);
$hostmacroids = DB::insert('hostmacro', $hostMacros);
return array('hostmacroids' => $hostmacroids);
}
/**
* Validates the input parameters for the update() method.
*
* @param array $hostMacros
*
* @throws APIException if the input is invalid
*/
protected function validateUpdate(array $hostMacros) {
foreach ($hostMacros as $hostMacro) {
if (!isset($hostMacro['hostmacroid']) || zbx_empty($hostMacro['hostmacroid'])) {
self::exception(ZBX_API_ERROR_PARAMETERS, _('Invalid method parameters.'));
}
}
// make sure we have all the data we need
$hostMacros = $this->extendObjects($this->tableName(), $hostMacros, array('macro', 'hostid'));
$dbHostMacros = $this->get(array(
'hostmacroids' => zbx_objectValues($hostMacros, 'hostmacroid'),
'output' => API_OUTPUT_EXTEND
));
// check the data required for authorization first
foreach ($hostMacros as $hostMacro) {
$this->checkHostId($hostMacro);
}
// check permissions for all affected hosts
$affectedHostIds = array_merge(zbx_objectValues($dbHostMacros, 'hostid'), zbx_objectValues($hostMacros, 'hostid'));
$affectedHostIds = array_unique($affectedHostIds);
$this->checkHostPermissions($affectedHostIds);
foreach ($hostMacros as $hostMacro) {
$this->checkMacro($hostMacro);
$this->checkHostId($hostMacro);
$this->checkValue($hostMacro);
$this->checkUnsupportedFields('hostmacro', $hostMacro,
_s('Wrong fields for macro "%1$s".', $hostMacro['macro']));
}
$this->checkDuplicateMacros($hostMacros);
// check if the macros exist
$this->checkIfHostMacrosExistIn(zbx_objectValues($hostMacros, 'hostmacroid'), $dbHostMacros);
$this->checkIfHostMacrosDontRepeat($hostMacros);
}
/**
* Update host macros
*
* @param array $hostMacros an array of host macros
*
* @return boolean
*/
public function update($hostMacros) {
$hostMacros = zbx_toArray($hostMacros);
$this->validateUpdate($hostMacros);
$data = array();
foreach ($hostMacros as $macro) {
$hostMacroId = $macro['hostmacroid'];
unset($macro['hostmacroid']);
$data[] = array(
'values' => $macro,
'where' => array('hostmacroid' => $hostMacroId)
);
}
DB::update('hostmacro', $data);
return array('hostmacroids' => zbx_objectValues($hostMacros, 'hostmacroid'));
}
/**
* Validates the input parameters for the delete() method.
*
* @param array $hostMacroIds
*
* @throws APIException if the input is invalid
*/
protected function validateDelete(array $hostMacroIds) {
if (!$hostMacroIds) {
self::exception(ZBX_API_ERROR_PARAMETERS, _('Empty input parameter.'));
}
$dbHostMacros = API::getApi()->select('hostmacro', array(
'output' => array('hostid', 'hostmacroid'),
'hostmacroids' => $hostMacroIds
));
// check permissions for all affected hosts
$this->checkHostPermissions(array_unique(zbx_objectValues($dbHostMacros, 'hostid')));
// check if the macros exist
$this->checkIfHostMacrosExistIn($hostMacroIds, $dbHostMacros);
}
/**
* Remove Macros from Hosts
*
* @param mixed $hostMacroIds
*
* @return boolean
*/
public function delete($hostMacroIds) {
$hostMacroIds = zbx_toArray($hostMacroIds);
$this->validateDelete($hostMacroIds);
DB::delete('hostmacro', array('hostmacroid' => $hostMacroIds));
return array('hostmacroids' => $hostMacroIds);
}
/**
* Replace macros on hosts/templates.
* $macros input array has hostid as key and array of that host macros as value.
*
* @param array $macros
*
* @return void
*/
public function replaceMacros(array $macros) {
$hostIds = array_keys($macros);
if (!API::Host()->isWritable($hostIds)) {
self::exception(ZBX_API_ERROR_PERMISSIONS, _('No permissions to referred object or it does not exist!'));
}
$dbMacros = API::Host()->get(array(
'hostids' => $hostIds,
'selectMacros' => API_OUTPUT_EXTEND,
'templated_hosts' => true,
'output' => API_OUTPUT_REFER,
'preservekeys' => true
));
$macroIdsToDelete = array();
$macrosToUpdate = array();
$macrosToAdd = array();
foreach ($macros as $hostid => $hostMacros) {
$dbHostMacros = zbx_toHash($dbMacros[$hostid]['macros'], 'hostmacroid');
// look for db macros which hostmacroids are not in list of new macros
// if there are any, they should be deleted
$hostMacroIds = zbx_toHash($hostMacros, 'hostmacroid');
foreach ($dbHostMacros as $dbHostMacro) {
if (!isset($hostMacroIds[$dbHostMacro['hostmacroid']])) {
$macroIdsToDelete[] = $dbHostMacro['hostmacroid'];
}
}
// if macro has hostmacroid it should be updated otherwise created as new
foreach ($hostMacros as $hostMacro) {
if (isset($hostMacro['hostmacroid']) && isset($dbHostMacros[$hostMacro['hostmacroid']])) {
$macrosToUpdate[] = $hostMacro;
}
else {
$hostMacro['hostid'] = $hostid;
$macrosToAdd[] = $hostMacro;
}
}
}
if ($macroIdsToDelete) {
$this->delete($macroIdsToDelete);
}
if ($macrosToAdd) {
$this->create($macrosToAdd);
}
if ($macrosToUpdate) {
$this->update($macrosToUpdate);
}
}
/**
* Validates the "macro" field.
*
* @param array $macro
*
* @throws APIException if the field is empty, too long or doesn't match the ZBX_PREG_EXPRESSION_USER_MACROS
* regex.
*/
protected function checkMacro(array $macro) {
if (!isset($macro['macro']) || zbx_empty($macro['macro'])) {
self::exception(ZBX_API_ERROR_PARAMETERS, _('Empty macro.'));
}
if (zbx_strlen($macro['macro']) > 64) {
self::exception(ZBX_API_ERROR_PARAMETERS, _s('Macro name "%1$s" is too long, it should not exceed 64 chars.', $macro['macro']));
}
if (!preg_match('/^'.ZBX_PREG_EXPRESSION_USER_MACROS.'$/', $macro['macro'])) {
self::exception(ZBX_API_ERROR_PARAMETERS, _s('Wrong macro "%1$s".', $macro['macro']));
}
}
/**
* Validate the "value" field.
*
* @param array $macro
*
* @throws APIException if the field is too long.
*/
protected function checkValue(array $macro) {
if (isset($macro['value']) && zbx_strlen($macro['value']) > 255) {
self::exception(ZBX_API_ERROR_PARAMETERS, _s('Macro "%1$s" value is too long, it should not exceed 255 chars.', $macro['macro']));
}
}
/**
* Validates the "hostid" field.
*
* @param array $macro
*
* @throws APIException if the field is empty.
*/
protected function checkHostId(array $macro) {
if (!isset($macro['hostid']) || zbx_empty($macro['hostid'])) {
self::exception(ZBX_API_ERROR_PARAMETERS, _s('No host given for macro "%1$s".', $macro['macro']));
}
if (!is_numeric($macro['hostid'])) {
self::exception(ZBX_API_ERROR_PARAMETERS, _s('Invalid hostid for macro "%1$s".', $macro['macro']));
}
}
/**
* Checks if the current user has access to the given hosts and templates. Assumes the "hostid" field is valid.
*
* @throws APIException if the user doesn't have write permissions for the given hosts
*
* @param array $hostIds an array of host or template IDs
*/
protected function checkHostPermissions(array $hostIds) {
if (!API::Host()->isWritable($hostIds)) {
self::exception(ZBX_API_ERROR_PERMISSIONS, _('No permissions to referred object or it does not exist!'));
}
}
/**
* Checks if the given macros contain duplicates. Assumes the "macro" field is valid.
*
* @throws APIException if the given macros contain duplicates
*
* @param array $macros
*
* @return void
*/
protected function checkDuplicateMacros(array $macros) {
$existingMacros = array();
foreach ($macros as $macro) {
// global macros don't have hostid
$hostid = isset($macro['hostid']) ? $macro['hostid'] : 1;
if (isset($existingMacros[$hostid][$macro['macro']])) {
self::exception(ZBX_API_ERROR_PARAMETERS, _s('Macro "%1$s" is not unique.', $macro['macro']));
}
$existingMacros[$hostid][$macro['macro']] = 1;
}
}
/**
* Checks if any of the given host macros already exist on the corresponding hosts. If the macros are updated and
* the "hostmacroid" field is set, the method will only fail, if a macro with a different hostmacroid exists.
* Assumes the "macro", "hostid" and "hostmacroid" fields are valid.
*
* @param array $hostMacros
*
* @throws APIException if any of the given macros already exist
*/
protected function checkIfHostMacrosDontRepeat(array $hostMacros) {
$dbHostMacros = API::getApi()->select($this->tableName(), array(
'output' => array('hostmacroid', 'hostid', 'macro'),
'filter' => array(
'macro' => zbx_objectValues($hostMacros, 'macro'),
'hostid' => array_unique(zbx_objectValues($hostMacros, 'hostid'))
)
));
foreach ($hostMacros as $hostMacro) {
foreach ($dbHostMacros as $dbHostMacro) {
$differentMacros = ((isset($hostMacro['hostmacroid'])
&& bccomp($hostMacro['hostmacroid'], $dbHostMacro['hostmacroid']) != 0)
|| !isset($hostMacro['hostmacroid']));
if ($hostMacro['macro'] == $dbHostMacro['macro'] && bccomp($hostMacro['hostid'], $dbHostMacro['hostid']) == 0
&& $differentMacros) {
$hosts = API::getApi()->select('hosts', array(
'output' => array('name'),
'hostids' => $hostMacro['hostid']
));
$host = reset($hosts);
$error = _s('Macro "%1$s" already exists on "%2$s".', $hostMacro['macro'], $host['name']);
self::exception(ZBX_API_ERROR_PARAMETERS, $error);
}
}
}
}
/**
* Checks if all of the host macros with hostmacrosids given in $hostMacrosIds are present in $hostMacros.
* Assumes the "hostmacroid" field is valid.
*
* @param array $hostMacrosIds
* @param array $hostMacros
*
* @throws APIException if any of the host macros is not present in $hostMacros
*/
protected function checkIfHostMacrosExistIn(array $hostMacrosIds, array $hostMacros) {
$hostMacros = zbx_toHash($hostMacros, 'hostmacroid');
foreach ($hostMacrosIds as $hostMacroId) {
if (!isset($hostMacros[$hostMacroId])) {
self::exception(ZBX_API_ERROR_PARAMETERS, _s('Macro with hostmacroid "%1$s" does not exist.', $hostMacroId));
}
}
}
/**
* Checks if any of the given host global macros already exist. If the macros are updated and
* the "globalmacroid" field is set, the method will only fail, if a macro with a different globalmacroid exists.
* Assumes the "macro", "hostmacroid" fields are valid.
*
* @param array $globalMacros
*
* @throws APIException if any of the given macros already exist
*/
protected function checkIfGlobalMacrosDontRepeat(array $globalMacros) {
$nameMacro = zbx_toHash($globalMacros, 'macro');
$macroNames = zbx_objectValues($globalMacros, 'macro');
if ($macroNames) {
$dbMacros = API::getApi()->select('globalmacro', array(
'filter' => array('macro' => $macroNames),
'output' => array('globalmacroid', 'macro')
));
foreach ($dbMacros as $dbMacro) {
$macro = $nameMacro[$dbMacro['macro']];
if (!isset($macro['globalmacroid']) || bccomp($macro['globalmacroid'], $dbMacro['globalmacroid']) != 0) {
self::exception(ZBX_API_ERROR_PARAMETERS, _s('Macro "%1$s" already exists.', $dbMacro['macro']));
}
}
}
}
/**
* Checks if the user has the permissions to edit global macros.
*
* @param string $error a message that will be used as the error text
*
* @throws APIException if the user doesn't have the required permissions
*/
protected function checkGlobalMacrosPermissions($error) {
if (self::$userData['type'] != USER_TYPE_SUPER_ADMIN) {
self::exception(ZBX_API_ERROR_PERMISSIONS, $error);
}
}
/**
* Checks if all of the global macros with globalmacroids given in $globalMacroIds are present in $globalMacros.
* Assumes the "globalmacroids" field is valid.
*
* @param array $globalMacroIds
*
* @throws APIException if any of the global macros is not present in $globalMacros
*/
protected function checkIfGlobalMacrosExist(array $globalMacroIds) {
$globalMacros = API::getApi()->select('globalmacro', array(
'output' => array('globalmacroid'),
'globalmacroids' => $globalMacroIds
));
$globalMacros = zbx_toHash($globalMacros, 'globalmacroid');
foreach ($globalMacroIds as $globalMacroId) {
if (!isset($globalMacros[$globalMacroId])) {
self::exception(ZBX_API_ERROR_PARAMETERS, _s('Macro with globalmacroid "%1$s" does not exist.', $globalMacroId));
}
}
}
protected function applyQueryOutputOptions($tableName, $tableAlias, array $options, array $sqlParts) {
$sqlParts = parent::applyQueryOutputOptions($tableName, $tableAlias, $options, $sqlParts);
if ($options['output'] != API_OUTPUT_COUNT && $options['globalmacro'] === null) {
if ($options['selectGroups'] !== null || $options['selectHosts'] !== null || $options['selectTemplates'] !== null) {
$sqlParts = $this->addQuerySelect($this->fieldId('hostid'), $sqlParts);
}
}
return $sqlParts;
}
protected function addRelatedObjects(array $options, array $result) {
$result = parent::addRelatedObjects($options, $result);
if ($options['globalmacro'] === null) {
$hostMacroIds = array_keys($result);
/*
* Adding objects
*/
// adding groups
if ($options['selectGroups'] !== null && $options['selectGroups'] != API_OUTPUT_COUNT) {
$res = DBselect(
'SELECT hm.hostmacroid,hg.groupid'.
' FROM hostmacro hm,hosts_groups hg'.
' WHERE '.dbConditionInt('hm.hostmacroid', $hostMacroIds).
' AND hm.hostid=hg.hostid'
);
$relationMap = new CRelationMap();
while ($relation = DBfetch($res)) {
$relationMap->addRelation($relation['hostmacroid'], $relation['groupid']);
}
$groups = API::HostGroup()->get(array(
'output' => $options['selectGroups'],
'groupids' => $relationMap->getRelatedIds(),
'preservekeys' => true
));
$result = $relationMap->mapMany($result, $groups, 'groups');
}
// adding templates
if ($options['selectTemplates'] !== null && $options['selectTemplates'] != API_OUTPUT_COUNT) {
$relationMap = $this->createRelationMap($result, 'hostmacroid', 'hostid');
$templates = API::Template()->get(array(
'output' => $options['selectTemplates'],
'templateids' => $relationMap->getRelatedIds(),
'preservekeys' => true
));
$result = $relationMap->mapMany($result, $templates, 'templates');
}
// adding templates
if ($options['selectHosts'] !== null && $options['selectHosts'] != API_OUTPUT_COUNT) {
$relationMap = $this->createRelationMap($result, 'hostmacroid', 'hostid');
$templates = API::Host()->get(array(
'output' => $options['selectHosts'],
'hostids' => $relationMap->getRelatedIds(),
'preservekeys' => true
));
$result = $relationMap->mapMany($result, $templates, 'hosts');
}
}
return $result;
}
}
|