Try to replace:
Code
	$CGIURL2 = preg_replace("!/!", "/", $CGIURL);
	$NonCGIURL2 = preg_replace("!/!", "/", $NonCGIURL);
	if(($ref != "") && (!preg_match("/^($CGIURL2|$NonCGIURL2)/ultimatebb.(php|cgi)/", $ref)) && ($this[0] == "")) {
with
Code
      $domain = explode("/", $CGIURL);
      if ($ref && preg_match("/$domain[2]/i",$ref) && preg_match("/ultimatebb.(php|cgi)/",$ref) && !$this[0]) {