display_import.lib.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * functions for displaying import for: server, database and table
  5. *
  6. * @usedby display_import.inc.php
  7. *
  8. * @package PhpMyAdmin
  9. */
  10. if (! defined('PHPMYADMIN')) {
  11. exit;
  12. }
  13. /**
  14. * Prints Html For Display Import Hidden Input
  15. *
  16. * @param String $import_type Import type: server, database, table
  17. * @param String $db Selected DB
  18. * @param String $table Selected Table
  19. *
  20. * @return string
  21. */
  22. function PMA_getHtmlForHiddenInputs($import_type, $db, $table)
  23. {
  24. $html = '';
  25. if ($import_type == 'server') {
  26. $html .= PMA_URL_getHiddenInputs('', '', 1);
  27. } elseif ($import_type == 'database') {
  28. $html .= PMA_URL_getHiddenInputs($db, '', 1);
  29. } else {
  30. $html .= PMA_URL_getHiddenInputs($db, $table, 1);
  31. }
  32. $html .= ' <input type="hidden" name="import_type" value="'
  33. . $import_type . '" />' . "\n";
  34. return $html;
  35. }
  36. /**
  37. * Prints Html For Import Javascript
  38. *
  39. * @param int $upload_id The selected upload id
  40. *
  41. * @return string
  42. */
  43. function PMA_getHtmlForImportJS($upload_id)
  44. {
  45. global $SESSION_KEY;
  46. $html = '';
  47. $html .= '<script type="text/javascript">';
  48. $html .= ' //<![CDATA[';
  49. //with "\n", so that the following lines won't be commented out by //<![CDATA[
  50. $html .= "\n";
  51. $html .= ' $( function() {';
  52. // add event when user click on "Go" button
  53. $html .= ' $("#buttonGo").bind("click", function() {';
  54. // hide form
  55. $html .= ' $("#upload_form_form").css("display", "none");';
  56. if ($_SESSION[$SESSION_KEY]["handler"] != "UploadNoplugin") {
  57. $html .= PMA_getHtmlForImportWithPlugin($upload_id);
  58. } else { // no plugin available
  59. $image_tag = '<img src="' . $GLOBALS['pmaThemeImage']
  60. . 'ajax_clock_small.gif" width="16" height="16" alt="ajax clock" /> '
  61. . PMA_jsFormat(
  62. __(
  63. 'Please be patient, the file is being uploaded. '
  64. . 'Details about the upload are not available.'
  65. ),
  66. false
  67. ) . PMA_Util::showDocu('faq', 'faq2-9');
  68. $html .= " $('#upload_form_status_info').html('" . $image_tag . "');";
  69. $html .= ' $("#upload_form_status").css("display", "none");';
  70. } // else
  71. // onclick
  72. $html .= ' });';
  73. // domready
  74. $html .= ' });';
  75. $html .= ' //]]>';
  76. //with "\n", so that the following lines won't be commented out by //]]>
  77. $html .= "\n";
  78. $html .= '</script>';
  79. return $html;
  80. }
  81. /**
  82. * Prints Html For Display Export options
  83. *
  84. * @param String $import_type Import type: server, database, table
  85. * @param String $db Selected DB
  86. * @param String $table Selected Table
  87. *
  88. * @return string
  89. */
  90. function PMA_getHtmlForExportOptions($import_type, $db, $table)
  91. {
  92. $html = ' <div class="exportoptions" id="header">';
  93. $html .= ' <h2>';
  94. $html .= PMA_Util::getImage('b_import.png', __('Import'));
  95. if ($import_type == 'server') {
  96. $html .= __('Importing into the current server');
  97. } elseif ($import_type == 'database') {
  98. $import_str = sprintf(
  99. __('Importing into the database "%s"'),
  100. htmlspecialchars($db)
  101. );
  102. $html .= $import_str;
  103. } else {
  104. $import_str = sprintf(
  105. __('Importing into the table "%s"'),
  106. htmlspecialchars($table)
  107. );
  108. $html .= $import_str;
  109. }
  110. $html .= ' </h2>';
  111. $html .= ' </div>';
  112. return $html;
  113. }
  114. /**
  115. * Prints Html For Display Import options : Compressions
  116. *
  117. * @return string
  118. */
  119. function PMA_getHtmlForImportCompressions()
  120. {
  121. global $cfg;
  122. $html = '';
  123. // zip, gzip and bzip2 encode features
  124. $compressions = array();
  125. if ($cfg['GZipDump'] && @function_exists('gzopen')) {
  126. $compressions[] = 'gzip';
  127. }
  128. if ($cfg['BZipDump'] && @function_exists('bzopen')) {
  129. $compressions[] = 'bzip2';
  130. }
  131. if ($cfg['ZipDump'] && @function_exists('zip_open')) {
  132. $compressions[] = 'zip';
  133. }
  134. // We don't have show anything about compression, when no supported
  135. if ($compressions != array()) {
  136. $html .= '<div class="formelementrow" id="compression_info">';
  137. $compress_str = sprintf(
  138. __('File may be compressed (%s) or uncompressed.'),
  139. implode(", ", $compressions)
  140. );
  141. $html .= $compress_str;
  142. $html .= '<br />';
  143. $html .= __(
  144. 'A compressed file\'s name must end in <b>.[format].[compression]</b>. '
  145. . 'Example: <b>.sql.zip</b>'
  146. );
  147. $html .= '</div>';
  148. }
  149. return $html;
  150. }
  151. /**
  152. * Prints Html For Display Import charset
  153. *
  154. * @return string
  155. */
  156. function PMA_getHtmlForImportCharset()
  157. {
  158. global $cfg;
  159. $html = ' <div class="formelementrow" id="charaset_of_file">';
  160. // charset of file
  161. if ($GLOBALS['PMA_recoding_engine'] != PMA_CHARSET_NONE) {
  162. $html .= '<label for="charset_of_file">' . __('Character set of the file:')
  163. . '</label>';
  164. reset($cfg['AvailableCharsets']);
  165. $html .= '<select id="charset_of_file" name="charset_of_file" size="1">';
  166. foreach ($cfg['AvailableCharsets'] as $temp_charset) {
  167. $html .= '<option value="' . htmlentities($temp_charset) . '"';
  168. if ((empty($cfg['Import']['charset']) && $temp_charset == 'utf-8')
  169. || $temp_charset == $cfg['Import']['charset']
  170. ) {
  171. $html .= ' selected="selected"';
  172. }
  173. $html .= '>' . htmlentities($temp_charset) . '</option>';
  174. }
  175. $html .= ' </select><br />';
  176. } else {
  177. $html .= '<label for="charset_of_file">' . __('Character set of the file:')
  178. . '</label>' . "\n";
  179. $html .= PMA_generateCharsetDropdownBox(
  180. PMA_CSDROPDOWN_CHARSET,
  181. 'charset_of_file',
  182. 'charset_of_file',
  183. 'utf8',
  184. false
  185. );
  186. } // end if (recoding)
  187. $html .= ' </div>';
  188. return $html;
  189. }
  190. /**
  191. * Prints Html For Display Import options : file property
  192. *
  193. * @param int $max_upload_size Max upload size
  194. * @param Array $import_list import list
  195. *
  196. * @return string
  197. */
  198. function PMA_getHtmlForImportOptionsFile($max_upload_size, $import_list)
  199. {
  200. global $cfg;
  201. $html = ' <div class="importoptions">';
  202. $html .= ' <h3>' . __('File to Import:') . '</h3>';
  203. $html .= PMA_getHtmlForImportCompressions();
  204. $html .= ' <div class="formelementrow" id="upload_form">';
  205. if ($GLOBALS['is_upload'] && !empty($cfg['UploadDir'])) {
  206. $html .= ' <ul>';
  207. $html .= ' <li>';
  208. $html .= ' <input type="radio" name="file_location" '
  209. . 'id="radio_import_file" required="required" />';
  210. $html .= PMA_Util::getBrowseUploadFileBlock($max_upload_size);
  211. $html .= ' </li>';
  212. $html .= ' <li>';
  213. $html .= ' <input type="radio" name="file_location" '
  214. . 'id="radio_local_import_file" />';
  215. $html .= PMA_Util::getSelectUploadFileBlock($import_list, $cfg['UploadDir']);
  216. $html .= ' </li>';
  217. $html .= ' </ul>';
  218. } elseif ($GLOBALS['is_upload']) {
  219. $html .= PMA_Util::getBrowseUploadFileBlock($max_upload_size);
  220. } elseif (!$GLOBALS['is_upload']) {
  221. $html .= PMA_Message::notice(
  222. __('File uploads are not allowed on this server.')
  223. )->getDisplay();
  224. } elseif (!empty($cfg['UploadDir'])) {
  225. $html .= PMA_Util::getSelectUploadFileBlock($import_list, $cfg['UploadDir']);
  226. } // end if (web-server upload directory)
  227. $html .= ' </div>';
  228. $html .= PMA_getHtmlForImportCharset();
  229. $html .= ' </div>';
  230. return $html;
  231. }
  232. /**
  233. * Prints Html For Display Import options : Partial Import
  234. *
  235. * @param String $timeout_passed timeout passed
  236. * @param String $offset timeout offset
  237. *
  238. * @return string
  239. */
  240. function PMA_getHtmlForImportOptionsPartialImport($timeout_passed, $offset)
  241. {
  242. $html = ' <div class="importoptions">';
  243. $html .= ' <h3>' . __('Partial Import:') . '</h3>';
  244. if (isset($timeout_passed) && $timeout_passed) {
  245. $html .= '<div class="formelementrow">' . "\n";
  246. $html .= '<input type="hidden" name="skip" value="' . $offset . '" />';
  247. $html .= sprintf(
  248. __(
  249. 'Previous import timed out, after resubmitting '
  250. . 'will continue from position %d.'
  251. ),
  252. $offset
  253. );
  254. $html .= '</div>' . "\n";
  255. }
  256. $html .= ' <div class="formelementrow">';
  257. $html .= ' <input type="checkbox" name="allow_interrupt" value="yes"';
  258. $html .= ' id="checkbox_allow_interrupt" '
  259. . PMA_pluginCheckboxCheck('Import', 'allow_interrupt') . '/>';
  260. $html .= ' <label for="checkbox_allow_interrupt">'
  261. . __(
  262. 'Allow the interruption of an import in case the script detects '
  263. . 'it is close to the PHP timeout limit. <i>(This might be a good way'
  264. . ' to import large files, however it can break transactions.)</i>'
  265. ) . '</label><br />';
  266. $html .= ' </div>';
  267. if (! (isset($timeout_passed) && $timeout_passed)) {
  268. $html .= ' <div class="formelementrow">';
  269. $html .= ' <label for="text_skip_queries">'
  270. . __(
  271. 'Skip this number of queries (for SQL) or lines (for other '
  272. . 'formats), starting from the first one:'
  273. )
  274. . '</label>';
  275. $html .= ' <input type="number" name="skip_queries" value="'
  276. . PMA_pluginGetDefault('Import', 'skip_queries')
  277. . '" id="text_skip_queries" min="0" />';
  278. $html .= ' </div>';
  279. } else {
  280. // If timeout has passed,
  281. // do not show the Skip dialog to avoid the risk of someone
  282. // entering a value here that would interfere with "skip"
  283. $html .= ' <input type="hidden" name="skip_queries" value="'
  284. . PMA_pluginGetDefault('Import', 'skip_queries')
  285. . '" id="text_skip_queries" />';
  286. }
  287. $html .= ' </div>';
  288. return $html;
  289. }
  290. /**
  291. * Prints Html For Display Import options : Format
  292. *
  293. * @param Array $import_list import list
  294. *
  295. * @return string
  296. */
  297. function PMA_getHtmlForImportOptionsFormat($import_list)
  298. {
  299. $html = ' <div class="importoptions">';
  300. $html .= ' <h3>' . __('Format:') . '</h3>';
  301. $html .= PMA_pluginGetChoice('Import', 'format', $import_list);
  302. $html .= ' <div id="import_notification"></div>';
  303. $html .= ' </div>';
  304. $html .= ' <div class="importoptions" id="format_specific_opts">';
  305. $html .= ' <h3>' . __('Format-Specific Options:') . '</h3>';
  306. $html .= ' <p class="no_js_msg" id="scroll_to_options_msg">'
  307. . 'Scroll down to fill in the options for the selected format '
  308. . 'and ignore the options for other formats.</p>';
  309. $html .= PMA_pluginGetOptions('Import', $import_list);
  310. $html .= ' </div>';
  311. $html .= ' <div class="clearfloat"></div>';
  312. // Encoding setting form appended by Y.Kawada
  313. if (function_exists('PMA_Kanji_encodingForm')) {
  314. $html .= ' <div class="importoptions" id="kanji_encoding">';
  315. $html .= ' <h3>' . __('Encoding Conversion:') . '</h3>';
  316. $html .= PMA_Kanji_encodingForm();
  317. $html .= ' </div>';
  318. }
  319. $html .= "\n";
  320. return $html;
  321. }
  322. /**
  323. * Prints Html For Display Import options : submit
  324. *
  325. * @return string
  326. */
  327. function PMA_getHtmlForImportOptionsSubmit()
  328. {
  329. $html = ' <div class="importoptions" id="submit">';
  330. $html .= ' <input type="submit" value="' . __('Go') . '" id="buttonGo" />';
  331. $html .= ' </div>';
  332. return $html;
  333. }
  334. /**
  335. * Prints Html For Display Import
  336. *
  337. * @param int $upload_id The selected upload id
  338. * @param String $import_type Import type: server, database, table
  339. * @param String $db Selected DB
  340. * @param String $table Selected Table
  341. * @param int $max_upload_size Max upload size
  342. * @param Array $import_list Import list
  343. * @param String $timeout_passed Timeout passed
  344. * @param String $offset Timeout offset
  345. *
  346. * @return string
  347. */
  348. function PMA_getHtmlForImport(
  349. $upload_id, $import_type, $db, $table,
  350. $max_upload_size, $import_list, $timeout_passed, $offset
  351. ) {
  352. global $SESSION_KEY;
  353. $html = '';
  354. $html .= '<iframe id="import_upload_iframe" name="import_upload_iframe" '
  355. . 'width="1" height="1" style="display: none;"></iframe>';
  356. $html .= '<div id="import_form_status" style="display: none;"></div>';
  357. $html .= '<div id="importmain">';
  358. $html .= ' <img src="' . $GLOBALS['pmaThemeImage'] . 'ajax_clock_small.gif" '
  359. . 'width="16" height="16" alt="ajax clock" style="display: none;" />';
  360. $html .= PMA_getHtmlForImportJS($upload_id);
  361. $html .= ' <form id="import_file_form" action="import.php" method="post" '
  362. . 'enctype="multipart/form-data"';
  363. $html .= ' name="import"';
  364. if ($_SESSION[$SESSION_KEY]["handler"] != "UploadNoplugin") {
  365. $html .= ' target="import_upload_iframe"';
  366. }
  367. $html .= ' class="ajax"';
  368. $html .= '>';
  369. $html .= ' <input type="hidden" name="';
  370. $html .= $_SESSION[$SESSION_KEY]['handler']::getIdKey();
  371. $html .= '" value="' . $upload_id . '" />';
  372. $html .= PMA_getHtmlForHiddenInputs($import_type, $db, $table);
  373. $html .= PMA_getHtmlForExportOptions($import_type, $db, $table);
  374. $html .= PMA_getHtmlForImportOptionsFile($max_upload_size, $import_list);
  375. $html .= PMA_getHtmlForImportOptionsPartialImport($timeout_passed, $offset);
  376. $html .= PMA_getHtmlForImportOptionsFormat($import_list);
  377. $html .= PMA_getHtmlForImportOptionsSubmit();
  378. $html .= '</form>';
  379. $html .= '</div>';
  380. return $html;
  381. }
  382. /**
  383. * Prints javascript for upload with plugin, upload process bar
  384. *
  385. * @param int $upload_id The selected upload id
  386. *
  387. * @return string
  388. */
  389. function PMA_getHtmlForImportWithPlugin($upload_id)
  390. {
  391. //some variable for javasript
  392. $ajax_url = "import_status.php?id=" . $upload_id . "&"
  393. . PMA_URL_getCommon(array('import_status'=>1), '&');
  394. $promot_str = PMA_jsFormat(
  395. __(
  396. 'The file being uploaded is probably larger than '
  397. . 'the maximum allowed size or this is a known bug in webkit '
  398. . 'based (Safari, Google Chrome, Arora etc.) browsers.'
  399. ),
  400. false
  401. );
  402. $statustext_str = PMA_escapeJsString(__('%s of %s'));
  403. $upload_str = PMA_jsFormat(__('Uploading your import file…'), false);
  404. $second_str = PMA_jsFormat(__('%s/sec.'), false);
  405. $remaining_min = PMA_jsFormat(__('About %MIN min. %SEC sec. remaining.'), false);
  406. $remaining_second = PMA_jsFormat(__('About %SEC sec. remaining.'), false);
  407. $processed_str = PMA_jsFormat(
  408. __('The file is being processed, please be patient.'),
  409. false
  410. );
  411. $import_url = PMA_URL_getCommon(array('import_status'=>1), '&');
  412. //start output
  413. $html = 'var finished = false; ';
  414. $html .= 'var percent = 0.0; ';
  415. $html .= 'var total = 0; ';
  416. $html .= 'var complete = 0; ';
  417. $html .= 'var original_title = '
  418. . 'parent && parent.document ? parent.document.title : false; ';
  419. $html .= 'var import_start; ';
  420. $html .= 'var perform_upload = function () { ';
  421. $html .= 'new $.getJSON( ';
  422. $html .= ' "' . $ajax_url . '", ';
  423. $html .= ' {}, ';
  424. $html .= ' function(response) { ';
  425. $html .= ' finished = response.finished; ';
  426. $html .= ' percent = response.percent; ';
  427. $html .= ' total = response.total; ';
  428. $html .= ' complete = response.complete; ';
  429. $html .= ' if (total==0 && complete==0 && percent==0) { ';
  430. $img_tag = '<img src="' . $GLOBALS['pmaThemeImage'] . 'ajax_clock_small.gif"';
  431. $html .= ' $("#upload_form_status_info").html(\''
  432. . $img_tag . ' width="16" height="16" alt="ajax clock" /> '
  433. . $promot_str . '\'); ';
  434. $html .= ' $("#upload_form_status").css("display", "none"); ';
  435. $html .= ' } else { ';
  436. $html .= ' var now = new Date(); ';
  437. $html .= ' now = Date.UTC( ';
  438. $html .= ' now.getFullYear(), now.getMonth(), now.getDate(), ';
  439. $html .= ' now.getHours(), now.getMinutes(), now.getSeconds()) ';
  440. $html .= ' + now.getMilliseconds() - 1000; ';
  441. $html .= ' var statustext = $.sprintf("' . $statustext_str . '", ';
  442. $html .= ' formatBytes(complete, 1, PMA_messages.strDecimalSeparator), ';
  443. $html .= ' formatBytes(total, 1, PMA_messages.strDecimalSeparator) ';
  444. $html .= ' ); ';
  445. $html .= ' if ($("#importmain").is(":visible")) { ';
  446. // show progress UI
  447. $html .= ' $("#importmain").hide(); ';
  448. $html .= ' $("#import_form_status") ';
  449. $html .= ' .html(\'<div class="upload_progress">'
  450. . '<div class="upload_progress_bar_outer"><div class="percentage">'
  451. . '</div><div id="status" class="upload_progress_bar_inner">'
  452. . '<div class="percentage"></div></div></div><div>'
  453. . '<img src="' . $GLOBALS['pmaThemeImage']
  454. . 'ajax_clock_small.gif" width="16" height="16" alt="ajax clock" /> '
  455. . $upload_str . '</div><div id="statustext"></div></div>\') ';
  456. $html .= ' .show(); ';
  457. $html .= ' import_start = now; ';
  458. $html .= ' } ';
  459. $html .= ' else if (percent > 9 || complete > 2000000) { ';
  460. // calculate estimated time
  461. $html .= ' var used_time = now - import_start; ';
  462. $html .= ' var seconds = '
  463. . 'parseInt(((total - complete) / complete) * used_time / 1000); ';
  464. $html .= ' var speed = $.sprintf("' . $second_str . '"';
  465. $html .= ' , formatBytes(complete / used_time * 1000, 1,'
  466. . ' PMA_messages.strDecimalSeparator)); ';
  467. $html .= ' var minutes = parseInt(seconds / 60); ';
  468. $html .= ' seconds %= 60; ';
  469. $html .= ' var estimated_time; ';
  470. $html .= ' if (minutes > 0) { ';
  471. $html .= ' estimated_time = "' . $remaining_min . '"';
  472. $html .= ' .replace("%MIN", minutes).replace("%SEC", seconds); ';
  473. $html .= ' } ';
  474. $html .= ' else { ';
  475. $html .= ' estimated_time = "' . $remaining_second . '"';
  476. $html .= ' .replace("%SEC", seconds); ';
  477. $html .= ' } ';
  478. $html .= ' statustext += "<br />" + speed + "<br /><br />" '
  479. . '+ estimated_time; ';
  480. $html .= ' } ';
  481. $html .= ' var percent_str = Math.round(percent) + "%"; ';
  482. $html .= ' $("#status").animate({width: percent_str}, 150); ';
  483. $html .= ' $(".percentage").text(percent_str); ';
  484. // show percent in window title
  485. $html .= ' if (original_title !== false) { ';
  486. $html .= ' parent.document.title = percent_str + " - " + original_title; ';
  487. $html .= ' } ';
  488. $html .= ' else { ';
  489. $html .= ' document.title = percent_str + " - " + original_title; ';
  490. $html .= ' } ';
  491. $html .= ' $("#statustext").html(statustext); ';
  492. $html .= ' } ';
  493. $html .= ' if (finished == true) { ';
  494. $html .= ' if (original_title !== false) { ';
  495. $html .= ' parent.document.title = original_title; ';
  496. $html .= ' } ';
  497. $html .= ' else { ';
  498. $html .= ' document.title = original_title; ';
  499. $html .= ' } ';
  500. $html .= ' $("#importmain").hide(); ';
  501. // loads the message, either success or mysql error
  502. $html .= ' $("#import_form_status") ';
  503. $html .= ' .html(\'<img src="' . $GLOBALS['pmaThemeImage']
  504. . 'ajax_clock_small.gif" width="16" height="16" alt="ajax clock" /> '
  505. . $processed_str . '\')';
  506. $html .= ' .show(); ';
  507. $html .= ' $("#import_form_status").load("import_status.php?'
  508. . 'message=true&' . $import_url . '"); ';
  509. $html .= ' PMA_reloadNavigation(); ';
  510. // if finished
  511. $html .= ' } ';
  512. $html .= ' else { ';
  513. $html .= ' setTimeout(perform_upload, 1000); ';
  514. $html .= ' } ';
  515. $html .= '}); ';
  516. $html .= '}; ';
  517. $html .= 'setTimeout(perform_upload, 1000); ';
  518. return $html;
  519. }
  520. ?>