sql_backup.php 4.0 KB

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