Subversion-Projekte lars-tiefland.ci

Revision

Revision 2257 | Details | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
68 lars 1
<?php
2
/**
3
 * CodeIgniter
4
 *
5
 * An open source application development framework for PHP
6
 *
7
 * This content is released under the MIT License (MIT)
8
 *
2414 lars 9
 * Copyright (c) 2014 - 2019, British Columbia Institute of Technology
68 lars 10
 *
11
 * Permission is hereby granted, free of charge, to any person obtaining a copy
12
 * of this software and associated documentation files (the "Software"), to deal
13
 * in the Software without restriction, including without limitation the rights
14
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15
 * copies of the Software, and to permit persons to whom the Software is
16
 * furnished to do so, subject to the following conditions:
17
 *
18
 * The above copyright notice and this permission notice shall be included in
19
 * all copies or substantial portions of the Software.
20
 *
21
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27
 * THE SOFTWARE.
28
 *
29
 * @package	CodeIgniter
30
 * @author	EllisLab Dev Team
31
 * @copyright	Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
2414 lars 32
 * @copyright	Copyright (c) 2014 - 2019, British Columbia Institute of Technology (https://bcit.ca/)
33
 * @license	https://opensource.org/licenses/MIT	MIT License
68 lars 34
 * @link	https://codeigniter.com
35
 * @since	Version 1.0.0
36
 * @filesource
37
 */
38
defined('BASEPATH') OR exit('No direct script access allowed');
39
 
40
/**
41
 * User Agent Class
42
 *
43
 * Identifies the platform, browser, robot, or mobile device of the browsing agent
44
 *
45
 * @package		CodeIgniter
46
 * @subpackage	Libraries
47
 * @category	User Agent
48
 * @author		EllisLab Dev Team
49
 * @link		https://codeigniter.com/user_guide/libraries/user_agent.html
50
 */
