1<?php if (!defined('BB2_CORE')) die("I said no cheating!");
2
3// Miscellaneous helper functions.
4
5// stripos() needed because stripos is only present on PHP 5
6if (!function_exists('stripos')) {
7	function stripos($haystack,$needle,$offset = 0) {
8		return(strpos(strtolower($haystack),strtolower($needle),$offset));
9	}
10}
11
12// str_split() needed because str_split is only present on PHP 5
13if (!function_exists('str_split')) {
14	function str_split($string, $split_length=1)
15	{
16		if ($split_length < 1) {
17			return false;
18		}
19
20		for ($pos=0, $chunks = array(); $pos < strlen($string); $pos+=$split_length) {
21			$chunks[] = substr($string, $pos, $split_length);
22		}
23		return $chunks;
24	}
25}
26
27// Convert a string to mixed-case on word boundaries.
28function uc_all($string) {
29	$temp = preg_split('/(\W)/', str_replace("_", "-", $string), -1, PREG_SPLIT_DELIM_CAPTURE);
30	foreach ($temp as $key=>$word) {
31		$temp[$key] = ucfirst(strtolower($word));
32	}
33	return join ('', $temp);
34}
35
36// Determine if an IP address resides in a CIDR netblock or netblocks.
37function match_cidr($addr, $cidr) {
38	$output = false;
39
40	if (is_array($cidr)) {
41		foreach ($cidr as $cidrlet) {
42			if (match_cidr($addr, $cidrlet)) {
43				$output = true;
44				break;
45			}
46		}
47	} else {
48		@list($ip, $mask) = explode('/', $cidr);
49		if (!$mask) $mask = 32;
50		$mask = pow(2,32) - pow(2, (32 - $mask));
51		$output = ((ip2long($addr) & $mask) == (ip2long($ip) & $mask));
52	}
53	return $output;
54}
55
56// Obtain all the HTTP headers.
57// NB: on PHP-CGI we have to fake it out a bit, since we can't get the REAL
58// headers. Run PHP as Apache 2.0 module if possible for best results.
59function bb2_load_headers() {
60	if (!is_callable('getallheaders')) {
61		$headers = array();
62		foreach ($_SERVER as $h => $v)
63			if (preg_match('/HTTP_(.+)/', $h, $hp))
64				$headers[str_replace("_", "-", uc_all($hp[1]))] = $v;
65	} else {
66		$headers = getallheaders();
67	}
68	return $headers;
69}
70
71?>
72