";
}
//setup the transfer status and enqueue an FTP put file transfer
public function put_ftp_file(){
$file = $_GET['ftp_put_backup_file'];
$status['message'] = "$file is Queued for Transfer";
$status['code'] = "PENDING";
$status['percentage'] = 0;
update_option('simple-backup-file-transfer', $status);
$args['file'] = $_GET['ftp_put_backup_file'];
$file_transfer_time = current_time('timestamp', true);
wp_schedule_single_event($file_transfer_time, 'ftp_put_backup', $args);
echo "
✘ ERROR: Could not connect to FTP Server: {$this->server}
";
}
$files = array();
foreach($data[$this->directory] as $item){
$files[] = $item;
}
//print_r($files);
return $files;
}
//downloads backup file from FTP server and then relays it to browser as a file download
public function ftp_download_file($filename){
$url = "ftp://{$this->user}:{$this->pass}@{$this->server}/{$this->directory}/$filename";
clearstatcache();
$size = filesize($url);
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header("Content-Disposition: attachment; filename=$filename");
header("Content-Length: $size");
header('Connection: close');
ob_clean();
flush();
readfile($url);
die();
}
//deletes file from remote FTP server
public function delete_ftp_file($file){
$path = ABSPATH."simple-backup/".$file;
$connection = $this->connect();
if($connection === false){
die('Can not Connect to FTP Server');
}
if($login = $this->login($connection)){
$upload_path = $this->find_target_dir($connection);
if(ftp_delete($connection, $file )){
echo "
Successfully deleted $file from $upload_path
\n";
} else {
echo "
There was a problem while deleting $file from $upload_path
\n";
}
ftp_close($connection);
}
}
//Helper FTP Stuff
private function ftp_recursive_file_listing($ftp_connection, $path = ".") {
static $allFiles = array();
$contents = ftp_nlist($ftp_connection, $path);
foreach($contents as $currentFile) {
// assuming its a folder if there's no dot in the name
if (strpos($currentFile, '.') === false) {
$this->ftp_recursive_file_listing($ftp_connection, $currentFile);
}
$new_file['filename'] = substr($currentFile, strlen($path) + 1);
$new_file['size'] = ftp_size( $ftp_connection , $currentFile );
$new_file['date'] = ftp_mdtm( $ftp_connection , $currentFile );
$new_file['date']= date_i18n('Y-m-d g:i:s A T', $new_file['date']);
$allFiles[$path][] = $new_file;
}
return $allFiles;
}
private function connect(){
if("" == $this->server || "" == $this->port){
$link = admin_url()."options-general.php?page=simple-backup-settings&tab=ftp_server_settings";
$message = '
The FTP Server Settings need to be configured.';
$message .= ' Please Configure and Save the FTP Server Settings before you continue!!
';
echo sprintf($message, $link);
return false;
}
$ftp_connection = ftp_connect($this->server, $this->port);
if(false === $ftp_connection){
return false;
}else{
return $ftp_connection;
}
}
private function login($connection){
$ftp_login = @ftp_login($connection, $this->user, $this->pass);
if(true === $ftp_login){
return true;
}else{
return false;
}
}
private function find_target_dir($connection){
$dir = trim($this->directory, "/");
$dirs = explode("/", $dir);
$path = "/";
foreach($dirs as $dir){
$path = $path . $dir . "/";
if(ftp_chdir($connection, $dir)){
}
}
return $path;
}
private function timer(){
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
//AJAX Stuff
public function ajax_put_ftp_file($file){
//$this->put_large_ftp_file($_REQUEST['filename']);
die();
}
public function ajax_get_ftp_file($file){
//$this->get_large_ftp_file($_REQUEST['filename']);
die();
}
public function get_file_transfer_status(){
check_ajax_referer('simple_backup_ajax_nonce');
$status = get_option('simple-backup-file-transfer');
header('Content-Type: application/json');
echo json_encode($status);
die();
}
//CRON Stuff
//schedule FTP put file transfer
public function cron_put_ftp_file($file){
$this->clear_cron_last_run_time();
$this->put_large_ftp_file($file);
die();
}
//schedule FTP get file transfer
public function cron_get_ftp_file($file){
$this->clear_cron_last_run_time();
$this->get_large_ftp_file($file);
die();
}
//delete the transient option that keeps track of the last time cron was run.
//this will basically trigger cron to run when called
//otherwise it will only run once per minute at most
private function clear_cron_last_run_time(){
delete_transient('doing_cron');
}
}
?>