Exemplo n.º 1
0
function executeSql($sql_file, $database, $table_prefix = '', $isupgrade = false)
{
    $debug = false;
    if (!defined('DB_PREFIX')) {
        define('DB_PREFIX', $table_prefix);
    }
    //	  echo 'start SQL execute';
    global $db;
    $ignored_count = 0;
    $ignore_line = false;
    $results = 0;
    $string = '';
    $result = '';
    $collateSuffix = '';
    $errors = array();
    // prepare for upgrader processing
    if ($isupgrade) {
        zen_create_upgrader_table();
    }
    // only creates table if doesn't already exist
    if (version_compare(PHP_VERSION, 5.4, '>=') || !get_cfg_var('safe_mode')) {
        @set_time_limit(1200);
    }
    $counter = 0;
    $lines = file($sql_file);
    $newline = '';
    $lines_to_keep_together_counter = 0;
    //  $saveline = '';
    foreach ($lines as $line) {
        $line = trim($line);
        //    $line = $saveline . $line;
        $keep_together = 1;
        // count of number of lines to treat as a single command
        // split the line into words ... starts at $param[0] and so on.  Also remove the ';' from end of last param if exists
        $param = explode(" ", substr($line, -1) == ';' ? substr($line, 0, strlen($line) - 1) : $line);
        if (!isset($param[4])) {
            $param[4] = '';
        }
        if (!isset($param[5])) {
            $param[5] = '';
        }
        // The following command checks to see if we're asking for a block of commands to be run at once.
        // Syntax: #NEXT_X_ROWS_AS_ONE_COMMAND:6     for running the next 6 commands together (commands denoted by a ;)
        if (substr($line, 0, 28) == '#NEXT_X_ROWS_AS_ONE_COMMAND:') {
            $keep_together = substr($line, 28);
        }
        if (substr($line, 0, 1) != '#' && substr($line, 0, 1) != '-' && $line != '') {
            //        if ($table_prefix != -1) {
            //echo '*}'.$line.'<br>';
            $line_upper = strtoupper($line);
            switch (true) {
                case substr($line_upper, 0, 21) == 'DROP TABLE IF EXISTS ':
                    $line = 'DROP TABLE IF EXISTS ' . $table_prefix . substr($line, 21);
                    break;
                case substr($line_upper, 0, 11) == 'DROP TABLE ' && $param[2] != 'IF':
                    if (!($checkprivs = zen_check_database_privs('DROP'))) {
                        $result = sprintf(REASON_NO_PRIVILEGES, 'DROP');
                    }
                    if (!zen_table_exists($param[2]) || zen_not_null($result)) {
                        zen_write_to_upgrade_exceptions_table($line, zen_not_null($result) ? $result : sprintf(REASON_TABLE_DOESNT_EXIST, $param[2]), $sql_file);
                        $ignore_line = true;
                        $result = zen_not_null($result) ? $result : sprintf(REASON_TABLE_DOESNT_EXIST, $param[2]);
                        //duplicated here for on-screen error-reporting
                        break;
                    } else {
                        $line = 'DROP TABLE ' . $table_prefix . substr($line, 11);
                    }
                    break;
                case substr($line_upper, 0, 13) == 'CREATE TABLE ':
                    // check to see if table exists
                    $table = strtoupper($param[2] . ' ' . $param[3] . ' ' . $param[4]) == 'IF NOT EXISTS' ? $param[5] : $param[2];
                    $result = zen_table_exists($table);
                    if ($result == true) {
                        $ignore_line = true;
                        if (strtoupper($param[2] . ' ' . $param[3] . ' ' . $param[4]) != 'IF NOT EXISTS') {
                            zen_write_to_upgrade_exceptions_table($line, sprintf(REASON_TABLE_ALREADY_EXISTS, $table), $sql_file);
                            $result = sprintf(REASON_TABLE_ALREADY_EXISTS, $table);
                            //duplicated here for on-screen error-reporting
                        }
                        break;
                    } else {
                        $line = strtoupper($param[2] . ' ' . $param[3] . ' ' . $param[4]) == 'IF NOT EXISTS' ? 'CREATE TABLE IF NOT EXISTS ' . $table_prefix . substr($line, 27) : 'CREATE TABLE ' . $table_prefix . substr($line, 13);
                        $collateSuffix = strtoupper($param[3]) == 'AS' || isset($param[6]) && strtoupper($param[6]) == 'AS' ? '' : ' COLLATE ' . DB_CHARSET . '_general_ci';
                    }
                    break;
                case substr($line_upper, 0, 13) == 'REPLACE INTO ':
                    //check to see if table prefix is going to match
                    if (!($tbl_exists = zen_table_exists($param[2]))) {
                        $result = sprintf(REASON_TABLE_NOT_FOUND, $param[2]) . ' CHECK PREFIXES!';
                    }
                    // check to see if INSERT command may be safely executed for "configuration" or "product_type_layout" tables
                    if ($param[2] == 'configuration' && ($result = zen_check_config_key($line)) or $param[2] == 'product_type_layout' && ($result = zen_check_product_type_layout_key($line)) or $param[2] == 'configuration_group' && ($result = zen_check_cfggroup_key($line)) or !$tbl_exists) {
                        zen_write_to_upgrade_exceptions_table($line, $result, $sql_file);
                        $ignore_line = true;
                        break;
                    } else {
                        $line = 'REPLACE INTO ' . $table_prefix . substr($line, 13);
                    }
                    break;
                case substr($line_upper, 0, 12) == 'INSERT INTO ':
                    //check to see if table prefix is going to match
                    if (!($tbl_exists = zen_table_exists($param[2]))) {
                        $result = sprintf(REASON_TABLE_NOT_FOUND, $param[2]) . ' CHECK PREFIXES!';
                    }
                    // check to see if INSERT command may be safely executed for "configuration" or "product_type_layout" tables
                    if ($param[2] == 'configuration' && ($result = zen_check_config_key($line)) or $param[2] == 'product_type_layout' && ($result = zen_check_product_type_layout_key($line)) or $param[2] == 'configuration_group' && ($result = zen_check_cfggroup_key($line)) or !$tbl_exists) {
                        zen_write_to_upgrade_exceptions_table($line, $result, $sql_file);
                        $ignore_line = true;
                        break;
                    } else {
                        $line = 'INSERT INTO ' . $table_prefix . substr($line, 12);
                    }
                    break;
                case substr($line_upper, 0, 19) == 'INSERT IGNORE INTO ':
                    //check to see if table prefix is going to match
                    if (!($tbl_exists = zen_table_exists($param[3]))) {
                        $result = sprintf(REASON_TABLE_NOT_FOUND, $param[3]) . ' CHECK PREFIXES!';
                        zen_write_to_upgrade_exceptions_table($line, $result, $sql_file);
                        $ignore_line = true;
                        break;
                    } else {
                        $line = 'INSERT IGNORE INTO ' . $table_prefix . substr($line, 19);
                    }
                    break;
                case substr($line_upper, 0, 19) == 'ALTER IGNORE TABLE ':
                    // check to see if ALTER IGNORE command may be safely executed
                    if ($result = zen_check_alter_command($param)) {
                        zen_write_to_upgrade_exceptions_table($line, $result, $sql_file);
                        $ignore_line = true;
                        break;
                    } else {
                        $line = 'ALTER IGNORE TABLE ' . $table_prefix . substr($line, 19);
                    }
                    break;
                case substr($line_upper, 0, 12) == 'ALTER TABLE ':
                    //if (ZC_UPG_DEBUG3==true) echo 'ALTER -- Table check ('.$param[2].')' .'<br>';
                    // check to see if ALTER command may be safely executed
                    if ($result = zen_check_alter_command($param)) {
                        zen_write_to_upgrade_exceptions_table($line, $result, $sql_file);
                        $ignore_line = true;
                        break;
                    } else {
                        $line = 'ALTER TABLE ' . $table_prefix . substr($line, 12);
                    }
                    break;
                case substr($line_upper, 0, 15) == 'TRUNCATE TABLE ':
                    // check to see if TRUNCATE command may be safely executed
                    if (!($tbl_exists = zen_table_exists($param[2]))) {
                        $result = sprintf(REASON_TABLE_NOT_FOUND, $param[3]) . ' CHECK PREFIXES!';
                        zen_write_to_upgrade_exceptions_table($line, $result, $sql_file);
                        $ignore_line = true;
                        break;
                    } else {
                        $line = 'TRUNCATE TABLE ' . $table_prefix . substr($line, 15);
                    }
                    break;
                case substr($line_upper, 0, 13) == 'RENAME TABLE ':
                    // RENAME TABLE command cannot be parsed unless it is split into two lines
                    if (isset($param[3]) && $param[3] != '') {
                        zen_write_to_upgrade_exceptions_table($line, 'RENAME TABLE command must be split onto 2 rows for proper parsing.  Or use phpMyAdmin instead.', $sql_file);
                        $result = sprintf('RENAME TABLE [%s] command must be split onto 2 rows for proper parsing.', $param[2]) . ' CHECK PREFIXES!';
                        $ignore_line = true;
                    }
                    //check to see if table prefix is going to match
                    if (!($tbl_exists = zen_table_exists($param[2]))) {
                        zen_write_to_upgrade_exceptions_table($line, sprintf(REASON_TABLE_NOT_FOUND, $param[2]) . ' CHECK PREFIXES!', $sql_file);
                        $result = sprintf('RENAME TABLE problem: ' . REASON_TABLE_NOT_FOUND, $param[2]) . ' CHECK PREFIXES!';
                        $ignore_line = true;
                        break;
                    } else {
                        $line = 'RENAME TABLE ' . $table_prefix . substr($line, 13);
                    }
                    break;
                case substr($line_upper, 0, 3) == 'TO ':
                    if (!isset($param[1]) || $param[1] == '') {
                        zen_write_to_upgrade_exceptions_table($line, 'RENAME TABLE command must be split onto 2 rows (with TO clause on 2nd line) for proper parsing.  Or use phpMyAdmin instead.', $sql_file);
                        $result = sprintf('RENAME TABLE problem: %s', $param[1]) . ' CHECK PREFIXES!';
                        $ignore_line = true;
                    } else {
                        $line = 'TO ' . $table_prefix . substr($line, 3);
                    }
                    break;
                case substr($line_upper, 0, 7) == 'UPDATE ':
                    //check to see if table prefix is going to match
                    if (!($tbl_exists = zen_table_exists($param[1]))) {
                        zen_write_to_upgrade_exceptions_table($line, sprintf(REASON_TABLE_NOT_FOUND, $param[1]) . ' CHECK PREFIXES!', $sql_file);
                        $result = sprintf(REASON_TABLE_NOT_FOUND, $param[1]) . ' CHECK PREFIXES!';
                        $ignore_line = true;
                        break;
                    } else {
                        $line = 'UPDATE ' . $table_prefix . substr($line, 7);
                    }
                    break;
                case substr($line_upper, 0, 14) == 'UPDATE IGNORE ':
                    //check to see if table prefix is going to match
                    if (!($tbl_exists = zen_table_exists($param[2]))) {
                        zen_write_to_upgrade_exceptions_table($line, sprintf(REASON_TABLE_NOT_FOUND, $param[2]) . ' CHECK PREFIXES!', $sql_file);
                        $result = sprintf(REASON_TABLE_NOT_FOUND, $param[2]) . ' CHECK PREFIXES!';
                        $ignore_line = true;
                        break;
                    } else {
                        $line = 'UPDATE IGNORE ' . $table_prefix . substr($line, 14);
                    }
                    break;
                case substr($line_upper, 0, 12) == 'DELETE FROM ':
                    $line = 'DELETE FROM ' . $table_prefix . substr($line, 12);
                    break;
                case substr($line_upper, 0, 11) == 'DROP INDEX ':
                    // check to see if DROP INDEX command may be safely executed
                    if ($result = zen_drop_index_command($param)) {
                        zen_write_to_upgrade_exceptions_table($line, $result, $sql_file);
                        $ignore_line = true;
                        break;
                    } else {
                        $line = 'DROP INDEX ' . $param[2] . ' ON ' . $table_prefix . $param[4];
                    }
                    break;
                case substr($line_upper, 0, 13) == 'CREATE INDEX ' || strtoupper($param[0]) == 'CREATE' && strtoupper($param[2]) == 'INDEX':
                    // check to see if CREATE INDEX command may be safely executed
                    if ($result = zen_create_index_command($param)) {
                        zen_write_to_upgrade_exceptions_table($line, $result, $sql_file);
                        $ignore_line = true;
                        break;
                    } else {
                        if (strtoupper($param[1]) == 'INDEX') {
                            $line = trim('CREATE INDEX ' . $param[2] . ' ON ' . $table_prefix . implode(' ', array($param[4], $param[5], $param[6], $param[7], $param[8], $param[9], $param[10], $param[11], $param[12], $param[13]))) . ';';
                            // add the ';' back since it was removed from $param at start
                        } else {
                            $line = trim('CREATE ' . $param[1] . ' INDEX ' . $param[3] . ' ON ' . $table_prefix . implode(' ', array($param[5], $param[6], $param[7], $param[8], $param[9], $param[10], $param[11], $param[12], $param[13])));
                            // add the ';' back since it was removed from $param at start
                        }
                    }
                    break;
                case substr($line_upper, 0, 7) == 'SELECT ' && substr_count($line, 'FROM ') > 0:
                    $line = str_replace('FROM ', 'FROM ' . $table_prefix, $line);
                    break;
                case substr($line_upper, 0, 10) == 'LEFT JOIN ':
                    $line = 'LEFT JOIN ' . $table_prefix . substr($line, 10);
                    break;
                case substr($line_upper, 0, 5) == 'FROM ':
                    if (substr_count($line, ',') > 0) {
                        // contains FROM and a comma, thus must parse for multiple tablenames
                        $tbl_list = explode(',', substr($line, 5));
                        $line = 'FROM ';
                        foreach ($tbl_list as $val) {
                            $line .= $table_prefix . trim($val) . ',';
                            // add prefix and comma
                        }
                        //end foreach
                        if (substr($line, -1) == ',') {
                            $line = substr($line, 0, strlen($line) - 1);
                        }
                        // remove trailing ','
                    } else {
                        //didn't have a comma, but starts with "FROM ", so insert table prefix
                        $line = str_replace('FROM ', 'FROM ' . $table_prefix, $line);
                    }
                    //endif substr_count(,)
                    break;
                default:
                    break;
            }
            //end switch
            //        } // endif $table_prefix
            $newline .= $line . ' ';
            if (substr($line, -1) == ';') {
                //found a semicolon, so treat it as a full command, incrementing counter of rows to process at once
                if (substr($newline, -1) == ' ') {
                    $newline = substr($newline, 0, strlen($newline) - 1);
                }
                $lines_to_keep_together_counter++;
                if ($lines_to_keep_together_counter == $keep_together) {
                    // if all grouped rows have been loaded, go to execute.
                    $complete_line = true;
                    $lines_to_keep_together_counter = 0;
                    if ($collateSuffix != '' && @mysql_get_server_info() >= '4.1' && (!defined('IGNORE_DB_CHARSET') || defined('IGNORE_DB_CHARSET') && IGNORE_DB_CHARSET != FALSE)) {
                        $newline = rtrim($newline, ';') . $collateSuffix . ';';
                        $collateSuffix = '';
                    }
                } else {
                    $complete_line = false;
                }
            }
            //endif found ';'
            if ($complete_line) {
                if ($debug == true) {
                    echo (!$ignore_line ? '<br /><strong>About to execute.</strong>' : '<strong>Ignoring statement. This command WILL NOT be executed.</strong>') . '<br />Debug info:<br />$ line=' . $line . '<br />$ complete_line=' . $complete_line . '<br>$ keep_together=' . $keep_together . '<br />SQL=' . $newline . '<br /><br />';
                }
                if (get_magic_quotes_runtime() > 0) {
                    $newline = stripslashes($newline);
                }
                $output = trim(str_replace(';', '', $newline)) != '' && !$ignore_line ? $db->Execute($newline) : '';
                $results++;
                $string .= $newline . '<br />';
                $return_output[] = $output;
                if (zen_not_null($result) && !zen_check_exceptions($result, $line)) {
                    $errors[] = $result;
                }
                // reset var's
                $newline = '';
                $keep_together = 1;
                $complete_line = false;
                if ($ignore_line && !zen_check_exceptions($result, $line)) {
                    $ignored_count++;
                }
                $ignore_line = false;
                // show progress bar
                global $zc_show_progress;
                if ($zc_show_progress == 'yes') {
                    $counter++;
                    if ($counter / 5 == (int) ($counter / 5)) {
                        echo '~ ';
                    }
                    if ($counter > 200) {
                        echo '<br /><br />';
                        $counter = 0;
                    }
                    if (function_exists('ob_flush')) {
                        @ob_flush();
                    }
                    @flush();
                }
            }
            //endif $complete_line
        }
        //endif ! # or -
    }
    // end foreach $lines
    return array('queries' => $results, 'string' => $string, 'output' => $return_output, 'ignored' => $ignored_count, 'errors' => $errors);
}
Exemplo n.º 2
0
function executeSql($lines, $database, $table_prefix = '')
{
    if (!get_cfg_var('safe_mode')) {
        @set_time_limit(1200);
    }
    global $db, $debug;
    $sql_file = 'SQLPATCH';
    $newline = '';
    $saveline = '';
    $ignored_count = 0;
    $return_output = array();
    $errors = array();
    foreach ($lines as $line) {
        if ($_GET['debug'] == 'ON') {
            echo $line . '<br />';
        }
        $line = trim($line);
        $line = str_replace('`', '', $line);
        //remove backquotes
        $line = $saveline . $line;
        $keep_together = 1;
        // count of number of lines to treat as a single command
        // split the line into words ... starts at $param[0] and so on.  Also remove the ';' from end of last param if exists
        $param = explode(" ", substr($line, -1) == ';' ? substr($line, 0, strlen($line) - 1) : $line);
        // The following command checks to see if we're asking for a block of commands to be run at once.
        // Syntax: #NEXT_X_ROWS_AS_ONE_COMMAND:6     for running the next 6 commands together (commands denoted by a ;)
        if (substr($line, 0, 28) == '#NEXT_X_ROWS_AS_ONE_COMMAND:') {
            $keep_together = substr($line, 28);
        }
        if (substr($line, 0, 1) != '#' && substr($line, 0, 1) != '-' && $line != '') {
            //        if ($table_prefix != -1) {
            //echo '*}'.$line.'<br>';
            $line_upper = strtoupper($line);
            switch (true) {
                case substr($line_upper, 0, 21) == 'DROP TABLE IF EXISTS ':
                    //            if (!$checkprivs = zen_check_database_privs('DROP')) return sprintf(REASON_NO_PRIVILEGES,'DROP');
                    $line = 'DROP TABLE IF EXISTS ' . $table_prefix . substr($line, 21);
                    break;
                case substr($line_upper, 0, 11) == 'DROP TABLE ' && $param[2] != 'IF':
                    if (!($checkprivs = zen_check_database_privs('DROP'))) {
                        $result = sprintf(REASON_NO_PRIVILEGES, 'DROP');
                    }
                    if (!zen_table_exists($param[2]) || zen_not_null($result)) {
                        zen_write_to_upgrade_exceptions_table($line, zen_not_null($result) ? $result : sprintf(REASON_TABLE_DOESNT_EXIST, $param[2]), $sql_file);
                        $ignore_line = true;
                        $result = zen_not_null($result) ? $result : sprintf(REASON_TABLE_DOESNT_EXIST, $param[2]);
                        //duplicated here for on-screen error-reporting
                        break;
                    } else {
                        $line = 'DROP TABLE ' . $table_prefix . substr($line, 11);
                    }
                    break;
                case substr($line_upper, 0, 13) == 'CREATE TABLE ':
                    // check to see if table exists
                    $table = strtoupper($param[2] . ' ' . $param[3] . ' ' . $param[4]) == 'IF NOT EXISTS' ? $param[5] : $param[2];
                    $result = zen_table_exists($table);
                    if ($result == true) {
                        zen_write_to_upgrade_exceptions_table($line, sprintf(REASON_TABLE_ALREADY_EXISTS, $table), $sql_file);
                        $ignore_line = true;
                        $result = sprintf(REASON_TABLE_ALREADY_EXISTS, $table);
                        //duplicated here for on-screen error-reporting
                        break;
                    } else {
                        $line = strtoupper($param[2] . ' ' . $param[3] . ' ' . $param[4]) == 'IF NOT EXISTS' ? 'CREATE TABLE IF NOT EXISTS ' . $table_prefix . substr($line, 27) : 'CREATE TABLE ' . $table_prefix . substr($line, 13);
                    }
                    break;
                case substr($line_upper, 0, 15) == 'TRUNCATE TABLE ':
                    // check to see if TRUNCATE command may be safely executed
                    if (!($tbl_exists = zen_table_exists($param[2]))) {
                        $result = sprintf(REASON_TABLE_NOT_FOUND, $param[2]) . ' CHECK PREFIXES!' . $param[2];
                        zen_write_to_upgrade_exceptions_table($line, $result, $sql_file);
                        $ignore_line = true;
                        break;
                    } else {
                        $line = 'TRUNCATE TABLE ' . $table_prefix . substr($line, 15);
                    }
                    break;
                case substr($line_upper, 0, 13) == 'REPLACE INTO ':
                    //check to see if table prefix is going to match
                    if (!($tbl_exists = zen_table_exists($param[2]))) {
                        $result = sprintf(REASON_TABLE_NOT_FOUND, $param[2]) . ' CHECK PREFIXES!';
                    }
                    // check to see if INSERT command may be safely executed for "configuration" or "product_type_layout" tables
                    if ($param[2] == 'configuration' && ($result = zen_check_config_key($line)) or $param[2] == 'product_type_layout' && ($result = zen_check_product_type_layout_key($line)) or !$tbl_exists) {
                        zen_write_to_upgrade_exceptions_table($line, $result, $sql_file);
                        $ignore_line = true;
                        break;
                    } else {
                        $line = 'REPLACE INTO ' . $table_prefix . substr($line, 13);
                    }
                    break;
                case substr($line_upper, 0, 12) == 'INSERT INTO ':
                    //check to see if table prefix is going to match
                    if (!($tbl_exists = zen_table_exists($param[2]))) {
                        $result = sprintf(REASON_TABLE_NOT_FOUND, $param[2]) . ' CHECK PREFIXES!';
                    }
                    // check to see if INSERT command may be safely executed for "configuration" or "product_type_layout" tables
                    if ($param[2] == 'configuration' && ($result = zen_check_config_key($line)) or $param[2] == 'product_type_layout' && ($result = zen_check_product_type_layout_key($line)) or !$tbl_exists) {
                        zen_write_to_upgrade_exceptions_table($line, $result, $sql_file);
                        $ignore_line = true;
                        break;
                    } else {
                        $line = 'INSERT INTO ' . $table_prefix . substr($line, 12);
                    }
                    break;
                case substr($line_upper, 0, 19) == 'INSERT IGNORE INTO ':
                    //check to see if table prefix is going to match
                    if (!($tbl_exists = zen_table_exists($param[3]))) {
                        $result = sprintf(REASON_TABLE_NOT_FOUND, $param[3]) . ' CHECK PREFIXES!';
                        zen_write_to_upgrade_exceptions_table($line, $result, $sql_file);
                        $ignore_line = true;
                        break;
                    } else {
                        $line = 'INSERT IGNORE INTO ' . $table_prefix . substr($line, 19);
                    }
                    break;
                case substr($line_upper, 0, 12) == 'ALTER TABLE ':
                    // check to see if ALTER command may be safely executed
                    if ($result = zen_check_alter_command($param)) {
                        zen_write_to_upgrade_exceptions_table($line, $result, $sql_file);
                        $ignore_line = true;
                        break;
                    } else {
                        $line = 'ALTER TABLE ' . $table_prefix . substr($line, 12);
                    }
                    break;
                case substr($line_upper, 0, 13) == 'RENAME TABLE ':
                    // RENAME TABLE command cannot be parsed to insert table prefixes, so skip if zen is using prefixes
                    if (zen_not_null(DB_PREFIX)) {
                        zen_write_to_upgrade_exceptions_table($line, 'RENAME TABLE command not supported by upgrader. Please use phpMyAdmin instead.', $sql_file);
                        $messageStack->add('RENAME TABLE command not supported by upgrader. Please use phpMyAdmin instead.', 'caution');
                        $ignore_line = true;
                    }
                    break;
                case substr($line_upper, 0, 7) == 'UPDATE ':
                    //check to see if table prefix is going to match
                    if (!($tbl_exists = zen_table_exists($param[1]))) {
                        zen_write_to_upgrade_exceptions_table($line, sprintf(REASON_TABLE_NOT_FOUND, $param[1]) . ' CHECK PREFIXES!', $sql_file);
                        $result = sprintf(REASON_TABLE_NOT_FOUND, $param[1]) . ' CHECK PREFIXES!';
                        $ignore_line = true;
                        break;
                    } else {
                        $line = 'UPDATE ' . $table_prefix . substr($line, 7);
                    }
                    break;
                case substr($line_upper, 0, 14) == 'UPDATE IGNORE ':
                    //check to see if table prefix is going to match
                    if (!($tbl_exists = zen_table_exists($param[2]))) {
                        zen_write_to_upgrade_exceptions_table($line, sprintf(REASON_TABLE_NOT_FOUND, $param[2]) . ' CHECK PREFIXES!', $sql_file);
                        $result = sprintf(REASON_TABLE_NOT_FOUND, $param[2]) . ' CHECK PREFIXES!';
                        $ignore_line = true;
                        break;
                    } else {
                        $line = 'UPDATE IGNORE ' . $table_prefix . substr($line, 14);
                    }
                    break;
                case substr($line_upper, 0, 12) == 'DELETE FROM ':
                    $line = 'DELETE FROM ' . $table_prefix . substr($line, 12);
                    break;
                case substr($line_upper, 0, 11) == 'DROP INDEX ':
                    // check to see if DROP INDEX command may be safely executed
                    if ($result = zen_drop_index_command($param)) {
                        zen_write_to_upgrade_exceptions_table($line, $result, $sql_file);
                        $ignore_line = true;
                        break;
                    } else {
                        $line = 'DROP INDEX ' . $param[2] . ' ON ' . $table_prefix . $param[4];
                    }
                    break;
                case substr($line_upper, 0, 13) == 'CREATE INDEX ' || strtoupper($param[0]) == 'CREATE' && strtoupper($param[2]) == 'INDEX':
                    // check to see if CREATE INDEX command may be safely executed
                    if ($result = zen_create_index_command($param)) {
                        zen_write_to_upgrade_exceptions_table($line, $result, $sql_file);
                        $ignore_line = true;
                        break;
                    } else {
                        if (strtoupper($param[1]) == 'INDEX') {
                            $line = trim('CREATE INDEX ' . $param[2] . ' ON ' . $table_prefix . implode(' ', array($param[4], $param[5], $param[6], $param[7], $param[8], $param[9], $param[10], $param[11], $param[12], $param[13]))) . ';';
                            // add the ';' back since it was removed from $param at start
                        } else {
                            $line = trim('CREATE ' . $param[1] . ' INDEX ' . $param[3] . ' ON ' . $table_prefix . implode(' ', array($param[5], $param[6], $param[7], $param[8], $param[9], $param[10], $param[11], $param[12], $param[13])));
                            // add the ';' back since it was removed from $param at start
                        }
                    }
                    break;
                case substr($line_upper, 0, 8) == 'SELECT (' && substr_count($line, 'FROM ') > 0:
                    $line = str_replace('FROM ', 'FROM ' . $table_prefix, $line);
                    break;
                case substr($line_upper, 0, 10) == 'LEFT JOIN ':
                    $line = 'LEFT JOIN ' . $table_prefix . substr($line, 10);
                    break;
                case substr($line_upper, 0, 5) == 'FROM ':
                    if (substr_count($line, ',') > 0) {
                        // contains FROM and a comma, thus must parse for multiple tablenames
                        $tbl_list = explode(',', substr($line, 5));
                        $line = 'FROM ';
                        foreach ($tbl_list as $val) {
                            $line .= $table_prefix . trim($val) . ',';
                            // add prefix and comma
                        }
                        //end foreach
                        if (substr($line, -1) == ',') {
                            $line = substr($line, 0, strlen($line) - 1);
                        }
                        // remove trailing ','
                    } else {
                        //didn't have a comma, but starts with "FROM ", so insert table prefix
                        $line = str_replace('FROM ', 'FROM ' . $table_prefix, $line);
                    }
                    //endif substr_count(,)
                    break;
                default:
                    break;
            }
            //end switch
            //        } // endif $table_prefix
            $newline .= $line . ' ';
            if (substr($line, -1) == ';') {
                //found a semicolon, so treat it as a full command, incrementing counter of rows to process at once
                if (substr($newline, -1) == ' ') {
                    $newline = substr($newline, 0, strlen($newline) - 1);
                }
                $lines_to_keep_together_counter++;
                if ($lines_to_keep_together_counter == $keep_together) {
                    // if all grouped rows have been loaded, go to execute.
                    $complete_line = true;
                    $lines_to_keep_together_counter = 0;
                } else {
                    $complete_line = false;
                }
            }
            //endif found ';'
            if ($complete_line) {
                if ($debug == true) {
                    echo (!$ignore_line ? '<br />About to execute.' : 'Ignoring statement. This command WILL NOT be executed.') . '<br />Debug info:<br>$ line=' . $line . '<br>$ complete_line=' . $complete_line . '<br>$ keep_together=' . $keep_together . '<br>SQL=' . $newline . '<br><br>';
                }
                if (get_magic_quotes_runtime() > 0 && $keepslashes != true) {
                    $newline = stripslashes($newline);
                }
                if (trim(str_replace(';', '', $newline)) != '' && !$ignore_line) {
                    $output = $db->Execute($newline);
                }
                $results++;
                $string .= $newline . '<br />';
                $return_output[] = $output;
                if (zen_not_null($result)) {
                    $errors[] = $result;
                }
                // reset var's
                $newline = '';
                $keep_together = 1;
                $complete_line = false;
                if ($ignore_line) {
                    $ignored_count++;
                }
                $ignore_line = false;
                // show progress bar
                global $zc_show_progress;
                if ($zc_show_progress == 'yes') {
                    $counter++;
                    if ($counter / 5 == (int) ($counter / 5)) {
                        echo '~ ';
                    }
                    if ($counter > 200) {
                        echo '<br /><br />';
                        $counter = 0;
                    }
                    @ob_flush();
                    @flush();
                }
            }
            //endif $complete_line
        }
        //endif ! # or -
    }
    // end foreach $lines
    return array('queries' => $results, 'string' => $string, 'output' => $return_output, 'ignored' => $ignored_count, 'errors' => $errors);
}