public function restore($file = '')
 {
     global $wpdb;
     $wpdb->show_errors();
     $error = false;
     $return = array();
     if (empty($file) || !strpos(wp_get_referer(), OPT_NAME)) {
         $error = true;
         $return['info'] = false;
         $return['warning'] = empty($file) ? __("The file you have insert doesn't valid.", 'alfath') : __("For security reason restore can only be done from Theme Options Page.", 'alfath');
         return $return;
     }
     $filename = basename($file);
     switch (substr($filename, -3)) {
         case 'xml':
             $error = true;
             $return['warning'] = sprintf(__('The file you have insert is a WordPress eXtended RSS (WXR) file. You need to use this into the admin page to import this file. Here only <b>.gz</b> file are allowed. <a href="%s" title="Tools -> Import">Tools -> Import</a>', 'alfath'), admin_url('import.php', false));
             break;
         case 'zip':
         case 'rar':
             $error = true;
             $return['warning'] = __('The file you have insert is a ZIP/RAR file. Here only <b>.gz</b> file are allowed', 'alfath');
             break;
     }
     // End Switch
     if ($error) {
         $return['info'] = false;
         return $return;
     }
     if (substr($filename, -2) != 'gz') {
         $error = true;
         $return['warning'] = __('The file you have insert is not a valid file. Here only <b>.gz</b> file are allowed', 'alfath');
         return $return;
     }
     // get db encoded
     $content_file = file_get_contents($file);
     $db = unserialize(base64_decode(gzuncompress($content_file)));
     array_walk_recursive($db, 'AlfathBackup::change_url', 'restore');
     if (!is_array($db)) {
         wp_die(__('An error encoured during during import. Please try again.', 'alfath'));
     }
     set_time_limit(0);
     // tables
     foreach (self::$wptables as $table) {
         // delete all row of each table
         $wpdb->query("TRUNCATE TABLE {$wpdb->{$table}}");
         // insert new data
         $insert = array();
         foreach ($db[$table] as $id => $data) {
             $insert[] = AlfathBackup::_insert_sql($data);
         }
         if (!empty($db[$table])) {
             $insert = implode(', ', $insert);
             $fields = implode('`, `', array_keys($db[$table][0]));
             $wpdb->query("INSERT INTO `{$wpdb->{$table}}` ( `{$fields}` ) VALUES " . $insert);
         }
     }
     // End foreach
     $tables = apply_filters('alfath_db_tables', array());
     $tables = apply_filters('alfath_db_tables_restore', $tables);
     if (!empty($tables)) {
         foreach ($tables as $table) {
             if (!isset($db[$table])) {
                 continue;
             }
             if ($wpdb->get_var("SHOW TABLES LIKE '{$wpdb->prefix}{$table}'") == $wpdb->prefix . $table) {
                 // delete all row of each table
                 $wpdb->query("TRUNCATE TABLE {$wpdb->prefix}{$table}");
                 // insert new data
                 $insert = array();
                 foreach ($db[$table] as $id => $data) {
                     $insert[] = AlfathBackup::_insert_sql($data);
                 }
                 if (!empty($db[$table])) {
                     $insert = implode(', ', $insert);
                     $fields = implode('`, `', array_keys($db[$table][0]));
                     $wpdb->query("INSERT INTO `{$wpdb->prefix}{$table}` ( `{$fields}` ) VALUES " . $insert);
                 }
             }
             // End if
         }
         // End foreach
     }
     // End if
     $options = apply_filters('alfath_db_options', self::$options);
     $options = apply_filters('alfath_db_options_restore', $options);
     $sql_options = array();
     foreach ($options as $option) {
         if (strpos($option, '%') !== FALSE) {
             $operator = 'LIKE';
         } else {
             $operator = '=';
         }
         $sql_options[] = "option_name {$operator} '{$option}'";
     }
     $sql_options = implode(' OR ', $sql_options);
     $sql = "DELETE FROM {$wpdb->options} WHERE {$sql_options};";
     $wpdb->query($sql);
     // update options
     $warning = array();
     $check = $wpdb->get_results("SELECT * FROM {$wpdb->options} WHERE option_id = 1", ARRAY_A);
     foreach ($db['options'] as $id => $option) {
         if (!isset($check['blog_id'])) {
             unset($option['blog_id']);
         }
         if ($wpdb->insert($wpdb->options, $option)) {
             $insert = true;
         } else {
             $insert = false;
         }
         // save the ID that has error, to show.
         if (!$insert) {
             // $wpdb->print_error();
             $warning[$option] = $wpdb->last_error;
         }
     }
     if (!empty($insert_warn)) {
         $return['warning'] = $warning;
     }
     $return['info'] = true;
     return $return;
 }
Example #2
0
 /**
  * alfath_download_backup
  *
  * @return string
  */
 function alfath_download_backup()
 {
     if (!isset($_GET['secret']) || $_GET['secret'] != md5(AUTH_KEY . SECURE_AUTH_KEY)) {
         wp_die('Invalid Secret for options use');
         exit;
     }
     if (!isset($_GET['feed'])) {
         wp_die('No Feed Defined');
         exit;
     }
     require_once ADMIN_DIR . '/class-alfath-backup.php';
     $backup = AlfathBackup::backup();
     if (isset($_GET['action']) && $_GET['action'] == 'alfath_download_options') {
         header("Content-type: application/gzip-compressed");
         header("Content-Disposition: attachment; filename={$backup['filename']}");
         header("Content-Length: " . strlen($backup['content']));
         header("Content-Transfer-Encoding: binary");
         header('Accept-Ranges: bytes');
         header("Pragma: no-cache");
         header("Expires: 0");
         echo $backup['content'];
         exit;
     }
 }