1<?php
2/**
3	Standalone version of get_browser() in PHP
4		http://www.php.net/manual/function.get-browser.php
5	Detection of the capacities of a Web browser client.
6	Requires a compatible browscap.ini database,
7		such as php_browscap.ini on
8		http://browsers.garykeith.com/downloads.asp
9
10	Interface:
11	- function get_browser_local($user_agent=null,$return_array=false,$db='./browscap.ini',$cache=false)
12		[Implied] $user_agent=null: The signature of the browser to be analysed. If this parameter is left to null, then it uses $_SERVER['HTTP_USER_AGENT'].
13		[Implied] $return_array=false: When this parameter is activated, the function returns an array instead of an object.
14		[Implied] $db='./browscap.ini': Allows specifying the path of the browscap.ini database, otherwise assumes that it is in the current directory.
15		[Implied] $cache=false: Specify if the database can be kept in memory, to improve performances when querying this function more than once.
16		Returns: An object (or an array, if asked to do so) with the capacities of the browser.
17
18	Typical use:
19	{
20		if (get_cfg_var('browscap'))
21			$browser=get_browser(); //If available, use PHP native function
22		else
23		{
24			require_once('php-local-browscap.php');
25			$browser=get_browser_local();
26		}
27		print_r($browser);
28	}
29
30	Version 1.5, 2010-02-06, http://alexandre.alapetite.fr/doc-alex/php-local-browscap/
31
32	------------------------------------------------------------------
33	Written by Alexandre Alapetite, http://alexandre.alapetite.fr/cv/
34
35	Copyright 2005-2010, Licence: Creative Commons "Attribution-ShareAlike 2.0 France" BY-SA (FR),
36	http://creativecommons.org/licenses/by-sa/2.0/fr/
37	http://alexandre.alapetite.fr/divers/apropos/#by-sa
38	- Attribution. You must give the original author credit
39	- Share Alike. If you alter, transform, or build upon this work,
40			you may distribute the resulting work only under a license identical to this one
41			(Can be included in GPL/LGPL projects)
42	- The French law is authoritative
43	- Any of these conditions can be waived if you get permission from Alexandre Alapetite
44	- Please send to Alexandre Alapetite the modifications you make,
45			in order to improve this file for the benefit of everybody
46
47	If you want to distribute this code, please do it as a link to:
48	http://alexandre.alapetite.fr/doc-alex/php-local-browscap/
49*/
50
51$browscapIni=null;	//Cache
52$browscapPath='';	//Cached database
53
54function _sortBrowscap($a,$b)
55{
56	$sa=strlen($a);
57	$sb=strlen($b);
58	if ($sa>$sb) return -1;
59	elseif ($sa<$sb) return 1;
60	else return strcasecmp($a,$b);
61}
62
63function _lowerBrowscap($r) {return array_change_key_case($r,CASE_LOWER);}
64
65function get_browser_local($user_agent=null,$return_array=false,$db='./browscap.ini',$cache=false)
66{//http://alexandre.alapetite.fr/doc-alex/php-local-browscap/
67	//Get php_browscap.ini on http://browsers.garykeith.com/downloads.asp
68	if (($user_agent==null)&&isset($_SERVER['HTTP_USER_AGENT'])) $user_agent=$_SERVER['HTTP_USER_AGENT'];
69	global $browscapIni;
70	global $browscapPath;
71	if ((!isset($browscapIni))||(!$cache)||($browscapPath!==$db))
72	{
73		$browscapIni=defined('INI_SCANNER_RAW') ? parse_ini_file($db,true,INI_SCANNER_RAW) : parse_ini_file($db,true);
74		$browscapPath=$db;
75		uksort($browscapIni,'_sortBrowscap');
76		$browscapIni=array_map('_lowerBrowscap',$browscapIni);
77	}
78	$cap=null;
79	foreach ($browscapIni as $key=>$value)
80	{
81		if (($key!='*')&&(!array_key_exists('parent',$value))) continue;
82		$keyEreg='^'.str_replace(
83			array('\\','.','?','*','^','$','[',']','|','(',')','+','{','}','%'),
84			array('\\\\','\\.','.','.*','\\^','\\$','\\[','\\]','\\|','\\(','\\)','\\+','\\{','\\}','\\%'),
85			$key).'$';
86		if (preg_match('%'.$keyEreg.'%i',$user_agent))
87		{
88			$cap=array('browser_name_regex'=>strtolower($keyEreg),'browser_name_pattern'=>$key)+$value;
89			$maxDeep=8;
90			while (array_key_exists('parent',$value)&&array_key_exists($parent=$value['parent'],$browscapIni)&&(--$maxDeep>0))
91				$cap+=($value=$browscapIni[$parent]);
92			break;
93		}
94	}
95	if (!$cache) $browscapIni=null;
96	return $return_array ? $cap : (object)$cap;
97}
98