PluginProbe
WP-DBManager / 2.76
WP-DBManager v2.76
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.76, at wp-dbmanager.php

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