github.com/vnforks/kid/v5@v5.22.1-0.20200408055009-b89d99c65676/web/unsupported_browser.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See LICENSE.txt for license information.
     3  
     4  package web
     5  
     6  import (
     7  	"net/http"
     8  
     9  	"github.com/avct/uasurfer"
    10  
    11  	"github.com/vnforks/kid/v5/app"
    12  	"github.com/vnforks/kid/v5/utils"
    13  )
    14  
    15  // MattermostApp describes downloads for the Mattermost App
    16  type MattermostApp struct {
    17  	LogoSrc                string
    18  	Title                  string
    19  	SupportedVersionString string
    20  	Label                  string
    21  	Link                   string
    22  	InstallGuide           string
    23  	InstallGuideLink       string
    24  }
    25  
    26  // Browser describes a browser with a download link
    27  type Browser struct {
    28  	LogoSrc                string
    29  	Title                  string
    30  	SupportedVersionString string
    31  	Src                    string
    32  	GetLatestString        string
    33  }
    34  
    35  // SystemBrowser describes a browser but includes 2 links: one to open the local browser, and one to make it default
    36  type SystemBrowser struct {
    37  	LogoSrc                string
    38  	Title                  string
    39  	SupportedVersionString string
    40  	LabelOpen              string
    41  	LinkOpen               string
    42  	LinkMakeDefault        string
    43  	OrString               string
    44  	MakeDefaultString      string
    45  }
    46  
    47  func renderUnsupportedBrowser(app app.AppIface, w http.ResponseWriter, r *http.Request) {
    48  	w.Header().Set("Cache-Control", "no-store")
    49  	page := utils.NewHTMLTemplate(app.HTMLTemplates(), "unsupported_browser")
    50  
    51  	// User Agent info
    52  	ua := uasurfer.Parse(r.UserAgent())
    53  	isWindows := ua.OS.Platform.String() == "PlatformWindows"
    54  	isWindows10 := isWindows && ua.OS.Version.Major == 10
    55  	isMacOSX := ua.OS.Name.String() == "OSMacOSX" && ua.OS.Version.Major == 10
    56  	isSafari := ua.Browser.Name.String() == "BrowserSafari"
    57  
    58  	// Basic heading translations
    59  	if isSafari {
    60  		page.Props["NoLongerSupportString"] = app.T("web.error.unsupported_browser.no_longer_support_version")
    61  	} else {
    62  		page.Props["NoLongerSupportString"] = app.T("web.error.unsupported_browser.no_longer_support")
    63  	}
    64  	page.Props["DownloadAppOrUpgradeBrowserString"] = app.T("web.error.unsupported_browser.download_app_or_upgrade_browser")
    65  	page.Props["LearnMoreString"] = app.T("web.error.unsupported_browser.learn_more")
    66  
    67  	// Mattermost app version
    68  	if isWindows {
    69  		page.Props["App"] = renderMattermostAppWindows(app)
    70  	} else if isMacOSX {
    71  		page.Props["App"] = renderMattermostAppMac(app)
    72  	}
    73  
    74  	// Browsers to download
    75  	// Show a link to Safari if you're using safari and it's outdated
    76  	// Can't show on Mac all the time because there's no way to open it via URI
    77  	browsers := []Browser{renderBrowserChrome(app), renderBrowserFirefox(app)}
    78  	if isSafari {
    79  		browsers = append(browsers, renderBrowserSafari(app))
    80  	}
    81  	page.Props["Browsers"] = browsers
    82  
    83  	// If on Windows 10, show link to Edge
    84  	if isWindows10 {
    85  		page.Props["SystemBrowser"] = renderSystemBrowserEdge(app, r)
    86  	}
    87  
    88  	page.RenderToWriter(w)
    89  }
    90  
    91  func renderMattermostAppMac(app app.AppIface) MattermostApp {
    92  	return MattermostApp{
    93  		"/static/images/browser-icons/mac.png",
    94  		app.T("web.error.unsupported_browser.download_the_app"),
    95  		app.T("web.error.unsupported_browser.min_os_version.mac"),
    96  		app.T("web.error.unsupported_browser.download"),
    97  		"https://mattermost.com/download/#mattermostApps",
    98  		app.T("web.error.unsupported_browser.install_guide.mac"),
    99  		"https://docs.mattermost.com/install/desktop.html#mac-os-x-10-9",
   100  	}
   101  }
   102  
   103  func renderMattermostAppWindows(app app.AppIface) MattermostApp {
   104  	return MattermostApp{
   105  		"/static/images/browser-icons/windows.svg",
   106  		app.T("web.error.unsupported_browser.download_the_app"),
   107  		app.T("web.error.unsupported_browser.min_os_version.windows"),
   108  		app.T("web.error.unsupported_browser.download"),
   109  		"https://mattermost.com/download/#mattermostApps",
   110  		app.T("web.error.unsupported_browser.install_guide.windows"),
   111  		"https://docs.mattermost.com/install/desktop.html#windows-10-windows-8-1-windows-7",
   112  	}
   113  }
   114  
   115  func renderBrowserChrome(app app.AppIface) Browser {
   116  	return Browser{
   117  		"/static/images/browser-icons/chrome.svg",
   118  		app.T("web.error.unsupported_browser.browser_title.chrome"),
   119  		app.T("web.error.unsupported_browser.min_browser_version.chrome"),
   120  		"http://www.google.com/chrome",
   121  		app.T("web.error.unsupported_browser.browser_get_latest.chrome"),
   122  	}
   123  }
   124  
   125  func renderBrowserFirefox(app app.AppIface) Browser {
   126  	return Browser{
   127  		"/static/images/browser-icons/firefox.svg",
   128  		app.T("web.error.unsupported_browser.browser_title.firefox"),
   129  		app.T("web.error.unsupported_browser.min_browser_version.firefox"),
   130  		"https://www.mozilla.org/firefox/new/",
   131  		app.T("web.error.unsupported_browser.browser_get_latest.firefox"),
   132  	}
   133  }
   134  
   135  func renderBrowserSafari(app app.AppIface) Browser {
   136  	return Browser{
   137  		"/static/images/browser-icons/safari.svg",
   138  		app.T("web.error.unsupported_browser.browser_title.safari"),
   139  		app.T("web.error.unsupported_browser.min_browser_version.safari"),
   140  		"macappstore://showUpdatesPage",
   141  		app.T("web.error.unsupported_browser.browser_get_latest.safari"),
   142  	}
   143  }
   144  
   145  func renderSystemBrowserEdge(app app.AppIface, r *http.Request) SystemBrowser {
   146  	return SystemBrowser{
   147  		"/static/images/browser-icons/edge.svg",
   148  		app.T("web.error.unsupported_browser.browser_title.edge"),
   149  		app.T("web.error.unsupported_browser.min_browser_version.edge"),
   150  		app.T("web.error.unsupported_browser.open_system_browser.edge"),
   151  		"microsoft-edge:http://" + r.Host + r.RequestURI, //TODO: Can we get HTTP or HTTPS? If someone's server doesn't have a redirect this won't work
   152  		"ms-settings:defaultapps",
   153  		app.T("web.error.unsupported_browser.system_browser_or"),
   154  		app.T("web.error.unsupported_browser.system_browser_make_default"),
   155  	}
   156  }