1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package net.fckeditor.tool;
22
23 import java.util.regex.Matcher;
24 import java.util.regex.Pattern;
25
26 import javax.servlet.http.HttpServletRequest;
27
28
29
30
31
32
33 public class Compatibility {
34
35
36
37
38
39
40
41 public static boolean check(final String userAgentString) {
42 if (Utils.isEmpty(userAgentString))
43 return false;
44
45 String userAgentStr = userAgentString.toLowerCase();
46
47
48
49 if (userAgentStr.indexOf("opera") < 0 && userAgentStr.indexOf("mac") < 0) {
50 if (getBrowserVersion(userAgentStr, ".*msie ([\\d]+.[\\d]+).*") >= 5.5f)
51 return true;
52 }
53
54
55 if (getBrowserVersion(userAgentStr, ".*rv:([\\d]+.[\\d]+).*") > 1.7f)
56 return true;
57
58
59 if (getBrowserVersion(userAgentStr, "opera/([\\d]+.[\\d]+).*") >= 9.5f
60 || getBrowserVersion(userAgentStr, ".*opera ([\\d]+.[\\d]+)") >= 9.5f)
61 return true;
62
63
64 if (getBrowserVersion(userAgentStr, ".*applewebkit/([\\d]+).*") >= 522f)
65 return true;
66
67 return false;
68 }
69
70
71
72
73
74
75 public static boolean isCompatibleBrowser(final HttpServletRequest request) {
76 return (request == null) ? false : check(request.getHeader("user-agent"));
77 }
78
79
80
81
82
83
84
85
86
87 private static float getBrowserVersion(final String userAgent, final String regex) {
88 Pattern pattern = Pattern.compile(regex);
89 Matcher matcher = pattern.matcher(userAgent);
90 if (matcher.matches()) {
91 try {
92 return Float.parseFloat(matcher.group(1));
93 } catch (NumberFormatException e) {
94 return -1f;
95 }
96 }
97 return -1f;
98 }
99
100 }