DeviceModels.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. <?php
  2. namespace WhichBrowser\Data;
  3. use WhichBrowser\Constants;
  4. use WhichBrowser\Model\Device;
  5. class DeviceModels
  6. {
  7. public static $ANDROID_MODELS = [];
  8. public static $ASHA_MODELS = [];
  9. public static $BADA_MODELS = [];
  10. public static $BREW_MODELS = [];
  11. public static $FIREFOXOS_MODELS = [];
  12. public static $TIZEN_MODELS = [];
  13. public static $TOUCHWIZ_MODELS = [];
  14. public static $WM_MODELS = [];
  15. public static $WP_MODELS = [];
  16. public static $PALMOS_MODELS = [];
  17. public static $S30PLUS_MODELS = [];
  18. public static $S40_MODELS = [];
  19. public static $SYMBIAN_MODELS = [];
  20. public static $FEATURE_MODELS = [];
  21. public static $BLACKBERRY_MODELS = [];
  22. public static $IOS_MODELS = [];
  23. public static $KDDI_MODELS = [];
  24. public static $ANDROID_INDEX = [];
  25. public static $ASHA_INDEX = [];
  26. public static $BADA_INDEX = [];
  27. public static $BREW_INDEX = [];
  28. public static $FIREFOXOS_INDEX = [];
  29. public static $TIZEN_INDEX = [];
  30. public static $TOUCHWIZ_INDEX = [];
  31. public static $WM_INDEX = [];
  32. public static $WP_INDEX = [];
  33. public static $PALMOS_INDEX = [];
  34. public static $S30PLUS_INDEX = [];
  35. public static $S40_INDEX = [];
  36. public static $SYMBIAN_INDEX = [];
  37. public static $FEATURE_INDEX = [];
  38. public static $KDDI_INDEX = [];
  39. public static function identify($type, $model)
  40. {
  41. require_once __DIR__ . '/../../data/models-' . $type . '.php';
  42. if ($type != 'blackberry' && $type != 'ios') {
  43. require_once __DIR__ . '/../../data/indices/models-' . $type . '.php';
  44. }
  45. switch ($type) {
  46. case 'android':
  47. return self::identifyAndroid($model);
  48. case 'asha':
  49. return self::identifyList(self::$ASHA_INDEX, self::$ASHA_MODELS, $model);
  50. case 'bada':
  51. return self::identifyList(self::$BADA_INDEX, self::$BADA_MODELS, $model);
  52. case 'blackberry':
  53. return self::identifyBlackBerry($model);
  54. case 'brew':
  55. return self::identifyList(self::$BREW_INDEX, self::$BREW_MODELS, $model);
  56. case 'firefoxos':
  57. return self::identifyList(self::$FIREFOXOS_INDEX, self::$FIREFOXOS_MODELS, $model, false);
  58. case 'ios':
  59. return self::identifyIOS($model);
  60. case 'tizen':
  61. return self::identifyList(self::$TIZEN_INDEX, self::$TIZEN_MODELS, $model);
  62. case 'touchwiz':
  63. return self::identifyList(self::$TOUCHWIZ_INDEX, self::$TOUCHWIZ_MODELS, $model);
  64. case 'wm':
  65. return self::identifyWindowsMobile($model);
  66. case 'wp':
  67. return self::identifyList(self::$WP_INDEX, self::$WP_MODELS, $model);
  68. case 's30plus':
  69. return self::identifyList(self::$S30PLUS_INDEX, self::$S30PLUS_MODELS, $model);
  70. case 's40':
  71. return self::identifyList(self::$S40_INDEX, self::$S40_MODELS, $model);
  72. case 'symbian':
  73. return self::identifyList(self::$SYMBIAN_INDEX, self::$SYMBIAN_MODELS, $model);
  74. case 'palmos':
  75. return self::identifyList(self::$PALMOS_INDEX, self::$PALMOS_MODELS, $model);
  76. case 'kddi':
  77. return self::identifyList(self::$KDDI_INDEX, self::$KDDI_MODELS, $model);
  78. }
  79. require_once __DIR__ . '/../../data/models-feature.php';
  80. require_once __DIR__ . '/../../data/indices/models-feature.php';
  81. return self::identifyList(self::$FEATURE_INDEX, self::$FEATURE_MODELS, $model);
  82. }
  83. public static function identifyWindowsMobile($model)
  84. {
  85. $model = preg_replace('/^(HTC|SAMSUNG|SHARP|Toshiba)\//u', '', $model);
  86. return self::identifyList(self::$WM_INDEX, self::$WM_MODELS, $model);
  87. }
  88. public static function identifyIOS($model)
  89. {
  90. $original = $model;
  91. $model = str_replace('Unknown ', '', $model);
  92. $model = preg_replace("/iPh([0-9],[0-9])/", 'iPhone\\1', $model);
  93. $model = preg_replace("/iPd([0-9],[0-9])/", 'iPod\\1', $model);
  94. $device = new Device([
  95. 'type' => Constants\DeviceType::MOBILE,
  96. 'identified' => Constants\Id::NONE,
  97. 'manufacturer' => null,
  98. 'model' => $model,
  99. 'identifier' => $original,
  100. 'generic' => false
  101. ]);
  102. if (isset(self::$IOS_MODELS[$model])) {
  103. $match = self::$IOS_MODELS[$model];
  104. $device->manufacturer = $match[0];
  105. $device->model = $match[1];
  106. $device->identified = Constants\Id::MATCH_UA;
  107. if (isset($match[2]) || isset($match['type'])) {
  108. $type = isset($match[2]) ? $match[2] : $match['type'];
  109. $device->type = $type;
  110. }
  111. return $device;
  112. }
  113. return $device;
  114. }
  115. public static function identifyBlackBerry($model)
  116. {
  117. $original = $model;
  118. if (preg_match("/BlackBerry ?([0-9]+)/ui", $model, $match)) {
  119. $model = $match[1];
  120. }
  121. $device = new Device([
  122. 'type' => Constants\DeviceType::MOBILE,
  123. 'identified' => Constants\Id::NONE,
  124. 'manufacturer' => null,
  125. 'model' => $model,
  126. 'identifier' => $original,
  127. 'generic' => false
  128. ]);
  129. if (preg_match("/^[1-9][0-9][0-9][0-9][ei]?$/u", $model)) {
  130. $device->manufacturer = 'RIM';
  131. $device->model = 'BlackBerry ' . $model;
  132. $device->identified = Constants\Id::PATTERN;
  133. if (isset(self::$BLACKBERRY_MODELS[$model])) {
  134. $device->model = 'BlackBerry ' . self::$BLACKBERRY_MODELS[$model] . ' ' . $model;
  135. $device->identified |= Constants\Id::MATCH_UA;
  136. }
  137. }
  138. return $device;
  139. }
  140. public static function identifyAndroid($model)
  141. {
  142. $result = self::identifyList(self::$ANDROID_INDEX, self::$ANDROID_MODELS, $model);
  143. if (!$result->identified) {
  144. $model = self::cleanup($model);
  145. if (preg_match('/AndroVM/iu', $model) || $model == 'Emulator' || $model == 'x86 Emulator' || $model == 'x86 VirtualBox' || $model == 'vm') {
  146. return new Device([
  147. 'type' => Constants\DeviceType::EMULATOR,
  148. 'identified' => Constants\Id::PATTERN,
  149. 'manufacturer' => null,
  150. 'model' => null,
  151. 'generic' => false
  152. ]);
  153. }
  154. }
  155. return $result;
  156. }
  157. public static function identifyList(&$index, &$list, $model, $cleanup = true)
  158. {
  159. $original = $model;
  160. if ($cleanup) {
  161. $model = self::cleanup($model);
  162. }
  163. $device = new Device([
  164. 'type' => Constants\DeviceType::MOBILE,
  165. 'identified' => Constants\Id::NONE,
  166. 'manufacturer' => null,
  167. 'model' => $model,
  168. 'identifier' => $original,
  169. 'generic' => false
  170. ]);
  171. $keys = [ '@' . strtoupper(substr($model, 0, 2)), '@' ];
  172. $pattern = null;
  173. $match = null;
  174. foreach ($keys as $k => $key) {
  175. if (isset($index[$key])) {
  176. foreach ($index[$key] as $m => $v) {
  177. if (self::hasMatch($v, $model)) {
  178. if ($v) {
  179. if (substr($v, -2) == "!!") {
  180. foreach ($list[$v] as $m2 => $v2) {
  181. if (self::hasMatch($m2, $model)) {
  182. $match = $v2;
  183. $pattern = $m2;
  184. break;
  185. }
  186. }
  187. } else {
  188. $match = $list[$v];
  189. $pattern = $v;
  190. }
  191. }
  192. if ($match) {
  193. $device->manufacturer = $match[0];
  194. $device->model = self::applyMatches($match[1], $model, $pattern);
  195. $device->identified = Constants\Id::MATCH_UA;
  196. if (isset($match[2]) || isset($match['type'])) {
  197. $type = isset($match[2]) ? $match[2] : $match['type'];
  198. if (is_array($type)) {
  199. $device->type = $type[0];
  200. $device->subtype = $type[1];
  201. } else {
  202. $device->type = $type;
  203. }
  204. }
  205. if (isset($match[3]) || isset($match['flag'])) {
  206. $device->flag = isset($match[3]) ? $match[3] : $match['flag'];
  207. }
  208. if (isset($match['carrier'])) {
  209. $device->carrier = $match['carrier'];
  210. }
  211. if ($device->manufacturer == null && $device->model == null) {
  212. $device->identified = Constants\Id::PATTERN;
  213. }
  214. return $device;
  215. }
  216. }
  217. }
  218. }
  219. }
  220. return $device;
  221. }
  222. public static function applyMatches($model, $original, $pattern)
  223. {
  224. if (strpos($model, '$') !== false && substr($pattern, -1) == "!") {
  225. if (preg_match('/^' . substr($pattern, 0, -1) . '/iu', $original, $matches)) {
  226. foreach ($matches as $k => $v) {
  227. $model = str_replace('$' . $k, $v, $model);
  228. }
  229. }
  230. }
  231. return $model;
  232. }
  233. public static function hasMatch($pattern, $model)
  234. {
  235. if (substr($pattern, -2) == "!!") {
  236. return !! preg_match('/^' . substr($pattern, 0, -2) . '/iu', $model);
  237. } elseif (substr($pattern, -1) == "!") {
  238. return !! preg_match('/^' . substr($pattern, 0, -1) . '/iu', $model);
  239. } else {
  240. return strtolower($pattern) == strtolower($model);
  241. }
  242. }
  243. public static function cleanup($s = '')
  244. {
  245. // var_dump($s);
  246. $s = preg_replace('/^phone\//', '', $s);
  247. $s = preg_replace('/^(\/|; |;)/u', '', $s);
  248. $s = preg_replace('/\/[^\/]+$/u', '', $s);
  249. $s = preg_replace('/\/[^\/]+ Android\/.*/u', '', $s);
  250. $s = preg_replace('/UCBrowser$/u', '', $s);
  251. $s = preg_replace('/(_TD|_LTE|_GPRS|_BLEU|_CMCC|_CMCC_TD|_CUCC)$/u', '', $s);
  252. $s = preg_replace('/(-BREW| MIDP).+$/u', '', $s);
  253. $s = preg_replace('/ AU-MIC.+$/u', '', $s);
  254. $s = preg_replace('/ (AU|UP)\.Browser$/u', '', $s);
  255. $s = preg_replace('/_/u', ' ', $s);
  256. $s = preg_replace('/^\*+/u', '', $s);
  257. $s = preg_replace('/^\s+|\s+$/u', '', $s);
  258. $s = preg_replace('/^De-Sensed /u', '', $s);
  259. $s = preg_replace('/^Full JellyBean( on )?/u', '', $s);
  260. $s = preg_replace('/^(Android|Generic Android|Baidu Yi|Buildroid|Gingerbread|ICS AOSP|AOSPA?|tita) (on |for )/u', '', $s);
  261. $s = preg_replace('/^Full (AOSP on |Android on |Base for |Cappuccino on |MIPS Android on |Webdroid on |JellyBean on |Android)/u', '', $s);
  262. $s = preg_replace('/^Acer( |-)?/iu', '', $s);
  263. $s = preg_replace('/^Iconia( Tab)? /u', '', $s);
  264. $s = preg_replace('/^ASUS ?/u', '', $s);
  265. $s = preg_replace('/^Ainol /u', '', $s);
  266. $s = preg_replace('/^Coolpad-?Coolpad/iu', 'Coolpad', $s);
  267. $s = preg_replace('/^Coolpad ?/iu', 'Coolpad ', $s);
  268. $s = preg_replace('/^Alcatel[_ ]OT[_-](.*)/iu', 'One Touch $1', $s);
  269. $s = preg_replace('/^ALCATEL /u', '', $s);
  270. $s = preg_replace('/^YL-/u', '', $s);
  271. $s = preg_replace('/^TY-K[_\- ]Touch/iu', 'K-Touch', $s);
  272. $s = preg_replace('/^K-Touch[_\-]/u', 'K-Touch ', $s);
  273. $s = preg_replace('/^Novo7 ?/iu', 'Novo7 ', $s);
  274. $s = preg_replace('/^HW-HUAWEI/u', 'HUAWEI', $s);
  275. $s = preg_replace('/^Huawei[ -]/iu', 'Huawei ', $s);
  276. $s = preg_replace('/^SAMSUNG SAMSUNG-/iu', '', $s);
  277. $s = preg_replace('/^SAMSUNG[ -]/iu', '', $s);
  278. $s = preg_replace('/^(Sony ?Ericsson|Sony)/u', '', $s);
  279. $s = preg_replace('/^(Lenovo Lenovo|LNV-Lenovo|LENOVO-Lenovo)/u', 'Lenovo', $s);
  280. $s = preg_replace('/^Lenovo-/u', 'Lenovo', $s);
  281. $s = preg_replace('/^Lenovo/u', 'Lenovo ', $s);
  282. $s = preg_replace('/^ZTE-/u', 'ZTE ', $s);
  283. $s = preg_replace('/^(LG)[ _\/]/u', '$1-', $s);
  284. $s = preg_replace('/^(HTC.+)\s[v|V][0-9.]+$/u', '$1', $s);
  285. $s = preg_replace('/^(HTC)[-\/]/u', '$1 ', $s);
  286. $s = preg_replace('/^(HTC)([A-Z][0-9][0-9][0-9])/u', '$1 $2', $s);
  287. $s = preg_replace('/^(Motorola MOT-|MOT-|Motorola[\s|-])/u', '', $s);
  288. $s = preg_replace('/^Moto([^\s])/u', '$1', $s);
  289. $s = preg_replace('/^(UTStar-)/u', '', $s);
  290. $s = preg_replace('/^VZW:/iu', '', $s);
  291. $s = preg_replace('/^(Swisscom|Vodafone)\/1.0\//iu', '', $s);
  292. $s = preg_replace('/-?(orange(-ls)?|vodafone|bouygues|parrot|Kust)$/iu', '', $s);
  293. $s = preg_replace('/[ -](Mozilla|Opera|Obigo|Java|PPC)$/iu', '', $s);
  294. $s = preg_replace('/ ?Build$/iu', '', $s);
  295. $s = preg_replace('/ \(compatible$/iu', '', $s);
  296. $s = preg_replace('/http:\/\/.+$/iu', '', $s);
  297. $s = preg_replace('/^\s+|\s+$/u', '', $s);
  298. $s = preg_replace('/\s+/u', ' ', $s);
  299. return $s;
  300. }
  301. }