IgtUserAgentParser.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. <?php
  2. namespace App\Game\Services;
  3. class IgtUserAgentParser
  4. {
  5. private $userAgent;
  6. private $parsed = [];
  7. public function __construct($userAgent = null)
  8. {
  9. $this->userAgent = $userAgent ?? ($_SERVER['HTTP_USER_AGENT'] ?? '');
  10. $this->parse();
  11. }
  12. private function parse()
  13. {
  14. $ua = $this->userAgent;
  15. $this->parsed['IsCrawler'] = $this->isCrawler($ua) ? 'Y' : 'N';
  16. $this->parsed['BrowserName'] = $this->getBrowserName($ua);
  17. $this->parsed['BrowserVersion'] = $this->getBrowserVersion($ua);
  18. $this->parsed['BrowserVendor'] = $this->getBrowserVendor($this->parsed['BrowserName']);
  19. $this->parsed['PlatformName'] = $this->getPlatformName($ua);
  20. $this->parsed['PlatformVersion'] = $this->getPlatformVersion($ua);
  21. $this->parsed['PlatformVendor'] = $this->getPlatformVendor($this->parsed['PlatformName']);
  22. $this->parsed['HardwareName'] = $this->getHardwareName($ua);
  23. $this->parsed['HardwareModel'] = $this->getHardwareModel($ua);
  24. $this->parsed['HardwareFamily'] = $this->getHardwareFamily($this->parsed['HardwareName']);
  25. $this->parsed['HardwareVendor'] = $this->getHardwareVendor($ua);
  26. $this->parsed['OEM'] = $this->parsed['HardwareVendor'];
  27. $this->parsed['ScreenPixelsWidth'] = $this->getScreenWidth($ua);
  28. $this->parsed['ScreenPixelsHeight'] = $this->getScreenHeight($ua);
  29. $this->parsed['ScreenInchesDiagonal'] = $this->getScreenDiagonal($ua);
  30. $this->parsed['ScreenInchesWidth'] = '0';
  31. $this->parsed['ScreenInchesHeight'] = '0';
  32. $this->parsed['Popularity'] = $this->getPopularity($ua);
  33. }
  34. public function getDeviceConfig()
  35. {
  36. return $this->parsed;
  37. }
  38. private function isCrawler($ua)
  39. {
  40. $crawlers = [
  41. 'bot', 'crawl', 'spider', 'slurp', 'mediapartners',
  42. 'google', 'yahoo', 'bing', 'facebook', 'twitter'
  43. ];
  44. $uaLower = strtolower($ua);
  45. foreach ($crawlers as $crawler) {
  46. if (strpos($uaLower, $crawler) !== false) {
  47. return true;
  48. }
  49. }
  50. return false;
  51. }
  52. private function getBrowserName($ua)
  53. {
  54. if (preg_match('/Edg(e|A|iOS)\//', $ua)) {
  55. return 'Microsoft Edge';
  56. }
  57. if (preg_match('/Chrome\//', $ua) && !preg_match('/Chromium\//', $ua)) {
  58. return 'Chrome';
  59. }
  60. if (preg_match('/Safari\//', $ua) && !preg_match('/Chrome\//', $ua)) {
  61. if (preg_match('/Mobile\//', $ua)) {
  62. return 'Mobile Safari';
  63. }
  64. return 'Safari';
  65. }
  66. if (preg_match('/Firefox\//', $ua)) {
  67. return 'Firefox';
  68. }
  69. if (preg_match('/Opera\/|OPR\//', $ua)) {
  70. return 'Opera';
  71. }
  72. if (preg_match('/MSIE |Trident\//', $ua)) {
  73. return 'Internet Explorer';
  74. }
  75. if (preg_match('/SamsungBrowser\//', $ua)) {
  76. return 'Samsung Browser';
  77. }
  78. if (preg_match('/UCBrowser\//', $ua)) {
  79. return 'UC Browser';
  80. }
  81. return 'Unknown';
  82. }
  83. private function getBrowserVersion($ua)
  84. {
  85. $patterns = [
  86. '/Edg(e|A|iOS)\/(\d+\.\d+)/',
  87. '/Chrome\/(\d+\.\d+)/',
  88. '/Safari\/(\d+\.\d+)/',
  89. '/Firefox\/(\d+\.\d+)/',
  90. '/OPR\/(\d+\.\d+)/',
  91. '/Version\/(\d+\.\d+)/',
  92. '/MSIE (\d+\.\d+)/',
  93. '/rv:(\d+\.\d+)/',
  94. ];
  95. foreach ($patterns as $pattern) {
  96. if (preg_match($pattern, $ua, $matches)) {
  97. return $matches[count($matches) - 1];
  98. }
  99. }
  100. return '0.0';
  101. }
  102. private function getBrowserVendor($browserName)
  103. {
  104. $vendors = [
  105. 'Chrome' => 'Google',
  106. 'Safari' => 'Apple',
  107. 'Mobile Safari' => 'Apple',
  108. 'Firefox' => 'Mozilla',
  109. 'Opera' => 'Opera Software',
  110. 'Microsoft Edge' => 'Microsoft',
  111. 'Internet Explorer' => 'Microsoft',
  112. 'Samsung Browser' => 'Samsung',
  113. 'UC Browser' => 'UCWeb',
  114. ];
  115. return $vendors[$browserName] ?? 'Unknown';
  116. }
  117. private function getPlatformName($ua)
  118. {
  119. if (preg_match('/Windows NT/', $ua)) {
  120. return 'Windows';
  121. }
  122. if (preg_match('/Mac OS X|MacPPC|MacIntel/', $ua)) {
  123. if (preg_match('/iPhone|iPod|iPad/', $ua)) {
  124. return 'iOS';
  125. }
  126. return 'macOS';
  127. }
  128. if (preg_match('/Android/', $ua)) {
  129. return 'Android';
  130. }
  131. if (preg_match('/Linux/', $ua)) {
  132. return 'Linux';
  133. }
  134. if (preg_match('/CrOS/', $ua)) {
  135. return 'Chrome OS';
  136. }
  137. return 'Unknown';
  138. }
  139. private function getPlatformVersion($ua)
  140. {
  141. if (preg_match('/OS (\d+)[_\.](\d+)/', $ua, $matches)) {
  142. return $matches[1] . '.' . $matches[2];
  143. }
  144. if (preg_match('/Android (\d+\.\d+)/', $ua, $matches)) {
  145. return $matches[1];
  146. }
  147. if (preg_match('/Windows NT (\d+\.\d+)/', $ua, $matches)) {
  148. $version = $matches[1];
  149. $windowsVersions = [
  150. '10.0' => '10',
  151. '6.3' => '8.1',
  152. '6.2' => '8',
  153. '6.1' => '7',
  154. '6.0' => 'Vista',
  155. '5.1' => 'XP',
  156. ];
  157. return $windowsVersions[$version] ?? $version;
  158. }
  159. if (preg_match('/Mac OS X (\d+)[_\.](\d+)/', $ua, $matches)) {
  160. return $matches[1] . '.' . $matches[2];
  161. }
  162. return '0.0';
  163. }
  164. private function getPlatformVendor($platformName)
  165. {
  166. $vendors = [
  167. 'iOS' => 'Apple',
  168. 'macOS' => 'Apple',
  169. 'Android' => 'Google',
  170. 'Windows' => 'Microsoft',
  171. 'Chrome OS' => 'Google',
  172. 'Linux' => 'Linux Foundation',
  173. ];
  174. return $vendors[$platformName] ?? 'Unknown';
  175. }
  176. private function getHardwareName($ua)
  177. {
  178. if (preg_match('/iPhone/', $ua)) {
  179. return 'iPhone';
  180. }
  181. if (preg_match('/iPad/', $ua)) {
  182. return 'iPad';
  183. }
  184. if (preg_match('/iPod/', $ua)) {
  185. return 'iPod';
  186. }
  187. if (preg_match('/SM-[A-Z]\d+/', $ua, $matches)) {
  188. return $matches[0];
  189. }
  190. if (preg_match('/(HW|HUAWEI)[- ]([A-Z0-9\-]+)/', $ua, $matches)) {
  191. return $matches[2];
  192. }
  193. if (preg_match('/MI [A-Z0-9]+|Redmi/', $ua, $matches)) {
  194. return $matches[0];
  195. }
  196. if (preg_match('/Android/', $ua)) {
  197. return 'Android Device';
  198. }
  199. if (preg_match('/Windows|Mac OS X|Linux/', $ua)) {
  200. return 'Desktop';
  201. }
  202. return 'Unknown Device';
  203. }
  204. private function getHardwareModel($ua)
  205. {
  206. if (preg_match('/iPhone/', $ua)) {
  207. return 'iPhone';
  208. }
  209. if (preg_match('/iPad/', $ua)) {
  210. return 'iPad';
  211. }
  212. if (preg_match('/\(([^)]*Android[^)]*)\)/', $ua, $matches)) {
  213. $parts = explode(';', $matches[1]);
  214. if (count($parts) >= 2) {
  215. return trim($parts[count($parts) - 1]);
  216. }
  217. }
  218. return $this->getHardwareName($ua);
  219. }
  220. private function getHardwareFamily($hardwareName)
  221. {
  222. if (strpos($hardwareName, 'iPhone') !== false) {
  223. return 'iPhone';
  224. }
  225. if (strpos($hardwareName, 'iPad') !== false) {
  226. return 'iPad';
  227. }
  228. if (strpos($hardwareName, 'iPod') !== false) {
  229. return 'iPod';
  230. }
  231. if (strpos($hardwareName, 'SM-') !== false) {
  232. return 'Samsung Galaxy';
  233. }
  234. if (strpos($hardwareName, 'Desktop') !== false) {
  235. return 'Desktop PC';
  236. }
  237. return 'Mobile';
  238. }
  239. private function getHardwareVendor($ua)
  240. {
  241. if (preg_match('/iPhone|iPad|iPod|Macintosh/', $ua)) {
  242. return 'Apple';
  243. }
  244. if (preg_match('/Samsung|SM-/', $ua)) {
  245. return 'Samsung';
  246. }
  247. if (preg_match('/Huawei|HW-|HUAWEI/', $ua)) {
  248. return 'Huawei';
  249. }
  250. if (preg_match('/Xiaomi|MI |Redmi/', $ua)) {
  251. return 'Xiaomi';
  252. }
  253. if (preg_match('/OPPO/', $ua)) {
  254. return 'OPPO';
  255. }
  256. if (preg_match('/vivo/', $ua)) {
  257. return 'Vivo';
  258. }
  259. if (preg_match('/OnePlus/', $ua)) {
  260. return 'OnePlus';
  261. }
  262. if (preg_match('/LG-/', $ua)) {
  263. return 'LG';
  264. }
  265. if (preg_match('/Sony/', $ua)) {
  266. return 'Sony';
  267. }
  268. if (preg_match('/Pixel/', $ua)) {
  269. return 'Google';
  270. }
  271. return 'Unknown';
  272. }
  273. private function getScreenWidth($ua)
  274. {
  275. if (preg_match('/iPhone/', $ua)) {
  276. return '750';
  277. }
  278. if (preg_match('/iPad/', $ua)) {
  279. return '2048';
  280. }
  281. if (preg_match('/Android.*Mobile/', $ua)) {
  282. return '1080';
  283. }
  284. if (preg_match('/Android/', $ua) && !preg_match('/Mobile/', $ua)) {
  285. return '1280';
  286. }
  287. return '1920';
  288. }
  289. private function getScreenHeight($ua)
  290. {
  291. if (preg_match('/iPhone/', $ua)) {
  292. return '1334';
  293. }
  294. if (preg_match('/iPad/', $ua)) {
  295. return '2732';
  296. }
  297. if (preg_match('/Android.*Mobile/', $ua)) {
  298. return '1920';
  299. }
  300. if (preg_match('/Android/', $ua) && !preg_match('/Mobile/', $ua)) {
  301. return '800';
  302. }
  303. return '1080';
  304. }
  305. private function getScreenDiagonal($ua)
  306. {
  307. if (preg_match('/iPhone/', $ua)) {
  308. return '4.7';
  309. }
  310. if (preg_match('/iPad/', $ua)) {
  311. return '10.2';
  312. }
  313. if (preg_match('/Android.*Mobile/', $ua)) {
  314. return '6.0';
  315. }
  316. if (preg_match('/Android/', $ua) && !preg_match('/Mobile/', $ua)) {
  317. return '10.0';
  318. }
  319. return '24.0';
  320. }
  321. private function getPopularity($ua)
  322. {
  323. if (preg_match('/iPhone/', $ua)) {
  324. return '9';
  325. }
  326. if (preg_match('/iPad/', $ua)) {
  327. return '8';
  328. }
  329. if (preg_match('/Samsung/', $ua)) {
  330. return '8';
  331. }
  332. if (preg_match('/Chrome/', $ua)) {
  333. return '9';
  334. }
  335. if (preg_match('/Safari/', $ua)) {
  336. return '7';
  337. }
  338. return '5';
  339. }
  340. public function getUserAgent()
  341. {
  342. return $this->userAgent;
  343. }
  344. public function get($field)
  345. {
  346. return $this->parsed[$field] ?? null;
  347. }
  348. public function getAll()
  349. {
  350. return $this->parsed;
  351. }
  352. }