and rewritten to functions
authorRoland Häder <roland@mxchange.org>
Sat, 28 Feb 2009 14:06:49 +0000 (14:06 +0000)
committerRoland Häder <roland@mxchange.org>
Sat, 28 Feb 2009 14:06:49 +0000 (14:06 +0000)
inc/db/lib-mysql3.php
inc/filters.php
inc/footer.php
inc/header.php
inc/install-inc.php
inc/mysql-connect.php
modules.php

index ea128166a729c5e3632e70da4df57ee9be7915d2..4492111863fba94c9f3d3f29edc9763e75de89ff 100644 (file)
@@ -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."<br />\n";
-       $result = mysql_query($sql_string, $link)
+       $result = mysql_query($sql_string, SQL_GET_LINK())
         or addFatalMessage($F." (".$L."):".mysql_error()."<br />
 Query string:<br />
 ".$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__."(<font color=\"#0000aa\">".__LINE__."</font>):str={$str}<br />\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));
+}
+
 //
 ?>
index 1ffea5b4208a6d0d597807fa718e0cb0be955720..72c61a02c4d26ecb681240151bdc4993a961f7a3 100644 (file)
@@ -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;
index 365b5dceece923ae634a90c9dbfa9e1f880e7129..758414823f5030c292ba5e23f249ef634b1c3f80 100644 (file)
@@ -32,7 +32,7 @@
  ************************************************************************/
 
 // Global variable stuff
-global $link, $frame;
+global $frame;
 
 // Some security stuff...
 if (!defined('__SECURITY')) {
index dc81c5a8689222ff330f8bbd8101e26e76344858..650dce7dddc31d60ba1a836acfaaa2eac9acda57 100644 (file)
@@ -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
index dad33f5438c98e51f9164c01a92ab512dc337287..1b8d1f486c1219b135e18b62ac8f6415cfe80e74 100644 (file)
@@ -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
index fdc059a8fb27904640f3cce2499486a0081d2199..cb41c079d1ca69b262df774e57bd44aa72067957 100644 (file)
@@ -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");
index cdb5daf0b8b29fc4790734598132132cd87a3129..0b111174af4508e3aaa7040b9fd4e310e3154a5e 100644 (file)
@@ -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'])));