Options.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <?php
  2. declare(strict_types=1);
  3. namespace PhpMyAdmin\Export;
  4. use PhpMyAdmin\Core;
  5. use PhpMyAdmin\Encoding;
  6. use PhpMyAdmin\Plugins;
  7. use PhpMyAdmin\Plugins\ExportPlugin;
  8. use PhpMyAdmin\Query\Utilities;
  9. use PhpMyAdmin\Relation;
  10. use PhpMyAdmin\Table;
  11. use PhpMyAdmin\Util;
  12. use function explode;
  13. use function function_exists;
  14. use function in_array;
  15. use function is_array;
  16. use function mb_strpos;
  17. use function strlen;
  18. use function urldecode;
  19. final class Options
  20. {
  21. /** @var Relation */
  22. private $relation;
  23. /** @var TemplateModel */
  24. private $templateModel;
  25. public function __construct(Relation $relation, TemplateModel $templateModel)
  26. {
  27. $this->relation = $relation;
  28. $this->templateModel = $templateModel;
  29. }
  30. /**
  31. * Outputs appropriate checked statement for checkbox.
  32. *
  33. * @param string $str option name
  34. *
  35. * @return bool
  36. */
  37. private function checkboxCheck($str)
  38. {
  39. return isset($GLOBALS['cfg']['Export'][$str])
  40. && $GLOBALS['cfg']['Export'][$str];
  41. }
  42. /**
  43. * Prints Html For Export Selection Options
  44. *
  45. * @param string $tmpSelect Tmp selected method of export
  46. *
  47. * @return array
  48. */
  49. public function getDatabasesForSelectOptions($tmpSelect = '')
  50. {
  51. // Check if the selected databases are defined in $_POST
  52. // (from clicking Back button on /export page)
  53. if (isset($_POST['db_select'])) {
  54. $_POST['db_select'] = urldecode($_POST['db_select']);
  55. $_POST['db_select'] = explode(',', $_POST['db_select']);
  56. }
  57. $databases = [];
  58. foreach ($GLOBALS['dblist']->databases as $currentDb) {
  59. if (Utilities::isSystemSchema($currentDb, true)) {
  60. continue;
  61. }
  62. $isSelected = false;
  63. if (isset($_POST['db_select'])) {
  64. if (in_array($currentDb, $_POST['db_select'])) {
  65. $isSelected = true;
  66. }
  67. } elseif (! empty($tmpSelect)) {
  68. if (mb_strpos(
  69. ' ' . $tmpSelect,
  70. '|' . $currentDb . '|'
  71. )) {
  72. $isSelected = true;
  73. }
  74. } else {
  75. $isSelected = true;
  76. }
  77. $databases[] = [
  78. 'name' => $currentDb,
  79. 'is_selected' => $isSelected,
  80. ];
  81. }
  82. return $databases;
  83. }
  84. /**
  85. * @param string $exportType export type: server|database|table
  86. * @param string $db selected DB
  87. * @param string $table selected table
  88. * @param string $sqlQuery SQL query
  89. * @param int|string $numTables number of tables
  90. * @param int|string $unlimNumRows unlimited number of rows
  91. * @param ExportPlugin[] $exportList
  92. *
  93. * @return array<string, mixed>
  94. */
  95. public function getOptions(
  96. $exportType,
  97. $db,
  98. $table,
  99. $sqlQuery,
  100. $numTables,
  101. $unlimNumRows,
  102. array $exportList
  103. ) {
  104. global $cfg;
  105. $cfgRelation = $this->relation->getRelationsParam();
  106. $templates = [];
  107. if ($cfgRelation['exporttemplateswork']) {
  108. $templates = $this->templateModel->getAll(
  109. $cfgRelation['db'],
  110. $cfgRelation['export_templates'],
  111. $GLOBALS['cfg']['Server']['user'],
  112. $exportType
  113. );
  114. $templates = is_array($templates) ? $templates : [];
  115. }
  116. $dropdown = Plugins::getChoice('Export', 'what', $exportList, 'format');
  117. $tableObject = new Table($table, $db);
  118. $rows = [];
  119. if (strlen($table) > 0 && empty($numTables) && ! $tableObject->isMerge() && $exportType !== 'raw') {
  120. $rows = [
  121. 'allrows' => $_POST['allrows'] ?? null,
  122. 'limit_to' => $_POST['limit_to'] ?? null,
  123. 'limit_from' => $_POST['limit_from'] ?? null,
  124. 'unlim_num_rows' => $unlimNumRows,
  125. 'number_of_rows' => $tableObject->countRecords(),
  126. ];
  127. }
  128. $hasAliases = isset($_SESSION['tmpval']['aliases']) && ! Core::emptyRecursive($_SESSION['tmpval']['aliases']);
  129. $aliases = $_SESSION['tmpval']['aliases'] ?? [];
  130. unset($_SESSION['tmpval']['aliases']);
  131. $filenameTemplate = $this->getFileNameTemplate($exportType, $_POST['filename_template'] ?? null);
  132. $isEncodingSupported = Encoding::isSupported();
  133. $selectedCompression = $_POST['compression'] ?? $cfg['Export']['compression'] ?? 'none';
  134. if (isset($cfg['Export']['as_separate_files']) && $cfg['Export']['as_separate_files']) {
  135. $selectedCompression = 'zip';
  136. }
  137. $hiddenInputs = [
  138. 'db' => $db,
  139. 'table' => $table,
  140. 'export_type' => $exportType,
  141. 'export_method' => $_POST['export_method'] ?? $cfg['Export']['method'] ?? 'quick',
  142. 'template_id' => $_POST['template_id'] ?? '',
  143. ];
  144. if (! empty($GLOBALS['single_table'])) {
  145. $hiddenInputs['single_table'] = true;
  146. }
  147. if (! empty($sqlQuery)) {
  148. $hiddenInputs['sql_query'] = $sqlQuery;
  149. }
  150. return [
  151. 'export_type' => $exportType,
  152. 'db' => $db,
  153. 'table' => $table,
  154. 'templates' => [
  155. 'is_enabled' => $cfgRelation['exporttemplateswork'],
  156. 'templates' => $templates,
  157. 'selected' => $_POST['template_id'] ?? null,
  158. ],
  159. 'sql_query' => $sqlQuery,
  160. 'hidden_inputs' => $hiddenInputs,
  161. 'export_method' => $_POST['quick_or_custom'] ?? $cfg['Export']['method'] ?? '',
  162. 'dropdown' => $dropdown,
  163. 'options' => Plugins::getOptions('Export', $exportList),
  164. 'can_convert_kanji' => Encoding::canConvertKanji(),
  165. 'exec_time_limit' => $cfg['ExecTimeLimit'],
  166. 'rows' => $rows,
  167. 'has_save_dir' => isset($cfg['SaveDir']) && ! empty($cfg['SaveDir']),
  168. 'save_dir' => Util::userDir($cfg['SaveDir'] ?? ''),
  169. 'export_is_checked' => $this->checkboxCheck('quick_export_onserver'),
  170. 'export_overwrite_is_checked' => $this->checkboxCheck('quick_export_onserver_overwrite'),
  171. 'has_aliases' => $hasAliases,
  172. 'aliases' => $aliases,
  173. 'is_checked_lock_tables' => $this->checkboxCheck('lock_tables'),
  174. 'is_checked_asfile' => $this->checkboxCheck('asfile'),
  175. 'is_checked_as_separate_files' => $this->checkboxCheck('as_separate_files'),
  176. 'is_checked_export' => $this->checkboxCheck('onserver'),
  177. 'is_checked_export_overwrite' => $this->checkboxCheck('onserver_overwrite'),
  178. 'is_checked_remember_file_template' => $this->checkboxCheck('remember_file_template'),
  179. 'repopulate' => isset($_POST['repopulate']),
  180. 'lock_tables' => isset($_POST['lock_tables']),
  181. 'is_encoding_supported' => $isEncodingSupported,
  182. 'encodings' => $isEncodingSupported ? Encoding::listEncodings() : [],
  183. 'export_charset' => $cfg['Export']['charset'],
  184. 'export_asfile' => $cfg['Export']['asfile'],
  185. 'has_zip' => $cfg['ZipDump'] && function_exists('gzcompress'),
  186. 'has_gzip' => $cfg['GZipDump'] && function_exists('gzencode'),
  187. 'selected_compression' => $selectedCompression,
  188. 'filename_template' => $filenameTemplate,
  189. ];
  190. }
  191. private function getFileNameTemplate(string $exportType, ?string $filename = null): string
  192. {
  193. global $cfg, $PMA_Config;
  194. if ($filename !== null) {
  195. return $filename;
  196. }
  197. if ($exportType === 'database') {
  198. return (string) $PMA_Config->getUserValue(
  199. 'pma_db_filename_template',
  200. $cfg['Export']['file_template_database']
  201. );
  202. }
  203. if ($exportType === 'table') {
  204. return (string) $PMA_Config->getUserValue(
  205. 'pma_table_filename_template',
  206. $cfg['Export']['file_template_table']
  207. );
  208. }
  209. return (string) $PMA_Config->getUserValue(
  210. 'pma_server_filename_template',
  211. $cfg['Export']['file_template_server']
  212. );
  213. }
  214. }