~clint-fewbar/charms/oneiric/omgubuntu-wp/trunk

« back to all changes in this revision

Viewing changes to files/wordpress/wp-content/plugins/wp-dbmanager/database-manage.php

  • Committer: Marco Ceppi
  • Date: 2012-03-19 14:27:51 UTC
  • Revision ID: marco@ceppi.net-20120319142751-xvn4zfl0rq99r00p
First revision of OMG charm

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/*
 
3
+----------------------------------------------------------------+
 
4
|                                                                                                                                                                                       |
 
5
|       WordPress 2.8 Plugin: WP-DBManager 2.63                                                         |
 
6
|       Copyright (c) 2009 Lester "GaMerZ" Chan                                                                 |
 
7
|                                                                                                                                                                                       |
 
8
|       File Written By:                                                                                                                                        |
 
9
|       - Lester "GaMerZ" Chan                                                                                                                  |
 
10
|       - http://lesterchan.net                                                                                                                 |
 
11
|                                                                                                                                                                                       |
 
12
|       File Information:                                                                                                                                       |
 
13
|       - Database Restore                                                                                                                              |
 
14
|       - wp-content/plugins/wp-dbmanager/database-restore.php                  |
 
15
|                                                                                                                                                                                       |
 
16
+----------------------------------------------------------------+
 
17
*/
 
18
 
 
19
 
 
20
### Check Whether User Can Manage Database
 
21
if(!current_user_can('manage_database')) {
 
22
        die('Access Denied');
 
23
}
 
24
 
 
25
 
 
26
### Variables Variables Variables
 
27
$base_name = plugin_basename('wp-dbmanager/database-manager.php');
 
28
$base_page = 'admin.php?page='.$base_name;
 
29
$backup = array();
 
30
$backup_options = get_option('dbmanager_options');
 
31
$backup['date'] = current_time('timestamp');
 
32
$backup['mysqldumppath'] = $backup_options['mysqldumppath'];
 
33
$backup['mysqlpath'] = $backup_options['mysqlpath'];
 
34
$backup['path'] = $backup_options['path'];
 
35
$backup['password'] = str_replace('$', '\$', DB_PASSWORD);
 
36
 
 
37
 
 
38
### Form Processing 
 