51
class CI_User_agent {
52
 
53
	/**
54
	 * Current user-agent
55
	 *
56
	 * @var string
57
	 */
58
	public $agent = NULL;
59
 
60
	/**
61
	 * Flag for if the user-agent belongs to a browser
62
	 *
63
	 * @var bool
64
	 */
65
	public $is_browser = FALSE;
66
 
67
	/**
68
	 * Flag for if the user-agent is a robot
69
	 *
70
	 * @var bool
71
	 */
72
	public $is_robot = FALSE;
73
 
74
	/**
75
	 * Flag for if the user-agent is a mobile browser
76
	 *
77
	 * @var bool
78
	 */
79
	public $is_mobile = FALSE;
80
 
81
	/**
82
	 * Languages accepted by the current user agent
83
	 *
84
	 * @var array
85
	 */
86
	public $languages = array();
87
 
88
	/**
89
	 * Character sets accepted by the current user agent
90
	 *
91
	 * @var array
92
	 */
93
	public $charsets = array();
94
 
95
	/**
96
	 * List of platforms to compare against current user agent
97
	 *
98
	 * @var array
99
	 */
100
	public $platforms = array();
101
 
102
	/**
103
	 * List of browsers to compare against current user agent
104
	 *
105
	 * @var array
106
	 */
107
	public $browsers = array();
108
 
109
	/**
110
	 * List of mobile browsers to compare against current user agent
111
	 *
112
	 * @var array
113
	 */
114
	public $mobiles = array();
115
 
116
	/**
117
	 * List of robots to compare against current user agent
118
	 *
119
	 * @var array
120
	 */
121
	public $robots = array();
122
 
123
	/**
124
	 * Current user-agent platform
125
	 *
126
	 * @var string
127
	 */
128
	public $platform = '';
129
 
130
	/**
131
	 * Current user-agent browser
132
	 *
133
	 * @var string
134
	 */
135
	public $browser = '';
136
 
137
	/**
138
	 * Current user-agent version
139
	 *
140
	 * @var string
141
	 */
142
	public $version = '';
143
 
144
	/**
145
	 * Current user-agent mobile name
146
	 *
147
	 * @var string
148
	 */
149
	public $mobile = '';
150
 
151
	/**
152
	 * Current user-agent robot name
153
	 *
154
	 * @var string
155
	 */
156
	public $robot = '';
157
 
158
	/**
159
	 * HTTP Referer
160
	 *
161
	 * @var	mixed
162
	 */
163
	public $referer;
164
 
165
	// --------------------------------------------------------------------
166
 
167
	/**
168
	 * Constructor
169
	 *
170
	 * Sets the User Agent and runs the compilation routine
171
	 *
172
	 * @return	void
173
	 */
174
	public function __construct()
175
	{
176
		$this->_load_agent_file();
177
 
178
		if (isset($_SERVER['HTTP_USER_AGENT']))
179
		{
180
			$this->agent = trim($_SERVER['HTTP_USER_AGENT']);
181
			$this->_compile_data();
182
		}
183
 
184
		log_message('info', 'User Agent Class Initialized');
185
	}
186
 
187
	// --------------------------------------------------------------------
188
 
189
	/**
190
	 * Compile the User Agent Data
191
	 *
192
	 * @return	bool
193
	 */
194
	protected function _load_agent_file()
195
	{
196
		if (($found = file_exists(APPPATH.'config/user_agents.php')))
197
		{
198
			include(APPPATH.'config/user_agents.php');
199
		}
200
 
201
		if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/user_agents.php'))
202
		{
203
			include(APPPATH.'config/'.ENVIRONMENT.'/user_agents.php');
204
			$found = TRUE;
205
		}
206
 
207
		if ($found !== TRUE)
208
		{
209
			return FALSE;
210
		}
211
 
212
		$return = FALSE;
213
 
214
		if (isset($platforms))
215
		{
216
			$this->platforms = $platforms;
217
			unset($platforms);
218
			$return = TRUE;
219
		}
220
 
221
		if (isset($browsers))
222
		{
223
			$this->browsers = $browsers;
224
			unset($browsers);
225
			$return = TRUE;
226
		}
227
 
228
		if (isset($mobiles))
229
		{
230
			$this->mobiles = $mobiles;
231
			unset($mobiles);
232
			$return = TRUE;
233
		}
234
 
235
		if (isset($robots))
236
		{
237
			$this->robots = $robots;
238
			unset($robots);
239
			$return = TRUE;
240
		}
241
 
242
		return $return;
243
	}
244
 
245
	// --------------------------------------------------------------------
246
 
247
	/**
248
	 * Compile the User Agent Data
249
	 *
250
	 * @return	bool
251
	 */
252
	protected function _compile_data()
253
	{
254
		$this->_set_platform();
255
 
256
		foreach (array('_set_robot', '_set_browser', '_set_mobile') as $function)
257
		{
258
			if ($this->$function() === TRUE)
259
			{
260
				break;
261
			}
262
		}
263
	}
264
 
265
	// --------------------------------------------------------------------
266
 
267
	/**
268
	 * Set the Platform
269
	 *
270
	 * @return	bool
271
	 */
272
	protected function _set_platform()
273
	{
274
		if (is_array($this->platforms) && count($this->platforms) > 0)
275
		{
276
			foreach ($this->platforms as $key => $val)
277
			{
278
				if (preg_match('|'.preg_quote($key).'|i', $this->agent))
279
				{
280
					$this->platform = $val;
281
					return TRUE;
282
				}
283
			}
284
		}
285
 
286
		$this->platform = 'Unknown Platform';
287
		return FALSE;
288
	}
289
 
290
	// --------------------------------------------------------------------
291
 
292
	/**
293
	 * Set the Browser
294
	 *
295
	 * @return	bool
296
	 */
297
	protected function _set_browser()
298
	{
299
		if (is_array($this->browsers) && count($this->browsers) > 0)
300
		{
301
			foreach ($this->browsers as $key => $val)
302
			{
303
				if (preg_match('|'.$key.'.*?([0-9\.]+)|i', $this->agent, $match))
304
				{
305
					$this->is_browser = TRUE;
306
					$this->version = $match[1];
307
					$this->browser = $val;
308
					$this->_set_mobile();
309
					return TRUE;
310
				}
311
			}
312
		}
313
 
314
		return FALSE;
315
	}
316
 
317
	// --------------------------------------------------------------------
318
 
319
	/**
320
	 * Set the Robot
321
	 *
322
	 * @return	bool
323
	 */
324
	protected function _set_robot()
325
	{
326
		if (is_array($this->robots) && count($this->robots) > 0)
327
		{
328
			foreach ($this->robots as $key => $val)
329
			{
330
				if (preg_match('|'.preg_quote($key).'|i', $this->agent))
331
				{
332
					$this->is_robot = TRUE;
333
					$this->robot = $val;
334
					$this->_set_mobile();
335
					return TRUE;
336
				}
337
			}
338
		}
339
 
340
		return FALSE;
341
	}
342
 
343
	// --------------------------------------------------------------------
344
 
345
	/**
346
	 * Set the Mobile Device
347
	 *
348
	 * @return	bool
349
	 */
350
	protected function _set_mobile()
351
	{
352
		if (is_array($this->mobiles) && count($this->mobiles) > 0)
353
		{
354
			foreach ($this->mobiles as $key => $val)
355
			{
356
				if (FALSE !== (stripos($this->agent, $key)))
357
				{
358
					$this->is_mobile = TRUE;
359
					$this->mobile = $val;
360
					return TRUE;
361
				}
362
			}
363
		}
364
 
365
		return FALSE;
366
	}
367
 
368
	// --------------------------------------------------------------------
369
 
370
	/**
371
	 * Set the accepted languages
372
	 *
373
	 * @return	void
374
	 */
375
	protected function _set_languages()
376
	{
377
		if ((count($this->languages) === 0) && ! empty($_SERVER['HTTP_ACCEPT_LANGUAGE']))
378
		{
379
			$this->languages = explode(',', preg_replace('/(;\s?q=[0-9\.]+)|\s/i', '', strtolower(trim($_SERVER['HTTP_ACCEPT_LANGUAGE']))));
380
		}
381
 
382
		if (count($this->languages) === 0)
383
		{
384
			$this->languages = array('Undefined');
385
		}
386
	}
387
 
388
	// --------------------------------------------------------------------
389
 
390
	/**
391
	 * Set the accepted character sets
392
	 *
393
	 * @return	void
394
	 */
395
	protected function _set_charsets()
396
	{
397
		if ((count($this->charsets) === 0) && ! empty($_SERVER['HTTP_ACCEPT_CHARSET']))
398
		{
399
			$this->charsets = explode(',', preg_replace('/(;\s?q=.+)|\s/i', '', strtolower(trim($_SERVER['HTTP_ACCEPT_CHARSET']))));
400
		}
401
 
402
		if (count($this->charsets) === 0)
403
		{
404
			$this->charsets = array('Undefined');
405
		}
406
	}
407
 
408
	// --------------------------------------------------------------------
409
 
410
	/**
411
	 * Is Browser
412
	 *
413
	 * @param	string	$key
414
	 * @return	bool
415
	 */
416
	public function is_browser($key = NULL)
417
	{
418
		if ( ! $this->is_browser)
419
		{
420
			return FALSE;
421
		}
422
 
423
		// No need to be specific, it's a browser
424
		if ($key === NULL)
425
		{
426
			return TRUE;
427
		}
428
 
429
		// Check for a specific browser
430
		return (isset($this->browsers[$key]) && $this->browser === $this->browsers[$key]);
431
	}
432
 
433
	// --------------------------------------------------------------------
434
 
435
	/**
436
	 * Is Robot
437
	 *
438
	 * @param	string	$key
439
	 * @return	bool
440
	 */
441
	public function is_robot($key = NULL)
442
	{
443
		if ( ! $this->is_robot)
444
		{
445
			return FALSE;
446
		}
447
 
448
		// No need to be specific, it's a robot
449
		if ($key === NULL)
450
		{
451
			return TRUE;
452
		}
453
 
454
		// Check for a specific robot
455
		return (isset($this->robots[$key]) && $this->robot === $this->robots[$key]);
456
	}
457
 
458
	// --------------------------------------------------------------------
459
 
460
	/**
461
	 * Is Mobile
462
	 *
463
	 * @param	string	$key
464
	 * @return	bool
465
	 */
466
	public function is_mobile($key = NULL)
467
	{
468
		if ( ! $this->is_mobile)
469
		{
470
			return FALSE;
471
		}
472
 
473
		// No need to be specific, it's a mobile
474
		if ($key === NULL)
475
		{
476
			return TRUE;
477
		}
478
 
479
		// Check for a specific robot
480
		return (isset($this->mobiles[$key]) && $this->mobile === $this->mobiles[$key]);
481
	}
482
 
483
	// --------------------------------------------------------------------
484
 
485
	/**
486
	 * Is this a referral from another site?
487
	 *
488
	 * @return	bool
489
	 */
490
	public function is_referral()
491
	{
492
		if ( ! isset($this->referer))
493
		{
494
			if (empty($_SERVER['HTTP_REFERER']))
495
			{
496
				$this->referer = FALSE;
497
			}
498
			else
499
			{
500
				$referer_host = @parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST);
501
				$own_host = parse_url(config_item('base_url'), PHP_URL_HOST);
502
 
503
				$this->referer = ($referer_host && $referer_host !== $own_host);
504
			}
505
		}
506
 
507
		return $this->referer;
508
	}
