1<?php
2///////////////////////////////////////////////////////////////////////////////
3//
4// NagiosQL
5//
6///////////////////////////////////////////////////////////////////////////////
7//
8// (c) 2005-2020 by Martin Willisegger
9//
10// Project   : NagiosQL
11// Component : Service extended information definition
12// Component : Service escalation definition
13// Website   : https://sourceforge.net/projects/nagiosql/
14// Version   : 3.4.1
15// GIT Repo  : https://gitlab.com/wizonet/NagiosQL
16//
17///////////////////////////////////////////////////////////////////////////////
18//
19// Path settings
20// ===================
21$strPattern = '(admin/[^/]*.php)';
22$preRelPath  = preg_replace($strPattern, '', filter_input(INPUT_SERVER, 'PHP_SELF', FILTER_SANITIZE_STRING));
23$preBasePath = preg_replace($strPattern, '', filter_input(INPUT_SERVER, 'SCRIPT_FILENAME', FILTER_SANITIZE_STRING));
24//
25// Define common variables
26// =======================
27$prePageId        = 24;
28$preContent       = 'admin/serviceextinfo.htm.tpl';
29$preListTpl       = 'admin/datalist.htm.tpl';
30$preSearchSession = 'serviceextinfo';
31$preTableName     = 'tbl_serviceextinfo';
32$preKeyField      = 'host_name';
33$preAccess        = 1;
34$preFieldvars     = 1;
35//
36// Include preprocessing files
37// ===========================
38require $preBasePath.'functions/prepend_adm.php';
39require $preBasePath.'functions/prepend_content.php';
40//
41// Add or modify data
42// ==================
43if ((($chkModus == 'insert') || ($chkModus == 'modify')) && ($intGlobalWriteAccess == 0)) {
44    $strSQLx = "`$preTableName` SET `$preKeyField`='$chkSelValue1', `service_description`='$chkSelValue2', "
45        . "`notes`='$chkTfValue1', `notes_url`='$chkTfValue2', `action_url`='$chkTfValue3', "
46        . "`icon_image`='$chkTfValue4', `icon_image_alt`='$chkTfValue5', $preSQLCommon1";
47    if ($chkModus == 'insert') {
48        $strSQL = 'INSERT INTO ' .$strSQLx;
49    } else {
50        $strSQL = 'UPDATE ' .$strSQLx. ' WHERE `id`=' .$chkDataId;
51    }
52    if ($intWriteAccessId == 0) {
53        if (($chkSelValue1 != 0) && ($chkSelValue2 != 0)) {
54            $intReturn = $myDataClass->dataInsert($strSQL, $intInsertId);
55            if ($chkModus == 'insert') {
56                $chkDataId = $intInsertId;
57            }
58            if ($intReturn == 1) {
59                $myVisClass->processMessage($myDataClass->strErrorMessage, $strErrorMessage);
60            } else {
61                $myVisClass->processMessage($myDataClass->strInfoMessage, $strInfoMessage);
62                $myDataClass->updateStatusTable($preTableName);
63                if ($chkModus == 'insert') {
64                    $myDataClass->writeLog(translate('New service extended information inserted:'). ' ' .$chkSelValue1.
65                        '::' .$chkSelValue2);
66                }
67                if ($chkModus == 'modify') {
68                    $myDataClass->writeLog(translate('Service extended information modified:'). ' ' .$chkSelValue1.
69                        '::' .$chkSelValue2);
70                }
71                //
72                // Update Import HASH
73                // ==================
74                $booReturn = $myDataClass->updateHash($preTableName, $chkDataId);
75                if ($booReturn != 0) {
76                    $myVisClass->processMessage($myDataClass->strErrorMessage, $strErrorMessage);
77                }
78            }
79        } else {
80            $myVisClass->processMessage(
81                translate('Database entry failed! Not all necessary data filled in!'),
82                $strErrorMessage
83            );
84        }
85    } else {
86        $myVisClass->processMessage(translate('Database entry failed! No write access!'), $strErrorMessage);
87    }
88    $chkModus = 'display';
89}
90if (($chkModus != 'add') && ($chkModus != 'refresh')) {
91    $chkModus    = 'display';
92}
93//
94// Get date/time of last database and config file manipulation
95// ===========================================================
96$intReturn = $myConfigClass->lastModifiedFile($preTableName, $arrTimeData, $strTimeInfoString);
97if ($intReturn != 0) {
98    $myVisClass->processMessage($myConfigClass->strErrorMessage, $strErrorMessage);
99}
100//
101// Singe data form
102// ===============
103if (($chkModus == 'add') || ($chkModus == 'refresh')) {
104    $conttp->setVariable('TITLE', translate('Define service extended information (serviceextinfo.cfg)'));
105    // Do not show modified time list
106    $intNoTime = 1;
107    // Refresh mode
108    if ($chkModus == 'refresh') {
109        $_SESSION['refresh']['se_host'] = $chkSelValue1;
110        $myVisClass->arrSession = $_SESSION;
111    } else {
112        $_SESSION['refresh']['se_host'] = $chkSelValue1;
113        if (isset($arrModifyData[$preKeyField]) && ($arrModifyData[$preKeyField] != 0)) {
114            $strSQL     = "SELECT `$preKeyField` FROM `$preTableName` WHERE `id` = ".$arrModifyData['id'];
115            $booReturn  = $myDBClass->hasDataArray($strSQL, $arrData, $intDC);
116            if ($intDC != 0) {
117                $_SESSION['refresh']['se_host'] = $arrData[0][$preKeyField];
118            }
119        } else {
120            $strSQL    = 'SELECT `id` FROM `tbl_host` '
121                . "WHERE `active`='1' AND `config_id`=$chkDomainId ORDER BY `$preKeyField`";
122            $booReturn = $myDBClass->hasDataArray($strSQL, $arrData, $intDC);
123            if ($intDC != 0) {
124                $_SESSION['refresh']['se_host'] = $arrData[0]['id'];
125            }
126        }
127        $myVisClass->arrSession = $_SESSION;
128    }
129    // Process host selection field
130    if (isset($arrModifyData[$preKeyField])) {
131        $intFieldId = $arrModifyData[$preKeyField];
132    } else {
133        $intFieldId = 0;
134    }
135    if (($chkModus == 'refresh') && ($chkSelValue1 != 0)) {
136        $intFieldId = $chkSelValue1;
137    }
138    $intReturn1 = $myVisClass->parseSelectSimple('tbl_host', $preKeyField, 'host', 0, $intFieldId);
139    if ($intReturn1 != 0) {
140        $myVisClass->processMessage($myVisClass->strErrorMessage, $strErrorMessage);
141        $myVisClass->processMessage(translate('Attention, no hosts defined!'), $strDBWarning);
142        $intDataWarning = 1;
143    }
144    // Process service selection field
145    if (isset($arrModifyData['service_description'])) {
146        $intFieldId = $arrModifyData['service_description'];
147    } else {
148        $intFieldId = 0;
149    }
150    $intReturn1 = $myVisClass->parseSelectSimple(
151        'tbl_service',
152        'service_description',
153        'service_extinfo',
154        0,
155        $intFieldId
156    );
157    if ($intReturn1 != 0) {
158        $myVisClass->processMessage($myVisClass->strErrorMessage, $strErrorMessage);
159    }
160    // Process access group selection field
161    if (isset($arrModifyData['access_group'])) {
162        $intFieldId = $arrModifyData['access_group'];
163    } else {
164        $intFieldId = 0;
165    }
166    $intReturn = $myVisClass->parseSelectSimple('tbl_group', 'groupname', 'acc_group', 0, $intFieldId);
167    if ($intReturn != 0) {
168        $myVisClass->processMessage($myVisClass->strErrorMessage, $strErrorMessage);
169    }
170    // Initial add/modify form definitions
171    $myContentClass->addFormInit($conttp);
172    if ($intDataWarning == 1) {
173        $conttp->setVariable('WARNING', $strDBWarning. '<br>' .translate('Saving not possible!'));
174    }
175    if ($intVersion < 3) {
176        $conttp->setVariable('VERSION_20_VALUE_MUST', 'mselValue1,');
177    }
178    if ($chkModus == 'refresh') {
179        $conttp->setVariable('DAT_NOTES', $chkTfValue1);
180        $conttp->setVariable('DAT_NOTES_URL', $chkTfValue2);
181        $conttp->setVariable('DAT_ACTION_URL', $chkTfValue3);
182        $conttp->setVariable('DAT_ICON_IMAGE', $chkTfValue4);
183        $conttp->setVariable('DAT_ICON_IMAGE_ALT', $chkTfValue5);
184        if ($chkActive   != 1) {
185            $conttp->setVariable('ACT_CHECKED', '');
186        }
187        if ($chkRegister != 1) {
188            $conttp->setVariable('REG_CHECKED', '');
189        }
190        if ($chkDataId   != 0) {
191            $conttp->setVariable('MODUS', 'modify');
192            $conttp->setVariable('DAT_ID', $chkDataId);
193        }
194        // Insert data from database in "modify" mode
195    } elseif (isset($arrModifyData) && ($chkSelModify == 'modify')) {
196        // Check relation information to find out locked configuration datasets
197        $intLocked = $myDataClass->infoRelation($preTableName, $arrModifyData['id'], $preKeyField);
198        $myVisClass->processMessage($myDataClass->strInfoMessage, $strRelMessage);
199        $strInfo  = '<br><span class="redmessage">' .translate('Entry cannot be activated because it is used by '
200                .'another configuration'). ':</span>';
201        $strInfo .= '<br><span class="greenmessage">' .$strRelMessage. '</span>';
202        // Process data
203        $myContentClass->addInsertData($conttp, $arrModifyData, $intLocked, $strInfo);
204    }
205    $conttp->parse('datainsert');
206    $conttp->show('datainsert');
207}
208//
209// List view
210// ==========
211if ($chkModus == 'display') {
212    // Initial list view definitions
213    $myContentClass->listViewInit($mastertp);
214    $mastertp->setVariable('TITLE', translate('Define service extended information (serviceextinfo.cfg)'));
215    $mastertp->setVariable('FIELD_1', translate('Hostname'));
216    $mastertp->setVariable('FIELD_2', translate('Service'));
217    $mastertp->setVariable('FILTER_VISIBLE', 'visibility: hidden');
218    // Process search string
219    if ($_SESSION['search'][$preSearchSession] != '') {
220        $strSearchTxt   = $_SESSION['search'][$preSearchSession];
221        $strSearchWhere = "AND (`tbl_host`.`$preKeyField` LIKE '%".$strSearchTxt."%' OR `$preTableName`.`notes` "
222            . "LIKE '%".$strSearchTxt."%' OR `$preTableName`.`notes_url` LIKE '%".$strSearchTxt."%')";
223    }
224    // Row sorting
225    $strOrderString = "ORDER BY `$preTableName`.`config_id`, `$preKeyField` $hidSortDir";
226    if ($hidSortBy == 2) {
227        $strOrderString = "ORDER BY `$preTableName`.`config_id`, `tbl_service`.`service_description` $hidSortDir";
228    }
229    // Count datasets
230    $strSQL    = "SELECT count(*) AS `number` FROM `$preTableName` "
231        . "LEFT JOIN `tbl_host` ON `$preTableName`.`$preKeyField` = `tbl_host`.`id` "
232        . "LEFT JOIN `tbl_service` ON `$preTableName`.`service_description` = `tbl_service`.`id` "
233        . "WHERE $strDomainWhere $strSearchWhere AND `$preTableName`.`access_group` IN ($strAccess)";
234    $booReturn = $myDBClass->hasSingleDataset($strSQL, $arrDataLinesCount);
235    if ($booReturn == false) {
236        $myVisClass->processMessage(translate('Error while selecting data from database:'), $strErrorMessage);
237        $myVisClass->processMessage($myDBClass->strErrorMessage, $strErrorMessage);
238    } else {
239        $intLineCount = (int)$arrDataLinesCount['number'];
240        if ($intLineCount < $chkLimit) {
241            $chkLimit = 0;
242        }
243    }
244    // Get datasets
245    $strSQL    = "SELECT `$preTableName`.`id`, `tbl_host`.`$preKeyField`, `tbl_service`.`service_description`, "
246        . "`$preTableName`.`notes`, `$preTableName`.`register`, `$preTableName`.`active`, `$preTableName`.`config_id`, "
247        . "`$preTableName`.`access_group` FROM `$preTableName` "
248        . "LEFT JOIN `tbl_host` ON `$preTableName`.`$preKeyField` = `tbl_host`.`id` "
249        . "LEFT JOIN `tbl_service` ON `$preTableName`.`service_description` = `tbl_service`.`id` "
250        . "WHERE $strDomainWhere $strSearchWhere AND `$preTableName`.`access_group` IN ($strAccess) $strOrderString "
251        . "LIMIT $chkLimit,".$SETS['common']['pagelines'];
252    $booReturn = $myDBClass->hasDataArray($strSQL, $arrDataLines, $intDataCount);
253    if ($booReturn == false) {
254        $myVisClass->processMessage(translate('Error while selecting data from database:'), $strErrorMessage);
255        $myVisClass->processMessage($myDBClass->strErrorMessage, $strErrorMessage);
256    }
257    // Process data
258    $myContentClass->listData(
259        $mastertp,
260        $arrDataLines,
261        $intDataCount,
262        $intLineCount,
263        $preKeyField,
264        'service_description'
265    );
266}
267// Show messages
268$myContentClass->showMessages(
269    $mastertp,
270    $strErrorMessage,
271    $strInfoMessage,
272    $strConsistMessage,
273    $arrTimeData,
274    $strTimeInfoString,
275    $intNoTime
276);
277//
278// Process footer
279// ==============
280$myContentClass->showFooter($maintp, $setFileVersion);
281