File: backup.php

package info (click to toggle)
opendb 0.81p18-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 4,716 kB
  • ctags: 6,787
  • sloc: php: 50,213; sql: 3,098; sh: 272; makefile: 54; xml: 48
file content (279 lines) | stat: -rw-r--r-- 8,382 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
<?php
/* 	OpenDb - Open Media Lending Database
	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/database.php");
include_once("./functions/auth.php");
include_once("./functions/logging.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, $crlf)
{
	global $CONFIG_VARS;
	
   	$result = run_opendb_query("SELECT * FROM $table");

	//prefix if required to table name before exporting.
	if(strlen($CONFIG_VARS['db_server.table_prefix'])>0)
	{
		$table = $CONFIG_VARS['db_server.table_prefix'].$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).";".$crlf);
		else
			echo htmlspecialchars(trim($schema_insert).";".$crlf);
				
		$i++;
	}
	return TRUE;
}

if($CONFIG_VARS['site.enable']!==FALSE)
{
	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']))
		{
			$_TABLES = array('user'=>'Y',
							'user_address'=>'Y',
							'user_address_attribute'=>'Y',
							'review'=>'Y',
							'item'=>'Y',
							'item_instance'=>'Y',
							'item_attribute'=>'Y',
							'borrowed_item'=>'Y',
							's_item_type'=>'N',
							's_item_type_group'=>'N',
							's_item_type_group_rltshp'=>'N',
							's_attribute_type'=>'N',
							's_attribute_type_lookup'=>'N',
							's_item_attribute_type'=>'N',
							's_status_type'=>'N',
							's_address_type'=>'N',
							's_addr_attribute_type_rltshp'=>'N',
							's_site_plugin'=>'N',
							's_site_plugin_conf'=>'N',
							's_site_plugin_input_field'=>'N',
							's_site_plugin_s_attribute_type_map'=>'N',
							's_site_plugin_s_attribute_type_lookup_map'=>'N',
							's_site_plugin_link'=>'N'
						);
								
			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";
					
					// if in browser then use simple unix newline.
					$CRLF = "\n";
				}
				else
				{
					header("Cache-control: no-store");
					header("Pragma: no-store");
					header("Expires: 0");
					header("Content-disposition: attachment; filename=backup.sql");
					header("Content-type: application/octet-stream");
					
					$CRLF = get_user_browser_crlf();
				}
	
				echo("# -------------------------------------------------------------".$CRLF);
			    echo("# ".replace_lang_vars(array('site'=>$CONFIG_VARS['site.title'], 'version'=>$CONFIG_VARS['site.version']), $LANG_VARS['tbl_dump_header']).$CRLF);
	   			echo("# http://sourceforge.net/projects/opendb/".$CRLF);
			    echo("#".$CRLF);
	   			echo("# ".replace_lang_var("host", $CONFIG_VARS['db_server.dbname']."@".$CONFIG_VARS['db_server.host'], $LANG_VARS['connected_to']).$CRLF);
				echo("# ".replace_lang_var("date", get_localised_timestamp($CONFIG_VARS['print_listing.datetime_mask']), $LANG_VARS['backup_log_generated']).$CRLF);
			    echo("# -------------------------------------------------------------".$CRLF);
	
				// special all tables option reset $HTTP_VARS['tables'] array as a result
				if(strcasecmp($HTTP_VARS['all_tables'],'y')===0)
				{
					unset($HTTP_VARS['tables']);
					
					while(list($key,$value) = each($_TABLES))
					{
						$HTTP_VARS['tables'][] = $key;
					}
				}
				
				@reset($HTTP_VARS['tables']);
				while (list(,$table) = @each($HTTP_VARS['tables']))
				{
					echo $CRLF."#".$CRLF;
					echo "# ".replace_lang_var('table', $table, $LANG_VARS['dumping_data_for_table']).$CRLF; 
					echo "#".$CRLF.$CRLF;
	
					get_table_content($table, $HTTP_VARS['send_as_format'], $CRLF);
				}			
	
				if(strlen($HTTP_VARS['send_as_format'])==0)
				{
					echo "\n</pre>";
					echo _theme_footer();
				}
			}
			else //if($HTTP_VARS['op'] == 'export')
			{
				echo _theme_header($LANG_VARS['backup_database']);
				echo("<h2>".$LANG_VARS['backup_database']."</h2>");
				
				echo(get_setcheckboxes_javascript());
				
				echo("<form method=\"post\" action=\"$PHP_SELF\">"
					."<input type=hidden name=op value=export>"
					."<table cellspacing=1 border=0>");
				
				$field = "<table border=0>\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++;
				}
				
				if($count>0 && $count < 2) // $count == 1 would have been easier, but might change to 3 columns, so logic is easier to maintain
				{
					for($i=$count; $i<2; $i++)
					{
						$field .= "\n<td>&nbsp;</td>";
					}					
				}
				
				$field .= "\n</tr>\n";
				
				$field .= "\n<tr><td colspan=2>&nbsp;</td></tr>";
				$field .= "\n<tr><td colspan=2>".
							"<input type=button value=\"".$LANG_VARS['check_all']."\" onClick=\"setCheckboxes(this.form, 'tables[]', true);\">&nbsp;".
							"<input type=button value=\"".$LANG_VARS['uncheck_all']."\" onClick=\"setCheckboxes(this.form, 'tables[]', false);\">&nbsp;".
							"<input type=reset value=\"".$LANG_VARS['reset']."\">".
							"</td></tr>";
				
				$field .= "\n</table>\n";
				
				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\" CHECKED>");
			
				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
	{
		// invalid login, so login instead.
		include("./login.php");
	}
}//if($CONFIG_VARS['site.enable']!==FALSE)
else
{
	echo _theme_header($LANG_VARS['site_is_disabled'], FALSE);
	echo _theme_error($LANG_VARS['site_is_disabled']);
	echo _theme_footer();
}

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