2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-01-08 22:08:42 -05:00
|
|
|
module MobileDetection
|
|
|
|
def self.mobile_device?(user_agent)
|
2016-03-26 05:02:23 -04:00
|
|
|
user_agent =~ /Mobile/ && !(user_agent =~ /iPad/)
|
2014-01-08 22:08:42 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# we need this as a reusable chunk that is called from the cache
|
|
|
|
def self.resolve_mobile_view!(user_agent, params, session)
|
|
|
|
return false unless SiteSetting.enable_mobile_theme
|
|
|
|
|
2014-01-16 21:07:17 -05:00
|
|
|
session[:mobile_view] = params[:mobile_view] if params && params.has_key?(:mobile_view)
|
2019-02-22 13:43:40 -05:00
|
|
|
session[:mobile_view] = nil if params && params.has_key?(:mobile_view) &&
|
|
|
|
params[:mobile_view] == "auto"
|
2014-01-08 22:08:42 -05:00
|
|
|
|
2014-01-16 23:04:38 -05:00
|
|
|
if session && session[:mobile_view]
|
2014-01-08 22:08:42 -05:00
|
|
|
session[:mobile_view] == "1"
|
|
|
|
else
|
|
|
|
mobile_device?(user_agent)
|
|
|
|
end
|
|
|
|
end
|
2019-05-07 10:44:43 -04:00
|
|
|
|
|
|
|
def self.ios_device?(user_agent)
|
|
|
|
user_agent =~ /iPad|iPhone|iPod/
|
|
|
|
end
|
|
|
|
|
2022-04-06 06:09:12 -04:00
|
|
|
MODERN_MOBILE_REGEX =
|
|
|
|
%r{
|
2023-01-16 12:28:59 -05:00
|
|
|
\(.*iPhone\ OS\ 1[5-9].*\)|
|
|
|
|
\(.*iPad.*OS\ 1[5-9].*\)|
|
2022-04-06 06:09:12 -04:00
|
|
|
Chrome\/8[89]|
|
|
|
|
Chrome\/9[0-9]|
|
|
|
|
Chrome\/1[0-9][0-9]|
|
|
|
|
Firefox\/8[5-9]|
|
|
|
|
Firefox\/9[0-9]|
|
|
|
|
Firefox\/1[0-9][0-9]
|
|
|
|
}x
|
|
|
|
|
2023-01-25 11:55:33 -05:00
|
|
|
USER_AGENT_MAX_LENGTH = 400
|
|
|
|
|
2022-04-06 06:09:12 -04:00
|
|
|
def self.modern_mobile_device?(user_agent)
|
2023-01-25 11:55:33 -05:00
|
|
|
user_agent[0...USER_AGENT_MAX_LENGTH].match?(MODERN_MOBILE_REGEX)
|
2022-04-06 06:09:12 -04:00
|
|
|
end
|
2014-01-08 22:08:42 -05:00
|
|
|
end
|