/my/libraries/export/csv.php

https://github.com/cabenitez/factuweb · PHP · 217 lines · 125 code · 16 blank · 76 comment · 37 complexity · 7b099e18f0b62a1db6cf11bcf64bc6f6 MD5 · raw file

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