device_detector 1.1.2 → 1.1.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -14,12 +14,12 @@ class DeviceDetector
14
14
  return if headers.nil?
15
15
 
16
16
  @headers = headers
17
- @full_version = headers['Sec-CH-UA-Full-Version']
17
+ @full_version = extract_full_version
18
18
  @browser_list = extract_browser_list
19
19
  @app_name = extract_app_name
20
- @platform = headers['Sec-CH-UA-Platform']
20
+ @platform = extract_platform
21
21
  @platform_version = extract_platform_version
22
- @mobile = headers['Sec-CH-UA-Mobile']
22
+ @mobile = extract_mobile
23
23
  @model = extract_model
24
24
  end
25
25
 
@@ -27,19 +27,24 @@ class DeviceDetector
27
27
  :platform_version
28
28
 
29
29
  def browser_name
30
- return 'Iridium' if is_iridium?
30
+ return 'Iridium' if iridium?
31
+ return '360 Secure Browser' if secure_browser?
31
32
 
32
33
  browser_name_from_list || app_name
33
34
  end
34
35
 
35
36
  def os_version
36
37
  return windows_version if platform == 'Windows'
38
+ return lineage_version if lineage_os_app?
39
+ return fire_os_version if fire_os_app?
37
40
 
38
41
  platform_version
39
42
  end
40
43
 
41
44
  def os_name
42
45
  return 'Android' if android_app?
46
+ return 'Lineage OS' if lineage_os_app?
47
+ return 'Fire OS' if fire_os_app?
43
48
  return unless ['Windows', 'Chromium OS'].include?(platform)
44
49
 
45
50
  platform
@@ -59,21 +64,27 @@ class DeviceDetector
59
64
 
60
65
  private
61
66
 
62
- def extract_platform_version
63
- return if headers['Sec-CH-UA-Platform-Version'].nil?
64
- return if headers['Sec-CH-UA-Platform-Version'] == ''
65
-
66
- headers['Sec-CH-UA-Platform-Version']
67
- end
68
-
69
67
  # https://github.com/matomo-org/device-detector/blob/28211c6f411528abf41304e07b886fdf322a49b7/Parser/OperatingSystem.php#L330
70
68
  def android_app?
71
69
  %w[com.hisense.odinbrowser com.seraphic.openinet.pre
72
- com.appssppa.idesktoppcbrowser].include?(app_name_from_headers)
70
+ com.appssppa.idesktoppcbrowser every.browser.inc].include?(app_name_from_headers)
71
+ end
72
+
73
+ # https://github.com/matomo-org/device-detector/blob/67ae11199a5129b42fa8b985d372ea834104fe3a/Parser/OperatingSystem.php#L449-L456
74
+ def fire_os_app?
75
+ app_name_from_headers == 'org.mozilla.tv.firefox'
76
+ end
77
+
78
+ # https://github.com/matomo-org/device-detector/blob/67ae11199a5129b42fa8b985d372ea834104fe3a/Parser/OperatingSystem.php#L439-L447
79
+ def lineage_os_app?
80
+ app_name_from_headers == 'org.lineageos.jelly'
73
81
  end
74
82
 
83
+ # https://github.com/matomo-org/device-detector/blob/75d88bbefb0182f9207c9f48dc39b1bc8c7cc43f/Parser/Client/Browser.php#L1076-L1079
75
84
  def browser_name_from_list
76
- @browser_name_from_list ||= browser_list&.reject { |b| b.name == 'Chromium' }&.last&.name
85
+ @browser_name_from_list ||= browser_list&.reject do |b|
86
+ ['Chromium', 'Microsoft Edge'].include?(b.name)
87
+ end&.last&.name
77
88
  end
78
89
 
79
90
  def available_browsers
@@ -94,14 +105,35 @@ class DeviceDetector
94
105
  major_version < 11 ? '10' : '11'
95
106
  end
96
107
 
97
- # https://github.com/matomo-org/device-detector/blob/be1c9ef486c247dc4886668da5ed0b1c49d90ba8/Parser/Client/Browser.php#L749
98
- # If version from client hints report 2022 or 2022.04, then is the Iridium browser
99
- # https://iridiumbrowser.de/news/2022/05/16/version-2022-04-released
100
- def is_iridium?
108
+ def lineage_version
109
+ DeviceDetector::OS
110
+ .mapped_os_version(platform_version, DeviceDetector::OS::LINEAGE_OS_VERSION_MAPPING)
111
+ end
112
+
113
+ def fire_os_version
114
+ DeviceDetector::OS
115
+ .mapped_os_version(platform_version, DeviceDetector::OS::FIRE_OS_VERSION_MAPPING)
116
+ end
117
+
118
+ # https://github.com/matomo-org/device-detector/blob/67ae11199a5129b42fa8b985d372ea834104fe3a/Parser/Client/Browser.php#L923-L929
119
+ # If the version reported from the client hints is YYYY or YYYY.MM (e.g., 2022 or 2022.04),
120
+ # then it is the Iridium browser
121
+ # https://iridiumbrowser.de/news/
122
+ def iridium?
101
123
  return if browser_list.nil?
