Example #1
0
function restore_id($storeid)
{
    $unix = new unix();
    $pidfile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".{$storeid}.pid";
    $pid = @file_get_contents("{$pidfile}");
    if ($unix->process_exists($pid, basename(__FILE__))) {
        events("Process {$pid} already exists...aborting");
        die;
    }
    @file_put_contents($pidfile, getmypid());
    include_once dirname(__FILE__) . "/ressources/class.mysql.syslogs.inc";
    $sock = new sockets();
    $SargOutputDir = $sock->GET_INFO("SargOutputDir");
    if ($SargOutputDir == null) {
        $SargOutputDir = "/var/www/html/squid-reports";
    }
    $sarg_bin = $unix->find_program("sarg");
    if (!is_file($sarg_bin)) {
        sarg_admin_events("Fatal, unable to locate sarg binary, aborting...", __FUNCTION__, __FILE__, __LINE__, "sarg");
        return;
    }
    $bzip2 = $unix->find_program("bzip2");
    $gunzip = $unix->find_program("gunzip");
    $TempDir = "/home/artica-extract-temp";
    @mkdir($TempDir, 0777);
    @chown($TempDir, "mysql");
    @chdir($TempDir, "mysql");
    if (!is_file("/etc/squid3/sarg.conf")) {
        buildconf();
    }
    $q = new mysql_storelogs();
    $ligne = @mysql_fetch_array($q->QUERY_SQL("SELECT filename FROM files_info WHERE storeid='{$storeid}'"));
    $filename = $ligne["filename"];
    events("Extracting infos from {$filename}");
    $EnableSyslogDB = @file_get_contents("/etc/artica-postfix/settings/Daemons/EnableSyslogDB");
    if (!is_numeric($EnableSyslogDB)) {
        $EnableSyslogDB = 0;
    }
    if ($EnableSyslogDB == 0) {
        events("Extracting infos from {$filename} failed, SyslogDB is not enabled");
        return;
    }
    $q = new mysql_storelogs();
    $sql = "SELECT filecontent INTO DUMPFILE '{$TempDir}/{$filename}' FROM files_store WHERE ID = '{$storeid}'";
    $q->QUERY_SQL($sql);
    if (!$q->ok) {
        events("Failed!!! {$q->mysql_error}", 100);
        return;
    }
    $file_extension = file_extension($filename);
    progress("Extract {$filename} extension: {$file_extension}", 5);
    $newtFile = $filename . ".log";
    if ($file_extension == "bz2") {
        $cmdline = "{$bzip2} -d \"{$TempDir}/{$filename}\" -c >\"{$TempDir}/{$newtFile}.log\" 2>&1";
        exec($cmdline, $results);
    }
    if ($file_extension == "gz") {
        $cmdline = "{$gunzip} -d \"{$TempDir}/{$filename}\" -c >\"{$TempDir}/{$newtFile}.log\" 2>&1";
    }
    if ($cmdline != null) {
        exec($cmdline, $results);
        progress("Extract done " . @implode(" ", $results), 7);
    } else {
        if (!@copy("{$TempDir}/{$filename}", "{$TempDir}/{$newtFile}.log")) {
            progress("Failed!!! Copy error", 100);
            return;
        }
    }
    @unlink("{$TempDir}/{$filename}");
    if (!is_file("{$TempDir}/{$newtFile}.log")) {
        progress("Failed!!! {$TempDir}/{$newtFile}.log error no such file", 100);
        return;
    }
    $t = time();
    sargToFile("{$TempDir}/{$newtFile}.log");
    progress("{$filename} ({$storeid}) done " . $unix->distanceOfTimeInWords($t, time()));
    backup();
}
function search_database()
{
    $page = 1;
    $MyPage = CurrentPageName();
    $users = new usersMenus();
    $tpl = new templates();
    $sock = new sockets();
    $q = new mysql_storelogs();
    if (!$q->BD_CONNECT()) {
        senderror($q->mysql_error);
    }
    if ($q->start_error != null) {
        senderror($q->start_error);
    }
    $table = "files_info";
    $tableOrg = $table;
    $database = $q->database;
    $t = time();
    $delete_alert = $tpl->javascript_parse_text("{delete_this_item}");
    $MySQLType = $tpl->_ENGINE_parse_body($q->MYSQLTypeText);
    //if(!$q->TABLE_EXISTS($table,$database)==0){senderror("{table_does_not_exists}: <strong>$database/$table</strong> $MySQLType: `$q->SocketName`!");}
    if ($q->COUNT_ROWS($table, $database) == 0) {
        senderror("{$table}/{$database} is empty");
    }
    $searchstring = string_to_flexquery("search-database");
    $limit = "LIMIT 0,250";
    if (is_numeric($_GET["xtime"])) {
        $WHERE1 = " (DATE_FORMAT(filetime,'%Y-%m-%d')='" . date("Y-m-d") . "') AND";
        unset($_SESSION["QUERY_SYSLOG_DATE"]);
    }
    if ($_SESSION["QUERY_SYSLOG_LIMIT"] > 0) {
        $limit = "LIMIT 0,{$_SESSION["QUERY_SYSLOG_LIMIT"]}";
    }
    $filters = array();
    $filters[] = SearchToSql("DATE_FORMAT(filetime,'%Y-%m-%d')", $_SESSION["QUERY_SYSLOG_DATE"]);
    $filters[] = SearchToSql("filename", $_SESSION["QUERY_SYSLOG_FILE"]);
    $filters[] = SearchToSql("hostname", $_SESSION["QUERY_SYSLOG_HOST"]);
    $table = "(SELECT `filename`,`taskid`,`storeid`,`filesize`,`filetime`,`hostname` FROM {$table}\n\t\t\tWHERE {$WHERE1} (`filename` LIKE 'auth.log%') OR (filename LIKE 'squid-access%')) as t";
    $sql = "SELECT *  FROM {$table} WHERE 1 {$searchstring} " . @implode(" ", $filters) . " ORDER BY `filetime` DESC LIMIT 0,250";
    writelogs($sql, __FUNCTION__, __FILE__, __LINE__);
    $results = $q->QUERY_SQL($sql, $database);
    if (!$q->ok) {
        senderror("{$q->mysql_error}");
    }
    if (mysql_num_rows($results) == 0) {
        senderror("Query return empty array");
    }
    $boot = new boostrap_form();
    while ($ligne = mysql_fetch_assoc($results)) {
        $md5S = md5(serialize($ligne));
        $filename = $ligne["filename"];
        $hostname = $ligne["hostname"];
        $storeid = $ligne["storeid"];
        $taskid = $ligne["taskid"];
        $filesize = FormatBytes($ligne["filesize"] / 1024);
        $filetime = $ligne["filetime"];
        $delete = imgsimple("delete-32.png", null, "Delete{$t}('{$storeid}','{$md5S}')");
        $action = "&nbsp;";
        if (preg_match("#auth\\.log-.*?#", $ligne["filename"])) {
            $action = imgsimple("32-import.png", null, "Loadjs('squid.restoreSource.php?filename={$ligne["filename"]}&storeid={$storeid}')");
        }
        if (preg_match("#squid-access-(.*?)\\.log\\.#", $ligne["filename"])) {
            $action = imgsimple("32-import.png", null, "Loadjs('squid.restoreSource.php?filename={$ligne["filename"]}&storeid={$storeid}')");
        }
        $download = "<a href=\"{$MyPage}?download={$storeid}&filename={$ligne["filename"]}&storeid={$storeid}\"><img src='img/arrow-down-32.png'></a>";
        $js = "Loadjs('logrotate.php?log-js=yes&filename={$filename}&storeid={$storeid}&t=1368560783');";
        $trlink = $boot->trswitch($js);
        $tr[] = "\n\t\t<tr id='{$md5S}'>\n\t\t<td nowrap {$trlink}>{$filetime}</td>\n\t\t<td nowrap {$trlink}>{$filename}</td>\n\t\t<td nowrap {$trlink}>{$hostname}</td>\n\t\t<td nowrap {$trlink}>{$filesize}</td>\n\t\t<td width=1% align=center>{$download}</td>\n\t\t<td width=1% align=center>{$action}</td>\n\t\t<td width=1% align=center>{$delete}</td>\n\t\t</tr>\n\t\t";
    }
    echo $tpl->_ENGINE_parse_body("\n\n\t\t\t<table class='table table-bordered table-hover'>\n\n\t\t\t<thead>\n\t\t\t\t<tr>\n\t\t\t\t\t<th>{date}</th>\n\t\t\t\t\t<th>{filename}</th>\n\t\t\t\t\t<th>{hostname}</th>\n\t\t\t\t\t<th>{size}</th>\n\t\t\t\t\t<th>&nbsp;</th>\n\t\t\t\t\t<th>&nbsp;</th>\n\t\t\t\t\t<th>&nbsp;</th>\n\t\t\t\t</tr>\n\t\t\t</thead>\n\t\t\t <tbody>\n\t\t\t") . @implode("", $tr) . "</tbody></table>\n\t\t\t<script>\n\t\t\tvar memedb{$t}='';\n\t\t\tvar xDelete{$t}= function (obj) {\n\t\t\tvar results=obj.responseText;\n\t\t\tif(results.length>2){alert(results);return;}\n\t\t\t\$('#'+memedb{$t}).remove();\n}\n\n\t\t\tfunction Delete{$t}(ID,md){\n\t\t\tmemedb{$t}=md;\n\t\t\tif(confirm('{$delete_alert} '+ID+' ?')){\n\t\t\tvar XHR = new XHRConnection();\n\t\t\tXHR.appendData('syslog-delete',ID);\n\t\t\tXHR.sendAndLoad('{$MyPage}', 'POST', xDelete{$t});\n}\n}\n\t\t\t</script>";
}
Example #3
0
function storage_view_extract()
{
    @chmod("ressources/logs", 0777);
    $q = new mysql_syslog();
    $mydir = dirname(__FILE__);
    $newtFile = $_POST["extract-file"];
    $sock = new sockets();
    @unlink("{$mydir}/ressources/logs/{$newtFile}");
    $EnableSyslogDB = $sock->GET_INFO("EnableSyslogDB");
    if (!is_numeric($EnableSyslogDB)) {
        $EnableSyslogDB = 0;
    }
    if ($EnableSyslogDB == 0) {
        $ligne = mysql_fetch_array($q->QUERY_SQL("SELECT SavedInDisk,FileStorePath FROM store WHERE filename = '{$newtFile}'"));
        writelogs("SavedInDisk = {$ligne["SavedInDisk"]}", __FUNCTION__, __FILE__, __LINE__);
        if ($ligne["SavedInDisk"] == 1) {
            $array["FROM"] = $ligne["FileStorePath"];
            $array["TO"] = "{$mydir}/ressources/logs/{$newtFile}";
            $sock->getFrameWork("services.php?copyFiles=" . base64_encode(serialize($array)));
            return;
        }
        $sql = "SELECT filedata INTO DUMPFILE '{$mydir}/ressources/logs/{$newtFile}' FROM access_store WHERE ID = '{$newtFile}'";
        writelogs("{$sql}", __FUNCTION__, __FILE__, __LINE__);
        $q->QUERY_SQL($sql);
        if (!$q->ok) {
            writelogs("{$q->mysql_error}", __FUNCTION__, __FILE__, __LINE__);
            echo $q->mysql_error;
            return;
        }
    } else {
        $q = new mysql_storelogs();
        $sql = "SELECT filecontent INTO DUMPFILE '{$mydir}/ressources/logs/{$newtFile}' FROM files_store WHERE ID = '{$_POST["storeid"]}'";
        writelogs("{$sql}", __FUNCTION__, __FILE__, __LINE__);
        $q->QUERY_SQL($sql);
    }
    $ext = file_extension($newtFile);
    writelogs("{$mydir}/ressources/logs/{$newtFile} -> " . @filesize("{$mydir}/ressources/logs/{$newtFile}") . " bytes...", __FUNCTION__, __FILE__, __LINE__);
    $cmdline = "cp -f {$mydir}/ressources/logs/{$newtFile} {$mydir}/ressources/logs/{$newtFile}.log";
    if ($ext == "bz2") {
        $cmdline = "bzip2 -d \"{$mydir}/ressources/logs/{$newtFile}\" -c >\"{$mydir}/ressources/logs/{$newtFile}.log\" 2>&1";
        exec($cmdline, $results);
    }
    if ($ext == "gz") {
        $cmdline = "gunzip -d \"{$mydir}/ressources/logs/{$newtFile}\" -c >\"{$mydir}/ressources/logs/{$newtFile}.log\"";
    }
    if ($cmdline != null) {
        writelogs("{$cmdline}", __FUNCTION__, __FILE__, __LINE__);
        exec($cmdline, $results);
        while (list($key, $line) = each($results)) {
            writelogs("{$line}", __FUNCTION__, __FILE__, __LINE__);
        }
    }
    @unlink("{$mydir}/ressources/logs/{$newtFile}");
    writelogs(@filesize("{$mydir}/ressources/logs/{$newtFile}.log") . " bytes...", __FUNCTION__, __FILE__, __LINE__);
}
function restore($filename, $storeid)
{
    $filename = trim($filename);
    $unix = new unix();
    $pidfile = "/etc/artica-postfix/pids/" . basename(__FILE__) . ".{$filename}.pid";
    $pid = @file_get_contents("{$pidfile}");
    if ($unix->process_exists($pid, basename(__FILE__))) {
        die;
    }
    @file_put_contents($pidfile, getmypid());
    $EnableSyslogDB = @file_get_contents("/etc/artica-postfix/settings/Daemons/EnableSyslogDB");
    if (!is_numeric($EnableSyslogDB)) {
        $EnableSyslogDB = 0;
    }
    @mkdir("/var/log/artica-postfix/squid-brut", 0777, true);
    @mkdir("/var/log/artica-postfix/squid-reverse", 0777, true);
    $GLOBALS["filename"] = $filename;
    $sock = new sockets();
    $TempDir = "/home/artica-extract-temp";
    @mkdir($TempDir, 0777);
    @chown($TempDir, "mysql");
    @chdir($TempDir, "mysql");
    $BackupMaxDaysDir = $sock->GET_INFO("BackupMaxDaysDir");
    $bzip2 = $unix->find_program("bzip2");
    $gunzip = $unix->find_program("gunzip");
    progress("Extract {$filename} from MySQL database into {$TempDir}", 4);
    if ($EnableSyslogDB == 1) {
        $q = new mysql_storelogs();
        $sql = "SELECT filecontent INTO DUMPFILE '{$TempDir}/{$filename}' FROM files_store WHERE ID = '{$storeid}'";
        $q->QUERY_SQL($sql);
    } else {
        $q = new mysql_syslog();
        $sql = "SELECT filedata INTO DUMPFILE '{$TempDir}/{$filename}' FROM store WHERE filename = '{$filename}'";
        $q->QUERY_SQL($sql);
    }
    if (!$q->ok) {
        progress("Failed!!! {$q->mysql_error}", 100);
        return;
    }
    $file_extension = file_extension($filename);
    progress("Extract {$filename} extension: {$file_extension}", 5);
    $newtFile = $filename . ".log";
    if ($file_extension == "bz2") {
        $cmdline = "bzip2 -d \"{$TempDir}/{$filename}\" -c >\"{$TempDir}/{$newtFile}.log\" 2>&1";
        exec($cmdline, $results);
    }
    if ($file_extension == "gz") {
        $cmdline = "gunzip -d \"{$TempDir}/{$filename}\" -c >\"{$TempDir}/{$newtFile}.log\" 2>&1";
    }
    if ($cmdline != null) {
        exec($cmdline, $results);
        progress("Extract done " . @implode(" ", $results), 7);
    } else {
        if (!@copy("{$TempDir}/{$filename}", "{$TempDir}/{$newtFile}.log")) {
            progress("Failed!!! Copy error", 100);
            return;
        }
    }
    @unlink("{$TempDir}/{$filename}");
    if (!is_file("{$TempDir}/{$newtFile}.log")) {
        progress("Failed!!! {$TempDir}/{$newtFile}.log error no such file", 100);
        return;
    }
    $linesNumber = $unix->COUNT_LINES_OF_FILE("{$TempDir}/{$newtFile}.log");
    progress("Open {$TempDir}/{$newtFile}.log {$linesNumber}", 10);
    $handle = @fopen("{$TempDir}/{$newtFile}.log", "r");
    if (!$handle) {
        progress("Failed!!! {$TempDir}/{$newtFile}.log open failed", 100);
        return;
    }
    $c = 0;
    $d = 0;
    $TTEV = 0;
    while (!feof($handle)) {
        $c++;
        $buffer = trim(fgets($handle, 4096));
        if (!preg_match("#MAC:.*?\\[([0-9]+)\\/(.*?)\\/([0-9]+).*?:([0-9]+):([0-9]+):([0-9]+)\\s+(.*?)\\]\\s+\"#", $buffer, $re)) {
            continue;
        }
        $dteStr = "{$re[1]}/{$re[2]}/{$re[3]}:{$re[4]}:{$re[5]}:{$re[6]} {$re[7]}";
        $ttime = strtotime($dteStr);
        $newDate = date("Y-m-d H", $ttime) . "h";
        $datelog = date("Y-m-d-h", $ttime);
        $MD5Buffer = md5($buffer);
        $TTEV++;
        @mkdir("/var/log/artica-postfix/squid-brut/{$datelog}", 0777, true);
        @file_put_contents("/var/log/artica-postfix/squid-brut/{$datelog}/{$MD5Buffer}", $buffer);
        if ($c > 10) {
            $d = $d + $c;
            $pp = $d / $linesNumber;
            $pp = $pp * 100;
            $pp = round($pp, 1);
            if ($pp > 10) {
                if ($pp > 100) {
                    $pp = 99;
                }
                progress("Processing {$d}/{$linesNumber} - {$newDate} ", $pp);
                $c = 0;
            }
        }
    }
    progress("Success, {$TTEV} events sent to MySQL injector ", 100);
    @unlink("{$TempDir}/{$newtFile}.log");
}
function download()
{
    $filename = $_GET["filename"];
    $storeid = $_GET["storeid"];
    $sock = new sockets();
    $q = new mysql_storelogs();
    $WorkDir = dirname(__FILE__) . "/ressources/logs/web/export";
    @mkdir($WorkDir, 0777, true);
    @chmod($WorkDir, 0777);
    $destination = "{$WorkDir}/{$filename}";
    if (is_file($destination)) {
        $sock->getFrameWork("services.php?chowndir={$destination}");
        @unlink($destination);
    }
    $ligne = mysql_fetch_array($q->QUERY_SQL("SELECT LENGTH(filecontent) as lent FROM files_store WHERE ID = '{$storeid}'"));
    writelogs("{$storeid}: {$ligne["lent"]} bytes {$q->mysql_error}", __FUNCTION__, __FILE__, __LINE__);
    $sql = "SELECT filecontent INTO DUMPFILE '{$destination}' FROM files_store WHERE ID = '{$storeid}'";
    $q->QUERY_SQL($sql);
    if (!is_file($destination)) {
        writelogs("{$destination}: No such file", __FUNCTION__, __FILE__, __LINE__);
        return;
    }
    if (!$q->ok) {
        writelogs("Fatal: {$q->mysql_error}", __FUNCTION__, __FILE__, __LINE__);
    }
    $sock->getFrameWork("services.php?chowndir={$destination}");
    $content_type = base64_decode($sock->getFrameWork("cmd.php?mime-type=" . base64_encode($destination)));
    writelogs("{$destination}: {$content_type}", __FUNCTION__, __FILE__, __LINE__);
    header('Content-type: ' . $content_type);
    header('Content-Transfer-Encoding: binary');
    header("Content-Disposition: attachment; filename=\"{$filename}\"");
    header("Pragma: public");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
    // Date dans le passé
    $fsize = filesize($destination);
    header("Content-Length: " . $fsize);
    ob_clean();
    flush();
    readfile($destination);
}
function storage_view_extract()
{
    ini_set('html_errors', 0);
    ini_set('display_errors', 1);
    ini_set('error_reporting', E_ALL);
    ini_set('error_prepend_string', '');
    ini_set('error_append_string', '');
    @chmod("ressources/logs", 0777);
    $q = new mysql_storelogs();
    $mydir = dirname(__FILE__);
    $newtFile = $_POST["extract-file"];
    $sock = new sockets();
    @unlink("{$mydir}/ressources/logs/{$newtFile}");
    $q = new mysql_storelogs();
    $sql = "SELECT filecontent INTO DUMPFILE '{$mydir}/ressources/logs/{$newtFile}' FROM access_store WHERE ID = '{$_POST["storeid"]}'";
    writelogs("{$sql}", __FUNCTION__, __FILE__, __LINE__);
    $q->QUERY_SQL($sql);
    $ext = file_extension($newtFile);
    writelogs("{$mydir}/ressources/logs/{$newtFile} -> " . @filesize("{$mydir}/ressources/logs/{$newtFile}") . " bytes...", __FUNCTION__, __FILE__, __LINE__);
    $cmdline = "cp -f {$mydir}/ressources/logs/{$newtFile} {$mydir}/ressources/logs/{$newtFile}.log";
    if ($ext == "bz2") {
        $cmdline = "bzip2 -d \"{$mydir}/ressources/logs/{$newtFile}\" -c >\"{$mydir}/ressources/logs/{$newtFile}.log\" 2>&1";
        exec($cmdline, $results);
    }
    if ($ext == "gz") {
        $cmdline = "gunzip -d \"{$mydir}/ressources/logs/{$newtFile}\" -c >\"{$mydir}/ressources/logs/{$newtFile}.log\"";
    }
    if ($cmdline != null) {
        writelogs("{$cmdline}", __FUNCTION__, __FILE__, __LINE__);
        exec($cmdline, $results);
        while (list($key, $line) = each($results)) {
            writelogs("{$line}", __FUNCTION__, __FILE__, __LINE__);
        }
    }
    @unlink("{$mydir}/ressources/logs/{$newtFile}");
    writelogs(@filesize("{$mydir}/ressources/logs/{$newtFile}.log") . " bytes...", __FUNCTION__, __FILE__, __LINE__);
}
Example #7
0
function search_store()
{
    $tpl = new templates();
    $MyPage = CurrentPageName();
    $q = new mysql_storelogs();
    $search = '%';
    $table = "accesslogs";
    $page = 1;
    $ORDER = "ORDER BY ID filetime";
    $sock = new sockets();
    $t = $_GET["t"];
    if (!$q->TABLE_EXISTS($table)) {
        $q->CheckTables();
    }
    $database = "syslogs";
    $total = 0;
    if ($q->COUNT_ROWS($table, $database) == 0) {
        json_error_show("No data...");
    }
    if (isset($_POST["sortname"])) {
        if ($_POST["sortname"] != null) {
            $ORDER = "ORDER BY {$_POST["sortname"]} {$_POST["sortorder"]}";
        }
    }
    if (isset($_POST['page'])) {
        $page = $_POST['page'];
    }
    $searchstring = string_to_flexquery();
    if ($searchstring != null) {
        $sql = "SELECT COUNT(*) as TCOUNT FROM {$table} WHERE 1 {$searchstring}";
        $ligne = mysql_fetch_array($q->QUERY_SQL($sql, $database));
        $total = $ligne["TCOUNT"];
    } else {
        $sql = "SELECT COUNT(*) as TCOUNT FROM {$table}";
        $ligne = mysql_fetch_array($q->QUERY_SQL($sql, $database));
        $total = $ligne["TCOUNT"];
    }
    if (isset($_POST['rp'])) {
        $rp = $_POST['rp'];
    }
    $pageStart = ($page - 1) * $rp;
    $limitSql = "LIMIT {$pageStart}, {$rp}";
    $sql = "SELECT * FROM {$table} WHERE 1 {$searchstring} {$ORDER} {$limitSql}";
    $results = $q->QUERY_SQL($sql);
    writelogs($sql, __FUNCTION__, __FILE__, __LINE__);
    $data = array();
    $data['page'] = $page;
    $data['total'] = $total;
    $data['rows'] = array();
    if (!$q->ok) {
        json_error_show($q->mysql_error, 1);
    }
    while ($ligne = mysql_fetch_assoc($results)) {
        $color = "black";
        $events = "&nbsp;";
        $md5 = md5("RotateTask{$ligne['filename']}");
        $span = "<span style='font-size:16px'>";
        $delete = imgtootltip("delete-24.png", "{delete} {$ligne['ID']}", "StorageTaskDelete('{$ligne['filename']}','{$md5}')");
        $jsEdit = "Loadjs('logrotate.php?Rotate-js=yes&ID={$ligne['taskid']}&t={$t}');";
        $jstask = "<a href=\"javascript:blur();\" OnClick=\"javascript:{$jsEdit}\"\n\t\tstyle='font-size:16px;font-weight:bold;color:{$color};text-decoration:underline'>";
        $jslloop = "Loadjs('logrotate.php?log-js=yes&filename={$ligne['filename']}&t={$t}');";
        $view = "<a href=\"javascript:blur();\" OnClick=\"javascript:{$jslloop}\"\n\t\tstyle='font-size:16px;font-weight:bold;color:{$color};text-decoration:underline'>";
        $ligne["filesize"] = FormatBytes($ligne["filesize"] / 1024);
        if ($ligne['taskid'] == 0) {
            $jstask = null;
        }
        $action = null;
        if (preg_match("#auth\\.log-.*?#", $ligne["filename"])) {
            $action = imgsimple("service-restart-32.png", null, "Loadjs('squid.restoreSource.php?filename={$ligne["filename"]}')");
        }
        if (preg_match("#^squid-access.*?#", $ligne["filename"])) {
            $action = imgsimple("service-restart-32.png", null, "Loadjs('squid.restoreSource.php?filename={$ligne["filename"]}')");
        }
        $xtime = strtotime("{$ligne['filetime']}");
        $dateTex = date("Y {l} {F} d", $xtime);
        if ($tpl->language == "fr") {
            $dateTex = date("{l} d {F} Y", $xtime);
        }
        $dateTex = $tpl->_ENGINE_parse_body("{$dateTex}");
        //rowSquidTask
        $data['rows'][] = array('id' => $md5, 'cell' => array("{$span}{$ligne['filetime']}</a></span><div style='font-size:11px'><i>{$dateTex}</i></div>", "{$span}{$ligne["filename"]}</a><br><i>{$ligne['hostname']}</a></span>", "{$span}{$ligne["filesize"]}</a></span>", "{$span}{$ligne["taskid"]}</a></span>", $action, ""));
    }
    echo json_encode($data);
}
Example #8
0
function CleanMysqlDatabase($PURGE_ALL = false)
{
    $filter = null;
    $users = new usersMenus();
    $unix = new unix();
    $pidfile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".pid";
    $timefile = "/etc/artica-postfix/pids/logrotate." . __FUNCTION__ . ".time";
    $pid = @file_get_contents("{$pidfile}");
    if ($unix->process_exists($pid, basename(__FILE__))) {
        system_admin_events("Already executed PID {$pid}", __FUNCTION__, __FILE__, __LINE__, "logrotate");
        die;
    }
    @file_put_contents($pidfile, getmypid());
    $time = $unix->file_time_min($timefile);
    if (!$PURGE_ALL) {
        if (!$GLOBALS["FORCE"]) {
            if ($time < 15) {
                events("No less than 15mn or delete {$timefile} file to force...");
                system_admin_events("No less than 15mn or delete {$timefile} file", __FUNCTION__, __FILE__, __LINE__, "logrotate");
                return;
            }
        }
    }
    @unlink($timefile);
    @file_put_contents($timefile, time());
    $sock = new sockets();
    $MySQLSyslogType = $sock->GET_INFO("MySQLSyslogType");
    $EnableSyslogDB = $sock->GET_INFO("EnableSyslogDB");
    if (!is_numeric($EnableSyslogDB)) {
        $EnableSyslogDB = 0;
    }
    if (!is_numeric($MySQLSyslogType)) {
        $MySQLSyslogType = 1;
    }
    if ($MySQLSyslogType == 0) {
        $MySQLSyslogType = 4;
    }
    $LogRotatePath = $sock->GET_INFO("LogRotatePath");
    if ($LogRotatePath == null) {
        $LogRotatePath = "/home/logrotate";
    }
    $TuningParameters = unserialize(base64_decode($sock->GET_INFO("MySQLSyslogParams")));
    if ($EnableSyslogDB == 1) {
        if ($MySQLSyslogType == 2) {
            events("Is a client of remote MySQL server , aborting");
            if ($GLOBALS["VERBOSE"]) {
                echo "Is a client of remote MySQL server , aborting\n";
            }
            return;
        }
    }
    $LogRotateCompress = 1;
    $LogRotatePath = $sock->GET_INFO("LogRotatePath");
    $SystemLogsPath = $sock->GET_INFO("SystemLogsPath");
    $BackupMaxDays = $sock->GET_INFO("BackupMaxDays");
    $BackupMaxDaysDir = $sock->GET_INFO("BackupMaxDaysDir");
    $BackupSquidLogsUseNas = $sock->GET_INFO("BackupSquidLogsUseNas");
    if ($SystemLogsPath == null) {
        $SystemLogsPath = "/var/log";
    }
    $MySQLSyslogType = $sock->GET_INFO("MySQLSyslogType");
    $EnableSyslogDB = $sock->GET_INFO("EnableSyslogDB");
    if (!is_numeric($EnableSyslogDB)) {
        $EnableSyslogDB = 0;
    }
    if (!is_numeric($MySQLSyslogType)) {
        $MySQLSyslogType = 1;
    }
    if (!is_numeric($BackupSquidLogsUseNas)) {
        $BackupSquidLogsUseNas = 0;
    }
    if ($EnableSyslogDB == 1) {
        if ($MySQLSyslogType != 1) {
            return;
        }
    }
    if (!is_numeric($BackupMaxDays)) {
        $BackupMaxDays = 30;
    }
    if ($LogRotatePath == null) {
        $LogRotatePath = "/home/logrotate";
    }
    if ($BackupMaxDaysDir == null) {
        $BackupMaxDaysDir = "/home/logrotate_backup";
    }
    $mount = new mount("/var/log/artica-postfix/logrotate.debug");
    if ($BackupSquidLogsUseNas == 1) {
        $BackupSquidLogsNASIpaddr = $sock->GET_INFO("BackupSquidLogsNASIpaddr");
        $BackupSquidLogsNASFolder = $sock->GET_INFO("BackupSquidLogsNASFolder");
        $BackupSquidLogsNASUser = $sock->GET_INFO("BackupSquidLogsNASUser");
        $BackupSquidLogsNASPassword = $sock->GET_INFO("BackupSquidLogsNASPassword");
        $BackupSquidLogsNASRetry = $sock->GET_INFO("BackupSquidLogsNASRetry");
        if (!is_numeric($BackupSquidLogsNASRetry)) {
            $BackupSquidLogsNASRetry = 0;
        }
        $mountPoint = "/mnt/BackupSquidLogsUseNas";
        if (!$mount->smb_mount($mountPoint, $BackupSquidLogsNASIpaddr, $BackupSquidLogsNASUser, $BackupSquidLogsNASPassword, $BackupSquidLogsNASFolder)) {
            events("Unable to connect to NAS storage system (1): {$BackupSquidLogsNASUser}@{$BackupSquidLogsNASIpaddr}");
            if ($BackupSquidLogsNASRetry == 0) {
                return;
            }
            sleep(3);
            $mount = new mount("/var/log/artica-postfix/logrotate.debug");
            if (!$mount->smb_mount($mountPoint, $BackupSquidLogsNASIpaddr, $BackupSquidLogsNASUser, $BackupSquidLogsNASPassword, $BackupSquidLogsNASFolder)) {
                events("Unable to connect to NAS storage system (2): {$BackupSquidLogsNASUser}@{$BackupSquidLogsNASIpaddr}");
                return;
            }
        }
        $BackupMaxDaysDir = "{$mountPoint}/artica-backup-syslog/{$users->hostname}";
    }
    @mkdir("{$BackupMaxDaysDir}", 0755, true);
    if (!is_dir($BackupMaxDaysDir)) {
        if ($GLOBALS["VERBOSE"]) {
            echo "Fatal {$BackupMaxDaysDir} permission denied\n";
        }
        events("Fatal {$BackupMaxDaysDir} permission denied");
        squid_admin_notifs("SYSLOG: FATAL {$BackupMaxDaysDir} permission denied", __FUNCTION__, __FILE__, __LINE__);
        system_admin_events($q->mysql_error, __FUNCTION__, __FILE__, __LINE__, "logrotate");
        if ($BackupSquidLogsUseNas == 1) {
            $mount->umount($mountPoint);
        }
        return false;
    }
    $t = time();
    @file_put_contents("{$BackupMaxDaysDir}/{$t}", time());
    if (!is_file("{$BackupMaxDaysDir}/{$t}")) {
        events("Fatal {$BackupMaxDaysDir} permission denied");
        if ($GLOBALS["VERBOSE"]) {
            echo "Fatal {$BackupMaxDaysDir} permission denied\n";
        }
        squid_admin_notifs("SYSLOG: FATAL {$BackupMaxDaysDir} permission denied", __FUNCTION__, __FILE__, __LINE__);
        system_admin_events($q->mysql_error, __FUNCTION__, __FILE__, __LINE__, "logrotate");
        if ($BackupSquidLogsUseNas == 1) {
            $mount->umount($mountPoint);
        }
        return false;
    }
    @unlink("{$BackupMaxDaysDir}/{$t}");
    if ($BackupSquidLogsUseNas == 1) {
        if (is_dir("/home/logrotate_backup")) {
            $files = $unix->DirFiles("/home/logrotate_backup");
            events("Scanning the old storage systems.. " . count($files) . " file(s)");
            while (list($basename, $none) = each($files)) {
                $filepath = "/home/logrotate_backup/{$basename}";
                if ($GLOBALS["VERBOSE"]) {
                    echo "Checking \"{$filepath}\"\n";
                }
                $size = @filesize($filepath);
                if ($size < 20) {
                    events("Removing {$filepath}");
                    @unlink($filepath);
                    continue;
                }
                if (!@copy($filepath, "{$BackupMaxDaysDir}/{$basename}")) {
                    events("copy Failed {$filepath} to \"{$BackupMaxDaysDir}/{$basename}\" permission denied...");
                    continue;
                }
                events("Move {$filepath} to {$BackupSquidLogsNASIpaddr} success...");
                @unlink($filepath);
            }
        }
    }
    if ($PURGE_ALL == false) {
        $filter = "WHERE filetime<DATE_SUB(NOW(),INTERVAL {$BackupMaxDays} DAY)";
    }
    if ($EnableSyslogDB == 1) {
        $q = new mysql_storelogs();
        $sql = "SELECT `filename`,`hostname`,`storeid` FROM `files_info` {$filter}";
        if ($GLOBALS["VERBOSE"]) {
            echo "{$sql}\n";
        }
        $results = $q->QUERY_SQL($sql);
        if (!$q->ok) {
            system_admin_events($q->mysql_error, __FUNCTION__, __FILE__, __LINE__);
            return;
        }
        $Count = mysql_num_rows($results);
        $c = 0;
        while ($ligne = mysql_fetch_assoc($results)) {
            $c++;
            if ($GLOBALS["VERBOSE"]) {
                echo "{$c}/{$Count} ******** {$ligne["filename"]} {$ligne["storeid"]} *********\n";
            }
            if (!$q->ExtractFile("{$BackupMaxDaysDir}/{$ligne["hostname"]}.{$ligne["filename"]}", $ligne["storeid"])) {
                if ($GLOBALS["VERBOSE"]) {
                    echo "{$c}/{$Count} ******** {$ligne["filename"]} ExtractFile() = FALSE !!! *********\n";
                }
                continue;
            }
            if ($GLOBALS["VERBOSE"]) {
                echo "{$c}/{$Count} DelteItem({$ligne["storeid"]}) *********\n";
            }
            $q->DelteItem($ligne["storeid"]);
            if ($GLOBALS["VERBOSE"]) {
                echo "********* EVENTS NOW --->\n";
            }
            $q->events("{$ligne["filename"]} saved into {$BackupMaxDaysDir}");
            if ($GLOBALS["VERBOSE"]) {
                echo "\n\n###### {$c}/{$Count} Continue to Next ##########\n";
            }
        }
        $sql = "SELECT `filename`,`hostname`,`storeid` FROM `accesslogs` {$filter}";
        if ($GLOBALS["VERBOSE"]) {
            echo "{$sql}\n";
        }
        $results = $q->QUERY_SQL($sql);
        if (!$q->ok) {
            system_admin_events($q->mysql_error, __FUNCTION__, __FILE__, __LINE__);
            return;
        }
        $Count = mysql_num_rows($results);
        $c = 0;
        while ($ligne = mysql_fetch_assoc($results)) {
            $c++;
            if (!$q->ExtractAccessFile("{$BackupMaxDaysDir}/{$ligne["hostname"]}.{$ligne["filename"]}", $ligne["storeid"])) {
                continue;
            }
            $q->DelteAccessItem($ligne["storeid"]);
            $q->events("{$ligne["filename"]} saved into {$BackupMaxDaysDir}");
        }
        if ($BackupSquidLogsUseNas == 1) {
            $mount->umount($mountPoint);
        }
        return;
    }
    $q = new mysql_syslog();
    $sql = "SELECT `filename`,`taskid`,`filesize`,`filetime` FROM `store` {$filter}";
    $results = $q->QUERY_SQL($sql);
    if ($GLOBALS["VERBOSE"]) {
        echo "{$sql} ({$q->mysql_error}) " . mysql_num_rows($results) . " file(s)\n";
    }
    if (!$q->ok) {
        system_admin_events($q->mysql_error, __FUNCTION__, __FILE__, __LINE__);
        return;
    }
    while ($ligne = mysql_fetch_assoc($results)) {
        if ($GLOBALS["VERBOSE"]) {
            echo "Processing {$ligne["filename"]}\n";
        }
        if (!ExtractFileFromDatabase($ligne["filename"], $BackupMaxDaysDir)) {
            events("Unable to extract {$ligne["filename"]} to {$BackupMaxDaysDir}");
            squid_admin_notifs("SYSLOG: Unable to extract {$ligne["filename"]} to {$BackupMaxDaysDir}", __FUNCTION__, __FILE__, __LINE__);
            if ($BackupSquidLogsUseNas == 1) {
                $mount->umount($mountPoint);
            }
            return false;
        } else {
            events("Success extracting {$ligne["filename"]} to {$BackupMaxDaysDir}");
        }
    }
    if ($BackupSquidLogsUseNas == 1) {
        $mount->umount($mountPoint);
    }
}