ILIAS  Release_4_2_x_branch Revision 61807
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilBrowser.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2011 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4  //**************************************************************************\
5  //* Browser detect functions *
6  // This file written by Miles Lott <milosch@phpgroupware.org> *
7  // Majority of code borrowed from Sourceforge 2.5 *
8  // Copyright 1999-2000 (c) The SourceForge Crew - http://sourceforge.net *
9  // Browser detection functions for phpGroupWare developers *
10  // -------------------------------------------------------------------------*
11  // This library is borrowed from the phpGroupWare API *
12  // http://www.phpgroupware.org/api *
13  // Modifications made by Sascha Hofmann <sascha.hofmann@uni-koeln.de> *
14  // *
15  //**************************************************************************/
16 
28 class ilBrowser
29 {
30  protected $BROWSER_AGENT;
31  protected $BROWSER_VER;
32  protected $BROWSER_PLATFORM;
33  protected $br;
34  protected $p;
35  protected $data;
36  protected $accept;
37  protected $userAgent;
38  protected $isMobile = false;
39  protected $isAndroid = null;
40  protected $isAndroidtablet = null;
41  protected $isIphone = null;
42  protected $isIpad = null;
43  protected $isBlackberry = null;
44  protected $isOpera = null;
45  protected $isPalm = null;
46  protected $isWindows = null;
47  protected $isWindowsphone = null;
48  protected $isGeneric = null;
49  protected $devices = array(
50  'android' => 'android.*mobile',
51  'androidtablet' => 'android(?!.*mobile)',
52  'blackberry' => 'blackberry',
53  'blackberrytablet' => 'rim tablet os',
54  'iphone' => '(iphone|ipod)',
55  'ipad' => '(ipad)',
56  'palm' => '(avantgo|blazer|elaine|hiptop|palm|plucker|xiino)',
57  'windows' => 'windows ce; (iemobile|ppc|smartphone)',
58  'windowsphone' => 'windows phone os',
59  'generic' => '(kindle|mobile|mmp|midp|o2|pda|pocket|psp|symbian|smartphone|treo|up.browser|up.link|vodafone|wap|opera mini)'
60  );
61 
69  public function __construct()
70  {
71  $HTTP_USER_AGENT = $_SERVER['HTTP_USER_AGENT'];
72  /*
73  Determine browser and version
74  */
75  if(preg_match('/MSIE ([0-9].[0-9]{1,2})/',$HTTP_USER_AGENT,$log_version))
76  {
77  $this->BROWSER_VER = $log_version[1];
78  $this->BROWSER_AGENT = 'IE';
79  }
80  elseif(preg_match('/Opera ([0-9].[0-9]{1,2})/',$HTTP_USER_AGENT,$log_version) ||
81  preg_match('/Opera\/([0-9].[0-9]{1,2})/',$HTTP_USER_AGENT,$log_version))
82  {
83  $this->BROWSER_VER = $log_version[1];
84  $this->BROWSER_AGENT = 'OPERA';
85  }
86  elseif(preg_match('/Safari ([0-9\/.]*)/',$HTTP_USER_AGENT,$log_version) ||
87  preg_match('/Safari\/([0-9\/.]*)/',$HTTP_USER_AGENT,$log_version))
88  {
89  $this->BROWSER_VER = $log_version[1];
90  $this->BROWSER_AGENT = 'Safari';
91  }
92  elseif(preg_match('/Firefox ([0-9\/.]*)/',$HTTP_USER_AGENT,$log_version) ||
93  preg_match('/Firefox\/([0-9\/.]*)/',$HTTP_USER_AGENT,$log_version))
94  {
95  $this->BROWSER_VER = $log_version[1];
96  $this->BROWSER_AGENT = 'Firefox';
97  }
98  elseif(preg_match('/iCab ([0-9].[0-9a-zA-Z]{1,4})/',$HTTP_USER_AGENT,$log_version) ||
99  preg_match('/iCab\/([0-9].[0-9a-zA-Z]{1,4})/',$HTTP_USER_AGENT,$log_version))
100  {
101  $this->BROWSER_VER = $log_version[1];
102  $this->BROWSER_AGENT = 'iCab';
103  }
104  elseif(preg_match('/Mozilla ([0-9].[0-9a-zA-Z]{1,4})/',$HTTP_USER_AGENT,$log_version) ||
105  preg_match('/Mozilla\/([0-9].[0-9a-zA-Z]{1,4})/',$HTTP_USER_AGENT,$log_version))
106  {
107  $this->BROWSER_VER = $log_version[1];
108  if (preg_match('/Gecko/',$HTTP_USER_AGENT,$log_version))
109  {
110  $this->BROWSER_AGENT = 'Mozilla';
111  }
112  else
113  {
114  $this->BROWSER_AGENT = 'Netscape';
115  }
116  }
117  elseif(preg_match('/Konqueror\/([0-9].[0-9].[0-9]{1,2})/',$HTTP_USER_AGENT,$log_version) ||
118  preg_match('/Konqueror\/([0-9].[0-9]{1,2})/',$HTTP_USER_AGENT,$log_version))
119  {
120  $this->BROWSER_VER=$log_version[1];
121  $this->BROWSER_AGENT='Konqueror';
122  }
123  else
124  {
125  $this->BROWSER_VER=0;
126  $this->BROWSER_AGENT='OTHER';
127  }
128 
129  /*
130  Determine platform
131  */
132  if(strstr($HTTP_USER_AGENT,'Win'))
133  {
134  $this->BROWSER_PLATFORM='Win';
135  }
136  elseif(strstr($HTTP_USER_AGENT,'Mac'))
137  {
138  $this->BROWSER_PLATFORM='Mac';
139  }
140  elseif(strstr($HTTP_USER_AGENT,'Linux'))
141  {
142  $this->BROWSER_PLATFORM='Linux';
143  }
144  elseif(strstr($HTTP_USER_AGENT,'Unix'))
145  {
146  $this->BROWSER_PLATFORM='Unix';
147  }
148  elseif(strstr($HTTP_USER_AGENT,'Beos'))
149  {
150  $this->BROWSER_PLATFORM='Beos';
151  }
152  else
153  {
154  $this->BROWSER_PLATFORM='Other';
155  }
156 
157  // Mobile detection
158  $this->userAgent = $_SERVER['HTTP_USER_AGENT'];
159  $this->accept = $_SERVER['HTTP_ACCEPT'];
160 
161  if(isset($_SERVER['HTTP_X_WAP_PROFILE']) || isset($_SERVER['HTTP_PROFILE']))
162  {
163  $this->isMobile = true;
164  }
165  else if(strpos($this->accept, 'text/vnd.wap.wml') > 0 || strpos($this->accept, 'application/vnd.wap.xhtml+xml') > 0)
166  {
167  $this->isMobile = true;
168  }
169  else
170  {
171  foreach($this->devices as $device => $regexp)
172  {
173  if($this->isDevice($device))
174  {
175  $this->isMobile = true;
176  }
177  }
178  }
179 
180 /*
181  echo "<br>Agent: $HTTP_USER_AGENT";
182  echo "<br><b>Browser</b>";
183  echo "<br>IE: ".$this->isIE();
184  echo "<br>Netscape: ".$this->isNetscape();
185  echo "<br>Mozilla: ".$this->isMozilla();
186  echo "<br>Firefox: ".$this->isFirefox();
187  echo "<br>Safari: ".$this->isSafari();
188  echo "<br>Opera: ".$this->isOpera();
189  echo "<br><b>OS</b>";
190  echo "<br>Mac: ".$this->isMac();
191  echo "<br>Windows: ".$this->isWindows();
192  echo "<br>Linux: ".$this->isLinux();
193  echo "<br>Unix: ".$this->isUnix();
194  echo "<br>Beos: ".$this->isBeos();
195  echo "<br><b>Summary</b>";
196  echo "<br>OS: ".$this->getPlatform();
197  echo "<br>Version: ".$this->getVersion(false);
198  echo "<br>Agent: ".$this->getAgent();
199 */
200 
201  // The br and p functions are supposed to return the correct
202  // value for tags that do not need to be closed. This is
203  // per the xhmtl spec, so we need to fix this to include
204  // all compliant browsers we know of.
205  if($this->BROWSER_AGENT == 'IE')
206  {
207  $this->br = '<br/>';
208  $this->p = '<p/>';
209  }
210  else
211  {
212  $this->br = '<br>';
213  $this->p = '<p>';
214  }
215  }
216 
217  public function isMobile()
218  {
219  return $this->isMobile;
220  }
221 
222  public function __call($name, $arguments)
223  {
224  $device = substr($name, 2);
225  if($name == 'is' . ucfirst($device) && array_key_exists(strtolower($device), $this->devices))
226  {
227  return $this->isDevice($device);
228  }
229  else
230  {
231  trigger_error('Method '.$name.' not defined', E_USER_WARNING);
232  }
233  }
234 
235  protected function isDevice($device)
236  {
237  $var = 'is' . ucfirst($device);
238  $return = $this->$var === null ? (bool) preg_match('/' . $this->devices[strtolower($device)] . '/i', $this->userAgent) : $this->$var;
239  if($device != 'generic' && $return == true)
240  {
241  $this->isGeneric = false;
242  }
243 
244  return $return;
245  }
246 
247  public function returnArray()
248  {
249  $this->data = array(
250  'agent' => $this->getAgent(),
251  'version' => $this->getVersion(false),
252  'platform' => $this->getPlatform()
253  );
254 
255  return $this->data;
256  }
257 
258  public function getAgent()
259  {
260  return $this->BROWSER_AGENT;
261  }
262 
263  public function getVersion($a_as_array = true)
264  {
265  return explode('.', $this->BROWSER_VER);
266  }
267 
268  public function getPlatform()
269  {
271  }
272 
273  public function isLinux()
274  {
275  if($this->getPlatform() == 'Linux')
276  {
277  return true;
278  }
279  else
280  {
281  return false;
282  }
283  }
284 
285  public function isUnix()
286  {
287  if($this->getPlatform() == 'Unix')
288  {
289  return true;
290  }
291  else
292  {
293  return false;
294  }
295  }
296 
297  public function isBeos()
298  {
299  if($this->getPlatform() == 'Beos')
300  {
301  return true;
302  }
303  else
304  {
305  return false;
306  }
307  }
308 
309  public function isMac()
310  {
311  if($this->getPlatform() == 'Mac')
312  {
313  return true;
314  }
315  else
316  {
317  return false;
318  }
319  }
320 
321  public function isWindows()
322  {
323  if($this->getPlatform() == 'Win')
324  {
325  return true;
326  }
327  else
328  {
329  return false;
330  }
331  }
332 
333  public function isIE()
334  {
335  if($this->getAgent() == 'IE')
336  {
337  return true;
338  }
339  else
340  {
341  return false;
342  }
343  }
344 
350  public function isNetscape()
351  {
352  if($this->getAgent() == 'Netscape')
353  {
354  return true;
355  }
356  else
357  {
358  return false;
359  }
360  }
361 
367  public function isMozilla()
368  {
369  if($this->getAgent() == 'Mozilla')
370  {
371  return true;
372  }
373  else
374  {
375  return false;
376  }
377  }
378 
379  public function isOpera()
380  {
381  if($this->getAgent() == 'OPERA')
382  {
383  return true;
384  }
385  else
386  {
387  return false;
388  }
389  }
390 
391  public function isSafari()
392  {
393  if($this->getAgent() == 'Safari')
394  {
395  return true;
396  }
397  else
398  {
399  return false;
400  }
401  }
402 
403  public function isFirefox()
404  {
405  if($this->getAgent() == 'Firefox')
406  {
407  return true;
408  }
409  else
410  {
411  return false;
412  }
413  }
414 }
415 ?>