39
if($_POST['do']) {
 
40
        check_admin_referer('wp-dbmanager_manage');
 
41
        // Lets Prepare The Variables
 
42
        $database_file = trim($_POST['database_file']);
 
43
        $nice_file_date = mysql2date(sprintf(__('%s @ %s', 'wp-dbmanager'), get_option('date_format'), get_option('time_format')), gmdate('Y-m-d H:i:s', substr($database_file, 0, 10)));
 
44
 
 
45
        // Decide What To Do
 
46
        switch($_POST['do']) {
 
47
                case __('Restore', 'wp-dbmanager'):
 
48
                        if(!empty($database_file)) {
 
49
                                $brace = (substr(PHP_OS, 0, 3) == 'WIN') ? '"' : '';
 
50
                                $backup['host'] = DB_HOST;
 
51
                                $backup['port'] = '';
 
52
                                $backup['sock'] = '';   
 
53
                                if(strpos(DB_HOST, ':') !== false) {
 
54
                                        $db_host = explode(':', DB_HOST);
 
55
                                        $backup['host'] = $db_host[0];
 
56
                                        if(is_int($db_host[1])) {
 
57
                                                $backup['port'] = ' --port="'.intval($db_host[1]).'"';
 
58
                                        } else {
 
59
                                                $backup['sock'] = ' --socket="'.$db_host[1].'"';
 
60
                                        }
 
61
                                }
 
62
                                if(stristr($database_file, '.gz')) {
 
63
                                        $backup['command'] = 'gunzip < '.$brace.$backup['path'].'/'.$database_file.$brace.' | '.$brace.$backup['mysqlpath'].$brace.' --host="'.$backup['host'].'" --user="'.DB_USER.'" --password="'.$backup['password'].'"'.$backup['port'].$backup['sock'].' '.DB_NAME;
 
64
                                } else {
 
65
                                        $backup['command'] = $brace.$backup['mysqlpath'].$brace.' --host="'.$backup['host'].'" --user="'.DB_USER.'" --password="'.$backup['password'].'"'.$backup['port'].$backup['sock'].' '.DB_NAME.' < '.$brace.$backup['path'].'/'.$database_file.$brace;
 
66
                                }
 
67
                                passthru($backup['command'], $error);
 
68
                                if($error) {
 
69
                                        $text = '<font color="red">'.sprintf(__('Database On \'%s\' Failed To Restore', 'wp-dbmanager'), $nice_file_date).'</font>';
 
70
                                } else {
 
71
                                        $text = '<font color="green">'.sprintf(__('Database On \'%s\' Restored Successfully', 'wp-dbmanager'), $nice_file_date).'</font>';
 
72
                                }
 
73
                        } else {
 
74
                                $text = '<font color="red">'.__('No Backup Database File Selected', 'wp-dbmanager').'</font>';
 
75
                        }
 
76
                        break;
 
77
                case __('E-Mail', 'wp-dbmanager'):
 
78
                        if(!empty($database_file)) {
 
79
                                // Get And Read The Database Backup File
 
80
                                $file_path = $backup['path'].'/'.$database_file;
 
81
                                $file_size = format_size(filesize($file_path));
 
82
                                $file_date = $nice_file_date;
 
83
                                $file = fopen($file_path,'rb');
 
84
                                $file_data = fread($file,filesize($file_path));
 
85
                                fclose($file);
 
86
                                $file_data = chunk_split(base64_encode($file_data));
 
87
                                // Create Mail To, Mail Subject And Mail Header
 
88
                                if(!empty($_POST['email_to'])) {
 
89
                                        $mail_to = trim($_POST['email_to']);
 
90
                                } else {
 
91
                                        $mail_to = get_option('admin_email');
 
92
                                }
 
93
                                $mail_subject = sprintf(__('%s Database Backup File For %s', 'wp-dbmanager'), wp_specialchars_decode(get_option('blogname')), $file_date);
 
94
                                $mail_header = 'From: '.wp_specialchars_decode(get_option('blogname')).' Administrator <'.get_option('admin_email').'>';
 
95
                                // MIME Boundary
 
96
                                $random_time = md5(time());
 
97
                                $mime_boundary = "==WP-DBManager- $random_time";
 
98
                                // Create Mail Header And Mail Message
 
99
                                $mail_header .= "\nMIME-Version: 1.0\n" .
 
100
                                                                                "Content-Type: multipart/mixed;\n" .
 
101
                                                                                " boundary=\"{$mime_boundary}\"";
 
102
                                $mail_message = __('Website Name:', 'wp-dbmanager').' '.wp_specialchars_decode(get_option('blogname'))."\n".
 
103
                                                                                __('Website URL:', 'wp-dbmanager').' '.get_bloginfo('siteurl')."\n".
 
104
                                                                                __('Backup File Name:', 'wp-dbmanager').' '.$database_file."\n".
 
105
                                                                                __('Backup File Date:', 'wp-dbmanager').' '.$file_date."\n".
 
106
                                                                                __('Backup File Size:', 'wp-dbmanager').' '.$file_size."\n\n".
 
107
                                                                                __('With Regards,', 'wp-dbmanager')."\n".
 
108
                                                                                wp_specialchars_decode(get_option('blogname')).' '. __('Administrator', 'wp-dbmanager')."\n".
 
109
                                                                                get_bloginfo('siteurl');
 
110
                                $mail_message = "This is a multi-part message in MIME format.\n\n" .
 
111
                                                                                "--{$mime_boundary}\n" .
 
112
                                                                                "Content-Type: text/plain; charset=\"utf-8\"\n" .
 
113
                                                                                "Content-Transfer-Encoding: 7bit\n\n".$mail_message."\n\n";                             
 
114
                                $mail_message .= "--{$mime_boundary}\n" .
 
115
                                                                                "Content-Type: application/octet-stream;\n" .
 
116
                                                                                " name=\"$database_file\"\n" .
 
117
                                                                                "Content-Disposition: attachment;\n" .
 
118
                                                                                " filename=\"$database_file\"\n" .
 
119
                                                                                "Content-Transfer-Encoding: base64\n\n" .
 
120
                                                                                $file_data."\n\n--{$mime_boundary}--\n";
 
121
                                if(mail($mail_to, $mail_subject, $mail_message, $mail_header)) {
 
122
                                        $text .= '<font color="green">'.sprintf(__('Database Backup File For \'%s\' Successfully E-Mailed To \'%s\'', 'wp-dbmanager'), $file_date, $mail_to).'</font><br />';
 
123
                                } else {
 
124
                                        $text = '<font color="red">'.sprintf(__('Unable To E-Mail Database Backup File For \'%s\' To \'%s\'', 'wp-dbmanager'), $file_date, $mail_to).'</font>';
 
125
                                }
 
126
                        } else {
 
127
                                $text = '<font color="red">'.__('No Backup Database File Selected', 'wp-dbmanager').'</font>';
 
128
                        }
 
129
                        break;
 
130
                case __('Download', 'wp-dbmanager'):
 
131
                        if(empty($database_file)) {
 
132
                                $text = '<font color="red">'.__('No Backup Database File Selected', 'wp-dbmanager').'</font>';
 
133
                        }
 
134
                        break;
 
135
                case __('Delete', 'wp-dbmanager'):
 
136
                        if(!empty($database_file)) {
 
137
                                if(is_file($backup['path'].'/'.$database_file)) {
 
138
                                        if(!unlink($backup['path'].'/'.$database_file)) {
 
139
                                                $text .= '<font color="red">'.sprintf(__('Unable To Delete Database Backup File On \'%s\'', 'wp-dbmanager'), $nice_file_date).'</font><br />';
 
140
                                        } else {
 
141
                                                $text .= '<font color="green">'.sprintf(__('Database Backup File On \'%s\' Deleted Successfully', 'wp-dbmanager'), $nice_file_date).'</font><br />';
 
142
                                        }
 
143
                                } else {
 
144
                                        $text = '<font color="red">'.sprintf(__('Invalid Database Backup File On \'%s\'', 'wp-dbmanager'), $nice_file_date).'</font>';
 
145
                                }
 
146
                        } else {
 
147
                                $text = '<font color="red">'.__('No Backup Database File Selected', 'wp-dbmanager').'</font>';
 
148
                        }
 
149
                        break;
 
150
        }
 
151
}
 
