home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Plugins / wp-dbmanager / dbmanager / database-manage.php < prev    next >
PHP Script  |  2008-02-19  |  11KB  |  205 lines

  1. <?php
  2. /*
  3. +----------------------------------------------------------------+
  4. |                                                                                            |
  5. |    WordPress 2.1 Plugin: WP-DBManager 2.20                                |
  6. |    Copyright (c) 2007 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/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('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.  
  36.  
  37. ### Form Processing 
  38. if($_POST['do']) {
  39.     // Lets Prepare The Variables
  40.     $database_file = trim($_POST['database_file']);
  41.     $nice_file_date = gmdate(sprintf(__('%s @ %s', 'wp-dbmanager'), get_option('date_format'), get_option('time_format')), substr($database_file, 0, 10));
  42.  
  43.     // Decide What To Do
  44.     switch($_POST['do']) {
  45.         case __('Restore', 'wp-dbmanager'):
  46.             if(!empty($database_file)) {
  47.                 if(stristr($database_file, '.gz')) {
  48.                     $backup['command'] = 'gunzip < '.$backup['path'].'/'.$database_file.' | '.$backup['mysqlpath'].' --host="'.DB_HOST.'" --user="'.DB_USER.'" --password="'.DB_PASSWORD.'" '.DB_NAME;
  49.                 } else {
  50.                     $backup['command'] = $backup['mysqlpath'].' --host="'.DB_HOST.'" --user="'.DB_USER.'" --password="'.DB_PASSWORD.'" '.DB_NAME.' < '.$backup['path'].'/'.$database_file;
  51.                 }
  52.                 passthru($backup['command'], $error);
  53.                 if($error) {
  54.                     $text = '<font color="red">'.sprintf(__('Database On \'%s\' Failed To Restore', 'wp-dbmanager'), $nice_file_date).'</font>';
  55.                 } else {
  56.                     $text = '<font color="green">'.sprintf(__('Database On \'%s\' Restored Successfully', 'wp-dbmanager'), $nice_file_date).'</font>';
  57.                 }
  58.             } else {
  59.                 $text = '<font color="red">'.__('No Backup Database File Selected', 'wp-dbmanager').'</font>';
  60.             }
  61.             break;
  62.         case __('E-Mail', 'wp-dbmanager'):
  63.             if(!empty($database_file)) {
  64.                 // Get And Read The Database Backup File
  65.                 $file_path = $backup['path'].'/'.$database_file;
  66.                 $file_size = format_size(filesize($file_path));
  67.                 $file_date = gmdate(sprintf(__('%s @ %s', 'wp-dbmanager'), get_option('date_format'), get_option('time_format')), substr($database_file, 0, 10));
  68.                 $file = fopen($file_path,'rb');
  69.                 $file_data = fread($file,filesize($file_path));
  70.                 fclose($file);
  71.                 $file_data = chunk_split(base64_encode($file_data));
  72.                 // Create Mail To, Mail Subject And Mail Header
  73.                 if(!empty($_POST['email_to'])) {
  74.                     $mail_to = trim($_POST['email_to']);
  75.                 } else {
  76.                     $mail_to = get_option('admin_email');
  77.                 }
  78.                 $mail_subject = sprintf(__('%s Database Backup File For %s', 'wp-dbmanager'), get_bloginfo('name'), $file_date);
  79.                 $mail_header = 'From: '.get_bloginfo('name').' Administrator <'.get_option('admin_email').'>';
  80.                 // MIME Boundary
  81.                 $random_time = md5(time());
  82.                 $mime_boundary = "==WP-DBManager- $random_time";
  83.                 // Create Mail Header And Mail Message
  84.                 $mail_header .= "\nMIME-Version: 1.0\n" .
  85.                                         "Content-Type: multipart/mixed;\n" .
  86.                                         " boundary=\"{$mime_boundary}\"";
  87.                 $mail_message = __('Website Name:', 'wp-dbmanager').' '.get_bloginfo('name')."\n".
  88.                                         __('Website URL:', 'wp-dbmanager').' '.get_bloginfo('siteurl')."\n".
  89.                                         __('Backup File Name:', 'wp-dbmanager').' '.$database_file."\n".
  90.                                         __('Backup File Date:', 'wp-dbmanager').' '.$file_date."\n".
  91.                                         __('Backup File Size:', 'wp-dbmanager').' '.$file_size."\n\n".
  92.                                         __('With Regards,', 'wp-dbmanager')."\n".
  93.                                         get_bloginfo('name').' '. __('Administrator', 'wp-dbmanager')."\n".
  94.                                         get_bloginfo('siteurl');
  95.                 $mail_message = "This is a multi-part message in MIME format.\n\n" .
  96.                                         "--{$mime_boundary}\n" .
  97.                                         "Content-Type: text/plain; charset=\"utf-8\"\n" .
  98.                                         "Content-Transfer-Encoding: 7bit\n\n".$mail_message."\n\n";                
  99.                 $mail_message .= "--{$mime_boundary}\n" .
  100.                                         "Content-Type: application/octet-stream;\n" .
  101.                                         " name=\"$database_file\"\n" .
  102.                                         "Content-Disposition: attachment;\n" .
  103.                                         " filename=\"$database_file\"\n" .
  104.                                         "Content-Transfer-Encoding: base64\n\n" .
  105.                                         $file_data."\n\n--{$mime_boundary}--\n";
  106.                 if(mail($mail_to, $mail_subject, $mail_message, $mail_header)) {
  107.                     $text .= '<font color="green">'.sprintf(__('Database Backup File For \'%s\' Successfully E-Mailed To \'%s\'', 'wp-dbmanager'), $file_date, $mail_to).'</font><br />';
  108.                 } else {
  109.                     $text = '<font color="red">'.sprintf(__('Unable To E-Mail Database Backup File For \'%s\' To \'%s\'', 'wp-dbmanager'), $file_date, $mail_to).'</font>';
  110.                 }
  111.             } else {
  112.                 $text = '<font color="red">'.__('No Backup Database File Selected', 'wp-dbmanager').'</font>';
  113.             }
  114.             break;
  115.         case __('Download', 'wp-dbmanager'):
  116.             if(empty($database_file)) {
  117.                 $text = '<font color="red">'.__('No Backup Database File Selected', 'wp-dbmanager').'</font>';
  118.             }
  119.             break;
  120.         case __('Delete', 'wp-dbmanager'):
  121.             if(!empty($database_file)) {
  122.                 $nice_file_date = gmdate(sprintf(__('%s @ %s', 'wp-dbmanager'), get_option('date_format'), get_option('time_format')), substr($database_file, 0, 10));
  123.                 if(is_file($backup['path'].'/'.$database_file)) {
  124.                     if(!unlink($backup['path'].'/'.$database_file)) {
  125.                         $text .= '<font color="red">'.sprintf(__('Unable To Delete Database Backup File On \'%s\'', 'wp-dbmanager'), $nice_file_date).'</font><br />';
  126.                     } else {
  127.                         $text .= '<font color="green">'.sprintf(__('Database Backup File On \'%s\' Deleted Successfully', 'wp-dbmanager'), $nice_file_date).'</font><br />';
  128.                     }
  129.                 } else {
  130.                     $text = '<font color="red">'.sprintf(__('Invalid Database Backup File On \'%s\'', 'wp-dbmanager'), $nice_file_date).'</font>';
  131.                 }
  132.             } else {
  133.                 $text = '<font color="red">'.__('No Backup Database File Selected', 'wp-dbmanager').'</font>';
  134.             }
  135.             break;
  136.     }
  137. }
  138. ?>
  139. <?php if(!empty($text)) { echo '<!-- Last Action --><div id="message" class="updated fade"><p>'.$text.'</p></div>'; } ?>
  140. <!-- Manage Backup Database -->
  141. <div class="wrap">
  142.     <h2><?php _e('Manage Backup Database', 'wp-dbmanager'); ?></h2>
  143.     <p><?php _e('Choose A Backup Date To E-Mail, Restore, Download Or Delete', 'wp-dbmanager'); ?></p>
  144.     <form action="<?php echo $_SERVER['REQUEST_URI']; ?>" method="post">
  145.         <table width="100%" cellspacing="3" cellpadding="3" border="0">
  146.             <tr class="thead">
  147.                 <th align="left"><?php _e('No.', 'wp-dbmanager'); ?></th>
  148.                 <th align="left"><?php _e('Database File', 'wp-dbmanager'); ?></th>
  149.                 <th align="left"><?php _e('Date/Time', 'wp-dbmanager'); ?></th>
  150.                 <th align="left"><?php _e('Size', 'wp-dbmanager'); ?></th>
  151.                 <th align="left"><?php _e('Select', 'wp-dbmanager'); ?></th>
  152.             </tr>
  153.             <?php
  154.                 if(!is_emtpy_folder($backup['path'])) {
  155.                     if ($handle = opendir($backup['path'])) {
  156.                         $database_files = array();
  157.                         while (false !== ($file = readdir($handle))) { 
  158.                             if ($file != '.' && $file != '..' && (file_ext($file) == 'sql' || file_ext($file) == 'gz')) {
  159.                                 $database_files[] = $file;
  160.                             } 
  161.                         }
  162.                         closedir($handle);
  163.                         sort($database_files);
  164.                         for($i = (sizeof($database_files)-1); $i > -1; $i--) {
  165.                             if($no%2 == 0) {
  166.                                 $style = 'style=\'background: none\'';                                
  167.                             } else {
  168.                                 $style = 'style=\'background-color: #eee\'';
  169.                             }
  170.                             $no++;
  171.                             $database_text = substr($database_files[$i], 13);
  172.                             $date_text = gmdate(sprintf(__('%s @ %s', 'wp-dbmanager'), get_option('date_format'), get_option('time_format')), substr($database_files[$i], 0, 10));
  173.                             $size_text = filesize($backup['path'].'/'.$database_files[$i]);
  174.                             echo "<tr $style>\n<td>$no</td>";
  175.                             echo "<td>$database_text</td>";
  176.                             echo "<td>$date_text</td>";
  177.                             echo '<td>'.format_size($size_text).'</td>';
  178.                             echo "<td><input type=\"radio\" name=\"database_file\" value=\"$database_files[$i]\" /></td>\n</tr>\n";
  179.                             $totalsize += $size_text;
  180.                         }
  181.                     } else {
  182.                         echo '<tr><td align="center" colspan="5">'.__('There Are No Database Backup Files Available.', 'wp-dbmanager').'</td></tr>';
  183.                     }
  184.                 } else {
  185.                     echo '<tr><td align="center" colspan="5">'.__('There Are No Database Backup Files Available.', 'wp-dbmanager').'</td></tr>';
  186.                 }
  187.             ?>
  188.             <tr class="thead">
  189.                 <th align="left" colspan="3"><?php echo $no; ?> <?php _e('Backup File(s)', 'wp-dbmanager'); ?></th>
  190.                 <th align="left"><?php echo format_size($totalsize); ?></th>
  191.                 <td> </td>
  192.             </tr>
  193.             <tr>
  194.                 <td colspan="5"><?php _e('E-mail database backup file to:', 'wp-dbmanager'); ?> <input type="text" name="email_to" size="30" maxlength="50" value="<?php echo get_option('admin_email'); ?>" />  <input type="submit" name="do" value="<?php _e('E-Mail', 'wp-dbmanager'); ?>" class="button" /></td>
  195.             </tr>
  196.             <tr>
  197.                 <td colspan="5" align="center">
  198.                     <input type="submit" name="do" value="<?php _e('Download', 'wp-dbmanager'); ?>" class="button" />  
  199.                     <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" />  
  200.                     <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'); ?>')" />  
  201.                     <input type="button" name="cancel" value="<?php _e('Cancel', 'wp-dbmanager'); ?>" class="button" onclick="javascript:history.go(-1)" /></td>
  202.             </tr>                    
  203.         </table>
  204.     </form>
  205. </div>