1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Sergey Nehaenko <[email protected]> |
4
|
|
|
* @license GPL |
5
|
|
|
* @copyright Sergey Nehaenko © 2016 |
6
|
|
|
* @version 2.0 |
7
|
|
|
* @project browser-detector |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace EndorphinStudio\Detector; |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
class Detector |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @return string Path to directory with xml data files |
17
|
|
|
*/ |
18
|
|
|
public function getPathToData() |
19
|
|
|
{ |
20
|
|
|
return $this->PathToData; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @return array Xml data object |
25
|
|
|
*/ |
26
|
|
|
public function getXmlData() |
27
|
|
|
{ |
28
|
|
|
return $this->xmlData; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param string $PathToData |
33
|
|
|
*/ |
34
|
|
|
public function setPathToData($PathToData) |
35
|
|
|
{ |
36
|
|
|
$this->PathToData = $PathToData; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param array $xmlData Xml data object |
41
|
|
|
*/ |
42
|
|
|
public function setXmlData($xmlData) |
43
|
|
|
{ |
44
|
|
|
$this->xmlData = $xmlData; |
45
|
|
|
} |
46
|
|
|
/** @var string Path to directory with xml data */ |
47
|
|
|
private $PathToData; |
48
|
|
|
|
49
|
|
|
/** @var array Xml Data */ |
50
|
|
|
private $xmlData; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Detector constructor. |
54
|
|
|
* @param string $pathToData Path to directory with xml data files |
55
|
|
|
*/ |
56
|
|
|
private function __construct($pathToData='auto') |
57
|
|
|
{ |
58
|
|
|
if($pathToData == 'auto') |
59
|
|
|
{ |
60
|
|
|
$this->setPathToData(str_replace('src','data',__DIR__).'/'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$xml = array('browser','device','os','robot'); |
64
|
|
|
$xmlData = array(); |
65
|
|
|
foreach($xml as $name) |
66
|
|
|
{ |
67
|
|
|
$xmlData[$name] = simplexml_load_file($this->getPathToData().$name.'.xml'); |
68
|
|
|
} |
69
|
|
|
$this->setXmlData($xmlData); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public static function Analyse($uaString='UA',$pathToData='auto') |
|
|
|
|
73
|
|
|
{ |
74
|
|
|
$ua = $uaString; |
75
|
|
|
$pathXML = $pathToData; |
|
|
|
|
76
|
|
|
if($uaString == 'UA') |
77
|
|
|
{ |
78
|
|
|
$ua = $_SERVER['HTTP_USER_AGENT']; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$detector = new Detector($pathToData); |
82
|
|
|
$xml = $detector->getXmlData(); |
83
|
|
|
$data = array(); |
84
|
|
|
foreach($xml as $key => $item) |
85
|
|
|
{ |
86
|
|
|
$data[$key] = self::analysePart($xml,$key,$ua); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$detectorResult = new DetectorResult(); |
90
|
|
|
$detectorResult->uaString = $ua; |
91
|
|
|
|
92
|
|
|
foreach($data as $key => $result) |
93
|
|
|
{ |
94
|
|
|
switch($key) |
95
|
|
|
{ |
96
|
|
|
case 'os': |
97
|
|
|
$os = new OS($result); |
98
|
|
|
$os->Version = self::getVersion($result,$ua); |
99
|
|
|
$detectorResult->OS = $os; |
|
|
|
|
100
|
|
|
break; |
101
|
|
|
case 'device': |
102
|
|
|
$device = new Device($result); |
103
|
|
|
$detectorResult->Device = $device; |
|
|
|
|
104
|
|
|
break; |
105
|
|
|
case 'browser': |
106
|
|
|
$browser = new Browser($result); |
107
|
|
|
$browser->Version = self::getVersion($result,$ua); |
108
|
|
|
$detectorResult->Browser = $browser; |
|
|
|
|
109
|
|
|
break; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return $detectorResult; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param array $xmlData Xml data array |
118
|
|
|
* @param string $key Key in data array |
119
|
|
|
* @param string $uaString User agent |
120
|
|
|
* @return \SimpleXMLElement xml element |
121
|
|
|
*/ |
122
|
|
|
private static function analysePart($xmlData,$key,$uaString) |
123
|
|
|
{ |
124
|
|
|
$data = $xmlData[$key]->data; |
125
|
|
|
foreach($data as $xmlItem) |
126
|
|
|
{ |
127
|
|
|
$pattern = '/'.$xmlItem->pattern.'/'; |
128
|
|
|
if(preg_match($pattern,$uaString)) |
129
|
|
|
{ |
130
|
|
|
return $xmlItem; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
return null; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param \SimpleXMLElement $xmlItem xmlItem |
138
|
|
|
* @param string $uaString User agent |
139
|
|
|
* @return string Version |
140
|
|
|
*/ |
141
|
|
|
private static function getVersion($xmlItem,$uaString) |
142
|
|
|
{ |
143
|
|
|
$vPattern = $xmlItem->versionPattern; |
|
|
|
|
144
|
|
|
$version = @'/'.$vPattern.'(\/| )[\w-._]{1,15}/'; |
145
|
|
|
$uaString = str_replace(' NT','',$uaString); |
146
|
|
|
if(preg_match($version,$uaString)) |
147
|
|
|
{ |
148
|
|
|
preg_match($version,$uaString,$v); |
149
|
|
|
@$version = $v[0]; |
|
|
|
|
150
|
|
|
$version = preg_replace('/'.$vPattern.'/','',$version); |
151
|
|
|
$version = str_replace(';','',$version); |
152
|
|
|
$version = str_replace(' ','',$version); |
153
|
|
|
$version = str_replace('/','',$version); |
154
|
|
|
$version = str_replace('_','.',$version); |
155
|
|
|
|
156
|
|
|
if($xmlItem->id == 'Windows') |
157
|
|
|
{ |
158
|
|
|
$version = self::getWindowsVersion($version); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
return $version; |
162
|
|
|
} |
163
|
|
|
return null; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @param $version Windows number version |
168
|
|
|
* @return string Windows version |
169
|
|
|
*/ |
170
|
|
|
private static function getWindowsVersion($version) |
171
|
|
|
{ |
172
|
|
|
$versions = array( |
173
|
|
|
'5.1' => 'XP', |
174
|
|
|
'5.2' => 'Server 2003', |
175
|
|
|
'6.0' => 'Vista', |
176
|
|
|
'6.1' => '7', |
177
|
|
|
'6.2' => '8', |
178
|
|
|
'6.3' => '8.1', |
179
|
|
|
'6.4' => '10' |
180
|
|
|
); |
181
|
|
|
|
182
|
|
|
return $versions[$version]; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
} |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: