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 / read_dump.php < prev    next >
PHP Script  |  2002-01-06  |  12KB  |  374 lines

  1. <?php
  2. /* $Id: read_dump.php,v 1.14 2001/12/11 12:11:14 loic1 Exp $ */
  3.  
  4.  
  5. /**
  6.  * Removes comment lines and splits up large sql files into individual queries
  7.  *
  8.  * Last revision: September 23, 2001 - gandon
  9.  *
  10.  * @param   array    the splitted sql commands
  11.  * @param   string   the sql commands
  12.  * @param   integer  the MySQL release number (because certains php3 versions
  13.  *                   can't get the value of a constant from within a function)
  14.  *
  15.  * @return  boolean  always true
  16.  *
  17.  * @access  public
  18.  */
  19. function PMA_splitSqlFile(&$ret, $sql, $release)
  20. {
  21.     $sql          = trim($sql);
  22.     $sql_len      = strlen($sql);
  23.     $char         = '';
  24.     $string_start = '';
  25.     $in_string    = FALSE;
  26.  
  27.     for ($i = 0; $i < $sql_len; ++$i) {
  28.         $char = $sql[$i];
  29.  
  30.         // We are in a string, check for not escaped end of strings except for
  31.         // backquotes that can't be escaped
  32.         if ($in_string) {
  33.             for (;;) {
  34.                 $i         = strpos($sql, $string_start, $i);
  35.                 // No end of string found -> add the current substring to the
  36.                 // returned array
  37.                 if (!$i) {
  38.                     $ret[] = $sql;
  39.                     return TRUE;
  40.                 }
  41.                 // Backquotes or no backslashes before quotes: it's indeed the
  42.                 // end of the string -> exit the loop
  43.                 else if ($string_start == '`' || $sql[$i-1] != '\\') {
  44.                     $string_start      = '';
  45.                     $in_string         = FALSE;
  46.                     break;
  47.                 }
  48.                 // one or more Backslashes before the presumed end of string...
  49.                 else {
  50.                     // ... first checks for escaped backslashes
  51.                     $j                     = 2;
  52.                     $escaped_backslash     = FALSE;
  53.                     while ($i-$j > 0 && $sql[$i-$j] == '\\') {
  54.                         $escaped_backslash = !$escaped_backslash;
  55.                         $j++;
  56.                     }
  57.                     // ... if escaped backslashes: it's really the end of the
  58.                     // string -> exit the loop
  59.                     if ($escaped_backslash) {
  60.                         $string_start  = '';
  61.                         $in_string     = FALSE;
  62.                         break;
  63.                     }
  64.                     // ... else loop
  65.                     else {
  66.                         $i++;
  67.                     }
  68.                 } // end if...elseif...else
  69.             } // end for
  70.         } // end if (in string)
  71.  
  72.         // We are not in a string, first check for delimiter...
  73.         else if ($char == ';') {
  74.             // if delimiter found, add the parsed part to the returned array
  75.             $ret[]      = substr($sql, 0, $i);
  76.             $sql        = ltrim(substr($sql, min($i + 1, $sql_len)));
  77.             $sql_len    = strlen($sql);
  78.             if ($sql_len) {
  79.                 $i      = -1;
  80.             } else {
  81.                 // The submited statement(s) end(s) here
  82.                 return TRUE;
  83.             }
  84.         } // end else if (is delimiter)
  85.  
  86.         // ... then check for start of a string,...
  87.         else if (($char == '"') || ($char == '\'') || ($char == '`')) {
  88.             $in_string    = TRUE;
  89.             $string_start = $char;
  90.         } // end else if (is start of string)
  91.  
  92.         // ... for start of a comment (and remove this comment if found)...
  93.         else if ($char == '#'
  94.                  || ($char == ' ' && $i > 1 && $sql[$i-2] . $sql[$i-1] == '--')) {
  95.             // starting position of the comment depends on the comment type
  96.             $start_of_comment = (($sql[$i] == '#') ? $i : $i-2);
  97.             // if no "\n" exits in the remaining string, checks for "\r"
  98.             // (Mac eol style)
  99.             $end_of_comment   = (strpos(' ' . $sql, "\012", $i+2))
  100.                               ? strpos(' ' . $sql, "\012", $i+2)
  101.                               : strpos(' ' . $sql, "\015", $i+2);
  102.             if (!$end_of_comment) {
  103.                 // no eol found after '#', add the parsed part to the returned
  104.                 // array if required and exit
  105.                 if ($start_of_comment > 0) {
  106.                     $ret[]    = trim(substr($sql, 0, $start_of_comment));
  107.                 }
  108.                 return TRUE;
  109.             } else {
  110.                 $sql          = substr($sql, 0, $start_of_comment)
  111.                               . ltrim(substr($sql, $end_of_comment));
  112.                 $sql_len      = strlen($sql);
  113.                 $i--;
  114.             } // end if...else
  115.         } // end else if (is comment)
  116.  
  117.         // ... and finally disactivate the "/*!...*/" syntax if MySQL < 3.22.07
  118.         else if ($release < 32270
  119.                  && ($char == '!' && $i > 1  && $sql[$i-2] . $sql[$i-1] == '/*')) {
  120.             $sql[$i] = ' ';
  121.         } // end else if
  122.  
  123.         // loic1: send a fake header to bypass browser timeout
  124.         header('Expires: 0');
  125.     } // end for
  126.  
  127.     // add any rest to the returned array
  128.     if (!empty($sql) && ereg('[^[:space:]]+', $sql)) {
  129.         $ret[] = $sql;
  130.     }
  131.  
  132.     return TRUE;
  133. } // end of the 'PMA_splitSqlFile()' function
  134.  
  135.  
  136. if (!function_exists('is_uploaded_file')) {
  137.     /**
  138.      * Emulates the 'is_uploaded_file()' function for old php versions.
  139.      * Grabbed at the php manual:
  140.      *     http://www.php.net/manual/en/features.file-upload.php
  141.      *
  142.      * @param   string    the name of the file to check
  143.      *
  144.      * @return  boolean   wether the file has been uploaded or not
  145.      *
  146.      * @access  public
  147.      */
  148.     function is_uploaded_file($filename) {
  149.         if (!$tmp_file = @get_cfg_var('upload_tmp_dir')) {
  150.             $tmp_file = tempnam('','');
  151.             $deleted  = @unlink($tmp_file);
  152.             $tmp_file = dirname($tmp_file);
  153.         }
  154.         $tmp_file     .= '/' . basename($filename);
  155.  
  156.         // User might have trailing slash in php.ini...
  157.         return (ereg_replace('/+', '/', $tmp_file) == $filename);
  158.     } // end of the 'is_uploaded_file()' emulated function
  159. } // end if
  160.  
  161.  
  162.  
  163. /**
  164.  * Increases the max. allowed time to run a script
  165.  */
  166. @set_time_limit($cfgExecTimeLimit);
  167.  
  168.  
  169. /**
  170.  * Gets some core libraries
  171.  */
  172. require('./libraries/grab_globals.lib.php');
  173. require('./libraries/common.lib.php');
  174.  
  175.  
  176. /**
  177.  * Defines the url to return to in case of error in a sql statement
  178.  */
  179. if (!isset($goto)
  180.     || ($goto != 'db_details.php' && $goto != 'tbl_properties.php')) {
  181.     $goto = 'db_details.php';
  182. }
  183. $err_url  = $goto
  184.           . '?lang=' . $lang
  185.           . '&server=' . $server
  186.           . '&db=' . urlencode($db)
  187.           . (($goto == 'tbl_properties.php') ? '&table=' . urlencode($table) : '');
  188.  
  189.  
  190. /**
  191.  * Set up default values for some variables and 
  192.  */
  193. $view_bookmark = 0;
  194. $sql_bookmark  = isset($sql_bookmark) ? $sql_bookmark : '';
  195. $sql_query     = isset($sql_query)    ? $sql_query    : '';
  196. $sql_file      = !empty($sql_file)    ? $sql_file     : 'none';
  197.  
  198.  
  199. /**
  200.  * Bookmark Support: get a query back from bookmark if required
  201.  */
  202. if (!empty($id_bookmark)) {
  203.     include('./libraries/bookmark.lib.php');
  204.     switch($action_bookmark) {
  205.         case 0: // bookmarked query that have to be run
  206.             $sql_query = PMA_queryBookmarks($db, $cfgBookmark, $id_bookmark);
  207.             break;
  208.         case 1: // bookmarked query that have to be displayed
  209.             $sql_query = PMA_queryBookmarks($db, $cfgBookmark, $id_bookmark);
  210.             $view_bookmark = 1;
  211.             break;
  212.         case 2: // bookmarked query that have to be deleted
  213.             $sql_query = PMA_deleteBookmarks($db, $cfgBookmark, $id_bookmark);
  214.             break;
  215.     }
  216. } // end if
  217.  
  218.  
  219. /**
  220.  * Prepares the sql query
  221.  */
  222. // Gets the query from a file if required 
  223. if ($sql_file != 'none') {
  224.     if (file_exists($sql_file) && is_uploaded_file($sql_file)) {
  225.         $sql_query = fread(fopen($sql_file, 'r'), filesize($sql_file));
  226.         if (get_magic_quotes_runtime() == 1) {
  227.             $sql_query = stripslashes($sql_query);
  228.         }
  229.     }
  230. }
  231. else if (empty($id_bookmark) && get_magic_quotes_gpc() == 1) {
  232.     $sql_query = stripslashes($sql_query);
  233. }
  234. $sql_query = trim($sql_query);
  235. // $sql_query come from the query textarea, if it's a reposted query gets its
  236. // 'true' value
  237. if (!empty($prev_sql_query)) {
  238.     $prev_sql_query = urldecode($prev_sql_query);
  239.     if ($sql_query == trim(htmlspecialchars($prev_sql_query))) {
  240.         $sql_query  = $prev_sql_query;
  241.     }
  242. }
  243.  
  244. // Drop database is not allowed -> ensure the query can be run
  245. if (!$cfgAllowUserDropDatabase
  246.     && eregi('DROP[[:space:]]+(IF EXISTS[[:space:]]+)?DATABASE ', $sql_query)) {
  247.     // Checks if the user is a Superuser
  248.     // TODO: set a global variable with this information
  249.     // loic1: optimized query
  250.     $result = @mysql_query('USE mysql');
  251.     if (mysql_error()) {
  252.         include('./header.inc.php');
  253.         PMA_mysqlDie($strNoDropDatabases, '', '', $err_url);
  254.     }
  255. }
  256. define('PMA_CHK_DROP', 1);
  257.  
  258.  
  259. /**
  260.  * Executes the query
  261.  */
  262. if ($sql_query != '') {
  263.     $pieces       = array();
  264.     PMA_splitSqlFile($pieces, $sql_query, PMA_MYSQL_INT_VERSION);
  265.     $pieces_count = count($pieces);
  266.  
  267.     // Copy of the cleaned sql statement for display purpose only (see near the
  268.     // beginning of "db_details.php" & "tbl_properties.php")
  269.     if ($sql_file != 'none' && $pieces_count > 10) {
  270.          // Be nice with bandwidth...
  271.         $sql_query_cpy = $sql_query = '';
  272.     } else {
  273.         $sql_query_cpy = implode(";\n", $pieces) . ';';
  274.     }
  275.  
  276.     // Only one query to run
  277.     if ($pieces_count == 1 && !empty($pieces[0]) && $view_bookmark == 0) {
  278.         // sql.php will stripslash the query if get_magic_quotes_gpc
  279.         if (get_magic_quotes_gpc() == 1) {
  280.             $sql_query = addslashes($pieces[0]);
  281.         } else {
  282.             $sql_query = $pieces[0];
  283.         }
  284.         if (eregi('^(DROP|CREATE)[[:space:]]+(IF EXISTS[[:space:]]+)?(TABLE|DATABASE)[[:space:]]+(.+)', $sql_query)) {
  285.             $reload = 1;
  286.         }
  287.         include('./sql.php');
  288.         exit();
  289.     }
  290.  
  291.     // Runs multiple queries
  292.     else if (mysql_select_db($db)) {
  293.         for ($i = 0; $i < $pieces_count; $i++) {
  294.             $a_sql_query = $pieces[$i];
  295.             $result = mysql_query($a_sql_query);
  296.             if ($result == FALSE) { // readdump failed
  297.                 $my_die = $a_sql_query;
  298.                 break;
  299.             }
  300.             if (!isset($reload) && eregi('^(DROP|CREATE)[[:space:]]+(IF EXISTS[[:space:]]+)?(TABLE|DATABASE)[[:space:]]+(.+)', $a_sql_query)) {
  301.                 $reload = 1;
  302.             }
  303.         } // end for
  304.     } // end else if
  305.     unset($pieces);
  306. } // end if
  307.  
  308.  
  309. /**
  310.  * MySQL error
  311.  */
  312. if (isset($my_die)) {
  313.     $js_to_run = 'functions.js';
  314.     include('./header.inc.php');
  315.     PMA_mysqlDie('', $my_die, '', $err_url);
  316. }
  317.  
  318.  
  319. /**
  320.  * Go back to the calling script
  321.  */
  322. // Checks for a valid target script
  323. if (isset($table) && $table == '') {
  324.     unset($table);
  325. }
  326. if (isset($db) && $db == '') {
  327.     unset($db);
  328. }
  329. $is_db = $is_table = FALSE;
  330. if ($goto == 'tbl_properties.php') {
  331.     if (!isset($table)) {
  332.         $goto     = 'db_details.php';
  333.     } else {
  334.         $is_table = @mysql_query('SHOW TABLES LIKE \'' . PMA_sqlAddslashes($table, TRUE) . '\'');
  335.         if (!@mysql_numrows($is_table)) {
  336.             $goto = 'db_details.php';
  337.             unset($table);
  338.         }
  339.     } // end if... else...
  340. }
  341. if ($goto == 'db_details.php') {
  342.     if (isset($table)) {
  343.         unset($table);
  344.     }
  345.     if (!isset($db)) {
  346.         $goto     = 'main.php';
  347.     } else {
  348.         $is_db    = @mysql_select_db($db);
  349.         if (!$is_db) {
  350.             $goto = 'main.php';
  351.             unset($db);
  352.         }
  353.     } // end if... else...
  354. }
  355. // Defines the message to be displayed
  356. if (!empty($id_bookmark) && $action_bookmark == 2) {
  357.     $message   = $strBookmarkDeleted;
  358. } else if (!isset($sql_query_cpy)) {
  359.     $message   = $strNoQuery;
  360. } else if ($sql_query_cpy == '') {
  361.     $message   = "$strSuccess :<br />$strTheContent ($pieces_count $strInstructions) ";
  362. } else {
  363.     $message   = $strSuccess;
  364. }
  365. // Loads to target script
  366. if ($goto == 'db_details.php' || $goto == 'tbl_properties.php') {
  367.     $js_to_run = 'functions.js';
  368. }
  369. if ($goto != 'main.php') {
  370.     include('./header.inc.php');
  371. }
  372. require('./' . $goto);
  373. ?>
  374.