3 // vim: expandtab sw=4 ts=4 sts=4:
5 /* Library that provides common import functions that are used by import plugins */
7 // We need to know something about user
8 require_once('./libraries/check_user_privileges.lib.php');
10 define('PMA_CHK_DROP', 1);
13 * Check whether timeout is getting close
15 * @return boolean true if timeout is close
18 function PMA_checkTimeout()
20 global $timestamp, $maximum_time, $timeout_passed;
21 if ($maximum_time == 0) {
23 } elseif ($timeout_passed) {
25 /* 5 in next row might be too much */
26 } elseif ((time() - $timestamp) > ($maximum_time - 5)) {
27 $timeout_passed = TRUE;
35 * Detects what compression filse uses
37 * @param string filename to check
38 * @return string MIME type of compression, none for none
41 function PMA_detectCompression($filepath)
43 $file = @fopen
($filepath, 'rb');
47 $test = fread($file, 4);
50 if ($len >= 2 && $test[0] == chr(31) && $test[1] == chr(139)) {
51 return 'application/gzip';
53 if ($len >= 3 && substr($test, 0, 3) == 'BZh') {
54 return 'application/bzip2';
56 if ($len >= 4 && $test == "PK\003\004") {
57 return 'application/zip';
63 * Runs query inside import buffer. This is needed to allow displaying
64 * of last SELECT, SHOW or HANDLER results and similar nice stuff.
66 * @param string query to run
67 * @param string query to display, this might be commented
70 function PMA_importRunQuery($sql = '', $full = '')
72 global $import_run_buffer, $go_sql, $complete_query, $display_query, $sql_query, $cfg, $my_die, $error, $reload, $finished, $timeout_passed, $skip_queries, $executed_queries, $max_sql_len, $read_multiply, $cfg, $sql_query_disabled, $db, $run_query, $is_superuser, $message, $show_error_header;
74 if (isset($import_run_buffer)) {
75 // Should we skip something?
76 if ($skip_queries > 0) {
79 if (!empty($import_run_buffer['sql']) && trim($import_run_buffer['sql']) != '') {
80 $max_sql_len = max($max_sql_len, strlen($import_run_buffer['sql']));
81 if (!$sql_query_disabled) {
82 $sql_query .= $import_run_buffer['full'];
84 if (!$cfg['AllowUserDropDatabase']
86 && preg_match('@^[[:space:]]*DROP[[:space:]]+(IF EXISTS[[:space:]]+)?DATABASE @i', $import_run_buffer['sql'])) {
87 $message = $GLOBALS['strNoDropDatabases'];
88 $show_error_header = TRUE;
92 if ($run_query && $finished && empty($sql) && !$error && (
93 (!empty($import_run_buffer['sql']) && preg_match('/^[\s]*(SELECT|SHOW|HANDLER)/i', $import_run_buffer['sql'])) ||
94 ($executed_queries == 1)
97 if (!$sql_query_disabled) {
98 $complete_query = $sql_query;
99 $display_query = $sql_query;
101 $complete_query = '';
104 $sql_query = $import_run_buffer['sql'];
105 } elseif ($run_query) {
106 $result = PMA_DBI_try_query($import_run_buffer['sql']);
108 if ($result === FALSE) { // execution failed
109 if (!isset($my_die)) {
112 $my_die[] = array('sql' => $import_run_buffer['full'], 'error' => PMA_DBI_getError());
114 if ($cfg['VerboseMultiSubmit']) {
115 $msg .= $GLOBALS['strError'];
118 if (!$cfg['IgnoreMultiSubmitErrors']) {
122 } elseif ($cfg['VerboseMultiSubmit']) {
123 $a_num_rows = (int)@PMA_DBI_num_rows
($result);
124 $a_aff_rows = (int)@PMA_DBI_affected_rows
();
125 if ($a_num_rows > 0) {
126 $msg .= $GLOBALS['strRows'] . ': ' . $a_num_rows;
127 } elseif ($a_aff_rows > 0) {
129 $msg .= $GLOBALS['strAffectedRows'] . ' ' . $a_aff_rows;
131 $msg .= $GLOBALS['strEmptyResultSet'];
134 if (!$sql_query_disabled) {
135 $sql_query .= $msg . "\n";
138 // If a 'USE <db>' SQL-clause was found and the query succeeded, set our current $db to the new one
139 if ($result != FALSE && preg_match('@^[\s]*USE[[:space:]]*([\S]+)@i', $import_run_buffer['sql'], $match)) {
140 $db = trim($match[1]);
141 $db = trim($db,';'); // for example, USE abc;
145 if ($result != FALSE && preg_match('@^[\s]*(DROP|CREATE)[\s]+(IF EXISTS[[:space:]]+)?(TABLE|DATABASE)[[:space:]]+(.+)@im', $import_run_buffer['sql'])) {
149 } // end if not DROP DATABASE
150 } // end non empty query
151 elseif (!empty($import_run_buffer['full'])) {
153 $complete_query .= $import_run_buffer['full'];
154 $display_query .= $import_run_buffer['full'];
156 if (!$sql_query_disabled) {
157 $sql_query .= $import_run_buffer['full'];
161 // check length of query unless we decided to pass it to sql.php
163 if ($cfg['VerboseMultiSubmit'] && !empty($sql_query)) {
164 if (strlen($sql_query) > 50000 ||
$executed_queries > 50 ||
$max_sql_len > 1000) {
166 $sql_query_disabled = TRUE;
169 if (strlen($sql_query) > 10000 ||
$executed_queries > 10 ||
$max_sql_len > 500) {
171 $sql_query_disabled = TRUE;
175 } // end do query (no skip)
176 } // end buffer exists
178 // Do we have something to push into buffer?
179 if (!empty($sql) ||
!empty($full)) {
180 $import_run_buffer = array('sql' => $sql, 'full' => $full);
182 unset($GLOBALS['import_run_buffer']);
188 * Returns next part of imported file/buffer
190 * @param integer size of buffer to read (this is maximal size
191 * function will return)
192 * @return string part of file/buffer
195 function PMA_importGetNextChunk($size = 32768)
197 global $import_file, $import_text, $finished, $compression, $import_handle, $offset, $charset_conversion, $charset_of_file, $charset, $read_multiply, $read_limit;
199 // Add some progression while reading large amount of data
200 if ($read_multiply <= 8) {
201 $size *= $read_multiply;
207 // We can not read too much
208 if ($size > $read_limit) {
212 if (PMA_checkTimeout()) {
219 if ($import_file == 'none') {
220 // Well this is not yet supported and tested, but should return content of textarea
221 if (strlen($import_text) < $size) {
225 $r = substr($import_text, 0, $size);
227 $import_text = substr($import_text, $size);
232 switch ($compression) {
233 case 'application/bzip2':
234 $result = bzread($import_handle, $size);
235 $finished = feof($import_handle);
237 case 'application/gzip':
238 $result = gzread($import_handle, $size);
239 $finished = feof($import_handle);
241 case 'application/zip':
242 $result = substr($import_text, 0, $size);
243 $import_text = substr($import_text, $size);
244 $finished = empty($import_text);
247 $result = fread($import_handle, $size);
248 $finished = feof($import_handle);
253 if ($charset_conversion) {
254 return PMA_convert_string($charset_of_file, $charset, $result);
257 * Skip possible byte order marks (I do not think we need more
258 * charsets, but feel free to add more, you can use wikipedia for
259 * reference: <http://en.wikipedia.org/wiki/Byte_Order_Mark>)
261 * @todo BOM could be used for charset autodetection
263 if ($offset == $size) {
265 if (strncmp($result, "\xEF\xBB\xBF", 3) == 0) {
266 $result = substr($result, 3);
268 } elseif (strncmp($result, "\xFE\xFF", 2) == 0 ||
strncmp($result, "\xFF\xFE", 2) == 0) {
269 $result = substr($result, 2);