152
?>
 
153
<?php if(!empty($text)) { echo '<!-- Last Action --><div id="message" class="updated fade"><p>'.$text.'</p></div>'; } ?>
 
154
<!-- Manage Backup Database -->
 
155
<form method="post" action="<?php echo admin_url('admin.php?page='.plugin_basename(__FILE__)); ?>">
 
156
        <?php wp_nonce_field('wp-dbmanager_manage'); ?>
 
157
        <div class="wrap">
 
158
                <div id="icon-wp-dbmanager" class="icon32"><br /></div>
 
159
                <h2><?php _e('Manage Backup Database', 'wp-dbmanager'); ?></h2>
 
160
                <p><?php _e('Choose A Backup Date To E-Mail, Restore, Download Or Delete', 'wp-dbmanager'); ?></p>      
 
161
                <table class="widefat">
 
162
                        <thead>
 
163
                                <tr>
 
164
                                        <th><?php _e('No.', 'wp-dbmanager'); ?></th>
 
165
                                        <th><?php _e('Database File', 'wp-dbmanager'); ?></th>
 
166
                                        <th><?php _e('Date/Time', 'wp-dbmanager'); ?></th>
 
167
                                        <th><?php _e('Size', 'wp-dbmanager'); ?></th>
 
168
                                        <th><?php _e('Select', 'wp-dbmanager'); ?></th>
 
169
                                </tr>
 
170
                        </thead>
 
171
                        <?php
 
