PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/22222/htdocs/db/pma/libraries/plugins/export/ExportCsv.php

https://gitlab.com/jmcdowall/transactions
PHP | 331 lines | 227 code | 21 blank | 83 comment | 36 complexity | 1366eb9022be3d15913a24c4639e4923 MD5 | raw file
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * CSV export code
  5. *
  6. * @package PhpMyAdmin-Export
  7. * @subpackage CSV
  8. */
  9. namespace PMA\libraries\plugins\export;
  10. use PMA\libraries\properties\options\items\BoolPropertyItem;
  11. use PMA\libraries\plugins\ExportPlugin;
  12. use PMA\libraries\properties\plugins\ExportPluginProperties;
  13. use PMA\libraries\properties\options\items\HiddenPropertyItem;
  14. use PMA\libraries\properties\options\groups\OptionsPropertyMainGroup;
  15. use PMA\libraries\properties\options\groups\OptionsPropertyRootGroup;
  16. use PMA;
  17. use PMA\libraries\properties\options\items\TextPropertyItem;
  18. /**
  19. * Handles the export for the CSV format
  20. *
  21. * @package PhpMyAdmin-Export
  22. * @subpackage CSV
  23. */
  24. class ExportCsv extends ExportPlugin
  25. {
  26. /**
  27. * Constructor
  28. */
  29. public function __construct()
  30. {
  31. $this->setProperties();
  32. }
  33. /**
  34. * Sets the export CSV properties
  35. *
  36. * @return void
  37. */
  38. protected function setProperties()
  39. {
  40. $exportPluginProperties = new ExportPluginProperties();
  41. $exportPluginProperties->setText('CSV');
  42. $exportPluginProperties->setExtension('csv');
  43. $exportPluginProperties->setMimeType('text/comma-separated-values');
  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 leaf items and add them to the group
  54. $leaf = new TextPropertyItem(
  55. "separator",
  56. __('Columns separated with:')
  57. );
  58. $generalOptions->addProperty($leaf);
  59. $leaf = new TextPropertyItem(
  60. "enclosed",
  61. __('Columns enclosed with:')
  62. );
  63. $generalOptions->addProperty($leaf);
  64. $leaf = new TextPropertyItem(
  65. "escaped",
  66. __('Columns escaped with:')
  67. );
  68. $generalOptions->addProperty($leaf);
  69. $leaf = new TextPropertyItem(
  70. "terminated",
  71. __('Lines terminated with:')
  72. );
  73. $generalOptions->addProperty($leaf);
  74. $leaf = new TextPropertyItem(
  75. 'null',
  76. __('Replace NULL with:')
  77. );
  78. $generalOptions->addProperty($leaf);
  79. $leaf = new BoolPropertyItem(
  80. 'removeCRLF',
  81. __('Remove carriage return/line feed characters within columns')
  82. );
  83. $generalOptions->addProperty($leaf);
  84. $leaf = new BoolPropertyItem(
  85. 'columns',
  86. __('Put columns names in the first row')
  87. );
  88. $generalOptions->addProperty($leaf);
  89. $leaf = new HiddenPropertyItem(
  90. 'structure_or_data'
  91. );
  92. $generalOptions->addProperty($leaf);
  93. // add the main group to the root group
  94. $exportSpecificOptions->addProperty($generalOptions);
  95. // set the options for the export plugin property item
  96. $exportPluginProperties->setOptions($exportSpecificOptions);
  97. $this->properties = $exportPluginProperties;
  98. }
  99. /**
  100. * Outputs export header
  101. *
  102. * @return bool Whether it succeeded
  103. */
  104. public function exportHeader()
  105. {
  106. global $what, $csv_terminated, $csv_separator, $csv_enclosed, $csv_escaped;
  107. // Here we just prepare some values for export
  108. if ($what == 'excel') {
  109. $csv_terminated = "\015\012";
  110. switch ($GLOBALS['excel_edition']) {
  111. case 'win':
  112. // as tested on Windows with Excel 2002 and Excel 2007
  113. $csv_separator = ';';
  114. break;
  115. case 'mac_excel2003':
  116. $csv_separator = ';';
  117. break;
  118. case 'mac_excel2008':
  119. $csv_separator = ',';
  120. break;
  121. }
  122. $csv_enclosed = '"';
  123. $csv_escaped = '"';
  124. if (isset($GLOBALS['excel_columns'])) {
  125. $GLOBALS['csv_columns'] = 'yes';
  126. }
  127. } else {
  128. if (empty($csv_terminated)
  129. || mb_strtolower($csv_terminated) == 'auto'
  130. ) {
  131. $csv_terminated = $GLOBALS['crlf'];
  132. } else {
  133. $csv_terminated = str_replace('\\r', "\015", $csv_terminated);
  134. $csv_terminated = str_replace('\\n', "\012", $csv_terminated);
  135. $csv_terminated = str_replace('\\t', "\011", $csv_terminated);
  136. } // end if
  137. $csv_separator = str_replace('\\t', "\011", $csv_separator);
  138. }
  139. return true;
  140. }
  141. /**
  142. * Outputs export footer
  143. *
  144. * @return bool Whether it succeeded
  145. */
  146. public function exportFooter()
  147. {
  148. return true;
  149. }
  150. /**
  151. * Outputs database header
  152. *
  153. * @param string $db Database name
  154. * @param string $db_alias Alias of db
  155. *
  156. * @return bool Whether it succeeded
  157. */
  158. public function exportDBHeader($db, $db_alias = '')
  159. {
  160. return true;
  161. }
  162. /**
  163. * Outputs database footer
  164. *
  165. * @param string $db Database name
  166. *
  167. * @return bool Whether it succeeded
  168. */
  169. public function exportDBFooter($db)
  170. {
  171. return true;
  172. }
  173. /**
  174. * Outputs CREATE DATABASE statement
  175. *
  176. * @param string $db Database name
  177. * @param string $export_type 'server', 'database', 'table'
  178. * @param string $db_alias Aliases of db
  179. *
  180. * @return bool Whether it succeeded
  181. */
  182. public function exportDBCreate($db, $export_type, $db_alias = '')
  183. {
  184. return true;
  185. }
  186. /**
  187. * Outputs the content of a table in CSV format
  188. *
  189. * @param string $db database name
  190. * @param string $table table name
  191. * @param string $crlf the end of line sequence
  192. * @param string $error_url the url to go back in case of error
  193. * @param string $sql_query SQL query for obtaining data
  194. * @param array $aliases Aliases of db/table/columns
  195. *
  196. * @return bool Whether it succeeded
  197. */
  198. public function exportData(
  199. $db,
  200. $table,
  201. $crlf,
  202. $error_url,
  203. $sql_query,
  204. $aliases = array()
  205. ) {
  206. global $what, $csv_terminated, $csv_separator, $csv_enclosed, $csv_escaped;
  207. $db_alias = $db;
  208. $table_alias = $table;
  209. $this->initAlias($aliases, $db_alias, $table_alias);
  210. // Gets the data from the database
  211. $result = $GLOBALS['dbi']->query(
  212. $sql_query,
  213. null,
  214. PMA\libraries\DatabaseInterface::QUERY_UNBUFFERED
  215. );
  216. $fields_cnt = $GLOBALS['dbi']->numFields($result);
  217. // If required, get fields name at the first line
  218. if (isset($GLOBALS['csv_columns'])) {
  219. $schema_insert = '';
  220. for ($i = 0; $i < $fields_cnt; $i++) {
  221. $col_as = $GLOBALS['dbi']->fieldName($result, $i);
  222. if (!empty($aliases[$db]['tables'][$table]['columns'][$col_as])) {
  223. $col_as = $aliases[$db]['tables'][$table]['columns'][$col_as];
  224. }
  225. $col_as = stripslashes($col_as);
  226. if ($csv_enclosed == '') {
  227. $schema_insert .= $col_as;
  228. } else {
  229. $schema_insert .= $csv_enclosed
  230. . str_replace(
  231. $csv_enclosed,
  232. $csv_escaped . $csv_enclosed,
  233. $col_as
  234. )
  235. . $csv_enclosed;
  236. }
  237. $schema_insert .= $csv_separator;
  238. } // end for
  239. $schema_insert = trim(mb_substr($schema_insert, 0, -1));
  240. if (!PMA_exportOutputHandler($schema_insert . $csv_terminated)) {
  241. return false;
  242. }
  243. } // end if
  244. // Format the data
  245. while ($row = $GLOBALS['dbi']->fetchRow($result)) {
  246. $schema_insert = '';
  247. for ($j = 0; $j < $fields_cnt; $j++) {
  248. if (!isset($row[$j]) || is_null($row[$j])) {
  249. $schema_insert .= $GLOBALS[$what . '_null'];
  250. } elseif ($row[$j] == '0' || $row[$j] != '') {
  251. // always enclose fields
  252. if ($what == 'excel') {
  253. $row[$j] = preg_replace("/\015(\012)?/", "\012", $row[$j]);
  254. }
  255. // remove CRLF characters within field
  256. if (isset($GLOBALS[$what . '_removeCRLF'])
  257. && $GLOBALS[$what . '_removeCRLF']
  258. ) {
  259. $row[$j] = str_replace(
  260. "\n",
  261. "",
  262. str_replace(
  263. "\r",
  264. "",
  265. $row[$j]
  266. )
  267. );
  268. }
  269. if ($csv_enclosed == '') {
  270. $schema_insert .= $row[$j];
  271. } else {
  272. // also double the escape string if found in the data
  273. if ($csv_escaped != $csv_enclosed) {
  274. $schema_insert .= $csv_enclosed
  275. . str_replace(
  276. $csv_enclosed,
  277. $csv_escaped . $csv_enclosed,
  278. str_replace(
  279. $csv_escaped,
  280. $csv_escaped . $csv_escaped,
  281. $row[$j]
  282. )
  283. )
  284. . $csv_enclosed;
  285. } else {
  286. // avoid a problem when escape string equals enclose
  287. $schema_insert .= $csv_enclosed
  288. . str_replace(
  289. $csv_enclosed,
  290. $csv_escaped . $csv_enclosed,
  291. $row[$j]
  292. )
  293. . $csv_enclosed;
  294. }
  295. }
  296. } else {
  297. $schema_insert .= '';
  298. }
  299. if ($j < $fields_cnt - 1) {
  300. $schema_insert .= $csv_separator;
  301. }
  302. } // end for
  303. if (!PMA_exportOutputHandler($schema_insert . $csv_terminated)) {
  304. return false;
  305. }
  306. } // end while
  307. $GLOBALS['dbi']->freeResult($result);
  308. return true;
  309. }
  310. }