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