172
                                if(!is_emtpy_folder($backup['path'])) {
 
173
                                        if ($handle = opendir($backup['path'])) {
 
174
                                                $database_files = array();
 
175
                                                while (false !== ($file = readdir($handle))) { 
 
176
                                                        if ($file != '.' && $file != '..' && $file != '.htaccess' && (file_ext($file) == 'sql' || file_ext($file) == 'gz')) {
 
177
                                                                $database_files[] = $file;
 
178
                                                        } 
 
179
                                                }
 
180
                                                closedir($handle);
 
181
                                                sort($database_files);
 
182
                                                for($i = (sizeof($database_files)-1); $i > -1; $i--) {
 
183
                                                        if($no%2 == 0) {
 
184
                                                                $style = '';                                                            
 
185
                                                        } else {
 
186
                                                                $style = ' class="alternate"';
 
187
                                                        }
 
188
                                                        $no++;
 
189
                                                        $database_text = substr($database_files[$i], 13);
 
190
                                                        $date_text = mysql2date(sprintf(__('%s @ %s', 'wp-dbmanager'), get_option('date_format'), get_option('time_format')), gmdate('Y-m-d H:i:s', substr($database_files[$i], 0, 10)));
 
191
                                                        $size_text = filesize($backup['path'].'/'.$database_files[$i]);
 
192
                                                        echo "<tr$style>\n";
 
193
                                                        echo '<td>'.number_format_i18n($no).'</td>';
 
194
                                                        echo "<td>$database_text</td>";
 
195
                                                        echo "<td>$date_text</td>";
 
196
                                                        echo '<td>'.format_size($size_text).'</td>';
 
197
                                                        echo "<td><input type=\"radio\" name=\"database_file\" value=\"$database_files[$i]\" /></td>\n</tr>\n";
 
198
                                                        $totalsize += $size_text;
 
199
                                                }
 
200
                                        } else {
 
201
                                                echo '<tr><td align="center" colspan="5">'.__('There Are No Database Backup Files Available.', 'wp-dbmanager').'</td></tr>';
 
202
                                        }
 
203
                                } else {
 
204
                                        echo '<tr><td align="center" colspan="5">'.__('There Are No Database Backup Files Available.', 'wp-dbmanager').'</td></tr>';
 
205
                                }
 
206
                        ?>
 
207
                        <tr class="thead">
 
208
                                <th colspan="3"><?php printf(_n('%s Backup File', '%s Backup Files', $no, 'wp-dbmanager'), number_format_i18n($no)); ?></th>
 
209
                                <th><?php echo format_size($totalsize); ?></th>
 
210
                                <th>&nbsp;</th>
 
211
                        </tr>
 
212
                </table>
 
213
                <table class="form-table">
 
214
                        <tr>
 
215
                                <td colspan="5" align="center"><label for="email_to"><?php _e('E-mail database backup file to:', 'wp-dbmanager'); ?></label> <input type="text" id="email_to" name="email_to" size="30" maxlength="50" value="<?php echo get_option('admin_email'); ?>" dir="ltr" />&nbsp;&nbsp;<input type="submit" name="do" value="<?php _e('E-Mail', 'wp-dbmanager'); ?>" class="button" /></td>
 
216
                        </tr>
 
217
                        <tr>
 
218
                                <td colspan="5" align="center">
 
219
                                        <input type="submit" name="do" value="<?php _e('Download', 'wp-dbmanager'); ?>" class="button" />&nbsp;&nbsp;
 
220
                                        <input type="submit" name="do" value="<?php _e('Restore', 'wp-dbmanager'); ?>" onclick="return confirm('<?php _e('You Are About To Restore A Database.\nThis Action Is Not Reversible.\nAny Data Inserted After The Backup Date Will Be Gone.\n\n Choose [Cancel] to stop, [Ok] to restore.', 'wp-dbmanager'); ?>')" class="button" />&nbsp;&nbsp;
 
221
                                        <input type="submit" class="button" name="do" value="<?php _e('Delete', 'wp-dbmanager'); ?>" onclick="return confirm('<?php _e('You Are About To Delete The Selected Database Backup Files.\nThis Action Is Not Reversible.\n\n Choose [Cancel] to stop, [Ok] to delete.', 'wp-dbmanager'); ?>')" />&nbsp;&nbsp;
 
222
                                        <input type="button" name="cancel" value="<?php _e('Cancel', 'wp-dbmanager'); ?>" class="button" onclick="javascript:history.go(-1)" /></td>
 
223
                        </tr>                                   
 
224
                </table>
 
225
        </div>
 
226
</form>
 
 
b'\\ No newline at end of file'