File: backup.php

package info (click to toggle)
opendb 0.62p9-3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 3,544 kB
  • ctags: 3,560
  • sloc: php: 26,575; sql: 1,982; sh: 250; makefile: 54
file content (209 lines) | stat: -rw-r--r-- 6,052 bytes parent folder | download
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<?php
/* 	OpenDb - Open Lending Database Project
	Copyright (C) 2001,2002 by Jason Pell

	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.

	Note:
		This backup code is based on the "dump" feature from the phpMyAdmin project.
*/

// This must be first - includes config.php
require_once("./include/begin.inc.php");

include_once("./include/language.php");
include_once("./include/theme.php");

include_once("./functions/function.php");
include_once("./functions/user.php");
include_once("./functions/widgets.php");
include_once("./functions/http.php");
include_once("./functions/utils.php");
include_once("./functions/datetime.php");

/**
	Get the content of $table as a series of INSERT statements.
	
	Simplified, as this is purely a SQL generation script
	now. 
*/	
function get_table_content($table, $send_as_format)
{
   	$result = run_opendb_query("SELECT * FROM $table");
   	$i = 0;
    while($row = mysql_fetch_row($result))
	{
		$table_list = "";
		for($j=0; $j<mysql_num_fields($result);$j++)
		{
			if(strlen($table_list)>0)
				$table_list .= ", ";
				
			$table_list .= mysql_field_name($result,$j);
		}
		$table_list = "(".$table_list.")";

		$schema_insert = "";	
		for($j=0; $j<mysql_num_fields($result);$j++)
		{
			if(strlen($schema_insert)>0)
				$schema_insert .= ", ";
				
			if(!isset($row[$j]))
				$schema_insert .= "NULL";
			else if($row[$j] != "")
			{
				$row[$j] = replace_newlines($row[$j]);
				
				// Escape normal addslashes: \', \", \\, \0 add to that \n
				$row[$j] = addcslashes($row[$j], "\'\"\\\0\n");
				$schema_insert .= "'".$row[$j]."'";
			}
			else
				$schema_insert .= "''";
		}
		
		$schema_insert = "INSERT INTO $table $table_list VALUES (".$schema_insert.")";

		// Get rid of newlines.
		$schema_insert = str_replace("\n","", $schema_insert);
		$schema_insert = str_replace("\r","", $schema_insert);
		
		if(strlen($send_as_format)>0)
			echo(trim($schema_insert).";\n");
		else
			echo htmlspecialchars(trim($schema_insert).";\n");
				
		$i++;
	}
	return TRUE;
}

session_start();
if (is_opendb_valid_session())
{
	// Only admin user is allowed to access this.
	if(is_user_admin($HTTP_SESSION_VARS['user_id'], $HTTP_SESSION_VARS['user_type']))
	{
		if($HTTP_VARS['op'] == 'export')
		{
			@set_time_limit(600);
			if(strlen($HTTP_VARS['send_as_format'])==0)
			{ 
				echo _theme_header($LANG_VARS['backup_database']);
				echo "<pre>\n";
			}
			else
			{
				header("Cache-control: no-store"); // HTTP/1.1
				header("Pragma: no-store"); // HTTP/1.0
				header("Expires: 0");
				header("Content-disposition: attachment; filename=backup.sql");
				header("Content-type: text/plain");
			}

			echo("# -------------------------------------------------------------\n");
		    echo("# ".replace_lang_vars(array('site'=>$CONFIG_VARS['site.title'], 'version'=>$CONFIG_VARS['site.version']), $LANG_VARS['tbl_dump_header'])."\n");
   			echo("# http://sourceforge.net/projects/opendb/\n");
		    echo("#\n");
   			echo("# ".replace_lang_var("host", $CONFIG_VARS['db_server.dbname']."@".$CONFIG_VARS['db_server.host'], $LANG_VARS['connected_to'])."\n");
			echo("# ".replace_lang_var("date", get_localised_timestamp($CONFIG_VARS['print_listing.datetime_mask']), $LANG_VARS['backup_log_generated'])."\n");
		    echo("# -------------------------------------------------------------\n");

			@reset($HTTP_VARS['tables']);
			while (list(,$table) = @each($HTTP_VARS['tables']))
			{
				echo "\n#\n";
				echo "# ".replace_lang_var('table', $table, $LANG_VARS['dumping_data_for_table'])."\n"; 
				echo "#\n\n";

				get_table_content($table, $HTTP_VARS['send_as_format']);
			}			

			if(strlen($HTTP_VARS['send_as_format'])==0)
			{
				echo "</pre>\n";
				echo _theme_footer();
			}
		}
		else //if($HTTP_VARS['op'] == 'export')
		{
			echo _theme_header($LANG_VARS['backup_database']);
			echo("<h2>".$LANG_VARS['backup_database']."</h2>");
			
			
			echo("<form method=\"post\" action=\"$PHP_SELF\">"
				."<input type=hidden name=op value=export>"
				."<table cellspacing=1 border=0>");

			$tables = array('user'=>'Y',
							'review'=>'Y',
							'item'=>'Y',
							'item_instance'=>'Y',
							'item_attribute'=>'Y',
							'borrowed_item'=>'Y',
							's_item_type'=>'N',
							's_attribute_type'=>'N',
							's_attribute_type_lookup'=>'N',
							's_item_attribute_type'=>'N',
							's_status_type'=>'N');
			
			$field = "<table>\n<tr>";
			$count=0;
			while(list($key,$value) = each($tables))
			{
				if($count>=2)
				{
					$field .= "\n</tr>\n<tr>";
					$count=0;
				}
				$field .= "\n<td nowrap><input type=checkbox name=\"tables[]\" value=\"$key\" ".($value=='Y'?"CHECKED":"").">$key</td>";
				
				$count++;
			}
			$field .= "\n</tr>\n</table>";
			
			echo format_field(
				$LANG_VARS['table'], 
				NULL, 
				$field);

			echo format_field(
				$LANG_VARS['send_as_file'],
				NULL,
				"<input type=\"checkbox\" name=\"send_as_format\" value=\"file\">");
		
			echo("</table>
				<br>
				<input type=\"submit\" value=\"".$LANG_VARS['backup_database']."\">
				</form>");
				
			echo _theme_footer();
		}
	}
	else//not an administrator or own user.
	{
		echo _theme_header($LANG_VARS['not_authorized_to_page']);
		echo _theme_error($LANG_VARS['not_authorized_to_page']);
		echo _theme_footer();
	}	
}else
{
	include("./include/invalidsession.php");
}

// Cleanup after begin.inc.php
require_once("./include/end.inc.php");
?>