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

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