1<?php
2///////////////////////////////////////////////////////////////////////////////
3//
4// NagiosQL
5//
6///////////////////////////////////////////////////////////////////////////////
7//
8// (c) 2005-2020 by Martin Willisegger
9//
10// Project   : NagiosQL
11// Component : Admin servicegroup 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        = 11;
27$preContent       = 'admin/servicegroups.htm.tpl';
28$preListTpl       = 'admin/datalist.htm.tpl';
29$preSearchSession = 'servicegroup';
30$preTableName     = 'tbl_servicegroup';
31$preKeyField      = 'servicegroup_name';
32$preAccess        = 1;
33$preFieldvars     = 1;
34//
35// Include preprocessing file
36// ==========================
37require $preBasePath.'functions/prepend_adm.php';
38require $preBasePath.'functions/prepend_content.php';
39//
40// Add or modify data
41// ==================
42if ((($chkModus == 'insert') || ($chkModus == 'modify')) && ($intGlobalWriteAccess == 0)) {
43    $strSQLx = "`$preTableName` SET `$preKeyField`='$chkTfValue1', `alias`='$chkTfValue2', `members`=$intMselValue1, "
44             . "`servicegroup_members`=$intMselValue2, `notes`='$chkTfValue3', `notes_url`='$chkTfValue4', "
45             . "`action_url`='$chkTfValue5', $preSQLCommon1";
46    if ($chkModus == 'insert') {
47        $strSQL = 'INSERT INTO ' .$strSQLx;
48    } else {
49        $strSQL = 'UPDATE ' .$strSQLx. ' WHERE `id`=' .$chkDataId;
50    }
51    if ($intWriteAccessId == 0) {
52        if (($chkTfValue1 != '') && ($chkTfValue2 != '') && (($intMselValue1 != 0) || ($intVersion >= 3))) {
53            $intReturn = $myDataClass->dataInsert($strSQL, $intInsertId);
54            if ($chkModus == 'insert') {
55                $chkDataId = $intInsertId;
56            }
57            if ($intReturn == 1) {
58                $myVisClass->processMessage($myDataClass->strErrorMessage, $strErrorMessage);
59            } else {
60                $myVisClass->processMessage($myDataClass->strInfoMessage, $strInfoMessage);
61                $myDataClass->updateStatusTable($preTableName);
62                if ($chkModus  == 'insert') {
63                    $myDataClass->writeLog(translate('New service group inserted:'). ' ' .$chkTfValue1);
64                }
65                if ($chkModus  == 'modify') {
66                    $myDataClass->writeLog(translate('Service group modified:'). ' ' .$chkTfValue1);
67                }
68                //
69                // Insert/update relations
70                // =======================
71                if ($chkModus == 'insert') {
72                    if ($intMselValue1 != 0) {
73                        $intRet1 = $myDataClass->dataInsertRelation(
74                            'tbl_lnkServicegroupToService',
75                            $chkDataId,
76                            $chkMselValue1,
77                            1
78                        );
79                    }
80                    if (isset($intRet1) && ($intRet1 != 0)) {
81                        $myVisClass->processMessage($myDataClass->strErrorMessage, $strErrorMessage);
82                    }
83                    if ($intMselValue2 != 0) {
84                        $intRet2 = $myDataClass->dataInsertRelation(
85                            'tbl_lnkServicegroupToServicegroup',
86                            $chkDataId,
87                            $chkMselValue2
88                        );
89                    }
90                    if (isset($intRet2) && ($intRet2 != 0)) {
91                        $myVisClass->processMessage($myDataClass->strErrorMessage, $strErrorMessage);
92                    }
93                } elseif ($chkModus == 'modify') {
94                    if ($intMselValue1 != 0) {
95                        $intRet1 = $myDataClass->dataUpdateRelation(
96                            'tbl_lnkServicegroupToService',
97                            $chkDataId,
98                            $chkMselValue1,
99                            1
100                        );
101                    } else {
102                        $intRet1 = $myDataClass->dataDeleteRelation('tbl_lnkServicegroupToService', $chkDataId);
103                    }
104                    if ($intRet1 != 0) {
105                        $myVisClass->processMessage($myDataClass->strErrorMessage, $strErrorMessage);
106                    }
107                    if ($intMselValue2 != 0) {
108                        $intRet2 = $myDataClass->dataUpdateRelation(
109                            'tbl_lnkServicegroupToServicegroup',
110                            $chkDataId,
111                            $chkMselValue2
112                        );
113                    } else {
114                        $intRet2 = $myDataClass->dataDeleteRelation('tbl_lnkServicegroupToServicegroup', $chkDataId);
115                    }
116                    if ($intRet2 != 0) {
117                        $myVisClass->processMessage($myDataClass->strErrorMessage, $strErrorMessage);
118                    }
119                }
120                if (($intRet1 + $intRet2) != 0) {
121                    $strInfoMessage = '';
122                }
123            }
124        } else {
125            $myVisClass->processMessage(
126                translate('Database entry failed! Not all necessary data filled in!'),
127                $strErrorMessage
128            );
129        }
130    } else {
131        $myVisClass->processMessage(translate('Database entry failed! No write access!'), $strErrorMessage);
132    }
133    $chkModus = 'display';
134}
135if ($chkModus != 'add') {
136    $chkModus    = 'display';
137}
138//
139// Get date/time of last database and config file manipulation
140// ===========================================================
141$intReturn = $myConfigClass->lastModifiedFile($preTableName, $arrTimeData, $strTimeInfoString);
142if ($intReturn != 0) {
143    $myVisClass->processMessage($myConfigClass->strErrorMessage, $strErrorMessage);
144}
145//
146// Singe data form
147// ===============
148if ($chkModus == 'add') {
149    $conttp->setVariable('TITLE', translate('Define service groups (servicegroups.cfg)'));
150    // Do not show modified time list
151    $intNoTime = 1;
152    // Process service selection field
153    if (isset($arrModifyData['members'])) {
154        $intFieldId = $arrModifyData['members'];
155    } else {
156        $intFieldId = 0;
157    }
158    $intReturn1 = $myVisClass->parseSelectMulti(
159        'tbl_service',
160        'service_description',
161        'service_members',
162        'tbl_lnkServicegroupToService',
163        0,
164        $intFieldId
165    );
166    if ($intReturn1 != 0) {
167        $myVisClass->processMessage($myVisClass->strErrorMessage, $strErrorMessage);
168    }
169    if (($intReturn1 != 0) && ($intVersion < 3)) {
170        $myVisClass->processMessage(translate('Attention, no services defined!'), $strDBWarning);
171        $intDataWarning = 1;
172    }
173    // Process service group selection field
174    if (isset($arrModifyData['servicegroup_members'])) {
175        $intFieldId = $arrModifyData['servicegroup_members'];
176    } else {
177        $intFieldId = 0;
178    }
179    $intReturn2 = $myVisClass->parseSelectMulti(
180        $preTableName,
181        $preKeyField,
182        'servicegroups',
183        'tbl_lnkServicegroupToServicegroup',
184        0,
185        $intFieldId,
186        $chkListId
187    );
188    if ($intReturn2 != 0) {
189        $myVisClass->processMessage($myVisClass->strErrorMessage, $strErrorMessage);
190    }
191    // Process access group selection field
192    if (isset($arrModifyData['access_group'])) {
193        $intFieldId = $arrModifyData['access_group'];
194    } else {
195        $intFieldId = 0;
196    }
197    $intReturn3 = $myVisClass->parseSelectSimple('tbl_group', 'groupname', 'acc_group', 0, $intFieldId);
198    if ($intReturn3 != 0) {
199        $myVisClass->processMessage($myVisClass->strErrorMessage, $strErrorMessage);
200    }
201    // Initial add/modify form definitions
202    $myContentClass->addFormInit($conttp);
203    if ($intDataWarning == 1) {
204        $conttp->setVariable('WARNING', $strDBWarning. '<br>' .translate('Saving not possible!'));
205    }
206    if ($intVersion < 3) {
207        $conttp->setVariable('VERSION_20_VALUE_MUST', 'mselValue1,');
208    }
209    // Insert data from database in "modify" mode
210    if (isset($arrModifyData) && ($chkSelModify == 'modify')) {
211        // Check relation information to find out locked configuration datasets
212        $intLocked = $myDataClass->infoRelation($preTableName, $arrModifyData['id'], $preKeyField);
213        $myVisClass->processMessage($myDataClass->strInfoMessage, $strRelMessage);
214        $strInfo  = '<br><span class="redmessage">' .translate('Entry cannot be activated because it is used by '
215                  . 'another configuration'). ':</span>';
216        $strInfo .= '<br><span class="greenmessage">' .$strRelMessage. '</span>';
217        // Process data
218        $myContentClass->addInsertData($conttp, $arrModifyData, $intLocked, $strInfo);
219    }
220    $conttp->parse('datainsert');
221    $conttp->show('datainsert');
222}
223//
224// List view
225// ==========
226if ($chkModus == 'display') {
227    // Initial list view definitions
228    $myContentClass->listViewInit($mastertp);
229    $mastertp->setVariable('TITLE', translate('Define service groups (servicegroups.cfg)'));
230    $mastertp->setVariable('FIELD_1', translate('Service group'));
231    $mastertp->setVariable('FIELD_2', translate('Description'));
232    // Process search string and filter
233    $strSearchWhere = '';
234    if ($_SESSION['search'][$preSearchSession] != '') {
235        $strSearchTxt    = $_SESSION['search'][$preSearchSession];
236        $strSearchWhere .= "AND (`$preKeyField` LIKE '%".$strSearchTxt."%' OR `alias` LIKE '%".$strSearchTxt."%' "
237                        . "OR `notes` LIKE '%".$strSearchTxt."%') ";
238    }
239    if ($_SESSION['filter'][$preSearchSession]['registered'] != '') {
240        $intRegistered = (int)$_SESSION['filter'][$preSearchSession]['registered'];
241        if ($intRegistered == 1) {
242            $strSearchWhere .= "AND `register` = '1' ";
243        }
244        if ($intRegistered == 2) {
245            $strSearchWhere .= "AND `register` = '0' ";
246        }
247        $mastertp->setVariable('SEL_REGFILTER_'.$intRegistered.'_SELECTED', 'selected');
248    }
249    if ($_SESSION['filter'][$preSearchSession]['active'] != '') {
250        $intActivated = (int)$_SESSION['filter'][$preSearchSession]['active'];
251        if ($intActivated == 1) {
252            $strSearchWhere .= "AND `active` = '1' ";
253        }
254        if ($intActivated == 2) {
255            $strSearchWhere .= "AND `active` = '0' ";
256        }
257        $mastertp->setVariable('SEL_ACTIVEFILTER_'.$intActivated.'_SELECTED', 'selected');
258    }
259    // Row sorting
260    $strOrderString = "ORDER BY `config_id`, `$preKeyField` $hidSortDir";
261    if ($hidSortBy == 2) {
262        $strOrderString = "ORDER BY `config_id`, `alias` $hidSortDir";
263    }
264    // Count datasets
265    $strSQL1    = "SELECT count(*) AS `number` FROM `$preTableName` WHERE $strDomainWhere $strSearchWhere "
266                . "AND `access_group` IN ($strAccess)";
267    $booReturn1 = $myDBClass->hasSingleDataset($strSQL1, $arrDataLinesCount);
268    if ($booReturn1 == false) {
269        $myVisClass->processMessage(translate('Error while selecting data from database:'), $strErrorMessage);
270        $myVisClass->processMessage($myDBClass->strErrorMessage, $strErrorMessage);
271    } else {
272        $intLineCount = (int)$arrDataLinesCount['number'];
273        if ($intLineCount < $chkLimit) {
274            $chkLimit = 0;
275        }
276    }
277    // Get datasets
278    $strSQL2    = "SELECT `id`, `$preKeyField`, `alias`, `register`, `active`, `config_id`, `access_group` "
279                . "FROM `$preTableName` WHERE $strDomainWhere $strSearchWhere AND `access_group` IN ($strAccess) "
280                . "$strOrderString LIMIT $chkLimit,".$SETS['common']['pagelines'];
281    $booReturn2 = $myDBClass->hasDataArray($strSQL2, $arrDataLines, $intDataCount);
282    if ($booReturn2 == false) {
283        $myVisClass->processMessage(translate('Error while selecting data from database:'), $strErrorMessage);
284        $myVisClass->processMessage($myDBClass->strErrorMessage, $strErrorMessage);
285    }
286    // Process data
287    $myContentClass->listData($mastertp, $arrDataLines, $intDataCount, $intLineCount, $preKeyField, 'alias');
288}
289// Show messages
290$myContentClass->showMessages(
291    $mastertp,
292    $strErrorMessage,
293    $strInfoMessage,
294    $strConsistMessage,
295    $arrTimeData,
296    $strTimeInfoString,
297    $intNoTime
298);
299//
300// Process footer
301// ==============
302$myContentClass->showFooter($maintp, $setFileVersion);
303