home *** CD-ROM | disk | FTP | other *** search
/ PC World 2005 April / PCWorld_2005-04_cd.bin / akce / web / phptriad / phptriad2-2-1.exe / htdocs / phpmyadmin / tbl_dump.php < prev    next >
PHP Script  |  2002-01-06  |  11KB  |  321 lines

  1. <?php
  2. /* $Id: tbl_dump.php,v 1.42 2001/12/15 16:38:46 loic1 Exp $ */
  3.  
  4.  
  5. /**
  6.  * Formats the INSERT statements depending on the target (screen/file) of the
  7.  * sql dump
  8.  *
  9.  * @param   string  the insert statement
  10.  *
  11.  * @global  string  the buffer containing formatted strings
  12.  */
  13. function PMA_myHandler($sql_insert)
  14. {
  15.     global $tmp_buffer;
  16.  
  17.     // Defines the end of line delimiter to use
  18.     $eol_dlm = (isset($GLOBALS['extended_ins']) && ($GLOBALS['current_row'] < $GLOBALS['rows_cnt']))
  19.              ? ','
  20.              : ';';
  21.     // Result has to be displayed on screen
  22.     if (empty($GLOBALS['asfile'])) {
  23.         echo htmlspecialchars($sql_insert . $eol_dlm . $GLOBALS['crlf']);
  24.     }
  25.     // Result has to be saved in a text file
  26.     else if (!isset($GLOBALS['zip']) && !isset($GLOBALS['bzip']) && !isset($GLOBALS['gzip'])) {
  27.         echo $sql_insert . $eol_dlm . $GLOBALS['crlf'];
  28.     }
  29.     // Result will be saved in a *zipped file
  30.     else {
  31.         $tmp_buffer .= $sql_insert . $eol_dlm . $GLOBALS['crlf'];
  32.     }
  33. } // end of the 'PMA_myHandler()' function
  34.  
  35.  
  36. /**
  37.  * Formats the INSERT statements depending on the target (screen/file) of the
  38.  * cvs export
  39.  *
  40.  * Revisions: 2001-05-07, Lem9: added $add_character
  41.  *            2001-07-12, loic1: $crlf should be used only if there is no EOL
  42.  *                               character defined by the user
  43.  *
  44.  * @param   string  the insert statement
  45.  *
  46.  * @global  string  the character to add at the end of lines
  47.  * @global  string  the buffer containing formatted strings
  48.  */
  49. function PMA_myCsvHandler($sql_insert)
  50. {
  51.     global $add_character;
  52.     global $tmp_buffer;
  53.  
  54.     // Result has to be displayed on screen
  55.     if (empty($GLOBALS['asfile'])) {
  56.         echo htmlspecialchars($sql_insert) . $add_character;
  57.     }
  58.     // Result has to be saved in a text file
  59.     else if (!isset($GLOBALS['zip']) && !isset($GLOBALS['bzip']) && !isset($GLOBALS['gzip'])) {
  60.         echo $sql_insert . $add_character;
  61.     }
  62.     // Result will be saved in a *zipped file
  63.     else {
  64.         $tmp_buffer .= $sql_insert . $add_character;
  65.     }
  66. } // end of the 'PMA_myCsvHandler()' function
  67.  
  68.  
  69.  
  70. /**
  71.  * Get the variables sent or posted to this script and a core script
  72.  */
  73. require('./libraries/grab_globals.lib.php');
  74. require('./libraries/common.lib.php');
  75. require('./libraries/build_dump.lib.php');
  76. require('./libraries/zip.lib.php');
  77.  
  78.  
  79. /**
  80.  * Defines the url to return to in case of error in a sql statement
  81.  */
  82. $err_url = 'tbl_properties.php'
  83.          . '?lang=' . $lang
  84.          . '&server=' . $server
  85.          . '&db=' . urlencode($db)
  86.          . (isset($table) ? '&table=' . urlencode($table) : '');
  87.  
  88.  
  89. /**
  90.  * Increase time limit for script execution and initializes some variables
  91.  */
  92. @set_time_limit($cfgExecTimeLimit);
  93. $dump_buffer = '';
  94. // Defines the default <CR><LF> format
  95. $crlf        = PMA_whichCrlf();
  96.  
  97.  
  98. /**
  99.  * Ensure zipped formats are associated with the download feature
  100.  */
  101. if (empty($asfile)
  102.     && (!empty($zip) || !empty($gzip) || !empty($bzip))) {
  103.     $asfile = 1;
  104. }
  105.  
  106.  
  107. /**
  108.  * Send headers depending on whether the user choosen to download a dump file
  109.  * or not
  110.  */
  111. // No download
  112. if (empty($asfile)) {
  113.     $cfgServer_backup = $cfgServer;
  114.     include('./header.inc.php');
  115.     $cfgServer = $cfgServer_backup;
  116.     unset($cfgServer_backup);
  117.     echo '<div align="' . $cell_align_left . '">' . "\n";
  118.     echo '    <pre>' . "\n";
  119. } // end if
  120.  
  121. // Download
  122. else {
  123.     // Defines filename and extension, and also mime types
  124.     if (!isset($table)) {
  125.         $filename = $db;
  126.     } else {
  127.         $filename = $table;
  128.     }
  129.     if (isset($bzip) && $bzip == 'bzip') {
  130.         $ext       = 'bz2';
  131.         $mime_type = 'application/x-bzip';
  132.     } else if (isset($gzip) && $gzip == 'gzip') {
  133.         $ext       = 'gz';
  134.         $mime_type = 'application/x-gzip';
  135.     } else if (isset($zip) && $zip == 'zip') {
  136.         $ext       = 'zip';
  137.         $mime_type = 'application/x-zip';
  138.     } else if ($what == 'csv' || $what == 'excel') {
  139.         $ext       = 'csv';
  140.         $mime_type = 'text/x-csv';
  141.     } else {
  142.         $ext       = 'sql';
  143.         // loic1: 'application/octet-stream' is the registered IANA type but
  144.         //        MSIE and Opera seems to prefer 'application/octetstream'
  145.         $mime_type = (PMA_USR_BROWSER_AGENT == 'IE' || PMA_USR_BROWSER_AGENT == 'OPERA')
  146.                    ? 'application/octetstream'
  147.                    : 'application/octet-stream';
  148.     }
  149.  
  150.     // Send headers
  151.     header('Content-Type: ' . $mime_type);
  152.     // lem9 & loic1: IE need specific headers
  153.     if (PMA_USR_BROWSER_AGENT == 'IE') {
  154.         header('Content-Disposition: inline; filename="' . $filename . '.' . $ext . '"');
  155.         header('Expires: 0');
  156.         header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  157.         header('Pragma: public');
  158.     } else {
  159.         header('Content-Disposition: attachment; filename="' . $filename . '.' . $ext . '"');
  160.         header('Expires: 0');
  161.         header('Pragma: no-cache');
  162.     }
  163. } // end download
  164.  
  165.  
  166. /**
  167.  * Builds the dump
  168.  */
  169. // Gets the number of tables if a dump of a database has been required
  170. if (!isset($table)) {
  171.     $tables     = mysql_list_tables($db);
  172.     $num_tables = @mysql_numrows($tables);
  173. } else {
  174.     $num_tables = 1;
  175.     $single     = TRUE;
  176. }
  177.  
  178. // No table -> error message
  179. if ($num_tables == 0) {
  180.     echo '# ' . $strNoTablesFound;
  181. }
  182. // At least on table -> do the work
  183. else {
  184.     // No csv format -> add some comments at the top
  185.     if ($what != 'csv' &&  $what != 'excel') {
  186.         $dump_buffer       .= '# phpMyAdmin MySQL-Dump' . $crlf
  187.                            .  '# version ' . PMA_VERSION . $crlf
  188.                            .  '# http://phpwizard.net/phpMyAdmin/' . $crlf
  189.                            .  '# http://phpmyadmin.sourceforge.net/ (download page)' . $crlf
  190.                            .  '#' . $crlf
  191.                            .  '# ' . $strHost . ': ' . $cfgServer['host'];
  192.         if (!empty($cfgServer['port'])) {
  193.             $dump_buffer   .= ':' . $cfgServer['port'];
  194.         }
  195.         $formatted_db_name = (isset($use_backquotes))
  196.                            ? PMA_backquote($db)
  197.                            : '\'' . $db . '\'';
  198.         $dump_buffer       .= $crlf
  199.                            .  '# ' . $strGenTime . ': ' . PMA_localisedDate() . $crlf
  200.                            .  '# ' . $strServerVersion . ': ' . substr(PMA_MYSQL_INT_VERSION, 0, 1) . '.' . substr(PMA_MYSQL_INT_VERSION, 1, 2) . '.' . substr(PMA_MYSQL_INT_VERSION, 3) . $crlf
  201.                            .  '# ' . $strPHPVersion . ': ' . phpversion() . $crlf
  202.                            .  '# ' . $strDatabase . ': ' . $formatted_db_name . $crlf;
  203.  
  204.         $i = 0;
  205.         if (isset($table_select)) {
  206.             $tmp_select = implode($table_select, '|');
  207.             $tmp_select = '|' . $tmp_select . '|';
  208.         }
  209.         while ($i < $num_tables) {
  210.             if (!isset($single)) {
  211.                 $table = mysql_tablename($tables, $i);
  212.             }
  213.             if (isset($tmp_select) && is_int(strpos($tmp_select, '|' . $table . '|')) == FALSE) {
  214.                 $i++;
  215.             } else {
  216.                 $formatted_table_name = (isset($use_backquotes))
  217.                                       ? PMA_backquote($table)
  218.                                       : '\'' . $table . '\'';
  219.                 // If only datas, no need to displays table name
  220.                 if ($what != 'dataonly') {
  221.                     $dump_buffer.= '# --------------------------------------------------------' . $crlf
  222.                                 .  $crlf . '#' . $crlf
  223.                                 .  '# ' . $strTableStructure . ' ' . $formatted_table_name . $crlf
  224.                                 .  '#' . $crlf . $crlf
  225.                                 .  PMA_getTableDef($db, $table, $crlf, $err_url) . ';' . $crlf;
  226.                 }
  227.                 // At least data
  228.                 if (($what == 'data') || ($what == 'dataonly')) {
  229.                     $dump_buffer .= $crlf . '#' . $crlf
  230.                                  .  '# ' . $strDumpingData . ' ' . $formatted_table_name . $crlf
  231.                                  .  '#' . $crlf .$crlf;
  232.                     $tmp_buffer  = '';
  233.                     if (!isset($limit_from) || !isset($limit_to)) {
  234.                         $limit_from = $limit_to = 0;
  235.                     }
  236.                     // loic1: display data if they aren't bufferized
  237.                     if (!isset($zip) && !isset($bzip) && !isset($gzip)) {
  238.                         echo $dump_buffer;
  239.                         $dump_buffer = '';
  240.                     }
  241.                     PMA_getTableContent($db, $table, $limit_from, $limit_to, 'PMA_myHandler', $err_url);
  242.                     $dump_buffer .= $tmp_buffer;
  243.                 } // end if
  244.                 $i++;
  245.             } // end if-else
  246.         } // end while
  247.  
  248.         // staybyte: don't remove, it makes easier to select & copy from
  249.         // browser
  250.         $dump_buffer .= $crlf;
  251.     } // end 'no csv' case
  252.  
  253.     // 'csv' case
  254.     else {
  255.         // Handles the EOL character
  256.         if ($GLOBALS['what'] == 'excel') {
  257.             $add_character = "\015\012";
  258.         } else if (empty($add_character)) {
  259.             $add_character = $GLOBALS['crlf'];
  260.         } else {
  261.             if (get_magic_quotes_gpc()) {
  262.                 $add_character = stripslashes($add_character);
  263.             }
  264.             $add_character = str_replace('\\r', "\015", $add_character);
  265.             $add_character = str_replace('\\n', "\012", $add_character);
  266.             $add_character = str_replace('\\t', "\011", $add_character);
  267.         } // end if
  268.  
  269.         $tmp_buffer = '';
  270.         PMA_getTableCsv($db, $table, $limit_from, $limit_to, $separator, $enclosed, $escaped, 'PMA_myCsvHandler', $err_url);
  271.         $dump_buffer .= $tmp_buffer;
  272.     } // end 'csv case
  273. } // end building the dump
  274.  
  275.  
  276. /**
  277.  * "Displays" the dump...
  278.  */
  279. // 1. as a gzipped file
  280. if (isset($zip) && $zip == 'zip') {
  281.     if (PMA_PHP_INT_VERSION >= 40000 && @function_exists('gzcompress')) {
  282.         if ($what == 'csv' || $what == 'excel') {
  283.             $extbis = '.csv';
  284.         } else {
  285.             $extbis = '.sql';
  286.         }
  287.         $zipfile = new zipfile();
  288.         $zipfile -> addFile($dump_buffer, $filename . $extbis);
  289.         echo $zipfile -> file();
  290.     }
  291. }
  292. // 2. as a bzipped file
  293. else if (isset($bzip) && $bzip == 'bzip') {
  294.     if (PMA_PHP_INT_VERSION >= 40004 && @function_exists('bzcompress')) {
  295.         echo bzcompress($dump_buffer);
  296.     } 
  297. }
  298. // 3. as a gzipped file
  299. else if (isset($gzip) && $gzip == 'gzip') {
  300.     if (PMA_PHP_INT_VERSION >= 40004 && @function_exists('gzencode')) {
  301.         // without the optional parameter level because it bug
  302.         echo gzencode($dump_buffer);
  303.     }
  304. }
  305. // 4. on screen or as a text file
  306. else {
  307.     echo $dump_buffer;
  308. }
  309.  
  310.  
  311. /**
  312.  * Close the html tags and add the footers in dump is displayed on screen
  313.  */
  314. if (empty($asfile)) {
  315.     echo '    </pre>' . "\n";
  316.     echo '</div>' . "\n";
  317.     echo "\n";
  318.     include('./footer.inc.php');
  319. } // end if
  320. ?>
  321.