Beispiel #1
0
function clean_squid_stats_dbs()
{
    $sock = new sockets();
    $DisableArticaProxyStatistics = $sock->GET_INFO("DisableArticaProxyStatistics");
    $CleanArticaSquidDatabases = $sock->GET_INFO("CleanArticaSquidDatabases");
    if (!is_numeric($DisableArticaProxyStatistics)) {
        $DisableArticaProxyStatistics = 0;
    }
    if (!is_numeric($CleanArticaSquidDatabases)) {
        $CleanArticaSquidDatabases = 0;
    }
    if (!$GLOBALS["FORCE"]) {
        if ($CleanArticaSquidDatabases == 0) {
            echo "Option is not activated...\n";
            return;
        }
    }
    $q = new mysql_squid_builder();
    $tables = $q->LIST_TABLES_DAYS();
    $rows = 0;
    $count_tables = 0;
    while (list($num, $table) = each($tables)) {
        $rows = $rows + $q->COUNT_ROWS($table);
        if ($GLOBALS["VERBOSE"]) {
            echo " Delete table {$table} {$rows} rows \n";
        }
        $count_tables++;
        $q->DELETE_TABLE($table);
    }
    $tables = $q->LIST_TABLES_DAYS_BLOCKED();
    while (list($num, $table) = each($tables)) {
        $rows = $rows + $q->COUNT_ROWS($table);
        if ($GLOBALS["VERBOSE"]) {
            echo " Delete table {$table} {$rows} rows \n";
        }
        $count_tables++;
        $q->DELETE_TABLE($table);
    }
    $tables = $q->LIST_TABLES_MEMBERS();
    while (list($num, $table) = each($tables)) {
        $rows = $rows + $q->COUNT_ROWS($table);
        if ($GLOBALS["VERBOSE"]) {
            echo " Delete table {$table} {$rows} rows \n";
        }
        $count_tables++;
        $q->DELETE_TABLE($table);
    }
    $tables = $q->LIST_TABLES_MONTH();
    while (list($num, $table) = each($tables)) {
        $rows = $rows + $q->COUNT_ROWS($table);
        if ($GLOBALS["VERBOSE"]) {
            echo " Delete table {$table} {$rows} rows \n";
        }
        $count_tables++;
        $q->DELETE_TABLE($table);
    }
    $tables = $q->LIST_TABLES_WEEKS();
    while (list($num, $table) = each($tables)) {
        $rows = $rows + $q->COUNT_ROWS($table);
        if ($GLOBALS["VERBOSE"]) {
            echo " Delete table {$table} {$rows} rows \n";
        }
        $count_tables++;
        $q->DELETE_TABLE($table);
    }
    $q = new mysql_catz();
    $tables = $q->LIST_TABLES_CATEGORIES();
    while (list($num, $table) = each($tables)) {
        $rows = $rows + $q->COUNT_ROWS($table);
        if ($GLOBALS["VERBOSE"]) {
            echo " Delete table {$table} {$rows} rows \n";
        }
        $count_tables++;
        $q->DELETE_TABLE($table);
    }
    $q = new mysql();
    if ($q->DATABASE_EXISTS("catz")) {
        $q->DELETE_DATABASE("catz");
    }
    if ($count_tables > 0) {
        mysql_admin_mysql(1, "Restarting MySQL service...", null, __FILE__, __LINE__);
        shell_exec("/etc/init.d/mysql restart");
    }
    $sock->TOP_NOTIFY("{$count_tables} statistics tables as been deleted with {$rows} rows", "info");
    //print_r($tables);
}
function month_list_members(){
	$q=new mysql_squid_builder();
	$page=CurrentPageName();
	$array=$q->LIST_TABLES_MEMBERS();
	while (list ($num, $ligne) = each ($array) ){
		$ligne=trim($ligne);
		if(!preg_match("#([0-9]+)_members#", $ligne,$re)){continue;}
		$len=strlen($re[1]);
		if($len>6){continue;}
		$year=substr($re[1], 0,4);
		$month=substr($re[1], 4,2);
		if($year<>date('Y')){continue;}
		$uri="<a href=\"javascript:blur()\" OnClick=\"javascript:LoadAjax('squid-members-graphs','$page?squid-members-graphs=yes&month=$year-$month');\" style='font-size:14px;text-decoration:underline;font-weight:bold'>";
		
		
		$tr=$tr."<td style='font-size:14px'>$uri$month</a></td>";
		
		
	}
	
	$html="
<center>
		<table class=form>
		<tbody>
			<tr>$tr</tr>
		</tbody>
	</table>
</center>";
	echo $html;
}
function start_export()
{
    $unix = new unix();
    $sock = new sockets();
    $q = new mysql_squid_builder();
    $ArticaProxyStatisticsBackupFolder = $sock->GET_INFO("ArticaProxyStatisticsBackupFolder");
    if ($ArticaProxyStatisticsBackupFolder == null) {
        $ArticaProxyStatisticsBackupFolder = "/home/artica/squid/backup-statistics";
    }
    $ArticaProxyStatisticsBackupFolder = $ArticaProxyStatisticsBackupFolder . "/export";
    $LIST_TABLES_YOUTUBE_HOURS = $q->LIST_TABLES_YOUTUBE_HOURS();
    $LIST_TABLES_SIZEHOURS = $q->LIST_TABLES_SIZEHOURS();
    $LIST_TABLES_QUOTA_HOURS = $q->LIST_TABLES_QUOTA_HOURS();
    $LIST_TABLES_QUOTADAY = $q->LIST_TABLES_QUOTADAY();
    $LIST_TABLES_QUOTAMONTH = $q->LIST_TABLES_QUOTAMONTH();
    $LIST_TABLES_SEARCHWORDS_HOURS = $q->LIST_TABLES_SEARCHWORDS_HOURS();
    $LIST_TABLES_SEARCHWORDS_DAY = $q->LIST_TABLES_SEARCHWORDS_DAY();
    $LIST_TABLES_dansguardian_events = $q->LIST_TABLES_dansguardian_events();
    $LIST_TABLES_HOURS = $q->LIST_TABLES_HOURS();
    $LIST_TABLES_USERSIZED = $q->LIST_TABLES_USERSIZED();
    $LIST_TABLES_YOUTUBE_WEEK = $q->LIST_TABLES_YOUTUBE_WEEK();
    $LIST_TABLES_WEEKS = $q->LIST_TABLES_WEEKS();
    $LIST_TABLES_MEMBERS = $q->LIST_TABLES_MEMBERS();
    $LIST_TABLES_GSIZE = $q->LIST_TABLES_GSIZE();
    $LIST_TABLES_GCACHE = $q->LIST_TABLES_GCACHE();
    $LIST_TABLES_VISITED = $q->LIST_TABLES_VISITED();
    $LIST_TABLES_BLOCKED = $q->LIST_TABLES_BLOCKED();
    $LIST_TABLES_BLOCKED_WEEK = $q->LIST_TABLES_BLOCKED_WEEK();
    $LIST_TABLES_BLOCKED_DAY = $q->LIST_TABLES_BLOCKED_DAY();
    $LIST_TABLES_WWWUID = $q->LIST_TABLES_WWWUID();
    $LIST_CAT_FAMDAY = $q->LIST_CAT_FAMDAY();
    while (list($tablename, $none) = each($LIST_TABLES_YOUTUBE_HOURS)) {
        if (trim($tablename) == null) {
            continue;
        }
        $EXPORT_SOURCES[$tablename] = true;
    }
    while (list($tablename, $none) = each($LIST_TABLES_SIZEHOURS)) {
        if (trim($tablename) == null) {
            continue;
        }
        $EXPORT_SOURCES[$tablename] = true;
    }
    while (list($tablename, $none) = each($LIST_TABLES_QUOTADAY)) {
        if (trim($tablename) == null) {
            continue;
        }
        $EXPORT_SOURCES[$tablename] = true;
    }
    while (list($tablename, $none) = each($LIST_TABLES_QUOTAMONTH)) {
        if (trim($tablename) == null) {
            continue;
        }
        $EXPORT_SOURCES[$tablename] = true;
    }
    while (list($tablename, $none) = each($LIST_TABLES_SEARCHWORDS_HOURS)) {
        if (trim($tablename) == null) {
            continue;
        }
        $EXPORT_SOURCES[$tablename] = true;
    }
    while (list($tablename, $none) = each($LIST_TABLES_SEARCHWORDS_DAY)) {
        if (trim($tablename) == null) {
            continue;
        }
        $EXPORT_SOURCES[$tablename] = true;
    }
    while (list($tablename, $none) = each($LIST_TABLES_QUOTA_HOURS)) {
        if (trim($tablename) == null) {
            continue;
        }
        $EXPORT_SOURCES[$tablename] = true;
    }
    while (list($tablename, $none) = each($LIST_TABLES_dansguardian_events)) {
        if (trim($tablename) == null) {
            continue;
        }
        $EXPORT_SOURCES[$tablename] = true;
    }
    while (list($tablename, $none) = each($LIST_TABLES_HOURS)) {
        if (trim($tablename) == null) {
            continue;
        }
        $EXPORT_SOURCES[$tablename] = true;
    }
    while (list($tablename, $none) = each($LIST_TABLES_USERSIZED)) {
        if (trim($tablename) == null) {
            continue;
        }
        $EXPORT_SOURCES[$tablename] = true;
    }
    while (list($tablename, $none) = each($LIST_TABLES_BLOCKED_WEEK)) {
        if (trim($tablename) == null) {
            continue;
        }
        $EXPORT_SOURCES[$tablename] = true;
    }
    while (list($tablename, $none) = each($LIST_TABLES_BLOCKED)) {
        if (trim($tablename) == null) {
            continue;
        }
        $EXPORT_SOURCES[$tablename] = true;
    }
    while (list($tablename, $none) = each($LIST_TABLES_YOUTUBE_WEEK)) {
        if (trim($tablename) == null) {
            continue;
        }
        $EXPORT_SOURCES[$tablename] = true;
    }
    while (list($tablename, $none) = each($LIST_TABLES_WEEKS)) {
        if (trim($tablename) == null) {
            continue;
        }
        $EXPORT_SOURCES[$tablename] = true;
    }
    while (list($tablename, $none) = each($LIST_TABLES_MEMBERS)) {
        if (trim($tablename) == null) {
            continue;
        }
        $EXPORT_SOURCES[$tablename] = true;
    }
    while (list($tablename, $none) = each($LIST_TABLES_GSIZE)) {
        if (trim($tablename) == null) {
            continue;
        }
        $EXPORT_SOURCES[$tablename] = true;
    }
    while (list($tablename, $none) = each($LIST_TABLES_GCACHE)) {
        if (trim($tablename) == null) {
            continue;
        }
        $EXPORT_SOURCES[$tablename] = true;
    }
    while (list($tablename, $none) = each($LIST_TABLES_VISITED)) {
        if (trim($tablename) == null) {
            continue;
        }
        $EXPORT_SOURCES[$tablename] = true;
    }
    while (list($tablename, $none) = each($LIST_TABLES_BLOCKED_DAY)) {
        if (trim($tablename) == null) {
            continue;
        }
        $EXPORT_SOURCES[$tablename] = true;
    }
    while (list($tablename, $none) = each($LIST_CAT_FAMDAY)) {
        if (trim($tablename) == null) {
            continue;
        }
        $EXPORT_SOURCES[$tablename] = true;
    }
    while (list($tablename, $none) = each($LIST_TABLES_WWWUID)) {
        if (trim($tablename) == null) {
            continue;
        }
        $EXPORT_SOURCES[$tablename] = true;
    }
    while (list($tablename, $none) = each($EXPORT_SOURCES)) {
        if (trim($tablename) == null) {
            continue;
        }
        $EXPORT_DESTINATIONS[] = $tablename;
    }
    @mkdir($ArticaProxyStatisticsBackupFolder, 0755, true);
    $target_file = $ArticaProxyStatisticsBackupFolder . "/" . time() . ".sql.gz";
    if (is_file($target_file)) {
        @unlink($target_file);
    }
    $EXPORT_DESTINATIONS[] = "visited_sites";
    $EXPORT_DESTINATIONS[] = "youtube_objects";
    $EXPORT_DESTINATIONS[] = "UserAgents";
    $EXPORT_DESTINATIONS[] = "UserAutDB";
    $EXPORT_DESTINATIONS[] = "UserAuthDays";
    $EXPORT_DESTINATIONS[] = "UserAuthDaysGrouped";
    $EXPORT_DESTINATIONS[] = "UserSizeRTT";
    $EXPORT_DESTINATIONS[] = "allsizes";
    $unix = new unix();
    $mysqldump = $unix->find_program("mysqldump");
    $bzip2 = $unix->find_program("bzip2");
    $bzip2_cmd = "| {$bzip2} ";
    $AllTables = @implode(" ", $EXPORT_DESTINATIONS);
    $cmd = "{$mysqldump} -S /var/run/mysqld/squid-db.sock --single-transaction --skip-add-drop-table --no-create-db --insert-ignore --skip-add-locks --skip-lock-tables squidlogs {$AllTables} {$bzip2_cmd}> {$target_file} 2>&1";
    $t = time();
    $failed = false;
    if ($GLOBALS["VERBOSE"]) {
        echo $cmd . "\n";
    }
    exec($cmd, $results);
    while (list($index, $line) = each($results)) {
        if (preg_match("#Couldn't#", $line)) {
            @unlink($target_file);
            stats_admin_events(0, "Exporting tables failed {$line} took:" . $unix->distanceOfTimeInWords($t, time()), null, __FILE__, __LINE__);
            return;
        }
        if (preg_match("#Error\\s+([0-9]+)#", $line)) {
            @unlink($target_file);
            stats_admin_events(0, "Exporting tables failed {$line} took:" . $unix->distanceOfTimeInWords($t, time()), null, __FILE__, __LINE__);
            return;
        }
        echo "{$line}\n";
    }
    $size = @filesize($target_file);
    if ($size < 10000) {
        @unlink($target_file);
        stats_admin_events(0, "Exporting tables failed {$size}Bytes < 10000bytes took:" . $unix->distanceOfTimeInWords($t, time()), null, __FILE__, __LINE__);
        return;
    }
    if ($GLOBALS["VERBOSE"]) {
        echo "{$target_file} {$size}Bytes " . FormatBytes($size / 1024) . "\n";
    }
    reset($EXPORT_SOURCES);
    while (list($tablename, $none) = each($EXPORT_SOURCES)) {
        $q->QUERY_SQL("DROP TABLE {$tablename}");
        if ($GLOBALS["VERBOSE"]) {
            echo "Removing table {$tablename}\n";
        }
    }
}
function remove_numeric_members()
{
    $unix = new unix();
    $pidfile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".pid";
    $pid = @file_get_contents($pidfile);
    if ($pid < 100) {
        $pid = null;
    }
    if ($unix->process_exists($pid, basename(__FILE__))) {
        $timepid = $unix->PROCCESS_TIME_MIN($pid);
        ufdbguard_admin_events("Already executed pid {$pid} since {$timepid}", __FUNCTION__, __FILE__, __LINE__, "reports");
        if ($GLOBALS["VERBOSE"]) {
            echo "Already executed pid {$pid}\n";
        }
        return;
    }
    $q = new mysql_squid_builder();
    $q->QUERY_SQL("DELETE FROM members_uid WHERE uid REGEXP '^[0-9]+\$'");
    $q->QUERY_SQL("DELETE FROM UserAuthDaysGrouped WHERE uid REGEXP '^[0-9]+\$'");
    $q->QUERY_SQL("DELETE FROM UserAutDB WHERE uid REGEXP '^[0-9]+\$'");
    $tables = $q->LIST_TABLES_WWWUID();
    while (list($tablename, $ligne) = each($tables)) {
        if (!preg_match("#^www_[0-9]+\$#", $ligne)) {
            continue;
        }
        $q->QUERY_SQL("DROP TABLE {$tablename}");
    }
    $tables = $q->LIST_TABLES_DAYS();
    while (list($tablename, $ligne) = each($tables)) {
        $q->QUERY_SQL("DELETE FROM {$tablename} WHERE uid REGEXP '^[0-9]+\$'");
    }
    $tables = $q->LIST_TABLES_WEEKS();
    while (list($tablename, $ligne) = each($tables)) {
        $q->QUERY_SQL("DELETE FROM {$tablename} WHERE uid REGEXP '^[0-9]+\$'");
    }
    $tables = $q->LIST_TABLES_MONTH();
    while (list($tablename, $ligne) = each($tables)) {
        $q->QUERY_SQL("DELETE FROM {$tablename} WHERE uid REGEXP '^[0-9]+\$'");
    }
    $tables = $q->LIST_TABLES_MEMBERS();
    while (list($tablename, $ligne) = each($tables)) {
        $q->QUERY_SQL("DELETE FROM {$tablename} WHERE uid REGEXP '^[0-9]+\$'");
    }
}
function start($xtime = 0)
{
    build_progress("Loading...", 10);
    $unix = new unix();
    if ($GLOBALS["VERBOSE"]) {
        "echo Loading done...\n";
    }
    $pidfile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".pid";
    $timefile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".time";
    if ($GLOBALS["VERBOSE"]) {
        echo "Timefile = {$timefile}\n";
    }
    $pid = @file_get_contents($pidfile);
    if (!$GLOBALS["FORCE"]) {
        if ($pid < 100) {
            $pid = null;
        }
        $unix = new unix();
        if ($unix->process_exists($pid, basename(__FILE__))) {
            if ($GLOBALS["VERBOSE"]) {
                echo "Already executed pid {$pid}\n";
            }
            return;
        }
        $timeexec = $unix->file_time_min($timefile);
        if (!$GLOBALS["SCHEDULED"]) {
            if ($timeexec < 2880) {
                return;
            }
        }
        $mypid = getmypid();
        @file_put_contents($pidfile, $mypid);
    }
    if ($GLOBALS["RESTART"]) {
        REBUILD_FULL_MEMBERS();
        exit;
    }
    @unlink($timefile);
    @file_put_contents($timefile, time());
    $php = $unix->LOCATE_PHP5_BIN();
    if (!$GLOBALS["VERBOSE"]) {
        build_progress("Execute exec.squid.stats.members.hours.php {please_wait}", 20);
        $repaircmd = "{$php} /usr/share/artica-postfix/exec.squid.stats.members.hours.php --repair --verbose --force 2>&1";
        system($repaircmd);
    }
    $q = new mysql_squid_builder();
    if (!$q->FIELD_EXISTS("tables_day", "usersday")) {
        $q->QUERY_SQL("ALTER TABLE `tables_day` ADD `usersday` TINYINT( 1 ) NOT NULL DEFAULT '0', ADD INDEX ( `usersday` )");
    }
    $sql = "SELECT tablename, usersday,DATE_FORMAT(zDate,'%Y%m%d') AS `suffix` FROM tables_day \n\t\t\tWHERE usersday=0 AND DAY(zDate)<DAY(NOW()) AND YEAR(zDate) = YEAR(NOW()) AND MONTH(zDate) = MONTH(NOW())";
    $q->CheckTables();
    $results = $q->QUERY_SQL($sql);
    if (!$q->ok) {
        echo $q->mysql_error;
        build_progress("MySQL error", 110);
        return;
    }
    build_progress("tables_day =  " . mysql_num_rows($results) . " rows {please_wait}", 30);
    if ($GLOBALS["VERBOSE"]) {
        echo mysql_num_rows($results) . " rows\n";
    }
    while ($ligne = @mysql_fetch_array($results, MYSQL_ASSOC)) {
        $worktable = "{$ligne["suffix"]}_members";
        $SourceTable = "{$ligne["suffix"]}_hour";
        if (!$q->TABLE_EXISTS($worktable)) {
            continue;
        }
        $desttable = "{$ligne["suffix"]}_users";
        build_progress("{$desttable} {please_wait}", 30);
        if (!perform($worktable, $desttable)) {
            continue;
        }
        $q->QUERY_SQL("UPDATE tables_day SET usersday=1 WHERE tablename='{$ligne["tablename"]}'");
    }
    $currentdestable = date("Ymd") . "_users";
    if (!$q->CreateUsersTempTable()) {
        build_progress("CreateUsersTempTable failed", 110);
        return false;
    }
    if (!$q->CreateUsersFullTable()) {
        build_progress("CreateUsersFullTable failed", 110);
        return false;
    }
    build_progress("Empty working tables", 40);
    $q->QUERY_SQL("TRUNCATE TABLE UsersTMP");
    $q->QUERY_SQL("TRUNCATE TABLE UsersToTal");
    $LIST_TABLES_MEMBERS = $q->LIST_TABLES_MEMBERS();
    while (list($tablename, $ligne) = each($LIST_TABLES_MEMBERS)) {
        $xtime = $q->TIME_FROM_DAY_TABLE($tablename);
        $desttable = date("Ymd", $xtime) . "_users";
        build_progress("Scanning {$tablename} -> {$desttable}", 50);
        if ($desttable == $currentdestable) {
            continue;
        }
        if (!$q->TABLE_EXISTS($desttable)) {
            if (!perform($tablename, $desttable)) {
                continue;
            }
        }
        MONTH_TABLE_FROM_MEMBER_DAY($tablename);
        CONDENSED_perform($desttable);
    }
    CONDENSED();
    build_progress("Empty table UsersTMP", 90);
    $q->QUERY_SQL("TRUNCATE TABLE UsersTMP");
    build_progress("{done}", 100);
}