PageRenderTime 49ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/phpmyadmin/libraries/plugins/export/ExportPhparray.php

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