OSDN Git Service

Small update to iSQL-Dumper.
[idb/iDB.git.git] / inc / admin / sqldumper / mysql.php
1 <?php
2 /*
3     This program is free software; you can redistribute it and/or modify
4     it under the terms of the Revised BSD License.
5
6     This program is distributed in the hope that it will be useful,
7     but WITHOUT ANY WARRANTY; without even the implied warranty of
8     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9     Revised BSD License for more details.
10
11     Copyright 2004-2010 iDB Support - http://idb.berlios.de/
12     Copyright 2004-2010 Game Maker 2k - http://gamemaker2k.org/
13
14     $FileInfo: mysql.php - Last Update: 05/28/2010 SVN 504 - Author: cooldude2k $
15 */
16 $File3Name = basename($_SERVER['SCRIPT_NAME']);
17 if ($File3Name=="mysql.php"||$File3Name=="/mysql.php") {
18         require('index.php');
19         exit(); }
20
21 if($_SESSION['UserGroup']==$Settings['GuestGroup']||$GroupInfo['HasAdminCP']=="no") {
22 redirect("location",$basedir.url_maker($exfile['index'],$Settings['file_ext'],"act=view",$Settings['qstr'],$Settings['qsep'],$prexqstr['index'],$exqstr['index'],false));
23 ob_clean(); header("Content-Type: text/plain; charset=".$Settings['charset']);
24 gzip_page($Settings['use_gzip'],$GZipEncode['Type']); session_write_close(); die(); }
25 if($Settings['sqltype']!="mysql"&&$Settings['sqltype']!="mysqli") {
26 redirect("location",$basedir.url_maker($exfile['index'],$Settings['file_ext'],"act=view",$Settings['qstr'],$Settings['qsep'],$prexqstr['index'],$exqstr['index'],false));
27 ob_clean(); header("Content-Type: text/plain; charset=".$Settings['charset']);
28 gzip_page($Settings['use_gzip'],$GZipEncode['Type']); session_write_close(); die(); }
29 if(!isset($_GET['outtype'])) { $_GET['outtype'] = "UTF-8"; }
30 header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
31 header("Cache-Control: private",false); 
32 header("Content-Description: File Transfer");
33 if(!isset($_GET['comlevel'])) {
34         $_GET['comlevel'] = -1; }
35 if(!is_numeric($_GET['comlevel'])) {
36         $_GET['comlevel'] = -1; }
37 if($_GET['comlevel']>9||$_GET['comlevel']<-1) {
38         $_GET['comlevel'] = -1; }
39 if(!isset($_GET['compress'])) {
40         $_GET['compress'] = "none"; }
41 if($_GET['compress']=="gzip") {
42         $_GET['compress'] = "gzencode"; }
43 if($_GET['compress']=="bzip"||
44         $_GET['compress']=="bzip2") {
45         $_GET['compress'] = "bzcompress"; }
46 if($_GET['compress']!="none"&&
47         $_GET['compress']!="gzencode"&&
48         $_GET['compress']!="gzcompress"&&
49         $_GET['compress']!="gzdeflate"&&
50         $_GET['compress']!="bzcompress") {
51         $_GET['compress'] = "none"; }
52 if(!extension_loaded("zlib")) {
53 if($_GET['compress']=="gzencode"&&
54         $_GET['compress']=="gzcompress"&&
55         $_GET['compress']=="gzdeflate") {
56         $_GET['compress'] = "none"; } }
57 if(!extension_loaded("bz2")) {
58 if($_GET['compress']=="bzcompress") {
59         $_GET['compress'] = "none"; } }
60 if($_GET['compress']=="bzcompress") {
61 if($_GET['comlevel']>9||$_GET['comlevel']<0) {
62         $_GET['comlevel'] = 4; } }
63 $fname = null;
64 if(isset($Settings['sqldb'])&&$Settings['sqldb']!="") {
65 $fname = str_replace("_","", $Settings['sqldb'])."_"; }
66 if($_GET['compress']=="none") {
67 $fname .= str_replace("_","", $Settings['sqltable']).".sql"; }
68 if($_GET['compress']=="gzencode") {
69 $fname .= str_replace("_","", $Settings['sqltable']).".sql.gz"; }
70 if($_GET['compress']=="gzcompress") {
71 $fname .= str_replace("_","", $Settings['sqltable']).".sql.gz"; }
72 if($_GET['compress']=="gzdeflate") {
73 $fname .= str_replace("_","", $Settings['sqltable']).".sql.gz"; }
74 if($_GET['compress']=="bzcompress") {
75 $fname .= str_replace("_","", $Settings['sqltable']).".sql.bz2"; }
76 header("Content-Disposition: attachment; filename=".$fname);
77 header("Content-Type: application/octet-stream");
78 header("Content-Transfer-Encoding: binary");
79 $SQLDumper = "SQL Dumper";
80 function GetAllRows($table) { $rene_j = 0; $trowout = null;
81 global $SQLStat;
82 $tresult = sql_query("SELECT * FROM \"".$table."\"",$SQLStat);
83 while ($trow = sql_fetch_assoc($tresult)) {
84 $trowout[$rene_j] = $trow;
85 ++$rene_j; }
86 sql_free_result($tresult);
87 return $trowout; }
88 $TablePreFix = $Settings['sqltable'];
89 function add_prefix($tarray) {
90 global $TablePreFix;
91 return $TablePreFix.$tarray; }
92 $TableChCk = array("categories", "catpermissions", "events", "forums", "groups", "members", "messenger", "permissions", "posts", "restrictedwords", "sessions", "smileys", "themes", "topics", "wordfilter");
93 $TableChCk = array_map("add_prefix",$TableChCk);
94 if(!isset($_GET['outtype'])||$_GET['outtype']=="UTF-8") {
95 header("Content-Type: text/plain; charset=UTF-8"); }
96 if($_GET['outtype']=="latin1") {
97 header("Content-Type: text/plain; charset=ISO-8859-1"); }
98 if($_GET['outtype']=="latin15") {
99 header("Content-Type: text/plain; charset=ISO-8859-15"); }
100 $sql = "SHOW TABLES LIKE '".$Settings['sqltable']."%'";
101 $result = sql_query($sql,$SQLStat);
102 if (!$result) {
103 echo "DB Error, could not list tables\n";
104 echo 'MySQL Error: ' . sql_error($SQLStat);
105 exit; }
106 $DropTable = null; $CreateTable = null; $TableNames = null; $l = 0;
107 while ($row = sql_fetch_row($result)) { 
108 if(in_array($row[0],$TableChCk)) {
109 $TableNames[$l] = $row[0];
110 $DropTable[$l] = "DROP TABLE IF EXISTS \"".$row[0]."\";\n";
111 $CreateTable[$l] = "CREATE TABLE IF NOT EXISTS \"".$row[0]."\" (\n";
112 $CreateTable[$l] = null;
113 $result2 = sql_query("SHOW COLUMNS FROM ".$row[0],$SQLStat);
114 $tabsta = sql_query("SHOW TABLE STATUS LIKE '".$row[0]."'",$SQLStat);
115 $tabstats = sql_fetch_array($tabsta); $AutoIncrement = " ";
116 $tabstaz = sql_query("SHOW CREATE TABLE \"".$row[0]."\"",$SQLStat);
117 $tabstatz = sql_fetch_array($tabstaz);
118 $FullTable[$l] = $DropTable[$l].$tabstatz[1].";\n";
119 $tabstats = sql_fetch_array($tabsta); $AutoIncrement = " ";
120 /*
121 if($tabstats["Auto_increment"]!="") {
122 $AutoIncrement = " AUTO_INCREMENT=".$tabstats["Auto_increment"]." "; }
123         $TableInfo[$l] = null; $TableStats = null; $i = 0;
124         while ($row2 = sql_fetch_assoc($result2)) {
125                 $row2["Default"] = "'".$row2["Default"]."'"; 
126                 if($i==0) { $row2["Default"] = null; } $DefaVaule = null;
127                 if($row2["Default"]!=null) { $DefaVaule = " default ".$row2["Default"]; }
128                 if($row2["Extra"]!="") { $row2["Extra"] = " ".$row2["Extra"]; }
129         if($row2["Type"]=="text") { $DefaVaule = null; }
130         if(isset($PrimaryKey[$l])) { 
131         if($row2["Key"]=="PRI"||$row2["Key"]=="UNI") {
132         $PrimaryKey[$l] .= ",\n"; } }
133         if(!isset($PrimaryKey[$l])) { $PrimaryKey[$l] = null; }
134         $TableInfo[$l] .= "  \"".$row2["Field"]."\" ".$row2["Type"]." NOT NULL".$DefaVaule.$row2["Extra"].",\n";
135                 if($row2["Key"]=="PRI") { $PrimaryKey[$l] .= "  PRIMARY KEY (\"".$row2["Field"]."\")"; }
136                 if($row2["Key"]=="UNI") { $PrimaryKey[$l] .= "  UNIQUE KEY \"".$row2["Field"]."\" (\"".$row2["Field"]."\")"; }
137         ++$i; } */
138         /*
139         $TableStats[$l] = ") ENGINE=".$tabstats["Engine"]." DEFAULT CHARSET=".mysql_client_encoding()." COLLATE=".$tabstats["Collation"].$AutoIncrement.";\n";
140         $TableInfo[$l] .= $PrimaryKey[$l]."\n".$TableStats[$l];
141         $FullTable[$l] = $DropTable[$l].$CreateTable[$l].$TableInfo[$l];
142          }
143         $TableStats[$l] = ") ENGINE=".$tabstats["Engine"]." DEFAULT CHARSET=".mysql_client_encoding()." COLLATE=".$tabstats["Collation"].$AutoIncrement.";\n";
144         $TableInfo[$l] .= $PrimaryKey[$l]."\n".$TableStats[$l];
145         $FullTable[$l] = $DropTable[$l].$CreateTable[$l].$TableInfo[$l]; */ }
146 if (!$result2) {
147     echo 'Could not run query: ' . sql_error($SQLStat);
148     exit; }
149 sql_free_result($result2);
150 sql_free_result($tabsta);
151 ++$l; } $tableout = null;
152 $num = count($TableNames); $renee_s = 0;
153 $sqldump = "-- ".$OrgName." ".$SQLDumper."\n";
154 $sqldump .= "-- version ".$VerInfo['iDB_Ver_SVN']."\n";
155 $sqldump .= "-- ".$iDBHome."support/\n";
156 $sqldump .= "--\n";
157 $sqldump .= "-- Host: ".$Settings['sqlhost']."\n";
158 $sqldump .= "-- Generation Time: ".GMTimeGet('F d, Y \a\t h:i A',$_SESSION['UserTimeZone'],0,$_SESSION['UserDST'])."\n";
159 $sqldump .= "-- Server version: ".sql_server_info($SQLStat)."\n";
160 $sqldump .= "-- PHP Version: ".phpversion()."\n\n";
161 $sqldump .= "SET SQL_MODE=\"NO_AUTO_VALUE_ON_ZERO\";\n";
162 $sqldump .= "SET SESSION SQL_MODE='ANSI_QUOTES';\n\n";
163 $sqldump .= "--\n";
164 $sqldump .= "-- Database: \"".$Settings['sqldb']."\"\n";
165 $sqldump .= "--\n\n";
166 $sqldump .= "-- --------------------------------------------------------\n\n";
167 while ($renee_s < $num) { $tnum = $num - 1;
168 $trow = GetAllRows($TableNames[$renee_s]);
169 $numz = count($trow); $kazuki_p = 0;
170 $sqldump .= "--\n";
171 $sqldump .= "-- Table structure for table \"".$TableNames[$renee_s]."\"\n";
172 $sqldump .= "--\n\n";
173 $sqldump .= $FullTable[$renee_s]."\n";
174 while ($kazuki_p < $numz) { $tnumz = $numz - 1;
175 $srow = null; $srowvalue = null;
176 $trownew = $trow[$kazuki_p];
177 $trowname = array_keys($trownew);
178 $nums = count($trownew); $il = 0;
179 while ($il < $nums) { $tnums = $nums - 1;
180 $trowrname = sql_escape_string($trowname[$il],$SQLStat);
181 $trowrvalue = sql_escape_string($trownew[$trowrname],$SQLStat);
182 if($_GET['outtype']=="UTF-8"&&$Settings['charset']!="UTF-8") {
183 $trowrvalue = utf8_encode($trowrvalue); }
184 $trowrvalue = str_replace( array("\n", "\r"), array('\n', '\r'), $trowrvalue);
185 /*if($kazuki_p===0) {*/
186 if($il===0) { $srow = "INSERT INTO \"".$TableNames[$renee_s]."\" ("; }
187 if($il<$tnums&&$il!=$tnums) { $srow .= "\"".$trowrname."\", "; }
188 if($il==$tnums) { $srow .= "\"".$trowrname."\") VALUES"; } /*}*/
189 if($il===0) { $srowvalue = "("; }
190 if(!is_numeric($trowrvalue)) { $trowrvalue = "'".$trowrvalue."'"; }
191 if($il<$tnums) { $srowvalue .= $trowrvalue.", "; }
192 if($il==$tnums) { $srowvalue .= $trowrvalue;
193 /*if($kazuki_p<$tnumz) { $srowvalue .= "),"; }*/
194 /*if($kazuki_p==$tnumz) {*/ $srowvalue .= ");"; /*}*/ }
195 ++$il; }
196 if($kazuki_p===0) {
197 $sqldump .= "--\n";
198 $sqldump .= "-- Dumping data for table \"".$TableNames[$renee_s]."\"\n";
199 $sqldump .= "--\n\n"; }
200 $sqldump .= $srow."\n"; /*}*/
201 $sqldump .= $srowvalue."\n";
202 if($kazuki_p==$tnumz&&$renee_s<$tnum) {
203 $sqldump .= "\n-- --------------------------------------------------------\n"; }
204 ++$kazuki_p; }
205 if($numz===0) {
206 $sqldump .= "--\n";
207 $sqldump .= "-- Dumping data for table \"".$TableNames[$renee_s]."\"\n";
208 $sqldump .= "--\n\n";
209 $sqldump .= "\n-- --------------------------------------------------------\n"; }
210 $sqldump .= "\n";
211 ++$renee_s; }
212 if($_GET['compress']=="none") { echo $sqldump; }
213 if($_GET['compress']=="gzencode") { echo gzencode($sqldump,$_GET['comlevel']); }
214 if($_GET['compress']=="gzcompress") { echo gzcompress($sqldump,$_GET['comlevel']); }
215 if($_GET['compress']=="gzdeflate") { echo gzdeflate($sqldump,$_GET['comlevel']); }
216 if($_GET['compress']=="bzcompress") { echo bzcompress($sqldump,$_GET['comlevel']); }
217 fix_amp($Settings['use_gzip'],$GZipEncode['Type']);
218 ?>