ExportPhparray.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <?php
  2. /**
  3. * Set of functions used to build dumps of tables as PHP Arrays
  4. */
  5. declare(strict_types=1);
  6. namespace PhpMyAdmin\Plugins\Export;
  7. use PhpMyAdmin\DatabaseInterface;
  8. use PhpMyAdmin\Plugins\ExportPlugin;
  9. use PhpMyAdmin\Properties\Options\Groups\OptionsPropertyMainGroup;
  10. use PhpMyAdmin\Properties\Options\Groups\OptionsPropertyRootGroup;
  11. use PhpMyAdmin\Properties\Options\Items\HiddenPropertyItem;
  12. use PhpMyAdmin\Properties\Plugins\ExportPluginProperties;
  13. use PhpMyAdmin\Util;
  14. use PhpMyAdmin\Version;
  15. use function __;
  16. use function preg_match;
  17. use function preg_replace;
  18. use function stripslashes;
  19. use function strtr;
  20. use function var_export;
  21. /**
  22. * Handles the export for the PHP Array class
  23. */
  24. class ExportPhparray extends ExportPlugin
  25. {
  26. /**
  27. * @psalm-return non-empty-lowercase-string
  28. */
  29. public function getName(): string
  30. {
  31. return 'phparray';
  32. }
  33. protected function setProperties(): ExportPluginProperties
  34. {
  35. $exportPluginProperties = new ExportPluginProperties();
  36. $exportPluginProperties->setText('PHP array');
  37. $exportPluginProperties->setExtension('php');
  38. $exportPluginProperties->setMimeType('text/plain');
  39. $exportPluginProperties->setOptionsText(__('Options'));
  40. // create the root group that will be the options field for
  41. // $exportPluginProperties
  42. // this will be shown as "Format specific options"
  43. $exportSpecificOptions = new OptionsPropertyRootGroup('Format Specific Options');
  44. // general options main group
  45. $generalOptions = new OptionsPropertyMainGroup('general_opts');
  46. // create primary items and add them to the group
  47. $leaf = new HiddenPropertyItem('structure_or_data');
  48. $generalOptions->addProperty($leaf);
  49. // add the main group to the root group
  50. $exportSpecificOptions->addProperty($generalOptions);
  51. // set the options for the export plugin property item
  52. $exportPluginProperties->setOptions($exportSpecificOptions);
  53. return $exportPluginProperties;
  54. }
  55. /**
  56. * Removes end of comment from a string
  57. *
  58. * @param string $string String to replace
  59. *
  60. * @return string
  61. */
  62. public function commentString($string)
  63. {
  64. return strtr($string, '*/', '-');
  65. }
  66. /**
  67. * Outputs export header
  68. */
  69. public function exportHeader(): bool
  70. {
  71. $this->export->outputHandler(
  72. '<?php' . $GLOBALS['crlf']
  73. . '/**' . $GLOBALS['crlf']
  74. . ' * Export to PHP Array plugin for PHPMyAdmin' . $GLOBALS['crlf']
  75. . ' * @version ' . Version::VERSION . $GLOBALS['crlf']
  76. . ' */' . $GLOBALS['crlf'] . $GLOBALS['crlf']
  77. );
  78. return true;
  79. }
  80. /**
  81. * Outputs export footer
  82. */
  83. public function exportFooter(): bool
  84. {
  85. return true;
  86. }
  87. /**
  88. * Outputs database header
  89. *
  90. * @param string $db Database name
  91. * @param string $dbAlias Aliases of db
  92. */
  93. public function exportDBHeader($db, $dbAlias = ''): bool
  94. {
  95. if (empty($dbAlias)) {
  96. $dbAlias = $db;
  97. }
  98. $this->export->outputHandler(
  99. '/**' . $GLOBALS['crlf']
  100. . ' * Database ' . $this->commentString(Util::backquote($dbAlias))
  101. . $GLOBALS['crlf'] . ' */' . $GLOBALS['crlf']
  102. );
  103. return true;
  104. }
  105. /**
  106. * Outputs database footer
  107. *
  108. * @param string $db Database name
  109. */
  110. public function exportDBFooter($db): bool
  111. {
  112. return true;
  113. }
  114. /**
  115. * Outputs CREATE DATABASE statement
  116. *
  117. * @param string $db Database name
  118. * @param string $exportType 'server', 'database', 'table'
  119. * @param string $dbAlias Aliases of db
  120. */
  121. public function exportDBCreate($db, $exportType, $dbAlias = ''): bool
  122. {
  123. return true;
  124. }
  125. /**
  126. * Outputs the content of a table in PHP array format
  127. *
  128. * @param string $db database name
  129. * @param string $table table name
  130. * @param string $crlf the end of line sequence
  131. * @param string $errorUrl the url to go back in case of error
  132. * @param string $sqlQuery SQL query for obtaining data
  133. * @param array $aliases Aliases of db/table/columns
  134. */
  135. public function exportData(
  136. $db,
  137. $table,
  138. $crlf,
  139. $errorUrl,
  140. $sqlQuery,
  141. array $aliases = []
  142. ): bool {
  143. global $dbi;
  144. $db_alias = $db;
  145. $table_alias = $table;
  146. $this->initAlias($aliases, $db_alias, $table_alias);
  147. $result = $dbi->query($sqlQuery, DatabaseInterface::CONNECT_USER, DatabaseInterface::QUERY_UNBUFFERED);
  148. $columns_cnt = $result->numFields();
  149. $columns = [];
  150. foreach ($result->getFieldNames() as $i => $col_as) {
  151. if (! empty($aliases[$db]['tables'][$table]['columns'][$col_as])) {
  152. $col_as = $aliases[$db]['tables'][$table]['columns'][$col_as];
  153. }
  154. $columns[$i] = stripslashes($col_as);
  155. }
  156. $tablefixed = $table;
  157. // fix variable names (based on
  158. // https://www.php.net/manual/en/language.variables.basics.php)
  159. if (! preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $table_alias)) {
  160. // fix invalid characters in variable names by replacing them with
  161. // underscores
  162. $tablefixed = preg_replace('/[^a-zA-Z0-9_\x7f-\xff]/', '_', $table_alias);
  163. // variable name must not start with a number or dash...
  164. if (preg_match('/^[a-zA-Z_\x7f-\xff]/', $tablefixed) === 0) {
  165. $tablefixed = '_' . $tablefixed;
  166. }
  167. }
  168. $buffer = '';
  169. $record_cnt = 0;
  170. // Output table name as comment
  171. $buffer .= $crlf . '/* '
  172. . $this->commentString(Util::backquote($db_alias)) . '.'
  173. . $this->commentString(Util::backquote($table_alias)) . ' */' . $crlf;
  174. $buffer .= '$' . $tablefixed . ' = array(';
  175. if (! $this->export->outputHandler($buffer)) {
  176. return false;
  177. }
  178. // Reset the buffer
  179. $buffer = '';
  180. while ($record = $result->fetchRow()) {
  181. $record_cnt++;
  182. if ($record_cnt == 1) {
  183. $buffer .= $crlf . ' array(';
  184. } else {
  185. $buffer .= ',' . $crlf . ' array(';
  186. }
  187. for ($i = 0; $i < $columns_cnt; $i++) {
  188. $buffer .= var_export($columns[$i], true)
  189. . ' => ' . var_export($record[$i], true)
  190. . ($i + 1 >= $columns_cnt ? '' : ',');
  191. }
  192. $buffer .= ')';
  193. if (! $this->export->outputHandler($buffer)) {
  194. return false;
  195. }
  196. // Reset the buffer
  197. $buffer = '';
  198. }
  199. $buffer .= $crlf . ');' . $crlf;
  200. return $this->export->outputHandler($buffer);
  201. }
  202. /**
  203. * Outputs result of raw query as PHP array
  204. *
  205. * @param string $errorUrl the url to go back in case of error
  206. * @param string|null $db the database where the query is executed
  207. * @param string $sqlQuery the rawquery to output
  208. * @param string $crlf the end of line sequence
  209. */
  210. public function exportRawQuery(string $errorUrl, ?string $db, string $sqlQuery, string $crlf): bool
  211. {
  212. global $dbi;
  213. if ($db !== null) {
  214. $dbi->selectDb($db);
  215. }
  216. return $this->exportData($db ?? '', '', $crlf, $errorUrl, $sqlQuery);
  217. }
  218. }