PluginProbe
ManageWP Worker / 3.6.3
ManageWP Worker v3.6.3
4.9.38 4.9.37 4.9.36 4.9.35 4.9.34 3.8.7 3.8.8 3.9.0 3.9.1 3.9.10 3.9.11 3.9.12 3.9.13 3.9.14 3.9.15 3.9.16 3.9.17 3.9.18 3.9.19 3.9.2 3.9.20 3.9.21 3.9.22 3.9.23 3.9.24 All 73 releases
worker / backup.class.php

backup.class.php in ManageWP Worker 3.6.3, at backup.class.php

655 lines 21.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class Mmb_Backup extends Mmb_Core {
4
5 function __construct() {
6 $upload_dir = wp_upload_dir();
7 $sec_string = md5('mwp-worker');
8 $file = "/$sec_string/backups";
9 $file_path = $upload_dir['basedir'] . $file;
10 file_put_contents($file_path . '/index.php', '');
11
12 if(!file_exists($file_path)){
13 mkdir($file_path, 0755, true);
14 }
15 parent::__construct();
16 }
17
18 function backup($args) {
19
20 $this->_escape($args);
21 $username = $args[0];
22 $password = $args[1];
23 //type like manual, weekly, daily
24 $type = $args[2];
25 //what like full, only db, only wp-content
26 $what = $args[3];
27 if(trim($type) == '')
28 $type = 'manual';
29
30 if (!$user = $this->login($username, $password))
31 {
32 return $this->error;
33 }
34
35 if(!current_user_can('administrator'))
36 return new IXR_Error(401, 'Sorry, you are not allowed to perform backup action on this site.');
37
38 $upload_dir = wp_upload_dir();
39 $sec_string = md5('mwp-worker');
40 $file = "/$sec_string/backups";
41 $file_path = $upload_dir['basedir'] . $file;
42
43 if(!file_exists($file_path)){
44 mkdir($file_path, 0755, true);
45 }
46
47 if(trim($what) == 'full' || trim($what) == 'content' ){
48 //take wp-conetnt backup
49 $content_backup = $this->backup_wpcontent($type);
50 if(!$content_backup){
51 @unlink($content_backup['path']);
52 return FALSE;
53 }
54 }
55
56 if(trim($what) == 'full' || trim($what) == 'db' ){
57 //take batabase backup
58 $db_backup = $this->backup_db($type);
59 if(!$db_backup){
60 if(trim($what) == 'full')
61 @unlink($content_backup['path']);
62
63 @unlink($db_backup['path']);
64 return false;
65 }
66 }
67 require_once ABSPATH.'/wp-admin/includes/class-pclzip.php';
68
69 // Get previous backup in tmp
70 $worker_options = get_option('mwp-worker');
71 $tmp_file = $upload_dir['basedir'].'/'.basename($worker_options['backups'][$type]['path']);
72
73 if(rename($worker_options['backups'][$type]['path'], $tmp_file)){
74 @unlink($worker_options['backups'][$type]['path']);
75 }
76
77 $site_name = get_bloginfo('name');
78
79 $site_name = str_replace(array(" ","_"),array("",""),$site_name);
80
81 $backup_file = $file_path.'/'.$site_name.'_'.$type.'_'.$what.'_'.date('Y-m-d').'.zip';
82
83
84 if (!$this->check_zip())
85 {
86 $archive = new PclZip($backup_file);
87
88 }
89
90 if(trim($what) == 'full'){
91
92 if ($this->check_zip())
93 {
94 $command = "zip $backup_file -j $content_backup[path] -j $db_backup[path]";
95 ob_start();
96 passthru($command,$err);
97 ob_get_clean();
98 }
99 else
100 {
101 $result = $archive->add($content_backup['path'], PCLZIP_OPT_REMOVE_ALL_PATH);
102 $result = $archive->add($db_backup['path'], PCLZIP_OPT_REMOVE_ALL_PATH);
103 $err = !$result;
104 }
105
106 }elseif(trim($what) == 'db'){
107
108 if ($this->check_zip())
109 {
110
111 $command = "zip $backup_file -j $db_backup[path]";
112 ob_start();
113 passthru($command,$err);
114 ob_get_clean();
115
116 }
117 else
118 {
119
120 $result = $archive->add($db_backup['path'], PCLZIP_OPT_REMOVE_ALL_PATH);
121 $err = !$result;
122 }
123
124
125 }elseif(trim($what) == 'content'){
126 if ($this->check_zip())
127 {
128 $command = "zip $backup_file -j $content_backup[path]";
129 ob_start();
130 passthru($command,$err);
131 ob_get_clean();
132 }
133 else
134 {
135 $result = $archive->add($content_backup['path'], PCLZIP_OPT_REMOVE_ALL_PATH);
136 $err = !$result;
137 }
138
139
140 }
141
142
143 if($err){
144
145 if(rename($tmp_file,$worker_options['backups'][$type]['path'])){
146 @unlink($tmp_file);
147 }
148 return false;
149 }
150
151 @unlink($tmp_file);
152
153 @unlink($content_backup['path']);
154 @unlink($db_backup['path']);
155
156 $backup_url = $upload_dir['baseurl'].$file.'/'.$site_name.'_'.$type.'_'.$what.'_'.date('Y-m-d').'.zip';
157 $worker_options = get_option('mwp-worker');
158
159 //remove old file
160 if($worker_options['backups'][$type]['path'] != $backup_file)
161 @unlink($worker_options['backups'][$type]['path']);
162
163
164 $worker_options['backups'][$type]['path'] = $backup_file;
165 $worker_options['backups'][$type]['url'] = $backup_url;
166 update_option('mwp-worker', $worker_options);
167
168
169 return $worker_options['backups'][$type]['url'];
170 }
171
172 function backup_wpcontent($type) {
173
174 $upload_dir = wp_upload_dir();
175 $sec_string = md5('mwp-worker');
176 // $file = "/$sec_string/backups";
177 $file = '/'.$sec_string.'/backups/wp-content_'.date('Y-m-d').'.zip';
178 $file_path = $upload_dir['basedir'] . $file;
179 if ($this->check_zip())
180 {
181 chdir(WP_CONTENT_DIR);
182 $command = "zip -r $file_path 'plugins/' 'themes/' 'uploads/' -x 'uploads/".$sec_string."/*'";
183 ob_start();
184 passthru($command,$err);
185 ob_get_clean();
186
187 if(!$err){
188 $file_url = $upload_dir['baseurl'] . $file;
189 return array('path' => $file_path, 'url' => $file_url);
190 }
191 @unlink($file_path);
192 return false;
193
194 }
195 else
196 {
197 require_once ABSPATH.'/wp-admin/includes/class-pclzip.php';
198 $archive = new PclZip($file_path);
199 $result = $archive->add(WP_CONTENT_DIR . '/plugins', PCLZIP_OPT_REMOVE_PATH, WP_CONTENT_DIR);
200 $result = $archive->add(WP_CONTENT_DIR . '/themes', PCLZIP_OPT_REMOVE_PATH, WP_CONTENT_DIR);
201 $result = $archive->add(WP_CONTENT_DIR . '/uploads', PCLZIP_OPT_REMOVE_PATH, WP_CONTENT_DIR);
202 // $this->_log($archive);
203 $result = $archive->delete(PCLZIP_OPT_BY_NAME, 'uploads/'.$sec_string.'/');
204 if($result){
205 $file_url = $upload_dir['baseurl'] . $file;
206 return array('path' => $file_path, 'url' => $file_url);
207 }
208 @unlink($file_path);
209 return false;
210 }
211 }
212
213
214 function backup_db($type) {
215 $mysqldump_exists = $this->check_mysqldump();
216
217
218 if(is_array($mysqldump_exists)){
219 // $this->_log('backup dump');
220
221 $result = $this->backup_db_dump($type, $mysqldump_exists);
222
223 }else{
224
225 $result = $this->backup_db_php($type);
226 }
227 return $result;
228 }
229
230 function backup_db_dump($type, $paths) {
231 global $wpdb;
232 $upload_dir = wp_upload_dir();
233 $sec_string = md5('mwp-worker');
234 $brace = (substr(PHP_OS, 0, 3) == 'WIN') ? '"' : '';
235
236 $file = $upload_dir['path'] . '/'.DB_NAME.'.sql';
237 $file_url = $upload_dir['baseurl']. '/'.DB_NAME.'.sql';
238
239 $command = $brace.$paths['mysqldump'].$brace.' --host="'.DB_HOST.'" --user="'.DB_USER.'" --password="'.DB_PASSWORD.'" --add-drop-table --skip-lock-tables "'.DB_NAME.'" > '.$brace.$file.$brace;
240
241
242 passthru($command, $error);
243
244 if(filesize($file) == 0 || !is_file($file) || $error) {
245
246 @unlink($file);
247 return false;
248 }else{
249
250 return array('path' => $file, 'url' => $file_url);
251 }
252 }
253
254 function backup_db_php($type) {
255 global $wpdb;
256 //require_once ABSPATH.'/wp-admin/includes/class-pclzip.php';
257 $tables = $wpdb->get_results('SHOW TABLES', ARRAY_N);
258 $upload_dir = wp_upload_dir();
259 $sec_string = md5('mwp-worker');
260 $zip_file = '/'.$sec_string.'/backups/db_'.date('Y-m-d').'.zip';
261 $zip_file_path = $upload_dir['basedir'] . $zip_file;
262
263 $file = $upload_dir['path'] . '/'.DB_NAME.'.sql';
264 $file_url = $upload_dir['baseurl']. '/'.DB_NAME.'.sql';
265
266 // $archive = new PclZip($zip_file_path);
267 // $this->_log($zip_file_path);
268 // $this->_log($tables);
269 foreach($tables as $table){
270 //$file = $upload_dir['path'] . '/'.$table[0].'.sql';
271
272 //drop exixting table
273 $dump_data = "DROP TABLE IF EXISTS $table[0];";
274 //create table
275 $create_table = $wpdb->get_row("SHOW CREATE TABLE $table[0]", ARRAY_N);
276 $dump_data .= "\n\n".$create_table[1].";\n\n";
277
278 $count = $wpdb->get_var("SELECT count(*) FROM $table[0]");
279 if($count > 100)
280 $count = ceil($count/100)-1;
281 else
282 $count = 1;
283 for($i=0;$i<$count;$i++){
284 $low_limit = $i * 100;
285 $qry = "SELECT * FROM $table[0] LIMIT $low_limit, 100";
286 $rows = $wpdb->get_results($qry, ARRAY_A);
287 if(is_array($rows)){
288 foreach ($rows as $row) {
289 //insert single row
290 $dump_data .= "INSERT INTO $table[0] VALUES(";
291 $num_values = count($row);
292 $j = 1;
293 foreach ($row as $value) {
294 $value = addslashes($value);
295 $value = ereg_replace("\n", "\\n", $value);
296 // $dump_data .= "'".$value . "', ";
297
298 // if($num_values == $i)
299 // $dump_data .= "'".$value . "'";
300 // else
301 // $dump_data .= "'".$value . "', ";
302 $num_values == $j ? $dump_data .= "'".$value . "'" : $dump_data .= "'".$value . "', ";
303 $j ++;
304 unset ($value);
305 }
306 // $dump_data = substr($dump_data, 0, -2);
307 $dump_data .= ");\n";
308 }
309 }
310 }
311 $dump_data .= "\n\n\n";
312
313 unset ($rows);
314 file_put_contents($file, $dump_data, FILE_APPEND);
315 unset ($dump_data);
316
317
318 }
319
320
321 //return false;
322 return array('path' => $file, 'url' => $file_url);
323
324 }
325
326 function restore($args) {
327 $this->_escape($args);
328 $username = $args[0];
329 $password = $args[1];
330 $type = $args[2];
331
332 if(trim($type) == ''){
333 return false;
334 }
335
336 if (!$user = $this->login($username, $password))
337 {
338 return $this->error;
339 }
340
341 if(!current_user_can('administrator'))
342 return new IXR_Error(401, 'Sorry, you are not allowed to perform restore action on this site.');
343
344 // Set paths
345
346 $upload_dir = wp_upload_dir();
347 $sec_string = md5('mwp-worker');
348 $backup_dir = "/$sec_string/backups";
349 $file = "/$sec_string/restore";
350 $file_path = $upload_dir['basedir'] . $file; //restore path - temporary
351 $backup_path = $upload_dir['basedir'] . $backup_dir; //backup path
352
353
354 // If manual backup - get backup file from master, if not - get backup file from worker
355
356 if($type != 'weekly' && $type != 'daily')
357 {
358 // Download backup file from master
359 $tmp_file = download_url($type);
360
361 $backup_file = $backup_path."/". basename($type);
362
363
364 if(rename($tmp_file, $backup_file)){
365 @unlink($tmp_file);
366 }else{
367 $backup_file = $tmp_file;
368 }
369 }
370 else
371 {
372 // Getting file from worker
373 $backup_file = $worker_options['backups'][$type]['path'];
374 }
375
376
377 // $backup_file = glob($file_path);
378 // $backup_file = $backup_file[0];
379 if($backup_file){
380
381 if($this->check_unzip())
382 {
383 mkdir($file_path);
384 chdir($file_path);
385 $command = "unzip -o $backup_file";
386 ob_start();
387 passthru($command,$err);
388 ob_get_clean();
389 }
390 else
391 {
392 require_once ABSPATH.'/wp-admin/includes/class-pclzip.php';
393 $archive = new PclZip($backup_file);
394 $extracted = $archive->extract(PCLZIP_OPT_PATH, $file_path, PCLZIP_OPT_REMOVE_ALL_PATH);
395 $err = !$extracted;
396 }
397
398
399 if($err){
400
401 return false;
402 }
403
404 list(, $name, $what) = explode('_', basename($backup_file, '.zip'));
405
406 $this->_log($what);
407
408
409 if(trim($what) == 'full' || trim($what) == 'db' ){
410
411 if(!$this->restore_db($type, $file_path)){
412 return false;
413 }
414 }
415
416
417
418 if(trim($what) == 'full' || trim($what) == 'content' ){
419 if(!$this->restore_wpcontent($type, $file_path)){
420 return false;
421 }
422 }
423
424
425
426
427 $this->_deleteTempDir($file_path);
428 }
429 //@lk : better error messages
430 return true;
431
432 }
433
434 function restore_wpcontent($type, $file_path) {
435 require_once ABSPATH.'/wp-admin/includes/class-pclzip.php';
436 $content_file = glob($file_path."/wp*");
437
438
439 // $this->_log($content_file);
440 if($this->check_unzip())
441 {
442
443 chdir(WP_CONTENT_DIR);
444 $con_file = $content_file[0];
445 $command = "unzip -o $con_file";
446 ob_start();
447 passthru($command,$err);
448 ob_get_clean();
449
450 }
451 else
452 {
453 $archive = new PclZip($content_file[0]);
454 $restore_content = $archive->extract(PCLZIP_OPT_PATH, WP_CONTENT_DIR, PCLZIP_OPT_REPLACE_NEWER);
455 $err = !$restore_content;
456 }
457 // $this->_log($archive);
458 // $this->_log($archive->errorInfo(TRUE));
459 if($err)
460 return FALSE;
461 else
462 return TRUE;
463 }
464
465 function restore_db($type, $file_path) {
466 global $wpdb;
467
468 $mysqldump = $this->check_mysqldump();
469
470 //$mysqldump = ''; //debug
471 if(is_array($mysqldump))
472 {
473
474 $brace = (substr(PHP_OS, 0, 3) == 'WIN') ? '"' : '';
475
476 foreach(glob($file_path.'/*.sql') as $filename){
477
478 $command = $brace.$mysqldump['mysql'].$brace.' --host="'.DB_HOST.'" --user="'.DB_USER.'" --password="'.DB_PASSWORD.'" '.DB_NAME.' < '.$brace.$filename.$brace;
479 passthru($command, $error);
480
481 break;
482 }
483 return $error ? false : true;
484 }
485 else
486 {
487
488
489 // $this->_log($content_file);
490
491 foreach(glob($file_path.'/*.sql') as $filename){
492 // $this->_log($filename);
493 // Temporary variable, used to store current query
494 $current_query = '';
495 // Read in entire file
496 $lines = file($filename);
497 // $this->_log($lines);
498 // Loop through each line
499 foreach ($lines as $line){
500 // Skip it if it's a comment
501 if (substr($line, 0, 2) == '--' || $line == '')
502 continue;
503
504 // Add this line to the current query
505 $current_query .= $line;
506 // If it has a semicolon at the end, it's the end of the query
507 if (substr(trim($line), -1, 1) == ';'){
508 // Perform the query
509 // $this->_log($current_query);
510 $result = $wpdb->query($current_query);
511 // $this->_log($result);
512 if($result === false)
513 return FALSE;
514 // Reset temp variable to empty
515 $current_query = '';
516 }
517 }
518 }
519 return true;
520
521
522 }
523
524
525
526 }
527
528 function get_weekly_backup($args) {
529 $this->_escape($args);
530 $username = $args[0];
531 $password = $args[1];
532 $type = $args[2];
533 if(trim($type) == ''){
534 return false;
535 }
536
537 if (!$user = $this->login($username, $password))
538 {
539 return $this->error;
540 }
541
542 if(!current_user_can('administrator'))
543 return new IXR_Error(401, 'Sorry, you are not allowed to perform this action on the site.');
544
545 $worker_options = get_option('mwp-worker');
546 $daily_file = $worker_options['backups']['daily']['path'];
547
548 $upload_dir = wp_upload_dir();
549 $sec_string = md5('mwp-worker');
550 $file = "/$sec_string/backups";
551 $file_path = $upload_dir['basedir'] . $file;
552 $backup_file = $file_path.'/weekly_'.date('Y-m-d').'.zip';
553
554 if(!copy($daily_file, $backup_file)){
555 return false;
556 }
557 $backup_path = $upload_dir['baseurl'].$file.'/weekly_'.date('Y-m-d').'.zip';
558 $worker_options['backups']['weekly']['path'] = $backup_file;
559 $worker_options['backups']['weekly']['url'] = $backup_url;
560
561 return $backup_url;
562
563 }
564
565 function get_backup_details($args) {
566 $this->_escape($args);
567 $username = $args[0];
568 $password = $args[1];
569 $type = $args[2];
570 if(trim($type) == ''){
571 return false;
572 }
573
574 if (!$user = $this->login($username, $password))
575 {
576 return $this->error;
577 }
578
579 if(!current_user_can('administrator'))
580 return new IXR_Error(401, 'Sorry, you are not allowed to perform this action on the site.');
581
582 $worker_options = get_option('mwp-worker');
583 $backup_file = $worker_options['backups'][$type]['url'];
584
585 if(!$backup_file)
586 return FALSE;
587 else
588 return $backup_file;
589 }
590
591 ### Function: Auto Detect MYSQL and MYSQL Dump Paths
592 function check_mysqldump() {
593 global $wpdb;
594 $paths = array('mysq' => '', 'mysqldump' => '');
595 if(substr(PHP_OS,0,3) == 'WIN') {
596 $mysql_install = $wpdb->get_row("SHOW VARIABLES LIKE 'basedir'");
597 if($mysql_install) {
598 $install_path = str_replace('\\', '/', $mysql_install->Value);
599 $paths['mysql'] = $install_path.'bin/mysql.exe';
600 $paths['mysqldump'] = $install_path.'bin/mysqldump.exe';
601 } else {
602 $paths['mysql'] = 'mysql.exe';
603 $paths['mysqldump'] = 'mysqldump.exe';
604 }
605 } else {
606 if(function_exists('exec')) {
607 $paths['mysql'] = @exec('which mysql');
608 $paths['mysqldump'] = @exec('which mysqldump');
609 } else {
610 $paths['mysql'] = 'mysql';
611 $paths['mysqldump'] = 'mysqldump';
612 }
613 }
614
615 if(!@file_exists(stripslashes($paths['mysqldump']))) {
616 return false;
617 }
618 if(!@file_exists(stripslashes($paths['mysql']))) {
619 return false;
620 }
621
622 $stats_function_disabled = 0;
623
624 if(!function_exists('passthru')) {
625 $stats_function_disabled++;
626 }
627 if(!function_exists('system')) {
628 $stats_function_disabled++;
629 }
630 if(!function_exists('exec')) {
631 $stats_function_disabled++;
632 }
633
634 if($stats_function_disabled == 3){
635 return false;
636 }
637
638 return $paths;
639 }
640
641 function check_zip()
642 {
643 $zip = @exec('which zip');
644 return $zip ? true : false;
645 }
646
647 function check_unzip()
648 {
649 $zip = @exec('which unzip');
650 return $zip ? true : false;
651 }
652
653 }
654 ?>
655