Fixed problems with latin2 encoding due to changes in htmlspecialchars() behaviour...
[fa-stable.git] / includes / db / connect_db.inc
index ed706fe31401ef82b8f1b15215551c51bd47a243..bca8b86f52477a96561c1f5188d881802efe1dcd 100644 (file)
@@ -26,6 +26,15 @@ function set_global_connection($company=-1)
 
        $db = mysql_connect($connection["host"], $connection["dbuser"], $connection["dbpassword"]);
                mysql_select_db($connection["dbname"], $db);
+       ///// From MySql release 5.6.6 the sql_mode is no longer empty as it was prior to
+       ///// this release. Just for safety we set it empty for all 5.6 release and higher.
+       ///// This non empty sql_mode values can interphere with FA, so all is set empty during
+       ///// our sessions.
+       ///// We are, however, investigating the existing code to be compatible in the future.
+       ///// We are also working on a mysql/mysqli solution to go to release 2.4.
+       if (strncmp(mysql_get_server_info(), "5.6", 3) >= 0) 
+               db_query("SET sql_mode = ''");
+       /////
        return $db;
 }
 
@@ -36,7 +45,7 @@ $db_duplicate_error_code = 1062;
 function db_query($sql, $err_msg=null)
 {
        global $db, $show_sql, $sql_trail, $select_trail, $go_debug, $sql_queries, $Ajax,
-               $db_connections;
+               $db_connections, $db_last_inserted_id;
        
        // set current db prefix
        $cur_prefix = $db_connections[$_SESSION["wa_current_user"]->cur_con]['tbpref'];
@@ -49,7 +58,9 @@ function db_query($sql, $err_msg=null)
        }
 
        $result = mysql_query($sql, $db);
+       
        if($sql_trail) {
+               $db_last_inserted_id = mysql_insert_id($db);    // preserve in case trail insert is done
                if ($select_trail || (strstr($sql, 'SELECT') === false)) {
                        mysql_query(
                        "INSERT INTO ".$cur_prefix."sql_trail
@@ -111,7 +122,7 @@ function db_num_fields ($result)
 function db_escape($value = "", $nullify = false)
 {
        $value = @html_entity_decode($value, ENT_QUOTES, $_SESSION['language']->encoding);
-       $value = @htmlspecialchars($value, ENT_QUOTES, $_SESSION['language']->encoding);
+       $value = @htmlspecialchars($value, ENT_QUOTES, $_SESSION['language']->encoding=='iso-8859-2' ? 'ISO-8859-1' : $_SESSION['language']->encoding);
 
        //reset default if second parameter is skipped
        $nullify = ($nullify === null) ? (false) : ($nullify);
@@ -150,8 +161,9 @@ function db_error_msg($conn)
 
 function db_insert_id()
 {
-       global $db;
-       return mysql_insert_id($db);
+       global $db_last_inserted_id, $sql_trail, $db;
+
+       return $sql_trail ? $db_last_inserted_id : mysql_insert_id($db);
 }
 
 function db_num_affected_rows()
@@ -171,7 +183,7 @@ function db_create_db($connection)
                $connection["dbuser"], $connection["dbpassword"]);
        if (!mysql_select_db($connection["dbname"], $db))
        {
-               $sql = "CREATE DATABASE " . $connection["dbname"] . "";
+               $sql = "CREATE DATABASE IF NOT EXISTS " . $connection["dbname"] . "";
                if (!mysql_query($sql) || !mysql_select_db($connection["dbname"], $db))
                        return 0;
        }
@@ -183,7 +195,7 @@ function db_drop_db($connection)
 
        if ($connection["tbpref"] == "")
        {
-               $sql = "DROP DATABASE " . $connection["dbname"] . "";
+               $sql = "DROP DATABASE IF EXISTS " . $connection["dbname"] . "";
                return mysql_query($sql);
        }
        else