ExportPhparray.php 7.3 KB

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