1<?php
2///////////////////////////////////////////////////////////////////////////////
3//
4// NagiosQL
5//
6///////////////////////////////////////////////////////////////////////////////
7//
8// (c) 2005-2020 by Martin Willisegger
9//
10// Project   : NagiosQL
11// Component : Admin configuration target administration
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      = 36;
27$preContent     = 'admin/configtargets.htm.tpl';
28$preListTpl     = 'admin/datalist_common.htm.tpl';
29$preTableName   = 'tbl_configtarget';
30$preKeyField    = 'target';
31$preAccess      = 1;
32$preFieldvars   = 1;
33$intIsError     = 0;
34$strPathMessage = '';
35//
36// Include preprocessing files
37// ===========================
38require $preBasePath.'functions/prepend_adm.php';
39require $preBasePath.'functions/prepend_content.php';
40//
41// Process path values (add slashes)
42// =================================
43$chkTfValue8  = $myVisClass->addSlash($chkTfValue8);
44$chkTfValue9  = $myVisClass->addSlash($chkTfValue9);
45$chkTfValue10 = $myVisClass->addSlash($chkTfValue10);
46$chkTfValue11 = $myVisClass->addSlash($chkTfValue11);
47$chkTfValue12 = $myVisClass->addSlash($chkTfValue12);
48$chkTfValue13 = $myVisClass->addSlash($chkTfValue13);
49$chkTfValue14 = $myVisClass->addSlash($chkTfValue14);
50$chkTfValue15 = $myVisClass->addSlash($chkTfValue15);
51$chkTfValue16 = $myVisClass->addSlash($chkTfValue16);
52//
53// Check Port Value
54// ================
55/** @noinspection UnnecessaryCastingInspection */
56$chkTfValue23 = (int)$chkTfValue23;
57if ($chkTfValue23 == 0) {
58    $chkTfValue23 = 22;
59}
60//
61// Check if the permissions and other parameters
62// =============================================
63if (($chkModus == 'modify' || $chkModus == 'insert') && $chkDataId != 0) {
64    if ($chkSelValue1 == 1) {
65        $arrPaths = array($chkTfValue8,$chkTfValue9,$chkTfValue10,$chkTfValue11,$chkTfValue12,$chkTfValue13);
66        foreach ($arrPaths as $elem) {
67            if ($myConfigClass->isDirWriteable($elem) == 1) {
68                $myVisClass->processMessage($elem. ' ' .translate('is not writeable'), $strPathMessage);
69                $intIsError = 1;
70            }
71        }
72        // Nagios base configuration files
73        if (!is_writable($chkTfValue20)) {
74            $myVisClass->processMessage(str_replace('  ', ' ', translate('Nagios config file'). ' ' .$chkTfValue20
75                    . ' ' .translate('is not writeable')), $strPathMessage);
76            $intIsError = 1;
77        } else {
78            $intCheck = 0;
79            if (file_exists($chkTfValue20) && is_readable($chkTfValue20)) {
80                $resFile = fopen($chkTfValue20, 'rb');
81                while (!feof($resFile)) {
82                    $strLine = trim(fgets($resFile));
83                    if ((substr_count($strLine, 'cfg_dir') != 0) || (substr_count($strLine, 'cfg_file') != 0)) {
84                        $intCheck = 1;
85                    }
86                }
87                fclose($resFile);
88            }
89            if ($intCheck == 0) {
90                $myVisClass->processMessage(str_replace('  ', ' ', translate('Nagios config file'). ' ' .
91                        $chkTfValue20. ' ' .translate('is not a valid configuration file!')), $strPathMessage);
92                $intIsError = 1;
93            }
94        }
95        if (!is_writable($chkTfValue14)) {
96            $myVisClass->processMessage(str_replace('  ', ' ', translate('Nagios base directory'). ' ' .
97                $chkTfValue14. ' ' .translate('is not writeable')), $strPathMessage);
98            $intIsError = 1;
99        }
100        if (!is_writable($chkTfValue21)) {
101            $myVisClass->processMessage(str_replace('  ', ' ', translate('Nagios cgi config file'). ' ' .
102                    $chkTfValue21. ' ' .translate('is not writeable')), $strPathMessage);
103            $intIsError = 1;
104        }
105        if (!is_readable($chkTfValue22)) {
106            $myVisClass->processMessage(str_replace('  ', ' ', translate('Nagios resource config file'). ' ' .
107                $chkTfValue22. ' ' .translate('is not readable')), $strPathMessage);
108            $intIsError = 1;
109        }
110    }
111    // Check SSH Method
112    if (($chkSelValue1  == 3) && !function_exists('ssh2_connect')) {
113        $myVisClass->processMessage(translate('SSH module not loaded!'), $strPathMessage);
114        $intIsError = 1;
115    }
116    // Check FTP Method
117    if (($chkSelValue1  == 2) && !function_exists('ftp_connect')) {
118        $myVisClass->processMessage(translate('FTP module not loaded!'), $strPathMessage);
119        $intIsError = 1;
120    }
121    if ($intIsError == 1) {
122        $chkModus     = 'add';
123        $chkSelModify = 'errormodify';
124    }
125}
126//
127// Add or modify data
128// ==================
129if ((($chkModus == 'insert') || ($chkModus == 'modify')) && ($intGlobalWriteAccess == 0)) {
130    $strSQLx = "`$preTableName` SET `$preKeyField`='$chkTfValue1', `alias`='$chkTfValue2', `server`='$chkTfValue4', "
131             . "`port`='$chkTfValue23', `method`='$chkSelValue1', `user`='$chkTfValue5', `password`='$chkTfValue6', "
132             . "`ssh_key_path`='$chkTfValue7', `ftp_secure`=$chkChbValue1, `basedir`='$chkTfValue8', "
133             . "`hostconfig`='$chkTfValue9', `serviceconfig`='$chkTfValue10', `backupdir`='$chkTfValue11', "
134             . "`hostbackup`='$chkTfValue12', `servicebackup`='$chkTfValue13', `nagiosbasedir`='$chkTfValue14', "
135             . "`importdir`='$chkTfValue15', `picturedir`='$chkTfValue16', `commandfile`='$chkTfValue17', "
136             . "`binaryfile`='$chkTfValue18', `pidfile`='$chkTfValue19', `conffile`='$chkTfValue20', "
137             . "`cgifile`='$chkTfValue21', `resourcefile`='$chkTfValue22',`version`=$chkSelValue2, "
138             . "`access_group`=$chkSelAccGr, `active`='$chkActive',`last_modified`=NOW()";
139    if ($chkModus == 'insert') {
140        $strSQL = 'INSERT INTO ' .$strSQLx;
141    } else {
142        $strSQL = 'UPDATE ' .$strSQLx. ' WHERE `id`=' .$chkDataId;
143    }
144    if ($intWriteAccessId == 0) {
145        if (($chkTfValue1 != '') && ($chkTfValue2 != '') && (($chkTfValue4 != '') || ($chkDataId == 0))) {
146            $intReturn = $myDataClass->dataInsert($strSQL, $intInsertId);
147            if ($intReturn == 1) {
148                $myVisClass->processMessage($myDataClass->strErrorMessage, $strErrorMessage);
149            } else {
150                $myVisClass->processMessage($myDataClass->strInfoMessage, $strInfoMessage);
151                if ($chkModus == 'insert') {
152                    $myDataClass->writeLog(translate('New Domain inserted:'). ' ' .$chkTfValue1);
153                }
154                if ($chkModus == 'modify') {
155                    $myDataClass->writeLog(translate('Domain modified:'). ' ' .$chkTfValue1);
156                }
157            }
158        } else {
159            $myVisClass->processMessage(
160                translate('Database entry failed! Not all necessary data filled in!'),
161                $strErrorMessage
162            );
163        }
164    } else {
165        $myVisClass->processMessage(translate('Database entry failed! No write access!'), $strErrorMessage);
166    }
167    $chkModus = 'display';
168}
169if ($chkModus != 'add') {
170    $chkModus = 'display';
171}
172//
173// Single view
174// ===========
175if ($chkModus == 'add') {
176    // Process acces group selection field
177    if (isset($arrModifyData['access_group'])) {
178        $intFieldId = $arrModifyData['access_group'];
179    } else {
180        $intFieldId = 0;
181    }
182    $intReturn = $myVisClass->parseSelectSimple('tbl_group', 'groupname', 'acc_group', 0, $intFieldId);
183    if ($intReturn != 0) {
184        $myVisClass->processMessage($myVisClass->strErrorMessage, $strErrorMessage);
185    }
186    // Initial add/modify form definitions
187    $myContentClass->addFormInit($conttp);
188    $conttp->setVariable('TITLE', translate('Configuration domain administration'));
189    if ($intIsError == 1) {
190        $conttp->setVariable('PATHMESSAGE', '<h2 style="padding-bottom:5px;">' .translate('Warning, at least one ' .
191                'error occured, please check!'). '</h2>' .$strPathMessage);
192    }
193    $conttp->setVariable('CLASS_NAME_1', 'elementHide');
194    $conttp->setVariable('CLASS_NAME_2', 'elementHide');
195    $conttp->setVariable('CLASS_NAME_3', 'elementHide');
196    $conttp->setVariable('FILL_ALLFIELDS', translate('Please fill in all fields marked with an *'));
197    $conttp->setVariable('FILL_ILLEGALCHARS', translate('The following field contains illegal characters:'));
198    // Insert data from database in "modify" mode
199    if (isset($arrModifyData) && ($chkSelModify == 'modify')) {
200        // Process data
201        $myContentClass->addInsertData($conttp, $arrModifyData, 0, '');
202        // Connection method
203        if ($arrModifyData['method'] == 1) {
204            $conttp->setVariable('FILE_SELECTED', 'selected');
205        }
206        if ($arrModifyData['method'] == 2) {
207            $conttp->setVariable('FTP_SELECTED', 'selected');
208            $conttp->setVariable('CLASS_NAME_1', 'elementShow');
209            $conttp->setVariable('CLASS_NAME_2', 'elementHide');
210            $conttp->setVariable('CLASS_NAME_3', 'elementShow');
211        }
212        if ($arrModifyData['method'] == 3) {
213            $conttp->setVariable('SFTP_SELECTED', 'selected');
214            $conttp->setVariable('CLASS_NAME_1', 'elementShow');
215            $conttp->setVariable('CLASS_NAME_2', 'elementShow');
216            $conttp->setVariable('CLASS_NAME_3', 'elementHide');
217        }
218        if ($arrModifyData['ftp_secure'] == 1) {
219            $conttp->setVariable('FTPS_CHECKED', 'checked');
220        }
221        // Nagios version
222        $conttp->setVariable('VER_SELECTED_' .$arrModifyData['version'], 'selected');
223        // Domain localhost cant' be renamed
224        if ($arrModifyData[$preKeyField] == 'localhost') {
225            $conttp->setVariable('DOMAIN_DISABLE', 'readonly');
226            $conttp->setVariable('LOCKCLASS', 'inputlock');
227        } elseif ($arrModifyData[$preKeyField] == 'common') {
228            $conttp->setVariable('DOMAIN_DISABLE', 'readonly');
229            $conttp->setVariable('COMMON_INVISIBLE', 'class="elementHide"');
230            $conttp->setVariable('LOCKCLASS', 'inputlock');
231        }
232    }
233    if ($chkSelModify == 'errormodify') {
234        $conttp->setVariable('DAT_TARGET', $chkTfValue1);
235        // Domain localhost cant' be renamed
236        if ($chkTfValue1 == 'localhost') {
237            $conttp->setVariable('DOMAIN_DISABLE', 'readonly');
238            $conttp->setVariable('LOCKCLASS', 'inputlock');
239        } elseif ($chkTfValue1 == 'common') {
240            $conttp->setVariable('DOMAIN_DISABLE', 'readonly');
241            $conttp->setVariable('COMMON_INVISIBLE', 'class="elementHide"');
242            $conttp->setVariable('LOCKCLASS', 'inputlock');
243        } else {
244            $conttp->setVariable('LOCKCLASS', 'inpmust');
245        }
246        $conttp->setVariable('DAT_ALIAS', $chkTfValue2);
247        $conttp->setVariable('DAT_SERVER', $chkTfValue4);
248        // Connection method
249        if ($chkSelValue1 == 1) {
250            $conttp->setVariable('FILE_SELECTED', 'selected');
251            $conttp->setVariable('CLASS_NAME_1', 'elementHide');
252            $conttp->setVariable('CLASS_NAME_2', 'elementHide');
253            $conttp->setVariable('CLASS_NAME_3', 'elementHide');
254        }
255        if ($chkSelValue1 == 2) {
256            $conttp->setVariable('FTP_SELECTED', 'selected');
257            $conttp->setVariable('CLASS_NAME_1', 'elementShow');
258            $conttp->setVariable('CLASS_NAME_2', 'elementHide');
259            $conttp->setVariable('CLASS_NAME_3', 'elementShow');
260        }
261        if ($chkSelValue1 == 3) {
262            $conttp->setVariable('SFTP_SELECTED', 'selected');
263            $conttp->setVariable('CLASS_NAME_1', 'elementShow');
264            $conttp->setVariable('CLASS_NAME_2', 'elementShow');
265            $conttp->setVariable('CLASS_NAME_3', 'elementHide');
266        }
267        $conttp->setVariable('DAT_USER', $chkTfValue5);
268        $conttp->setVariable('DAT_SSH_KEY_PATH', $chkTfValue7);
269        if ($chkChbValue1== 1) {
270            $conttp->setVariable('FTPS_CHECKED', 'checked');
271        }
272        $conttp->setVariable('DAT_BASEDIR', $chkTfValue8);
273        $conttp->setVariable('DAT_HOSTCONFIG', $chkTfValue9);
274        $conttp->setVariable('DAT_SERVICECONFIG', $chkTfValue10);
275        $conttp->setVariable('DAT_BACKUPDIR', $chkTfValue11);
276        $conttp->setVariable('DAT_HOSTBACKUP', $chkTfValue12);
277        $conttp->setVariable('DAT_SERVICEBACKUP', $chkTfValue13);
278        $conttp->setVariable('DAT_NAGIOSBASEDIR', $chkTfValue14);
279        $conttp->setVariable('DAT_IMPORTDIR', $chkTfValue15);
280        $conttp->setVariable('DAT_COMMANDFILE', $chkTfValue17);
281        $conttp->setVariable('DAT_BINARYFILE', $chkTfValue18);
282        $conttp->setVariable('DAT_PIDFILE', $chkTfValue19);
283        $conttp->setVariable('DAT_CONFFILE', $chkTfValue20);
284        $conttp->setVariable('DAT_CGIFILE', $chkTfValue21);
285        $conttp->setVariable('DAT_RESOURCEFILE', $chkTfValue22);
286        $conttp->setVariable('DAT_PICTUREDIR', $chkTfValue16);
287        // NagiosQL version
288        if ($chkSelValue2 == 1) {
289            $conttp->setVariable('VER_SELECTED_1', 'selected');
290        }
291        if ($chkSelValue2 == 2) {
292            $conttp->setVariable('VER_SELECTED_2', 'selected');
293        }
294        if ($chkSelValue2 == 3) {
295            $conttp->setVariable('VER_SELECTED_3', 'selected');
296        }
297        // Hidden variables
298        $conttp->setVariable('MODUS', filter_input(INPUT_POST, 'modus', FILTER_SANITIZE_STRING));
299        $conttp->setVariable('DAT_ID', filter_input(INPUT_POST, 'hidId', FILTER_VALIDATE_INT));
300        $conttp->setVariable('LIMIT', filter_input(INPUT_POST, 'hidLimit', FILTER_VALIDATE_INT));
301        // Active
302        if (filter_input(INPUT_POST, 'chbActive')) {
303            $conttp->setVariable('ACT_CHECKED', 'checked');
304        } else {
305            $conttp->setVariable('ACT_CHECKED', '');
306        }
307    }
308    $conttp->parse('datainsert');
309    $conttp->show('datainsert');
310}
311//
312// List view
313// ==========
314if ($chkModus == 'display') {
315    // Initial list view definitions
316    $myContentClass->listViewInit($mastertp);
317    $mastertp->setVariable('TITLE', translate('Configuration domain administration'));
318    $mastertp->setVariable('FIELD_1', translate('Configuration target'));
319    $mastertp->setVariable('FIELD_2', translate('Description'));
320    // Row sorting
321    $strOrderString = "ORDER BY `$preKeyField` $hidSortDir";
322    if ($hidSortBy == 2) {
323        $strOrderString = "ORDER BY `alias` $hidSortDir";
324    }
325    // Count datasets
326    $strSQL     = "SELECT count(*) AS `number` FROM `$preTableName` WHERE `access_group` IN ($strAccess)";
327    $booReturn1 = $myDBClass->hasSingleDataset($strSQL, $arrDataLinesCount);
328    if ($booReturn1 == false) {
329        $myVisClass->processMessage(translate('Error while selecting data from database:'), $strErrorMessage);
330        $myVisClass->processMessage($myDBClass->strErrorMessage, $strErrorMessage);
331    } else {
332        $intLineCount = (int)$arrDataLinesCount['number'];
333        if ($intLineCount < $chkLimit) {
334            $chkLimit = 0;
335        }
336    }
337    // Get datasets
338    $strSQL     = "SELECT `id`, `$preKeyField`, `alias`, `active`, `nodelete`, `access_group` "
339                . "FROM `$preTableName` WHERE `access_group` IN ($strAccess) $strOrderString "
340                . "LIMIT $chkLimit,".$SETS['common']['pagelines'];
341    $booReturn2 = $myDBClass->hasDataArray($strSQL, $arrDataLines, $intDataCount);
342    if ($booReturn2 == false) {
343        $myVisClass->processMessage(translate('Error while selecting data from database:'), $strErrorMessage);
344        $myVisClass->processMessage($myDBClass->strErrorMessage, $strErrorMessage);
345    }
346    // Process data
347    $myContentClass->listData($mastertp, $arrDataLines, $intDataCount, $intLineCount, $preKeyField, 'alias');
348}
349// Show messages
350$myContentClass->showMessages($mastertp, $strErrorMessage, $strInfoMessage, $strConsistMessage, array(), '', 1);
351//
352// Process footer
353// ==============
354$myContentClass->showFooter($maintp, $setFileVersion);
355