PDA

View Full Version : A function like is_broswer, but for Operating System Ident.


neocorteqz
03-11-2005, 10:34 PM
I was wondering if anyone could find a small amount of time to do this.

We have the built in function of is_browser, to select specific browsers, I was wondering if someone could code something similar for a browser conditional.

I use a plugin for grabbing game server info, called q3px, and it currently does not support Linux. So i'd like to have something that can exclude Operating systems that are not compatible with what I may install to add to the already existing scripts.

Thanks for anyone that can take the time to do this. :)

why-not
03-11-2005, 11:30 PM
Hi


Something like this...


<?

// usage

$out = this_system ();

// good systems! (example)

$good = array ( 'windows', 'mac' );

// test it!

if ( ! in_array ( $out, $good ) )
{
echo 'Sorry this service does not support your system type';

exit ();
}

// keep processing script, system OK!

echo 'You are using ' . $out . ' for your OS!';

function this_system ()
{
$ua = $_SERVER['HTTP_USER_AGENT'];
$sta = stristr ( $ua, 'x11' );
$stb = stristr ( $ua, 'linux' );
$stc = stristr ( $ua, 'sunos' );
$std = stristr ( $ua, 'os/2' );
$ste = stristr ( $ua, 'mac' );
$stf = stristr ( $ua, 'ppc' );
$stg = stristr ( $ua, '32bit' );
$sth = stristr ( $ua, 'winnt' );
$sti = stristr ( $ua, 'win95' );
$stj = stristr ( $ua, 'win32' );
$stk = stristr ( $ua, 'windows' );

if ( $stk !== false )
{
$system = 'windows';
}
else if ( $stj !== false )
{
$system = 'windows';
}
else if ( $sti !== false )
{
$system = 'windows';
}
else if ( $sth !== false )
{
$system = 'windows';
}
else if ( $stg !== false )
{
$system = 'windows';
}
else
{
if ( $stf !== false )
{
$system = 'mac';
}
else if ( $ste !== false )
{
$system = 'mac';
}
else
{
if ( $std !== false )
{
$system = 'os2';
}
else
{
if ( $stc !== false )
{
$system = 'unix';
}
else if ( $stb !== false )
{
$system = 'linux';
}
else if ( $sta !== false )
{
$system = 'unix';
}
else
{
$system = 'unknown';
}
}
}
}

return ( $system );
}

?>


Sonia

neocorteqz
03-12-2005, 08:32 AM
I was thinking of something based off of the existing code for is_browser


