Location: PHPKode > projects > SnortCenter 2.x > snortcenter-release/backup.php
    <?php
    /*
    ** SnortCenter Copyright (C) 2001,2002,2003 Stefan Dens
    **
    ** Author: Stefan Dens <hide@address.com>
    **
    ** This program is free software; you can redistribute it and/or modify
    ** it under the terms of the GNU General Public License as published by
    ** the Free Software Foundation; either version 2 of the License, or
    ** (at your option) any later version.
    **
    ** This program is distributed in the hope that it will be useful,
        ** but WITHOUT ANY WARRANTY; without even the implied warranty of
    ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    ** GNU General Public License for more details.
    **
    ** You should have received a copy of the GNU General Public License
    ** along with this program; if not, write to the Free Software
    ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
    */
?>
<?php
include("config.php");
$db = NewACIDDBConnection($DBlib_path, $DBtype);
$db->acidConnect($DB_dbname, $DB_host, $DB_port, $DB_user, $DB_password);

    if (strpos($_SERVER["SERVER_SOFTWARE"], "Win")  !== false) {
        $senstmpfnam = tempnam("c:/temp", 'backup');
        $fp = fopen ($senstmpfnam, "w");
    } else {
        $senstmpfnam = tempnam("/tmp", 'backup');
        $fp = fopen ($senstmpfnam, "w");
    }
$result_sensor = $db->acidExecute("SELECT id from sensor where sensor_name != 'default'");
while ($myrow_sensor = $result_sensor->acidFetchRow()) {
$sensor_id = $myrow_sensor[0];
    backup_sensor($sensor_id, $fp, $db);
}

function get_active ($element_type, $sensor_id, $db) {
    $result_id = $db->acidExecute("SELECT $element_type from sensor where id = '$sensor_id'");
    $myrow_id = $result_id->acidFetchRow();
    $rule_act = explode(";", $myrow_id[0]);
    $result_id->acidFreeRows();
    return $rule_act;
}
function array_export($a)
{
    $result = "";
   switch (gettype($a))
    {
             case "array":
	     reset($a);
	     $result = "array(";
	     while (list($k, $v) = each($a))
	     $result .= "$k => ".array_export($v).", ";
	     $result .= ")";
	     break;
	     
	     case "string":
	     $result = "'$a'";
	     break;
	     
	     case "boolean":
	     $result = ($a) ? "true" : "false";
	     break;
	     
	     default:
	     $result = $a;
	     break;
    }
     return $result;
}

function backup_sensor ($sensor_id, $fp, $db) {
    $rules = get_active('rules', $sensor_id, $db);
    $vars = get_active('vars', $sensor_id, $db);
    $spp = get_active('preprocessor', $sensor_id, $db);
    $spo = get_active('output', $sensor_id, $db);
    $ruletype = get_active('ruletype', $sensor_id, $db);
    $config = get_active('config', $sensor_id, $db);
    fputs($fp, '<?php ');
    $result = $db->acidExecute("SELECT * FROM sensor where id='$sensor_id'");
    $myrow = $result->acidFetchRow();
    fputs($fp, '$sensor['.$sensor_id.'][]= ');
    fputs($fp, array_export($myrow).'; ');
    $result->acidFreeRows();
    $result = $db->acidExecute("SELECT * FROM rules where category='local.rules'");
	while ($myrow = $result->acidFetchRow()) {
	    fputs($fp, '$localrules['.$sensor_id.'][] = ');
	    fputs($fp, array_export($myrow));
	    fputs($fp, '; ');
	}
    $result = $db->acidExecute("SELECT sid, action, src_ip, src_port, dst_ip, dst_port  FROM rulechange where sensor_id='$sensor_id'");
	while ($myrow = $result->acidFetchRow()) {
	    fputs($fp, '$rulechange['.$sensor_id.'][] = ');
	    fputs($fp, array_export($myrow));
	    fputs($fp, '; ');
	}
// get activated rule sid
    fputs($fp, '$rules['.$sensor_id.'] = ');
    $rule_print = array_export($rules);
    fputs($fp, '; ');
    fputs($fp, str_replace('|', '', $rule_print));
// vars    
    foreach($vars as $var_id) {
	$var_id = trim($var_id, '|');
	$result = $db->acidExecute("SELECT * from vars where id = '$var_id'");
	$myrow = $result->acidFetchRow();
	if ($myrow) {
	fputs($fp, '$vars['.$sensor_id.'][] = ');
	fputs($fp, array_export($myrow));
	fputs($fp, '; ');
	}
    }
// spp    
    foreach($spp as $spp_id) {
	$spp_id = trim($spp_id, '|');
	$result = $db->acidExecute("SELECT * from preprocessor where id = '$spp_id'");
	$myrow = $result->acidFetchRow();
	if ($myrow) {
	fputs($fp, '$spp['.$sensor_id.'][] = ');
	fputs($fp, array_export($myrow));
	fputs($fp, '; ');
	}
    }
// spo    
    foreach($spo as $spo_id) {
	$spo_id = trim($spo_id, '|');
	$result = $db->acidExecute("SELECT * from output where id = '$spo_id'");
	$myrow = $result->acidFetchRow();
	if ($myrow) {
	fputs($fp, '$spo['.$sensor_id.'][] = ');
	fputs($fp, array_export($myrow));
	fputs($fp, '; ');
	}
    }
// ruletypes    
    foreach($ruletype as $ruletype_id) {
	$ruletype_id = trim($ruletype_id, '|');
	$result = $db->acidExecute("SELECT * from ruletype where id = '$ruletype_id'");
	$myrow = $result->acidFetchRow();
	if ($myrow) {
	fputs($fp, '$ruletype['.$sensor_id.'][] = ');
	fputs($fp, array_export($myrow));
	fputs($fp, '; ');
	}
    }
// config    
    foreach($config as $config_id) {
	$config_id = trim($config_id, '|');
	$result = $db->acidExecute("SELECT * from config where id = '$config_id'");
	$myrow = $result->acidFetchRow();
	if ($myrow) {
	fputs($fp, '$config['.$sensor_id.'][] = ');
	fputs($fp, array_export($myrow));
	fputs($fp, '; ');
	}
    }
    fputs($fp, '?>');
} 
?>
Return current item: SnortCenter 2.x