509
 
510
	// --------------------------------------------------------------------
511
 
512
	/**
513
	 * Agent String
514
	 *
515
	 * @return	string
516
	 */
517
	public function agent_string()
518
	{
519
		return $this->agent;
520
	}
521
 
522
	// --------------------------------------------------------------------
523
 
524
	/**
525
	 * Get Platform
526
	 *
527
	 * @return	string
528
	 */
529
	public function platform()
530
	{
531
		return $this->platform;
532
	}
533
 
534
	// --------------------------------------------------------------------
535
 
536
	/**
537
	 * Get Browser Name
538
	 *
539
	 * @return	string
540
	 */
541
	public function browser()
542
	{
543
		return $this->browser;
544
	}
545
 
546
	// --------------------------------------------------------------------
547
 
548
	/**
549
	 * Get the Browser Version
550
	 *
551
	 * @return	string
552
	 */
553
	public function version()
554
	{
555
		return $this->version;
556
	}
557
 
558
	// --------------------------------------------------------------------
559
 
560
	/**
561
	 * Get The Robot Name
562
	 *
563
	 * @return	string
564
	 */
565
	public function robot()
566
	{
567
		return $this->robot;
568
	}
569
	// --------------------------------------------------------------------
570
 
571
	/**
572
	 * Get the Mobile Device
573
	 *
574
	 * @return	string
575
	 */
