Location: PHPKode > projects > PHPKode - PHPGuestbook > kode_functions.php
<?php

    function getIP() {
	if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown"))
	    $ip = getenv("HTTP_CLIENT_IP");
	else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown"))
	    $ip = getenv("HTTP_X_FORWARDED_FOR");
	else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown"))
	    $ip = getenv("REMOTE_ADDR");
	else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown"))
	    $ip = $_SERVER['REMOTE_ADDR'];
	else
	    $ip = "unknown";
	return $ip;
    }

    function getSystem($userAgent) {
	if (stripos($userAgent, "NT 6.1"))
	    $os = "win_new";
	elseif (stripos($userAgent, "NT 6.0"))
	    $os = "win_new";
	elseif (stripos($userAgent, "NT 5.1"))
	    $os = "win_new";
	elseif (stripos($userAgent, "NT 5.2"))
	    $os = "win_new";
	elseif (stripos($userAgent, "NT 5"))
	    $os = "win_old";
	elseif (stripos($userAgent, "NT 4.9"))
	    $os = "win_old";
	elseif (stripos($userAgent, "NT 4"))
	    $os = "win_old";
	elseif (stripos($userAgent, "98"))
	    $os = "win_old";
	elseif (stripos($userAgent, "95"))
	    $os = "win_old";
	elseif (stripos($userAgent, "Mac"))
	    $os = "macos";
	elseif (stripos($userAgent, "Linux"))
	    $os = "linux";
	elseif (stripos($userAgent, "Unix"))
	    $os = "unix";
	elseif (stripos($userAgent, "FreeBSD"))
	    $os = "freebsd";
	elseif (stripos($userAgent, "SunOS"))
	    $os = "solaris";
	else
	    $os = "Unknow OS";

	return $os;
    }

    function getBrowser($userAgent) {
// Declare known browsers to look for
	$known = array('msie', 'firefox', 'chrome', 'safari', 'webkit', 'opera', 'netscape', 'konqueror', 'gecko', 'googlebot', 'googlebot-image', 'ask', 'msnbot-products');

	$os_known = array('linux', 'mac', 'win', 'iphone os');

	// Clean up agent and build regex that matches phrases for known browsers
	// (e.g. "Firefox/2.0" or "MSIE 6.0" (This only matches the major and minor
	// version numbers.  E.g. "2.0.0.6" is parsed as simply "2.0"
	// Use the User Agent String to get the OS full details.
	$os = '';
	if (preg_match('#\(.*?([' . join('|', $os_known) . '].*?)\)#i', $userAgent, $os_matches) != 0) {
	    $os = $os_matches[1];
	}

	$pattern = '#(?<browser>' . join('|', $known) . ')[/ ]+(?<version>[0-9]+(?:\.[0-9]+)?)#i';

	// Find all phrases (or return empty array if none found)
	if (!preg_match_all($pattern, $userAgent, $matches))
	    return array('name' => '', 'version' => '', 'os' => $os);

	//A tweak for chrome in Mac as this is giving safari as the last match.
	if ($key = array_search('chrome', $matches['browser']))
	    return array('name' => $matches['browser'][$key], 'version' => $matches['version'][$key], 'os' => $os);

	// Since some UAs have more than one phrase (e.g Firefox has a Gecko phrase,
	// Opera 7,8 have a MSIE phrase), use the last one found (the right-most one
	// in the UA).  That's usually the most correct.
	$i = count($matches['browser']) - 1;
	return array('name' => $matches['browser'][$i], 'version' => $matches['version'][$i], 'os' => $os);
    }

    $GLOBALS['_LANG'] = array(
	'af' => 'afrikaans',
	'ar' => 'arabic',
	'bg' => 'bulgarian',
	'ca' => 'catalan',
	'cs' => 'czech',
	'da' => 'danish',
	'de' => 'german',
	'el' => 'greek',
	'en' => 'english',
	'es' => 'spanish',
	'et' => 'estonian',
	'fi' => 'finnish',
	'fr' => 'french',
	'gl' => 'galician',
	'he' => 'hebrew',
	'hi' => 'hindi',
	'hr' => 'croatian',
	'hu' => 'hungarian',
	'id' => 'indonesian',
	'it' => 'italian',
	'ja' => 'japanese',
	'ko' => 'korean',
	'ka' => 'georgian',
	'lt' => 'lithuanian',
	'lv' => 'latvian',
	'ms' => 'malay',
	'nl' => 'dutch',
	'no' => 'norwegian',
	'pl' => 'polish',
	'pt' => 'portuguese',
	'ro' => 'romanian',
	'ru' => 'russian',
	'sk' => 'slovak',
	'sl' => 'slovenian',
	'sq' => 'albanian',
	'sr' => 'serbian',
	'sv' => 'swedish',
	'th' => 'thai',
	'tr' => 'turkish',
	'uk' => 'ukrainian',
	'zh' => 'chinese'
    );

    function getLanguage($acceptLanguage) {
	$_AL = strtolower($acceptLanguage);

	// Try to detect Primary language if several languages are accepted.
	foreach ($GLOBALS['_LANG'] as $K => $V) {
	    if (strpos($_AL, $K) > -1)
		return $V;
	}

	return 'unknow';
    }

    function is_email($email) {
	return (preg_match('/^[_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,4}$/', $str)) ? true : false;
    }

    function is_url($url) {
	$regex = "((https?|ftp)\:\/\/)?"; // SCHEME
	$regex .= "([a-z0-9+!*(),;?&=\$_.-]+(\:[a-z0-9+!*(),;?&=\$_.-]+)?@)?"; // User and Pass
	$regex .= "([a-z0-9-.]*)\.([a-z]{2,3})"; // Host or IP
	$regex .= "(\:[0-9]{2,5})?"; // Port
	$regex .= "(\/([a-z0-9+\$_-]\.?)+)*\/?"; // Path
	$regex .= "(\?[a-z+&\$_.-][a-z0-9;:@&%=+\/\$_.-]*)?"; // GET Query
	$regex .= "(#[a-z_.-][a-z0-9+\$_.-]*)?"; // Anchor

	return (preg_match("/^$regex$/", $url)) ? true : false;
    }

    function randKey($len=6) {
	    $chars = array(
		"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
		"l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
		"w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G",
		"H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
		"S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2",
		"3", "4", "5", "6", "7", "8", "9"
	    );
	    $charsLen = count($chars) - 1;
	    shuffle($chars);
	    $output = "";
	    for ($i = 0; $i < $len; $i++) {
		$output .= $chars[mt_rand(0, $charsLen)];
	    }
	    return $output;
	}
?>
Return current item: PHPKode - PHPGuestbook