Skip to content

Commit

Permalink
Fix usage of static variables
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamie Burchell committed May 16, 2024
1 parent a1559a8 commit ae72052
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 18 deletions.
44 changes: 31 additions & 13 deletions system/database/DB_driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,20 @@ abstract class CI_DB_driver {
*/
protected $_like_escape_chr = '!';

/**
* RegExp used to escape identifiers
*
* @var array
*/
protected $_preg_escape_char = array();

/**
* RegExp used to get operators
*
* @var string[]
*/
protected $_preg_operators = array();

/**
* ORDER BY random keyword
*
Expand Down Expand Up @@ -1354,13 +1368,11 @@ public function escape_identifiers($item, $split = TRUE)
return $item;
}

static $preg_ec;

if (empty($preg_ec))
if (empty($this->_preg_escape_char))
{
if (is_array($this->_escape_char))
{
$preg_ec = array(
$this->_preg_escape_char = array(
preg_quote($this->_escape_char[0]),
preg_quote($this->_escape_char[1]),
$this->_escape_char[0],
Expand All @@ -1369,22 +1381,30 @@ public function escape_identifiers($item, $split = TRUE)
}
else
{
$preg_ec[0] = $preg_ec[1] = preg_quote($this->_escape_char);
$preg_ec[2] = $preg_ec[3] = $this->_escape_char;
$this->_preg_escape_char[0] = $this->_preg_escape_char[1] = preg_quote($this->_escape_char);
$this->_preg_escape_char[2] = $this->_preg_escape_char[3] = $this->_escape_char;
}
}

foreach ($this->_reserved_identifiers as $id)
{
if (strpos($item, '.'.$id) !== FALSE)
{
return preg_replace('#'.$preg_ec[0].'?([^'.$preg_ec[1].'\.]+)'.$preg_ec[1].'?\.#i', $preg_ec[2].'$1'.$preg_ec[3].'.', $item);
return preg_replace(
'#'.$this->_preg_escape_char[0].'?([^'.$this->_preg_escape_char[1].'\.]+)'.$this->_preg_escape_char[1].'?\.#i',
$this->_preg_escape_char[2].'$1'.$this->_preg_escape_char[3].'.',
$item
);
}
}

$dot = ($split !== FALSE) ? '\.' : '';

return preg_replace('#'.$preg_ec[0].'?([^'.$preg_ec[1].$dot.']+)'.$preg_ec[1].'?(\.)?#i', $preg_ec[2].'$1'.$preg_ec[3].'$2', $item);
return preg_replace(
'#'.$this->_preg_escape_char[0].'?([^'.$this->_preg_escape_char[1].$dot.']+)'.$this->_preg_escape_char[1].'?(\.)?#i',
$this->_preg_escape_char[2].'$1'.$this->_preg_escape_char[3].'$2',
$item
);
}

// --------------------------------------------------------------------
Expand Down Expand Up @@ -1503,14 +1523,12 @@ protected function _has_operator($str)
*/
protected function _get_operator($str)
{
static $_operators;

if (empty($_operators))
if (empty($this->_preg_operators))
{
$_les = ($this->_like_escape_str !== '')
? '\s+'.preg_quote(trim(sprintf($this->_like_escape_str, $this->_like_escape_chr)), '/')
: '';
$_operators = array(
$this->_preg_operators = array(
'\s*(?:<|>|!)?=\s*', // =, <=, >=, !=
'\s*<>?\s*', // <, <>
'\s*>\s*', // >
Expand All @@ -1528,7 +1546,7 @@ protected function _get_operator($str)

}

return preg_match('/'.implode('|', $_operators).'/i', $str, $match)
return preg_match('/'.implode('|', $this->_preg_operators).'/i', $str, $match)
? $match[0] : FALSE;
}

Expand Down
15 changes: 10 additions & 5 deletions system/database/DB_query_builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,13 @@ abstract class CI_DB_query_builder extends CI_DB_driver {
*/
protected $qb_cache_no_escape = array();

/**
* Strings that determine if a string represents a literal value or a field name
*
* @var string[]
*/
protected $is_literal_str = array();

// --------------------------------------------------------------------

/**
Expand Down Expand Up @@ -2787,15 +2794,13 @@ protected function _is_literal($str)
return TRUE;
}

static $_str;

if (empty($_str))
if (empty($this->is_literal_str))
{
$_str = ($this->_escape_char !== '"')
$this->is_literal_str = ($this->_escape_char !== '"')
? array('"', "'") : array("'");
}

return in_array($str[0], $_str, TRUE);
return in_array($str[0], $this->is_literal_str, TRUE);
}

// --------------------------------------------------------------------
Expand Down

0 comments on commit ae72052

Please sign in to comment.