<?php
include("connect.php");
$result_refresh=$dbh->query("select * from refresh where type='1'");
$row_refresh=$result_refresh->fetchObject();
$count_refresh=$result_refresh->rowCount();

if($count_refresh>0){

// 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 recursivly
function ftp_sync($dir, $conn_id) {
	if ($dir !== '.') {
		if (ftp_chdir($conn_id, $dir) === FALSE) {
			echo 'Change dir failed: ' . $dir . PHP_EOL;
			return;
		}
		
	}
	$contents = ftp_nlist($conn_id, '.');
	foreach ($contents as $file) {
		if ($file == '.' || $file == '..') {
			continue;
		}
		if (@ftp_chdir($conn_id, $file)) {
			ftp_chdir($conn_id, "..");
			ftp_sync($file, $conn_id);
		} else {
			ftp_get($conn_id, $file, $file, 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 'start copying....' . PHP_EOL;
$conn_id = ftp_connect($ftp_server);
if ($conn_id) {
	$login_result = ftp_login($conn_id, $user, $password);
	if ($login_result) {
		ftp_chdir($conn_id, $document_root);
		ftp_sync("../".$sync_path, $conn_id);
		ftp_close($conn_id);
	} else {
		echo 'login to server failed!' . PHP_EOL;
	}
} else {
	echo 'connection to server failed!';
}
echo 'done.' . PHP_EOL;

}else{
echo 0;
}
?>