Projekt

Obecné

Profil

Stáhnout (10.5 KB) Statistiky
| Větev: | Tag: | Revize:
1 6daefa8c Petr Lukašík
<?php
2
3
	/**
4
	 * Does an export to the screen or as a download.  This checks to
5
	 * see if they have pg_dump set up, and will use it if possible.
6
	 *
7
	 * $Id: dataexport.php,v 1.22 2005/10/18 03:45:16 chriskl Exp $
8
	 */
9
10
	$extensions = array(
11
		'sql' => 'sql',
12
		'copy' => 'sql',
13
		'csv' => 'csv',
14
		'tab' => 'txt',
15
		'html' => 'html',
16
		'xml' => 'xml'
17
	);
18
19
	// Prevent timeouts on large exports (non-safe mode only)
20
	if (!ini_get('safe_mode')) set_time_limit(0);
21
22
	// if (!isset($_REQUEST['table']) && !isset($_REQUEST['query']))
23
	// What must we do in this case? Maybe redirect to the homepage?
24
25
	// If format is set, then perform the export
26
	if (isset($_REQUEST['what'])) {
27
		
28
		// Include application functions
29
		$_no_output = true;
30
		include_once('./libraries/lib.inc.php');
31
		
32
		switch ($_REQUEST['what']) {
33
			case 'dataonly':
34
				// Check to see if they have pg_dump set up and if they do, use that
35
				// instead of custom dump code
36
				if ($misc->isDumpEnabled()
37
						&& ($_REQUEST['d_format'] == 'copy' || $_REQUEST['d_format'] == 'sql')) {
38
					include('./dbexport.php');
39
					exit;
40
				}
41
				else {
42
					$format = $_REQUEST['d_format'];
43
					$oids = isset($_REQUEST['d_oids']);
44
				}
45
				break;
46
			case 'structureonly':
47
				// Check to see if they have pg_dump set up and if they do, use that
48
				// instead of custom dump code
49
				if ($misc->isDumpEnabled()) {
50
					include('./dbexport.php');
51
					exit;
52
				}
53
				else $clean = isset($_REQUEST['s_clean']);
54
				break;
55
			case 'structureanddata':
56
				// Check to see if they have pg_dump set up and if they do, use that
57
				// instead of custom dump code
58
				if ($misc->isDumpEnabled()) {
59
					include('./dbexport.php');
60
					exit;
61
				}
62
				else {
63
					$format = $_REQUEST['sd_format'];
64
					$clean = isset($_REQUEST['sd_clean']);
65
					$oids = isset($_REQUEST['sd_oids']);
66
				}
67
				break;
68
		}
69
70
		// Make it do a download, if necessary
71
		if ($_REQUEST['output'] == 'download') {
72
			// Set headers.  MSIE is totally broken for SSL downloading, so
73
			// we need to have it download in-place as plain text
74
			if (strstr($_SERVER['HTTP_USER_AGENT'], 'MSIE') && isset($_SERVER['HTTPS'])) {
75
				header('Content-Type: text/plain');
76
			}
77
			else {
78
				header('Content-Type: application/download');
79
		
80
				if (isset($extensions[$format]))
81
					$ext = $extensions[$format];
82
				else
83
					$ext = 'txt';
84
		
85
				header('Content-Disposition: attachment; filename=dump.' . $ext);
86
			}
87
		}
88
		else {
89
			header('Content-Type: text/plain');
90
		}
91
	
92
		if (isset($_REQUEST['query'])) $_REQUEST['query'] = trim(urldecode($_REQUEST['query']));
93
94
		// Set the schema search path
95
		if ($data->hasSchemas() && isset($_REQUEST['search_path'])) {
96
			$data->setSearchPath(array_map('trim',explode(',',$_REQUEST['search_path'])));
97
		}
98
		
99
		// Set up the dump transaction
100
		$status = $data->beginDump();
101
102
		// If the dump is not dataonly then dump the structure prefix
103
		if ($_REQUEST['what'] != 'dataonly')
104
			echo $data->getTableDefPrefix($_REQUEST['table'], $clean);
105
106
		// If the dump is not structureonly then dump the actual data
107
		if ($_REQUEST['what'] != 'structureonly') {
108
			// Get database encoding
109
			$dbEncoding = $data->getDatabaseEncoding();
110
111
			// Set fetch mode to NUM so that duplicate field names are properly returned
112
			$data->conn->setFetchMode(ADODB_FETCH_NUM);
113
114
			// Execute the query, if set, otherwise grab all rows from the table
115
			if (isset($_REQUEST['table']))
116
				$rs = $data->dumpRelation($_REQUEST['table'], $oids);
117
			else
118
				$rs = $data->conn->Execute($_REQUEST['query']);
119
120
			if ($format == 'copy') {
121
				$data->fieldClean($_REQUEST['table']);
122
				echo "COPY \"{$_REQUEST['table']}\"";
123
				if ($oids) echo " WITH OIDS";
124
				echo " FROM stdin;\n";
125
				while (!$rs->EOF) {
126
					$first = true;
127
					while(list($k, $v) = each($rs->f)) {
128
						// Escape value
129
						$v = $data->escapeBytea($v);
130
						
131
						// We add an extra escaping slash onto octal encoded characters
132
						$v = ereg_replace('\\\\([0-7]{3})', '\\\\\1', $v);
133
						if ($first) {
134
							echo ($v == null) ? '\\N' : $v;
135
							$first = false;
136
						}
137
						else echo "\t", ($v == null) ? '\\N' : $v;
138
					}
139
					echo "\n";
140
					$rs->moveNext();
141
				}
142
				echo "\\.\n";
143
			}
144
			elseif ($format == 'html') {
145
				echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\r\n";
146
				echo "<html xmlns=\"http://www.w3.org/1999/xhtml\">\r\n";
147
				echo "<head>\r\n";
148
				echo "\t<title></title>\r\n";
149
				echo "\t<meta http-equiv=\"Content-Type\" content=\"text/html; charset={$data->codemap[$dbEncoding]}\" />\r\n";
150
				echo "</head>\r\n";
151
				echo "<body>\r\n";
152
				echo "<table class=\"phppgadmin\">\r\n";
153
				echo "\t<tr>\r\n";
154
				if (!$rs->EOF) {
155
					// Output header row
156
					$j = 0;
157
					foreach ($rs->f as $k => $v) {
158
						$finfo = $rs->fetchField($j++);
159
						if ($finfo->name == $data->id && !$oids) continue;
160
						echo "\t\t<th>", $misc->printVal($finfo->name, true), "</th>\r\n";
161
					}
162
				}
163
				echo "\t</tr>\r\n";
164
				while (!$rs->EOF) {
165
					echo "\t<tr>\r\n";
166
					$j = 0;
167
					foreach ($rs->f as $k => $v) {
168
						$finfo = $rs->fetchField($j++);
169
						if ($finfo->name == $data->id && !$oids) continue;
170
						echo "\t\t<td>", $misc->printVal($v, true, $finfo->type), "</td>\r\n";
171
					}
172
					echo "\t</tr>\r\n";
173
					$rs->moveNext();
174
				}
175
				echo "</table>\r\n";
176
				echo "</body>\r\n";
177
				echo "</html>\r\n";
178
			}
179
			elseif ($format == 'xml') {
180
				echo "<?xml version=\"1.0\"";
181
				if (isset($data->codemap[$dbEncoding]))
182
					echo " encoding=\"{$data->codemap[$dbEncoding]}\"";
183
				echo " ?>\n";
184
				echo "<data>\n";
185
				if (!$rs->EOF) {
186
					// Output header row
187
					$j = 0;
188
					echo "\t<header>\n";
189
					foreach ($rs->f as $k => $v) {
190
						$finfo = $rs->fetchField($j++);
191
						$name = htmlspecialchars($finfo->name);
192
						$type = htmlspecialchars($finfo->type);
193
						echo "\t\t<column name=\"{$name}\" type=\"{$type}\" />\n";
194
					}
195
					echo "\t</header>\n";
196
				}
197
				echo "\t<records>\n";
198
				while (!$rs->EOF) {
199
					$j = 0;
200
					echo "\t\t<row>\n";
201
					foreach ($rs->f as $k => $v) {
202
						$finfo = $rs->fetchField($j++);
203
						$name = htmlspecialchars($finfo->name);
204
						if ($v != null) $v = htmlspecialchars($v);
205
						echo "\t\t\t<column name=\"{$name}\"", ($v == null ? ' null="null"' : ''), ">{$v}</column>\n";
206
					}
207
					echo "\t\t</row>\n";
208
					$rs->moveNext();
209
				}
210
				echo "\t</records>\n";
211
				echo "</data>\n";
212
			}
213
			elseif ($format == 'sql') {
214
				$data->fieldClean($_REQUEST['table']);
215
				while (!$rs->EOF) {
216
					echo "INSERT INTO \"{$_REQUEST['table']}\" (";
217
					$first = true;
218
					$j = 0;
219
					foreach ($rs->f as $k => $v) {
220
						$finfo = $rs->fetchField($j++);
221
						$k = $finfo->name;
222
						// SQL (INSERT) format cannot handle oids
223
	//						if ($k == $data->id) continue;
224
						// Output field
225
						$data->fieldClean($k);
226
						if ($first) echo "\"{$k}\"";
227
						else echo ", \"{$k}\"";
228
229
						if ($v != null) {
230
							// Output value
231
							// addCSlashes converts all weird ASCII characters to octal representation,
232
							// EXCEPT the 'special' ones like \r \n \t, etc.
233
							$v = addCSlashes($v, "\0..\37\177..\377");
234
							// We add an extra escaping slash onto octal encoded characters
235
							$v = ereg_replace('\\\\([0-7]{3})', '\\\1', $v);
236
							// Finally, escape all apostrophes
237
							$v = str_replace("'", "''", $v);
238
						}
239
						if ($first) {
240
							$values = ($v === null) ? 'NULL' : "'{$v}'";
241
							$first = false;
242
						}
243
						else $values .= ', ' . (($v === null) ? 'NULL' : "'{$v}'");
244
					}
245
					echo ") VALUES ({$values});\n";
246
					$rs->moveNext();
247
				}
248
			}
249
			else {
250
				switch ($format) {
251
					case 'tab':
252
						$sep = "\t";
253
						break;
254
					case 'csv':
255
					default:
256
						$sep = ',';
257
						break;
258
				}
259
				if (!$rs->EOF) {
260
					// Output header row
261
					$first = true;
262
					foreach ($rs->f as $k => $v) {
263
						$finfo = $rs->fetchField($k);
264
						$v = $finfo->name;
265
						if ($v != null) $v = str_replace('"', '""', $v);
266
						if ($first) {
267
							echo "\"{$v}\"";
268
							$first = false;
269
						}
270
						else echo "{$sep}\"{$v}\"";
271
					}
272
					echo "\r\n";
273
				}
274
				while (!$rs->EOF) {
275
					$first = true;
276
					foreach ($rs->f as $k => $v) {
277
						if ($v != null) $v = str_replace('"', '""', $v);
278
						if ($first) {
279
							echo ($v == null) ? "\"\\N\"" : "\"{$v}\"";
280
							$first = false;
281
						}
282
						else echo ($v == null) ? "{$sep}\"\\N\"" : "{$sep}\"{$v}\"";
283
					}
284
					echo "\r\n";
285
					$rs->moveNext();
286
				}
287
			}
288
		}
289
290
		// If the dump is not dataonly then dump the structure suffix
291
		if ($_REQUEST['what'] != 'dataonly') {
292
			// Set fetch mode back to ASSOC for the table suffix to work
293
			$data->conn->setFetchMode(ADODB_FETCH_ASSOC);
294
			echo $data->getTableDefSuffix($_REQUEST['table']);
295
		}
296
297
		// Finish the dump transaction
298
		$status = $data->endDump();
299
	}
