OSDN Git Service

Added PostgreSQL dumper.
[idb/iDB.git.git] / inc / admin / sqldumper / pgsql.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-2009 iDB Support - http://idb.berlios.de/
12     Copyright 2004-2009 Game Maker 2k - http://gamemaker2k.org/
13
14     $FileInfo: sqlite.php - Last Update: 12/14/2009 SVN 406 - Author: cooldude2k $
15 */
16 $File3Name = basename($_SERVER['SCRIPT_NAME']);
17 if ($File3Name=="sqlite.php"||$File3Name=="/sqlite.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']!="pgsql") {
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 $fname = str_replace("_","", $Settings['sqltable']);
34 header("Content-Disposition: attachment; filename=".$fname.".sql");
35 header("Content-Type: application/octet-stream");
36 header("Content-Transfer-Encoding: binary");
37 $SQLDumper = "SQL Dumper";
38 function GetAllRows($table) { $rene_j = 0; $trowout = null;
39 global $SQLStat;
40 $tresult = sql_query("SELECT * FROM \"".$table."\"",$SQLStat);
41 while ($trow = sql_fetch_assoc($tresult)) {
42 $trowout[$rene_j] = $trow;
43 ++$rene_j; }
44 sql_free_result($tresult);
45 return $trowout; }
46 $TablePreFix = $Settings['sqltable'];
47 function add_prefix($tarray) {
48 global $TablePreFix;
49 return $TablePreFix.$tarray; }
50 $TableChCk = array("categories", "catpermissions", "events", "forums", "groups", "members", "messenger", "permissions", "posts", "restrictedwords", "sessions", "smileys", "topics", "wordfilter");
51 $TableChCk = array_map("add_prefix",$TableChCk);
52 if(!isset($_GET['outtype'])||$_GET['outtype']=="UTF-8") {
53 header("Content-Type: text/plain; charset=UTF-8"); }
54 if($_GET['outtype']=="latin1") {
55 header("Content-Type: text/plain; charset=ISO-8859-15"); }
56 if($_GET['outtype']=="latin15") {
57 header("Content-Type: text/plain; charset=ISO-8859-15"); }
58 $sli = 0; $slnum = count($TableChCk);
59 while ($sli < $slnum) {
60 $FullTable[$sli] = "CREATE TABLE \"".$TableChCk[$sli]."\" (";
61 $tabsta = sql_query("select * from information_schema.columns where table_name='".$TableChCk[$sli]."';",$SQLStat);
62 $zli = 0; $zlnum = sql_num_rows($tabsta);
63 $UniKeyRow = null;
64 while ($zli < $zlnum) {
65 $SQL['column_name'] = sql_result($tabsta,$zli,"column_name");
66 $SQL['column_default'] = sql_result($tabsta,$zli,"column_default");
67 $PSQL = null;
68 $PSQL = explode("::", $SQL['column_default']);
69 if(count($PSQL)>1) { $SQL['column_default'] = $PSQL[0]; }
70 if (preg_match("/nextval(.*)\_seq/i", $SQL['column_default'])) {
71 $SQL['udt_name'] = "SERIAL"; $SQL['column_default'] = null;
72 } else { $SQL['column_default'] = " DEFAULT ".$SQL['column_default'];
73 $SQL['udt_name'] = sql_result($tabsta,$zli,"udt_name"); }
74 if($SQL['udt_name']=="text") { $SQL['column_default'] = null; }
75 $SQL['character_maximum_length'] = sql_result($tabsta,$zli,"character_maximum_length");
76 if($SQL['udt_name']=="varchar") {
77         $SQL['udt_name'] = $SQL['udt_name']."(".$SQL['character_maximum_length'].")"; }
78 $pristats = null; $PriKeyRow = " ";
79 $prista = sql_query("SELECT * FROM information_schema.key_column_usage WHERE table_name = '".$TableChCk[$sli]."' and column_name = '".$SQL['column_name']."';",$SQLStat);
80 $pristats = @pg_fetch_result($prista,0,"\"constraint_name\"");
81 if (preg_match("/(.*)\_pkey/i", $pristats)) {
82 $PriKeyRow = " PRIMARY KEY ";
83 } else {
84 if (preg_match("/(.*)\_key/i", $pristats)) {
85 $UniKeyRow = $UniKeyRow.",\n  UNIQUE (\"".$SQL['column_name']."\")";
86 } else {
87 /*Nothing*/ } }
88 if($pristats==$SQL['column_name']) { $PriKeyRow = " PRIMARY KEY "; }
89 $FullTable[$sli] .= "\n  \"".$SQL['column_name']."\" ".$SQL['udt_name'].$PriKeyRow."NOT NULL".$SQL['column_default'];
90 if($zli+1 < $zlnum) { $FullTable[$sli] .= ","; }
91 ++$zli; }
92 $FullTable[$sli] .= $UniKeyRow."\n);\n";
93 ++$sli; }
94 $TableNames = $TableChCk;
95 $num = count($TableNames); $renee_s = 0;
96 echo "-- ".$OrgName." ".$SQLDumper."\n";
97 echo "-- version ".$VerInfo['iDB_Ver_SVN']."\n";
98 echo "-- ".$iDBHome."support/\n";
99 echo "--\n";
100 echo "-- Host: ".$Settings['sqlhost']."\n";
101 echo "-- Generation Time: ".GMTimeGet('F d, Y \a\t h:i A',$_SESSION['UserTimeZone'],0,$_SESSION['UserDST'])."\n";
102 echo "-- Server version: ".sql_server_info($SQLStat)."\n";
103 echo "-- PHP Version: ".phpversion()."\n\n";
104 echo "--\n";
105 echo "-- Database: \"".$Settings['sqldb']."\"\n";
106 echo "--\n\n";
107 echo "-- --------------------------------------------------------\n\n";
108 while ($renee_s < $num) { $tnum = $num - 1;
109 $trow = GetAllRows($TableNames[$renee_s]);
110 $numz = count($trow); $kazuki_p = 0;
111 echo "--\n";
112 echo "-- Table structure for table \"".$TableNames[$renee_s]."\"\n";
113 echo "--\n\n";
114 echo $FullTable[$renee_s]."\n";
115 while ($kazuki_p < $numz) { $tnumz = $numz - 1;
116 $srow = null; $srowvalue = null;
117 $trownew = $trow[$kazuki_p];
118 $trowname = array_keys($trownew);
119 $nums = count($trownew); $il = 0;
120 while ($il < $nums) { $tnums = $nums - 1;
121 $trowrname = sql_escape_string($trowname[$il],$SQLStat);
122 $trowrvalue = sql_escape_string($trownew[$trowrname],$SQLStat);
123 if($_GET['outtype']=="UTF-8"&&$Settings['charset']!="UTF-8") {
124 $trowrvalue = utf8_encode($trowrvalue); }
125 $trowrvalue = str_replace( array("\n", "\r"), array('\n', '\r'), $trowrvalue);
126 if($kazuki_p===0) {
127 if($il===0) { $srow = "INSERT INTO \"".$TableNames[$renee_s]."\" ("; }
128 if($il<$tnums&&$il!=$tnums) { $srow .= "\"".$trowrname."\", "; }
129 if($il==$tnums) { $srow .= "\"".$trowrname."\") VALUES"; } }
130 if($il===0) { $srowvalue = "("; }
131 if(!is_numeric($trowrvalue)) { $trowrvalue = "'".$trowrvalue."'"; }
132 if($il<$tnums) { $srowvalue .= $trowrvalue.", "; }
133 if($il==$tnums) { $srowvalue .= $trowrvalue;
134 if($kazuki_p<$tnumz) { $srowvalue .= "),"; }
135 if($kazuki_p==$tnumz) { $srowvalue .= ");"; } }
136 ++$il; }
137 if($kazuki_p===0) {
138 echo "--\n";
139 echo "-- Dumping data for table \"".$TableNames[$renee_s]."\"\n";
140 echo "--\n\n";
141 echo $srow."\n"; }
142 echo $srowvalue."\n";
143 if($kazuki_p==$tnumz&&$renee_s<$tnum) {
144 echo "\n-- --------------------------------------------------------\n"; }
145 ++$kazuki_p; }
146 if($numz===0) {
147 echo "--\n";
148 echo "-- Dumping data for table \"".$TableNames[$renee_s]."\"\n";
149 echo "--\n\n";
150 echo "\n-- --------------------------------------------------------\n"; }
151 echo "\n";
152 ++$renee_s; }
153 fix_amp($Settings['use_gzip'],$GZipEncode['Type']);
154 ?>