Commit ce5a5fce by Scott

Optimize qa_is_ip_blocked

10x faster with many blocked IPs
parent 3fc51a92
......@@ -134,20 +134,32 @@
}
/**
* Determine whether the requesting IP address has been blocked from write operations.
* @return bool
*/
function qa_is_ip_blocked()
/*
Return whether the requesting IP address has been blocked from write operations
*/
{
if (qa_to_override(__FUNCTION__)) { $args=func_get_args(); return qa_call_override(__FUNCTION__, $args); }
$blockipclauses=qa_block_ips_explode(qa_opt('block_ips_write'));
global $qa_curr_ip_blocked;
foreach ($blockipclauses as $blockipclause)
if (qa_block_ip_match(qa_remote_ip_address(), $blockipclause))
return true;
// return cached value early
if (isset($qa_curr_ip_blocked))
return $qa_curr_ip_blocked;
return false;
$qa_curr_ip_blocked = false;
$blockipclauses = qa_block_ips_explode(qa_opt('block_ips_write'));
$ip = qa_remote_ip_address();
foreach ($blockipclauses as $blockipclause) {
if (qa_block_ip_match($ip, $blockipclause)) {
$qa_curr_ip_blocked = true;
break;
}
}
return $qa_curr_ip_blocked;
}
......
......@@ -1048,14 +1048,15 @@
}
/**
* Determine the remote IP address of the user accessing the site.
* @return mixed String representing IP if it's available, or null otherwise.
*/
function qa_remote_ip_address()
/*
Return the remote IP address of the user accessing the site, if it's available, or null otherwise
*/
{
if (qa_to_override(__FUNCTION__)) { $args=func_get_args(); return qa_call_override(__FUNCTION__, $args); }
return @$_SERVER['REMOTE_ADDR'];
return isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : null;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment