]> git.mxchange.org Git - mailer.git/blobdiff - inc/db/lib-mysql3.php
Some more rewrites to better functions
[mailer.git] / inc / db / lib-mysql3.php
index d5586ba55215418c6ca54704b76d5e702e54f306..4dfca09fd0a1bb53fe2b6bb4b24323125217ffd6 100644 (file)
@@ -66,7 +66,7 @@ function SQL_QUERY ($sqlString, $F, $L, $enableCodes = TRUE) {
                }
 
                // Remove \t, \n and \r from queries they may confuse some MySQL versions
-               $sqlStringModified = str_replace(array(chr(9), chr(10), chr(13)), array(' ', ' ', ' '), $sqlStringModified);
+               $sqlStringModified = str_replace(array(chr(9), PHP_EOL, chr(13)), array(' ', ' ', ' '), $sqlStringModified);
 
                // Compile config entries out
                $sqlStringModified = SQL_PREPARE_SQL_STRING($sqlStringModified, $enableCodes);
@@ -118,7 +118,7 @@ function SQL_QUERY ($sqlString, $F, $L, $enableCodes = TRUE) {
                } // END - if
 
                // Append debug line
-               appendLineToFile(getCachePath() . 'mysql.log', basename($F) . '|LINE=' . $L . '|NUM=' . (is_resource($result) ? SQL_NUMROWS($result) : 'false') . '|AFFECTED=' . SQL_AFFECTEDROWS() . '|QUERYTIME:' . ($queryTime * 1000) . 'ms): ' . str_replace(array(chr(13), chr(10)), array('', ' '), $GLOBALS['last_sql']));
+               appendLineToFile(getCachePath() . 'mysql.log', basename($F) . '|LINE=' . $L . '|NUM=' . (is_resource($result) ? SQL_NUMROWS($result) : 'false') . '|AFFECTED=' . SQL_AFFECTEDROWS() . '|QUERYTIME:' . ($queryTime * 1000) . 'ms): ' . str_replace(array(chr(13), PHP_EOL), array('', ' '), $GLOBALS['last_sql']));
        } // END - if
 
        // Increment stats entry
@@ -275,7 +275,7 @@ function SQL_QUERY_ESC ($sqlString, $data, $F, $L, $run = TRUE, $strip = TRUE, $
                return FALSE;
        } // END - if
 
-       // Init array for escaped data with SQL string
+       // Init array for escape'd data with SQL string
        $dataSecured = array(
                '__sql_string' => $sqlString
        );
@@ -307,21 +307,42 @@ function SQL_INSERTID () {
 function SQL_ESCAPE ($str, $secureString = TRUE, $strip = TRUE) {
        // Is there cache?
        if (!isset($GLOBALS['sql_escapes']['' . $str . ''])) {
+               // Debug message
+               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'str=' . $str . ' - BEFORE!');
+
                // Prepare the string here
                $str = SQL_PREPARE_SQL_STRING($str);
 
+               // Debug message
+               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'str=' . $str . ' - AFTER!');
+
                // Secure string first? (which is the default behaviour!)
                if ($secureString === TRUE) {
+                       // Debug message
+                       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'str=' . $str . ',strip=' . intval($strip) . ' - BEFORE!');
+
                        // Then do it here
                        $str = secureString($str, $strip);
+
+                       // Debug message
+                       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'str=' . $str . ',strip=' . intval($strip) . ' - AFTER!');
                } // END - if
 
+               // Init (invalid) value
+               $ret = '!INVALID!';
+
                if (!SQL_IS_LINK_UP()) {
                        // Fall-back to escapeQuotes() when there is no link
                        $ret = escapeQuotes($str);
                } elseif (function_exists('mysql_real_escape_string')) {
+                       // Debug message
+                       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'str=' . $str);
+
                        // The new and improved version
                        $ret = mysql_real_escape_string($str, SQL_GET_LINK());
+
+                       // Debug message
+                       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'str=' . $str . ',ret=' . $ret);
                } elseif (function_exists('mysql_escape_string')) {
                        // The obsolete function
                        $ret = mysql_escape_string($str, SQL_GET_LINK());
@@ -330,10 +351,16 @@ function SQL_ESCAPE ($str, $secureString = TRUE, $strip = TRUE) {
                        $ret = escapeQuotes($str);
                }
 
+               // Log message
+               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'str=' . $str . ',ret=' . $ret);
+
                // Cache result
                $GLOBALS['sql_escapes']['' . $str . ''] = $ret;
        } // END - if
 
+       // Log message
+       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'str=' . $str . ',sql_escapes=' . $GLOBALS['sql_escapes']['' . $str . '']);
+
        // Return it
        return $GLOBALS['sql_escapes']['' . $str . ''];
 }
