sql_backup.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. //disabled
  22. echo "access denied";
  23. exit;
  24. //includes
  25. include "root.php";
  26. require_once "resources/require.php";
  27. require_once "resources/check_auth.php";
  28. //check permisions
  29. if (permission_exists('exec_sql_backup')) {
  30. //access granted
  31. }
  32. else {
  33. echo "access denied";
  34. exit;
  35. }
  36. //add multi-lingual support
  37. $language = new text;
  38. $text = $language->get();
  39. //pdo database connection
  40. if (strlen($_REQUEST['id']) > 0) {
  41. require_once "sql_query_pdo.php";
  42. }
  43. //get the $apps array from the installed apps from the core and mod directories
  44. $config_list = glob($_SERVER["DOCUMENT_ROOT"] . PROJECT_PATH . "/*/*/app_config.php");
  45. $x = 0;
  46. foreach ($config_list as &$config_path) {
  47. include($config_path);
  48. $x++;
  49. }
  50. //define a function that checks if the field exists
  51. function field_exists($apps, $table_name, $field_name) {
  52. $result = false;
  53. foreach ($apps as &$row) {
  54. $tables = $row["db"];
  55. foreach ($tables as &$table) {
  56. if ($table['table'] == $table_name) {
  57. foreach ($table["fields"] as &$field) {
  58. if ($field['deprecated'] != "true") {
  59. if (is_array($field["name"])) {
  60. if ($field["name"]["text"] == $field_name) {
  61. $result = true;
  62. break;
  63. }
  64. }
  65. else {
  66. if ($field["name"] == $field_name) {
  67. $result = true;
  68. break;
  69. }
  70. }
  71. }
  72. }
  73. }
  74. }
  75. }
  76. return $result;
  77. }
  78. //set the headers
  79. header('Content-type: application/octet-binary');
  80. header('Content-Disposition: attachment; filename=database_backup.sql');
  81. //get the list of tables
  82. if ($db_type == "sqlite") {
  83. $sql = "select name from sqlite_master ";
  84. $sql .= "where type='table' ";
  85. $sql .= "order by name;";
  86. }
  87. if ($db_type == "pgsql") {
  88. $sql = "select table_name as name ";
  89. $sql .= "from information_schema.tables ";
  90. $sql .= "where table_schema='public' ";
  91. $sql .= "and table_type='BASE TABLE' ";
  92. $sql .= "order by table_name ";
  93. }
  94. if ($db_type == "mysql") {
  95. $sql = "show tables";
  96. }
  97. $database = new database;
  98. $result_1 = $database->select($sql, null, 'all');
  99. unset($sql);
  100. if (is_array($result_1) && @sizeof($result_1) != 0) {
  101. foreach ($result_1 as &$row_1) {
  102. $row_1 = array_values($row_1);
  103. $table_name = $row_1[0];
  104. //get the table data
  105. $sql = "select * from ".$table_name;
  106. $database = new database;
  107. $result_2 = $database->select($sql, null, 'all');
  108. unset($sql);
  109. foreach ($result_2[0] as $key => $value) {
  110. if ($row_1[$column] != "db") {
  111. if (field_exists($apps, $table_name, $key)) {
  112. $column_array[] = $key;
  113. }
  114. }
  115. }
  116. $column_array_count = count($column_array);
  117. foreach ($result_2 as &$row_2) {
  118. foreach ($column_array as $column) {
  119. $columns[] = $column;
  120. $values[] = $row_2[$column] != '' ? "'".check_str($row_2[$column])."'" : 'null';
  121. }
  122. $sql = "insert into ".$table_name." (";
  123. $sql .= implode(', ', $columns);
  124. $sql .= ") values ( ";
  125. $sql .= implode(', ', $values);
  126. $sql .= ");";
  127. echo $sql."\n";
  128. unset($columns, $values);
  129. }
  130. unset($result_2, $row_2);
  131. unset($column_array);
  132. }
  133. }
  134. unset($result_1, $row_1);
  135. ?>