576
	public function mobile()
577
	{
578
		return $this->mobile;
579
	}
580
 
581
	// --------------------------------------------------------------------
582
 
583
	/**
584
	 * Get the referrer
585
	 *
586
	 * @return	bool
587
	 */
588
	public function referrer()
589
	{
590
		return empty($_SERVER['HTTP_REFERER']) ? '' : trim($_SERVER['HTTP_REFERER']);
591
	}
592
 
593
	// --------------------------------------------------------------------
594
 
595
	/**
596
	 * Get the accepted languages
597
	 *
598
	 * @return	array
599
	 */
600
	public function languages()
601
	{
602
		if (count($this->languages) === 0)
603
		{
604
			$this->_set_languages();
605
		}
606
 
607
		return $this->languages;
608
	}
609
 
610
	// --------------------------------------------------------------------
611
 
612
	/**
613
	 * Get the accepted Character Sets
614
	 *
615
	 * @return	array
616
	 */
617
	public function charsets()
618
	{
619
		if (count($this->charsets) === 0)
620
		{
621
			$this->_set_charsets();
622
		}
623
 
624
		return $this->charsets;
625
	}
626
 
627
	// --------------------------------------------------------------------
628
 
629
	/**
630
	 * Test for a particular language
631
	 *
632
	 * @param	string	$lang
633
	 * @return	bool
634
	 */
635
	public function accept_lang($lang = 'en')
636
	{
637
		return in_array(strtolower($lang), $this->languages(), TRUE);
638
	}
639
 
640
	// --------------------------------------------------------------------
641
 
642
	/**
643
	 * Test for a particular character set
644
	 *
645
	 * @param	string	$charset
646
	 * @return	bool
647
	 */
648
	public function accept_charset($charset = 'utf-8')
649
	{
650
		return in_array(strtolower($charset), $this->charsets(), TRUE);
651
	}
652
 
653
	// --------------------------------------------------------------------
654
 
655
	/**
656
	 * Parse a custom user-agent string
657
	 *
658
	 * @param	string	$string
659
	 * @return	void
660
	 */
661
	public function parse($string)
662
	{
663
		// Reset values
664
		$this->is_browser = FALSE;
665
		$this->is_robot = FALSE;
666
		$this->is_mobile = FALSE;
667
		$this->browser = '';
668
		$this->version = '';
669
		$this->mobile = '';
670
		$this->robot = '';
671
 
672
		// Set the new user-agent string and parse it, unless empty
673
		$this->agent = $string;
674
 
675
		if ( ! empty($string))
676
		{
677
			$this->_compile_data();
678
		}
679
	}
680
 
681
}