Mercurial Hosting > nabble
view src/nabble/view/lib/Shared.java @ 62:4674ed7d56df default tip
remove n2
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sat, 30 Sep 2023 20:25:29 -0600 |
parents | 72765b66e2c3 |
children |
line wrap: on
line source
package nabble.view.lib; import fschmidt.util.java.HtmlUtils; import fschmidt.util.java.MD5Util; import nabble.model.Init; import nabble.model.ModelHome; import nabble.model.Node; import nabble.model.Person; import nabble.model.Site; import nabble.model.User; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.List; public final class Shared { private static final Logger logger = LoggerFactory.getLogger(Shared.class); private Shared() {} // never public static volatile int javascriptVersion = 102; public static final int cssVersion = 29; public static String getCssPath() { return "/nabble.css?v="+cssVersion; } public static String getJavascriptPath(HttpServletRequest request) { return Jtp.getSite(request) == null? "/Javascript.jtp" : "/template/NamlServlet.jtp?macro=javascript_library"; } public static String getTabsPath() { return "/util/nabbletabs.js"; } public static String getJQueryPath() { return "/util/jquery-1.7.2.pack.js"; } public static String getDropdownJsPath() { return "/util/nabbledropdown-2.4.1.js"; } public static void head(HttpServletRequest request,HttpServletResponse response) throws IOException { head(request, response, true); } public static void head(HttpServletRequest request,HttpServletResponse response, boolean useSiteStyle) throws IOException { if( request.getAttribute("head") != null ) return; request.setAttribute("head","x"); PrintWriter out = response.getWriter(); Site site = Jtp.getSite(request); boolean isApp = site != null && site.getRootNode().getKind() == Node.Kind.APP; out.print( "\r\n<link rel=\"stylesheet\" href=\"" ); out.print( (Shared.getCssPath()) ); out.print( "\" type=\"text/css\" />\r\n" ); if (isApp && useSiteStyle) { out.print( "\r\n<link rel=\"stylesheet\" href=\"/template/NamlServlet.jtp?macro=site_style\" type=\"text/css\" />\r\n" ); } out.print( "\r\n" ); loadJavascript(request, out); out.print( "\r\n" ); if (isApp) { out.print( "\r\n<script type=\"text/javascript\">\r\n Nabble.setFontSize();\r\n</script>\r\n" ); } out.print( "\r\n" ); loadAnalytics(request,response); } public static void loadJavascript(HttpServletRequest request, PrintWriter out) { out.print( "\r\n<script src=\"" ); out.print( (getJQueryPath()) ); out.print( "\" type=\"text/javascript\"></script>\r\n<script src=\"" ); out.print( (getDropdownJsPath()) ); out.print( "\" type=\"text/javascript\"></script>\r\n<script src=\"" ); out.print( (getJavascriptPath(request)) ); out.print( "\" type=\"text/javascript\"></script>\r\n" ); } private static void checkHead(HttpServletRequest request) { if( request.getAttribute("head") == null ) throw new RuntimeException("Shared.head not called"); } public static void minHeader(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException { minHeader(request,response,null); } public static void minHeader(HttpServletRequest request,HttpServletResponse response,Node node) throws IOException, ServletException { minHeader(request, response, node, null); } public static void minHeaderGlobal(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException { Site site = Jtp.getSite(request); if (site == null) { String homepageLink = "<a id=\"homelink\" href=\"" + Jtp.homePage() + "\">Nabble</a>"; minHeader(request, response, null, new String[] { homepageLink }); PrintWriter out = response.getWriter(); out.print( "\r\n<script type=\"text/javascript\">\r\n if (Nabble.getParent().nabbleinfo && Nabble.getParent().nabbleinfo.what) {\r\n var home = Nabble.get('homelink');\r\n home.setAttribute('href', '/');\r\n home.innerHTML='Back to ' + Nabble.getParent().nabbleinfo.what;\r\n }\r\n</script>\r\n" ); } else { Node node = site.getRootNode(); String homepageLink = "<a href=\"" + Jtp.url(node) + "\">" + node.getSubjectHtml() + "</a>"; minHeader(request, response, null, new String[] { homepageLink }); } } public static void minHeader(HttpServletRequest request, HttpServletResponse response, Node node, String[] breadcrumbLinks) throws IOException, ServletException { minHeader(request, response, node, breadcrumbLinks, true); } public static void minHeader(HttpServletRequest request, HttpServletResponse response, Node node, String[] breadcrumbLinks, boolean embeddedRedirect) throws IOException, ServletException { PrintWriter out = response.getWriter(); checkHead(request); Node app = node==null ? null : node.getApp(); if (embeddedRedirect) embeddedRedirect(request, out, app); out.print( "\r\n<div id=\"notice\" class=\"notice rounded-bottom\"></div>\r\n<div class=\"nabble\" id=\"nabble\">\r\n <div class=\"top-bar\">\r\n <div class=\"breadcrumbs\" style=\"float:left;\">\r\n " ); if (app != null) breadcrumb(request,out,app); else if (breadcrumbLinks != null) breadcrumb(out,breadcrumbLinks); out.print( "\r\n</div>\r\n<div style=\"text-align:right;\">\r\n<span style=\"white-space:nowrap;\" id=\"nabble-user-header\"></span>\r\n<script type=\"text/javascript\">Nabble.userHeader();</script>\r\n</div>\r\n</div>\r\n<div style=\"clear:both;\"></div>\r\n" ); if (request.getAttribute("search") == null) { out.print( "\r\n<script type=\"text/javascript\">\r\n Nabble.deleteCookie(\"query\");\r\n Nabble.deleteCookie(\"searchuser\");\r\n Nabble.deleteCookie(\"searchterms\");\r\n</script>\r\n" ); } } public static void noHeader(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException { PrintWriter out = response.getWriter(); out.print( "\r\n<div class=\"nabble\" id=\"nabble\">\r\n" ); } public static void editHeader(String firstText, String secondText, PrintWriter out) { out.print( "\r\n<div class=\"second-font shaded-bg-color\" style=\"font-size: 120%;padding: .4em;font-weight:bold\">\r\n " ); out.print( (firstText) ); out.print( " <span class=\"weak-color\">" ); out.print( (secondText == null? "" : "– " + secondText) ); out.print( "</span>\r\n</div>\r\n" ); } public static void helpHeader(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException { Site site = Jtp.getSite(request); String homepageLink = "<a href=\"/\">" + (site == null? "Nabble":site.getRootNode().getSubjectHtml()) + "</a>"; String helpLink = "<a href=\"/help/Index.jtp\">Help</a>"; minHeader(request, response, null, new String[] { homepageLink, helpLink }); } public static void footer(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); Site site = Jtp.getSite(request); if( site == null ) { out.print( "\r\n<table class=\"footer-table shaded-bg-color\">\r\n <tr>\r\n <td class=\"footer-left\">\r\n <a href=\"" ); out.print( (Jtp.homePage()) ); out.print( "\" target=\"_top\">Free Forum</a> by Nabble\r\n </td>\r\n </tr>\r\n</table>\r\n" ); } else { out.print( "\r\n<table class=\"footer-table shaded-bg-color\">\r\n <tr>\r\n <td class=\"footer-left\">\r\n <a href=\"" ); out.print( (Jtp.homePage()) ); out.print( "\" target=\"_top\">Free Forum</a>\r\n by Nabble\r\n </td>\r\n <td class=\"footer-right\">\r\n <a href=\"" ); out.print( (Jtp.helpIndexUrl(request,response)) ); out.print( "\">Help</a>\r\n </td>\r\n </tr>\r\n</table>\r\n" ); } out.print( "\r\n</div>\r\n" ); } public static void noFooter(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); out.print( "\r\n</div>\r\n" ); } public static void breadcrumb(HttpServletRequest request,PrintWriter out,Node node) throws ServletException, IOException { List<String> links = new ArrayList<String>(); node = node.getApp(); while (node != null) { links.add(0, "<a href=\"" + Jtp.path(node) + "\">" + node.getSubjectHtml() + "</a>"); node = node.getParent(); } String[] array = new String[links.size()]; links.toArray(array); breadcrumb(out, array); } private static void breadcrumb(PrintWriter out, String[] links) throws ServletException, IOException { out.print( "<span class=\"weak-color\" style=\"white-space:nowrap\">" ); for (String s : links) { out.print( (s) ); boolean isLastString = s.equals(links[links.length-1]); if (!isLastString) { out.print( " › " ); } } out.print( "</span>" ); } public static void javascriptRedirect(HttpServletRequest request,HttpServletResponse response,String url) throws IOException, ServletException { javascriptRedirect(request,response,url,null); } public static void javascriptRedirect(HttpServletRequest request,HttpServletResponse response,String url,String script) throws IOException, ServletException { javascriptRedirect(request, response, url, script, false); } public static void javascriptRedirect(HttpServletRequest request,HttpServletResponse response,String url,String script, boolean leaveEmbedding) throws IOException, ServletException { PrintWriter out = response.getWriter(); out.print( "\r\n<html>\r\n<head>\r\n <script src=\"" ); out.print( (getJQueryPath()) ); out.print( "\" type=\"text/javascript\"></script>\r\n <script src=\"" ); out.print( (getJavascriptPath(request)) ); out.print( "\" type=\"text/javascript\"></script>\r\n " ); loadAnalytics(request,response); out.print( "\r\n <script type=\"text/javascript\">\r\n " ); out.print( (Jtp.hideNull(script)) ); out.print( "\r\n var url = \"" ); out.print( (HtmlUtils.javascriptStringEncode(url)) ); out.print( "\";\r\n\r\n " ); out.print( (leaveEmbedding? "top." : "") ); out.print( "location.replace(url);\r\n </script>\r\n</head>\r\n</html>\r\n" ); } /* Rebuilds the embedding iframe and reload the contents.*/ public static void restartEmbedding(Node node, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { PrintWriter out = response.getWriter(); out.print( "\r\n<html>\r\n <head>\r\n <script src=\"" ); out.print( (getJQueryPath()) ); out.print( "\" type=\"text/javascript\"></script>\r\n <script src=\"" ); out.print( (getJavascriptPath(request)) ); out.print( "\" type=\"text/javascript\"></script>\r\n <script type=\"text/javascript\">\r\n Nabble.restartEmbedding(" ); out.print( (node.getId()) ); out.print( ",'" ); out.print( (node.getSite().getBaseUrl()) ); out.print( "');\r\n </script>\r\n </head>\r\n</html>\r\n" ); } public static void errorMessage(HttpServletRequest request,HttpServletResponse response,String errorMsg,String prompt) throws ServletException, IOException { if( errorMsg==null ) return; PrintWriter out = response.getWriter(); errorMsg = HtmlUtils.htmlEncode(errorMsg); int posLinkOpen = errorMsg.indexOf("[link]"); if (posLinkOpen >= 0) { int posLinkClose = errorMsg.indexOf("[/link]", posLinkOpen); String url = errorMsg.substring(posLinkOpen+6, posLinkClose); errorMsg = errorMsg.substring(0, posLinkOpen) + "<a href=\"" + url + "\">" + url + "</a>" + errorMsg.substring(posLinkClose+7); } out.print( "\r\n<table class=\"error-message\" style=\"width:100%\">\r\n <tr>\r\n <td style=\"padding: .4em; width: 40px;\">\r\n <img src=\"/images/icon_alert.png\" class=\"image32\"/>\r\n </td>\r\n <td style=\"padding: .4em;\">\r\n <strong>" ); out.print( (errorMsg) ); out.print( "</strong>\r\n " ); out.print( (prompt == null? "" : "<div style=\"padding-top:.3em\">" + prompt + "</div>") ); out.print( "\r\n </td>\r\n </tr>\r\n</table>\r\n" ); } public static void title(HttpServletRequest request,HttpServletResponse response,String s) throws ServletException, IOException { PrintWriter out = response.getWriter(); Site site = Jtp.getSite(request); out.print( "\r\n<meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\" />\r\n<title>" ); out.print( (site == null?"Nabble":site.getRootNode().getSubjectHtml()) ); out.print( " - " ); out.print( (s) ); out.print( "</title>\r\n" ); head(request,response); } private static void loadAnalytics(HttpServletRequest request,HttpServletResponse response) throws IOException { PrintWriter out = response.getWriter(); HtmlViewUtils.googleAnalytics(out); } public static void analytics(HttpServletRequest request,HttpServletResponse response) throws IOException { PrintWriter out = response.getWriter(); out.print( "\r\n<script type=\"text/javascript\">\r\n if (Nabble.analytics) Nabble.analytics();\r\n</script>\r\n" ); } public static void postInThreadLink(HttpServletRequest request, PrintWriter out, Node post) throws ServletException, IOException { String topicSubject = Jtp.breakUp(post.getTopic().getSubjectHtml()); String postSubject = Jtp.breakUp(post.getSubjectHtml()); if( postSubject.endsWith(topicSubject) ) { out.print( "<a href=\"" ); out.print( (Jtp.path(post)) ); out.print( "\">" ); out.print( (postSubject) ); out.print( "</a>" ); } else { out.print( (topicSubject) ); out.print( "</a> (<a href=\"" ); out.print( (Jtp.path(post)) ); out.print( "\">" ); out.print( (postSubject) ); out.print( "</a>)" ); } } public static void profileHeading(HttpServletRequest request,PrintWriter out,User user,String heading) { out.print( "\r\n<h1>" ); out.print( (heading) ); out.print( "</h1>\r\n" ); } public static void returnToJs(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { returnToJs(null, request, response); } public static void returnToJs(Node node, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { PrintWriter out = response.getWriter(); if (node == null) { Site site = Jtp.getSite(request); if (site != null && site.getRootNode().getKind() == Node.Kind.APP) node = site.getRootNode(); } out.print( "\r\n<div id=\"return-link\" style=\"margin-top:1em\">\r\n " ); if (node != null) { out.print( "\r\n «\r\n <a href=\"" ); out.print( (Jtp.path(node)) ); out.print( "\">Return to " ); out.print( (node.getSubject()) ); out.print( "</a>\r\n " ); } out.print( "\r\n</div>\r\n" ); } public static void framedCss(PrintWriter out) { out.print( "\r\n<style>\r\nbody {\r\n margin: .8em;\r\n}\r\n</style>\r\n" ); } public static void embeddedRedirect(HttpServletRequest request, PrintWriter out, Node forum) throws IOException { // The embedding URL is stored in the node.embedding_url field. String embeddingUrl = null; for( Node node = forum; embeddingUrl == null && node != null; node = node.getParent() ) { embeddingUrl = node.getEmbeddingUrl(); } if (embeddingUrl == null) return; out.print( "\r\n<script type=\"text/javascript\">\r\n if (!Nabble.isEmbedded) {\r\n var url = top.location.href;\r\n var cidPos = url.indexOf(';cid=');\r\n if (cidPos > 0) {\r\n url = url.substring(0, cidPos);\r\n }\r\n\r\n var posHtml = url.lastIndexOf('.html');\r\n var hash;\r\n " ); /* If the URL is mapped, we extract the last part, which gives information about the item. For example: https://www.nabble.com/text-p100.html We extract "p100", because we know how to rebuild this URL with this information. See JsEmbed.jtp for more details on how to rebuild the URL. */ out.print( "\r\nif (url.indexOf('?') == -1 && posHtml > 0) {\r\n var posStart = url.lastIndexOf('-');\r\n hash = (posStart > 0)? url.substring(posStart, posHtml):'';\r\n} else {\r\n " ); /* If the URL is NOT mapped, we have to extract the information after the domain. For example: - http://localhost/forum/NewTopic.jtp?forum=2 We extract "forum/NewTopic.jtp?forum=2". */ out.print( "\r\nhash = url.replace('http://', '');\r\nhash = hash.substring(hash.indexOf('/')+1);\r\nhash = '+' + hash;\r\n}\r\n" ); /* In order to restore the current hash, I append it to the same string after a pipe separator. This will be restored in the Nabble.getCurrentUrl() function in JsEmbed.jtp. */ out.print( "\r\nif (top.location.hash) {\r\n hash += '|' + top.location.hash.substring(1);\r\n}\r\nif (Nabble.analytics) Nabble.analytics();\r\ntop.location.replace('" ); out.print( (embeddingUrl) ); out.print( "' + (hash.length==1?'':'#nabble' + encodeURIComponent(hash)));\r\n}\r\n</script>\r\n" ); } public static String getAvatarImageURL(Person visitor, boolean smallImage) { String imageName = smallImage? ModelHome.AVATAR_SMALL : ModelHome.AVATAR_BIG; String defaultImage = "/images/" + imageName; if( visitor instanceof User ) { User user = (User)visitor; if( user.hasAvatar() ) return "/file/a" + user.getId() + '/' + imageName; } return defaultImage; } public static String getDefaultAvatarImageURL(boolean smallImage) { String imageName = smallImage? ModelHome.AVATAR_SMALL : ModelHome.AVATAR_BIG; return "/images/" + imageName; } public static String simpleBannedMessage(Node app) { StringBuilder builder = new StringBuilder(); builder .append("Sorry, but the administrators of this ") .append(Jtp.viewName(app).toLowerCase()) .append(" have banned you.\n "); return builder.toString(); } public static String bannedMessage(Node app, boolean isPost) { StringBuilder builder = new StringBuilder(); builder.append(simpleBannedMessage(app)); if (isPost) builder.append("You can't post a message here, but you can post in other places."); else builder.append("You can't create a forum here, but you can create in other places."); return builder.toString(); } public static void simplePage(String pageTitle, Node node, String messageTitle, String message, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { PrintWriter out = response.getWriter(); out.print( "\r\n<html>\r\n <head>\r\n <META NAME=\"robots\" CONTENT=\"noindex,nofollow\">\r\n " ); title(request, response, pageTitle); out.print( "\r\n <style type=\"text/css\">\r\n div.main-title {\r\n font-size:120%;\r\n font-weight:bold;\r\n margin:1em 0;\r\n padding: .2em;\r\n }\r\n </style>\r\n <script type=\"text/javascript\">\r\n " ); /* here I set the next url for the login UI */ out.print( "\r\n var loginNextUrl = '" ); out.print( (node==null?"/":Jtp.url(node)) ); out.print( "';\r\n </script>\r\n </head>\r\n <body>\r\n " ); if (node == null) minHeaderGlobal(request, response); else minHeader(request, response, node); out.print( "\r\n<div class=\"shaded-bg-color rounded second-font main-title\">\r\n " ); out.print( (messageTitle) ); out.print( "\r\n</div>\r\n" ); out.print( (message) ); out.print( "\r\n" ); if (node != null) { out.print( "\r\n<br><br>« <a href=\"" ); out.print( (Jtp.url(node)) ); out.print( "\">Back to " ); out.print( (node.getSubjectHtml()) ); out.print( "</a>\r\n" ); } else { out.print( "\r\n<br>\r\n" ); } out.print( "\r\n<br/><br/>\r\n" ); footer(request,response); out.print( "\r\n" ); analytics(request,response); out.print( "\r\n</body>\r\n</html>\r\n" ); } public static void canonical(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { String canonical = Jtp.getCanonicalUrl(request); if (canonical != null) { PrintWriter out = response.getWriter(); out.print( "\r\n<link rel=\"canonical\" href=\"" ); out.print( (canonical) ); out.print( "\" />\r\n" ); } } /** (To be used by scripts) * http://tablesorter.com/docs/ */ public static void tableSorter(PrintWriter out, String sortList) throws IOException, ServletException { out.print( "\r\n<link rel=\"stylesheet\" href=\"/util/tablesorter/style.css\" type=\"text/css\" />\r\n<script src=\"/util/tablesorter/jquery.tablesorter.min.js\" type=\"text/javascript\"></script>\r\n<script type=\"text/javascript\">\r\n $(document).ready(function() {\r\n $(\"table.tablesorter\").tablesorter(" ); out.print( (sortList) ); out.print( ");\r\n });\r\n</script>\r\n" ); } /** (To be used by scripts) */ public static void countTableRows(PrintWriter out) throws IOException, ServletException { out.print( "\r\n<div id=\"rows\"></div>\r\n<script type=\"text/javascript\">\r\n $(document).ready(function() {\r\n var count = $('tbody tr').size();\r\n $(\"#rows\").html(count + \" rows\");\r\n });\r\n</script>\r\n" ); } }