Renamed ifSqlHasZeroNums() to ifSqlHasZeroNumRows() and improved some queries.
[mailer.git] / inc / sql-functions.php
index b0fa5065174aec57328561c610832519390b39b1..267dcb4fb8be6e4f4fb43a12a1e47c2dc0070bba 100644 (file)
  * -------------------------------------------------------------------- *
  * Kurzbeschreibung  : SQL-Funktionen fuer Queries                      *
  * -------------------------------------------------------------------- *
- * $Revision::                                                        $ *
- * $Date::                                                            $ *
- * $Tag:: 0.2.1-FINAL                                                 $ *
- * $Author::                                                          $ *
- * -------------------------------------------------------------------- *
  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
- * Copyright (c) 2009 - 2013 by Mailer Developer Team                   *
+ * Copyright (c) 2009 - 2015 by Mailer Developer Team                   *
  * For more information visit: http://mxchange.org                      *
  *                                                                      *
  * This program is free software; you can redistribute it and/or modify *
@@ -304,7 +299,9 @@ function sqlQueryEscaped ($sqlString, $data, $file, $line, $run = TRUE, $strip =
 
        // Escape all data
        foreach ($data as $key => $value) {
+               //* DEBUG: */ logDebugMessage(basename($file) . '/' . __FUNCTION__, $line . '/' . __LINE__, 'key=' . $key . ',value=' . $value . ',run=' . intval($run) . ',strip=' . intval($strip) . ',secure=' . intval($secure));
                $dataSecured[$key] = sqlEscapeString($value, $secure, $strip);
+               //* DEBUG: */ logDebugMessage(basename($file) . '/' . __FUNCTION__, $line . '/' . __LINE__, 'dataSecured[key]=' . $dataSecured[$key]);
        } // END - foreach
 
        // Generate query
@@ -510,7 +507,7 @@ function setSqlLink ($file, $line, $link) {
        $GLOBALS['__sql_link'] = $link;
 
        // Re-init cache
-       $GLOBALS['__is_sql_link_up'] = isValidSqlLink($link);
+       $GLOBALS['__is_sql_link_up'] = ((function_exists('isValidSqlLink')) && (isValidSqlLink($link)));
 }
 
 // Checks if the link is up
@@ -527,7 +524,7 @@ function isSqlLinkUp () {
 }
 
 // Wrapper function to make code more readable
-function ifSqlHasZeroNums ($result) {
+function ifSqlHasZeroNumRows ($result) {
        // Just pass it through
        return (sqlNumRows($result) === 0);
 }
@@ -546,20 +543,23 @@ function sqlPrepareQueryString ($sqlString, $enableCodes = TRUE) {
        // Is it already cached?
        if (!isset($GLOBALS['sql_strings']['' . $sqlString . ''])) {
                // Preserve escaping and compile URI codes+config+expression code
-               $sqlString2 = FILTER_COMPILE_EXPRESSION_CODE(FILTER_COMPILE_CONFIG($sqlString));
+               $sqlString2 = str_replace(chr(92), '{BACKLASH}', $sqlString);
+               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'sqlString2=' . $sqlString2);
+               $sqlString2 = FILTER_COMPILE_EXPRESSION_CODE(FILTER_COMPILE_CONFIG($sqlString2));
 
                // Debug message
                //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'sqlString2=' . $sqlString2);
 
-               // Do final compilation and revert {ESCAPE}
+               // Do final compilation and revert {BACKSLASH}
                $GLOBALS['sql_strings']['' . $sqlString . ''] = doFinalCompilation($sqlString2, FALSE, $enableCodes);
+               $GLOBALS['sql_strings']['' . $sqlString . ''] = str_replace('{BACKLASH}', chr(92), $GLOBALS['sql_strings']['' . $sqlString . '']);
        } 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!');
+       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'sqlString=' . $sqlString . ',enableCodes=' . intval($enableCodes) . ',returned sql_string=' . $GLOBALS['sql_strings']['' . $sqlString . ''] . ' - EXIT!');
 
        // Return it
        return $GLOBALS['sql_strings']['' . $sqlString . ''];
@@ -585,7 +585,7 @@ function ifSqlTableExists ($tableName) {
                        array($tableName), __FUNCTION__, __LINE__);
 
                // Is a link there?
-               if (!is_resource($result)) {
+               if (!isValidSqlLink($result)) {
                        // Is installation phase?
                        if (isInstaller()) {
                                // Then silently abort here
@@ -630,7 +630,7 @@ function ifSqlTableColumnExists ($tableName, $columnName, $forceFound = FALSE) {
                ), __FUNCTION__, __LINE__);
 
        // Is a link there?
-       if (!is_resource($result)) {
+       if (!isValidSqlLink($result)) {
                // Is installation phase?
                if (isInstaller()) {
                        // Then silently abort here
@@ -643,7 +643,7 @@ function ifSqlTableColumnExists ($tableName, $columnName, $forceFound = FALSE) {
        } // END - if
 
        // Determine it
-       $doesExist = (!ifSqlHasZeroNums($result));
+       $doesExist = (!ifSqlHasZeroNumRows($result));
 
        // Free result
        sqlFreeResult($result);
@@ -672,7 +672,7 @@ function ifSqlTableIndexExist ($tableName, $keyName, $forceFound = FALSE) {
        $result = sqlQueryEscaped("SHOW INDEX FROM `%s`", array($tableName), __FUNCTION__, __LINE__);
 
        // Is a link there?
-       if (!is_resource($result)) {
+       if (!isValidSqlLink($result)) {
                // Is installation phase?
                if (isInstaller()) {
                        // Then silently abort here
@@ -725,7 +725,7 @@ function getArrayFromSupportedSqlEngines ($requestedEngine = 'ALL') {
        $result = sqlQuery('SHOW ENGINES', __FUNCTION__, __LINE__);
 
        // Are there entries? (Bad if not)
-       if (!ifSqlHasZeroNums($result)) {
+       if (!ifSqlHasZeroNumRows($result)) {
                // Load all and check for active entries
                while ($content = sqlFetchArray($result)) {
                        // Debug message
@@ -777,5 +777,41 @@ WHERE
        );
 }
 
+// Log SQL errors to debug.log in installation phase or call reportBug()
+function logSqlError ($file, $line, $message) {
+       // Remember plain error in last_sql_error
+       setSqlError($file, $line, $message);
+
+       // Is login set?
+       if (!empty($GLOBALS['mysql']['login'])) {
+               // Secure login name in message
+               $message = str_replace($GLOBALS['mysql']['login'], '***', $message);
+       } // END - if
+
+       // Is database password set?
+       if (!empty($GLOBALS['mysql']['password'])) {
+               // Secure password in message
+               $message = str_replace($GLOBALS['mysql']['password'], '***', $message);
+       } // END - if
+
+       // Is database name set?
+       if (!empty($GLOBALS['mysql']['dbase'])) {
+               // Secure database name in message
+               $message = str_replace($GLOBALS['mysql']['dbase'], '***', $message);
+       } // END - if
+
+       // Is there installation phase?
+       if (isInstaller()) {
+               /*
+                * In installation phase, we don't want SQL errors abort e.g. connection
+                * tests, so just log it away.
+                */
+               logDebugMessage($file, $line, $message);
+       } else {
+               // Regular mode, then call reportBug()
+               reportBug($file, $line, $message);
+       }
+}
+
 // [EOF]
 ?>