1. Check existence of mb_string, mysql and xml extensions before installation.
[openemr.git] / phpmyadmin / libraries / display_import.lib.php
blobb8059d9a63b549234105690a763887bebc6d9b98
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 /**
5 * functions for displaying import for: server, database and table
7 * @usedby display_import.inc.php
9 * @package PhpMyAdmin
12 if (! defined('PHPMYADMIN')) {
13 exit;
16 /**
17 * Prints Html For Display Import Hidden Input
19 * @param String $import_type Import type: server, database, table
20 * @param String $db Selected DB
21 * @param String $table Selected Table
23 * @return string
25 function PMA_getHtmlForHiddenInputs($import_type, $db, $table)
27 $html = '';
28 if ($import_type == 'server') {
29 $html .= PMA_URL_getHiddenInputs('', '', 1);
30 } elseif ($import_type == 'database') {
31 $html .= PMA_URL_getHiddenInputs($db, '', 1);
32 } else {
33 $html .= PMA_URL_getHiddenInputs($db, $table, 1);
35 $html .= ' <input type="hidden" name="import_type" value="'
36 . $import_type . '" />' . "\n";
38 return $html;
41 /**
42 * Prints Html For Import Javascript
44 * @param int $upload_id The selected upload id
46 * @return string
48 function PMA_getHtmlForImportJS($upload_id)
50 global $SESSION_KEY;
51 $html = '';
52 $html .= '<script type="text/javascript">';
53 $html .= ' //<![CDATA[';
54 //with "\n", so that the following lines won't be commented out by //<![CDATA[
55 $html .= "\n";
56 $html .= ' $( function() {';
57 // add event when user click on "Go" button
58 $html .= ' $("#buttonGo").bind("click", function() {';
59 // hide form
60 $html .= ' $("#upload_form_form").css("display", "none");';
62 if ($_SESSION[$SESSION_KEY]["handler"] != "UploadNoplugin") {
64 $html .= PMA_getHtmlForImportWithPlugin($upload_id);
66 } else { // no plugin available
67 $image_tag = '<img src="' . $GLOBALS['pmaThemeImage']
68 . 'ajax_clock_small.gif" width="16" height="16" alt="ajax clock" /> '
69 . PMA_jsFormat(
70 __(
71 'Please be patient, the file is being uploaded. '
72 . 'Details about the upload are not available.'
74 false
75 ) . PMA_Util::showDocu('faq', 'faq2-9');
76 $html .= " $('#upload_form_status_info').html('" . $image_tag . "');";
77 $html .= ' $("#upload_form_status").css("display", "none");';
78 } // else
80 // onclick
81 $html .= ' });';
82 // domready
83 $html .= ' });';
84 $html .= ' //]]>';
85 //with "\n", so that the following lines won't be commented out by //]]>
86 $html .= "\n";
87 $html .= '</script>';
89 return $html;
92 /**
93 * Prints Html For Display Export options
95 * @param String $import_type Import type: server, database, table
96 * @param String $db Selected DB
97 * @param String $table Selected Table
99 * @return string
101 function PMA_getHtmlForImportOptions($import_type, $db, $table)
103 $html = ' <div class="exportoptions" id="header">';
104 $html .= ' <h2>';
105 $html .= PMA_Util::getImage('b_import.png', __('Import'));
107 if ($import_type == 'server') {
108 $html .= __('Importing into the current server');
109 } elseif ($import_type == 'database') {
110 $import_str = sprintf(
111 __('Importing into the database "%s"'),
112 htmlspecialchars($db)
114 $html .= $import_str;
115 } else {
116 $import_str = sprintf(
117 __('Importing into the table "%s"'),
118 htmlspecialchars($table)
120 $html .= $import_str;
122 $html .= ' </h2>';
123 $html .= ' </div>';
125 return $html;
129 * Prints Html For Display Import options : Compressions
131 * @return string
133 function PMA_getHtmlForImportCompressions()
135 global $cfg;
136 $html = '';
137 // zip, gzip and bzip2 encode features
138 $compressions = array();
140 if ($cfg['GZipDump'] && @function_exists('gzopen')) {
141 $compressions[] = 'gzip';
143 if ($cfg['BZipDump'] && @function_exists('bzopen')) {
144 $compressions[] = 'bzip2';
146 if ($cfg['ZipDump'] && @function_exists('zip_open')) {
147 $compressions[] = 'zip';
149 // We don't have show anything about compression, when no supported
150 if ($compressions != array()) {
151 $html .= '<div class="formelementrow" id="compression_info">';
152 $compress_str = sprintf(
153 __('File may be compressed (%s) or uncompressed.'),
154 implode(", ", $compressions)
156 $html .= $compress_str;
157 $html .= '<br />';
158 $html .= __(
159 'A compressed file\'s name must end in <b>.[format].[compression]</b>. '
160 . 'Example: <b>.sql.zip</b>'
162 $html .= '</div>';
165 return $html;
169 * Prints Html For Display Import charset
171 * @return string
173 function PMA_getHtmlForImportCharset()
175 global $cfg;
176 $html = ' <div class="formelementrow" id="charaset_of_file">';
177 // charset of file
178 if ($GLOBALS['PMA_recoding_engine'] != PMA_CHARSET_NONE) {
179 $html .= '<label for="charset_of_file">' . __('Character set of the file:')
180 . '</label>';
181 reset($cfg['AvailableCharsets']);
182 $html .= '<select id="charset_of_file" name="charset_of_file" size="1">';
183 foreach ($cfg['AvailableCharsets'] as $temp_charset) {
184 $html .= '<option value="' . htmlentities($temp_charset) . '"';
185 if ((empty($cfg['Import']['charset']) && $temp_charset == 'utf-8')
186 || $temp_charset == $cfg['Import']['charset']
188 $html .= ' selected="selected"';
190 $html .= '>' . htmlentities($temp_charset) . '</option>';
192 $html .= ' </select><br />';
193 } else {
194 $html .= '<label for="charset_of_file">' . __('Character set of the file:')
195 . '</label>' . "\n";
196 $html .= PMA_generateCharsetDropdownBox(
197 PMA_CSDROPDOWN_CHARSET,
198 'charset_of_file',
199 'charset_of_file',
200 'utf8',
201 false
203 } // end if (recoding)
205 $html .= ' </div>';
207 return $html;
211 * Prints Html For Display Import options : file property
213 * @param int $max_upload_size Max upload size
214 * @param ImportPlugin[] $import_list import list
215 * @param String $local_import_file from upload directory
217 * @return string
219 function PMA_getHtmlForImportOptionsFile(
220 $max_upload_size, $import_list, $local_import_file
222 global $cfg;
223 $html = ' <div class="importoptions">';
224 $html .= ' <h3>' . __('File to import:') . '</h3>';
225 $html .= PMA_getHtmlForImportCompressions();
226 $html .= ' <div class="formelementrow" id="upload_form">';
228 if ($GLOBALS['is_upload'] && !empty($cfg['UploadDir'])) {
229 $html .= ' <ul>';
230 $html .= ' <li>';
231 $html .= ' <input type="radio" name="file_location" '
232 . 'id="radio_import_file" required="required" />';
233 $html .= PMA_Util::getBrowseUploadFileBlock($max_upload_size);
234 $html .= '<br />' . __('You may also drag and drop a file on any page.');
235 $html .= ' </li>';
236 $html .= ' <li>';
237 $html .= ' <input type="radio" name="file_location" '
238 . 'id="radio_local_import_file"';
239 if (! empty($GLOBALS['timeout_passed'])
240 && ! empty($local_import_file)
242 $html .= ' checked="checked"';
244 $html .= ' />';
245 $html .= PMA_Util::getSelectUploadFileBlock($import_list, $cfg['UploadDir']);
246 $html .= ' </li>';
247 $html .= ' </ul>';
249 } elseif ($GLOBALS['is_upload']) {
250 $html .= PMA_Util::getBrowseUploadFileBlock($max_upload_size);
251 $html .= '<br />' . __('You may also drag and drop a file on any page.');
252 } elseif (!$GLOBALS['is_upload']) {
253 $html .= PMA_Message::notice(
254 __('File uploads are not allowed on this server.')
255 )->getDisplay();
256 } elseif (!empty($cfg['UploadDir'])) {
257 $html .= PMA_Util::getSelectUploadFileBlock($import_list, $cfg['UploadDir']);
258 } // end if (web-server upload directory)
260 $html .= ' </div>';
261 $html .= PMA_getHtmlForImportCharset();
262 $html .= ' </div>';
264 return $html;
268 * Prints Html For Display Import options : Partial Import
270 * @param String $timeout_passed timeout passed
271 * @param String $offset timeout offset
273 * @return string
275 function PMA_getHtmlForImportOptionsPartialImport($timeout_passed, $offset)
277 $html = ' <div class="importoptions">';
278 $html .= ' <h3>' . __('Partial import:') . '</h3>';
280 if (isset($timeout_passed) && $timeout_passed) {
281 $html .= '<div class="formelementrow">' . "\n";
282 $html .= '<input type="hidden" name="skip" value="' . $offset . '" />';
283 $html .= sprintf(
285 'Previous import timed out, after resubmitting '
286 . 'will continue from position %d.'
288 $offset
290 $html .= '</div>' . "\n";
293 $html .= ' <div class="formelementrow">';
294 $html .= ' <input type="checkbox" name="allow_interrupt" value="yes"';
295 $html .= ' id="checkbox_allow_interrupt" '
296 . PMA_pluginCheckboxCheck('Import', 'allow_interrupt') . '/>';
297 $html .= ' <label for="checkbox_allow_interrupt">'
298 . __(
299 'Allow the interruption of an import in case the script detects '
300 . 'it is close to the PHP timeout limit. <i>(This might be a good way'
301 . ' to import large files, however it can break transactions.)</i>'
302 ) . '</label><br />';
303 $html .= ' </div>';
305 if (! (isset($timeout_passed) && $timeout_passed)) {
306 $html .= ' <div class="formelementrow">';
307 $html .= ' <label for="text_skip_queries">'
308 . __(
309 'Skip this number of queries (for SQL) starting from the first one:'
311 . '</label>';
312 $html .= ' <input type="number" name="skip_queries" value="'
313 . PMA_pluginGetDefault('Import', 'skip_queries')
314 . '" id="text_skip_queries" min="0" />';
315 $html .= ' </div>';
317 } else {
318 // If timeout has passed,
319 // do not show the Skip dialog to avoid the risk of someone
320 // entering a value here that would interfere with "skip"
321 $html .= ' <input type="hidden" name="skip_queries" value="'
322 . PMA_pluginGetDefault('Import', 'skip_queries')
323 . '" id="text_skip_queries" />';
326 $html .= ' </div>';
328 return $html;
332 * Prints Html For Display Import options : Other
334 * @return string
336 function PMA_getHtmlForImportOptionsOther()
338 $html = ' <div class="importoptions">';
339 $html .= ' <h3>' . __('Other options:') . '</h3>';
340 $html .= ' <div class="formelementrow">';
341 $html .= PMA_Util::getFKCheckbox();
342 $html .= ' </div>';
343 $html .= ' </div>';
345 return $html;
349 * Prints Html For Display Import options : Format
351 * @param ImportPlugin[] $import_list import list
353 * @return string
355 function PMA_getHtmlForImportOptionsFormat($import_list)
357 $html = ' <div class="importoptions">';
358 $html .= ' <h3>' . __('Format:') . '</h3>';
359 $html .= PMA_pluginGetChoice('Import', 'format', $import_list);
360 $html .= ' <div id="import_notification"></div>';
361 $html .= ' </div>';
363 $html .= ' <div class="importoptions" id="format_specific_opts">';
364 $html .= ' <h3>' . __('Format-specific options:') . '</h3>';
365 $html .= ' <p class="no_js_msg" id="scroll_to_options_msg">'
366 . 'Scroll down to fill in the options for the selected format '
367 . 'and ignore the options for other formats.</p>';
368 $html .= PMA_pluginGetOptions('Import', $import_list);
369 $html .= ' </div>';
370 $html .= ' <div class="clearfloat"></div>';
372 // Encoding setting form appended by Y.Kawada
373 if (function_exists('PMA_Kanji_encodingForm')) {
374 $html .= ' <div class="importoptions" id="kanji_encoding">';
375 $html .= ' <h3>' . __('Encoding Conversion:') . '</h3>';
376 $html .= PMA_Kanji_encodingForm();
377 $html .= ' </div>';
380 $html .= "\n";
382 return $html;
386 * Prints Html For Display Import options : submit
388 * @return string
390 function PMA_getHtmlForImportOptionsSubmit()
392 $html = ' <div class="importoptions" id="submit">';
393 $html .= ' <input type="submit" value="' . __('Go') . '" id="buttonGo" />';
394 $html .= ' </div>';
396 return $html;
400 * Prints Html For Display Import
402 * @param int $upload_id The selected upload id
403 * @param String $import_type Import type: server, database, table
404 * @param String $db Selected DB
405 * @param String $table Selected Table
406 * @param int $max_upload_size Max upload size
407 * @param ImportPlugin[] $import_list Import list
408 * @param String $timeout_passed Timeout passed
409 * @param String $offset Timeout offset
410 * @param String $local_import_file from upload directory
412 * @return string
414 function PMA_getHtmlForImport(
415 $upload_id, $import_type, $db, $table,
416 $max_upload_size, $import_list, $timeout_passed, $offset, $local_import_file
418 global $SESSION_KEY;
419 $html = '';
420 $html .= '<iframe id="import_upload_iframe" name="import_upload_iframe" '
421 . 'width="1" height="1" style="display: none;"></iframe>';
422 $html .= '<div id="import_form_status" style="display: none;"></div>';
423 $html .= '<div id="importmain">';
424 $html .= ' <img src="' . $GLOBALS['pmaThemeImage'] . 'ajax_clock_small.gif" '
425 . 'width="16" height="16" alt="ajax clock" style="display: none;" />';
427 $html .= PMA_getHtmlForImportJS($upload_id);
429 $html .= ' <form id="import_file_form" action="import.php" method="post" '
430 . 'enctype="multipart/form-data"';
431 $html .= ' name="import"';
432 if ($_SESSION[$SESSION_KEY]["handler"] != "UploadNoplugin") {
433 $html .= ' target="import_upload_iframe"';
435 $html .= ' class="ajax"';
436 $html .= '>';
437 $html .= ' <input type="hidden" name="';
438 $html .= $_SESSION[$SESSION_KEY]['handler']::getIdKey();
439 $html .= '" value="' . $upload_id . '" />';
441 $html .= PMA_getHtmlForHiddenInputs($import_type, $db, $table);
443 $html .= PMA_getHtmlForImportOptions($import_type, $db, $table);
445 $html .= PMA_getHtmlForImportOptionsFile(
446 $max_upload_size, $import_list, $local_import_file
449 $html .= PMA_getHtmlForImportOptionsPartialImport($timeout_passed, $offset);
451 $html .= PMA_getHtmlForImportOptionsOther();
453 $html .= PMA_getHtmlForImportOptionsFormat($import_list);
455 $html .= PMA_getHtmlForImportOptionsSubmit();
457 $html .= '</form>';
458 $html .= '</div>';
460 return $html;
464 * Prints javascript for upload with plugin, upload process bar
466 * @param int $upload_id The selected upload id
468 * @return string
470 function PMA_getHtmlForImportWithPlugin($upload_id)
472 //some variable for javascript
473 $ajax_url = "import_status.php?id=" . $upload_id . "&"
474 . PMA_URL_getCommon(array('import_status'=>1), 'text');
475 $promot_str = PMA_jsFormat(
477 'The file being uploaded is probably larger than '
478 . 'the maximum allowed size or this is a known bug in webkit '
479 . 'based (Safari, Google Chrome, Arora etc.) browsers.'
481 false
483 $statustext_str = PMA_escapeJsString(__('%s of %s'));
484 $upload_str = PMA_jsFormat(__('Uploading your import file…'), false);
485 $second_str = PMA_jsFormat(__('%s/sec.'), false);
486 $remaining_min = PMA_jsFormat(__('About %MIN min. %SEC sec. remaining.'), false);
487 $remaining_second = PMA_jsFormat(__('About %SEC sec. remaining.'), false);
488 $processed_str = PMA_jsFormat(
489 __('The file is being processed, please be patient.'),
490 false
492 $import_url = PMA_URL_getCommon(array('import_status'=>1), 'text');
494 //start output
495 $html = 'var finished = false; ';
496 $html .= 'var percent = 0.0; ';
497 $html .= 'var total = 0; ';
498 $html .= 'var complete = 0; ';
499 $html .= 'var original_title = '
500 . 'parent && parent.document ? parent.document.title : false; ';
501 $html .= 'var import_start; ';
503 $html .= 'var perform_upload = function () { ';
504 $html .= 'new $.getJSON( ';
505 $html .= ' "' . $ajax_url . '", ';
506 $html .= ' {}, ';
507 $html .= ' function(response) { ';
508 $html .= ' finished = response.finished; ';
509 $html .= ' percent = response.percent; ';
510 $html .= ' total = response.total; ';
511 $html .= ' complete = response.complete; ';
513 $html .= ' if (total==0 && complete==0 && percent==0) { ';
514 $img_tag = '<img src="' . $GLOBALS['pmaThemeImage'] . 'ajax_clock_small.gif"';
515 $html .= ' $("#upload_form_status_info").html(\''
516 . $img_tag . ' width="16" height="16" alt="ajax clock" /> '
517 . $promot_str . '\'); ';
518 $html .= ' $("#upload_form_status").css("display", "none"); ';
519 $html .= ' } else { ';
520 $html .= ' var now = new Date(); ';
521 $html .= ' now = Date.UTC( ';
522 $html .= ' now.getFullYear(), ';
523 $html .= ' now.getMonth(), ';
524 $html .= ' now.getDate(), ';
525 $html .= ' now.getHours(), ';
526 $html .= ' now.getMinutes(), ';
527 $html .= ' now.getSeconds()) ';
528 $html .= ' + now.getMilliseconds() - 1000; ';
529 $html .= ' var statustext = PMA_sprintf(';
530 $html .= ' "' . $statustext_str . '", ';
531 $html .= ' formatBytes( ';
532 $html .= ' complete, 1, PMA_messages.strDecimalSeparator';
533 $html .= ' ), ';
534 $html .= ' formatBytes(';
535 $html .= ' total, 1, PMA_messages.strDecimalSeparator';
536 $html .= ' ) ';
537 $html .= ' ); ';
539 $html .= ' if ($("#importmain").is(":visible")) { ';
540 // show progress UI
541 $html .= ' $("#importmain").hide(); ';
542 $html .= ' $("#import_form_status") ';
543 $html .= ' .html(\'<div class="upload_progress">'
544 . '<div class="upload_progress_bar_outer"><div class="percentage">'
545 . '</div><div id="status" class="upload_progress_bar_inner">'
546 . '<div class="percentage"></div></div></div><div>'
547 . '<img src="' . $GLOBALS['pmaThemeImage']
548 . 'ajax_clock_small.gif" width="16" height="16" alt="ajax clock" /> '
549 . $upload_str . '</div><div id="statustext"></div></div>\') ';
550 $html .= ' .show(); ';
551 $html .= ' import_start = now; ';
552 $html .= ' } ';
553 $html .= ' else if (percent > 9 || complete > 2000000) { ';
554 // calculate estimated time
555 $html .= ' var used_time = now - import_start; ';
556 $html .= ' var seconds = '
557 . 'parseInt(((total - complete) / complete) * used_time / 1000); ';
558 $html .= ' var speed = PMA_sprintf("' . $second_str . '"';
559 $html .= ' , formatBytes(complete / used_time * 1000, 1,'
560 . ' PMA_messages.strDecimalSeparator)); ';
562 $html .= ' var minutes = parseInt(seconds / 60); ';
563 $html .= ' seconds %= 60; ';
564 $html .= ' var estimated_time; ';
565 $html .= ' if (minutes > 0) { ';
566 $html .= ' estimated_time = "' . $remaining_min . '"';
567 $html .= ' .replace("%MIN", minutes)';
568 $html .= ' .replace("%SEC", seconds); ';
569 $html .= ' } ';
570 $html .= ' else { ';
571 $html .= ' estimated_time = "' . $remaining_second . '"';
572 $html .= ' .replace("%SEC", seconds); ';
573 $html .= ' } ';
575 $html .= ' statustext += "<br />" + speed + "<br /><br />" '
576 . '+ estimated_time; ';
577 $html .= ' } ';
579 $html .= ' var percent_str = Math.round(percent) + "%"; ';
580 $html .= ' $("#status").animate({width: percent_str}, 150); ';
581 $html .= ' $(".percentage").text(percent_str); ';
583 // show percent in window title
584 $html .= ' if (original_title !== false) { ';
585 $html .= ' parent.document.title ';
586 $html .= ' = percent_str + " - " + original_title; ';
587 $html .= ' } ';
588 $html .= ' else { ';
589 $html .= ' document.title ';
590 $html .= ' = percent_str + " - " + original_title; ';
591 $html .= ' } ';
592 $html .= ' $("#statustext").html(statustext); ';
593 $html .= ' } ';
595 $html .= ' if (finished == true) { ';
596 $html .= ' if (original_title !== false) { ';
597 $html .= ' parent.document.title = original_title; ';
598 $html .= ' } ';
599 $html .= ' else { ';
600 $html .= ' document.title = original_title; ';
601 $html .= ' } ';
602 $html .= ' $("#importmain").hide(); ';
603 // loads the message, either success or mysql error
604 $html .= ' $("#import_form_status") ';
605 $html .= ' .html(\'<img src="' . $GLOBALS['pmaThemeImage']
606 . 'ajax_clock_small.gif" width="16" height="16" alt="ajax clock" /> '
607 . $processed_str . '\')';
608 $html .= ' .show(); ';
609 $html .= ' $("#import_form_status").load("import_status.php?'
610 . 'message=true&' . $import_url . '"); ';
611 $html .= ' PMA_reloadNavigation(); ';
613 // if finished
614 $html .= ' } ';
615 $html .= ' else { ';
616 $html .= ' setTimeout(perform_upload, 1000); ';
617 $html .= ' } ';
618 $html .= '}); ';
619 $html .= '}; ';
620 $html .= 'setTimeout(perform_upload, 1000); ';
622 return $html;
626 * Gets HTML to display import dialogs
628 * @param String $import_type Import type: server|database|table
629 * @param String $db Selected DB
630 * @param String $table Selected Table
631 * @param int $max_upload_size Max upload size
633 * @return string $html
635 function PMA_getImportDisplay($import_type, $db, $table, $max_upload_size)
637 global $SESSION_KEY;
638 include_once './libraries/file_listing.lib.php';
639 include_once './libraries/plugin_interface.lib.php';
640 // this one generates also some globals
641 include_once './libraries/display_import_ajax.lib.php';
643 /* Scan for plugins */
644 /* @var $import_list ImportPlugin[] */
645 $import_list = PMA_getPlugins(
646 "import",
647 'libraries/plugins/import/',
648 $import_type
651 /* Fail if we didn't find any plugin */
652 if (empty($import_list)) {
653 PMA_Message::error(
655 'Could not load import plugins, please check your installation!'
657 )->display();
658 exit;
661 if (PMA_isValid($_REQUEST['offset'], 'numeric')) {
662 $offset = $_REQUEST['offset'];
664 if (isset($_REQUEST['timeout_passed'])) {
665 $timeout_passed = $_REQUEST['timeout_passed'];
668 $local_import_file = '';
669 if (isset($_REQUEST['local_import_file'])) {
670 $local_import_file = $_REQUEST['local_import_file'];
673 $timeout_passed_str = isset($timeout_passed)? $timeout_passed : null;
674 $offset_str = isset($offset)? $offset : null;
675 return PMA_getHtmlForImport(
676 $upload_id,
677 $import_type,
678 $db,
679 $table,
680 $max_upload_size,
681 $import_list,
682 $timeout_passed_str,
683 $offset_str,
684 $local_import_file