summaryrefslogtreecommitdiff
path: root/browser.class
blob: e67b6584a74dbc18f4365959594c651d22b6c8c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
/*
**  $Id$
**
**	ITools - the Internet Tools Library
**
**	Copyright (C) 1995-2003 by the ITools Authors.
**	This program is free software; you can redistribute it and/or
**	modify it under the terms of either the GNU General Public License
**	or the GNU Lesser General Public License, as published by the Free
**	Software Foundation. See http://www.gnu.org/licenses/ for details.
**
**	Class it_Browser: Browser capability class
**	For a good list see http://www.msb.edu/dept/msbtc/waehner/browser/
**
**	Inspired by browser.pinc,v 1.1.1.1 1999/03/23 14:51:39 bobak Exp
*/

/**
 * Browser capabilities class
 * var $Type		string: Browser type, "Netscape", "MSIE", "Konqueror", "Opera", "Safari", "Gecko" etc.
 * var $Version		float:  Browser-Version x.y for all brands
 * var $Platform	string: "Win95", "Macintosh", "Linux" ... may be wrong
 * var $Language	string: Browser's language, "de", "en", ... or unset
 * var $HTMLVersion	float:  Suported HTML version x.y
 * var $JavaScript	bool:   Browser supports Javascript
 * var $CSS		bool:   Browser supports Style sheets
 * var $TextOnly	bool:   Browser supports only text, no graphics
 */
class it_browser
{
	/* Generic attributes */
	var $UserAgent;		/* string: Unparsed HTTP_USER_AGENT */
	var $Type = '';		/* string: Browser type, "Netscape", "MSIE", etc. */
	var $Version;		/* float:  Browser-Version x.y for all brands */
	var $Platform;		/* string: "Win95", "Macintosh", "Linux" ... may be wrong */
	var $Language;		/* string: Browser's language, "de", "en", ... or unset */
	var $HTMLVersion=0;	/* float:  Suported HTML version x.y */
	var $JavaScript=0;	/* bool:   Browser supports Javascript */
	var $CSS=1;			/* int:    Style sheet level (0..3) */
	var $CSSFilter=0;	/* bool:   Is MSIE and supports CSS filter: property */
	var $TextOnly=0;	/* bool:   Browser supports only text, no graphics */
	var $VML=0;			/* bool:   Browser support VectorMarkupLanguage */
	var $Canvas=0;		/* bool:   Supports Canvas */
	var $XMLHTTP=0;		/* bool:   Browser supports XMLHTTP requests */

	/* Browser specific stuff (DEPRECATED, use generic attributes above) */
	var $MSIE;		/* float:  Microsoft Internet Explorer Version or 0 */
	var $NS;		/* float:  Netscape Navigator Version or 0 */

/**
 * Constructor: Initializes public fields with browser capabilities
 */
function it_browser()
{
	$this->UserAgent = $_SERVER['HTTP_USER_AGENT'];
	/* $this->UserAgent = "Mozilla/4.0 [de] (compatible; MSIE 5.0; Bill Gates 1.0; Windows 95)"; */

	/* Find "Mozilla/4.0" */
	if (ereg("([^/]*)/([^ ]*)", $this->UserAgent, $regs))
	{
		$this->Type = $regs[1];
		$this->Version = (double)$regs[2];
	}

	/* Find optional "(compatible; MSIE 3.0; Win95)" */
	if (preg_match("/\((.+)\)/U", $this->UserAgent, $regs))
	{
		$optattr = preg_split("/;\s+/", $regs[1]);
		if ($optattr[0] == "compatible")
		{
			if (ereg("([^/]*)[/ ]([^/]*)", $optattr[1], $regs))
			{
				$this->Type = $regs[1];
				$this->Version = (double)$regs[2];
			}
			else
				$this->Type = $optattr[1];

			$this->Platform = $optattr[count($optattr)-1];
		}
		else
			$this->Platform = $optattr[0];

		for ($i=1; $i < count($optattr); $i++)
		{
			if (preg_match("/^([a-z]{2})(\-[a-z]{2})?$/i", $optattr[$i], $regs))
				$this->Language = $regs[1];
			if (preg_match("/^rv:([0-9\.]+)/", $optattr[$i], $regs))
				$this->Version = floatval($regs[1]);
			if (strstr($optattr[$i], "Linux") || $optattr[$i] == "X11")
				$this->Platform = "Linux";
		}
	}

	/* Find optional "[de]" */
	if (!$this->Language && ereg("\[(.*)\]", $this->UserAgent, $regs))
		$this->Language = $regs[1];

	/* Check for Safari/KHTML */
	if (preg_match("/(Safari|AppleWebKit)\/([0-9\.]+)/", $this->UserAgent, $regs))
	{
		$this->Type = "Safari";
		$this->Version = floatval($regs[2]);
	}

	/* Check for Gecko based browser */
	if ($this->Type == "Mozilla" && preg_match("/[^a-z]Gecko[^a-z]/", $this->UserAgent))
		$this->Type = "Gecko";
	else if ($this->Type == "Mozilla" && $this->Version < 5)
		$this->Type = "Netscape";


	/* And now for the browser capabilities ... */
	if ($this->Type == "MSIE")
	{
		$this->MSIE = $this->Version;
		$this->HTMLVersion = 4.0;
		$this->JavaScript  = true;
		$this->VML = ($this->Version >= 5.5);
		$this->XMLHTTP = ($this->Version >= 5);
		$this->CSS = $this->Version >= 4.0 ? 2 : 1;
		$this->CSSFilter = ($this->Version >= 5.0);
	}
	else if ($this->Type == "Opera")
	{
		$this->Opera = $this->Version;
		if ($this->Version >= 3.5)
		{
			$this->HTMLVersion = 4.0;
			$this->CSS = 2;
		}
		else
		{
			$this->HTMLVersion = 3.2;
			$this->CSS = 1;
		}
		$this->JavaScript = true;
		$this->XMLHTTP = ($this->Version >= 8);
	}
	else if ($this->Type == "Konqueror")
	{
		$this->HTMLVersion = 4.0;
		$this->CSS = 2;
		$this->JavaScript = true;
		$this->XMLHTTP = ($this->Version >= 3);
	}
	else if ($this->Type == "Netscape")
	{
		$this->NS = $this->Version;
		$this->HTMLVersion = 4.0;
		$this->JavaScript = true;

		if ($this->Version >= 5.0)
			$this->CSS = 2;
	}
	else if ($this->Type == "Gecko")
	{
		$this->HTMLVersion = 4.0;
		$this->CSS = 2;
		$this->JavaScript = true;
		$this->XMLHTTP = true;
		$this->Canvas = ($this->Version >= 1.8);
	}
	else if ($this->Type == "Safari")
	{
		$this->HTMLVersion = 4.0;
		$this->JavaScript = true;
		$this->CSS = 2;
		$this->XMLHTTP = true;
		$this->Canvas = ($this->Version >= 418);
	}
	else if ($this->Type == "Lynx")
		$this->TextOnly = 1;
}

} /* End Class it_Browser */
?>