1<?php
2///////////////////////////////////////////////////////////////////////////////
3//
4// NagiosQL
5//
6///////////////////////////////////////////////////////////////////////////////
7//
8// (c) 2005-2020 by Martin Willisegger
9//
10// Project   : NagiosQL
11// Component : Host definition
12// Website   : https://sourceforge.net/projects/nagiosql/
13// Version   : 3.4.1
14// GIT Repo  : https://gitlab.com/wizonet/NagiosQL
15//
16///////////////////////////////////////////////////////////////////////////////
17//
18// Path settings
19// ===================
20$strPattern = '(admin/[^/]*.php)';
21$preRelPath  = preg_replace($strPattern, '', filter_input(INPUT_SERVER, 'PHP_SELF', FILTER_SANITIZE_STRING));
22$preBasePath = preg_replace($strPattern, '', filter_input(INPUT_SERVER, 'SCRIPT_FILENAME', FILTER_SANITIZE_STRING));
23//
24// Define common variables
25// =======================
26$prePageId        = 8;
27$preContent       = 'admin/hosts.htm.tpl';
28$preListTpl       = 'admin/datalist_hosts.htm.tpl';
29$preSearchSession = 'host';
30$preTableName     = 'tbl_host';
31$preKeyField      = 'host_name';
32$preAccess        = 1;
33$preFieldvars     = 1;
34$strSqlParents    = '';
35//
36// Include preprocessing files
37// ===========================
38require $preBasePath.'functions/prepend_adm.php';
39require $preBasePath.'functions/prepend_content.php';
40//
41// Data processing
42// ===============
43$strNO = substr($chkChbGr1a.$chkChbGr1b.$chkChbGr1c.$chkChbGr1d.$chkChbGr1e, 0, -1);
44$strIS = substr($chkChbGr2a.$chkChbGr2b.$chkChbGr2c, 0, -1);
45$strFL = substr($chkChbGr3a.$chkChbGr3b.$chkChbGr3c, 0, -1);
46$strST = substr($chkChbGr4a.$chkChbGr4b.$chkChbGr4c, 0, -1);
47if ($chkSelValue1 != '') {
48    for ($i = 1; $i <= 8; $i++) {
49        $tmpVar = 'chkTfArg'.$i;
50        $$tmpVar = str_replace('!', '::bang::', $$tmpVar);
51        if ($$tmpVar != '') {
52            $chkSelValue1 .= '!' .$$tmpVar;
53        }
54    }
55}
56//
57// Add or modify data
58// ==================
59if ((($chkModus == 'insert') || ($chkModus == 'modify')) && ($intGlobalWriteAccess == 0)) {
60    if ($SETS['performance']['parents'] == 1) {
61        $strSqlParents = "`parents`=$intMselValue1, `parents_tploptions`=$chkRadValue1,";
62    }
63    $strSQLx = "`$preTableName` SET `$preKeyField`='$chkTfValue1', `alias`='$chkTfValue3', "
64             . "`display_name`='$chkTfValue4', `address`='$chkTfValue5', `name`='$chkTfValue6', "
65             . "$strSqlParents `importance`=$chkTfNullVal9, `hostgroups`=$intMselValue2, "
66             . "`hostgroups_tploptions`=$chkRadValue2, `check_command`='$chkSelValue1', `use_template`=$intTemplates, "
67             . "`initial_state`='$strIS', `max_check_attempts`=$chkTfNullVal2, `check_interval`=$chkTfNullVal3, "
68             . "`retry_interval`=$chkTfNullVal1, `active_checks_enabled`=$chkRadValue5, "
69             . "`passive_checks_enabled`=$chkRadValue6, `check_period`=$chkSelValue2, "
70             . "`obsess_over_host`=$chkRadValue8, `check_freshness`=$chkRadValue7, "
71             . "`freshness_threshold`=$chkTfNullVal4, `event_handler`=$chkSelValue3, "
72             . "`event_handler_enabled`=$chkRadValue9, `low_flap_threshold`=$chkTfNullVal5, "
73             . "`high_flap_threshold`=$chkTfNullVal6, `flap_detection_enabled`=$chkRadValue10, "
74             . "`flap_detection_options`='$strFL', `process_perf_data`=$chkRadValue13, "
75             . "`retain_status_information`=$chkRadValue11, `retain_nonstatus_information`=$chkRadValue12, "
76             . "`contacts`=$intMselValue3, `contacts_tploptions`=$chkRadValue3, `contact_groups`=$intMselValue4, "
77             . "`contact_groups_tploptions`=$chkRadValue4, `notification_interval`=$chkTfNullVal7, "
78             . "`notification_period`=$chkSelValue4, `first_notification_delay`=$chkTfNullVal8, "
79             . "`notification_options`='$strNO', `notifications_enabled`=$chkRadValue14, `stalking_options`='$strST', "
80             . "`notes`='$chkTfValue7', `notes_url`='$chkTfValue9', `action_url`='$chkTfValue11', "
81             . "`icon_image`='$chkTfValue12', `icon_image_alt`='$chkTfValue13', `vrml_image`='$chkTfValue8', "
82             . "`statusmap_image`='$chkTfValue10', `2d_coords`='$chkTfValue14', `3d_coords`='$chkTfValue15', "
83             . $preSQLCommon1;
84    if ($chkModus == 'insert') {
85        $strSQL = 'INSERT INTO ' .$strSQLx;
86    } else {
87        $strSQL = 'UPDATE ' .$strSQLx. ' WHERE `id`=' .$chkDataId;
88    }
89    if ($intWriteAccessId == 0) {
90        if (($chkTfValue1 != '') && ($chkTfValue5 != '')) {
91            $intReturn = $myDataClass->dataInsert($strSQL, $intInsertId);
92            if ($chkModus == 'insert') {
93                $chkDataId = $intInsertId;
94            }
95            if ($intReturn == 1) {
96                $myVisClass->processMessage($myDataClass->strErrorMessage, $strErrorMessage);
97            } else {
98                $myVisClass->processMessage($myDataClass->strInfoMessage, $strInfoMessage);
99                if ($chkModus == 'insert') {
100                    $myDataClass->writeLog(translate('New host inserted:'). ' ' .$chkTfValue1);
101                }
102                if ($chkModus == 'modify') {
103                    $myDataClass->writeLog(translate('Host modified:'). ' ' .$chkTfValue1);
104                }
105                //
106                // Insert/update relations
107                // =======================
108                if ($chkModus == 'insert') {
109                    if ($SETS['performance']['parents'] == 1) {
110                        if ($intMselValue1 != 0) {
111                            $intRet1 = $myDataClass->dataInsertRelation('tbl_lnkHostToHost', $chkDataId, $chkMselValue1);
112                        }
113                        if (isset($intRet1) && ($intRet1 != 0)) {
114                            $myVisClass->processMessage($myDataClass->strErrorMessage, $strErrorMessage);
115                        }
116                    }
117                    if ($intMselValue2 != 0) {
118                        $intRet2 = $myDataClass->dataInsertRelation(
119                            'tbl_lnkHostToHostgroup',
120                            $chkDataId,
121                            $chkMselValue2
122                        );
123                    }
124                    if (isset($intRet2) && ($intRet2 != 0)) {
125                        $myVisClass->processMessage($myDataClass->strErrorMessage, $strErrorMessage);
126                    }
127                    if ($intMselValue3 != 0) {
128                        $intRet3 = $myDataClass->dataInsertRelation('tbl_lnkHostToContact', $chkDataId, $chkMselValue3);
129                    }
130                    if (isset($intRet3) && ($intRet3 != 0)) {
131                        $myVisClass->processMessage($myDataClass->strErrorMessage, $strErrorMessage);
132                    }
133                    if ($intMselValue4 != 0) {
134                        $intRet4 = $myDataClass->dataInsertRelation(
135                            'tbl_lnkHostToContactgroup',
136                            $chkDataId,
137                            $chkMselValue4
138                        );
139                    }
140                    if (isset($intRet4) && ($intRet4 != 0)) {
141                        $myVisClass->processMessage($myDataClass->strErrorMessage, $strErrorMessage);
142                    }
143                } elseif ($chkModus == 'modify') {
144                    if ($SETS['performance']['parents'] == 1) {
145                        if ($intMselValue1 != 0) {
146                            $intRet1 = $myDataClass->dataUpdateRelation('tbl_lnkHostToHost', $chkDataId, $chkMselValue1);
147                        } else {
148                            $intRet1 = $myDataClass->dataDeleteRelation('tbl_lnkHostToHost', $chkDataId);
149                        }
150                        if ($intRet1 != 0) {
151                            $myVisClass->processMessage($myDataClass->strErrorMessage, $strErrorMessage);
152                        }
153                    }
154                    if ($intMselValue2 != 0) {
155                        $intRet2 = $myDataClass->dataUpdateRelation(
156                            'tbl_lnkHostToHostgroup',
157                            $chkDataId,
158                            $chkMselValue2
159                        );
160                    } else {
161                        $intRet2 = $myDataClass->dataDeleteRelation('tbl_lnkHostToHostgroup', $chkDataId);
162                    }
163                    if ($intRet2 != 0) {
164                        $myVisClass->processMessage($myDataClass->strErrorMessage, $strErrorMessage);
165                    }
166                    if ($intMselValue3 != 0) {
167                        $intRet3 = $myDataClass->dataUpdateRelation('tbl_lnkHostToContact', $chkDataId, $chkMselValue3);
168                    } else {
169                        $intRet3 = $myDataClass->dataDeleteRelation('tbl_lnkHostToContact', $chkDataId);
170                    }
171                    if ($intRet3 != 0) {
172                        $myVisClass->processMessage($myDataClass->strErrorMessage, $strErrorMessage);
173                    }
174                    if ($intMselValue4 != 0) {
175                        $intRet4 = $myDataClass->dataUpdateRelation(
176                            'tbl_lnkHostToContactgroup',
177                            $chkDataId,
178                            $chkMselValue4
179                        );
180                    } else {
181                        $intRet4 = $myDataClass->dataDeleteRelation('tbl_lnkHostToContactgroup', $chkDataId);
182                    }
183                    if ($intRet4 != 0) {
184                        $myVisClass->processMessage($myDataClass->strErrorMessage, $strErrorMessage);
185                    }
186                }
187                if (($intRet1 + $intRet2 + $intRet3 + $intRet4) != 0) {
188                    $strInfoMessage = '';
189                }
190                //
191                // Removing the config file if an entry was deleted or renamed
192                // ===========================================================
193                if (($chkModus == 'modify') && ($chkTfValue2 != $chkTfValue1) && ($chkDomainId != 0)) {
194                    $myConfigClass->getConfigTargets($arrConfigID);
195                    if (($arrConfigID != 1) && is_array($arrConfigID)) {
196                        $intReturn = 0;
197                        foreach ($arrConfigID as $intConfigID) {
198                            $intReturn += $myConfigClass->moveFile('host', $chkTfValue2. '.cfg', $intConfigID);
199                        }
200                        if ($intReturn == 0) {
201                            $myVisClass->processMessage(translate('The assigned, no longer used configuration files '
202                                    . 'were deleted successfully!'), $strInfoMessage);
203                            $myDataClass->writeLog(translate('Host file deleted:'). ' ' .$chkTfValue2. '.cfg');
204                        } else {
205                            if ($chkDomainId == 0) {
206                                $myVisClass->processMessage(translate('Common files cannot be removed from target '
207                                        . 'systems - please check manually'), $strErrorMessage);
208                            } else {
209                                $myVisClass->processMessage(translate('Errors while deleting the old configuration '
210                                        . 'file - please check!:'), $strErrorMessage);
211                                $myVisClass->processMessage($myConfigClass->strErrorMessage, $strErrorMessage);
212                            }
213                        }
214                    }
215                }
216                //
217                // Removing the config file if an entry was dectivated
218                // ===================================================
219                if (($chkModus == 'modify') && ($chkActive == 0)) {
220                    $myConfigClass->getConfigTargets($arrConfigID);
221                    if (($arrConfigID != 1) && is_array($arrConfigID)) {
222                        $intReturn = 0;
223                        foreach ($arrConfigID as $intConfigID) {
224                            $intReturn += $myConfigClass->moveFile('host', $chkTfValue2. '.cfg', $intConfigID);
225                        }
226                        if ($intReturn == 0) {
227                            $myVisClass->processMessage(translate('The assigned, no longer used configuration files '
228                                    . 'were deleted successfully!'), $strInfoMessage);
229                            $myDataClass->writeLog(translate('Host file deleted:'). ' ' .$chkTfValue1. '.cfg');
230                        } else {
231                            if ($chkDomainId == 0) {
232                                $myVisClass->processMessage(translate('Common files cannot be removed from target '
233                                        . 'systems - please check manually'), $strErrorMessage);
234                            } else {
235                                $myVisClass->processMessage(translate('Errors while deleting the old configuration '
236                                        . 'file - please check!:'), $strErrorMessage);
237                                $myVisClass->processMessage($myConfigClass->strErrorMessage, $strErrorMessage);
238                            }
239                        }
240                    }
241                }
242                //
243                // Insert/update session data for templates
244                // ========================================
245                if ($chkModus == 'modify') {
246                    $strSQL    = 'DELETE FROM `tbl_lnkHostToHosttemplate` WHERE `idMaster`=' .$chkDataId;
247                    $intReturn  = $myDataClass->dataInsert($strSQL, $intInsertId);
248                    if ($intReturn != 0) {
249                        $myVisClass->processMessage($myDataClass->strErrorMessage, $strErrorMessage);
250                    }
251                }
252                if (isset($_SESSION['templatedefinition']) && is_array($_SESSION['templatedefinition']) &&
253                    (count($_SESSION['templatedefinition']) != 0)) {
254                    $intSortId = 1;
255                    /** @noinspection ForeachSourceInspection */
256                    foreach ($_SESSION['templatedefinition'] as $elem) {
257                        if ($elem['status'] == 0) {
258                            $strSQL    = 'INSERT INTO `tbl_lnkHostToHosttemplate` (`idMaster`,`idSlave`,`idTable`, '
259                                       . "`idSort`) VALUES ($chkDataId,".$elem['idSlave']. ',' .$elem['idTable']. ', '
260                                       . $intSortId. ')';
261                            $intReturn  = $myDataClass->dataInsert($strSQL, $intInsertId);
262                            if ($intReturn != 0) {
263                                $myVisClass->processMessage($myDataClass->strErrorMessage, $strErrorMessage);
264                            }
265                        }
266                        $intSortId++;
267                    }
268                }
269                //
270                // Insert/update session data for free variables
271                // =============================================
272                if ($chkModus == 'modify') {
273                    $strSQL    = 'SELECT * FROM `tbl_lnkHostToVariabledefinition` WHERE `idMaster`=' .$chkDataId;
274                    $booReturn  = $myDBClass->hasDataArray($strSQL, $arrData, $intDataCount);
275                    if ($booReturn == false) {
276                        $myVisClass->processMessage($myDBClass->strErrorMessage, $strErrorMessage);
277                    }
278                    if ($intDataCount != 0) {
279                        foreach ($arrData as $elem) {
280                            $strSQL    = 'DELETE FROM `tbl_variabledefinition` WHERE `id`=' .$elem['idSlave'];
281                            $intReturn  = $myDataClass->dataInsert($strSQL, $intInsertId);
282                            if ($intReturn != 0) {
283                                $myVisClass->processMessage($myDataClass->strErrorMessage, $strErrorMessage);
284                            }
285                        }
286                    }
287                    $strSQL     = 'DELETE FROM `tbl_lnkHostToVariabledefinition` WHERE `idMaster`=' .$chkDataId;
288                    $intReturn1 = $myDataClass->dataInsert($strSQL, $intInsertId);
289                    if ($intReturn1 != 0) {
290                        $myVisClass->processMessage($myDataClass->strErrorMessage, $strErrorMessage);
291                    }
292                    $strSQL     = 'UPDATE `tbl_host` SET `use_variables`=0 WHERE `id`=' .$chkDataId;
293                    $intReturn2 = $myDataClass->dataInsert($strSQL, $intInsertId);
294                    if ($intReturn2 != 0) {
295                        $myVisClass->processMessage($myDataClass->strErrorMessage, $strErrorMessage);
296                    }
297                }
298                if (isset($_SESSION['variabledefinition']) && is_array($_SESSION['variabledefinition']) &&
299                     (count($_SESSION['variabledefinition']) != 0)) {
300                    $intCountVariable = 0;
301                    /** @noinspection ForeachSourceInspection */
302                    foreach ($_SESSION['variabledefinition'] as $elem) {
303                        if ($elem['status'] == 0) {
304                            $strSQL     = 'INSERT INTO `tbl_variabledefinition` (`name`,`value`,`last_modified`) '
305                                        . "VALUES ('".$elem['definition']."','".$elem['range']."',now())";
306                            $intReturn1 = $myDataClass->dataInsert($strSQL, $intInsertId);
307                            if ($intReturn1 != 0) {
308                                $myVisClass->processMessage($myDataClass->strErrorMessage, $strErrorMessage);
309                            }
310                            $strSQL     = 'INSERT INTO `tbl_lnkHostToVariabledefinition` (`idMaster`,`idSlave`) '
311                                        . "VALUES ($chkDataId,$intInsertId)";
312                            $intReturn2 = $myDataClass->dataInsert($strSQL, $intInsertId);
313                            if ($intReturn2 != 0) {
314                                $myVisClass->processMessage($myDataClass->strErrorMessage, $strErrorMessage);
315                            }
316                            if (($intReturn1 == 0) && ($intReturn2 == 0)) {
317                                $intCountVariable++;
318                            }
319                        }
320                    }
321                    if ($intCountVariable != 0) {
322                        $strSQL    = 'UPDATE `tbl_host` SET `use_variables`=1 WHERE `id`=' .$chkDataId;
323                        $intReturn = $myDataClass->dataInsert($strSQL, $intInsertId);
324                        if ($intReturn != 0) {
325                            $myVisClass->processMessage($myDataClass->strErrorMessage, $strErrorMessage);
326                        }
327                    }
328                }
329                //
330                // Insert/Update service <-> host relations
331                // ========================================
332                // Update service table last modified date
333                $strSQL    = 'SELECT `idMaster` FROM `tbl_lnkServiceToHost` WHERE `idSlave`=' .$chkDataId;
334                $booReturn = $myDBClass->hasDataArray($strSQL, $arrData, $intDataCount);
335                if ($booReturn == false) {
336                    $myVisClass->processMessage($myDBClass->strErrorMessage, $strErrorMessage);
337                }
338                if ($intDataCount != 0) {
339                    foreach ($arrData as $elem) {
340                        $strSQL    = 'UPDATE `tbl_service` SET `last_modified` = NOW() WHERE `id`=' .$elem['idMaster'];
341                        $intReturn = $myDataClass->dataInsert($strSQL, $intInsertId);
342                        if ($intReturn != 0) {
343                            $myVisClass->processMessage($myDataClass->strErrorMessage, $strErrorMessage);
344                        }
345                    }
346                }
347                // Remove any link data from host to service
348                $strSQL    = 'DELETE FROM `tbl_lnkServiceToHost` WHERE `idSlave`=' .$chkDataId;
349                $intReturn = $myDataClass->dataInsert($strSQL, $intInsertId);
350                if ($intReturn != 0) {
351                    $myVisClass->processMessage($myDataClass->strErrorMessage, $strErrorMessage);
352                }
353                if (is_array($chkMselValue5)) {
354                    foreach ($chkMselValue5 as $elem) {
355                        if ($elem != '') {
356                            $intExclude = 0;
357                            if (0 === strpos($elem, 'e')) {
358                                $intExclude = 1;
359                                $elem = substr_replace($elem, '', 0, 1);
360                            }
361                            $strSQL1 = 'INSERT INTO `tbl_lnkServiceToHost` (`idMaster`,`idSlave`,`exclude`) '
362                                . "VALUES ($elem,$chkDataId,$intExclude)";
363                            $intReturn1 = $myDataClass->dataInsert($strSQL1, $intInsertId);
364                            if ($intReturn1 != 0) {
365                                $myVisClass->processMessage($myDataClass->strErrorMessage, $strErrorMessage);
366                            }
367                            // Update service table last modified date
368                            $strSQL2 = 'UPDATE `tbl_service` SET `last_modified` = NOW() WHERE `id`=' . $elem;
369                            $intReturn2 = $myDataClass->dataInsert($strSQL2, $intInsertId);
370                            if ($intReturn2 != 0) {
371                                $myVisClass->processMessage($myDataClass->strErrorMessage, $strErrorMessage);
372                            }
373                            // Check if Service ID is not 0
374                            $strSQL3 = "SELECT `$preKeyField` FROM `tbl_service` WHERE `id`=$elem";
375                            $strResult3 = $myDBClass->getFieldData($strSQL3);
376                            if ($strResult3 == 0) {
377                                $strSQL = "UPDATE `tbl_service` SET `$preKeyField`=1 WHERE `id`=$elem";
378                                $intReturn = $myDataClass->dataInsert($strSQL, $intInsertId);
379                                if ($intReturn != 0) {
380                                    $myVisClass->processMessage($myDataClass->strErrorMessage, $strErrorMessage);
381                                }
382                            }
383                        }
384                    }
385                }
386            }
387        } else {
388            $myVisClass->processMessage(
389                translate('Database entry failed! Not all necessary data filled in!'),
390                $strErrorMessage
391            );
392        }
393    } else {
394        $myVisClass->processMessage(translate('Database entry failed! No write access!'), $strErrorMessage);
395    }
396    $chkModus = 'display';
397}
398if ($chkModus != 'add') {
399    $chkModus = 'display';
400}
401//
402// Singe data form
403// ===============
404if ($chkModus == 'add') {
405    $conttp->setVariable('TITLE', translate('Define hosts (hosts.cfg)'));
406    // Do not show modified time list
407    $intNoTime = 1;
408    // Process template fields
409    $strWhere = '';
410    if (isset($arrModifyData) && ($chkSelModify == 'modify')) {
411        $strWhere = 'AND `id` <> ' .$arrModifyData['id'];
412    }
413    $strSQL1    = 'SELECT `id`,`template_name`, `active` ' .
414                  "FROM `tbl_hosttemplate` WHERE $strDomainWhere2 ORDER BY `template_name`";
415    $booReturn1 = $myDBClass->hasDataArray($strSQL1, $arrDataTpl, $intDataCountTpl);
416    if ($booReturn == false) {
417        $myVisClass->processMessage($myDBClass->strErrorMessage, $strErrorMessage);
418    }
419    if ($intDataCountTpl != 0) {
420        /** @var array $arrDataTpl */
421        foreach ($arrDataTpl as $elem) {
422            if ($elem['active'] == 0) {
423                $strActive = ' [inactive]';
424                $conttp->setVariable('SPECIAL_STYLE', 'inactive_option');
425            } else {
426                $strActive = '';
427                $conttp->setVariable('SPECIAL_STYLE', '');
428            }
429            $conttp->setVariable('DAT_TEMPLATE', htmlspecialchars($elem['template_name'], ENT_QUOTES, 'UTF-8').
430                    $strActive);
431            $conttp->setVariable('DAT_TEMPLATE_ID', $elem['id']. '::1');
432            $conttp->parse('template');
433        }
434    }
435    $strSQL2    = 'SELECT `id`, `name`, `active` '
436                . "FROM `$preTableName` WHERE `name` <> '' $strWhere AND $strDomainWhere ORDER BY `name`";
437    $booReturn2 = $myDBClass->hasDataArray($strSQL2, $arrDataHpl, $intDataCountHpl);
438    if ($booReturn == false) {
439        $myVisClass->processMessage($myDBClass->strErrorMessage, $strErrorMessage);
440    }
441    if ($intDataCountHpl != 0) {
442        /** @var array $arrDataHpl */
443        foreach ($arrDataHpl as $elem) {
444            if ($elem['active'] == 0) {
445                $strActive = ' [inactive]';
446                $conttp->setVariable('SPECIAL_STYLE', 'inactive_option');
447            } else {
448                $strActive = '';
449                $conttp->setVariable('SPECIAL_STYLE', '');
450            }
451            $conttp->setVariable('DAT_TEMPLATE', htmlspecialchars($elem['name'], ENT_QUOTES, 'UTF-8').$strActive);
452            $conttp->setVariable('DAT_TEMPLATE_ID', $elem['id']. '::2');
453            $conttp->parse('template');
454        }
455    }
456    if ($SETS['performance']['parents'] == 1) {
457        // Process host selection field
458        if (isset($arrModifyData['parents'])) {
459            $intFieldId = $arrModifyData['parents'];
460        } else {
461            $intFieldId = 0;
462        }
463        if (isset($arrModifyData['id'])) {
464            $intKeyId = $arrModifyData['id'];
465        } else {
466            $intKeyId = 0;
467        }
468        $intReturn3 = $myVisClass->parseSelectMulti(
469            $preTableName,
470            $preKeyField,
471            'host_parents',
472            'tbl_lnkHostToHost',
473            0,
474            $intFieldId,
475            $intKeyId
476        );
477        if ($intReturn3 != 0) {
478            $myVisClass->processMessage($myVisClass->strErrorMessage, $strErrorMessage);
479        }
480        $conttp->setVariable('PARENTS_VISIBLE', 'elementShow');
481    } else {
482        $conttp->setVariable('PARENTS_VISIBLE', 'elementHide');
483    }
484    // Process hostgroup selection field
485    if (isset($arrModifyData['hostgroups'])) {
486        $intFieldId = $arrModifyData['hostgroups'];
487    } else {
488        $intFieldId = 0;
489    }
490    $intReturn4 = $myVisClass->parseSelectMulti(
491        'tbl_hostgroup',
492        'hostgroup_name',
493        'hostgroup',
494        'tbl_lnkHostToHostgroup',
495        0,
496        $intFieldId
497    );
498    if ($intReturn4 != 0) {
499        $myVisClass->processMessage($myVisClass->strErrorMessage, $strErrorMessage);
500    }
501    // Process check command selection field
502    if (isset($arrModifyData['check_command']) && ($arrModifyData['check_command'] != '')) {
503        $arrCommand = explode('!', $arrModifyData['check_command']);
504        $intFieldId = $arrCommand[0];
505    } else {
506        $intFieldId = 0;
507    }
508    $intReturn5 = $myVisClass->parseSelectSimple('tbl_command', 'command_name', 'hostcommand', 2, $intFieldId);
509    if ($intReturn5 != 0) {
510        $myVisClass->processMessage($myVisClass->strErrorMessage, $strErrorMessage);
511    }
512    // Process check period selection field
513    if (isset($arrModifyData['check_period'])) {
514        $intFieldId = $arrModifyData['check_period'];
515    } else {
516        $intFieldId = 0;
517    }
518    $intReturn6 = $myVisClass->parseSelectSimple('tbl_timeperiod', 'timeperiod_name', 'checkperiod', 1, $intFieldId);
519    if ($intReturn6 != 0) {
520        $myVisClass->processMessage($myVisClass->strErrorMessage, $strErrorMessage);
521    }
522    if (isset($arrModifyData['notification_period'])) {
523        $intFieldId = $arrModifyData['notification_period'];
524    } else {
525        $intFieldId = 0;
526    }
527    $intReturn7 = $myVisClass->parseSelectSimple('tbl_timeperiod', 'timeperiod_name', 'notifyperiod', 1, $intFieldId);
528    if ($intReturn7 != 0) {
529        $myVisClass->processMessage(translate('Attention, no time periods defined!'), $strDBWarning);
530        $intDataWarning = 1;
531    }
532    // Process event handler selection field
533    if (isset($arrModifyData['event_handler'])) {
534        $intFieldId = $arrModifyData['event_handler'];
535    } else {
536        $intFieldId = 0;
537    }
538    $intReturn8 = $myVisClass->parseSelectSimple('tbl_command', 'command_name', 'eventhandler', 1, $intFieldId);
539    if ($intReturn8 != 0) {
540        $myVisClass->processMessage($myVisClass->strErrorMessage, $strErrorMessage);
541    }
542    // Process contact and contact group selection field
543    if (isset($arrModifyData['contacts'])) {
544        $intFieldId = $arrModifyData['contacts'];
545    } else {
546        $intFieldId = 0;
547    }
548    $intReturn9 = $myVisClass->parseSelectMulti(
549        'tbl_contact',
550        'contact_name',
551        'host_contacts',
552        'tbl_lnkHostToContact',
553        2,
554        $intFieldId
555    );
556    if ($intReturn9 != 0) {
557        $myVisClass->processMessage($myVisClass->strErrorMessage, $strErrorMessage);
558    }
559    if (isset($arrModifyData['contact_groups'])) {
560        $intFieldId = $arrModifyData['contact_groups'];
561    } else {
562        $intFieldId = 0;
563    }
564    $intReturn10 = $myVisClass->parseSelectMulti(
565        'tbl_contactgroup',
566        'contactgroup_name',
567        'host_contactgroups',
568        'tbl_lnkHostToContactgroup',
569        2,
570        $intFieldId
571    );
572    if ($intReturn10 != 0) {
573        $myVisClass->processMessage($myVisClass->strErrorMessage, $strErrorMessage);
574    }
575    if (($intReturn8 != 0) && ($intReturn10 != 0)) {
576        $myVisClass->processMessage(translate('Attention, no contact groups defined!'), $strDBWarning);
577        $intDataWarning = 1;
578    }
579    // Process access group selection field
580    if (isset($arrModifyData['access_group'])) {
581        $intFieldId = $arrModifyData['access_group'];
582    } else {
583        $intFieldId = 0;
584    }
585    $intReturn11 = $myVisClass->parseSelectSimple('tbl_group', 'groupname', 'acc_group', 0, $intFieldId);
586    if ($intReturn11 != 0) {
587        $myVisClass->processMessage($myVisClass->strErrorMessage, $strErrorMessage);
588    }
589    // Process service selection field
590    if (isset($arrModifyData['contacts'])) {
591        $intFieldId = $arrModifyData['id'];
592    } else {
593        $intFieldId = 0;
594    }
595    $intReturn12 = $myVisClass->parseSelectMulti(
596        'tbl_service',
597        'service_description',
598        'host_services',
599        'tbl_lnkServiceToHost',
600        0,
601        $intFieldId
602    );
603    if ($intReturn12 != 0) {
604        $myVisClass->processMessage($myVisClass->strErrorMessage, $strErrorMessage);
605    }
606    // Initial add/modify form definitions
607    $strChbFields = 'ACE,PCE,FRE,OBS,EVH,FLE,STI,NSI,PED,NOE,PAR,HOG,COT,COG,TPL';
608    $myContentClass->addFormInit($conttp, $strChbFields);
609    if ($intDataWarning == 1) {
610        $conttp->setVariable('WARNING', $strDBWarning. '<br>' .translate('Saving not possible!'));
611    }
612    if ($intVersion < 3) {
613        $conttp->setVariable('VERSION_20_VALUE_MUST', 'mselValue1,');
614    }
615    // Insert data from database in "modify" mode
616    if (isset($arrModifyData) && ($chkSelModify == 'modify')) {
617        // Check relation information to find out locked configuration datasets
618        $intLocked = $myDataClass->infoRelation($preTableName, $arrModifyData['id'], $preKeyField);
619        $myVisClass->processMessage($myDataClass->strInfoMessage, $strRelMessage);
620        $strInfo  = '<br><span class="redmessage">' .translate('Entry cannot be activated because it is used by '
621                . 'another configuration'). ':</span>';
622        $strInfo .= '<br><span class="greenmessage">' .$strRelMessage. '</span>';
623        // Process data
624        $myContentClass->addInsertData($conttp, $arrModifyData, $intLocked, $strInfo, $strChbFields);
625        $conttp->setVariable('DAT_ACE' .$arrModifyData['active_checks_enabled']. '_CHECKED', 'checked');
626        $conttp->setVariable('DAT_PCE' .$arrModifyData['passive_checks_enabled']. '_CHECKED', 'checked');
627        $conttp->setVariable('DAT_FRE' .$arrModifyData['check_freshness']. '_CHECKED', 'checked');
628        $conttp->setVariable('DAT_OBS' .$arrModifyData['obsess_over_host']. '_CHECKED', 'checked');
629        $conttp->setVariable('DAT_EVH' .$arrModifyData['event_handler_enabled']. '_CHECKED', 'checked');
630        $conttp->setVariable('DAT_FLE' .$arrModifyData['flap_detection_enabled']. '_CHECKED', 'checked');
631        $conttp->setVariable('DAT_STI' .$arrModifyData['retain_status_information']. '_CHECKED', 'checked');
632        $conttp->setVariable('DAT_NSI' .$arrModifyData['retain_nonstatus_information']. '_CHECKED', 'checked');
633        $conttp->setVariable('DAT_PED' .$arrModifyData['process_perf_data']. '_CHECKED', 'checked');
634        $conttp->setVariable('DAT_NOE' .$arrModifyData['notifications_enabled']. '_CHECKED', 'checked');
635        $conttp->setVariable('DAT_PAR' .$arrModifyData['parents_tploptions']. '_CHECKED', 'checked');
636        $conttp->setVariable('DAT_HOG' .$arrModifyData['hostgroups_tploptions']. '_CHECKED', 'checked');
637        $conttp->setVariable('DAT_COT' .$arrModifyData['contacts_tploptions']. '_CHECKED', 'checked');
638        $conttp->setVariable('DAT_COG' .$arrModifyData['contact_groups_tploptions']. '_CHECKED', 'checked');
639        $conttp->setVariable('DAT_TPL' .$arrModifyData['use_template_tploptions']. '_CHECKED', 'checked');
640        // Special processing for -1 values - write 'null' to integer fields
641        $strIntegerfelder  = 'max_check_attempts,check_interval,retry_interval,freshness_threshold,low_flap_threshold,'
642                           . 'high_flap_threshold,';
643        $strIntegerfelder .= 'notification_interval,first_notification_delay';
644        foreach (explode(',', $strIntegerfelder) as $elem) {
645            if ($arrModifyData[$elem] == -1) {
646                $conttp->setVariable('DAT_' .strtoupper($elem), 'null');
647            }
648        }
649        if ($arrModifyData['check_command'] != '') {
650            $arrArgument = explode('!', $arrModifyData['check_command']);
651            foreach ($arrArgument as $key => $value) {
652                if ($key == 0) {
653                    $conttp->setVariable('IFRAME_SRC', $_SESSION['SETS']['path']['base_url'].
654                        'admin/commandline.php?cname=' .$value);
655                } else {
656                    $value1 = str_replace('::bang::', '!', $value);
657                    $value2 = str_replace('::back::', "\\", $value1);
658                    $conttp->setVariable('DAT_ARG' .$key, htmlentities($value2, ENT_QUOTES, 'UTF-8'));
659                }
660            }
661        }
662        // Process option fields
663        foreach (explode(',', $arrModifyData['initial_state']) as $elem) {
664            $conttp->setVariable('DAT_IS' .strtoupper($elem). '_CHECKED', 'checked');
665        }
666        foreach (explode(',', $arrModifyData['flap_detection_options']) as $elem) {
667            $conttp->setVariable('DAT_FL' .strtoupper($elem). '_CHECKED', 'checked');
668        }
669        foreach (explode(',', $arrModifyData['notification_options']) as $elem) {
670            $conttp->setVariable('DAT_NO' .strtoupper($elem). '_CHECKED', 'checked');
671        }
672        foreach (explode(',', $arrModifyData['stalking_options']) as $elem) {
673            $conttp->setVariable('DAT_ST' .strtoupper($elem). '_CHECKED', 'checked');
674        }
675    }
676    $conttp->parse('datainsert');
677    $conttp->show('datainsert');
678}
679//
680// List view
681// ==========
682if ($chkModus == 'display') {
683    // Initial list view definitions
684    $myContentClass->listViewInit($mastertp);
685    $mastertp->setVariable('TITLE', translate('Define hosts (hosts.cfg)'));
686    $mastertp->setVariable('FIELD_1', translate('Host name'));
687    $mastertp->setVariable('FIELD_2', translate('Description'));
688    // Process filter string and filter
689    $strSearchWhere = '';
690    if ($_SESSION['search'][$preSearchSession] != '') {
691        $strSearchTxt    = $_SESSION['search'][$preSearchSession];
692        $strSearchWhere .= "AND (`$preKeyField` LIKE '%".$strSearchTxt."%' OR `alias` LIKE '%".$strSearchTxt."%' OR "
693                        . "`display_name` LIKE '%".$strSearchTxt."%' OR `address` LIKE '%".$strSearchTxt."%') ";
694    }
695    if ($_SESSION['filter'][$preSearchSession]['registered'] != '') {
696        $intRegistered = (int)$_SESSION['filter'][$preSearchSession]['registered'];
697        if ($intRegistered == 1) {
698            $strSearchWhere .= "AND `register` = '1' ";
699        }
700        if ($intRegistered == 2) {
701            $strSearchWhere .= "AND `register` = '0' ";
702        }
703        $mastertp->setVariable('SEL_REGFILTER_'.$intRegistered.'_SELECTED', 'selected');
704    }
705    if ($_SESSION['filter'][$preSearchSession]['active'] != '') {
706        $intActivated = (int)$_SESSION['filter'][$preSearchSession]['active'];
707        if ($intActivated == 1) {
708            $strSearchWhere .= "AND `active` = '1' ";
709        }
710        if ($intActivated == 2) {
711            $strSearchWhere .= "AND `active` = '0' ";
712        }
713        $mastertp->setVariable('SEL_ACTIVEFILTER_'.$intActivated.'_SELECTED', 'selected');
714    }
715    // Row sorting
716    $strOrderString = "ORDER BY `config_id`, `$preKeyField` $hidSortDir";
717    if ($hidSortBy == 2) {
718        $strOrderString = "ORDER BY `config_id`, `alias` $hidSortDir";
719    }
720    // Count datasets
721    $strSQL     = "SELECT count(*) AS `number` FROM `$preTableName` WHERE $strDomainWhere $strSearchWhere "
722                . "AND `access_group` IN ($strAccess)";
723    $booReturn1 = $myDBClass->hasSingleDataset($strSQL, $arrDataLinesCount);
724    if ($booReturn1 == false) {
725        $myVisClass->processMessage(translate('Error while selecting data from database:'), $strErrorMessage);
726        $myVisClass->processMessage($myDBClass->strErrorMessage, $strErrorMessage);
727    } else {
728        $intLineCount = (int)$arrDataLinesCount['number'];
729        if ($intLineCount < $chkLimit) {
730            $chkLimit = 0;
731        }
732    }
733    // Get datasets
734    $strSQL     = "SELECT `id`, `$preKeyField`, `alias`, `register`, `active`, `last_modified`, `config_id`, "
735                . "`access_group` FROM `$preTableName` WHERE $strDomainWhere $strSearchWhere AND `access_group` "
736                . "IN ($strAccess) $strOrderString LIMIT $chkLimit,".$SETS['common']['pagelines'];
737    $booReturn2 = $myDBClass->hasDataArray($strSQL, $arrDataLines, $intDataCount);
738    if ($booReturn2 == false) {
739        $myVisClass->processMessage(translate('Error while selecting data from database:'), $strErrorMessage);
740        $myVisClass->processMessage($myDBClass->strErrorMessage, $strErrorMessage);
741    }
742    // Process data
743    $myContentClass->listData($mastertp, $arrDataLines, $intDataCount, $intLineCount, $preKeyField, 'alias', 0);
744    if ($myContentClass->strErrorMessage != '') {
745        $myVisClass->processMessage($myContentClass->strErrorMessage, $strErrorMessage);
746    }
747}
748// Show messages
749$arrTimeData       = array();
750$strTimeInfoString = '';
751$myContentClass->showMessages(
752    $mastertp,
753    $strErrorMessage,
754    $strInfoMessage,
755    $strConsistMessage,
756    $arrTimeData,
757    $strTimeInfoString,
758    $intNoTime
759);
760//
761// Process footer
762// ==============
763$myContentClass->showFooter($maintp, $setFileVersion);
764