author | ymh <ymh.work@gmail.com> |
Mon, 08 Sep 2025 19:44:41 +0200 | |
changeset 23 | 417f20492bf7 |
parent 19 | 3d72ae0968f4 |
permissions | -rwxr-xr-x |
0 | 1 |
<?php |
2 |
/* |
|
18 | 3 |
Plugin Name: Database Backup for WordPress |
19 | 4 |
Plugin URI: https://github.com/deliciousbrains/wp-db-backup |
0 | 5 |
Description: On-demand backup of your WordPress database. Navigate to <a href="edit.php?page=wp-db-backup">Tools → Backup</a> to get started. |
18 | 6 |
Author: Delicious Brains |
7 |
Author URI: https://deliciousbrains.com |
|
19 | 8 |
Version: 2.5.2 |
18 | 9 |
Domain Path: /languages |
0 | 10 |
|
18 | 11 |
This program is free software; you can redistribute it and/or modify |
12 |
it under the terms of the GNU General Public License as published by |
|
13 |
the Free Software Foundation; either version 2 of the License, or |
|
14 |
(at your option) any later version. |
|
0 | 15 |
|
18 | 16 |
This program is distributed in the hope that it will be useful, |
17 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
18 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
19 |
GNU General Public License for more details. |
|
0 | 20 |
|
18 | 21 |
You should have received a copy of the GNU General Public License |
22 |
along with this program; if not, write to the Free Software |
|
23 |
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA |
|
0 | 24 |
*/ |
25 |
||
18 | 26 |
if ( ! defined( 'ABSPATH' ) ) { |
27 |
die( 'Please do not load this file directly.' ); |
|
0 | 28 |
} |
29 |
||
18 | 30 |
if ( ! defined( 'DBBWP_ROWS_PER_SEGMENT' ) ) { |
31 |
define( 'DBBWP_ROWS_PER_SEGMENT', 100 ); |
|
0 | 32 |
} |
33 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
34 |
/** |
18 | 35 |
* Set DBBWP_MOD_EVASIVE_OVERRIDE to true |
36 |
* and increase DBBWP_MOD_EVASIVE_DELAY |
|
0 | 37 |
* if the backup stops prematurely. |
38 |
*/ |
|
18 | 39 |
// define('DBBWP_MOD_EVASIVE_OVERRIDE', false); |
40 |
if ( ! defined( 'DBBWP_MOD_EVASIVE_DELAY' ) ) { |
|
41 |
define( 'DBBWP_MOD_EVASIVE_DELAY', '500' ); |
|
0 | 42 |
} |
43 |
||
44 |
class wpdbBackup { |
|
45 |
||
46 |
var $backup_complete = false; |
|
18 | 47 |
var $backup_file = ''; |
0 | 48 |
var $backup_filename; |
49 |
var $core_table_names = array(); |
|
18 | 50 |
var $errors = array(); |
0 | 51 |
var $basename; |
52 |
var $page_url; |
|
53 |
var $referer_check_key; |
|
19 | 54 |
var $version = '2.5.2'; |
0 | 55 |
|
56 |
function module_check() { |
|
57 |
$mod_evasive = false; |
|
18 | 58 |
|
59 |
if ( defined( 'DBBWP_MOD_EVASIVE_OVERRIDE' ) && true === DBBWP_MOD_EVASIVE_OVERRIDE ) { |
|
60 |
return true; |
|
61 |
} |
|
62 |
||
63 |
if ( ! defined( 'DBBWP_MOD_EVASIVE_OVERRIDE' ) || false === DBBWP_MOD_EVASIVE_OVERRIDE ) { |
|
64 |
return false; |
|
65 |
} |
|
66 |
||
67 |
if ( function_exists( 'apache_get_modules' ) ) { |
|
68 |
foreach ( (array) apache_get_modules() as $mod ) { |
|
69 |
if ( false !== strpos( $mod, 'mod_evasive' ) || false !== strpos( $mod, 'mod_dosevasive' ) ) { |
|
0 | 70 |
return true; |
18 | 71 |
} |
72 |
} |
|
73 |
} |
|
74 |
||
0 | 75 |
return false; |
76 |
} |
|
77 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
78 |
function __construct() { |
0 | 79 |
global $table_prefix, $wpdb; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
80 |
|
18 | 81 |
add_action( 'wp_ajax_save_backup_time', array( &$this, 'save_backup_time' ) ); |
82 |
add_action( 'init', array( &$this, 'init_textdomain' ) ); |
|
83 |
add_action( 'init', array( &$this, 'set_page_url' ) ); |
|
84 |
add_action( 'admin_notices', array( &$this, 'update_notice' ) ); |
|
85 |
add_action( 'wp_db_backup_cron', array( &$this, 'cron_backup' ) ); |
|
86 |
add_action( 'wp_cron_daily', array( &$this, 'wp_cron_daily' ) ); |
|
87 |
add_filter( 'cron_schedules', array( &$this, 'add_sched_options' ) ); |
|
88 |
add_filter( 'wp_db_b_schedule_choices', array( &$this, 'schedule_choices' ) ); |
|
89 |
||
90 |
$table_prefix = ( isset( $table_prefix ) ) ? $table_prefix : $wpdb->prefix; |
|
91 |
$datum = date( 'Ymd_B' ); |
|
0 | 92 |
$this->backup_filename = DB_NAME . "_$table_prefix$datum.sql"; |
93 |
||
94 |
$possible_names = array( |
|
95 |
'categories', |
|
96 |
'commentmeta', |
|
97 |
'comments', |
|
98 |
'link2cat', |
|
99 |
'linkcategories', |
|
100 |
'links', |
|
101 |
'options', |
|
102 |
'post2cat', |
|
103 |
'postmeta', |
|
104 |
'posts', |
|
105 |
'terms', |
|
106 |
'term_taxonomy', |
|
107 |
'term_relationships', |
|
18 | 108 |
'termmeta', |
0 | 109 |
'users', |
110 |
'usermeta', |
|
111 |
); |
|
112 |
||
18 | 113 |
foreach ( $possible_names as $name ) { |
0 | 114 |
if ( isset( $wpdb->{$name} ) ) { |
115 |
$this->core_table_names[] = $wpdb->{$name}; |
|
116 |
} |
|
117 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
118 |
|
18 | 119 |
$tmp_dir = get_temp_dir(); |
120 |
||
121 |
if ( isset( $_GET['wp_db_temp_dir'] ) ) { |
|
122 |
$requested_dir = sanitize_text_field( $_GET['wp_db_temp_dir'] ); |
|
123 |
if ( is_writeable( $requested_dir ) ) { |
|
124 |
$tmp_dir = $requested_dir; |
|
125 |
} |
|
126 |
} |
|
127 |
||
128 |
$this->backup_dir = trailingslashit( apply_filters( 'wp_db_b_backup_dir', $tmp_dir ) ); |
|
129 |
$this->basename = 'wp-db-backup'; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
130 |
|
0 | 131 |
$this->referer_check_key = $this->basename . '-download_' . DB_NAME; |
18 | 132 |
if ( isset( $_POST['do_backup'] ) ) { |
133 |
$this->wp_secure( 'fatal' ); |
|
134 |
check_admin_referer( $this->referer_check_key ); |
|
135 |
$this->can_user_backup( 'main' ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
136 |
|
0 | 137 |
// save exclude prefs |
18 | 138 |
update_option( |
139 |
'wp_db_backup_excs', |
|
140 |
array( |
|
141 |
'revisions' => $this->get_revisions_to_exclude(), |
|
142 |
'spam' => $this->get_spam_to_exclude(), |
|
143 |
) |
|
144 |
); |
|
145 |
switch ( $_POST['do_backup'] ) { |
|
146 |
case 'backup': |
|
147 |
add_action( 'init', array( &$this, 'perform_backup' ) ); |
|
148 |
break; |
|
149 |
case 'fragments': |
|
19 | 150 |
add_action( 'admin_menu', array( &$this, 'admin_menu' ) ); |
18 | 151 |
break; |
0 | 152 |
} |
18 | 153 |
} elseif ( isset( $_GET['fragment'] ) ) { |
154 |
$this->can_user_backup( 'frame' ); |
|
155 |
add_action( 'init', array( &$this, 'init' ) ); |
|
156 |
} elseif ( isset( $_GET['backup'] ) ) { |
|
0 | 157 |
$this->can_user_backup(); |
18 | 158 |
add_action( 'init', array( &$this, 'init' ) ); |
0 | 159 |
} else { |
18 | 160 |
add_action( 'admin_menu', array( &$this, 'admin_menu' ) ); |
0 | 161 |
} |
162 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
163 |
|
0 | 164 |
function init() { |
165 |
$this->can_user_backup(); |
|
18 | 166 |
if ( isset( $_GET['backup'] ) ) { |
167 |
$via = isset( $_GET['via'] ) ? sanitize_text_field( $_GET['via'] ) : 'http'; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
168 |
|
18 | 169 |
$this->backup_file = sanitize_text_field( $_GET['backup'] ); |
170 |
$this->validate_file( $this->backup_file ); |
|
0 | 171 |
|
18 | 172 |
switch ( $via ) { |
173 |
case 'smtp': |
|
174 |
case 'email': |
|
175 |
$success = $this->deliver_backup( $this->backup_file, 'smtp', sanitize_text_field( $_GET['recipient'] ), 'frame' ); |
|
176 |
$this->error_display( 'frame' ); |
|
177 |
if ( $success ) { |
|
178 |
echo ' |
|
0 | 179 |
<!-- ' . $via . ' --> |
180 |
<script type="text/javascript"><!--\\ |
|
181 |
'; |
|
18 | 182 |
echo ' |
183 |
alert("' . __( 'Backup Complete!', 'wp-db-backup' ) . '"); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
184 |
window.onbeforeunload = null; |
0 | 185 |
</script> |
186 |
'; |
|
18 | 187 |
} |
188 |
break; |
|
189 |
default: |
|
190 |
$success = $this->deliver_backup( $this->backup_file, $via ); |
|
191 |
echo $this->error_display( 'frame', false ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
192 |
|
18 | 193 |
if ( $success ) { |
194 |
echo ' |
|
0 | 195 |
<script type="text/javascript"> |
18 | 196 |
window.parent.setProgress("' . __( 'Backup Complete!', 'wp-db-backup' ) . '"); |
0 | 197 |
</script> |
198 |
'; |
|
18 | 199 |
} |
0 | 200 |
} |
201 |
exit; |
|
202 |
} |
|
18 | 203 |
|
204 |
if ( isset( $_GET['fragment'] ) ) { |
|
205 |
list($table, $segment, $filename) = explode( ':', sanitize_text_field( $_GET['fragment'] ) ); |
|
19 | 206 |
|
207 |
if ( empty( $table ) || in_array( $table, $this->get_tables() ) ) { |
|
208 |
$this->validate_file( $filename ); |
|
209 |
$this->backup_fragment( $table, $segment, $filename ); |
|
210 |
} else { |
|
211 |
$this->error( |
|
212 |
array( |
|
213 |
'loc' => 'frame', |
|
214 |
'kind' => 'fatal', |
|
215 |
'msg' => __( |
|
216 |
'There was an error determining the table to backup. Please check the settings used for the backup and try again.', |
|
217 |
'wp-db-backup' |
|
218 |
), |
|
219 |
) |
|
220 |
); |
|
221 |
} |
|
0 | 222 |
} |
223 |
||
224 |
die(); |
|
225 |
} |
|
226 |
||
227 |
function init_textdomain() { |
|
18 | 228 |
load_plugin_textdomain( |
229 |
'wp-db-backup', |
|
230 |
false, |
|
231 |
dirname( plugin_basename( __FILE__ ) ) . '/languages' |
|
232 |
); |
|
0 | 233 |
} |
234 |
||
235 |
function set_page_url() { |
|
236 |
$query_args = array( 'page' => $this->basename ); |
|
18 | 237 |
|
238 |
if ( function_exists( 'wp_create_nonce' ) ) { |
|
239 |
$query_args = array_merge( $query_args, array( '_wpnonce' => wp_create_nonce( $this->referer_check_key ) ) ); |
|
240 |
} |
|
241 |
||
242 |
$base = ( function_exists( 'site_url' ) ) ? site_url( '', 'admin' ) : get_option( 'siteurl' ); |
|
243 |
$this->page_url = add_query_arg( $query_args, $base . '/wp-admin/edit.php' ); |
|
0 | 244 |
} |
245 |
||
246 |
/* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
247 |
* Add a link to back up your database when doing a core upgrade. |
0 | 248 |
*/ |
18 | 249 |
function update_notice() { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
250 |
global $pagenow; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
251 |
|
18 | 252 |
if ( empty( $pagenow ) || 'update-core.php' !== $pagenow ) { |
253 |
return false; |
|
254 |
} |
|
255 |
?> |
|
256 |
<div class="notice notice-warning"> |
|
257 |
<p> |
|
258 |
<?php |
|
259 |
printf( |
|
260 |
__( 'Click <a href="%s">here</a> to back up your database using the WordPress Database Backup plugin. <strong>Note:</strong> WordPress Database Backup does <em>not</em> back up your files, just your database.', 'wp-db-backup' ), |
|
261 |
esc_url( get_admin_url( null, 'tools.php?page=wp-db-backup' ) ) |
|
262 |
); |
|
263 |
?> |
|
264 |
</p> |
|
265 |
</div> |
|
266 |
<?php |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
267 |
} |
0 | 268 |
|
269 |
function build_backup_script() { |
|
270 |
global $table_prefix, $wpdb; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
271 |
|
19 | 272 |
echo '<fieldset class="options backup-running"><legend>' . __( 'Progress', 'wp-db-backup' ) . '</legend> |
273 |
||
274 |
<div class="panel-heading"> |
|
275 |
<h3>Backup In Progress...</h3> |
|
276 |
</div> |
|
277 |
||
278 |
<div class="panel-content"> |
|
279 |
<div class="progress-bar"> |
|
280 |
<div id="progress-status"></div> |
|
281 |
<div id="meterbox" style="height:11px;width:80%;padding:3px;border:1px solid #659fff;"><div id="meter" style="color:#fff;height:11px;line-height:11px;background-color:#659fff;width:0%;text-align:center;font-size:6pt;"></div></div> |
|
282 |
<div id="progress_message"></div>'?> |
|
283 |
</div> |
|
284 |
||
285 |
<div class="info-notice"> |
|
286 |
<img src="<?php echo plugin_dir_url( __FILE__ ) . 'assets/warning.svg'; ?>"> |
|
287 |
<p> |
|
288 |
Whilst the backup is in progress, please do not close the browser, reload or change the page, or click the stop or back browser buttons. This would result in the backup failing. |
|
289 |
</p> |
|
290 |
</div> |
|
291 |
||
292 |
<?php echo '<div id="errors"></div> |
|
293 |
<iframe id="backuploader" src="about:blank" style="display:none;border:none;height:1em;width:1px;"></iframe> |
|
0 | 294 |
</fieldset> |
295 |
<script type="text/javascript"> |
|
296 |
//<![CDATA[ |
|
297 |
window.onbeforeunload = function() { |
|
18 | 298 |
return "' . __( 'Navigating away from this page will cause your backup to fail.', 'wp-db-backup' ) . '"; |
0 | 299 |
} |
300 |
function setMeter(pct) { |
|
19 | 301 |
var meterStatus = document.getElementById("progress-status"); |
0 | 302 |
var meter = document.getElementById("meter"); |
303 |
meter.style.width = pct + "%"; |
|
19 | 304 |
meterStatus.innerHTML = Math.floor(pct) + "%"; |
0 | 305 |
} |
306 |
function setProgress(str) { |
|
307 |
var progress = document.getElementById("progress_message"); |
|
308 |
progress.innerHTML = str; |
|
309 |
} |
|
310 |
function addError(str) { |
|
311 |
var errors = document.getElementById("errors"); |
|
312 |
errors.innerHTML = errors.innerHTML + str + "<br />"; |
|
313 |
} |
|
314 |
||
315 |
function backup(table, segment) { |
|
316 |
var fram = document.getElementById("backuploader"); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
317 |
fram.src = "' . $this->page_url . '&fragment=" + table + ":" + segment + ":' . $this->backup_filename . ':&wp_db_temp_dir=' . $this->backup_dir . '"; |
0 | 318 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
319 |
|
0 | 320 |
var curStep = 0; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
321 |
|
0 | 322 |
function nextStep() { |
323 |
backupStep(curStep); |
|
324 |
curStep++; |
|
325 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
326 |
|
0 | 327 |
function finishBackup() { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
328 |
var fram = document.getElementById("backuploader"); |
0 | 329 |
setMeter(100); |
330 |
'; |
|
331 |
||
18 | 332 |
$download_uri = add_query_arg( 'backup', $this->backup_filename, $this->page_url ); |
333 |
switch ( $_POST['deliver'] ) { |
|
334 |
case 'http': |
|
335 |
echo ' |
|
336 |
setProgress("' . __( 'Preparing download.', 'wp-db-backup' ) . '"); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
337 |
window.onbeforeunload = null; |
0 | 338 |
fram.src = "' . $download_uri . '"; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
339 |
|
0 | 340 |
setTimeout( function() { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
341 |
var secondFrame = document.createElement("iframe"); |
19 | 342 |
secondFrame.style.display = "none"; |
0 | 343 |
fram.parentNode.insertBefore(secondFrame, fram); |
344 |
secondFrame.src = "' . $download_uri . '&download-retry=1"; |
|
345 |
}, 30000 ); |
|
346 |
'; |
|
18 | 347 |
break; |
348 |
case 'smtp': |
|
349 |
$email = sanitize_text_field( wp_unslash( $_POST['backup_recipient'] ) ); |
|
350 |
if ( get_option( 'wpdb_backup_recip' ) != $email ) { |
|
351 |
update_option( 'wpdb_backup_recip', $email ); |
|
352 |
} |
|
353 |
echo ' |
|
354 |
setProgress("' . sprintf( __( 'Your backup has been emailed to %s', 'wp-db-backup' ), $email ) . '"); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
355 |
window.onbeforeunload = null; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
356 |
fram.src = "' . $download_uri . '&via=email&recipient=' . $email . '"; |
0 | 357 |
'; |
18 | 358 |
break; |
359 |
default: |
|
360 |
echo ' |
|
361 |
setProgress("' . __( 'Backup Complete!', 'wp-db-backup' ) . '"); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
362 |
window.onbeforeunload = null; |
0 | 363 |
'; |
364 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
365 |
|
0 | 366 |
echo ' |
367 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
368 |
|
0 | 369 |
function backupStep(step) { |
370 |
switch(step) { |
|
371 |
case 0: backup("", 0); break; |
|
372 |
'; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
373 |
|
18 | 374 |
$also_backup = $this->get_post_data_array( 'other_tables' ); |
375 |
$core_tables = $this->get_post_data_array( 'core_tables' ); |
|
376 |
$tables = array_merge( $core_tables, $also_backup ); |
|
377 |
$step_count = 1; |
|
378 |
||
379 |
foreach ( $tables as $table ) { |
|
380 |
$rec_count = $wpdb->get_var( "SELECT count(*) FROM {$table}" ); |
|
381 |
$rec_segments = ceil( $rec_count / DBBWP_ROWS_PER_SEGMENT ); |
|
382 |
$table_count = 0; |
|
0 | 383 |
if ( $this->module_check() ) { |
18 | 384 |
$delay = "setTimeout('"; |
385 |
$delay_time = "', " . (int) DBBWP_MOD_EVASIVE_DELAY . ')'; |
|
386 |
} else { |
|
387 |
$delay = $delay_time = ''; } |
|
0 | 388 |
do { |
389 |
echo "case {$step_count}: {$delay}backup(\"{$table}\", {$table_count}){$delay_time}; break;\n"; |
|
390 |
$step_count++; |
|
391 |
$table_count++; |
|
18 | 392 |
} while ( $table_count < $rec_segments ); |
0 | 393 |
echo "case {$step_count}: {$delay}backup(\"{$table}\", -1){$delay_time}; break;\n"; |
394 |
$step_count++; |
|
395 |
} |
|
19 | 396 |
|
0 | 397 |
echo "case {$step_count}: finishBackup(); break;"; |
398 |
echo ' |
|
399 |
} |
|
400 |
if(step != 0) setMeter(100 * step / ' . $step_count . '); |
|
401 |
} |
|
402 |
||
403 |
nextStep(); |
|
404 |
// ]]> |
|
405 |
</script> |
|
406 |
'; |
|
407 |
} |
|
408 |
||
18 | 409 |
function backup_fragment( $table, $segment, $filename ) { |
0 | 410 |
global $table_prefix, $wpdb; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
411 |
|
0 | 412 |
echo "$table:$segment:$filename"; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
413 |
|
18 | 414 |
if ( $table == '' ) { |
415 |
$msg = __( 'Creating backup file...', 'wp-db-backup' ); |
|
0 | 416 |
} else { |
18 | 417 |
if ( $segment == -1 ) { |
418 |
$msg = sprintf( __( 'Finished backing up table \\"%s\\".', 'wp-db-backup' ), $table ); |
|
0 | 419 |
} else { |
18 | 420 |
$msg = sprintf( __( 'Backing up table \\"%s\\"...', 'wp-db-backup' ), $table ); |
0 | 421 |
} |
422 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
423 |
|
18 | 424 |
if ( is_writable( $this->backup_dir ) ) { |
425 |
$this->fp = $this->open( $this->backup_dir . $filename, 'a' ); |
|
426 |
if ( ! $this->fp ) { |
|
427 |
$this->error( __( 'Could not open the backup file for writing!', 'wp-db-backup' ) ); |
|
428 |
$this->error( |
|
429 |
array( |
|
430 |
'loc' => 'frame', |
|
431 |
'kind' => 'fatal', |
|
432 |
'msg' => __( |
|
433 |
'The backup file could not be saved. Please check the permissions for writing to your backup directory and try again.', |
|
434 |
'wp-db-backup' |
|
435 |
), |
|
436 |
) |
|
437 |
); |
|
438 |
} else { |
|
439 |
if ( $table == '' ) { |
|
0 | 440 |
//Begin new backup of MySql |
18 | 441 |
$this->stow( '# ' . __( 'WordPress MySQL database backup', 'wp-db-backup' ) . "\n" ); |
442 |
$this->stow( "#\n" ); |
|
443 |
$this->stow( '# ' . sprintf( __( 'Generated: %s', 'wp-db-backup' ), date( 'l j. F Y H:i T' ) ) . "\n" ); |
|
444 |
$this->stow( '# ' . sprintf( __( 'Hostname: %s', 'wp-db-backup' ), DB_HOST ) . "\n" ); |
|
445 |
$this->stow( '# ' . sprintf( __( 'Database: %s', 'wp-db-backup' ), $this->backquote( DB_NAME ) ) . "\n" ); |
|
446 |
$this->stow( "# --------------------------------------------------------\n" ); |
|
0 | 447 |
} else { |
18 | 448 |
if ( $segment == 0 ) { |
0 | 449 |
// Increase script execution time-limit to 15 min for every table. |
18 | 450 |
if ( ! ini_get( 'safe_mode' ) ) { |
451 |
@set_time_limit( 15 * 60 ); |
|
452 |
} |
|
0 | 453 |
// Create the SQL statements |
18 | 454 |
$this->stow( "# --------------------------------------------------------\n" ); |
455 |
$this->stow( '# ' . sprintf( __( 'Table: %s', 'wp-db-backup' ), $this->backquote( $table ) ) . "\n" ); |
|
456 |
$this->stow( "# --------------------------------------------------------\n" ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
457 |
} |
18 | 458 |
$this->backup_table( $table, $segment ); |
0 | 459 |
} |
460 |
} |
|
461 |
} else { |
|
18 | 462 |
$this->error( |
463 |
array( |
|
464 |
'kind' => 'fatal', |
|
465 |
'loc' => 'frame', |
|
466 |
'msg' => __( |
|
467 |
'The backup directory is not writeable! Please check the permissions for writing to your backup directory and try again.', |
|
468 |
'wp-db-backup' |
|
469 |
), |
|
470 |
) |
|
471 |
); |
|
0 | 472 |
} |
473 |
||
18 | 474 |
if ( $this->fp ) { |
475 |
$this->close( $this->fp ); |
|
476 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
477 |
|
18 | 478 |
$this->error_display( 'frame' ); |
0 | 479 |
|
480 |
echo '<script type="text/javascript"><!--// |
|
481 |
var msg = "' . $msg . '"; |
|
482 |
window.parent.setProgress(msg); |
|
483 |
window.parent.nextStep(); |
|
484 |
//--></script> |
|
485 |
'; |
|
486 |
die(); |
|
487 |
} |
|
488 |
||
489 |
function perform_backup() { |
|
490 |
// are we backing up any other tables? |
|
491 |
$also_backup = array(); |
|
18 | 492 |
if ( isset( $_POST['other_tables'] ) ) { |
493 |
$also_backup = sanitize_text_field( $_POST['other_tables'] ); |
|
494 |
} |
|
495 |
||
496 |
$core_tables = sanitize_text_field( $_POST['core_tables'] ); |
|
497 |
$this->backup_file = $this->db_backup( $core_tables, $also_backup ); |
|
498 |
||
499 |
if ( false !== $this->backup_file ) { |
|
500 |
if ( 'smtp' == $_POST['deliver'] ) { |
|
501 |
$email = sanitize_text_field( wp_unslash( $_POST['backup_recipient'] ) ); |
|
502 |
$this->deliver_backup( $this->backup_file, sanitize_text_field( $_POST['deliver'] ), $email, 'main' ); |
|
503 |
if ( get_option( 'wpdb_backup_recip' ) != $email ) { |
|
504 |
update_option( 'wpdb_backup_recip', $email ); |
|
0 | 505 |
} |
18 | 506 |
wp_redirect( $this->page_url ); |
507 |
} elseif ( 'http' == $_POST['deliver'] ) { |
|
508 |
$download_uri = add_query_arg( 'backup', $this->backup_file, $this->page_url ); |
|
509 |
wp_redirect( $download_uri ); |
|
0 | 510 |
exit; |
511 |
} |
|
18 | 512 |
|
0 | 513 |
// we do this to say we're done. |
514 |
$this->backup_complete = true; |
|
515 |
} |
|
516 |
} |
|
517 |
||
518 |
function admin_header() { |
|
519 |
?> |
|
520 |
<script type="text/javascript"> |
|
521 |
//<![CDATA[ |
|
522 |
if ( 'undefined' != typeof addLoadEvent ) { |
|
523 |
addLoadEvent(function() { |
|
524 |
var t = {'extra-tables-list':{name: 'other_tables[]'}, 'include-tables-list':{name: 'wp_cron_backup_tables[]'}}; |
|
525 |
||
526 |
for ( var k in t ) { |
|
527 |
t[k].s = null; |
|
528 |
var d = document.getElementById(k); |
|
529 |
if ( ! d ) |
|
530 |
continue; |
|
531 |
var ul = d.getElementsByTagName('ul').item(0); |
|
532 |
if ( ul ) { |
|
533 |
var lis = ul.getElementsByTagName('li'); |
|
534 |
if ( 2 < lis.length ) { |
|
19 | 535 |
var text = document.querySelector('.instructions-container p'); |
536 |
text.style.display = 'block'; |
|
0 | 537 |
} |
538 |
} |
|
539 |
t[k].p = d.getElementsByTagName("input"); |
|
540 |
for(var i=0; i < t[k].p.length; i++) { |
|
541 |
if(t[k].name == t[k].p[i].getAttribute('name')) { |
|
542 |
t[k].p[i].id = k + '-table-' + i; |
|
19 | 543 |
var label = document.getElementById(t[k].p[i].id).parentNode; |
544 |
t[k].p[i].onkeyup = label.onclick = function(e) { |
|
0 | 545 |
e = e ? e : event; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
546 |
if ( 16 == e.keyCode ) |
0 | 547 |
return; |
19 | 548 |
var match = /([\w-]*)-table-(\d*)/.exec(this.querySelector('input').id); |
0 | 549 |
var listname = match[1]; |
550 |
var that = match[2]; |
|
551 |
if ( null === t[listname].s ) |
|
552 |
t[listname].s = that; |
|
553 |
else if ( e.shiftKey ) { |
|
19 | 554 |
console.log(this); |
0 | 555 |
var start = Math.min(that, t[listname].s) + 1; |
556 |
var end = Math.max(that, t[listname].s); |
|
19 | 557 |
this.querySelector('input').checked = true; |
0 | 558 |
for( var j=start; j < end; j++) |
559 |
t[listname].p[j].checked = t[listname].p[j].checked ? false : true; |
|
560 |
t[listname].s = null; |
|
561 |
} |
|
562 |
} |
|
563 |
} |
|
564 |
} |
|
565 |
} |
|
566 |
||
18 | 567 |
<?php if ( function_exists( 'wp_schedule_event' ) ) : // needs to be at least WP 2.1 for ajax ?> |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
568 |
if ( 'undefined' == typeof XMLHttpRequest ) |
0 | 569 |
var xml = new ActiveXObject( navigator.userAgent.indexOf('MSIE 5') >= 0 ? 'Microsoft.XMLHTTP' : 'Msxml2.XMLHTTP' ); |
570 |
else |
|
571 |
var xml = new XMLHttpRequest(); |
|
572 |
||
573 |
var initTimeChange = function() { |
|
574 |
var timeWrap = document.getElementById('backup-time-wrap'); |
|
575 |
var backupTime = document.getElementById('next-backup-time'); |
|
19 | 576 |
if ( !! timeWrap && !! backupTime && ( 1 == |
18 | 577 |
<?php |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
578 |
echo (int) ( 'en' == strtolower( substr( get_locale(), 0, 2 ) ) ); |
18 | 579 |
?> |
580 |
) ) { |
|
0 | 581 |
var span = document.createElement('span'); |
582 |
span.className = 'submit'; |
|
583 |
span.id = 'change-wrap'; |
|
18 | 584 |
span.innerHTML = '<input type="submit" id="change-backup-time" name="change-backup-time" value="<?php _e( 'Change', 'wp-db-backup' ); ?>" />'; |
0 | 585 |
timeWrap.appendChild(span); |
586 |
backupTime.ondblclick = function(e) { span.parentNode.removeChild(span); clickTime(e, backupTime); }; |
|
587 |
span.onclick = function(e) { span.parentNode.removeChild(span); clickTime(e, backupTime); }; |
|
588 |
} |
|
589 |
} |
|
590 |
||
591 |
var clickTime = function(e, backupTime) { |
|
592 |
var tText = backupTime.innerHTML; |
|
18 | 593 |
backupTime.innerHTML = '<input type="text" value="' + tText + '" name="backup-time-text" id="backup-time-text" /> <span class="submit"><input type="submit" name="save-backup-time" id="save-backup-time" value="<?php _e( 'Save', 'wp-db-backup' ); ?>" /></span>'; |
0 | 594 |
backupTime.ondblclick = null; |
595 |
var mainText = document.getElementById('backup-time-text'); |
|
596 |
mainText.focus(); |
|
597 |
var saveTButton = document.getElementById('save-backup-time'); |
|
598 |
if ( !! saveTButton ) |
|
599 |
saveTButton.onclick = function(e) { saveTime(backupTime, mainText); return false; }; |
|
600 |
if ( !! mainText ) |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
601 |
mainText.onkeydown = function(e) { |
0 | 602 |
e = e || window.event; |
603 |
if ( 13 == e.keyCode ) { |
|
604 |
saveTime(backupTime, mainText); |
|
605 |
return false; |
|
606 |
} |
|
607 |
} |
|
608 |
} |
|
609 |
||
610 |
var saveTime = function(backupTime, mainText) { |
|
611 |
var tVal = mainText.value; |
|
612 |
||
613 |
xml.open('POST', 'admin-ajax.php', true); |
|
614 |
xml.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); |
|
615 |
if ( xml.overrideMimeType ) |
|
616 |
xml.setRequestHeader('Connection', 'close'); |
|
18 | 617 |
xml.send('action=save_backup_time&_wpnonce=<?php echo wp_create_nonce( $this->referer_check_key ); ?>&backup-time='+tVal); |
0 | 618 |
xml.onreadystatechange = function() { |
619 |
if ( 4 == xml.readyState && '0' != xml.responseText ) { |
|
620 |
backupTime.innerHTML = xml.responseText; |
|
621 |
initTimeChange(); |
|
622 |
} |
|
623 |
} |
|
624 |
} |
|
625 |
||
626 |
initTimeChange(); |
|
627 |
<?php endif; // wp_schedule_event exists ?> |
|
628 |
}); |
|
629 |
} |
|
630 |
//]]> |
|
631 |
</script> |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
632 |
<?php |
0 | 633 |
} |
634 |
||
635 |
function admin_load() { |
|
18 | 636 |
add_action( 'admin_head', array( &$this, 'admin_header' ) ); |
19 | 637 |
wp_enqueue_style( 'wp-db-backup-styles', plugin_dir_url( __FILE__ ) . 'assets/css/style.css', array( 'common', 'forms' ) ); |
638 |
wp_enqueue_script( 'wp-db-backup-script', plugin_dir_url( __FILE__ ) . 'assets/js/script.js', array( 'jquery' ), $this->version, true ); |
|
0 | 639 |
} |
640 |
||
641 |
function admin_menu() { |
|
18 | 642 |
$_page_hook = add_management_page( __( 'Backup', 'wp-db-backup' ), __( 'Backup', 'wp-db-backup' ), 'import', $this->basename, array( &$this, 'backup_menu' ) ); |
643 |
add_action( 'load-' . $_page_hook, array( &$this, 'admin_load' ) ); |
|
644 |
if ( function_exists( 'get_current_screen' ) ) { |
|
645 |
$screen = convert_to_screen( $_page_hook ); |
|
646 |
if ( method_exists( $screen, 'add_help_tab' ) ) { |
|
647 |
$screen->add_help_tab( |
|
648 |
array( |
|
649 |
'title' => __( 'Backup', 'wp-db-backup' ), |
|
650 |
'id' => $_page_hook, |
|
651 |
'content' => $this->help_menu(), |
|
652 |
) |
|
653 |
); |
|
0 | 654 |
} |
18 | 655 |
} elseif ( function_exists( 'add_contextual_help' ) ) { |
0 | 656 |
$text = $this->help_menu(); |
18 | 657 |
add_contextual_help( $_page_hook, $text ); |
0 | 658 |
} |
659 |
} |
|
660 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
661 |
/** |
0 | 662 |
* Add WP-DB-Backup-specific help options to the 2.7 =< WP contextual help menu |
663 |
* @return string The text of the help menu. |
|
664 |
*/ |
|
665 |
function help_menu() { |
|
18 | 666 |
$text = "\n<a href=\"http://wordpress.org/extend/plugins/wp-db-backup/faq/\" target=\"_blank\">" . __( 'FAQ', 'wp-db-backup' ) . '</a>'; |
0 | 667 |
return $text; |
668 |
} |
|
669 |
||
670 |
function save_backup_time() { |
|
671 |
if ( $this->can_user_backup() ) { |
|
672 |
// try to get a time from the input string |
|
18 | 673 |
$time = strtotime( strval( $_POST['backup-time'] ) ); |
0 | 674 |
if ( ! empty( $time ) && time() < $time ) { |
675 |
wp_clear_scheduled_hook( 'wp_db_backup_cron' ); // unschedule previous |
|
676 |
$scheds = (array) wp_get_schedules(); |
|
18 | 677 |
$name = get_option( 'wp_cron_backup_schedule' ); |
0 | 678 |
if ( 0 != $time ) { |
18 | 679 |
wp_schedule_event( $time, $name, 'wp_db_backup_cron' ); |
680 |
echo gmdate( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), $time + ( get_option( 'gmt_offset' ) * 3600 ) ); |
|
0 | 681 |
exit; |
682 |
} |
|
683 |
} |
|
684 |
} else { |
|
18 | 685 |
die( 0 ); |
0 | 686 |
} |
687 |
} |
|
688 |
||
689 |
/** |
|
690 |
* Better addslashes for SQL queries. |
|
691 |
* Taken from phpMyAdmin. |
|
692 |
*/ |
|
18 | 693 |
function sql_addslashes( $a_string = '', $is_like = false ) { |
694 |
if ( $is_like ) { |
|
695 |
$a_string = str_replace( '\\', '\\\\\\\\', $a_string ); |
|
696 |
} else { |
|
697 |
$a_string = str_replace( '\\', '\\\\', $a_string ); |
|
698 |
} |
|
699 |
||
700 |
return str_replace( '\'', '\\\'', $a_string ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
701 |
} |
0 | 702 |
|
703 |
/** |
|
704 |
* Add backquotes to tables and db-names in |
|
705 |
* SQL queries. Taken from phpMyAdmin. |
|
706 |
*/ |
|
18 | 707 |
function backquote( $a_name ) { |
708 |
if ( ! empty( $a_name ) && $a_name != '*' ) { |
|
709 |
if ( is_array( $a_name ) ) { |
|
0 | 710 |
$result = array(); |
18 | 711 |
reset( $a_name ); |
712 |
while ( list($key, $val) = each( $a_name ) ) { |
|
713 |
$result[ $key ] = '`' . $val . '`'; |
|
714 |
} |
|
0 | 715 |
return $result; |
716 |
} else { |
|
717 |
return '`' . $a_name . '`'; |
|
718 |
} |
|
719 |
} else { |
|
720 |
return $a_name; |
|
721 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
722 |
} |
0 | 723 |
|
18 | 724 |
function open( $filename = '', $mode = 'w' ) { |
725 |
if ( '' == $filename ) { |
|
726 |
return false; |
|
727 |
} |
|
728 |
$fp = @fopen( $filename, $mode ); |
|
0 | 729 |
return $fp; |
730 |
} |
|
731 |
||
18 | 732 |
function close( $fp ) { |
733 |
fclose( $fp ); |
|
0 | 734 |
} |
735 |
||
736 |
/** |
|
737 |
* Write to the backup file |
|
738 |
* @param string $query_line the line to write |
|
739 |
* @return null |
|
740 |
*/ |
|
18 | 741 |
function stow( $query_line ) { |
742 |
if ( false === @fwrite( $this->fp, $query_line ) ) { |
|
743 |
$this->error( __( 'There was an error writing a line to the backup script:', 'wp-db-backup' ) . ' ' . $query_line . ' ' . $php_errormsg ); |
|
744 |
} |
|
0 | 745 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
746 |
|
0 | 747 |
/** |
748 |
* Logs any error messages |
|
749 |
* @param array $args |
|
750 |
* @return bool |
|
751 |
*/ |
|
18 | 752 |
function error( $args = array() ) { |
753 |
if ( is_string( $args ) ) { |
|
754 |
$args = array( 'msg' => $args ); |
|
755 |
} |
|
756 |
||
757 |
$args = array_merge( |
|
758 |
array( |
|
759 |
'loc' => 'main', |
|
760 |
'kind' => 'warn', |
|
761 |
'msg' => '', |
|
762 |
), |
|
763 |
$args |
|
764 |
); |
|
765 |
||
766 |
$this->errors[ $args['kind'] ][] = $args['msg']; |
|
767 |
||
768 |
if ( 'fatal' == $args['kind'] || 'frame' == $args['loc'] ) { |
|
769 |
$this->error_display( $args['loc'] ); |
|
770 |
} |
|
771 |
||
0 | 772 |
return true; |
773 |
} |
|
774 |
||
775 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
776 |
* Displays error messages |
0 | 777 |
* @param array $errs |
778 |
* @param string $loc |
|
779 |
* @return string |
|
780 |
*/ |
|
18 | 781 |
function error_display( $loc = 'main', $echo = true ) { |
0 | 782 |
$errs = $this->errors; |
783 |
unset( $this->errors ); |
|
18 | 784 |
|
785 |
if ( ! count( $errs ) ) { |
|
786 |
return; |
|
787 |
} |
|
788 |
||
789 |
$msg = ''; |
|
0 | 790 |
$errs['fatal'] = isset( $errs['fatal'] ) ? (array) $errs['fatal'] : array(); |
18 | 791 |
$errs['warn'] = isset( $errs['warn'] ) ? (array) $errs['warn'] : array(); |
792 |
$err_list = array_slice( array_merge( $errs['fatal'], $errs['warn'] ), 0, 10 ); |
|
793 |
||
794 |
if ( 10 == count( $err_list ) ) { |
|
795 |
$err_list[9] = __( 'Subsequent errors have been omitted from this log.', 'wp-db-backup' ); |
|
796 |
} |
|
797 |
||
0 | 798 |
$wrap = ( 'frame' == $loc ) ? "<script type=\"text/javascript\">\n var msgList = ''; \n %1\$s \n if ( msgList ) alert(msgList); \n </script>" : '%1$s'; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
799 |
$line = ( 'frame' == $loc ) ? |
0 | 800 |
"try{ window.parent.addError('%1\$s'); } catch(e) { msgList += ' %1\$s';}\n" : |
801 |
"%1\$s<br />\n"; |
|
18 | 802 |
|
803 |
foreach ( (array) $err_list as $err ) { |
|
804 |
$msg .= sprintf( $line, str_replace( array( "\n", "\r" ), '', addslashes( $err ) ) ); |
|
0 | 805 |
} |
18 | 806 |
|
807 |
$msg = sprintf( $wrap, $msg ); |
|
808 |
||
809 |
if ( count( $errs['fatal'] ) ) { |
|
810 |
if ( function_exists( 'wp_die' ) && 'frame' != $loc ) { |
|
811 |
wp_die( stripslashes( $msg ) ); |
|
812 |
} else { |
|
813 |
die( $msg ); |
|
814 |
} |
|
815 |
} else { |
|
816 |
if ( $echo ) { |
|
817 |
echo $msg; |
|
818 |
} else { |
|
819 |
return $msg; |
|
820 |
} |
|
0 | 821 |
} |
822 |
} |
|
823 |
||
824 |
/** |
|
825 |
* Taken partially from phpMyAdmin and partially from |
|
826 |
* Alain Wolf, Zurich - Switzerland |
|
827 |
* Website: http://restkultur.ch/personal/wolf/scripts/db_backup/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
828 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
829 |
* Modified by Scott Merrill (http://www.skippy.net/) |
0 | 830 |
* to use the WordPress $wpdb object |
831 |
* @param string $table |
|
832 |
* @param string $segment |
|
833 |
* @return void |
|
834 |
*/ |
|
18 | 835 |
function backup_table( $table, $segment = 'none' ) { |
0 | 836 |
global $wpdb; |
837 |
||
18 | 838 |
$table_structure = $wpdb->get_results( "DESCRIBE $table" ); |
839 |
if ( ! $table_structure ) { |
|
840 |
$this->error( __( 'Error getting table details', 'wp-db-backup' ) . ": $table" ); |
|
0 | 841 |
return false; |
842 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
843 |
|
18 | 844 |
if ( ( $segment == 'none' ) || ( $segment == 0 ) ) { |
0 | 845 |
// Add SQL statement to drop existing table |
18 | 846 |
$this->stow( "\n\n" ); |
847 |
$this->stow( "#\n" ); |
|
848 |
$this->stow( '# ' . sprintf( __( 'Delete any existing table %s', 'wp-db-backup' ), $this->backquote( $table ) ) . "\n" ); |
|
849 |
$this->stow( "#\n" ); |
|
850 |
$this->stow( "\n" ); |
|
851 |
$this->stow( 'DROP TABLE IF EXISTS ' . $this->backquote( $table ) . ";\n" ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
852 |
|
0 | 853 |
// Table structure |
854 |
// Comment in SQL-file |
|
18 | 855 |
$this->stow( "\n\n" ); |
856 |
$this->stow( "#\n" ); |
|
857 |
$this->stow( '# ' . sprintf( __( 'Table structure of table %s', 'wp-db-backup' ), $this->backquote( $table ) ) . "\n" ); |
|
858 |
$this->stow( "#\n" ); |
|
859 |
$this->stow( "\n" ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
860 |
|
18 | 861 |
$create_table = $wpdb->get_results( "SHOW CREATE TABLE $table", ARRAY_N ); |
862 |
if ( false === $create_table ) { |
|
863 |
$err_msg = sprintf( __( 'Error with SHOW CREATE TABLE for %s.', 'wp-db-backup' ), $table ); |
|
864 |
$this->error( $err_msg ); |
|
865 |
$this->stow( "#\n# $err_msg\n#\n" ); |
|
0 | 866 |
} |
18 | 867 |
$this->stow( $create_table[0][1] . ' ;' ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
868 |
|
18 | 869 |
if ( false === $table_structure ) { |
870 |
$err_msg = sprintf( __( 'Error getting table structure of %s', 'wp-db-backup' ), $table ); |
|
871 |
$this->error( $err_msg ); |
|
872 |
$this->stow( "#\n# $err_msg\n#\n" ); |
|
0 | 873 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
874 |
|
0 | 875 |
// Comment in SQL-file |
18 | 876 |
$this->stow( "\n\n" ); |
877 |
$this->stow( "#\n" ); |
|
878 |
$this->stow( '# ' . sprintf( __( 'Data contents of table %s', 'wp-db-backup' ), $this->backquote( $table ) ) . "\n" ); |
|
879 |
$this->stow( "#\n" ); |
|
0 | 880 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
881 |
|
18 | 882 |
if ( ( $segment == 'none' ) || ( $segment >= 0 ) ) { |
0 | 883 |
$defs = array(); |
884 |
$ints = array(); |
|
18 | 885 |
foreach ( $table_structure as $struct ) { |
886 |
if ( ( 0 === strpos( $struct->Type, 'tinyint' ) ) || |
|
887 |
( 0 === strpos( strtolower( $struct->Type ), 'smallint' ) ) || |
|
888 |
( 0 === strpos( strtolower( $struct->Type ), 'mediumint' ) ) || |
|
889 |
( 0 === strpos( strtolower( $struct->Type ), 'int' ) ) || |
|
890 |
( 0 === strpos( strtolower( $struct->Type ), 'bigint' ) ) ) { |
|
891 |
$defs[ strtolower( $struct->Field ) ] = ( null === $struct->Default ) ? 'NULL' : $struct->Default; |
|
892 |
$ints[ strtolower( $struct->Field ) ] = '1'; |
|
0 | 893 |
} |
894 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
895 |
|
0 | 896 |
// Batch by $row_inc |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
897 |
|
18 | 898 |
if ( $segment == 'none' ) { |
0 | 899 |
$row_start = 0; |
18 | 900 |
$row_inc = DBBWP_ROWS_PER_SEGMENT; |
0 | 901 |
} else { |
18 | 902 |
$row_start = $segment * DBBWP_ROWS_PER_SEGMENT; |
903 |
$row_inc = DBBWP_ROWS_PER_SEGMENT; |
|
0 | 904 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
905 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
906 |
do { |
0 | 907 |
// don't include extra stuff, if so requested |
18 | 908 |
$excs = (array) get_option( 'wp_db_backup_excs' ); |
0 | 909 |
$where = ''; |
18 | 910 |
|
911 |
if ( is_array( $excs['spam'] ) && in_array( $table, $excs['spam'] ) ) { |
|
912 |
$where = " WHERE comment_approved != 'spam'"; |
|
913 |
} elseif ( is_array( $excs['revisions'] ) && in_array( $table, $excs['revisions'] ) ) { |
|
914 |
$where = " WHERE post_type != 'revision'"; |
|
0 | 915 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
916 |
|
18 | 917 |
if ( ! ini_get( 'safe_mode' ) ) { |
918 |
@set_time_limit( 15 * 60 ); |
|
919 |
} |
|
920 |
$table_data = $wpdb->get_results( "SELECT * FROM $table $where LIMIT {$row_start}, {$row_inc}", ARRAY_A ); |
|
0 | 921 |
|
18 | 922 |
$entries = 'INSERT INTO ' . $this->backquote( $table ) . ' VALUES ('; |
0 | 923 |
// \x08\\x09, not required |
18 | 924 |
$search = array( "\x00", "\x0a", "\x0d", "\x1a" ); |
925 |
$replace = array( '\0', '\n', '\r', '\Z' ); |
|
926 |
||
927 |
if ( $table_data ) { |
|
928 |
foreach ( $table_data as $row ) { |
|
0 | 929 |
$values = array(); |
18 | 930 |
foreach ( $row as $key => $value ) { |
931 |
if ( ! empty( $ints[ strtolower( $key ) ] ) ) { |
|
0 | 932 |
// make sure there are no blank spots in the insert syntax, |
933 |
// yet try to avoid quotation marks around integers |
|
18 | 934 |
$value = ( null === $value || '' === $value ) ? $defs[ strtolower( $key ) ] : $value; |
0 | 935 |
$values[] = ( '' === $value ) ? "''" : $value; |
936 |
} else { |
|
18 | 937 |
$values[] = "'" . str_replace( $search, $replace, $this->sql_addslashes( $value ) ) . "'"; |
0 | 938 |
} |
939 |
} |
|
18 | 940 |
$this->stow( " \n" . $entries . implode( ', ', $values ) . ');' ); |
0 | 941 |
} |
942 |
$row_start += $row_inc; |
|
943 |
} |
|
18 | 944 |
} while ( ( count( $table_data ) > 0 ) and ( $segment == 'none' ) ); |
0 | 945 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
946 |
|
18 | 947 |
if ( ( $segment == 'none' ) || ( $segment < 0 ) ) { |
0 | 948 |
// Create footer/closing comment in SQL-file |
18 | 949 |
$this->stow( "\n" ); |
950 |
$this->stow( "#\n" ); |
|
951 |
$this->stow( '# ' . sprintf( __( 'End of data contents of table %s', 'wp-db-backup' ), $this->backquote( $table ) ) . "\n" ); |
|
952 |
$this->stow( "# --------------------------------------------------------\n" ); |
|
953 |
$this->stow( "\n" ); |
|
0 | 954 |
} |
955 |
} // end backup_table() |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
956 |
|
18 | 957 |
function db_backup( $core_tables, $other_tables ) { |
0 | 958 |
global $table_prefix, $wpdb; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
959 |
|
18 | 960 |
if ( is_writable( $this->backup_dir ) ) { |
961 |
$this->fp = $this->open( $this->backup_dir . $this->backup_filename ); |
|
962 |
if ( ! $this->fp ) { |
|
963 |
$this->error( __( 'Could not open the backup file for writing!', 'wp-db-backup' ) ); |
|
0 | 964 |
return false; |
965 |
} |
|
966 |
} else { |
|
18 | 967 |
$this->error( __( 'The backup directory is not writeable!', 'wp-db-backup' ) ); |
0 | 968 |
return false; |
969 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
970 |
|
0 | 971 |
//Begin new backup of MySql |
18 | 972 |
$this->stow( '# ' . __( 'WordPress MySQL database backup', 'wp-db-backup' ) . "\n" ); |
973 |
$this->stow( "#\n" ); |
|
974 |
$this->stow( '# ' . sprintf( __( 'Generated: %s', 'wp-db-backup' ), date( 'l j. F Y H:i T' ) ) . "\n" ); |
|
975 |
$this->stow( '# ' . sprintf( __( 'Hostname: %s', 'wp-db-backup' ), DB_HOST ) . "\n" ); |
|
976 |
$this->stow( '# ' . sprintf( __( 'Database: %s', 'wp-db-backup' ), $this->backquote( DB_NAME ) ) . "\n" ); |
|
977 |
$this->stow( "# --------------------------------------------------------\n" ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
978 |
|
18 | 979 |
if ( ( is_array( $other_tables ) ) && ( count( $other_tables ) > 0 ) ) { |
980 |
$tables = array_merge( $core_tables, $other_tables ); |
|
981 |
} else { |
|
0 | 982 |
$tables = $core_tables; |
983 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
984 |
|
18 | 985 |
foreach ( $tables as $table ) { |
986 |
// Increase script execution time-limit to 15 min for every table. |
|
987 |
if ( ! ini_get( 'safe_mode' ) ) { |
|
988 |
@set_time_limit( 15 * 60 ); |
|
989 |
} |
|
990 |
// Create the SQL statements |
|
991 |
$this->stow( "# --------------------------------------------------------\n" ); |
|
992 |
$this->stow( '# ' . sprintf( __( 'Table: %s', 'wp-db-backup' ), $this->backquote( $table ) ) . "\n" ); |
|
993 |
$this->stow( "# --------------------------------------------------------\n" ); |
|
994 |
$this->backup_table( $table ); |
|
995 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
996 |
|
18 | 997 |
$this->close( $this->fp ); |
998 |
||
999 |
if ( count( $this->errors ) ) { |
|
0 | 1000 |
return false; |
1001 |
} else { |
|
1002 |
return $this->backup_filename; |
|
1003 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1004 |
|
0 | 1005 |
} //wp_db_backup |
1006 |
||
1007 |
/** |
|
1008 |
* Sends the backed-up file via email |
|
18 | 1009 |
* |
0 | 1010 |
* @param string $to |
1011 |
* @param string $subject |
|
1012 |
* @param string $message |
|
18 | 1013 |
* @param string $diskfile |
1014 |
* |
|
0 | 1015 |
* @return bool |
1016 |
*/ |
|
18 | 1017 |
function send_mail( $to, $subject, $message, $diskfile ) { |
1018 |
return wp_mail( $to, $subject, $message, array(), array( $diskfile ) ); |
|
1019 |
} |
|
0 | 1020 |
|
18 | 1021 |
function deliver_backup( $filename = '', $delivery = 'http', $recipient = '', $location = 'main' ) { |
1022 |
if ( '' == $filename ) { |
|
1023 |
return false; } |
|
0 | 1024 |
|
18 | 1025 |
$diskfile = $this->backup_dir . $filename; |
1026 |
$gz_diskfile = "{$diskfile}.gz"; |
|
1027 |
$retry = isset( $_GET['download-retry'] ); |
|
1028 |
$success = false; |
|
0 | 1029 |
|
18 | 1030 |
// Try to gzip the file if we can. |
1031 |
if ( file_exists( $diskfile ) && ! file_exists( $gz_diskfile ) && ! $retry ) { |
|
1032 |
if ( function_exists( 'gzencode' ) && function_exists( 'file_get_contents' ) ) { |
|
1033 |
// Try upping the memory limit before gzipping |
|
1034 |
if ( function_exists( 'memory_get_usage' ) && ( (int) @ini_get( 'memory_limit' ) < 64 ) ) { |
|
1035 |
@ini_set( 'memory_limit', '64M' ); |
|
1036 |
} |
|
1037 |
||
1038 |
$contents = file_get_contents( $diskfile ); |
|
1039 |
$gzipped = gzencode( $contents, 9 ); |
|
1040 |
$fp = fopen( $gz_diskfile, 'w' ); |
|
1041 |
||
1042 |
fwrite( $fp, $gzipped ); |
|
1043 |
||
1044 |
if ( fclose( $fp ) ) { |
|
1045 |
unlink( $diskfile ); |
|
1046 |
} |
|
1047 |
} |
|
0 | 1048 |
} |
1049 |
||
18 | 1050 |
if ( file_exists( $gz_diskfile ) ) { |
1051 |
$filename = $filename . '.gz'; |
|
1052 |
$file_to_deliver = $gz_diskfile; |
|
0 | 1053 |
} else { |
18 | 1054 |
$file_to_deliver = $diskfile; |
0 | 1055 |
} |
1056 |
||
18 | 1057 |
if ( 'http' == $delivery ) { |
1058 |
if ( ! file_exists( $file_to_deliver ) ) { |
|
1059 |
if ( ! $retry ) { |
|
1060 |
$this->error( |
|
1061 |
array( |
|
1062 |
'kind' => 'fatal', |
|
1063 |
'msg' => sprintf( |
|
1064 |
__( |
|
1065 |
'File not found:%s', |
|
1066 |
'wp-db-backup' |
|
1067 |
), |
|
1068 |
" <strong>$filename</strong><br />" |
|
1069 |
) . '<br /><a href="' . $this->page_url . '">' . __( |
|
1070 |
'Return to Backup', |
|
1071 |
'wp-db-backup' |
|
1072 |
) . '</a>', |
|
1073 |
) |
|
1074 |
); |
|
0 | 1075 |
} else { |
1076 |
return true; |
|
1077 |
} |
|
18 | 1078 |
} else { |
1079 |
header( 'Content-Description: File Transfer' ); |
|
1080 |
header( 'Content-Type: application/octet-stream' ); |
|
1081 |
header( 'Content-Length: ' . filesize( $file_to_deliver ) ); |
|
1082 |
header( "Content-Disposition: attachment; filename=$filename" ); |
|
1083 |
$success = readfile( $file_to_deliver ); |
|
0 | 1084 |
if ( $success ) { |
18 | 1085 |
unlink( $file_to_deliver ); |
0 | 1086 |
} |
1087 |
} |
|
18 | 1088 |
} elseif ( 'smtp' == $delivery ) { |
1089 |
if ( ! file_exists( $file_to_deliver ) ) { |
|
1090 |
$msg = sprintf( __( 'File %s does not exist!', 'wp-db-backup' ), $file_to_deliver ); |
|
1091 |
$this->error( $msg ); |
|
0 | 1092 |
return false; |
1093 |
} |
|
18 | 1094 |
|
1095 |
if ( ! is_email( $recipient ) ) { |
|
1096 |
$recipient = get_option( 'admin_email' ); |
|
0 | 1097 |
} |
18 | 1098 |
|
1099 |
$message = sprintf( __( "Attached to this email is\n %1\$1s\n Size:%2\$2s kilobytes\n", 'wp-db-backup' ), $filename, round( filesize( $file_to_deliver ) / 1024 ) ); |
|
1100 |
$success = $this->send_mail( $recipient, get_bloginfo( 'name' ) . ' ' . __( 'Database Backup', 'wp-db-backup' ), $message, $file_to_deliver ); |
|
0 | 1101 |
|
1102 |
if ( false === $success ) { |
|
18 | 1103 |
$msg = __( 'The following errors were reported:', 'wp-db-backup' ) . "\n "; |
1104 |
if ( function_exists( 'error_get_last' ) ) { |
|
1105 |
$err = error_get_last(); |
|
0 | 1106 |
$msg .= $err['message']; |
1107 |
} else { |
|
18 | 1108 |
$msg .= __( 'ERROR: The mail application has failed to deliver the backup.', 'wp-db-backup' ); |
0 | 1109 |
} |
18 | 1110 |
$this->error( |
1111 |
array( |
|
1112 |
'kind' => 'fatal', |
|
1113 |
'loc' => $location, |
|
1114 |
'msg' => $msg, |
|
1115 |
) |
|
1116 |
); |
|
0 | 1117 |
} else { |
18 | 1118 |
if ( file_exists( $file_to_deliver ) ) { |
1119 |
unlink( $file_to_deliver ); |
|
0 | 1120 |
} |
1121 |
} |
|
1122 |
} |
|
18 | 1123 |
|
0 | 1124 |
return $success; |
1125 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1126 |
|
0 | 1127 |
function backup_menu() { |
1128 |
global $table_prefix, $wpdb; |
|
1129 |
$feedback = ''; |
|
18 | 1130 |
$whoops = false; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1131 |
|
0 | 1132 |
// did we just do a backup? If so, let's report the status |
1133 |
if ( $this->backup_complete ) { |
|
19 | 1134 |
$feedback = '<div class="wp-db-backup-updated"><p>' . __( 'Backup Successful', 'wp-db-backup' ) . '!'; |
18 | 1135 |
$file = $this->backup_file; |
1136 |
switch ( $_POST['deliver'] ) { |
|
1137 |
case 'http': |
|
1138 |
$feedback .= '<br />' . sprintf( __( 'Your backup file: %2s should begin downloading shortly.', 'wp-db-backup' ), "{$this->backup_file}", $this->backup_file ); |
|
1139 |
break; |
|
1140 |
case 'smtp': |
|
1141 |
$email = sanitize_text_field( wp_unslash( $_POST['backup_recipient'] ) ); |
|
1142 |
if ( ! is_email( $email ) ) { |
|
1143 |
$feedback .= get_option( 'admin_email' ); |
|
1144 |
} else { |
|
1145 |
$feedback .= $email; |
|
1146 |
} |
|
1147 |
$feedback = '<br />' . sprintf( __( 'Your backup has been emailed to %s', 'wp-db-backup' ), $feedback ); |
|
1148 |
break; |
|
0 | 1149 |
} |
18 | 1150 |
|
0 | 1151 |
$feedback .= '</p></div>'; |
1152 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1153 |
|
0 | 1154 |
// security check |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1155 |
$this->wp_secure(); |
0 | 1156 |
|
18 | 1157 |
if ( count( $this->errors ) ) { |
19 | 1158 |
$feedback .= '<div class="wp-db-backup-updated error inline"><p><strong>' . __( 'The following errors were reported:', 'wp-db-backup' ) . '</strong></p>'; |
0 | 1159 |
$feedback .= '<p>' . $this->error_display( 'main', false ) . '</p>'; |
18 | 1160 |
$feedback .= '</p></div>'; |
0 | 1161 |
} |
1162 |
||
1163 |
// did we just save options for wp-cron? |
|
18 | 1164 |
if ( ( function_exists( 'wp_schedule_event' ) || function_exists( 'wp_cron_init' ) ) && isset( $_POST['wp_cron_backup_options'] ) ) : |
19 | 1165 |
check_admin_referer( $this->referer_check_key ); |
1166 |
||
18 | 1167 |
do_action( 'wp_db_b_update_cron_options' ); |
1168 |
||
1169 |
if ( function_exists( 'wp_schedule_event' ) ) { |
|
0 | 1170 |
wp_clear_scheduled_hook( 'wp_db_backup_cron' ); // unschedule previous |
18 | 1171 |
$scheds = (array) wp_get_schedules(); |
1172 |
$name = sanitize_text_field( strval( $_POST['wp_cron_schedule'] ) ); |
|
1173 |
$interval = ( isset( $scheds[ $name ]['interval'] ) ) ? (int) $scheds[ $name ]['interval'] : 0; |
|
1174 |
update_option( 'wp_cron_backup_schedule', $name, false ); |
|
1175 |
||
0 | 1176 |
if ( 0 !== $interval ) { |
18 | 1177 |
wp_schedule_event( time() + $interval, $name, 'wp_db_backup_cron' ); |
0 | 1178 |
} |
18 | 1179 |
} else { |
1180 |
update_option( 'wp_cron_backup_schedule', intval( $_POST['cron_schedule'] ), false ); |
|
0 | 1181 |
} |
18 | 1182 |
|
1183 |
update_option( 'wp_cron_backup_tables', $this->get_submitted_tables_to_backup_in_cron() ); |
|
1184 |
||
1185 |
if ( is_email( $_POST['cron_backup_recipient'] ) ) { |
|
1186 |
update_option( 'wp_cron_backup_recipient', sanitize_text_field( $_POST['cron_backup_recipient'] ), false ); |
|
0 | 1187 |
} |
18 | 1188 |
|
19 | 1189 |
$feedback .= '<div class="wp-db-backup-updated wp-db-backup-schedule-updated"><p>' . __( 'Scheduled Backup Options Saved!', 'wp-db-backup' ) . '</p></div>'; |
0 | 1190 |
endif; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1191 |
|
0 | 1192 |
$other_tables = array(); |
18 | 1193 |
$also_backup = array(); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1194 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1195 |
// Get complete db table list |
19 | 1196 |
$all_tables = $this->get_tables(); |
18 | 1197 |
|
0 | 1198 |
// Get list of WP tables that actually exist in this DB (for 1.6 compat!) |
18 | 1199 |
$wp_backup_default_tables = array_intersect( $all_tables, $this->core_table_names ); |
0 | 1200 |
// Get list of non-WP tables |
18 | 1201 |
$other_tables = array_diff( $all_tables, $wp_backup_default_tables ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1202 |
|
18 | 1203 |
if ( ! $this->wp_secure() ) { |
0 | 1204 |
return; |
18 | 1205 |
} |
0 | 1206 |
|
1207 |
// Give the new dirs the same perms as wp-content. |
|
18 | 1208 |
// $stat = stat( ABSPATH . 'wp-content' ); |
1209 |
// $dir_perms = $stat['mode'] & 0000777; // Get the permission bits. |
|
0 | 1210 |
$dir_perms = '0777'; |
1211 |
||
1212 |
// the file doesn't exist and can't create it |
|
18 | 1213 |
if ( ! file_exists( $this->backup_dir ) && ! @mkdir( $this->backup_dir ) ) { |
1214 |
?> |
|
19 | 1215 |
<div class="wp-db-backup-updated error inline"> |
18 | 1216 |
<p><?php _e( 'WARNING: Your backup directory does <strong>NOT</strong> exist, and we cannot create it.', 'wp-db-backup' ); ?></p> |
1217 |
<p><?php printf( __( 'Using your FTP client, try to create the backup directory yourself: %s', 'wp-db-backup' ), '<code>' . $this->backup_dir . '</code>' ); ?></p> |
|
1218 |
</div> |
|
1219 |
<?php |
|
1220 |
// not writable due to write permissions |
|
0 | 1221 |
$whoops = true; |
18 | 1222 |
} elseif ( ! is_writable( $this->backup_dir ) && ! @chmod( $this->backup_dir, $dir_perms ) ) { |
1223 |
?> |
|
19 | 1224 |
<div class="wp-db-backup-updated error inline"> |
18 | 1225 |
<p><?php _e( 'WARNING: Your backup directory is <strong>NOT</strong> writable! We cannot create the backup files.', 'wp-db-backup' ); ?></p> |
1226 |
<p><?php printf( __( 'Using your FTP client, try to set the backup directory’s write permission to %1$s or %2$s: %3$s', 'wp-db-backup' ), '<code>777</code>', '<code>a+w</code>', '<code>' . $this->backup_dir . '</code>' ); ?></p> |
|
1227 |
</div> |
|
1228 |
<?php |
|
0 | 1229 |
$whoops = true; |
1230 |
} else { |
|
18 | 1231 |
$this->fp = $this->open( $this->backup_dir . 'test' ); |
1232 |
||
1233 |
if ( $this->fp ) { |
|
1234 |
$this->close( $this->fp ); |
|
1235 |
@unlink( $this->backup_dir . 'test' ); |
|
1236 |
// the directory is not writable probably due to safe mode |
|
0 | 1237 |
} else { |
18 | 1238 |
?> |
19 | 1239 |
<div class="wp-db-backup-updated error inline"> |
18 | 1240 |
<p><?php _e( 'WARNING: Your backup directory is <strong>NOT</strong> writable! We cannot create the backup files.', 'wp-db-backup' ); ?></p> |
1241 |
<?php |
|
1242 |
if ( ini_get( 'safe_mode' ) ) { |
|
1243 |
?> |
|
1244 |
<p><?php _e( 'This problem seems to be caused by your server’s <code>safe_mode</code> file ownership restrictions, which limit what files web applications like WordPress can create.', 'wp-db-backup' ); ?></p> |
|
1245 |
<?php |
|
1246 |
} |
|
1247 |
||
1248 |
printf( __( 'You can try to correct this problem by using your FTP client to delete and then re-create the backup directory: %s', 'wp-db-backup' ), '<code>' . $this->backup_dir . '</code>' ); |
|
1249 |
?> |
|
1250 |
</div> |
|
1251 |
<?php |
|
0 | 1252 |
$whoops = true; |
1253 |
} |
|
1254 |
} |
|
1255 |
||
18 | 1256 |
if ( ! file_exists( $this->backup_dir . 'index.php' ) ) { |
1257 |
@touch( $this->backup_dir . 'index.php' ); |
|
1258 |
} |
|
1259 |
?> |
|
19 | 1260 |
<div id="wpdb" class='wrap'> |
1261 |
<div class="header"> |
|
1262 |
<img src="<?php echo plugin_dir_url( __FILE__ ) . 'assets/logo.svg'; ?>"> |
|
1263 |
<h2 class="title"><?php _e( 'Database Backup for WordPress', 'wp-db-backup' ); ?></h2> |
|
1264 |
</div> |
|
1265 |
||
1266 |
<div class="subnav"> |
|
1267 |
<ul> |
|
1268 |
<li> |
|
1269 |
<a class="active" href="#backup" data-type="backup">Backup Now</a> |
|
1270 |
</li> |
|
1271 |
<li> |
|
1272 |
<a href="#schedule" data-type="schedule">Scheduled Backup</a> |
|
1273 |
</li> |
|
1274 |
</ul> |
|
1275 |
</div> |
|
1276 |
||
1277 |
<div class="content-wrap"> |
|
1278 |
||
1279 |
<?php |
|
1280 |
if ( '' != $feedback ) { |
|
1281 |
echo $feedback; |
|
1282 |
} |
|
1283 |
||
1284 |
if ( isset( $_POST['do_backup'] ) && $_POST['do_backup'] === 'fragments' ) { |
|
1285 |
$this->build_backup_script(); |
|
1286 |
} |
|
1287 |
?> |
|
1288 |
||
18 | 1289 |
<form method="post" action=""> |
1290 |
<?php |
|
1291 |
if ( function_exists( 'wp_nonce_field' ) ) { |
|
1292 |
wp_nonce_field( $this->referer_check_key ); |
|
0 | 1293 |
} |
1294 |
?> |
|
19 | 1295 |
|
1296 |
<fieldset class="options backup-content"> |
|
1297 |
<legend><?php _e( 'Tables', 'wp-db-backup' ); ?></legend> |
|
1298 |
||
1299 |
<div class="panel-heading"> |
|
1300 |
<h3>Tables</h3> |
|
1301 |
</div> |
|
1302 |
||
1303 |
<div class="panel-content tables"> |
|
1304 |
||
18 | 1305 |
<div class="tables-list core-tables alternate"> |
19 | 1306 |
<div class="instructions-container"> |
1307 |
<h4><?php _e( 'Core WordPress tables to backup', 'wp-db-backup' ); ?></h4> |
|
1308 |
</div> |
|
18 | 1309 |
<ul> |
1310 |
<?php |
|
1311 |
$excs = (array) get_option( 'wp_db_backup_excs' ); |
|
1312 |
foreach ( $wp_backup_default_tables as $table ) { |
|
1313 |
if ( $table == $wpdb->comments ) { |
|
1314 |
$checked = ( isset( $excs['spam'] ) && is_array( $excs['spam'] ) && in_array( $table, $excs['spam'] ) ) ? ' checked=\'checked\'' : ''; |
|
19 | 1315 |
echo "<li><input type='hidden' name='core_tables[]' value='$table' /><code>$table</code> <span class='instructions'><label for='exclude-spam'><input type='checkbox' id='exclude-spam' name='exclude-spam[]' value='$table' $checked /> " . __( 'Exclude spam comments', 'wp-db-backup' ) . '</label></span></li>'; |
18 | 1316 |
} elseif ( function_exists( 'wp_get_post_revisions' ) && $table == $wpdb->posts ) { |
1317 |
$checked = ( isset( $excs['revisions'] ) && is_array( $excs['revisions'] ) && in_array( $table, $excs['revisions'] ) ) ? ' checked=\'checked\'' : ''; |
|
19 | 1318 |
echo "<li><input type='hidden' name='core_tables[]' value='$table' /><code>$table</code> <span class='instructions'><label for='exclude-revisions'><input type='checkbox'id='exclude-revisions' name='exclude-revisions[]' value='$table' $checked /> " . __( 'Exclude post revisions', 'wp-db-backup' ) . '</label></span></li>'; |
18 | 1319 |
} else { |
1320 |
echo "<li><input type='hidden' name='core_tables[]' value='$table' /><code>$table</code></li>"; |
|
1321 |
} |
|
1322 |
} |
|
1323 |
?> |
|
1324 |
</ul> |
|
1325 |
</div> |
|
19 | 1326 |
|
18 | 1327 |
<div class="tables-list extra-tables" id="extra-tables-list"> |
19 | 1328 |
|
0 | 1329 |
<?php |
18 | 1330 |
if ( count( $other_tables ) > 0 ) { |
0 | 1331 |
?> |
19 | 1332 |
<div class="instructions-container"> |
1333 |
<h4><?php _e( 'Additional tables to backup', 'wp-db-backup' ); ?></h4> |
|
1334 |
<p hidden><?php _e( 'Hold <code class="shift-key">SHIFT</code> to toggle multiple checkboxes', 'wp-db-backup' ); ?></p> |
|
1335 |
</div> |
|
18 | 1336 |
<ul> |
1337 |
<?php |
|
1338 |
foreach ( $other_tables as $table ) { |
|
1339 |
?> |
|
1340 |
<li><label><input type="checkbox" name="other_tables[]" value="<?php echo $table; ?>" /> <code><?php echo $table; ?></code></label> |
|
1341 |
<?php |
|
1342 |
} |
|
1343 |
?> |
|
1344 |
</ul> |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1345 |
<?php |
0 | 1346 |
} |
18 | 1347 |
?> |
1348 |
</div> |
|
19 | 1349 |
|
1350 |
</div><!--panel-content--> |
|
0 | 1351 |
</fieldset> |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1352 |
|
19 | 1353 |
|
1354 |
<!--BACKUP PANEL--> |
|
1355 |
<fieldset class="options backup-content"> |
|
18 | 1356 |
<legend><?php _e( 'Backup Options', 'wp-db-backup' ); ?></legend> |
19 | 1357 |
|
1358 |
<div class="panel-heading"> |
|
1359 |
<h3>Backup Options</h3> |
|
1360 |
</div> |
|
1361 |
||
1362 |
<div class="panel-content backup"> |
|
0 | 1363 |
<ul> |
1364 |
<li><label for="do_download"> |
|
1365 |
<input type="radio" checked="checked" id="do_download" name="deliver" value="http" style="border:none;" /> |
|
19 | 1366 |
<?php _e( 'Download', 'wp-db-backup' ); ?> |
0 | 1367 |
</label></li> |
1368 |
<li><label for="do_email"> |
|
1369 |
<input type="radio" name="deliver" id="do_email" value="smtp" style="border:none;" /> |
|
18 | 1370 |
<?php |
1371 |
$backup_recip = get_option( 'wpdb_backup_recip' ); |
|
1372 |
if ( empty( $backup_recip ) ) { |
|
1373 |
$backup_recip = get_option( 'admin_email' ); |
|
1374 |
} |
|
19 | 1375 |
_e( 'Send to email address', 'wp-db-backup' ); |
18 | 1376 |
?> |
19 | 1377 |
|
1378 |
<div class="email"> |
|
1379 |
<label for="backup_recipient">Email Address</label> |
|
1380 |
<input type="text" id="backup_recipient" name="backup_recipient" size="20" value="<?php echo esc_attr( $backup_recip ); ?>" /> |
|
1381 |
</div> |
|
0 | 1382 |
</label></li> |
1383 |
</ul> |
|
1384 |
<?php if ( ! $whoops ) : ?> |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1385 |
<input type="hidden" name="do_backup" id="do_backup" value="backup" /> |
0 | 1386 |
<p class="submit"> |
19 | 1387 |
<input type="submit" name="submit" onclick="document.getElementById('do_backup').value='fragments';" value="<?php _e( 'Backup now', 'wp-db-backup' ); ?>" /> |
0 | 1388 |
</p> |
1389 |
<?php else : ?> |
|
19 | 1390 |
<div class="wp-db-backup-updated error inline"><p><?php _e( 'WARNING: Your backup directory is <strong>NOT</strong> writable!', 'wp-db-backup' ); ?></p></div> |
0 | 1391 |
<?php endif; // ! whoops ?> |
19 | 1392 |
|
1393 |
</div><!--panel-content--> |
|
0 | 1394 |
</fieldset> |
18 | 1395 |
<?php do_action( 'wp_db_b_backup_opts' ); ?> |
0 | 1396 |
</form> |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1397 |
|
0 | 1398 |
<?php |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1399 |
// this stuff only displays if some sort of wp-cron is available |
18 | 1400 |
$cron = ( function_exists( 'wp_schedule_event' ) ) ? true : false; // wp-cron in WP 2.1+ |
1401 |
$cron_old = ( function_exists( 'wp_cron_init' ) && ! $cron ) ? true : false; // wp-cron plugin by Skippy |
|
1402 |
||
0 | 1403 |
if ( $cron_old || $cron ) : |
19 | 1404 |
echo '<fieldset class="options schedule-content" hidden><legend>' . __( 'Scheduled Backup', 'wp-db-backup' ) . '</legend>'; |
1405 |
echo '<div class="panel-heading"><h3>Scheduled Backup</h3></div>'; |
|
1406 |
||
1407 |
echo '<div class="panel-content scheduled-backup">'; |
|
1408 |
||
18 | 1409 |
$datetime = get_option( 'date_format' ) . ' ' . get_option( 'time_format' ); |
0 | 1410 |
if ( $cron ) : |
18 | 1411 |
$next_cron = wp_next_scheduled( 'wp_db_backup_cron' ); |
0 | 1412 |
if ( ! empty( $next_cron ) ) : |
1413 |
?> |
|
1414 |
<p id="backup-time-wrap"> |
|
19 | 1415 |
<?php printf( __( '<strong>Next Backup:</strong> %s', 'wp-db-backup' ), '<span id="next-backup-time">' . gmdate( $datetime, $next_cron + ( get_option( 'gmt_offset' ) * 3600 ) ) . '</span>' ); ?> |
0 | 1416 |
</p> |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1417 |
<?php |
0 | 1418 |
endif; |
1419 |
elseif ( $cron_old ) : |
|
18 | 1420 |
?> |
1421 |
<p><?php printf( __( 'Last WP-Cron Daily Execution: %s', 'wp-db-backup' ), gmdate( $datetime, get_option( 'wp_cron_daily_lastrun' ) + ( get_option( 'gmt_offset' ) * 3600 ) ) ); ?><br /> |
|
1422 |
<?php |
|
1423 |
printf( __( 'Next WP-Cron Daily Execution: %s', 'wp-db-backup' ), gmdate( $datetime, ( get_option( 'wp_cron_daily_lastrun' ) + ( get_option( 'gmt_offset' ) * 3600 ) + 86400 ) ) ); |
|
1424 |
?> |
|
1425 |
</p> |
|
1426 |
<?php |
|
0 | 1427 |
endif; |
18 | 1428 |
?> |
1429 |
<form method="post" action=""> |
|
1430 |
<?php |
|
1431 |
if ( function_exists( 'wp_nonce_field' ) ) { |
|
1432 |
wp_nonce_field( $this->referer_check_key );} |
|
1433 |
?> |
|
19 | 1434 |
|
1435 |
<div class="panel-content row"> |
|
1436 |
<div class="tables-list scheduled"> |
|
1437 |
<h4><?php _e( 'Schedule', 'wp-db-backup' ); ?></h4> |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1438 |
<?php |
0 | 1439 |
if ( $cron_old ) : |
18 | 1440 |
$wp_cron_backup_schedule = get_option( 'wp_cron_backup_schedule' ); |
1441 |
$schedule = array( |
|
1442 |
0 => __( 'None', 'wp-db-backup' ), |
|
1443 |
1 => __( 'Daily', 'wp-db-backup' ), |
|
1444 |
); |
|
1445 |
foreach ( $schedule as $value => $name ) { |
|
19 | 1446 |
echo '<input type="radio" style="border:none;" name="cron_schedule"'; |
18 | 1447 |
if ( $wp_cron_backup_schedule == $value ) { |
0 | 1448 |
echo ' checked="checked" '; |
1449 |
} |
|
1450 |
echo 'value="' . $value . '" /> ' . $name; |
|
1451 |
} |
|
1452 |
elseif ( $cron ) : |
|
18 | 1453 |
echo apply_filters( 'wp_db_b_schedule_choices', wp_get_schedules() ); |
0 | 1454 |
endif; |
19 | 1455 |
|
18 | 1456 |
$cron_recipient = get_option( 'wp_cron_backup_recipient' ); |
19 | 1457 |
|
18 | 1458 |
if ( ! is_email( $cron_recipient ) ) { |
1459 |
$cron_recipient = get_option( 'admin_email' ); |
|
0 | 1460 |
} |
19 | 1461 |
|
1462 |
$cron_recipient_input = '<div class="email"><label for="cron_backup_recipient">' . __( 'Email backup to', 'wp-db-backup' ) . ' <input type="text" name="cron_backup_recipient" id="cron_backup_recipient" size="20" value="' . $cron_recipient . '" /></div></label>'; |
|
18 | 1463 |
echo apply_filters( 'wp_db_b_cron_recipient_input', $cron_recipient_input ); |
0 | 1464 |
echo '</div>'; |
18 | 1465 |
$cron_tables = get_option( 'wp_cron_backup_tables' ); |
19 | 1466 |
|
18 | 1467 |
if ( ! is_array( $cron_tables ) ) { |
0 | 1468 |
$cron_tables = array(); |
1469 |
} |
|
19 | 1470 |
|
18 | 1471 |
if ( count( $other_tables ) > 0 ) { |
0 | 1472 |
echo '<div class="tables-list alternate" id="include-tables-list">'; |
19 | 1473 |
echo '<div class="instructions-container">'; |
1474 |
echo '<h4>' . __( 'Tables to include in the scheduled backup:', 'wp-db-backup' ) . '</h4>'; |
|
1475 |
if ( count( $other_tables ) > 1 ) { |
|
1476 |
echo '<p>' . __( 'Hold <code class="shift-key">SHIFT</code> to toggle multiple checkboxes', 'wp-db-backup' ) . '</p>'; |
|
1477 |
} |
|
1478 |
echo '</div><ul>'; |
|
18 | 1479 |
foreach ( $other_tables as $table ) { |
19 | 1480 |
echo '<li><label><input type="checkbox" '; |
18 | 1481 |
if ( in_array( $table, $cron_tables ) ) { |
0 | 1482 |
echo 'checked="checked" '; |
1483 |
} |
|
19 | 1484 |
echo "name='wp_cron_backup_tables[]' value='{$table}' /> <code>{$table}</code></label></li>"; |
0 | 1485 |
} |
1486 |
echo '</ul></div>'; |
|
19 | 1487 |
echo '</div><!-- panel-content .row -->'; |
0 | 1488 |
} |
19 | 1489 |
|
1490 |
echo '<p class="submit"><input type="submit" name="submit" value="' . __( 'Save schedule', 'wp-db-backup' ) . '" /></p>'; |
|
1491 |
||
0 | 1492 |
echo '<input type="hidden" name="wp_cron_backup_options" value="SET" /></form>'; |
19 | 1493 |
echo '</div><!-- .panel-content scheduled-backup -->'; |
0 | 1494 |
echo '</fieldset>'; |
1495 |
endif; // end of wp_cron (legacy) section |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1496 |
|
19 | 1497 |
echo '</div><!-- .content-wrap -->'; |
0 | 1498 |
echo '</div><!-- .wrap -->'; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1499 |
|
0 | 1500 |
} // end wp_backup_menu() |
1501 |
||
1502 |
function get_sched() { |
|
1503 |
$options = array_keys( (array) wp_get_schedules() ); |
|
18 | 1504 |
$freq = get_option( 'wp_cron_backup_schedule' ); |
1505 |
$freq = ( in_array( $freq, $options ) ) ? $freq : 'never'; |
|
1506 |
||
0 | 1507 |
return $freq; |
1508 |
} |
|
1509 |
||
18 | 1510 |
function schedule_choices( $schedule ) { |
1511 |
// create the cron menu based on the schedule |
|
0 | 1512 |
$wp_cron_backup_schedule = $this->get_sched(); |
18 | 1513 |
$next_cron = wp_next_scheduled( 'wp_db_backup_cron' ); |
0 | 1514 |
$wp_cron_backup_schedule = ( empty( $next_cron ) ) ? 'never' : $wp_cron_backup_schedule; |
18 | 1515 |
$sort = array(); |
1516 |
||
1517 |
foreach ( (array) $schedule as $key => $value ) { |
|
1518 |
$sort[ $key ] = $value['interval']; |
|
1519 |
} |
|
0 | 1520 |
asort( $sort ); |
18 | 1521 |
|
0 | 1522 |
$schedule_sorted = array(); |
18 | 1523 |
foreach ( (array) $sort as $key => $value ) { |
1524 |
$schedule_sorted[ $key ] = $schedule[ $key ]; |
|
1525 |
} |
|
1526 |
||
1527 |
$menu = '<ul>'; |
|
1528 |
$schedule = array_merge( |
|
1529 |
array( |
|
1530 |
'never' => array( |
|
1531 |
'interval' => 0, |
|
1532 |
'display' => __( 'Never', 'wp-db-backup' ), |
|
1533 |
), |
|
1534 |
), |
|
1535 |
(array) $schedule_sorted |
|
1536 |
); |
|
1537 |
||
1538 |
foreach ( $schedule as $name => $settings ) { |
|
0 | 1539 |
$interval = (int) $settings['interval']; |
18 | 1540 |
if ( 0 == $interval && ! 'never' == $name ) { |
1541 |
continue; |
|
1542 |
} |
|
1543 |
$display = ( ! '' == $settings['display'] ) ? $settings['display'] : sprintf( __( '%s seconds', 'wp-db-backup' ), $interval ); |
|
19 | 1544 |
$menu .= "<li><label for='$name'><input type='radio' name='wp_cron_schedule' style='border:none;' "; |
18 | 1545 |
if ( $wp_cron_backup_schedule == $name ) { |
0 | 1546 |
$menu .= " checked='checked' "; |
1547 |
} |
|
19 | 1548 |
$menu .= "id='$name' value='$name' />$display</label></li>"; |
0 | 1549 |
} |
18 | 1550 |
|
0 | 1551 |
$menu .= '</ul>'; |
18 | 1552 |
|
0 | 1553 |
return $menu; |
1554 |
} // end schedule_choices() |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1555 |
|
18 | 1556 |
function wp_cron_daily() { |
1557 |
// for legacy cron plugin |
|
1558 |
$schedule = intval( get_option( 'wp_cron_backup_schedule' ) ); |
|
1559 |
||
0 | 1560 |
// If scheduled backup is disabled |
18 | 1561 |
if ( 0 == $schedule ) { |
1562 |
return; |
|
1563 |
} else { |
|
1564 |
return $this->cron_backup(); |
|
1565 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1566 |
} |
0 | 1567 |
|
1568 |
function cron_backup() { |
|
1569 |
global $table_prefix, $wpdb; |
|
18 | 1570 |
|
19 | 1571 |
$all_tables = $this->get_tables(); |
18 | 1572 |
$core_tables = array_intersect( $all_tables, $this->core_table_names ); |
1573 |
$other_tables = get_option( 'wp_cron_backup_tables' ); |
|
1574 |
$recipient = get_option( 'wp_cron_backup_recipient' ); |
|
1575 |
$backup_file = $this->db_backup( $core_tables, $other_tables ); |
|
1576 |
||
1577 |
if ( false !== $backup_file ) { |
|
1578 |
return $this->deliver_backup( $backup_file, 'smtp', $recipient, 'main' ); |
|
1579 |
} else { |
|
1580 |
return false; |
|
1581 |
} |
|
0 | 1582 |
} |
1583 |
||
18 | 1584 |
function add_sched_options( $sched ) { |
1585 |
$sched['weekly'] = array( |
|
1586 |
'interval' => 604800, |
|
1587 |
'display' => __( 'Once Weekly', 'wp-db-backup' ), |
|
1588 |
); |
|
1589 |
||
0 | 1590 |
return $sched; |
1591 |
} |
|
1592 |
||
1593 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1594 |
* Checks that WordPress has sufficient security measures |
0 | 1595 |
* @param string $kind |
1596 |
* @return bool |
|
1597 |
*/ |
|
18 | 1598 |
function wp_secure( $kind = 'warn', $loc = 'main' ) { |
0 | 1599 |
global $wp_version; |
18 | 1600 |
|
1601 |
if ( function_exists( 'wp_verify_nonce' ) ) { |
|
1602 |
return true; |
|
1603 |
} else { |
|
1604 |
$this->error( |
|
1605 |
array( |
|
1606 |
'kind' => $kind, |
|
1607 |
'loc' => $loc, |
|
1608 |
'msg' => sprintf( |
|
1609 |
__( |
|
1610 |
'Your WordPress version, %1$1s, lacks important security features without which it is unsafe to use the WP-DB-Backup plugin. Hence, this plugin is automatically disabled. Please consider <a href="%2$2s">upgrading WordPress</a> to a more recent version.', |
|
1611 |
'wp-db-backup' |
|
1612 |
), |
|
1613 |
$wp_version, |
|
1614 |
'http://wordpress.org/download/' |
|
1615 |
), |
|
1616 |
) |
|
1617 |
); |
|
1618 |
||
0 | 1619 |
return false; |
1620 |
} |
|
1621 |
} |
|
1622 |
||
1623 |
/** |
|
1624 |
* Checks that the user has sufficient permission to backup |
|
1625 |
* @param string $loc |
|
1626 |
* @return bool |
|
1627 |
*/ |
|
18 | 1628 |
function can_user_backup( $loc = 'main' ) { |
0 | 1629 |
$can = false; |
18 | 1630 |
|
0 | 1631 |
// make sure WPMU users are site admins, not ordinary admins |
18 | 1632 |
if ( function_exists( 'is_site_admin' ) && ! is_site_admin() ) { |
0 | 1633 |
return false; |
18 | 1634 |
} |
1635 |
||
1636 |
if ( ( $this->wp_secure( 'fatal', $loc ) ) && current_user_can( 'import' ) ) { |
|
1637 |
$can = $this->verify_nonce( $_REQUEST['_wpnonce'], $this->referer_check_key, $loc ); |
|
1638 |
} |
|
1639 |
||
1640 |
if ( false == $can ) { |
|
1641 |
$this->error( |
|
1642 |
array( |
|
1643 |
'loc' => $loc, |
|
1644 |
'kind' => 'fatal', |
|
1645 |
'msg' => __( |
|
1646 |
'You are not allowed to perform backups.', |
|
1647 |
'wp-db-backup' |
|
1648 |
), |
|
1649 |
) |
|
1650 |
); |
|
1651 |
} |
|
1652 |
||
0 | 1653 |
return $can; |
1654 |
} |
|
1655 |
||
1656 |
/** |
|
1657 |
* Verify that the nonce is legitimate |
|
18 | 1658 |
* @param string $rec the nonce received |
1659 |
* @param string $nonce what the nonce should be |
|
1660 |
* @param string $loc the location of the check |
|
0 | 1661 |
* @return bool |
1662 |
*/ |
|
18 | 1663 |
function verify_nonce( $rec = '', $nonce = 'X', $loc = 'main' ) { |
1664 |
if ( wp_verify_nonce( $rec, $nonce ) ) { |
|
0 | 1665 |
return true; |
18 | 1666 |
} else { |
1667 |
$this->error( |
|
1668 |
array( |
|
1669 |
'loc' => $loc, |
|
1670 |
'kind' => 'fatal', |
|
1671 |
'msg' => sprintf( |
|
1672 |
__( |
|
1673 |
'There appears to be an unauthorized attempt from this site to access your database located at %1s. The attempt has been halted.', |
|
1674 |
'wp-db-backup' |
|
1675 |
), |
|
1676 |
get_option( 'home' ) |
|
1677 |
), |
|
1678 |
) |
|
1679 |
); |
|
1680 |
} |
|
0 | 1681 |
} |
1682 |
||
1683 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1684 |
* Check whether a file to be downloaded is |
0 | 1685 |
* surreptitiously trying to download a non-backup file |
1686 |
* @param string $file |
|
1687 |
* @return null |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1688 |
*/ |
18 | 1689 |
function validate_file( $file ) { |
1690 |
if ( ( false !== strpos( $file, '..' ) ) || ( false !== strpos( $file, './' ) ) || ( ':' == substr( $file, 1, 1 ) ) ) { |
|
1691 |
$this->error( |
|
1692 |
array( |
|
1693 |
'kind' => 'fatal', |
|
1694 |
'loc' => 'frame', |
|
1695 |
'msg' => __( |
|
1696 |
"Cheatin' uh ?", |
|
1697 |
'wp-db-backup' |
|
1698 |
), |
|
1699 |
) |
|
1700 |
); |
|
1701 |
} |
|
0 | 1702 |
} |
1703 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1704 |
/** |
18 | 1705 |
* Get the sitename by query $_SERVER['SERVER_NAME']. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1706 |
* If it is not set, then use site_url() instead |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1707 |
* @return string |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1708 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1709 |
function get_sitename() { |
18 | 1710 |
$sitename = ''; |
1711 |
||
1712 |
if ( isset( $_SERVER['SERVER_NAME'] ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1713 |
$sitename = strtolower( sanitize_text_field( $_SERVER['SERVER_NAME'] ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1714 |
} else { |
18 | 1715 |
if ( function_exists( 'site_url' ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1716 |
// site_url() was added since 3.0.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1717 |
// force http scheme so we can easily get rid of leading http:// |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1718 |
$sitename = strtolower( site_url( '', 'http' ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1719 |
$sitename = substr( $sitename, 7 ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1720 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1721 |
// try to be compatible with versions < 3.0.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1722 |
$sitename = strtolower( get_option( 'siteurl' ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1723 |
if ( substr( $sitename, 0, 7 ) == 'http://' ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1724 |
$sitename = substr( $sitename, 7 ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1725 |
} elseif ( substr( $sitename, 0, 8 ) == 'https://' ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1726 |
$sitename = substr( $sitename, 8 ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1727 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1728 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1729 |
} |
18 | 1730 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1731 |
// get rid of www |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1732 |
if ( substr( $sitename, 0, 4 ) == 'www.' ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1733 |
$sitename = substr( $sitename, 4 ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1734 |
} |
18 | 1735 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1736 |
return $sitename; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1737 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1738 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1739 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1740 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1741 |
* Sanitize an array of content. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1742 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1743 |
* @param array $array_of_data |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1744 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1745 |
* @return array |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1746 |
*/ |
18 | 1747 |
function sanitize_array( $array_to_sanitize ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1748 |
$sanitized = array(); |
18 | 1749 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1750 |
foreach ( $array_to_sanitize as $key => $value ) { |
18 | 1751 |
$sanitized[ $key ] = sanitize_text_field( $value ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1752 |
} |
18 | 1753 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1754 |
return $sanitized; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1755 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1756 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1757 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1758 |
* Get a sanitized array of submitted $_POST values |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1759 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1760 |
* @param string $post_key The key of the $_POST array. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1761 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1762 |
* @return array |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1763 |
*/ |
18 | 1764 |
function get_post_data_array( $post_key ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1765 |
$sanitized_data = array(); |
18 | 1766 |
|
1767 |
if ( isset( $_POST[ $post_key ] ) ) { |
|
1768 |
$sanitized_data = (array) $_POST[ $post_key ]; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1769 |
} |
18 | 1770 |
|
1771 |
return $this->sanitize_array( $sanitized_data ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1772 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1773 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1774 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1775 |
* Get the revisions to exclude. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1776 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1777 |
* @return array |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1778 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1779 |
function get_revisions_to_exclude() { |
18 | 1780 |
return $this->get_post_data_array( 'exclude-revisions' ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1781 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1782 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1783 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1784 |
* Get the spam to exclude. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1785 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1786 |
* @return array |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1787 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1788 |
function get_spam_to_exclude() { |
18 | 1789 |
return $this->get_post_data_array( 'exclude-spam' ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1790 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1791 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1792 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1793 |
* Get the submitted tables to backup. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1794 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1795 |
* @return array |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1796 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1797 |
function get_submitted_tables_to_backup_in_cron() { |
18 | 1798 |
return $this->get_post_data_array( 'wp_cron_backup_tables' ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1799 |
} |
18 | 1800 |
|
19 | 1801 |
/** |
1802 |
* Get an array of all tables on the current WP install. |
|
1803 |
* |
|
1804 |
* @return array |
|
1805 |
*/ |
|
1806 |
function get_tables() { |
|
1807 |
global $wpdb; |
|
1808 |
||
1809 |
$all_tables = $wpdb->get_results( 'SHOW TABLES', ARRAY_N ); |
|
1810 |
||
1811 |
return array_map( |
|
1812 |
function( $a ) { |
|
1813 |
return $a[0]; |
|
1814 |
}, |
|
1815 |
$all_tables |
|
1816 |
); |
|
1817 |
} |
|
1818 |
||
0 | 1819 |
} |
1820 |
||
1821 |
function wpdbBackup_init() { |
|
1822 |
global $mywpdbbackup; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
1823 |
$mywpdbbackup = new wpdbBackup(); |
0 | 1824 |
} |
1825 |
||
18 | 1826 |
add_action( 'plugins_loaded', 'wpdbBackup_init' ); |