sql_backup.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. /*
  3. FusionPBX
  4. Version: MPL 1.1
  5. The contents of this file are subject to the Mozilla Public License Version
  6. 1.1 (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.mozilla.org/MPL/
  9. Software distributed under the License is distributed on an "AS IS" basis,
  10. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11. for the specific language governing rights and limitations under the
  12. License.
  13. The Original Code is FusionPBX
  14. The Initial Developer of the Original Code is
  15. Mark J Crane <[email protected]>
  16. Portions created by the Initial Developer are Copyright (C) 2008-2014
  17. the Initial Developer. All Rights Reserved.
  18. Contributor(s):
  19. Mark J Crane <[email protected]>
  20. */
  21. include "root.php";
  22. require_once "resources/require.php";
  23. require_once "resources/check_auth.php";
  24. if (permission_exists('exec_sql_backup')) {
  25. //access granted
  26. }
  27. else {
  28. echo "access denied";
  29. exit;
  30. }
  31. //add multi-lingual support
  32. $language = new text;
  33. $text = $language->get();
  34. //pdo database connection
  35. if (strlen($_REQUEST['id']) > 0) {
  36. require_once "sql_query_pdo.php";
  37. }
  38. //get the $apps array from the installed apps from the core and mod directories
  39. $config_list = glob($_SERVER["DOCUMENT_ROOT"] . PROJECT_PATH . "/*/*/app_config.php");
  40. $x = 0;
  41. foreach ($config_list as &$config_path) {
  42. include($config_path);
  43. $x++;
  44. }
  45. //define a function that checks if the field exists
  46. function field_exists($apps, $table_name, $field_name) {
  47. $result = false;
  48. foreach ($apps as &$row) {
  49. $tables = $row["db"];
  50. foreach ($tables as &$table) {
  51. if ($table['table'] == $table_name) {
  52. foreach ($table["fields"] as &$field) {
  53. if ($field['deprecated'] != "true") {
  54. if (is_array($field["name"])) {
  55. if ($field["name"]["text"] == $field_name) {
  56. $result = true;
  57. break;
  58. }
  59. }
  60. else {
  61. if ($field["name"] == $field_name) {
  62. $result = true;
  63. break;
  64. }
  65. }
  66. }
  67. }
  68. }
  69. }
  70. }
  71. return $result;
  72. }
  73. //set the headers
  74. header('Content-type: application/octet-binary');
  75. header('Content-Disposition: attachment; filename=database_backup.sql');
  76. //get the list of tables
  77. if ($db_type == "sqlite") {
  78. $sql = "SELECT name FROM sqlite_master ";
  79. $sql .= "WHERE type='table' ";
  80. $sql .= "order by name;";
  81. }
  82. if ($db_type == "pgsql") {
  83. $sql = "select table_name as name ";
  84. $sql .= "from information_schema.tables ";
  85. $sql .= "where table_schema='public' ";
  86. $sql .= "and table_type='BASE TABLE' ";
  87. $sql .= "order by table_name ";
  88. }
  89. if ($db_type == "mysql") {
  90. $sql = "show tables";
  91. }
  92. $prep_statement = $db->prepare(check_sql($sql));
  93. $prep_statement->execute();
  94. $result = $prep_statement->fetchAll(PDO::FETCH_NAMED);
  95. foreach ($result as &$row) {
  96. $row = array_values($row);
  97. $table_name = $row[0];
  98. //get the table data
  99. $sql = "select * from $table_name";
  100. if (strlen($sql) > 0) {
  101. $prep_statement_2 = $db->prepare(check_sql($sql));
  102. if ($prep_statement_2) {
  103. $prep_statement_2->execute();
  104. $result2 = $prep_statement_2->fetchAll(PDO::FETCH_ASSOC);
  105. }
  106. else {
  107. echo "<b>".$text['label-error'].":</b>\n";
  108. echo "<pre>\n";
  109. print_r($db->errorInfo());
  110. echo "</pre>\n";
  111. }
  112. $x = 0;
  113. foreach ($result2[0] as $key => $value) {
  114. if ($row[$column] != "db") {
  115. if (field_exists($apps, $table_name, $key)) {
  116. $column_array[$x] = $key;
  117. }
  118. $x++;
  119. }
  120. }
  121. $column_array_count = count($column_array);
  122. foreach ($result2 as &$row) {
  123. $sql = "INSERT INTO $table_name (";
  124. $x = 1;
  125. foreach ($column_array as $column) {
  126. if ($x < $column_array_count) {
  127. if (strlen($row[$column]) > 0) {
  128. $sql .= ''.$column.',';
  129. }
  130. }
  131. else {
  132. if (strlen($row[$column]) > 0) {
  133. $sql .= ''.$column.'';
  134. }
  135. }
  136. $x++;
  137. }
  138. $sql .= ") ";
  139. $sql .= "VALUES( ";
  140. $x = 1;
  141. foreach ($column_array as $column) {
  142. if ($x < $column_array_count) {
  143. if (strlen($row[$column])> 0) {
  144. $sql .= "'".check_str($row[$column])."',";
  145. }
  146. }
  147. else {
  148. if (strlen($row[$column])> 0) {
  149. $sql .= "'".check_str($row[$column])."'";
  150. }
  151. }
  152. $x++;
  153. }
  154. $sql .= ");\n";
  155. echo str_replace(",)", ")", $sql);
  156. }
  157. }
  158. unset($column_array);
  159. }
  160. ?>