Small update
[iDB.git] / inc / admin / sqldumper / pgsql.php
blobe414771eca3adc0ba39127ee49d41969702782d4
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.
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.
11 Copyright 2004-2019 iDB Support - https://idb.osdn.jp/support/category.php?act=view&id=1
12 Copyright 2004-2019 Game Maker 2k - https://idb.osdn.jp/support/category.php?act=view&id=2
14 $FileInfo: pgsql.php - Last Update: 08/02/2019 SVN 905 - Author: cooldude2k $
16 $File3Name = basename($_SERVER['SCRIPT_NAME']);
17 if ($File3Name=="pgsql.php"||$File3Name=="/pgsql.php") {
18 require('index.php');
19 exit(); }
21 if($_SESSION['UserGroup']==$Settings['GuestGroup']||$GroupInfo['HasAdminCP']=="no") {
22 redirect("location",$rbasedir.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']!="pgsql") {
26 redirect("location",$rbasedir.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 if(!isset($AltSQLDumper)||$AltSQLDumper===null) {
80 $SQLDumper = "SQL Dumper"; }
81 if(isset($AltSQLDumper)&&$AltSQLDumper!==null) {
82 $SQLDumper = $AltSQLDumper; }
83 function GetAllRows($table) { $rene_j = 0; $trowout = null;
84 global $SQLStat;
85 $tresult = sql_query("SELECT * FROM \"".$table."\"",$SQLStat);
86 while ($trow = sql_fetch_assoc($tresult)) {
87 $trowout[$rene_j] = $trow;
88 ++$rene_j; }
89 sql_free_result($tresult);
90 return $trowout; }
91 $TablePreFix = $Settings['sqltable'];
92 function add_prefix($tarray) {
93 global $TablePreFix;
94 return $TablePreFix.$tarray; }
95 $TableChCk = array("categories", "catpermissions", "events", "forums", "groups", "levels", "members", "mempermissions", "messenger", "permissions", "polls", "posts", "restrictedwords", "sessions", "smileys", "themes", "topics", "wordfilter");
96 $TableChCk = array_map("add_prefix",$TableChCk);
97 if(!isset($_GET['outtype'])||$_GET['outtype']=="UTF-8") {
98 header("Content-Type: text/plain; charset=UTF-8"); }
99 if($_GET['outtype']=="latin1") {
100 header("Content-Type: text/plain; charset=ISO-8859-15"); }
101 if($_GET['outtype']=="latin15") {
102 header("Content-Type: text/plain; charset=ISO-8859-15"); }
103 $sli = 0; $slnum = count($TableChCk);
104 while ($sli < $slnum) {
105 $FullTable[$sli] = "CREATE TABLE \"".$TableChCk[$sli]."\" (";
106 $tabsta = sql_query("select * from information_schema.columns where table_name='".$TableChCk[$sli]."';",$SQLStat);
107 $zli = 0; $zlnum = sql_num_rows($tabsta);
108 $UniKeyRow = null;
109 while ($zli < $zlnum) {
110 $SQL['column_name'] = sql_result($tabsta,$zli,"column_name");
111 $SQL['column_default'] = sql_result($tabsta,$zli,"column_default");
112 $PSQL = null;
113 $PSQL = explode("::", $SQL['column_default']);
114 if(count($PSQL)>1) { $SQL['column_default'] = $PSQL[0]; }
115 if (preg_match("/nextval(.*)\_seq/i", $SQL['column_default'])) {
116 $SQL['udt_name'] = "SERIAL"; $SQL['column_default'] = null;
117 } else { $SQL['column_default'] = " DEFAULT ".$SQL['column_default'];
118 $SQL['udt_name'] = sql_result($tabsta,$zli,"udt_name"); }
119 if($SQL['udt_name']=="text") { $SQL['column_default'] = null; }
120 $SQL['character_maximum_length'] = sql_result($tabsta,$zli,"character_maximum_length");
121 if($SQL['udt_name']=="varchar") {
122 $SQL['udt_name'] = $SQL['udt_name']."(".$SQL['character_maximum_length'].")"; }
123 $pristats = null; $PriKeyRow = " ";
124 $prista = sql_query("SELECT * FROM information_schema.key_column_usage WHERE table_name = '".$TableChCk[$sli]."' and column_name = '".$SQL['column_name']."';",$SQLStat);
125 $pristats = @pg_fetch_result($prista,0,"\"constraint_name\"");
126 if (preg_match("/(.*)\_pkey/i", $pristats)) {
127 $PriKeyRow = " PRIMARY KEY ";
128 } else {
129 if (preg_match("/(.*)\_key/i", $pristats)) {
130 $UniKeyRow = $UniKeyRow.",\n UNIQUE (\"".$SQL['column_name']."\")";
131 } else {
132 /*Nothing*/ } }
133 if($pristats==$SQL['column_name']) { $PriKeyRow = " PRIMARY KEY "; }
134 $FullTable[$sli] .= "\n \"".$SQL['column_name']."\" ".$SQL['udt_name'].$PriKeyRow."NOT NULL".$SQL['column_default'];
135 if($zli+1 < $zlnum) { $FullTable[$sli] .= ","; }
136 ++$zli; }
137 $FullTable[$sli] .= $UniKeyRow."\n);\n";
138 ++$sli; }
139 $TableNames = $TableChCk;
140 $num = count($TableNames); $melanie_p = 0;
141 $sqldump = "-- ".$OrgName." ".$SQLDumper."\n";
142 $sqldump .= "-- version ".$VerInfo['iDB_Ver_SVN']."\n";
143 $sqldump .= "-- ".$iDBHome."support/\n";
144 $sqldump .= "--\n";
145 $sqldump .= "-- Host: ".$Settings['sqlhost']."\n";
146 $sqldump .= "-- Generation Time: ".$usercurtime->format('F d, Y \a\t h:i A')."\n";
147 $sqldump .= "-- Server version: ".sql_server_info($SQLStat)."\n";
148 $sqldump .= "-- PHP Version: ".phpversion()."\n\n";
149 $sqldump .= "--\n";
150 $sqldump .= "-- Database: \"".$Settings['sqldb']."\"\n";
151 $sqldump .= "--\n\n";
152 $sqldump .= "-- --------------------------------------------------------\n\n";
153 while ($melanie_p < $num) { $tnum = $num - 1;
154 $trow = GetAllRows($TableNames[$melanie_p]);
155 $numz = count($trow); $kazuki_p = 0;
156 $sqldump .= "--\n";
157 $sqldump .= "-- Table structure for table \"".$TableNames[$melanie_p]."\"\n";
158 $sqldump .= "--\n\n";
159 $sqldump .= $FullTable[$melanie_p]."\n";
160 while ($kazuki_p < $numz) { $tnumz = $numz - 1;
161 $srow = null; $srowvalue = null;
162 $trownew = $trow[$kazuki_p];
163 $trowname = array_keys($trownew);
164 $nums = count($trownew); $il = 0;
165 while ($il < $nums) { $tnums = $nums - 1;
166 $trowrname = sql_escape_string($trowname[$il],$SQLStat);
167 $trowrvalue = sql_escape_string($trownew[$trowrname],$SQLStat);
168 if($_GET['outtype']=="UTF-8"&&$Settings['charset']!="UTF-8") {
169 $trowrvalue = utf8_encode($trowrvalue); }
170 $trowrvalue = str_replace( array("\n", "\r"), array('\n', '\r'), $trowrvalue);
171 if($kazuki_p===0) {
172 if($il===0) { $srow = "INSERT INTO \"".$TableNames[$melanie_p]."\" ("; }
173 if($il<$tnums&&$il!=$tnums) { $srow .= "\"".$trowrname."\", "; }
174 if($il==$tnums) { $srow .= "\"".$trowrname."\") VALUES"; } }
175 if($il===0) { $srowvalue = "("; }
176 if(!is_numeric($trowrvalue)) { $trowrvalue = "'".$trowrvalue."'"; }
177 if($il<$tnums) { $srowvalue .= $trowrvalue.", "; }
178 if($il==$tnums) { $srowvalue .= $trowrvalue;
179 if($kazuki_p<$tnumz) { $srowvalue .= "),"; }
180 if($kazuki_p==$tnumz) { $srowvalue .= ");"; } }
181 ++$il; }
182 if($kazuki_p===0) {
183 $sqldump .= "--\n";
184 $sqldump .= "-- Dumping data for table \"".$TableNames[$melanie_p]."\"\n";
185 $sqldump .= "--\n\n";
186 $sqldump .= $srow."\n"; }
187 $sqldump .= $srowvalue."\n";
188 if($kazuki_p==$tnumz&&$melanie_p<$tnum) {
189 $sqldump .= "\n-- --------------------------------------------------------\n"; }
190 ++$kazuki_p; }
191 if($numz===0) {
192 $sqldump .= "--\n";
193 $sqldump .= "-- Dumping data for table \"".$TableNames[$melanie_p]."\"\n";
194 $sqldump .= "--\n\n";
195 $sqldump .= "\n-- --------------------------------------------------------\n"; }
196 $sqldump .= "\n";
197 ++$melanie_p; }
198 if($_GET['compress']=="none") { echo $sqldump; }
199 if($_GET['compress']=="gzencode") { echo gzencode($sqldump,$_GET['comlevel']); }
200 if($_GET['compress']=="gzcompress") { echo gzcompress($sqldump,$_GET['comlevel']); }
201 if($_GET['compress']=="gzdeflate") { echo gzdeflate($sqldump,$_GET['comlevel']); }
202 if($_GET['compress']=="bzcompress") { echo bzcompress($sqldump,$_GET['comlevel']); }
203 fix_amp($Settings['use_gzip'],$GZipEncode['Type']);