// #################### Start is browser ##########################
// browser detection script
function is_browser($browser, $version = 0)
{
global $_SERVER;
static $is;
if (!is_array($is))
{
$useragent = strtolower($_SERVER['HTTP_USER_AGENT']);
$is = array(
'opera' => 0,
'ie' => 0,
'mozilla' => 0,
'firebird' => 0,
'firefox' => 0,
'camino' => 0,
'konqueror' => 0,
'safari' => 0,
'webkit' => 0,
'webtv' => 0,
'netscape' => 0,
'mac' => 0
);

// detect opera
# Opera/7.11 (Windows NT 5.1; U) [en]
# Mozilla/4.0 (compatible; MSIE 6.0; MSIE 5.5; Windows NT 5.0) Opera 7.02 Bork-edition [en]
# Mozilla/4.0 (compatible; MSIE 6.0; MSIE 5.5; Windows NT 4.0) Opera 7.0 [en]
# Mozilla/4.0 (compatible; MSIE 5.0; Windows 2000) Opera 6.0 [en]
# Mozilla/4.0 (compatible; MSIE 5.0; Mac_PowerPC) Opera 5.0 [en]
if (strpos($useragent, 'opera') !== false)
{
preg_match('#opera(/| )([0-9\.]+)#', $useragent, $regs);
$is['opera'] = $regs[2];
}

// detect internet explorer
# Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; Q312461)
# Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.0.3705)
# Mozilla/4.0 (compatible; MSIE 5.22; Mac_PowerPC)
# Mozilla/4.0 (compatible; MSIE 5.0; Mac_PowerPC; e504460WanadooNL)
if (strpos($useragent, 'msie ') !== false AND !$is['opera'])
{
preg_match('#msie ([0-9\.]+)#', $useragent, $regs);
$is['ie'] = $regs[1];
}

// detect macintosh
if (strpos($useragent, 'mac') !== false)
{
$is['mac'] = 1;
}

// detect safari
# Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en-us) AppleWebKit/74 (KHTML, like Gecko) Safari/74
# Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/51 (like Gecko) Safari/51
if (strpos($useragent, 'applewebkit') !== false AND $is['mac'])
{
preg_match('#applewebkit/(\d+)#', $useragent, $regs);
$is['webkit'] = $regs[1];

if (strpos($useragent, 'safari') !== false)
{
preg_match('#safari/([0-9\.]+)#', $useragent, $regs);
$is['safari'] = $regs[1];
}
}

// detect konqueror
# Mozilla/5.0 (compatible; Konqueror/3.1; Linux; X11; i686)
# Mozilla/5.0 (compatible; Konqueror/3.1; Linux 2.4.19-32mdkenterprise; X11; i686; ar, en_US)
# Mozilla/5.0 (compatible; Konqueror/2.1.1; X11)
if (strpos($useragent, 'konqueror') !== false)
{
preg_match('#konqueror/([0-9\.-]+)#', $useragent, $regs);
$is['konqueror'] = $regs[1];
}

// detect mozilla
# Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.4b) Gecko/20030504 Mozilla
# Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.2a) Gecko/20020910
# Mozilla/5.0 (X11; U; Linux 2.4.3-20mdk i586; en-US; rv:0.9.1) Gecko/20010611
if (strpos($useragent, 'gecko') !== false AND !$is['safari'] AND !$is['konqueror'])
{
preg_match('#gecko/(\d+)#', $useragent, $regs);
$is['mozilla'] = $regs[1];

// detect firebird / firefox
# Mozilla/5.0 (Windows; U; WinNT4.0; en-US; rv:1.3a) Gecko/20021207 Phoenix/0.5
# Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.4b) Gecko/20030516 Mozilla Firebird/0.6
# Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.4a) Gecko/20030423 Firebird Browser/0.6
# Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.6) Gecko/20040206 Firefox/0.8
if (strpos($useragent, 'firefox') !== false OR strpos($useragent, 'firebird') !== false OR strpos($useragent, 'phoenix') !== false)
{
preg_match('#(phoenix|firebird|firefox)( browser)?/([0-9\.]+)#', $useragent, $regs);
$is['firebird'] = $regs[3];

if ($regs[1] == 'firefox')
{
$is['firefox'] = $regs[3];
}
}

// detect camino
# Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en-US; rv:1.0.1) Gecko/20021104 Chimera/0.6
if (strpos($useragent, 'chimera') !== false OR strpos($useragent, 'camino') !== false)
{
preg_match('#(chimera|camino)/([0-9\.]+)#', $useragent, $regs);
$is['camino'] = $regs[2];
}
}

// detect web tv
if (strpos($useragent, 'webtv') !== false)
{
preg_match('#webtv/([0-9\.]+)#', $useragent, $regs);
$is['webtv'] = $regs[1];
}

// detect pre-gecko netscape
if (preg_match('#mozilla/([1-4]{1})\.([0-9]{2}|[1-8]{1})#', $useragent, $regs))
{
$is['netscape'] = "$regs[1].$regs[2]";
}
}

// sanitize the incoming browser name
$browser = strtolower($browser);
if (substr($browser, 0, 3) == 'is_')
{
$browser = substr($browser, 3);
}

// return the version number of the detected browser if it is the same as $browser
if ($is["$browser"])
{
// $version was specified - only return version number if detected version is >= to specified $version
if ($version)
{
if ($is["$browser"] >= $version)
{
return $is["$browser"];
}
}
else
{
return $is["$browser"];
}
}

// if we got this far, we are not the specified browser, or the version number is too low
return 0;
}


So I can add it to functions php and make it usable forum wide.

But the code you posted works fine. :) Just i need to integrate it into vB for a conditional i can use anywhere. :)

like

<if condition="!is_os('win,mac')>
You must be using Windows or Mac to view the q3px plugin.
<else />
<code here>
</if>

Thanks.



Edit:
will this work?


// #################### Start is os ##########################
// os detection script
function is_os($os)
{
global $_SERVER;
static $is;
if (!is_array($is))
{
$useragent = strtolower($_SERVER['HTTP_USER_AGENT']);
$is = array(
'linux' => 0,
'mac' => 0,
'windows' => 0,
'webtv' => 0
);

// detect windows
if (strpos($useragent, 'winnt,win32,windows,32bit') !== false)
{
$is['windows'] = 1;
}

// detect macintosh
if (strpos($useragent, 'mac') !== false)
{
$is['mac'] = 1;
}


// detect linux
if (strpos($useragent, 'linux,x11,u,i686') !== false)
{
$is['linux'] = 1;
}


// detect web tv
if (strpos($useragent, 'webtv') !== false)
{
preg_match('#webtv/([0-9\.]+)#', $useragent, $regs);
$is['webtv'] = $regs[1];
}

}

// sanitize the incoming os name
$os = strtolower($os);
if (substr($os, 0, 3) == 'is_')
{
$os = substr($os, 3);
}

}
// #################### End is os ##########################