-
Notifications
You must be signed in to change notification settings - Fork 0
/
useragent.class.php
111 lines (96 loc) · 2.81 KB
/
useragent.class.php
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
<?php
/**
* Useragent Class
* @package php-useragent
* @author zsx <[email protected]>
* @license GNU/GPL http://www.gnu.org/copyleft/gpl.html
*/
Class UserAgentFactory {
/**
* analyze
* @static
* @param string $string UserAgent String]
* @param string $imageSize Image Size(16 / 24)
* @param string $imagePath Image Path
* @param string $imageExtension Image Description
* @return UserAgent
*/
public static function analyze($string, $imageSize = null, $imagePath = null, $imageExtension = null) {
$class = new UserAgent();
$imageSize === null || $class->imageSize = $imageSize;
$imagePath === null || $class->imagePath = $imagePath;
$imageExtension === null || $class->imageExtension = $imageExtension;
$class->analyze($string);
return $class;
}
}
class UserAgent {
private $_imagePath = "";
private $_imageSize = 16;
private $_imageExtension = ".png";
private $_data = array();
public function __get($param) {
$privateParam = '_' . $param;
switch ($param) {
case 'imagePath':
return $this->_imagePath . $this->_imageSize . '/';
break;
default:
if (isset($this->$privateParam)) {
return $this->$privateParam;
} else if (isset($this->_data[$param])) {
return $this->_data[$param];
}
break;
}
return null;
}
public function __set($name, $value) {
$trueName = '_' . $name;
if (isset($this->$trueName)) {
$this->$trueName = $value;
}
}
public function __construct() {
$this->_imagePath = 'img/';
}
private function _makeImage($dir, $code) {
return $this->imagePath . $dir . '/' . $code . $this->imageExtension;
}
private function _makePlatform() {
$this->_data['platform'] = &$this->_data['device'];
if ($this->_data['device']['title'] != '') {
$this->_data['platform'] = &$this->_data['device'];
} else if ($this->_data['os']['title'] != '') {
$this->_data['platform'] = &$this->_data['os'];
} else {
$this->_data['platform'] = array(
"link" => "#",
"title" => "Unknown",
"code" => "null",
"dir" => "browser",
"image" => $this->_makeImage('browser', 'null'),
);
}
}
public static function __autoload($className) {
$filePath = dirname(__file__) . '/lib/' . $className . '.php';
if (is_file($filePath)) {
require_once $filePath;
}
}
public function analyze($string) {
$this->_data['useragent'] = $string;
$classList = array("device", "os", "browser");
foreach ($classList as $value) {
$class = "useragent_detect_" . $value;
// Not support in PHP 5.2
//$this->_data[$value] = $class::analyze($string);
$this->_data[$value] = call_user_func($class . '::analyze', $string);
$this->_data[$value]['image'] = $this->_makeImage($value, $this->_data[$value]['code']);
}
// platform
$this->_makePlatform();
}
}
spl_autoload_register(array('UserAgent', '__autoload'));