PluginProbe
WP-DBManager / 2.75
WP-DBManager v2.75
4.0.0 trunk 1.00 2.00 2.01 2.02 2.03 2.04 2.05 2.10 2.11 2.20 2.30 2.31 2.40 2.50 2.65 2.70 2.71 2.72 2.73 2.74 2.75 2.76 2.78 All 40 releases
wp-dbmanager / wp-dbmanager.php

wp-dbmanager.php in WP-DBManager 2.75, at wp-dbmanager.php

817 lines 36.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 Plugin Name: WP-DBManager
4 Plugin URI: http://lesterchan.net/portfolio/programming/php/
5 Description: Manages your WordPress database. Allows you to optimize database, repair database, backup database, restore database, delete backup database , drop/empty tables and run selected queries. Supports automatic scheduling of backing up, optimizing and repairing of database.
6 Version: 2.75
7 Author: Lester 'GaMerZ' Chan
8 Author URI: http://lesterchan.net
9 Text Domain: wp-dbmanager
10 */
11
12
13 /*
14 Copyright 2014 Lester Chan (email : lesterchan@gmail.com)
15
16 This program is free software; you can redistribute it and/or modify
17 it under the terms of the GNU General Public License as published by
18 the Free Software Foundation; either version 2 of the License, or
19 (at your option) any later version.
20
21 This program is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
25
26 You should have received a copy of the GNU General Public License
27 along with this program; if not, write to the Free Software
28 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 */
30
31
32 ### Create Text Domain For Translations
33 add_action( 'plugins_loaded', 'dbmanager_textdomain' );
34 function dbmanager_textdomain() {
35 load_plugin_textdomain( 'wp-dbmanager', false, dirname( plugin_basename( __FILE__ ) ) );
36 }
37
38
39 ### Function: Database Manager Menu
40 add_action('admin_menu', 'dbmanager_menu');
41 function dbmanager_menu() {
42 if (function_exists('add_menu_page')) {
43 add_menu_page(__('Database', 'wp-dbmanager'), __('Database', 'wp-dbmanager'), 'manage_database', 'wp-dbmanager/database-manager.php', '', 'dashicons-archive');
44 }
45 if (function_exists('add_submenu_page')) {
46 add_submenu_page('wp-dbmanager/database-manager.php', __('Backup DB', 'wp-dbmanager'), __('Backup DB', 'wp-dbmanager'), 'manage_database', 'wp-dbmanager/database-backup.php');
47 add_submenu_page('wp-dbmanager/database-manager.php', __('Manage Backup DB', 'wp-dbmanager'), __('Manage Backup DB', 'wp-dbmanager'), 'manage_database', 'wp-dbmanager/database-manage.php');
48 add_submenu_page('wp-dbmanager/database-manager.php', __('Optimize DB', 'wp-dbmanager'), __('Optimize DB', 'wp-dbmanager'), 'manage_database', 'wp-dbmanager/database-optimize.php');
49 add_submenu_page('wp-dbmanager/database-manager.php', __('Repair DB', 'wp-dbmanager'), __('Repair DB', 'wp-dbmanager'), 'manage_database', 'wp-dbmanager/database-repair.php');
50 add_submenu_page('wp-dbmanager/database-manager.php', __('Empty/Drop Tables', 'wp-dbmanager'), __('Empty/Drop Tables', 'wp-dbmanager'), 'manage_database', 'wp-dbmanager/database-empty.php');
51 add_submenu_page('wp-dbmanager/database-manager.php', __('Run SQL Query', 'wp-dbmanager'), __('Run SQL Query', 'wp-dbmanager'), 'manage_database', 'wp-dbmanager/database-run.php');
52 add_submenu_page('wp-dbmanager/database-manager.php', __('DB Options', 'wp-dbmanager'), __('DB Options', 'wp-dbmanager'), 'manage_database', 'wp-dbmanager/wp-dbmanager.php', 'dbmanager_options');
53 }
54 }
55
56
57 ### Funcion: Database Manager Cron
58 add_filter('cron_schedules', 'cron_dbmanager_reccurences');
59 add_action('dbmanager_cron_backup', 'cron_dbmanager_backup');
60 add_action('dbmanager_cron_optimize', 'cron_dbmanager_optimize');
61 add_action('dbmanager_cron_repair', 'cron_dbmanager_repair');
62 function cron_dbmanager_backup() {
63 $backup_options = get_option('dbmanager_options');
64 $backup_email = stripslashes($backup_options['backup_email']);
65 if(intval($backup_options['backup_period']) > 0) {
66 $backup = array();
67 $backup['date'] = current_time('timestamp');
68 $backup['mysqldumppath'] = $backup_options['mysqldumppath'];
69 $backup['mysqlpath'] = $backup_options['mysqlpath'];
70 $backup['path'] = $backup_options['path'];
71 $backup['host'] = DB_HOST;
72 $backup['port'] = '';
73 $backup['sock'] = '';
74 if(strpos(DB_HOST, ':') !== false) {
75 $db_host = explode(':', DB_HOST);
76 $backup['host'] = $db_host[0];
77 if(intval($db_host[1]) != 0) {
78 $backup['port'] = ' --port=' . escapeshellarg( intval( $db_host[1] ) );
79 } else {
80 $backup['sock'] = ' --socket=' . escapeshellarg( $db_host[1] );
81 }
82 }
83 $backup['command'] = '';
84 $brace = (substr(PHP_OS, 0, 3) == 'WIN') ? '"' : '';
85 if(intval($backup_options['backup_gzip']) == 1) {
86 $backup['filename'] = $backup['date'].'_-_'.DB_NAME.'.sql.gz';
87 $backup['filepath'] = $backup['path'].'/'.$backup['filename'];
88 $backup['command'] = escapeshellcmd( $brace . $backup['mysqldumppath'] . $brace ) . ' --force --host=' . escapeshellarg( $backup['host'] ).' --user=' . escapeshellarg( DB_USER ) . ' --password=' . escapeshellarg( DB_PASSWORD ) . $backup['port'] . $backup['sock'] . ' --add-drop-table --skip-lock-tables ' . DB_NAME . ' | gzip > '.escapeshellcmd( $brace . $backup['filepath'] . $brace );
89 } else {
90 $backup['filename'] = $backup['date'].'_-_'.DB_NAME.'.sql';
91 $backup['filepath'] = $backup['path'].'/'.$backup['filename'];
92 $backup['command'] = escapeshellcmd( $brace . $backup['mysqldumppath'] . $brace ) . ' --force --host=' . escapeshellarg( $backup['host'] ).' --user=' . escapeshellarg( DB_USER ). ' --password=' . escapeshellarg( DB_PASSWORD ) . $backup['port'] . $backup['sock'] . ' --add-drop-table --skip-lock-tables ' . DB_NAME . ' > '.escapeshellcmd( $brace . $backup['filepath'] . $brace );
93 }
94 execute_backup($backup['command']);
95 if( ! empty( $backup_email ) )
96 {
97 dbmanager_email_backup( $backup_email, $backup['filepath'] );
98 }
99 }
100 return;
101 }
102 function cron_dbmanager_optimize() {
103 global $wpdb;
104 $backup_options = get_option('dbmanager_options');
105 $optimize_period = intval($backup_options['optimize_period']);
106 if($optimize_period > 0) {
107 $optimize_tables = array();
108 $tables = $wpdb->get_col("SHOW TABLES");
109 foreach($tables as $table_name) {
110 $optimize_tables[] = '`'.$table_name.'`';
111 }
112 $wpdb->query('OPTIMIZE TABLE '.implode(',', $optimize_tables));
113 }
114 return;
115 }
116 function cron_dbmanager_repair() {
117 global $wpdb;
118 $backup_options = get_option('dbmanager_options');
119 $repair_period = intval($backup_options['repair_period']);
120 if($repair_period > 0) {
121 $repair_tables = array();
122 $tables = $wpdb->get_col("SHOW TABLES");
123 foreach($tables as $table_name) {
124 $repair_tables[] = '`'.$table_name.'`';
125 }
126 $wpdb->query('REPAIR TABLE '.implode(',', $repair_tables));
127 }
128 return;
129 }
130 function cron_dbmanager_reccurences($schedules) {
131 $backup_options = get_option('dbmanager_options');
132 $backup = intval($backup_options['backup'])*intval($backup_options['backup_period']);
133 $optimize = intval($backup_options['optimize'])*intval($backup_options['optimize_period']);
134 $repair = intval($backup_options['repair'])*intval($backup_options['repair_period']);
135 if($backup == 0) {
136 $backup = 31536000;
137 }
138 if($optimize == 0) {
139 $optimize = 31536000;
140 }
141 if($repair == 0) {
142 $repair = 31536000;
143 }
144 $schedules['dbmanager_backup'] = array('interval' => $backup, 'display' => __('WP-DBManager Backup Schedule', 'wp-dbmanager'));
145 $schedules['dbmanager_optimize'] = array('interval' => $optimize, 'display' => __('WP-DBManager Optimize Schedule', 'wp-dbmanager'));
146 $schedules['dbmanager_repair'] = array('interval' => $repair, 'display' => __('WP-DBManager Repair Schedule', 'wp-dbmanager'));
147 return $schedules;
148 }
149
150
151 ### Function: Ensure .htaccess Is In The Backup Folder
152 add_action( 'admin_notices', 'dbmanager_admin_notices' );
153 function dbmanager_admin_notices() {
154 $backup_options = get_option( 'dbmanager_options' );
155 $backup_folder_writable = ( is_dir( $backup_options['path'] ) && wp_is_writable( $backup_options['path'] ) );
156 $htaccess_exists = ( file_exists( $backup_options['path'] . '/.htaccess' ) );
157 $webconfig_exists = ( file_exists( $backup_options['path'] . '/Web.config' ) );
158 $index_exists = ( file_exists( $backup_options['path'] . '/index.php' ) );
159
160 if( ! isset( $backup_options['hide_admin_notices'] ) || intval( $backup_options['hide_admin_notices'] ) === 0 )
161 {
162 if( ! $backup_folder_writable || ! $index_exists || ( is_iis() && ! $webconfig_exists ) || ( ! is_iis() && ! $htaccess_exists ) ) {
163
164 echo '<div class="error">';
165 if( !$backup_folder_writable ) {
166 echo '<p style="font-weight: bold;">' . __( 'Your backup folder is NOT writable', 'wp-postratings') . '</p>';
167 echo '<p>'.sprintf( __( 'To correct this issue, make the folder <strong>%s</strong> writable.', 'wp-dbmanager' ), $backup_options['path'] ).'</p>';
168 }
169 if( ! $index_exists || ( is_iis() && ! $webconfig_exists ) || ( ! is_iis() && ! $htaccess_exists ) ) {
170 echo '<p style="font-weight: bold;">'.__( 'Your backup folder MIGHT be visible to the public', 'wp-dbmanager' ).'</p>';
171 }
172 if( is_iis() ) {
173 if( ! $webconfig_exists ) {
174 echo '<p>'.sprintf( __( 'To correct this issue, move the file from <strong>%s</strong> to <strong>%s</strong>', 'wp-dbmanager'), plugin_dir_path( __FILE__ ) . 'Web.config.txt', $backup_options['path'] .'/Web.config' ).'</p>';
175 }
176 } else {
177 if( ! $htaccess_exists ) {
178 echo '<p>'.sprintf( __( 'To correct this issue, move the file from <strong>%s</strong> to <strong>%s</strong>', 'wp-dbmanager'), plugin_dir_path( __FILE__ ) . 'htaccess.txt', $backup_options['path'] .'/.htaccess' ).'</p>';
179 }
180 }
181 if( ! $index_exists ) {
182 echo '<p>'.sprintf( __( 'To correct this issue, move the file from <strong>%s</strong> to <strong>%s</strong>', 'wp-dbmanager'), plugin_dir_path( __FILE__ ) . 'index.php', $backup_options['path'] .'/index.php' ).'</p>';
183 }
184 echo '<p>' . sprintf( __( '<a href="%s">Click here</a> to let WP-DBManager try to fix it', 'wp-dbmanager' ), wp_nonce_url( admin_url( 'admin.php?page=wp-dbmanager/database-backup.php&try_fix=1' ), 'wp-dbmanager_fix' ) ) . '</a></p>';
185 echo '</div>';
186 }
187 }
188 }
189
190
191 ### Function: Auto Detect MYSQL and MYSQL Dump Paths
192 function detect_mysql() {
193 global $wpdb;
194 $paths = array('mysq' => '', 'mysqldump' => '');
195 if(substr(PHP_OS,0,3) == 'WIN') {
196 $mysql_install = $wpdb->get_row("SHOW VARIABLES LIKE 'basedir'");
197 if($mysql_install) {
198 $install_path = str_replace('\\', '/', $mysql_install->Value);
199 $paths['mysql'] = $install_path.'bin/mysql.exe';
200 $paths['mysqldump'] = $install_path.'bin/mysqldump.exe';
201 } else {
202 $paths['mysql'] = 'mysql.exe';
203 $paths['mysqldump'] = 'mysqldump.exe';
204 }
205 } else {
206 if(function_exists('exec')) {
207 $paths['mysql'] = @exec('which mysql');
208 $paths['mysqldump'] = @exec('which mysqldump');
209 } else {
210 $paths['mysql'] = 'mysql';
211 $paths['mysqldump'] = 'mysqldump';
212 }
213 }
214 return $paths;
215 }
216
217 ### Function: Check if WordPress is installed on IIS
218 function is_iis() {
219 $server_software = strtolower( $_SERVER['SERVER_SOFTWARE'] );
220 if ( strpos( $server_software, 'microsoft-iis') !== false ) {
221 return true;
222 }
223
224 return false;
225 }
226
227 ### Executes OS-Dependent mysqldump Command (By: Vlad Sharanhovich)
228 function execute_backup($command) {
229 $backup_options = get_option('dbmanager_options');
230 check_backup_files();
231
232 if( realpath( $backup_options['path'] ) === false ) {
233 return sprintf( __( '%s is not a valid backup path', 'wp-dbmanager' ), stripslashes( $backup_options['path'] ) );
234 } else if( dbmanager_is_valid_path( $backup_options['mysqldumppath'] ) === 0 ) {
235 return sprintf( __( '%s is not a valid mysqldump path', 'wp-dbmanager' ), stripslashes( $backup_options['mysqldumppath'] ) );
236 } else if( dbmanager_is_valid_path( $backup_options['mysqlpath'] ) === 0 ) {
237 return sprintf( __( '%s is not a valid mysql path', 'wp-dbmanager' ), stripslashes( $backup_options['mysqlpath'] ) );
238 }
239
240 if( substr( PHP_OS, 0, 3 ) === 'WIN' ) {
241 $writable_dir = $backup_options['path'];
242 $tmpnam = $writable_dir.'/wp-dbmanager.bat';
243 $fp = fopen( $tmpnam, 'w' );
244 fwrite ($fp, $command );
245 fclose( $fp );
246 system( $tmpnam.' > NUL', $error );
247 unlink( $tmpnam );
248 } else {
249 passthru( $command, $error );
250 }
251 return $error;
252 }
253
254 ### Function: Check for valid file path
255 function dbmanager_is_valid_path( $path ) {
256 return preg_match( '/^[^*?"<>|;]*$/', $path );
257 }
258
259 ### Function: Email database backup
260 function dbmanager_email_backup($to = '', $backup_file_path)
261 {
262 if( is_email( $to ) && file_exists( $backup_file_path ) )
263 {
264 $backup_options = get_option( 'dbmanager_options' );
265
266 $file_name = basename( $backup_file_path );
267 $file_gmt_date = gmdate( 'Y-m-d H:i:s', substr( $file_name, 0, 10 ) );
268 $file_size = format_size( filesize( $backup_file_path) );
269 $file_date = mysql2date( sprintf( __( '%s @ %s', 'wp-dbmanager' ), get_option( 'date_format' ), get_option( 'time_format' ) ), $file_gmt_date );
270
271 $to = ( !empty( $to ) ? $to : get_option( 'admin_email' ) );
272
273 $subject = ( !empty( $backup_options['backup_email_subject'] ) ? $backup_options['backup_email_subject'] : dbmanager_default_options( 'backup_email_subject' ) );
274 $subject = str_replace(
275 array(
276 '%SITE_NAME%'
277 , '%POST_DATE%'
278 , '%POST_TIME%'
279 )
280 , array(
281 wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES )
282 , mysql2date( get_option( 'date_format' ), $file_gmt_date )
283 , mysql2date( get_option( 'time_format' ), $file_gmt_date )
284 )
285 , $subject
286 );
287 $message = __( 'Website Name:', 'wp-dbmanager').' '. wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ) . "\n".
288 __( 'Website URL:', 'wp-dbmanager' ).' '.get_bloginfo( 'url' )."\n".
289 __( 'Backup File Name:', 'wp-dbmanager' ).' '.$file_name."\n".
290 __( 'Backup File Date:', 'wp-dbmanager' ).' '.$file_date."\n".
291 __( 'Backup File Size:', 'wp-dbmanager' ).' '.$file_size."\n\n".
292 __( 'With Regards,', 'wp-dbmanager' )."\n".
293 wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ). ' ' . __('Administrator', 'wp-dbmanager' )."\n".
294 get_bloginfo('url');
295
296 $from = ( !empty( $backup_options['backup_email_from'] ) ? $backup_options['backup_email_from'] : dbmanager_default_options( 'backup_email_from' ) );
297 $from_name = ( !empty( $backup_options['backup_email_from_name'] ) ? $backup_options['backup_email_from_name'] : dbmanager_default_options( 'backup_email_from_name' ) );
298 $headers[] = 'From: "' . wp_specialchars_decode( stripslashes_deep( $from_name ), ENT_QUOTES ) . '" <'.$from.'>';
299
300 return wp_mail( $to, $subject, $message, $headers, $backup_file_path );
301 }
302
303 return false;
304 }
305
306
307 ### Function: Format Bytes Into KB/MB
308 if(!function_exists('format_size')) {
309 function format_size($rawSize) {
310 if($rawSize / 1073741824 > 1)
311 return number_format_i18n($rawSize/1048576, 1) . ' '.__('GiB', 'wp-dbmanager');
312 else if ($rawSize / 1048576 > 1)
313 return number_format_i18n($rawSize/1048576, 1) . ' '.__('MiB', 'wp-dbmanager');
314 else if ($rawSize / 1024 > 1)
315 return number_format_i18n($rawSize/1024, 1) . ' '.__('KiB', 'wp-dbmanager');
316 else
317 return number_format_i18n($rawSize, 0) . ' '.__('bytes', 'wp-dbmanager');
318 }
319 }
320
321
322 ### Function: Get File Extension
323 if(!function_exists('file_ext')) {
324 function file_ext($file_name) {
325 return substr(strrchr($file_name, '.'), 1);
326 }
327 }
328
329
330 ### Function: Check Folder Whether There Is Any File Inside
331 if(!function_exists('is_emtpy_folder')) {
332 function is_emtpy_folder($folder){
333 if(is_dir($folder) ){
334 $folder_content = '';
335 $handle = opendir($folder);
336 while( (gettype( $name = readdir($handle)) != 'boolean')){
337 if($name != '.htaccess') {
338 $name_array[] = $name;
339 }
340 }
341 foreach($name_array as $temp)
342 $folder_content .= $temp;
343
344 if($folder_content == '...')
345 return true;
346 else
347 return false;
348 closedir($handle);
349 }
350 else
351 return true;
352 }
353 }
354
355
356 ### Function: Make Sure Maximum Number Of Database Backup Files Does Not Exceed
357 function check_backup_files() {
358 $backup_options = get_option('dbmanager_options');
359 $database_files = array();
360 if(!is_emtpy_folder($backup_options['path'])) {
361 if ($handle = opendir($backup_options['path'])) {
362 while (false !== ($file = readdir($handle))) {
363 if ($file != '.' && $file != '..' && (file_ext($file) == 'sql' || file_ext($file) == 'gz')) {
364 $database_files[] = $file;
365 }
366 }
367 closedir($handle);
368 sort($database_files);
369 }
370 }
371 if(sizeof($database_files) >= $backup_options['max_backup']) {
372 @unlink($backup_options['path'].'/'.$database_files[0]);
373 }
374 }
375
376
377 ### Function: DBManager Default Options
378 function dbmanager_default_options( $option_name )
379 {
380 switch( $option_name )
381 {
382 case 'backup_email_from':
383 return get_option( 'admin_email' );
384 break;
385 case 'backup_email_from_name':
386 return wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ) .' '.__( 'Administrator', 'wp-dbmanager' );
387 break;
388 case 'backup_email_subject':
389 return __( '%SITE_NAME% Database Backup File For %POST_DATE% @ %POST_TIME%', 'wp-dbmanager' );
390 break;
391 case 'hide_admin_notices':
392 return 0;
393 break;
394 }
395 }
396
397 ### Function: Acticate Plugin
398 register_activation_hook( __FILE__, 'dbmanager_activation' );
399 function dbmanager_activation( $network_wide )
400 {
401 $auto = detect_mysql();
402 // Add Options
403 $option_name = 'dbmanager_options';
404 $option = array(
405 'mysqldumppath' => $auto['mysqldump']
406 , 'mysqlpath' => $auto['mysql']
407 , 'path' => str_replace( '\\', '/', WP_CONTENT_DIR ).'/backup-db'
408 , 'max_backup' => 10
409 , 'backup' => 1
410 , 'backup_gzip' => 0
411 , 'backup_period' => 604800
412 , 'backup_email' => get_option( 'admin_email' )
413 , 'backup_email_from' => dbmanager_default_options( 'backup_email_from' )
414 , 'backup_email_from_name' => dbmanager_default_options( 'backup_email_from_name' )
415 , 'backup_email_subject' => dbmanager_default_options( 'backup_email_subject' )
416 , 'optimize' => 3
417 , 'optimize_period' => 86400
418 , 'repair' => 2
419 , 'repair_period' => 604800
420 , 'hide_admin_notices' => 0
421 );
422
423 if ( is_multisite() && $network_wide )
424 {
425 $ms_sites = wp_get_sites();
426
427 if( 0 < sizeof( $ms_sites ) )
428 {
429 foreach ( $ms_sites as $ms_site )
430 {
431 switch_to_blog( $ms_site['blog_id'] );
432 add_option( $option_name, $option );
433 dbmanager_activate();
434 }
435 }
436
437 restore_current_blog();
438 }
439 else
440 {
441 add_option( $option_name, $option );
442 dbmanager_activate();
443 }
444 }
445
446 function dbmanager_activate() {
447 dbmanager_create_backup_folder();
448
449 // Set 'manage_database' Capabilities To Administrator
450 $role = get_role( 'administrator' );
451 if( ! $role->has_cap( 'manage_database') )
452 {
453 $role->add_cap( 'manage_database' );
454 }
455 }
456
457 function dbmanager_create_backup_folder() {
458 $plugin_path = plugin_dir_path( __FILE__ );
459 $backup_path = WP_CONTENT_DIR . '/backup-db';
460 $backup_options = get_option( 'dbmanager_options' );
461
462 if( ! empty( $backup_options['path'] ) ) {
463 $backup_path = $backup_options['path'];
464 }
465
466 // Create Backup Folder
467 if( is_dir( $backup_path ) && wp_is_writable( $backup_path ) )
468 {
469 if( wp_mkdir_p( $backup_path ) )
470 {
471 if( is_iis() ) {
472 if ( ! is_file( $backup_path . '/Web.config' ) ) {
473 @copy( $plugin_path . 'Web.config.txt', $backup_path . '/Web.config' );
474 }
475 } else {
476 if( ! is_file( $backup_path . '/.htaccess' ) ) {
477 @copy( $plugin_path . 'htaccess.txt', $backup_path . '/.htaccess' );
478 }
479 }
480 if( ! is_file( $backup_path . '/index.php' ) ) {
481 @copy( $plugin_path . 'index.php', $backup_path . '/index.php' );
482 }
483 @chmod( $backup_path, 0750 );
484 }
485 }
486 }
487
488 add_action( 'init', 'dbmanager_try_fix' );
489 function dbmanager_try_fix() {
490 if ( ! empty( $_GET['try_fix'] ) ) {
491 if ( intval( $_GET['try_fix'] ) === 1 ) {
492 check_admin_referer( 'wp-dbmanager_fix' );
493 dbmanager_create_backup_folder();
494 }
495 }
496 }
497
498
499 ### Function: Download Database
500 add_action('init', 'download_database');
501 function download_database() {
502 if(isset($_POST['do']) && $_POST['do'] == __('Download', 'wp-dbmanager') && !empty($_POST['database_file'])) {
503 if(strpos($_SERVER['HTTP_REFERER'], admin_url('admin.php?page=wp-dbmanager/database-manage.php')) !== false) {
504 $database_file = trim($_POST['database_file']);
505 if(substr($database_file, strlen($database_file) - 4, 4) == '.sql' || substr($database_file, strlen($database_file) - 7, 7) == '.sql.gz') {
506 check_admin_referer('wp-dbmanager_manage');
507 $backup_options = get_option('dbmanager_options');
508 $clean_file_name = sanitize_file_name($database_file);
509 $clean_file_name = str_replace('sql_.gz', 'sql.gz', $clean_file_name);
510 $file_path = $backup_options['path'].'/'.$clean_file_name;
511 header("Pragma: public");
512 header("Expires: 0");
513 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
514 header("Content-Type: application/force-download");
515 header("Content-Type: application/octet-stream");
516 header("Content-Type: application/download");
517 header("Content-Disposition: attachment; filename=".basename($file_path).";");
518 header("Content-Transfer-Encoding: binary");
519 header("Content-Length: ".filesize($file_path));
520 @readfile($file_path);
521 }
522 }
523 exit();
524 }
525 }
526
527 ### Function: Database Options
528 function dbmanager_options() {
529 $text = '';
530 $backup_options = get_option('dbmanager_options');
531 $old_backup_options = $backup_options;
532 if(!empty($_POST['Submit'])) {
533 check_admin_referer('wp-dbmanager_options');
534 $backup_options['mysqldumppath'] = sanitize_text_field( $_POST['db_mysqldumppath'] );
535 $backup_options['mysqlpath'] = sanitize_text_field( $_POST['db_mysqlpath'] );
536 $backup_options['path'] = sanitize_text_field( $_POST['db_path'] );
537 $backup_options['max_backup'] = intval( $_POST['db_max_backup'] );
538 $backup_options['backup'] = intval( $_POST['db_backup'] );
539 $backup_options['backup_gzip'] = intval( $_POST['db_backup_gzip'] );
540 $backup_options['backup_period'] = intval( $_POST['db_backup_period'] );
541 $backup_options['backup_email'] = sanitize_email( $_POST['db_backup_email'] );
542 $backup_options['backup_email_from'] = sanitize_email( $_POST['db_backup_email_from'] );
543 $backup_options['backup_email_from_name'] = sanitize_text_field( $_POST['db_backup_email_from_name'] );
544 $backup_options['backup_email_subject'] = sanitize_text_field( $_POST['db_backup_email_subject'] );
545 $backup_options['optimize'] = intval( $_POST['db_optimize'] );
546 $backup_options['optimize_period'] = intval( $_POST['db_optimize_period'] );
547 $backup_options['repair'] = intval( $_POST['db_repair'] );
548 $backup_options['repair_period'] = intval( $_POST['db_repair_period'] );
549 $backup_options['hide_admin_notices'] = intval( $_POST['db_hide_admin_notices'] );
550
551 if( realpath( $backup_options['path'] ) === false ) {
552 $text = '<div id="message" class="error"><p>' . sprintf( __( '%s is not a valid backup path', 'wp-dbmanager' ), stripslashes( $backup_options['path'] ) ) . '</p></div>';
553 $backup_options['path'] = $old_backup_options['path'];
554 } else if( dbmanager_is_valid_path( $backup_options['mysqldumppath'] ) === 0 ) {
555 $text = '<div id="message" class="error"><p>' . sprintf( __( '%s is not a valid mysqldump path', 'wp-dbmanager' ), stripslashes( $backup_options['mysqldumppath'] ) ) . '</p></div>';
556 $backup_options['mysqldumppath'] = $old_backup_options['mysqldumppath'];
557 } else if( dbmanager_is_valid_path( $backup_options['mysqlpath'] ) === 0 ) {
558 $text = '<div id="message" class="error"><p>' . sprintf( __( '%s is not a valid mysql path', 'wp-dbmanager' ), stripslashes( $backup_options['mysqlpath'] ) ) . '</p></div>';
559 $backup_options['mysqlpath'] = $old_backup_options['mysqlpath'];
560 }
561
562 $update_db_options = update_option( 'dbmanager_options', $backup_options );
563 if( $update_db_options ) {
564 $text = '<div id="message" class="updated"><p>' . __( 'Database Options Updated', 'wp-dbmanager' ) . '</p></div>';
565 }
566 if( empty( $text ) ) {
567 $text = '<div id="message" class="error"><p>' . __( 'No Database Option Updated', 'wp-dbmanager' ) . '</p></div>';
568 }
569 wp_clear_scheduled_hook( 'dbmanager_cron_backup' );
570 if( $backup_options['backup_period'] > 0 ) {
571 if ( ! wp_next_scheduled( 'dbmanager_cron_backup' ) ) {
572 wp_schedule_event( time(), 'dbmanager_backup', 'dbmanager_cron_backup' );
573 }
574 }
575 wp_clear_scheduled_hook( 'dbmanager_cron_optimize' );
576 if( $backup_options['optimize_period'] > 0 ) {
577 if ( ! wp_next_scheduled('dbmanager_cron_optimize' ) ) {
578 wp_schedule_event( time(), 'dbmanager_optimize', 'dbmanager_cron_optimize' );
579 }
580 }
581 wp_clear_scheduled_hook( 'dbmanager_cron_repair' );
582 if( $backup_options['repair_period'] > 0 ) {
583 if ( ! wp_next_scheduled( 'dbmanager_cron_repair' ) ) {
584 wp_schedule_event( time(), 'dbmanager_repair', 'dbmanager_cron_repair' );
585 }
586 }
587 }
588 $path = detect_mysql();
589
590 // Default Options
591 if( !isset( $backup_options['backup_email_from'] ) )
592 {
593 $backup_options['backup_email_from'] = dbmanager_default_options( 'backup_email_from' );
594 }
595 if( !isset( $backup_options['backup_email_from_name'] ) )
596 {
597 $backup_options['backup_email_from_name'] = dbmanager_default_options( 'backup_email_from_name' );
598 }
599 if( !isset( $backup_options['backup_email_subject'] ) )
600 {
601 $backup_options['backup_email_subject'] = dbmanager_default_options( 'backup_email_subject' );
602 }
603 if( !isset( $backup_options['hide_admin_notices'] ) )
604 {
605 $backup_options['hide_admin_notices'] = dbmanager_default_options( 'hide_admin_notices' );
606 }
607
608 ?>
609 <script type="text/javascript">
610 /* <![CDATA[*/
611 function mysqlpath() {
612 jQuery("#db_mysqlpath").val("<?php echo $path['mysql']; ?>");
613 }
614 function mysqldumppath() {
615 jQuery("#db_mysqldumppath").val("<?php echo $path['mysqldump']; ?>");
616 }
617 /* ]]> */
618 </script>
619 <?php if( ! empty( $text ) ) { echo $text; } ?>
620 <!-- Database Options -->
621 <form method="post" action="<?php echo admin_url('admin.php?page='.plugin_basename(__FILE__)); ?>">
622 <?php wp_nonce_field('wp-dbmanager_options'); ?>
623 <div class="wrap">
624 <h2><?php _e('Database Options', 'wp-dbmanager'); ?></h2>
625 <h3><?php _e('Paths', 'wp-dbmanager'); ?></h3>
626 <table class="form-table">
627 <tr>
628 <td width="20%" valign="top"><strong><?php _e('Path To mysqldump:', 'wp-dbmanager'); ?></strong></td>
629 <td width="80%">
630 <input type="text" id="db_mysqldumppath" name="db_mysqldumppath" size="60" maxlength="100" value="<?php echo stripslashes($backup_options['mysqldumppath']); ?>" dir="ltr" />&nbsp;&nbsp;<input type="button" value="<?php _e('Auto Detect', 'wp-dbmanager'); ?>" onclick="mysqldumppath();" />
631 <p><?php _e('The absolute path to mysqldump without trailing slash. If unsure, please email your server administrator about this.', 'wp-dbmanager'); ?></p>
632 </td>
633 </tr>
634 <tr>
635 <td valign="top"><strong><?php _e('Path To mysql:', 'wp-dbmanager'); ?></strong></td>
636 <td>
637 <input type="text" id="db_mysqlpath" name="db_mysqlpath" size="60" maxlength="100" value="<?php echo stripslashes($backup_options['mysqlpath']); ?>" dir="ltr" />&nbsp;&nbsp;<input type="button" value="<?php _e('Auto Detect', 'wp-dbmanager'); ?>" onclick="mysqlpath();" />
638 <p><?php _e('The absolute path to mysql without trailing slash. If unsure, please email your server administrator about this.', 'wp-dbmanager'); ?></p>
639 </td>
640 </tr>
641 <tr>
642 <td valign="top"><strong><?php _e('Path To Backup:', 'wp-dbmanager'); ?></strong></td>
643 <td>
644 <input type="text" name="db_path" size="60" maxlength="100" value="<?php echo stripslashes($backup_options['path']); ?>" dir="ltr" />
645 <p><?php _e('The absolute path to your database backup folder without trailing slash. Make sure the folder is writable.', 'wp-dbmanager'); ?></p>
646 </td>
647 </tr>
648 <tr>
649 <td valign="top"><strong><?php _e('Maximum Backup Files:', 'wp-dbmanager'); ?></strong></td>
650 <td>
651 <input type="text" name="db_max_backup" size="5" maxlength="5" value="<?php echo stripslashes($backup_options['max_backup']); ?>" />
652 <p><?php _e('The maximum number of database backup files that is allowed in the backup folder as stated above. The oldest database backup file is always deleted in order to maintain this value. This is to prevent the backup folder from getting too large.', 'wp-dbmanager'); ?></p>
653 </td>
654 </tr>
655 </table>
656
657 <h3><?php _e('Note', 'wp-dbmanager'); ?></h3>
658 <table class="form-table">
659 <tr>
660 <td>
661 <strong><?php _e('Windows Server', 'wp-dbmanager'); ?></strong><br />
662 <?php _e('For mysqldump path, you can try \'<strong>mysqldump.exe</strong>\'.', 'wp-dbmanager'); ?><br />
663 <?php _e('For mysql path, you can try \'<strong>mysql.exe</strong>\'.', 'wp-dbmanager'); ?>
664 </td>
665 </tr>
666 <tr>
667 <td>
668 <strong><?php _e('Linux Server', 'wp-dbmanager'); ?></strong><br />
669 <?php _e('For mysqldump path, normally is just \'<strong>mysqldump</strong>\'.', 'wp-dbmanager'); ?><br />
670 <?php _e('For mysql path, normally is just \'<strong>mysql</strong>\'.', 'wp-dbmanager'); ?>
671 </td>
672 </tr>
673 <tr>
674 <td>
675 <strong><?php _e('Note', 'wp-dbmanager'); ?></strong><br />
676 <?php _e('The \'Auto Detect\' function does not work for some servers. If it does not work for you, please contact your server administrator for the MYSQL and MYSQL DUMP paths.', 'wp-dbmanager'); ?>
677 </td>
678 </tr>
679 </table>
680
681 <h3><?php _e('Automatic Scheduling', 'wp-dbmanager'); ?></h3>
682 <table class="form-table">
683 <tr>
684 <td valign="top"><strong><?php _e('Automatic Backing Up Of DB:', 'wp-dbmanager'); ?></strong></td>
685 <td>
686 <?php
687 _e('Next backup date: ', 'wp-dbmanager');
688 if(wp_next_scheduled('dbmanager_cron_backup')) {
689 echo '<strong>'.mysql2date(sprintf(__('%s @ %s', 'wp-dbmanager'), get_option('date_format'), get_option('time_format')), gmdate('Y-m-d H:i:s', (wp_next_scheduled('dbmanager_cron_backup') + (get_option('gmt_offset') * 3600)))).'</strong>';
690 } else {
691 _e('N/A', 'wp-dbmanager');
692 }
693 ?>
694 <p>
695 <?php _e('Every', 'wp-dbmanager'); ?>&nbsp;<input type="text" name="db_backup" size="3" maxlength="5" value="<?php echo intval($backup_options['backup']); ?>" />&nbsp;
696 <select name="db_backup_period" size="1">
697 <option value="0"<?php selected('0', $backup_options['backup_period']); ?>><?php _e('Disable', 'wp-dbmanager'); ?></option>
698 <option value="60"<?php selected('60', $backup_options['backup_period']); ?>><?php _e('Minutes(s)', 'wp-dbmanager'); ?></option>
699 <option value="3600"<?php selected('3600', $backup_options['backup_period']); ?>><?php _e('Hour(s)', 'wp-dbmanager'); ?></option>
700 <option value="86400"<?php selected('86400', $backup_options['backup_period']); ?>><?php _e('Day(s)', 'wp-dbmanager'); ?></option>
701 <option value="604800"<?php selected('604800', $backup_options['backup_period']); ?>><?php _e('Week(s)', 'wp-dbmanager'); ?></option>
702 <option value="2592000"<?php selected('2592000', $backup_options['backup_period']); ?>><?php _e('Month(s)', 'wp-dbmanager'); ?></option>
703 </select>&nbsp;&nbsp;&nbsp;
704 <?php _e('Gzip', 'wp-dbmanager'); ?>
705 <select name="db_backup_gzip" size="1">
706 <option value="0"<?php selected('0', $backup_options['backup_gzip']); ?>><?php _e('No', 'wp-dbmanager'); ?></option>
707 <option value="1"<?php selected('1', $backup_options['backup_gzip']); ?>><?php _e('Yes', 'wp-dbmanager'); ?></option>
708 </select>
709 </p>
710 <p><?php _e('WP-DBManager can automatically backup your database after a certain period.', 'wp-dbmanager'); ?></p>
711 </td>
712 </tr>
713 <tr>
714 <td valign="top"><strong><?php _e('Automatic Optimizing Of DB:', 'wp-dbmanager'); ?></strong></td>
715 <td>
716 <?php
717 _e('Next optimize date: ', 'wp-dbmanager');
718 if(wp_next_scheduled('dbmanager_cron_optimize')) {
719 echo '<strong>'.mysql2date(sprintf(__('%s @ %s', 'wp-dbmanager'), get_option('date_format'), get_option('time_format')), gmdate('Y-m-d H:i:s', (wp_next_scheduled('dbmanager_cron_optimize') + (get_option('gmt_offset') * 3600)))).'</strong>';
720 } else {
721 _e('N/A', 'wp-dbmanager');
722 }
723 ?>
724 <p>
725 <?php _e('Every', 'wp-dbmanager'); ?>&nbsp;<input type="text" name="db_optimize" size="3" maxlength="5" value="<?php echo intval($backup_options['optimize']); ?>" />&nbsp;
726 <select name="db_optimize_period" size="1">
727 <option value="0"<?php selected('0', $backup_options['optimize_period']); ?>><?php _e('Disable', 'wp-dbmanager'); ?></option>
728 <option value="60"<?php selected('60', $backup_options['optimize_period']); ?>><?php _e('Minutes(s)', 'wp-dbmanager'); ?></option>
729 <option value="3600"<?php selected('3600', $backup_options['optimize_period']); ?>><?php _e('Hour(s)', 'wp-dbmanager'); ?></option>
730 <option value="86400"<?php selected('86400', $backup_options['optimize_period']); ?>><?php _e('Day(s)', 'wp-dbmanager'); ?></option>
731 <option value="604800"<?php selected('604800', $backup_options['optimize_period']); ?>><?php _e('Week(s)', 'wp-dbmanager'); ?></option>
732 <option value="2592000"<?php selected('2592000', $backup_options['optimize_period']); ?>><?php _e('Month(s)', 'wp-dbmanager'); ?></option>
733 </select>
734 </p>
735 <p><?php _e('WP-DBManager can automatically optimize your database after a certain period.', 'wp-dbmanager'); ?></p>
736 </td>
737 </tr>
738 <tr>
739 <td valign="top"><strong><?php _e('Automatic Repairing Of DB:', 'wp-dbmanager'); ?></strong></td>
740 <td>
741 <?php
742 _e('Next repair date: ', 'wp-dbmanager');
743 if(wp_next_scheduled('dbmanager_cron_repair')) {
744 echo '<strong>'.mysql2date(sprintf(__('%s @ %s', 'wp-dbmanager'), get_option('date_format'), get_option('time_format')), gmdate('Y-m-d H:i:s', (wp_next_scheduled('dbmanager_cron_repair') + (get_option('gmt_offset') * 3600)))).'</strong>';
745 } else {
746 _e('N/A', 'wp-dbmanager');
747 }
748 ?>
749 <p>
750 <?php _e('Every', 'wp-dbmanager'); ?>&nbsp;<input type="text" name="db_repair" size="3" maxlength="5" value="<?php echo intval($backup_options['repair']); ?>" />&nbsp;
751 <select name="db_repair_period" size="1">
752 <option value="0"<?php selected('0', $backup_options['repair_period']); ?>><?php _e('Disable', 'wp-dbmanager'); ?></option>
753 <option value="60"<?php selected('60', $backup_options['repair_period']); ?>><?php _e('Minutes(s)', 'wp-dbmanager'); ?></option>
754 <option value="3600"<?php selected('3600', $backup_options['repair_period']); ?>><?php _e('Hour(s)', 'wp-dbmanager'); ?></option>
755 <option value="86400"<?php selected('86400', $backup_options['repair_period']); ?>><?php _e('Day(s)', 'wp-dbmanager'); ?></option>
756 <option value="604800"<?php selected('604800', $backup_options['repair_period']); ?>><?php _e('Week(s)', 'wp-dbmanager'); ?></option>
757 <option value="2592000"<?php selected('2592000', $backup_options['repair_period']); ?>><?php _e('Month(s)', 'wp-dbmanager'); ?></option>
758 </select>
759 </p>
760 <p><?php _e('WP-DBManager can automatically repair your database after a certain period.', 'wp-dbmanager'); ?></p>
761 </td>
762 </tr>
763 </table>
764
765 <h3><?php _e('Backup Email Options', 'wp-dbmanager'); ?></h3>
766 <table class="form-table">
767 <tr>
768 <td valign="top"><strong><?php _e('To', 'wp-dbmanager'); ?></strong></td>
769 <td>
770 <p>
771 <input type="text" name="db_backup_email" size="30" maxlength="250" placeholder="<?php _e ( 'To E-mail', 'wp-dbmanager' ); ?>" value="<?php echo esc_attr( stripslashes( $backup_options['backup_email'] ) ) ?>" dir="ltr" />
772 </p>
773 <p><?php _e('(Leave blank to disable this feature)', 'wp-dbmanager'); ?></p>
774 </td>
775 </tr>
776 <tr>
777 <td valign="top"><strong><?php _e('From', 'wp-dbmanager'); ?></strong></td>
778 <td>
779 <p>
780 <input type="text" name="db_backup_email_from_name" size="60" maxlength="250" placeholder="<?php _e ( 'From Name', 'wp-dbmanager' ); ?>" value="<?php echo esc_attr( stripslashes( $backup_options['backup_email_from_name'] ) ) ?>" dir="ltr" />&nbsp;
781 &lt;<input type="text" name="db_backup_email_from" size="30" maxlength="250" placeholder="<?php _e ( 'From E-mail', 'wp-dbmanager' ); ?>" value="<?php echo esc_attr( stripslashes( $backup_options['backup_email_from'] ) ) ?>" dir="ltr" />&gt;
782 </p>
783 <p><?php _e('(Leave blank to use the default)', 'wp-dbmanager'); ?></p>
784 </td>
785 </tr>
786 <tr>
787 <td valign="top"><strong><?php _e('Subject:', 'wp-dbmanager'); ?></strong></td>
788 <td>
789 <p>
790 <input type="text" name="db_backup_email_subject" size="90" maxlength="255" placeholder="<?php _e ( 'Subject', 'wp-dbmanager' ); ?>" value="<?php echo esc_attr( stripslashes( $backup_options['backup_email_subject'] ) ) ?>" dir="ltr" />
791 </p>
792 <p><?php _e('(Leave blank to use the default)', 'wp-dbmanager'); ?></p>
793 </td>
794 </tr>
795 </table>
796
797 <h3><?php _e('Miscellaneous Options', 'wp-dbmanager'); ?></h3>
798 <table class="form-table">
799 <tr>
800 <td valign="top"><strong><?php _e('Hide Admin Notices', 'wp-dbmanager'); ?></strong></td>
801 <td>
802 <p>
803 <input type="radio" name="db_hide_admin_notices" value="1"<?php echo (intval( $backup_options['hide_admin_notices'] ) === 1 ? ' checked="checked"' : '' ); ?> />&nbsp;<?php _e('Yes', 'wp-dbmanager'); ?>
804 <input type="radio" name="db_hide_admin_notices" value="0"<?php echo (intval( $backup_options['hide_admin_notices'] ) === 0 ? ' checked="checked"' : '' ); ?> />&nbsp;<?php _e('No', 'wp-dbmanager'); ?>
805 </p>
806 </td>
807 </tr>
808 </table>
809
810 <p class="submit">
811 <input type="submit" name="Submit" class="button" value="<?php _e('Save Changes', 'wp-dbmanager'); ?>" />
812 </p>
813 </div>
814 </form>
815 <?php
816 }
817 ?>