Fix for the Open in New Window in Patient/Client->Patients search gui, take 2.
[openemr.git] / phpmyadmin / libraries / import.lib.php
blobd69102e14136a6a25497773eefa566af3fcbf8d4
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Library that provides common import functions that are used by import plugins
6 * @version $Id$
7 */
8 if (! defined('PHPMYADMIN')) {
9 exit;
12 /**
13 * We need to know something about user
15 require_once './libraries/check_user_privileges.lib.php';
17 /**
18 * We do this check, DROP DATABASE does not need to be confirmed elsewhere
20 define('PMA_CHK_DROP', 1);
22 /**
23 * Check whether timeout is getting close
25 * @return boolean true if timeout is close
26 * @access public
28 function PMA_checkTimeout()
30 global $timestamp, $maximum_time, $timeout_passed;
31 if ($maximum_time == 0) {
32 return FALSE;
33 } elseif ($timeout_passed) {
34 return TRUE;
35 /* 5 in next row might be too much */
36 } elseif ((time() - $timestamp) > ($maximum_time - 5)) {
37 $timeout_passed = TRUE;
38 return TRUE;
39 } else {
40 return FALSE;
44 /**
45 * Detects what compression filse uses
47 * @param string filename to check
48 * @return string MIME type of compression, none for none
49 * @access public
51 function PMA_detectCompression($filepath)
53 $file = @fopen($filepath, 'rb');
54 if (!$file) {
55 return FALSE;
57 $test = fread($file, 4);
58 $len = strlen($test);
59 fclose($file);
60 if ($len >= 2 && $test[0] == chr(31) && $test[1] == chr(139)) {
61 return 'application/gzip';
63 if ($len >= 3 && substr($test, 0, 3) == 'BZh') {
64 return 'application/bzip2';
66 if ($len >= 4 && $test == "PK\003\004") {
67 return 'application/zip';
69 return 'none';
72 /**
73 * Runs query inside import buffer. This is needed to allow displaying
74 * of last SELECT, SHOW or HANDLER results and similar nice stuff.
76 * @uses $GLOBALS['finished'] read and write
77 * @param string query to run
78 * @param string query to display, this might be commented
79 * @param bool whether to use control user for queries
80 * @access public
82 function PMA_importRunQuery($sql = '', $full = '', $controluser = false)
84 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;
85 $read_multiply = 1;
86 if (isset($import_run_buffer)) {
87 // Should we skip something?
88 if ($skip_queries > 0) {
89 $skip_queries--;
90 } else {
91 if (!empty($import_run_buffer['sql']) && trim($import_run_buffer['sql']) != '') {
92 $max_sql_len = max($max_sql_len, strlen($import_run_buffer['sql']));
93 if (!$sql_query_disabled) {
94 $sql_query .= $import_run_buffer['full'];
96 if (!$cfg['AllowUserDropDatabase']
97 && !$is_superuser
98 && preg_match('@^[[:space:]]*DROP[[:space:]]+(IF EXISTS[[:space:]]+)?DATABASE @i', $import_run_buffer['sql'])) {
99 $message = $GLOBALS['strNoDropDatabases'];
100 $show_error_header = TRUE;
101 $error = TRUE;
102 } else {
103 $executed_queries++;
104 if ($run_query && $GLOBALS['finished'] && empty($sql) && !$error && (
105 (!empty($import_run_buffer['sql']) && preg_match('/^[\s]*(SELECT|SHOW|HANDLER)/i', $import_run_buffer['sql'])) ||
106 ($executed_queries == 1)
107 )) {
108 $go_sql = TRUE;
109 if (!$sql_query_disabled) {
110 $complete_query = $sql_query;
111 $display_query = $sql_query;
112 } else {
113 $complete_query = '';
114 $display_query = '';
116 $sql_query = $import_run_buffer['sql'];
117 } elseif ($run_query) {
118 if ($controluser) {
119 $result = PMA_query_as_cu($import_run_buffer['sql']);
120 } else {
121 $result = PMA_DBI_try_query($import_run_buffer['sql']);
123 $msg = '# ';
124 if ($result === FALSE) { // execution failed
125 if (!isset($my_die)) {
126 $my_die = array();
128 $my_die[] = array('sql' => $import_run_buffer['full'], 'error' => PMA_DBI_getError());
130 if ($cfg['VerboseMultiSubmit']) {
131 $msg .= $GLOBALS['strError'];
134 if (!$cfg['IgnoreMultiSubmitErrors']) {
135 $error = TRUE;
136 return;
138 } elseif ($cfg['VerboseMultiSubmit']) {
139 $a_num_rows = (int)@PMA_DBI_num_rows($result);
140 $a_aff_rows = (int)@PMA_DBI_affected_rows();
141 if ($a_num_rows > 0) {
142 $msg .= $GLOBALS['strRows'] . ': ' . $a_num_rows;
143 } elseif ($a_aff_rows > 0) {
144 $a_rows =
145 $msg .= $GLOBALS['strAffectedRows'] . ' ' . $a_aff_rows;
146 } else {
147 $msg .= $GLOBALS['strEmptyResultSet'];
150 if (!$sql_query_disabled) {
151 $sql_query .= $msg . "\n";
154 // If a 'USE <db>' SQL-clause was found and the query succeeded, set our current $db to the new one
155 if ($result != FALSE && preg_match('@^[\s]*USE[[:space:]]*([\S]+)@i', $import_run_buffer['sql'], $match)) {
156 $db = trim($match[1]);
157 $db = trim($db,';'); // for example, USE abc;
158 $reload = TRUE;
161 if ($result != FALSE && preg_match('@^[\s]*(DROP|CREATE)[\s]+(IF EXISTS[[:space:]]+)?(TABLE|DATABASE)[[:space:]]+(.+)@im', $import_run_buffer['sql'])) {
162 $reload = TRUE;
164 } // end run query
165 } // end if not DROP DATABASE
166 } // end non empty query
167 elseif (!empty($import_run_buffer['full'])) {
168 if ($go_sql) {
169 $complete_query .= $import_run_buffer['full'];
170 $display_query .= $import_run_buffer['full'];
171 } else {
172 if (!$sql_query_disabled) {
173 $sql_query .= $import_run_buffer['full'];
177 // check length of query unless we decided to pass it to sql.php
178 // (if $run_query is false, we are just displaying so show
179 // the complete query in the textarea)
180 if (! $go_sql && $run_query) {
181 if ($cfg['VerboseMultiSubmit'] && ! empty($sql_query)) {
182 if (strlen($sql_query) > 50000 || $executed_queries > 50 || $max_sql_len > 1000) {
183 $sql_query = '';
184 $sql_query_disabled = TRUE;
186 } else {
187 if (strlen($sql_query) > 10000 || $executed_queries > 10 || $max_sql_len > 500) {
188 $sql_query = '';
189 $sql_query_disabled = TRUE;
193 } // end do query (no skip)
194 } // end buffer exists
196 // Do we have something to push into buffer?
197 if (!empty($sql) || !empty($full)) {
198 $import_run_buffer = array('sql' => $sql, 'full' => $full);
199 } else {
200 unset($GLOBALS['import_run_buffer']);
206 * Returns next part of imported file/buffer
208 * @uses $GLOBALS['offset'] read and write
209 * @uses $GLOBALS['import_file'] read only
210 * @uses $GLOBALS['import_text'] read and write
211 * @uses $GLOBALS['finished'] read and write
212 * @uses $GLOBALS['read_limit'] read only
213 * @param integer size of buffer to read (this is maximal size
214 * function will return)
215 * @return string part of file/buffer
216 * @access public
218 function PMA_importGetNextChunk($size = 32768)
220 global $compression, $import_handle, $charset_conversion, $charset_of_file,
221 $charset, $read_multiply;
223 // Add some progression while reading large amount of data
224 if ($read_multiply <= 8) {
225 $size *= $read_multiply;
226 } else {
227 $size *= 8;
229 $read_multiply++;
231 // We can not read too much
232 if ($size > $GLOBALS['read_limit']) {
233 $size = $GLOBALS['read_limit'];
236 if (PMA_checkTimeout()) {
237 return FALSE;
239 if ($GLOBALS['finished']) {
240 return TRUE;
243 if ($GLOBALS['import_file'] == 'none') {
244 // Well this is not yet supported and tested, but should return content of textarea
245 if (strlen($GLOBALS['import_text']) < $size) {
246 $GLOBALS['finished'] = TRUE;
247 return $GLOBALS['import_text'];
248 } else {
249 $r = substr($GLOBALS['import_text'], 0, $size);
250 $GLOBALS['offset'] += $size;
251 $GLOBALS['import_text'] = substr($GLOBALS['import_text'], $size);
252 return $r;
256 switch ($compression) {
257 case 'application/bzip2':
258 $result = bzread($import_handle, $size);
259 $GLOBALS['finished'] = feof($import_handle);
260 break;
261 case 'application/gzip':
262 $result = gzread($import_handle, $size);
263 $GLOBALS['finished'] = feof($import_handle);
264 break;
265 case 'application/zip':
266 $result = substr($GLOBALS['import_text'], 0, $size);
267 $GLOBALS['import_text'] = substr($GLOBALS['import_text'], $size);
268 $GLOBALS['finished'] = empty($GLOBALS['import_text']);
269 break;
270 case 'none':
271 $result = fread($import_handle, $size);
272 $GLOBALS['finished'] = feof($import_handle);
273 break;
275 $GLOBALS['offset'] += $size;
277 if ($charset_conversion) {
278 return PMA_convert_string($charset_of_file, $charset, $result);
279 } else {
281 * Skip possible byte order marks (I do not think we need more
282 * charsets, but feel free to add more, you can use wikipedia for
283 * reference: <http://en.wikipedia.org/wiki/Byte_Order_Mark>)
285 * @todo BOM could be used for charset autodetection
287 if ($GLOBALS['offset'] == $size) {
288 // UTF-8
289 if (strncmp($result, "\xEF\xBB\xBF", 3) == 0) {
290 $result = substr($result, 3);
291 // UTF-16 BE, LE
292 } elseif (strncmp($result, "\xFE\xFF", 2) == 0 || strncmp($result, "\xFF\xFE", 2) == 0) {
293 $result = substr($result, 2);
296 return $result;