sql_backup.php 4.0 KB

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