Export.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * functions for displaying server, database and table export
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. namespace PhpMyAdmin\Display;
  9. use PhpMyAdmin\Core;
  10. use PhpMyAdmin\DatabaseInterface;
  11. use PhpMyAdmin\Encoding;
  12. use PhpMyAdmin\Message;
  13. use PhpMyAdmin\Plugins;
  14. use PhpMyAdmin\Plugins\ExportPlugin;
  15. use PhpMyAdmin\Relation;
  16. use PhpMyAdmin\Response;
  17. use PhpMyAdmin\Table;
  18. use PhpMyAdmin\Template;
  19. use PhpMyAdmin\Url;
  20. use PhpMyAdmin\Util;
  21. /**
  22. * PhpMyAdmin\Display\Export class
  23. *
  24. * @package PhpMyAdmin
  25. */
  26. class Export
  27. {
  28. /**
  29. * @var Relation $relation
  30. */
  31. private $relation;
  32. /**
  33. * Constructor
  34. */
  35. public function __construct()
  36. {
  37. $this->relation = new Relation();
  38. }
  39. /**
  40. * Outputs appropriate checked statement for checkbox.
  41. *
  42. * @param string $str option name
  43. *
  44. * @return boolean
  45. */
  46. private function checkboxCheck($str)
  47. {
  48. return isset($GLOBALS['cfg']['Export'][$str])
  49. && $GLOBALS['cfg']['Export'][$str];
  50. }
  51. /**
  52. * Prints Html For Export Selection Options
  53. *
  54. * @param string $tmpSelect Tmp selected method of export
  55. *
  56. * @return string
  57. */
  58. public function getHtmlForSelectOptions($tmpSelect = '')
  59. {
  60. // Check if the selected databases are defined in $_POST
  61. // (from clicking Back button on export.php)
  62. if (isset($_POST['db_select'])) {
  63. $_POST['db_select'] = urldecode($_POST['db_select']);
  64. $_POST['db_select'] = explode(",", $_POST['db_select']);
  65. }
  66. $databases = [];
  67. foreach ($GLOBALS['dblist']->databases as $currentDb) {
  68. if ($GLOBALS['dbi']->isSystemSchema($currentDb, true)) {
  69. continue;
  70. }
  71. $isSelected = false;
  72. if (isset($_POST['db_select'])) {
  73. if (in_array($currentDb, $_POST['db_select'])) {
  74. $isSelected = true;
  75. }
  76. } elseif (!empty($tmpSelect)) {
  77. if (mb_strpos(
  78. ' ' . $tmpSelect,
  79. '|' . $currentDb . '|'
  80. )) {
  81. $isSelected = true;
  82. }
  83. } else {
  84. $isSelected = true;
  85. }
  86. $databases[] = [
  87. 'name' => $currentDb,
  88. 'is_selected' => $isSelected,
  89. ];
  90. }
  91. return Template::get('display/export/select_options')->render([
  92. 'databases' => $databases,
  93. ]);
  94. }
  95. /**
  96. * Prints Html For Export Hidden Input
  97. *
  98. * @param string $exportType Selected Export Type
  99. * @param string $db Selected DB
  100. * @param string $table Selected Table
  101. * @param string $singleTable Single Table
  102. * @param string $sqlQuery SQL Query
  103. *
  104. * @return string
  105. */
  106. public function getHtmlForHiddenInputs(
  107. $exportType,
  108. $db,
  109. $table,
  110. $singleTable,
  111. $sqlQuery
  112. ) {
  113. global $cfg;
  114. // If the export method was not set, the default is quick
  115. if (isset($_POST['export_method'])) {
  116. $cfg['Export']['method'] = $_POST['export_method'];
  117. } elseif (! isset($cfg['Export']['method'])) {
  118. $cfg['Export']['method'] = 'quick';
  119. }
  120. if (empty($sqlQuery) && isset($_POST['sql_query'])) {
  121. $sqlQuery = $_POST['sql_query'];
  122. }
  123. return Template::get('display/export/hidden_inputs')->render([
  124. 'db' => $db,
  125. 'table' => $table,
  126. 'export_type' => $exportType,
  127. 'export_method' => $cfg['Export']['method'],
  128. 'single_table' => $singleTable,
  129. 'sql_query' => $sqlQuery,
  130. 'template_id' => isset($_POST['template_id']) ? $_POST['template_id'] : '',
  131. ]);
  132. }
  133. /**
  134. * Returns HTML for the options in template dropdown
  135. *
  136. * @param string $exportType export type - server, database, or table
  137. *
  138. * @return string HTML for the options in teplate dropdown
  139. */
  140. private function getOptionsForTemplates($exportType)
  141. {
  142. // Get the relation settings
  143. $cfgRelation = $this->relation->getRelationsParam();
  144. $query = "SELECT `id`, `template_name` FROM "
  145. . Util::backquote($cfgRelation['db']) . '.'
  146. . Util::backquote($cfgRelation['export_templates'])
  147. . " WHERE `username` = "
  148. . "'" . $GLOBALS['dbi']->escapeString($GLOBALS['cfg']['Server']['user'])
  149. . "' AND `export_type` = '" . $GLOBALS['dbi']->escapeString($exportType) . "'"
  150. . " ORDER BY `template_name`;";
  151. $result = $this->relation->queryAsControlUser($query);
  152. $templates = [];
  153. if ($result !== false) {
  154. while ($row = $GLOBALS['dbi']->fetchAssoc($result, DatabaseInterface::CONNECT_CONTROL)) {
  155. $templates[] = [
  156. 'name' => $row['template_name'],
  157. 'id' => $row['id'],
  158. ];
  159. }
  160. }
  161. return Template::get('display/export/template_options')->render([
  162. 'templates' => $templates,
  163. 'selected_template' => !empty($_POST['template_id']) ? $_POST['template_id'] : null,
  164. ]);
  165. }
  166. /**
  167. * Prints Html For Export Options Method
  168. *
  169. * @return string
  170. */
  171. private function getHtmlForOptionsMethod()
  172. {
  173. global $cfg;
  174. if (isset($_POST['quick_or_custom'])) {
  175. $exportMethod = $_POST['quick_or_custom'];
  176. } else {
  177. $exportMethod = $cfg['Export']['method'];
  178. }
  179. return Template::get('display/export/method')->render([
  180. 'export_method' => $exportMethod,
  181. ]);
  182. }
  183. /**
  184. * Prints Html For Export Options Selection
  185. *
  186. * @param string $exportType Selected Export Type
  187. * @param string $multiValues Export Options
  188. *
  189. * @return string
  190. */
  191. private function getHtmlForOptionsSelection($exportType, $multiValues)
  192. {
  193. return Template::get('display/export/selection')->render([
  194. 'export_type' => $exportType,
  195. 'multi_values' => $multiValues,
  196. ]);
  197. }
  198. /**
  199. * Prints Html For Export Options Format dropdown
  200. *
  201. * @param ExportPlugin[] $exportList Export List
  202. *
  203. * @return string
  204. */
  205. private function getHtmlForOptionsFormatDropdown($exportList)
  206. {
  207. $dropdown = Plugins::getChoice('Export', 'what', $exportList, 'format');
  208. return Template::get('display/export/format_dropdown')->render([
  209. 'dropdown' => $dropdown,
  210. ]);
  211. }
  212. /**
  213. * Prints Html For Export Options Format-specific options
  214. *
  215. * @param ExportPlugin[] $exportList Export List
  216. *
  217. * @return string
  218. */
  219. private function getHtmlForOptionsFormat($exportList)
  220. {
  221. global $cfg;
  222. $options = Plugins::getOptions('Export', $exportList);
  223. return Template::get('display/export/options_format')->render([
  224. 'options' => $options,
  225. 'can_convert_kanji' => Encoding::canConvertKanji(),
  226. 'exec_time_limit' => $cfg['ExecTimeLimit'],
  227. ]);
  228. }
  229. /**
  230. * Prints Html For Export Options Rows
  231. *
  232. * @param string $db Selected DB
  233. * @param string $table Selected Table
  234. * @param string $unlimNumRows Num of Rows
  235. *
  236. * @return string
  237. */
  238. private function getHtmlForOptionsRows($db, $table, $unlimNumRows)
  239. {
  240. $tableObject = new Table($table, $db);
  241. $numberOfRows = $tableObject->countRecords();
  242. return Template::get('display/export/options_rows')->render([
  243. 'allrows' => isset($_POST['allrows']) ? $_POST['allrows'] : null,
  244. 'limit_to' => isset($_POST['limit_to']) ? $_POST['limit_to'] : null,
  245. 'limit_from' => isset($_POST['limit_from']) ? $_POST['limit_from'] : null,
  246. 'unlim_num_rows' => $unlimNumRows,
  247. 'number_of_rows' => $numberOfRows,
  248. ]);
  249. }
  250. /**
  251. * Prints Html For Export Options Quick Export
  252. *
  253. * @return string
  254. */
  255. private function getHtmlForOptionsQuickExport()
  256. {
  257. global $cfg;
  258. $saveDir = Util::userDir($cfg['SaveDir']);
  259. $exportIsChecked = $this->checkboxCheck(
  260. 'quick_export_onserver'
  261. );
  262. $exportOverwriteIsChecked = $this->checkboxCheck(
  263. 'quick_export_onserver_overwrite'
  264. );
  265. return Template::get('display/export/options_quick_export')->render([
  266. 'save_dir' => $saveDir,
  267. 'export_is_checked' => $exportIsChecked,
  268. 'export_overwrite_is_checked' => $exportOverwriteIsChecked,
  269. ]);
  270. }
  271. /**
  272. * Prints Html For Export Options Save Dir
  273. *
  274. * @return string
  275. */
  276. private function getHtmlForOptionsOutputSaveDir()
  277. {
  278. global $cfg;
  279. $saveDir = Util::userDir($cfg['SaveDir']);
  280. $exportIsChecked = $this->checkboxCheck(
  281. 'onserver'
  282. );
  283. $exportOverwriteIsChecked = $this->checkboxCheck(
  284. 'onserver_overwrite'
  285. );
  286. return Template::get('display/export/options_output_save_dir')->render([
  287. 'save_dir' => $saveDir,
  288. 'export_is_checked' => $exportIsChecked,
  289. 'export_overwrite_is_checked' => $exportOverwriteIsChecked,
  290. ]);
  291. }
  292. /**
  293. * Prints Html For Export Options
  294. *
  295. * @param string $exportType Selected Export Type
  296. *
  297. * @return string
  298. */
  299. private function getHtmlForOptionsOutputFormat($exportType)
  300. {
  301. $trans = new Message;
  302. $trans->addText(__('@SERVER@ will become the server name'));
  303. if ($exportType == 'database' || $exportType == 'table') {
  304. $trans->addText(__(', @DATABASE@ will become the database name'));
  305. if ($exportType == 'table') {
  306. $trans->addText(__(', @TABLE@ will become the table name'));
  307. }
  308. }
  309. $msg = new Message(
  310. __(
  311. 'This value is interpreted using %1$sstrftime%2$s, '
  312. . 'so you can use time formatting strings. '
  313. . 'Additionally the following transformations will happen: %3$s. '
  314. . 'Other text will be kept as is. See the %4$sFAQ%5$s for details.'
  315. )
  316. );
  317. $msg->addParamHtml(
  318. '<a href="' . Core::linkURL(Core::getPHPDocLink('function.strftime.php'))
  319. . '" target="documentation" title="' . __('Documentation') . '">'
  320. );
  321. $msg->addParamHtml('</a>');
  322. $msg->addParam($trans);
  323. $docUrl = Util::getDocuLink('faq', 'faq6-27');
  324. $msg->addParamHtml(
  325. '<a href="' . $docUrl . '" target="documentation">'
  326. );
  327. $msg->addParamHtml('</a>');
  328. if (isset($_POST['filename_template'])) {
  329. $filenameTemplate = $_POST['filename_template'];
  330. } else {
  331. if ($exportType == 'database') {
  332. $filenameTemplate = $GLOBALS['PMA_Config']->getUserValue(
  333. 'pma_db_filename_template',
  334. $GLOBALS['cfg']['Export']['file_template_database']
  335. );
  336. } elseif ($exportType == 'table') {
  337. $filenameTemplate = $GLOBALS['PMA_Config']->getUserValue(
  338. 'pma_table_filename_template',
  339. $GLOBALS['cfg']['Export']['file_template_table']
  340. );
  341. } else {
  342. $filenameTemplate = $GLOBALS['PMA_Config']->getUserValue(
  343. 'pma_server_filename_template',
  344. $GLOBALS['cfg']['Export']['file_template_server']
  345. );
  346. }
  347. }
  348. return Template::get('display/export/options_output_format')->render([
  349. 'message' => $msg->getMessage(),
  350. 'filename_template' => $filenameTemplate,
  351. 'is_checked' => $this->checkboxCheck('remember_file_template'),
  352. ]);
  353. }
  354. /**
  355. * Prints Html For Export Options Charset
  356. *
  357. * @return string
  358. */
  359. private function getHtmlForOptionsOutputCharset()
  360. {
  361. global $cfg;
  362. return Template::get('display/export/options_output_charset')->render([
  363. 'encodings' => Encoding::listEncodings(),
  364. 'export_charset' => $cfg['Export']['charset'],
  365. ]);
  366. }
  367. /**
  368. * Prints Html For Export Options Compression
  369. *
  370. * @return string
  371. */
  372. private function getHtmlForOptionsOutputCompression()
  373. {
  374. global $cfg;
  375. if (isset($_POST['compression'])) {
  376. $selectedCompression = $_POST['compression'];
  377. } elseif (isset($cfg['Export']['compression'])) {
  378. $selectedCompression = $cfg['Export']['compression'];
  379. } else {
  380. $selectedCompression = 'none';
  381. }
  382. // Since separate files export works with ZIP only
  383. if (isset($cfg['Export']['as_separate_files'])
  384. && $cfg['Export']['as_separate_files']
  385. ) {
  386. $selectedCompression = 'zip';
  387. }
  388. // zip and gzip encode features
  389. $isZip = ($cfg['ZipDump'] && function_exists('gzcompress'));
  390. $isGzip = ($cfg['GZipDump'] && function_exists('gzencode'));
  391. return Template::get('display/export/options_output_compression')->render([
  392. 'is_zip' => $isZip,
  393. 'is_gzip' => $isGzip,
  394. 'selected_compression' => $selectedCompression,
  395. ]);
  396. }
  397. /**
  398. * Prints Html For Export Options Radio
  399. *
  400. * @return string
  401. */
  402. private function getHtmlForOptionsOutputRadio()
  403. {
  404. return Template::get('display/export/options_output_radio')->render([
  405. 'has_repopulate' => isset($_POST['repopulate']),
  406. 'export_asfile' => $GLOBALS['cfg']['Export']['asfile'],
  407. ]);
  408. }
  409. /**
  410. * Prints Html For Export Options Checkbox - Separate files
  411. *
  412. * @param string $exportType Selected Export Type
  413. *
  414. * @return string
  415. */
  416. private function getHtmlForOptionsOutputSeparateFiles($exportType)
  417. {
  418. $isChecked = $this->checkboxCheck('as_separate_files');
  419. return Template::get('display/export/options_output_separate_files')->render([
  420. 'is_checked' => $isChecked,
  421. 'export_type' => $exportType,
  422. ]);
  423. }
  424. /**
  425. * Prints Html For Export Options
  426. *
  427. * @param string $exportType Selected Export Type
  428. *
  429. * @return string
  430. */
  431. private function getHtmlForOptionsOutput($exportType)
  432. {
  433. global $cfg;
  434. $hasAliases = isset($_SESSION['tmpval']['aliases'])
  435. && !Core::emptyRecursive($_SESSION['tmpval']['aliases']);
  436. unset($_SESSION['tmpval']['aliases']);
  437. $isCheckedLockTables = $this->checkboxCheck('lock_tables');
  438. $isCheckedAsfile = $this->checkboxCheck('asfile');
  439. $optionsOutputSaveDir = '';
  440. if (isset($cfg['SaveDir']) && !empty($cfg['SaveDir'])) {
  441. $optionsOutputSaveDir = $this->getHtmlForOptionsOutputSaveDir();
  442. }
  443. $optionsOutputFormat = $this->getHtmlForOptionsOutputFormat($exportType);
  444. $optionsOutputCharset = '';
  445. if (Encoding::isSupported()) {
  446. $optionsOutputCharset = $this->getHtmlForOptionsOutputCharset();
  447. }
  448. $optionsOutputCompression = $this->getHtmlForOptionsOutputCompression();
  449. $optionsOutputSeparateFiles = '';
  450. if ($exportType == 'server' || $exportType == 'database') {
  451. $optionsOutputSeparateFiles = $this->getHtmlForOptionsOutputSeparateFiles(
  452. $exportType
  453. );
  454. }
  455. $optionsOutputRadio = $this->getHtmlForOptionsOutputRadio();
  456. return Template::get('display/export/options_output')->render([
  457. 'has_aliases' => $hasAliases,
  458. 'export_type' => $exportType,
  459. 'is_checked_lock_tables' => $isCheckedLockTables,
  460. 'is_checked_asfile' => $isCheckedAsfile,
  461. 'repopulate' => isset($_POST['repopulate']),
  462. 'lock_tables' => isset($_POST['lock_tables']),
  463. 'save_dir' => isset($cfg['SaveDir']) ? $cfg['SaveDir'] : null,
  464. 'is_encoding_supported' => Encoding::isSupported(),
  465. 'options_output_save_dir' => $optionsOutputSaveDir,
  466. 'options_output_format' => $optionsOutputFormat,
  467. 'options_output_charset' => $optionsOutputCharset,
  468. 'options_output_compression' => $optionsOutputCompression,
  469. 'options_output_separate_files' => $optionsOutputSeparateFiles,
  470. 'options_output_radio' => $optionsOutputRadio,
  471. ]);
  472. }
  473. /**
  474. * Prints Html For Export Options
  475. *
  476. * @param string $exportType Selected Export Type
  477. * @param string $db Selected DB
  478. * @param string $table Selected Table
  479. * @param string $multiValues Export selection
  480. * @param string $numTables number of tables
  481. * @param ExportPlugin[] $exportList Export List
  482. * @param string $unlimNumRows Number of Rows
  483. *
  484. * @return string
  485. */
  486. public function getHtmlForOptions(
  487. $exportType,
  488. $db,
  489. $table,
  490. $multiValues,
  491. $numTables,
  492. $exportList,
  493. $unlimNumRows
  494. ) {
  495. global $cfg;
  496. $html = $this->getHtmlForOptionsMethod();
  497. $html .= $this->getHtmlForOptionsFormatDropdown($exportList);
  498. $html .= $this->getHtmlForOptionsSelection($exportType, $multiValues);
  499. $tableObject = new Table($table, $db);
  500. if (strlen($table) > 0 && empty($numTables) && ! $tableObject->isMerge()) {
  501. $html .= $this->getHtmlForOptionsRows($db, $table, $unlimNumRows);
  502. }
  503. if (isset($cfg['SaveDir']) && !empty($cfg['SaveDir'])) {
  504. $html .= $this->getHtmlForOptionsQuickExport();
  505. }
  506. $html .= $this->getHtmlForAliasModalDialog();
  507. $html .= $this->getHtmlForOptionsOutput($exportType);
  508. $html .= $this->getHtmlForOptionsFormat($exportList);
  509. return $html;
  510. }
  511. /**
  512. * Generate Html For currently defined aliases
  513. *
  514. * @return string
  515. */
  516. private function getHtmlForCurrentAlias()
  517. {
  518. $result = '<table id="alias_data"><thead><tr><th colspan="4">'
  519. . __('Defined aliases')
  520. . '</th></tr></thead><tbody>';
  521. $template = Template::get('export/alias_item');
  522. if (isset($_SESSION['tmpval']['aliases'])) {
  523. foreach ($_SESSION['tmpval']['aliases'] as $db => $dbData) {
  524. if (isset($dbData['alias'])) {
  525. $result .= $template->render(array(
  526. 'type' => _pgettext('Alias', 'Database'),
  527. 'name' => $db,
  528. 'field' => 'aliases[' . $db . '][alias]',
  529. 'value' => $dbData['alias'],
  530. ));
  531. }
  532. if (! isset($dbData['tables'])) {
  533. continue;
  534. }
  535. foreach ($dbData['tables'] as $table => $tableData) {
  536. if (isset($tableData['alias'])) {
  537. $result .= $template->render(array(
  538. 'type' => _pgettext('Alias', 'Table'),
  539. 'name' => $db . '.' . $table,
  540. 'field' => 'aliases[' . $db . '][tables][' . $table . '][alias]',
  541. 'value' => $tableData['alias'],
  542. ));
  543. }
  544. if (! isset($tableData['columns'])) {
  545. continue;
  546. }
  547. foreach ($tableData['columns'] as $column => $columnName) {
  548. $result .= $template->render(array(
  549. 'type' => _pgettext('Alias', 'Column'),
  550. 'name' => $db . '.' . $table . '.'. $column,
  551. 'field' => 'aliases[' . $db . '][tables][' . $table . '][colums][' . $column . ']',
  552. 'value' => $columnName,
  553. ));
  554. }
  555. }
  556. }
  557. }
  558. // Empty row for javascript manipulations
  559. $result .= '</tbody><tfoot class="hide">' . $template->render(array(
  560. 'type' => '', 'name' => '', 'field' => 'aliases_new', 'value' => ''
  561. )) . '</tfoot>';
  562. return $result . '</table>';
  563. }
  564. /**
  565. * Generate Html For Alias Modal Dialog
  566. *
  567. * @return string
  568. */
  569. public function getHtmlForAliasModalDialog()
  570. {
  571. $title = __('Rename exported databases/tables/columns');
  572. $html = '<div id="alias_modal" class="hide" title="' . $title . '">';
  573. $html .= $this->getHtmlForCurrentAlias();
  574. $html .= Template::get('export/alias_add')->render();
  575. $html .= '</div>';
  576. return $html;
  577. }
  578. /**
  579. * Gets HTML to display export dialogs
  580. *
  581. * @param string $exportType export type: server|database|table
  582. * @param string $db selected DB
  583. * @param string $table selected table
  584. * @param string $sqlQuery SQL query
  585. * @param int $numTables number of tables
  586. * @param int $unlimNumRows unlimited number of rows
  587. * @param string $multiValues selector options
  588. *
  589. * @return string $html
  590. */
  591. public function getDisplay(
  592. $exportType,
  593. $db,
  594. $table,
  595. $sqlQuery,
  596. $numTables,
  597. $unlimNumRows,
  598. $multiValues
  599. ) {
  600. $cfgRelation = $this->relation->getRelationsParam();
  601. if (isset($_POST['single_table'])) {
  602. $GLOBALS['single_table'] = $_POST['single_table'];
  603. }
  604. // Export a single table
  605. if (isset($_GET['single_table'])) {
  606. $GLOBALS['single_table'] = $_GET['single_table'];
  607. }
  608. /* Scan for plugins */
  609. /* @var $exportList ExportPlugin[] */
  610. $exportList = Plugins::getPlugins(
  611. "export",
  612. 'libraries/classes/Plugins/Export/',
  613. array(
  614. 'export_type' => $exportType,
  615. 'single_table' => isset($GLOBALS['single_table'])
  616. )
  617. );
  618. /* Fail if we didn't find any plugin */
  619. if (empty($exportList)) {
  620. Message::error(
  621. __('Could not load export plugins, please check your installation!')
  622. )->display();
  623. exit;
  624. }
  625. $html = Template::get('display/export/option_header')->render([
  626. 'export_type' => $exportType,
  627. 'db' => $db,
  628. 'table' => $table,
  629. ]);
  630. if ($cfgRelation['exporttemplateswork']) {
  631. $html .= Template::get('display/export/template_loading')->render([
  632. 'options' => $this->getOptionsForTemplates($exportType),
  633. ]);
  634. }
  635. $html .= '<form method="post" action="export.php" '
  636. . ' name="dump" class="disableAjax">';
  637. //output Hidden Inputs
  638. $singleTableStr = isset($GLOBALS['single_table']) ? $GLOBALS['single_table']
  639. : '';
  640. $html .= $this->getHtmlForHiddenInputs(
  641. $exportType,
  642. $db,
  643. $table,
  644. $singleTableStr,
  645. $sqlQuery
  646. );
  647. //output Export Options
  648. $html .= $this->getHtmlForOptions(
  649. $exportType,
  650. $db,
  651. $table,
  652. $multiValues,
  653. $numTables,
  654. $exportList,
  655. $unlimNumRows
  656. );
  657. $html .= '</form>';
  658. return $html;
  659. }
  660. /**
  661. * Handles export template actions
  662. *
  663. * @param array $cfgRelation Relation configuration
  664. *
  665. * @return void
  666. */
  667. public function handleTemplateActions(array $cfgRelation)
  668. {
  669. if (isset($_POST['templateId'])) {
  670. $id = $GLOBALS['dbi']->escapeString($_POST['templateId']);
  671. } else {
  672. $id = '';
  673. }
  674. $templateTable = Util::backquote($cfgRelation['db']) . '.'
  675. . Util::backquote($cfgRelation['export_templates']);
  676. $user = $GLOBALS['dbi']->escapeString($GLOBALS['cfg']['Server']['user']);
  677. switch ($_POST['templateAction']) {
  678. case 'create':
  679. $query = "INSERT INTO " . $templateTable . "("
  680. . " `username`, `export_type`,"
  681. . " `template_name`, `template_data`"
  682. . ") VALUES ("
  683. . "'" . $user . "', "
  684. . "'" . $GLOBALS['dbi']->escapeString($_POST['exportType'])
  685. . "', '" . $GLOBALS['dbi']->escapeString($_POST['templateName'])
  686. . "', '" . $GLOBALS['dbi']->escapeString($_POST['templateData'])
  687. . "');";
  688. break;
  689. case 'load':
  690. $query = "SELECT `template_data` FROM " . $templateTable
  691. . " WHERE `id` = " . $id . " AND `username` = '" . $user . "'";
  692. break;
  693. case 'update':
  694. $query = "UPDATE " . $templateTable . " SET `template_data` = "
  695. . "'" . $GLOBALS['dbi']->escapeString($_POST['templateData']) . "'"
  696. . " WHERE `id` = " . $id . " AND `username` = '" . $user . "'";
  697. break;
  698. case 'delete':
  699. $query = "DELETE FROM " . $templateTable
  700. . " WHERE `id` = " . $id . " AND `username` = '" . $user . "'";
  701. break;
  702. default:
  703. $query = '';
  704. break;
  705. }
  706. $result = $this->relation->queryAsControlUser($query, false);
  707. $response = Response::getInstance();
  708. if (! $result) {
  709. $error = $GLOBALS['dbi']->getError(DatabaseInterface::CONNECT_CONTROL);
  710. $response->setRequestStatus(false);
  711. $response->addJSON('message', $error);
  712. exit;
  713. }
  714. $response->setRequestStatus(true);
  715. if ('create' == $_POST['templateAction']) {
  716. $response->addJSON(
  717. 'data',
  718. $this->getOptionsForTemplates($_POST['exportType'])
  719. );
  720. } elseif ('load' == $_POST['templateAction']) {
  721. $data = null;
  722. while ($row = $GLOBALS['dbi']->fetchAssoc(
  723. $result, DatabaseInterface::CONNECT_CONTROL
  724. )) {
  725. $data = $row['template_data'];
  726. }
  727. $response->addJSON('data', $data);
  728. }
  729. $GLOBALS['dbi']->freeResult($result);
  730. }
  731. }