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