1<?php 2/////////////////////////////////////////////////////////////////////////////// 3// 4// NagiosQL 5// 6/////////////////////////////////////////////////////////////////////////////// 7// 8// (c) 2005-2020 by Martin Willisegger 9// 10// Project : NagiosQL 11// Component : Import Class 12// Website : https://sourceforge.net/projects/nagiosql/ 13// Version : 3.4.1 14// GIT Repo : https://gitlab.com/wizonet/NagiosQL 15// 16/////////////////////////////////////////////////////////////////////////////////////////////// 17// 18/////////////////////////////////////////////////////////////////////////////////////////////// 19// 20// Class: Data import class 21// 22/////////////////////////////////////////////////////////////////////////////////////////////// 23// 24// Includes any functions to import data from config files 25// 26// Name: nagimport 27// 28/////////////////////////////////////////////////////////////////////////////////////////////// 29namespace functions; 30 31class NagImportClass 32{ 33 // Define class variables 34 private $arrSettings = array(); // Array includes all global settings 35 public $intDomainId = 0; // Configuration domain ID 36 public $strErrorMessage = ''; // String including error messages 37 public $strInfoMessage = ''; // String including information messages 38 private $strList1 = ''; 39 private $strList2 = ''; 40 41 // Class includes 42 /** @var MysqliDbClass */ 43 public $myDBClass; // Database class reference 44 /** @var NagDataClass */ 45 public $myDataClass; // Data processing class reference 46 /** @var NagConfigClass */ 47 public $myConfigClass; // Configuration class reference 48 49 /** 50 * NagImportClass constructor. 51 * @param array $arrSession PHP Session array 52 */ 53 public function __construct($arrSession) 54 { 55 if (isset($arrSession['SETS'])) { 56 // Read global settings 57 $this->arrSettings = $arrSession['SETS']; 58 } 59 if (isset($arrSession['domain'])) { 60 $this->intDomainId = $arrSession['domain']; 61 } 62 } 63 64 /** 65 * Import a config file and writes the values to the database 66 * @param string $strFileNameRaw Import file name 67 * @param int $intConfigId Configuration set id 68 * @param int $intOverwrite 0 = Do not replace existing data 69 * 1 = Replace existing data in tables 70 * @return int 0 = successful / 1 = error 71 * Status messages are stored in class variables 72 */ 73 public function fileImport($strFileNameRaw, $intConfigId, $intOverwrite = 0) 74 { 75 // Define variables 76 $intBlock = 0; 77 $intRemoveTmp = 0; 78 $strImportFile = ''; 79 $strConfLineTemp = ''; 80 $strBlockKey = ''; 81 $arrData = array(); 82 $strFileName = trim($strFileNameRaw); 83 // Get file 84 $intReturn = $this->getImportFile($intConfigId, $strFileName, $strImportFile, $intRemoveTmp); 85 // Open and read config file 86 if ($intReturn == 0) { 87 $resFile = fopen($strImportFile, 'rb'); 88 $intMultiple = 0; 89 while ($resFile && !feof($resFile)) { 90 // Read line and remove blank chars 91 $strConfLine = trim(fgets($resFile)); 92 // Process multi-line configuration instructions 93 if (substr($strConfLine, -1) == '\\') { 94 if ($intMultiple == 0) { 95 $strConfLineTemp = str_replace("\\", ',', $strConfLine); 96 $intMultiple = 1; 97 } else { 98 $strConfLineTemp .= str_replace("\\", ',', $strConfLine); 99 } 100 continue; 101 } 102 if ($intMultiple == 1) { 103 $strConfLine = $strConfLineTemp.$strConfLine; 104 $intMultiple = 0; 105 } 106 // Find NAGIOSQL variable 107 if (substr_count($strConfLine, '#NAGIOSQL_') != 0) { 108 $strConfLine = str_replace('#NAGIOSQL_CONFIG_NAME', '_NAGIOSQL_CONFIG_NAME', $strConfLine); 109 } 110 // Pass comments and empty lines 111 if (0 === strpos($strConfLine, '#')) { 112 continue; 113 } 114 if ($strConfLine == '') { 115 continue; 116 } 117 if (($intBlock == 1) && ($strConfLine == '{')) { 118 continue; 119 } 120 // Process line (remove blanks and cut comments) 121 $strLineTmp = str_replace("\;", ':semi:', $strConfLine); 122 $arrLine = preg_split("/[\s]+/", $strLineTmp); 123 $arrTemp = explode(';', implode(' ', $arrLine)); 124 $strNewLine = str_replace(':semi:', "\;", trim($arrTemp[0])); 125 // Find block begin 126 if ($arrLine[0] == 'define') { 127 $intBlock = 1; 128 $strBlockKey = str_replace('{', '', $arrLine[1]); 129 $arrData = array(); 130 continue; 131 } 132 // Store the block data to an array 133 if (($intBlock == 1) && ($arrLine[0] != '}')) { 134 $strExclude = 'template_name,alias,name,use'; 135 if (($strBlockKey == 'timeperiod') && (!\in_array($arrLine[0], explode(',', $strExclude), true))) { 136 $arrNewLine = explode(' ', $strNewLine); 137 $strTPKey = str_replace(' ' .$arrNewLine[\count($arrNewLine)-1], '', $strNewLine); 138 $strTPValue = $arrNewLine[\count($arrNewLine)-1]; 139 $arrData[$strTPKey] = array('key' => $strTPKey, 140 'value' => $strTPValue); 141 } else { 142 $key = $arrLine[0]; 143 $value = str_replace($arrLine[0]. ' ', '', $strNewLine); 144 // Special retry_check_interval, normal_check_interval 145 if ($key == 'retry_check_interval') { 146 $key = 'retry_interval'; 147 } 148 if ($key == 'normal_check_interval') { 149 $key = 'check_interval'; 150 } 151 $arrData[$arrLine[0]] = array('key' => $key, 'value' => $value); 152 } 153 } 154 // Process data at end of block 155 if ((substr_count($strConfLine, '}') == 1) && ($arrData !== null) && \is_array($arrData)) { 156 $intBlock = 0; 157 $intRetVal = $this->importTable($strBlockKey, $arrData, $intOverwrite); 158 if ($intRetVal != 0) { 159 $intReturn = 1; 160 } 161 } elseif ($arrData === null) { 162 $this->strErrorMessage .= translate('No valid configuration found:'). ' ' .$strFileName. '::'; 163 $intReturn = 1; 164 } 165 } 166 if ($intRemoveTmp == 1) { 167 unlink($strImportFile); 168 } 169 } else { 170 $this->strErrorMessage .= translate('Import file does not exist or is not readable:'). ' ' .$strFileName 171 . '::'; 172 $intReturn = 1; 173 } 174 return $intReturn; 175 } 176 177 // PRIVATE functions 178 179 /** 180 * @param int $intConfigId Configuration set id 181 * @param string $strFileName Configuration file name 182 * @param string|bool $strImportFile Temporary file for data import (by reference) 183 * @param int $intRemoveTmp Remove temporary file (1 = yes / 0 = no) (by reference) 184 * @return int 0 = successful / 1 = error 185 * Status messages are stored in class variables 186 */ 187 private function getImportFile($intConfigId, $strFileName, &$strImportFile, &$intRemoveTmp) 188 { 189 $intMethod = 1; 190 $intReturn = 0; 191 $intRemoveTmp = 0; 192 $strImportFile = ''; 193 $strImportFileTmp = ''; 194 // File transfer method 195 if (substr_count($strFileName, 'nagiosql_local_imp') == 1) { 196 $intMethod = 1; 197 $intRetVal = 0; 198 } else { 199 $intRetVal = $this->myConfigClass->getConfigData($intConfigId, 'method', $intMethod); 200 } 201 if ($intRetVal != 0) { 202 $this->strErrorMessage .= translate('Unable to get configuration data:'). ' method::'; 203 $intReturn = 1; 204 } 205 if ($intReturn == 0) { 206 // Read import file 207 if ($intMethod == 1) { // Local file system 208 if (!is_readable($strFileName)) { 209 $this->strErrorMessage .= translate('Cannot open the data file (check the permissions)!'). ' ' . 210 $strFileName. '::'; 211 $intReturn = 1; 212 } else { 213 $strImportFileTmp = $strFileName; 214 } 215 } elseif ($intMethod == 2) { // FTP access 216 // Open ftp connection 217 $intRetVal = $this->myConfigClass->getFTPConnection($intConfigId); 218 if ($intRetVal != 0) { 219 $this->strErrorMessage .= $this->myConfigClass->strErrorMessage; 220 $intReturn = 1; 221 } else { 222 // Transfer file from remote server to a local temp file 223 if (isset($this->arrSettings['path']) && isset($this->arrSettings['path']['tempdir'])) { 224 $strConfigFile = tempnam($this->arrSettings['path']['tempdir'], 'nagiosql_imp'); 225 } else { 226 $strConfigFile = tempnam(sys_get_temp_dir(), 'nagiosql_imp'); 227 } 228 if (!ftp_get($this->myConfigClass->resConnectId, $strConfigFile, $strFileName, FTP_ASCII)) { 229 $this->strErrorMessage .= translate('Cannot receive the configuration file (FTP connection)!'). 230 '::'; 231 ftp_close($this->myConfigClass->resConnectId); 232 $intReturn = 1; 233 } else { 234 $intRemoveTmp = 1; 235 $strImportFileTmp = $strConfigFile; 236 } 237 } 238 } elseif ($intMethod == 3) { // SSH Access 239 // Open ssh connection 240 $intRetVal = $this->myConfigClass->getSSHConnection($intConfigId); 241 if ($intRetVal != 0) { 242 $this->strErrorMessage .= $this->myConfigClass->strErrorMessage; 243 $intReturn = 1; 244 } else { 245 // Transfer file from remote server to a local temp file 246 if (isset($this->arrSettings['path']) && isset($this->arrSettings['path']['tempdir'])) { 247 $strConfigFile = tempnam($this->arrSettings['path']['tempdir'], 'nagiosql_imp'); 248 } else { 249 $strConfigFile = tempnam(sys_get_temp_dir(), 'nagiosql_imp'); 250 } 251 if (!ssh2_scp_recv($this->myConfigClass->resConnectId, $strFileName, $strConfigFile)) { 252 $this->strErrorMessage .= translate('Cannot receive the configuration file (SSH connection)!'). 253 '::'; 254 $intReturn = 1; 255 } else { 256 $intRemoveTmp = 1; 257 $strImportFileTmp = $strConfigFile; 258 } 259 } 260 } 261 // Open and read config file 262 if (file_exists($strImportFileTmp) && is_readable($strImportFileTmp)) { 263 $strImportFile = $strImportFileTmp; 264 } else { 265 $intReturn = 1; 266 $intRemoveTmp = 0; 267 } 268 } 269 return $intReturn; 270 } 271 272 /** 273 * Writes the block data to the database 274 * @param string $strBlockKey Config key (from define) 275 * @param array $arrImportData Imported block data 276 * @param int $intOverwrite 0 = Do not replace existing data 277 * 1 = Replace existing data in tables 278 * @return int 0 = successful / 1 = error 279 * Status messages are stored in class variables 280 */ 281 private function importTable($strBlockKey, $arrImportData, $intOverwrite) 282 { 283 // Define variables 284 $intIsTemplate = 0; 285 $intExists = 0; 286 $intInsertRelations = 0; 287 $intInsertVariables = 0; 288 $strHash = ''; 289 $strConfigName = ''; 290 $arrImportRelations = array(); 291 $arrFreeVariables = array(); 292 $arrRelations = array(); 293 // Block data from template or real configuration? 294 if (array_key_exists('name', $arrImportData) && (isset($arrImportData['register']) && 295 ($arrImportData['register']['value'] == 0))) { 296 $intIsTemplate = 1; 297 } 298 // Get table name and key for import 299 $intReturn = $this->getTableData($strBlockKey, $intIsTemplate, $strTable, $strKeyField); 300 if ($intReturn == 0) { 301 // Create an import hash if no key field is available 302 if ($strKeyField == '') { 303 $this->createHash($strTable, $arrImportData, $strHash, $strConfigName); 304 $arrImportData['config_name']['key'] = 'config_name'; 305 $arrImportData['config_name']['value'] = $strConfigName; 306 $strKeyField = 'config_name'; 307 } else { 308 $strHash = ''; 309 } 310 // Get relation data 311 $intRelation = $this->myDataClass->tableRelations($strTable, $arrRelations); 312 // Does this entry already exist? 313 if (($intIsTemplate == 0) && ($strKeyField != '') && isset($arrImportData[$strKeyField])) { 314 if ($strHash == '') { 315 // Special key field values 316 if ($strBlockKey == 'hostextinfo') { 317 $strSQL = 'SELECT `id`FROM `tbl_host` ' . 318 "WHERE `host_name`='".$arrImportData[$strKeyField]['value']."'"; 319 $intHost = (int)$this->myDBClass->getFieldData($strSQL); 320 $strSQL = 'SELECT `id` FROM `' .$strTable. '` ' . 321 'WHERE `config_id`=' .$this->intDomainId. ' AND `' .$strKeyField."`='".$intHost."'"; 322 } else { 323 $strSQL = 'SELECT `id` FROM `' .$strTable. '` ' . 324 'WHERE `config_id`=' .$this->intDomainId. ' AND ' . 325 '`' .$strKeyField."`='".$arrImportData[$strKeyField]['value']."'"; 326 } 327 } else { 328 $strSQL = 'SELECT `id` FROM `' .$strTable. '` ' . 329 'WHERE `config_id`=' .$this->intDomainId." AND `import_hash`='".$strHash."'"; 330 } 331 $intExists = $this->myDBClass->getFieldData($strSQL); 332 if ($intExists == false) { 333 $intExists = 0; 334 } 335 } elseif (($intIsTemplate == 1) && ($strKeyField != '') && isset($arrImportData['name'])) { 336 $strSQL = 'SELECT `id` FROM `' .$strTable. '` ' . 337 'WHERE `config_id`=' .$this->intDomainId. ' AND ' . 338 "`template_name`='".$arrImportData['name']['value']."'"; 339 $intExists = $this->myDBClass->getFieldData($strSQL); 340 if ($intExists == false) { 341 $intExists = 0; 342 } 343 } 344 // Entry exsists but should not be overwritten 345 if (($intExists != 0) && ($intOverwrite == 0)) { 346 if ($strKeyField == 'config_name') { 347 $strSQLConfig = 'SELECT `config_name` FROM `' .$strTable. '` WHERE `id`=' .$intExists; 348 $arrImportData[$strKeyField]['value'] = $this->myDBClass->getFieldData($strSQLConfig); 349 } 350 $this->strInfoMessage .= translate('Entry'). ' <b class="blackmessage">' .$strKeyField. ' -> ' . 351 $arrImportData[$strKeyField]['value']. '</b> ' .translate('inside'). ' <b class="blackmessage">' . 352 $strTable. '</b> ' .translate('exists and were not overwritten'). '::'; 353 } elseif (isset($arrImportData[$strKeyField]) && ($arrImportData[$strKeyField] == '*')) { 354 // Do not write "*" values 355 $this->strInfoMessage .= translate('Entry'). ' <b class="blackmessage">' .$strKeyField. ' -> ' . 356 $arrImportData[$strKeyField]['value']. '</b> ' .translate('inside'). ' <b class="blackmessage">' . 357 $strTable. '</b> ' .translate('were not written'). '::'; 358 } else { 359 // Define SQL statement - part 1 360 $this->getSQLPart1( 361 $arrImportData, 362 $strHash, 363 $intExists, 364 $strTable, 365 $strKeyField, 366 $intRelation, 367 $arrRelations, 368 $strSQL1, 369 $strSQL2 370 ); 371 // Read command configurations 372 list($strVCValues, $intWriteConfig, $strVIValues, $strRLValues, $strVWValues) = 373 $this->getImportValues($arrImportData, $strKeyField, $strSQL1, $strTable); 374 375 376 // Build value statemets 377 foreach ($arrImportData as $elem) { 378 // Write text values 379 $intCheckVC = $this->writeTextValues( 380 $elem, 381 $strVCValues, 382 $strSQL1, 383 $intIsTemplate, 384 $intExists, 385 $strTable 386 ); 387 // Write status values 388 $intCheckVI = $this->writeStatusValues($elem, $strVIValues, $strSQL1); 389 // Write integer values 390 $intCheckVW = $this->writeIntegerValues($elem, $strVWValues, $strSQL1); 391 // Write relations 392 $intCheckRel = $this->writeRelations($elem, $strRLValues, $arrImportRelations, $intInsertRelations); 393 // Write free variables 394 $intCheck = $intCheckVC+$intCheckVI+$intCheckVW+$intCheckRel; 395 if ($intCheck == 0) { 396 $arrTemp = array(); 397 $arrTemp['key'] = $elem['key']; 398 $arrTemp['value'] = $elem['value']; 399 $arrFreeVariables[] = $arrTemp; 400 $intInsertVariables = 1; 401 } 402 } 403 $strTemp1 = ''; 404 $strTemp2 = ''; 405 // Update database 406 if ($intWriteConfig == 1) { 407 $booResult = $this->myDBClass->insertData($strSQL1.$strSQL2); 408 } else { 409 $booResult = false; 410 } 411 if ($strKeyField == '') { 412 $strKey = $strConfigName; 413 } else { 414 $strKey = $strKeyField; 415 } 416 if ($booResult != true) { 417 $this->strErrorMessage .= $this->myDBClass->strErrorMessage; 418 if ($strKeyField != '') { 419 $this->strErrorMessage .= translate('Entry'). ' <b class="blackmessage">' .$strKey. ' -> ' . 420 $arrImportData[$strKeyField]['value']. '</b> ' .translate('inside'). 421 ' <b class="blackmessage">' .$strTable. '</b> ' .translate('could not be inserted:'). ' ' . 422 $this->myDBClass->strErrorMessage. '::'; 423 } 424 if ($strKeyField == '') { 425 $this->strErrorMessage .= translate('Entry'). ' <b class="blackmessage">' .$strTemp1. ' -> ' . 426 $strTemp2.translate('inside'). '</b> ' .$strTable. ' <b class="blackmessage">' .$strTable. 427 '</b> ' .translate('could not be inserted:'). ' ' .$this->myDBClass->strErrorMessage. '::'; 428 } 429 return 1; 430 } 431 if ($strKeyField != '') { 432 $this->strInfoMessage .= translate('Entry'). ' <b class="blackmessage">' .$strKey. ' -> ' . 433 $arrImportData[$strKeyField]['value']. '</b> ' .translate('inside'). 434 ' <b class="blackmessage">' .$strTable. '</b> ' .translate('successfully inserted'). '::'; 435 } 436 if ($strKeyField == '') { 437 $this->strInfoMessage .= translate('Entry'). ' <b class="blackmessage">' .$strTemp1. ' -> ' . 438 $strTemp2. '</b> ' .translate('inside'). ' <b class="blackmessage">' .$strTable. 439 '</b> ' .translate('successfully inserted'). '::'; 440 } 441 // Define data ID 442 if ($intExists != 0) { 443 $intDataId = $intExists; 444 } else { 445 $intDataId = $this->myDBClass->intLastId; 446 } 447 // Are there any relations to be filled in? 448 if ($intInsertRelations == 1) { 449 foreach ($arrImportRelations as $elem) { 450 foreach ($arrRelations as $reldata) { 451 if ($reldata['fieldName'] == $elem['key']) { 452 $strValue = $elem['value']; 453 $strKey = $elem['key']; 454 if ($elem['key'] == 'check_command') { 455 $this->writeRelation5($strValue, $intDataId, $strTable, $reldata); 456 } elseif ($reldata['type'] == 1) { 457 $this->writeRelation1( 458 $strKey, 459 $strValue, 460 $intDataId, 461 $strTable, 462 $reldata, 463 $arrImportData 464 ); 465 } elseif ($reldata['type'] == 2) { 466 $this->writeRelation2($strKey, $strValue, $intDataId, $strTable, $reldata); 467 } elseif ($reldata['type'] == 3) { 468 $this->writeRelation3($strValue, $intDataId, $strTable, $reldata); 469 } elseif ($reldata['type'] == 4) { 470 $this->writeRelation4($strKey, $strValue, $intDataId, $strTable, $reldata); 471 } elseif ($reldata['type'] == 5) { 472 $this->writeRelation6($strValue, $intDataId, $strTable, $reldata); 473 } elseif ($reldata['type'] == 6) { 474 $this->writeRelation7($strValue, $intDataId, $strTable, $reldata); 475 } elseif ($reldata['type'] == 7) { 476 $this->writeRelation8($strValue, $intDataId, $strTable, $reldata); 477 } 478 } 479 } 480 } 481 } 482 // Are there any free variables ore time definitions to be filled in? 483 if ($intInsertVariables == 1) { 484 if ($strTable == 'tbl_timeperiod') { 485 // Remove old values 486 $strSQL = "DELETE FROM `tbl_timedefinition` WHERE `tipId` = $intDataId"; 487 $booResult = $this->myDBClass->insertData($strSQL); 488 if ($booResult == false) { 489 $this->strErrorMessage .= $this->myDBClass->strErrorMessage; 490 } 491 foreach ($arrFreeVariables as $elem) { 492 $strSQL = "INSERT INTO `tbl_timedefinition` SET `tipId` = $intDataId, ". 493 "`definition` = '".addslashes($elem['key'])."', ". 494 "`range` = '".addslashes($elem['value'])."'"; 495 $booResult = $this->myDBClass->insertData($strSQL); 496 if ($booResult == false) { 497 $this->strErrorMessage .= $this->myDBClass->strErrorMessage; 498 } 499 } 500 } else { 501 $intRemoveOldVariables = 1; 502 foreach ($arrFreeVariables as $elem) { 503 foreach ($arrRelations as $reldata) { 504 if ($reldata['type'] == 4) { 505 $this->writeRelation4( 506 $elem['key'], 507 $elem['value'], 508 $intDataId, 509 $intRemoveOldVariables, 510 $strTable, 511 $reldata 512 ); 513 $intRemoveOldVariables = 0; 514 } 515 } 516 } 517 } 518 } 519 // Update Table times 520 $this->myDataClass->updateStatusTable($strTable); 521 } 522 } 523 return $intReturn; 524 } 525 526 /** 527 * Get table name and key for import 528 * @param string $strBlockKey Block data key 529 * @param int $intIsTemplate Template data 1 = yes / 0 - no 530 * @param string $strTable Template name 531 * @param string $strKeyField Table key name 532 * @return int 0 = successful / 1 = error 533 */ 534 private function getTableData($strBlockKey, $intIsTemplate, &$strTable, &$strKeyField) 535 { 536 // Define variables 537 $intReturn = 0; 538 $arrTableData['command'] = array('tbl_command', 'command_name'); 539 $arrTableData['contactgroup'] = array('tbl_contactgroup', 'contactgroup_name'); 540 $arrTableData['contact'] = array('tbl_contact', 'contact_name'); 541 $arrTableData['timeperiod'] = array('tbl_timeperiod', 'timeperiod_name'); 542 $arrTableData['host'] = array('tbl_host', 'host_name'); 543 $arrTableData['service'] = array('tbl_service', ''); 544 $arrTableData['hostgroup'] = array('tbl_hostgroup', 'hostgroup_name'); 545 $arrTableData['servicegroup'] = array('tbl_servicegroup', 'servicegroup_name'); 546 $arrTableData['hostescalation'] = array('tbl_hostescalation', ''); 547 $arrTableData['serviceescalation'] = array('tbl_serviceescalation', ''); 548 $arrTableData['hostdependency'] = array('tbl_hostdependency', ''); 549 $arrTableData['servicedependency'] = array('tbl_servicedependency', ''); 550 $arrTableData['hostextinfo'] = array('tbl_hostextinfo', 'host_name'); 551 $arrTableData['serviceextinfo'] = array('tbl_serviceextinfo', ''); 552 $arrTableData['contactgroup'] = array('tbl_contactgroup', 'contactgroup_name'); 553 $arrTableDataTpl['contact'] = array('tbl_contacttemplate', 'name'); 554 $arrTableDataTpl['host'] = array('tbl_hosttemplate', 'name'); 555 $arrTableDataTpl['service'] = array('tbl_servicetemplate', 'name'); 556 557 // Define table name and key 558 if (($intIsTemplate == 0) && isset($arrTableData[$strBlockKey])) { 559 $strTable = $arrTableData[$strBlockKey][0]; 560 /** @noinspection MultiAssignmentUsageInspection */ 561 $strKeyField = $arrTableData[$strBlockKey][1]; 562 } elseif (($intIsTemplate == 1) && isset($arrTableDataTpl[$strBlockKey])) { 563 $strTable = $arrTableDataTpl[$strBlockKey][0]; 564 /** @noinspection MultiAssignmentUsageInspection */ 565 $strKeyField = $arrTableDataTpl[$strBlockKey][1]; 566 } else { 567 $this->strErrorMessage .= translate('Table for import definition').$strBlockKey. 568 translate('is not available!') . '::'; 569 $intReturn = 1; 570 } 571 return $intReturn; 572 } 573 574 /** 575 * Create a unique data hash from table data 576 * @param $strTable 577 * @param $arrBlockData 578 * @param $strHash 579 * @param $strConfigName 580 */ 581 public function createHash($strTable, $arrBlockData, &$strHash, &$strConfigName) 582 { 583 $strRawString = ''; 584 $strConfigName = 'imp_temporary'; 585 if ($strTable == 'tbl_service') { 586 // HASH from any host, any hostgroup and service description - step 1 587 if (isset($arrBlockData['host_name'])) { 588 $strRawString .= $arrBlockData['host_name']['value']. ','; 589 } 590 if (isset($arrBlockData['hostgroup_name'])) { 591 $strRawString .= $arrBlockData['hostgroup_name']['value']. ','; 592 } 593 // Replace *, + and ! in HASH raw string 594 $strRawString = str_replace(array('*,', '!', '+'), array('any,', 'not_', ''), $strRawString); 595 // Create configuration name from NagiosQL variable if exists 596 if (isset($arrBlockData['_NAGIOSQL_CONFIG_NAME'])) { 597 $strConfigName = $arrBlockData['_NAGIOSQL_CONFIG_NAME']['value']; 598 } else { 599 // Create configuration name from first two hosts / hostgroups 600 $arrConfig = explode(',', $strRawString); 601 if (isset($arrConfig[0]) && ($arrConfig[0] != '')) { 602 $strConfigName = 'imp_' .$arrConfig[0]; 603 } 604 if (isset($arrConfig[1]) && ($arrConfig[1] != '')) { 605 $strConfigName .= '_' .$arrConfig[1]; 606 } 607 } 608 // HASH from any host, any hostgroup and service description - step 2 609 if (isset($arrBlockData['service_description'])) { 610 $strRawString .= $arrBlockData['service_description']['value']. ','; 611 } 612 if (isset($arrBlockData['display_name'])) { 613 $strRawString .= $arrBlockData['display_name']['value']. ','; 614 } 615 if (isset($arrBlockData['check_command'])) { 616 $strRawString .= $arrBlockData['check_command']['value']. ','; 617 } 618 } 619 if (($strTable == 'tbl_hostdependency') || ($strTable == 'tbl_servicedependency')) { 620 $strRawString1 = ''; 621 $strRawString2 = ''; 622 $strRawString3 = ''; 623 // HASH from any dependent host and any dependent hostgroup 624 if (isset($arrBlockData['dependent_host_name'])) { 625 $strRawString1 .= $arrBlockData['dependent_host_name']['value']. ','; 626 } 627 if (isset($arrBlockData['dependent_hostgroup_name'])) { 628 $strRawString1 .= $arrBlockData['dependent_hostgroup_name']['value']. ','; 629 } 630 if (isset($arrBlockData['host_name'])) { 631 $strRawString2 .= $arrBlockData['host_name']['value']. ','; 632 } 633 if (isset($arrBlockData['hostgroup_name'])) { 634 $strRawString2 .= $arrBlockData['hostgroup_name']['value']. ','; 635 } 636 if (isset($arrBlockData['dependent_service_description'])) { 637 $strRawString3 .= $arrBlockData['dependent_service_description']['value']. ','; 638 } 639 if (isset($arrBlockData['service_description'])) { 640 $strRawString3 .= $arrBlockData['service_description']['value']. ','; 641 } 642 if (isset($arrBlockData['dependent_servicegroup_name'])) { 643 $strRawString3 .= $arrBlockData['dependent_servicegroup_name']['value']. ','; 644 } 645 if (isset($arrBlockData['servicegroup_name'])) { 646 $strRawString3 .= $arrBlockData['servicegroup_name']['value']. ','; 647 } 648 // Replace *, + and ! in HASH raw string 649 $strRawString1 = str_replace('*,', 'any,', $strRawString1); 650 $strRawString2 = str_replace('*,', 'any,', $strRawString2); 651 $strRawString3 = str_replace('*,', 'any,', $strRawString3); 652 $strRawString1 = str_replace('!', 'not_', $strRawString1); 653 $strRawString2 = str_replace('!', 'not_', $strRawString2); 654 $strRawString3 = str_replace('!', 'not_', $strRawString3); 655 // Create configuration name from NagiosQL variable if exists 656 if (isset($arrBlockData['_NAGIOSQL_CONFIG_NAME'])) { 657 $strConfigName = $arrBlockData['_NAGIOSQL_CONFIG_NAME']['value']; 658 } else { 659 $arrConfig1 = explode(',', $strRawString1); 660 $arrConfig2 = explode(',', $strRawString2); 661 $arrConfig3 = explode(',', $strRawString3); 662 if (isset($arrConfig1[0])) { 663 $strConfigName = 'imp_' .$arrConfig1[0]; 664 } 665 if (isset($arrConfig2[0])) { 666 $strConfigName .= '_' .$arrConfig2[0]; 667 } 668 if (isset($arrConfig3[0])) { 669 $strConfigName .= '_' .$arrConfig3[0]; 670 } 671 $strSQL = 'SELECT * FROM `' .$strTable."` WHERE `config_name`='$strConfigName'"; 672 $booRet = $this->myDBClass->hasDataArray($strSQL, $arrData, $intDC); 673 if ($booRet && ($intDC != 0)) { 674 $intCounter = 1; 675 do { 676 $strConfigNameTemp = $strConfigName. '_' .$intCounter; 677 $strSQL = 'SELECT * FROM `' .$strTable."` WHERE `config_name`='$strConfigNameTemp'"; 678 $booRet = $this->myDBClass->hasDataArray($strSQL, $arrData, $intDC); 679 $intCounter++; 680 } while ($booRet && ($intDC != 0)); 681 $strConfigName = $strConfigNameTemp; 682 } 683 } 684 // HASH string 685 $strRawString = $strRawString1.$strRawString2.$strRawString3; 686 $strRawString = substr($strRawString, 0, -1); 687 } 688 if (($strTable == 'tbl_hostescalation') || ($strTable == 'tbl_serviceescalation')) { 689 $strRawString1 = ''; 690 $strRawString2 = ''; 691 $strRawString3 = ''; 692 // HASH from any host and any hostgroup 693 if (isset($arrBlockData['host_name'])) { 694 $strRawString1 .= $arrBlockData['host_name']['value']. ','; 695 } 696 if (isset($arrBlockData['hostgroup_name'])) { 697 $strRawString1 .= $arrBlockData['hostgroup_name']['value']. ','; 698 } 699 if (isset($arrBlockData['contacts'])) { 700 $strRawString2 .= $arrBlockData['contacts']['value']. ','; 701 } 702 if (isset($arrBlockData['contact_groups'])) { 703 $strRawString2 .= $arrBlockData['contact_groups']['value']. ','; 704 } 705 if (isset($arrBlockData['service_description'])) { 706 $strRawString3 .= $arrBlockData['service_description']['value']. ','; 707 } 708 // Replace *, + and ! in HASH raw string 709 $strRawString1 = str_replace('*,', 'any,', $strRawString1); 710 $strRawString2 = str_replace('*,', 'any,', $strRawString2); 711 $strRawString3 = str_replace('*,', 'any,', $strRawString3); 712 $strRawString1 = str_replace('!', 'not_', $strRawString1); 713 $strRawString2 = str_replace('!', 'not_', $strRawString2); 714 $strRawString3 = str_replace('!', 'not_', $strRawString3); 715 // Create configuration name from NagiosQL variable if exists 716 if (isset($arrBlockData['_NAGIOSQL_CONFIG_NAME'])) { 717 $strConfigName = $arrBlockData['_NAGIOSQL_CONFIG_NAME']['value']; 718 } else { 719 $arrConfig1 = explode(',', $strRawString1); 720 $arrConfig2 = explode(',', $strRawString2); 721 $arrConfig3 = explode(',', $strRawString3); 722 if (isset($arrConfig1[0])) { 723 $strConfigName = 'imp_' .$arrConfig1[0]; 724 } 725 if (isset($arrConfig2[0])) { 726 $strConfigName .= '_' .$arrConfig2[0]; 727 } 728 if (isset($arrConfig3[0])) { 729 $strConfigName .= '_' .$arrConfig3[0]; 730 } 731 $strSQL = 'SELECT * FROM `' .$strTable."` WHERE `config_name`='$strConfigName'"; 732 $booRet = $this->myDBClass->hasDataArray($strSQL, $arrData, $intDC); 733 if ($booRet && ($intDC != 0)) { 734 $intCounter = 1; 735 do { 736 $strConfigNameTemp = $strConfigName. '_' .$intCounter; 737 $strSQL = 'SELECT * FROM `' .$strTable."` WHERE `config_name`='$strConfigNameTemp'"; 738 $booRet = $this->myDBClass->hasDataArray($strSQL, $arrData, $intDC); 739 $intCounter++; 740 } while ($booRet && ($intDC != 0)); 741 $strConfigName = $strConfigNameTemp; 742 } 743 } 744 // HASH string 745 $strRawString = $strRawString1.$strRawString2.$strRawString3; 746 $strRawString = substr($strRawString, 0, -1); 747 } 748 if ($strTable == 'tbl_serviceextinfo') { 749 // HASH from any host, any hostgroup and service description - step 1 750 if (isset($arrBlockData['host_name'])) { 751 $strRawString .= $arrBlockData['host_name']['value']. ','; 752 } 753 if (isset($arrBlockData['service_description'])) { 754 $strRawString .= $arrBlockData['service_description']['value']. ','; 755 } 756 // HASH string 757 $strRawString = substr($strRawString, 0, -1); 758 // Create configuration name from NagiosQL variable if exists 759 if (isset($arrBlockData['_NAGIOSQL_CONFIG_NAME'])) { 760 $strConfigName = $arrBlockData['_NAGIOSQL_CONFIG_NAME']['value']; 761 } else { 762 // Create configuration name from first two items 763 $arrConfig = explode(',', $strRawString); 764 if (isset($arrConfig[0]) && ($arrConfig[0] != '')) { 765 $strConfigName = 'imp_' .$arrConfig[0]; 766 } 767 if (isset($arrConfig[1]) && ($arrConfig[1] != '')) { 768 $strConfigName .= '_' .$arrConfig[1]; 769 } 770 } 771 } 772 while (substr_count($strRawString, ' ') != 0) { 773 $strRawString = str_replace(' ', '', $strRawString); 774 } 775 // Sort hash string 776 $arrTemp = explode(',', $strRawString); 777 sort($arrTemp); 778 $strRawString = implode(',', $arrTemp); 779 $strHash = sha1($strRawString); 780 //echo "Hash: ".$strRawString." --> ".$strHash."<br>"; 781 } 782 783 /** 784 * @param array $arrImportData Imported block data 785 * @param string $strHash Unique data hash 786 * @param int $intExists Does the dataset already exist? 787 * @param string $strTable Table name 788 * @param string $strKeyField Table key file 789 * @param int $intRelation Relation type 790 * @param array $arrRelations Relation array 791 * @param string $strSQL1 SQL statement part 1 792 * @param string $strSQL2 SQL statement part 2 793 */ 794 private function getSQLPart1( 795 $arrImportData, 796 $strHash, 797 $intExists, 798 $strTable, 799 $strKeyField, 800 $intRelation, 801 $arrRelations, 802 &$strSQL1, 803 &$strSQL2 804 ) { 805 // Define variables 806 $intActive = 1; 807 $arrData = array(); 808 $intDataCount = 0; 809 810 if ($strHash != '') { 811 $strHash = " `import_hash`='" . $strHash . "', "; 812 } 813 if ($intExists != 0) { 814 // Update database 815 $strSQL1 = 'UPDATE `' .$strTable. '` SET '; 816 $strSQL2 = ' `config_id`=' .$this->intDomainId.", $strHash `active`='$intActive', ". 817 "`last_modified`=NOW() WHERE `id`=$intExists"; 818 // Keep config name while update 819 if (($strKeyField == 'config_name') && !isset($arrImportData['_NAGIOSQL_CONFIG_NAME'])) { 820 $strSQLConfig = 'SELECT `config_name` FROM `' .$strTable. '` WHERE `id`=' . $intExists; 821 $arrImportData['config_name']['value'] = $this->myDBClass->getFieldData($strSQLConfig); 822 } 823 // Remove free variables 824 if ($intRelation != 0) { 825 foreach ($arrRelations as $relVar) { 826 if ($relVar['type'] == 4) { 827 $strSQL = 'SELECT * FROM `' .$relVar['linkTable']."` WHERE `idMaster`=$intExists"; 828 $booReturn = $this->myDBClass->hasDataArray($strSQL, $arrData, $intDataCount); 829 if ($booReturn && ($intDataCount != 0)) { 830 foreach ($arrData as $elem) { 831 $strSQL = 'DELETE FROM `tbl_variabledefinition` WHERE `id`=' .$elem['idSlave']; 832 $this->myDataClass->dataInsert($strSQL, $intInsertId); 833 } 834 } 835 $strSQL = 'DELETE FROM `' .$relVar['linkTable']."` WHERE `idMaster`=$intExists"; 836 $this->myDataClass->dataInsert($strSQL, $intInsertId); 837 } 838 } 839 } 840 } else { 841 // DB Eintrag einfügen 842 $test=''; 843 $strSQL1 = 'INSERT INTO `' .$strTable."` SET $test"; 844 $strSQL2 = ' `config_id`=' .$this->intDomainId.", $strHash `active`='$intActive', `last_modified`=NOW()"; 845 } 846 } 847 848 /** 849 * @param array $arrImportData Imported block data 850 * @param string $strKeyField Table key file 851 * @param string $strSQL1 SQL statement part 1 852 * @param string $strTable Table name 853 * @return array List of import values 854 */ 855 private function getImportValues($arrImportData, $strKeyField, &$strSQL1, $strTable) 856 { 857 // Description for the values 858 // -------------------------- 859 // $strVCValues = Simple text values, will be stored as varchar / null = 'null' as text value / empty = '' 860 // $strRLValues = Relations - values with relations to other tables 861 // $strVWValues = Integer values - will be stored as INT values / null = -1, / empty values as NULL 862 // $strVIValues = Decision values 0 = no, 1 = yes, 2 = skip, 3 = null 863 864 // Define variables 865 $strVCValues = ''; 866 $strVIValues = ''; 867 $strRLValues = ''; 868 $strVWValues = ''; 869 $intWriteConfig = 0; 870 871 // Read command configurations 872 if ($strKeyField == 'command_name') { 873 $strVCValues = 'command_name,command_line'; 874 // Find out command type 875 if (isset($arrImportData['command_line'])) { 876 if ((substr_count($arrImportData['command_line']['value'], 'ARG1') != 0) || 877 (substr_count($arrImportData['command_line']['value'], 'USER1') != 0)) { 878 $strSQL1 .= '`command_type` = 1,'; 879 } else { 880 $strSQL1 .= '`command_type` = 2,'; 881 } 882 } 883 $intWriteConfig = 1; 884 885 // Read contact configurations 886 } elseif ($strKeyField == 'contact_name') { 887 $strVCValues = 'contact_name,alias,host_notification_options,service_notification_options,email,'; 888 $strVCValues .= 'pager,address1,address2,address3,address4,address5,address6,name'; 889 890 $strVWValues = 'minimum_importance'; 891 892 $strVIValues = 'host_notifications_enabled,service_notifications_enabled,can_submit_commands,'; 893 $strVIValues .= 'retain_status_information,retain_nonstatus_information'; 894 895 $strRLValues = 'contactgroups,host_notification_period,service_notification_period,'; 896 $strRLValues .= 'host_notification_commands,service_notification_commands,use'; 897 $intWriteConfig = 1; 898 899 // Read contactgroup configurations 900 } elseif ($strKeyField == 'contactgroup_name') { 901 $strVCValues = 'contactgroup_name,alias'; 902 903 $strRLValues = 'members,contactgroup_members'; 904 $intWriteConfig = 1; 905 906 // Read timeperiod configurations 907 } elseif ($strKeyField == 'timeperiod_name') { 908 $strVCValues = 'timeperiod_name,alias,name'; 909 910 $strRLValues = 'use,exclude'; 911 $intWriteConfig = 1; 912 913 // Read contacttemplate configurations 914 } elseif (($strKeyField == 'name') && ($strTable == 'tbl_contacttemplate')) { 915 $strVCValues = 'contact_name,alias,host_notification_options,service_notification_options,email,'; 916 $strVCValues .= 'pager,address1,address2,address3,address4,address5,address6,name'; 917 918 $strVWValues = 'minimum_importance'; 919 920 $strVIValues = 'host_notifications_enabled,service_notifications_enabled,can_submit_commands,'; 921 $strVIValues .= 'retain_status_information,retain_nonstatus_information'; 922 923 $strRLValues = 'contactgroups,host_notification_period,service_notification_period,'; 924 $strRLValues .= 'host_notification_commands,service_notification_commands,use'; 925 $intWriteConfig = 1; 926 927 // Read host configurations 928 } elseif ($strTable == 'tbl_host') { 929 $strVCValues = 'host_name,alias,display_name,address,initial_state,flap_detection_options,'; 930 $strVCValues .= 'notification_options,stalking_options,notes,notes_url,action_url,icon_image,'; 931 $strVCValues .= 'icon_image_alt,vrml_image,statusmap_image,2d_coords,3d_coords,name'; 932 933 $strVWValues = 'max_check_attempts,retry_interval,check_interval,freshness_threshold,low_flap_threshold,'; 934 $strVWValues .= 'high_flap_threshold,notification_interval,first_notification_delay,importance'; 935 936 $strVIValues = 'active_checks_enabled,passive_checks_enabled,check_freshness,obsess_over_host,'; 937 $strVIValues .= 'event_handler_enabled,flap_detection_enabled,process_perf_data,retain_status_information,'; 938 $strVIValues .= 'retain_nonstatus_information,notifications_enabled'; 939 940 $strRLValues = 'parents,hostgroups,check_command,use,check_period,event_handler,contacts,contact_groups,'; 941 $strRLValues .= 'notification_period'; 942 $intWriteConfig = 1; 943 944 // Read hosttemplate configurations 945 } elseif (($strKeyField == 'name') && ($strTable == 'tbl_hosttemplate')) { 946 $strVCValues = 'template_name,alias,initial_state,flap_detection_options,notification_options,'; 947 $strVCValues .= 'stalking_options,notes,notes_url,action_url,icon_image,icon_image_alt,vrml_image,'; 948 $strVCValues .= 'statusmap_image,2d_coords,3d_coords,name'; 949 950 $strVWValues = 'max_check_attempts,retry_interval,check_interval,freshness_threshold,low_flap_threshold,'; 951 $strVWValues .= 'high_flap_threshold,notification_interval,first_notification_delay,importance'; 952 953 $strVIValues = 'active_checks_enabled,passive_checks_enabled,check_freshness,obsess_over_host,'; 954 $strVIValues .= 'event_handler_enabled,flap_detection_enabled,process_perf_data,retain_status_information,'; 955 $strVIValues .= 'retain_nonstatus_information,notifications_enabled'; 956 957 $strRLValues = 'parents,hostgroups,check_command,use,check_period,event_handler,contacts,contact_groups,'; 958 $strRLValues .= 'notification_period'; 959 $intWriteConfig = 1; 960 961 // Read hostgroup configurations 962 } elseif ($strKeyField == 'hostgroup_name') { 963 $strVCValues = 'hostgroup_name,alias,notes,notes_url,action_url'; 964 965 $strRLValues = 'members,hostgroup_members'; 966 $intWriteConfig = 1; 967 968 // Read service configurations 969 } elseif ($strTable == 'tbl_service') { 970 $strVCValues = 'service_description,display_name,initial_state,flap_detection_options,stalking_options,'; 971 $strVCValues .= 'notes,notes_url,action_url,icon_image,icon_image_alt,name,config_name,'; 972 $strVCValues .= 'notification_options'; 973 974 $strVWValues = 'max_check_attempts,check_interval,retry_interval,freshness_threshold,low_flap_threshold,'; 975 $strVWValues .= 'high_flap_threshold,notification_interval,first_notification_delay,importance'; 976 977 $strVIValues = 'is_volatile,active_checks_enabled,passive_checks_enabled,parallelize_check,'; 978 $strVIValues .= 'obsess_over_service,check_freshness,event_handler_enabled,flap_detection_enabled,'; 979 $strVIValues .= 'process_perf_data,retain_status_information,retain_nonstatus_information,'; 980 $strVIValues .= 'notifications_enabled'; 981 982 $strRLValues = 'host_name,hostgroup_name,servicegroups,use,check_command,check_period,event_handler,'; 983 $strRLValues .= 'notification_period,contacts,contact_groups,parents'; 984 $intWriteConfig = 1; 985 986 // Read servicetemplate configurations 987 } elseif (($strKeyField == 'name') && ($strTable == 'tbl_servicetemplate')) { 988 $strVCValues = 'template_name,service_description,display_name,initial_state,flap_detection_options,'; 989 $strVCValues .= 'stalking_options,notes,notes_url,action_url,icon_image,icon_image_alt,name,'; 990 $strVCValues .= 'notification_options'; 991 992 $strVWValues = 'max_check_attempts,check_interval,retry_interval,freshness_threshold,low_flap_threshold,'; 993 $strVWValues .= 'high_flap_threshold,notification_interval,first_notification_delay,importance'; 994 995 $strVIValues = 'is_volatile,active_checks_enabled,passive_checks_enabled,parallelize_check,'; 996 $strVIValues .= 'obsess_over_service,check_freshness,event_handler_enabled,flap_detection_enabled,'; 997 $strVIValues .= 'process_perf_data,retain_status_information,retain_nonstatus_information,'; 998 $strVIValues .= 'notifications_enabled'; 999 1000 $strRLValues = 'host_name,hostgroup_name,servicegroups,use,check_command,check_period,event_handler,'; 1001 $strRLValues .= 'notification_period,contacts,contact_groups,parents'; 1002 $intWriteConfig = 1; 1003 1004 // Read servicegroup configurations 1005 } elseif ($strKeyField == 'servicegroup_name') { 1006 $strVCValues = 'servicegroup_name,alias,notes,notes_url,action_url'; 1007 1008 $strRLValues = 'members,servicegroup_members'; 1009 $intWriteConfig = 1; 1010 1011 // Read hostdependency configurations 1012 } elseif ($strTable == 'tbl_hostdependency') { 1013 $strVCValues = 'config_name,execution_failure_criteria,notification_failure_criteria'; 1014 1015 $strVIValues = 'inherits_parent'; 1016 1017 $strRLValues = 'dependent_host_name,dependent_hostgroup_name,host_name,hostgroup_name,dependency_period'; 1018 $intWriteConfig = 1; 1019 1020 // Read hostescalation configurations 1021 } elseif ($strTable == 'tbl_hostescalation') { 1022 $strVCValues = 'config_name,escalation_options'; 1023 1024 $strVWValues = 'first_notification,last_notification,notification_interval'; 1025 1026 $strRLValues = 'host_name,hostgroup_name,contacts,contact_groups,escalation_period'; 1027 $intWriteConfig = 1; 1028 1029 // Read hostextinfo configurations 1030 } elseif ($strTable == 'tbl_hostextinfo') { 1031 $strVCValues = 'notes,notes_url,action_url,icon_image,icon_image_alt,vrml_image,statusmap_image,'; 1032 $strVCValues .= '2d_coords,3d_coords'; 1033 1034 $strRLValues = 'host_name'; 1035 $intWriteConfig = 1; 1036 1037 // Read servicedependency configurations 1038 } elseif ($strTable == 'tbl_servicedependency') { 1039 $strVCValues = 'config_name,execution_failure_criteria,notification_failure_criteria'; 1040 1041 $strVIValues = 'inherits_parent'; 1042 1043 $strRLValues = 'dependent_host_name,dependent_hostgroup_name,dependent_service_description,host_name,'; 1044 $strRLValues .= 'hostgroup_name,dependency_period,service_description,dependent_servicegroup_name,'; 1045 $strRLValues .= 'servicegroup_name'; 1046 $intWriteConfig = 1; 1047 1048 // Read serviceescalation configurations 1049 } elseif ($strTable == 'tbl_serviceescalation') { 1050 $strVCValues = 'config_name,escalation_options'; 1051 1052 $strVIValues = 'first_notification,last_notification,notification_interval'; 1053 1054 $strRLValues = 'host_name,hostgroup_name,contacts,contact_groups,service_description,escalation_period,'; 1055 $strRLValues .= 'servicegroup_name'; 1056 $intWriteConfig = 1; 1057 1058 // Serviceextinfo configurations 1059 } elseif ($strTable == 'tbl_serviceextinfo') { 1060 $strVCValues = 'notes,notes_url,action_url,icon_image,icon_image_alt'; 1061 1062 $strRLValues = 'host_name,service_description'; 1063 $intWriteConfig = 1; 1064 } 1065 1066 // Common values (all configurations) 1067 if ($strVWValues == '') { 1068 $strVWValues = 'register'; 1069 } else { 1070 $strVWValues .= ',register'; 1071 } 1072 return array($strVCValues, $intWriteConfig, $strVIValues, $strRLValues, $strVWValues); 1073 } 1074 1075 /** 1076 * @param $elem 1077 * @param $strVCValues 1078 * @param $strSQL1 1079 * @param $intIsTemplate 1080 * @param $intExists 1081 * @param $strTable 1082 * @return int 1083 */ 1084 private function writeTextValues($elem, $strVCValues, &$strSQL1, $intIsTemplate, $intExists, $strTable) 1085 { 1086 $intCheck = 0; 1087 if (\in_array($elem['key'], explode(',', $strVCValues), true)) { 1088 if (strtolower(trim($elem['value'])) == 'null') { 1089 $strSQL1 .= '`' . $elem['key'] . "` = 'null',"; 1090 } else { 1091 $elem['value'] = addslashes($elem['value']); 1092 if ($intIsTemplate == 1) { 1093 if ($elem['key'] == 'name') { 1094 $strSQL1 .= "template_name = '" . $elem['value'] . "',"; 1095 } elseif (($elem['key'] == 'config_name') && ($intExists != 0)) { 1096 // Do not overwrite config_names during an update! 1097 $strSQLConfig = 'SELECT `config_name` FROM `' . $strTable . '` WHERE `id`=' . $intExists; 1098 $elem['value'] = $this->myDBClass->getFieldData($strSQLConfig); 1099 $strSQL1 .= '`' . $elem['key'] . "` = '" . $elem['value'] . "',"; 1100 } else { 1101 $strSQL1 .= '`' . $elem['key'] . "` = '" . $elem['value'] . "',"; 1102 } 1103 } else { 1104 $strSQL1 .= '`' . $elem['key'] . "` = '" . $elem['value'] . "',"; 1105 } 1106 } 1107 $intCheck = 1; 1108 } 1109 return $intCheck; 1110 } 1111 1112 /** 1113 * @param $elem 1114 * @param $strVIValues 1115 * @param $strSQL1 1116 * @return int 1117 */ 1118 private function writeStatusValues($elem, $strVIValues, &$strSQL1) 1119 { 1120 $intCheck = 0; 1121 if (\in_array($elem['key'], explode(',', $strVIValues), true)) { 1122 if (strtolower(trim($elem['value'])) == 'null') { 1123 $strSQL1 .= '`' . $elem['key'] . '` = 3,'; 1124 } else { 1125 $strSQL1 .= '`' . $elem['key'] . "` = '" . $elem['value'] . "',"; 1126 } 1127 $intCheck = 1; 1128 } 1129 return $intCheck; 1130 } 1131 1132 /** 1133 * @param $elem 1134 * @param $strVWValues 1135 * @param $strSQL1 1136 * @return int 1137 */ 1138 private function writeIntegerValues($elem, $strVWValues, &$strSQL1) 1139 { 1140 $intCheck = 0; 1141 if (\in_array($elem['key'], explode(',', $strVWValues), true)) { 1142 if (strtolower(trim($elem['value'])) == 'null') { 1143 $strSQL1 .= '`' . $elem['key'] . '` = -1,'; 1144 } else { 1145 $strSQL1 .= '`' . $elem['key'] . "` = '" . $elem['value'] . "',"; 1146 } 1147 $intCheck = 1; 1148 } 1149 return $intCheck; 1150 } 1151 1152 /** 1153 * @param array $elem 1154 * @param string $strRLValues 1155 * @param array $arrImportRelations 1156 * @param $intInsertRelations 1157 * @return int 1158 */ 1159 private function writeRelations(&$elem, $strRLValues, &$arrImportRelations, &$intInsertRelations) 1160 { 1161 $intCheck = 0; 1162 if (($intCheck == 0) && \in_array($elem['key'], explode(',', $strRLValues), true)) { 1163 if ($elem['key'] == 'use') { 1164 $elem['key'] = 'use_template'; 1165 } 1166 $arrTemp = array(); 1167 $arrTemp['key'] = $elem['key']; 1168 $arrTemp['value'] = $elem['value']; 1169 $arrImportRelations[] = $arrTemp; 1170 $intInsertRelations = 1; 1171 $intCheck = 1; 1172 } 1173 return $intCheck; 1174 } 1175 1176 /** 1177 * Inserts a relation type 1 (1:1) 1178 * @param string $strKey Data field name 1179 * @param string $strValue Data value 1180 * @param int $intDataId Data ID 1181 * @param string $strDataTable Data table (Master) 1182 * @param array $arrRelData Relation data 1183 * @param array $arrImportData Import Data 1184 */ 1185 public function writeRelation1($strKey, $strValue, $intDataId, $strDataTable, $arrRelData, $arrImportData) 1186 { 1187 // Define variables 1188 $intSlaveId = 0; 1189 if (strtolower(trim($strValue)) == 'null') { 1190 // Update data in master table 1191 $strSQL = 'UPDATE `' .$strDataTable. '` SET `' .$arrRelData['fieldName']. '` = -1 WHERE `id` = ' 1192 .$intDataId; 1193 $booResult = $this->myDBClass->insertData($strSQL); 1194 if ($booResult == false) { 1195 $this->strErrorMessage .= $this->myDBClass->strErrorMessage; 1196 } 1197 } else { 1198 // Decompose data value 1199 $arrValues = explode(',', $strValue); 1200 // Process data values 1201 foreach ($arrValues as $elem) { 1202 $strWhere = ''; 1203 $strLink = ''; 1204 $strAdd = ''; 1205 // Special processing for serviceextinfo 1206 if (($strDataTable == 'tbl_serviceextinfo') && ($strKey == 'service_description')) { 1207 $strLink = 'LEFT JOIN `tbl_lnkServiceToHost` on `tbl_service`.`id`=`idMaster` ' . 1208 'LEFT JOIN `tbl_host` ON `idSlave`=`tbl_host`.`id`'; 1209 $strWhere = "AND `tbl_host`.`host_name`='".$arrImportData['host_name']['value']."'"; 1210 } 1211 // Does the value already exist? 1212 $strSQL = 'SELECT `' .$arrRelData['tableName1']. '`.`id` FROM `' .$arrRelData['tableName1']. 1213 "` $strLink " . 'WHERE `' .$arrRelData['target1']."` = '".$elem."' $strWhere AND ". 1214 '`' .$arrRelData['tableName1']."`.`active`='1' AND ". 1215 '`' .$arrRelData['tableName1']. '`.`config_id`=' .$this->intDomainId; 1216 $strId = $this->myDBClass->getFieldData($strSQL); 1217 if ($strId != '') { 1218 $intSlaveId = (int)$strId; 1219 } 1220 if ($intSlaveId == 0) { 1221 // Insert a temporary value 1222 if (($strDataTable == 'tbl_serviceextinfo') && ($arrRelData['tableName1'] == 'tbl_service')) { 1223 $strAdd = "`config_name`='imp_tmp_by_serviceextinfo',"; 1224 } 1225 $strSQL = 'INSERT INTO `' .$arrRelData['tableName1']. '` ' . 1226 'SET `' .$arrRelData['target1']."` = '".$elem."', ". 1227 "$strAdd `config_id`=".$this->intDomainId.", `active`='0', ". 1228 '`last_modified`=NOW()'; 1229 $booResult = $this->myDBClass->insertData($strSQL); 1230 if ($booResult == false) { 1231 $this->strErrorMessage .= $this->myDBClass->strErrorMessage; 1232 } 1233 $intSlaveId = $this->myDBClass->intLastId; 1234 1235 // Special processing for serviceextinfo 1236 if (($strDataTable == 'tbl_serviceextinfo') && ($strKey == 'service_description')) { 1237 $strSQL = 'SELECT `id` FROM `tbl_host` ' . 1238 "WHERE `host_name`='".$arrImportData['host_name']['value']."'"; 1239 $strId = $this->myDBClass->getFieldData($strSQL); 1240 if ($strId != '') { 1241 $strSQL = 'INSERT INTO `tbl_lnkServiceToHost` ' . 1242 "SET `idMaster` = '".$intSlaveId."', `idSlave` = '".$strId."'"; 1243 $booResult = $this->myDBClass->insertData($strSQL); 1244 if ($booResult == false) { 1245 $this->strErrorMessage .= $this->myDBClass->strErrorMessage; 1246 } 1247 $strSQL = "UPDATE `tbl_service` SET `host_name`=0 WHERE `id`='".$intSlaveId."'"; 1248 $booResult = $this->myDBClass->insertData($strSQL); 1249 if ($booResult == false) { 1250 $this->strErrorMessage .= $this->myDBClass->strErrorMessage; 1251 } 1252 } 1253 } 1254 } 1255 // Update data in master table 1256 $strSQL = 'UPDATE `' .$strDataTable. '` SET `' .$arrRelData['fieldName']. '` = ' .$intSlaveId. ' ' . 1257 'WHERE `id` = ' .$intDataId; 1258 $booResult = $this->myDBClass->insertData($strSQL); 1259 if ($booResult == false) { 1260 $this->strErrorMessage .= $this->myDBClass->strErrorMessage; 1261 } 1262 } 1263 } 1264 } 1265 1266 /** 1267 * Inserts a relation type 2 (1:n) 1268 * @param string $strKey Data field name 1269 * @param string $strValue Data value 1270 * @param int $intDataId Data ID 1271 * @param string $strDataTable Data table (Master) 1272 * @param array $arrRelData Relation data 1273 */ 1274 public function writeRelation2($strKey, $strValue, $intDataId, $strDataTable, $arrRelData) 1275 { 1276 // Does a tploption field exist? 1277 $strSQL = 'SELECT * FROM `' .$strDataTable. '` WHERE `id` = ' .$intDataId; 1278 $this->myDBClass->hasSingleDataset($strSQL, $arrDataset); 1279 $strFieldName = $arrRelData['fieldName']. '_tploptions'; 1280 if (isset($arrDataset[$strFieldName])) { 1281 $intTplOption = 1; 1282 } else { 1283 $intTplOption = 0; 1284 } 1285 // Delete data from link table 1286 $strSQL = 'DELETE FROM `' .$arrRelData['linkTable']. '` WHERE `idMaster` = ' .$intDataId; 1287 $booResult = $this->myDBClass->insertData($strSQL); 1288 if ($booResult == false) { 1289 $this->strErrorMessage .= $this->myDBClass->strErrorMessage; 1290 } 1291 // Define variables 1292 if (strtolower(trim($strValue)) == 'null') { 1293 // Update data in master table 1294 if ($intTplOption == 1) { 1295 $strSQL = 'UPDATE `' .$strDataTable. '` SET `' .$arrRelData['fieldName']. '` = 0, ' . 1296 '`' .$arrRelData['fieldName']. '_tploptions` = 1 WHERE `id` = ' .$intDataId; 1297 } else { 1298 $strSQL = 'UPDATE `' .$strDataTable. '` SET `' .$arrRelData['fieldName']. '` = 0 WHERE `id` = ' . 1299 $intDataId; 1300 } 1301 $this->myDBClass->insertData($strSQL); 1302 } else { 1303 if (0 === strpos(trim($strValue), '+')) { 1304 $intOption = 0; 1305 $strValue = str_replace('+', '', $strValue); 1306 } else { 1307 $intOption = 2; 1308 } 1309 // Decompose data value 1310 $arrValues = explode(',', $strValue); 1311 // Process data values 1312 foreach ($arrValues as $elem) { 1313 if ($elem != '*') { 1314 $strWhere = ''; 1315 $strLink = ''; 1316 // Exclude values 1317 if (0 === strpos($elem, '!')) { 1318 $intExclude = 1; 1319 $elem = substr($elem, 1); 1320 } else { 1321 $intExclude = 0; 1322 } 1323 if ((($strDataTable == 'tbl_servicedependency') || ($strDataTable == 'tbl_serviceescalation')) && 1324 (substr_count($strKey, 'service') != 0) && (substr_count($strKey, 'group') == 0)) { 1325 if (substr_count($strKey, 'depend') != 0) { 1326 $strLink = 'LEFT JOIN `tbl_lnkServiceToHost` on `id`=`idMaster`'; 1327 $strWhere = 'AND `idSlave` IN (' .substr($this->strList1, 0, -1). ')'; 1328 } else { 1329 $strLink = 'LEFT JOIN `tbl_lnkServiceToHost` on `id`=`idMaster`'; 1330 $strWhere = 'AND `idSlave` IN (' .substr($this->strList2, 0, -1). ')'; 1331 } 1332 } 1333 // Does the entry already exist? 1334 $strSQL = 'SELECT `id` FROM `' .$arrRelData['tableName1']."` $strLink ". 1335 'WHERE `' .$arrRelData['target1']."` = '".$elem."' $strWhere AND ". 1336 '`config_id`=' .$this->intDomainId; 1337 $strId = $this->myDBClass->getFieldData($strSQL); 1338 if ($strId != '') { 1339 $intSlaveId = (int)$strId; 1340 } else { 1341 $intSlaveId = 0; 1342 } 1343 if (($intSlaveId == 0) && ($elem != '*')) { 1344 // Insert a temporary value to the target table 1345 $strSQL = 'INSERT INTO `' .$arrRelData['tableName1']. '` ' . 1346 'SET `' .$arrRelData['target1']."`='".$elem."', ". 1347 '`config_id`=' .$this->intDomainId.", `active`='0', `last_modified`=NOW()"; 1348 $booResult = $this->myDBClass->insertData($strSQL); 1349 if ($booResult == false) { 1350 $this->strErrorMessage .= $this->myDBClass->strErrorMessage; 1351 } 1352 $intSlaveId = $this->myDBClass->intLastId; 1353 } 1354 // Insert relations 1355 $strSQL = 'INSERT INTO `' .$arrRelData['linkTable']. '` ' . 1356 'SET `idMaster` = ' .$intDataId. ', `idSlave` = ' .$intSlaveId. ', `exclude`=' .$intExclude; 1357 $booResult = $this->myDBClass->insertData($strSQL); 1358 if ($booResult == false) { 1359 $this->strErrorMessage .= $this->myDBClass->strErrorMessage; 1360 } 1361 // Keep values 1362 if (($strDataTable == 'tbl_servicedependency') || ($strDataTable == 'tbl_serviceescalation')) { 1363 $strTemp = ''; 1364 if (($strKey == 'dependent_host_name') || ($strKey == 'host_name')) { 1365 $strTemp .= $intSlaveId. ','; 1366 } elseif (($strKey == 'dependent_hostgroup_name') || ($strKey == 'hostgroup_name')) { 1367 $arrDataHostgroups = array(); 1368 $intDCHostgroups = 0; 1369 $strSQL = 'SELECT DISTINCT `id` FROM `tbl_host` ' . 1370 'LEFT JOIN `tbl_lnkHostToHostgroup` ON `id`=`tbl_lnkHostToHostgroup`.`idMaster` ' . 1371 'LEFT JOIN `tbl_lnkHostgroupToHost` ON `id`=`tbl_lnkHostgroupToHost`.`idSlave` ' . 1372 "WHERE (`tbl_lnkHostgroupToHost`.`idMaster` = $intSlaveId ". 1373 "OR `tbl_lnkHostToHostgroup`.`idSlave` = $intSlaveId) ". 1374 "AND `active`='1' AND `config_id`=".$this->intDomainId; 1375 $booReturn = $this->myDBClass->hasDataArray($strSQL, $arrDataHostgroups, $intDCHostgroups); 1376 if ($booReturn && ($intDCHostgroups != 0)) { 1377 foreach ($arrDataHostgroups as $elem2) { 1378 $strTemp .= $elem2['id']. ','; 1379 } 1380 } 1381 } 1382 if (substr_count($strKey, 'dependent') != 0) { 1383 $this->strList1 .= $strTemp; 1384 } else { 1385 $this->strList2 .= $strTemp; 1386 } 1387 } 1388 } 1389 // Update field values in master table 1390 if (substr_count($strValue, '*') != 0) { 1391 $intRelValue = 2; 1392 } else { 1393 $intRelValue = 1; 1394 } 1395 if ($intTplOption == 1) { 1396 $strSQL = 'UPDATE `' .$strDataTable. '` SET `' .$arrRelData['fieldName']."`=$intRelValue, ". 1397 '`' .$arrRelData['fieldName']. '_tploptions` = ' .$intOption. ' WHERE `id` = ' .$intDataId; 1398 } else { 1399 $strSQL = 'UPDATE `' .$strDataTable. '` SET `' .$arrRelData['fieldName']."`=$intRelValue ". 1400 'WHERE `id` = ' .$intDataId; 1401 } 1402 $booResult = $this->myDBClass->insertData($strSQL); 1403 if ($booResult == false) { 1404 $this->strErrorMessage .= $this->myDBClass->strErrorMessage; 1405 } 1406 } 1407 } 1408 } 1409 1410 /** 1411 * Inserts a relation type 3 (templates) 1412 * @param string $strValue Data value 1413 * @param int $intDataId Data ID 1414 * @param string $strDataTable Data table (Master) 1415 * @param array $arrRelData Relation data 1416 */ 1417 public function writeRelation3($strValue, $intDataId, $strDataTable, $arrRelData) 1418 { 1419 // Define variables 1420 $intSlaveId = 0; 1421 $intTable = 0; 1422 $intSortNr = 1; 1423 if (strtolower(trim($strValue)) == 'null') { 1424 // Update data in master table 1425 $strSQL = 'UPDATE `' .$strDataTable. '` SET `' .$arrRelData['fieldName']. '` = 0, ' . 1426 '`' .$arrRelData['fieldName']. '_tploptions` = 1 WHERE `id` = ' .$intDataId; 1427 $this->myDBClass->insertData($strSQL); 1428 } else { 1429 if (0 === strpos(trim($strValue), '+')) { 1430 $intOption = 0; 1431 $strValue = str_replace('+', '', $strValue); 1432 } else { 1433 $intOption = 2; 1434 } 1435 // Remove old relations 1436 $strSQL = 'DELETE FROM `' .$arrRelData['linkTable']. '` WHERE `idMaster` = ' .$intDataId; 1437 $booResult = $this->myDBClass->insertData($strSQL); 1438 if ($booResult == false) { 1439 $this->strErrorMessage .= $this->myDBClass->strErrorMessage; 1440 } 1441 // Decompose data value 1442 $arrValues = explode(',', $strValue); 1443 // Process data values 1444 foreach ($arrValues as $elem) { 1445 // Does the template already exist? (table 1) 1446 $strSQL = 'SELECT `id` FROM `' .$arrRelData['tableName1']. '` ' . 1447 'WHERE `' .$arrRelData['target1']."` = '".$elem."' AND `config_id`=".$this->intDomainId; 1448 $strId = $this->myDBClass->getFieldData($strSQL); 1449 if ($strId != '') { 1450 $intSlaveId = (int)$strId; 1451 $intTable = 1; 1452 } 1453 if ($intSlaveId == 0) { 1454 // Does the template already exist? (table 2) 1455 $strSQL = 'SELECT `id` FROM `' .$arrRelData['tableName2']. '` ' . 1456 'WHERE `' .$arrRelData['target2']."` = '".$elem."' AND `config_id`=".$this->intDomainId; 1457 $strId = $this->myDBClass->getFieldData($strSQL); 1458 if ($strId != '') { 1459 $intSlaveId = (int)$strId; 1460 $intTable = 2; 1461 } 1462 } 1463 if ($intSlaveId == 0) { 1464 // Insert a temporary value to the target table 1465 $strSQL = 'INSERT INTO `' .$arrRelData['tableName1']. '` ' . 1466 'SET `' .$arrRelData['target1']."` = '".$elem."', `config_id`=".$this->intDomainId. ', ' . 1467 "`active`='0', `last_modified`=NOW()"; 1468 $booResult = $this->myDBClass->insertData($strSQL); 1469 if ($booResult == false) { 1470 $this->strErrorMessage .= $this->myDBClass->strErrorMessage; 1471 } 1472 $intSlaveId = $this->myDBClass->intLastId; 1473 $intTable = 1; 1474 } 1475 // Insert relations 1476 $strSQL = 'INSERT INTO `' .$arrRelData['linkTable']. '` ' . 1477 'SET `idMaster` = ' .$intDataId. ', `idSlave`=' .$intSlaveId. ', `idSort`=' .$intSortNr. ', ' . 1478 '`idTable` = ' .$intTable; 1479 $booResult = $this->myDBClass->insertData($strSQL); 1480 if ($booResult == false) { 1481 $this->strErrorMessage .= $this->myDBClass->strErrorMessage; 1482 } 1483 $intSortNr++; 1484 $intSlaveId = 0; 1485 // Update field data in master table 1486 $strSQL = 'UPDATE `' .$strDataTable. '` SET `' .$arrRelData['fieldName']. '` = 1, ' . 1487 '`' .$arrRelData['fieldName']. '_tploptions` = ' .$intOption. ' WHERE `id` = ' .$intDataId; 1488 $booResult = $this->myDBClass->insertData($strSQL); 1489 if ($booResult == false) { 1490 $this->strErrorMessage .= $this->myDBClass->strErrorMessage; 1491 } 1492 } 1493 } 1494 } 1495 1496 /** 1497 * Inserts a relation type 4 (free variables) 1498 * @param string $strKey Data field name 1499 * @param string $strValue Data value 1500 * @param int $intDataId Data ID 1501 * @param int $intRemoveData 0 = do not remove data / 1 = do remove data 1502 * @param string $strDataTable Data table (Master) 1503 * @param array $arrRelData Relation data 1504 * @return int 0 = successful / 1 = error 1505 */ 1506 public function writeRelation4($strKey, $strValue, $intDataId, $intRemoveData, $strDataTable, $arrRelData) 1507 { 1508 // Define variables 1509 $intReturn = 0; 1510 // Remove empty variables 1511 if (($strKey == '') || ($strValue == '')) { 1512 $intReturn = 1; 1513 } 1514 // Remove NagiosQL variables 1515 if ($strKey == '_NAGIOSQL_CONFIG_NAME') { 1516 $intReturn = 1; 1517 } 1518 // Remove old variables 1519 if ($intRemoveData == 1) { 1520 $strSQL = 'SELECT * FROM '.$arrRelData['linkTable'].' WHERE idMaster='.$intDataId; 1521 $booResult = $this->myDBClass->hasDataArray($strSQL, $arrLinkData, $intLinkCount); 1522 if ($booResult && ($intLinkCount != 0)) { 1523 /** @var array $arrLinkData */ 1524 foreach ($arrLinkData as $elem) { 1525 $strSQL1 = 'DELETE FROM tbl_variabledefinition WHERE id=' .$elem['idSlave']; 1526 $booResult = $this->myDBClass->insertData($strSQL1); 1527 if ($booResult == false) { 1528 $this->strErrorMessage .= $this->myDBClass->strErrorMessage; 1529 } 1530 $strSQL2 = 'DELETE FROM '.$arrRelData['linkTable'].' WHERE idMaster='.$elem['idMaster']; 1531 $booResult = $this->myDBClass->insertData($strSQL2); 1532 if ($booResult == false) { 1533 $this->strErrorMessage .= $this->myDBClass->strErrorMessage; 1534 } 1535 } 1536 } 1537 } 1538 // Insert free Variables 1539 if ($intReturn == 0) { 1540 // Insert values to the table 1541 $strSQL = "INSERT INTO `tbl_variabledefinition` SET `name` = '" . addslashes($strKey) . "', " . 1542 "`value` = '" . addslashes($strValue) . "', `last_modified`=now()"; 1543 $booResult = $this->myDBClass->insertData($strSQL); 1544 if ($booResult == false) { 1545 $this->strErrorMessage .= $this->myDBClass->strErrorMessage; 1546 } 1547 $intSlaveId = $this->myDBClass->intLastId; 1548 // Insert relations to the table 1549 $strSQL = 'INSERT INTO `' . $arrRelData['linkTable'] . '` ' . 1550 'SET `idMaster` = ' . $intDataId . ', `idSlave` = ' . $intSlaveId; 1551 $booResult = $this->myDBClass->insertData($strSQL); 1552 if ($booResult == false) { 1553 $this->strErrorMessage .= $this->myDBClass->strErrorMessage; 1554 } 1555 // Update data in master table 1556 $strSQL = 'UPDATE `' . $strDataTable . '` SET `use_variables` = 1 WHERE `id` = ' . $intDataId; 1557 $booResult = $this->myDBClass->insertData($strSQL); 1558 if ($booResult == false) { 1559 $this->strErrorMessage .= $this->myDBClass->strErrorMessage; 1560 } 1561 } 1562 return $intReturn; 1563 } 1564 1565 /** 1566 * Inserts a relation type 5 (1:1 check command) 1567 * @param string $strValue Data value 1568 * @param int $intDataId Data ID 1569 * @param string $strDataTable Data table (Master) 1570 * @param array $arrRelData Relation data 1571 */ 1572 public function writeRelation5($strValue, $intDataId, $strDataTable, $arrRelData) 1573 { 1574 // Extract data values 1575 $arrCommand = explode('!', $strValue); 1576 $strValue = $arrCommand[0]; 1577 // Define variables 1578 $intSlaveId = 0; 1579 if (strtolower(trim($strValue)) == 'null') { 1580 // Update data in master table 1581 $strSQL = 'UPDATE `' .$strDataTable. '` SET `' .$arrRelData['fieldName']. '` = -1 WHERE `id` = ' . 1582 $intDataId; 1583 $booResult = $this->myDBClass->insertData($strSQL); 1584 if ($booResult == false) { 1585 $this->strErrorMessage .= $this->myDBClass->strErrorMessage; 1586 } 1587 } else { 1588 // Decompose data values 1589 $arrValues = explode(',', $strValue); 1590 // Process data values 1591 foreach ($arrValues as $elem) { 1592 // Does the entry already exist? 1593 $strSQL = 'SELECT `id` FROM `' .$arrRelData['tableName1']. '` ' . 1594 'WHERE `' .$arrRelData['target1']."` = '".$elem."' AND `config_id`=".$this->intDomainId; 1595 $strId = $this->myDBClass->getFieldData($strSQL); 1596 if ($strId != '') { 1597 $intSlaveId = (int)$strId; 1598 } 1599 if ($intSlaveId == 0) { 1600 // Insert a temporary value in target table 1601 $strSQL = 'INSERT INTO `' .$arrRelData['tableName1']. '` ' . 1602 'SET `' .$arrRelData['target1']."` = '".$elem."', `config_id`=".$this->intDomainId. ', ' . 1603 "`active`='0', `last_modified`=NOW()"; 1604 $booResult = $this->myDBClass->insertData($strSQL); 1605 if ($booResult == false) { 1606 $this->strErrorMessage .= $this->myDBClass->strErrorMessage; 1607 } 1608 $intSlaveId = $this->myDBClass->intLastId; 1609 } 1610 // Update data in master table 1611 $arrCommand[0] = $intSlaveId; 1612 $strValue = implode('!', $arrCommand); 1613 $strSQL = 'UPDATE `' .$strDataTable. '` ' . 1614 'SET `' .$arrRelData['fieldName']."`='".$this->myDBClass->realEscape($strValue)."' ". 1615 'WHERE `id` = ' .$intDataId; 1616 $booResult = $this->myDBClass->insertData($strSQL); 1617 if ($booResult == false) { 1618 $this->strErrorMessage .= $this->myDBClass->strErrorMessage; 1619 } 1620 } 1621 } 1622 } 1623 1624 /////////////////////////////////////////////////////////////////////////////////////////// 1625 /** 1626 * Inserts a relation type 5 (1:n:n service groups) 1627 * @param string $strValue Data value 1628 * @param int $intDataId Data ID 1629 * @param string $strDataTable Data table (Master) 1630 * @param array $arrRelData Relation data 1631 */ 1632 public function writeRelation6($strValue, $intDataId, $strDataTable, $arrRelData) 1633 { 1634 // Define variables 1635 $intSlaveIdH = 0; 1636 $intSlaveIdHG = 0; 1637 // Decompose data value 1638 $arrValues = explode(',', $strValue); 1639 // Remove data from link table 1640 $strSQL = 'DELETE FROM `' .$arrRelData['linkTable']. '` WHERE `idMaster` = ' .$intDataId; 1641 $booResult = $this->myDBClass->insertData($strSQL); 1642 if ($booResult == false) { 1643 $this->strErrorMessage .= $this->myDBClass->strErrorMessage; 1644 } 1645 // Check the sum of elements 1646 if (\count($arrValues) % 2 != 0) { 1647 $this->strErrorMessage .= translate('Error: incorrect number of arguments - cannot import service group ' . 1648 'members'). '::'; 1649 } else { 1650 // Process data values 1651 $intCounter = 1; 1652 foreach ($arrValues as $elem) { 1653 if ($intCounter % 2 == 0) { 1654 // Does the host entry already exist? 1655 $strSQL = 'SELECT `id` FROM `' .$arrRelData['tableName1']. '` ' . 1656 'WHERE `' .$arrRelData['target1']."` = '".$strValue."' AND `active`='1' ". 1657 'AND `config_id`=' .$this->intDomainId; 1658 $strId = $this->myDBClass->getFieldData($strSQL); 1659 if ($strId != '') { 1660 $intSlaveIdH = (int)$strId; 1661 } 1662 // Does a hostgroup entry already exist? 1663 if ($intSlaveIdH == 0) { 1664 $strSQL = "SELECT `id` FROM `tbl_hostgroup` WHERE `hostgroup_name` = '".$strValue."' ". 1665 "AND `active`='1' AND `config_id`=".$this->intDomainId; 1666 $strId = $this->myDBClass->getFieldData($strSQL); 1667 if ($strId != '') { 1668 $intSlaveIdHG = (int)$strId; 1669 } 1670 } 1671 if (($intSlaveIdH == 0) && ($intSlaveIdHG == 0)) { 1672 // Insert a temporary value in table 1673 $strSQL = 'INSERT INTO `' .$arrRelData['tableName1']. '` ' . 1674 'SET `' .$arrRelData['target1']."` = '".$strValue."', ". 1675 '`config_id`=' .$this->intDomainId.", `active`='0', `last_modified`=NOW()"; 1676 $booResult = $this->myDBClass->insertData($strSQL); 1677 if ($booResult == false) { 1678 $this->strErrorMessage .= $this->myDBClass->strErrorMessage; 1679 } 1680 $intSlaveIdH = $this->myDBClass->intLastId; 1681 } 1682 // Does the service entry already exist? 1683 if ($intSlaveIdH != 0) { 1684 $strSQL = 'SELECT `id` FROM `' .$arrRelData['tableName2']. '` ' . 1685 'LEFT JOIN `tbl_lnkServiceToHost` ON `id` = `idMaster` ' . 1686 'WHERE `' .$arrRelData['target2']."` = '".$elem."' AND `idSlave` = ".$intSlaveIdH. ' ' . 1687 'AND `config_id`=' .$this->intDomainId; 1688 $strId = $this->myDBClass->getFieldData($strSQL); 1689 if ($strId == '') { 1690 $strSQL = 'SELECT `id` FROM `' .$arrRelData['tableName2']. '` ' . 1691 'LEFT JOIN `tbl_lnkServiceToHostgroup` ON ' . 1692 '`id`=`tbl_lnkServiceToHostgroup`.`idMaster` ' . 1693 'LEFT JOIN `tbl_lnkHostgroupToHost` ON ' . 1694 '`tbl_lnkHostgroupToHost`.`idMaster`=`tbl_lnkServiceToHostgroup`.`idSlave` ' . 1695 'WHERE `' .$arrRelData['target2']."` = '".$elem."' AND ". 1696 '`tbl_lnkHostgroupToHost`.`idSlave` = ' .$intSlaveIdH. ' AND ' . 1697 "`active`='1' AND `config_id`=".$this->intDomainId; 1698 $strId = $this->myDBClass->getFieldData($strSQL); 1699 } 1700 if ($strId == '') { 1701 $strSQL = 'SELECT `id` FROM `' .$arrRelData['tableName2']. '` ' . 1702 'LEFT JOIN `tbl_lnkServiceToHostgroup` ON ' . 1703 '`id` = `tbl_lnkServiceToHostgroup`.`idMaster` ' . 1704 'LEFT JOIN `tbl_lnkHostToHostgroup` ON ' . 1705 '`tbl_lnkHostToHostgroup`.`idSlave`=`tbl_lnkServiceToHostgroup`.`idSlave` ' . 1706 'WHERE `' .$arrRelData['target2']."` = '".$elem."' AND ". 1707 '`tbl_lnkHostToHostgroup`.`idMaster` = ' .$intSlaveIdH. ' AND ' . 1708 "`active`='1' AND `config_id`=".$this->intDomainId; 1709 $strId = $this->myDBClass->getFieldData($strSQL); 1710 } 1711 } elseif ($intSlaveIdHG != 0) { 1712 $strSQL = 'SELECT `id` FROM `' .$arrRelData['tableName2']. '` ' . 1713 'LEFT JOIN `tbl_lnkServiceToHostgroup` ON `id` = `idMaster` ' . 1714 'WHERE `' .$arrRelData['target2']."` = '".$elem."' AND `idSlave`=".$intSlaveIdHG. ' ' . 1715 "AND `active`='1' AND `config_id`=".$this->intDomainId; 1716 $strId = $this->myDBClass->getFieldData($strSQL); 1717 } 1718 if ($strId != '') { 1719 $intSlaveIdS = (int)$strId; 1720 } else { 1721 $intSlaveIdS = 0; 1722 } 1723 if ($intSlaveIdS == 0) { 1724 // Insert a temporary value in table 1725 $intHostName = 0; 1726 $intHostgroupName = 0; 1727 if ($intSlaveIdH != 0) { 1728 $intHostName = 1; 1729 } elseif ($intSlaveIdHG != 0) { 1730 $intHostgroupName = 1; 1731 } 1732 $strSQL = 'INSERT INTO `' .$arrRelData['tableName2']. '` ' . 1733 "SET `config_name`='imp_tmp_by_servicegroup', `host_name`=$intHostName, ". 1734 "`hostgroup_name`=$intHostgroupName, `".$arrRelData['target2']."` = '".$elem."', ". 1735 '`config_id`=' .$this->intDomainId.", `active`='0', `last_modified`=NOW()"; 1736 $booResult = $this->myDBClass->insertData($strSQL); 1737 if ($booResult == false) { 1738 $this->strErrorMessage .= $this->myDBClass->strErrorMessage; 1739 } 1740 $intSlaveIdS = $this->myDBClass->intLastId; 1741 // Make a relation from temp service to host / hostgroup 1742 if ($intSlaveIdH != 0) { 1743 $strSQL = 'INSERT INTO `tbl_lnkServiceToHost` ' . 1744 "SET `idMaster`='".$intSlaveIdS."', `idSlave`=".$intSlaveIdH.", `exclude`='0'"; 1745 $booResult = $this->myDBClass->insertData($strSQL); 1746 if ($booResult == false) { 1747 $this->strErrorMessage .= $this->myDBClass->strErrorMessage; 1748 } 1749 } elseif ($intSlaveIdHG != 0) { 1750 $strSQL = 'INSERT INTO `tbl_lnkServiceToHostgroup` ' . 1751 "SET `idMaster`='".$intSlaveIdS."', `idSlave`=".$intSlaveIdHG. ', ' . 1752 "`exclude`='0'"; 1753 $booResult = $this->myDBClass->insertData($strSQL); 1754 if ($booResult == false) { 1755 $this->strErrorMessage .= $this->myDBClass->strErrorMessage; 1756 } 1757 } 1758 } 1759 // Insert relation 1760 $strSQL = 'INSERT INTO `' .$arrRelData['linkTable']. '` ' . 1761 'SET `idMaster`=' .$intDataId. ', `idSlaveH`=' .$intSlaveIdH. ', `idSlaveS`=' .$intSlaveIdS; 1762 $booResult = $this->myDBClass->insertData($strSQL); 1763 if ($booResult == false) { 1764 $this->strErrorMessage .= $this->myDBClass->strErrorMessage; 1765 } 1766 // Update data in master table 1767 $strSQL = 'UPDATE `' .$strDataTable. '` ' . 1768 'SET `' .$arrRelData['fieldName']. '` = 1 WHERE `id` = ' .$intDataId; 1769 $booResult = $this->myDBClass->insertData($strSQL); 1770 if ($booResult == false) { 1771 $this->strErrorMessage .= $this->myDBClass->strErrorMessage; 1772 } 1773 } else { 1774 $strValue = $elem; 1775 } 1776 $intCounter++; 1777 } 1778 } 1779 } 1780 1781 /** 1782 * Inserts a relation type 6 (1:n:str) 1783 * @param string $strValue Data value 1784 * @param int $intDataId Data ID 1785 * @param string $strDataTable Data table (Master) 1786 * @param array $arrRelData Relation data 1787 */ 1788 public function writeRelation7($strValue, $intDataId, $strDataTable, $arrRelData) 1789 { 1790 // Delete data from link table 1791 $strSQL = 'DELETE FROM `' .$arrRelData['linkTable']. '` WHERE `idMaster` = ' .$intDataId; 1792 $booResult = $this->myDBClass->insertData($strSQL); 1793 if ($booResult == false) { 1794 $this->strErrorMessage .= $this->myDBClass->strErrorMessage; 1795 } 1796 // Decompose data value 1797 $arrValues = explode(',', $strValue); 1798 // Process data values 1799 foreach ($arrValues as $elem) { 1800 if ($elem != '*') { 1801 $strWhere = ''; 1802 // Exclude values 1803 if (0 === strpos($elem, '!')) { 1804 $intExclude = 1; 1805 $elem = substr($elem, 1); 1806 } else { 1807 $intExclude = 0; 1808 } 1809 // Does the entry already exist? 1810 $strSQL = 'SELECT `id` FROM `' .$arrRelData['tableName1']. '` ' . 1811 'WHERE `' .$arrRelData['target1']."`='".$elem."' $strWhere ". 1812 'AND `config_id`=' .$this->intDomainId; 1813 $strId = $this->myDBClass->getFieldData($strSQL); 1814 if ($strId != '') { 1815 $intSlaveId = (int)$strId; 1816 } else { 1817 $intSlaveId = 0; 1818 } 1819 if (($intSlaveId == 0) && ($elem != '*')) { 1820 // Insert a temporary value to the target table 1821 $strSQL = 'INSERT INTO `' .$arrRelData['tableName1']. '` ' . 1822 'SET `' .$arrRelData['target1']."` = '".$elem."', `host_name`=2, `hostgroup_name`=2, ". 1823 "`config_name`='imp_tmp_by_servicedependency', `config_id`=".$this->intDomainId. ', ' . 1824 "`active`='0', `last_modified`=NOW()"; 1825 $booResult = $this->myDBClass->insertData($strSQL); 1826 if ($booResult == false) { 1827 $this->strErrorMessage .= $this->myDBClass->strErrorMessage; 1828 } 1829 $intSlaveId = $this->myDBClass->intLastId; 1830 } 1831 // Insert relations 1832 $strSQL = 'INSERT INTO `' .$arrRelData['linkTable']. '` ' . 1833 'SET `idMaster` = ' .$intDataId. ', `idSlave` = ' .$intSlaveId.", `strSlave`='".$elem."', ". 1834 '`exclude`=' .$intExclude; 1835 $booResult = $this->myDBClass->insertData($strSQL); 1836 if ($booResult == false) { 1837 $this->strErrorMessage .= $this->myDBClass->strErrorMessage; 1838 } 1839 } 1840 // Update field values in master table 1841 if (substr_count($strValue, '*') != 0) { 1842 $intRelValue = 2; 1843 } else { 1844 $intRelValue = 1; 1845 } 1846 $strSQL = 'UPDATE `' .$strDataTable. '` ' . 1847 'SET `' .$arrRelData['fieldName']."` = $intRelValue WHERE `id` = ".$intDataId; 1848 $booResult = $this->myDBClass->insertData($strSQL); 1849 if ($booResult == false) { 1850 $this->strErrorMessage .= $this->myDBClass->strErrorMessage; 1851 } 1852 } 1853 } 1854 1855 /** 1856 * Inserts a relation type 6 (service and servicetemplate parents - 1:service:host) 1857 * @param string $strValue Data value 1858 * @param int $intDataId Data ID 1859 * @param string $strDataTable Data table (Master) 1860 * @param array $arrRelData Relation data 1861 */ 1862 public function writeRelation8($strValue, $intDataId, $strDataTable, $arrRelData) 1863 { 1864 // Decompose data value 1865 $arrValues = explode(',', $strValue); 1866 // Delete data from link table 1867 $strSQL = 'DELETE FROM `' .$arrRelData['linkTable']. '` WHERE `idMaster` = ' .$intDataId; 1868 $booResult = $this->myDBClass->insertData($strSQL); 1869 if ($booResult == false) { 1870 $this->strErrorMessage .= $this->myDBClass->strErrorMessage; 1871 } 1872 // Check the sum of elements 1873 /** @noinspection ExplodeMissUseInspection */ 1874 if (count($arrValues) % 2 != 0) { 1875 $this->strErrorMessage .= translate('Error: incorrect number of arguments - cannot import service parent ' . 1876 'members'). '::'; 1877 } else { 1878 // Process data values 1879 $intCounter = 1; 1880 $strHostName = ''; 1881 foreach ($arrValues as $elem) { 1882 if ($intCounter % 2 == 0) { 1883 $strServiceName = $elem; 1884 if (($strServiceName != '') && ($strHostName != '')) { 1885 $strSQL = 'SELECT tbl_service.id AS id_1, C.id AS id_2, D.id AS id_3, E.id AS id_4 ' 1886 . 'FROM tbl_service ' 1887 . 'LEFT JOIN tbl_lnkServiceToHost ON tbl_service.id=tbl_lnkServiceToHost.idMaster ' 1888 . 'LEFT JOIN tbl_lnkServiceToHostgroup ' 1889 . 'ON tbl_service.id=tbl_lnkServiceToHostgroup.idMaster ' 1890 . 'LEFT JOIN tbl_lnkHostgroupToHost AS A ON tbl_lnkServiceToHostgroup.idSlave=A.idMaster ' 1891 . 'LEFT JOIN tbl_lnkHostToHostgroup AS B ON tbl_lnkServiceToHostgroup.idSlave=B.idSlave ' 1892 . 'LEFT JOIN tbl_host AS C ON A.idSlave=C.id ' 1893 . 'LEFT JOIN tbl_host AS D ON B.idMaster=D.id ' 1894 . 'LEFT JOIN tbl_host AS E ON tbl_lnkServiceToHost.idSlave=E.id ' 1895 . "WHERE tbl_service.service_description='".$strServiceName."' " 1896 . "AND (C.host_name='".$strHostName."' OR D.host_name='".$strHostName."' " 1897 . "OR E.host_name='".$strHostName."')"; 1898 $booResult = $this->myDBClass->hasDataArray($strSQL, $arrDataset, $intCount); 1899 if ($booResult && ($intCount == 1)) { 1900 $intServiceId = 0; 1901 $intHostId = 0; 1902 $intId1 = $arrDataset[0]['id_1']; 1903 $intId2 = $arrDataset[0]['id_2']; 1904 $intId3 = $arrDataset[0]['id_3']; 1905 $intId4 = $arrDataset[0]['id_4']; 1906 if (($intId1!= null) && ($intId1 != 0) && ($intServiceId == 0)) { 1907 $intServiceId = (int)$intId1; 1908 } 1909 $intHostSum = 0; 1910 if (($intId2 != null) && ($intId2 != 0) && ($intHostId == 0)) { 1911 $intHostId = (int)$intId2; 1912 $intHostSum += $intHostId; 1913 } 1914 if (($intId3 != null) && ($intId3 != 0) && ($intHostId == 0)) { 1915 $intHostId = (int)$intId3; 1916 $intHostSum += $intHostId; 1917 } 1918 if (($intId4 != null) && ($intId4 != 0) && ($intHostId == 0)) { 1919 $intHostId = (int)$intId4; 1920 $intHostSum += $intHostId; 1921 } 1922 if (($intHostId == $intHostSum) && ($intServiceId != 0) && ($intHostId != 0)) { 1923 $strSQL = 'INSERT INTO ' .$arrRelData['linkTable']. ' ' 1924 . "SET idMaster=$intDataId, idSlave=$intServiceId, idHost=$intHostId"; 1925 $booResult = $this->myDBClass->insertData($strSQL); 1926 if ($booResult == false) { 1927 $this->strErrorMessage .= $this->myDBClass->strErrorMessage; 1928 } 1929 $strSQL = 'UPDATE `' .$strDataTable. '` ' . 1930 'SET `' .$arrRelData['fieldName']. '` = 1 WHERE `id` = ' .$intDataId; 1931 $booResult = $this->myDBClass->insertData($strSQL); 1932 if ($booResult == false) { 1933 $this->strErrorMessage .= $this->myDBClass->strErrorMessage; 1934 } 1935 } else { 1936 $this->strErrorMessage .= translate('Error: cannot import the service parent member ') 1937 . $strServiceName . '-' . $strHostName . '. ' 1938 . translate('This combination is not unique!') . '::'; 1939 } 1940 } else { 1941 $this->strErrorMessage .= translate('Error: cannot import the service parent member ') 1942 . $strServiceName. '-' .$strHostName. '. ' 1943 . translate('This combination is not unique or does not exist!').'::'; 1944 } 1945 } 1946 } else { 1947 $strHostName = $elem; 1948 } 1949 $intCounter++; 1950 } 1951 } 1952 } 1953} 1954