function OpenLogFile() { global $log_handle; global $DEBUG; if ($DEBUG) { $log_handle = @fopen("/var/tmp/nfsen-log", "a"); $_d = date("Y-m-d-H:i:s"); ReportLog("\n=========================\nPic run at {$_d}\n"); } else { $log_handle = null; } }
function NewProfileCreate($profileinfo, $type) { ob_start(); print "ADD PROFILE, type {$type}"; // compile argument options for nfsend $cmd_opts['profile'] = $profileinfo['profileswitch']; $cmd_opts['description'] = $profileinfo['description']; if (!is_null($profileinfo['tstart'])) { $cmd_opts['tstart'] = UNIX2ISO($profileinfo['tstart']); } if (!is_null($profileinfo['tend'])) { $cmd_opts['tend'] = UNIX2ISO($profileinfo['tend']); } $cmd_opts['expire'] = $profileinfo['expire']; $cmd_opts['maxsize'] = $profileinfo['maxsize']; $cmd_opts['shadow'] = $profileinfo['shadow']; print "Add profile"; $cmd_out = nfsend_query("add-profile", $cmd_opts, 0); if (is_bool($cmd_out) && $cmd_out == FALSE) { // Add profile failed. print "Add profile failed."; ReportLog(ob_get_contents()); ob_clean(); return FALSE; } if ($type == 'individual') { print "Add profile succeeded."; ReportLog(ob_get_contents()); ob_clean(); return TRUE; } // clear argument options to start over for each channel unset($cmd_opts); $cmd_opts['profile'] = $profileinfo['profileswitch']; $cmd_opts['sign'] = '+'; $cmd_opts['filter'] = $profileinfo['filter']; foreach ($profileinfo['channel'] as $channel) { $cmd_opts['channel'] = $channel; $cmd_opts['sourcelist'] = $channel; print "Add channel '{$channel}'"; $cmd_out = nfsend_query("add-channel", $cmd_opts, 0); if (is_bool($cmd_out) && $cmd_out == FALSE) { // Add channel failed. - hmm .. ugly unset($cmd_opts); print "Add channel failed."; // we force to delete this profile to clean up any remains $cmd_opts['profile'] = $profileinfo['profileswitch']; $cmd_opts['force'] = 1; print "Delete profile"; $cmd_out = nfsend_query("delete-profile", $cmd_opts, 0); if (is_bool($cmd_out) && $cmd_out == FALSE) { // Delete profile failed. - double failure print "Add profile failed."; ReportLog(ob_get_contents()); ob_clean(); } return FALSE; } } print "All channels added"; // successfully added all channels => commit the profile unset($cmd_opts); $cmd_opts['profile'] = $profileinfo['profileswitch']; print "Commit profile"; $cmd_out = nfsend_query("commit-profile", $cmd_opts, 0); if (is_bool($cmd_out) && $cmd_out == FALSE) { // Commit profile failed. - strange .. print "Commit failed"; $cmd_opts['force'] = 1; print "Force delete profile"; $cmd_out = nfsend_query("delete-profile", $cmd_opts, 0); if (is_bool($cmd_out) && $cmd_out == FALSE) { // Delete profile failed. - double failure print "Force delete profile failed."; ReportLog(ob_get_contents()); ob_clean(); } return FALSE; } print "Add profile succeeded.>"; ReportLog(ob_get_contents()); ob_clean(); return TRUE; }
function nfsend_query($command, $cmd_opts) { global $DEBUG; if (!isset($_SESSION['nfsend'])) { ReportLog("nfsend No socket - open connection first"); nfsend_connect(); } if (!isset($_SESSION['nfsend'])) { SetMessage('error', _("nfsend - connection failed!")); return FALSE; } $nfsend = $_SESSION['nfsend']; $is_binary = preg_match("/^@/", $command); if ($DEBUG == 1 && !$is_binary) { ReportLog("nfsend INTERNAL '.debug=1'"); @socket_write($nfsend, ".debug=1\n"); } ReportLog("nfsend COMMAND '{$command}' binary: {$is_binary}"); // Socket may have timouted since last query // check for errors while sending command, and reopenn socket in case of an error if (@socket_write($nfsend, "{$command}\n") == FALSE) { $errstr = socket_strerror(socket_last_error($nfsend)); ReportLog("nfsend 1st write() failed: reason: " . socket_strerror(socket_last_error($nfsend))); nfsend_connect(); $nfsend = $_SESSION['nfsend']; if (@socket_write($nfsend, "{$command}\n") == FALSE) { $errstr = socket_strerror(socket_last_error($nfsend)); SetMessage('error', _("nfsend socket_write() communication error") . ": {$errstr}"); @socket_close($nfsend); unset($_SESSION['nfsend']); return FALSE; } } // the socket is established and ready - just send the opts foreach ($cmd_opts as $key => $value) { if (is_array($value)) { foreach ($value as $val) { ReportLog("nfsend WRITE: '_{$key}'='{$val}'"); @socket_write($nfsend, "_{$key}={$val}\n"); } } else { ReportLog("nfsend WRITE: '{$key}'='{$value}'"); @socket_write($nfsend, "{$key}={$value}\n"); } } // send EODATA ReportLog("nfsend EODATA"); @socket_write($nfsend, ".\n"); $out_list = array(); $debug = array(); $done = 0; $EODATA = 0; $error_occured = 0; while (!$done) { if ($is_binary) { $line = @socket_read($nfsend, 1024, PHP_BINARY_READ); } else { $line = @socket_read($nfsend, 1024, PHP_NORMAL_READ); } if ($line == FALSE) { $errno = socket_last_error($nfsend); if ($errno) { $errstr = socket_strerror(socket_last_error($nfsend)); $ret = FALSE; SetMessage('error', _("nfsend socket_read() communication error") . ": {$errstr}"); ReportLog("nfsend connection error '{$errno}' '{$errstr}'"); } else { // connection closed in binary mode $ret = TRUE; } @socket_close($nfsend); unset($_SESSION['nfsend']); return $ret; } if ($is_binary) { print "{$line}"; continue; } $line = rtrim($line); if (preg_match("/^\$/", $line)) { continue; } // was last line EODATA? if ($EODATA) { // if so, $line contains the status message $done = 1; $EODATA = 0; ReportLog("nfsend STATUS '{$line}'"); // parse status line for various messages if (strncasecmp($line, "ok ", 3) == 0) { continue; } if (strncasecmp($line, "err ", 4) == 0) { $msg = substr($line, 4); $error_occured = 1; SetMessage('error', "nfsend: {$msg}"); continue; } if (strncasecmp($line, "warn ", 5) == 0) { $msg = substr($line, 5); SetMessage('warning', "nfsend: {$msg}"); continue; } if (strncasecmp($line, "alert ", 6) == 0) { $msg = substr($line, 6); SetMessage('alert', "nfsend: {$msg}"); continue; } // not needed, but catch it anyway continue; } if (preg_match("/^\\..+/", $line)) { ReportLog("nfsend Skip line '{$line}'"); $debug[] = $line; continue; } if (preg_match("/^INFO /", $line)) { ReportLog("nfsend Skip info line '{$line}'"); continue; } // EODATA received if (preg_match("/^\\.\$/", $line)) { $EODATA = 1; continue; } if (!preg_match("/=/", $line)) { ReportLog("nfsend Skip buggy line '{$line}' Expected key=value pair"); continue; } ReportLog("nfsend Process line '{$line}'"); // parse regular output lines list($key, $value) = split('=', $line, 2); // check for multiline output if (preg_match("/^\\_(.+)/", $key, $matches)) { $key = $matches[1]; $out_list[$key][] = $value; } else { $out_list[$key] = $value; } } return $is_binary ? TRUE : ($error_occured ? FALSE : $out_list); }
function ParseInput() { global $TabList; global $BookmarkVars; global $GraphTabs; global $Refresh; // Preset refresh value. Any Input pasring routing may reset refresh to 0, to disable refresh $_SESSION['refresh'] = $Refresh; /* * user input may come from forms or links (POST or GET data) due to normal * form processing. If a bookmark is specified in the URL, this overwrites other * input data. To simplify data input checks, the bookmark is handled as any other post request */ ReportLog("ParseInput:"); if (isset($_GET['bookmark'])) { // process bookmarkstring $_bookmark = Util::htmlentities(base64_decode(urldecode($_GET['bookmark']))); ReportLog("Bookmark: '{$_bookmark}'"); $_vars = explode('|', $_bookmark); if (count($BookmarkVars) == count($_vars)) { for ($i = 0; $i < count($BookmarkVars); $i++) { if ($_vars[$i] != '-') { $_varpath = explode('/', $BookmarkVars[$i]); $_varname = count($_varpath) == 2 ? $_varpath[1] : $_varpath[0]; ReportLog("Bookmark: Set {$_varname}"); $_POST[$_varname] = $_vars[$i]; } } } else { SetMessage('warning', "Bookmark processing error"); } } // process tab if (!array_key_exists('tab', $_SESSION)) { // first time in this session // initialize some more vars in the SESSION var InitSession(count($BookmarkVars)); } else { $_tab = $_SESSION['tab']; } // click on tab list if (array_key_exists('tab', $_GET)) { $_tab = $_GET['tab']; } // tab from bookmark overwrites other entries if (array_key_exists('tab', $_POST)) { $_tab = $_POST['tab']; } $tab_changed = 0; if ($_tab != $_SESSION['tab'] || $_SESSION['tab'] == NULL) { // _tab changed since last cycle if (array_key_exists('tablock', $_SESSION)) { // must not change tab right now SetMessage('error', $_SESSION['tablock']); } else { // Verify new tab if (!is_numeric($_tab) || ($_tab > count($TabList) || $_tab < 0)) { SetMessage('warning', "Requested Tab not available. Set default tab to " . $TabList[0]); $_tab = 0; } $_tab = (int) $_tab; $_SESSION['tab'] = $_tab; if (!isset($_GET['bookmark'])) { $tab_changed = 1; } ReportLog("Tab: Set tab to {$_tab}: " . $TabList[$_tab]); } } // rebuild profile list if ($tab_changed && $_SESSION['tab'] == 4) { unset($_SESSION['ProfileList']); unset($_SESSION['PluginList']); $profiles = GetProfiles(); GetPlugins(); } // process sub tab $_tab = -1; if (array_key_exists('sub_tab', $_GET)) { $_tab = strip_tags($_GET['sub_tab']); } if (array_key_exists('sub_tab', $_POST)) { $_tab = strip_tags($_POST['sub_tab']); } if ($_tab >= 0) { if (!is_numeric($_tab) || $_tab < 0) { $_tab = 0; } $_SESSION['sub_tab'] = $_tab; ReportLog("Subtab: Set tab to {$_tab}: " . $GraphTabs[$_tab]); } // process profileswitch if (!array_key_exists('profileswitch', $_SESSION)) { // this is fishy - InitSession should have set this SetMessage('error', "Missing session parameter 'profileswitch'"); $_SESSION['refresh'] = 0; return array(FALSE, 0, 0); } else { $_profileswitch = $_SESSION['profileswitch']; } if (array_key_exists('profileswitch', $_POST)) { $_profileswitch = Util::htmlentities($_POST['profileswitch']); } // the alerting module only accepts profile live for now if ($_SESSION['tab'] == 3) { $_profileswitch = './live'; } $profile_changed = 0; if ($_profileswitch != $_SESSION['profileswitch']) { if ($_profileswitch == "New Profile ...") { // make sure the profile admin page gets this request; $_SESSION['tab'] = 4; $_SESSION['new_profile'] = TRUE; $_SESSION['refresh'] = 0; } else { // process new profileswitch if (preg_match("/^(.+)\\/(.+)/", $_profileswitch, $matches)) { $_profilegroup = $matches[1]; $_profilename = $matches[2]; // Check if profilegroup/profilename exists $_found = FALSE; foreach ($_SESSION['ProfileList'] as $p) { if ($p == $_profileswitch) { $_found = TRUE; } } if (!$_found) { SetMessage('error', "Profile '{$_profilename}' does not exists in profile group '{$_profilegroup}'"); SetMessage('warning', "Fall back to profile live"); $_profilegroup = '.'; $_profilename = 'live'; } } else { SetMessage('error', "Can not parse profileswitch"); SetMessage('warning', "Fall back to profile live"); $_profilegroup = '.'; $_profilename = 'live'; } $profile_changed = 1; $_SESSION['profile'] = $_profilename; $_SESSION['profilegroup'] = $_profilegroup; $_SESSION['profileswitch'] = $_profileswitch; } } $profileinfo = ReadProfile($_SESSION['profileswitch']); if ($profileinfo == FALSE) { SetMessage('warning', "Fall back to profile live"); unset($_SESSION['ProfileList']); $profiles = GetProfiles(); $_SESSION['profileswitch'] = './live'; $_SESSION['profile'] = 'live'; $_SESSION['profilegroup'] = '.'; $profileinfo = ReadProfile('./live'); if ($profileinfo == FALSE) { // double failure SetMessage('error', "Can't read profile 'live'"); $_SESSION['refresh'] = 0; return array(FALSE, 0, 0); } } if ($profileinfo['status'] == 'new') { $_SESSION['tab'] = 4; $_SESSION['refresh'] = 0; $_SESSION['tablock'] = "A new profile needs to be completed first."; } else { unset($_SESSION['tablock']); } $_SESSION['profileinfo'] = $profileinfo; // no refresh for history profiles if (($profileinfo['type'] & 3) == 1) { $_SESSION['refresh'] = 0; } return array(TRUE, $tab_changed, $profile_changed); }
function DisplayAlerts() { global $num_ConditionList; ?> <script language="Javascript" src="js/alerting.js" type="text/javascript"> </script> <?php ReportLog("Alert action eval: " . $_SESSION['action']); $setup = 0; switch ($_SESSION['action']) { case 'list': $alertstatus = $_SESSION['alertstatus']; DisplayAlertTable($alertstatus); break; case 'new': $alertinfo = $_SESSION['alertinfo']; $setup = 1; DisplayAlert(1, $alertinfo); break; case 'details': $setup = 1; $alertinfo = $_SESSION['alertinfo']; DisplayAlert(0, $alertinfo); break; default: print "<h3>ERROR action: " . $_SESSION['action'] . "</h3>"; break; } unset($_SESSION['action']); if ($setup) { ?> <script language="Javascript" type="text/javascript"> window.onload=function() { for (i=0; i< <?php echo $num_ConditionList; ?> ; i++ ) SetConditionRow(i); SetupCondition(<?php echo "{$num_ConditionList}, " . $alertinfo['type']; ?> ); } </script> <?php } }