Location: PHPKode > projects > Zitro-Stats > include/update_sync.inc.php
<?php

	// ANTIHACK
	defined('INSTATS') AND defined('UPDATE') OR die();

	fecho("\n==[ SYNCHRONIZE ]====================================\n");

	$killlogs	= array();
	$delete		= array();

	// GET SERVERS
	$i = 0;
	$limit = intval(@$_GET['limit']);
	$result = $sql->query("
		SELECT
			`server`,
			`name`,
			`ip`,
			`port`,
			`logdir`
		FROM `$options[SQL_TABPREFIX]servers`
		WHERE `enabled`
		");
	while(list($server,$name,$ip,$port,$logdir) = $sql->fetchrow($result)) {
		fecho(" server $server ($ip:$port) $name\n");

		if(isset($serverdays[$server]))		$serverdays[$server]	= array();
		if(isset($serverhours[$server]))	$serverhours[$server]	= array();

		if(substr($logdir,-1)!="/")	// add trailing slash
			$logdir .= "/";

		$type = $logdir ? (substr($logdir,0,6)=='ftp://' ? 'FTP' : (substr($logdir,0,7)=='http://' ? 'HTTP' : 'LOCAL')) : '';
		$new = array();
		$http = array();
		$changed = array();

		// SCAN DIRECTORIES
		if($type=='LOCAL') {
			if(!@chdir($logdir.'kills/')) {
				fecho("  ERROR: chdir failed!\n");
				continue;
			}
			if(!$dir = @opendir('.')) {
				fecho("  ERROR: opendir failed!\n");
				continue;
			}
			while($file = readdir($dir))
				if(is_file($file) && $size=@filesize($file))
					$new[$file] = $size;
			closedir($dir);
		} elseif($type=='HTTP') {
			$list = file($logdir.'kills/?C=M;O=A');
			foreach($list as $line) {
				if(preg_match('/<a href=\"(.*)\">(.*)<\/a>.*(\d{2})-(\w{3})-(\d{4}) (\d{2}):(\d{2})/i',$line,$line,0) && $line[1]==$line[2]) {
					switch($line[4]) {
						case 'Jan': $line[4]= 1; break;
						case 'Feb': $line[4]= 2; break;
						case 'Mar': $line[4]= 3; break;
						case 'Apr': $line[4]= 4; break;
						case 'May': $line[4]= 5; break;
						case 'Jun': $line[4]= 6; break;
						case 'Jul': $line[4]= 7; break;
						case 'Aug': $line[4]= 8; break;
						case 'Sep': $line[4]= 9; break;
						case 'Oct': $line[4]=10; break;
						case 'Nov': $line[4]=11; break;
						case 'Dec': $line[4]=12; break;
					}
					$line[2] = mktime($line[6],$line[7],0,$line[4],$line[3],$line[5]);
					$new[$line[1]] = $line[2];
					$http[$line[1]] = $line[2];
				}
			}
		} elseif($type=='FTP') {
			if(!preg_match('/^ftp:\/\/(?:([^@:]+)(?::([^@]*))?@)?([^:\/]+)(?::([0-9]+))?(\/.*)?$/',$logdir,$reg,0) OR ($reg[4] && ($reg[4]<1 || $reg[4]>65535))) {
				fecho("  ERROR: invalid ftp address!\n");
				continue;
			}
			if(!$ftp = @ftp_connect($reg[3],$reg[4]?$reg[4]:21)) {
				fecho("  ERROR: ftp_connect failed!\n");
				continue;
			}
			if(!@ftp_login($ftp,urldecode($reg[1]),urldecode($reg[2]))) {
				fecho("  ERROR: ftp_login failed!\n");
				continue;
			}
			ftp_pasv($ftp,true);
			if(!@ftp_chdir($ftp,urldecode($reg[5]).'kills/')) {
				fecho("  ERROR: ftp_chdir failed!\n");
				continue;
			}
			$list = ftp_rawlist($ftp,'.');
			foreach($list as $line) {
				if(preg_match('/^-r[w-][x-]r[w-][x-]r[w-][x-]\s+\d+\s+\S+\s+\S+\s+(\d+)\s\S+\s+\d+\s+\d+:\d+\s(.*)$/',$line,$line,0) && $line[1]>0)
					$new[$line[2]] = $line[1];
			}
		}
		uksort($new,"strnatcmp");
		fecho("  log files: ".count($new)." total");

		// CHECK FOR CHANGED LOGS
		$result2 = $sql->query("
			SELECT
				`log`,
				`size`,
				`offset`
			FROM `$options[SQL_TABPREFIX]logs`
			WHERE `server`=$server
			");
		while(list($log,$size,$offset) = $sql->fetchrow($result2)) {
			// IF LOG STILL IN LOGDIR
			if(isset($new[$log])) {
				// IF SIZE HAS CHANGED
				if($new[$log]!=$size) {
					$changed[$log] = $size>$new[$log] ? 0 : $offset;
				}
				// REMOVE LOG FROM ARRAY
				unset($new[$log]);
			// IF LOG NOT LONGER IN LOGDIR
			} else {
				$delete[$server][] = $sql->escape($log);
			}
		}
		fecho(", ".count($changed)." changed");

		// NEW LOGS
		$new = array_keys($new);
		fecho(", ".count($new)." new\n");
		foreach($new as $log)
			$changed[$log] = 0;

		// PROCESS CHANGED
		foreach($changed as $log=>$offset) {
			if($limit && ++$i>$limit) break;
			if($type=='LOCAL') {
				$killlogs[] = new Killlog($server,$log,$logdir.'kills/'.$log,$offset);
			} elseif($type=='HTTP') {
				$temp = "temp/http-${server}-${log}";
				if(!$offset) @unlink($temp);
				fecho("  caching $log");
				if(!@copy($logdir.'kills/'.$log,$temp)) {
					fecho("  ERROR: copy for $log failed!\n");
					continue;
				}
				$killlogs[] = new Killlog($server,$log,$temp,$offset,$http[$log]);
				fecho((count($changed)-1?repeat(" ",31,$log,count($changed)-1).(count($changed)-1)." left":repeat(" ",33,$log)."done")."\n");
			} elseif($type=='FTP') {
				$temp = "temp/ftp-${server}-${log}";
				if(!$offset) @unlink($temp);
				if(!@touch($temp)) {
					fecho("  ERROR: touch for $log failed!\n");
					continue;
				}
				fecho("  caching $log");
				if(!@ftp_get($ftp,$temp,$log,FTP_BINARY,$offset)) {
					fecho("\n  ERROR: ftp_get for $log failed!\n");
					continue;
				}
				$killlogs[] = new Killlog($server,$log,$temp,$offset);
				fecho((count($changed)-1?repeat(" ",31,$log,count($changed)-1).(count($changed)-1)." left":repeat(" ",33,$log)."done")."\n");
			}
			unset($changed[$log]);
		}

		// RESET DIR / CLOSE CONNECTION
		if($type=='LOCAL')
			chdir($options['PATH_SELF']);
		elseif($type=='FTP')
			ftp_close($ftp);
	}

	// REMOVE ORPHAN FILES FROM DB
	if(count($delete)) {
		foreach($delete as $server=>$logs)
			$delete[$server] = "`server`=$server AND `log` IN ('".implode("','",$logs)."')";
		$sql->query("
			DELETE FROM `$options[SQL_TABPREFIX]logs`
			WHERE ".implode(" OR ",$delete)."
			");
	}

?>
Return current item: Zitro-Stats