| 1 |
<?php |
| 2 |
### Check Whether User Can Manage Database |
| 3 |
if( ! current_user_can('edit_files') ) { |
| 4 |
die('Access Denied'); |
| 5 |
} |
| 6 |
|
| 7 |
|
| 8 |
### Variables Variables Variables |
| 9 |
$base_name = plugin_basename('wp-dbmanager/database-manager.php'); |
| 10 |
$base_page = 'admin.php?page='.$base_name; |
| 11 |
$current_date = mysql2date(sprintf(__('%s @ %s', 'wp-dbmanager'), get_option('date_format'), get_option('time_format')), gmdate('Y-m-d H:i:s', current_time('timestamp'))); |
| 12 |
$backup = array(); |
| 13 |
$backup_options = get_option('dbmanager_options'); |
| 14 |
$backup['date'] = current_time('timestamp'); |
| 15 |
$backup['mysqldumppath'] = $backup_options['mysqldumppath']; |
| 16 |
$backup['mysqlpath'] = $backup_options['mysqlpath']; |
| 17 |
$backup['path'] = $backup_options['path']; |
| 18 |
$backup['charset'] = ' --default-character-set="utf8mb4"'; |
| 19 |
|
| 20 |
### Form Processing |
| 21 |
if(!empty($_POST['do'])) { |
| 22 |
$text = ''; |
| 23 |
// Decide What To Do |
| 24 |
switch($_POST['do']) { |
| 25 |
case __('Backup', 'wp-dbmanager'): |
| 26 |
check_admin_referer('wp-dbmanager_backup'); |
| 27 |
$brace = 0 === strpos( PHP_OS, 'WIN' ) ? '"' : ''; |
| 28 |
$backup['host'] = DB_HOST; |
| 29 |
$backup['port'] = ''; |
| 30 |
$backup['sock'] = ''; |
| 31 |
if ( strpos( DB_HOST, ':' ) !== false ) { |
| 32 |
$db_host = explode(':', DB_HOST); |
| 33 |
$backup['host'] = $db_host[0]; |
| 34 |
if ( (int) $db_host[1] !== 0) { |
| 35 |
$backup['port'] = ' --port=' . escapeshellarg( (int) $db_host[1] ); |
| 36 |
} else { |
| 37 |
$backup['sock'] = ' --socket=' . escapeshellarg( $db_host[1] ); |
| 38 |
} |
| 39 |
} |
| 40 |
$gzip = isset( $_POST['gzip'] ) ? (int) $_POST['gzip'] : 0; |
| 41 |
$backup['filename'] = $backup['date'] . '_-_' . DB_NAME . '.sql'; |
| 42 |
if ( $gzip === 1 ) { |
| 43 |
$backup['filename'] .= '.gz'; |
| 44 |
$backup['filepath'] = $backup['path'] . '/' . $backup['filename']; |
| 45 |
do_action( 'wp_dbmanager_before_escapeshellcmd' ); |
| 46 |
$backup['command'] = $brace . escapeshellcmd( $backup['mysqldumppath'] ) . $brace . ' --force --host=' . escapeshellarg( $backup['host'] ) . ' --user=' . escapeshellarg( DB_USER ) . ' --password=' . escapeshellarg( DB_PASSWORD ) . $backup['port'] . $backup['sock'] . $backup['charset'] . ' --add-drop-table --skip-lock-tables ' . DB_NAME . ' | gzip > ' . $brace . escapeshellcmd( $backup['filepath'] ) . $brace; |
| 47 |
} else { |
| 48 |
$backup['filepath'] = $backup['path'] . '/' . $backup['filename']; |
| 49 |
do_action( 'wp_dbmanager_before_escapeshellcmd' ); |
| 50 |
$backup['command'] = $brace . escapeshellcmd( $backup['mysqldumppath'] ) . $brace . ' --force --host=' . escapeshellarg( $backup['host'] ) . ' --user=' . escapeshellarg( DB_USER ) . ' --password=' . escapeshellarg( DB_PASSWORD ) . $backup['port'] . $backup['sock'] . $backup['charset'] . ' --add-drop-table --skip-lock-tables ' . DB_NAME . ' > ' . $brace . escapeshellcmd( $backup['filepath'] ) . $brace; |
| 51 |
} |
| 52 |
$error = execute_backup( $backup['command'] ); |
| 53 |
if ( ! is_writable( $backup['path'] ) ) { |
| 54 |
$text = '<p style="color: red;">'.sprintf(__('Database Failed To Backup On \'%s\'. Backup Folder Not Writable.', 'wp-dbmanager'), $current_date).'</p>'; |
| 55 |
} elseif ( is_file( $backup['filepath'] ) && filesize( $backup['filepath'] ) === 0 ) { |
| 56 |
$text = '<p style="color: red;">'.sprintf(__('Database Failed To Backup On \'%s\'. Backup File Size Is 0KB.', 'wp-dbmanager'), $current_date).'</p>'; |
| 57 |
} elseif ( ! is_file( $backup['filepath'] ) ) { |
| 58 |
$text = '<p style="color: red;">'.sprintf(__('Database Failed To Backup On \'%s\'. Invalid Backup File Path.', 'wp-dbmanager'), $current_date).'</p>'; |
| 59 |
} elseif ( $error ) { |
| 60 |
$text = '<p style="color: red;">'.sprintf(__('Database Failed To Backup On \'%s\'.', 'wp-dbmanager'), $current_date).'</p>'; |
| 61 |
} else { |
| 62 |
rename( $backup['filepath'], $backup['path'] . '/' . md5_file( $backup['filepath'] ) . '_-_' . $backup['filename'] ); |
| 63 |
$text = '<p style="color: green;">'.sprintf(__('Database Backed Up Successfully On \'%s\'.', 'wp-dbmanager'), $current_date).'</p>'; |
| 64 |
} |
| 65 |
break; |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
### Backup File Name |
| 70 |
$backup['filename'] = $backup['date'].'_-_'.DB_NAME.'.sql'; |
| 71 |
$backup_path = stripslashes( $backup['path'] ); |
| 72 |
|
| 73 |
### MYSQL Base Dir |
| 74 |
$has_error = false; |
| 75 |
$disabled_function = false; |
| 76 |
?> |
| 77 |
<?php if( ! empty( $text ) ) { echo '<div id="message" class="updated">'.$text.'</div>'; } ?> |
| 78 |
<!-- Checking Backup Status --> |
| 79 |
<div class="wrap"> |
| 80 |
<h2><?php _e('Backup Database', 'wp-dbmanager'); ?></h2> |
| 81 |
<h3><?php _e('Checking Security Status', 'wp-dbmanager'); ?></h3> |
| 82 |
<p> |
| 83 |
<?php |
| 84 |
if( is_iis() ) { |
| 85 |
if ( ! is_file( $backup_path . '/Web.config' ) ) { |
| 86 |
echo '<p style="color: red;">' . sprintf( __( 'Web.config is missing from %s', 'wp-dbmanager' ), $backup_path ) . '</p>'; |
| 87 |
$has_error = true; |
| 88 |
} else { |
| 89 |
echo '<p style="color: green;">' . sprintf( __( 'Web.config is present in %s', 'wp-dbmanager' ), $backup_path ) . '</p>'; |
| 90 |
} |
| 91 |
} else { |
| 92 |
if( ! is_file( $backup_path . '/.htaccess' ) ) { |
| 93 |
echo '<p style="color: red;">' . sprintf( __( '.htaccess is missing from %s', 'wp-dbmanager' ), $backup_path ) . '</p>'; |
| 94 |
$has_error = true; |
| 95 |
} else { |
| 96 |
echo '<p style="color: green;">' . sprintf( __( '.htaccess is present in %s', 'wp-dbmanager' ), $backup_path ) . '</p>'; |
| 97 |
} |
| 98 |
} |
| 99 |
if( ! is_file( $backup_path . '/index.php' ) ) { |
| 100 |
echo '<p style="color: red;">' . sprintf( __( 'index.php is missing from %s', 'wp-dbmanager' ), $backup_path ) . '</p>'; |
| 101 |
$has_error = true; |
| 102 |
} else { |
| 103 |
echo '<p style="color: green;">' . sprintf( __( 'index.php is present in %s', 'wp-dbmanager' ), $backup_path ) . '</p>'; |
| 104 |
} |
| 105 |
?> |
| 106 |
</p> |
| 107 |
<h3><?php _e('Checking Backup Status', 'wp-dbmanager'); ?></h3> |
| 108 |
<p> |
| 109 |
<?php _e('Checking Backup Folder', 'wp-dbmanager'); ?> <span dir="ltr">(<strong><?php echo $backup_path; ?></strong>)</span> ...<br /> |
| 110 |
<?php |
| 111 |
if( realpath( $backup_path ) === false ) { |
| 112 |
echo '<p style="color: red;">' . sprintf( __( '%s is not a valid backup path', 'wp-dbmanager' ), $backup_path ) . '</p>'; |
| 113 |
$has_error = true; |
| 114 |
} else { |
| 115 |
if ( @is_dir( $backup_path ) ) { |
| 116 |
echo '<p style="color: green;">' . __('Backup folder exists', 'wp-dbmanager') . '</p>'; |
| 117 |
} else { |
| 118 |
echo '<p style="color: red;">' . sprintf(__('Backup folder does NOT exist. Please create \'backup-db\' folder in \'%s\' folder and CHMOD it to \'777\' or change the location of the backup folder under DB Option.', 'wp-dbmanager'), WP_CONTENT_DIR) . '</p>'; |
| 119 |
$has_error = true; |
| 120 |
} |
| 121 |
if ( @is_writable( $backup_path ) ) { |
| 122 |
echo '<p style="color: green;">' . __('Backup folder is writable', 'wp-dbmanager') . '</p>'; |
| 123 |
} else { |
| 124 |
echo '<p style="color: red;">' . __('Backup folder is NOT writable. Please CHMOD it to \'777\'.', 'wp-dbmanager') . '</p>'; |
| 125 |
$has_error = true; |
| 126 |
} |
| 127 |
} |
| 128 |
?> |
| 129 |
</p> |
| 130 |
<p> |
| 131 |
<?php |
| 132 |
if( dbmanager_is_valid_path( $backup['mysqldumppath'] ) === 0 ) { |
| 133 |
echo '<p style="color: red;">' . sprintf( __( '%s is not a valid backup mysqldump path', 'wp-dbmanager' ), stripslashes( $backup['mysqldumppath'] ) ) . '</p>'; |
| 134 |
$has_error = true; |
| 135 |
} else { |
| 136 |
if ( @file_exists( stripslashes( $backup['mysqldumppath'] ) ) ) { |
| 137 |
echo __('Checking MYSQL Dump Path', 'wp-dbmanager') . ' <span dir="ltr">(<strong>' . stripslashes( $backup['mysqldumppath'] ) . '</strong>)</span> ...<br />'; |
| 138 |
echo '<p style="color: green;">' . __('MYSQL dump path exists.', 'wp-dbmanager') . '</p>'; |
| 139 |
} else { |
| 140 |
echo __('Checking MYSQL Dump Path', 'wp-dbmanager') . ' ...<br />'; |
| 141 |
echo '<p style="color: red;">' . __('MYSQL dump path does NOT exist. Please check your mysqldump path under DB Options. If uncertain, contact your server administrator.', 'wp-dbmanager') . '</p>'; |
| 142 |
$has_error = true; |
| 143 |
} |
| 144 |
} |
| 145 |
?> |
| 146 |
</p> |
| 147 |
<p> |
| 148 |
<?php |
| 149 |
if( dbmanager_is_valid_path( $backup['mysqlpath'] ) === 0 ) { |
| 150 |
echo '<p style="color: red;">' . sprintf( __( '%s is not a valid backup mysql path', 'wp-dbmanager' ), stripslashes( $backup['mysqlpath'] ) ) . '</p>'; |
| 151 |
$has_error = true; |
| 152 |
} else { |
| 153 |
if ( @file_exists( stripslashes($backup['mysqlpath'] ) ) ) { |
| 154 |
echo __('Checking MYSQL Path', 'wp-dbmanager') . ' <span dir="ltr">(<strong>' . stripslashes($backup['mysqlpath']) . '</strong>)</span> ...<br />'; |
| 155 |
echo '<p style="color: green;">' . __('MYSQL path exists.', 'wp-dbmanager') . '</p>'; |
| 156 |
} else { |
| 157 |
echo __('Checking MYSQL Path', 'wp-dbmanager') . ' ...<br />'; |
| 158 |
echo '<p style="color: red;">' . __('MYSQL path does NOT exist. Please check your mysql path under DB Options. If uncertain, contact your server administrator.', 'wp-dbmanager') . '</p>'; |
| 159 |
$has_error = true; |
| 160 |
} |
| 161 |
} |
| 162 |
?> |
| 163 |
</p> |
| 164 |
<p> |
| 165 |
<?php _e('Checking PHP Functions', 'wp-dbmanager'); ?> <span dir="ltr">(<strong>passthru()</strong>, <strong>system()</strong> <?php _e('and', 'wp-dbmanager'); ?> <strong>exec()</strong>)</span> ...<br /> |
| 166 |
<?php |
| 167 |
if( dbmanager_is_function_disabled( 'passthru' ) ) { |
| 168 |
echo '<p style="color: red;"><span dir="ltr">passthru()</span> '.__('disabled', 'wp-dbmanager').'.</p>'; |
| 169 |
$disabled_function = true; |
| 170 |
} else if( ! function_exists( 'passthru' ) ) { |
| 171 |
echo '<p style="color: red;"><span dir="ltr">passthru()</span> '.__('missing', 'wp-dbmanager').'.</p>'; |
| 172 |
$disabled_function = true; |
| 173 |
} else { |
| 174 |
echo '<p style="color: green;"><span dir="ltr">passthru()</span> '.__('enabled', 'wp-dbmanager').'.</p>'; |
| 175 |
} |
| 176 |
if( dbmanager_is_function_disabled( 'system' ) ) { |
| 177 |
echo '<p style="color: red;"><span dir="ltr">system()</span> '.__('disabled', 'wp-dbmanager').'.</p>'; |
| 178 |
$disabled_function = true; |
| 179 |
} else if( ! function_exists( 'system' ) ) { |
| 180 |
echo '<p style="color: red;"><span dir="ltr">system()</span> '.__('missing', 'wp-dbmanager').'.</p>'; |
| 181 |
$disabled_function = true; |
| 182 |
} else { |
| 183 |
echo '<p style="color: green;"><span dir="ltr">system()</span> '.__('enabled', 'wp-dbmanager').'.</p>'; |
| 184 |
} |
| 185 |
if( dbmanager_is_function_disabled( 'exec' ) ) { |
| 186 |
echo '<p style="color: red;"><span dir="ltr">exec()</span> '.__('disabled', 'wp-dbmanager').'.</p>'; |
| 187 |
$disabled_function = true; |
| 188 |
} else if( ! function_exists( 'exec' ) ) { |
| 189 |
echo '<p style="color: red;"><span dir="ltr">exec()</span> '.__('missing', 'wp-dbmanager').'.</p>'; |
| 190 |
$disabled_function = true; |
| 191 |
} else { |
| 192 |
echo '<p style="color: green;"><span dir="ltr">exec()</span> '.__('enabled', 'wp-dbmanager').'.</p>'; |
| 193 |
} |
| 194 |
?> |
| 195 |
</p> |
| 196 |
<p> |
| 197 |
<?php |
| 198 |
if( $disabled_function ) { |
| 199 |
echo '<strong><p style="color: red;">' . __( 'I\'m sorry, your server administrator has disabled passthru(), system() and/or exec(), thus you cannot use this plugin. Please find an alternative plugin.', 'wp-dbmanager' ) . '</p></strong>'; |
| 200 |
} else if( ! $has_error ) { |
| 201 |
echo '<strong><p style="color: green;">'.__('Excellent. You Are Good To Go.', 'wp-dbmanager').'</p></strong>'; |
| 202 |
} else { |
| 203 |
echo '<strong><p style="color: red;">'.__('Please Rectify The Error Highlighted In Red Before Proceeding On.', 'wp-dbmanager').'</p></strong>'; |
| 204 |
} |
| 205 |
?> |
| 206 |
</p> |
| 207 |
<p><i><?php _e('Note: The checking of backup status is still undergoing testing, it may not be accurate.', 'wp-dbmanager'); ?></i></p> |
| 208 |
</div> |
| 209 |
<!-- Backup Database --> |
| 210 |
<form method="post" action="<?php echo admin_url('admin.php?page='.plugin_basename(__FILE__)); ?>"> |
| 211 |
<?php wp_nonce_field('wp-dbmanager_backup'); ?> |
| 212 |
<div class="wrap"> |
| 213 |
<h3><?php _e('Backup Database', 'wp-dbmanager'); ?></h3> |
| 214 |
<br style="clear" /> |
| 215 |
<table class="widefat"> |
| 216 |
<thead> |
| 217 |
<tr> |
| 218 |
<th><?php _e('Option', 'wp-dbmanager'); ?></th> |
| 219 |
<th><?php _e('Value', 'wp-dbmanager'); ?></th> |
| 220 |
</tr> |
| 221 |
</thead> |
| 222 |
<tr> |
| 223 |
<th><?php _e('Database Name:', 'wp-dbmanager'); ?></th> |
| 224 |
<td><?php echo DB_NAME; ?></td> |
| 225 |
</tr> |
| 226 |
<tr style="background-color: #eee;"> |
| 227 |
<th><?php _e('Database Backup To:', 'wp-dbmanager'); ?></th> |
| 228 |
<td><span dir="ltr"><?php echo $backup_path; ?></span></td> |
| 229 |
</tr> |
| 230 |
<tr> |
| 231 |
<th><?php _e('Database Backup Date:', 'wp-dbmanager'); ?></th> |
| 232 |
<td><?php echo mysql2date(sprintf(__('%s @ %s', 'wp-dbmanager'), get_option('date_format'), get_option('time_format')), gmdate('Y-m-d H:i:s', $backup['date'])); ?></td> |
| 233 |
</tr> |
| 234 |
<tr style="background-color: #eee;"> |
| 235 |
<th><?php _e('Database Backup File Name:', 'wp-dbmanager'); ?></th> |
| 236 |
<td><span dir="ltr"><?php echo $backup['filename']; ?></span></td> |
| 237 |
</tr> |
| 238 |
<tr> |
| 239 |
<th><?php _e('Database Backup Type:', 'wp-dbmanager'); ?></th> |
| 240 |
<td><?php _e('Full (Structure and Data)', 'wp-dbmanager'); ?></td> |
| 241 |
</tr> |
| 242 |
<tr style="background-color: #eee;"> |
| 243 |
<th><?php _e('MYSQL Dump Location:', 'wp-dbmanager'); ?></th> |
| 244 |
<td><span dir="ltr"><?php echo stripslashes($backup['mysqldumppath']); ?></span></td> |
| 245 |
</tr> |
| 246 |
<tr> |
| 247 |
<th><?php _e('GZIP Database Backup File?', 'wp-dbmanager'); ?></th> |
| 248 |
<td><input type="radio" id="gzip-yes" name="gzip" value="1" /> <label for="gzip-yes"><?php _e('Yes', 'wp-dbmanager'); ?></label> <input type="radio" id="gzip-no" name="gzip" value="0" checked="checked" /> <label for="gzip-no"><?php _e('No', 'wp-dbmanager'); ?></label></td> |
| 249 |
</tr> |
| 250 |
<tr> |
| 251 |
<td colspan="2" align="center"><input type="submit" name="do" value="<?php _e('Backup', 'wp-dbmanager'); ?>" class="button" /> <input type="button" name="cancel" value="<?php _e('Cancel', 'wp-dbmanager'); ?>" class="button" onclick="javascript:history.go(-1)" /></td> |
| 252 |
</tr> |
| 253 |
</table> |
| 254 |
</div> |
| 255 |
</form> |
| 256 |
|