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