| 1 |
<?php |
| 2 |
/************************************************************* |
| 3 |
* |
| 4 |
* backup.class.php |
| 5 |
* |
| 6 |
* Manage Backups |
| 7 |
* |
| 8 |
* |
| 9 |
* Copyright (c) 2011 Prelovac Media |
| 10 |
* www.prelovac.com |
| 11 |
**************************************************************/ |
| 12 |
define('MWP_BACKUP_DIR', WP_CONTENT_DIR . '/managewp/backups'); |
| 13 |
define('MWP_DB_DIR', MWP_BACKUP_DIR . '/mwp_db'); |
| 14 |
|
| 15 |
$zip_errors = array( |
| 16 |
'No error', |
| 17 |
'No error', |
| 18 |
'Unexpected end of zip file', |
| 19 |
'A generic error in the zipfile format was detected.', |
| 20 |
'zip was unable to allocate itself memory', |
| 21 |
'A severe error in the zipfile format was detected', |
| 22 |
'Entry too large to be split with zipsplit', |
| 23 |
'Invalid comment format', |
| 24 |
'zip -T failed or out of memory', |
| 25 |
'The user aborted zip prematurely', |
| 26 |
'zip encountered an error while using a temp file', |
| 27 |
'Read or seek error', |
| 28 |
'zip has nothing to do', |
| 29 |
'Missing or empty zip file', |
| 30 |
'Error writing to a file', |
| 31 |
'zip was unable to create a file to write to', |
| 32 |
'bad command line parameters', |
| 33 |
'no error', |
| 34 |
'zip could not open a specified file to read' |
| 35 |
); |
| 36 |
$unzip_errors = array( |
| 37 |
'No error', |
| 38 |
'One or more warning errors were encountered, but processing completed successfully anyway', |
| 39 |
'A generic error in the zipfile format was detected', |
| 40 |
'A severe error in the zipfile format was detected.', |
| 41 |
'unzip was unable to allocate itself memory.', |
| 42 |
'unzip was unable to allocate memory, or encountered an encryption error', |
| 43 |
'unzip was unable to allocate memory during decompression to disk', |
| 44 |
'unzip was unable allocate memory during in-memory decompression', |
| 45 |
'unused', |
| 46 |
'The specified zipfiles were not found', |
| 47 |
'Bad command line parameters', |
| 48 |
'No matching files were found', |
| 49 |
50 => 'The disk is (or was) full during extraction', |
| 50 |
51 => 'The end of the ZIP archive was encountered prematurely.', |
| 51 |
80 => 'The user aborted unzip prematurely.', |
| 52 |
81 => 'Testing or extraction of one or more files failed due to unsupported compression methods or unsupported decryption.', |
| 53 |
82 => 'No files were found due to bad decryption password(s)' |
| 54 |
); |
| 55 |
|
| 56 |
|
| 57 |
class MMB_Backup extends MMB_Core |
| 58 |
{ |
| 59 |
var $site_name; |
| 60 |
var $statuses; |
| 61 |
var $tasks; |
| 62 |
var $s3; |
| 63 |
var $ftp; |
| 64 |
var $dropbox; |
| 65 |
function __construct() |
| 66 |
{ |
| 67 |
parent::__construct(); |
| 68 |
$this->site_name = str_replace(array( |
| 69 |
"_", |
| 70 |
"/" |
| 71 |
), array( |
| 72 |
"", |
| 73 |
"-" |
| 74 |
), rtrim($this->remove_http(get_bloginfo('url')), "/")); |
| 75 |
$this->statuses = array( |
| 76 |
'db_dump' => 1, |
| 77 |
'db_zip' => 2, |
| 78 |
'files_zip' => 3, |
| 79 |
's3' => 4, |
| 80 |
'dropbox' => 5, |
| 81 |
'ftp' => 6, |
| 82 |
'email' => 7, |
| 83 |
'finished' => 100 |
| 84 |
); |
| 85 |
$this->tasks = get_option('mwp_backup_tasks'); |
| 86 |
} |
| 87 |
|
| 88 |
function get_backup_settings() |
| 89 |
{ |
| 90 |
$backup_settings = get_option('mwp_backup_tasks'); |
| 91 |
if (!empty($backup_settings)) |
| 92 |
return $backup_settings; |
| 93 |
else |
| 94 |
return false; |
| 95 |
} |
| 96 |
|
| 97 |
function set_backup_task($params) |
| 98 |
{ |
| 99 |
//$params => [$task_name, $args, $error] |
| 100 |
if (!empty($params)) { |
| 101 |
|
| 102 |
//Make sure backup cron job is set |
| 103 |
if (!wp_next_scheduled('mwp_backup_tasks')) { |
| 104 |
wp_schedule_event( time(), 'tenminutes', 'mwp_backup_tasks' ); |
| 105 |
} |
| 106 |
|
| 107 |
extract($params); |
| 108 |
|
| 109 |
//$before = $this->get_backup_settings(); |
| 110 |
$before = $this->tasks; |
| 111 |
if (!$before || empty($before)) |
| 112 |
$before = array(); |
| 113 |
|
| 114 |
if (isset($args['remove'])) { |
| 115 |
unset($before[$task_name]); |
| 116 |
$return = array( |
| 117 |
'removed' => true |
| 118 |
); |
| 119 |
} else { |
| 120 |
if (isset($params['account_info']) && is_array($params['account_info'])) { //only if sends from master first time(secure data) |
| 121 |
$args['account_info'] = $account_info; |
| 122 |
} |
| 123 |
|
| 124 |
$before[$task_name]['task_args'] = $args; |
| 125 |
if (strlen($args['schedule'])) |
| 126 |
$before[$task_name]['task_args']['next'] = $this->schedule_next($args['type'], $args['schedule']); |
| 127 |
|
| 128 |
$return = $before[$task_name]; |
| 129 |
} |
| 130 |
|
| 131 |
//Update with error |
| 132 |
if (isset($error)) { |
| 133 |
if (is_array($error)) { |
| 134 |
$before[$task_name]['task_results'][count($before[$task_name]['task_results']) - 1]['error'] = $error['error']; |
| 135 |
} else { |
| 136 |
$before[$task_name]['task_results'][count($before[$task_name]['task_results'])]['error'] = $error; |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
if ($time) { //set next result time before backup |
| 141 |
if (is_array($before[$task_name]['task_results'])) { |
| 142 |
$before[$task_name]['task_results'] = array_values($before[$task_name]['task_results']); |
| 143 |
} |
| 144 |
$before[$task_name]['task_results'][count($before[$task_name]['task_results'])]['time'] = $time; |
| 145 |
} |
| 146 |
|
| 147 |
$this->update_tasks($before); |
| 148 |
//update_option('mwp_backup_tasks', $before); |
| 149 |
|
| 150 |
if ($task_name == 'Backup Now') { |
| 151 |
$result = $this->backup($args, $task_name); |
| 152 |
$backup_settings = $this->tasks; |
| 153 |
|
| 154 |
if (is_array($result) && array_key_exists('error', $result)) { |
| 155 |
$return = $result; |
| 156 |
} else { |
| 157 |
$return = $backup_settings[$task_name]; |
| 158 |
} |
| 159 |
} |
| 160 |
return $return; |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
return false; |
| 166 |
} |
| 167 |
|
| 168 |
//Cron check |
| 169 |
function check_backup_tasks() |
| 170 |
{ |
| 171 |
|
| 172 |
$this->check_cron_remove(); |
| 173 |
|
| 174 |
$settings = $this->tasks; |
| 175 |
if (is_array($settings) && !empty($settings)) { |
| 176 |
foreach ($settings as $task_name => $setting) { |
| 177 |
if ($setting['task_args']['next'] && $setting['task_args']['next'] < time()) { |
| 178 |
//if ($setting['task_args']['next'] && $_GET['force_backup']) { |
| 179 |
if ($setting['task_args']['url'] && $setting['task_args']['task_id'] && $setting['task_args']['site_key']) { |
| 180 |
//Check orphan task |
| 181 |
$check_data = array( |
| 182 |
'task_name' => $task_name, |
| 183 |
'task_id' => $setting['task_args']['task_id'], |
| 184 |
'site_key' => $setting['task_args']['site_key'] |
| 185 |
); |
| 186 |
|
| 187 |
$check = $this->validate_task($check_data, $setting['task_args']['url']); |
| 188 |
|
| 189 |
} |
| 190 |
|
| 191 |
$update = array( |
| 192 |
'task_name' => $task_name, |
| 193 |
'args' => $settings[$task_name]['task_args'] |
| 194 |
); |
| 195 |
|
| 196 |
|
| 197 |
if($check != 'paused'){ |
| 198 |
$update['time'] = time(); |
| 199 |
} |
| 200 |
|
| 201 |
//Update task with next schedule |
| 202 |
$this->set_backup_task($update); |
| 203 |
|
| 204 |
if($check == 'paused'){ |
| 205 |
continue; |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
$result = $this->backup($setting['task_args'], $task_name); |
| 210 |
$error = ''; |
| 211 |
if (is_array($result) && array_key_exists('error', $result)) { |
| 212 |
$error = $result; |
| 213 |
$this->set_backup_task(array( |
| 214 |
'task_name' => $task_name, |
| 215 |
'args' => $settings[$task_name]['task_args'], |
| 216 |
'error' => $error |
| 217 |
)); |
| 218 |
} else { |
| 219 |
$error = ''; |
| 220 |
} |
| 221 |
break; //Only one backup per cron |
| 222 |
} |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
} |
| 227 |
|
| 228 |
function task_now($task_name){ |
| 229 |
|
| 230 |
$settings = $this->tasks; |
| 231 |
if(!array_key_exists($task_name,$settings)){ |
| 232 |
return array('error' => $task_name." does not exist."); |
| 233 |
} else { |
| 234 |
$setting = $settings[$task_name]; |
| 235 |
} |
| 236 |
|
| 237 |
$this->set_backup_task(array( |
| 238 |
'task_name' => $task_name, |
| 239 |
'args' => $settings[$task_name]['task_args'], |
| 240 |
'time' => time() |
| 241 |
)); |
| 242 |
|
| 243 |
//Run backup |
| 244 |
$result = $this->backup($setting['task_args'], $task_name); |
| 245 |
|
| 246 |
//Check for error |
| 247 |
if (is_array($result) && array_key_exists('error', $result)) { |
| 248 |
$this->set_backup_task(array( |
| 249 |
'task_name' => $task_name, |
| 250 |
'args' => $settings[$task_name]['task_args'], |
| 251 |
'error' => $result |
| 252 |
)); |
| 253 |
return $result; |
| 254 |
} else { |
| 255 |
return $this->get_backup_stats(); |
| 256 |
} |
| 257 |
|
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
/* |
| 263 |
* If Task Name not set then it's manual backup |
| 264 |
* Backup args: |
| 265 |
* type -> db, full |
| 266 |
* what -> daily, weekly, monthly |
| 267 |
* account_info -> ftp, amazons3, dropbox |
| 268 |
* exclude-> array of paths to exclude from backup |
| 269 |
*/ |
| 270 |
|
| 271 |
function backup($args, $task_name = false) |
| 272 |
{ |
| 273 |
|
| 274 |
if (!$args || empty($args)) |
| 275 |
return false; |
| 276 |
|
| 277 |
extract($args); //extract settings |
| 278 |
|
| 279 |
//Try increase memory limit and execution time |
| 280 |
@ini_set('memory_limit', '256M'); |
| 281 |
@set_time_limit(600); //ten minutes |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
//Remove old backup(s) |
| 286 |
$this->remove_old_backups($task_name); |
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
$new_file_path = MWP_BACKUP_DIR; |
| 291 |
|
| 292 |
if (!file_exists($new_file_path)) { |
| 293 |
if (!mkdir($new_file_path, 0755, true)) |
| 294 |
return array( |
| 295 |
'error' => 'Permission denied, make sure you have write permission to wp-content folder.' |
| 296 |
); |
| 297 |
} |
| 298 |
|
| 299 |
@file_put_contents($new_file_path . '/index.php', ''); //safe |
| 300 |
|
| 301 |
//Prepare .zip file name |
| 302 |
$hash = md5(time()); |
| 303 |
$label = $type ? $type : 'manual'; |
| 304 |
$backup_file = $new_file_path . '/' . $this->site_name . '_' . $label . '_' . $what . '_' . date('Y-m-d') . '_' . $hash . '.zip'; |
| 305 |
$backup_url = WP_CONTENT_URL . '/managewp/backups/' . $this->site_name . '_' . $label . '_' . $what . '_' . date('Y-m-d') . '_' . $hash . '.zip'; |
| 306 |
|
| 307 |
//Optimize tables? |
| 308 |
if (isset($optimize_tables) && !empty($optimize_tables)) { |
| 309 |
$this->optimize_tables(); |
| 310 |
} |
| 311 |
|
| 312 |
//What to backup - db or full? |
| 313 |
if (trim($what) == 'db') { |
| 314 |
//Take database backup |
| 315 |
$this->update_status($task_name, $this->statuses['db_dump']); |
| 316 |
$db_result = $this->backup_db(); |
| 317 |
if ($db_result == false) { |
| 318 |
return array( |
| 319 |
'error' => 'Failed to backup database.' |
| 320 |
); |
| 321 |
} else if (is_array($db_result) && isset($db_result['error'])) { |
| 322 |
return array( |
| 323 |
'error' => $db_result['error'] |
| 324 |
); |
| 325 |
} else { |
| 326 |
$this->update_status($task_name, $this->statuses['db_dump'], true); |
| 327 |
$this->update_status($task_name, $this->statuses['db_zip']); |
| 328 |
|
| 329 |
$disable_comp = $this->tasks[$task_name]['task_args']['disable_comp']; |
| 330 |
$comp_level = $disable_comp ? '-0' : '-1'; |
| 331 |
|
| 332 |
chdir(MWP_BACKUP_DIR); |
| 333 |
$zip = $this->get_zip(); |
| 334 |
$command = "$zip -q -r $comp_level $backup_file 'mwp_db'"; |
| 335 |
ob_start(); |
| 336 |
$result = $this->mmb_exec($command); |
| 337 |
ob_get_clean(); |
| 338 |
if (!$result) { // fallback to pclzip |
| 339 |
define('PCLZIP_TEMPORARY_DIR', MWP_BACKUP_DIR . '/'); |
| 340 |
require_once ABSPATH . '/wp-admin/includes/class-pclzip.php'; |
| 341 |
$archive = new PclZip($backup_file); |
| 342 |
if ($disable_comp) { |
| 343 |
$result = $archive->add($db_result, PCLZIP_OPT_REMOVE_PATH, MWP_BACKUP_DIR, PCLZIP_OPT_NO_COMPRESSION); |
| 344 |
} else { |
| 345 |
$result = $archive->add($db_result, PCLZIP_OPT_REMOVE_PATH, MWP_BACKUP_DIR); |
| 346 |
} |
| 347 |
@unlink($db_result); |
| 348 |
@rmdir(MWP_DB_DIR); |
| 349 |
if (!$result) { |
| 350 |
return array( |
| 351 |
'error' => 'Failed to zip database (pclZip - ' . $archive->error_code . '): .' . $archive->error_string |
| 352 |
); |
| 353 |
} |
| 354 |
} |
| 355 |
|
| 356 |
@unlink($db_result); |
| 357 |
@rmdir(MWP_DB_DIR); |
| 358 |
if (!$result) { |
| 359 |
return array( |
| 360 |
'error' => 'Failed to zip database.' |
| 361 |
); |
| 362 |
} |
| 363 |
$this->update_status($task_name, $this->statuses['db_zip'], true); |
| 364 |
} |
| 365 |
} elseif (trim($what) == 'full') { |
| 366 |
$content_backup = $this->backup_full($task_name, $backup_file, $exclude, $include); |
| 367 |
if (is_array($content_backup) && array_key_exists('error', $content_backup)) { |
| 368 |
return array( |
| 369 |
'error' => $content_backup['error'] |
| 370 |
); |
| 371 |
} |
| 372 |
} |
| 373 |
|
| 374 |
//Update backup info |
| 375 |
if ($task_name) { |
| 376 |
//backup task (scheduled) |
| 377 |
$backup_settings = $this->tasks; |
| 378 |
$paths = array(); |
| 379 |
$size = ceil(filesize($backup_file) / 1024); |
| 380 |
|
| 381 |
if ($size > 1000) { |
| 382 |
$paths['size'] = ceil($size / 1024) . "Mb"; |
| 383 |
} else { |
| 384 |
$paths['size'] = $size . 'kb'; |
| 385 |
} |
| 386 |
|
| 387 |
if ($task_name != 'Backup Now') { |
| 388 |
if (!$backup_settings[$task_name]['task_args']['del_host_file']) { |
| 389 |
$paths['server'] = array( |
| 390 |
'file_path' => $backup_file, |
| 391 |
'file_url' => $backup_url |
| 392 |
); |
| 393 |
} |
| 394 |
} else { |
| 395 |
$paths['server'] = array( |
| 396 |
'file_path' => $backup_file, |
| 397 |
'file_url' => $backup_url |
| 398 |
); |
| 399 |
} |
| 400 |
|
| 401 |
if (isset($backup_settings[$task_name]['task_args']['account_info']['mwp_ftp'])) { |
| 402 |
$paths['ftp'] = basename($backup_url); |
| 403 |
} |
| 404 |
|
| 405 |
if (isset($backup_settings[$task_name]['task_args']['account_info']['mwp_amazon_s3'])) { |
| 406 |
$paths['amazons3'] = basename($backup_url); |
| 407 |
} |
| 408 |
|
| 409 |
if (isset($backup_settings[$task_name]['task_args']['account_info']['mwp_dropbox'])) { |
| 410 |
$paths['dropbox'] = basename($backup_url); |
| 411 |
} |
| 412 |
|
| 413 |
if (isset($backup_settings[$task_name]['task_args']['account_info']['mwp_email'])) { |
| 414 |
$paths['email'] = basename($backup_url); |
| 415 |
} |
| 416 |
|
| 417 |
$temp = $backup_settings[$task_name]['task_results']; |
| 418 |
$temp = array_values($temp); |
| 419 |
$paths['time'] = time(); |
| 420 |
|
| 421 |
if ($task_name != 'Backup Now') { |
| 422 |
$paths['status'] = $temp[count($temp) - 1]['status']; |
| 423 |
$temp[count($temp) - 1] = $paths; |
| 424 |
|
| 425 |
} else { |
| 426 |
$temp[count($temp)] = $paths; |
| 427 |
} |
| 428 |
|
| 429 |
$backup_settings[$task_name]['task_results'] = $temp; |
| 430 |
$this->update_tasks($backup_settings); |
| 431 |
//update_option('mwp_backup_tasks', $backup_settings); |
| 432 |
} |
| 433 |
|
| 434 |
|
| 435 |
//Additional: Email, ftp, amazon_s3, dropbox... |
| 436 |
|
| 437 |
if ($task_name != 'Backup Now') { |
| 438 |
if (isset($account_info['mwp_ftp']) && !empty($account_info['mwp_ftp'])) { |
| 439 |
$this->update_status($task_name, $this->statuses['ftp']); |
| 440 |
$account_info['mwp_ftp']['backup_file'] = $backup_file; |
| 441 |
$ftp_result = $this->ftp_backup($account_info['mwp_ftp']); |
| 442 |
|
| 443 |
if ($ftp_result !== true && $del_host_file) { |
| 444 |
@unlink($backup_file); |
| 445 |
} |
| 446 |
|
| 447 |
if (is_array($ftp_result) && isset($ftp_result['error'])) { |
| 448 |
return $ftp_result; |
| 449 |
} |
| 450 |
$this->wpdb_reconnect(); |
| 451 |
$this->update_status($task_name, $this->statuses['ftp'], true); |
| 452 |
} |
| 453 |
|
| 454 |
if (isset($account_info['mwp_amazon_s3']) && !empty($account_info['mwp_amazon_s3'])) { |
| 455 |
$this->update_status($task_name, $this->statuses['s3']); |
| 456 |
$account_info['mwp_amazon_s3']['backup_file'] = $backup_file; |
| 457 |
$amazons3_result = $this->amazons3_backup($account_info['mwp_amazon_s3']); |
| 458 |
if ($amazons3_result !== true && $del_host_file) { |
| 459 |
@unlink($backup_file); |
| 460 |
} |
| 461 |
if (is_array($amazons3_result) && isset($amazons3_result['error'])) { |
| 462 |
return $amazons3_result; |
| 463 |
} |
| 464 |
$this->wpdb_reconnect(); |
| 465 |
$this->update_status($task_name, $this->statuses['s3'], true); |
| 466 |
} |
| 467 |
|
| 468 |
if (isset($account_info['mwp_dropbox']) && !empty($account_info['mwp_dropbox'])) { |
| 469 |
$this->update_status($task_name, $this->statuses['dropbox']); |
| 470 |
$account_info['mwp_dropbox']['backup_file'] = $backup_file; |
| 471 |
$dropbox_result = $this->dropbox_backup($account_info['mwp_dropbox']); |
| 472 |
if ($dropbox_result !== true && $del_host_file) { |
| 473 |
@unlink($backup_file); |
| 474 |
} |
| 475 |
|
| 476 |
if (is_array($dropbox_result) && isset($dropbox_result['error'])) { |
| 477 |
return $dropbox_result; |
| 478 |
} |
| 479 |
$this->wpdb_reconnect(); |
| 480 |
$this->update_status($task_name, $this->statuses['dropbox'], true); |
| 481 |
} |
| 482 |
|
| 483 |
if (isset($account_info['mwp_email']) && !empty($account_info['mwp_email'])) { |
| 484 |
$this->update_status($task_name, $this->statuses['email']); |
| 485 |
$account_info['mwp_email']['task_name'] = $task_name; |
| 486 |
$account_info['mwp_email']['file_path'] = $backup_file; |
| 487 |
|
| 488 |
$email_result = $this->email_backup($account_info['mwp_email']); |
| 489 |
if (is_array($email_result) && isset($email_result['error'])) { |
| 490 |
return $email_result; |
| 491 |
} |
| 492 |
$this->update_status($task_name, $this->statuses['email'], true); |
| 493 |
} |
| 494 |
|
| 495 |
if ($del_host_file) { |
| 496 |
@unlink($backup_file); |
| 497 |
} |
| 498 |
|
| 499 |
} //end additional |
| 500 |
|
| 501 |
//$this->update_status($task_name,$this->statuses['finished'],true); |
| 502 |
return $backup_url; //Return url to backup file |
| 503 |
} |
| 504 |
|
| 505 |
function backup_full($task_name, $backup_file, $exclude = array(), $include = array()) |
| 506 |
{ |
| 507 |
global $zip_errors; |
| 508 |
$sys = substr(PHP_OS, 0, 3); |
| 509 |
|
| 510 |
$this->update_status($task_name, $this->statuses['db_dump']); |
| 511 |
$db_result = $this->backup_db(); |
| 512 |
|
| 513 |
if ($db_result == false) { |
| 514 |
return array( |
| 515 |
'error' => 'Failed to backup database.' |
| 516 |
); |
| 517 |
} else if (is_array($db_result) && isset($db_result['error'])) { |
| 518 |
return array( |
| 519 |
'error' => $db_result['error'] |
| 520 |
); |
| 521 |
} |
| 522 |
|
| 523 |
$this->update_status($task_name, $this->statuses['db_dump'], true); |
| 524 |
$this->update_status($task_name, $this->statuses['db_zip']); |
| 525 |
$disable_comp = $this->tasks[$task_name]['task_args']['disable_comp']; |
| 526 |
$comp_level = $disable_comp ? '-0' : '-1'; |
| 527 |
|
| 528 |
$zip = $this->get_zip(); |
| 529 |
//Add database file |
| 530 |
chdir(MWP_BACKUP_DIR); |
| 531 |
$command = "$zip -q -r $comp_level $backup_file 'mwp_db'"; |
| 532 |
ob_start(); |
| 533 |
$result = $this->mmb_exec($command); |
| 534 |
ob_get_clean(); |
| 535 |
|
| 536 |
|
| 537 |
if (!$result) { |
| 538 |
define('PCLZIP_TEMPORARY_DIR', MWP_BACKUP_DIR . '/'); |
| 539 |
require_once ABSPATH . '/wp-admin/includes/class-pclzip.php'; |
| 540 |
$archive = new PclZip($backup_file); |
| 541 |
|
| 542 |
if ($disable_comp) { |
| 543 |
$result_db = $archive->add($db_result, PCLZIP_OPT_REMOVE_PATH, MWP_BACKUP_DIR, PCLZIP_OPT_NO_COMPRESSION); |
| 544 |
} else { |
| 545 |
$result_db = $archive->add($db_result, PCLZIP_OPT_REMOVE_PATH, MWP_BACKUP_DIR); |
| 546 |
} |
| 547 |
|
| 548 |
@unlink($db_result); |
| 549 |
@rmdir(MWP_DB_DIR); |
| 550 |
|
| 551 |
if (!$result_db) { |
| 552 |
return array( |
| 553 |
'error' => 'Failed to zip database. pclZip error (' . $archive->error_code . '): .' . $archive->error_string |
| 554 |
); |
| 555 |
} |
| 556 |
} |
| 557 |
|
| 558 |
@unlink($db_result); |
| 559 |
@rmdir(MWP_DB_DIR); |
| 560 |
|
| 561 |
$this->update_status($task_name, $this->statuses['db_zip'], true); |
| 562 |
|
| 563 |
|
| 564 |
//Always remove backup folders |
| 565 |
$remove = array( |
| 566 |
trim(basename(WP_CONTENT_DIR)) . "/managewp/backups", |
| 567 |
trim(basename(WP_CONTENT_DIR)) . "/" . md5('mmb-worker') . "/mwp_backups" |
| 568 |
); |
| 569 |
|
| 570 |
//Exclude paths |
| 571 |
$exclude_data = "-x"; |
| 572 |
|
| 573 |
$exclude_file_data = ''; |
| 574 |
|
| 575 |
if (!empty($exclude)) { |
| 576 |
foreach ($exclude as $data) { |
| 577 |
if (is_dir(ABSPATH . $data)) { |
| 578 |
if ($sys == 'WIN') |
| 579 |
$exclude_data .= " $data/*.*"; |
| 580 |
else |
| 581 |
$exclude_data .= " $data/*"; |
| 582 |
|
| 583 |
|
| 584 |
} else { |
| 585 |
if ($sys == 'WIN'){ |
| 586 |
if(file_exists(ABSPATH . $data)){ |
| 587 |
$exclude_data .= " $data"; |
| 588 |
$exclude_file_data .= " $data"; |
| 589 |
} |
| 590 |
} else { |
| 591 |
if(file_exists(ABSPATH . $data)){ |
| 592 |
$exclude_data .= " '$data'"; |
| 593 |
$exclude_file_data .= " '$data'"; |
| 594 |
} |
| 595 |
} |
| 596 |
} |
| 597 |
} |
| 598 |
} |
| 599 |
|
| 600 |
if($exclude_file_data){ |
| 601 |
$exclude_file_data = "-x".$exclude_file_data; |
| 602 |
} |
| 603 |
|
| 604 |
foreach ($remove as $data) { |
| 605 |
if ($sys == 'WIN') |
| 606 |
$exclude_data .= " $data/*.*"; |
| 607 |
else |
| 608 |
$exclude_data .= " '$data/*'"; |
| 609 |
} |
| 610 |
|
| 611 |
//Include paths by default |
| 612 |
$add = array( |
| 613 |
trim(WPINC), |
| 614 |
trim(basename(WP_CONTENT_DIR)), |
| 615 |
"wp-admin" |
| 616 |
); |
| 617 |
|
| 618 |
$include_data = ". -i"; |
| 619 |
foreach ($add as $data) { |
| 620 |
if ($sys == 'WIN') |
| 621 |
$include_data .= " $data/*.*"; |
| 622 |
else |
| 623 |
$include_data .= " '$data/*'"; |
| 624 |
} |
| 625 |
|
| 626 |
//Additional includes? |
| 627 |
if (!empty($include)) { |
| 628 |
foreach ($include as $data) { |
| 629 |
if ($data) { |
| 630 |
if ($sys == 'WIN') |
| 631 |
$include_data .= " $data/*.*"; |
| 632 |
else |
| 633 |
$include_data .= " '$data/*'"; |
| 634 |
} |
| 635 |
} |
| 636 |
} |
| 637 |
|
| 638 |
$this->update_status($task_name, $this->statuses['files_zip']); |
| 639 |
chdir(ABSPATH); |
| 640 |
ob_start(); |
| 641 |
$command = "$zip -q -j $comp_level $backup_file .* * $exclude_file_data"; |
| 642 |
$result_f = $this->mmb_exec($command, false, true); |
| 643 |
if (!$result_f || $result_f == 18) { // disregard permissions error, file can't be accessed |
| 644 |
$command = "$zip -q -r $comp_level $backup_file $include_data $exclude_data"; |
| 645 |
$result_d = $this->mmb_exec($command, false, true); |
| 646 |
if ($result_d && $result_d != 18) { |
| 647 |
@unlink($backup_file); |
| 648 |
if ($result_d > 0 && $result_d < 18) |
| 649 |
return array( |
| 650 |
'error' => 'Failed to archive files (' . $zip_errors[$result_d] . ') .' |
| 651 |
); |
| 652 |
else |
| 653 |
return array( |
| 654 |
'error' => 'Failed to archive files.' |
| 655 |
); |
| 656 |
} |
| 657 |
} |
| 658 |
ob_get_clean(); |
| 659 |
|
| 660 |
if ($result_f && $result_f != 18) { //Try pclZip |
| 661 |
|
| 662 |
if (!isset($archive)) { |
| 663 |
define('PCLZIP_TEMPORARY_DIR', MWP_BACKUP_DIR . '/'); |
| 664 |
require_once ABSPATH . '/wp-admin/includes/class-pclzip.php'; |
| 665 |
$archive = new PclZip($backup_file); |
| 666 |
} |
| 667 |
|
| 668 |
//Include paths |
| 669 |
$include_data = array(); |
| 670 |
if (!empty($include)) { |
| 671 |
foreach ($include as $data) { |
| 672 |
if ($data && file_exists(ABSPATH . $data)) |
| 673 |
$include_data[] = ABSPATH . $data . '/'; |
| 674 |
} |
| 675 |
} |
| 676 |
|
| 677 |
foreach ($add as $data) { |
| 678 |
if (file_exists(ABSPATH . $data)) |
| 679 |
$include_data[] = ABSPATH . $data . '/'; |
| 680 |
} |
| 681 |
|
| 682 |
//Include root files |
| 683 |
if ($handle = opendir(ABSPATH)) { |
| 684 |
while (false !== ($file = readdir($handle))) { |
| 685 |
if ($file != "." && $file != ".." && !is_dir($file) && file_exists(ABSPATH . $file)) { |
| 686 |
$include_data[] = ABSPATH . $file; |
| 687 |
} |
| 688 |
} |
| 689 |
closedir($handle); |
| 690 |
} |
| 691 |
|
| 692 |
if ($disable_comp) { |
| 693 |
$result = $archive->add($include_data, PCLZIP_OPT_REMOVE_PATH, ABSPATH, PCLZIP_OPT_NO_COMPRESSION); |
| 694 |
} else { |
| 695 |
$result = $archive->add($include_data, PCLZIP_OPT_REMOVE_PATH, ABSPATH); |
| 696 |
} |
| 697 |
if (!$result) { |
| 698 |
@unlink($backup_file); |
| 699 |
return array( |
| 700 |
'error' => 'Failed to zip files. pclZip error (' . $archive->error_code . '): .' . $archive->error_string |
| 701 |
); |
| 702 |
} |
| 703 |
|
| 704 |
//Now exclude paths |
| 705 |
$exclude_data = array(); |
| 706 |
if (!empty($exclude)) { |
| 707 |
foreach ($exclude as $data) { |
| 708 |
if (is_dir(ABSPATH . $data)) |
| 709 |
$exclude_data[] = $data . '/'; |
| 710 |
else |
| 711 |
$exclude_data[] = $data; |
| 712 |
} |
| 713 |
} |
| 714 |
|
| 715 |
foreach ($remove as $rem) { |
| 716 |
$exclude_data[] = $rem . '/'; |
| 717 |
} |
| 718 |
|
| 719 |
$result_excl = $archive->delete(PCLZIP_OPT_BY_NAME, $exclude_data); |
| 720 |
if (!$result_excl) { |
| 721 |
@unlink($backup_file); |
| 722 |
return array( |
| 723 |
'error' => 'Failed to zip files. pclZip error (' . $archive->error_code . '): .' . $archive->error_string |
| 724 |
); |
| 725 |
} |
| 726 |
|
| 727 |
} |
| 728 |
|
| 729 |
//Reconnect |
| 730 |
$this->wpdb_reconnect(); |
| 731 |
|
| 732 |
$this->update_status($task_name, $this->statuses['files_zip'], true); |
| 733 |
return true; |
| 734 |
} |
| 735 |
|
| 736 |
|
| 737 |
function backup_db() |
| 738 |
{ |
| 739 |
$db_folder = MWP_DB_DIR . '/'; |
| 740 |
if (!file_exists($db_folder)) { |
| 741 |
if (!mkdir($db_folder, 0755, true)) |
| 742 |
return array( |
| 743 |
'error' => 'Error creating database backup folder (' . $db_folder . '). Make sure you have corrrect write permissions.' |
| 744 |
); |
| 745 |
} |
| 746 |
|
| 747 |
$file = $db_folder . DB_NAME . '.sql'; |
| 748 |
$result = $this->backup_db_dump($file); // try mysqldump always then fallback to php dump |
| 749 |
return $result; |
| 750 |
} |
| 751 |
|
| 752 |
function backup_db_dump($file) |
| 753 |
{ |
| 754 |
global $wpdb; |
| 755 |
$paths = $this->check_mysql_paths(); |
| 756 |
$brace = (substr(PHP_OS, 0, 3) == 'WIN') ? '"' : ''; |
| 757 |
$command = $brace . $paths['mysqldump'] . $brace . ' --force --host="' . DB_HOST . '" --user="' . DB_USER . '" --password="' . DB_PASSWORD . '" --add-drop-table --skip-lock-tables "' . DB_NAME . '" > ' . $brace . $file . $brace; |
| 758 |
ob_start(); |
| 759 |
$result = $this->mmb_exec($command); |
| 760 |
ob_get_clean(); |
| 761 |
|
| 762 |
if (!$result) { // Fallback to php |
| 763 |
$result = $this->backup_db_php($file); |
| 764 |
return $result; |
| 765 |
} |
| 766 |
|
| 767 |
if (filesize($file) == 0 || !is_file($file) || !$result) { |
| 768 |
@unlink($file); |
| 769 |
return false; |
| 770 |
} else { |
| 771 |
return $file; |
| 772 |
} |
| 773 |
} |
| 774 |
|
| 775 |
function backup_db_php($file) |
| 776 |
{ |
| 777 |
global $wpdb; |
| 778 |
$tables = $wpdb->get_results('SHOW TABLES', ARRAY_N); |
| 779 |
foreach ($tables as $table) { |
| 780 |
//drop existing table |
| 781 |
$dump_data = "DROP TABLE IF EXISTS $table[0];"; |
| 782 |
//create table |
| 783 |
$create_table = $wpdb->get_row("SHOW CREATE TABLE $table[0]", ARRAY_N); |
| 784 |
$dump_data .= "\n\n" . $create_table[1] . ";\n\n"; |
| 785 |
|
| 786 |
$count = $wpdb->get_var("SELECT count(*) FROM $table[0]"); |
| 787 |
if ($count > 100) |
| 788 |
$count = ceil($count / 100); |
| 789 |
else if ($count > 0) |
| 790 |
$count = 1; |
| 791 |
|
| 792 |
for ($i = 0; $i < $count; $i++) { |
| 793 |
$low_limit = $i * 100; |
| 794 |
$qry = "SELECT * FROM $table[0] LIMIT $low_limit, 100"; |
| 795 |
$rows = $wpdb->get_results($qry, ARRAY_A); |
| 796 |
if (is_array($rows)) { |
| 797 |
foreach ($rows as $row) { |
| 798 |
//insert single row |
| 799 |
$dump_data .= "INSERT INTO $table[0] VALUES("; |
| 800 |
$num_values = count($row); |
| 801 |
$j = 1; |
| 802 |
foreach ($row as $value) { |
| 803 |
$value = addslashes($value); |
| 804 |
$value = preg_replace("/\n/Ui", "\\n", $value); |
| 805 |
$num_values == $j ? $dump_data .= "'" . $value . "'" : $dump_data .= "'" . $value . "', "; |
| 806 |
$j++; |
| 807 |
unset($value); |
| 808 |
} |
| 809 |
$dump_data .= ");\n"; |
| 810 |
} |
| 811 |
} |
| 812 |
} |
| 813 |
$dump_data .= "\n\n\n"; |
| 814 |
|
| 815 |
unset($rows); |
| 816 |
file_put_contents($file, $dump_data, FILE_APPEND); |
| 817 |
unset($dump_data); |
| 818 |
} |
| 819 |
|
| 820 |
if (filesize($file) == 0 || !is_file($file)) { |
| 821 |
@unlink($file); |
| 822 |
return array( |
| 823 |
'error' => 'Database backup failed. Try to enable MySQL dump on your server.' |
| 824 |
); |
| 825 |
} |
| 826 |
|
| 827 |
return $file; |
| 828 |
|
| 829 |
} |
| 830 |
|
| 831 |
function restore($args) |
| 832 |
{ |
| 833 |
global $wpdb; |
| 834 |
if (empty($args)) { |
| 835 |
return false; |
| 836 |
} |
| 837 |
|
| 838 |
extract($args); |
| 839 |
@ini_set('memory_limit', '256M'); |
| 840 |
@set_time_limit(600); |
| 841 |
|
| 842 |
$unlink_file = true; //Delete file after restore |
| 843 |
|
| 844 |
//Detect source |
| 845 |
if ($backup_url) { |
| 846 |
//This is for clone (overwrite) |
| 847 |
include_once ABSPATH . 'wp-admin/includes/file.php'; |
| 848 |
$backup_file = download_url($backup_url); |
| 849 |
if (is_wp_error($backup_file)) { |
| 850 |
return array( |
| 851 |
'error' => 'Unable to download backup file ('.$backup_file->get_error_message().')' |
| 852 |
); |
| 853 |
} |
| 854 |
$what = 'full'; |
| 855 |
} else { |
| 856 |
$tasks = $this->tasks; |
| 857 |
$task = $tasks[$task_name]; |
| 858 |
if (isset($task['task_results'][$result_id]['server'])) { |
| 859 |
$backup_file = $task['task_results'][$result_id]['server']['file_path']; |
| 860 |
$unlink_file = false; //Don't delete file if stored on server |
| 861 |
} elseif (isset($task['task_results'][$result_id]['ftp'])) { |
| 862 |
$ftp_file = $task['task_results'][$result_id]['ftp']; |
| 863 |
$args = $task['task_args']['account_info']['mwp_ftp']; |
| 864 |
$args['backup_file'] = $ftp_file; |
| 865 |
$backup_file = $this->get_ftp_backup($args); |
| 866 |
if ($backup_file == false) { |
| 867 |
return array( |
| 868 |
'error' => 'Failed to download file from FTP.' |
| 869 |
); |
| 870 |
} |
| 871 |
} elseif (isset($task['task_results'][$result_id]['amazons3'])) { |
| 872 |
$amazons3_file = $task['task_results'][$result_id]['amazons3']; |
| 873 |
$args = $task['task_args']['account_info']['mwp_amazon_s3']; |
| 874 |
$args['backup_file'] = $amazons3_file; |
| 875 |
$backup_file = $this->get_amazons3_backup($args); |
| 876 |
if ($backup_file == false) { |
| 877 |
return array( |
| 878 |
'error' => 'Failed to download file from Amazon S3.' |
| 879 |
); |
| 880 |
} |
| 881 |
} elseif(isset($task['task_results'][$result_id]['dropbox'])){ |
| 882 |
$dropbox_file = $task['task_results'][$result_id]['dropbox']; |
| 883 |
$args = $task['task_args']['account_info']['mwp_dropbox']; |
| 884 |
$args['backup_file'] = $dropbox_file; |
| 885 |
$backup_file = $this->get_dropbox_backup($args); |
| 886 |
|
| 887 |
if ($backup_file == false) { |
| 888 |
return array( |
| 889 |
'error' => 'Failed to download file from Dropbox.' |
| 890 |
); |
| 891 |
} |
| 892 |
} |
| 893 |
|
| 894 |
$what = $tasks[$task_name]['task_args']['what']; |
| 895 |
} |
| 896 |
|
| 897 |
$this->wpdb_reconnect(); |
| 898 |
|
| 899 |
if ($backup_file && file_exists($backup_file)) { |
| 900 |
if ($overwrite) { |
| 901 |
//Keep old db credentials before overwrite |
| 902 |
if (!copy(ABSPATH . 'wp-config.php', ABSPATH . 'mwp-temp-wp-config.php')) { |
| 903 |
@unlink($backup_file); |
| 904 |
return array( |
| 905 |
'error' => 'Error creating wp-config. Please check your write permissions.' |
| 906 |
); |
| 907 |
} |
| 908 |
|
| 909 |
$db_host = DB_HOST; |
| 910 |
$db_user = DB_USER; |
| 911 |
$db_password = DB_PASSWORD; |
| 912 |
$home = rtrim(get_option('home'), "/"); |
| 913 |
$site_url = get_option('site_url'); |
| 914 |
|
| 915 |
$clone_options = array(); |
| 916 |
if (trim($clone_from_url) || trim($mwp_clone)) { |
| 917 |
|
| 918 |
$clone_options['_worker_nossl_key'] = get_option('_worker_nossl_key'); |
| 919 |
$clone_options['_worker_public_key'] = get_option('_worker_public_key'); |
| 920 |
$clone_options['_action_message_id'] = get_option('_action_message_id'); |
| 921 |
|
| 922 |
} |
| 923 |
$clone_options['upload_path'] = get_option('upload_path'); |
| 924 |
$clone_options['upload_url_path'] = get_option('upload_url_path'); |
| 925 |
|
| 926 |
$clone_options['mwp_backup_tasks'] = serialize(get_option('mwp_backup_tasks')); |
| 927 |
$clone_options['mwp_notifications'] = serialize(get_option('mwp_notifications')); |
| 928 |
$clone_options['mwp_pageview_alerts'] = serialize(get_option('mwp_pageview_alerts')); |
| 929 |
|
| 930 |
|
| 931 |
} else { |
| 932 |
$restore_options = array(); |
| 933 |
$restore_options['mwp_notifications'] = get_option('mwp_notifications'); |
| 934 |
$restore_options['mwp_pageview_alerts'] = get_option('mwp_pageview_alerts'); |
| 935 |
$restore_options['user_hit_count'] = get_option('user_hit_count'); |
| 936 |
} |
| 937 |
|
| 938 |
|
| 939 |
chdir(ABSPATH); |
| 940 |
$unzip = $this->get_unzip(); |
| 941 |
$command = "$unzip -o $backup_file"; |
| 942 |
ob_start(); |
| 943 |
$result = $this->mmb_exec($command); |
| 944 |
ob_get_clean(); |
| 945 |
|
| 946 |
if (!$result) { //fallback to pclzip |
| 947 |
define('PCLZIP_TEMPORARY_DIR', MWP_BACKUP_DIR . '/'); |
| 948 |
require_once ABSPATH . '/wp-admin/includes/class-pclzip.php'; |
| 949 |
$archive = new PclZip($backup_file); |
| 950 |
$result = $archive->extract(PCLZIP_OPT_PATH, ABSPATH, PCLZIP_OPT_REPLACE_NEWER); |
| 951 |
} |
| 952 |
|
| 953 |
if ($unlink_file) { |
| 954 |
@unlink($backup_file); |
| 955 |
} |
| 956 |
|
| 957 |
if (!$result) { |
| 958 |
return array( |
| 959 |
'error' => 'Failed to unzip files. pclZip error (' . $archive->error_code . '): .' . $archive->error_string |
| 960 |
); |
| 961 |
} |
| 962 |
|
| 963 |
$db_result = $this->restore_db(); |
| 964 |
|
| 965 |
if (!$db_result) { |
| 966 |
return array( |
| 967 |
'error' => 'Error restoring database.' |
| 968 |
); |
| 969 |
} else if(is_array($db_result) && isset($db_result['error'])){ |
| 970 |
return array( |
| 971 |
'error' => $db_result['error'] |
| 972 |
); |
| 973 |
} |
| 974 |
|
| 975 |
} else { |
| 976 |
return array( |
| 977 |
'error' => 'Error restoring. Cannot find backup file.' |
| 978 |
); |
| 979 |
} |
| 980 |
|
| 981 |
$this->wpdb_reconnect(); |
| 982 |
|
| 983 |
//Replace options and content urls |
| 984 |
if ($overwrite) { |
| 985 |
//Get New Table prefix |
| 986 |
$new_table_prefix = trim($this->get_table_prefix()); |
| 987 |
//Retrieve old wp_config |
| 988 |
@unlink(ABSPATH . 'wp-config.php'); |
| 989 |
//Replace table prefix |
| 990 |
$lines = file(ABSPATH . 'mwp-temp-wp-config.php'); |
| 991 |
|
| 992 |
foreach ($lines as $line) { |
| 993 |
if (strstr($line, '$table_prefix')) { |
| 994 |
$line = '$table_prefix = "' . $new_table_prefix . '";' . PHP_EOL; |
| 995 |
} |
| 996 |
file_put_contents(ABSPATH . 'wp-config.php', $line, FILE_APPEND); |
| 997 |
} |
| 998 |
|
| 999 |
@unlink(ABSPATH . 'mwp-temp-wp-config.php'); |
| 1000 |
|
| 1001 |
//Replace options |
| 1002 |
$query = "SELECT option_value FROM " . $new_table_prefix . "options WHERE option_name = 'home'"; |
| 1003 |
$old = $wpdb->get_var($wpdb->prepare($query)); |
| 1004 |
$old = rtrim($old, "/"); |
| 1005 |
$query = "UPDATE " . $new_table_prefix . "options SET option_value = '$home' WHERE option_name = 'home'"; |
| 1006 |
$wpdb->query($wpdb->prepare($query)); |
| 1007 |
$query = "UPDATE " . $new_table_prefix . "options SET option_value = '$home' WHERE option_name = 'siteurl'"; |
| 1008 |
$wpdb->query($wpdb->prepare($query)); |
| 1009 |
//Replace content urls |
| 1010 |
$query = "UPDATE " . $new_table_prefix . "posts SET post_content = REPLACE (post_content, '$old','$home') WHERE post_content REGEXP 'src=\"(.*)$old(.*)\"' OR post_content REGEXP 'href=\"(.*)$old(.*)\"'"; |
| 1011 |
$wpdb->query($wpdb->prepare($query)); |
| 1012 |
|
| 1013 |
if (trim($new_password)) { |
| 1014 |
$new_password = wp_hash_password($new_password); |
| 1015 |
} |
| 1016 |
if (!trim($clone_from_url) && !trim($mwp_clone)) { |
| 1017 |
if ($new_user && $new_password) { |
| 1018 |
$query = "UPDATE " . $new_table_prefix . "users SET user_login = '$new_user', user_pass = '$new_password' WHERE user_login = '$old_user'"; |
| 1019 |
$wpdb->query($wpdb->prepare($query)); |
| 1020 |
} |
| 1021 |
} else { |
| 1022 |
if ($clone_from_url) { |
| 1023 |
if ($new_user && $new_password) { |
| 1024 |
$query = "UPDATE " . $new_table_prefix . "users SET user_pass = '$new_password' WHERE user_login = '$new_user'"; |
| 1025 |
$wpdb->query($wpdb->prepare($query)); |
| 1026 |
} |
| 1027 |
} |
| 1028 |
|
| 1029 |
if ($mwp_clone) { |
| 1030 |
if ($admin_email) { |
| 1031 |
//Clean Install |
| 1032 |
$query = "UPDATE " . $new_table_prefix . "options SET option_value = '$admin_email' WHERE option_name = 'admin_email'"; |
| 1033 |
$wpdb->query($wpdb->prepare($query)); |
| 1034 |
$query = "SELECT * FROM " . $new_table_prefix . "users LIMIT 1"; |
| 1035 |
$temp_user = $wpdb->get_row($query); |
| 1036 |
if (!empty($temp_user)) { |
| 1037 |
$query = "UPDATE " . $new_table_prefix . "users SET user_email='$admin_email', user_login = '$new_user', user_pass = '$new_password' WHERE user_login = '$temp_user->user_login'"; |
| 1038 |
$wpdb->query($wpdb->prepare($query)); |
| 1039 |
} |
| 1040 |
|
| 1041 |
} |
| 1042 |
} |
| 1043 |
} |
| 1044 |
|
| 1045 |
if (is_array($clone_options) && !empty($clone_options)) { |
| 1046 |
foreach ($clone_options as $key => $option) { |
| 1047 |
if (!empty($key)) { |
| 1048 |
$query = "SELECT option_value FROM " . $new_table_prefix . "options WHERE option_name = '$key'"; |
| 1049 |
$res = $wpdb->get_var($query); |
| 1050 |
if ($res == false) { |
| 1051 |
$query = "INSERT INTO " . $new_table_prefix . "options (option_value,option_name) VALUES('$option','$key')"; |
| 1052 |
$wpdb->query($wpdb->prepare($query)); |
| 1053 |
} else { |
| 1054 |
$query = "UPDATE " . $new_table_prefix . "options SET option_value = '$option' WHERE option_name = '$key'"; |
| 1055 |
$wpdb->query($wpdb->prepare($query)); |
| 1056 |
} |
| 1057 |
} |
| 1058 |
} |
| 1059 |
} |
| 1060 |
|
| 1061 |
//Remove hit count |
| 1062 |
$query = "DELETE FROM " . $new_table_prefix . "options WHERE option_name = 'user_hit_count'"; |
| 1063 |
$wpdb->query($wpdb->prepare($query)); |
| 1064 |
|
| 1065 |
//Check for .htaccess permalinks update |
| 1066 |
$this->replace_htaccess($home); |
| 1067 |
} else { |
| 1068 |
|
| 1069 |
//restore worker options |
| 1070 |
if (is_array($restore_options) && !empty($restore_options)) { |
| 1071 |
foreach ($restore_options as $key => $option) { |
| 1072 |
update_option($key,$option); |
| 1073 |
} |
| 1074 |
} |
| 1075 |
|
| 1076 |
} |
| 1077 |
|
| 1078 |
|
| 1079 |
|
| 1080 |
|
| 1081 |
return true; |
| 1082 |
} |
| 1083 |
|
| 1084 |
function restore_db() |
| 1085 |
{ |
| 1086 |
global $wpdb; |
| 1087 |
$paths = $this->check_mysql_paths(); |
| 1088 |
$file_path = ABSPATH . 'mwp_db'; |
| 1089 |
@chmod($file_path,0755); |
| 1090 |
$file_name = glob($file_path . '/*.sql'); |
| 1091 |
$file_name = $file_name[0]; |
| 1092 |
|
| 1093 |
if(!$file_name){ |
| 1094 |
return array('error' => 'Cannot access database file.'); |
| 1095 |
} |
| 1096 |
|
| 1097 |
$brace = (substr(PHP_OS, 0, 3) == 'WIN') ? '"' : ''; |
| 1098 |
$command = $brace . $paths['mysql'] . $brace . ' --host="' . DB_HOST . '" --user="' . DB_USER . '" --password="' . DB_PASSWORD . '" ' . DB_NAME . ' < ' . $brace . $file_name . $brace; |
| 1099 |
|
| 1100 |
ob_start(); |
| 1101 |
$result = $this->mmb_exec($command); |
| 1102 |
ob_get_clean(); |
| 1103 |
if (!$result) { |
| 1104 |
//try php |
| 1105 |
$this->restore_db_php($file_name); |
| 1106 |
} |
| 1107 |
|
| 1108 |
|
| 1109 |
@unlink($file_name); |
| 1110 |
return true; |
| 1111 |
} |
| 1112 |
|
| 1113 |
function restore_db_php($file_name) |
| 1114 |
{ |
| 1115 |
global $wpdb; |
| 1116 |
$current_query = ''; |
| 1117 |
// Read in entire file |
| 1118 |
$lines = file($file_name); |
| 1119 |
// Loop through each line |
| 1120 |
foreach ($lines as $line) { |
| 1121 |
// Skip it if it's a comment |
| 1122 |
if (substr($line, 0, 2) == '--' || $line == '') |
| 1123 |
continue; |
| 1124 |
|
| 1125 |
// Add this line to the current query |
| 1126 |
$current_query .= $line; |
| 1127 |
// If it has a semicolon at the end, it's the end of the query |
| 1128 |
if (substr(trim($line), -1, 1) == ';') { |
| 1129 |
// Perform the query |
| 1130 |
$result = $wpdb->query($current_query); |
| 1131 |
if ($result === false) |
| 1132 |
return false; |
| 1133 |
// Reset temp variable to empty |
| 1134 |
$current_query = ''; |
| 1135 |
} |
| 1136 |
} |
| 1137 |
|
| 1138 |
@unlink($file_name); |
| 1139 |
return true; |
| 1140 |
} |
| 1141 |
|
| 1142 |
function get_table_prefix() |
| 1143 |
{ |
| 1144 |
$lines = file(ABSPATH . 'wp-config.php'); |
| 1145 |
foreach ($lines as $line) { |
| 1146 |
if (strstr($line, '$table_prefix')) { |
| 1147 |
$pattern = "/(\'|\")[^(\'|\")]*/"; |
| 1148 |
preg_match($pattern, $line, $matches); |
| 1149 |
$prefix = substr($matches[0], 1); |
| 1150 |
return $prefix; |
| 1151 |
break; |
| 1152 |
} |
| 1153 |
} |
| 1154 |
return 'wp_'; //default |
| 1155 |
} |
| 1156 |
|
| 1157 |
function optimize_tables() |
| 1158 |
{ |
| 1159 |
global $wpdb; |
| 1160 |
$query = 'SHOW TABLE STATUS FROM ' . DB_NAME; |
| 1161 |
$tables = $wpdb->get_results($wpdb->prepare($query), ARRAY_A); |
| 1162 |
foreach ($tables as $table) { |
| 1163 |
if (in_array($table['Engine'], array( |
| 1164 |
'MyISAM', |
| 1165 |
'ISAM', |
| 1166 |
'HEAP', |
| 1167 |
'MEMORY', |
| 1168 |
'ARCHIVE' |
| 1169 |
))) |
| 1170 |
$table_string .= $table['Name'] . ","; |
| 1171 |
elseif ($table['Engine'] == 'InnoDB') { |
| 1172 |
$optimize = $wpdb->query("ALTER TABLE {$table['Name']} ENGINE=InnoDB"); |
| 1173 |
} |
| 1174 |
} |
| 1175 |
|
| 1176 |
$table_string = rtrim($table_string); |
| 1177 |
$optimize = $wpdb->query("OPTIMIZE TABLE $table_string"); |
| 1178 |
|
| 1179 |
return $optimize ? true : false; |
| 1180 |
} |
| 1181 |
|
| 1182 |
### Function: Auto Detect MYSQL and MYSQL Dump Paths |
| 1183 |
function check_mysql_paths() |
| 1184 |
{ |
| 1185 |
global $wpdb; |
| 1186 |
$paths = array( |
| 1187 |
'mysql' => '', |
| 1188 |
'mysqldump' => '' |
| 1189 |
); |
| 1190 |
if (substr(PHP_OS, 0, 3) == 'WIN') { |
| 1191 |
$mysql_install = $wpdb->get_row("SHOW VARIABLES LIKE 'basedir'"); |
| 1192 |
if ($mysql_install) { |
| 1193 |
$install_path = str_replace('\\', '/', $mysql_install->Value); |
| 1194 |
$paths['mysql'] = $install_path . 'bin/mysql.exe'; |
| 1195 |
$paths['mysqldump'] = $install_path . 'bin/mysqldump.exe'; |
| 1196 |
} else { |
| 1197 |
$paths['mysql'] = 'mysql.exe'; |
| 1198 |
$paths['mysqldump'] = 'mysqldump.exe'; |
| 1199 |
} |
| 1200 |
} else { |
| 1201 |
$paths['mysql'] = $this->mmb_exec('which mysql', true); |
| 1202 |
if (empty($paths['mysql'])) |
| 1203 |
$paths['mysql'] = 'mysql'; // try anyway |
| 1204 |
|
| 1205 |
$paths['mysqldump'] = $this->mmb_exec('which mysqldump', true); |
| 1206 |
if (empty($paths['mysqldump'])) |
| 1207 |
$paths['mysqldump'] = 'mysqldump'; // try anyway |
| 1208 |
|
| 1209 |
} |
| 1210 |
|
| 1211 |
|
| 1212 |
return $paths; |
| 1213 |
} |
| 1214 |
|
| 1215 |
//Check if exec, system, passthru functions exist |
| 1216 |
function check_sys() |
| 1217 |
{ |
| 1218 |
if ($this->mmb_function_exists('exec')) |
| 1219 |
return 'exec'; |
| 1220 |
|
| 1221 |
if ($this->mmb_function_exists('system')) |
| 1222 |
return 'system'; |
| 1223 |
|
| 1224 |
if ($this->mmb_function_exists('passhtru')) |
| 1225 |
return 'passthru'; |
| 1226 |
|
| 1227 |
return false; |
| 1228 |
|
| 1229 |
} |
| 1230 |
|
| 1231 |
function mmb_exec($command, $string = false, $rawreturn = false) |
| 1232 |
{ |
| 1233 |
if ($command == '') |
| 1234 |
return false; |
| 1235 |
|
| 1236 |
if ($this->mmb_function_exists('exec')) { |
| 1237 |
$log = @exec($command, $output, $return); |
| 1238 |
|
| 1239 |
if ($string) |
| 1240 |
return $log; |
| 1241 |
if ($rawreturn) |
| 1242 |
return $return; |
| 1243 |
|
| 1244 |
return $return ? false : true; |
| 1245 |
} elseif ($this->mmb_function_exists('system')) { |
| 1246 |
$log = @system($command, $return); |
| 1247 |
|
| 1248 |
if ($string) |
| 1249 |
return $log; |
| 1250 |
|
| 1251 |
if ($rawreturn) |
| 1252 |
return $return; |
| 1253 |
|
| 1254 |
return $return ? false : true; |
| 1255 |
} elseif ($this->mmb_function_exists('passthru') && !$string) { |
| 1256 |
$log = passthru($command, $return); |
| 1257 |
|
| 1258 |
if ($rawreturn) |
| 1259 |
return $return; |
| 1260 |
|
| 1261 |
return $return ? false : true; |
| 1262 |
} |
| 1263 |
|
| 1264 |
if ($rawreturn) |
| 1265 |
return -1; |
| 1266 |
|
| 1267 |
return false; |
| 1268 |
} |
| 1269 |
|
| 1270 |
function get_zip() |
| 1271 |
{ |
| 1272 |
$zip = $this->mmb_exec('which zip', true); |
| 1273 |
if (!$zip) |
| 1274 |
$zip = "zip"; |
| 1275 |
return $zip; |
| 1276 |
} |
| 1277 |
|
| 1278 |
function get_unzip() |
| 1279 |
{ |
| 1280 |
$unzip = $this->mmb_exec('which unzip', true); |
| 1281 |
if (!$unzip) |
| 1282 |
$unzip = "unzip"; |
| 1283 |
return $unzip; |
| 1284 |
} |
| 1285 |
|
| 1286 |
function check_backup_compat() |
| 1287 |
{ |
| 1288 |
$reqs = array(); |
| 1289 |
if (strpos($_SERVER['DOCUMENT_ROOT'], '/') === 0) { |
| 1290 |
$reqs['Server OS']['status'] = 'Linux (or compatible)'; |
| 1291 |
$reqs['Server OS']['pass'] = true; |
| 1292 |
} else { |
| 1293 |
$reqs['Server OS']['status'] = 'Windows'; |
| 1294 |
$reqs['Server OS']['pass'] = true; |
| 1295 |
$pass = false; |
| 1296 |
} |
| 1297 |
$reqs['PHP Version']['status'] = phpversion(); |
| 1298 |
if ((float) phpversion() >= 5.1) { |
| 1299 |
$reqs['PHP Version']['pass'] = true; |
| 1300 |
} else { |
| 1301 |
$reqs['PHP Version']['pass'] = false; |
| 1302 |
$pass = false; |
| 1303 |
} |
| 1304 |
|
| 1305 |
|
| 1306 |
if (is_writable(WP_CONTENT_DIR)) { |
| 1307 |
$reqs['Backup Folder']['status'] = "writable"; |
| 1308 |
$reqs['Backup Folder']['pass'] = true; |
| 1309 |
} else { |
| 1310 |
$reqs['Backup Folder']['status'] = "not writable"; |
| 1311 |
$reqs['Backup Folder']['pass'] = false; |
| 1312 |
} |
| 1313 |
|
| 1314 |
|
| 1315 |
$file_path = MWP_BACKUP_DIR; |
| 1316 |
$reqs['Backup Folder']['status'] .= ' (' . $file_path . ')'; |
| 1317 |
|
| 1318 |
if ($func = $this->check_sys()) { |
| 1319 |
$reqs['Execute Function']['status'] = $func; |
| 1320 |
$reqs['Execute Function']['pass'] = true; |
| 1321 |
} else { |
| 1322 |
$reqs['Execute Function']['status'] = "not found"; |
| 1323 |
$reqs['Execute Function']['info'] = "(will try PHP replacement)"; |
| 1324 |
$reqs['Execute Function']['pass'] = false; |
| 1325 |
} |
| 1326 |
$reqs['Zip']['status'] = $this->get_zip(); |
| 1327 |
|
| 1328 |
$reqs['Zip']['pass'] = true; |
| 1329 |
|
| 1330 |
|
| 1331 |
|
| 1332 |
$reqs['Unzip']['status'] = $this->get_unzip(); |
| 1333 |
|
| 1334 |
$reqs['Unzip']['pass'] = true; |
| 1335 |
|
| 1336 |
$paths = $this->check_mysql_paths(); |
| 1337 |
|
| 1338 |
if (!empty($paths['mysqldump'])) { |
| 1339 |
$reqs['MySQL Dump']['status'] = $paths['mysqldump']; |
| 1340 |
$reqs['MySQL Dump']['pass'] = true; |
| 1341 |
} else { |
| 1342 |
$reqs['MySQL Dump']['status'] = "not found"; |
| 1343 |
$reqs['MySQL Dump']['info'] = "(will try PHP replacement)"; |
| 1344 |
$reqs['MySQL Dump']['pass'] = false; |
| 1345 |
} |
| 1346 |
|
| 1347 |
$exec_time = ini_get('max_execution_time'); |
| 1348 |
$reqs['Execution time']['status'] = $exec_time ? $exec_time . "s" : 'unknown'; |
| 1349 |
$reqs['Execution time']['pass'] = true; |
| 1350 |
|
| 1351 |
$mem_limit = ini_get('memory_limit'); |
| 1352 |
$reqs['Memory limit']['status'] = $mem_limit ? $mem_limit : 'unknown'; |
| 1353 |
$reqs['Memory limit']['pass'] = true; |
| 1354 |
|
| 1355 |
|
| 1356 |
return $reqs; |
| 1357 |
} |
| 1358 |
|
| 1359 |
function email_backup($args) |
| 1360 |
{ |
| 1361 |
$email = $args['email']; |
| 1362 |
|
| 1363 |
if (!is_email($email)) { |
| 1364 |
return array( |
| 1365 |
'error' => 'Your email (' . $email . ') is not correct' |
| 1366 |
); |
| 1367 |
} |
| 1368 |
$backup_file = $args['file_path']; |
| 1369 |
$task_name = isset($args['task_name']) ? $args['task_name'] : ''; |
| 1370 |
if (file_exists($backup_file) && $email) { |
| 1371 |
$attachments = array( |
| 1372 |
$backup_file |
| 1373 |
); |
| 1374 |
$headers = 'From: ManageWP <no-reply@managewp.com>' . "\r\n"; |
| 1375 |
$subject = "ManageWP - " . $task_name . " - " . $this->site_name; |
| 1376 |
ob_start(); |
| 1377 |
$result = wp_mail($email, $subject, $subject, $headers, $attachments); |
| 1378 |
ob_end_clean(); |
| 1379 |
|
| 1380 |
} |
| 1381 |
|
| 1382 |
if (!$result) { |
| 1383 |
return array( |
| 1384 |
'error' => 'Email not sent. Maybe your backup is too big for email or email server is not available on your website.' |
| 1385 |
); |
| 1386 |
} |
| 1387 |
return true; |
| 1388 |
|
| 1389 |
} |
| 1390 |
|
| 1391 |
function ftp_backup($args) |
| 1392 |
{ |
| 1393 |
extract($args); |
| 1394 |
//Args: $ftp_username, $ftp_password, $ftp_hostname, $backup_file, $ftp_remote_folder, $ftp_site_folder |
| 1395 |
$port = $ftp_port ? $ftp_port : 21; //default port is 21 |
| 1396 |
if ($ftp_ssl) { |
| 1397 |
if (function_exists('ftp_ssl_connect')) { |
| 1398 |
$conn_id = ftp_ssl_connect($ftp_hostname,$port); |
| 1399 |
} else { |
| 1400 |
return array( |
| 1401 |
'error' => 'Your server doesn\'t support SFTP', |
| 1402 |
'partial' => 1 |
| 1403 |
); |
| 1404 |
} |
| 1405 |
} else { |
| 1406 |
if (function_exists('ftp_connect')) { |
| 1407 |
$conn_id = ftp_connect($ftp_hostname,$port); |
| 1408 |
if ($conn_id === false) { |
| 1409 |
return array( |
| 1410 |
'error' => 'Failed to connect to ' . $ftp_hostname, |
| 1411 |
'partial' => 1 |
| 1412 |
); |
| 1413 |
} |
| 1414 |
} else { |
| 1415 |
return array( |
| 1416 |
'error' => 'Your server doesn\'t support FTP', |
| 1417 |
'partial' => 1 |
| 1418 |
); |
| 1419 |
} |
| 1420 |
} |
| 1421 |
$login = @ftp_login($conn_id, $ftp_username, $ftp_password); |
| 1422 |
if ($login === false) { |
| 1423 |
return array( |
| 1424 |
'error' => 'FTP login failed for ' . $ftp_username . ', ' . $ftp_password, |
| 1425 |
'partial' => 1 |
| 1426 |
); |
| 1427 |
} |
| 1428 |
|
| 1429 |
if($ftp_passive){ |
| 1430 |
@ftp_pasv($conn_id,true); |
| 1431 |
} |
| 1432 |
|
| 1433 |
@ftp_mkdir($conn_id, $ftp_remote_folder); |
| 1434 |
if ($ftp_site_folder) { |
| 1435 |
$ftp_remote_folder .= '/' . $this->site_name; |
| 1436 |
} |
| 1437 |
@ftp_mkdir($conn_id, $ftp_remote_folder); |
| 1438 |
|
| 1439 |
$upload = @ftp_put($conn_id, $ftp_remote_folder . '/' . basename($backup_file), $backup_file, FTP_BINARY); |
| 1440 |
|
| 1441 |
if ($upload === false) { //Try ascii |
| 1442 |
$upload = @ftp_put($conn_id, $ftp_remote_folder . '/' . basename($backup_file), $backup_file, FTP_ASCII); |
| 1443 |
} |
| 1444 |
ftp_close($conn_id); |
| 1445 |
|
| 1446 |
if ($upload === false) { |
| 1447 |
return array( |
| 1448 |
'error' => 'Failed to upload file to FTP. Please check your specified path.', |
| 1449 |
'partial' => 1 |
| 1450 |
); |
| 1451 |
} |
| 1452 |
|
| 1453 |
return true; |
| 1454 |
} |
| 1455 |
|
| 1456 |
function remove_ftp_backup($args) |
| 1457 |
{ |
| 1458 |
extract($args); |
| 1459 |
//Args: $ftp_username, $ftp_password, $ftp_hostname, $backup_file, $ftp_remote_folder |
| 1460 |
$port = $ftp_port ? $ftp_port : 21; //default port is 21 |
| 1461 |
if ($ftp_ssl && function_exists('ftp_ssl_connect')) { |
| 1462 |
$conn_id = ftp_ssl_connect($ftp_hostname,$port); |
| 1463 |
} else if (function_exists('ftp_connect')) { |
| 1464 |
$conn_id = ftp_connect($ftp_hostname,$port); |
| 1465 |
} |
| 1466 |
|
| 1467 |
if ($conn_id) { |
| 1468 |
$login = @ftp_login($conn_id, $ftp_username, $ftp_password); |
| 1469 |
if ($ftp_site_folder) |
| 1470 |
$ftp_remote_folder .= '/' . $this->site_name; |
| 1471 |
|
| 1472 |
if($ftp_passive){ |
| 1473 |
@ftp_pasv($conn_id,true); |
| 1474 |
} |
| 1475 |
|
| 1476 |
$delete = ftp_delete($conn_id, $ftp_remote_folder . '/' . $backup_file); |
| 1477 |
|
| 1478 |
ftp_close($conn_id); |
| 1479 |
} |
| 1480 |
|
| 1481 |
} |
| 1482 |
|
| 1483 |
function get_ftp_backup($args) |
| 1484 |
{ |
| 1485 |
extract($args); |
| 1486 |
//Args: $ftp_username, $ftp_password, $ftp_hostname, $backup_file, $ftp_remote_folder |
| 1487 |
$port = $ftp_port ? $ftp_port : 21; //default port is 21 |
| 1488 |
if ($ftp_ssl && function_exists('ftp_ssl_connect')) { |
| 1489 |
$conn_id = ftp_ssl_connect($ftp_hostname,$port); |
| 1490 |
|
| 1491 |
} else if (function_exists('ftp_connect')) { |
| 1492 |
$conn_id = ftp_connect($ftp_hostname,$port); |
| 1493 |
if ($conn_id === false) { |
| 1494 |
return false; |
| 1495 |
} |
| 1496 |
} |
| 1497 |
$login = @ftp_login($conn_id, $ftp_username, $ftp_password); |
| 1498 |
if ($login === false) { |
| 1499 |
return false; |
| 1500 |
} else { |
| 1501 |
} |
| 1502 |
|
| 1503 |
if ($ftp_site_folder) |
| 1504 |
$ftp_remote_folder .= '/' . $this->site_name; |
| 1505 |
|
| 1506 |
if($ftp_passive){ |
| 1507 |
@ftp_pasv($conn_id,true); |
| 1508 |
} |
| 1509 |
|
| 1510 |
$temp = ABSPATH . 'mwp_temp_backup.zip'; |
| 1511 |
$get = ftp_get($conn_id, $temp, $ftp_remote_folder . '/' . $backup_file, FTP_BINARY); |
| 1512 |
if ($get === false) { |
| 1513 |
return false; |
| 1514 |
} else { |
| 1515 |
} |
| 1516 |
ftp_close($conn_id); |
| 1517 |
|
| 1518 |
return $temp; |
| 1519 |
} |
| 1520 |
|
| 1521 |
function dropbox_backup($args) |
| 1522 |
{ |
| 1523 |
|
| 1524 |
extract($args); |
| 1525 |
|
| 1526 |
if(isset($consumer_secret) && !empty($consumer_secret)){ |
| 1527 |
//New way |
| 1528 |
require_once('lib/dropbox.oauth.php'); |
| 1529 |
|
| 1530 |
$dropbox = new Dropbox($consumer_key, $consumer_secret); |
| 1531 |
$dropbox->setOAuthToken($oauth_token); |
| 1532 |
$dropbox->setOAuthTokenSecret($oauth_token_secret); |
| 1533 |
|
| 1534 |
if ($dropbox_site_folder == true) |
| 1535 |
$dropbox_destination .= '/' . $this->site_name; |
| 1536 |
|
| 1537 |
try{ |
| 1538 |
|
| 1539 |
$dropbox->filesPost($dropbox_destination, $backup_file,true); |
| 1540 |
|
| 1541 |
} catch(Exception $e){ |
| 1542 |
return array( |
| 1543 |
'error' => 'Dropbox upload error. '.$e->getMessage() |
| 1544 |
); |
| 1545 |
} |
| 1546 |
|
| 1547 |
return true; |
| 1548 |
|
| 1549 |
} else { |
| 1550 |
//old way |
| 1551 |
require_once('lib/dropbox.php'); |
| 1552 |
|
| 1553 |
//$email, $password, $backup_file, $destination, $dropbox_site_folder |
| 1554 |
|
| 1555 |
$size = ceil(filesize($backup_file) / 1024); |
| 1556 |
if ($size > 300000) { |
| 1557 |
return array( |
| 1558 |
'error' => 'Cannot upload file to Dropbox. Dropbox has upload limit of 300Mb per file.', |
| 1559 |
'partial' => 1 |
| 1560 |
); |
| 1561 |
} |
| 1562 |
|
| 1563 |
if ($dropbox_site_folder == true) |
| 1564 |
$dropbox_destination .= '/' . $this->site_name; |
| 1565 |
|
| 1566 |
try { |
| 1567 |
$uploader = new DropboxUploader($dropbox_username, $dropbox_password); |
| 1568 |
$uploader->upload($backup_file, $dropbox_destination); |
| 1569 |
} |
| 1570 |
catch (Exception $e) { |
| 1571 |
return array( |
| 1572 |
'error' => $e->getMessage(), |
| 1573 |
'partial' => 1 |
| 1574 |
); |
| 1575 |
} |
| 1576 |
|
| 1577 |
return true; |
| 1578 |
} |
| 1579 |
|
| 1580 |
} |
| 1581 |
|
| 1582 |
function remove_dropbox_backup($args){ |
| 1583 |
extract($args); |
| 1584 |
if(isset($consumer_secret) && !empty($consumer_secret)){ |
| 1585 |
//New way |
| 1586 |
require_once('lib/dropbox.oauth.php'); |
| 1587 |
|
| 1588 |
$dropbox = new Dropbox($consumer_key, $consumer_secret); |
| 1589 |
$dropbox->setOAuthToken($oauth_token); |
| 1590 |
$dropbox->setOAuthTokenSecret($oauth_token_secret); |
| 1591 |
|
| 1592 |
if ($dropbox_site_folder == true) |
| 1593 |
$dropbox_destination .= '/' . $this->site_name; |
| 1594 |
|
| 1595 |
try{ |
| 1596 |
$dropbox->fileopsDelete($dropbox_destination.'/'.$backup_file, true); |
| 1597 |
} catch(Exception $e){ |
| 1598 |
|
| 1599 |
} |
| 1600 |
} |
| 1601 |
} |
| 1602 |
|
| 1603 |
function get_dropbox_backup($args){ |
| 1604 |
extract($args); |
| 1605 |
|
| 1606 |
if(isset($consumer_secret) && !empty($consumer_secret)){ |
| 1607 |
//New way |
| 1608 |
require_once('lib/dropbox.oauth.php'); |
| 1609 |
|
| 1610 |
$dropbox = new Dropbox($consumer_key, $consumer_secret); |
| 1611 |
$dropbox->setOAuthToken($oauth_token); |
| 1612 |
$dropbox->setOAuthTokenSecret($oauth_token_secret); |
| 1613 |
|
| 1614 |
if ($dropbox_site_folder == true) |
| 1615 |
$dropbox_destination .= '/' . $this->site_name; |
| 1616 |
|
| 1617 |
$temp = ABSPATH . 'mwp_temp_backup.zip'; |
| 1618 |
|
| 1619 |
try{ |
| 1620 |
$file = $dropbox->filesGet($dropbox_destination.'/'.$backup_file, true); |
| 1621 |
|
| 1622 |
if(isset($file['data']) && !empty($file['data']) ) |
| 1623 |
$stream = base64_decode($file['data']); |
| 1624 |
else |
| 1625 |
return false; |
| 1626 |
|
| 1627 |
$handle = @fopen($temp, 'w+'); |
| 1628 |
$result = fwrite($handle,$stream); |
| 1629 |
fclose($handle); |
| 1630 |
|
| 1631 |
if($result) |
| 1632 |
return $temp; |
| 1633 |
else |
| 1634 |
return false; |
| 1635 |
|
| 1636 |
} catch(Exception $e){ |
| 1637 |
|
| 1638 |
|
| 1639 |
return false; |
| 1640 |
} |
| 1641 |
|
| 1642 |
} else { |
| 1643 |
return false; |
| 1644 |
} |
| 1645 |
|
| 1646 |
|
| 1647 |
} |
| 1648 |
|
| 1649 |
function amazons3_backup($args) |
| 1650 |
{ |
| 1651 |
if ($this->mmb_function_exists('curl_init')) { |
| 1652 |
require_once('lib/s3.php'); |
| 1653 |
extract($args); |
| 1654 |
|
| 1655 |
if ($as3_site_folder == true) |
| 1656 |
$as3_directory .= '/' . $this->site_name; |
| 1657 |
|
| 1658 |
$endpoint = isset($as3_bucket_region) ? $as3_bucket_region : 's3.amazonaws.com'; |
| 1659 |
try{ |
| 1660 |
$s3 = new mwpS3(trim($as3_access_key), trim(str_replace(' ', '+', $as3_secure_key)), false, $endpoint); |
| 1661 |
if ($s3->putObjectFile($backup_file, $as3_bucket, $as3_directory . '/' . basename($backup_file), mwpS3::ACL_PRIVATE)) { |
| 1662 |
return true; |
| 1663 |
} else { |
| 1664 |
|
| 1665 |
return array( |
| 1666 |
'error' => 'Failed to upload to Amazon S3. Please check your details and set upload/delete permissions on your bucket.', |
| 1667 |
'partial' => 1 |
| 1668 |
); |
| 1669 |
} |
| 1670 |
|
| 1671 |
}catch (Exception $e){ |
| 1672 |
$err = $e->getMessage(); |
| 1673 |
if($err){ |
| 1674 |
return array( |
| 1675 |
'error' => 'Failed to upload to AmazonS3 ('.$err.').' |
| 1676 |
); |
| 1677 |
} else { |
| 1678 |
return array( |
| 1679 |
'error' => 'Failed to upload to Amazon S3.' |
| 1680 |
); |
| 1681 |
} |
| 1682 |
} |
| 1683 |
|
| 1684 |
} else { |
| 1685 |
return array( |
| 1686 |
'error' => 'You cannot use Amazon S3 on your server. Please enable curl extension first.', |
| 1687 |
'partial' => 1 |
| 1688 |
); |
| 1689 |
} |
| 1690 |
} |
| 1691 |
|
| 1692 |
function remove_amazons3_backup($args) |
| 1693 |
{ |
| 1694 |
if ($this->mmb_function_exists('curl_init')) { |
| 1695 |
require_once('lib/s3.php'); |
| 1696 |
extract($args); |
| 1697 |
if ($as3_site_folder == true) |
| 1698 |
$as3_directory .= '/' . $this->site_name; |
| 1699 |
$endpoint = isset($as3_bucket_region) ? $as3_bucket_region : 's3.amazonaws.com'; |
| 1700 |
try{ |
| 1701 |
$s3 = new mwpS3(trim($as3_access_key), trim(str_replace(' ', '+', $as3_secure_key)), false, $endpoint); |
| 1702 |
$s3->deleteObject($as3_bucket, $as3_directory . '/' . $backup_file); |
| 1703 |
} catch (Exception $e){ |
| 1704 |
|
| 1705 |
} |
| 1706 |
} |
| 1707 |
} |
| 1708 |
|
| 1709 |
function get_amazons3_backup($args) |
| 1710 |
{ |
| 1711 |
require_once('lib/s3.php'); |
| 1712 |
extract($args); |
| 1713 |
$endpoint = isset($as3_bucket_region) ? $as3_bucket_region : 's3.amazonaws.com'; |
| 1714 |
$temp = ''; |
| 1715 |
try{ |
| 1716 |
$s3 = new mwpS3($as3_access_key, str_replace(' ', '+', $as3_secure_key), false, $endpoint); |
| 1717 |
if ($as3_site_folder == true) |
| 1718 |
$as3_directory .= '/' . $this->site_name; |
| 1719 |
|
| 1720 |
$temp = ABSPATH . 'mwp_temp_backup.zip'; |
| 1721 |
$s3->getObject($as3_bucket, $as3_directory . '/' . $backup_file, $temp); |
| 1722 |
} catch (Exception $e){ |
| 1723 |
return $temp; |
| 1724 |
} |
| 1725 |
return $temp; |
| 1726 |
} |
| 1727 |
|
| 1728 |
function schedule_next($type, $schedule) |
| 1729 |
{ |
| 1730 |
$schedule = explode("|", $schedule); |
| 1731 |
if (empty($schedule)) |
| 1732 |
return false; |
| 1733 |
switch ($type) { |
| 1734 |
|
| 1735 |
case 'daily': |
| 1736 |
|
| 1737 |
if (isset($schedule[1]) && $schedule[1]) { |
| 1738 |
$delay_time = $schedule[1] * 60; |
| 1739 |
} |
| 1740 |
|
| 1741 |
$current_hour = date("H"); |
| 1742 |
$schedule_hour = $schedule[0]; |
| 1743 |
if ($current_hour >= $schedule_hour) |
| 1744 |
$time = mktime($schedule_hour, 0, 0, date("m"), date("d") + 1, date("Y")); |
| 1745 |
else |
| 1746 |
$time = mktime($schedule_hour, 0, 0, date("m"), date("d"), date("Y")); |
| 1747 |
break; |
| 1748 |
|
| 1749 |
|
| 1750 |
case 'weekly': |
| 1751 |
if (isset($schedule[2]) && $schedule[2]) { |
| 1752 |
$delay_time = $schedule[2] * 60; |
| 1753 |
} |
| 1754 |
$current_weekday = date('w'); |
| 1755 |
$schedule_weekday = $schedule[1]; |
| 1756 |
$current_hour = date("H"); |
| 1757 |
$schedule_hour = $schedule[0]; |
| 1758 |
|
| 1759 |
if ($current_weekday > $schedule_weekday) |
| 1760 |
$weekday_offset = 7 - ($week_day - $task_schedule[1]); |
| 1761 |
else |
| 1762 |
$weekday_offset = $schedule_weekday - $current_weekday; |
| 1763 |
|
| 1764 |
|
| 1765 |
if (!$weekday_offset) { //today is scheduled weekday |
| 1766 |
if ($current_hour >= $schedule_hour) |
| 1767 |
$time = mktime($schedule_hour, 0, 0, date("m"), date("d") + 7, date("Y")); |
| 1768 |
else |
| 1769 |
$time = mktime($schedule_hour, 0, 0, date("m"), date("d"), date("Y")); |
| 1770 |
} else { |
| 1771 |
$time = mktime($schedule_hour, 0, 0, date("m"), date("d") + $weekday_offset, date("Y")); |
| 1772 |
} |
| 1773 |
|
| 1774 |
break; |
| 1775 |
|
| 1776 |
case 'monthly': |
| 1777 |
if (isset($schedule[2]) && $schedule[2]) { |
| 1778 |
$delay_time = $schedule[2] * 60; |
| 1779 |
} |
| 1780 |
$current_monthday = date('j'); |
| 1781 |
$schedule_monthday = $schedule[1]; |
| 1782 |
$current_hour = date("H"); |
| 1783 |
$schedule_hour = $schedule[0]; |
| 1784 |
|
| 1785 |
if ($current_monthday > $schedule_monthday) { |
| 1786 |
$time = mktime($schedule_hour, 0, 0, date("m") + 1, $schedule_monthday, date("Y")); |
| 1787 |
} else if ($current_monthday < $schedule_monthday) { |
| 1788 |
$time = mktime($schedule_hour, 0, 0, date("m"), $schedule_monthday, date("Y")); |
| 1789 |
} else if ($current_monthday == $schedule_monthday) { |
| 1790 |
if ($current_hour >= $schedule_hour) |
| 1791 |
$time = mktime($schedule_hour, 0, 0, date("m") + 1, $schedule_monthday, date("Y")); |
| 1792 |
else |
| 1793 |
$time = mktime($schedule_hour, 0, 0, date("m"), $schedule_monthday, date("Y")); |
| 1794 |
break; |
| 1795 |
} |
| 1796 |
|
| 1797 |
break; |
| 1798 |
default: |
| 1799 |
break; |
| 1800 |
} |
| 1801 |
|
| 1802 |
if (isset($delay_time) && $delay_time) { |
| 1803 |
$time += $delay_time; |
| 1804 |
} |
| 1805 |
|
| 1806 |
|
| 1807 |
return $time; |
| 1808 |
|
| 1809 |
} |
| 1810 |
|
| 1811 |
//Parse task arguments for info on master |
| 1812 |
function get_backup_stats() |
| 1813 |
{ |
| 1814 |
$stats = array(); |
| 1815 |
$tasks = $this->tasks; |
| 1816 |
if (is_array($tasks) && !empty($tasks)) { |
| 1817 |
foreach ($tasks as $task_name => $info) { |
| 1818 |
if (is_array($info['task_results']) && !empty($info['task_results'])) { |
| 1819 |
foreach ($info['task_results'] as $key => $result) { |
| 1820 |
if (isset($result['server']) && !isset($result['error'])) { |
| 1821 |
if (!file_exists($result['server']['file_path'])) { |
| 1822 |
$info['task_results'][$key]['error'] = 'Backup created but manually removed from server.'; |
| 1823 |
} |
| 1824 |
} |
| 1825 |
} |
| 1826 |
} |
| 1827 |
if (is_array($info['task_results'])) |
| 1828 |
$stats[$task_name] = array_values($info['task_results']); |
| 1829 |
|
| 1830 |
} |
| 1831 |
} |
| 1832 |
return $stats; |
| 1833 |
} |
| 1834 |
|
| 1835 |
function get_next_schedules() |
| 1836 |
{ |
| 1837 |
$stats = array(); |
| 1838 |
$tasks = $this->tasks; |
| 1839 |
if (is_array($tasks) && !empty($tasks)) { |
| 1840 |
foreach ($tasks as $task_name => $info) { |
| 1841 |
$stats[$task_name] = isset($info['task_args']['next']) ? $info['task_args']['next'] : array(); |
| 1842 |
} |
| 1843 |
} |
| 1844 |
return $stats; |
| 1845 |
} |
| 1846 |
|
| 1847 |
function remove_old_backups($task_name) |
| 1848 |
{ |
| 1849 |
|
| 1850 |
//Check for previous failed backups first |
| 1851 |
$this->cleanup(); |
| 1852 |
|
| 1853 |
//Remove by limit |
| 1854 |
$backups = $this->tasks; |
| 1855 |
if ($task_name == 'Backup Now') { |
| 1856 |
$num = 0; |
| 1857 |
} else { |
| 1858 |
$num = 1; |
| 1859 |
} |
| 1860 |
|
| 1861 |
|
| 1862 |
if ((count($backups[$task_name]['task_results']) - $num) >= $backups[$task_name]['task_args']['limit']) { |
| 1863 |
//how many to remove ? |
| 1864 |
$remove_num = (count($backups[$task_name]['task_results']) - $num - $backups[$task_name]['task_args']['limit']) + 1; |
| 1865 |
for ($i = 0; $i < $remove_num; $i++) { |
| 1866 |
//Remove from the server |
| 1867 |
if (isset($backups[$task_name]['task_results'][$i]['server'])) { |
| 1868 |
@unlink($backups[$task_name]['task_results'][$i]['server']['file_path']); |
| 1869 |
} |
| 1870 |
|
| 1871 |
//Remove from ftp |
| 1872 |
if (isset($backups[$task_name]['task_results'][$i]['ftp']) && isset($backups[$task_name]['task_args']['account_info']['mwp_ftp'])) { |
| 1873 |
$ftp_file = $backups[$task_name]['task_results'][$i]['ftp']; |
| 1874 |
$args = $backups[$task_name]['task_args']['account_info']['mwp_ftp']; |
| 1875 |
$args['backup_file'] = $ftp_file; |
| 1876 |
$this->remove_ftp_backup($args); |
| 1877 |
} |
| 1878 |
|
| 1879 |
if (isset($backups[$task_name]['task_results'][$i]['amazons3']) && isset($backups[$task_name]['task_args']['account_info']['mwp_amazon_s3'])) { |
| 1880 |
$amazons3_file = $backups[$task_name]['task_results'][$i]['amazons3']; |
| 1881 |
$args = $backups[$task_name]['task_args']['account_info']['mwp_amazon_s3']; |
| 1882 |
$args['backup_file'] = $amazons3_file; |
| 1883 |
$this->remove_amazons3_backup($args); |
| 1884 |
} |
| 1885 |
|
| 1886 |
if (isset($backups[$task_name]['task_results'][$i]['dropbox']) && isset($backups[$task_name]['task_args']['account_info']['mwp_dropbox'])) { |
| 1887 |
//To do: dropbox remove |
| 1888 |
$dropbox_file = $backups[$task_name]['task_results'][$i]['dropbox']; |
| 1889 |
$args = $backups[$task_name]['task_args']['account_info']['mwp_dropbox']; |
| 1890 |
$args['backup_file'] = $dropbox_file; |
| 1891 |
$this->remove_dropbox_backup($args); |
| 1892 |
} |
| 1893 |
|
| 1894 |
//Remove database backup info |
| 1895 |
unset($backups[$task_name]['task_results'][$i]); |
| 1896 |
|
| 1897 |
} //end foreach |
| 1898 |
|
| 1899 |
if (is_array($backups[$task_name]['task_results'])) |
| 1900 |
$backups[$task_name]['task_results'] = array_values($backups[$task_name]['task_results']); |
| 1901 |
else |
| 1902 |
$backups[$task_name]['task_results']=array(); |
| 1903 |
|
| 1904 |
$this->update_tasks($backups); |
| 1905 |
//update_option('mwp_backup_tasks', $backups); |
| 1906 |
} |
| 1907 |
} |
| 1908 |
|
| 1909 |
/** |
| 1910 |
* Delete specified backup |
| 1911 |
* Args: $task_name, $result_id |
| 1912 |
*/ |
| 1913 |
|
| 1914 |
function delete_backup($args) |
| 1915 |
{ |
| 1916 |
if (empty($args)) |
| 1917 |
return false; |
| 1918 |
extract($args); |
| 1919 |
|
| 1920 |
$tasks = $this->tasks; |
| 1921 |
$task = $tasks[$task_name]; |
| 1922 |
$backups = $task['task_results']; |
| 1923 |
$backup = $backups[$result_id]; |
| 1924 |
|
| 1925 |
if (isset($backup['server'])) { |
| 1926 |
@unlink($backup['server']['file_path']); |
| 1927 |
} |
| 1928 |
|
| 1929 |
//Remove from ftp |
| 1930 |
if (isset($backup['ftp'])) { |
| 1931 |
$ftp_file = $backup['ftp']; |
| 1932 |
$args = $tasks[$task_name]['task_args']['account_info']['mwp_ftp']; |
| 1933 |
$args['backup_file'] = $ftp_file; |
| 1934 |
$this->remove_ftp_backup($args); |
| 1935 |
} |
| 1936 |
|
| 1937 |
if (isset($backup['amazons3'])) { |
| 1938 |
$amazons3_file = $backup['amazons3']; |
| 1939 |
$args = $tasks[$task_name]['task_args']['account_info']['mwp_amazon_s3']; |
| 1940 |
$args['backup_file'] = $amazons3_file; |
| 1941 |
$this->remove_amazons3_backup($args); |
| 1942 |
} |
| 1943 |
|
| 1944 |
if (isset($backup['dropbox'])) { |
| 1945 |
$dropbox_file = $backup['dropbox']; |
| 1946 |
$args = $tasks[$task_name]['task_args']['account_info']['mwp_dropbox']; |
| 1947 |
$args['backup_file'] = $dropbox_file; |
| 1948 |
$this->remove_dropbox_backup($args); |
| 1949 |
} |
| 1950 |
|
| 1951 |
unset($backups[$result_id]); |
| 1952 |
|
| 1953 |
if (count($backups)) { |
| 1954 |
$tasks[$task_name]['task_results'] = $backups; |
| 1955 |
} else { |
| 1956 |
unset($tasks[$task_name]['task_results']); |
| 1957 |
} |
| 1958 |
|
| 1959 |
$this->update_tasks($tasks); |
| 1960 |
//update_option('mwp_backup_tasks', $tasks); |
| 1961 |
return true; |
| 1962 |
|
| 1963 |
} |
| 1964 |
|
| 1965 |
function cleanup() |
| 1966 |
{ |
| 1967 |
$tasks = $this->tasks; |
| 1968 |
$backup_folder = WP_CONTENT_DIR . '/' . md5('mmb-worker') . '/mwp_backups/'; |
| 1969 |
$backup_folder_new = MWP_BACKUP_DIR . '/'; |
| 1970 |
$files = glob($backup_folder . "*"); |
| 1971 |
$new = glob($backup_folder_new . "*"); |
| 1972 |
|
| 1973 |
//Failed db files first |
| 1974 |
$db_folder = MWP_DB_DIR . '/'; |
| 1975 |
$db_files = glob($db_folder . "*"); |
| 1976 |
if (is_array($db_files) && !empty($db_files)) { |
| 1977 |
foreach ($db_files as $file) { |
| 1978 |
@unlink($file); |
| 1979 |
} |
| 1980 |
@rmdir(MWP_DB_DIR); |
| 1981 |
} |
| 1982 |
|
| 1983 |
|
| 1984 |
//clean_old folder? |
| 1985 |
if ((isset($files[0]) && basename($files[0]) == 'index.php' && count($files) == 1) || (empty($files))) { |
| 1986 |
foreach ($files as $file) { |
| 1987 |
@unlink($file); |
| 1988 |
} |
| 1989 |
@rmdir(WP_CONTENT_DIR . '/' . md5('mmb-worker') . '/mwp_backups'); |
| 1990 |
@rmdir(WP_CONTENT_DIR . '/' . md5('mmb-worker')); |
| 1991 |
} |
| 1992 |
|
| 1993 |
|
| 1994 |
foreach ($new as $b) { |
| 1995 |
$files[] = $b; |
| 1996 |
} |
| 1997 |
$deleted = array(); |
| 1998 |
|
| 1999 |
if (is_array($files) && count($files)) { |
| 2000 |
$results = array(); |
| 2001 |
if (!empty($tasks)) { |
| 2002 |
foreach ((array) $tasks as $task) { |
| 2003 |
if (isset($task['task_results']) && count($task['task_results'])) { |
| 2004 |
foreach ($task['task_results'] as $backup) { |
| 2005 |
if (isset($backup['server'])) { |
| 2006 |
$results[] = $backup['server']['file_path']; |
| 2007 |
} |
| 2008 |
} |
| 2009 |
} |
| 2010 |
} |
| 2011 |
} |
| 2012 |
|
| 2013 |
$num_deleted = 0; |
| 2014 |
foreach ($files as $file) { |
| 2015 |
if (!in_array($file, $results) && basename($file) != 'index.php') { |
| 2016 |
@unlink($file); |
| 2017 |
$deleted[] = basename($file); |
| 2018 |
$num_deleted++; |
| 2019 |
} |
| 2020 |
} |
| 2021 |
} |
| 2022 |
|
| 2023 |
|
| 2024 |
|
| 2025 |
return $deleted; |
| 2026 |
} |
| 2027 |
|
| 2028 |
|
| 2029 |
function remote_backup_now($args) |
| 2030 |
{ |
| 2031 |
@ini_set('memory_limit', '256M'); |
| 2032 |
@set_time_limit(600); //ten minutes |
| 2033 |
|
| 2034 |
|
| 2035 |
|
| 2036 |
if (!empty($args)) |
| 2037 |
extract($args); |
| 2038 |
|
| 2039 |
$tasks = $this->tasks; |
| 2040 |
$task = $tasks['Backup Now']; |
| 2041 |
|
| 2042 |
if (!empty($task)) { |
| 2043 |
extract($task['task_args']); |
| 2044 |
} |
| 2045 |
|
| 2046 |
$results = $task['task_results']; |
| 2047 |
|
| 2048 |
if (is_array($results) && count($results)) { |
| 2049 |
$backup_file = $results[count($results) - 1]['server']['file_path']; |
| 2050 |
} |
| 2051 |
|
| 2052 |
if ($backup_file && file_exists($backup_file)) { |
| 2053 |
//FTP, Amazon S3 or Dropbox |
| 2054 |
if (isset($account_info['mwp_ftp']) && !empty($account_info['mwp_ftp'])) { |
| 2055 |
$account_info['mwp_ftp']['backup_file'] = $backup_file; |
| 2056 |
$return = $this->ftp_backup($account_info['mwp_ftp']); |
| 2057 |
} |
| 2058 |
|
| 2059 |
if (isset($account_info['mwp_amazon_s3']) && !empty($account_info['mwp_amazon_s3'])) { |
| 2060 |
$account_info['mwp_amazon_s3']['backup_file'] = $backup_file; |
| 2061 |
$return = $this->amazons3_backup($account_info['mwp_amazon_s3']); |
| 2062 |
} |
| 2063 |
|
| 2064 |
if (isset($account_info['mwp_dropbox']) && !empty($account_info['mwp_dropbox'])) { |
| 2065 |
$account_info['mwp_dropbox']['backup_file'] = $backup_file; |
| 2066 |
$return = $this->dropbox_backup($account_info['mwp_dropbox']); |
| 2067 |
} |
| 2068 |
|
| 2069 |
if (isset($account_info['mwp_email']) && !empty($account_info['mwp_email'])) { |
| 2070 |
$account_info['mwp_email']['file_path'] = $backup_file; |
| 2071 |
$account_info['mwp_email']['task_name'] = 'Backup Now'; |
| 2072 |
$return = $this->email_backup($account_info['mwp_email']); |
| 2073 |
} |
| 2074 |
|
| 2075 |
|
| 2076 |
if ($return == true && $del_host_file) { |
| 2077 |
@unlink($backup_file); |
| 2078 |
unset($tasks['Backup Now']['task_results'][count($results) - 1]['server']); |
| 2079 |
$this->update_tasks($tasks); |
| 2080 |
//update_option('mwp_backup_tasks', $tasks); |
| 2081 |
} |
| 2082 |
|
| 2083 |
|
| 2084 |
|
| 2085 |
} else { |
| 2086 |
$return = array( |
| 2087 |
'error' => 'Backup file not found on your server. Please try again.' |
| 2088 |
); |
| 2089 |
} |
| 2090 |
|
| 2091 |
return $return; |
| 2092 |
|
| 2093 |
} |
| 2094 |
|
| 2095 |
function validate_task($args, $url) |
| 2096 |
{ |
| 2097 |
if (!class_exists('WP_Http')) { |
| 2098 |
include_once(ABSPATH . WPINC . '/class-http.php'); |
| 2099 |
} |
| 2100 |
$params = array(); |
| 2101 |
$params['body'] = $args; |
| 2102 |
$result = wp_remote_post($url, $params); |
| 2103 |
if (is_array($result) && $result['body'] == 'mwp_delete_task') { |
| 2104 |
//$tasks = $this->get_backup_settings(); |
| 2105 |
$tasks = $this->tasks; |
| 2106 |
unset($tasks[$args['task_name']]); |
| 2107 |
$this->update_tasks($tasks); |
| 2108 |
$this->cleanup(); |
| 2109 |
exit; |
| 2110 |
} elseif(is_array($result) && $result['body'] == 'mwp_pause_task'){ |
| 2111 |
return 'paused'; |
| 2112 |
} |
| 2113 |
|
| 2114 |
return 'ok'; |
| 2115 |
} |
| 2116 |
|
| 2117 |
function update_status($task_name, $status, $completed = false) |
| 2118 |
{ |
| 2119 |
/* Statuses: |
| 2120 |
0 - Backup started |
| 2121 |
1 - DB dump |
| 2122 |
2 - DB ZIP |
| 2123 |
3 - Files ZIP |
| 2124 |
4 - Amazon S3 |
| 2125 |
5 - Dropbox |
| 2126 |
6 - FTP |
| 2127 |
7 - Email |
| 2128 |
100 - Finished |
| 2129 |
*/ |
| 2130 |
if ($task_name != 'Backup Now') { |
| 2131 |
$tasks = $this->tasks; |
| 2132 |
$index = count($tasks[$task_name]['task_results']) - 1; |
| 2133 |
if (!is_array($tasks[$task_name]['task_results'][$index]['status'])) { |
| 2134 |
$tasks[$task_name]['task_results'][$index]['status'] = array(); |
| 2135 |
} |
| 2136 |
if (!$completed) { |
| 2137 |
$tasks[$task_name]['task_results'][$index]['status'][] = (int) $status * (-1); |
| 2138 |
} else { |
| 2139 |
$status_index = count($tasks[$task_name]['task_results'][$index]['status']) - 1; |
| 2140 |
$tasks[$task_name]['task_results'][$index]['status'][$status_index] = abs($tasks[$task_name]['task_results'][$index]['status'][$status_index]); |
| 2141 |
} |
| 2142 |
|
| 2143 |
$this->update_tasks($tasks); |
| 2144 |
//update_option('mwp_backup_tasks',$tasks); |
| 2145 |
} |
| 2146 |
} |
| 2147 |
|
| 2148 |
function update_tasks($tasks) |
| 2149 |
{ |
| 2150 |
$this->tasks = $tasks; |
| 2151 |
update_option('mwp_backup_tasks', $tasks); |
| 2152 |
} |
| 2153 |
|
| 2154 |
function wpdb_reconnect(){ |
| 2155 |
global $wpdb; |
| 2156 |
//Reconnect to avoid timeout problem after ZIP files |
| 2157 |
if(class_exists('wpdb') && function_exists('wp_set_wpdb_vars')){ |
| 2158 |
@mysql_close($wpdb->dbh); |
| 2159 |
$wpdb = new wpdb( DB_USER, DB_PASSWORD, DB_NAME, DB_HOST ); |
| 2160 |
wp_set_wpdb_vars(); |
| 2161 |
} |
| 2162 |
} |
| 2163 |
|
| 2164 |
function replace_htaccess($url) |
| 2165 |
{ |
| 2166 |
$file = @file_get_contents(ABSPATH.'.htaccess'); |
| 2167 |
if ($file && strlen($file)) { |
| 2168 |
$args = parse_url($url); |
| 2169 |
$string = rtrim($args['path'], "/"); |
| 2170 |
$regex = "/BEGIN WordPress(.*?)RewriteBase(.*?)\n(.*?)RewriteRule \.(.*?)index\.php(.*?)END WordPress/sm"; |
| 2171 |
$replace = "BEGIN WordPress$1RewriteBase " . $string . "/ \n$3RewriteRule . " . $string . "/index.php$5END WordPress"; |
| 2172 |
$file = preg_replace($regex, $replace, $file); |
| 2173 |
@file_put_contents(ABSPATH.'.htaccess', $file); |
| 2174 |
} |
| 2175 |
} |
| 2176 |
|
| 2177 |
function check_cron_remove(){ |
| 2178 |
if(empty($this->tasks) || (count($this->tasks) == 1 && isset($this->tasks['Backup Now'])) ){ |
| 2179 |
wp_clear_scheduled_hook('mwp_backup_tasks'); |
| 2180 |
exit; |
| 2181 |
} |
| 2182 |
} |
| 2183 |
|
| 2184 |
} |
| 2185 |
|
| 2186 |
?> |