Skip to content

Commit

Permalink
+ Added support for MySQL strict mode -- more specifically, the abili…
Browse files Browse the repository at this point in the history
…ty to disable it through a global variable. I wrote that for a previous server, and never bothered to commit it because I didn't need it anymore by the time I was committing everything. But still, can be useful to some... I know I wasted a few hours on that one..! (Class-DB.php)

! Fixed {literal:...} code not allowing for flexible strings. (Class-DB.php)
  • Loading branch information
Nao committed Jul 22, 2017
1 parent aabdd5f commit 7f9ec53
Showing 1 changed file with 18 additions and 16 deletions.
34 changes: 18 additions & 16 deletions core/app/Class-DB.php
Expand Up @@ -49,7 +49,7 @@ public static function is_connected()

public static function connect($db_server, $db_name, $db_user, $db_passwd, $db_prefix, $db_options = array())
{
global $mysql_set_mode, $db_link;
global $mysql_set_mode, $mysql_autocommit, $mysql_strict_mode, $db_link;

// Attempt to connect. (And in non SSI mode, also select the database)
$connection = mysqli_connect((!empty($db_options['persist']) ? 'p:' : '') . $db_server, $db_user, $db_passwd, empty($db_options['dont_select_db']) ? $db_name : '') or die(mysqli_connect_error());
Expand All @@ -63,11 +63,19 @@ public static function connect($db_server, $db_name, $db_user, $db_passwd, $db_p
show_db_error();
}

// This is just for compatibility purposes.
if (isset($mysql_set_mode) && $mysql_set_mode === true)
wesql::query('SET sql_mode = \'\', AUTOCOMMIT = 1',
array(),
false
);
mysqli_query($connection, 'SET SESSION sql_mode = \'\', AUTOCOMMIT = 1', array(), false);
elseif (isset($mysql_strict_mode) || isset($mysql_autocommit))
{
$request = @mysqli_query($connection, 'SELECT @@sql_mode');
$modes = mysqli_fetch_row($request);
$modes = $modes ? $modes[0] : '';
mysqli_free_result($request);
if (isset($mysql_strict_mode) && $mysql_strict_mode === false)
$modes = implode(',', array_diff(explode(',', $modes), array('ONLY_FULL_GROUP_BY', 'STRICT_TRANS_TABLES')));
mysqli_query($connection, 'SET SESSION sql_mode = "' . mysqli_real_escape_string($connection, $modes) . '"' . (empty($mysql_autocommit) ? '' : ', AUTOCOMMIT = 1'));
}

// Otherwise set, and return true so that we can tell we did manage a connection.
return self::$link = $db_link = $connection;
Expand All @@ -90,7 +98,7 @@ public static function quote($query, $db_values, $connection = null)
$db_callback = array($db_values, $connection == null ? self::$link : $connection);

// Do the quoting and escaping
$query = preg_replace_callback('~{([a-z_]+)(?::([a-zA-Z0-9_-]+))?}~', 'wesql::replace_value', $query);
$query = preg_replace_callback('~{([a-z_]+)(?::([^}]+))?}~', 'wesql::replace_value', $query);

// Clear this global variable.
$db_callback = array();
Expand Down Expand Up @@ -144,7 +152,7 @@ public static function query($query, $db_values = array(), $connection = null)
$db_callback = array($db_values, $connection);

// Inject the values passed to this function.
$query = preg_replace_callback('~{([a-z_]+)(?::([a-zA-Z0-9_-]+))?}~', 'wesql::replace_value', $query);
$query = preg_replace_callback('~{([a-z_]+)(?::([^}]+))?}~', 'wesql::replace_value', $query);

// This shouldn't be residing in global space any longer.
$db_callback = array();
Expand Down Expand Up @@ -254,15 +262,9 @@ public static function transaction($operation = 'commit', $connection = null)
// Determine whether to use the known connection or not.
$connection = $connection === null ? self::$link : $connection;

switch ($operation)
{
case 'begin':
case 'rollback':
case 'commit':
return @mysqli_query($connection, strtoupper($operation));
default:
return false;
}
if (in_array($operation, ['begin', 'rollback', 'commit']))
return @mysqli_query($connection, strtoupper($operation));
return false;
}

public static function error($connection = null)
Expand Down

0 comments on commit 7f9ec53

Please sign in to comment.