The code consists of two files, one will be placed on the source server and the other will be placed on the host server. I will call the file as "mirror-agent.php" which will be placed on the remote ftp server. The file which will be placed on the host server, i will call that as "ftp-mirror.php". Below is the code of these files.
mirror-agent.php
- Code: Select all
<?php
// disable errors ( files can be unreadable by PHP due to permissions )
error_reporting(0);
// disable time limit ( you never know how big this site is )
set_time_limit(0);
// convert all files to hex
function recur_dir($dir)
{
$dirlist = opendir($dir);
while ($file = readdir ($dirlist))
{
if ($file != '.' && $file != '..' && $file != '.DS_Store' && $file != 'mirror-agent.php')
{
$newpath = $dir.'/'.$file;
$permissions = substr(decoct(fileperms($newpath)),-4);
if (is_dir($newpath))
{
echo 'dir|'.$newpath.'|'.$permissions."\n";
recur_dir($newpath);
}
else
{
echo 'file|'.$newpath.'|'.$permissions.'|'.bin2hex(file_get_contents($newpath))."\n";
}
}
}
closedir($dirlist);
return $mod_array;
}
echo "mirror-agent starting...\n";
recur_dir('.');
?>
ftp-mirror.php
- Code: Select all
<?php
/* --------------------- configuration ---------------------- */
// set this to the URL of the mirror-agent.php on the server you want to mirror
$mirror_agent = 'http://www.server.com/mirror-agent.php';
/* --------------------- functions -------------------------- */
// convert hex to binary
function hex2asc($temp)
{
for ($i = 0; $i < strlen($temp); $i += 2)
$data .= chr(hexdec(substr($temp,$i,2)));
return $data;
}
// write file to disk
function file_write($filename, $filecontent, $mode='wb')
{
if($fp = fopen($filename,$mode))
{
fwrite($fp, $filecontent);
fclose($fp);
return true;
}
else
{
return false;
}
}
/* --------------------- the code --------------------------- */
// disable errors ( just in case you try this script before you read the usage above )
//error_reporting(E_ALL);
@ini_set('display_errors', 0);
// disable time limit ( you never know how big this site is )
set_time_limit(0);
// get the hex data from mirror
//$cont = file_get_contents($mirror_agent); // uncomment if you don't want to use curl
$sHandle = curl_init($mirror_agent);
curl_setopt($sHandle, CURLOPT_HEADER, FALSE);
curl_setopt($sHandle, CURLOPT_RETURNTRANSFER, TRUE);
$sResponse = curl_exec($sHandle);
curl_close ($sHandle);
$files = explode("\n", $sResponse);
// check if it's really the mirror_agent and if we have write permission
if(trim($files[0]) == 'mirror-agent starting...&& is_writeable(dirname(__file__)))
{
$count = count($files);
// yes - start copying
for($i = 1; $i < $count; $i++)
{
if(trim($files[$i]) != '')
{
$parts = explode('|',trim($files[$i]));
$permissions = intval($parts[2], 8);
if($parts[0] == 'dir')
{
// it's a directory - make it
mkdir($parts[1],$permissions);
$done[] = $permissions.'|'.$parts[1];
}
elseif($parts[0] == 'file')
{
// it's a file - write it down
file_write($parts[1], hex2asc($parts[3]));
chmod($parts[1],$permissions);
$done[] = $permissions.'|'.$parts[1];
}
}
}
// output status
echo '
<pre>
following files were copied:';
for($i = 0; $i < count($done); $i++)
{
echo '
'.$done[$i];
}
echo '
</pre>';
}
else
{
// error - it's not the mirror_agent, or you didn't chmod this directory
echo '
<pre>
ERROR:
'.$mirror_agent.'
does not respond like a mirror-agent should or
the directory '.dirname(__file__).' is not writeable.
</pre>';
}
?>
Just make one change in the "ftp-mirror.php" file that set the path of the "mirror-agent.php" in the top of the file. Then place the files on the server and execute this script, hopefully you will not face any error.
Any comments or questions are welcome