PluginProbe
UpdraftPlus: WP Backup & Migration Plugin / 0.8.31
UpdraftPlus: WP Backup & Migration Plugin v0.8.31
1.26.7 1.26.6 1.26.5 1.26.4 1.26.3 1.9.19 1.9.25 1.9.26 1.9.30 1.9.31 1.9.32 1.9.4 1.9.40 1.9.41 1.9.42 1.9.43 1.9.44 1.9.45 1.9.46 1.9.5 1.9.50 1.9.51 1.9.60 1.9.62 1.9.63 All 371 releases
updraftplus / updraftplus.php

updraftplus.php in UpdraftPlus: WP Backup & Migration Plugin 0.8.31, at updraftplus.php

2,162 lines 91.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 Plugin Name: UpdraftPlus - Backup/Restore
4 Plugin URI: http://wordpress.org/extend/plugins/updraftplus
5 Description: Uploads, themes, plugins, and your DB can be automatically backed up to Amazon S3, Google Drive, FTP, or emailed. Files and DB can be on separate schedules.
6 Author: David Anderson.
7 Version: 0.8.31
8 Donate link: http://david.dw-perspective.org.uk/donate
9 Author URI: http://wordshell.net
10 */
11
12 //TODO:
13 //Add DropBox support
14 //Struggles with large uploads - runs out of time before finishing. Break into chunks? Resume download on later run? (Add a new scheduled event to check on progress? Separate the upload from the creation?). Add in some logging (in a .php file that exists first).
15 //improve error reporting. s3 and dir backup have decent reporting now, but not sure i know what to do from here
16 //list backups that aren't tracked (helps with double backup problem)
17 //investigate $php_errormsg further
18 //pretty up return messages in admin area
19 //check s3/ftp download
20
21 /* More TODO:
22 Are all directories in wp-content covered? No; only plugins, themes, content. We should check for others and allow the user the chance to choose which ones he wants
23 Use only one entry in WP options database
24 Encrypt filesystem, if memory allows (and have option for abort if not); split up into multiple zips when needed
25 */
26
27 /* Portions copyright 2010 Paul Kehrer
28 Portions copyright 2011-12 David Anderson
29
30 This program is free software; you can redistribute it and/or modify
31 it under the terms of the GNU General Public License as published by
32 the Free Software Foundation; either version 2 of the License, or
33 (at your option) any later version.
34
35 This program is distributed in the hope that it will be useful,
36 but WITHOUT ANY WARRANTY; without even the implied warranty of
37 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 GNU General Public License for more details.
39
40 You should have received a copy of the GNU General Public License
41 along with this program; if not, write to the Free Software
42 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
43 */
44 // TODO: Note this might *lower* the limit - should check first.
45
46 @set_time_limit(900); //15 minutes max. i'm not sure how long a really big site could take to back up?
47
48 $updraft = new UpdraftPlus();
49
50 if(!$updraft->memory_check(192)) {
51 # TODO: Better solution is to split the backup set into manageable chunks based on this limit
52 @ini_set('memory_limit', '192M'); //up the memory limit for large backup files... should split the backup set into manageable chunks based on the limit
53 }
54
55 class UpdraftPlus {
56
57 var $version = '0.8.31';
58
59 var $dbhandle;
60 var $errors = array();
61 var $nonce;
62 var $logfile_name = "";
63 var $logfile_handle = false;
64 var $backup_time;
65
66 function __construct() {
67 // Initialisation actions
68 # Create admin page
69 add_action('admin_menu', array($this,'add_admin_pages'));
70 add_action('admin_init', array($this,'admin_init'));
71 add_action('updraft_backup', array($this,'backup_files'));
72 add_action('updraft_backup_database', array($this,'backup_database'));
73 # backup_all is used by the manual "Backup Now" button
74 add_action('updraft_backup_all', array($this,'backup_all'));
75 add_action('wp_ajax_updraft_download_backup', array($this, 'updraft_download_backup'));
76 add_filter('cron_schedules', array($this,'modify_cron_schedules'));
77 add_filter('plugin_action_links', array($this, 'plugin_action_links'), 10, 2);
78 add_action('init', array($this, 'googledrive_backup_auth'));
79 }
80
81 // Handle Google OAuth 2.0
82 function googledrive_backup_auth() {
83 if ( is_admin() && isset( $_GET['page'] ) && $_GET['page'] == 'updraftplus' && isset( $_GET['action'] ) && $_GET['action'] == 'auth' ) {
84 if ( isset( $_GET['state'] ) ) {
85 if ( $_GET['state'] == 'token' )
86 $this->auth_token();
87 elseif ( $_GET['state'] == 'revoke' )
88 $this->auth_revoke();
89 } elseif (isset($_GET['updraftplus_googleauth'])) {
90 $this->auth_request();
91 }
92 }
93 }
94
95 /**
96 * Acquire single-use authorization code from Google OAuth 2.0
97 */
98 function auth_request() {
99 $params = array(
100 'response_type' => 'code',
101 'client_id' => get_option('updraft_googledrive_clientid'),
102 'redirect_uri' => admin_url('options-general.php?page=updraftplus&action=auth'),
103 'scope' => 'https://www.googleapis.com/auth/drive.file https://docs.google.com/feeds/ https://docs.googleusercontent.com/ https://spreadsheets.google.com/feeds/',
104 'state' => 'token',
105 'access_type' => 'offline',
106 'approval_prompt' => 'auto'
107 );
108 header('Location: https://accounts.google.com/o/oauth2/auth?'.http_build_query($params));
109 }
110
111 /**
112 * Get a Google account access token using the refresh token
113 */
114 function access_token( $token, $client_id, $client_secret ) {
115 $context = array(
116 'http' => array(
117 'method' => 'POST',
118 'header' => 'Content-type: application/x-www-form-urlencoded',
119 'content' => http_build_query( array(
120 'refresh_token' => $token,
121 'client_id' => $client_id,
122 'client_secret' => $client_secret,
123 'grant_type' => 'refresh_token'
124 ) )
125 )
126 );
127 $this->log("Google Drive: requesting access token: client_id=$client_id");
128 $result = @file_get_contents('https://accounts.google.com/o/oauth2/token', false, stream_context_create($context));
129 if($result) {
130 $result = json_decode( $result, true );
131 if ( isset( $result['access_token'] ) ) {
132 $this->log("Google Drive: successfully obtained access token");
133 return $result['access_token'];
134 } else {
135 $this->log("Google Drive error when requesting access token: response does not contain access_token");
136 return false;
137 }
138 } else {
139 $this->log("Google Drive error when requesting access token: no response");
140 return false;
141 }
142 }
143
144 /**
145 * Function to upload a file to Google Drive
146 *
147 * @param string $file Path to the file that is to be uploaded
148 * @param string $title Title to be given to the file
149 * @param string $parent ID of the folder in which to upload the file
150 * @param string $token Access token from Google Account
151 * @return boolean Returns TRUE on success, FALSE on failure
152 */
153 function googledrive_upload_file( $file, $title, $parent = '', $token) {
154
155 $size = filesize( $file );
156
157 $content = '<?xml version=\'1.0\' encoding=\'UTF-8\'?>
158 <entry xmlns="http://www.w3.org/2005/Atom" xmlns:docs="http://schemas.google.com/docs/2007">
159 <category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/docs/2007#file"/>
160 <title>' . $title . '</title>
161 </entry>';
162
163 $header = array(
164 'Authorization: Bearer ' . $token,
165 'Content-Length: ' . strlen( $content ),
166 'Content-Type: application/atom+xml',
167 'X-Upload-Content-Type: application/octet-stream',
168 'X-Upload-Content-Length: ' . $size,
169 'GData-Version: 3.0'
170 );
171
172 $context = array(
173 'http' => array(
174 'ignore_errors' => true,
175 'follow_location' => false,
176 'method' => 'POST',
177 'header' => join( "\r\n", $header ),
178 'content' => $content
179 )
180 );
181
182 $url = $this->get_resumable_create_media_link( $token, $parent );
183 if ( $url ) {
184 $url .= '?convert=false'; // needed to upload a file
185 $this->log("Google Drive: resumable create media link: ".$url);
186 } else {
187 $this->log('Could not retrieve resumable create media link.', __FILE__, __LINE__ );
188 return false;
189 }
190
191 $result = @file_get_contents( $url, false, stream_context_create( $context ) );
192
193 if ( $result !== FALSE ) {
194 if ( strpos( $response = array_shift( $http_response_header ), '200' ) ) {
195 $response_header = array();
196 foreach ( $http_response_header as $header_line ) {
197 list( $key, $value ) = explode( ':', $header_line, 2 );
198 $response_header[trim( $key )] = trim( $value );
199 #$this->log("Google Drive: header: ".trim($key).": ".trim($value));
200 }
201 if ( isset( $response_header['Location'] ) ) {
202 $next_location = $response_header['Location'];
203 $pointer = 0;
204 # 1Mb
205 $max_chunk_size = 524288*2;
206 while ( $pointer < $size - 1 ) {
207 $this->log(basename($file).": Google Drive upload: pointer=$pointer (size=$size)");
208 $chunk = file_get_contents( $file, false, NULL, $pointer, $max_chunk_size );
209 $next_location = $this->upload_chunk( $next_location, $chunk, $pointer, $size, $token );
210 if( $next_location === false ) {
211 $this->log("Google Drive Upload: next_location is false (pointer: $pointer; chunk length: ".strlen($chunk).")");
212 return false;
213 }
214 $pointer += strlen( $chunk );
215 // if object it means we have our simpleXMLElement response
216 if ( is_object( $next_location ) ) {
217 // return resource Id
218 $this->log("Google Drive Upload: Success");
219 # Google Drive returns 501 not implemented for me for some reason instead of expected result...
220 #return substr( $next_location->children( "http://schemas.google.com/g/2005" )->resourceId, 5 );
221 return true;
222 }
223
224 }
225 }
226 }
227 else {
228 $this->log( 'Bad response: ' . $response . ' Response header: ' . var_export( $response_header, true ) . ' Response body: ' . $result . ' Request URL: ' . $url, __FILE__, __LINE__ );
229 return false;
230 }
231 }
232 else {
233 $this->log( 'Unable to request file from ' . $url, __FILE__, __LINE__ );
234 }
235
236 return true;
237
238 }
239
240 /**
241 * Get the resumable-create-media link needed to upload files
242 *
243 * @param string $token The Google Account access token
244 * @param string $parent The Id of the folder where the upload is to be made. Default is empty string.
245 * @return string|boolean Returns a link on success, FALSE on failure.
246 */
247 function get_resumable_create_media_link( $token, $parent = '' ) {
248 $header = array(
249 'Authorization: Bearer ' . $token,
250 'GData-Version: 3.0'
251 );
252 $context = array(
253 'http' => array(
254 'ignore_errors' => true,
255 'method' => 'GET',
256 'header' => join( "\r\n", $header )
257 )
258 );
259 $url = 'https://docs.google.com/feeds/default/private/full';
260
261 if ( $parent ) {
262 $url .= '/' . $parent;
263 }
264
265 $result = @file_get_contents( $url, false, stream_context_create( $context ) );
266
267 if ( $result !== false ) {
268 $xml = simplexml_load_string( $result );
269 if ( $xml === false ) {
270 $this->log( 'Could not create SimpleXMLElement from ' . $result, __FILE__, __LINE__ );
271 return false;
272 }
273 else {
274 foreach ( $xml->link as $link ) {
275 if ( $link['rel'] == 'http://schemas.google.com/g/2005#resumable-create-media' ) { return $link['href']; }
276 }
277 }
278 }
279 return false;
280 }
281
282
283 /**
284 * Handles the upload to Google Drive of a single chunk of a file
285 *
286 * @param string $location URL where the chunk needs to be uploaded
287 * @param string $chunk Part of the file to upload
288 * @param integer $pointer The byte number marking the beginning of the chunk in file
289 * @param integer $size The size of the file the chunk is part of, in bytes
290 * @param string $token Google Account access token
291 * @return string|boolean The funcion returns the location where the next chunk needs to be uploaded, TRUE if the last chunk was uploaded or FALSE on failure
292 */
293 function upload_chunk( $location, $chunk, $pointer, $size, $token ) {
294 $chunk_size = strlen( $chunk );
295 $bytes = (string)$pointer . '-' . (string)($pointer + $chunk_size - 1) . '/' . (string)$size;
296 $this->log("Google Drive chunk: location=$location, length=$chunk_size, range=$bytes");
297 $header = array(
298 'Authorization: Bearer ' . $token,
299 'Content-Length: ' . $chunk_size,
300 'Content-Type: application/octet-stream',
301 'Content-Range: bytes ' . $bytes,
302 'GData-Version: 3.0'
303 );
304 $context = array(
305 'http' => array(
306 'ignore_errors' => true,
307 'follow_location' => false,
308 'method' => 'PUT',
309 'header' => join( "\r\n", $header ),
310 'content' => $chunk
311 )
312 );
313
314 $result = @file_get_contents( $location, false, stream_context_create( $context ) );
315
316 if ( isset( $http_response_header ) ) {
317 $response = array_shift( $http_response_header );
318 $headers = array();
319 foreach ( $http_response_header as $header_line ) {
320 list( $key, $value ) = explode( ':', $header_line, 2 );
321 $headers[trim( $key )] = trim( $value );
322 }
323
324 if ( strpos( $response, '308' ) ) {
325 if ( isset( $headers['Location'] ) ) {
326 $this->log('Google Drive: 308 response: '.$headers['Location']);
327 return $headers['Location'];
328 }
329 else {
330 $this->log('Google Drive 308 response: no location header: '.$location);
331 return $location;
332 }
333 }
334 elseif ( strpos( $response, '201' ) ) {
335 #$this->log("Google Drive response: ".$result);
336 $xml = simplexml_load_string( $result );
337 if ( $xml === false ) {
338 $this->log('ERROR: Could not create SimpleXMLElement from ' . $result, __FILE__, __LINE__ );
339 return false;
340 }
341 else {
342 return $xml;
343 }
344 }
345 else {
346 $this->log('ERROR: Bad response: ' . $response, __FILE__, __LINE__ );
347 return false;
348 }
349 }
350 else {
351 $this->log('ERROR: Received no response from ' . $location . ' while trying to upload bytes ' . $bytes );
352 return false;
353 }
354 }
355
356 function googledrive_delete_file( $file, $token) {
357 $this->log("Delete from Google Drive: $file: not yet implemented");
358 # TODO - somehow, turn this into a Gdata resource ID, then despatch it to googledrive_delete_file_byid
359 return;
360 }
361
362 /**
363 * Deletes a file from Google Drive
364 *
365 * @param string $id Gdata resource Id of the file to be deleted
366 * @param string $token Google Account access token
367 * @return boolean Returns TRUE on success, FALSE on failure
368 */
369 function googledrive_delete_file_byid( $id, $token ) {
370 $header = array(
371 'If-Match: *',
372 'Authorization: Bearer ' . $token,
373 'GData-Version: 3.0'
374 );
375 $context = array(
376 'http' => array(
377 'method' => 'DELETE',
378 'header' => join( "\r\n", $header )
379 )
380 );
381 stream_context_set_default( $context );
382 $headers = get_headers( 'https://docs.google.com/feeds/default/private/full/' . $id . '?delete=true',1 );
383
384 if ( strpos( $headers[0], '200' ) ) { return true; }
385 return false;
386 }
387
388 /**
389 * Get a Google account refresh token using the code received from auth_request
390 */
391 function auth_token() {
392 if( isset( $_GET['code'] ) ) {
393 $context = array(
394 'http' => array(
395 'timeout' => 30,
396 'method' => 'POST',
397 'header' => 'Content-type: application/x-www-form-urlencoded',
398 'content' => http_build_query( array(
399 'code' => $_GET['code'],
400 'client_id' => get_option('updraft_googledrive_clientid'),
401 'client_secret' => get_option('updraft_googledrive_secret'),
402 'redirect_uri' => admin_url('options-general.php?page=updraftplus&action=auth'),
403 'grant_type' => 'authorization_code'
404 ) )
405 )
406 );
407 $result = @file_get_contents('https://accounts.google.com/o/oauth2/token', false, stream_context_create($context));
408 # Oddly, sometimes fails and then trying again works...
409 /*
410 if (!$result) { sleep(1); $result = @file_get_contents('https://accounts.google.com/o/oauth2/token', false, stream_context_create($context));}
411 if (!$result) { sleep(1); $result = @file_get_contents('https://accounts.google.com/o/oauth2/token', false, stream_context_create($context));}
412 */
413 if($result) {
414 $result = json_decode( $result, true );
415 if ( isset( $result['refresh_token'] ) ) {
416 update_option('updraft_googledrive_token',$result['refresh_token']); // Save token
417 header('Location: '.admin_url('options-general.php?page=updraftplus&message=' . __( 'Authorization was successful.', 'updraftplus' ) ) );
418 }
419 else {
420 header('Location: '.admin_url('options-general.php?page=updraftplus&error=' . __( 'No refresh token was received!', 'updraftplus' ) ) );
421 }
422 } else {
423 header('Location: '.admin_url('options-general.php?page=updraftplus&error=' . __( 'Bad response!', 'backup' ) ) );
424 }
425 }
426 else {
427 header('Location: '.admin_url('options-general.php?page=updraftplus&error=' . __( 'Authorisation failed!', 'backup' ) ) );
428 }
429 }
430
431 /**
432 * Revoke a Google account refresh token
433 */
434 function auth_revoke() {
435 @file_get_contents( 'https://accounts.google.com/o/oauth2/revoke?token=' . get_option('updraft_googledrive_token') );
436 update_option('updraft_googledrive_token','');
437 header( 'Location: '.admin_url( 'options-general.php?page=updraftplus&message=' . __( 'Authorization revoked.', 'backup' ) ) );
438 }
439
440 # Adds the settings link under the plugin on the plugin screen.
441 function plugin_action_links($links, $file) {
442 if ($file == plugin_basename(__FILE__)){
443 $settings_link = '<a href="'.site_url().'/wp-admin/options-general.php?page=updraftplus">'.__("Settings", "UpdraftPlus").'</a>';
444 array_unshift($links, $settings_link);
445 $settings_link = '<a href="http://david.dw-perspective.org.uk/donate">'.__("Donate","UpdraftPlus").'</a>';
446 array_unshift($links, $settings_link);
447 }
448 return $links;
449 }
450
451 function backup_time_nonce() {
452 $this->backup_time = time();
453 $this->nonce = substr(md5(time().rand()),20);
454 }
455
456 # Logs the given line, adding date stamp and newline
457 function log($line) {
458 if ($this->logfile_handle) {
459 fwrite($this->logfile_handle,date('r')." ".$line."\n");
460 }
461 }
462
463 function backup_all() {
464 $this->backup(true,true);
465 }
466
467 function backup_files() {
468 # Note that the "false" for database gets over-ridden automatically if they turn out to have the same schedules
469 $this->backup(true,false);
470 }
471
472 function backup_database() {
473 # Note that nothing will happen if the file backup had the same schedule
474 $this->backup(false,true);
475 }
476
477 //scheduled wp-cron events can have a race condition here if page loads are coming fast enough, but there's nothing we can do about it.
478 function backup($backup_files, $backup_database) {
479
480 //generate backup information
481 $this->backup_time_nonce();
482
483 //set log file name and open log file
484 $updraft_dir = $this->backups_dir_location();
485 $this->logfile_name = $updraft_dir. "/log." . $this->nonce . ".txt";
486 // Use append mode in case it already exists
487 $this->logfile_handle = fopen($this->logfile_name, 'a');
488
489 // Log some information that may be helpful
490 global $wp_version;
491 $this->log("PHP version: ".phpversion()." WordPress version: ".$wp_version." Backup files: $backup_files (schedule: ".get_option('updraft_interval','unset').") Backup DB: $backup_database (schedule: ".get_option('updraft_interval_database','unset').")");
492
493 # If the files and database schedules are the same, and if this the file one, then we rope in database too.
494 # On the other hand, if the schedules were the same and this was the database run, then there is nothing to do.
495 if (get_option('updraft_interval') == get_option('updraft_interval_database') || get_option('updraft_interval_database','xyz') == 'xyz' ) {
496 if ($backup_files == true)
497 { $backup_database = true; }
498 else
499 { $backup_database = false; }
500 }
501
502 $this->log("Processed schedules. Tasks now: Backup files: $backup_files Backup DB: $backup_database");
503
504 # Possibly now nothing is to be done, except to close the log file
505 if ($backup_files || $backup_database) {
506
507 $backup_contains = "";
508
509 $backup_array = array();
510
511 //backup directories and return a numerically indexed array of file paths to the backup files
512 if ($backup_files) {
513 $this->log("Beginning backup of directories");
514 $backup_array = $this->backup_dirs();
515 $backup_contains = "Files only (no database)";
516 }
517
518 //backup DB and return string of file path
519 if ($backup_database) {
520 $this->log("Beginning backup of database");
521 $db_backup = $this->backup_db();
522 //add db path to rest of files
523 if(is_array($backup_array)) { $backup_array['db'] = $db_backup; }
524 $backup_contains = ($backup_files) ? "Files and database" : "Database only (no files)";
525 }
526
527 //save this to our history so we can track backups for the retain feature
528 $this->log("Saving backup history");
529 $this->save_backup_history($backup_array);
530
531 //cloud operations (S3,Google Drive,FTP,email,nothing)
532 //this also calls the retain feature at the end (done in this method to reuse existing cloud connections)
533 if(is_array($backup_array) && count($backup_array) >0) {
534 $this->log("Beginning dispatch of backup to remote");
535 $this->cloud_backup($backup_array);
536 }
537 //delete local files if the pref is set
538 foreach($backup_array as $file) {
539 $this->delete_local($file);
540 }
541
542 //save the last backup info, including errors, if any
543 $this->log("Saving last backup information into WordPress db");
544 $this->save_last_backup($backup_array);
545
546 if(get_option('updraft_email') != "" && get_option('updraft_service') != 'email') {
547 $sendmail_to = get_option('updraft_email');
548 $this->log("Sending email report to: ".$sendmail_to);
549 $append_log = "";
550 if(get_option('updraft_debug_mode') && $this->logfile_name != "") {
551 $append_log .= "\r\nLog contents:\r\n".file_get_contents($this->logfile_name);
552 }
553 wp_mail($sendmail_to,'Backed up: '.get_bloginfo('name').' (UpdraftPlus) '.date('Y-m-d H:i',time()),'Site: '.site_url()."\r\nUpdraftPlus WordPress backup is complete.\r\nBackup contains: $backup_contains\r\n\r\n".$this->wordshell_random_advert(0)."\r\n".$append_log);
554 }
555 }
556
557 // Close log file
558 @fclose($this->logfile_handle);
559 if (!get_option('updraft_debug_mode')) { @unlink($this->logfile_name); }
560 }
561
562 function save_last_backup($backup_array) {
563 $success = (empty($this->errors))?1:0;
564 $last_backup = array('backup_time'=>$this->backup_time,'backup_array'=>$backup_array,'success'=>$success,'errors'=>$this->errors);
565 update_option('updraft_last_backup',$last_backup);
566 }
567
568 function cloud_backup($backup_array) {
569 switch(get_option('updraft_service')) {
570 case 's3':
571 @set_time_limit(900);
572 $this->log("Cloud backup: S3");
573 if (count($backup_array) >0) { $this->s3_backup($backup_array); }
574 break;
575 case 'googledrive':
576 @set_time_limit(900);
577 $this->log("Cloud backup: Google Drive");
578 if (count($backup_array) >0) { $this->googledrive_backup($backup_array); }
579 break;
580 case 'ftp':
581 @set_time_limit(900);
582 $this->log("Cloud backup: FTP");
583 if (count($backup_array) >0) { $this->ftp_backup($backup_array); }
584 break;
585 case 'email':
586 @set_time_limit(900);
587 $this->log("Cloud backup: Email");
588 //files can easily get way too big for this...
589 foreach($backup_array as $type=>$file) {
590 $fullpath = trailingslashit(get_option('updraft_dir')).$file;
591 wp_mail(get_option('updraft_email'),"WordPress Backup ".date('Y-m-d H:i',$this->backup_time),"Backup is of the $type. Be wary; email backups may fail because of file size limitations on mail servers.",null,array($fullpath));
592 }
593 //we don't break here so it goes and executes all the default behavior below as well. this gives us retain behavior for email
594 default:
595 $this->prune_retained_backups("local");
596 break;
597 }
598 }
599
600 // Carries out retain behaviour. Pass in a valid S3 or FTP object and path if relevant.
601 function prune_retained_backups($updraft_service,$remote_object,$remote_path) {
602 $this->log("Retain: beginning examination of existing backup sets");
603 $updraft_retain = get_option('updraft_retain');
604 // Number of backups to retain
605 $retain = (isset($updraft_retain))?get_option('updraft_retain'):1;
606 $this->log("Retain: user setting: number to retain = $retain");
607 // Returns an array, most recent first, of backup sets
608 $backup_history = $this->get_backup_history();
609 $db_backups_found = 0; $file_backups_found = 0;
610 $this->log("Number of backup sets in history: ".count($backup_history));
611 foreach ($backup_history as $backup_datestamp => $backup_to_examine) {
612 // $backup_to_examine is an array of file names, keyed on db/plugins/themes/uploads
613 // The new backup_history array is saved afterwards, so remember to unset the ones that are to be deleted
614 $this->log("Examining backup set with datestamp: $backup_datestamp");
615 if (isset($backup_to_examine['db'])) {
616 $db_backups_found++;
617 $this->log("$backup_datestamp: this set includes a database (".$backup_to_examine['db']."); db count is now $db_backups_found");
618 if ($db_backups_found > $retain) {
619 $this->log("$backup_datestamp: over retain limit; will delete this database");
620 $file = $backup_to_examine['db'];
621 $this->log("$backup_datestamp: Delete this file: $file");
622 if ($file != '') {
623 $fullpath = trailingslashit(get_option('updraft_dir')).$file;
624 @unlink($fullpath); //delete it if it's locally available
625 if ($updraft_service == "s3") {
626 $this->log("$backup_datestamp: Delete remote: s3://$remote_path/$file");
627 if (!$remote_object->deleteObject($remote_path, $file)) {
628 $this->error("S3 Error: Failed to delete object $file. Error was ".$php_errormsg);
629 }
630 } elseif ($updraft_service == "ftp") {
631 $this->log("$backup_datestamp: Delete remote ftp: $remote_path/$file");
632 @$remote_object->delete($remote_path.$file);
633 } elseif ($updraft_service == "googledrive") {
634 $this->log("$backup_datestamp: Delete remote file from Google Drive: $remote_path/$file");
635 $this->googledrive_delete_file($remote_path.'/'.$file,$remote_object);
636 }
637 }
638 unset($backup_to_examine['db']);
639 }
640 }
641 if (isset($backup_to_examine['plugins']) || isset($backup_to_examine['themes']) || isset($backup_to_examine['uploads'])) {
642 $file_backups_found++;
643 $this->log("$backup_datestamp: this set includes files; fileset count is now $file_backups_found");
644 if ($file_backups_found > $retain) {
645 $this->log("$backup_datestamp: over retain limit; will delete this file set");
646 $file = isset($backup_to_examine['plugins']) ? $backup_to_examine['plugins'] : "";
647 $file2 = isset($backup_to_examine['themes']) ? $backup_to_examine['themes'] : "";
648 $file3 = isset($backup_to_examine['uploads']) ? $backup_to_examine['uploads'] : "";
649 if ($file) {
650 $this->log("$backup_datestamp: Delete this file: $file");
651 $fullpath = trailingslashit(get_option('updraft_dir')).$file;
652 @unlink($fullpath); //delete it if it's locally available
653 if ($updraft_service == "s3") {
654 $this->log("$backup_datestamp: Delete remote: s3://$remote_path/$file");
655 if (!$remote_object->deleteObject($remote_path, $file)) {
656 $this->error("S3 Error: Failed to delete object $file. Error was ".$php_errormsg);
657 }
658 } elseif ($updraft_service == "ftp") {
659 $this->log("$backup_datestamp: Delete remote ftp: $remote_path/$file");
660 @$remote_object->delete($remote_path.$file);
661 } elseif ($updraft_service == "googledrive") {
662 $this->log("$backup_datestamp: Delete remote file from Google Drive: $remote_path/$file");
663 $this->googledrive_delete_file($remote_path.'/'.$file,$remote_object);
664 }
665 }
666 if ($file2) {
667 $this->log("$backup_datestamp: Delete this file: $file2");
668 $fullpath = trailingslashit(get_option('updraft_dir')).$file2;
669 @unlink($fullpath); //delete it if it's locally available
670 if ($updraft_service == "s3") {
671 $this->log("$backup_datestamp: Delete remote: s3://$remote_path/$file2");
672 if (!$remote_object->deleteObject($remote_path, $file2)) {
673 $this->error("S3 Error: Failed to delete object $file2. Error was ".$php_errormsg);
674 }
675 } elseif ($updraft_service == "ftp") {
676 $this->log("$backup_datestamp: Delete remote ftp: $remote_path/$file2");
677 @$remote_object->delete($remote_path.$file2);
678 } elseif ($updraft_service == "googledrive") {
679 $this->log("$backup_datestamp: Delete remote file from Google Drive: $remote_path/$file");
680 $this->googledrive_delete_file($remote_path.'/'.$file,$remote_object);
681 }
682
683 }
684 if ($file3) {
685 $this->log("$backup_datestamp: Delete this file: $file3");
686 $fullpath = trailingslashit(get_option('updraft_dir')).$file3;
687 @unlink($fullpath); //delete it if it's locally available
688 if ($updraft_service == "s3") {
689 $this->log("$backup_datestamp: Delete remote: s3://$remote_path/$file3");
690 if (!$remote_object->deleteObject($remote_path, $file3)) {
691 $this->error("S3 Error: Failed to delete object $file3. Error was ".$php_errormsg);
692 }
693 } elseif ($updraft_service == "ftp") {
694 $this->log("$backup_datestamp: Delete remote ftp: $remote_path/$file3");
695 @$remote_object->delete($remote_path.$file3);
696 } elseif ($updraft_service == "googledrive") {
697 $this->log("$backup_datestamp: Delete remote file from Google Drive: $remote_path/$file");
698 $this->googledrive_delete_file($remote_path.'/'.$file,$remote_object);
699 }
700 }
701 unset($backup_to_examine['plugins']);
702 unset($backup_to_examine['themes']);
703 unset($backup_to_examine['uploads']);
704 }
705 }
706 // Delete backup set completely if empty, o/w just remove DB
707 if (count($backup_to_examine)==0) {
708 $this->log("$backup_datestamp: this backup set is now empty; will remove from history");
709 unset($backup_history[$backup_datestamp]);
710 } else {
711 $this->log("$backup_datestamp: this backup set remains non-empty; will retain in history");
712 $backup_history[$backup_datestamp] = $backup_to_examine;
713 }
714 }
715 $this->log("Retain: saving new backup history (sets now: ".count($backup_history).") and finishing retain operation");
716 update_option('updraft_backup_history',$backup_history);
717 }
718
719 function s3_backup($backup_array) {
720 if(!class_exists('S3')) {
721 require_once(dirname(__FILE__).'/includes/S3.php');
722 }
723 $s3 = new S3(get_option('updraft_s3_login'), get_option('updraft_s3_pass'));
724 $bucket_name = untrailingslashit(get_option('updraft_s3_remote_path'));
725 if (@$s3->putBucket($bucket_name, S3::ACL_PRIVATE)) {
726 foreach($backup_array as $file) {
727 $fullpath = trailingslashit(get_option('updraft_dir')).$file;
728 if (!$s3->putObjectFile($fullpath, $bucket_name, $file)) {
729 $this->error("S3 Error: Failed to upload $fullpath. Error was ".$php_errormsg);
730 }
731 }
732 $this->prune_retained_backups('s3',$s3,$bucket_name);
733 } else {
734 $this->error("S3 Error: Failed to create bucket $bucket_name. Error was ".$php_errormsg);
735 }
736 }
737
738 function googledrive_backup($backup_array) {
739 if ( $access = $this->access_token( get_option('updraft_googledrive_token'), get_option('updraft_googledrive_clientid'), get_option('updraft_googledrive_secret') ) ) {
740 foreach ($backup_array as $file) {
741 $file_path = trailingslashit(get_option('updraft_dir')).$file;
742 $file_name = basename($file_path);
743 $this->log("$file_name: Attempting to upload to Google Drive");
744 $timer_start = microtime( true );
745 if ( $id = $this->googledrive_upload_file( $file_path, $file_name, get_option('updraft_googledrive_remotepath'), $access ) ) {
746 $this->log('OK: Archive ' . $file_name . ' uploaded to Google Drive in ' . ( round(microtime( true ) - $timer_start,2) ) . ' seconds' );
747 } else {
748 $this->log("ERROR: $file_name: Failed to upload to Google Drive" );
749 }
750 }
751 $this->prune_retained_backups("googledrive",$access,get_option('updraft_googledrive_remotepath'));
752 } else {
753 $this->log('ERROR: Did not receive an access token from Google', __FILE__, __LINE__ );
754 }
755 }
756
757 function ftp_backup($backup_array) {
758 if( !class_exists('ftp_wrapper')) {
759 require_once(dirname(__FILE__).'/includes/ftp.class.php');
760 }
761 //handle SSL and errors at some point TODO
762 $ftp = new ftp_wrapper(get_option('updraft_server_address'),get_option('updraft_ftp_login'),get_option('updraft_ftp_pass'));
763 $ftp->passive = true;
764 $ftp->connect();
765 //$ftp->make_dir(); we may need to recursively create dirs? TODO
766
767 $ftp_remote_path = trailingslashit(get_option('updraft_ftp_remote_path'));
768 foreach($backup_array as $file) {
769 $fullpath = trailingslashit(get_option('updraft_dir')).$file;
770 $ftp->put($fullpath,$ftp_remote_path.$file,FTP_BINARY);
771 }
772 $this->prune_retained_backups("ftp",$ftp,$ftp_remote_path);
773 }
774
775 function delete_local($file) {
776 if(get_option('updraft_delete_local')) {
777 $this->log("Deleting local file: $file");
778 //need error checking so we don't delete what isn't successfully uploaded?
779 $fullpath = trailingslashit(get_option('updraft_dir')).$file;
780 return unlink($fullpath);
781 }
782 return true;
783 }
784
785 function backup_dirs() {
786 if(!$this->backup_time) {
787 $this->backup_time_nonce();
788 }
789 $wp_themes_dir = WP_CONTENT_DIR.'/themes';
790 $wp_upload_dir = wp_upload_dir();
791 $wp_upload_dir = $wp_upload_dir['basedir'];
792 $wp_plugins_dir = WP_PLUGIN_DIR;
793 if(!class_exists('PclZip')) {
794 if (file_exists(ABSPATH.'/wp-admin/includes/class-pclzip.php')) {
795 require_once(ABSPATH.'/wp-admin/includes/class-pclzip.php');
796 }
797 }
798 $updraft_dir = $this->backups_dir_location();
799 if(!is_writable($updraft_dir)) {
800 $this->error('Backup directory is not writable.','fatal');
801 }
802 //get the blog name and rip out all non-alphanumeric chars other than _
803 $blog_name = str_replace(' ','_',get_bloginfo());
804 $blog_name = preg_replace('/[^A-Za-z0-9_]/','', $blog_name);
805 if(!$blog_name) {
806 $blog_name = 'non_alpha_name';
807 }
808
809 $backup_file_base = $updraft_dir.'/backup_'.date('Y-m-d-Hi',$this->backup_time).'_'.$blog_name.'_'.$this->nonce;
810
811 $backup_array = array();
812
813 # Plugins
814 @set_time_limit(900);
815 if (get_option('updraft_include_plugins', true)) {
816 $this->log("Beginning backup of plugins");
817 $plugins = new PclZip($backup_file_base.'-plugins.zip');
818 if (!$plugins->create($wp_plugins_dir,PCLZIP_OPT_REMOVE_PATH,WP_CONTENT_DIR)) {
819 $this->error('Could not create plugins zip. Error was '.$php_errmsg,'fatal');
820 }
821 $backup_array['plugins'] = basename($backup_file_base.'-plugins.zip');
822 } else {
823 $this->log("No backup of plugins: excluded by user's options");
824 }
825
826 # Themes
827 @set_time_limit(900);
828 if (get_option('updraft_include_themes', true)) {
829 $this->log("Beginning backup of themes");
830 $themes = new PclZip($backup_file_base.'-themes.zip');
831 if (!$themes->create($wp_themes_dir,PCLZIP_OPT_REMOVE_PATH,WP_CONTENT_DIR)) {
832 $this->error('Could not create themes zip. Error was '.$php_errmsg,'fatal');
833 }
834 $backup_array['themes'] = basename($backup_file_base.'-themes.zip');
835 } else {
836 $this->log("No backup of themes: excluded by user's options");
837 }
838
839 # Uploads
840 @set_time_limit(900);
841 if (get_option('updraft_include_uploads', true)) {
842 $this->log("Beginning backup of uploads");
843 $uploads = new PclZip($backup_file_base.'-uploads.zip');
844 if (!$uploads->create($wp_upload_dir,PCLZIP_OPT_REMOVE_PATH,WP_CONTENT_DIR)) {
845 $this->error('Could not create uploads zip. Error was '.$php_errmsg,'fatal');
846 }
847 $backup_array['uploads'] = basename($backup_file_base.'-uploads.zip');
848 } else {
849 $this->log("No backup of uploads: excluded by user's options");
850 }
851 return $backup_array;
852 }
853
854 function save_backup_history($backup_array) {
855 //this stores full paths right now. should probably concatenate with ABSPATH to make it easier to move sites
856 if(is_array($backup_array)) {
857 $backup_history = get_option('updraft_backup_history');
858 $backup_history = (is_array($backup_history)) ? $backup_history : array();
859 $backup_history[$this->backup_time] = $backup_array;
860 update_option('updraft_backup_history',$backup_history);
861 } else {
862 $this->error('Could not save backup history because we have no backup array. Backup probably failed.');
863 }
864 }
865
866 function get_backup_history() {
867 //$backup_history = get_option('updraft_backup_history');
868 //by doing a raw DB query to get the most up-to-date data from this option we slightly narrow the window for the multiple-cron race condition
869 global $wpdb;
870 $backup_history = @unserialize($wpdb->get_var($wpdb->prepare("SELECT option_value from $wpdb->options WHERE option_name='updraft_backup_history'")));
871 if(is_array($backup_history)) {
872 krsort($backup_history); //reverse sort so earliest backup is last on the array. this way we can array_pop
873 } else {
874 $backup_history = array();
875 }
876 return $backup_history;
877 }
878
879
880 /*START OF WB-DB-BACKUP BLOCK*/
881
882 function backup_db() {
883
884 $total_tables = 0;
885
886 global $table_prefix, $wpdb;
887 if(!$this->backup_time) {
888 $this->backup_time_nonce();
889 }
890
891 $all_tables = $wpdb->get_results("SHOW TABLES", ARRAY_N);
892 $all_tables = array_map(create_function('$a', 'return $a[0];'), $all_tables);
893
894 $updraft_dir = $this->backups_dir_location();
895 //get the blog name and rip out all non-alphanumeric chars other than _
896 $blog_name = str_replace(' ','_',get_bloginfo());
897 $blog_name = preg_replace('/[^A-Za-z0-9_]/','', $blog_name);
898 if(!$blog_name) {
899 $blog_name = 'non_alpha_name';
900 }
901
902 $backup_file_base = $updraft_dir.'/backup_'.date('Y-m-d-Hi',$this->backup_time).'_'.$blog_name.'_'.$this->nonce;
903 if (is_writable($updraft_dir)) {
904 if (function_exists('gzopen')) {
905 $this->dbhandle = @gzopen($backup_file_base.'-db.gz','w');
906 } else {
907 $this->dbhandle = @fopen($backup_file_base.'-db.gz', 'w');
908 }
909 if(!$this->dbhandle) {
910 //$this->error(__('Could not open the backup file for writing!','wp-db-backup'));
911 }
912 } else {
913 //$this->error(__('The backup directory is not writable!','wp-db-backup'));
914 }
915
916 //Begin new backup of MySql
917 $this->stow("# " . __('WordPress MySQL database backup','wp-db-backup') . "\n");
918 $this->stow("#\n");
919 $this->stow("# " . sprintf(__('Generated: %s','wp-db-backup'),date("l j. F Y H:i T")) . "\n");
920 $this->stow("# " . sprintf(__('Hostname: %s','wp-db-backup'),DB_HOST) . "\n");
921 $this->stow("# " . sprintf(__('Database: %s','wp-db-backup'),$this->backquote(DB_NAME)) . "\n");
922 $this->stow("# --------------------------------------------------------\n");
923
924
925 if (defined("DB_CHARSET")) {
926 $this->stow("/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;\n");
927 $this->stow("/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;\n");
928 $this->stow("/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;\n");
929 $this->stow("/*!40101 SET NAMES " . DB_CHARSET . " */;\n");
930 }
931 $this->stow("/*!40101 SET foreign_key_checks = 0 */;\n");
932
933 foreach ($all_tables as $table) {
934 $total_tables++;
935 // Increase script execution time-limit to 15 min for every table.
936 if ( !ini_get('safe_mode')) @set_time_limit(15*60);
937 if ( strpos($table, $table_prefix) == 0 ) {
938 // Create the SQL statements
939 $this->stow("# --------------------------------------------------------\n");
940 $this->stow("# " . sprintf(__('Table: %s','wp-db-backup'),$this->backquote($table)) . "\n");
941 $this->stow("# --------------------------------------------------------\n");
942 $this->backup_table($table);
943 } else {
944 $this->stow("# --------------------------------------------------------\n");
945 $this->stow("# " . sprintf(__('Skipping non-WP table: %s','wp-db-backup'),$this->backquote($table)) . "\n");
946 $this->stow("# --------------------------------------------------------\n");
947 }
948 }
949
950 if (defined("DB_CHARSET")) {
951 $this->stow("/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;\n");
952 $this->stow("/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;\n");
953 $this->stow("/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;\n");
954 }
955
956 $this->close($this->dbhandle);
957
958 if (count($this->errors)) {
959 return false;
960 } else {
961 # Encrypt, if requested
962 $encryption = get_option('updraft_encryptionphrase');
963 if (strlen($encryption) > 0) {
964 $this->log("Database: applying encryption");
965 $encryption_error = 0;
966 require_once(dirname(__FILE__).'/includes/Rijndael.php');
967 $rijndael = new Crypt_Rijndael();
968 $rijndael->setKey($encryption);
969 $in_handle = @fopen($backup_file_base.'-db.gz','r');
970 $buffer = "";
971 while (!feof ($in_handle)) {
972 $buffer .= fread($in_handle, 16384);
973 }
974 fclose ($in_handle);
975 $out_handle = @fopen($backup_file_base.'-db.gz.crypt','w');
976 if (!fwrite($out_handle, $rijndael->encrypt($buffer))) {$encryption_error = 1;}
977 fclose ($out_handle);
978 if (0 == $encryption_error) {
979 # Delete unencrypted file
980 @unlink($backup_file_base.'-db.gz');
981 return basename($backup_file_base.'-db.gz.crypt');
982 } else {
983 $this->error("Encryption error occurred when encrypting database. Aborted.");
984 }
985 } else {
986 return basename($backup_file_base.'-db.gz');
987 }
988 }
989 $this->log("Total database tables backed up: $total_tables");
990
991 } //wp_db_backup
992
993 /**
994 * Taken partially from phpMyAdmin and partially from
995 * Alain Wolf, Zurich - Switzerland
996 * Website: http://restkultur.ch/personal/wolf/scripts/db_backup/
997 * Modified by Scott Merrill (http://www.skippy.net/)
998 * to use the WordPress $wpdb object
999 * @param string $table
1000 * @param string $segment
1001 * @return void
1002 */
1003 function backup_table($table, $segment = 'none') {
1004 global $wpdb;
1005
1006 $total_rows = 0;
1007
1008 $table_structure = $wpdb->get_results("DESCRIBE $table");
1009 if (! $table_structure) {
1010 //$this->error(__('Error getting table details','wp-db-backup') . ": $table");
1011 return false;
1012 }
1013
1014 if(($segment == 'none') || ($segment == 0)) {
1015 // Add SQL statement to drop existing table
1016 $this->stow("\n\n");
1017 $this->stow("#\n");
1018 $this->stow("# " . sprintf(__('Delete any existing table %s','wp-db-backup'),$this->backquote($table)) . "\n");
1019 $this->stow("#\n");
1020 $this->stow("\n");
1021 $this->stow("DROP TABLE IF EXISTS " . $this->backquote($table) . ";\n");
1022
1023 // Table structure
1024 // Comment in SQL-file
1025 $this->stow("\n\n");
1026 $this->stow("#\n");
1027 $this->stow("# " . sprintf(__('Table structure of table %s','wp-db-backup'),$this->backquote($table)) . "\n");
1028 $this->stow("#\n");
1029 $this->stow("\n");
1030
1031 $create_table = $wpdb->get_results("SHOW CREATE TABLE $table", ARRAY_N);
1032 if (false === $create_table) {
1033 $err_msg = sprintf(__('Error with SHOW CREATE TABLE for %s.','wp-db-backup'), $table);
1034 //$this->error($err_msg);
1035 $this->stow("#\n# $err_msg\n#\n");
1036 }
1037 $this->stow($create_table[0][1] . ' ;');
1038
1039 if (false === $table_structure) {
1040 $err_msg = sprintf(__('Error getting table structure of %s','wp-db-backup'), $table);
1041 //$this->error($err_msg);
1042 $this->stow("#\n# $err_msg\n#\n");
1043 }
1044
1045 // Comment in SQL-file
1046 $this->stow("\n\n");
1047 $this->stow("#\n");
1048 $this->stow('# ' . sprintf(__('Data contents of table %s','wp-db-backup'),$this->backquote($table)) . "\n");
1049 $this->stow("#\n");
1050 }
1051
1052 if(($segment == 'none') || ($segment >= 0)) {
1053 $defs = array();
1054 $ints = array();
1055 foreach ($table_structure as $struct) {
1056 if ( (0 === strpos($struct->Type, 'tinyint')) ||
1057 (0 === strpos(strtolower($struct->Type), 'smallint')) ||
1058 (0 === strpos(strtolower($struct->Type), 'mediumint')) ||
1059 (0 === strpos(strtolower($struct->Type), 'int')) ||
1060 (0 === strpos(strtolower($struct->Type), 'bigint')) ) {
1061 $defs[strtolower($struct->Field)] = ( null === $struct->Default ) ? 'NULL' : $struct->Default;
1062 $ints[strtolower($struct->Field)] = "1";
1063 }
1064 }
1065
1066
1067 // Batch by $row_inc
1068 if ( ! defined('ROWS_PER_SEGMENT') ) {
1069 define('ROWS_PER_SEGMENT', 100);
1070 }
1071
1072 if($segment == 'none') {
1073 $row_start = 0;
1074 $row_inc = ROWS_PER_SEGMENT;
1075 } else {
1076 $row_start = $segment * ROWS_PER_SEGMENT;
1077 $row_inc = ROWS_PER_SEGMENT;
1078 }
1079 do {
1080 // don't include extra stuff, if so requested
1081 $excs = array('revisions' => 0, 'spam' => 1); //TODO, FIX THIS
1082 $where = '';
1083 if ( is_array($excs['spam'] ) && in_array($table, $excs['spam']) ) {
1084 $where = ' WHERE comment_approved != "spam"';
1085 } elseif ( is_array($excs['revisions'] ) && in_array($table, $excs['revisions']) ) {
1086 $where = ' WHERE post_type != "revision"';
1087 }
1088
1089 if ( !ini_get('safe_mode')) @set_time_limit(15*60);
1090 $table_data = $wpdb->get_results("SELECT * FROM $table $where LIMIT {$row_start}, {$row_inc}", ARRAY_A);
1091 $entries = 'INSERT INTO ' . $this->backquote($table) . ' VALUES (';
1092 // \x08\\x09, not required
1093 $search = array("\x00", "\x0a", "\x0d", "\x1a");
1094 $replace = array('\0', '\n', '\r', '\Z');
1095 if($table_data) {
1096 foreach ($table_data as $row) {
1097 $total_rows++;
1098 $values = array();
1099 foreach ($row as $key => $value) {
1100 if ($ints[strtolower($key)]) {
1101 // make sure there are no blank spots in the insert syntax,
1102 // yet try to avoid quotation marks around integers
1103 $value = ( null === $value || '' === $value) ? $defs[strtolower($key)] : $value;
1104 $values[] = ( '' === $value ) ? "''" : $value;
1105 } else {
1106 $values[] = "'" . str_replace($search, $replace, $this->sql_addslashes($value)) . "'";
1107 }
1108 }
1109 $this->stow(" \n" . $entries . implode(', ', $values) . ');');
1110 }
1111 $row_start += $row_inc;
1112 }
1113 } while((count($table_data) > 0) and ($segment=='none'));
1114 }
1115
1116 if(($segment == 'none') || ($segment < 0)) {
1117 // Create footer/closing comment in SQL-file
1118 $this->stow("\n");
1119 $this->stow("#\n");
1120 $this->stow("# " . sprintf(__('End of data contents of table %s','wp-db-backup'),$this->backquote($table)) . "\n");
1121 $this->stow("# --------------------------------------------------------\n");
1122 $this->stow("\n");
1123 }
1124 $this->log("Table $table: Total rows added: $total_rows");
1125
1126 } // end backup_table()
1127
1128
1129 function stow($query_line) {
1130 if (function_exists('gzopen')) {
1131 if(! @gzwrite($this->dbhandle, $query_line)) {
1132 //$this->error(__('There was an error writing a line to the backup script:','wp-db-backup') . ' ' . $query_line . ' ' . $php_errormsg);
1133 }
1134 } else {
1135 if(false === @fwrite($this->dbhandle, $query_line)) {
1136 //$this->error(__('There was an error writing a line to the backup script:','wp-db-backup') . ' ' . $query_line . ' ' . $php_errormsg);
1137 }
1138 }
1139 }
1140
1141
1142 function close($handle) {
1143 if (function_exists('gzopen')) {
1144 gzclose($handle);
1145 } else {
1146 fclose($handle);
1147 }
1148 }
1149
1150 /**
1151 * Logs any error messages
1152 * @param array $args
1153 * @return bool
1154 */
1155 function error($error,$severity='') {
1156 $this->errors[] = array('error'=>$error,'severity'=>$severity);
1157 if ($severity == 'fatal') {
1158 //do something...
1159 }
1160 return true;
1161 }
1162
1163 /**
1164 * Add backquotes to tables and db-names in
1165 * SQL queries. Taken from phpMyAdmin.
1166 */
1167 function backquote($a_name) {
1168 if (!empty($a_name) && $a_name != '*') {
1169 if (is_array($a_name)) {
1170 $result = array();
1171 reset($a_name);
1172 while(list($key, $val) = each($a_name))
1173 $result[$key] = '`' . $val . '`';
1174 return $result;
1175 } else {
1176 return '`' . $a_name . '`';
1177 }
1178 } else {
1179 return $a_name;
1180 }
1181 }
1182
1183 /**
1184 * Better addslashes for SQL queries.
1185 * Taken from phpMyAdmin.
1186 */
1187 function sql_addslashes($a_string = '', $is_like = false) {
1188 if ($is_like) $a_string = str_replace('\\', '\\\\\\\\', $a_string);
1189 else $a_string = str_replace('\\', '\\\\', $a_string);
1190 return str_replace('\'', '\\\'', $a_string);
1191 }
1192
1193 /*END OF WP-DB-BACKUP BLOCK */
1194
1195 /*
1196 this function is both the backup scheduler and ostensibly a filter callback for saving the option.
1197 it is called in the register_setting for the updraft_interval, which means when the admin settings
1198 are saved it is called. it returns the actual result from wp_filter_nohtml_kses (a sanitization filter)
1199 so the option can be properly saved.
1200 */
1201 function schedule_backup($interval) {
1202 //clear schedule and add new so we don't stack up scheduled backups
1203 wp_clear_scheduled_hook('updraft_backup');
1204 switch($interval) {
1205 case 'daily':
1206 case 'weekly':
1207 case 'monthly':
1208 wp_schedule_event(time()+30, $interval, 'updraft_backup');
1209 break;
1210 }
1211 return wp_filter_nohtml_kses($interval);
1212 }
1213
1214 function schedule_backup_database($interval) {
1215 //clear schedule and add new so we don't stack up scheduled backups
1216 wp_clear_scheduled_hook('updraft_backup_database');
1217 switch($interval) {
1218 case 'daily':
1219 case 'weekly':
1220 case 'monthly':
1221 wp_schedule_event(time()+30, $interval, 'updraft_backup_database');
1222 break;
1223 }
1224 return wp_filter_nohtml_kses($interval);
1225 }
1226
1227 //wp-cron only has hourly, daily and twicedaily, so we need to add weekly and monthly.
1228 function modify_cron_schedules($schedules) {
1229 $schedules['weekly'] = array(
1230 'interval' => 604800,
1231 'display' => 'Once Weekly'
1232 );
1233 $schedules['monthly'] = array(
1234 'interval' => 2592000,
1235 'display' => 'Once Monthly'
1236 );
1237 return $schedules;
1238 }
1239
1240 function backups_dir_location() {
1241 $updraft_dir = untrailingslashit(get_option('updraft_dir'));
1242 $default_backup_dir = WP_CONTENT_DIR.'/updraft';
1243 //if the option isn't set, default it to /backups inside the upload dir
1244 $updraft_dir = ($updraft_dir)?$updraft_dir:$default_backup_dir;
1245 //check for the existence of the dir and an enumeration preventer.
1246 if(!is_dir($updraft_dir) || !is_file($updraft_dir.'/index.html') || !is_file($updraft_dir.'/.htaccess')) {
1247 @mkdir($updraft_dir,0777,true); //recursively create the dir with 0777 permissions. 0777 is default for php creation. not ideal, but I'll get back to this
1248 @file_put_contents($updraft_dir.'/index.html','Nothing to see here.');
1249 @file_put_contents($updraft_dir.'/.htaccess','deny from all');
1250 }
1251 return $updraft_dir;
1252 }
1253
1254 function updraft_download_backup() {
1255 $type = $_POST['type'];
1256 $timestamp = (int)$_POST['timestamp'];
1257 $backup_history = $this->get_backup_history();
1258 $file = $backup_history[$timestamp][$type];
1259 $fullpath = trailingslashit(get_option('updraft_dir')).$file;
1260 if(!is_readable($fullpath)) {
1261 //if the file doesn't exist and they're using one of the cloud options, fetch it down from the cloud.
1262 $this->download_backup($file);
1263 }
1264 if(@is_readable($fullpath) && is_file($fullpath)) {
1265 $len = filesize($fullpath);
1266
1267 $filearr = explode('.',$file);
1268 //we've only got zip and gz...for now
1269 $file_ext = array_pop($filearr);
1270 if($file_ext == 'zip') {
1271 header('Content-type: application/zip');
1272 } else {
1273 // This catches both when what was popped was 'crypt' (*-db.gz.crypt) and when it was 'gz' (unencrypted)
1274 header('Content-type: application/x-gzip');
1275 }
1276 header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
1277 header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
1278 header("Content-Length: $len;");
1279 if ($file_ext == 'crypt') {
1280 header("Content-Disposition: attachment; filename=\"".substr($file,0,-6)."\";");
1281 } else {
1282 header("Content-Disposition: attachment; filename=\"$file\";");
1283 }
1284 ob_end_flush();
1285 if ($file_ext == 'crypt') {
1286 $encryption = get_option('updraft_encryptionphrase');
1287 if ($encryption == "") {
1288 $this->error('Decryption of database failed: the database file is encrypted, but you have no encryption key entered.');
1289 } else {
1290 require_once(dirname(__FILE__).'/includes/Rijndael.php');
1291 $rijndael = new Crypt_Rijndael();
1292 $rijndael->setKey($encryption);
1293 $in_handle = fopen($fullpath,'r');
1294 $ciphertext = "";
1295 while (!feof ($in_handle)) {
1296 $ciphertext .= fread($in_handle, 16384);
1297 }
1298 fclose ($in_handle);
1299 print $rijndael->decrypt($ciphertext);
1300 }
1301 } else {
1302 readfile($fullpath);
1303 }
1304 $this->delete_local($file);
1305 exit; //we exit immediately because otherwise admin-ajax appends an additional zero to the end for some reason I don't understand. seriously, why die('0')?
1306 } else {
1307 echo 'Download failed. File '.$fullpath.' did not exist or was unreadable. If you delete local backups then S3 or Google Drive or FTP retrieval may have failed. (Note that Google Drive downloading is not yet supported - you need to download manually if you use Google Drive).';
1308 }
1309 }
1310
1311 function download_backup($file) {
1312 switch(get_option('updraft_service')) {
1313 case 'googledrive':
1314 $this->download_googledrive_backup($file);
1315 break;
1316 case 's3':
1317 $this->download_s3_backup($file);
1318 break;
1319 case 'ftp':
1320 $this->download_ftp_backup($file);
1321 break;
1322 default:
1323 $this->error('Automatic backup restoration is only available via S3, FTP, and local. Email and downloaded backup restoration must be performed manually.');
1324 }
1325 }
1326
1327 function download_googledrive_backup($file) {
1328 $this->error("Google Drive error: we do not yet support downloading existing backups from Google Drive - you need to restore the backup manually");
1329 }
1330
1331 function download_s3_backup($file) {
1332 if(!class_exists('S3')) {
1333 require_once(dirname(__FILE__).'/includes/S3.php');
1334 }
1335 $s3 = new S3(get_option('updraft_s3_login'), get_option('updraft_s3_pass'));
1336 $bucket_name = untrailingslashit(get_option('updraft_s3_remote_path'));
1337 if (@$s3->putBucket($bucket_name, S3::ACL_PRIVATE)) {
1338 $fullpath = trailingslashit(get_option('updraft_dir')).$file;
1339 if (!$s3->getObject($bucket_name, $file, $fullpath)) {
1340 $this->error("S3 Error: Failed to download $fullpath. Error was ".$php_errormsg);
1341 }
1342 } else {
1343 $this->error("S3 Error: Failed to create bucket $bucket_name. Error was ".$php_errormsg);
1344 }
1345 }
1346
1347 function download_ftp_backup($file) {
1348 if( !class_exists('ftp_wrapper')) {
1349 require_once(dirname(__FILE__).'/includes/ftp.class.php');
1350 }
1351 //handle SSL and errors at some point TODO
1352 $ftp = new ftp_wrapper(get_option('updraft_server_address'),get_option('updraft_ftp_login'),get_option('updraft_ftp_pass'));
1353 $ftp->passive = true;
1354 $ftp->connect();
1355 //$ftp->make_dir(); we may need to recursively create dirs? TODO
1356
1357 $ftp_remote_path = trailingslashit(get_option('updraft_ftp_remote_path'));
1358 $fullpath = trailingslashit(get_option('updraft_dir')).$file;
1359 $ftp->get($fullpath,$ftp_remote_path.$file,FTP_BINARY);
1360 }
1361
1362 function restore_backup($timestamp) {
1363 global $wp_filesystem;
1364 $backup_history = get_option('updraft_backup_history');
1365 if(!is_array($backup_history[$timestamp])) {
1366 echo '<p>This backup does not exist in the backup history -- restoration aborted! timestamp: '.$timestamp.'</p><br/>';
1367 return false;
1368 }
1369
1370 $credentials = request_filesystem_credentials("options-general.php?page=updraftplus&action=updraft_restore&backup_timestamp=$timestamp");
1371 WP_Filesystem($credentials);
1372 if ( $wp_filesystem->errors->get_error_code() ) {
1373 foreach ( $wp_filesystem->errors->get_error_messages() as $message )
1374 show_message($message);
1375 exit;
1376 }
1377
1378 //if we make it this far then WP_Filesystem has been instantiated and is functional (tested with ftpext, what about suPHP and other situations where direct may work?)
1379 echo '<span style="font-weight:bold">Restoration Progress </span><div id="updraft-restore-progress">';
1380
1381 $updraft_dir = trailingslashit(get_option('updraft_dir'));
1382 foreach($backup_history[$timestamp] as $type=>$file) {
1383 $fullpath = $updraft_dir.$file;
1384 if(!is_readable($fullpath) && $type != 'db') {
1385 $this->download_backup($file);
1386 }
1387 if(is_readable($fullpath) && $type != 'db') {
1388 if(!class_exists('WP_Upgrader')) {
1389 require_once( ABSPATH . 'wp-admin/includes/class-wp-upgrader.php' );
1390 }
1391 require_once('includes/updraft-restorer.php');
1392 $restorer = new Updraft_Restorer();
1393 $val = $restorer->restore_backup($fullpath,$type);
1394 if(is_wp_error($val)) {
1395 print_r($val);
1396 echo '</div>'; //close the updraft_restore_progress div even if we error
1397 return false;
1398 }
1399 }
1400 }
1401 echo '</div>'; //close the updraft_restore_progress div
1402 if(ini_get('safe_mode')) {
1403 echo "<p>DB could not be restored because safe_mode is active on your server. You will need to manually restore the file via phpMyAdmin or another method.</p><br/>";
1404 return false;
1405 }
1406 return true;
1407 }
1408
1409 //deletes the -old directories that are created when a backup is restored.
1410 function delete_old_dirs() {
1411 global $wp_filesystem;
1412 $credentials = request_filesystem_credentials("options-general.php?page=updraftplus&action=updraft_delete_old_dirs");
1413 WP_Filesystem($credentials);
1414 if ( $wp_filesystem->errors->get_error_code() ) {
1415 foreach ( $wp_filesystem->errors->get_error_messages() as $message )
1416 show_message($message);
1417 exit;
1418 }
1419
1420 $to_delete = array('themes-old','plugins-old','uploads-old');
1421
1422 foreach($to_delete as $name) {
1423 //recursively delete
1424 if(!$wp_filesystem->delete(WP_CONTENT_DIR.'/'.$name, true)) {
1425 return false;
1426 }
1427 }
1428 return true;
1429 }
1430
1431 //scans the content dir to see if any -old dirs are present
1432 function scan_old_dirs() {
1433 $dirArr = scandir(WP_CONTENT_DIR);
1434 foreach($dirArr as $dir) {
1435 if(strpos($dir,'-old') !== false) {
1436 return true;
1437 }
1438 }
1439 return false;
1440 }
1441
1442
1443 function retain_range($input) {
1444 $input = (int)$input;
1445 if($input > 0 && $input < 3650) {
1446 return $input;
1447 } else {
1448 return 1;
1449 }
1450 }
1451
1452 function create_backup_dir() {
1453 global $wp_filesystem;
1454 $credentials = request_filesystem_credentials("options-general.php?page=updraftplus&action=updraft_create_backup_dir");
1455 WP_Filesystem($credentials);
1456 if ( $wp_filesystem->errors->get_error_code() ) {
1457 foreach ( $wp_filesystem->errors->get_error_messages() as $message )
1458 show_message($message);
1459 exit;
1460 }
1461
1462 $updraft_dir = untrailingslashit(get_option('updraft_dir'));
1463 $default_backup_dir = WP_CONTENT_DIR.'/updraft';
1464 $updraft_dir = ($updraft_dir)?$updraft_dir:$default_backup_dir;
1465
1466 //chmod the backup dir to 0777. ideally we'd rather chgrp it but i'm not sure if it's possible to detect the group apache is running under (or what if it's not apache...)
1467 if(!$wp_filesystem->mkdir($updraft_dir, 0777)) {
1468 return false;
1469 }
1470 return true;
1471 }
1472
1473
1474 function memory_check_current() {
1475 # Returns in megabytes
1476 $memory_limit = ini_get('memory_limit');
1477 $memory_unit = $memory_limit[strlen($memory_limit)-1];
1478 $memory_limit = substr($memory_limit,0,strlen($memory_limit)-1);
1479 switch($memory_unit) {
1480 case 'K':
1481 $memory_limit = $memory_limit/1024;
1482 break;
1483 case 'G':
1484 $memory_limit = $memory_limit*1024;
1485 break;
1486 case 'M':
1487 //assumed size, no change needed
1488 break;
1489 }
1490 return $memory_limit;
1491 }
1492
1493 function memory_check($memory) {
1494 $memory_limit = $this->memory_check_current();
1495 return ($memory_limit >= $memory)?true:false;
1496 }
1497
1498 function execution_time_check($time) {
1499 return (ini_get('max_execution_time') >= $time)?true:false;
1500 }
1501
1502 function admin_init() {
1503 if(get_option('updraft_debug_mode')) {
1504 ini_set('display_errors',1);
1505 error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED);
1506 ini_set('track_errors',1);
1507 }
1508 wp_enqueue_script('jquery');
1509 register_setting( 'updraft-options-group', 'updraft_interval', array($this,'schedule_backup') );
1510 register_setting( 'updraft-options-group', 'updraft_interval_database', array($this,'schedule_backup_database') );
1511 register_setting( 'updraft-options-group', 'updraft_retain', array($this,'retain_range') );
1512 register_setting( 'updraft-options-group', 'updraft_encryptionphrase', 'wp_filter_nohtml_kses' );
1513 register_setting( 'updraft-options-group', 'updraft_service', 'wp_filter_nohtml_kses' );
1514 register_setting( 'updraft-options-group', 'updraft_s3_login', 'wp_filter_nohtml_kses' );
1515 register_setting( 'updraft-options-group', 'updraft_s3_pass', 'wp_filter_nohtml_kses' );
1516 register_setting( 'updraft-options-group', 'updraft_s3_remote_path', 'wp_filter_nohtml_kses' );
1517 register_setting( 'updraft-options-group', 'updraft_googledrive_clientid', 'wp_filter_nohtml_kses' );
1518 register_setting( 'updraft-options-group', 'updraft_googledrive_secret', 'wp_filter_nohtml_kses' );
1519 register_setting( 'updraft-options-group', 'updraft_googledrive_remotepath', 'wp_filter_nohtml_kses' );
1520 register_setting( 'updraft-options-group', 'updraft_ftp_login', 'wp_filter_nohtml_kses' );
1521 register_setting( 'updraft-options-group', 'updraft_ftp_pass', 'wp_filter_nohtml_kses' );
1522 register_setting( 'updraft-options-group', 'updraft_dir', 'wp_filter_nohtml_kses' );
1523 register_setting( 'updraft-options-group', 'updraft_email', 'wp_filter_nohtml_kses' );
1524 register_setting( 'updraft-options-group', 'updraft_ftp_remote_path', 'wp_filter_nohtml_kses' );
1525 register_setting( 'updraft-options-group', 'updraft_server_address', 'wp_filter_nohtml_kses' );
1526 register_setting( 'updraft-options-group', 'updraft_delete_local', 'absint' );
1527 register_setting( 'updraft-options-group', 'updraft_debug_mode', 'absint' );
1528 register_setting( 'updraft-options-group', 'updraft_include_plugins', 'absint' );
1529 register_setting( 'updraft-options-group', 'updraft_include_themes', 'absint' );
1530 register_setting( 'updraft-options-group', 'updraft_include_uploads', 'absint' );
1531
1532 /* I see no need for this check; people can only download backups/logs if they can guess a nonce formed from a random number and if .htaccess files have no effect. The database will be encrypted. Very unlikely.
1533 if (current_user_can('manage_options')) {
1534 $updraft_dir = $this->backups_dir_location();
1535 if(strpos($updraft_dir,WP_CONTENT_DIR) !== false) {
1536 $relative_dir = str_replace(WP_CONTENT_DIR,'',$updraft_dir);
1537 $possible_updraft_url = WP_CONTENT_URL.$relative_dir;
1538 $resp = wp_remote_request($possible_updraft_url, array('timeout' => 15));
1539 if ( is_wp_error($resp) ) {
1540 add_action('admin_notices', array($this,'show_admin_warning_accessible_unknownresult') );
1541 } else {
1542 if(strpos($resp['response']['code'],'403') === false) {
1543 add_action('admin_notices', array($this,'show_admin_warning_accessible') );
1544 }
1545 }
1546 }
1547 }
1548 */
1549 if (current_user_can('manage_options') && get_option('updraft_googledrive_clientid') != "" && get_option('updraft_googledrive_token','xyz') == 'xyz') {
1550 add_action('admin_notices', array($this,'show_admin_warning_googledrive') );
1551 }
1552 }
1553
1554 function add_admin_pages() {
1555 add_submenu_page('options-general.php', "UpdraftPlus", "UpdraftPlus", "manage_options", "updraftplus",
1556 array($this,"settings_output"));
1557 }
1558
1559 function wordshell_random_advert($urls) {
1560 $url_start = ($urls) ? '<a href="http://wordshell.net">' : "";
1561 $url_end = ($urls) ? '</a>' : " (www.wordshell.net)";
1562 if (rand(0,1) == 0) {
1563 return "Like automating WordPress operations? Use the CLI? ${url_start}You will love WordShell${url_end} - saves time and money fast.";
1564 } else {
1565 return "${url_start}Check out WordShell${url_end} - manage WordPress from the command line - huge time-saver";
1566 }
1567 }
1568
1569 function settings_output() {
1570
1571 $ws_advert = $this->wordshell_random_advert(1);
1572 echo <<<ENDHERE
1573 <div class="updated fade" style="font-size:140%; padding:14px;">${ws_advert}</div>
1574 ENDHERE;
1575
1576 /*
1577 we use request here because the initial restore is triggered by a POSTed form. we then may need to obtain credentials
1578 for the WP_Filesystem. to do this WP outputs a form that we can't insert variables into (apparently). So the values are
1579 passed back in as GET parameters. REQUEST covers both GET and POST so this weird logic works.
1580 */
1581 if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'updraft_restore' && isset($_REQUEST['backup_timestamp'])) {
1582 $backup_success = $this->restore_backup($_REQUEST['backup_timestamp']);
1583 if(empty($this->errors) && $backup_success == true) {
1584 echo '<p>Restore successful!</p><br/>';
1585 echo '<b>Actions:</b> <a href="options-general.php?page=updraftplus&updraft_restore_success=true">Return to Updraft Configuration</a>.';
1586 return;
1587 } else {
1588 echo '<p>Restore failed...</p><br/>';
1589 echo '<b>Actions:</b> <a href="options-general.php?page=updraftplus">Return to Updraft Configuration</a>.';
1590 return;
1591 }
1592 //uncomment the below once i figure out how i want the flow of a restoration to work.
1593 //echo '<b>Actions:</b> <a href="options-general.php?page=updraftplus">Return to Updraft Configuration</a>.';
1594 }
1595 $deleted_old_dirs = false;
1596 if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'updraft_delete_old_dirs') {
1597 if($this->delete_old_dirs()) {
1598 $deleted_old_dirs = true;
1599 } else {
1600 echo '<p>Old directory removal failed for some reason. You may want to do this manually.</p><br/>';
1601 }
1602 echo '<p>Old directories successfully removed.</p><br/>';
1603 echo '<b>Actions:</b> <a href="options-general.php?page=updraftplus">Return to Updraft Configuration</a>.';
1604 return;
1605 }
1606
1607 if(isset($_GET['error'])) {
1608 echo "<p><strong>ERROR:</strong> ".htmlspecialchars($_GET['error'])."</p>";
1609 }
1610 if(isset($_GET['message'])) {
1611 echo "<p><strong>Note:</strong> ".htmlspecialchars($_GET['message'])."</p>";
1612 }
1613
1614 if(isset($_GET['action']) && $_GET['action'] == 'updraft_create_backup_dir') {
1615 if(!$this->create_backup_dir()) {
1616 echo '<p>Backup directory could not be created...</p><br/>';
1617 }
1618 echo '<p>Backup directory successfully created.</p><br/>';
1619 echo '<b>Actions:</b> <a href="options-general.php?page=updraftplus">Return to Updraft Configuration</a>.';
1620 return;
1621 }
1622
1623 if(isset($_POST['action']) && $_POST['action'] == 'updraft_backup') {
1624 wp_schedule_single_event(time()+5, 'updraft_backup_all');
1625 }
1626 if(isset($_POST['action']) && $_POST['action'] == 'updraft_backup_debug_all') {
1627 $this->backup(true,true);
1628 }
1629 if(isset($_POST['action']) && $_POST['action'] == 'updraft_backup_debug_db') {
1630 $this->backup_db();
1631 }
1632
1633 ?>
1634 <div class="wrap">
1635 <h2>UpdraftPlus - Backup/Restore</h2>
1636
1637 Version: <b><?php echo $this->version; ?></b><br />
1638 Maintained by <b>David Anderson</b> (<a href="http://david.dw-perspective.org.uk">Homepage</a> | <a href="http://wordshell.net">WordShell - WordPress command line</a> | <a href="http://david.dw-perspective.org.uk/donate">Donate</a>)
1639 <br />
1640 Based on Updraft by <b>Paul Kehrer</b> (<a href="http://langui.sh" target="_blank">Blog</a> | <a href="http://twitter.com/reaperhulk" target="_blank">Twitter</a> )
1641 <br />
1642 <?php
1643 if(isset($_GET['updraft_restore_success'])) {
1644 echo "<div style=\"color:blue\">Your backup has been restored. Your old themes, uploads, and plugins directories have been retained with \"-old\" appended to their name. Remove them when you are satisfied that the backup worked properly. At this time Updraft does not automatically restore your DB. You will need to use an external tool like phpMyAdmin to perform that task.</div>";
1645 }
1646 if($deleted_old_dirs) {
1647 echo "<div style=\"color:blue\">Old directories successfully deleted.</div>";
1648 }
1649 if(!$this->memory_check(96)) {?>
1650 <div style="color:orange">Your PHP memory limit is too low. Updraft attempted to raise it but was unsuccessful. This plugin may not work properly with a memory limit of less than 96 Mb. Current limit is: <?php echo $this->memory_check_current(); ?> Mb</div>
1651 <?php
1652 }
1653 if(!$this->execution_time_check(300)) {?>
1654 <div style="color:orange">Your PHP max_execution_time is less than 300 seconds. This probably means you're running in safe_mode. Either disable safe_mode or modify your php.ini to set max_execution_time to a higher number. If you do not, there is a chance Updraft will be unable to complete a backup. Present limit is: <?php echo ini_get('max_execution_time'); ?> seconds.</div>
1655 <?php
1656 }
1657
1658 if($this->scan_old_dirs()) {?>
1659 <div style="color:orange">You have old directories from a previous backup. Click to delete them after you have verified that the restoration worked.</div>
1660 <form method="post" action="<?php echo remove_query_arg(array('updraft_restore_success','action')) ?>">
1661 <input type="hidden" name="action" value="updraft_delete_old_dirs" />
1662 <input type="submit" class="button-primary" value="Delete Old Dirs" onclick="return(confirm('Are you sure you want to delete the old directories? This cannot be undone.'))" />
1663 </form>
1664 <?php
1665 }
1666 if(!empty($this->errors)) {
1667 foreach($this->errors as $error) {
1668 //ignoring severity here right now
1669 echo '<div style="color:red">'.$error['error'].'</div>';
1670 }
1671 }
1672 ?>
1673 <table class="form-table" style="float:left;width:475px">
1674 <tr>
1675 <?php
1676 $next_scheduled_backup = wp_next_scheduled('updraft_backup');
1677 $next_scheduled_backup = ($next_scheduled_backup) ? date('D, F j, Y H:i T',$next_scheduled_backup) : 'No backups are scheduled at this time.';
1678 $next_scheduled_backup_database = wp_next_scheduled('updraft_backup_database');
1679 if (get_option('updraft_interval_database',get_option('updraft_interval')) == get_option('updraft_interval')) {
1680 $next_scheduled_backup_database = "Will take place at the same time as the files backup.";
1681 } else {
1682 $next_scheduled_backup_database = ($next_scheduled_backup_database) ? date('D, F j, Y H:i T',$next_scheduled_backup_database) : 'No backups are scheduled at this time.';
1683 }
1684 $current_time = date('D, F j, Y H:i T',time());
1685 $updraft_last_backup = get_option('updraft_last_backup');
1686 if($updraft_last_backup) {
1687 if($updraft_last_backup['success']) {
1688 $last_backup = date('D, F j, Y H:i T',$updraft_last_backup['backup_time']);
1689 $last_backup_color = 'green';
1690 } else {
1691 $last_backup = print_r($updraft_last_backup['errors'],true);
1692 $last_backup_color = 'red';
1693 }
1694 } else {
1695 $last_backup = 'No backup has been completed.';
1696 $last_backup_color = 'blue';
1697 }
1698
1699 $updraft_dir = $this->backups_dir_location();
1700 if(is_writable($updraft_dir)) {
1701 $dir_info = '<span style="color:green">Backup directory specified is writable, which is good.</span>';
1702 $backup_disabled = "";
1703 } else {
1704 $backup_disabled = 'disabled="disabled"';
1705 $dir_info = '<span style="color:red">Backup directory specified is <b>not</b> writable. <span style="font-size:110%;font-weight:bold"><a href="options-general.php?page=updraftplus&action=updraft_create_backup_dir">Click here</a></span> to attempt to create the directory and set the permissions. If that is unsuccessful check the permissions on your server or change it to another directory that is writable by your web server process.</span>';
1706 }
1707 ?>
1708 <th>Now:</th>
1709 <td style="color:blue"><?php echo $current_time?></td>
1710 </tr>
1711 <tr>
1712 <th>Next Scheduled Files Backup:</th>
1713 <td style="color:blue"><?php echo $next_scheduled_backup?></td>
1714 </tr>
1715 <tr>
1716 <th>Next Scheduled DB Backup:</th>
1717 <td style="color:blue"><?php echo $next_scheduled_backup_database?></td>
1718 </tr>
1719 <tr>
1720 <th>Last Backup:</th>
1721 <td style="color:<?php echo $last_backup_color ?>"><?php echo $last_backup?></td>
1722 </tr>
1723 </table>
1724 <div style="float:left;width:200px">
1725 <form method="post" action="">
1726 <input type="hidden" name="action" value="updraft_backup" />
1727 <p><input type="submit" <?php echo $backup_disabled ?> class="button-primary" value="Backup Now!" style="padding-top:7px;padding-bottom:7px;font-size:24px !important" onclick="return(confirm('This will schedule a one time backup. To trigger the backup immediately you may need to load a page on your site.'))" /></p>
1728 </form>
1729 <div style="position:relative">
1730 <div style="position:absolute;top:0;left:0">
1731 <?php
1732 $backup_history = get_option('updraft_backup_history');
1733 $backup_history = (is_array($backup_history))?$backup_history:array();
1734 $restore_disabled = (count($backup_history) == 0) ? 'disabled="disabled"' : "";
1735 ?>
1736 <input type="button" class="button-primary" <?php echo $restore_disabled ?> value="Restore" style="padding-top:7px;padding-bottom:7px;font-size:24px !important" onclick="jQuery('#backup-restore').fadeIn('slow');jQuery(this).parent().fadeOut('slow')" />
1737 </div>
1738 <div style="display:none;position:absolute;top:0;left:0" id="backup-restore">
1739 <form method="post" action="">
1740 <b>Choose: </b>
1741 <select name="backup_timestamp" style="display:inline">
1742 <?php
1743 foreach($backup_history as $key=>$value) {
1744 echo "<option value='$key'>".date('Y-m-d G:i',$key)."</option>\n";
1745 }
1746 ?>
1747 </select>
1748
1749 <input type="hidden" name="action" value="updraft_restore" />
1750 <input type="submit" <?php echo $restore_disabled ?> class="button-primary" value="Restore Now!" style="padding-top:7px;margin-top:5px;padding-bottom:7px;font-size:24px !important" onclick="return(confirm('Restoring from backup will replace this site\'s themes, plugins, and uploads directories. DB restoration must be done separately at this time. Continue with the restoration process?'))" />
1751 </form>
1752 </div>
1753 </div>
1754 </div>
1755 <br style="clear:both" />
1756 <table class="form-table">
1757 <tr>
1758 <th>Download Backups</th>
1759 <td><a href="#" title="Click to see available backups" onclick="jQuery('.download-backups').toggle();return false;"><?php echo count($backup_history)?> available</a></td>
1760 </tr>
1761 <tr>
1762 <td></td><td class="download-backups" style="display:none">
1763 <em>Click on a button to download the corresponding file to your computer. If you are using Opera, you should turn Turbo mode off.</em>
1764 <table>
1765 <?php
1766 foreach($backup_history as $key=>$value) {
1767 ?>
1768 <tr>
1769 <td><b><?php echo date('Y-m-d G:i',$key)?></b></td>
1770 <td>
1771 <?php if (isset($value['db'])) { ?>
1772 <form action="admin-ajax.php" method="post">
1773 <input type="hidden" name="action" value="updraft_download_backup" />
1774 <input type="hidden" name="type" value="db" />
1775 <input type="hidden" name="timestamp" value="<?php echo $key?>" />
1776 <input type="submit" value="Database" />
1777 </form>
1778 <?php } else { echo "(No database in backup)"; } ?>
1779 </td>
1780 <td>
1781 <?php if (isset($value['plugins'])) { ?>
1782 <form action="admin-ajax.php" method="post">
1783 <input type="hidden" name="action" value="updraft_download_backup" />
1784 <input type="hidden" name="type" value="plugins" />
1785 <input type="hidden" name="timestamp" value="<?php echo $key?>" />
1786 <input type="submit" value="Plugins" />
1787 </form>
1788 <?php } else { echo "(No plugins in backup)"; } ?>
1789 </td>
1790 <td>
1791 <?php if (isset($value['themes'])) { ?>
1792 <form action="admin-ajax.php" method="post">
1793 <input type="hidden" name="action" value="updraft_download_backup" />
1794 <input type="hidden" name="type" value="themes" />
1795 <input type="hidden" name="timestamp" value="<?php echo $key?>" />
1796 <input type="submit" value="Themes" />
1797 </form>
1798 <?php } else { echo "(No themes in backup)"; } ?>
1799 </td>
1800 <td>
1801 <?php if (isset($value['uploads'])) { ?>
1802 <form action="admin-ajax.php" method="post">
1803 <input type="hidden" name="action" value="updraft_download_backup" />
1804 <input type="hidden" name="type" value="uploads" />
1805 <input type="hidden" name="timestamp" value="<?php echo $key?>" />
1806 <input type="submit" value="Uploads" />
1807 </form>
1808 <?php } else { echo "(No uploads in backup)"; } ?>
1809 </td>
1810 </tr>
1811 <?php }?>
1812 </table>
1813 </td>
1814 </tr>
1815 </table>
1816 <form method="post" action="options.php">
1817 <?php settings_fields('updraft-options-group'); ?>
1818 <table class="form-table">
1819 <tr>
1820 <th>Backup Directory:</th>
1821 <td><input type="text" name="updraft_dir" style="width:525px" value="<?php echo $updraft_dir ?>" /></td>
1822 </tr>
1823 <tr>
1824 <td></td><td><?php echo $dir_info ?> This is where Updraft Backup/Restore will write the zip files it creates initially. This directory must be writable by your web server. Typically you'll want to have it inside your wp-content folder (this is the default). <b>Do not</b> place it inside your uploads dir, as that will cause recursion issues (backups of backups of backups of...).</td>
1825 </tr>
1826 <tr>
1827 <th>File Backup Intervals:</th>
1828 <td><select name="updraft_interval">
1829 <?php
1830 $intervals = array ("manual", "daily", "weekly", "monthly");
1831 foreach ($intervals as $ival) {
1832 echo "<option value=\"$ival\" ";
1833 if ($ival == get_option('updraft_interval','manual')) { echo 'selected="selected"';}
1834 echo ">".ucfirst($ival)."</option>\n";
1835 }
1836 ?>
1837 </select></td>
1838 </tr>
1839 <tr>
1840 <th>Database Backup Intervals:</th>
1841 <td><select name="updraft_interval_database">
1842 <?php
1843 $intervals = array ("manual", "daily", "weekly", "monthly");
1844 foreach ($intervals as $ival) {
1845 echo "<option value=\"$ival\" ";
1846 if ($ival == get_option('updraft_interval_database',get_option('updraft_interval'))) { echo 'selected="selected"';}
1847 echo ">".ucfirst($ival)."</option>\n";
1848 }
1849 ?>
1850 </select></td>
1851 </tr>
1852 <tr class="backup-interval-description">
1853 <td></td><td>If you would like to automatically schedule backups, choose schedules from the dropdown above. Backups will occur at the interval specified starting just after the current time. If you choose manual you must click the &quot;Backup Now!&quot; button whenever you wish a backup to occur. If the two schedules are the same, then the two backups will take place together.</td>
1854 </tr>
1855 <?php
1856 # The true (default value if non-existent) here has the effect of forcing a default of on.
1857 $include_themes = (get_option('updraft_include_themes',true)) ? 'checked="checked"' : "";
1858 $include_plugins = (get_option('updraft_include_plugins',true)) ? 'checked="checked"' : "";
1859 $include_uploads = (get_option('updraft_include_uploads',true)) ? 'checked="checked"' : "";
1860 ?>
1861 <tr>
1862 <th>Include in Files Backup:</th>
1863 <td>
1864 <input type="checkbox" name="updraft_include_plugins" value="1" <?php echo $include_plugins; ?> /> Plugins<br />
1865 <input type="checkbox" name="updraft_include_themes" value="1" <?php echo $include_themes; ?> /> Themes<br />
1866 <input type="checkbox" name="updraft_include_uploads" value="1" <?php echo $include_uploads; ?> /> Uploads<br />
1867 Include all of these, unless you are backing them up separately. Note that presently UpdraftPlus backs up these directories only - which is usually everything (except for WordPress core itself which you can download afresh from WordPress.org). But if you have made customised modifications outside of these directories, you need to back them up another way.<br />(<a href="http://wordshell.net">Use WordShell</a> for automatic backup, version control and patching).<br /></td>
1868 </td>
1869 </tr>
1870 <tr>
1871 <th>Retain Backups:</th>
1872 <?php
1873 $updraft_retain = get_option('updraft_retain');
1874 $retain = ((int)$updraft_retain > 0)?get_option('updraft_retain'):1;
1875 ?>
1876 <td><input type="text" name="updraft_retain" value="<?php echo $retain ?>" style="width:50px" /></td>
1877 </tr>
1878 <tr class="backup-retain-description">
1879 <td></td><td>By default only the most recent backup is retained. If you'd like to preserve more, specify the number here. (This many of <strong>both</strong> files and database backups will be retained.)</td>
1880 </tr>
1881 <tr>
1882 <th>Database encryption phrase:</th>
1883 <?php
1884 $updraft_encryptionphrase = get_option('updraft_encryptionphrase');
1885 ?>
1886 <td><input type="text" name="updraft_encryptionphrase" value="<?php echo $updraft_encryptionphrase ?>" style="width:132px" /></td>
1887 </tr>
1888 <tr class="backup-crypt-description">
1889 <td></td><td>If you enter a string here, it is used to encrypt backups (Rijndael). Do not lose it, or all your backups will be useless. Presently, only the database file is encrypted. This is also the key used to decrypt backups from this admin interface (so if you change it, then automatic decryption will not work until you change it back). You can also use the file example-decrypt.php from inside the UpdraftPlus plugin directory to decrypt manually.</td>
1890 </tr>
1891
1892 <tr>
1893 <th>Remote backup:</th>
1894 <td><select name="updraft_service" id="updraft-service">
1895 <?php
1896 $delete_local = (get_option('updraft_delete_local')) ? 'checked="checked"' : "";
1897 $debug_mode = (get_option('updraft_debug_mode')) ? 'checked="checked"' : "";
1898
1899 $display_none = 'style="display:none"';
1900 $s3 = ""; $ftp = ""; $email = ""; $googledrive="";
1901 $email_display="";
1902 $display_email_complete = "";
1903 $set = 'selected="selected"';
1904 switch(get_option('updraft_service')) {
1905 case 's3':
1906 $s3 = $set;
1907 $googledrive_display = $display_none;
1908 $ftp_display = $display_none;
1909 break;
1910 case 'googledrive':
1911 $googledrive = $set;
1912 $s3_display = $display_none;
1913 $ftp_display = $display_none;
1914 break;
1915 case 'ftp':
1916 $ftp = $set;
1917 $googledrive_display = $display_none;
1918 $s3_display = $display_none;
1919 break;
1920 case 'email':
1921 $email = $set;
1922 $ftp_display = $display_none;
1923 $s3_display = $display_none;
1924 $googledrive_display = $display_none;
1925 $display_email_complete = $display_none;
1926 break;
1927 default:
1928 $none = $set;
1929 $ftp_display = $display_none;
1930 $googledrive_display = $display_none;
1931 $s3_display = $display_none;
1932 $display_delete_local = $display_none;
1933 break;
1934 }
1935 ?>
1936 <option value="none" <?php echo $none?>>None</option>
1937 <option value="s3" <?php echo $s3?>>Amazon S3</option>
1938 <option value="googledrive" <?php echo $googledrive?>>Google Drive (experimental, may work for you, may not)</option>
1939 <option value="ftp" <?php echo $ftp?>>FTP</option>
1940 <option value="email" <?php echo $email?>>E-mail</option>
1941 </select></td>
1942 </tr>
1943 <tr class="backup-service-description">
1944 <td></td><td>Choose your backup method. Be aware that mail servers tend to have strict file size limitations; typically around 10-20Mb; backups larger than this may not arrive. Select none if you do not wish to send your backups anywhere <b>(not recommended)</b>.</td>
1945
1946 </tr>
1947
1948 <!-- Amazon S3 -->
1949 <tr class="s3" <?php echo $s3_display?>>
1950 <th>S3 access key:</th>
1951 <td><input type="text" autocomplete="off" style="width:292px" name="updraft_s3_login" value="<?php echo get_option('updraft_s3_login') ?>" /></td>
1952 </tr>
1953 <tr class="s3" <?php echo $s3_display?>>
1954 <th>S3 secret key:</th>
1955 <td><input type="password" autocomplete="off" style="width:292px" name="updraft_s3_pass" value="<?php echo get_option('updraft_s3_pass'); ?>" /></td>
1956 </tr>
1957 <tr class="s3" <?php echo $s3_display?>>
1958 <th>S3 bucket:</th>
1959 <td><input type="text" style="width:292px" name="updraft_s3_remote_path" value="<?php echo get_option('updraft_s3_remote_path'); ?>" /></td>
1960 </tr>
1961 <tr class="s3" <?php echo $s3_display?>>
1962 <th></th>
1963 <td><p>Get your access key and secret key from your AWS page, then pick a (globally unique) bucket name (letters and numbers) to use for storage. (Do not enter the s3:// prefix).</p></td>
1964 </tr>
1965
1966 <!-- Google Drive -->
1967 <tr class="googledrive" <?php echo $googledrive_display?>>
1968 <th>Google Drive Client ID:</th>
1969 <td><input type="text" autocomplete="off" style="width:332px" name="updraft_googledrive_clientid" value="<?php echo get_option('updraft_googledrive_clientid') ?>" /></td>
1970 </tr>
1971 <tr class="googledrive" <?php echo $googledrive_display?>>
1972 <th>Google Drive Client Secret:</th>
1973 <td><input type="password" autocomplete="off" style="width:332px" name="updraft_googledrive_secret" value="<?php echo get_option('updraft_googledrive_secret'); ?>" /></td>
1974 </tr>
1975 <tr class="googledrive" <?php echo $googledrive_display?>>
1976 <th>Google Drive Folder ID:</th>
1977 <td><input type="text" style="width:332px" name="updraft_googledrive_remotepath" value="<?php echo get_option('updraft_googledrive_remotepath'); ?>" /> <em>(Leave empty to use your root folder)</em></td>
1978 </tr>
1979 <tr class="googledrive" <?php echo $googledrive_display?>>
1980 <th>Authenticate with Google:</th>
1981 <td><p><a href="?page=updraftplus&action=auth&updraftplus_googleauth=doit"><strong>After</strong> you have saved your settings (by clicking &quot;Save Changes&quot; below), then come back here once and click this link to complete authentication with Google.</a>
1982
1983 <?php
1984 if (get_option('updraft_googledrive_token','xyz') != 'xyz') {
1985 echo " (You appear to be already authenticated)";
1986 }
1987 ?>
1988 </p>
1989 <p>To get a folder's ID navigate to that folder in Google Drive and copy the ID from your browser's address bar. It is the part that comes after <kbd>#folders/.</kbd></p>
1990 <p><strong>N.B. : If you choose Google Drive, then no backups will be deleted - all will be retained. Patches welcome!</strong></p>
1991 </td>
1992 </tr>
1993 <tr class="googledrive" <?php echo $googledrive_display?>>
1994 <th></th>
1995 <td><p>Create a Client ID in the API Access section of your <a href="https://code.google.com/apis/console/">Google API Console</a>. Select 'Web Application' as the application type.</p><p>You must add <kbd><?php echo admin_url('options-general.php?page=updraftplus&action=auth'); ?></kbd> as the authorised redirect URI when asked.</p>
1996 <?php
1997 if (!class_exists('SimpleXMLElement')) { echo "<p><b>WARNING:</b> You do not have SimpleXMLElement installed. Google Drive backups will <b>not</b> work until you do.</p>"; }
1998 ?>
1999 </td>
2000 </tr>
2001
2002 <tr class="ftp" <?php echo $ftp_display?>>
2003 <th><a href="#" title="Click for help!" onclick="jQuery('.ftp-description').toggle();return false;">FTP Server:</a></th>
2004 <td><input type="text" style="width:260px" name="updraft_server_address" value="<?php echo get_option('updraft_server_address'); ?>" /></td>
2005 </tr>
2006 <tr class="ftp" <?php echo $ftp_display?>>
2007 <th><a href="#" title="Click for help!" onclick="jQuery('.ftp-description').toggle();return false;">FTP Login:</a></th>
2008 <td><input type="text" autocomplete="off" name="updraft_ftp_login" value="<?php echo get_option('updraft_ftp_login') ?>" /></td>
2009 </tr>
2010 <tr class="ftp" <?php echo $ftp_display?>>
2011 <th><a href="#" title="Click for help!" onclick="jQuery('.ftp-description').toggle();return false;">FTP Password:</a></th>
2012 <td><input type="password" autocomplete="off" style="width:260px" name="updraft_ftp_pass" value="<?php echo get_option('updraft_ftp_pass'); ?>" /></td>
2013 </tr>
2014 <tr class="ftp" <?php echo $ftp_display?>>
2015 <th><a href="#" title="Click for help!" onclick="jQuery('.ftp-description').toggle();return false;">Remote Path:</a></th>
2016 <td><input type="text" style="width:260px" name="updraft_ftp_remote_path" value="<?php echo get_option('updraft_ftp_remote_path'); ?>" /></td>
2017 </tr>
2018 <tr class="ftp-description" style="display:none">
2019 <td colspan="2">An FTP remote path will look like '/home/backup/some/folder'</td>
2020 </tr>
2021 <tr class="email" <?php echo $email_display?>>
2022 <th>Email:</th>
2023 <td><input type="text" style="width:260px" name="updraft_email" value="<?php echo get_option('updraft_email'); ?>" /> <br />Enter an address here to have a report sent (and the whole backup, if you choose) to it.</td>
2024 </tr>
2025 <tr class="deletelocal s3 ftp email" <?php echo $display_delete_local?>>
2026 <th>Delete local backup:</th>
2027 <td><input type="checkbox" name="updraft_delete_local" value="1" <?php echo $delete_local; ?> /> <br />Check this to delete the local backup file (only sensible if you have enabled a remote backup, otherwise you will have no backup remaining).</td>
2028 </tr>
2029 <tr>
2030 <th>Debug mode:</th>
2031 <td><input type="checkbox" name="updraft_debug_mode" value="1" <?php echo $debug_mode; ?> /> <br />Check this for more information, if something is going wrong. Will also drop a log file in your backup directory which you can examine.</td>
2032 </tr>
2033 <tr>
2034 <td>
2035 <input type="hidden" name="action" value="update" />
2036 <input type="submit" class="button-primary" value="Save Changes" />
2037 </td>
2038 </tr>
2039 </table>
2040 </form>
2041 <?php
2042 if(get_option('updraft_debug_mode')) {
2043 ?>
2044 <div>
2045 <h3>Debug Information</h3>
2046 <?php
2047 $peak_memory_usage = memory_get_peak_usage(true)/1024/1024;
2048 $memory_usage = memory_get_usage(true)/1024/1024;
2049 echo 'Peak memory usage: '.$peak_memory_usage.' MB<br/>';
2050 echo 'Current memory usage: '.$memory_usage.' MB<br/>';
2051 echo 'PHP memory limit: '.ini_get('memory_limit').' <br/>';
2052 ?>
2053 <form method="post" action="">
2054 <input type="hidden" name="action" value="updraft_backup_debug_all" />
2055 <p><input type="submit" class="button-primary" <?php echo $backup_disabled ?> value="Debug Backup" onclick="return(confirm('This will cause an immediate backup. The page will stall loading until it finishes (ie, unscheduled). Use this if you\'re trying to see peak memory usage.'))" /></p>
2056 </form>
2057 <form method="post" action="">
2058 <input type="hidden" name="action" value="updraft_backup_debug_db" />
2059 <p><input type="submit" class="button-primary" <?php echo $backup_disabled ?> value="Debug DB Backup" onclick="return(confirm('This will cause an immediate DB backup. The page will stall loading until it finishes (ie, unscheduled). The backup will remain locally despite your prefs and will not go into the backup history or up into the cloud.'))" /></p>
2060 </form>
2061 </div>
2062 <?php } ?>
2063 <script type="text/javascript">
2064 jQuery(document).ready(function() {
2065 jQuery('#updraft-service').change(function() {
2066 switch(jQuery(this).val()) {
2067 case 'none':
2068 jQuery('.deletelocal,.s3,.ftp,.googledrive,.s3-description,.ftp-description').hide()
2069 jQuery('.email,.email-complete').show()
2070 break;
2071 case 's3':
2072 jQuery('.ftp,.ftp-description,.googledrive').hide()
2073 jQuery('.s3,.deletelocal,.email,.email-complete').show()
2074 break;
2075 case 'googledrive':
2076 jQuery('.ftp,.ftp-description,.s3').hide()
2077 jQuery('.googledrive,.deletelocal,.googledrive,.email,.email-complete').show()
2078 break;
2079 case 'ftp':
2080 jQuery('.googledrive,.s3,.s3-description').hide()
2081 jQuery('.ftp,.deletelocal,.email,.email-complete').show()
2082 break;
2083 case 'email':
2084 jQuery('.s3,.ftp,.s3-description,.googledrive,.ftp-description,.email-complete').hide()
2085 jQuery('.email,.deletelocal').show()
2086 break;
2087 }
2088 })
2089 })
2090 jQuery(window).load(function() {
2091 //this is for hiding the restore progress at the top after it is done
2092 setTimeout('jQuery("#updraft-restore-progress").toggle(1000)',3000)
2093 jQuery('#updraft-restore-progress-toggle').click(function() {
2094 jQuery('#updraft-restore-progress').toggle(500)
2095 })
2096 })
2097 </script>
2098 <?php
2099 }
2100
2101 /*array2json provided by bin-co.com under BSD license*/
2102 function array2json($arr) {
2103 if(function_exists('json_encode')) return stripslashes(json_encode($arr)); //Latest versions of PHP already have this functionality.
2104 $parts = array();
2105 $is_list = false;
2106
2107 //Find out if the given array is a numerical array
2108 $keys = array_keys($arr);
2109 $max_length = count($arr)-1;
2110 if(($keys[0] == 0) and ($keys[$max_length] == $max_length)) {//See if the first key is 0 and last key is length - 1
2111 $is_list = true;
2112 for($i=0; $i<count($keys); $i++) { //See if each key correspondes to its position
2113 if($i != $keys[$i]) { //A key fails at position check.
2114 $is_list = false; //It is an associative array.
2115 break;
2116 }
2117 }
2118 }
2119
2120 foreach($arr as $key=>$value) {
2121 if(is_array($value)) { //Custom handling for arrays
2122 if($is_list) $parts[] = $this->array2json($value); /* :RECURSION: */
2123 else $parts[] = '"' . $key . '":' . $this->array2json($value); /* :RECURSION: */
2124 } else {
2125 $str = '';
2126 if(!$is_list) $str = '"' . $key . '":';
2127
2128 //Custom handling for multiple data types
2129 if(is_numeric($value)) $str .= $value; //Numbers
2130 elseif($value === false) $str .= 'false'; //The booleans
2131 elseif($value === true) $str .= 'true';
2132 else $str .= '"' . addslashes($value) . '"'; //All other things
2133 // :TODO: Is there any more datatype we should be in the lookout for? (Object?)
2134
2135 $parts[] = $str;
2136 }
2137 }
2138 $json = implode(',',$parts);
2139
2140 if($is_list) return '[' . $json . ']';//Return numerical JSON
2141 return '{' . $json . '}';//Return associative JSON
2142 }
2143
2144 function show_admin_warning($message) {
2145 echo '<div id="updraftmessage" class="updated fade">';
2146 echo "<p>$message</p></div>";
2147 }
2148 function show_admin_warning_accessible() {
2149 $this->show_admin_warning("UpdraftPlus backup directory specified is accessible via the web. This is a potential security problem (people may be able to download your backups - which is undesirable if your database is not encrypted and if you have non-public assets amongst the files). If using Apache, enable .htaccess support to allow web access to be denied; otherwise, you should deny access manually.");
2150 }
2151 function show_admin_warning_googledrive() {
2152 $this->show_admin_warning('UpdraftPlus notice: <a href="?page=updraftplus&action=auth&updraftplus_googleauth=doit">Click here to authenticate your Google Drive account (you will not be able to back up to Google Drive without it).</a>');
2153 }
2154 function show_admin_warning_accessible_unknownresult() {
2155 $this->show_admin_warning("UpdraftPlus tried to check if the backup directory is accessible via web, but the result was unknown.");
2156 }
2157
2158
2159 }
2160
2161 ?>
2162