/
dumpdb.php
147 lines (132 loc) · 4.84 KB
/
dumpdb.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
<?php
/*
#############################################################
# >>> PHPSurveyor #
#############################################################
# > Author: Jason Cleeland #
# > E-mail: jason@cleeland.org #
# > Mail: Box 99, Trades Hall, 54 Victoria St, #
# > CARLTON SOUTH 3053, AUSTRALIA
# > Date: 20 February 2003 #
# #
# This set of scripts allows you to develop, publish and #
# perform data-entry on surveys. #
#############################################################
# #
# Copyright (C) 2003 Jason Cleeland #
# #
# This program is free software; you can redistribute #
# it and/or modify it under the terms of the GNU General #
# Public License as published by the Free Software #
# Foundation; either version 2 of the License, or (at your #
# option) any later version. #
# #
# This program is distributed in the hope that it will be #
# useful, but WITHOUT ANY WARRANTY; without even the #
# implied warranty of MERCHANTABILITY or FITNESS FOR A #
# PARTICULAR PURPOSE. See the GNU General Public License #
# for more details. #
# #
# You should have received a copy of the GNU General #
# Public License along with this program; if not, write to #
# the Free Software Foundation, Inc., 59 Temple Place - #
# Suite 330, Boston, MA 02111-1307, USA. #
#############################################################
*/
require_once(dirname(__FILE__).'/../config.php');
if($_SESSION['USER_RIGHT_CONFIGURATOR'] == 1)
{
if ($database_exists) $tables = $connect->MetaTables();
$export="";
$export .="#------------------------------------------"."\n";
$export .="# PHPSurveyor Database Dump of `$databasename`"."\n";
if ($allowexportalldb==0) {
$export .="# Only prefixed tables with: ". $dbprefix ."\n";
}
$export .="# Date of Dump: ". date("d-M-Y") ."\n";
$export .="#------------------------------------------"."\n\n\n";
foreach($tables as $table) {
if ($allowexportalldb==0) {
if ($dbprefix==substr($table, 0, strlen($dbprefix))) {
$export .= defdump($table);
$export .= datadump($table);
}
}
else {
$export .= defdump($table);
$export .= datadump($table);
}
}
$file_name = "PHPSurveyor_{$databasename}_dump_".date("Y-m-d").".sql";
Header("Content-type: application/octet-stream");
Header("Content-Disposition: attachment; filename=$file_name");
Header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
echo $export;
exit;
function defdump($tablename)
{
global $connect;
$def = "";
$def .="#------------------------------------------"."\n";
$def .="# Table definition for $tablename"."\n";
$def .="#------------------------------------------"."\n";
$def .= "DROP TABLE IF EXISTS $tablename;"."\n"."\n";
$def .= "CREATE TABLE $tablename ("."\n";
$result = db_execute_assoc("SHOW COLUMNS FROM $tablename") or die("Table $tablename not existing in database");
while($row = $result->FetchRow())
{
$def .= " $row[Field] $row[Type]";
if (!is_null($row["Default"])) $def .= " DEFAULT '$row[Default]'";
if ($row["Null"] != "YES") $def .= " NOT NULL";
if ($row["Extra"] != "") $def .= " $row[Extra]";
$def .= ",\n";
}
$def = ereg_replace(",\n$","", $def);
$result = db_execute_assoc("SHOW KEYS FROM $tablename");
while($row = $result->FetchRow())
{
$kname=$row["Key_name"];
if(($kname != "PRIMARY") && ($row["Non_unique"] == 0)) $kname="UNIQUE|$kname";
if(!isset($index[$kname])) $index[$kname] = array();
$index[$kname][] = $row["Column_name"];
}
while(list($x, $columns) = @each($index))
{
$def .= ",\n";
if($x == "PRIMARY") $def .= " PRIMARY KEY (" . implode($columns, ", ") . ")";
else if (substr($x,0,6) == "UNIQUE") $def .= " UNIQUE ".substr($x,7)." (" . implode($columns, ", ") . ")";
else $def .= " KEY $x (" . implode($columns, ", ") . ")";
}
$def .= "\n);\n\n\n";
//echo "<pre>$def</pre><br />";
return (stripslashes($def));
}
function datadump ($table) {
global $connect;
$result = "#------------------------------------------"."\n";
$result .="# Table data for $table"."\n";
$result .="#------------------------------------------"."\n";
$query = db_execute_num("select * from $table");
$num_fields = $query->FieldCount();
$numrow = $query->RecordCount();
while($row=$query->FetchRow()){
set_time_limit(5);
$result .= "INSERT INTO ".$table." VALUES(";
for($j=0; $j<$num_fields; $j++) {
$row[$j] = addslashes($row[$j]);
$row[$j] = ereg_replace("\n","\\n",$row[$j]);
if (isset($row[$j])) $result .= "\"$row[$j]\"" ; else $result .= "\"\"";
if ($j<($num_fields-1)) $result .= ",";
}
$result .= ");\n";
} // while
return $result . "\n\n\n";
}
}
else
{
$action = "dumpdb";
include("access_denied.php");
include("admin.php");
}
?>