C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\limesurvey\admin\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
<?php
/*
* LimeSurvey
* Copyright (C) 2007 The LimeSurvey Project Team / Carsten Schmitz
* All rights reserved.
* License: GNU/GPL License v2 or later, see LICENSE.php
* LimeSurvey is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.php for copyright notices and details.

* $Id: dumpdb.php 6606 2009-04-09 18:26:36Z c_schmitz $
*/

include_once("login_check.php");  //Login Check dies also if the script is started directly

if ($database_exists && $databasetype=='mysql' && $demoModeOnly != true) {

    
$tables $connect->MetaTables();
    
    
$export="";
    
$export .="#------------------------------------------"."\n";
    
$export .="# LimeSurvey 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($table0strlen($dbprefix))) {
                
$export .= defdump($table);
                
$export .= datadump($table);
            }
        }
        else {
            
$export .= defdump($table);
            
$export .= datadump($table);
        }
    }
    
    
$file_name "LimeSurvey_{$databasename}_dump_".date_shift(date("Y-m-d H:i:s"), "Y-m-d"$timeadjust).".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;
}
 else 
 {
    
$dumpdboutput"<br />\n"
                   
."<table class='alertbox' >\n"
                      
."\t<tr ><td height='4'><font size='1'><strong>".$clang->gT("Export database")."</strong></font></td></tr>\n"
                   
."\t<tr ><td height='4'>".$clang->gT("The database export is only available for MySQL databases. For other database types please use the according backup mechanism to create a database dump.")."</td></tr>"
                   
."</table><br />";
    return;           
 }


    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";
        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";
    }    

?>