Ejemplo n.º 1
0
 /**
  * Export form settings to sql file.
  *
  * @since 1.0
  * @access public
  * @param $table array
  * @param $zip boolean
  */
 public function exportFormSql($table = array(), $zip = false)
 {
     global $wpdb, $guiform;
     $forms = array_map('esc_html', $_POST['forms']);
     $form_column = $wpdb->get_col("SHOW COLUMNS FROM {$wpdb->guiform}");
     $form_field .= '`' . implode("`, `", $form_column) . '`';
     //cycle through data
     $return = "";
     $return .= "-- " . GuiForm_Plugin::PACKAGE . " SQL Dump \n";
     $return .= "-- Plugin Version " . GuiForm_Plugin::VERSION . " \n";
     $return .= "-- Site: https://www.guiform.com \n";
     $return .= "-- \n";
     $return .= "-- Host: " . get_site_url() . " \n";
     $return .= "-- Generation Time: " . date('M d, Y \\a\\t h:i a', strtotime("now" - 8)) . "\n";
     $return .= "-- MYSQL Version: " . $wpdb->db_version() . " \n";
     $return .= "-- PHP Version: " . phpversion() . " \n\n\n";
     foreach ($forms as $id) {
         $form_row = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->guiform} WHERE id = %d", $id), ARRAY_A);
         $param = $info = array();
         foreach ($wpdb->col_info as $col) {
             $info[$col->name] = $col;
         }
         $param['data'] = $form_row;
         $param['info'] = $info;
         $form_row = GuiForm_Module_Setup::isString($param);
         $return .= "-- ---------------------------------------------------------------- \n";
         $return .= "-- \n";
         $return .= "-- Dumping form data \n";
         $return .= "-- \n";
         $return .= "REPLACE INTO `{$wpdb->guiform}` (" . $form_field . ") VALUES\n";
         $return .= "(" . implode(", ", $form_row) . ");";
         $INSERT = $param = $info = array();
         $table = $wpdb->guiform_form . $id;
         if (isset($_POST['schema'])) {
             $return .= "\n\n--\n-- ";
             $return .= "Table structure for table `" . $table . "`\n--\n";
             $structure = $wpdb->get_row("SHOW CREATE TABLE {$table}", ARRAY_A);
             if (isset($_POST['drop_table'])) {
                 $return .= "DROP TABLE IF EXISTS `{$table}`;\n";
             }
             $return .= str_replace('CREATE TABLE', 'CREATE TABLE IF NOT EXISTS', $structure['Create Table']) . ";\n\n";
         }
         $column = $wpdb->get_col("SHOW COLUMNS FROM {$table}");
         $result = $wpdb->get_results("SELECT * FROM {$table}", ARRAY_A);
         $return .= "\n\n--\n-- Dumping data for table `" . $table . "`\n--\n";
         if (sizeof($result) > 0) {
             if (isset($_POST['entry'])) {
                 $entry_function = $_POST['entry_function'] == 'insert' ? 'INSERT' : 'REPLACE';
                 $return .= $entry_function . ' INTO `' . $table . "` (";
                 $return .= '`' . implode("`, `", $column) . '`';
                 $return .= ") VALUES \n";
                 foreach ($wpdb->col_info as $col) {
                     $info[$col->name] = $col;
                 }
                 foreach ($result as $value) {
                     $param['data'] = $value;
                     $param['info'] = $info;
                     $data = GuiForm_Module_Setup::isString($param);
                     $INSERT[] = '(' . implode(", ", $data) . ')';
                 }
                 $return .= implode(",\n", $INSERT) . ";";
             }
         } else {
             $return .= "\n\n-- \n";
             $return .= "-- " . __('No Record Found.', GuiForm_Plugin::NAME) . "\n";
             $return .= "--";
         }
         $return .= "\n\n\n";
     }
     date_default_timezone_set('GMT');
     $filename = date("Y-m-d-H-i", strtotime("now" - 8));
     $filename = strtolower(str_replace(" ", "-", $filename));
     //save file
     if ($_POST['zip']) {
         $zip = new ZipArchive();
         $zip_name = $filename . '.zip';
         $res = $zip->open($zip_name, ZipArchive::CREATE);
         if ($res === TRUE) {
             $zip->addFromString('guiform.sql', $return);
             $zip->close();
         }
         header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
         header('Content-Description: File Transfer');
         header('Content-Disposition: attachment; filename="' . $zip_name . '"');
         header('Content-type: application/zip');
         header('Content-Length: ' . strlen($return));
         header('Expires: 0');
         header('Pragma: public');
         readfile($zip_name);
         unlink($zip_name);
         //			$zp = gzopen(ABSPATH. '/' . 'db-backup-'.time().'-'.(md5(implode(',',$tables))).'.sql.gz', "w9");
         //			gzwrite($zp, $return);
         //			gzclose($zp);
     } else {
         $content_type = 'text/sql';
         header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
         header('Content-Description: File Transfer');
         header('Content-Disposition: attachment; filename=' . $filename . '.sql');
         header("Content-Type: {$content_type}; charset=" . get_option('blog_charset'), true);
         header('Expires: 0');
         header('Pragma: public');
         echo $return;
     }
     exit;
 }