ExportPhparray.php 7.2 KB

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