1 /*
2 * FCKeditor - The text editor for Internet - http://www.fckeditor.net
3 * Copyright (C) 2004-2009 Frederico Caldeira Knabben
4 *
5 * == BEGIN LICENSE ==
6 *
7 * Licensed under the terms of any of the following licenses at your
8 * choice:
9 *
10 * - GNU General Public License Version 2 or later (the "GPL")
11 * http://www.gnu.org/licenses/gpl.html
12 *
13 * - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
14 * http://www.gnu.org/licenses/lgpl.html
15 *
16 * - Mozilla Public License Version 1.1 or later (the "MPL")
17 * http://www.mozilla.org/MPL/MPL-1.1.html
18 *
19 * == END LICENSE ==
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 * Compatibility check.
30 *
31 * @version $Id: Compatibility.java 3840 2009-07-08 20:29:46Z mosipov $
32 */
33 public class Compatibility {
34
35 /**
36 * Checks if a browser's user agent string is compatible for the FCKeditor.
37 *
38 * @param userAgentString
39 * @return <code>true</code> if compatible otherwise <code>false</code>
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 // IE 5.5+, check special keys like 'Opera' and 'mac', because there are some
48 // other browsers, containing 'MSIE' in there agent string!
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 // for all gecko based browsers
55 if (getBrowserVersion(userAgentStr, ".*rv:([\\d]+.[\\d]+).*") > 1.7f)
56 return true;
57
58 // Opera 9.5+
59 if (getBrowserVersion(userAgentStr, "opera/([\\d]+.[\\d]+).*") >= 9.5f
60 || getBrowserVersion(userAgentStr, ".*opera ([\\d]+.[\\d]+)") >= 9.5f)
61 return true;
62
63 // Safari 3+
64 if (getBrowserVersion(userAgentStr, ".*applewebkit/([\\d]+).*") >= 522f)
65 return true;
66
67 return false;
68 }
69
70 /**
71 * Just a wrapper to {@link #check(String)}.
72 *
73 * @param request
74 */
75 public static boolean isCompatibleBrowser(final HttpServletRequest request) {
76 return (request == null) ? false : check(request.getHeader("user-agent"));
77 }
78
79 /**
80 * Helper method to get the browser version from 'userAgent' with regex. The
81 * first matching group has to be the version number!
82 *
83 * @param userAgent
84 * @param regex
85 * @return The browser version, or -1f if version can't be determined.
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 }