예제 #1
0
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 squid_status_stats()
{
    $off = "<script>UnlockPage();</script>";
    if (!$GLOBALS["AS_ROOT"]) {
        $cachefile = "/usr/share/artica-postfix/ressources/logs/web/traffic.statistics.html";
        if (is_file($cachefile)) {
            $tpl = new templates();
            $cacheContent = @file_get_contents($cachefile);
            if (strlen($cacheContent) > 20) {
                echo $tpl->_ENGINE_parse_body(@file_get_contents($cachefile)) . $off;
                return;
            }
        }
    }
    if (CACHE_SESSION_GET(__FUNCTION__, __FILE__)) {
        return;
    }
    if ($GLOBALS["VERBOSE"]) {
        echo __LINE__ . " Loading classes<br>\n";
    }
    $sock = new sockets();
    $users = new usersMenus();
    $EnableRemoteStatisticsAppliance = $sock->GET_INFO("EnableRemoteStatisticsAppliance");
    $SquidActHasReverse = $sock->GET_INFO("SquidActHasReverse");
    if (!is_numeric($SquidActHasReverse)) {
        $SquidActHasReverse = 0;
    }
    if ($EnableRemoteStatisticsAppliance == 1) {
        return;
    }
    $DisableArticaProxyStatistics = $sock->GET_INFO("DisableArticaProxyStatistics");
    if (!is_numeric($DisableArticaProxyStatistics)) {
        $DisableArticaProxyStatistics = 0;
    }
    $MalwarePatrolDatabasesCount = $sock->getFrameWork("cmd.php?MalwarePatrolDatabasesCount=yes");
    $mouse = "OnMouseOver=\";this.style.cursor='pointer';\" OnMouseOut=\";this.style.cursor='default';\"";
    $EnableMacAddressFilter = $sock->GET_INFO("EnableMacAddressFilter");
    if (!is_numeric($EnableMacAddressFilter)) {
        $EnableMacAddressFilter = 1;
    }
    if ($GLOBALS["VERBOSE"]) {
        echo __LINE__ . " Loading mysql_storelogs()<br>\n";
    }
    $syslogs = new mysql_storelogs();
    if ($GLOBALS["VERBOSE"]) {
        echo __LINE__ . " Count accesslogs<br>\n";
    }
    $SyslogsFiles = $syslogs->COUNT_ROWS("accesslogs");
    $TR_ACCESSLOG = "\n\t<tr>\n\t<td width=1%><img src='img/arrow-right-16.png'></td>\n\t<td valign='top' {$mouse} style='font-size:12px;text-decoration:underline' \n\t\tOnClick=\"javascript:Loadjs('squid.accesses.rotate.php')\"><b><span style='font-size:12px'>{$SyslogsFiles}</span></b><span style='font-size:12px'> {access_logs}</td>\n\t</tr>";
    $page = CurrentPageName();
    $tpl = new templates();
    $q = new mysql_squid_builder();
    if ($users->PROXYTINY_APPLIANCE) {
        $DisableArticaProxyStatistics = 1;
    }
    if ($DisableArticaProxyStatistics == 0) {
        $websitesnums = $q->COUNT_ROWS("visited_sites");
        $websitesnums = numberFormat($websitesnums, 0, "", " ");
        $sql = "DELETE FROM categorize WHERE LENGTH(pattern)=0";
        $q->QUERY_SQL($sql);
        $export = $q->COUNT_ROWS("categorize");
        $export = numberFormat($export, 0, "", " ");
    }
    $catz = new mysql_catz();
    $categories = $catz->COUNT_CATEGORIES();
    $categories = numberFormat($categories, 0, "", " ");
    if ($GLOBALS["VERBOSE"]) {
        echo __LINE__ . " categories= {$categories}<br>\n";
    }
    $YourItems = $q->COUNT_CATEGORIES();
    $YourItems = numberFormat($YourItems, 0, "", " ");
    if ($GLOBALS["VERBOSE"]) {
        echo __LINE__ . " LIST_TABLES_CATEGORIES()<br>\n";
    }
    $tablescat = $q->LIST_TABLES_CATEGORIES();
    if ($GLOBALS["VERBOSE"]) {
        echo __LINE__ . " tablescat={$tablescat}<br>\n";
    }
    $tablescatNUM = numberFormat(count($tablescat), 0, "", " ");
    if ($DisableArticaProxyStatistics == 0) {
        if ($GLOBALS["VERBOSE"]) {
            echo __LINE__ . " EVENTS_SUM()<br>\n";
        }
        $requests = $q->EVENTS_SUM();
        $requests = numberFormat($requests, 0, "", " ");
        if ($GLOBALS["VERBOSE"]) {
            echo __LINE__ . " requests = {$requests}<br>\n";
        }
    }
    if ($GLOBALS["VERBOSE"]) {
        echo __LINE__ . " no_license -> translate<br>\n";
    }
    $nolicense = $tpl->_ENGINE_parse_body("{no_license}");
    $PhishingURIS = $q->COUNT_ROWS("categoryuris_phishing");
    $PhishingURIS = numberFormat($PhishingURIS, 0, "", " ");
    if ($GLOBALS["VERBOSE"]) {
        echo __LINE__ . " >COUNT_ROWS('categoryuris_malware')<br>\n";
    }
    $MalwaresURIS = $q->COUNT_ROWS("categoryuris_malware");
    $MalwaresURIS = numberFormat($MalwaresURIS, 0, "", " ");
    if (!$users->CORP_LICENSE) {
        $PhishingURIS = $nolicense;
        $MalwaresURIS = $nolicense;
    }
    if ($DisableArticaProxyStatistics == 0) {
        if ($EnableMacAddressFilter == 1) {
            $Computers = $q->COUNT_ROWS("webfilters_nodes");
            $Computers = numberFormat($Computers, 0, "", " ");
            $nodes = "\n\t\t\t<tr>\n\t\t\t\t<td width=1%><img src='img/arrow-right-16.png'></td>\n\t\t\t\t<td valign='top' {$mouse} style='font-size:12px;text-decoration:underline' OnClick=\"javascript:Loadjs('squid.nodes.php',true)\"><b><span style='font-size:12px'>{$Computers}</span></b><span style='font-size:12px'> {computers}</td>\n\t\t\t</tr>";
        } else {
            $Computers = $q->COUNT_ROWS("UserAutDB");
            $Computers = numberFormat($Computers, 0, "", " ");
            $nodes = "\n\t\t\t<tr>\n\t\t\t<td width=1%><img src='img/arrow-right-16.png'></td>\n\t\t\t<td valign='top' {$mouse} style='font-size:12px;text-decoration:underline' OnClick=\"javascript:Loadjs('squid.UserAutDB.php')\"><b><span style='font-size:12px'>{$Computers}</span></b><span style='font-size:12px'> {clients}</td>\n\t\t\t</tr>";
        }
        if (!$users->CORP_LICENSE) {
            $license_inactive = "<br><strong style='font-size:11px;font-weight:bolder;color:#BA1010'>{license_inactive}</strong>";
        }
        if (!$q->TABLE_EXISTS("tables_day")) {
            $q->CheckTables();
        }
        $DAYSNumbers = $q->COUNT_ROWS("tables_day");
        if ($GLOBALS["VERBOSE"]) {
            echo __LINE__ . " DAYSNumbers = {$DAYSNumbers}<br>\n";
        }
        //$GLOBALS["FULL_DEBUG"]
        $ligne = mysql_fetch_array($q->QUERY_SQL("SELECT SUM(totalsize) as tsize FROM tables_day"));
        $totalsize = FormatBytes($ligne["tsize"] / 1024);
        $ligne = mysql_fetch_array($q->QUERY_SQL("SELECT AVG(cache_perfs) as pourc FROM tables_day"));
        $pref = round($ligne["pourc"]);
        $ligne = mysql_fetch_array($q->QUERY_SQL("SELECT COUNT(sitename) as tcount FROM visited_sites WHERE LENGTH(category)=0"));
        $websitesnumsNot = numberFormat($ligne["tcount"], 0, "", " ");
        if ($GLOBALS["VERBOSE"]) {
            echo __LINE__ . " SELECT count(youtubeid),youtubeid FROM `youtube_dayz` GROUP BY youtubeid<br>\n";
        }
        $results = $q->QUERY_SQL("SELECT count(youtubeid),youtubeid FROM `youtube_dayz` GROUP BY youtubeid");
        $youtube_objects = mysql_num_rows($results);
        $youtube_objects = numberFormat($youtube_objects, 0, "", " ");
        $CachePermformance = $q->CachePerfHour();
        if ($GLOBALS["VERBOSE"]) {
            echo __LINE__ . " CachePermformance = {$CachePermformance}<br>\n";
        }
        if ($CachePermformance > -1) {
            $color = "#E01313";
            if ($CachePermformance > 20) {
                $color = "#6DBB6A";
            }
            $cachePerfText = "\n\t\t\t<tr>\n\t\t\t<td width=1%><img src='img/arrow-right-16.png'></td>\n\t\t\t<td valign='top' style='font-size:12px;'><b style='color:{$color}'>{$CachePermformance}%</span></b><span style='font-size:12px'> {cache_performance} ({now})</td>\n\t\t\t</tr>\n\t\t\t";
        }
        if ($SquidActHasReverse == 1) {
            $TR_CAT_NUMBER = null;
        }
        $TR_CAT_NUMBER = "\n\t<tr>\n\t\t<td width=1%><img src='img/arrow-right-16.png'></td>\n\t\t<td valign='top' {$mouse} style='font-size:12px;text-decoration:underline' OnClick=\"javascript:Loadjs('squid.categories.php')\"><b><span style='font-size:12px'>{$categories}</span></b><span style='font-size:12px'> {websites_categorized}{$license_inactive}</td>\n\t</tr>\n\t<tr>\n\t\t<td width=1%><img src='img/arrow-right-16.png'></td>\n\t\t<td valign='top' {$mouse} style='font-size:12px;text-decoration:underline' OnClick=\"javascript:Loadjs('squid.categories.php')\"><span style='font-size:12px'>{youritems}: <b>{$YourItems}</span></b><span style='font-size:12px'></td>\n\t</tr>";
        $TR_CATZ = "\t\n\t<tr>\n\t\t<td width=1%><img src='img/arrow-right-16.png'></td>\n\t\t<td width=99% valign='top' style='font-size:12px;text-decoration:underline' \n\t\t{$mouse} OnClick=\"javascript:Loadjs('squid.traffic.statistics.days.php?js=yes&with-purge=yes')\"><b><span style='font-size:12px'>{$DAYSNumbers}</span></b><span style='font-size:12px'> {daysOfStatistics}</td>\n\t</tr>\n\t\n\t<tr>\n\t\t<td width=1%><img src='img/arrow-right-16.png'></td>\n\t\t<td valign='top' style='font-size:12px'><b><span style='font-size:12px'>{$requests}</span></b><span style='font-size:12px'> {requests}</td>\n\t</tr>\n\t{$nodes}\n\t\t\n\t{$TR_CAT_NUMBER}\n";
        $TR_YOUTUBE = "\t<tr>\n\t\t<td width=1%><img src='img/arrow-right-16.png'></td>\n\t\t<td valign='top' {$mouse} style='font-size:12px;text-decoration:underline'\n\t\t{$mouse} OnClick=\"javascript:Loadjs('squid.youtube.all.php')\"\n\t\t><b><span style='font-size:12px'>{$youtube_objects}</span></b><span style='font-size:12px'> Youtube {objects}</td>\n\t</tr>";
        if ($DisableArticaProxyStatistics == 1) {
            $TR_YOUTUBE = null;
            $TR_CATZ = null;
        }
        if ($SquidActHasReverse == 1) {
            $TR_YOUTUBE = null;
        }
        $submenu = "\t\n\t\n\t<tr>\n\t\t<td width=1%><img src='img/arrow-right-16.png'></td>\n\t\t<td valign='top' style='font-size:12px'><b><span style='font-size:12px'>{$totalsize}</span></b><span style='font-size:12px'> {downloaded_flow}</td>\n\t</tr>\n\t\n\t<tr>\n\t\t<td width=1%><img src='img/arrow-right-16.png'></td>\n\t\t<td valign='top' style='font-size:12px'><b><span style='font-size:12px'>{$pref}%</span></b><span style='font-size:12px'> {cache_performance}</td>\n\t</tr>\n\t\n\t{$cachePerfText}";
        $main_table = "\n\t\t\n\t\t{$TR_CATZ}\n\t\t{$TR_YOUTUBE}\t\n\t<tr>\n\t\t<td width=1%><img src='img/arrow-right-16.png'></td>\n\t\t<td valign='top' {$mouse} style='font-size:12px;text-decoration:underline' OnClick=\"blur()\"><b><span style='font-size:12px'>{$PhishingURIS}</span></b><span style='font-size:12px'> {phishing_uris}</td>\n\t</tr>\t\n\t\n\t<tr>\n\t\t<td width=1%><img src='img/arrow-right-16.png'></td>\n\t\t<td valign='top' {$mouse} style='font-size:12px;text-decoration:underline' OnClick=\"blur()\"><b><span style='font-size:12px'>{$MalwaresURIS}</span></b><span style='font-size:12px'> {viruses_uris}</td>\n\t</tr>\n\t\n\t<tr>\n\t\t<td width=1%><img src='img/arrow-right-16.png'></td>\n\t\t<td valign='top' {$mouse} style='font-size:12px;text-decoration:underline' OnClick=\"blur()\"><b><span style='font-size:12px'>{$MalwarePatrolDatabasesCount}</span></b><span style='font-size:12px'> Malware Patrol</td>\n\t</tr>\t\n\t\t\t\t\t\n\t<tr>\n\t\t<td width=1%><img src='img/arrow-right-16.png'></td>\n\t\t<td valign='top' {$mouse} style='font-size:12px;text-decoration:underline' OnClick=\"javascript:Loadjs('squid.visited.php?onlyNot=yes')\"><b><span style='font-size:12px'>{$websitesnumsNot}</span></b><span style='font-size:12px'> {not_categorized}</td>\n\t</tr>\t\n\t\t\t\t\n\t<tr>\n\t\t<td width=1%><img src='img/arrow-right-16.png'></td>\n\t\t<td valign='top' {$mouse} style='font-size:12px;text-decoration:underline' OnClick=\"javascript:Loadjs('squid.categories.php')\"><b><span style='font-size:12px'>{$tablescatNUM}</span></b><span style='font-size:12px'> {categories}</td>\n\t</tr>\t\n\t\n\t<tr>\n\t\t<td width=1%><img src='img/arrow-right-16.png'></td>\n\t\t<td valign='top' {$mouse} style='font-size:12px;text-decoration:underline' OnClick=\"javascript:Loadjs('squid.categories.toexport.php')\"><b><span style='font-size:12px'>{$export}</span></b><span style='font-size:12px'> {websites_to_export}</td>\n\t</tr>";
    }
    if ($DisableArticaProxyStatistics == 1) {
        $main_table = "\t\n\t\t\t\n\t\t\t<tr>\n\t\t\t\t<td width=1%><img src='img/arrow-right-16.png'></td>\n\t\t\t\t<td valign='top' {$mouse} style='font-size:12px;text-decoration:underline' OnClick=\"blur()\"><b><span style='font-size:12px'>{$PhishingURIS}</span></b><span style='font-size:12px'> {phishing_uris}</td>\n\t\t\t</tr>\t\n\t\t\t\n\t\t\t<tr>\n\t\t\t\t<td width=1%><img src='img/arrow-right-16.png'></td>\n\t\t\t\t<td valign='top' {$mouse} style='font-size:12px;text-decoration:underline' OnClick=\"blur()\"><b><span style='font-size:12px'>{$MalwaresURIS}</span></b><span style='font-size:12px'> {viruses_uris}</td>\n\t\t\t</tr>\n\t\t\t\n\t\t\t<tr>\n\t\t\t\t<td width=1%><img src='img/arrow-right-16.png'></td>\n\t\t\t\t<td valign='top' {$mouse} style='font-size:12px;text-decoration:underline' OnClick=\"blur()\"><b><span style='font-size:12px'>{$MalwarePatrolDatabasesCount}</span></b><span style='font-size:12px'> Malware Patrol</td>\n\t\t\t</tr>";
    }
    $addwebsites = "\n\t\t<tr>\n\t\t\t<td width=1%><img src='img/plus-16.png'></td>\n\t\t\t<td valign='top' {$mouse} style='font-size:12px;text-decoration:underline' \n\t\t\tOnClick=\"javascript:Loadjs('squid.visited.php?add-www=yes')\"><b><span style='font-size:12px'>{categorize_websites}</span></b><span style='font-size:12px'></td>\n\t\t</tr>\t";
    if ($users->PROXYTINY_APPLIANCE) {
        $addwebsites = null;
        $submenu = null;
    }
    $html = "\n<table style='width:100%'>\n\t<tbody>\n\t{$TR_ACCESSLOG}\n\t{$main_table}\t\n\t{$submenu}\n\t{$addwebsites}\n\t</tbody>\n\t</table>\n{$off}";
    if ($GLOBALS["VERBOSE"]) {
        echo __LINE__ . " tpl->_ENGINE_parse_body<br>\n";
    }
    $html = $tpl->_ENGINE_parse_body($html);
    if (!$GLOBALS["AS_ROOT"]) {
        CACHE_SESSION_SET(__FUNCTION__, __FILE__, $html);
    }
}
function rotate_client($uuid)
{
    $unix = new unix();
    $workingdir = "/usr/share/artica-postfix/ressources/conf/meta/hosts/uploaded/{$uuid}/syslog";
    $syslog = new mysql_storelogs();
    $files = $unix->DirFiles($workingdir);
    while (list($basepath, $none) = each($files)) {
        $syslog->events("META: Rotate {$basepath}", __FUNCTION__, __LINE__);
        $syslog->ROTATE_ACCESS_TOMYSQL("{$workingdir}/{$basepath}");
    }
}
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);
}
예제 #5
0
function CleanRotatedFiles()
{
    include_once dirname(__FILE__) . "/ressources/class.mysql.syslog.inc";
    $unix = new unix();
    $sock = new sockets();
    $LogRotateCompress = 1;
    $LogRotatePath = $sock->GET_INFO("LogRotatePath");
    $ApacheLogRotate = $sock->GET_INFO("ApacheLogRotate");
    $cpbin = $unix->find_program("cp");
    $php5 = $unix->LOCATE_PHP5_BIN();
    $tmpdir = $unix->TEMP_DIR();
    if (!is_numeric($ApacheLogRotate)) {
        $ApacheLogRotate = 1;
    }
    if (!is_numeric($LogRotatePath)) {
        $LogRotatePath = "/home/logrotate";
    }
    $DirsToScan["/var/log"] = true;
    $DirsToScan["/var/log/apache2"] = true;
    $DirsToScan["/var/log/lighttpd"] = true;
    $DirsToScan["/var/log/ejabberd"] = true;
    $apache2 = $unix->dirdir("/var/log/apache2");
    while (list($WorkingDir, $ligne) = each($apache2)) {
        $DirsToScan[$WorkingDir] = true;
    }
    $q = new mysql_storelogs();
    while (list($WorkingDir, $ligne) = each($DirsToScan)) {
        $RotateSquid = false;
        if ($WorkingDir == "/var/log/squid") {
            continue;
        }
        $table = $unix->DirFiles($WorkingDir, "(\\.|-)[0-9]+.*?\$");
        $compressed["gz"] = true;
        $compressed["bz"] = true;
        $compressed["bz2"] = true;
        while (list($filename, $ligne) = each($table)) {
            $path = "{$WorkingDir}/{$filename}";
            if ($unix->file_time_min($path) < 1440) {
                continue;
            }
            $filedate = date('Y-m-d H:i:s', filemtime($path));
            $q->events("Injecting {$path} {$filedate}");
            if (!$q->ROTATE_TOMYSQL($path, $filedate)) {
                continue;
            }
        }
    }
}
function start_import($aspid = false)
{
    $sock = new sockets();
    $syslog = new mysql_storelogs();
    $unix = new unix();
    $pidfile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".pid";
    $unix = new unix();
    if (!$aspid) {
        $pid = @file_get_contents($pidfile);
        if ($unix->process_exists($pid, basename(__FILE__))) {
            $time = $unix->PROCESS_TTL($pid);
            stats_admin_events(2, "A already Importation Task is executed pid:{$pid} since {$time}Mn", null, __FILE__, __LINE__);
            writelogs("Already executed pid:{$pid}", __FUNCTION__, __FILE__, __LINE__);
            return;
        }
        $pid = @file_get_contents("/var/run/squid-stats-central.pid");
        if ($unix->process_exists($pid, basename(__FILE__))) {
            $time = $unix->PROCESS_TTL($pid);
            stats_admin_events(2, "An anlready Importation Task is executed pid:{$pid} since {$time}Mn", null, __FILE__, __LINE__);
            writelogs("Already executed pid:{$pid}", __FUNCTION__, __FILE__, __LINE__);
            return;
        }
    }
    $import_processes = import_processes();
    if ($import_processes > 6) {
        Import_logs("Too many processes ({$import_processes})...aborting task...");
        return;
    }
    $mypid = getmypid();
    @file_put_contents($pidfile, $mypid);
    $ArticaProxyStatisticsBackupFolder = $sock->GET_INFO("ArticaProxyStatisticsBackupFolder");
    if ($ArticaProxyStatisticsBackupFolder == null) {
        $ArticaProxyStatisticsBackupFolder = "/home/artica/squid/backup-statistics";
    }
    $ArticaProxyStatisticsBackupFolder = $ArticaProxyStatisticsBackupFolder . "/import";
    $files = $unix->DirFiles($ArticaProxyStatisticsBackupFolder);
    if ($GLOBALS["VERBOSE"]) {
        echo "PUSH Scanning {$ArticaProxyStatisticsBackupFolder}\n";
    }
    $mysql = $unix->find_program("mysql");
    $bzip2 = $unix->find_program("bzip2");
    $total = count($files);
    $c = 0;
    if ($total == 0) {
        return;
    }
    stats_admin_events(2, "Importing {$total} files to MySQL", null, __FILE__, __LINE__);
    while (list($filename, $none) = each($files)) {
        $c++;
        if (if_process_import_exists($filename)) {
            Import_logs("SKIP {$ArticaProxyStatisticsBackupFolder}/{$filename} Already running...");
            continue;
        }
        percentage("Extracting {$filename} {$c}/{$total}", 2);
        $size = @filesize("{$ArticaProxyStatisticsBackupFolder}/{$filename}");
        Import_logs("IMPORT {$ArticaProxyStatisticsBackupFolder}/{$filename}");
        $f = array();
        $results = array();
        $f[] = "{$bzip2} -d -c {$ArticaProxyStatisticsBackupFolder}/{$filename} |";
        $f[] = "{$mysql} --show-warnings";
        $f[] = "--socket=/var/run/mysqld/squid-db.sock";
        $f[] = "--protocol=socket --user=root --batch --force";
        $f[] = "--debug-info --database=squidlogs 2>&1";
        $cmd = @implode(" ", $f);
        $results[] = $cmd;
        percentage("Importing {$filename} to MySQL {$c}/{$total}", 2);
        exec($cmd, $results);
        Import_logs(@implode("\n", $results));
        stats_admin_events(2, "Success importing {$filename} to MySQL", @implode("\n", $results), __FILE__, __LINE__);
        Import_logs("Backup {$ArticaProxyStatisticsBackupFolder}/{$filename}");
        $syslog->ROTATE_TOMYSQL("{$ArticaProxyStatisticsBackupFolder}/{$filename}");
    }
}
예제 #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);
}
예제 #8
0
function BackupToNas($directory)
{
    if (!is_dir($directory)) {
        return;
    }
    $syslog = new mysql_storelogs();
    $sock = new sockets();
    $users = new usersMenus();
    $unix = new unix();
    $myHostname = $unix->hostname_g();
    $DirSuffix = basename($directory);
    $mount = new mount("/var/log/artica-postfix/logrotate.debug");
    $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;
    }
    $mount = new mount("/var/log/artica-postfix/logrotate.debug");
    $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;
    }
    $mv = $unix->find_program("mv");
    if ($BackupSquidLogsNASIpaddr == null) {
        $this->events("Backup via NAS is disabled, skip", __FUNCTION__, __FILE__, __LINE__, "logrotate");
        return false;
    }
    $mountPoint = "/mnt/BackupSquidLogsUseNas";
    if (!$mount->smb_mount($mountPoint, $BackupSquidLogsNASIpaddr, $BackupSquidLogsNASUser, $BackupSquidLogsNASPassword, $BackupSquidLogsNASFolder)) {
        $syslog->events("Unable to connect to NAS storage system (1): {$BackupSquidLogsNASUser}@{$BackupSquidLogsNASIpaddr}", __FUNCTION__, __FILE__, __LINE__, "logrotate");
        if ($BackupSquidLogsNASRetry == 0) {
            return;
        }
        sleep(3);
        $mount = new mount("/var/log/artica-postfix/logrotate.debug");
        if (!$mount->smb_mount($mountPoint, $BackupSquidLogsNASIpaddr, $BackupSquidLogsNASUser, $BackupSquidLogsNASPassword, $BackupSquidLogsNASFolder)) {
            $syslog->events("Unable to connect to NAS storage system (1): {$BackupSquidLogsNASUser}@{$BackupSquidLogsNASIpaddr}", __FUNCTION__, __FILE__, __LINE__, "logrotate");
            return;
        }
    }
    $syslog->events("Hostname={$myHostname} Suffix = {$DirSuffix} {$BackupSquidLogsNASIpaddr}/{$BackupSquidLogsNASFolder}", __FUNCTION__, __LINE__);
    $BackupMaxDaysDir = "{$mountPoint}/artica-backup-syslog";
    @mkdir("{$BackupMaxDaysDir}", 0755, true);
    if (!is_dir($BackupMaxDaysDir)) {
        $syslog->events("Fatal {$BackupMaxDaysDir} permission denied", __FUNCTION__, __LINE__);
        if ($GLOBALS["VERBOSE"]) {
            echo "Fatal {$BackupMaxDaysDir} permission denied\n";
        }
        squid_admin_mysql(0, "SYSLOG: FATAL {$BackupMaxDaysDir} permission denied", null, __FILE__, __LINE__);
        $mount->umount($mountPoint);
        return false;
    }
    $t = time();
    @file_put_contents("{$BackupMaxDaysDir}/{$t}", time());
    if (!is_file("{$BackupMaxDaysDir}/{$t}")) {
        $syslog->events("Fatal {$BackupMaxDaysDir} permission denied ({$BackupMaxDaysDir}/{$t}) test failed", __FUNCTION__, __LINE__);
        squid_admin_mysql(0, "SYSLOG: FATAL {$BackupMaxDaysDir} permission denied", null, __FILE__, __LINE__);
        $mount->umount($mountPoint);
        return false;
    }
    @unlink("{$BackupMaxDaysDir}/{$t}");
    exec("{$mv} --force {$directory} --target-directory={$BackupMaxDaysDir}/ 2>&1", $results);
    while (list($index, $line) = each($results)) {
        $syslog->events("{$line}", __FUNCTION__, __LINE__);
    }
    analyze_destination_directory($BackupMaxDaysDir . "/proxy");
    $mount->umount($mountPoint);
    return true;
}
예제 #9
0
function awstats_mail()
{
    $users = new usersMenus();
    if (!$users->POSTFIX_INSTALLED) {
        return;
    }
    $unix = new unix();
    $sock = new sockets();
    $ArticaMetaEnabled = trim($sock->GET_INFO("ArticaMetaEnabled"));
    if (!is_numeric($ArticaMetaEnabled)) {
        $ArticaMetaEnabled = 0;
    }
    $EnableArticaSMTPStatistics = $sock->GET_INFO("EnableArticaSMTPStatistics");
    if (!is_numeric($EnableArticaSMTPStatistics)) {
        $EnableArticaSMTPStatistics = 1;
    }
    $pidfile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".pid";
    $pidTime = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".time";
    if ($EnableArticaSMTPStatistics == 0) {
        return;
    }
    $pid = $unix->get_pid_from_file($pidfile);
    if (!$GLOBALS["VERBOSE"]) {
        if ($unix->process_exists($pid, basename(__FILE__))) {
            $pidTime = $unix->PROCCESS_TIME_MIN($pid);
            events("Already process PID: {$pid} running since {$pidTime} minutes", __FUNCTION__, __FILE__, __LINE__, "postfix-stats");
            return;
        }
        $time = $unix->file_time_min("{$pidTime}");
        if ($time < 120) {
            events("Current {$time}mn, need 60mn");
            return;
        }
        if (system_is_overloaded(basename(__FILE__))) {
            system_admin_events("Overloaded system, aborting", __FUNCTION__, __FILE__, __LINE__, "postfix-stats");
            return;
        }
        @unlink($pidTime);
        @file_put_contents($pidTime, time());
        @file_put_contents($pidfile, getmypid());
    }
    $tt1 = time();
    $nohup = $unix->find_program("nohup");
    if (!$users->awstats_installed) {
        events("awstats is not installed, artica will install it itself", __FUNCTION__, __FILE__, __LINE__, "postfix-stats");
        events("{$nohup} /usr/share/artica-postfix/bin/artica-make APP_AWSTATS >/dev/null &");
        shell_exec(trim("{$nohup} /usr/share/artica-postfix/bin/artica-make APP_AWSTATS >/dev/null &"));
        return;
    }
    $sock = new sockets();
    $GLOBALS["EnablePostfixMultiInstance"] = $sock->GET_INFO("EnablePostfixMultiInstance");
    $GLOBALS["maillogconvert"] = $unix->LOCATE_maillogconvert();
    $GLOBALS["zcat"] = $unix->find_program("zcat");
    $GLOBALS["perl"] = $unix->find_program("perl");
    $GLOBALS["nice"] = EXEC_NICE();
    $GLOBALS["sed"] = $unix->find_program("sed");
    $GLOBALS["mv"] = $unix->find_program("mv");
    $GLOBALS["cp"] = $unix->find_program("cp");
    $GLOBALS["touch"] = $unix->find_program("touch");
    $GLOBALS["echo"] = $unix->find_program("echo");
    $GLOBALS["postfix"] = $unix->find_program("postfix");
    if ($GLOBALS["VERBOSE"]) {
        echo "maillogconvert..........:{$GLOBALS["maillogconvert"]}\n";
        echo "zcat....................:{$GLOBALS["zcat"]}\n";
        echo "perl....................:{$GLOBALS["perl"]}\n";
        echo "nice....................:{$GLOBALS["nice"]}\n";
        echo "sed.....................:{$GLOBALS["sed"]}\n";
    }
    if (strlen($GLOBALS["maillogconvert"]) == null) {
        events("maillogconvert.pl, no such file", __FUNCTION__, __FILE__, __LINE__, "postfix-stats");
        return;
    }
    @mkdir("/var/log/mail-backup", 666, true);
    foreach (glob("/var/log/mail.log.*.gz") as $filename) {
        events("{$GLOBALS["nice"]}{$GLOBALS["zcat"]} {$filename} >/tmp/mail.log");
        shell_exec("{$GLOBALS["nice"]}{$GLOBALS["zcat"]} {$filename} >/tmp/mail.log");
        $t1 = time();
        prepflog("/tmp/mail.log");
        $distanceOfTimeInWords = distanceOfTimeInWords($t1, time());
        shell_exec("/bin/mv {$filename} /var/log/mail-backup/");
        events("/bin/mv {$filename} /var/log/mail-backup/", __FUNCTION__, __FILE__, __LINE__, "postfix-stats");
        events(basename($filename) . " {$distanceOfTimeInWords}");
        $ev[] = basename($filename) . " " . $distanceOfTimeInWords;
        @unlink("/tmp/mail.log");
    }
    foreach (glob("/var/log/mail.log.*") as $filename) {
        if (!preg_match("#\\.[0-9]+\$#", basename($filename))) {
            if ($GLOBALS["VERBOSE"]) {
                echo basename($filename) . " SKIP\n";
            }
            continue;
        }
        $t1 = time();
        prepflog($filename);
        $distanceOfTimeInWords = distanceOfTimeInWords($t1, time());
        if ($GLOBALS["VERBOSE"]) {
            echo basename($filename) . " {$distanceOfTimeInWords}\n";
        }
        $ev[] = basename($filename) . " " . $distanceOfTimeInWords;
        shell_exec("/bin/mv {$filename} /var/log/mail-backup/");
    }
    $t1 = time();
    if (is_file("/var/log/mail.log")) {
        $nextFile = "/var/log/mail.log." . time();
        shell_exec("{$GLOBALS["cp"]} /var/log/mail.log {$nextFile}");
        shell_exec("{$GLOBALS["echo"]} \"\" > /var/log/mail.log");
        events("Restarting Syslogs...");
        $syslog_init = $unix->RESTART_SYSLOG_FORMAIL();
        prepflog($nextFile);
        $storelogs = new mysql_storelogs();
        events("Injecting {$nextFile} to MySQL store logs");
        $storelogs->InjectFile($nextFile);
        artica_parse();
        events("reloading postfix");
        shell_exec("{$GLOBALS["postfix"]} reload");
    }
    $distanceOfTimeInWords = distanceOfTimeInWords($t1, time());
    $ev[] = basename("/var/log/mail.log") . " " . $distanceOfTimeInWords;
    if ($GLOBALS["VERBOSE"]) {
        echo basename("/var/log/mail.log") . " {$distanceOfTimeInWords}\n";
    }
    //$cmd="$nice$perl /usr/share/artica-postfix/bin/prepflog.pl </tmp/mail.log|$nice$perl $maillogconvert standard >>/var/log/artica-postfix/awstats-postfix.stats";
    foreach (glob("/var/log/artica-mail/*.stats") as $filename) {
        if (preg_match("#(.+?)\\.([0-9]+)\\.stats#", basename($filename), $re)) {
            $instance = $re[1];
            $time = $re[2];
            $cmd = "{$GLOBALS["nice"]}{$GLOBALS["perl"]} {$GLOBALS["maillogconvert"]} standard< {$filename} >/var/log/artica-mail/{$instance}.{$time}.aws";
            events($cmd);
            shell_exec($cmd);
            @unlink($filename);
            if (system_is_overloaded(basename(__FILE__))) {
                system_admin_events("Overloaded system, aborting", __FUNCTION__, __FILE__, __LINE__, "postfix-stats");
                return;
            }
        }
    }
    $filecount = 0;
    foreach (glob("/var/log/artica-mail/*.aws") as $filename) {
        artica_parse($filename);
        $filecount++;
        $filecountl[] = $filename;
    }
    $distanceOfTimeInWords = distanceOfTimeInWords($tt1, time());
    if ($filecount > 0) {
        system_admin_events("Success generating {$filecount} stats files ({$distanceOfTimeInWords})\n" . @implode("\n", $filename), __FUNCTION__, __FILE__, __LINE__, "postfix-stats");
        if ($ArticaMetaEnabled == 1) {
            $cmd = "{$GLOBALS["nice"]}" . LOCATE_PHP5_BIN() . " " . dirname(__FILE__) . "/exec.artica.meta.users.php --export-postfix-events >/dev/null 2>&1 &";
            shell_exec($cmd);
        }
    }
}
예제 #10
0
function CleanPercent($BackupMaxDaysDir, $TOTAL_AVAILABLE)
{
    $unix = new unix();
    $q = new mysql_storelogs();
    $ARRAY = array();
    $DIRSIZE = $unix->DIRSIZE_BYTES($BackupMaxDaysDir);
    $q->events("Remove files in {$BackupMaxDaysDir} " . FormatBytes($DIRSIZE / 1024) . "/" . FormatBytes($TOTAL_AVAILABLE / 1024), __FUNCTION__, __LINE__);
    $q2 = new mysql();
    $results = $q2->QUERY_SQL("SELECT * FROM backuped_logs ORDER BY zDate LIMIT 0,50", "artica_backup");
    if (!$q2->ok) {
        squid_admin_mysql(0, "MySQL error", $q2->mysql_error, __FILE__, __LINE__);
        return;
    }
    while ($ligne = mysql_fetch_assoc($results)) {
        if (preg_match("#^\\/mnt\\/", $ligne["path"])) {
            continue;
        }
        $filepath = $ligne["path"];
        if (!is_file($filepath)) {
            continue;
        }
        $sizeBytes = @filesize($filepath);
        @unlink($filepath);
        $DIRSIZE = $DIRSIZE - $sizeBytes;
        $q->events("Remove {$filepath} (" . FormatBytes($sizeBytes / 1024) . ") New DIR SIZE=" . FormatBytes($DIRSIZE / 1024) . "/" . FormatBytes($TOTAL_AVAILABLE / 1024), __FUNCTION__, __LINE__);
        if ($DIRSIZE < $TOTAL_AVAILABLE) {
            break;
        }
    }
}
예제 #11
0
function check_all_squid()
{
    $sock = new sockets();
    $unix = new unix();
    $syslog = new mysql_storelogs();
    $php5 = $unix->LOCATE_PHP5_BIN();
    $pidfile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".pid";
    $timefile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".time";
    $pid = file_get_contents("{$pidfile}");
    if (system_is_overloaded(basename(__FILE__))) {
        die;
    }
    if ($unix->process_exists($pid, basename(__FILE__))) {
        $timeMin = $unix->PROCCESS_TIME_MIN($pid);
        if ($timeMin > 240) {
            system_admin_events("Too many TTL, {$pid} will be killed", __FUNCTION__, __FILE__, __LINE__, "logrotate");
            $kill = $unix->find_program("kill");
            unix_system_kill_force($pid);
        } else {
            die;
        }
    }
    $time = $unix->file_time_min($timefile);
    if ($time < 300) {
        return;
    }
    @file_put_contents($pidfile, getmypid());
    @file_put_contents($timefile, time());
    $php = $unix->LOCATE_PHP5_BIN();
    $bzip2 = $unix->find_program("bzip2");
    $ALREADYCOMP["gz"] = true;
    $ALREADYCOMP["bz2"] = true;
    $LogRotateCompress = 1;
    $LogRotatePath = $sock->GET_INFO("LogRotatePath");
    $ApacheLogRotate = $sock->GET_INFO("ApacheLogRotate");
    if (!is_numeric($ApacheLogRotate)) {
        $ApacheLogRotate = 1;
    }
    if ($LogRotatePath == null) {
        $LogRotatePath = "/home/logrotate";
    }
    $LogsRotateDefaultSizeRotation = $sock->GET_INFO("LogsRotateDefaultSizeRotation");
    if (!is_numeric($LogsRotateDefaultSizeRotation)) {
        $LogsRotateDefaultSizeRotation = 100;
    }
    foreach (glob("/var/log/squid/*") as $filename) {
        if (is_dir($filename)) {
            continue;
        }
        $size = $unix->file_size($filename);
        $time = $unix->file_time_min($filename);
        $size = round($size / 1024 / 1000, 2);
        if ($size > $LogsRotateDefaultSizeRotation) {
            if ($filename == "/var/log/squid/access.log") {
                events("{$filename} -> is a production log for Squid, launch the rotation procedure.");
                squid_admin_mysql(1, "{$filename} {$size}M exceed {$LogsRotateDefaultSizeRotation}M, launch rotation", null, __FILE__, __LINE__);
                shell_exec("{$php} /usr/share/artica-postfix/exec.squid.php --rotate");
                continue;
            }
            $TOROT[$filename] = true;
            events("{$filename} -> Add to queue {$size}M exceed {$LogsRotateDefaultSizeRotation}M");
            continue;
        }
        if ($time > 1440) {
            if ($filename == "/var/log/squid/access.log") {
                events("{$filename} -> is a production log for Squid, launch the rotation procedure.");
                squid_admin_mysql(1, "{$filename} {$size}M exceed {$LogsRotateDefaultSizeRotation}M, launch rotation", null, __FILE__, __LINE__);
                shell_exec("{$php} /usr/share/artica-postfix/exec.squid.php --rotate");
                continue;
            }
            events("{$filename} -> Add to queue {$time}mn exceed 1440mn");
            $TOROT[$filename] = true;
            continue;
        }
    }
    if (count($TOROT) == 0) {
        return;
    }
    while (list($filename, $none) = each($TOROT)) {
        $extension = pathinfo($filename, PATHINFO_EXTENSION);
        $filedate = date('Y-m-d H:i:s', filemtime($filename));
        $basename = basename($filename);
        if (preg_match("#sarg\\.#", $filename)) {
            shell_exec("{$php5} " . dirname(__FILE__) . "/exec.sarg.php --rotate {$basename} >/dev/null 2>&1 &");
            continue;
        }
        if (preg_match("#access\\.log\\.[0-9]+\$#", $filename)) {
            continue;
        }
        if ($extension != "gz") {
            if (!$unix->compress($filename, "{$filename}.gz")) {
                continue;
            }
            $filename = $filename . ".gz";
            $extension = "gz";
        }
        echo "[{$filedate}]: {$filename} ({$extension})\n";
        if ($syslog->ROTATE_TOMYSQL($filename, $filedate)) {
            @unlink($filename);
        }
    }
    foreach (glob("/home/squid/cache-logs/*") as $filename) {
        $filedate = date('Y-m-d H:i:s', filemtime($filename));
        $filename = $filename . ".gz";
        if ($syslog->ROTATE_TOMYSQL($filename, $filedate)) {
            @unlink($filename);
        }
    }
}
예제 #12
0
function wizard10(){
	
	$q=new mysql_squid_builder();
	$page=CurrentPageName();
	$tpl=new templates();
	$sock=new sockets();
	$t=$_GET["t"];
	$tt=time()+rand(0,time());
	if(!$q->BD_CONNECT()){
		echo FATAL_WARNING_SHOW_128("<hr>{error}<hr>{statistics_database}<hr>$q->mysql_error".wizard_restart());return;
	}
	
	$q=new mysql_storelogs();
	if(!$q->BD_CONNECT()){
		echo FATAL_WARNING_SHOW_128("<hr>{error}<hr>{logs_database}<hr>$q->mysql_error".wizard_restart());return;
	}
	
	echo $tpl->_ENGINE_parse_body("<center style='font-size:18px'>{statistics_database}:OK</center>
			<center style='font-size:18px'>{logs_database}:OK</center>")."
			<div id='$tt'></div>
			<script>
			LoadAjax('$t','$page?wizard11=yes&t=$t');
			</script>
	";
	
}
예제 #13
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_mysql(1, "SYSLOG: FATAL {$BackupMaxDaysDir} permission denied", null, __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_mysql(1, "SYSLOG: FATAL {$BackupMaxDaysDir} permission denied", null, __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_mysql(1, "SYSLOG: Unable to extract {$ligne["filename"]} to {$BackupMaxDaysDir}", null, __FILE__, __LINE__);
            if ($BackupSquidLogsUseNas == 1) {
                $mount->umount($mountPoint);
            }
            return false;
        } else {
            events("Success extracting {$ligne["filename"]} to {$BackupMaxDaysDir}");
        }
    }
    if ($BackupSquidLogsUseNas == 1) {
        $mount->umount($mountPoint);
    }
}
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>";
}
예제 #15
0
function stats_appliance_privs()
{
    if ($GLOBALS["VERBOSE"]) {
        echo "stats_appliance_privs():: {$_SERVER["REMOTE_ADDR"]}<br> \n";
    }
    $q = new mysql_squid_builder();
    $sock = new sockets();
    $OrginalPassword = $q->mysql_password;
    $server = $_SERVER["REMOTE_ADDR"];
    $username = str_replace(".", "", $server);
    $password = md5($server);
    if ($GLOBALS["VERBOSE"]) {
        echo "USER:{$username}@{$server} and password: {$password} Line:" . __LINE__ . "<br> \n";
    }
    writelogs("USER:{$username}@{$server} and password: {$password}", __FUNCTION__, __FILE__, __LINE__);
    // Enable Ufdbguard...
    $UFDB = unserialize(base64_decode($sock->GET_INFO("ufdbguardConfig")));
    $UFDB["tcpsockets"] = 1;
    $UFDB["listen_port"] = 3977;
    $UFDB["listen_addr"] = "all";
    $UFDB["UseRemoteUfdbguardService"] = "0";
    $sock->SET_INFO("EnableUfdbGuard", 1);
    $sock->SET_INFO("EnableUfdbGuard2", 1);
    $sock->SET_INFO("UseRemoteUfdbguardService", 0);
    $sock->SaveConfigFile(base64_encode(serialize($UFDB)), "ufdbguardConfig");
    //
    if (!$q->GRANT_PRIVS($server, $username, $password)) {
        $array["ERROR"] = $q->mysql_error;
        if ($GLOBALS["VERBOSE"]) {
            echo "stats_appliance_privs():: MySQL Error line: " . __LINE__ . " {$q->mysql_error}<br> \n";
        }
        echo "\n\n<RESULTS>" . base64_encode(serialize($array)) . "</RESULTS>\n\n";
        return;
    }
    $q = new mysql_storelogs();
    if (!$q->GRANT_PRIVS($server, $username, $password)) {
        $array["ERROR"] = $q->mysql_error;
        echo "\n\n<RESULTS>" . base64_encode(serialize($array)) . "</RESULTS>\n\n";
        return;
    }
    writelogs("Send Correctly USER:{$array["mysql"]["username"]} and password: {$array["mysql"]["password"]}", __FUNCTION__, __FILE__, __LINE__);
    $array["mysql"]["username"] = $username;
    $array["mysql"]["password"] = $password;
    if ($GLOBALS["VERBOSE"]) {
        print_r($array);
    }
    $sock->getFrameWork("cmd.php?restart-ufdb=yes");
    $sock->getFrameWork("cmd.php?squidnewbee=yes");
    echo "\n\n<RESULTS>" . base64_encode(serialize($array)) . "</RESULTS>\n\n";
}
예제 #16
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__);
}
예제 #17
0
function ParseFile($servername, $fullpath)
{
    events("[{$servername}]: Parsing {$fullpath}", __FUNCTION__, __LINE__);
    $unix = new unix();
    $size = @filesize($fullpath);
    events("[{$servername}]: open {$fullpath} {$size} bytes", __FUNCTION__, __LINE__);
    $handle = @fopen($fullpath, "r");
    if (!$handle) {
        events("[{$servername}]: open {$fullpath} fatal, unable to open ", __FUNCTION__, __LINE__);
        return;
    }
    $c = 0;
    $d = 0;
    $t = time();
    $WORKARRAY = array();
    while (!feof($handle)) {
        $d++;
        $line = trim(fgets($handle, 4096));
        if ($line == null) {
            continue;
        }
        if (!preg_match('#(.*?)\\s+(.*?)\\s+(.*?)\\s+\\[(.*?)\\]\\s+([A-Z]+)\\s+(.*?)\\s+HTTP.*?\\/.*?"([0-9]+)"\\s+([0-9]+)\\s+"(.*?)"\\s+"(.*?)"\\s+"(.*?)"#', $line, $re)) {
            events("[{$servername}]: {{$line}} unable to parse...", __FUNCTION__, __LINE__);
            continue;
        }
        while (list($a, $b) = each($re)) {
            $re[$a] = mysql_escape_string2($b);
        }
        $c++;
        $md5 = md5($re[0]);
        $ipaddr = $re[1];
        $time = strtotime($re[4]);
        $proto = $re[5];
        $uri = $re[6];
        $code = $re[7];
        $size = $re[8];
        $UserAgent = $re[10];
        $Country = mysql_escape_string2(GeoLoc($ipaddr));
        $currDate = date("Y-m-d H:i:s");
        $linesql = "('{$md5}','{$currDate}','{$ipaddr}','{$proto}','{$uri}','{$code}','{$size}','{$UserAgent}','{$Country}')";
        $table = "hour_" . date("YmdH", $time);
        $WORKARRAY[$table][] = $linesql;
        if ($c > 500) {
            if (!ParseArray($servername, $WORKARRAY)) {
                return;
            }
            $WORKARRAY = array();
            $c = 0;
        }
    }
    if (count($WORKARRAY) > 0) {
        if (!ParseArray($servername, $WORKARRAY)) {
            return;
        }
    }
    $timeTOScan = $unix->distanceOfTimeInWords($t, time(), true);
    events("[{$servername}]: {$fullpath} {$timeTOScan} {$d} lines", __FUNCTION__, __LINE__);
    if ($d == 0) {
        @unlink($fullpath);
    }
    $sys = new mysql_storelogs();
    $filedate = date('Y-m-d H:i:s', filemtime($fullpath));
    $sys->ROTATE_TOMYSQL($fullpath, $filedate);
}
예제 #18
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();
}
예제 #19
0
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__);
}
예제 #20
0
function awstats()
{
    $sock = new sockets();
    $unix = new unix();
    $pidfile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".pid";
    $pidTime = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".time";
    if ($unix->file_time_min($pidTime) < 60) {
        return;
    }
    $pid = $unix->get_pid_from_file($pidfile);
    if ($unix->process_exists($pid, basename(__FILE__))) {
        $time = $unix->PROCCESS_TIME_MIN($pid);
        return;
    }
    @file_put_contents($pidfile, getmypid());
    @unlink($pidTime);
    @file_put_contents($pidTime, time());
    $sock = new sockets();
    $EnableNginxStats = $sock->GET_INFO("EnableNginxStats");
    if (!is_numeric($EnableNginxStats)) {
        $EnableNginxStats = 0;
    }
    if ($EnableNginxStats == 1) {
        return;
    }
    include_once dirname(__FILE__) . "/ressources/class.awstats.inc";
    include_once dirname(__FILE__) . "/ressources/class.mysql.syslogs.inc";
    $awstats_bin = $unix->LOCATE_AWSTATS_BIN();
    $nice = EXEC_NICE();
    $perl = $unix->find_program("perl");
    $awstats_buildstaticpages = $unix->LOCATE_AWSTATS_BUILDSTATICPAGES_BIN();
    if ($GLOBALS["VERBOSE"]) {
        echo "awstats......: {$awstats_bin}\n";
        echo "statics Pages: {$awstats_buildstaticpages}\n";
        echo "Nice.........: {$nice}\n";
        echo "perl.........: {$perl}\n";
    }
    if (!is_file($awstats_buildstaticpages)) {
        echo "buildstaticpages no such binary...\n";
        return;
    }
    $sock = new sockets();
    $kill = $unix->find_program("kill");
    $NginxWorkLogsDir = $sock->GET_INFO("NginxWorkLogsDir");
    if ($NginxWorkLogsDir == null) {
        $NginxWorkLogsDir = "/home/nginx/logsWork";
    }
    $sys = new mysql_storelogs();
    $files = $unix->DirFiles($NginxWorkLogsDir, "-([0-9\\-]+)\\.log");
    while (list($filename, $line) = each($files)) {
        if (!preg_match("#^(.+?)-[0-9]+-[0-9]+-[0-9]+-[0-9]+\\.log\$#", $filename, $re)) {
            if ($GLOBALS["VERBOSE"]) {
                echo "{$filename}, skip\n";
            }
            continue;
        }
        if ($GLOBALS["VERBOSE"]) {
            echo "{$filename}, domain:{$re[1]}\n";
        }
        $servername = $re[1];
        $GLOBALS["nice"] = $nice;
        $aw = new awstats($servername);
        $aw->set_LogFile("{$NginxWorkLogsDir}/{$filename}");
        $aw->set_LogType("W");
        $aw->set_LogFormat(1);
        $config = $aw->buildconf();
        $SOURCE_FILE_PATH = "{$NginxWorkLogsDir}/{$filename}";
        $configlength = strlen($config);
        if ($configlength < 10) {
            if ($GLOBALS["VERBOSE"]) {
                echo "configuration file lenght failed {$configlength} bytes, aborting {$servername}\n";
            }
            return;
        }
        @file_put_contents("/etc/awstats/awstats.{$servername}.conf", $config);
        @chmod("/etc/awstats/awstats.{$servername}.conf", 644);
        $Lang = $aw->GET("Lang");
        if ($Lang == null) {
            $Lang = "auto";
        }
        @mkdir("/var/tmp/awstats/{$servername}", 666, true);
        $t1 = time();
        $cmd = "{$nice}{$perl} {$awstats_buildstaticpages} -config={$servername} -update -lang={$Lang} -awstatsprog={$awstats_bin} -dir=/var/tmp/awstats/{$servername} -LogFile=\"{$SOURCE_FILE_PATH}\" 2>&1";
        if ($GLOBALS["VERBOSE"]) {
            echo $cmd . "\n";
        }
        shell_exec($cmd);
        $filedate = date('Y-m-d H:i:s', filemtime($SOURCE_FILE_PATH));
        if (!awstats_import_sql($servername)) {
            continue;
        }
        $sys->ROTATE_TOMYSQL($SOURCE_FILE_PATH, $filedate);
    }
}
예제 #21
0
function wizard10()
{
    $q = new mysql_squid_builder();
    $page = CurrentPageName();
    $tpl = new templates();
    $sock = new sockets();
    $t = $_GET["t"];
    $tt = time() + rand(0, time());
    if (!$q->BD_CONNECT()) {
        echo FATAL_ERROR_SHOW_128("<hr>{error}<hr>{statistics_database}<hr>{$q->mysql_error}" . wizard_restart());
        return;
    }
    $q = new mysql_storelogs();
    if (!$q->BD_CONNECT()) {
        echo FATAL_ERROR_SHOW_128("<hr>{error}<hr>{logs_database}<hr>{$q->mysql_error}" . wizard_restart());
        return;
    }
    echo $tpl->_ENGINE_parse_body("<center style='font-size:18px'>{statistics_database}:OK</center>\n\t\t\t<center style='font-size:18px'>{logs_database}:OK</center>") . "\n\t\t\t<div id='{$tt}'></div>\n\t\t\t<script>\n\t\t\tLoadAjax('{$t}','{$page}?wizard11=yes&t={$t}');\n\t\t\t</script>\n\t";
}