home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / phpMyAdmin / libraries / export / csv.php next >
Encoding:
PHP Script  |  2008-06-23  |  6.0 KB  |  210 lines

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