@@ -474,11 +501,11 @@ function SQL_ALTER_TABLE ($sql, $F, $L, $enableCodes = TRUE) {
 
                // Shall we run it?
                //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'tableName=' . $tableName . ', tableArray[3]=' . $tableArray[3] . ',keyName=' . $keyName);
-               if (($tableArray[3] == 'ADD') && (!isSqlTableIndexFound($tableName, $keyName))) {
+               if (($tableArray[3] == 'ADD') && (!ifSqlTableIndexExist($tableName, $keyName))) {
                        // Send it to the SQL_QUERY() function to add it
                        //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'sql=' . $sql . ' - ADDING!');
                        $result = SQL_QUERY($sql, $F, $L, $enableCodes);
-               } elseif (($tableArray[3] == 'DROP') && (isSqlTableIndexFound($tableName, $keyName))) {
+               } elseif (($tableArray[3] == 'DROP') && (ifSqlTableIndexExist($tableName, $keyName))) {
                        // Send it to the SQL_QUERY() function to drop it
                        //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'sql=' . $sql . ' - DROPPING!');
                        $result = SQL_QUERY($sql, $F, $L, $enableCodes);
@@ -555,17 +582,29 @@ function SQL_HASZEROAFFECTED () {
 
 // Private function to prepare the SQL query string
 function SQL_PREPARE_SQL_STRING ($sqlString, $enableCodes = TRUE) {
+       // Debug message
+       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'sqlString=' . $sqlString . ',enableCodes=' . intval($enableCodes) . ' - ENTERED!');
+
        // Is it already cached?
-       if (!isset($GLOBALS['sql_strings'][$sqlString])) {
-               // Compile URI codes+config+expression code
-               $sqlString2 = FILTER_COMPILE_EXPRESSION_CODE(FILTER_COMPILE_CONFIG(compileUriCode($sqlString)));
+       if (!isset($GLOBALS['sql_strings']['' . $sqlString . ''])) {
+               // Preserve escaping and compile URI codes+config+expression code
+               $sqlString2 = FILTER_COMPILE_EXPRESSION_CODE(FILTER_COMPILE_CONFIG($sqlString));
 
-               // Do final compilation
-               $GLOBALS['sql_strings'][$sqlString] = doFinalCompilation($sqlString2, FALSE, $enableCodes);
-       } // END - if
+               // Debug message
+               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'sqlString2=' . $sqlString2);
+
+               // Do final compilation and revert {ESCAPE}
+               $GLOBALS['sql_strings']['' . $sqlString . ''] = doFinalCompilation($sqlString2, FALSE, $enableCodes);
+       } else {
+               // Log message
+               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'sqlString=' . $sqlString . ' - CACHE!');
+       }
+
+       // Debug message
+       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'sqlString=' . $sqlString . ',enableCodes=' . intval($enableCodes) . ',sql_strings=' . $GLOBALS['sql_strings']['' . $sqlString . ''] . ' - EXIT!');
 
        // Return it
-       return $GLOBALS['sql_strings'][$sqlString];
+       return $GLOBALS['sql_strings']['' . $sqlString . ''];
 }
 
 // Creates a MySQL TIMESTAMP compatible string from given Uni* timestamp
@@ -600,6 +639,19 @@ function ifSqlTableExists ($tableName) {
                $result = SQL_QUERY_ESC("SHOW TABLES FROM `{?__DB_NAME?}` WHERE `Tables_in_{?__DB_NAME?}`='{?_MYSQL_PREFIX?}_%s'",
                        array($tableName), __FILE__, __LINE__);
 
+               // Is a link there?
+               if (!is_resource($result)) {
+                       // Is installation phase?
+                       if (isInstallationPhase()) {
+                               // Then silently abort here
+                               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'result[]=' . gettype($result) . ',isLinkUp=' . intval(SQL_IS_LINK_UP()) . ',tableName=' . $tableName . ' - Returning FALSE ...');
+                               return FALSE;
+                       } else {
+                               // Please report this
+                               reportBug(__FUNCTION__, __LINE__, 'result[]=' . gettype($result) . ' is not a resource.');
+                       }
+               } // END - if
+
                // Is there an entry?
                $GLOBALS[__FUNCTION__][$tableName] = (SQL_NUMROWS($result) == 1);
                //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'tableName=' . $tableName . ',numRows=' . intval($GLOBALS[__FUNCTION__][$tableName]));