300
	else {
301
		// Include application functions
302
		include_once('./libraries/lib.inc.php');
303
304
		$misc->printHeader($lang['strexport']);
305
		$misc->printBody();
306
		$misc->printTrail(isset($_REQUEST['subject']) ? $_REQUEST['subject'] : 'database');
307
		$misc->printTitle($lang['strexport']);
308
		if (isset($msg)) $misc->printMsg($msg);
309
310
		echo "<form action=\"{$_SERVER['PHP_SELF']}\" method=\"post\">\n";
311
		echo "<table>\n";
312
		echo "<tr><th class=\"data\">{$lang['strformat']}:</th><td><select name=\"d_format\">\n";
313
		// COPY and SQL require a table
314
		if (isset($_REQUEST['table'])) {
315
			echo "<option value=\"copy\">COPY</option>\n";
316
			echo "<option value=\"sql\">SQL</option>\n";
317
		}
318
		echo "<option value=\"csv\">CSV</option>\n";
319
		echo "<option value=\"tab\">{$lang['strtabbed']}</option>\n";
320
		echo "<option value=\"html\">XHTML</option>\n";
321
		echo "<option value=\"xml\">XML</option>\n";
322
		echo "</select></td></tr>";
323
		echo "</table>\n";
324
325
		echo "<h3>{$lang['stroptions']}</h3>\n";
326
		echo "<p><input type=\"radio\" name=\"output\" value=\"show\" checked=\"checked\" />{$lang['strshow']}\n";
327
		echo "<br/><input type=\"radio\" name=\"output\" value=\"download\" />{$lang['strdownload']}</p>\n";
328
329
		echo "<p><input type=\"hidden\" name=\"action\" value=\"export\" />\n";
330
		echo "<input type=\"hidden\" name=\"what\" value=\"dataonly\" />\n";
331
		if (isset($_REQUEST['table'])) {
332
			echo "<input type=\"hidden\" name=\"table\" value=\"", htmlspecialchars($_REQUEST['table']), "\" />\n";
333
		}
334
		echo "<input type=\"hidden\" name=\"query\" value=\"", htmlspecialchars(urlencode($_REQUEST['query'])), "\" />\n";
335
		if (isset($_REQUEST['search_path'])) {
336
			echo "<input type=\"hidden\" name=\"search_path\" value=\"", htmlspecialchars($_REQUEST['search_path']), "\" />\n";
337
		}
338
		echo $misc->form;
339
		echo "<input type=\"submit\" value=\"{$lang['strexport']}\" /></p>\n";
340
		echo "</form>\n";
341
		
342
		$misc->printFooter();
343
	}	
344
345
?>