2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Library that provides common import functions that are used by import plugins
10 * We need to know something about user
12 require_once './libraries/check_user_privileges.lib.php';
15 * We do this check, DROP DATABASE does not need to be confirmed elsewhere
17 define('PMA_CHK_DROP', 1);
20 * Check whether timeout is getting close
22 * @return boolean true if timeout is close
25 function PMA_checkTimeout()
27 global $timestamp, $maximum_time, $timeout_passed;
28 if ($maximum_time == 0) {
30 } elseif ($timeout_passed) {
32 /* 5 in next row might be too much */
33 } elseif ((time() - $timestamp) > ($maximum_time - 5)) {
34 $timeout_passed = TRUE;
42 * Detects what compression filse uses
44 * @param string filename to check
45 * @return string MIME type of compression, none for none
48 function PMA_detectCompression($filepath)
50 $file = @fopen
($filepath, 'rb');
54 $test = fread($file, 4);
57 if ($len >= 2 && $test[0] == chr(31) && $test[1] == chr(139)) {
58 return 'application/gzip';
60 if ($len >= 3 && substr($test, 0, 3) == 'BZh') {
61 return 'application/bzip2';
63 if ($len >= 4 && $test == "PK\003\004") {
64 return 'application/zip';
70 * Runs query inside import buffer. This is needed to allow displaying
71 * of last SELECT, SHOW or HANDLER results and similar nice stuff.
73 * @uses $GLOBALS['finished'] read and write
74 * @param string query to run
75 * @param string query to display, this might be commented
76 * @param bool whether to use control user for queries
79 function PMA_importRunQuery($sql = '', $full = '', $controluser = false)
81 global $import_run_buffer, $go_sql, $complete_query, $display_query, $sql_query, $cfg, $my_die, $error, $reload, $timeout_passed, $skip_queries, $executed_queries, $max_sql_len, $read_multiply, $cfg, $sql_query_disabled, $db, $run_query, $is_superuser, $message, $show_error_header;
83 if (isset($import_run_buffer)) {
84 // Should we skip something?
85 if ($skip_queries > 0) {
88 if (!empty($import_run_buffer['sql']) && trim($import_run_buffer['sql']) != '') {
89 $max_sql_len = max($max_sql_len, strlen($import_run_buffer['sql']));
90 if (!$sql_query_disabled) {
91 $sql_query .= $import_run_buffer['full'];
93 if (!$cfg['AllowUserDropDatabase']
95 && preg_match('@^[[:space:]]*DROP[[:space:]]+(IF EXISTS[[:space:]]+)?DATABASE @i', $import_run_buffer['sql'])) {
96 $message = $GLOBALS['strNoDropDatabases'];
97 $show_error_header = TRUE;
101 if ($run_query && $GLOBALS['finished'] && empty($sql) && !$error && (
102 (!empty($import_run_buffer['sql']) && preg_match('/^[\s]*(SELECT|SHOW|HANDLER)/i', $import_run_buffer['sql'])) ||
103 ($executed_queries == 1)
106 if (!$sql_query_disabled) {
107 $complete_query = $sql_query;
108 $display_query = $sql_query;
110 $complete_query = '';
113 $sql_query = $import_run_buffer['sql'];
114 } elseif ($run_query) {
116 $result = PMA_query_as_cu($import_run_buffer['sql']);
118 $result = PMA_DBI_try_query($import_run_buffer['sql']);
121 if ($result === FALSE) { // execution failed
122 if (!isset($my_die)) {
125 $my_die[] = array('sql' => $import_run_buffer['full'], 'error' => PMA_DBI_getError());
127 if ($cfg['VerboseMultiSubmit']) {
128 $msg .= $GLOBALS['strError'];
131 if (!$cfg['IgnoreMultiSubmitErrors']) {
135 } elseif ($cfg['VerboseMultiSubmit']) {
136 $a_num_rows = (int)@PMA_DBI_num_rows
($result);
137 $a_aff_rows = (int)@PMA_DBI_affected_rows
();
138 if ($a_num_rows > 0) {
139 $msg .= $GLOBALS['strRows'] . ': ' . $a_num_rows;
140 } elseif ($a_aff_rows > 0) {
142 $msg .= $GLOBALS['strAffectedRows'] . ' ' . $a_aff_rows;
144 $msg .= $GLOBALS['strEmptyResultSet'];
147 if (!$sql_query_disabled) {
148 $sql_query .= $msg . "\n";
151 // If a 'USE <db>' SQL-clause was found and the query succeeded, set our current $db to the new one
152 if ($result != FALSE && preg_match('@^[\s]*USE[[:space:]]*([\S]+)@i', $import_run_buffer['sql'], $match)) {
153 $db = trim($match[1]);
154 $db = trim($db,';'); // for example, USE abc;
158 if ($result != FALSE && preg_match('@^[\s]*(DROP|CREATE)[\s]+(IF EXISTS[[:space:]]+)?(TABLE|DATABASE)[[:space:]]+(.+)@im', $import_run_buffer['sql'])) {
162 } // end if not DROP DATABASE
163 } // end non empty query
164 elseif (!empty($import_run_buffer['full'])) {
166 $complete_query .= $import_run_buffer['full'];
167 $display_query .= $import_run_buffer['full'];
169 if (!$sql_query_disabled) {
170 $sql_query .= $import_run_buffer['full'];
174 // check length of query unless we decided to pass it to sql.php
176 if ($cfg['VerboseMultiSubmit'] && ! empty($sql_query)) {
177 if (strlen($sql_query) > 50000 ||
$executed_queries > 50 ||
$max_sql_len > 1000) {
179 $sql_query_disabled = TRUE;
182 if (strlen($sql_query) > 10000 ||
$executed_queries > 10 ||
$max_sql_len > 500) {
184 $sql_query_disabled = TRUE;
188 } // end do query (no skip)
189 } // end buffer exists
191 // Do we have something to push into buffer?
192 if (!empty($sql) ||
!empty($full)) {
193 $import_run_buffer = array('sql' => $sql, 'full' => $full);
195 unset($GLOBALS['import_run_buffer']);
201 * Returns next part of imported file/buffer
203 * @uses $GLOBALS['offset'] read and write
204 * @uses $GLOBALS['import_file'] read only
205 * @uses $GLOBALS['import_text'] read and write
206 * @uses $GLOBALS['finished'] read and write
207 * @uses $GLOBALS['read_limit'] read only
208 * @param integer size of buffer to read (this is maximal size
209 * function will return)
210 * @return string part of file/buffer
213 function PMA_importGetNextChunk($size = 32768)
215 global $compression, $import_handle, $charset_conversion, $charset_of_file,
216 $charset, $read_multiply;
218 // Add some progression while reading large amount of data
219 if ($read_multiply <= 8) {
220 $size *= $read_multiply;
226 // We can not read too much
227 if ($size > $GLOBALS['read_limit']) {
228 $size = $GLOBALS['read_limit'];
231 if (PMA_checkTimeout()) {
234 if ($GLOBALS['finished']) {
238 if ($GLOBALS['import_file'] == 'none') {
239 // Well this is not yet supported and tested, but should return content of textarea
240 if (strlen($GLOBALS['import_text']) < $size) {
241 $GLOBALS['finished'] = TRUE;
242 return $GLOBALS['import_text'];
244 $r = substr($GLOBALS['import_text'], 0, $size);
245 $GLOBALS['offset'] +
= $size;
246 $GLOBALS['import_text'] = substr($GLOBALS['import_text'], $size);
251 switch ($compression) {
252 case 'application/bzip2':
253 $result = bzread($import_handle, $size);
254 $GLOBALS['finished'] = feof($import_handle);
256 case 'application/gzip':
257 $result = gzread($import_handle, $size);
258 $GLOBALS['finished'] = feof($import_handle);
260 case 'application/zip':
261 $result = substr($GLOBALS['import_text'], 0, $size);
262 $GLOBALS['import_text'] = substr($GLOBALS['import_text'], $size);
263 $GLOBALS['finished'] = empty($GLOBALS['import_text']);
266 $result = fread($import_handle, $size);
267 $GLOBALS['finished'] = feof($import_handle);
270 $GLOBALS['offset'] +
= $size;
272 if ($charset_conversion) {
273 return PMA_convert_string($charset_of_file, $charset, $result);
276 * Skip possible byte order marks (I do not think we need more
277 * charsets, but feel free to add more, you can use wikipedia for
278 * reference: <http://en.wikipedia.org/wiki/Byte_Order_Mark>)
280 * @todo BOM could be used for charset autodetection
282 if ($GLOBALS['offset'] == $size) {
284 if (strncmp($result, "\xEF\xBB\xBF", 3) == 0) {
285 $result = substr($result, 3);
287 } elseif (strncmp($result, "\xFE\xFF", 2) == 0 ||
strncmp($result, "\xFF\xFE", 2) == 0) {
288 $result = substr($result, 2);