1<?php
2///////////////////////////////////////////////////////////////////////////////
3//
4// NagiosQL
5//
6///////////////////////////////////////////////////////////////////////////////
7//
8// (c) 2005-2020 by Martin Willisegger
9//
10// Project   : NagiosQL
11// Component : Host escalation 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        = 20;
27$preContent       = 'admin/hostescalations.htm.tpl';
28$preListTpl       = 'admin/datalist.htm.tpl';
29$preSearchSession = 'hostescalation';
30$preTableName     = 'tbl_hostescalation';
31$preKeyField      = 'config_name';
32$preAccess        = 1;
33$preFieldvars     = 1;
34//
35// Include preprocessing files
36// ===========================
37require $preBasePath.'functions/prepend_adm.php';
38require $preBasePath.'functions/prepend_content.php';
39//
40// Data processing
41// ===============
42$strEO = substr($chkChbGr1a.$chkChbGr1b.$chkChbGr1c, 0, -1);
43//
44// Add or modify data
45// ==================
46if ((($chkModus == 'insert') || ($chkModus == 'modify')) && ($intGlobalWriteAccess == 0)) {
47    $strSQLx = "`$preTableName` SET `$preKeyField`='$chkTfValue1', `host_name`=$intMselValue3, "
48        . "`hostgroup_name`=$intMselValue4, `contacts`=$intMselValue1, `contact_groups`=$intMselValue2, "
49        . "`first_notification`=$chkTfNullVal1, `last_notification`=$chkTfNullVal2, "
50        . "`notification_interval`=$chkTfNullVal3, `escalation_period`='$chkSelValue1', "
51        . "`escalation_options`='$strEO', $preSQLCommon1";
52    if ($chkModus == 'insert') {
53        $strSQL = 'INSERT INTO ' .$strSQLx;
54    } else {
55        $strSQL = 'UPDATE ' .$strSQLx. ' WHERE `id`=' .$chkDataId;
56    }
57    if ($intWriteAccessId == 0) {
58        if ((($intMselValue3 != 0) || ($chkMselValue4 != 0)) && (($intMselValue1 != 0) || ($intMselValue2 != 0)) &&
59            ($chkTfNullVal1 != 'NULL') && ($chkTfNullVal2 != 'NULL') && ($chkTfNullVal3 != 'NULL')) {
60            $intReturn = $myDataClass->dataInsert($strSQL, $intInsertId);
61            if ($chkModus == 'insert') {
62                $chkDataId = $intInsertId;
63            }
64            if ($intReturn == 1) {
65                $myVisClass->processMessage($myDataClass->strErrorMessage, $strErrorMessage);
66            } else {
67                $myVisClass->processMessage($myDataClass->strInfoMessage, $strInfoMessage);
68                $myDataClass->updateStatusTable($preTableName);
69                if ($chkModus == 'insert') {
70                    $myDataClass->writeLog(translate('New host escalation inserted:'). ' ' .$chkTfValue1);
71                }
72                if ($chkModus == 'modify') {
73                    $myDataClass->writeLog(translate('Host escalation modified:'). ' ' .$chkTfValue1);
74                }
75                //
76                // Insert/update relations
77                // =======================
78                if ($chkModus == 'insert') {
79                    if ($intMselValue1 != 0) {
80                        $intRet1 = $myDataClass->dataInsertRelation(
81                            'tbl_lnkHostescalationToContact',
82                            $chkDataId,
83                            $chkMselValue1
84                        );
85                    }
86                    if (isset($intRet1) && ($intRet1 != 0)) {
87                        $myVisClass->processMessage($myDataClass->strErrorMessage, $strErrorMessage);
88                    }
89                    if ($intMselValue2 != 0) {
90                        $intRet2 = $myDataClass->dataInsertRelation(
91                            'tbl_lnkHostescalationToContactgroup',
92                            $chkDataId,
93                            $chkMselValue2
94                        );
95                    }
96                    if (isset($intRet2) && ($intRet2 != 0)) {
97                        $myVisClass->processMessage($myDataClass->strErrorMessage, $strErrorMessage);
98                    }
99                    if ($intMselValue3 != 0) {
100                        $intRet3 = $myDataClass->dataInsertRelation(
101                            'tbl_lnkHostescalationToHost',
102                            $chkDataId,
103                            $chkMselValue3
104                        );
105                    }
106                    if (isset($intRet3) && ($intRet3 != 0)) {
107                        $myVisClass->processMessage($myDataClass->strErrorMessage, $strErrorMessage);
108                    }
109                    if ($intMselValue4 != 0) {
110                        $intRet4 = $myDataClass->dataInsertRelation(
111                            'tbl_lnkHostescalationToHostgroup',
112                            $chkDataId,
113                            $chkMselValue4
114                        );
115                    }
116                    if (isset($intRet4) && ($intRet4 != 0)) {
117                        $myVisClass->processMessage($myDataClass->strErrorMessage, $strErrorMessage);
118                    }
119                } elseif ($chkModus == 'modify') {
120                    if ($intMselValue1 != 0) {
121                        $intRet1 = $myDataClass->dataUpdateRelation(
122                            'tbl_lnkHostescalationToContact',
123                            $chkDataId,
124                            $chkMselValue1
125                        );
126                    } else {
127                        $intRet1 = $myDataClass->dataDeleteRelation('tbl_lnkHostescalationToContact', $chkDataId);
128                    }
129                    if ($intRet1 != 0) {
130                        $myVisClass->processMessage($myDataClass->strErrorMessage, $strErrorMessage);
131                    }
132                    if ($intMselValue2 != 0) {
133                        $intRet2 = $myDataClass->dataUpdateRelation(
134                            'tbl_lnkHostescalationToContactgroup',
135                            $chkDataId,
136                            $chkMselValue2
137                        );
138                    } else {
139                        $intRet2 = $myDataClass->dataDeleteRelation('tbl_lnkHostescalationToContactgroup', $chkDataId);
140                    }
141                    if ($intRet2 != 0) {
142                        $myVisClass->processMessage($myDataClass->strErrorMessage, $strErrorMessage);
143                    }
144                    if ($intMselValue3 != 0) {
145                        $intRet3 = $myDataClass->dataUpdateRelation(
146                            'tbl_lnkHostescalationToHost',
147                            $chkDataId,
148                            $chkMselValue3
149                        );
150                    } else {
151                        $intRet3 = $myDataClass->dataDeleteRelation('tbl_lnkHostescalationToHost', $chkDataId);
152                    }
153                    if ($intRet3 != 0) {
154                        $myVisClass->processMessage($myDataClass->strErrorMessage, $strErrorMessage);
155                    }
156                    if ($intMselValue4 != 0) {
157                        $intRet4 = $myDataClass->dataUpdateRelation(
158                            'tbl_lnkHostescalationToHostgroup',
159                            $chkDataId,
160                            $chkMselValue4
161                        );
162                    } else {
163                        $intRet4 = $myDataClass->dataDeleteRelation('tbl_lnkHostescalationToHostgroup', $chkDataId);
164                    }
165                    if ($intRet4 != 0) {
166                        $myVisClass->processMessage($myDataClass->strErrorMessage, $strErrorMessage);
167                    }
168                }
169                if (($intRet1 + $intRet2 + $intRet3 + $intRet4) != 0) {
170                    $strInfoMessage = '';
171                }
172                //
173                // Update Import HASH
174                // ==================
175                $booReturn = $myDataClass->updateHash($preTableName, $chkDataId);
176                if ($booReturn != 0) {
177                    $myVisClass->processMessage($myDataClass->strErrorMessage, $strErrorMessage);
178                }
179            }
180        } else {
181            $myVisClass->processMessage(
182                translate('Database entry failed! Not all necessary data filled in!'),
183                $strErrorMessage
184            );
185        }
186    } else {
187        $myVisClass->processMessage(translate('Database entry failed! No write access!'), $strErrorMessage);
188    }
189    $chkModus = 'display';
190}
191if ($chkModus != 'add') {
192    $chkModus    = 'display';
193}
194//
195// Get date/time of last database and config file manipulation
196// ===========================================================
197$intReturn = $myConfigClass->lastModifiedFile($preTableName, $arrTimeData, $strTimeInfoString);
198if ($intReturn != 0) {
199    $myVisClass->processMessage($myConfigClass->strErrorMessage, $strErrorMessage);
200}
201//
202// Singe data form
203// ===============
204if ($chkModus == 'add') {
205    $conttp->setVariable('TITLE', translate('Define host escalation (hostescalations.cfg)'));
206    // Do not show modified time list
207    $intNoTime = 1;
208    // Process host and host group selection field
209    if (isset($arrModifyData['host_name'])) {
210        $intFieldId = $arrModifyData['host_name'];
211    } else {
212        $intFieldId = 0;
213    }
214    $intReturn1 = $myVisClass->parseSelectMulti(
215        'tbl_host',
216        'host_name',
217        'host',
218        'tbl_lnkHostescalationToHost',
219        2,
220        $intFieldId
221    );
222    if ($intReturn1 != 0) {
223        $myVisClass->processMessage($myVisClass->strErrorMessage, $strErrorMessage);
224    }
225    if (isset($arrModifyData['hostgroup_name'])) {
226        $intFieldId = $arrModifyData['hostgroup_name'];
227    } else {
228        $intFieldId = 0;
229    }
230    $intReturn2 = $myVisClass->parseSelectMulti(
231        'tbl_hostgroup',
232        'hostgroup_name',
233        'hostgroup',
234        'tbl_lnkHostescalationToHostgroup',
235        2,
236        $intFieldId
237    );
238    if ($intReturn2 != 0) {
239        $myVisClass->processMessage($myVisClass->strErrorMessage, $strErrorMessage);
240    }
241    if (($intReturn1 != 0) && ($intReturn2 != 0)) {
242        $myVisClass->processMessage(translate('Attention, no hosts and hostgroups defined!'), $strDBWarning);
243        $intDataWarning = 1;
244    }
245    // Process timeperiod selection field
246    if (isset($arrModifyData['escalation_period'])) {
247        $intFieldId = $arrModifyData['escalation_period'];
248    } else {
249        $intFieldId = 0;
250    }
251    $intReturn = $myVisClass->parseSelectSimple('tbl_timeperiod', 'timeperiod_name', 'escperiod', 1, $intFieldId);
252    if ($intReturn != 0) {
253        $myVisClass->processMessage($myVisClass->strErrorMessage, $strErrorMessage);
254    }
255    // Process contact and contact group selection field
256    if (isset($arrModifyData['contacts'])) {
257        $intFieldId = $arrModifyData['contacts'];
258    } else {
259        $intFieldId = 0;
260    }
261    $intReturn1 = $myVisClass->parseSelectMulti(
262        'tbl_contact',
263        'contact_name',
264        'contact',
265        'tbl_lnkHostescalationToContact',
266        2,
267        $intFieldId
268    );
269    if ($intReturn1 != 0) {
270        $myVisClass->processMessage($myVisClass->strErrorMessage, $strErrorMessage);
271    }
272    if (isset($arrModifyData['contact_groups'])) {
273        $intFieldId = $arrModifyData['contact_groups'];
274    } else {
275        $intFieldId = 0;
276    }
277    $intReturn2 = $myVisClass->parseSelectMulti(
278        'tbl_contactgroup',
279        'contactgroup_name',
280        'contactgroup',
281        'tbl_lnkHostescalationToContactgroup',
282        2,
283        $intFieldId
284    );
285    if ($intReturn2 != 0) {
286        $myVisClass->processMessage($myVisClass->strErrorMessage, $strErrorMessage);
287    }
288    if (($intReturn1 != 0) && ($intReturn2 != 0)) {
289        $myVisClass->processMessage(translate('Attention, no contacts and contactgroups defined!'), $strDBWarning);
290        $intDataWarning = 1;
291    }
292    // Process access group selection field
293    if (isset($arrModifyData['access_group'])) {
294        $intFieldId = $arrModifyData['access_group'];
295    } else {
296        $intFieldId = 0;
297    }
298    $intReturn = $myVisClass->parseSelectSimple('tbl_group', 'groupname', 'acc_group', 0, $intFieldId);
299    if ($intReturn != 0) {
300        $myVisClass->processMessage($myVisClass->strErrorMessage, $strErrorMessage);
301    }
302    // Initial add/modify form definitions
303    $myContentClass->addFormInit($conttp);
304    if ($intDataWarning == 1) {
305        $conttp->setVariable('WARNING', $strDBWarning. '<br>' .translate('Saving not possible!'));
306    }
307    // Insert data from database in "modify" mode
308    if (isset($arrModifyData) && ($chkSelModify == 'modify')) {
309        // Check relation information to find out locked configuration datasets
310        $intLocked = $myDataClass->infoRelation($preTableName, $arrModifyData['id'], $preKeyField);
311        $myVisClass->processMessage($myDataClass->strInfoMessage, $strRelMessage);
312        $strInfo  = '<br><span class="redmessage">' .translate('Entry cannot be activated because it is used by '
313                . 'another configuration'). ':</span>';
314        $strInfo .= '<br><span class="greenmessage">' .$strRelMessage. '</span>';
315        // Process data
316        $myContentClass->addInsertData($conttp, $arrModifyData, $intLocked, $strInfo);
317        foreach (explode(',', $arrModifyData['escalation_options']) as $elem) {
318            $conttp->setVariable('DAT_EO' .strtoupper($elem). '_CHECKED', 'checked');
319        }
320    }
321    $conttp->parse('datainsert');
322    $conttp->show('datainsert');
323}
324//
325// List view
326// ==========
327if ($chkModus == 'display') {
328    // Initial list view definitions
329    $myContentClass->listViewInit($mastertp);
330    $mastertp->setVariable('TITLE', translate('Define host escalation (hostescalations.cfg)'));
331    $mastertp->setVariable('FIELD_1', translate('Config name'));
332    $mastertp->setVariable('FIELD_2', translate('Hosts'). ' / ' .translate('Host groups'));
333    $mastertp->setVariable('FILTER_VISIBLE', 'visibility: hidden');
334    // Process search string
335    if ($_SESSION['search'][$preSearchSession] != '') {
336        $strSearchTxt   = $_SESSION['search'][$preSearchSession];
337        $strSearchWhere = "AND (`$preKeyField` LIKE '%".$strSearchTxt."%')";
338    }
339    // Row sorting
340    $strOrderString = "ORDER BY `config_id`, `$preKeyField` $hidSortDir";
341    if ($hidSortBy == 2) {
342        $strOrderString = "ORDER BY `config_id`, `$preKeyField` $hidSortDir";
343    }
344    $mastertp->setVariable('DISABLE_SORT_2', 'disable');
345    // Count datasets
346    $strSQL    = "SELECT count(*) AS `number` FROM `$preTableName` "
347        . "WHERE $strDomainWhere $strSearchWhere AND `access_group` IN ($strAccess)";
348    $booReturn = $myDBClass->hasSingleDataset($strSQL, $arrDataLinesCount);
349    if ($booReturn == false) {
350        $myVisClass->processMessage(translate('Error while selecting data from database:'), $strErrorMessage);
351        $myVisClass->processMessage($myDBClass->strErrorMessage, $strErrorMessage);
352    } else {
353        $intLineCount = (int)$arrDataLinesCount['number'];
354        if ($intLineCount < $chkLimit) {
355            $chkLimit = 0;
356        }
357    }
358    // Get datasets
359    $strSQL    = "SELECT `id`, `$preKeyField`, `host_name`, `hostgroup_name`, `register`, `active`, `config_id`, "
360        . "`access_group`  FROM `$preTableName` WHERE $strDomainWhere $strSearchWhere AND `access_group` "
361        . "IN ($strAccess) $strOrderString LIMIT $chkLimit,".$SETS['common']['pagelines'];
362    $booReturn = $myDBClass->hasDataArray($strSQL, $arrDataLines, $intDataCount);
363    if ($booReturn == false) {
364        $myVisClass->processMessage(translate('Error while selecting data from database:'), $strErrorMessage);
365        $myVisClass->processMessage($myDBClass->strErrorMessage, $strErrorMessage);
366    }
367    // Process data
368    $myContentClass->listData(
369        $mastertp,
370        $arrDataLines,
371        $intDataCount,
372        $intLineCount,
373        $preKeyField,
374        'process_field',
375        40
376    );
377}
378// Show messages
379$myContentClass->showMessages(
380    $mastertp,
381    $strErrorMessage,
382    $strInfoMessage,
383    $strConsistMessage,
384    $arrTimeData,
385    $strTimeInfoString,
386    $intNoTime
387);
388//
389// Process footer
390// ==============
391$myContentClass->showFooter($maintp, $setFileVersion);
392