@@ -618,6 +670,12 @@ function ifSqlColumnExists ($tableName, $columnName) {
                // And column name as well
                $columnName = str_replace('`', '', $columnName);
 
+               // If the table is not there, it is okay
+               if (!ifSqlTableExists($tableName)) {
+                       // Then abort here
+                       return FALSE;
+               } // END - if
+
                // Get column information
                $result = SQL_QUERY_ESC("SHOW COLUMNS FROM `%s` LIKE '%s'",
                        array(
@@ -625,6 +683,19 @@ function ifSqlColumnExists ($tableName, $columnName) {
                                $columnName
                        ), __FUNCTION__, __LINE__);
 
+               // Is a link there?
+               if (!is_resource($result)) {
+                       // Is installation phase?
+                       if (isInstallationPhase()) {
+                               // Then silently abort here
+                               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'result[]=' . gettype($result) . ',isLinkUp=' . intval(SQL_IS_LINK_UP()) . ',tableName=' . $tableName . ',columnName=' . $columnName . ' - Returning FALSE ...');
+                               return FALSE;
+                       } else {
+                               // Please report this
+                               reportBug(__FUNCTION__, __LINE__, 'result[]=' . gettype($result) . ' is not a resource.');
+                       }
+               } // END - if
+
                // Determine it
                $GLOBALS[__FUNCTION__][$tableName][$columnName] = (!SQL_HASZERONUMS($result));
                //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'tableName=' . $tableName . ',columnName=' . $columnName . ',hasZeroNums=' . intval(SQL_HASZERONUMS($result)) . ',numRows=' . intval($GLOBALS[__FUNCTION__][$tableName][$columnName]));
@@ -639,13 +710,32 @@ function ifSqlColumnExists ($tableName, $columnName) {
 }
 
 // Checks depending on the mode if the index is there
-function isSqlTableIndexFound ($tableName, $keyName) {
+function ifSqlTableIndexExist ($tableName, $keyName) {
        //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'tableName=' . $tableName . ',columnName=' . $keyName . ' - ENTERED!');
        // Is there cache?
        if (!isset($GLOBALS[__FUNCTION__][$tableName][$keyName])) {
+               // If the table is not there, it is okay
+               if (!ifSqlTableExists($tableName)) {
+                       // Then abort here
+                       return FALSE;
+               } // END - if
+
                // Show indexes
                $result = SQL_QUERY_ESC("SHOW INDEX FROM `%s`", array($tableName), __FUNCTION__, __LINE__);
 
+               // Is a link there?
+               if (!is_resource($result)) {
+                       // Is installation phase?
+                       if (isInstallationPhase()) {
+                               // Then silently abort here
+                               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'result[]=' . gettype($result) . ',isLinkUp=' . intval(SQL_IS_LINK_UP()) . ',tableName=' . $tableName . ',keyName=' . $keyName . ' - Returning FALSE ...');
+                               return FALSE;
+                       } else {
+                               // Please report this
+                               reportBug(__FUNCTION__, __LINE__, 'result[]=' . gettype($result) . ' is not a resource.');
+                       }
+               } // END - if
+
                // The column is not found by default
                $GLOBALS[__FUNCTION__][$tableName][$keyName] = FALSE;
 
@@ -679,7 +769,7 @@ function getLastSqlError () {
 }
 
 // Gets an array (or false if none is found) from all supported engines
-function getArrayFromSupportedSqlEngines ($support = 'YES') {
+function getArrayFromSupportedSqlEngines ($requestedEngine = 'ALL') {
        // Init array
        $engines = array();
 
@@ -687,16 +777,19 @@ function getArrayFromSupportedSqlEngines ($support = 'YES') {
        $result = SQL_QUERY('SHOW ENGINES', __FUNCTION__, __LINE__);
 
        // Are there entries? (Bad if not)
-       if (SQL_NUMROWS($result) > 0) {
+       if (!SQL_HASZERONUMS($result)) {
                // Load all and check for active entries
                while ($content = SQL_FETCHARRAY($result)) {
+                       // Debug message
+                       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'support=' . $requestedEngine . ',Engine=' . $content['Engine'] . ',Support=' . $content['Support']);
+
                        // Is this supported?
-                       if (($support == 'ALL') || ($content['Support'] == $support)) {
+                       if ((($requestedEngine == 'ALL') || ($content['Engine'] == $requestedEngine)) && (in_array($content['Support'], array('YES', 'DEFAULT')))) {
                                // Add it
                                array_push($engines, $content);
                        } elseif (isDebugModeEnabled()) {
                                // Log it away in debug mode
-                               logDebugMessage(__FUNCTION__, __LINE__, 'Engine ' . $content['Engine'] . ' is not supported (' . $content['Supported'] . ')');
+                               logDebugMessage(__FUNCTION__, __LINE__, 'Engine ' . $content['Engine'] . ' is not supported (' . $content['Supported'] . ' - ' . $requestedEngine . ')');
                        }
                } // END - if
        } else {