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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
|
<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
* Library that provides common import functions that are used by import plugins
*
* @version $Id$
*/
if (! defined('PHPMYADMIN')) {
exit;
}
/**
* We need to know something about user
*/
require_once './libraries/check_user_privileges.lib.php';
/**
* We do this check, DROP DATABASE does not need to be confirmed elsewhere
*/
define('PMA_CHK_DROP', 1);
/**
* Check whether timeout is getting close
*
* @return boolean true if timeout is close
* @access public
*/
function PMA_checkTimeout()
{
global $timestamp, $maximum_time, $timeout_passed;
if ($maximum_time == 0) {
return FALSE;
} elseif ($timeout_passed) {
return TRUE;
/* 5 in next row might be too much */
} elseif ((time() - $timestamp) > ($maximum_time - 5)) {
$timeout_passed = TRUE;
return TRUE;
} else {
return FALSE;
}
}
/**
* Detects what compression filse uses
*
* @param string filename to check
* @return string MIME type of compression, none for none
* @access public
*/
function PMA_detectCompression($filepath)
{
$file = @fopen($filepath, 'rb');
if (!$file) {
return FALSE;
}
$test = fread($file, 4);
$len = strlen($test);
fclose($file);
if ($len >= 2 && $test[0] == chr(31) && $test[1] == chr(139)) {
return 'application/gzip';
}
if ($len >= 3 && substr($test, 0, 3) == 'BZh') {
return 'application/bzip2';
}
if ($len >= 4 && $test == "PK\003\004") {
return 'application/zip';
}
return 'none';
}
/**
* Runs query inside import buffer. This is needed to allow displaying
* of last SELECT, SHOW or HANDLER results and similar nice stuff.
*
* @uses $GLOBALS['finished'] read and write
* @param string query to run
* @param string query to display, this might be commented
* @param bool whether to use control user for queries
* @access public
*/
function PMA_importRunQuery($sql = '', $full = '', $controluser = false)
{
global $import_run_buffer, $go_sql, $complete_query, $display_query,
$sql_query, $my_die, $error, $reload,
$skip_queries, $executed_queries, $max_sql_len, $read_multiply,
$cfg, $sql_query_disabled, $db, $run_query, $is_superuser;
$read_multiply = 1;
if (isset($import_run_buffer)) {
// Should we skip something?
if ($skip_queries > 0) {
$skip_queries--;
} else {
if (!empty($import_run_buffer['sql']) && trim($import_run_buffer['sql']) != '') {
$max_sql_len = max($max_sql_len, strlen($import_run_buffer['sql']));
if (!$sql_query_disabled) {
$sql_query .= $import_run_buffer['full'];
}
if (!$cfg['AllowUserDropDatabase']
&& !$is_superuser
&& preg_match('@^[[:space:]]*DROP[[:space:]]+(IF EXISTS[[:space:]]+)?DATABASE @i', $import_run_buffer['sql'])) {
$GLOBALS['message'] = PMA_Message::error('strNoDropDatabases');
$error = TRUE;
} else {
$executed_queries++;
if ($run_query && $GLOBALS['finished'] && empty($sql) && !$error && (
(!empty($import_run_buffer['sql']) && preg_match('/^[\s]*(SELECT|SHOW|HANDLER)/i', $import_run_buffer['sql'])) ||
($executed_queries == 1)
)) {
$go_sql = TRUE;
if (!$sql_query_disabled) {
$complete_query = $sql_query;
$display_query = $sql_query;
} else {
$complete_query = '';
$display_query = '';
}
$sql_query = $import_run_buffer['sql'];
} elseif ($run_query) {
if ($controluser) {
$result = PMA_query_as_cu($import_run_buffer['sql']);
} else {
$result = PMA_DBI_try_query($import_run_buffer['sql']);
}
$msg = '# ';
if ($result === FALSE) { // execution failed
if (!isset($my_die)) {
$my_die = array();
}
$my_die[] = array('sql' => $import_run_buffer['full'], 'error' => PMA_DBI_getError());
if ($cfg['VerboseMultiSubmit']) {
$msg .= $GLOBALS['strError'];
}
if (!$cfg['IgnoreMultiSubmitErrors']) {
$error = TRUE;
return;
}
} elseif ($cfg['VerboseMultiSubmit']) {
$a_num_rows = (int)@PMA_DBI_num_rows($result);
$a_aff_rows = (int)@PMA_DBI_affected_rows();
if ($a_num_rows > 0) {
$msg .= $GLOBALS['strRows'] . ': ' . $a_num_rows;
} elseif ($a_aff_rows > 0) {
$a_rows =
$msg .= sprintf($GLOBALS['strRowsAffected'], $a_aff_rows);
} else {
$msg .= $GLOBALS['strEmptyResultSet'];
}
}
if (!$sql_query_disabled) {
$sql_query .= $msg . "\n";
}
// If a 'USE <db>' SQL-clause was found and the query succeeded, set our current $db to the new one
if ($result != FALSE && preg_match('@^[\s]*USE[[:space:]]*([\S]+)@i', $import_run_buffer['sql'], $match)) {
$db = trim($match[1]);
$db = trim($db,';'); // for example, USE abc;
$reload = TRUE;
}
if ($result != FALSE && preg_match('@^[\s]*(DROP|CREATE)[\s]+(IF EXISTS[[:space:]]+)?(TABLE|DATABASE)[[:space:]]+(.+)@im', $import_run_buffer['sql'])) {
$reload = TRUE;
}
} // end run query
} // end if not DROP DATABASE
} // end non empty query
elseif (!empty($import_run_buffer['full'])) {
if ($go_sql) {
$complete_query .= $import_run_buffer['full'];
$display_query .= $import_run_buffer['full'];
} else {
if (!$sql_query_disabled) {
$sql_query .= $import_run_buffer['full'];
}
}
}
// check length of query unless we decided to pass it to sql.php
// (if $run_query is false, we are just displaying so show
// the complete query in the textarea)
if (! $go_sql && $run_query) {
if ($cfg['VerboseMultiSubmit'] && ! empty($sql_query)) {
if (strlen($sql_query) > 50000 || $executed_queries > 50 || $max_sql_len > 1000) {
$sql_query = '';
$sql_query_disabled = TRUE;
}
} else {
if (strlen($sql_query) > 10000 || $executed_queries > 10 || $max_sql_len > 500) {
$sql_query = '';
$sql_query_disabled = TRUE;
}
}
}
} // end do query (no skip)
} // end buffer exists
// Do we have something to push into buffer?
if (!empty($sql) || !empty($full)) {
$import_run_buffer = array('sql' => $sql, 'full' => $full);
} else {
unset($GLOBALS['import_run_buffer']);
}
}
/**
* Returns next part of imported file/buffer
*
* @uses $GLOBALS['offset'] read and write
* @uses $GLOBALS['import_file'] read only
* @uses $GLOBALS['import_text'] read and write
* @uses $GLOBALS['finished'] read and write
* @uses $GLOBALS['read_limit'] read only
* @param integer size of buffer to read (this is maximal size
* function will return)
* @return string part of file/buffer
* @access public
*/
function PMA_importGetNextChunk($size = 32768)
{
global $compression, $import_handle, $charset_conversion, $charset_of_file,
$charset, $read_multiply;
// Add some progression while reading large amount of data
if ($read_multiply <= 8) {
$size *= $read_multiply;
} else {
$size *= 8;
}
$read_multiply++;
// We can not read too much
if ($size > $GLOBALS['read_limit']) {
$size = $GLOBALS['read_limit'];
}
if (PMA_checkTimeout()) {
return FALSE;
}
if ($GLOBALS['finished']) {
return TRUE;
}
if ($GLOBALS['import_file'] == 'none') {
// Well this is not yet supported and tested, but should return content of textarea
if (strlen($GLOBALS['import_text']) < $size) {
$GLOBALS['finished'] = TRUE;
return $GLOBALS['import_text'];
} else {
$r = substr($GLOBALS['import_text'], 0, $size);
$GLOBALS['offset'] += $size;
$GLOBALS['import_text'] = substr($GLOBALS['import_text'], $size);
return $r;
}
}
switch ($compression) {
case 'application/bzip2':
$result = bzread($import_handle, $size);
$GLOBALS['finished'] = feof($import_handle);
break;
case 'application/gzip':
$result = gzread($import_handle, $size);
$GLOBALS['finished'] = feof($import_handle);
break;
case 'application/zip':
$result = substr($GLOBALS['import_text'], 0, $size);
$GLOBALS['import_text'] = substr($GLOBALS['import_text'], $size);
$GLOBALS['finished'] = empty($GLOBALS['import_text']);
break;
case 'none':
$result = fread($import_handle, $size);
$GLOBALS['finished'] = feof($import_handle);
break;
}
$GLOBALS['offset'] += $size;
if ($charset_conversion) {
return PMA_convert_string($charset_of_file, $charset, $result);
} else {
/**
* Skip possible byte order marks (I do not think we need more
* charsets, but feel free to add more, you can use wikipedia for
* reference: <http://en.wikipedia.org/wiki/Byte_Order_Mark>)
*
* @todo BOM could be used for charset autodetection
*/
if ($GLOBALS['offset'] == $size) {
// UTF-8
if (strncmp($result, "\xEF\xBB\xBF", 3) == 0) {
$result = substr($result, 3);
// UTF-16 BE, LE
} elseif (strncmp($result, "\xFE\xFF", 2) == 0 || strncmp($result, "\xFF\xFE", 2) == 0) {
$result = substr($result, 2);
}
}
return $result;
}
}
?>
|