From b0693e1b6ee1f65dcb09dae6960dc3abe35c75e4 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Tue, 3 Nov 2009 21:59:10 +0000 Subject: [PATCH] Fixes for fix_menu.php and SQL_ALTER_TABLE() --- inc/db/lib-mysql3.php | 149 ++++++++++++++++----------------- inc/extensions-functions.php | 19 +++-- inc/extensions/ext-network.php | 6 +- inc/filters.php | 4 +- inc/fix_menu.php | 18 ++-- 5 files changed, 100 insertions(+), 96 deletions(-) diff --git a/inc/db/lib-mysql3.php b/inc/db/lib-mysql3.php index a0f4499b14..c597cafbbe 100644 --- a/inc/db/lib-mysql3.php +++ b/inc/db/lib-mysql3.php @@ -78,12 +78,12 @@ function SQL_QUERY ($sqlString, $F, $L) { $querytimeBefore = array_sum(explode(' ', microtime())); // Run SQL command - //* DEBUG: */ print($sqlString . '
'); + ///* DEBUG: */ print($sqlString . '
'); $result = mysql_query($sqlString, SQL_GET_LINK()) or addFatalMessage(__FUNCTION__, __LINE__, $F . ' (' . $L . '):' . mysql_error() . '
Query string:
' . $sqlString); - //* DEBUG: */ print(__LINE__ . ': numRows=' . SQL_NUMROWS($result) . ',affected=' . SQL_AFFECTEDROWS() . "
"); + //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'sql=' . $sqlString . ',numRows=' . SQL_NUMROWS($result) . ',affected=' . SQL_AFFECTEDROWS()); // Ending time $querytimeAfter = array_sum(explode(' ', microtime())); @@ -272,7 +272,6 @@ function SQL_QUERY_ESC ($qstring, $data, $F, $L, $run=true, $strip=true, $secure // Link is there? if ((!SQL_IS_LINK_UP()) || (!is_array($data))) return false; - /* Other way */ // Escape all data $dataSecured['__sql_string'] = $qstring; foreach ($data as $key => $value) { @@ -281,50 +280,12 @@ function SQL_QUERY_ESC ($qstring, $data, $F, $L, $run=true, $strip=true, $secure // Generate query $query = call_user_func_array('sprintf', $dataSecured); - /* - /* Old eval() way... * - // Init variable - $query = 'failed'; - - if ($strip === true) { - $strip = 'true'; - } else { - $strip = 'false'; - } - - if ($secure === true) { - $secure = 'true'; - } else { - $secure = 'false'; - } - - $eval = "\$query = sprintf(\"" . $qstring . "\""; - foreach ($data as $var) { - if ((!empty($var)) || ($var === 0)) { - $eval .= ", SQL_ESCAPE('" . $var . "', " . $secure . ', ' . $strip . ')'; - } else { - $eval .= ", ''"; - } - } // END - foreach - $eval .= ');'; - **/ // Debugging // - //* DEBUG: */ $fp = fopen(getConfig('CACHE_PATH') . 'escape_debug.log', 'a') or app_die(__FILE__, __LINE__, "Cannot write debug.log!"); - //* DEBUG: */ fwrite($fp, $F.'('.$L."): ".str_replace("\r", '', str_replace("\n", " ", $eval))."\n"); - //* DEBUG: */ fclose($fp); - - // Run the code - /** - eval($eval); - - // Was the eval() command fine? - if ($query == 'failed') { - // Something went wrong? - debug_report_bug('eval=' . $eval); - } // END - if - **/ + ///* DEBUG: */ $fp = fopen(getConfig('CACHE_PATH') . 'escape_debug.log', 'a') or app_die(__FILE__, __LINE__, "Cannot write debug.log!"); + ///* DEBUG: */ fwrite($fp, $F.'('.$L."): ".str_replace("\r", '', str_replace("\n", " ", $eval))."\n"); + ///* DEBUG: */ fclose($fp); if ($run === true) { // Run SQL query (default) @@ -356,7 +317,7 @@ function SQL_ESCAPE ($str, $secureString=true, $strip=true) { $ret = smartAddSlashes($str); } elseif (function_exists('mysql_real_escape_string')) { // The new and improved version - //* DEBUG: */ print(__FUNCTION__."(".__LINE__."):str={$str}
"); + ///* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'str='.$str); $ret = mysql_real_escape_string($str, SQL_GET_LINK()); } elseif (function_exists('mysql_escape_string')) { // The obsolete function @@ -414,6 +375,8 @@ function SQL_ALTER_TABLE ($sql, $F, $L) { $noIndex = ( ( strpos($sql, 'INDEX') === false + ) && ( + strpos($sql, 'KEY') === false ) && ( strpos($sql, 'FULLTEXT') === false ) && ( @@ -422,44 +385,58 @@ function SQL_ALTER_TABLE ($sql, $F, $L) { ); // Extract table name - $tableArray = explode(" ", $sql); + $tableArray = explode(' ', $sql); $tableName = str_replace('`', '', $tableArray[2]); // Shall we add/drop? if (((strpos($sql, 'ADD') !== false) || (strpos($sql, 'DROP') !== false)) && ($noIndex === true)) { - // And column name as well - $columnName = str_replace('`', '', $tableArray[4]); - - // Get column information - $result = SQL_QUERY_ESC("SHOW COLUMNS FROM `%s` LIKE '%s'", - array($tableName, $columnName), __FILE__, __LINE__); - - // Do we have no entry on ADD or an entry on DROP? - // 123 4 4 3 3 4 4 32 23 4 4 3 3 4 4 321 - if (((SQL_NUMROWS($result) == 0) && (strpos($sql, 'ADD') !== false)) || ((SQL_NUMROWS($result) == 1) && (strpos($sql, 'DROP') !== false))) { - // Do the query - //* DEBUG: */ print(__LINE__.':'.$sql."
"); - $result = SQL_QUERY($sql, $F, $L, false); - } // END - if + // Try two columns, one should fix + foreach (array(4,5) as $idx) { + // And column name as well + $columnName = str_replace('`', '', $tableArray[$idx]); + + // Get column information + $result = SQL_QUERY_ESC("SHOW COLUMNS FROM `%s` LIKE '%s'", + array($tableName, $columnName), __FILE__, __LINE__); + + // Do we have no entry on ADD or an entry on DROP? + // 123 4 4 3 3 4 4 32 23 4 4 3 3 4 4 321 + if (((SQL_NUMROWS($result) == 0) && (strpos($sql, 'ADD') !== false)) || ((SQL_NUMROWS($result) == 1) && (strpos($sql, 'DROP') !== false))) { + // Do the query + //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Executing: ' . $sql); + $result = SQL_QUERY($sql, $F, $L, false); + + // Skip further attempt(s) + break; + } elseif ((((SQL_NUMROWS($result) == 1) && (strpos($sql, 'ADD') !== false)) || ((SQL_NUMROWS($result) == 0) && (strpos($sql, 'DROP') !== false))) && ($columnName != 'KEY')) { + // Abort here because it is alreay there + //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Skipped: ' . $sql); + break; + } elseif ($columnName != 'KEY') { + // Something didn't fit + //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Possible problem: ' . $sql); + } + } // END - foreach } elseif ((getConfig('_TABLE_TYPE') == 'InnoDB') && (strpos($sql, 'FULLTEXT') !== false)) { // Skip this query silently //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, sprintf("Skipped FULLTEXT: sql=%s,file=%s,line=%s", $sql, $F, $L)); } elseif ($noIndex === false) { // And column name as well - $columnName = str_replace('`', '', $tableArray[4]); + //* DEBUG: */ print __LINE__.':tableArray=
' . print_r($tableArray, true) . '
'; + $keyName = str_replace('`', '', $tableArray[5]); // Is this "UNIQUE" or so? FULLTEXT has been handled the elseif() block above - if (in_array(strtoupper($columnName), array('INDEX', 'UNIQUE', 'KEY', 'FULLTEXT'))) { + if (in_array(strtoupper($keyName), array('INDEX', 'UNIQUE', 'KEY', 'FULLTEXT'))) { // Init loop - $begin = 1; $columnName = ','; - while (strpos($columnName, ',') !== false) { + $begin = 1; $keyName = ','; + while (strpos($keyName, ',') !== false) { // Use last - $columnName = str_replace('`', '', $tableArray[count($tableArray) - $begin]); - //* DEBUG: */ print(__LINE__.':'.$columnName."----------------".$begin."
"); + $keyName = str_replace('`', '', $tableArray[count($tableArray) - $begin]); + //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, $keyName . '----------------' . $begin); // Remove brackes - $columnName = str_replace('(', '', str_replace(')', '', $columnName)); - //* DEBUG: */ print(__LINE__.':'.$columnName."----------------".$begin."
"); + $keyName = str_replace('(', '', str_replace(')', '', $keyName)); + //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, $keyName . '----------------' . $begin); // Continue $begin++; @@ -467,20 +444,33 @@ function SQL_ALTER_TABLE ($sql, $F, $L) { } // END - if // Show indexes - $result = SQL_QUERY_ESC("SHOW INDEX FROM `%s`", - array($tableName), __FILE__, __LINE__); + $result = SQL_QUERY_ESC("SHOW INDEX FROM `%s`", array($tableName), __FILE__, __LINE__); - // Walk through all + // Non-skipping is default for ADD $skip = false; + + // But should we DROP? + if ($tableArray[3] == 'DROP') { + // Then skip if nothing found! + $skip = true; + //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Going to drop key ' . $keyName); + } // END - if + + // Walk through all while ($content = SQL_FETCHARRAY($result)) { // Is it found? - //* DEBUG: */ print("
".print_r($content, true)."
"); - if (($content['Column_name'] == $columnName) || ($content['Key_name'] == $columnName)) { + //* DEBUG: */ print(__LINE__.':columnName='.$keyName.',content=
' . print_r($content, true) . '
'); + if (($content['Key_name'] == $keyName) && ($tableArray[3] == 'ADD')) { // Skip this query! - //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, sprintf("Skiped: %s", $sql)); + //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, sprintf("ADD: Skiped: %s", $sql)); $skip = true; break; - } // END - if + } elseif (($content['Key_name'] == $keyName) && ($tableArray[3] == 'DROP')) { + // Don't skip this! + //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, sprintf("DROP: Not skiped: %s", $sql)); + $skip = false; + break; + } } // END - while // Free result @@ -489,12 +479,15 @@ function SQL_ALTER_TABLE ($sql, $F, $L) { // Shall we run it? if ($skip === false) { // Send it to the SQL_QUERY() function - //* DEBUG: */ print(__LINE__.':'.$sql."
"); + //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, $sql); $result = SQL_QUERY($sql, $F, $L, false); - } // END - if + } else { + // Not executed + //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Not executed: ' . $sql); + } } else { // Other ALTER TABLE query - //* DEBUG: */ print(__LINE__.':'.$sql."
"); + //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, $sql); $result = SQL_QUERY($sql, $F, $L, false); } diff --git a/inc/extensions-functions.php b/inc/extensions-functions.php index 710d154398..4a21cc45aa 100644 --- a/inc/extensions-functions.php +++ b/inc/extensions-functions.php @@ -325,6 +325,9 @@ function registerExtension ($ext_name, $task_id, $dry_run = false, $logout = tru // Remove all SQL commands unsetSqls(); + // Mark it as installed + $GLOBALS['ext_is_installed'][getCurrentExtensionName()] = true; + // In normal mode return a true on success $ret = true; } elseif (getExtensionDryRun() === true) { @@ -434,7 +437,10 @@ function isExtensionInstalled ($ext_name) { $isInstalled = false; // Check if there is a cache entry - if (isset($GLOBALS['cache_array']['extension']['ext_id'][$ext_name])) { + if (isset($GLOBALS['ext_is_installed'][$ext_name])) { + // Use cache built from below queries + $isInstalled = $GLOBALS['ext_is_installed'][$ext_name]; + } elseif (isset($GLOBALS['cache_array']['extension']['ext_id'][$ext_name])) { // Found! $isInstalled = true; @@ -452,12 +458,15 @@ function isExtensionInstalled ($ext_name) { // Is it installed, then cache the entry if ($isInstalled === true) { - // Dummy call + // Dummy call (get is okay here) getExtensionId($ext_name, true); } // END - if // Free result SQL_FREERESULT($result); + + // Remember the status + $GLOBALS['ext_is_installed'][$ext_name] = $isInstalled; } // Return status @@ -779,13 +788,13 @@ function getExtensionId ($ext_name, $forceDb = false) { if (SQL_NUMROWS($result) == 1) { // Get the extension's id from database list($ret) = SQL_FETCHROW($result); - - // Cache it - $GLOBALS['cache_array']['extension']['ext_id'][$ext_name] = $ret; } // END - if // Free result SQL_FREERESULT($result); + + // Cache it + $GLOBALS['cache_array']['extension']['ext_id'][$ext_name] = $ret; } if ($ret == 0) { diff --git a/inc/extensions/ext-network.php b/inc/extensions/ext-network.php index b8a65d727d..f82ba1ac46 100644 --- a/inc/extensions/ext-network.php +++ b/inc/extensions/ext-network.php @@ -62,7 +62,7 @@ switch (getExtensionMode()) { `network_data_seperator` CHAR(4) NOT NULL DEFAULT '|', `network_row_seperator` CHAR(4) NOT NULL DEFAULT '|', `network_request_type` ENUM('GET','POST') NOT NULL DEFAULT 'GET', -`network_charset` VARCHAR(20) NOT NULL DEFAULT 'ISO-8859-1', +`network_charset` VARCHAR(20) NOT NULL DEFAULT 'UTF-8', UNIQUE (`network_short_name`), PRIMARY KEY (`network_id`) ) TYPE={?_TABLE_TYPE?} COMMENT='Network data'"); @@ -160,7 +160,7 @@ PRIMARY KEY (`network_id`) `max_reload_time` BIGINT(20) UNSIGNED NOT NULL DEFAULT 0, `min_waiting_time` BIGINT(20) UNSIGNED NOT NULL DEFAULT 0, `min_remain_clicks` BIGINT(20) UNSIGNED NOT NULL DEFAULT 0, -`min_payment` BIGINT(20) UNSIGNED NOT NULL DEFAULT 0, +`min_payment` FLOAT(20,5) UNSIGNED NOT NULL DEFAULT 0.00000, `allow_erotic` VARCHAR(10) NOT NULL DEFAULT '', UNIQUE `provider_type` (`network_id`,`network_type_id`), PRIMARY KEY (`network_data_id`) @@ -187,7 +187,7 @@ PRIMARY KEY (`network_cache_id`) `network_id` BIGINT(20) UNSIGNED NOT NULL DEFAULT 0, `network_type_id` BIGINT(20) UNSIGNED NOT NULL DEFAULT 0, `network_reload_lock` SMALLINT(5) UNSIGNED NOT NULL DEFAULT 0, -`network_inserted` TIMESTAMP NOT NULL CURRENT_TIMESTAMP(), +`network_inserted` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP(), UNIQUE `provider_type` (`network_id`,`network_type_id`), PRIMARY KEY (`network_reload_id`) ) TYPE={?_TABLE_TYPE?} COMMENT='Reload locks'"); diff --git a/inc/filters.php b/inc/filters.php index d1a66567e5..e79c47bb21 100644 --- a/inc/filters.php +++ b/inc/filters.php @@ -258,11 +258,11 @@ function FILTER_RUN_SQLS ($data) { // Do we have an "ALTER TABLE" command? if (substr(strtolower($sql), 0, 11) == 'alter table') { // Analyse the alteration command - //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "Alterting table: {$sql}"); + /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "Alterting table: {$sql}"); SQL_ALTER_TABLE($sql, __FUNCTION__, __LINE__); } else { // Run regular SQL command - //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "Running regular query: {$sql}"); + /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "Running regular query: {$sql}"); SQL_QUERY($sql, __FUNCTION__, __LINE__, false); } } // END - if diff --git a/inc/fix_menu.php b/inc/fix_menu.php index b50b07fe9d..954524fae7 100644 --- a/inc/fix_menu.php +++ b/inc/fix_menu.php @@ -86,17 +86,19 @@ foreach (array('guest','member','admin') as $menu) { // Free result SQL_FREERESULT($result); + // Init sqls + initSqls(); + // Now insert our unqiue key for action-what combination - SQL_QUERY_ESC("ALTER TABLE `{?_MYSQL_PREFIX?}_%s_menu` DROP KEY `action_what`", - array($menu), __FILE__, __LINE__); - SQL_QUERY_ESC("ALTER TABLE `{?_MYSQL_PREFIX?}_%s_menu` ADD UNIQUE `action_what` (`action` , `what`)", - array($menu), __FILE__, __LINE__); + addSql(sprintf("ALTER TABLE `{?_MYSQL_PREFIX?}_%s_menu` DROP KEY `action_what`", $menu)); + addSql(sprintf("ALTER TABLE `{?_MYSQL_PREFIX?}_%s_menu` ADD UNIQUE `action_what` (`action` , `what`)", $menu)); // Now insert our unqiue key for what - SQL_QUERY_ESC("ALTER TABLE `{?_MYSQL_PREFIX?}_%s_menu` DROP KEY `what`", - array($menu), __FILE__, __LINE__); - SQL_QUERY_ESC("ALTER TABLE `{?_MYSQL_PREFIX?}_%s_menu` ADD UNIQUE `what` (`what`)", - array($menu), __FILE__, __LINE__); + addSql(sprintf("ALTER TABLE `{?_MYSQL_PREFIX?}_%s_menu` DROP KEY `what`", $menu)); + addSql(sprintf("ALTER TABLE `{?_MYSQL_PREFIX?}_%s_menu` ADD UNIQUE `what` (`what`)", $menu)); + + // And run all + runFilterChain('run_sqls'); } // END - foreach // [EOF] -- 2.39.2