Skip to content

Commit

Permalink
Fix usage of static variables
Browse files Browse the repository at this point in the history
  • Loading branch information
NielBuys committed May 16, 2024
1 parent 757829f commit 6b20273
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 18 deletions.
36 changes: 23 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 @@ -1415,13 +1429,11 @@ public function escape_identifiers($item)
return $item;
}

static $preg_ec = array();

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 @@ -1430,20 +1442,20 @@ public function escape_identifiers($item)
}
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);
}
}

return preg_replace('/'.$preg_ec[0].'?([^'.$preg_ec[1].'\.]+)'.$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].'\.]+)'.$this->_preg_escape_char[1].'?(\.)?/i', $this->_preg_escape_char[2].'$1'.$this->_preg_escape_char[3].'$2', $item);
}

// --------------------------------------------------------------------
Expand Down Expand Up @@ -1562,14 +1574,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 @@ -1587,7 +1597,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 @@ -2715,15 +2722,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 6b20273

Please sign in to comment.