backup.php (1852B)
1 <?php 2 class ModelToolBackup extends Model { 3 public function getTables() { 4 $table_data = array(); 5 6 $query = $this->db->query("SHOW TABLES FROM `" . DB_DATABASE . "`"); 7 8 foreach ($query->rows as $result) { 9 if (utf8_substr($result['Tables_in_' . DB_DATABASE], 0, strlen(DB_PREFIX)) == DB_PREFIX) { 10 if (isset($result['Tables_in_' . DB_DATABASE])) { 11 $table_data[] = $result['Tables_in_' . DB_DATABASE]; 12 } 13 } 14 } 15 16 return $table_data; 17 } 18 19 public function backup($tables) { 20 $output = ''; 21 22 foreach ($tables as $table) { 23 if (DB_PREFIX) { 24 if (strpos($table, DB_PREFIX) === false) { 25 $status = false; 26 } else { 27 $status = true; 28 } 29 } else { 30 $status = true; 31 } 32 33 if ($status) { 34 $output .= 'TRUNCATE TABLE `' . $table . '`;' . "\n\n"; 35 36 $query = $this->db->query("SELECT * FROM `" . $table . "`"); 37 38 foreach ($query->rows as $result) { 39 $fields = ''; 40 41 foreach (array_keys($result) as $value) { 42 $fields .= '`' . $value . '`, '; 43 } 44 45 $values = ''; 46 47 foreach (array_values($result) as $value) { 48 $value = str_replace(array("\x00", "\x0a", "\x0d", "\x1a"), array('\0', '\n', '\r', '\Z'), $value); 49 $value = str_replace(array("\n", "\r", "\t"), array('\n', '\r', '\t'), $value); 50 $value = str_replace('\\', '\\\\', $value); 51 $value = str_replace('\'', '\\\'', $value); 52 $value = str_replace('\\\n', '\n', $value); 53 $value = str_replace('\\\r', '\r', $value); 54 $value = str_replace('\\\t', '\t', $value); 55 56 $values .= '\'' . $value . '\', '; 57 } 58 59 $output .= 'INSERT INTO `' . $table . '` (' . preg_replace('/, $/', '', $fields) . ') VALUES (' . preg_replace('/, $/', '', $values) . ');' . "\n"; 60 } 61 62 $output .= "\n\n"; 63 } 64 } 65 66 return $output; 67 } 68 }