2010-11-04 15:40:50 +00:00
|
|
|
|
<?php
|
|
|
|
|
|
$sync = true;
|
|
|
|
|
|
$localDir = "projects";
|
|
|
|
|
|
$remoteFileSyncURL = "http://www.nada.kth.se/~ziver/sync.php";
|
|
|
|
|
|
$skip_filetype = array ("..",".");
|
|
|
|
|
|
$sessionPassword = "lol";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(isset($_GET["fileList"])){
|
|
|
|
|
|
if($_GET["fileList"] == "array") print_r(getFileArray($localDir));
|
|
|
|
|
|
else echo serialize(getFileArray($localDir));
|
|
|
|
|
|
}
|
|
|
|
|
|
else if(isset($_GET["copyid"]) && $sync){
|
|
|
|
|
|
session_start();
|
|
|
|
|
|
if($_SESSION['auth'] == $sessionPassword){
|
|
|
|
|
|
echo "
|
|
|
|
|
|
<html>
|
|
|
|
|
|
<body onload='stop = true;'>
|
|
|
|
|
|
<div id='load'>|</div>
|
|
|
|
|
|
<script language='javascript'>
|
|
|
|
|
|
var stop = false;
|
|
|
|
|
|
function load() {
|
|
|
|
|
|
var loading = document.getElementById('load');
|
|
|
|
|
|
if (loading.innerHTML == '-') {
|
|
|
|
|
|
loading.innerHTML = '\\\\';
|
|
|
|
|
|
}
|
|
|
|
|
|
else if(loading.innerHTML == '\\\\'){
|
|
|
|
|
|
loading.innerHTML = '|';
|
|
|
|
|
|
}
|
|
|
|
|
|
else if(loading.innerHTML == '|'){
|
|
|
|
|
|
loading.innerHTML = '/';
|
|
|
|
|
|
}
|
|
|
|
|
|
else if(loading.innerHTML == '/'){
|
|
|
|
|
|
loading.innerHTML = '-';
|
|
|
|
|
|
}
|
|
|
|
|
|
if(stop){
|
|
|
|
|
|
loading.innerHTML = 'OK';
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
setTimeout('load()',200);
|
|
|
|
|
|
}
|
|
|
|
|
|
load();
|
|
|
|
|
|
</script>
|
|
|
|
|
|
</body>
|
|
|
|
|
|
</html>
|
|
|
|
|
|
";
|
|
|
|
|
|
flush();
|
|
|
|
|
|
copyFile($_SESSION["id".$_GET["copyid"]]["copy"],$_SESSION["id".$_GET["copyid"]]["to"],$_SESSION["id".$_GET["copyid"]]["date"]);
|
|
|
|
|
|
}
|
|
|
|
|
|
else{
|
|
|
|
|
|
echo "<font color=red>ERROR</font>";
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else if($sync){
|
|
|
|
|
|
session_start();
|
|
|
|
|
|
$_SESSION['auth'] = $sessionPassword;
|
|
|
|
|
|
$sessionId = 0;
|
|
|
|
|
|
|
|
|
|
|
|
$remoteFileList = unserialize(file_get_contents($remoteFileSyncURL."?fileList"));
|
|
|
|
|
|
$localFileList = getFileArray($localDir);
|
|
|
|
|
|
|
|
|
|
|
|
echo "<center><b>KOC SYNC: </b>".$remoteFileSyncURL."<br /><table width='200' border='1' style='border:1px solid #CCCCCC; border-collapse:collapse; width:95%;'>";
|
|
|
|
|
|
foreach($remoteFileList as $remoteFile){
|
|
|
|
|
|
$temp = searchFile($remoteFile, $localFileList);
|
|
|
|
|
|
if($temp >= 0){
|
|
|
|
|
|
if($remoteFile[0] > $localFileList[$temp][0]){
|
|
|
|
|
|
$_SESSION["id".$sessionId]["copy"] = $remoteFile[2];
|
|
|
|
|
|
$_SESSION["id".$sessionId]["to"] = $localDir.$localFileList[$temp][1];
|
|
|
|
|
|
$_SESSION["id".$sessionId]["date"] = $remoteFile[0];
|
|
|
|
|
|
echo "<tr bgcolor='#FF6F5C'><td>Local File Old!</td><td><iframe src='sync.php?copyid=".$sessionId."' width='25' height='20' frameborder='0' marginwidth='0' marginheight='0'></iframe></td>";
|
|
|
|
|
|
$sessionId++;
|
|
|
|
|
|
//copyFile($remoteFile[2],$localDir.$localFileList[$temp][1], $remoteFile[0]);
|
|
|
|
|
|
}
|
|
|
|
|
|
else if($remoteFile[0] < $localFileList[$temp][0]){
|
|
|
|
|
|
echo "<tr bgcolor='#A9B4FF'><td>Local File Newer!</td><td></td>";
|
|
|
|
|
|
}
|
|
|
|
|
|
else{
|
|
|
|
|
|
echo "<tr><td>Synced!</td><td></td>";
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else{
|
|
|
|
|
|
$_SESSION["id".$sessionId]["copy"] = $remoteFile[2];
|
|
|
|
|
|
$_SESSION["id".$sessionId]["to"] = $localDir.$remoteFile[1];
|
|
|
|
|
|
$_SESSION["id".$sessionId]["date"] = $remoteFile[0];
|
|
|
|
|
|
echo "<tr bgcolor='#89FF8B'><td>New File!</td><td><iframe src='sync.php?copyid=".$sessionId."' width='25' height='20' frameborder='0' marginwidth='0' marginheight='0'></iframe></td>";
|
|
|
|
|
|
$sessionId++;
|
|
|
|
|
|
//copyFile($remoteFile[2],$localDir.$remoteFile[1],$remoteFile[0]);
|
|
|
|
|
|
}
|
|
|
|
|
|
echo "<td>".$remoteFile[2]."</td><td> => </td><td>".$remoteFile[1]."</td></tr>";
|
|
|
|
|
|
}
|
|
|
|
|
|
echo "</table></center>";
|
|
|
|
|
|
}
|
|
|
|
|
|
else {
|
|
|
|
|
|
echo"<center><font color=red>Sync disabled for this server!</font></center>";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function copyFile($source,$dest, $lastModDate = null){
|
|
|
|
|
|
@$file = fopen ($source, "rb");
|
|
|
|
|
|
if (!$file) {
|
|
|
|
|
|
echo"<font color=red>Failed to copy $source -> $dest!</font><br>";
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}else {
|
|
|
|
|
|
if(!file_exists(dirname($dest)))
|
|
|
|
|
|
mkdir(dirname($dest),0777,true);
|
|
|
|
|
|
$filename = basename($source);
|
|
|
|
|
|
$fc = fopen($dest, "wb");
|
|
|
|
|
|
echo "<font color=green>$source => $dest</font><br><font color=black>";
|
|
|
|
|
|
while (!feof ($file)) {
|
|
|
|
|
|
$line = fread ($file, 1028);
|
|
|
|
|
|
fwrite($fc,$line);
|
|
|
|
|
|
echo "#";
|
|
|
|
|
|
}
|
|
|
|
|
|
fclose($fc);
|
|
|
|
|
|
if($lastModDate != null) touch($dest,$lastModDate);
|
|
|
|
|
|
echo "</font><br>";
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$tempFilesArray = array();
|
|
|
|
|
|
|
|
|
|
|
|
function getFileArray($foldername) {
|
|
|
|
|
|
GLOBAL $localDir, $skip_filetype, $tempFilesArray;
|
|
|
|
|
|
|
|
|
|
|
|
$dirhandle = @opendir($foldername); //<2F>ppnar mappen
|
|
|
|
|
|
if($dirhandle !== false){ //gick det att <20>ppna mappen?
|
|
|
|
|
|
while($filename = readdir($dirhandle)){ //loopar igenom alla filer i mappen
|
|
|
|
|
|
$filepath = $foldername ."/". $filename;
|
|
|
|
|
|
if($filename != "." && $filename != ".." && is_dir($filepath)){ //<2F>r det en mapp?
|
|
|
|
|
|
getFileArray($filepath);
|
|
|
|
|
|
}
|
|
|
|
|
|
else{ // its a file
|
|
|
|
|
|
$filetype = ereg_replace("^.+\\.([^.]+)$", "\\1", $filename);
|
|
|
|
|
|
if (!in_array($filetype, $skip_filetype)) {
|
|
|
|
|
|
$tempFilesArray[] = array(filemtime($filepath),str_replace($localDir, "", $filepath),("http://".$_SERVER['SERVER_NAME'].str_replace("sync.php","",$_SERVER['SCRIPT_NAME']).$filepath),$filename);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return $tempFilesArray;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function searchFile($remoteFile, $localFileList){
|
|
|
|
|
|
if(isset($localFileList)){
|
|
|
|
|
|
foreach($localFileList as $key => $localFile){
|
|
|
|
|
|
if($localFile[1] == $remoteFile[1]){
|
|
|
|
|
|
return $key;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return -1;
|
|
|
|
|
|
}
|
|
|
|
|
|
?>
|