| 1 |
<?php |
| 2 |
|
| 3 |
class UpdraftPlus_BackupModule_ftp { |
| 4 |
|
| 5 |
function backup($backup_array) { |
| 6 |
|
| 7 |
global $updraftplus; |
| 8 |
|
| 9 |
if( !class_exists('ftp_wrapper')) require_once(UPDRAFTPLUS_DIR.'/includes/ftp.class.php'); |
| 10 |
|
| 11 |
$server = UpdraftPlus_Options::get_updraft_option('updraft_server_address'); |
| 12 |
$ftp = new ftp_wrapper($server , UpdraftPlus_Options::get_updraft_option('updraft_ftp_login'), UpdraftPlus_Options::get_updraft_option('updraft_ftp_pass')); |
| 13 |
$ftp->passive = true; |
| 14 |
|
| 15 |
if (!$ftp->connect()) { |
| 16 |
$ftp->ssl = true; |
| 17 |
if (!$ftp->connect()) { |
| 18 |
$updraftplus->log("FTP Failure: we did not successfully log in with those credentials."); |
| 19 |
$updraftplus->error("FTP login failure"); |
| 20 |
return false; |
| 21 |
} |
| 22 |
} |
| 23 |
|
| 24 |
//$ftp->make_dir(); we may need to recursively create dirs? TODO |
| 25 |
|
| 26 |
$ftp_remote_path = trailingslashit(UpdraftPlus_Options::get_updraft_option('updraft_ftp_remote_path')); |
| 27 |
foreach($backup_array as $file) { |
| 28 |
$fullpath = trailingslashit(UpdraftPlus_Options::get_updraft_option('updraft_dir')).$file; |
| 29 |
$updraftplus->log("FTP upload attempt: $file -> ftp://$server/${ftp_remote_path}${file}"); |
| 30 |
$timer_start = microtime(true); |
| 31 |
$size_k = round(filesize($fullpath)/1024,1); |
| 32 |
if ($ftp->put($fullpath, $ftp_remote_path.$file, FTP_BINARY)) { |
| 33 |
$updraftplus->log("FTP upload attempt successful (".$size_k."Kb in ".(round(microtime(true)-$timer_start,2)).'s)'); |
| 34 |
$updraftplus->uploaded_file($file); |
| 35 |
} else { |
| 36 |
$updraftplus->log("ERROR: FTP upload failed" ); |
| 37 |
$updraftplus->error("FTP upload failed" ); |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
$updraftplus->prune_retained_backups("ftp", $this, array('ftp_object' => $ftp, 'ftp_remote_path' => $ftp_remote_path)); |
| 42 |
} |
| 43 |
|
| 44 |
function delete($file, $ftparr) { |
| 45 |
global $updraftplus; |
| 46 |
$ftp = $ftparr['ftp_object']; |
| 47 |
$ftp_remote_path = $ftparr['ftp_remote_path']; |
| 48 |
if (@$ftp->delete($ftp_remote_path.$file)) { |
| 49 |
$updraftplus->log("FTP delete: succeeded (${ftp_remote_path}${file})"); |
| 50 |
} else { |
| 51 |
$updraftplus->log("FTP delete: failed (${ftp_remote_path}${file})"); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
function download($file) { |
| 56 |
if( !class_exists('ftp_wrapper')) require_once(UPDRAFTPLUS_DIR.'/includes/ftp.class.php'); |
| 57 |
|
| 58 |
//handle errors at some point TODO |
| 59 |
$ftp = new ftp_wrapper(UpdraftPlus_Options::get_updraft_option('updraft_server_address'),UpdraftPlus_Options::get_updraft_option('updraft_ftp_login'),UpdraftPlus_Options::get_updraft_option('updraft_ftp_pass')); |
| 60 |
$ftp->passive = true; |
| 61 |
|
| 62 |
if (!$ftp->connect()) { |
| 63 |
$ftp->ssl = true; |
| 64 |
if (!$ftp->connect()) { |
| 65 |
$updraftplus->log("FTP Failure: we did not successfully log in with those credentials."); |
| 66 |
$updraftplus->error("FTP login failure"); |
| 67 |
return false; |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
//$ftp->make_dir(); we may need to recursively create dirs? TODO |
| 72 |
|
| 73 |
$ftp_remote_path = trailingslashit(UpdraftPlus_Options::get_updraft_option('updraft_ftp_remote_path')); |
| 74 |
$fullpath = trailingslashit(UpdraftPlus_Options::get_updraft_option('updraft_dir')).$file; |
| 75 |
$ftp->get($fullpath, $ftp_remote_path.$file, FTP_BINARY); |
| 76 |
} |
| 77 |
|
| 78 |
function config_print_javascript_onready() { |
| 79 |
?> |
| 80 |
jQuery('#updraft-ftp-test').click(function(){ |
| 81 |
var data = { |
| 82 |
action: 'updraft_ajax', |
| 83 |
subaction: 'credentials_test', |
| 84 |
method: 'ftp', |
| 85 |
nonce: '<?php echo wp_create_nonce('updraftplus-credentialtest-nonce'); ?>', |
| 86 |
server: jQuery('#updraft_server_address').val(), |
| 87 |
login: jQuery('#updraft_ftp_login').val(), |
| 88 |
pass: jQuery('#updraft_ftp_pass').val(), |
| 89 |
path: jQuery('#updraft_ftp_remote_path').val() |
| 90 |
}; |
| 91 |
jQuery.post(ajaxurl, data, function(response) { |
| 92 |
alert('Settings test result: ' + response); |
| 93 |
}); |
| 94 |
}); |
| 95 |
<?php |
| 96 |
} |
| 97 |
|
| 98 |
function config_print() { |
| 99 |
?> |
| 100 |
<tr class="updraftplusmethod ftp"> |
| 101 |
<th>FTP Server:</th> |
| 102 |
<td><input type="text" size="40" id="updraft_server_address" name="updraft_server_address" value="<?php echo htmlspecialchars(UpdraftPlus_Options::get_updraft_option('updraft_server_address')); ?>" /> <em>Both SSL and non-SSL are supported</em></td> |
| 103 |
</tr> |
| 104 |
<tr class="updraftplusmethod ftp"> |
| 105 |
<th>FTP Login:</th> |
| 106 |
<td><input type="text" size="40" id="updraft_ftp_login" name="updraft_ftp_login" value="<?php echo htmlspecialchars(UpdraftPlus_Options::get_updraft_option('updraft_ftp_login')) ?>" /></td> |
| 107 |
</tr> |
| 108 |
<tr class="updraftplusmethod ftp"> |
| 109 |
<th>FTP Password:</th> |
| 110 |
<td><input type="text" size="40" id="updraft_ftp_pass" name="updraft_ftp_pass" value="<?php echo htmlspecialchars(UpdraftPlus_Options::get_updraft_option('updraft_ftp_pass')); ?>" /></td> |
| 111 |
</tr> |
| 112 |
<tr class="updraftplusmethod ftp"> |
| 113 |
<th>Remote Path:</th> |
| 114 |
<td><input type="text" size="64" id="updraft_ftp_remote_path" name="updraft_ftp_remote_path" value="<?php echo htmlspecialchars(UpdraftPlus_Options::get_updraft_option('updraft_ftp_remote_path')); ?>" /> <em>Needs to already exist</em></td> |
| 115 |
</tr> |
| 116 |
<tr class="updraftplusmethod ftp"> |
| 117 |
<th></th> |
| 118 |
<td><p><button id="updraft-ftp-test" type="button" class="button-primary" style="font-size:18px !important">Test FTP Login</button></p></td> |
| 119 |
</tr> |
| 120 |
<?php |
| 121 |
} |
| 122 |
|
| 123 |
function credentials_test() { |
| 124 |
|
| 125 |
$server = $_POST['server']; |
| 126 |
$login = $_POST['login']; |
| 127 |
$pass = $_POST['pass']; |
| 128 |
$path = $_POST['path']; |
| 129 |
|
| 130 |
if (empty($server)) { |
| 131 |
echo "Failure: No server details were given."; |
| 132 |
return; |
| 133 |
} |
| 134 |
if (empty($login)) { |
| 135 |
echo "Failure: No login was given."; |
| 136 |
return; |
| 137 |
} |
| 138 |
if (empty($pass)) { |
| 139 |
echo "Failure: No password was given."; |
| 140 |
return; |
| 141 |
} |
| 142 |
|
| 143 |
if( !class_exists('ftp_wrapper')) require_once(UPDRAFTPLUS_DIR.'/includes/ftp.class.php'); |
| 144 |
|
| 145 |
//handle SSL and errors at some point TODO |
| 146 |
$ftp = new ftp_wrapper($server, $login, $pass); |
| 147 |
$ftp->passive = true; |
| 148 |
|
| 149 |
if (!$ftp->connect()) { |
| 150 |
$ftp->ssl = true; |
| 151 |
if (!$ftp->connect()) { |
| 152 |
echo "Failure: we did not successfully log in with those credentials."; |
| 153 |
return; |
| 154 |
} |
| 155 |
} |
| 156 |
//$ftp->make_dir(); we may need to recursively create dirs? TODO |
| 157 |
|
| 158 |
$file = md5(rand(0,99999999)).'.tmp'; |
| 159 |
$fullpath = trailingslashit($path).$file; |
| 160 |
if (!file_exists(ABSPATH.'wp-includes/version.php')) { |
| 161 |
echo "Failure: an unexpected internal UpdraftPlus error occurred when testing the credentials - please contact the developer"; |
| 162 |
return; |
| 163 |
} |
| 164 |
if ($ftp->put(ABSPATH.'wp-includes/version.php', $fullpath, FTP_BINARY)) { |
| 165 |
echo "Success: we successfully logged in, and confirmed our ability to create a file in the given directory."; |
| 166 |
@$ftp->delete($fullpath); |
| 167 |
} else { |
| 168 |
echo "Failure: we successfully logged in, but were not able to create a file in the given directory."; |
| 169 |
} |
| 170 |
|
| 171 |
} |
| 172 |
|
| 173 |
} |
| 174 |
|
| 175 |
?> |