102
124
 
103
125
  !browser_list.find do |browser|
104
- browser.name == 'Chromium' && %w[2021.12 2022.04 2022].include?(browser.version)
126
+ browser.name == 'Chromium' && browser.version =~ /^202[0-4]/
127
+ end.nil?
128
+ end
129
+
130
+ # https://github.com/matomo-org/device-detector/blob/67ae11199a5129b42fa8b985d372ea834104fe3a/Parser/Client/Browser.php#L931-L937
131
+ # https://bbs.360.cn/thread-16096544-1-1.html
132
+ def secure_browser?
133
+ return if browser_list.nil?
134
+
135
+ !browser_list.find do |browser|
136
+ browser.name == 'Chromium' && browser.version =~ /^15/
105
137
  end.nil?
106
138
  end
107
139
 
@@ -141,9 +173,15 @@ class DeviceDetector
141
173
  end
142
174
 
143
175
  def extract_browser_list
144
- return if headers['Sec-CH-UA'].nil?
176
+ extract_browser_list_from_full_version_list ||
177
+ extract_browser_list_from_header('Sec-CH-UA') ||
178
+ extract_browser_list_from_header('Sec-CH-UA-Full-Version-List')
179
+ end
145
180
 
146
- headers['Sec-CH-UA'].split(', ').map do |component|
181
+ def extract_browser_list_from_header(header)
182
+ return if headers[header].nil?
183
+
184
+ headers[header].split(', ').map do |component|
147
185
  name_and_version = extract_browser_name_and_version(component)
148
186
  next if name_and_version[:name].nil?
149
187
 
@@ -158,24 +196,54 @@ class DeviceDetector
158
196
  { name: name, version: browser_version }
159
197
  end
160
198
 
199
+ def extract_browser_list_from_full_version_list
200
+ return if headers['fullVersionList'].nil? && headers['brands'].nil?
201
+
202
+ (headers['brands'] || headers['fullVersionList']).map do |item|
203
+ name = name_from_known_browsers(item['brand'])
204
+ next if name.nil?
205
+
206
+ HintBrowser.new(name, full_version || item['version'])
207
+ end.compact
208
+ end
209
+
161
210
  # https://github.com/matomo-org/device-detector/blob/be1c9ef486c247dc4886668da5ed0b1c49d90ba8/Parser/Client/Browser.php#L865
162
211
  def name_from_known_browsers(name)
163
- # https://github.com/matomo-org/device-detector/blob/be1c9ef486c247dc4886668da5ed0b1c49d90ba8/Parser/Client/Browser.php#L628
164
- return 'Chrome' if name == 'Google Chrome'
165
-
166
- available_browsers.find do |i|
167
- i == name ||
168
- i.gsub(' ', '') == name.gsub(' ', '') ||
169
- i == name.gsub('Browser', '') ||
170
- i == name.gsub(' Browser', '') ||
171
- i == "#{name} Browser"
212
+ DeviceDetector::Browser::KNOWN_BROWSER_TO_NAME.fetch(name) do
213
+ available_browsers.find do |i|
214
+ i == name ||
215
+ i.gsub(' ', '') == name.gsub(' ', '') ||
216
+ i == name.gsub('Browser', '') ||
217
+ i == name.gsub(' Browser', '') ||
218
+ i == "#{name} Browser"
219
+ end
172
220
  end
173
221
  end
174
222
 
175
- def extract_model
176
- return if headers['Sec-CH-UA-Model'].nil? || headers['Sec-CH-UA-Model'] == ''
223
+ def extract_from_header(header)
224
+ return if headers[header].nil? || headers[header] == ''
177
225
 
178
- headers['Sec-CH-UA-Model']
226
+ headers[header]
227
+ end
228
+
229
+ def extract_full_version
230
+ extract_from_header('Sec-CH-UA-Full-Version') || extract_from_header('uaFullVersion')
231
+ end
232
+
233
+ def extract_platform
234
+ extract_from_header('Sec-CH-UA-Platform') || extract_from_header('platform')
235
+ end
236
+
237
+ def extract_platform_version
238
+ extract_from_header('Sec-CH-UA-Platform-Version') || extract_from_header('platformVersion')
239
+ end
240
+
241
+ def extract_mobile
242
+ extract_from_header('Sec-CH-UA-Mobile') || extract_from_header('mobile')
243
+ end
244
+
245
+ def extract_model
246
+ extract_from_header('Sec-CH-UA-Model') || extract_from_header('model')
179
247
  end
180
248
  end
181
249
  end