InfiniteWP Client - Version 1.2.6

Version Description

  • Fix: Bug fixes
Download this release

Release Info

Developer infinitewp
Plugin Icon 128x128 InfiniteWP Client
Version 1.2.6
Comparing to
See all releases

Code changes from version 1.2.5 to 1.2.6

Files changed (5) hide show
  1. addons/post_links/post.class.php +131 -127
  2. backup.class.php +2806 -2806
  3. init.php +2 -2
  4. lib/amazon_s3/sdk.class.php +1556 -1556
  5. readme.txt +4 -1
addons/post_links/post.class.php CHANGED
@@ -109,133 +109,137 @@ class IWP_MMB_Post extends IWP_MMB_Core
109
if ($match_count > 0) {
110
$attachments = array();
111
$post_content = $post_data['post_content'];
112
-
113
- foreach ($get_urls as $get_url_k => $get_url) {
114
- // unset url in attachment array
115
- foreach ($post_atta_img as $atta_url_k => $atta_url_v) {
116
- $match_patt_url = '/' . str_replace($rep, $with, substr($atta_url_v['src'], 0, strrpos($atta_url_v['src'], '.'))) . '/';
117
- if (preg_match($match_patt_url, $get_url[4])) {
118
- unset($post_atta_img[$atta_url_k]);
119
- }
120
- }
121
- $pic_from_other_site = $get_urls[$get_url_k][4];
122
- /* if(strpos($pic_from_other_site,'exammple.com') === false){
123
- continue;
124
- }*/
125
-
126
- if (isset($get_urls[$get_url_k][6])) { // url have parent, don't download this url
127
- if ($get_url[1] != '') {
128
- // change src url
129
- $s_mmb_mp = '/' . str_replace($rep, $with, $get_url[4]) . '/';
130
-
131
- $s_img_atta = wp_get_attachment_image_src($get_urls[$get_url_k][6]);
132
- $s_mmb_rp = $s_img_atta[0];
133
- $post_content = preg_replace($s_mmb_mp, $s_mmb_rp, $post_content);
134
- // change attachment url
135
- if (preg_match('/attachment_id/i', $get_url[2])) {
136
- $iwp_mmb_mp = '/' . str_replace($rep, $with, $get_url[2]) . '/';
137
- $iwp_mmb_rp = get_bloginfo('wpurl') . '/?attachment_id=' . $get_urls[$get_url_k][6];
138
- $post_content = preg_replace($iwp_mmb_mp, $iwp_mmb_rp, $post_content);
139
- }
140
- }
141
- continue;
142
- }
143
-
144
- $no_thumb = '';
145
- if (preg_match('/-\d{3}x\d{3}\.[a-zA-Z0-9]{3,4}#x2F;', $get_url[4])) {
146
- $no_thumb = preg_replace('/-\d{3}x\d{3}\.[a-zA-Z0-9]{3,4}#x2F;', '.' . $get_url[5], $get_url[4]);
147
- } else {
148
- $no_thumb = $get_url[4];
149
- }
150
-
151
- if(isset($upload['error']) && !empty($upload['error'])){
152
- return array('error' => $upload['error']);
153
- }
154
- $file_name = basename($no_thumb);
155
- $tmp_file = download_url($no_thumb);
156
-
157
- if(is_wp_error($tmp_file)){
158
- return array('error' => $tmp_file->get_error_message());
159
- }
160
-
161
- $attach_upload['url'] = $upload['url'] . '/' . $file_name;
162
- $attach_upload['path'] = $upload['path'] . '/' . $file_name;
163
- $renamed = @rename($tmp_file, $attach_upload['path']);
164
- if ($renamed === true) {
165
- $match_pattern = '/' . str_replace($rep, $with, $get_url[4]) . '/';
166
- $replace_pattern = $attach_upload['url'];
167
- $post_content = preg_replace($match_pattern, $replace_pattern, $post_content);
168
- if (preg_match('/-\d{3}x\d{3}\.[a-zA-Z0-9]{3,4}#x2F;', $get_url[4])) {
169
- $match_pattern = '/' . str_replace($rep, $with, preg_replace('/-\d{3}x\d{3}\.[a-zA-Z0-9]{3,4}#x2F;', '.' . $get_url[5], $get_url[4])) . '/';
170
- $post_content = preg_replace($match_pattern, $replace_pattern, $post_content);
171
- }
172
-
173
- $attachment = array(
174
- 'post_title' => $file_name,
175
- 'post_content' => '',
176
- 'post_type' => 'attachment',
177
- //'post_parent' => $post_id,
178
- 'post_mime_type' => 'image/' . $get_url[5],
179
- 'guid' => $attach_upload['url']
180
- );
181
-
182
- // Save the data
183
-
184
- $attach_id = wp_insert_attachment($attachment, $attach_upload['path']);
185
-
186
- $attachments[$attach_id] = 0;
187
-
188
- // featured image
189
- if ($post_featured_img != '') {
190
- $feat_img_url = '';
191
- if (preg_match('/-\d{3}x\d{3}\.[a-zA-Z0-9]{3,4}#x2F;', $post_featured_img)) {
192
- $feat_img_url = substr($post_featured_img, 0, strrpos($post_featured_img, '.') - 8);
193
- } else {
194
- $feat_img_url = substr($post_featured_img, 0, strrpos($post_featured_img, '.'));
195
- }
196
- $m_feat_url = '/' . str_replace($rep, $with, $feat_img_url) . '/';
197
- if (preg_match($m_feat_url, $get_url[4])) {
198
- $post_featured_img = '';
199
- $attachments[$attach_id] = $attach_id;
200
- }
201
- }
202
-
203
- // set $get_urls value[6] - parent atta_id
204
- foreach ($get_urls as $url_k => $url_v) {
205
- if ($get_url_k != $url_k) {
206
- $s_get_url = '';
207
- if (preg_match('/-\d{3}x\d{3}\.[a-zA-Z0-9]{3,4}#x2F;', $url_v[4])) {
208
- $s_get_url = substr($url_v[4], 0, strrpos($url_v[4], '.') - 8);
209
- } else {
210
- $s_get_url = substr($url_v[4], 0, strrpos($url_v[4], '.'));
211
- }
212
- $m_patt_url = '/' . str_replace($rep, $with, $s_get_url) . '/';
213
- if (preg_match($m_patt_url, $get_url[4])) {
214
- array_push($get_urls[$url_k], $attach_id);
215
- }
216
- }
217
- }
218
-
219
-
220
- $some_data = wp_generate_attachment_metadata($attach_id, $attach_upload['path']);
221
- wp_update_attachment_metadata($attach_id, $some_data);
222
-
223
-
224
- //changing href of a tag
225
- if ($get_url[1] != '') {
226
- $iwp_mmb_mp = '/' . str_replace($rep, $with, $get_url[2]) . '/';
227
- if (preg_match('/attachment_id/i', $get_url[2])) {
228
- $iwp_mmb_rp = get_bloginfo('wpurl') . '/?attachment_id=' . $attach_id;
229
- $post_content = preg_replace($iwp_mmb_mp, $iwp_mmb_rp, $post_content);
230
- }
231
- }
232
- } else {
233
- @unlink($tmp_file);
234
- return array('error' => "Cannot create attachment file in ".$attach_upload['path']." Please set correct permissions.");
235
-
236
- }
237
- @unlink($tmp_file);
238
- }
239
240
241
$post_data['post_content'] = $post_content;
109
if ($match_count > 0) {
110
$attachments = array();
111
$post_content = $post_data['post_content'];
112
+
113
+ if(!empty($get_urls) && is_array($get_urls)){
114
+ foreach ($get_urls as $get_url_k => $get_url) {
115
+ // unset url in attachment array
116
+ if(!empty($post_atta_img) && is_array($post_atta_img)){
117
+ foreach ($post_atta_img as $atta_url_k => $atta_url_v) {
118
+ $match_patt_url = '/' . str_replace($rep, $with, substr($atta_url_v['src'], 0, strrpos($atta_url_v['src'], '.'))) . '/';
119
+ if (preg_match($match_patt_url, $get_url[4])) {
120
+ unset($post_atta_img[$atta_url_k]);
121
+ }
122
+ }
123
+ }
124
+ $pic_from_other_site = $get_urls[$get_url_k][4];
125
+ /* if(strpos($pic_from_other_site,'exammple.com') === false){
126
+ continue;
127
+ }*/
128
+
129
+ if (isset($get_urls[$get_url_k][6])) { // url have parent, don't download this url
130
+ if ($get_url[1] != '') {
131
+ // change src url
132
+ $s_mmb_mp = '/' . str_replace($rep, $with, $get_url[4]) . '/';
133
+
134
+ $s_img_atta = wp_get_attachment_image_src($get_urls[$get_url_k][6]);
135
+ $s_mmb_rp = $s_img_atta[0];
136
+ $post_content = preg_replace($s_mmb_mp, $s_mmb_rp, $post_content);
137
+ // change attachment url
138
+ if (preg_match('/attachment_id/i', $get_url[2])) {
139
+ $iwp_mmb_mp = '/' . str_replace($rep, $with, $get_url[2]) . '/';
140
+ $iwp_mmb_rp = get_bloginfo('wpurl') . '/?attachment_id=' . $get_urls[$get_url_k][6];
141
+ $post_content = preg_replace($iwp_mmb_mp, $iwp_mmb_rp, $post_content);
142
+ }
143
+ }
144
+ continue;
145
+ }
146
+
147
+ $no_thumb = '';
148
+ if (preg_match('/-\d{3}x\d{3}\.[a-zA-Z0-9]{3,4}#x2F;', $get_url[4])) {
149
+ $no_thumb = preg_replace('/-\d{3}x\d{3}\.[a-zA-Z0-9]{3,4}#x2F;', '.' . $get_url[5], $get_url[4]);
150
+ } else {
151
+ $no_thumb = $get_url[4];
152
+ }
153
+
154
+ if(isset($upload['error']) && !empty($upload['error'])){
155
+ return array('error' => $upload['error']);
156
+ }
157
+ $file_name = basename($no_thumb);
158
+ $tmp_file = download_url($no_thumb);
159
+
160
+ if(is_wp_error($tmp_file)){
161
+ return array('error' => $tmp_file->get_error_message());
162
+ }
163
+
164
+ $attach_upload['url'] = $upload['url'] . '/' . $file_name;
165
+ $attach_upload['path'] = $upload['path'] . '/' . $file_name;
166
+ $renamed = @rename($tmp_file, $attach_upload['path']);
167
+ if ($renamed === true) {
168
+ $match_pattern = '/' . str_replace($rep, $with, $get_url[4]) . '/';
169
+ $replace_pattern = $attach_upload['url'];
170
+ $post_content = preg_replace($match_pattern, $replace_pattern, $post_content);
171
+ if (preg_match('/-\d{3}x\d{3}\.[a-zA-Z0-9]{3,4}#x2F;', $get_url[4])) {
172
+ $match_pattern = '/' . str_replace($rep, $with, preg_replace('/-\d{3}x\d{3}\.[a-zA-Z0-9]{3,4}#x2F;', '.' . $get_url[5], $get_url[4])) . '/';
173
+ $post_content = preg_replace($match_pattern, $replace_pattern, $post_content);
174
+ }
175
+
176
+ $attachment = array(
177
+ 'post_title' => $file_name,
178
+ 'post_content' => '',
179
+ 'post_type' => 'attachment',
180
+ //'post_parent' => $post_id,
181
+ 'post_mime_type' => 'image/' . $get_url[5],
182
+ 'guid' => $attach_upload['url']
183
+ );
184
+
185
+ // Save the data
186
+
187
+ $attach_id = wp_insert_attachment($attachment, $attach_upload['path']);
188
+
189
+ $attachments[$attach_id] = 0;
190
+
191
+ // featured image
192
+ if ($post_featured_img != '') {
193
+ $feat_img_url = '';
194
+ if (preg_match('/-\d{3}x\d{3}\.[a-zA-Z0-9]{3,4}#x2F;', $post_featured_img)) {
195
+ $feat_img_url = substr($post_featured_img, 0, strrpos($post_featured_img, '.') - 8);
196
+ } else {
197
+ $feat_img_url = substr($post_featured_img, 0, strrpos($post_featured_img, '.'));
198
+ }
199
+ $m_feat_url = '/' . str_replace($rep, $with, $feat_img_url) . '/';
200
+ if (preg_match($m_feat_url, $get_url[4])) {
201
+ $post_featured_img = '';
202
+ $attachments[$attach_id] = $attach_id;
203
+ }
204
+ }
205
+
206
+ // set $get_urls value[6] - parent atta_id
207
+ foreach ($get_urls as $url_k => $url_v) {
208
+ if ($get_url_k != $url_k) {
209
+ $s_get_url = '';
210
+ if (preg_match('/-\d{3}x\d{3}\.[a-zA-Z0-9]{3,4}#x2F;', $url_v[4])) {
211
+ $s_get_url = substr($url_v[4], 0, strrpos($url_v[4], '.') - 8);
212
+ } else {
213
+ $s_get_url = substr($url_v[4], 0, strrpos($url_v[4], '.'));
214
+ }
215
+ $m_patt_url = '/' . str_replace($rep, $with, $s_get_url) . '/';
216
+ if (preg_match($m_patt_url, $get_url[4])) {
217
+ array_push($get_urls[$url_k], $attach_id);
218
+ }
219
+ }
220
+ }
221
+
222
+
223
+ $some_data = wp_generate_attachment_metadata($attach_id, $attach_upload['path']);
224
+ wp_update_attachment_metadata($attach_id, $some_data);
225
+
226
+
227
+ //changing href of a tag
228
+ if ($get_url[1] != '') {
229
+ $iwp_mmb_mp = '/' . str_replace($rep, $with, $get_url[2]) . '/';
230
+ if (preg_match('/attachment_id/i', $get_url[2])) {
231
+ $iwp_mmb_rp = get_bloginfo('wpurl') . '/?attachment_id=' . $attach_id;
232
+ $post_content = preg_replace($iwp_mmb_mp, $iwp_mmb_rp, $post_content);
233
+ }
234
+ }
235
+ } else {
236
+ @unlink($tmp_file);
237
+ return array('error' => "Cannot create attachment file in ".$attach_upload['path']." Please set correct permissions.");
238
+
239
+ }
240
+ @unlink($tmp_file);
241
+ }
242
+ }
243
244
245
$post_data['post_content'] = $post_content;
backup.class.php CHANGED
@@ -1,2807 +1,2807 @@
1
- <?php
2
- /************************************************************
3
- * This plugin was modified by Revmakx *
4
- * Copyright (c) 2012 Revmakx *
5
- * www.revmakx.com *
6
- * *
7
- ************************************************************/
8
- /*************************************************************
9
- *
10
- * backup.class.php
11
- *
12
- * Manage Backups
13
- *
14
- *
15
- * Copyright (c) 2011 Prelovac Media
16
- * www.prelovac.com
17
- **************************************************************/
18
- define('IWP_BACKUP_DIR', WP_CONTENT_DIR . '/infinitewp/backups');
19
- define('IWP_DB_DIR', IWP_BACKUP_DIR . '/iwp_db');
20
-
21
- $zip_errors = array(
22
- 'No error',
23
- 'No error',
24
- 'Unexpected end of zip file',
25
- 'A generic error in the zipfile format was detected',
26
- 'zip was unable to allocate itself memory',
27
- 'A severe error in the zipfile format was detected',
28
- 'Entry too large to be split with zipsplit',
29
- 'Invalid comment format',
30
- 'zip -T failed or out of memory',
31
- 'The user aborted zip prematurely',
32
- 'zip encountered an error while using a temp file. Please check if this domain\'s account has enough disk space.',
33
- 'Read or seek error',
34
- 'zip has nothing to do',
35
- 'Missing or empty zip file',
36
- 'Error writing to a file. Please check if this domain\'s account has enough disk space.',
37
- 'zip was unable to create a file to write to',
38
- 'bad command line parameters',
39
- 'no error',
40
- 'zip could not open a specified file to read'
41
- );
42
- $unzip_errors = array(
43
- 'No error',
44
- 'One or more warning errors were encountered, but processing completed successfully anyway',
45
- 'A generic error in the zipfile format was detected',
46
- 'A severe error in the zipfile format was detected.',
47
- 'unzip was unable to allocate itself memory.',
48
- 'unzip was unable to allocate memory, or encountered an encryption error',
49
- 'unzip was unable to allocate memory during decompression to disk',
50
- 'unzip was unable allocate memory during in-memory decompression',
51
- 'unused',
52
- 'The specified zipfiles were not found',
53
- 'Bad command line parameters',
54
- 'No matching files were found',
55
- 50 => 'The disk is (or was) full during extraction',
56
- 51 => 'The end of the ZIP archive was encountered prematurely.',
57
- 80 => 'The user aborted unzip prematurely.',
58
- 81 => 'Testing or extraction of one or more files failed due to unsupported compression methods or unsupported decryption.',
59
- 82 => 'No files were found due to bad decryption password(s)'
60
- );
61
-
62
-
63
- class IWP_MMB_Backup extends IWP_MMB_Core
64
- {
65
- var $site_name;
66
- var $statuses;
67
- var $tasks;
68
- var $s3;
69
- var $ftp;
70
- var $dropbox;
71
- function __construct()
72
- {
73
- parent::__construct();
74
- $this->site_name = str_replace(array(
75
- "_",
76
- "/",
77
- "~"
78
- ), array(
79
- "",
80
- "-",
81
- "-"
82
- ), rtrim($this->remove_http(get_bloginfo('url')), "/"));
83
- $this->statuses = array(
84
- 'db_dump' => 1,
85
- 'db_zip' => 2,
86
- 'files_zip' => 3,
87
- 'finished' => 100
88
- );
89
- $this->tasks = get_option('iwp_client_backup_tasks');
90
- }
91
- function set_memory()
92
- {
93
- $changed = array('execution_time' => 0, 'memory_limit' => 0);
94
- @ignore_user_abort(true);
95
- $memory_limit = trim(ini_get('memory_limit'));
96
- $last = strtolower(substr($memory_limit, -1));
97
-
98
- if($last == 'g')
99
- $memory_limit = ((int) $memory_limit)*1024;
100
- else if($last == 'm')
101
- $memory_limit = (int) $memory_limit;
102
- if($last == 'k')
103
- $memory_limit = ((int) $memory_limit)/1024;
104
-
105
- if ( $memory_limit < 384 ) {
106
- @ini_set('memory_limit', '384M');
107
- $changed['memory_limit'] = 1;
108
- }
109
-
110
- if ( (int) @ini_get('max_execution_time') < 1200 ) {
111
- @ini_set('max_execution_time', 1200);//twenty minutes
112
- @set_time_limit(1200);
113
- $changed['execution_time'] = 1;
114
- }
115
-
116
- return $changed;
117
-
118
- }
119
- function get_backup_settings()
120
- {
121
- $backup_settings = get_option('iwp_client_backup_tasks');
122
- if (!empty($backup_settings))
123
- return $backup_settings;
124
- else
125
- return false;
126
- }
127
-
128
- function set_backup_task($params)
129
- {
130
- //$params => [$task_name, $args, $error]
131
- if (!empty($params)) {
132
-
133
- //Make sure backup cron job is set
134
- if (!wp_next_scheduled('iwp_client_backup_tasks')) {
135
- wp_schedule_event( time(), 'tenminutes', 'iwp_client_backup_tasks' );
136
- }
137
-
138
- extract($params);
139
-
140
- //$before = $this->get_backup_settings();
141
- $before = $this->tasks;
142
- if (!$before || empty($before))
143
- $before = array();
144
-
145
- if (isset($args['remove'])) {
146
- unset($before[$task_name]);
147
- $return = array(
148
- 'removed' => true
149
- );
150
- } else {
151
-
152
- if (is_array($params['account_info'])) { //only if sends from IWP Admin Panel first time(secure data)
153
- $args['account_info'] = $account_info;
154
- }
155
-
156
- $before[$task_name]['task_args'] = $args;
157
- //$before[$task_name]['task_args'] = $task_name;
158
-
159
- /*if (strlen($args['schedule']))
160
- $before[$task_name]['task_args']['next'] = $this->schedule_next($args['type'], $args['schedule']);*///to WP cron
161
- $before[$task_name]['task_args']['task_name'] = $task_name;
162
-
163
- $return = $before[$task_name];
164
- }
165
-
166
- //Update with error
167
- if (isset($error)) {
168
- if (is_array($error)) {
169
- $before[$task_name]['task_results'][count($before[$task_name]['task_results']) - 1]['error'] = $error['error'];
170
- } else {
171
- $before[$task_name]['task_results'][count($before[$task_name]['task_results'])]['error'] = $error;
172
- }
173
- }
174
-
175
- // if (isset($time) && $time) { //set next result time before backup
176
- if (is_array($before[$task_name]['task_results'])) {
177
- $before[$task_name]['task_results'] = array_values($before[$task_name]['task_results']);
178
- }
179
- //$before[$task_name]['task_results'][count($before[$task_name]['task_results'])]['time'] = (isset($time) && $time) ? $time : time();
180
- //}
181
- if (isset($time) && $time) { //This will occur for schedule runtask.
182
- $before[$task_name]['task_results'][count($before[$task_name]['task_results'])]['time'] = $time;
183
- }else{
184
- if($task_name == 'Backup Now')
185
- $before[$task_name]['task_results'][count($before[$task_name]['task_results'])]['time'] = time();
186
- }
187
-
188
- $this->update_tasks($before);
189
- //update_option('iwp_client_backup_tasks', $before);
190
-
191
- if ($task_name == 'Backup Now') {
192
- $result = $this->backup($args, $task_name);
193
- $backup_settings = $this->tasks;
194
-
195
- if (is_array($result) && array_key_exists('error', $result)) {
196
- $return = $result;
197
- } else {
198
- $return = $backup_settings[$task_name];
199
- }
200
- }
201
- return $return;
202
- }
203
-
204
-
205
-
206
- return false;
207
- }
208
-
209
- //Cron check
210
- function check_backup_tasks()
211
- {
212
-
213
- $this->check_cron_remove();
214
-
215
- $settings = $this->tasks;
216
- if (is_array($settings) && !empty($settings)) {
217
- foreach ($settings as $task_name => $setting) {
218
-
219
- if ($setting['task_args']['next'] && $setting['task_args']['next'] < time()) {
220
- //if ($setting['task_args']['next'] && $_GET['force_backup']) {
221
- if ($setting['task_args']['url'] && $setting['task_args']['task_id'] && $setting['task_args']['site_key']) {
222
- //Check orphan task
223
- $check_data = array(
224
- 'task_name' => $task_name,
225
- 'task_id' => $setting['task_args']['task_id'],
226
- 'site_key' => $setting['task_args']['site_key']
227
- );
228
-
229
- $check = $this->validate_task($check_data, $setting['task_args']['url']);
230
-
231
- }
232
-
233
- $update = array(
234
- 'task_name' => $task_name,
235
- 'args' => $settings[$task_name]['task_args']
236
- );
237
-
238
-
239
- if($check != 'paused'){
240
- $update['time'] = time();
241
- }
242
-
243
- //Update task with next schedule
244
- $this->set_backup_task($update);
245
-
246
- if($check == 'paused'){
247
- continue;
248
- }
249
-
250
-
251
- $result = $this->backup($setting['task_args'], $task_name);
252
- $error = '';
253
- if (is_array($result) && array_key_exists('error', $result)) {
254
- $error = $result;
255
- $this->set_backup_task(array(
256
- 'task_name' => $task_name,
257
- 'args' => $settings[$task_name]['task_args'],
258
- 'error' => $error
259
- ));
260
- } else {
261
- $error = '';
262
- }
263
- break; //Only one backup per cron
264
- }
265
- }
266
- }
267
-
268
- }
269
-
270
-
271
- function task_now($task_name){
272
-
273
- $settings = $this->tasks;
274
- if(!array_key_exists($task_name,$settings)){
275
- return array('error' => $task_name." does not exist.");
276
- } else {
277
- $setting = $settings[$task_name];
278
- }
279
-
280
- $this->set_backup_task(array(
281
- 'task_name' => $task_name,
282
- 'args' => $settings[$task_name]['task_args'],
283
- 'time' => time()
284
- ));
285
-
286
- //Run backup
287
- $result = $this->backup($setting['task_args'], $task_name);
288
-
289
- //Check for error
290
- if (is_array($result) && array_key_exists('error', $result)) {
291
- $this->set_backup_task(array(
292
- 'task_name' => $task_name,
293
- 'args' => $settings[$task_name]['task_args'],
294
- 'error' => $result['error']
295
- ));
296
- return $result;
297
- } else {
298
- return $this->get_backup_stats();
299
- }
300
-
301
- }
302
-
303
- function delete_task_now($task_name){
304
- $tasks = $this->tasks;
305
- unset($tasks[$task_name]);
306
- $this->update_tasks($tasks);
307
- $this->cleanup();
308
-
309
- return $task_name;
310
-
311
- }
312
-
313
-
314
- /*
315
- * If Task Name not set then it's manual backup
316
- * Backup args:
317
- * type -> db, full
318
- * what -> daily, weekly, monthly
319
- * account_info -> ftp, amazons3, dropbox
320
- * exclude-> array of paths to exclude from backup
321
- */
322
-
323
- function backup($args, $task_name = false)
324
- {
325
- if (!$args || empty($args))
326
- return false;
327
-
328
- extract($args); //extract settings
329
-
330
- //$adminHistoryID - admin panel history ID for backup task.
331
-
332
-
333
- //Try increase memory limit and execution time
334
- $this->set_memory();
335
-
336
- //Remove old backup(s)
337
- $removed = $this->remove_old_backups($task_name);
338
- if (is_array($removed) && isset($removed['error'])) {
339
- //$error_message = $removed['error'];
340
- return $removed;
341
- }
342
-
343
- $new_file_path = IWP_BACKUP_DIR;
344
-
345
- if (!file_exists($new_file_path)) {
346
- if (!mkdir($new_file_path, 0755, true))
347
- return array(
348
- 'error' => 'Permission denied, make sure you have write permission to wp-content folder.'
349
- );
350
- }
351
-
352
- @file_put_contents($new_file_path . '/index.php', ''); //safe
353
-
354
- //Prepare .zip file name
355
- $hash = md5(time());
356
- $label = $type ? $type : 'manual';
357
- $backup_file = $new_file_path . '/' . $this->site_name . '_' . $label . '_' . $what . '_' . date('Y-m-d') . '_' . $hash . '.zip';
358
- $backup_url = WP_CONTENT_URL . '/infinitewp/backups/' . $this->site_name . '_' . $label . '_' . $what . '_' . date('Y-m-d') . '_' . $hash . '.zip';
359
-
360
- //Optimize tables?
361
- if (isset($optimize_tables) && !empty($optimize_tables)) {
362
- $this->optimize_tables();
363
- }
364
-
365
- //What to backup - db or full?
366
- if (trim($what) == 'db') {
367
- //Take database backup
368
- $this->update_status($task_name, 'db_dump');
369
- $GLOBALS['fail_safe_db'] = $this->tasks[$task_name]['task_args']['fail_safe_db'];
370
-
371
- $db_result = $this->backup_db();
372
- if ($db_result == false) {
373
- return array(
374
- 'error' => 'Failed to backup database.'
375
- );
376
- } else if (is_array($db_result) && isset($db_result['error'])) {
377
- return array(
378
- 'error' => $db_result['error']
379
- );
380
- } else {
381
- $this->update_status($task_name, 'db_dump', true);
382
- $this->update_status($task_name, 'db_zip');
383
-
384
- /*zip_backup_db*/
385
- $fail_safe_files = $this->tasks[$task_name]['task_args']['fail_safe_files'];
386
- $disable_comp = $this->tasks[$task_name]['task_args']['disable_comp'];
387
-
388
- if($fail_safe_files){
389
- $this->fail_safe_pcl_db($backup_file,$fail_safe_files,$disable_comp);
390
- }
391
- else{
392
- $comp_level = $disable_comp ? '-0' : '-1';
393
- chdir(IWP_BACKUP_DIR);
394
- $zip = $this->get_zip();
395
- $command = "$zip -q -r $comp_level $backup_file 'iwp_db'";
396
- iwp_mmb_print_flush('DB ZIP CMD: Start');
397
- ob_start();
398
- $result = $this->iwp_mmb_exec($command);
399
- ob_get_clean();
400
- iwp_mmb_print_flush('DB ZIP CMD: End');
401
- /*zip_backup_db */
402
- if(!$result){
403
- $zip_archive_db_result = false;
404
- if (class_exists("ZipArchive")) {
405
- $this->_log("DB zip, fallback to ZipArchive");
406
- iwp_mmb_print_flush('DB ZIP Archive: Start');
407
- $zip_archive_db_result = $this->zip_archive_backup_db($task_name, $db_result, $backup_file);
408
- iwp_mmb_print_flush('DB ZIP Archive: End');
409
- }
410
-
411
- if (!$zip_archive_db_result) {
412
- $this->fail_safe_pcl_db($backup_file,$fail_safe_files,$disable_comp);
413
- }
414
- }
415
- }
416
-
417
- @unlink($db_result);
418
- @unlink(IWP_BACKUP_DIR.'/iwp_db/index.php');
419
- @rmdir(IWP_DB_DIR);
420
- /*if (!$result) {
421
- return array(
422
- 'error' => 'Failed to zip database.'
423
- );
424
- }*///commented because of zipArchive
425
- $this->update_status($task_name, 'db_zip', true);
426
- }
427
- } elseif (trim($what) == 'full') {
428
- $content_backup = $this->backup_full($task_name, $backup_file, $exclude, $include);
429
- if (is_array($content_backup) && array_key_exists('error', $content_backup)) {
430
- return array(
431
- 'error' => $content_backup['error']
432
- );
433
- }
434
- }
435
-
436
- //Update backup info
437
- if ($task_name) {
438
- //backup task (scheduled)
439
- $backup_settings = $this->tasks;
440
- $paths = array();
441
- $size = round(filesize($backup_file) / 1024, 2);
442
-
443
- if ($size > 1000) {
444
- $paths['size'] = round($size / 1024, 2) . " MB";//Modified by IWP //Mb => MB
445
- } else {
446
- $paths['size'] = $size . 'KB';//Modified by IWP //Kb => KB
447
- }
448
-
449
- $paths['backup_name'] = $backup_settings[$task_name]['task_args']['backup_name'];
450
-
451
- if ($task_name != 'Backup Now') {
452
- if (!$backup_settings[$task_name]['task_args']['del_host_file']) {
453
- $paths['server'] = array(
454
- 'file_path' => $backup_file,
455
- 'file_url' => $backup_url
456
- );
457
- }
458
- } else {
459
- $paths['server'] = array(
460
- 'file_path' => $backup_file,
461
- 'file_url' => $backup_url
462
- );
463
- }
464
-
465
-
466
- if (isset($backup_settings[$task_name]['task_args']['account_info']['iwp_ftp'])) {
467
- $paths['ftp'] = basename($backup_url);
468
- }
469
-
470
- if (isset($backup_settings[$task_name]['task_args']['account_info']['iwp_amazon_s3'])) {
471
- $paths['amazons3'] = basename($backup_url);
472
- }
473
-
474
- if (isset($backup_settings[$task_name]['task_args']['account_info']['iwp_dropbox'])) {
475
- $paths['dropbox'] = basename($backup_url);
476
- }
477
-
478
- if (isset($backup_settings[$task_name]['task_args']['account_info']['iwp_email'])) {
479
- $paths['email'] = basename($backup_url);
480
- }
481
-
482
- $temp = $backup_settings[$task_name]['task_results'];
483
- $temp = @array_values($temp);
484
- $paths['time'] = time();
485
-
486
-
487
- //if ($task_name != 'Backup Now') {
488
- $paths['backhack_status'] = $temp[count($temp) - 1]['backhack_status'];
489
- //$paths['status'] = $temp[count($temp) - 1]['status'];
490
- $temp[count($temp) - 1] = $paths;
491
-
492
- /*
493
- } else {
494
- $temp[count($temp)] = $paths;
495
- }
496
- */
497
-
498
- $backup_settings[$task_name]['task_results'] = $temp;
499
- $this->update_tasks($backup_settings);
500
- //update_option('iwp_client_backup_tasks', $backup_settings);
501
- }
502
-
503
-
504
- if ($task_name != 'Backup Now') {
505
-
506
- if (isset($account_info['iwp_ftp']) && !empty($account_info['iwp_ftp'])) {
507
- $this->update_status($task_name, 'ftp');
508
- $account_info['iwp_ftp']['backup_file'] = $backup_file;
509
- iwp_mmb_print_flush('FTP upload: Start');
510
- $ftp_result = $this->ftp_backup($account_info['iwp_ftp']);
511
- iwp_mmb_print_flush('FTP upload: End');
512
- if ($ftp_result !== true && $del_host_file) {
513
- @unlink($backup_file);
514
- }
515
-
516
- if (is_array($ftp_result) && isset($ftp_result['error'])) {
517
- return $ftp_result;
518
- }
519
- $this->wpdb_reconnect();
520
- $this->update_status($task_name, 'ftp', true);
521
- }
522
-
523
- if (isset($account_info['iwp_amazon_s3']) && !empty($account_info['iwp_amazon_s3'])) {
524
- $this->update_status($task_name, 's3');
525
- $account_info['iwp_amazon_s3']['backup_file'] = $backup_file;
526
- iwp_mmb_print_flush('Amazon S3 upload: Start');
527
- $amazons3_result = $this->amazons3_backup($account_info['iwp_amazon_s3']);
528
- iwp_mmb_print_flush('Amazon S3 upload: End');
529
- if ($amazons3_result !== true && $del_host_file) {
530
- @unlink($backup_file);
531
- }
532
- if (is_array($amazons3_result) && isset($amazons3_result['error'])) {
533
- return $amazons3_result;
534
- }
535
- $this->wpdb_reconnect();
536
- $this->update_status($task_name, 's3', true);
537
- }
538
-
539
- if (isset($account_info['iwp_dropbox']) && !empty($account_info['iwp_dropbox'])) {
540
- $this->update_status($task_name, 'dropbox');
541
- $account_info['iwp_dropbox']['backup_file'] = $backup_file;
542
- iwp_mmb_print_flush('Dropbox upload: Start');
543
- $dropbox_result = $this->dropbox_backup($account_info['iwp_dropbox']);
544
- iwp_mmb_print_flush('Dropbox upload: End');
545
- if ($dropbox_result !== true && $del_host_file) {
546
- @unlink($backup_file);
547
- }
548
-
549
- if (is_array($dropbox_result) && isset($dropbox_result['error'])) {
550
- return $dropbox_result;
551
- }
552
- $this->wpdb_reconnect();
553
- $this->update_status($task_name, 'dropbox', true);
554
- }
555
-
556
- if ($del_host_file) {
557
- @unlink($backup_file);
558
- }
559
-
560
- } //end additional
561
-
562
- $this->update_status($task_name,'finished',true);
563
-
564
- return $backup_url; //Return url to backup file
565
- }
566
-
567
-
568
- function backup_full($task_name, $backup_file, $exclude = array(), $include = array())
569
- {
570
-
571
- global $zip_errors;
572
- $sys = substr(PHP_OS, 0, 3);
573
-
574
- $this->update_status($task_name, 'db_dump');
575
- $GLOBALS['fail_safe_db'] = $this->tasks[$task_name]['task_args']['fail_safe_db'];
576
- $db_result = $this->backup_db();
577
-
578
- if ($db_result == false) {
579
- return array(
580
- 'error' => 'Failed to backup database.'
581
- );
582
- } else if (is_array($db_result) && isset($db_result['error'])) {
583
- return array(
584
- 'error' => $db_result['error']
585
- );
586
- }
587
-
588
- $this->update_status($task_name, 'db_dump', true);
589
- $this->update_status($task_name, 'db_zip');
590
-
591
- /*zip_backup_db*/
592
- $fail_safe_files = $this->tasks[$task_name]['task_args']['fail_safe_files'];
593
- $disable_comp = $this->tasks[$task_name]['task_args']['disable_comp'];
594
-
595
- if($fail_safe_files){
596
- $this->fail_safe_pcl_db($backup_file,$fail_safe_files,$disable_comp);
597
- }
598
- else{
599
- $comp_level = $disable_comp ? '-0' : '-1';
600
- $zip = $this->get_zip();
601
- iwp_mmb_print_flush('DB ZIP CMD: Start');
602
- //Add database file
603
- chdir(IWP_BACKUP_DIR);
604
- $command = "$zip -q -r $comp_level $backup_file 'iwp_db'";
605
- ob_start();
606
- $result = $this->iwp_mmb_exec($command);
607
- ob_get_clean();
608
- iwp_mmb_print_flush('DB ZIP CMD: End');
609
- /*zip_backup_db*/
610
-
611
- if(!$result){
612
- $zip_archive_db_result = false;
613
- if (class_exists("ZipArchive")) {
614
- iwp_mmb_print_flush('DB ZIP Archive: Start');
615
- $this->_log("DB zip, fallback to ZipArchive");
616
- $zip_archive_db_result = $this->zip_archive_backup_db($task_name, $db_result, $backup_file);
617
- iwp_mmb_print_flush('DB ZIP Archive: End');
618
- }
619
-
620
- if (!$zip_archive_db_result) {
621
- $this->fail_safe_pcl_db($backup_file,$fail_safe_files,$disable_comp);
622
- }
623
- }
624
- }
625
- @unlink($db_result);
626
- @unlink(IWP_BACKUP_DIR.'/iwp_db/index.php');
627
- @rmdir(IWP_DB_DIR);
628
-
629
- $this->update_status($task_name, 'db_zip', true);
630
-
631
-
632
- //Always remove backup folders
633
- $remove = array(
634
- trim(basename(WP_CONTENT_DIR)) . "/infinitewp/backups",
635
- trim(basename(WP_CONTENT_DIR)) . "/" . md5('iwp_mmb-client') . "/iwp_backups",
636
- trim(basename(WP_CONTENT_DIR)) . "/cache",
637
- trim(basename(WP_CONTENT_DIR)) . "/w3tc"
638
- );
639
-
640
- $exclude = array_merge($exclude, $remove);
641
-
642
- //Exclude paths
643
- $exclude_data = "-x";
644
-
645
- $exclude_file_data = '';
646
-
647
- if (!empty($exclude) && is_array($exclude)) {
648
- foreach ($exclude as $data) {
649
- if(empty($data))
650
- continue;
651
- if (is_dir(ABSPATH . $data)) {
652
- if ($sys == 'WIN')
653
- $exclude_data .= " $data/*.*";
654
- else
655
- $exclude_data .= " '$data/*'";
656
- }else {
657
- if ($sys == 'WIN'){
658
- if(file_exists(ABSPATH . $data)){
659
- $exclude_data .= " $data";
660
- $exclude_file_data .= " $data";
661
- }
662
- }else {
663
- if(file_exists(ABSPATH . $data)){
664
- $exclude_data .= " '$data'";
665
- $exclude_file_data .= " '$data'";
666
- }
667
- }
668
- }
669
- }
670
- }
671
-
672
- if($exclude_file_data){
673
- $exclude_file_data = "-x".$exclude_file_data;
674
- }
675
-
676
- /* foreach ($remove as $data) {
677
- if(empty($data))
678
- continue;
679
- if ($sys == 'WIN')
680
- $exclude_data .= " $data/*.*";
681
- else
682
- $exclude_data .= " '$data/*'";
683
- }*/ //commented for pclzip modifications
684
-
685
- //Include paths by default
686
- $add = array(
687
- trim(WPINC),
688
- trim(basename(WP_CONTENT_DIR)),
689
- "wp-admin"
690
- );
691
-
692
- $include_data = ". -i";
693
- foreach ($add as $data) {
694
- if ($sys == 'WIN')
695
- $include_data .= " $data/*.*";
696
- else
697
- $include_data .= " '$data/*'";
698
- }
699
-
700
- //Additional includes?
701
- if (!empty($include) && is_array($include)) {
702
- foreach ($include as $data) {
703
- if(empty($data))
704
- continue;
705
- if ($data) {
706
- if ($sys == 'WIN')
707
- $include_data .= " $data/*.*";
708
- else
709
- $include_data .= " '$data/*'";
710
- }
711
- }
712
- }
713
-
714
- $this->update_status($task_name, 'files_zip');
715
- chdir(ABSPATH);
716
-
717
- if($fail_safe_files){
718
- $this->fail_safe_pcl_files($task_name, $backup_file, $exclude, $include, $fail_safe_files, $disable_comp, $add, $remove);
719
- }
720
- else{
721
- $do_cmd_zip_alternative = false;
722
- @copy($backup_file, $backup_file.'_2');
723
-
724
- iwp_mmb_print_flush('Files ZIP CMD: Start');
725
- $command = "$zip -q -j $comp_level $backup_file .* * $exclude_file_data";
726
- ob_start();
727
- $result_f = $this->iwp_mmb_exec($command, false, true);
728
- ob_get_clean();
729
- iwp_mmb_print_flush('Files ZIP CMD: 1/2 over');
730
- if (!$result_f || $result_f == 18) { // disregard permissions error, file can't be accessed
731
- $command = "$zip -q -r $comp_level $backup_file $include_data $exclude_data";
732
- ob_start();
733
- $result_d = $this->iwp_mmb_exec($command, false, true);
734
- ob_get_clean();
735
- if ($result_d && $result_d != 18) {
736
- @unlink($backup_file);
737
- $do_cmd_zip_alternative = true;
738
-
739
-
740
- if($result_d > 0 && $result_d < 18){
741
- //return array(
742
- // 'error' => 'Failed to archive files (' . $zip_errors[$result_d] . ') .'
743
- // );
744
- iwp_mmb_print_flush('Files ZIP CMD: Failed to archive files (' . $zip_errors[$result_d] . ') .');
745
- }
746
- else{
747
- //return array(
748
- // 'error' => 'Failed to archive files.'
749
- //);
750
- iwp_mmb_print_flush('Files ZIP CMD: Failed to archive files.');
751
- }
752
- }
753
- }
754
-
755
- if(!$do_cmd_zip_alternative){//if FILE ZIP CMD successful
756
- @unlink($backup_file.'_2');
757
- }
758
-
759
- iwp_mmb_print_flush('Files ZIP CMD: End');
760
- if (($result_f && $result_f != 18) || ($do_cmd_zip_alternative)) {
761
-
762
- if($do_cmd_zip_alternative){
763
- @copy($backup_file.'_2', $backup_file);
764
- @unlink($backup_file.'_2');
765
- }
766
-
767
- $zip_archive_result = false;
768
- if (class_exists("ZipArchive")) {
769
- iwp_mmb_print_flush('Files ZIP Archive: Start');
770
- $this->_log("Files zip fallback to ZipArchive");
771
- $zip_archive_result = $this->zip_archive_backup($task_name, $backup_file, $exclude, $include);
772
- iwp_mmb_print_flush('Files ZIP Archive: End');
773
- }
774
-
775
-
776
- if (!$zip_archive_result) {
777
- $this->fail_safe_pcl_files($task_name, $backup_file, $exclude, $include, $fail_safe_files, $disable_comp, $add, $remove);
778
- }
779
- }
780
- }
781
-
782
- //Reconnect
783
- $this->wpdb_reconnect();
784
-
785
- $this->update_status($task_name, 'files_zip', true);
786
- return true;
787
- }
788
-
789
-
790
- function fail_safe_pcl_files($task_name, $backup_file, $exclude, $include, $fail_safe_files, $disable_comp, $add, $remove){ //Try pclZip
791
- //$this->back_hack($task_name, 'Files ZIP PCL: Start');
792
- iwp_mmb_print_flush('Files ZIP PCL: Start');
793
- if (!isset($archive)) {
794
- define('PCLZIP_TEMPORARY_DIR', IWP_BACKUP_DIR . '/');
795
- //require_once ABSPATH . '/wp-admin/includes/class-pclzip.php';
796
- require_once $GLOBALS['iwp_mmb_plugin_dir'].'/pclzip.class.php';
797
- $archive = new IWPPclZip($backup_file);
798
- }
799
-
800
- //Include paths
801
- $include_data = array();
802
- if (!empty($include) && is_array($include)) {
803
- foreach ($include as $data) {
804
- if ($data && file_exists(ABSPATH . $data))
805
- $include_data[] = ABSPATH . $data . '/';
806
- }
807
- }
808
-
809
- foreach ($add as $data) {
810
- if (file_exists(ABSPATH . $data))
811
- $include_data[] = ABSPATH . $data . '/';
812
- }
813
-
814
- //Include root files
815
- if ($handle = opendir(ABSPATH)) {
816
- while (false !== ($file = readdir($handle))) {
817
- if ($file != "." && $file != ".." && !is_dir($file) && file_exists(ABSPATH . $file)) {
818
- $include_data[] = ABSPATH . $file;
819
- }
820
- }
821
- closedir($handle);
822
- }
823
-
824
- //exclude paths
825
- $exclude_data = array();
826
- if (!empty($exclude) && is_array($exclude)) {
827
- foreach ($exclude as $data) {
828
- if (is_dir(ABSPATH . $data))
829
- $exclude_data[] = $data . '/';
830
- else
831
- $exclude_data[] = $data;
832
- }
833
- }
834
-
835
- foreach ($remove as $rem) {
836
- $exclude_data[] = $rem . '/';
837
- }
838
-
839
- if($fail_safe_files && $disable_comp){
840
- $result = $archive->add($include_data, PCLZIP_OPT_REMOVE_PATH, ABSPATH, PCLZIP_OPT_IWP_EXCLUDE, $exclude_data, PCLZIP_OPT_NO_COMPRESSION, PCLZIP_OPT_TEMP_FILE_THRESHOLD, 1);
841
- }
842
- elseif(!$fail_safe_files && $disable_comp){
843
- $result = $archive->add($include_data, PCLZIP_OPT_REMOVE_PATH, ABSPATH, PCLZIP_OPT_IWP_EXCLUDE, $exclude_data, PCLZIP_OPT_NO_COMPRESSION);
844
- }
845
- elseif($fail_safe_files && !$disable_comp){
846
- $result = $archive->add($include_data, PCLZIP_OPT_REMOVE_PATH, ABSPATH, PCLZIP_OPT_IWP_EXCLUDE, $exclude_data, PCLZIP_OPT_TEMP_FILE_THRESHOLD, 1);
847
- }
848
- else{
849
- $result = $archive->add($include_data, PCLZIP_OPT_REMOVE_PATH, ABSPATH, PCLZIP_OPT_IWP_EXCLUDE, $exclude_data);
850
- }
851
-
852
- iwp_mmb_print_flush('Files ZIP PCL: End');
853
-
854
- if (!$result) {
855
- @unlink($backup_file);
856
- return array(
857
- 'error' => 'Failed to zip files. pclZip error (' . $archive->error_code . '): .' . $archive->error_string
858
- );
859
- }
860
- //}
861
- }
862
- //Reconnect
863
- function fail_safe_pcl_db($backup_file,$fail_safe_files,$disable_comp){
864
- //$this->back_hack($task_name, 'DB ZIP PCL: Start');
865
- iwp_mmb_print_flush('DB ZIP PCL: Start');
866
- define('PCLZIP_TEMPORARY_DIR', IWP_BACKUP_DIR . '/');
867
- require_once $GLOBALS['iwp_mmb_plugin_dir'].'/pclzip.class.php';
868
- $archive = new IWPPclZip($backup_file);
869
-
870
- if($fail_safe_files && $disable_comp){
871
- $result_db = $archive->add(IWP_DB_DIR, PCLZIP_OPT_REMOVE_PATH, IWP_BACKUP_DIR, PCLZIP_OPT_NO_COMPRESSION, PCLZIP_OPT_TEMP_FILE_THRESHOLD, 1);
872
- }
873
- elseif(!$fail_safe_files && $disable_comp){
874
- $result_db = $archive->add(IWP_DB_DIR, PCLZIP_OPT_REMOVE_PATH, IWP_BACKUP_DIR, PCLZIP_OPT_NO_COMPRESSION);
875
- }
876
- elseif($fail_safe_files && !$disable_comp){
877
- $result_db = $archive->add(IWP_DB_DIR, PCLZIP_OPT_REMOVE_PATH, IWP_BACKUP_DIR, PCLZIP_OPT_TEMP_FILE_THRESHOLD, 1);
878
- }
879
- else{
880
- $result_db = $archive->add(IWP_DB_DIR, PCLZIP_OPT_REMOVE_PATH, IWP_BACKUP_DIR);
881
- }
882
- //$this->back_hack($task_name, 'DB ZIP PCL: End');
883
- iwp_mmb_print_flush('DB ZIP PCL: End');
884
-
885
- @unlink($db_result);
886
- @unlink(IWP_BACKUP_DIR.'/iwp_db/index.php');
887
- @rmdir(IWP_DB_DIR);
888
-
889
- if (!$result_db) {
890
- return array(
891
- 'error' => 'Failed to zip database. pclZip error (' . $archive->error_code . '): .' . $archive->error_string
892
- );
893
- }
894
- }
895
- /**
896
- * Zipping database dump and index.php in folder iwp_db by ZipArchive class, requires php zip extension.
897
- *
898
- * @param string $task_name the name of backup task
899
- * @param string $db_result relative path to database dump file
900
- * @param string $backup_file absolute path to zip file
901
- * @return bool is compress successful or not
902
- */
903
- function zip_archive_backup_db($task_name, $db_result, $backup_file) {
904
- $disable_comp = $this->tasks[$task_name]['task_args']['disable_comp'];
905
- if (!$disable_comp) {
906
- $this->_log("Compression is not supported by ZipArchive");
907
- }
908
- $zip = new ZipArchive();
909
- $result = $zip->open($backup_file, ZIPARCHIVE::OVERWRITE); // Tries to open $backup_file for acrhiving
910
- if ($result === true) {
911
- $result = $result && $zip->addFile(IWP_BACKUP_DIR.'/iwp_db/index.php', "iwp_db/index.php"); // Tries to add iwp_db/index.php to $backup_file
912
- $result = $result && $zip->addFile($db_result, "iwp_db/" . basename($db_result)); // Tries to add db dump form iwp_db dir to $backup_file
913
- $result = $result && $zip->close(); // Tries to close $backup_file
914
- } else {
915
- $result = false;
916
- }
917
-
918
- return $result; // true if $backup_file iz zipped successfully, false if error is occured in zip process
919
- }
920
-
921
- /**
922
- * Zipping whole site root folder and append to backup file with database dump
923
- * by ZipArchive class, requires php zip extension.
924
- *
925
- * @param string $task_name the name of backup task
926
- * @param string $backup_file absolute path to zip file
927
- * @param array $exclude array of files of folders to exclude, relative to site's root
928
- * @param array $include array of folders from site root which are included to backup (wp-admin, wp-content, wp-includes are default)
929
- * @return array|bool true if successful or an array with error message if not
930
- */
931
- function zip_archive_backup($task_name, $backup_file, $exclude, $include, $overwrite = false) {
932
-
933
- $filelist = $this->get_backup_files($exclude, $include);
934
- $disable_comp = $this->tasks[$task_name]['task_args']['disable_comp'];
935
- if (!$disable_comp) {
936
- $this->_log("Compression is not supported by ZipArchive");
937
- }
938
- $zip = new ZipArchive();
939
- if ($overwrite) {
940
- $result = $zip->open($backup_file, ZipArchive::OVERWRITE); // Tries to open $backup_file for acrhiving
941
- } else {
942
- $result = $zip->open($backup_file); // Tries to open $backup_file for acrhiving
943
- }
944
- if ($result === true) {
945
- foreach ($filelist as $file) {
946
- iwp_mmb_auto_print('zip_archive_backup');
947
- $result = $result && $zip->addFile($file, sprintf("%s", str_replace(ABSPATH, '', $file))); // Tries to add a new file to $backup_file
948
- }
949
- $result = $result && $zip->close(); // Tries to close $backup_file
950
- } else {
951
- $result = false;
952
- }
953
- return $result; // true if $backup_file iz zipped successfully, false if error is occured in zip process
954
- }
955
-
956
-
957
- /**
958
- * Gets an array of relative paths of all files in site root recursively.
959
- * By default, there are all files from root folder, all files from folders wp-admin, wp-content, wp-includes recursively.
960
- * Parameter $include adds other folders from site root, and excludes any file or folder by relative path to site's root.
961
- *
962
- * @param array $exclude array of files of folders to exclude, relative to site's root
963
- * @param array $include array of folders from site root which are included to backup (wp-admin, wp-content, wp-includes are default)
964
- * @return array array with all files in site root dir
965
- */
966
- function get_backup_files($exclude, $include) {
967
-
968
- $add = array(
969
- trim(WPINC),
970
- trim(basename(WP_CONTENT_DIR)),
971
- "wp-admin"
972
- );
973
-
974
- $include = array_merge($add, $include);
975
-
976
- $filelist = array();
977
- if ($handle = opendir(ABSPATH)) {
978
- while (false !== ($file = readdir($handle))) {
979
- if (is_dir($file) && file_exists(ABSPATH . $file) && !(in_array($file, $include))) {
980
- $exclude[] = $file;
981
- }
982
- }
983
- closedir($handle);
984
- }
985
-
986
- $filelist = get_all_files_from_dir(ABSPATH, $exclude);
987
-
988
- return $filelist;
989
- }
990
-
991
-
992
- function backup_db()
993
- {
994
- $db_folder = IWP_DB_DIR . '/';
995
- if (!file_exists($db_folder)) {
996
- if (!mkdir($db_folder, 0755, true))
997
- return array(
998
- 'error' => 'Error creating database backup folder (' . $db_folder . '). Make sure you have corrrect write permissions.'
999
- );
1000
- $db_index_file = '<?php
1001
- global $old_url, $old_file_path;
1002
- $old_url = \''.get_option('siteurl').'\';
1003
- $old_file_path = \''.ABSPATH.'\';
1004
- ';
1005
- @file_put_contents(IWP_BACKUP_DIR.'/iwp_db/index.php', $db_index_file);
1006
- }
1007
-
1008
- $file = $db_folder . DB_NAME . '.sql';
1009
-
1010
- if($GLOBALS['fail_safe_db']){
1011
- $result = $this->backup_db_php($file);
1012
- return $result;
1013
- }
1014
-
1015
- $result = $this->backup_db_dump($file); // try mysqldump always then fallback to php dump
1016
- return $result;
1017
- }
1018
-
1019
- function backup_db_dump($file)
1020
- {
1021
- global $wpdb;
1022
- $paths = $this->check_mysql_paths();
1023
- $brace = (substr(PHP_OS, 0, 3) == 'WIN') ? '"' : '';
1024
- $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;
1025
- iwp_mmb_print_flush('DB DUMP CMD: Start');
1026
- ob_start();
1027
- $result = $this->iwp_mmb_exec($command);
1028
- ob_get_clean();
1029
- iwp_mmb_print_flush('DB DUMP CMD: End');
1030
-
1031
- if (!$result) { // Fallback to php
1032
- $result = $this->backup_db_php($file);
1033
- return $result;
1034
- }
1035
-
1036
- if (filesize($file) == 0 || !is_file($file) || !$result) {
1037
- @unlink($file);
1038
- return false;
1039
- } else {
1040
- return $file;
1041
- }
1042
- }
1043
-
1044
- function backup_db_php($file)
1045
- {
1046
- global $wpdb;
1047
-
1048
- if(empty($GLOBALS['fail_safe_db'])){
1049
- iwp_mmb_print_flush('DB DUMP PHP Normal: Start');
1050
- $fp = fopen( $file, 'w' );
1051
- if ( !mysql_ping( $wpdb->dbh ) ) {
1052
- mysql_connect( DB_HOST, DB_USER, DB_PASSWORD );
1053
- mysql_select_db( DB_NAME );
1054
- }
1055
- $_count = 0;
1056
- $insert_sql = '';
1057
- $result = mysql_query( 'SHOW TABLES' );
1058
- if(!$result)
1059
- {
1060
- return array(
1061
- 'error' => 'MySQL '.mysql_error()." "
1062
- );
1063
- }
1064
- while( $row = mysql_fetch_row( $result ) ) {
1065
- $tables[]=$row[0];
1066
- //array_push( $tables, $row[0] );
1067
- }
1068
-
1069
-
1070
- //$tables = $wpdb->get_results('SHOW TABLES', ARRAY_N);
1071
- foreach ($tables as $table) {
1072
- iwp_mmb_auto_print('backup_db_php_normal');
1073
-
1074
- $insert_sql .= "DROP TABLE IF EXISTS $table;";
1075
- //create table
1076
- $table_descr_query = mysql_query("SHOW CREATE TABLE `$table`");
1077
- $fetch_table_descr_row = mysql_fetch_array( $table_descr_query );
1078
- $insert_sql .= "\n\n" . $fetch_table_descr_row[1] . ";\n\n";
1079
-
1080
- fwrite( $fp, $insert_sql );
1081
- $insert_sql = '';
1082
-
1083
- $table_query = mysql_query("SELECT * FROM `$table`");
1084
- $num_fields = mysql_num_fields($table_query);
1085
- while ( $fetch_row = mysql_fetch_array( $table_query ) ) {
1086
- $insert_sql .= "INSERT INTO $table VALUES(";
1087
- for ( $n=1; $n<=$num_fields; $n++ ) {
1088
- $m = $n - 1;
1089
-
1090
- if ( $fetch_row[$m] === NULL ) {
1091
- $insert_sql .= "NULL, ";
1092
- } else {
1093
- $insert_sql .= "'" . mysql_real_escape_string( $fetch_row[$m] ) . "', ";
1094
- }
1095
- }
1096
- $insert_sql = substr( $insert_sql, 0, -2 );
1097
- $insert_sql .= ");\n";
1098
-
1099
- fwrite( $fp, $insert_sql );
1100
- $insert_sql = '';
1101
-
1102
- // Help keep HTTP alive.
1103
- $_count++;
1104
- if ($_count >= 400) {
1105
- echo ' ';
1106
- flush();
1107
- $_count = 0;
1108
- }
1109
- } // End foreach $tables.
1110
-
1111
- $insert_sql .= "\n\n\n";
1112
-
1113
- // testing: mysql_close( $wpdb->dbh );
1114
- // Verify database is still connected and working properly. Sometimes mysql runs out of memory and dies in the above foreach.
1115
- // No point in reconnecting as we can NOT trust that our dump was succesful anymore (it most likely was not).
1116
- if ( @mysql_ping( $wpdb->dbh ) ) { // Still connected to database.
1117
- mysql_free_result( $table_query ); // Free memory.
1118
- } /*else { // Database not connected.
1119
-
1120
- return false;
1121
- }*/
1122
-
1123
- // Help keep HTTP alive.
1124
- echo ' ';
1125
- flush();
1126
-
1127
- //unset( $tables[$table_key] );
1128
- }
1129
- fclose( $fp );
1130
- unset ($fp);
1131
- iwp_mmb_print_flush('DB DUMP PHP Normal: End');
1132
- }
1133
- else{
1134
- iwp_mmb_print_flush('DB DUMP PHP Fail-safe: Start');
1135
- file_put_contents($file, '');//safe to reset any old data
1136
- $tables = $wpdb->get_results('SHOW TABLES', ARRAY_N);
1137
- foreach ($tables as $table) {
1138
-
1139
- //drop existing table
1140
- $dump_data = "DROP TABLE IF EXISTS $table[0];";
1141
- file_put_contents($file, $dump_data, FILE_APPEND);
1142
- //create table
1143
- $create_table = $wpdb->get_row("SHOW CREATE TABLE $table[0]", ARRAY_N);
1144
- $dump_data = "\n\n" . $create_table[1] . ";\n\n";
1145
- file_put_contents($file, $dump_data, FILE_APPEND);
1146
-
1147
- $count = $wpdb->get_var("SELECT count(*) FROM $table[0]");
1148
- if ($count > 100)
1149
- $count = ceil($count / 100);
1150
- else if ($count > 0)
1151
- $count = 1;
1152
-
1153
- for ($i = 0; $i < $count; $i++) {
1154
- iwp_mmb_auto_print('backup_db_php_fail_safe');
1155
- $low_limit = $i * 100;
1156
- $qry = "SELECT * FROM $table[0] LIMIT $low_limit, 100";
1157
- $rows = $wpdb->get_results($qry, ARRAY_A);
1158
- if (is_array($rows)) {
1159
- foreach ($rows as $row) {
1160
- //insert single row
1161
- $dump_data = "INSERT INTO $table[0] VALUES(";
1162
- $num_values = count($row);
1163
- $j = 1;
1164
- foreach ($row as $value) {
1165
- $value = addslashes($value);
1166
- $value = preg_replace("/\n/Ui", "\\n", $value);
1167
- $num_values == $j ? $dump_data .= "'" . $value . "'" : $dump_data .= "'" . $value . "', ";
1168
- $j++;
1169
- unset($value);
1170
- }
1171
- $dump_data .= ");\n";
1172
- file_put_contents($file, $dump_data, FILE_APPEND);
1173
- }
1174
- }
1175
- }
1176
- $dump_data = "\n\n\n";
1177
- file_put_contents($file, $dump_data, FILE_APPEND);
1178
-
1179
- unset($rows);
1180
- unset($dump_data);
1181
- }
1182
- iwp_mmb_print_flush('DB DUMP PHP Fail-safe: End');
1183
- }
1184
-
1185
- if (filesize($file) == 0 || !is_file($file)) {
1186
- @unlink($file);
1187
- return array(
1188
- 'error' => 'Database backup failed. Try to enable MySQL dump on your server.'
1189
- );
1190
- }
1191
-
1192
- return $file;
1193
-
1194
- }
1195
-
1196
- /**
1197
- * Copies a directory from one location to another via the WordPress Filesystem Abstraction.
1198
- * Assumes that WP_Filesystem() has already been called and setup.
1199
- *
1200
- * @since 2.5.0
1201
- *
1202
- * @param string $from source directory
1203
- * @param string $to destination directory
1204
- * @param array $skip_list a list of files/folders to skip copying
1205
- * @return mixed WP_Error on failure, True on success.
1206
- */
1207
- function iwp_mmb_direct_to_any_copy_dir($from, $to, $skip_list = array() ) {//$from => direct file system, $to => automatic filesystem
1208
- global $wp_filesystem;
1209
-
1210
- $wp_temp_direct = new WP_Filesystem_Direct('');
1211
-
1212
-
1213
- $dirlist = $wp_temp_direct->dirlist($from);
1214
-
1215
- $from = trailingslashit($from);
1216
- $to = trailingslashit($to);
1217
-
1218
- $skip_regex = '';
1219
- foreach ( (array)$skip_list as $key => $skip_file )
1220
- $skip_regex .= preg_quote($skip_file, '!') . '|';
1221
-
1222
- if ( !empty($skip_regex) )
1223
- $skip_regex = '!(' . rtrim($skip_regex, '|') . ')$!i';
1224
-
1225
- foreach ( (array) $dirlist as $filename => $fileinfo ) {
1226
- if ( !empty($skip_regex) )
1227
- if ( preg_match($skip_regex, $from . $filename) )
1228
- continue;
1229
-
1230
- if ( 'f' == $fileinfo['type'] ) {
1231
- if ( ! $this->iwp_mmb_direct_to_any_copy($from . $filename, $to . $filename, true, FS_CHMOD_FILE) ) {
1232
- // If copy failed, chmod file to 0644 and try again.
1233
- $wp_filesystem->chmod($to . $filename, 0644);
1234
- if ( ! $this->iwp_mmb_direct_to_any_copy($from . $filename, $to . $filename, true, FS_CHMOD_FILE) )
1235
- return new WP_Error('copy_failed', __('Could not copy file.'), $to . $filename);
1236
- }
1237
- } elseif ( 'd' == $fileinfo['type'] ) {
1238
- if ( !$wp_filesystem->is_dir($to . $filename) ) {
1239
- if ( !$wp_filesystem->mkdir($to . $filename, FS_CHMOD_DIR) )
1240
- return new WP_Error('mkdir_failed', __('Could not create directory.'), $to . $filename);
1241
- }
1242
- $result = $this->iwp_mmb_direct_to_any_copy_dir($from . $filename, $to . $filename, $skip_list);
1243
- if ( is_wp_error($result) )
1244
- return $result;
1245
- }
1246
- }
1247
- return true;
1248
- }
1249
-
1250
- function iwp_mmb_direct_to_any_copy($source, $destination, $overwrite = false, $mode = false){
1251
- global $wp_filesystem;
1252
- if($wp_filesystem->method == 'direct'){
1253
- return $wp_filesystem->copy($source, $destination, $overwrite, $mode);
1254
- }
1255
- elseif($wp_filesystem->method == 'ftpext' || $wp_filesystem->method == 'ftpsockets'){
1256
- if ( ! $overwrite && $wp_filesystem->exists($destination) )
1257
- return false;
1258
- //$content = $this->get_contents($source);
1259
- // if ( false === $content)
1260
- // return false;
1261
-
1262
- //put content
1263
- //$tempfile = wp_tempnam($file);
1264
- $source_handle = fopen($source, 'r');
1265
- if ( ! $source_handle )
1266
- return false;
1267
-
1268
- //fwrite($temp, $contents);
1269
- //fseek($temp, 0); //Skip back to the start of the file being written to
1270
-
1271
- $sample_content = fread($source_handle, (1024 * 1024 * 2));//1024 * 1024 * 2 => 2MB
1272
- fseek($source_handle, 0); //Skip back to the start of the file being written to
1273
-
1274
- $type = $wp_filesystem->is_binary($sample_content) ? FTP_BINARY : FTP_ASCII;
1275
- unset($sample_content);
1276
- if($wp_filesystem->method == 'ftpext'){
1277
- $ret = @ftp_fput($wp_filesystem->link, $destination, $source_handle, $type);
1278
- }
1279
- elseif($wp_filesystem->method == 'ftpsockets'){
1280
- $wp_filesystem->ftp->SetType($type);
1281
- $ret = $wp_filesystem->ftp->fput($destination, $source_handle);
1282
- }
1283
-
1284
- fclose($source_handle);
1285
- unlink($source);//to immediately save system space
1286
- //unlink($tempfile);
1287
-
1288
- $wp_filesystem->chmod($destination, $mode);
1289
-
1290
- return $ret;
1291
-
1292
- //return $this->put_contents($destination, $content, $mode);
1293
- }
1294
- }
1295
-
1296
-
1297
- function restore($args)
1298
- {
1299
-
1300
- global $wpdb, $wp_filesystem;
1301
- if (empty($args)) {
1302
- return false;
1303
- }
1304
-
1305
- extract($args);
1306
- $this->set_memory();
1307
-
1308
- $unlink_file = true; //Delete file after restore
1309
-
1310
- include_once ABSPATH . 'wp-admin/includes/file.php';
1311
-
1312
- //Detect source
1313
- if ($backup_url) {
1314
- //This is for clone (overwrite)
1315
-
1316
- $backup_file = download_url($backup_url);
1317
- if (is_wp_error($backup_file)) {
1318
- return array(
1319
- 'error' => 'Unable to download backup file ('.$backup_file->get_error_message().')'
1320
- );
1321
- }
1322
- $what = 'full';
1323
- } else {
1324
- $tasks = $this->tasks;
1325
- $task = $tasks[$task_name];
1326
- if (isset($task['task_results'][$result_id]['server'])) {
1327
- $backup_file = $task['task_results'][$result_id]['server']['file_path'];
1328
- $unlink_file = false; //Don't delete file if stored on server
1329
-
1330
- } elseif (isset($task['task_results'][$result_id]['ftp'])) {
1331
- $ftp_file = $task['task_results'][$result_id]['ftp'];
1332
- $args = $task['task_args']['account_info']['iwp_ftp'];
1333
- $args['backup_file'] = $ftp_file;
1334
- iwp_mmb_print_flush('FTP download: Start');
1335
- $backup_file = $this->get_ftp_backup($args);
1336
- iwp_mmb_print_flush('FTP download: End');
1337
- if ($backup_file == false) {
1338
- return array(
1339
- 'error' => 'Failed to download file from FTP.'
1340
- );
1341
- }
1342
- } elseif (isset($task['task_results'][$result_id]['amazons3'])) {
1343
- $amazons3_file = $task['task_results'][$result_id]['amazons3'];
1344
- $args = $task['task_args']['account_info']['iwp_amazon_s3'];
1345
- $args['backup_file'] = $amazons3_file;
1346
- iwp_mmb_print_flush('Amazon S3 download: Start');
1347
- $backup_file = $this->get_amazons3_backup($args);
1348
- iwp_mmb_print_flush('Amazon S3 download: End');
1349
- if ($backup_file == false) {
1350
- return array(
1351
- 'error' => 'Failed to download file from Amazon S3.'
1352
- );
1353
- }
1354
- } elseif(isset($task['task_results'][$result_id]['dropbox'])){
1355
- $dropbox_file = $task['task_results'][$result_id]['dropbox'];
1356
- $args = $task['task_args']['account_info']['iwp_dropbox'];
1357
- $args['backup_file'] = $dropbox_file;
1358
- iwp_mmb_print_flush('Dropbox download: Start');
1359
- $backup_file = $this->get_dropbox_backup($args);
1360
- iwp_mmb_print_flush('Dropbox download: End');
1361
-
1362
- if ($backup_file == false) {
1363
- return array(
1364
- 'error' => 'Failed to download file from Dropbox.'
1365
- );
1366
- }
1367
- }
1368
-
1369
-
1370
- $what = $tasks[$task_name]['task_args']['what'];
1371
- }
1372
-
1373
-
1374
-
1375
- $this->wpdb_reconnect();
1376
-
1377
- /////////////////// dev ////////////////////////
1378
-
1379
-
1380
- if (!$this->is_server_writable()) {
1381
- return array(
1382
- 'error' => 'Failed, please add FTP details'
1383
- );
1384
- }
1385
-
1386
- $url = wp_nonce_url('index.php?page=iwp_no_page','iwp_fs_cred');
1387
- ob_start();
1388
- if (false === ($creds = request_filesystem_credentials($url, '', false, ABSPATH, null) ) ) {
1389
- return array(
1390
- 'error' => 'Unable to get file system credentials'
1391
- ); // stop processing here
1392
- }
1393
- ob_end_clean();
1394
-
1395
- if ( ! WP_Filesystem($creds, ABSPATH) ) {
1396
- //request_filesystem_credentials($url, '', true, false, null);
1397
- return array(
1398
- 'error' => 'Unable to initiate file system. Please check you have entered valid FTP credentials.'
1399
- ); // stop processing here
1400
- //return;
1401
- }
1402
-
1403
- require_once(ABSPATH . 'wp-admin/includes/class-wp-filesystem-direct.php');//will be used to copy from temp directory
1404
-
1405
- // do process
1406
- $temp_dir = get_temp_dir();
1407
- $new_temp_folder = untrailingslashit($temp_dir);
1408
- $temp_uniq = md5(microtime(1));//should be random
1409
- while (is_dir($new_temp_folder .'/'. $temp_uniq )) {
1410
- $temp_uniq = md5(microtime(1));
1411
- }
1412
- $new_temp_folder = trailingslashit($new_temp_folder .'/'. $temp_uniq);
1413
- $is_dir_created = mkdir($new_temp_folder);// new folder should be empty
1414
- if(!$is_dir_created){
1415
- return array(
1416
- 'error' => 'Unable to create a temporary directory.'
1417
- );
1418
- }
1419
-
1420
- //echo '<pre>$new_temp_folder:'; var_dump($new_temp_folder); echo '</pre>';
1421
-
1422
-
1423
- $remote_abspath = $wp_filesystem->abspath();
1424
- if(!empty($remote_abspath)){
1425
- $remote_abspath = trailingslashit($remote_abspath);
1426
- }else{
1427
- return array(
1428
- 'error' => 'Unable to locate WP root directory using file system.'
1429
- );
1430
- }
1431
-
1432
- //global $wp_filesystem;
1433
- // $wp_filesystem->put_contents(
1434
- // '/tmp/example.txt',
1435
- // 'Example contents of a file',
1436
- // FS_CHMOD_FILE // predefined mode settings for WP files
1437
- // );
1438
-
1439
- /////////////////// dev ////////////////////////
1440
-
1441
- if ($backup_file && file_exists($backup_file)) {
1442
- if ($overwrite) {//clone only fresh or existing to existing
1443
- //Keep old db credentials before overwrite
1444
- if (!$wp_filesystem->copy($remote_abspath . 'wp-config.php', $remote_abspath . 'iwp-temp-wp-config.php', true)) {
1445
- if($unlink_file) @unlink($backup_file);
1446
- return array(
1447
- 'error' => 'Error creating wp-config. Please check your write permissions.'
1448
- );
1449
- }
1450
-
1451
- $db_host = DB_HOST;
1452
- $db_user = DB_USER;
1453
- $db_password = DB_PASSWORD;
1454
- $home = rtrim(get_option('home'), "/");
1455
- $site_url = get_option('site_url');
1456
-
1457
- $clone_options = array();
1458
- if (trim($clone_from_url) || trim($iwp_clone) || trim($maintain_old_key)) {
1459
-
1460
- $clone_options['iwp_client_nossl_key'] = get_option('iwp_client_nossl_key');
1461
- $clone_options['iwp_client_public_key'] = get_option('iwp_client_public_key');
1462
- $clone_options['iwp_client_action_message_id'] = get_option('iwp_client_action_message_id');
1463
-
1464
- }
1465
-
1466
- $clone_options['iwp_client_backup_tasks'] = serialize(get_option('iwp_client_backup_tasks'));
1467
- $clone_options['iwp_client_notifications'] = serialize(get_option('iwp_client_notifications'));
1468
- $clone_options['iwp_client_pageview_alerts'] = serialize(get_option('iwp_client_pageview_alerts'));
1469
-
1470
-
1471
- } else {
1472
- $restore_options = array();
1473
- $restore_options['iwp_client_notifications'] = serialize(get_option('iwp_client_notifications'));
1474
- $restore_options['iwp_client_pageview_alerts'] = serialize(get_option('iwp_client_pageview_alerts'));
1475
- $restore_options['iwp_client_user_hit_count'] = serialize(get_option('iwp_client_user_hit_count'));
1476
- $restore_options['iwp_client_backup_tasks'] = serialize(get_option('iwp_client_backup_tasks'));
1477
- }
1478
-
1479
-
1480
-
1481
-
1482
- //Backup file will be extracted to a temporary path
1483
-
1484
- //chdir(ABSPATH);
1485
- $unzip = $this->get_unzip();
1486
- $command = "$unzip -o $backup_file -d $new_temp_folder";
1487
- iwp_mmb_print_flush('ZIP Extract CMD: Start');
1488
- ob_start();
1489
- $result = $this->iwp_mmb_exec($command);
1490
- ob_get_clean();
1491
- iwp_mmb_print_flush('ZIP Extract CMD: End');
1492
-
1493
- if (!$result) { //fallback to pclzip
1494
- define('PCLZIP_TEMPORARY_DIR', IWP_BACKUP_DIR . '/');
1495
- //require_once ABSPATH . '/wp-admin/includes/class-pclzip.php';
1496
- require_once $GLOBALS['iwp_mmb_plugin_dir'].'/pclzip.class.php';
1497
- iwp_mmb_print_flush('ZIP Extract PCL: Start');
1498
- $archive = new IWPPclZip($backup_file);
1499
- $result = $archive->extract(PCLZIP_OPT_PATH, $new_temp_folder, PCLZIP_OPT_REPLACE_NEWER);
1500
- iwp_mmb_print_flush('ZIP Extract PCL: End');
1501
- }
1502
- $this->wpdb_reconnect();
1503
-
1504
- if ($unlink_file) {
1505
- @unlink($backup_file);
1506
- }
1507
-
1508
- if (!$result) {
1509
- return array(
1510
- 'error' => 'Failed to unzip files. pclZip error (' . $archive->error_code . '): .' . $archive->error_string
1511
- );
1512
- }
1513
-
1514
- $db_result = $this->restore_db($new_temp_folder);
1515
-
1516
- if (!$db_result) {
1517
- return array(
1518
- 'error' => 'Error restoring database.'
1519
- );
1520
- } else if(is_array($db_result) && isset($db_result['error'])){
1521
- return array(
1522
- 'error' => $db_result['error']
1523
- );
1524
- }
1525
-
1526
- } else {
1527
- return array(
1528
- 'error' => 'Backup file not found.'
1529
- );
1530
- }
1531
-
1532
-
1533
- //copy files from temp to ABSPATH
1534
- $copy_result = $this->iwp_mmb_direct_to_any_copy_dir($new_temp_folder, $remote_abspath);
1535
-
1536
- if ( is_wp_error($copy_result) ){
1537
- $wp_temp_direct2 = new WP_Filesystem_Direct('');
1538
- $wp_temp_direct2->delete($new_temp_folder, true);
1539
- return $copy_result;
1540
- }
1541
-
1542
-
1543
- $this->wpdb_reconnect();
1544
-
1545
-
1546
-
1547
- //Replace options and content urls
1548
- if ($overwrite) {//fresh WP package or existing to existing site
1549
- //Get New Table prefix
1550
- $new_table_prefix = trim($this->get_table_prefix());
1551
- //Retrieve old wp_config
1552
- //@unlink(ABSPATH . 'wp-config.php');
1553
- $wp_filesystem->delete($remote_abspath . 'wp-config.php', false, 'f');
1554
- //Replace table prefix
1555
- //$lines = file(ABSPATH . 'iwp-temp-wp-config.php');
1556
- $lines = $wp_filesystem->get_contents_array($remote_abspath . 'iwp-temp-wp-config.php');
1557
-
1558
- $new_lines = '';
1559
- foreach ($lines as $line) {
1560
- if (strstr($line, '$table_prefix')) {
1561
- $line = '$table_prefix = "' . $new_table_prefix . '";' . PHP_EOL;
1562
- }
1563
- $new_lines .= $line;
1564
- //file_put_contents(ABSPATH . 'wp-config.php', $line, FILE_APPEND);
1565
- }
1566
-
1567
- $wp_filesystem->put_contents($remote_abspath . 'wp-config.php', $new_lines);
1568
-
1569
- //@unlink(ABSPATH . 'iwp-temp-wp-config.php');
1570
- $wp_filesystem->delete($remote_abspath . 'iwp-temp-wp-config.php', false, 'f');
1571
-
1572
- //Replace options
1573
- $query = "SELECT option_value FROM " . $new_table_prefix . "options WHERE option_name = 'home'";
1574
- $old = $wpdb->get_var($query);
1575
- $old = rtrim($old, "/");
1576
- $query = "UPDATE " . $new_table_prefix . "options SET option_value = %s WHERE option_name = 'home'";
1577
- $wpdb->query($wpdb->prepare($query, $home));
1578
- $query = "UPDATE " . $new_table_prefix . "options SET option_value = %s WHERE option_name = 'siteurl'";
1579
- $wpdb->query($wpdb->prepare($query, $home));
1580
- //Replace content urls
1581
-
1582
- $regexp1 = 'src="(.*)'.$old.'(.*)"';
1583
- $regexp2 = 'href="(.*)'.$old.'(.*)"';
1584
- $query = "UPDATE " . $new_table_prefix . "posts SET post_content = REPLACE (post_content, %s,%s) WHERE post_content REGEXP %s OR post_content REGEXP %s";
1585
- $wpdb->query($wpdb->prepare($query, $old, $home, $regexp1, $regexp2));
1586
-
1587
- if (trim($new_password)) {
1588
- $new_password = wp_hash_password($new_password);
1589
- }
1590
- if (!trim($clone_from_url) && !trim($iwp_clone)) {
1591
- if ($new_user && $new_password) {
1592
- $query = "UPDATE " . $new_table_prefix . "users SET user_login = %s, user_pass = %s WHERE user_login = %s";
1593
- $wpdb->query($wpdb->prepare($query, $new_user, $new_password, $old_user));
1594
- }
1595
- } else {
1596
-
1597
- // if ($iwp_clone) {
1598
- if ($admin_email) {
1599
- //Clean Install
1600
- $query = "UPDATE " . $new_table_prefix . "options SET option_value = %s WHERE option_name = 'admin_email'";
1601
- $wpdb->query($wpdb->prepare($query, $admin_email));
1602
- $query = "SELECT * FROM " . $new_table_prefix . "users LIMIT 1";
1603
- $temp_user = $wpdb->get_row($query);
1604
- if (!empty($temp_user)) {
1605
- $query = "UPDATE " . $new_table_prefix . "users SET user_email=%s, user_login = %s, user_pass = %s WHERE user_login = %s";
1606
- $wpdb->query($wpdb->prepare($query, $admin_email, $new_user, $new_password, $temp_user->user_login));
1607
- }
1608
-
1609
- }
1610
- // }
1611
-
1612
- //if ($clone_from_url) {
1613
- if ($new_user && $new_password) {
1614
- $query = "UPDATE " . $new_table_prefix . "users SET user_pass = %s WHERE user_login = %s";
1615
- $wpdb->query($wpdb->prepare($query, $new_password, $new_user));
1616
- }
1617
- // }
1618
-
1619
- }
1620
-
1621
- if (is_array($clone_options) && !empty($clone_options)) {
1622
- foreach ($clone_options as $key => $option) {
1623
- if (!empty($key)) {
1624
- $query = "SELECT option_value FROM " . $new_table_prefix . "options WHERE option_name = %s";
1625
- $res = $wpdb->get_var($wpdb->prepare($query, $key));
1626
- if ($res == false) {
1627
- $query = "INSERT INTO " . $new_table_prefix . "options (option_value,option_name) VALUES(%s,%s)";
1628
- $wpdb->query($wpdb->prepare($query, $option, $key));
1629
- } else {
1630
- $query = "UPDATE " . $new_table_prefix . "options SET option_value = %s WHERE option_name = %s";
1631
- $wpdb->query($wpdb->prepare($query, $option, $key));
1632
- }
1633
- }
1634
- }
1635
- }
1636
-
1637
- //Remove hit count
1638
- $query = "DELETE FROM " . $new_table_prefix . "options WHERE option_name = 'iwp_client_user_hit_count'";
1639
- $wpdb->query($query);
1640
-
1641
- //Check for .htaccess permalinks update
1642
- $this->replace_htaccess($home, $remote_abspath);
1643
- } else {
1644
- //restore client options
1645
- if (is_array($restore_options) && !empty($restore_options)) {
1646
- foreach ($restore_options as $key => $option) {
1647
- if (!empty($key)) {
1648
- $query = "SELECT option_value FROM " . $wpdb->base_prefix . "options WHERE option_name = %s";
1649
- $res = $wpdb->get_var($wpdb->prepare($query, $key));
1650
- if ($res == false) {
1651
- $query = "INSERT INTO " . $wpdb->base_prefix . "options (option_value,option_name) VALUES(%s,%s)";
1652
- $wpdb->query($wpdb->prepare($query, $option, $key));
1653
- } else {
1654
- $query = "UPDATE " . $wpdb->base_prefix . "options SET option_value = %s WHERE option_name = %s";
1655
- $wpdb->query($wpdb->prepare($query, $option, $key));
1656
- }
1657
- }
1658
-
1659
- /*$test = update_option($key,$option);*/
1660
- }
1661
- }
1662
- }
1663
-
1664
- //clear the temp directory
1665
- $wp_temp_direct2 = new WP_Filesystem_Direct('');
1666
- $wp_temp_direct2->delete($new_temp_folder, true);
1667
-
1668
- return !empty($new_user) ? $new_user : true ;
1669
- }
1670
-
1671
- function restore_db($new_temp_folder)
1672
- {
1673
- global $wpdb;
1674
- $paths = $this->check_mysql_paths();
1675
- $file_path = $new_temp_folder . '/iwp_db';
1676
- @chmod($file_path,0755);
1677
- $file_name = glob($file_path . '/*.sql');
1678
- $file_name = $file_name[0];
1679
-
1680
- if(!$file_name){
1681
- return array('error' => 'Cannot access database file.');
1682
- }
1683
-
1684
- $brace = (substr(PHP_OS, 0, 3) == 'WIN') ? '"' : '';
1685
- $command = $brace . $paths['mysql'] . $brace . ' --host="' . DB_HOST . '" --user="' . DB_USER . '" --password="' . DB_PASSWORD . '" --default-character-set="utf8" ' . DB_NAME . ' < ' . $brace . $file_name . $brace;
1686
- iwp_mmb_print_flush('DB Restore CMD: Start');
1687
- ob_start();
1688
- $result = $this->iwp_mmb_exec($command);
1689
- ob_get_clean();
1690
- iwp_mmb_print_flush('DB Restore CMD: End');
1691
- if (!$result) {
1692
- //try php
1693
- $this->restore_db_php($file_name);
1694
- }
1695
-
1696
-
1697
- @unlink($file_name);
1698
- @unlink(dirname($file_name).'/index.php');
1699
- @rmdir(dirname($file_name));//remove its folder
1700
- return true;
1701
- }
1702
-
1703
- function restore_db_php($file_name)
1704
- {
1705
-
1706
- $this->wpdb_reconnect();
1707
- global $wpdb;
1708
-
1709
- $wpdb->query("SET NAMES 'utf8'");
1710
-
1711
- $current_query = '';
1712
- // Read in entire file
1713
- $lines = file($file_name);
1714
- // Loop through each line
1715
- if(!empty($lines)){
1716
- foreach ($lines as $line) {
1717
- iwp_mmb_auto_print('restore_db_php');
1718
- // Skip it if it's a comment
1719
- if (substr($line, 0, 2) == '--' || $line == '')
1720
- continue;
1721
-
1722
- // Add this line to the current query
1723
- $current_query .= $line;
1724
- // If it has a semicolon at the end, it's the end of the query
1725
- if (substr(trim($line), -1, 1) == ';') {
1726
- // Perform the query
1727
- $result = $wpdb->query($current_query);
1728
- if ($result === false)
1729
- return false;
1730
- // Reset temp variable to empty
1731
- $current_query = '';
1732
- }
1733
- }
1734
- }
1735
-
1736
- return true;
1737
- }
1738
-
1739
- function get_table_prefix()
1740
- {
1741
- $lines = file(ABSPATH . 'wp-config.php');
1742
- foreach ($lines as $line) {
1743
- if (strstr($line, '$table_prefix')) {
1744
- $pattern = "/(\'|\")[^(\'|\")]*/";
1745
- preg_match($pattern, $line, $matches);
1746
- $prefix = substr($matches[0], 1);
1747
- return $prefix;
1748
- break;
1749
- }
1750
- }
1751
- return 'wp_'; //default
1752
- }
1753
-
1754
- function optimize_tables()
1755
- {
1756
- global $wpdb;
1757
- $query = 'SHOW TABLE STATUS';
1758
- $tables = $wpdb->get_results($query, ARRAY_A);
1759
- foreach ($tables as $table) {
1760
- if (in_array($table['Engine'], array(
1761
- 'MyISAM',
1762
- 'ISAM',
1763
- 'HEAP',
1764
- 'MEMORY',
1765
- 'ARCHIVE'
1766
- )))
1767
- $table_string .= $table['Name'] . ",";
1768
- elseif ($table['Engine'] == 'InnoDB') {
1769
- $optimize = $wpdb->query("ALTER TABLE {$table['Name']} ENGINE=InnoDB");
1770
- }
1771
- }
1772
-
1773
- if(!empty($table_string)){
1774
- $table_string = rtrim($table_string, ',');
1775
- $optimize = $wpdb->query("OPTIMIZE TABLE $table_string");
1776
- }
1777
-
1778
- return $optimize ? true : false;
1779
- }
1780
-
1781
- ### Function: Auto Detect MYSQL and MYSQL Dump Paths
1782
- function check_mysql_paths()
1783
- {
1784
- global $wpdb;
1785
- $paths = array(
1786
- 'mysql' => '',
1787
- 'mysqldump' => ''
1788
- );
1789
- if (substr(PHP_OS, 0, 3) == 'WIN') {
1790
- $mysql_install = $wpdb->get_row("SHOW VARIABLES LIKE 'basedir'");
1791
- if ($mysql_install) {
1792
- $install_path = str_replace('\\', '/', $mysql_install->Value);
1793
- $paths['mysql'] = $install_path . 'bin/mysql.exe';
1794
- $paths['mysqldump'] = $install_path . 'bin/mysqldump.exe';
1795
- } else {
1796
- $paths['mysql'] = 'mysql.exe';
1797
- $paths['mysqldump'] = 'mysqldump.exe';
1798
- }
1799
- } else {
1800
- $paths['mysql'] = $this->iwp_mmb_exec('which mysql', true);
1801
- if (empty($paths['mysql']))
1802
- $paths['mysql'] = 'mysql'; // try anyway
1803
-
1804
- $paths['mysqldump'] = $this->iwp_mmb_exec('which mysqldump', true);
1805
- if (empty($paths['mysqldump']))
1806
- $paths['mysqldump'] = 'mysqldump'; // try anyway
1807
-
1808
- }
1809
-
1810
-
1811
- return $paths;
1812
- }
1813
-
1814
- //Check if exec, system, passthru functions exist
1815
- function check_sys()
1816
- {
1817
- if ($this->iwp_mmb_function_exists('exec'))
1818
- return 'exec';
1819
-
1820
- if ($this->iwp_mmb_function_exists('system'))
1821
- return 'system';
1822
-
1823
- if ($this->iwp_mmb_function_exists('passhtru'))
1824
- return 'passthru';
1825
-
1826
- return false;
1827
-
1828
- }
1829
-
1830
- function iwp_mmb_exec($command, $string = false, $rawreturn = false)
1831
- {
1832
- if ($command == '')
1833
- return false;
1834
-
1835
- if ($this->iwp_mmb_function_exists('exec')) {
1836
- $log = @exec($command, $output, $return);
1837
-
1838
- if ($string)
1839
- return $log;
1840
- if ($rawreturn)
1841
- return $return;
1842
-
1843
- return $return ? false : true;
1844
- } elseif ($this->iwp_mmb_function_exists('system')) {
1845
- $log = @system($command, $return);
1846
-
1847
- if ($string)
1848
- return $log;
1849
-
1850
- if ($rawreturn)
1851
- return $return;
1852
-
1853
- return $return ? false : true;
1854
- } elseif ($this->iwp_mmb_function_exists('passthru') && !$string) {
1855
- $log = passthru($command, $return);
1856
-
1857
- if ($rawreturn)
1858
- return $return;
1859
-
1860
- return $return ? false : true;
1861
- }
1862
-
1863
- if ($rawreturn)
1864
- return -1;
1865
-
1866
- return false;
1867
- }
1868
-
1869
- function get_zip()
1870
- {
1871
- $zip = $this->iwp_mmb_exec('which zip', true);
1872
- if (!$zip)
1873
- $zip = "zip";
1874
- return $zip;
1875
- }
1876
-
1877
- function get_unzip()
1878
- {
1879
- $unzip = $this->iwp_mmb_exec('which unzip', true);
1880
- if (!$unzip)
1881
- $unzip = "unzip";
1882
- return $unzip;
1883
- }
1884
-
1885
- function check_backup_compat()
1886
- {
1887
- $reqs = array();
1888
- if (strpos($_SERVER['DOCUMENT_ROOT'], '/') === 0) {
1889
- $reqs['Server OS']['status'] = 'Linux (or compatible)';
1890
- $reqs['Server OS']['pass'] = true;
1891
- } else {
1892
- $reqs['Server OS']['status'] = 'Windows';
1893
- $reqs['Server OS']['pass'] = true;
1894
- $pass = false;
1895
- }
1896
- $reqs['PHP Version']['status'] = phpversion();
1897
- if ((float) phpversion() >= 5.1) {
1898
- $reqs['PHP Version']['pass'] = true;
1899
- } else {
1900
- $reqs['PHP Version']['pass'] = false;
1901
- $pass = false;
1902
- }
1903
-
1904
-
1905
- if (is_writable(WP_CONTENT_DIR)) {
1906
- $reqs['Backup Folder']['status'] = "writable";
1907
- $reqs['Backup Folder']['pass'] = true;
1908
- } else {
1909
- $reqs['Backup Folder']['status'] = "not writable";
1910
- $reqs['Backup Folder']['pass'] = false;
1911
- }
1912
-
1913
-
1914
- $file_path = IWP_BACKUP_DIR;
1915
- $reqs['Backup Folder']['status'] .= ' (' . $file_path . ')';
1916
-
1917
- if ($func = $this->check_sys()) {
1918
- $reqs['Execute Function']['status'] = $func;
1919
- $reqs['Execute Function']['pass'] = true;
1920
- } else {
1921
- $reqs['Execute Function']['status'] = "not found";
1922
- $reqs['Execute Function']['info'] = "(will try PHP replacement)";
1923
- $reqs['Execute Function']['pass'] = false;
1924
- }
1925
- $reqs['Zip']['status'] = $this->get_zip();
1926
-
1927
- $reqs['Zip']['pass'] = true;
1928
-
1929
-
1930
-
1931
- $reqs['Unzip']['status'] = $this->get_unzip();
1932
-
1933
- $reqs['Unzip']['pass'] = true;
1934
-
1935
- $paths = $this->check_mysql_paths();
1936
-
1937
- if (!empty($paths['mysqldump'])) {
1938
- $reqs['MySQL Dump']['status'] = $paths['mysqldump'];
1939
- $reqs['MySQL Dump']['pass'] = true;
1940
- } else {
1941
- $reqs['MySQL Dump']['status'] = "not found";
1942
- $reqs['MySQL Dump']['info'] = "(will try PHP replacement)";
1943
- $reqs['MySQL Dump']['pass'] = false;
1944
- }
1945
-
1946
- $exec_time = ini_get('max_execution_time');
1947
- $reqs['Execution time']['status'] = $exec_time ? $exec_time . "s" : 'unknown';
1948
- $reqs['Execution time']['pass'] = true;
1949
-
1950
- $mem_limit = ini_get('memory_limit');
1951
- $reqs['Memory limit']['status'] = $mem_limit ? $mem_limit : 'unknown';
1952
- $reqs['Memory limit']['pass'] = true;
1953
-
1954
-
1955
- return $reqs;
1956
- }
1957
-
1958
- function ftp_backup($args)
1959
- {
1960
- extract($args);
1961
- //Args: $ftp_username, $ftp_password, $ftp_hostname, $backup_file, $ftp_remote_folder, $ftp_site_folder
1962
- $port = $ftp_port ? $ftp_port : 21; //default port is 21
1963
- if ($ftp_ssl) {
1964
- if (function_exists('ftp_ssl_connect')) {
1965
- $conn_id = ftp_ssl_connect($ftp_hostname,$port);
1966
- if ($conn_id === false) {
1967
- return array(
1968
- 'error' => 'Failed to connect to ' . $ftp_hostname,
1969
- 'partial' => 1
1970
- );
1971
- }
1972
- } else {
1973
- return array(
1974
- 'error' => 'Your server doesn\'t support FTP SSL',
1975
- 'partial' => 1
1976
- );
1977
- }
1978
- } else {
1979
- if (function_exists('ftp_connect')) {
1980
- $conn_id = ftp_connect($ftp_hostname,$port);
1981
- if ($conn_id === false) {
1982
- return array(
1983
- 'error' => 'Failed to connect to ' . $ftp_hostname,
1984
- 'partial' => 1
1985
- );
1986
- }
1987
- } else {
1988
- return array(
1989
- 'error' => 'Your server doesn\'t support FTP',
1990
- 'partial' => 1
1991
- );
1992
- }
1993
- }
1994
- $login = @ftp_login($conn_id, $ftp_username, $ftp_password);
1995
- if ($login === false) {
1996
- return array(
1997
- 'error' => 'FTP login failed for ' . $ftp_username . ', ' . $ftp_password,
1998
- 'partial' => 1
1999
- );
2000
- }
2001
-
2002
- if($ftp_passive){
2003
- @ftp_pasv($conn_id,true);
2004
- }
2005
-
2006
- @ftp_mkdir($conn_id, $ftp_remote_folder);
2007
- if ($ftp_site_folder) {
2008
- $ftp_remote_folder .= '/' . $this->site_name;
2009
- }
2010
- @ftp_mkdir($conn_id, $ftp_remote_folder);
2011
-
2012
- $upload = @ftp_put($conn_id, $ftp_remote_folder . '/' . basename($backup_file), $backup_file, FTP_BINARY);
2013
-
2014
- if ($upload === false) { //Try ascii
2015
- $upload = @ftp_put($conn_id, $ftp_remote_folder . '/' . basename($backup_file), $backup_file, FTP_ASCII);
2016
- }
2017
- @ftp_close($conn_id);
2018
-
2019
- if ($upload === false) {
2020
- return array(
2021
- 'error' => 'Failed to upload file to FTP. Please check your specified path.',
2022
- 'partial' => 1
2023
- );
2024
- }
2025
-
2026
- return true;
2027
- }
2028
-
2029
- function remove_ftp_backup($args)
2030
- {
2031
- extract($args);
2032
- //Args: $ftp_username, $ftp_password, $ftp_hostname, $backup_file, $ftp_remote_folder
2033
- $port = $ftp_port ? $ftp_port : 21; //default port is 21
2034
- if ($ftp_ssl && function_exists('ftp_ssl_connect')) {
2035
- $conn_id = ftp_ssl_connect($ftp_hostname,$port);
2036
- } else if (function_exists('ftp_connect')) {
2037
- $conn_id = ftp_connect($ftp_hostname,$port);
2038
- }
2039
-
2040
- if ($conn_id) {
2041
- $login = @ftp_login($conn_id, $ftp_username, $ftp_password);
2042
- if ($ftp_site_folder)
2043
- $ftp_remote_folder .= '/' . $this->site_name;
2044
-
2045
- if($ftp_passive){
2046
- @ftp_pasv($conn_id,true);
2047
- }
2048
-
2049
- $delete = ftp_delete($conn_id, $ftp_remote_folder . '/' . $backup_file);
2050
-
2051
- ftp_close($conn_id);
2052
- }
2053
-
2054
- }
2055
-
2056
- function get_ftp_backup($args)
2057
- {
2058
- extract($args);
2059
- //Args: $ftp_username, $ftp_password, $ftp_hostname, $backup_file, $ftp_remote_folder
2060
- $port = $ftp_port ? $ftp_port : 21; //default port is 21
2061
- if ($ftp_ssl && function_exists('ftp_ssl_connect')) {
2062
- $conn_id = ftp_ssl_connect($ftp_hostname,$port);
2063
-
2064
- } else if (function_exists('ftp_connect')) {
2065
- $conn_id = ftp_connect($ftp_hostname,$port);
2066
- if ($conn_id === false) {
2067
- return false;
2068
- }
2069
- }
2070
- $login = @ftp_login($conn_id, $ftp_username, $ftp_password);
2071
- if ($login === false) {
2072
- return false;
2073
- }
2074
-
2075
- if ($ftp_site_folder)
2076
- $ftp_remote_folder .= '/' . $this->site_name;
2077
-
2078
- if($ftp_passive){
2079
- @ftp_pasv($conn_id,true);
2080
- }
2081
-
2082
- //$temp = ABSPATH . 'iwp_temp_backup.zip';
2083
- $temp = wp_tempnam('iwp_temp_backup.zip');
2084
-
2085
- $get = ftp_get($conn_id, $temp, $ftp_remote_folder . '/' . $backup_file, FTP_BINARY);
2086
- if ($get === false) {
2087
- return false;
2088
- } else {
2089
- }
2090
- ftp_close($conn_id);
2091
-
2092
- return $temp;
2093
- }
2094
-
2095
-
2096
- function dropbox_backup($args){
2097
- extract($args);
2098
-
2099
- if(isset($consumer_secret) && !empty($consumer_secret)){
2100
-
2101
- require_once $GLOBALS['iwp_mmb_plugin_dir'] . '/lib/dropbox.php';
2102
-
2103
- $dropbox = new Dropbox($consumer_key, $consumer_secret);
2104
- $dropbox->setOAuthTokens($oauth_token, $oauth_token_secret);
2105
-
2106
- if ($dropbox_site_folder == true)
2107
- $dropbox_destination .= '/' . $this->site_name . '/' . basename($backup_file);
2108
- else
2109
- $dropbox_destination .= '/' . basename($backup_file);
2110
-
2111
- try {
2112
- $dropbox->upload($backup_file, $dropbox_destination, true);
2113
- } catch (Exception $e) {
2114
- $this->_log($e->getMessage());
2115
- return array(
2116
- 'error' => $e->getMessage(),
2117
- 'partial' => 1
2118
- );
2119
- }
2120
-
2121
- return true;
2122
-
2123
- } else {
2124
- return array(
2125
- 'error' => 'Please connect your InfiniteWP panel with your Dropbox account.'
2126
- );
2127
- }
2128
-
2129
- }
2130
-
2131
-
2132
- function remove_dropbox_backup($args) {
2133
- extract($args);
2134
-
2135
- require_once $GLOBALS['iwp_mmb_plugin_dir'] . '/lib/dropbox.php';
2136
-
2137
- $dropbox = new Dropbox($consumer_key, $consumer_secret);
2138
- $dropbox->setOAuthTokens($oauth_token, $oauth_token_secret);
2139
-
2140
- if ($dropbox_site_folder == true)
2141
- $dropbox_destination .= '/' . $this->site_name;
2142
-
2143
- try {
2144
- $dropbox->fileopsDelete($dropbox_destination . '/' . $backup_file);
2145
- } catch (Exception $e) {
2146
- $this->_log($e->getMessage());
2147
- /*return array(
2148
- 'error' => $e->getMessage(),
2149
- 'partial' => 1
2150
- );*/
2151
- }
2152
-
2153
- //return true;
2154
- }
2155
-
2156
-
2157
- function get_dropbox_backup($args) {
2158
- extract($args);
2159
-
2160
- require_once $GLOBALS['iwp_mmb_plugin_dir'] . '/lib/dropbox.php';
2161
-
2162
- $dropbox = new Dropbox($consumer_key, $consumer_secret);
2163
- $dropbox->setOAuthTokens($oauth_token, $oauth_token_secret);
2164
-
2165
- if ($dropbox_site_folder == true)
2166
- $dropbox_destination .= '/' . $this->site_name;
2167
-
2168
- //$temp = ABSPATH . 'iwp_temp_backup.zip';
2169
- $temp = wp_tempnam('iwp_temp_backup.zip');
2170
-
2171
- try {
2172
-
2173
- $file = $dropbox->download($dropbox_destination.'/'.$backup_file);
2174
- $handle = @fopen($temp, 'w');
2175
- $result = fwrite($handle, $file);
2176
- fclose($handle);
2177
-
2178
- if($result)
2179
- return $temp;
2180
- else
2181
- return false;
2182
- } catch (Exception $e) {
2183
- $this->_log($e->getMessage());
2184
- return array(
2185
- 'error' => $e->getMessage(),
2186
- 'partial' => 1
2187
- );
2188
- }
2189
- }
2190
-
2191
-
2192
- function amazons3_backup($args)
2193
- {
2194
- if ($this->iwp_mmb_function_exists('curl_init')) {
2195
- require_once($GLOBALS['iwp_mmb_plugin_dir'].'/lib/amazon_s3/sdk.class.php');
2196
-
2197
- extract($args);
2198
-
2199
- if ($as3_site_folder == true)
2200
- $as3_directory .= '/' . $this->site_name;
2201
-
2202
- try{
2203
-
2204
- CFCredentials::set(array('development' => array('key' => trim($as3_access_key), 'secret' => trim(str_replace(' ', '+', $as3_secure_key)), 'default_cache_config' => '', 'certificate_authority' => true, 'use_ssl'=>false, 'ssl_verification'=>false), '@default' => 'development'));
2205
- $s3 = new AmazonS3();
2206
- $response = $s3->create_object($as3_bucket, $as3_directory . '/' . basename($backup_file), array('fileUpload' => $backup_file));
2207
- $upload = $response->isOk();
2208
- if($upload) {
2209
- return true;
2210
- } else {
2211
- return array(
2212
- 'error' => 'Failed to upload to Amazon S3. Please check your details and set upload/delete permissions on your bucket.',
2213
- 'partial' => 1
2214
- );
2215
- }
2216
-
2217
- }catch (Exception $e){
2218
- $err = $e->getMessage();
2219
- if($err){
2220
- return array(
2221
- 'error' => 'Failed to upload to AmazonS3 ('.$err.').'
2222
- );
2223
- } else {
2224
- return array(
2225
- 'error' => 'Failed to upload to Amazon S3.'
2226
- );
2227
- }
2228
- }
2229
- } else {
2230
- return array(
2231
- 'error' => 'You cannot use Amazon S3 on your server. Please enable curl first.',
2232
- 'partial' => 1
2233
- );
2234
- }
2235
- }
2236
-
2237
-
2238
- function remove_amazons3_backup($args)
2239
- {
2240
- if ($this->iwp_mmb_function_exists('curl_init')) {
2241
- require_once($GLOBALS['iwp_mmb_plugin_dir'].'/lib/amazon_s3/sdk.class.php');
2242
- extract($args);
2243
- if ($as3_site_folder == true)
2244
- $as3_directory .= '/' . $this->site_name;
2245
- try{
2246
- CFCredentials::set(array('development' => array('key' => trim($as3_access_key), 'secret' => trim(str_replace(' ', '+', $as3_secure_key)), 'default_cache_config' => '', 'certificate_authority' => true), '@default' => 'development'));
2247
- $s3 = new AmazonS3();
2248
- $s3->delete_object($as3_bucket, $as3_directory . '/' . $backup_file);
2249
- } catch (Exception $e){
2250
-
2251
- }
2252
- }
2253
- }
2254
-
2255
- function get_amazons3_backup($args)
2256
- {
2257
- require_once($GLOBALS['iwp_mmb_plugin_dir'].'/lib/amazon_s3/sdk.class.php');
2258
- extract($args);
2259
- $temp = '';
2260
- try{
2261
- CFCredentials::set(array('development' => array('key' => trim($as3_access_key), 'secret' => trim(str_replace(' ', '+', $as3_secure_key)), 'default_cache_config' => '', 'certificate_authority' => true), '@default' => 'development'));
2262
- $s3 = new AmazonS3();
2263
- if ($as3_site_folder == true)
2264
- $as3_directory .= '/' . $this->site_name;
2265
-
2266
- //$temp = ABSPATH . 'iwp_temp_backup.zip';
2267
- $temp = wp_tempnam('iwp_temp_backup.zip');
2268
- $s3->get_object($as3_bucket, $as3_directory . '/' . $backup_file, array("fileDownload" => $temp));
2269
- } catch (Exception $e){
2270
- return $temp;
2271
- }
2272
- return $temp;
2273
- }
2274
- //IWP Remove ends here
2275
-
2276
-
2277
- function schedule_next($type, $schedule)
2278
- {
2279
- $schedule = explode("|", $schedule);
2280
- if (empty($schedule))
2281
- return false;
2282
- switch ($type) {
2283
-
2284
- case 'daily':
2285
-
2286
- if (isset($schedule[1]) && $schedule[1]) {
2287
- $delay_time = $schedule[1] * 60;
2288
- }
2289
-
2290
- $current_hour = date("H");
2291
- $schedule_hour = $schedule[0];
2292
- if ($current_hour >= $schedule_hour){
2293
- $time = mktime($schedule_hour, 0, 0, date("m"), date("d") + 1, date("Y"));
2294
- //$time ='0001#'.$current_hour.'|'.$schedule_hour;
2295
-
2296
- }
2297
-
2298
- else{
2299
- $time = mktime($schedule_hour, 0, 0, date("m"), date("d"), date("Y"));
2300
- //$time ='0000#'.$current_hour.'|'.$schedule_hour;
2301
- }
2302
- $time = time() + 30;
2303
-
2304
-
2305
- break;
2306
-
2307
-
2308
- case 'weekly':
2309
- if (isset($schedule[2]) && $schedule[2]) {
2310
- $delay_time = $schedule[2] * 60;
2311
- }
2312
- $current_weekday = date('w');
2313
- $schedule_weekday = $schedule[1];
2314
- $current_hour = date("H");
2315
- $schedule_hour = $schedule[0];
2316
-
2317
- if ($current_weekday > $schedule_weekday)
2318
- $weekday_offset = 7 - ($week_day - $task_schedule[1]);
2319
- else
2320
- $weekday_offset = $schedule_weekday - $current_weekday;
2321
-
2322
-
2323
- if (!$weekday_offset) { //today is scheduled weekday
2324
- if ($current_hour >= $schedule_hour)
2325
- $time = mktime($schedule_hour, 0, 0, date("m"), date("d") + 7, date("Y"));
2326
- else
2327
- $time = mktime($schedule_hour, 0, 0, date("m"), date("d"), date("Y"));
2328
- } else {
2329
- $time = mktime($schedule_hour, 0, 0, date("m"), date("d") + $weekday_offset, date("Y"));
2330
- }
2331
-
2332
- break;
2333
-
2334
- case 'monthly':
2335
- if (isset($schedule[2]) && $schedule[2]) {
2336
- $delay_time = $schedule[2] * 60;
2337
- }
2338
- $current_monthday = date('j');
2339
- $schedule_monthday = $schedule[1];
2340
- $current_hour = date("H");
2341
- $schedule_hour = $schedule[0];
2342
-
2343
- if ($current_monthday > $schedule_monthday) {
2344
- $time = mktime($schedule_hour, 0, 0, date("m") + 1, $schedule_monthday, date("Y"));
2345
- } else if ($current_monthday < $schedule_monthday) {
2346
- $time = mktime($schedule_hour, 0, 0, date("m"), $schedule_monthday, date("Y"));
2347
- } else if ($current_monthday == $schedule_monthday) {
2348
- if ($current_hour >= $schedule_hour)
2349
- $time = mktime($schedule_hour, 0, 0, date("m") + 1, $schedule_monthday, date("Y"));
2350
- else
2351
- $time = mktime($schedule_hour, 0, 0, date("m"), $schedule_monthday, date("Y"));
2352
- break;
2353
- }
2354
-
2355
- break;
2356
- default:
2357
- break;
2358
- }
2359
-
2360
- if (isset($delay_time) && $delay_time) {
2361
- $time += $delay_time;
2362
- }
2363
-
2364
- return $time;
2365
- }
2366
-
2367
-
2368
- //Parse task arguments for info on IWP Admin Panel
2369
- function get_backup_stats()
2370
- {
2371
- $stats = array();
2372
- $tasks = $this->tasks;
2373
- if (is_array($tasks) && !empty($tasks)) {
2374
- foreach ($tasks as $task_name => $info) {
2375
- if (is_array($info['task_results']) && !empty($info['task_results'])) {
2376
- foreach ($info['task_results'] as $key => $result) {
2377
- if (isset($result['server']) && !isset($result['error'])) {
2378
- if (!file_exists($result['server']['file_path'])) {
2379
- $info['task_results'][$key]['error'] = 'Backup created but manually removed from server.';
2380
- }
2381
- }
2382
- }
2383
- }
2384
- if (is_array($info['task_results']))
2385
- $stats[$task_name] = $info['task_results'];
2386
- }
2387
- }
2388
- return $stats;
2389
- }
2390
-
2391
-
2392
- function get_next_schedules()
2393
- {
2394
- $stats = array();
2395
- $tasks = $this->tasks;
2396
- if (is_array($tasks) && !empty($tasks)) {
2397
- foreach ($tasks as $task_name => $info) {
2398
- $stats[$task_name] = isset($info['task_args']['next']) ? $info['task_args']['next'] : array();
2399
- }
2400
- }
2401
- return $stats;
2402
- }
2403
-
2404
-
2405
- function remove_old_backups($task_name)
2406
- {
2407
- //Check for previous failed backups first
2408
- $this->cleanup();
2409
-
2410
- //Remove by limit
2411
- $backups = $this->tasks;
2412
- if ($task_name == 'Backup Now') {
2413
- $num = 0;
2414
- } else {
2415
- $num = 1;
2416
- }
2417
-
2418
-
2419
- if ((count($backups[$task_name]['task_results']) - $num) >= $backups[$task_name]['task_args']['limit']) {
2420
- //how many to remove ?
2421
- $remove_num = (count($backups[$task_name]['task_results']) - $num - $backups[$task_name]['task_args']['limit']) + 1;
2422
- for ($i = 0; $i < $remove_num; $i++) {
2423
- //Remove from the server
2424
- if (isset($backups[$task_name]['task_results'][$i]['server'])) {
2425
- @unlink($backups[$task_name]['task_results'][$i]['server']['file_path']);
2426
- }
2427
-
2428
- if (isset($backups[$task_name]['task_results'][$i]['ftp'])) {
2429
- $ftp_file = $backups[$task_name]['task_results'][$i]['ftp'];
2430
- $args = $backups[$task_name]['task_args']['account_info']['iwp_ftp'];
2431
- $args['backup_file'] = $ftp_file;
2432
- $this->remove_ftp_backup($args);
2433
- }
2434
-
2435
- if (isset($backups[$task_name]['task_results'][$i]['amazons3'])) {
2436
- $amazons3_file = $backups[$task_name]['task_results'][$i]['amazons3'];
2437
- $args = $backups[$task_name]['task_args']['account_info']['iwp_amazon_s3'];
2438
- $args['backup_file'] = $amazons3_file;
2439
- $this->remove_amazons3_backup($args);
2440
- }
2441
-
2442
- if (isset($backups[$task_name]['task_results'][$i]['dropbox']) && isset($backups[$task_name]['task_args']['account_info']['iwp_dropbox'])) {
2443
- //To do: dropbox remove
2444
- $dropbox_file = $backups[$task_name]['task_results'][$i]['dropbox'];
2445
- $args = $backups[$task_name]['task_args']['account_info']['iwp_dropbox'];
2446
- $args['backup_file'] = $dropbox_file;
2447
- $this->remove_dropbox_backup($args);
2448
- }
2449
- //Remove database backup info
2450
- unset($backups[$task_name]['task_results'][$i]);
2451
-
2452
- } //end foreach
2453
-
2454
- if (is_array($backups[$task_name]['task_results']))
2455
- $backups[$task_name]['task_results'] = array_values($backups[$task_name]['task_results']);
2456
- else
2457
- $backups[$task_name]['task_results']=array();
2458
-
2459
- $this->update_tasks($backups);
2460
-
2461
- return true;
2462
- }
2463
- }
2464
-
2465
- /**
2466
- * Delete specified backup
2467
- * Args: $task_name, $result_id
2468
- */
2469
-
2470
- function delete_backup($args)
2471
- {
2472
- if (empty($args))
2473
- return false;
2474
- extract($args);
2475
-
2476
- $tasks = $this->tasks;
2477
- $task = $tasks[$task_name];
2478
- $backups = $task['task_results'];
2479
- $backup = $backups[$result_id];
2480
-
2481
- if (isset($backup['server'])) {
2482
- @unlink($backup['server']['file_path']);
2483
- }
2484
-
2485
- /*
2486
- //IWP Remove starts here//IWP Remove ends here
2487
- */
2488
- //Remove from ftp
2489
- if (isset($backup['ftp'])) {
2490
- $ftp_file = $backup['ftp'];
2491
- $args = $tasks[$task_name]['task_args']['account_info']['iwp_ftp'];
2492
- $args['backup_file'] = $ftp_file;
2493
- $this->remove_ftp_backup($args);
2494
- }
2495
-
2496
- if (isset($backup['amazons3'])) {
2497
- $amazons3_file = $backup['amazons3'];
2498
- $args = $tasks[$task_name]['task_args']['account_info']['iwp_amazon_s3'];
2499
- $args['backup_file'] = $amazons3_file;
2500
- $this->remove_amazons3_backup($args);
2501
- }
2502
-
2503
- if (isset($backup['dropbox'])) {
2504
- $dropbox_file = $backup['dropbox'];
2505
- $args = $tasks[$task_name]['task_args']['account_info']['iwp_dropbox'];
2506
- $args['backup_file'] = $dropbox_file;
2507
- $this->remove_dropbox_backup($args);
2508
- }
2509
-
2510
-
2511
- unset($backups[$result_id]);
2512
-
2513
- if (count($backups)) {
2514
- $tasks[$task_name]['task_results'] = $backups;
2515
- } else {
2516
- unset($tasks[$task_name]['task_results']);
2517
- }
2518
-
2519
- $this->update_tasks($tasks);
2520
- //update_option('iwp_client_backup_tasks', $tasks);
2521
- return true;
2522
-
2523
- }
2524
-
2525
- function cleanup()
2526
- {
2527
- $tasks = $this->tasks;
2528
- $backup_folder = WP_CONTENT_DIR . '/' . md5('iwp_mmb-client') . '/iwp_backups/';
2529
- $backup_folder_new = IWP_BACKUP_DIR . '/';
2530
- $files = glob($backup_folder . "*");
2531
- $new = glob($backup_folder_new . "*");
2532
-
2533
- //Failed db files first
2534
- $db_folder = IWP_DB_DIR . '/';
2535
- $db_files = glob($db_folder . "*");
2536
- if (is_array($db_files) && !empty($db_files)) {
2537
- foreach ($db_files as $file) {
2538
- @unlink($file);
2539
- }
2540
- @unlink(IWP_BACKUP_DIR.'/iwp_db/index.php');
2541
- @rmdir(IWP_DB_DIR);
2542
- }
2543
-
2544
-
2545
- //clean_old folder?
2546
- if ((basename($files[0]) == 'index.php' && count($files) == 1) || (!empty($files))) { //USE (!empty($files)
2547
- foreach ($files as $file) {
2548
- @unlink($file);
2549
- }
2550
- @rmdir(WP_CONTENT_DIR . '/' . md5('iwp_mmb-client') . '/iwp_backups');
2551
- @rmdir(WP_CONTENT_DIR . '/' . md5('iwp_mmb-client'));
2552
- }
2553
-
2554
- if (!empty($new)) {
2555
- foreach ($new as $b) {
2556
- $files[] = $b;
2557
- }
2558
- }
2559
- $deleted = array();
2560
-
2561
- if (is_array($files) && count($files)) {
2562
- $results = array();
2563
- if (!empty($tasks)) {
2564
- foreach ((array) $tasks as $task) {
2565
- if (isset($task['task_results']) && count($task['task_results'])) {
2566
- foreach ($task['task_results'] as $backup) {
2567
- if (isset($backup['server'])) {
2568
- $results[] = $backup['server']['file_path'];
2569
- }
2570
- }
2571
- }
2572
- }
2573
- }
2574
-
2575
- $num_deleted = 0;
2576
- foreach ($files as $file) {
2577
- if (!in_array($file, $results) && basename($file) != 'index.php') {
2578
- @unlink($file);
2579
- $deleted[] = basename($file);
2580
- $num_deleted++;
2581
- }
2582
- }
2583
- }
2584
-
2585
-
2586
-
2587
- return $deleted;
2588
- }
2589
-
2590
-
2591
- /*
2592
- */
2593
-
2594
- function validate_task($args, $url)
2595
- {
2596
- if (!class_exists('WP_Http')) {
2597
- include_once(ABSPATH . WPINC . '/class-http.php');
2598
- }
2599
- $params = array();
2600
- $params['body'] = $args;
2601
- $result = wp_remote_post($url, $params);
2602
- if (is_array($result) && $result['body'] == 'iwp_delete_task') {
2603
- //$tasks = $this->get_backup_settings();
2604
- $tasks = $this->tasks;
2605
- unset($tasks[$args['task_name']]);
2606
- $this->update_tasks($tasks);
2607
- $this->cleanup();
2608
- exit;
2609
- } elseif(is_array($result) && $result['body'] == 'iwp_pause_task'){
2610
- return 'paused';
2611
- }
2612
-
2613
- return 'ok';
2614
- }
2615
-
2616
- function update_status($task_name, $status, $completed = false)
2617
- {
2618
- /* Statuses:
2619
- 0 - Backup started
2620
- 1 - DB dump
2621
- 2 - DB ZIP
2622
- 3 - Files ZIP
2623
- 4 - Amazon S3
2624
- 5 - Dropbox
2625
- 6 - FTP
2626
- 7 - Email
2627
- 100 - Finished
2628
- */
2629
- //if ($task_name != 'Backup Now') {
2630
- $tasks = $this->tasks;
2631
- $index = count($tasks[$task_name]['task_results']) - 1;
2632
- //!is_array($tasks[$task_name]['task_results'][$index]['status']) &&
2633
- if (!is_array($tasks[$task_name]['task_results'][$index]['backhack_status'])) {
2634
- //$tasks[$task_name]['task_results'][$index]['status'] = array();
2635
- $tasks[$task_name]['task_results'][$index]['backhack_status'] = array();
2636
- }
2637
- $tasks[$task_name]['task_results'][$index]['backhack_status']['adminHistoryID'] = $GLOBALS['IWP_CLIENT_HISTORY_ID'];
2638
- if (!$completed) {
2639
- //$tasks[$task_name]['task_results'][$index]['status'][] = (int) $status * (-1);
2640
- $tasks[$task_name]['task_results'][$index]['backhack_status'][$status]['start'] = microtime(true);
2641
- } else {
2642
- $status_index = count($tasks[$task_name]['task_results'][$index]['status']) - 1;
2643
- //$tasks[$task_name]['task_results'][$index]['status'][$status_index] = abs($tasks[$task_name]['task_results'][$index]['status'][$status_index]);
2644
- $tasks[$task_name]['task_results'][$index]['backhack_status'][$status]['end'] = microtime(true);
2645
- }
2646
-
2647
- $this->update_tasks($tasks);
2648
- //update_option('iwp_client_backup_tasks',$tasks);
2649
- //}
2650
- }
2651
-
2652
- function update_tasks($tasks)
2653
- {
2654
- $this->tasks = $tasks;
2655
- update_option('iwp_client_backup_tasks', $tasks);
2656
- }
2657
-
2658
- function wpdb_reconnect(){
2659
- global $wpdb;
2660
- $old_wpdb = $wpdb;
2661
- //Reconnect to avoid timeout problem after ZIP files
2662
- if(class_exists('wpdb') && function_exists('wp_set_wpdb_vars')){
2663
- @mysql_close($wpdb->dbh);
2664
- $wpdb = new wpdb( DB_USER, DB_PASSWORD, DB_NAME, DB_HOST );
2665
- wp_set_wpdb_vars();
2666
- $wpdb->options = $old_wpdb->options;//fix for multi site full backup
2667
- }
2668
- }
2669
-
2670
- function replace_htaccess($url, $remote_abspath)
2671
- {
2672
- global $wp_filesystem;
2673
- //$file = @file_get_contents(ABSPATH.'.htaccess');
2674
- $file = $wp_filesystem->get_contents($remote_abspath.'.htaccess');
2675
- if ($file && strlen($file)) {
2676
- $args = parse_url($url);
2677
- $string = rtrim($args['path'], "/");
2678
- $regex = "/BEGIN WordPress(.*?)RewriteBase(.*?)\n(.*?)RewriteRule \.(.*?)index\.php(.*?)END WordPress/sm";
2679
- $replace = "BEGIN WordPress$1RewriteBase " . $string . "/ \n$3RewriteRule . " . $string . "/index.php$5END WordPress";
2680
- $file = preg_replace($regex, $replace, $file);
2681
- //@file_put_contents(ABSPATH.'.htaccess', $file);
2682
- $wp_filesystem->put_contents($remote_abspath.'.htaccess', $file);
2683
- }
2684
- }
2685
-
2686
- function check_cron_remove(){
2687
- if(empty($this->tasks) || (count($this->tasks) == 1 && isset($this->tasks['Backup Now'])) ){
2688
- wp_clear_scheduled_hook('iwp_client_backup_tasks');
2689
- exit;
2690
- }
2691
- }
2692
-
2693
-
2694
- public function readd_tasks( $params = array() ){
2695
- global $iwp_mmb_core;
2696
-
2697
- if( empty($params) || !isset($params['backups']) )
2698
- return $params;
2699
-
2700
- $before = array();
2701
- $tasks = $params['backups'];
2702
- if( !empty($tasks) ){
2703
- $iwp_mmb_backup = new IWP_MMB_Backup();
2704
-
2705
- if( function_exists( 'wp_next_scheduled' ) ){
2706
- if ( !wp_next_scheduled('iwp_client_backup_tasks') ) {
2707
- wp_schedule_event( time(), 'tenminutes', 'iwp_client_backup_tasks' );
2708
- }
2709
- }
2710
-
2711
- foreach( $tasks as $task ){
2712
- $before[$task['task_name']] = array();
2713
-
2714
- if(isset($task['secure'])){
2715
- if($decrypted = $iwp_mmb_core->_secure_data($task['secure'])){
2716
- $decrypted = maybe_unserialize($decrypted);
2717
- if(is_array($decrypted)){
2718
- foreach($decrypted as $key => $val){
2719
- if(!is_numeric($key))
2720
- $task[$key] = $val;
2721
- }
2722
- unset($task['secure']);
2723
- } else
2724
- $task['secure'] = $decrypted;
2725
- }
2726
-
2727
- }
2728
- if (isset($task['account_info']) && is_array($task['account_info'])) { //only if sends from panel first time(secure data)
2729
- $task['args']['account_info'] = $task['account_info'];
2730
- }
2731
-
2732
- $before[$task['task_name']]['task_args'] = $task['args'];
2733
- $before[$task['task_name']]['task_args']['next'] = $iwp_mmb_backup->schedule_next($task['args']['type'], $task['args']['schedule']);
2734
- }
2735
- }
2736
- update_option('iwp_client_backup_tasks', $before);
2737
-
2738
- unset($params['backups']);
2739
- return $params;
2740
- }
2741
-
2742
- function is_server_writable(){
2743
- if((!defined('FTP_HOST') || !defined('FTP_USER') || !defined('FTP_PASS')) && (get_filesystem_method(array(), ABSPATH) != 'direct'))