export.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Main export handling code
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. /**
  9. * If we are sending the export file (as opposed to just displaying it
  10. * as text), we have to bypass the usual PMA_Response mechanism
  11. */
  12. if ($_POST['output_format'] == 'sendit') {
  13. define('PMA_BYPASS_GET_INSTANCE', 1);
  14. }
  15. /**
  16. * Get the variables sent or posted to this script and a core script
  17. */
  18. require_once 'libraries/common.inc.php';
  19. require_once 'libraries/zip.lib.php';
  20. require_once 'libraries/plugin_interface.lib.php';
  21. /**
  22. * Sets globals from $_POST
  23. *
  24. * - Please keep the parameters in order of their appearance in the form
  25. * - Some of these parameters are not used, as the code below directly
  26. * verifies from the superglobal $_POST or $_REQUEST
  27. */
  28. $post_params = array(
  29. 'db',
  30. 'table',
  31. 'single_table',
  32. 'export_type',
  33. 'export_method',
  34. 'quick_or_custom',
  35. 'db_select',
  36. 'table_select',
  37. 'limit_to',
  38. 'limit_from',
  39. 'allrows',
  40. 'output_format',
  41. 'filename_template',
  42. 'remember_template',
  43. 'charset_of_file',
  44. 'compression',
  45. 'what',
  46. 'htmlword_structure_or_data',
  47. 'htmlword_null',
  48. 'htmlword_columns',
  49. 'mediawiki_structure_or_data',
  50. 'mediawiki_caption',
  51. 'pdf_report_title',
  52. 'pdf_structure_or_data',
  53. 'odt_structure_or_data',
  54. 'odt_relation',
  55. 'odt_comments',
  56. 'odt_mime',
  57. 'odt_columns',
  58. 'odt_null',
  59. 'codegen_structure_or_data',
  60. 'codegen_format',
  61. 'excel_null',
  62. 'excel_removeCRLF',
  63. 'excel_columns',
  64. 'excel_edition',
  65. 'excel_structure_or_data',
  66. 'yaml_structure_or_data',
  67. 'ods_null',
  68. 'ods_structure_or_data',
  69. 'ods_columns',
  70. 'json_structure_or_data',
  71. 'xml_structure_or_data',
  72. 'xml_export_functions',
  73. 'xml_export_procedures',
  74. 'xml_export_tables',
  75. 'xml_export_triggers',
  76. 'xml_export_views',
  77. 'xml_export_contents',
  78. 'texytext_structure_or_data',
  79. 'texytext_columns',
  80. 'texytext_null',
  81. 'phparray_structure_or_data',
  82. 'sql_include_comments',
  83. 'sql_header_comment',
  84. 'sql_dates',
  85. 'sql_relation',
  86. 'sql_mime',
  87. 'sql_use_transaction',
  88. 'sql_disable_fk',
  89. 'sql_compatibility',
  90. 'sql_structure_or_data',
  91. 'sql_create_database',
  92. 'sql_drop_table',
  93. 'sql_procedure_function',
  94. 'sql_create_table_statements',
  95. 'sql_if_not_exists',
  96. 'sql_auto_increment',
  97. 'sql_backquotes',
  98. 'sql_truncate',
  99. 'sql_delayed',
  100. 'sql_ignore',
  101. 'sql_type',
  102. 'sql_insert_syntax',
  103. 'sql_max_query_size',
  104. 'sql_hex_for_blob',
  105. 'sql_utc_time',
  106. 'csv_separator',
  107. 'csv_enclosed',
  108. 'csv_escaped',
  109. 'csv_terminated',
  110. 'csv_null',
  111. 'csv_removeCRLF',
  112. 'csv_columns',
  113. 'csv_structure_or_data',
  114. // csv_replace should have been here but we use it directly from $_POST
  115. 'latex_caption',
  116. 'latex_structure_or_data',
  117. 'latex_structure_caption',
  118. 'latex_structure_continued_caption',
  119. 'latex_structure_label',
  120. 'latex_relation',
  121. 'latex_comments',
  122. 'latex_mime',
  123. 'latex_columns',
  124. 'latex_data_caption',
  125. 'latex_data_continued_caption',
  126. 'latex_data_label',
  127. 'latex_null'
  128. );
  129. foreach ($post_params as $one_post_param) {
  130. if (isset($_POST[$one_post_param])) {
  131. $GLOBALS[$one_post_param] = $_POST[$one_post_param];
  132. }
  133. }
  134. // sanitize this parameter which will be used below in a file inclusion
  135. $what = PMA_securePath($what);
  136. PMA_Util::checkParameters(array('what', 'export_type'));
  137. // export class instance, not array of properties, as before
  138. $export_plugin = PMA_getPlugin(
  139. "export",
  140. $what,
  141. 'libraries/plugins/export/',
  142. array(
  143. 'export_type' => $export_type,
  144. 'single_table' => isset($single_table)
  145. )
  146. );
  147. // Backward compatbility
  148. $type = $what;
  149. // Check export type
  150. if (! isset($export_plugin)) {
  151. PMA_fatalError(__('Bad type!'));
  152. }
  153. /**
  154. * valid compression methods
  155. */
  156. $compression_methods = array(
  157. 'zip',
  158. 'gzip',
  159. 'bzip2',
  160. );
  161. /**
  162. * init and variable checking
  163. */
  164. $compression = false;
  165. $onserver = false;
  166. $save_on_server = false;
  167. $buffer_needed = false;
  168. // Is it a quick or custom export?
  169. if ($_REQUEST['quick_or_custom'] == 'quick') {
  170. $quick_export = true;
  171. } else {
  172. $quick_export = false;
  173. }
  174. if ($_REQUEST['output_format'] == 'astext') {
  175. $asfile = false;
  176. } else {
  177. $asfile = true;
  178. if (in_array($_REQUEST['compression'], $compression_methods)) {
  179. $compression = $_REQUEST['compression'];
  180. $buffer_needed = true;
  181. }
  182. if (($quick_export && ! empty($_REQUEST['quick_export_onserver']))
  183. || (! $quick_export && ! empty($_REQUEST['onserver']))
  184. ) {
  185. if ($quick_export) {
  186. $onserver = $_REQUEST['quick_export_onserver'];
  187. } else {
  188. $onserver = $_REQUEST['onserver'];
  189. }
  190. // Will we save dump on server?
  191. $save_on_server = ! empty($cfg['SaveDir']) && $onserver;
  192. }
  193. }
  194. // Does export require to be into file?
  195. if ($export_plugin->getProperties()->getForceFile() != null && ! $asfile) {
  196. $message = PMA_Message::error(
  197. __('Selected export type has to be saved in file!')
  198. );
  199. if ($export_type == 'server') {
  200. $active_page = 'server_export.php';
  201. include 'server_export.php';
  202. } elseif ($export_type == 'database') {
  203. $active_page = 'db_export.php';
  204. include 'db_export.php';
  205. } else {
  206. $active_page = 'tbl_export.php';
  207. include 'tbl_export.php';
  208. }
  209. exit();
  210. }
  211. // Generate error url and check for needed variables
  212. if ($export_type == 'server') {
  213. $err_url = 'server_export.php?' . PMA_generate_common_url();
  214. } elseif ($export_type == 'database' && strlen($db)) {
  215. $err_url = 'db_export.php?' . PMA_generate_common_url($db);
  216. // Check if we have something to export
  217. if (isset($table_select)) {
  218. $tables = $table_select;
  219. } else {
  220. $tables = array();
  221. }
  222. } elseif ($export_type == 'table' && strlen($db) && strlen($table)) {
  223. $err_url = 'tbl_export.php?' . PMA_generate_common_url($db, $table);
  224. } else {
  225. PMA_fatalError(__('Bad parameters!'));
  226. }
  227. /**
  228. * Increase time limit for script execution and initializes some variables
  229. */
  230. @set_time_limit($cfg['ExecTimeLimit']);
  231. if (! empty($cfg['MemoryLimit'])) {
  232. @ini_set('memory_limit', $cfg['MemoryLimit']);
  233. }
  234. // Start with empty buffer
  235. $dump_buffer = '';
  236. $dump_buffer_len = 0;
  237. // We send fake headers to avoid browser timeout when buffering
  238. $time_start = time();
  239. /**
  240. * Detect ob_gzhandler
  241. *
  242. * @return bool
  243. */
  244. function PMA_isGzHandlerEnabled()
  245. {
  246. return in_array('ob_gzhandler', ob_list_handlers());
  247. }
  248. /**
  249. * Detect whether gzencode is needed; it might not be needed if
  250. * the server is already compressing by itself
  251. *
  252. * @return bool Whether gzencode is needed
  253. */
  254. function PMA_gzencodeNeeded()
  255. {
  256. if (@function_exists('gzencode')
  257. && ! @ini_get('zlib.output_compression')
  258. && ! PMA_isGzHandlerEnabled()
  259. ) {
  260. return true;
  261. } else {
  262. return false;
  263. }
  264. }
  265. /**
  266. * Output handler for all exports, if needed buffering, it stores data into
  267. * $dump_buffer, otherwise it prints thems out.
  268. *
  269. * @param string $line the insert statement
  270. *
  271. * @return bool Whether output succeeded
  272. */
  273. function PMA_exportOutputHandler($line)
  274. {
  275. global $time_start, $dump_buffer, $dump_buffer_len, $save_filename;
  276. // Kanji encoding convert feature
  277. if ($GLOBALS['output_kanji_conversion']) {
  278. $line = PMA_kanji_str_conv(
  279. $line,
  280. $GLOBALS['knjenc'],
  281. isset($GLOBALS['xkana']) ? $GLOBALS['xkana'] : ''
  282. );
  283. }
  284. // If we have to buffer data, we will perform everything at once at the end
  285. if ($GLOBALS['buffer_needed']) {
  286. $dump_buffer .= $line;
  287. if ($GLOBALS['onfly_compression']) {
  288. $dump_buffer_len += strlen($line);
  289. if ($dump_buffer_len > $GLOBALS['memory_limit']) {
  290. if ($GLOBALS['output_charset_conversion']) {
  291. $dump_buffer = PMA_convert_string(
  292. 'utf-8',
  293. $GLOBALS['charset_of_file'],
  294. $dump_buffer
  295. );
  296. }
  297. // as bzipped
  298. if ($GLOBALS['compression'] == 'bzip2'
  299. && @function_exists('bzcompress')
  300. ) {
  301. $dump_buffer = bzcompress($dump_buffer);
  302. } elseif ($GLOBALS['compression'] == 'gzip'
  303. && PMA_gzencodeNeeded()
  304. ) {
  305. // as a gzipped file
  306. // without the optional parameter level because it bugs
  307. $dump_buffer = gzencode($dump_buffer);
  308. }
  309. if ($GLOBALS['save_on_server']) {
  310. $write_result = @fwrite($GLOBALS['file_handle'], $dump_buffer);
  311. if (! $write_result || ($write_result != strlen($dump_buffer))) {
  312. $GLOBALS['message'] = PMA_Message::error(
  313. __('Insufficient space to save the file %s.')
  314. );
  315. $GLOBALS['message']->addParam($save_filename);
  316. return false;
  317. }
  318. } else {
  319. echo $dump_buffer;
  320. }
  321. $dump_buffer = '';
  322. $dump_buffer_len = 0;
  323. }
  324. } else {
  325. $time_now = time();
  326. if ($time_start >= $time_now + 30) {
  327. $time_start = $time_now;
  328. header('X-pmaPing: Pong');
  329. } // end if
  330. }
  331. } else {
  332. if ($GLOBALS['asfile']) {
  333. if ($GLOBALS['output_charset_conversion']) {
  334. $line = PMA_convert_string(
  335. 'utf-8',
  336. $GLOBALS['charset_of_file'],
  337. $line
  338. );
  339. }
  340. if ($GLOBALS['save_on_server'] && strlen($line) > 0) {
  341. $write_result = @fwrite($GLOBALS['file_handle'], $line);
  342. if (! $write_result || ($write_result != strlen($line))) {
  343. $GLOBALS['message'] = PMA_Message::error(
  344. __('Insufficient space to save the file %s.')
  345. );
  346. $GLOBALS['message']->addParam($save_filename);
  347. return false;
  348. }
  349. $time_now = time();
  350. if ($time_start >= $time_now + 30) {
  351. $time_start = $time_now;
  352. header('X-pmaPing: Pong');
  353. } // end if
  354. } else {
  355. // We export as file - output normally
  356. echo $line;
  357. }
  358. } else {
  359. // We export as html - replace special chars
  360. echo htmlspecialchars($line);
  361. }
  362. }
  363. return true;
  364. } // end of the 'PMA_exportOutputHandler()' function
  365. // Defines the default <CR><LF> format.
  366. // For SQL always use \n as MySQL wants this on all platforms.
  367. if ($what == 'sql') {
  368. $crlf = "\n";
  369. } else {
  370. $crlf = PMA_Util::whichCrlf();
  371. }
  372. $output_kanji_conversion = function_exists('PMA_kanji_str_conv') && $type != 'xls';
  373. // Do we need to convert charset?
  374. $output_charset_conversion = $asfile
  375. && $GLOBALS['PMA_recoding_engine'] != PMA_CHARSET_NONE
  376. && isset($charset_of_file) && $charset_of_file != 'utf-8'
  377. && $type != 'xls';
  378. // Use on the fly compression?
  379. $onfly_compression = $GLOBALS['cfg']['CompressOnFly']
  380. && ($compression == 'gzip' || $compression == 'bzip2');
  381. if ($onfly_compression) {
  382. $memory_limit = trim(@ini_get('memory_limit'));
  383. // 2 MB as default
  384. if (empty($memory_limit)) {
  385. $memory_limit = 2 * 1024 * 1024;
  386. }
  387. if (strtolower(substr($memory_limit, -1)) == 'm') {
  388. $memory_limit = (int)substr($memory_limit, 0, -1) * 1024 * 1024;
  389. } elseif (strtolower(substr($memory_limit, -1)) == 'k') {
  390. $memory_limit = (int)substr($memory_limit, 0, -1) * 1024;
  391. } elseif (strtolower(substr($memory_limit, -1)) == 'g') {
  392. $memory_limit = (int)substr($memory_limit, 0, -1) * 1024 * 1024 * 1024;
  393. } else {
  394. $memory_limit = (int)$memory_limit;
  395. }
  396. // Some of memory is needed for other thins and as treshold.
  397. // Nijel: During export I had allocated (see memory_get_usage function)
  398. // approx 1.2MB so this comes from that.
  399. if ($memory_limit > 1500000) {
  400. $memory_limit -= 1500000;
  401. }
  402. // Some memory is needed for compression, assume 1/3
  403. $memory_limit /= 8;
  404. }
  405. // Generate filename and mime type if needed
  406. if ($asfile) {
  407. $pma_uri_parts = parse_url($cfg['PmaAbsoluteUri']);
  408. if ($export_type == 'server') {
  409. if (isset($remember_template)) {
  410. $GLOBALS['PMA_Config']->setUserValue(
  411. 'pma_server_filename_template',
  412. 'Export/file_template_server',
  413. $filename_template
  414. );
  415. }
  416. } elseif ($export_type == 'database') {
  417. if (isset($remember_template)) {
  418. $GLOBALS['PMA_Config']->setUserValue(
  419. 'pma_db_filename_template',
  420. 'Export/file_template_database',
  421. $filename_template
  422. );
  423. }
  424. } else {
  425. if (isset($remember_template)) {
  426. $GLOBALS['PMA_Config']->setUserValue(
  427. 'pma_table_filename_template',
  428. 'Export/file_template_table',
  429. $filename_template
  430. );
  431. }
  432. }
  433. $filename = PMA_Util::expandUserString($filename_template);
  434. // remove dots in filename (coming from either the template or already
  435. // part of the filename) to avoid a remote code execution vulnerability
  436. $filename = PMA_sanitizeFilename($filename, $replaceDots = true);
  437. // Grab basic dump extension and mime type
  438. // Check if the user already added extension;
  439. // get the substring where the extension would be if it was included
  440. $extension_start_pos = strlen($filename) - strlen(
  441. $export_plugin->getProperties()->getExtension()
  442. ) - 1;
  443. $user_extension = substr($filename, $extension_start_pos, strlen($filename));
  444. $required_extension = "." . $export_plugin->getProperties()->getExtension();
  445. if (strtolower($user_extension) != $required_extension) {
  446. $filename .= $required_extension;
  447. }
  448. $mime_type = $export_plugin->getProperties()->getMimeType();
  449. // If dump is going to be compressed, set correct mime_type and add
  450. // compression to extension
  451. if ($compression == 'bzip2') {
  452. $filename .= '.bz2';
  453. $mime_type = 'application/x-bzip2';
  454. } elseif ($compression == 'gzip') {
  455. $filename .= '.gz';
  456. $mime_type = 'application/x-gzip';
  457. } elseif ($compression == 'zip') {
  458. $filename .= '.zip';
  459. $mime_type = 'application/zip';
  460. }
  461. }
  462. // Open file on server if needed
  463. if ($save_on_server) {
  464. $save_filename = PMA_Util::userDir($cfg['SaveDir'])
  465. . preg_replace('@[/\\\\]@', '_', $filename);
  466. unset($message);
  467. if (file_exists($save_filename)
  468. && ((! $quick_export && empty($_REQUEST['onserverover']))
  469. || ($quick_export
  470. && $_REQUEST['quick_export_onserverover'] != 'saveitover'))
  471. ) {
  472. $message = PMA_Message::error(
  473. __('File %s already exists on server, change filename or check overwrite option.')
  474. );
  475. $message->addParam($save_filename);
  476. } else {
  477. if (is_file($save_filename) && ! is_writable($save_filename)) {
  478. $message = PMA_Message::error(
  479. __('The web server does not have permission to save the file %s.')
  480. );
  481. $message->addParam($save_filename);
  482. } else {
  483. if (! $file_handle = @fopen($save_filename, 'w')) {
  484. $message = PMA_Message::error(
  485. __('The web server does not have permission to save the file %s.')
  486. );
  487. $message->addParam($save_filename);
  488. }
  489. }
  490. }
  491. if (isset($message)) {
  492. if ($export_type == 'server') {
  493. $active_page = 'server_export.php';
  494. include 'server_export.php';
  495. } elseif ($export_type == 'database') {
  496. $active_page = 'db_export.php';
  497. include 'db_export.php';
  498. } else {
  499. $active_page = 'tbl_export.php';
  500. include 'tbl_export.php';
  501. }
  502. exit();
  503. }
  504. }
  505. /**
  506. * Send headers depending on whether the user chose to download a dump file
  507. * or not
  508. */
  509. if (! $save_on_server) {
  510. if ($asfile) {
  511. // Download
  512. // (avoid rewriting data containing HTML with anchors and forms;
  513. // this was reported to happen under Plesk)
  514. @ini_set('url_rewriter.tags', '');
  515. $filename = PMA_sanitizeFilename($filename);
  516. PMA_downloadHeader($filename, $mime_type);
  517. } else {
  518. // HTML
  519. if ($export_type == 'database') {
  520. $num_tables = count($tables);
  521. if ($num_tables == 0) {
  522. $message = PMA_Message::error(__('No tables found in database.'));
  523. $active_page = 'db_export.php';
  524. include 'db_export.php';
  525. exit();
  526. }
  527. }
  528. $backup_cfgServer = $cfg['Server'];
  529. $cfg['Server'] = $backup_cfgServer;
  530. unset($backup_cfgServer);
  531. echo "\n" . '<div style="text-align: ' . $cell_align_left . '">' . "\n";
  532. //echo ' <pre>' . "\n";
  533. /**
  534. * Displays a back button with all the $_REQUEST data in the URL
  535. * (store in a variable to also display after the textarea)
  536. */
  537. $back_button = '<p>[ <a href="';
  538. if ($export_type == 'server') {
  539. $back_button .= 'server_export.php?' . PMA_generate_common_url();
  540. } elseif ($export_type == 'database') {
  541. $back_button .= 'db_export.php?' . PMA_generate_common_url($db);
  542. } else {
  543. $back_button .= 'tbl_export.php?' . PMA_generate_common_url($db, $table);
  544. }
  545. // Convert the multiple select elements from an array to a string
  546. if ($export_type == 'server' && isset($_REQUEST['db_select'])) {
  547. $_REQUEST['db_select'] = implode(",", $_REQUEST['db_select']);
  548. } elseif ($export_type == 'database' && isset($_REQUEST['table_select'])) {
  549. $_REQUEST['table_select'] = implode(",", $_REQUEST['table_select']);
  550. }
  551. foreach ($_REQUEST as $name => $value) {
  552. $back_button .= '&amp;' . urlencode($name) . '=' . urlencode($value);
  553. }
  554. $back_button .= '&amp;repopulate=1">Back</a> ]</p>';
  555. echo $back_button;
  556. echo ' <form name="nofunction">' . "\n"
  557. // remove auto-select for now: there is no way to select
  558. // only a part of the text; anyway, it should obey
  559. // $cfg['TextareaAutoSelect']
  560. //. ' <textarea name="sqldump" cols="50" rows="30" onclick="this.select();" id="textSQLDUMP" wrap="OFF">' . "\n";
  561. . ' <textarea name="sqldump" cols="50" rows="30" id="textSQLDUMP" wrap="OFF">' . "\n";
  562. } // end download
  563. }
  564. // Fake loop just to allow skip of remain of this code by break, I'd really
  565. // need exceptions here :-)
  566. do {
  567. // Add possibly some comments to export
  568. if (! $export_plugin->exportHeader($db)) {
  569. break;
  570. }
  571. // Will we need relation & co. setup?
  572. $do_relation = isset($GLOBALS[$what . '_relation']);
  573. $do_comments = isset($GLOBALS[$what . '_include_comments']);
  574. $do_mime = isset($GLOBALS[$what . '_mime']);
  575. if ($do_relation || $do_comments || $do_mime) {
  576. $cfgRelation = PMA_getRelationsParam();
  577. }
  578. if ($do_mime) {
  579. include_once 'libraries/transformations.lib.php';
  580. }
  581. // Include dates in export?
  582. $do_dates = isset($GLOBALS[$what . '_dates']);
  583. /**
  584. * Builds the dump
  585. */
  586. // Gets the number of tables if a dump of a database has been required
  587. if ($export_type == 'server') {
  588. if (isset($db_select)) {
  589. $tmp_select = implode($db_select, '|');
  590. $tmp_select = '|' . $tmp_select . '|';
  591. }
  592. // Walk over databases
  593. foreach ($GLOBALS['pma']->databases as $current_db) {
  594. if (isset($tmp_select)
  595. && strpos(' ' . $tmp_select, '|' . $current_db . '|')
  596. ) {
  597. if (! $export_plugin->exportDBHeader($current_db)) {
  598. break 2;
  599. }
  600. if (! $export_plugin->exportDBCreate($current_db)) {
  601. break 2;
  602. }
  603. if (method_exists($export_plugin, 'exportRoutines')
  604. && strpos($GLOBALS['sql_structure_or_data'], 'structure') !== false
  605. && isset($GLOBALS['sql_procedure_function'])
  606. ) {
  607. $export_plugin->exportRoutines($current_db);
  608. }
  609. $tables = PMA_DBI_get_tables($current_db);
  610. $views = array();
  611. foreach ($tables as $table) {
  612. // if this is a view, collect it for later;
  613. // views must be exported after the tables
  614. $is_view = PMA_Table::isView($current_db, $table);
  615. if ($is_view) {
  616. $views[] = $table;
  617. }
  618. if ($GLOBALS[$what . '_structure_or_data'] == 'structure'
  619. || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data'
  620. ) {
  621. // for a view, export a stand-in definition of the table
  622. // to resolve view dependencies
  623. if (! $export_plugin->exportStructure(
  624. $current_db, $table, $crlf, $err_url,
  625. $is_view ? 'stand_in' : 'create_table', $export_type,
  626. $do_relation, $do_comments, $do_mime, $do_dates
  627. )) {
  628. break 3;
  629. }
  630. }
  631. // if this is a view or a merge table, don't export data
  632. if (($GLOBALS[$what . '_structure_or_data'] == 'data'
  633. || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data')
  634. && ! ($is_view || PMA_Table::isMerge($current_db, $table))
  635. ) {
  636. $local_query = 'SELECT * FROM ' . PMA_Util::backquote($current_db)
  637. . '.' . PMA_Util::backquote($table);
  638. if (! $export_plugin->exportData($current_db, $table, $crlf, $err_url, $local_query)) {
  639. break 3;
  640. }
  641. }
  642. // now export the triggers (needs to be done after the data
  643. // because triggers can modify already imported tables)
  644. if ($GLOBALS[$what . '_structure_or_data'] == 'structure'
  645. || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data'
  646. ) {
  647. if (! $export_plugin->exportStructure(
  648. $current_db, $table, $crlf, $err_url,
  649. 'triggers', $export_type,
  650. $do_relation, $do_comments, $do_mime, $do_dates
  651. )) {
  652. break 2;
  653. }
  654. }
  655. }
  656. foreach ($views as $view) {
  657. // no data export for a view
  658. if ($GLOBALS[$what . '_structure_or_data'] == 'structure'
  659. || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data'
  660. ) {
  661. if (! $export_plugin->exportStructure(
  662. $current_db, $view, $crlf, $err_url,
  663. 'create_view', $export_type,
  664. $do_relation, $do_comments, $do_mime, $do_dates
  665. )) {
  666. break 3;
  667. }
  668. }
  669. }
  670. if (! $export_plugin->exportDBFooter($current_db)) {
  671. break 2;
  672. }
  673. }
  674. }
  675. } elseif ($export_type == 'database') {
  676. if (! $export_plugin->exportDBHeader($db)) {
  677. break;
  678. }
  679. if (! $export_plugin->exportDBCreate($db)) {
  680. break;
  681. }
  682. if (method_exists($export_plugin, 'exportRoutines')
  683. && strpos($GLOBALS['sql_structure_or_data'], 'structure') !== false
  684. && isset($GLOBALS['sql_procedure_function'])
  685. ) {
  686. $export_plugin->exportRoutines($db);
  687. }
  688. $i = 0;
  689. $views = array();
  690. // $tables contains the choices from the user (via $table_select)
  691. foreach ($tables as $table) {
  692. // if this is a view, collect it for later; views must be exported after
  693. // the tables
  694. $is_view = PMA_Table::isView($db, $table);
  695. if ($is_view) {
  696. $views[] = $table;
  697. }
  698. if ($GLOBALS[$what . '_structure_or_data'] == 'structure'
  699. || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data'
  700. ) {
  701. // for a view, export a stand-in definition of the table
  702. // to resolve view dependencies
  703. if (! $export_plugin->exportStructure(
  704. $db, $table, $crlf, $err_url,
  705. $is_view ? 'stand_in' : 'create_table', $export_type,
  706. $do_relation, $do_comments, $do_mime, $do_dates
  707. )) {
  708. break 2;
  709. }
  710. }
  711. // if this is a view or a merge table, don't export data
  712. if (($GLOBALS[$what . '_structure_or_data'] == 'data'
  713. || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data')
  714. && ! ($is_view || PMA_Table::isMerge($db, $table))
  715. ) {
  716. $local_query = 'SELECT * FROM ' . PMA_Util::backquote($db)
  717. . '.' . PMA_Util::backquote($table);
  718. if (! $export_plugin->exportData($db, $table, $crlf, $err_url, $local_query)) {
  719. break 2;
  720. }
  721. }
  722. // now export the triggers (needs to be done after the data because
  723. // triggers can modify already imported tables)
  724. if ($GLOBALS[$what . '_structure_or_data'] == 'structure'
  725. || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data'
  726. ) {
  727. if (! $export_plugin->exportStructure(
  728. $db, $table, $crlf, $err_url,
  729. 'triggers', $export_type,
  730. $do_relation, $do_comments, $do_mime, $do_dates
  731. )) {
  732. break 2;
  733. }
  734. }
  735. }
  736. foreach ($views as $view) {
  737. // no data export for a view
  738. if ($GLOBALS[$what . '_structure_or_data'] == 'structure'
  739. || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data'
  740. ) {
  741. if (! $export_plugin->exportStructure(
  742. $db, $view, $crlf, $err_url,
  743. 'create_view', $export_type,
  744. $do_relation, $do_comments, $do_mime, $do_dates
  745. )) {
  746. break 2;
  747. }
  748. }
  749. }
  750. if (! $export_plugin->exportDBFooter($db)) {
  751. break;
  752. }
  753. } else {
  754. if (! $export_plugin->exportDBHeader($db)) {
  755. break;
  756. }
  757. // We export just one table
  758. // $allrows comes from the form when "Dump all rows" has been selected
  759. if (isset($allrows) && $allrows == '0' && $limit_to > 0 && $limit_from >= 0) {
  760. $add_query = ' LIMIT '
  761. . (($limit_from > 0) ? $limit_from . ', ' : '')
  762. . $limit_to;
  763. } else {
  764. $add_query = '';
  765. }
  766. $is_view = PMA_Table::isView($db, $table);
  767. if ($GLOBALS[$what . '_structure_or_data'] == 'structure'
  768. || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data'
  769. ) {
  770. if (! $export_plugin->exportStructure(
  771. $db, $table, $crlf, $err_url,
  772. $is_view ? 'create_view' : 'create_table', $export_type,
  773. $do_relation, $do_comments, $do_mime, $do_dates
  774. )) {
  775. break;
  776. }
  777. }
  778. // If this is an export of a single view, we have to export data;
  779. // for example, a PDF report
  780. // if it is a merge table, no data is exported
  781. if (($GLOBALS[$what . '_structure_or_data'] == 'data'
  782. || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data')
  783. && ! PMA_Table::isMerge($db, $table)
  784. ) {
  785. if (! empty($sql_query)) {
  786. // only preg_replace if needed
  787. if (! empty($add_query)) {
  788. // remove trailing semicolon before adding a LIMIT
  789. $sql_query = preg_replace('%;\s*$%', '', $sql_query);
  790. }
  791. $local_query = $sql_query . $add_query;
  792. PMA_DBI_select_db($db);
  793. } else {
  794. $local_query = 'SELECT * FROM ' . PMA_Util::backquote($db)
  795. . '.' . PMA_Util::backquote($table) . $add_query;
  796. }
  797. if (! $export_plugin->exportData($db, $table, $crlf, $err_url, $local_query)) {
  798. break;
  799. }
  800. }
  801. // now export the triggers (needs to be done after the data because
  802. // triggers can modify already imported tables)
  803. if ($GLOBALS[$what . '_structure_or_data'] == 'structure'
  804. || $GLOBALS[$what . '_structure_or_data'] == 'structure_and_data'
  805. ) {
  806. if (! $export_plugin->exportStructure(
  807. $db, $table, $crlf, $err_url,
  808. 'triggers', $export_type,
  809. $do_relation, $do_comments, $do_mime, $do_dates
  810. )) {
  811. break 2;
  812. }
  813. }
  814. if (! $export_plugin->exportDBFooter($db)) {
  815. break;
  816. }
  817. }
  818. if (! $export_plugin->exportFooter()) {
  819. break;
  820. }
  821. } while (false);
  822. // End of fake loop
  823. if ($save_on_server && isset($message)) {
  824. if ($export_type == 'server') {
  825. $active_page = 'server_export.php';
  826. include 'server_export.php';
  827. } elseif ($export_type == 'database') {
  828. $active_page = 'db_export.php';
  829. include 'db_export.php';
  830. } else {
  831. $active_page = 'tbl_export.php';
  832. include 'tbl_export.php';
  833. }
  834. exit();
  835. }
  836. /**
  837. * Send the dump as a file...
  838. */
  839. if (! empty($asfile)) {
  840. // Convert the charset if required.
  841. if ($output_charset_conversion) {
  842. $dump_buffer = PMA_convert_string(
  843. 'utf-8',
  844. $GLOBALS['charset_of_file'],
  845. $dump_buffer
  846. );
  847. }
  848. // Do the compression
  849. // 1. as a zipped file
  850. if ($compression == 'zip') {
  851. if (@function_exists('gzcompress')) {
  852. $zipfile = new ZipFile();
  853. $zipfile->addFile($dump_buffer, substr($filename, 0, -4));
  854. $dump_buffer = $zipfile->file();
  855. }
  856. } elseif ($compression == 'bzip2') {
  857. // 2. as a bzipped file
  858. if (@function_exists('bzcompress')) {
  859. $dump_buffer = bzcompress($dump_buffer);
  860. }
  861. } elseif ($compression == 'gzip' && PMA_gzencodeNeeded()) {
  862. // 3. as a gzipped file
  863. // without the optional parameter level because it bugs
  864. $dump_buffer = gzencode($dump_buffer);
  865. }
  866. /* If we saved on server, we have to close file now */
  867. if ($save_on_server) {
  868. $write_result = @fwrite($file_handle, $dump_buffer);
  869. fclose($file_handle);
  870. if (strlen($dump_buffer) > 0
  871. && (! $write_result || ($write_result != strlen($dump_buffer)))
  872. ) {
  873. $message = new PMA_Message(
  874. __('Insufficient space to save the file %s.'),
  875. PMA_Message::ERROR,
  876. $save_filename
  877. );
  878. } else {
  879. $message = new PMA_Message(
  880. __('Dump has been saved to file %s.'),
  881. PMA_Message::SUCCESS,
  882. $save_filename
  883. );
  884. }
  885. if ($export_type == 'server') {
  886. $active_page = 'server_export.php';
  887. include_once 'server_export.php';
  888. } elseif ($export_type == 'database') {
  889. $active_page = 'db_export.php';
  890. include_once 'db_export.php';
  891. } else {
  892. $active_page = 'tbl_export.php';
  893. include_once 'tbl_export.php';
  894. }
  895. exit();
  896. } else {
  897. echo $dump_buffer;
  898. }
  899. } else {
  900. /**
  901. * Displays the dump...
  902. *
  903. * Close the html tags and add the footers if dump is displayed on screen
  904. */
  905. echo '</textarea>' . "\n"
  906. . ' </form>' . "\n";
  907. echo $back_button;
  908. echo "\n";
  909. echo '</div>' . "\n";
  910. echo "\n";
  911. ?>
  912. <script type="text/javascript">
  913. //<![CDATA[
  914. var $body = $("body");
  915. $("#textSQLDUMP")
  916. .width($body.width() - 50)
  917. .height($body.height() - 100);
  918. //]]>
  919. </script>
  920. <?php
  921. } // end if
  922. ?>