From 5ae157f2010b3aae3c6f2ecdc6ec7c30b305be4a Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Sat, 28 Feb 2009 14:06:49 +0000 Subject: [PATCH] and rewritten to functions --- inc/db/lib-mysql3.php | 81 ++++++++++++++++++++++++++++++------------- inc/filters.php | 4 +-- inc/footer.php | 2 +- inc/header.php | 4 +-- inc/install-inc.php | 7 ++-- inc/mysql-connect.php | 19 +++------- modules.php | 2 +- 7 files changed, 70 insertions(+), 49 deletions(-) diff --git a/inc/db/lib-mysql3.php b/inc/db/lib-mysql3.php index ea128166a7..4492111863 100644 --- a/inc/db/lib-mysql3.php +++ b/inc/db/lib-mysql3.php @@ -39,10 +39,10 @@ if (!defined('__SECURITY')) { // SQL queries function SQL_QUERY ($sql_string, $F, $L) { - global $link, $OK; + global $OK; // Link is up? - if (!is_resource($link)) return false; + if (!SQL_IS_LINK_UP()) return false; // Remove \t, \n and \r from queries they may confuse some MySQL version I have heard $sql_string = str_replace("\t", " ", str_replace("\n", " ", str_replace("\r", " ", $sql_string))); @@ -55,7 +55,7 @@ function SQL_QUERY ($sql_string, $F, $L) { // Run SQL command //* DEBUG: */ echo $sql_string."
\n"; - $result = mysql_query($sql_string, $link) + $result = mysql_query($sql_string, SQL_GET_LINK()) or addFatalMessage($F." (".$L."):".mysql_error()."
Query string:
".$sql_string); @@ -120,13 +120,11 @@ function SQL_NUMROWS ($result) { // SQL affected rows function SQL_AFFECTEDROWS() { - global $link; - // Valid link resource? - if (!is_resource($link)) return false; + if (!SQL_IS_LINK_UP()) return false; // Get affected rows - $lines = mysql_affected_rows($link); + $lines = mysql_affected_rows(SQL_GET_LINK()); // Return it return $lines; @@ -183,21 +181,25 @@ function SQL_RESULT ($res, $row, $field) { // SQL connect function SQL_CONNECT ($host, $login, $password, $F, $L) { + // Try to connect $connect = mysql_connect($host, $login, $password) or addFatalMessage($F." (".$L."):".mysql_error()); - return $connect; + + // Set the link resource + SQL_SET_LINK($connect); } // SQL select database -function SQL_SELECT_DB ($dbName, $link, $F, $L) { +function SQL_SELECT_DB ($dbName, $F, $L) { // Is there still a valid link? If not, skip it. - if (!is_resource($link)) return false; + if (!SQL_IS_LINK_UP()) return false; - return mysql_select_db($dbName, $link) or addFatalMessage($F." (".$L."):".mysql_error()); + // Return the result + return mysql_select_db($dbName, SQL_GET_LINK()) or addFatalMessage($F." (".$L."):".mysql_error()); } // SQL close link -function SQL_CLOSE (&$link, $F, $L) { - if (!is_resource($link)) { +function SQL_CLOSE ($F, $L) { + if (!SQL_IS_LINK_UP()) { // Skip double close return false; } // END - if @@ -213,8 +215,12 @@ function SQL_CLOSE (&$link, $F, $L) { } // END - if // Close database link and forget the link - $close = mysql_close($link) or addFatalMessage($F." (".$L."):".mysql_error()); - $link = null; + $close = mysql_close(SQL_GET_LINK()) or addFatalMessage($F." (".$L."):".mysql_error()); + + // Close link + SQL_SET_LINK(null); + + // Return the result return $close; } @@ -231,10 +237,8 @@ function SQL_FREERESULT ($result) { // SQL string escaping function SQL_QUERY_ESC ($qstring, $data, $F, $L, $run=true, $strip=true) { - global $link; - // Link is there? - if (!is_resource($link)) return false; + if (!SQL_IS_LINK_UP()) return false; // Init variable $query = "failed"; @@ -281,31 +285,28 @@ function SQL_QUERY_ESC ($qstring, $data, $F, $L, $run=true, $strip=true) { // Get ID from last INSERT command function SQL_INSERTID () { - global $link; - if (!is_resource($link)) return false; + if (!SQL_IS_LINK_UP()) return false; return mysql_insert_id(); } // Escape a string for the database function SQL_ESCAPE ($str, $secureString=true,$strip=true) { - global $link; - // Secure string first? (which is the default behaviour!) if ($secureString) { // Then do it here $str = secureString($str, $strip); } // END - if - if (!is_resource($link)) { + if (!SQL_IS_LINK_UP()) { // Fall-back to smartAddSlashes() when there is no link return smartAddSlashes($str); } elseif (function_exists('mysql_real_escape_string')) { // The new and improved version //* DEBUG: */ print __FUNCTION__."(".__LINE__."):str={$str}
\n"; - return mysql_real_escape_string($str, $link); + return mysql_real_escape_string($str, SQL_GET_LINK()); } elseif (function_exists('mysql_escape_string')) { // The obsolete function - return mysql_escape_string($str, $link); + return mysql_escape_string($str, SQL_GET_LINK()); } else { // If nothing else works, fall back to smartAddSlashes() return smartAddSlashes($str); @@ -369,5 +370,35 @@ function SQL_ALTER_TABLE ($sql, $F, $L) { return $result; } +// Getter for SQL link +function SQL_GET_LINK () { + // Init link + $link = null; + + // Is it in the globals? + if (isset($GLOBALS['sql_link'])) { + // Then take it + $link = $GLOBALS['sql_link']; + } // END - if + + // Return it + return $link; +} + +// Setter for link +function SQL_SET_LINK ($link) { + // Set it + $GLOABLS['sql_link'] = $link; +} + +// Checks if the link is up +function SQL_IS_LINK_UP () { + // Get the link + $link = SQL_GET_LINK(); + + // Return the result + return (is_resource($link)); +} + // ?> diff --git a/inc/filters.php b/inc/filters.php index 1ffea5b420..72c61a02c4 100644 --- a/inc/filters.php +++ b/inc/filters.php @@ -243,13 +243,13 @@ function RUN_FILTER ($filterName, $data = null, $silentAbort = true) { // Filter for flushing all new filters to the database function FILTER_FLUSH_FILTERS () { - global $filters, $counter, $link, $loadedFilters, $SQLs; + global $filters, $counter, $loadedFilters, $SQLs; // Clear all previous SQL queries $SQLs = array(); // Is a database link here and not in installation mode? - if ((!is_resource($link)) && (!isBooleanConstantAndTrue('mxchange_installing'))) { + if ((!SQL_IS_LINK_UP()) && (!isBooleanConstantAndTrue('mxchange_installing'))) { // Abort here addFatalMessage(getMessage('FILTER_FLUSH_FAILED_NO_DATABASE'), array($filterFunction, $filterName)); return false; diff --git a/inc/footer.php b/inc/footer.php index 365b5dceec..758414823f 100644 --- a/inc/footer.php +++ b/inc/footer.php @@ -32,7 +32,7 @@ ************************************************************************/ // Global variable stuff -global $link, $frame; +global $frame; // Some security stuff... if (!defined('__SECURITY')) { diff --git a/inc/header.php b/inc/header.php index dc81c5a868..650dce7ddd 100644 --- a/inc/header.php +++ b/inc/header.php @@ -49,7 +49,7 @@ if (($GLOBALS['header_sent'] != "1") && ($GLOBALS['header_sent'] != "2")) { // Add title decorations? (left) if (!defined('__PAGE_TITLE')) { // Config and database connection valid? - if ((isset($_CONFIG)) && (is_array($_CONFIG)) && (count($_CONFIG) > 1) && (is_resource($link)) && (isset($db))) { + if ((isset($_CONFIG)) && (is_array($_CONFIG)) && (count($_CONFIG) > 1) && (SQL_IS_LINK_UP())) { // Title decoration enabled? if ((getConfig('enable_title_deco') == "Y") && (getConfig('title_left') != "")) $TITLE .= trim(getConfig('title_left'))." "; @@ -106,7 +106,7 @@ if (($GLOBALS['header_sent'] != "1") && ($GLOBALS['header_sent'] != "2")) { LOAD_TEMPLATE("metadata"); // Add meta description to header - if ((isBooleanConstantAndTrue('mxchange_installed')) && (isBooleanConstantAndTrue('admin_registered')) && (isset($db)) && (is_resource($link))) { + if ((isBooleanConstantAndTrue('mxchange_installed')) && (isBooleanConstantAndTrue('admin_registered')) && (SQL_IS_LINK_UP())) { // Add meta description not in admin and login module and when the script is installed META_DESCRIPTION($GLOBALS['module'], $GLOBALS['what']); } // END - if diff --git a/inc/install-inc.php b/inc/install-inc.php index dad33f5438..1b8d1f486c 100644 --- a/inc/install-inc.php +++ b/inc/install-inc.php @@ -269,11 +269,10 @@ if ((!isBooleanConstantAndTrue('mxchange_installed')) || (!isBooleanConstantAndT $SQLs = array(); // Connect to MySQL server - $link = SQL_CONNECT($mysql['host'], $mysql['login'], $mysql['pass1'], __FILE__, __LINE__); - if ($link) { + SQL_CONNECT($mysql['host'], $mysql['login'], $mysql['pass1'], __FILE__, __LINE__); + if (SQL_IS_LINK_UP()) { // Seems to work, also right database? - $db = SQL_SELECT_DB($mysql['dbase'], $link, __FILE__, __LINE__); - if ($db) { + if (SQL_SELECT_DB($mysql['dbase'], __FILE__, __LINE__) === true) { // Automatically run install.sql if ((FILE_READABLE($_POST['spath']."install/tables.sql")) && (FILE_READABLE($_POST['spath']."install/menu-".GET_LANGUAGE().".sql"))) { // Both exists so import them diff --git a/inc/mysql-connect.php b/inc/mysql-connect.php index fdc059a8fb..cb41c079d1 100644 --- a/inc/mysql-connect.php +++ b/inc/mysql-connect.php @@ -83,14 +83,12 @@ set_error_handler('__errorHandler'); // Call-back function for running shutdown functions and close database connection function __SHUTDOWN_HOOK () { - global $link; - // Call the filter chain 'shutdown' RUN_FILTER('shutdown', null, false); - if (is_resource($link)) { + if (SQL_IS_LINK_UP()) { // Close link - SQL_CLOSE($link, __FILE__, __LINE__); + SQL_CLOSE(__FILE__, __LINE__); } else { // No database link addFatalMessage(getMessage('NO_DB_LINK')); @@ -140,17 +138,12 @@ if ((!isBooleanConstantAndTrue('mxchange_installing')) && (isBooleanConstantAndT if ((!empty($MySQL['host'])) && (!empty($MySQL['login'])) && (!empty($MySQL['password'])) && (!empty($MySQL['dbase']))) { // Connect to DB - global $link; - $link = SQL_CONNECT($MySQL['host'], $MySQL['login'], $MySQL['password'], __FILE__, __LINE__); + SQL_CONNECT($MySQL['host'], $MySQL['login'], $MySQL['password'], __FILE__, __LINE__); // Is the link valid? - if (is_resource($link)) { - // Choose the database - global $db; - $db = SQL_SELECT_DB($MySQL['dbase'], $link, __FILE__, __LINE__); - + if (SQL_IS_LINK_UP()) { // Is it a valid resource? - if ($db === true) { + if (SQL_SELECT_DB($MySQL['dbase'], __FILE__, __LINE__) === true) { // This is required for extension 'optimize' to work define('__DB_NAME', $MySQL['dbase']); @@ -247,7 +240,6 @@ if ((!isBooleanConstantAndTrue('mxchange_installing')) && (isBooleanConstantAndT // No link to database! addFatalMessage(getMessage('NO_DB_LINK')); - $db = false; } } else { // Add language system @@ -263,7 +255,6 @@ if ((!isBooleanConstantAndTrue('mxchange_installing')) && (isBooleanConstantAndT // Set other missing variables if (!isset($GLOBALS['output_mode'])) $GLOBALS['output_mode'] = "0"; - $link = false; // No database link by default // Include required files LOAD_INC_ONCE("inc/databases.php"); diff --git a/modules.php b/modules.php index cdb5daf0b8..0b111174af 100644 --- a/modules.php +++ b/modules.php @@ -101,7 +101,7 @@ $MOD_VALID = false; $check = "failed"; if ((getConfig('maintenance') == "Y") && (!IS_ADMIN()) && ($GLOBALS['module'] != "admin")) { // Maintain mode is active and you are no admin addFatalMessage(getMessage('LANG_DOWN_MAINTAINCE')); -} elseif (($link) && ($db) && (getTotalFatalErrors() == 0)) { +} elseif ((SQL_IS_LINK_UP()) && (getTotalFatalErrors() == 0)) { // Construct module name define('__MODULE', sprintf("inc/modules/%s.php", SQL_ESCAPE($GLOBALS['module']))); -- 2.30.2