📄 general.php
字号:
<?php
/**
* general functions used by installer *
* @package Installer
* @access private
* @copyright Copyright 2003-2006 Zen Cart Development Team
* @copyright Portions Copyright 2003 osCommerce
* @license http://www.zen-cart.com/license/2_0.txt GNU Public License V2.0
* @version $Id: general.php 3447 2006-04-17 20:10:18Z drbyte $
*/
if (!defined('TABLE_UPGRADE_EXCEPTIONS')) define('TABLE_UPGRADE_EXCEPTIONS','upgrade_exceptions');
function zen_not_null($value) {
if (is_array($value)) {
if (sizeof($value) > 0) {
return true;
} else {
return false;
}
} else {
if (($value != '') && (strtolower($value) != 'null') && (strlen(trim($value)) > 0)) {
return true;
} else {
return false;
}
}
}
function zen_output_string($string, $translate = false, $protected = false) {
if ($protected == true) {
return htmlspecialchars($string);
} else {
if ($translate == false) {
return zen_parse_input_field_data($string, array('"' => '"'));
} else {
return zen_parse_input_field_data($string, $translate);
}
}
}
function zen_parse_input_field_data($data, $parse) {
return strtr(trim($data), $parse);
}
function setInputValue($input, $constant, $default) {
if (isset($input)) {
define($constant, $input);
} else {
define($constant, $default);
}
}
function setRadioChecked($input, $constant, $default) {
if ($input == '') {
$input = $default;
}
if ($input == 'true') {
define($constant . '_FALSE', '');
define($constant . '_TRUE', 'checked="checked" ');
} else {
define($constant . '_FALSE', 'checked="checked" ');
define($constant . '_TRUE', '');
}
}
function setSelected($input, $selected) {
if ($input == $selected) {
return ' selected="selected"';
}
}
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='';
$errors=array();
// prepare for upgrader processing
if ($isupgrade) zen_create_upgrader_table(); // only creates table if doesn't already exist
if (!get_cfg_var('safe_mode')) {
@set_time_limit(1200);
}
$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);
// 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) {
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, 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, 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);
$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 /><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 />';
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -