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