<?php
//step1
$cSession = curl_init(); 
//step2
curl_setopt($cSession,CURLOPT_URL,"http://apps.kinglinepress.com/ksync/fetchact.php");
curl_setopt($cSession,CURLOPT_RETURNTRANSFER,true);
curl_setopt($cSession,CURLOPT_HEADER, false); 
//step3
$result=curl_exec($cSession);
//step4
curl_close($cSession);
//step5
//echo $result;

if ($result=="1"){

echo $result;
//pull the files then

	// ftp_sync - copy directory and file structure
// based on http://www.php.net/manual/es/function.ftp-get.php#90910
// main function witch is called recursively
function ftp_sync($dir, $conn_id) {
	if ($dir !== '.') {
		if (ftp_chdir($conn_id, $dir) === FALSE) {
			echo 'Change dir failed: ' . $dir . PHP_EOL;
			return;
		}
		if (!(is_dir($dir))) {
			//mkdir($dir);
		}
		//chdir($dir);
	}
	$contents = ftp_nlist($conn_id, '.');

	foreach ($contents as $filer) {
		if ($filer == '.' || $filer == '..') {
			continue;}
		
		if (@ftp_chdir($conn_id, $filer)) {
			ftp_chdir($conn_id, "..");
			ftp_sync($filer, $conn_id);
		} else {
			ftp_get($conn_id, $filer, $filer, FTP_BINARY);
		}
	}
	ftp_chdir($conn_id, '..');
	chdir('..');
}
// your settings
$ftp_server    = 'ftp.kinglinepress.com';
$user          = 'ksync@apps.kinglinepress.com';
$password      = '0703611691k1Kq';
$document_root = '';
$sync_path     = 'winterfell';
// start copying
echo '<pre>' . PHP_EOL;
echo 'We sent a raven' . PHP_EOL;

$conn_id = ftp_connect($ftp_server);

if ($conn_id) {
	$login_result = ftp_login($conn_id, $user, $password);
	ftp_pasv($conn_id,true);
	if ($login_result) {
		ftp_chdir($conn_id, $document_root);
		ftp_sync("../".$sync_path, $conn_id);
		ftp_close($conn_id);
	} else {
		echo 'actually, we couldnt even cross castle black!' . PHP_EOL;
	}
} else {
	echo '!';
}
echo 'We even met Jon Snow.' . PHP_EOL;

}
else {echo "There are no ravens";}
?>