Passed
Push — master ( d9e5dd...36764d )
by Spuds
01:07 queued 26s
created

bb2_run_whitelist()   C

Complexity

Conditions 13
Paths 46

Size

Total Lines 31
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
eloc 19
dl 0
loc 31
rs 6.6166
c 0
b 0
f 0
nc 46
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php if (!defined('BB2_CORE')) die('I said no cheating!');
2
3
function bb2_run_whitelist($package)
4
{
5
	# FIXME: Transitional, until port maintainters implement bb2_read_whitelist
6
	if (function_exists('bb2_read_whitelist')) {
7
		$whitelists = bb2_read_whitelist();
8
	} else {
9
		$whitelists = @parse_ini_file(dirname(BB2_CORE) . "/whitelist.ini");
10
	}
11
12
	if (@!empty($whitelists['ip'])) {
13
		foreach (array_filter($whitelists['ip']) as $range) {
14
			if (match_cidr($package['ip'], $range)) return true;
15
		}
16
	}
17
	if (@!empty($whitelists['useragent'])) {
18
		foreach (array_filter($whitelists['useragent']) as $user_agent) {
19
			if (!strcmp($package['headers_mixed']['User-Agent'], $user_agent)) return true;
20
		}
21
	}
22
	if (@!empty($whitelists['url'])) {
23
		if (strpos($package['request_uri'], "?") === FALSE) {
24
			$request_uri = $package['request_uri'];
25
		} else {
26
			$request_uri = substr($package['request_uri'], 0, strpos($package['request_uri'], "?"));
27
		}
28
		foreach (array_filter($whitelists['url']) as $url) {
29
			$pos = strpos($request_uri, $url);
30
			if ($pos !== false && $pos == 0) return true;
31
		}
32
	}
33
	return false;
34
}
35