ExportPhparray.php 7.9 KB

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