PageRenderTime 50ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/admin/includes/db_backup.php

https://bitbucket.org/mpercy/deeemm-cms
PHP | 236 lines | 171 code | 36 blank | 29 comment | 62 complexity | 27c1348668d46055430a49f0deac74b0 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-2-Clause
  1. <?php
  2. defined( '_INDM' ) or die( 'POSSIBLE HACK ATTEMPT!' );
  3. /*===========================================================================
  4. Check user priviledges
  5. ===========================================================================*/
  6. require VALIDATE;
  7. if ($user != 'ADMIN') header("Location: " . $default_url . "index.php");
  8. require $abs_path . $core_dir . 'db_access.php';
  9. /*===========================================================================
  10. Get table definition
  11. ===========================================================================*/
  12. function get_table_def($table, $crlf,$db_name) {
  13. $schema_create = "DROP TABLE IF EXISTS `$table`;$crlf";
  14. $db = $table;
  15. $schema_create .= "CREATE TABLE `$table` (";
  16. $result = mysql_query("SHOW FIELDS FROM `" .$db_name."`.`" . $table."`");
  17. while($row = mysql_fetch_array($result)) {
  18. echo ".";
  19. $schema_create .= " `$row[Field]` $row[Type]";
  20. if(isset($row["Default"]) && (!empty($row["Default"]) || $row["Default"] == "0")) $schema_create .= " DEFAULT '$row[Default]'";
  21. if($row["Null"] != "YES") $schema_create .= " NOT NULL";
  22. if($row["Extra"] != "") {
  23. $schema_create .= " $row[Extra]";
  24. }
  25. $schema_create .= ",";
  26. }
  27. $schema_create = ereg_replace(",".$crlf."$", "", $schema_create);
  28. $result = mysql_query("SHOW KEYS FROM `" .$db_name."`.`" . $table."`") or die(" error!");
  29. while($row = mysql_fetch_array($result)) {
  30. echo ">";
  31. $kname=$row['Key_name'];
  32. $comment=(isset($row['Comment'])) ? $row['Comment'] : '';
  33. $sub_part=(isset($row['Sub_part'])) ? $row['Sub_part'] : '';
  34. if(($kname != "PRIMARY") && ($row['Non_unique'] == 0)) $kname="UNIQUE|$kname";
  35. if($comment=="FULLTEXT")$kname="FULLTEXT|$kname";
  36. if(!isset($index[$kname])) $index[$kname] = array();
  37. if ($sub_part>1){
  38. $index[$kname][] = $row['Column_name'] . "(" . $sub_part . ")";
  39. } else {
  40. $index[$kname][] = $row['Column_name'];
  41. }
  42. }
  43. while(list($x, $columns) = @each($index)) {
  44. $schema_create .= ",";
  45. if($x == "PRIMARY")
  46. $schema_create .= " PRIMARY KEY (`";
  47. elseif (substr($x,0,6) == "UNIQUE")
  48. $schema_create .= " UNIQUE `" .substr($x,7)."` (`";
  49. elseif (substr($x,0,8) == "FULLTEXT")
  50. $schema_create .= " FULLTEXT `".substr($x,9)."` (`";
  51. else
  52. $schema_create .= " KEY $x (`";
  53. $schema_create .= implode($columns,", ") . "`)";
  54. }
  55. $schema_create .= ")";
  56. $schema_create = str_replace(',,', ',', $schema_create);//bad bugfix 050407
  57. $schema_create = str_replace(',)', ')', $schema_create);//bad bugfix 050407
  58. if(get_magic_quotes_gpc()) {
  59. return (stripslashes($schema_create));
  60. } else {
  61. return ($schema_create);
  62. }
  63. }
  64. /*===========================================================================
  65. Get table content
  66. ===========================================================================*/
  67. function get_table_content($db, $table, $limit_from = 0, $limit_to = 0,$handler) {
  68. // Defines the offsets to use
  69. if ($limit_from > 0) {
  70. $limit_from--;
  71. } else {
  72. $limit_from = 0;
  73. }
  74. if ($limit_to > 0 && $limit_from >= 0) {
  75. $add_query = " LIMIT $limit_from, $limit_to";
  76. } else {
  77. $add_query = '';
  78. }
  79. get_table_content_fast($db, $table, $add_query,$handler);
  80. }
  81. /*===========================================================================
  82. //get table
  83. ===========================================================================*/
  84. function get_table_content_fast($db, $table, $add_query = '',$handler) {
  85. $result = mysql_query('SELECT * FROM `' . $db . '`.`' . $table . "`" . $add_query) or die();
  86. if ($result != false) {
  87. @set_time_limit(1200); // 20 Minutes
  88. // Checks whether the field is an integer or not
  89. for ($j = 0; $j < mysql_num_fields($result); $j++) {
  90. $field_set[$j] = mysql_field_name($result, $j);
  91. $type = mysql_field_type($result, $j);
  92. if ($type == 'tinyint' || $type == 'smallint' || $type == 'mediumint' || $type == 'int' || $type == 'bigint' ||$type == 'timestamp') {
  93. $field_num[$j] = true;
  94. } else {
  95. $field_num[$j] = false;
  96. }
  97. } // end for
  98. // Get the scheme
  99. if (isset($GLOBALS['showcolumns'])) {
  100. $fields = implode(', ', $field_set);
  101. $schema_insert = "INSERT INTO `$table` ($fields) VALUES (";
  102. } else {
  103. $schema_insert = "INSERT INTO `$table` VALUES (";
  104. }
  105. $field_count = mysql_num_fields($result);
  106. $search = array("\x0a","\x0d","\x1a"); //\x08\\x09, not required
  107. $replace = array("\\n","\\r","\Z");
  108. while ($row = mysql_fetch_row($result)) {
  109. for ($j = 0; $j < $field_count; $j++) {
  110. if (!isset($row[$j])) {
  111. $values[] = 'NULL';
  112. } else if (!empty($row[$j])) {
  113. // a number
  114. if ($field_num[$j]) {
  115. $values[] = $row[$j];
  116. // a string
  117. } else {
  118. $values[] = "'" . str_replace($search, $replace, addslashes($row[$j])) . "'";
  119. }
  120. } else {
  121. $values[] = "''";
  122. } // end if
  123. } // end for
  124. $insert_line = $schema_insert . implode(',', $values) . ')';
  125. unset($values);
  126. // Call the handler
  127. $handler($insert_line);
  128. } // end while
  129. } // end if ($result != false)
  130. return true;
  131. }
  132. /*===========================================================================
  133. //insert crlf
  134. ===========================================================================*/
  135. function my_handler($sql_insert) {
  136. global $crlf, $asfile;
  137. global $tmp_buffer;
  138. if(empty($asfile))
  139. $tmp_buffer.= htmlspecialchars("$sql_insert;$crlf");
  140. else
  141. $tmp_buffer.= "$sql_insert;$crlf";
  142. }
  143. /*===========================================================================
  144. //perform the backup
  145. ===========================================================================*/
  146. $asfile="download";
  147. $crlf="\r\n";
  148. $dump_buffer="";
  149. if ($backup_dmcms_tables_only) {
  150. $tables = mysql_query("SHOW TABLES LIKE '" . $db_table_prefix . "%'");
  151. } else {
  152. $tables = mysql_query("SHOW TABLES");
  153. }
  154. $num_tables = mysql_num_rows($tables);
  155. if($num_tables == 0){
  156. echo "#No Tables Found on " . $db_name;
  157. exit;
  158. }
  159. $dump_buffer.= "#DMCMS Database Backup; $crlf";
  160. $dump_buffer.= "#Backup made: " . date("F j, Y, g:i a").";$crlf";
  161. $dump_buffer.= "#Database: $db_name;$crlf";
  162. echo "Creating Backup for Database: $db_name;$crlf<br>";
  163. $i = 0;
  164. while($i < $num_tables) {
  165. $table = mysql_tablename($tables, $i);
  166. echo "<br>Adding Table: " . $table . "<br>";
  167. $dump_buffer.= "$crlf";
  168. $dump_buffer.= "#Table structure for table '$table';$crlf";
  169. $dump_buffer.= "$crlf";
  170. $db = $table;
  171. $dump_buffer.= get_table_def($table, $crlf,$db_name).";$crlf";
  172. echo ">";
  173. $dump_buffer.= "$crlf";
  174. $dump_buffer.= "#Dumping data for table '$table';$crlf";
  175. $dump_buffer.= "$crlf";
  176. $tmp_buffer="";
  177. get_table_content($db_name, $table, 0, 0, 'my_handler', $db_name);
  178. $dump_buffer.=$tmp_buffer;
  179. $i++;
  180. $dump_buffer.= "$crlf";
  181. }
  182. /*===========================================================================
  183. //perform backup - save to file or to db_backups directory
  184. ===========================================================================*/
  185. $filename = $abs_path . $db_backup_dir . date ("jMyHis") . '.sql';
  186. if (!$db_save_to_file){
  187. //save database to db_backup directory
  188. global $absolute_path;
  189. global $db_backup_dir;
  190. write_file($filename, $dump_buffer);
  191. } else {
  192. //save database to file
  193. @ob_start();
  194. @ob_implicit_flush(0);
  195. header('Content-Type: application/octetstream');
  196. header('Content-Disposition: attachment; filename="' . $filename . '"');
  197. echo $dump_buffer;
  198. exit;
  199. }
  200. ?>