/gui/tools/pma/libraries/export/csv.php

https://github.com/BenBE/ispCP · PHP · 235 lines · 141 code · 16 blank · 78 comment · 37 complexity · 9d5ad6044db7a82563593ca44f110f47 MD5 · raw file

  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * @package phpMyAdmin-Export-CSV
  5. */
  6. if (! defined('PHPMYADMIN')) {
  7. exit;
  8. }
  9. /**
  10. * Set of functions used to build CSV dumps of tables
  11. */
  12. if (isset($plugin_list)) {
  13. $plugin_list['csv'] = array(
  14. 'text' => __('CSV'),
  15. 'extension' => 'csv',
  16. 'mime_type' => 'text/comma-separated-values',
  17. 'options' => array(
  18. array('type' => 'begin_group', 'name' => 'general_opts'),
  19. array('type' => 'text', 'name' => 'separator', 'text' => __('Columns separated with:')),
  20. array('type' => 'text', 'name' => 'enclosed', 'text' => __('Columns enclosed with:')),
  21. array('type' => 'text', 'name' => 'escaped', 'text' => __('Columns escaped with:')),
  22. array('type' => 'text', 'name' => 'terminated', 'text' => __('Lines terminated with:')),
  23. array('type' => 'text', 'name' => 'null', 'text' => __('Replace NULL with:')),
  24. array('type' => 'bool', 'name' => 'removeCRLF', 'text' => __('Remove carriage return/line feed characters within columns')),
  25. array('type' => 'bool', 'name' => 'columns', 'text' => __('Put columns names in the first row')),
  26. array('type' => 'hidden', 'name' => 'structure_or_data'),
  27. array('type' => 'end_group'),
  28. ),
  29. 'options_text' => __('Options'),
  30. );
  31. } else {
  32. /**
  33. * Outputs comment
  34. *
  35. * @param string Text of comment
  36. *
  37. * @return bool Whether it suceeded
  38. */
  39. function PMA_exportComment($text) {
  40. return TRUE;
  41. }
  42. /**
  43. * Outputs export footer
  44. *
  45. * @return bool Whether it suceeded
  46. *
  47. * @access public
  48. */
  49. function PMA_exportFooter() {
  50. return TRUE;
  51. }
  52. /**
  53. * Outputs export header
  54. *
  55. * @return bool Whether it suceeded
  56. *
  57. * @access public
  58. */
  59. function PMA_exportHeader() {
  60. global $what;
  61. global $csv_terminated;
  62. global $csv_separator;
  63. global $csv_enclosed;
  64. global $csv_escaped;
  65. // Here we just prepare some values for export
  66. if ($what == 'excel') {
  67. $csv_terminated = "\015\012";
  68. switch($GLOBALS['excel_edition']) {
  69. case 'win':
  70. // as tested on Windows with Excel 2002 and Excel 2007
  71. $csv_separator = ';';
  72. break;
  73. case 'mac_excel2003':
  74. $csv_separator = ';';
  75. break;
  76. case 'mac_excel2008':
  77. $csv_separator = ',';
  78. break;
  79. }
  80. $csv_enclosed = '"';
  81. $csv_escaped = '"';
  82. if (isset($GLOBALS['excel_columns'])) {
  83. $GLOBALS['csv_columns'] = 'yes';
  84. }
  85. } else {
  86. if (empty($csv_terminated) || strtolower($csv_terminated) == 'auto') {
  87. $csv_terminated = $GLOBALS['crlf'];
  88. } else {
  89. $csv_terminated = str_replace('\\r', "\015", $csv_terminated);
  90. $csv_terminated = str_replace('\\n', "\012", $csv_terminated);
  91. $csv_terminated = str_replace('\\t', "\011", $csv_terminated);
  92. } // end if
  93. $csv_separator = str_replace('\\t', "\011", $csv_separator);
  94. }
  95. return TRUE;
  96. }
  97. /**
  98. * Outputs database header
  99. *
  100. * @param string Database name
  101. *
  102. * @return bool Whether it suceeded
  103. *
  104. * @access public
  105. */
  106. function PMA_exportDBHeader($db) {
  107. return TRUE;
  108. }
  109. /**
  110. * Outputs database footer
  111. *
  112. * @param string Database name
  113. *
  114. * @return bool Whether it suceeded
  115. *
  116. * @access public
  117. */
  118. function PMA_exportDBFooter($db) {
  119. return TRUE;
  120. }
  121. /**
  122. * Outputs create database database
  123. *
  124. * @param string Database name
  125. *
  126. * @return bool Whether it suceeded
  127. *
  128. * @access public
  129. */
  130. function PMA_exportDBCreate($db) {
  131. return TRUE;
  132. }
  133. /**
  134. * Outputs the content of a table in CSV format
  135. *
  136. * @param string the database name
  137. * @param string the table name
  138. * @param string the end of line sequence
  139. * @param string the url to go back in case of error
  140. * @param string SQL query for obtaining data
  141. *
  142. * @return bool Whether it suceeded
  143. *
  144. * @access public
  145. */
  146. function PMA_exportData($db, $table, $crlf, $error_url, $sql_query) {
  147. global $what;
  148. global $csv_terminated;
  149. global $csv_separator;
  150. global $csv_enclosed;
  151. global $csv_escaped;
  152. // Gets the data from the database
  153. $result = PMA_DBI_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED);
  154. $fields_cnt = PMA_DBI_num_fields($result);
  155. // If required, get fields name at the first line
  156. if (isset($GLOBALS['csv_columns'])) {
  157. $schema_insert = '';
  158. for ($i = 0; $i < $fields_cnt; $i++) {
  159. if ($csv_enclosed == '') {
  160. $schema_insert .= stripslashes(PMA_DBI_field_name($result, $i));
  161. } else {
  162. $schema_insert .= $csv_enclosed
  163. . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, stripslashes(PMA_DBI_field_name($result, $i)))
  164. . $csv_enclosed;
  165. }
  166. $schema_insert .= $csv_separator;
  167. } // end for
  168. $schema_insert =trim(substr($schema_insert, 0, -1));
  169. if (!PMA_exportOutputHandler($schema_insert . $csv_terminated)) {
  170. return FALSE;
  171. }
  172. } // end if
  173. // Format the data
  174. while ($row = PMA_DBI_fetch_row($result)) {
  175. $schema_insert = '';
  176. for ($j = 0; $j < $fields_cnt; $j++) {
  177. if (!isset($row[$j]) || is_null($row[$j])) {
  178. $schema_insert .= $GLOBALS[$what . '_null'];
  179. } elseif ($row[$j] == '0' || $row[$j] != '') {
  180. // always enclose fields
  181. if ($what == 'excel') {
  182. $row[$j] = preg_replace("/\015(\012)?/", "\012", $row[$j]);
  183. }
  184. // remove CRLF characters within field
  185. if (isset($GLOBALS[$what . '_removeCRLF']) && $GLOBALS[$what . '_removeCRLF']) {
  186. $row[$j] = str_replace("\n", "", str_replace("\r", "", $row[$j]));
  187. }
  188. if ($csv_enclosed == '') {
  189. $schema_insert .= $row[$j];
  190. } else {
  191. // also double the escape string if found in the data
  192. if ('csv' == $what) {
  193. $schema_insert .= $csv_enclosed
  194. . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, str_replace($csv_escaped, $csv_escaped . $csv_escaped, $row[$j]))
  195. . $csv_enclosed;
  196. } else {
  197. // for excel, avoid a problem when a field contains
  198. // double quotes
  199. $schema_insert .= $csv_enclosed
  200. . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, $row[$j])
  201. . $csv_enclosed;
  202. }
  203. }
  204. } else {
  205. $schema_insert .= '';
  206. }
  207. if ($j < $fields_cnt-1) {
  208. $schema_insert .= $csv_separator;
  209. }
  210. } // end for
  211. if (!PMA_exportOutputHandler($schema_insert . $csv_terminated)) {
  212. return FALSE;
  213. }
  214. } // end while
  215. PMA_DBI_free_result($result);
  216. return TRUE;
  217. } // end of the 'PMA_getTableCsv()' function
  218. }
  219. ?>