view src/nabble/view/lib/Shared.jtp @ 47:72765b66e2c3

remove mailing list code
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 18 Jun 2021 17:44:24 -0600
parents 7ecd1a4ef557
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;
		%>
		<link rel="stylesheet" href="<%=Shared.getCssPath()%>" type="text/css" />
		<% if (isApp && useSiteStyle) { %>
		<link rel="stylesheet" href="/template/NamlServlet.jtp?macro=site_style" type="text/css" />
		<% } %>
		<% loadJavascript(request, out); %>
		<% if (isApp) { %>
		<script type="text/javascript">
			Nabble.setFontSize();
		</script>
		<% } %>
		<%
		loadAnalytics(request,response);
	}

	public static void loadJavascript(HttpServletRequest request, PrintWriter out) {
		%>
		<script src="<%=getJQueryPath()%>" type="text/javascript"></script>
		<script src="<%=getDropdownJsPath()%>" type="text/javascript"></script>
		<script src="<%=getJavascriptPath(request)%>" type="text/javascript"></script>
		<%
	}

	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();
			%>
			<script type="text/javascript">
				if (Nabble.getParent().nabbleinfo && Nabble.getParent().nabbleinfo.what) {
					var home = Nabble.get('homelink');
					home.setAttribute('href', '/');
					home.innerHTML='Back to ' + Nabble.getParent().nabbleinfo.what;
				}
			</script>
			<%
		} 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);
		%>
		<div id="notice" class="notice rounded-bottom"></div>
		<div class="nabble" id="nabble">
			<div class="top-bar">
				<div class="breadcrumbs" style="float:left;">
					<%
					if (app != null)
						breadcrumb(request,out,app);
					else if (breadcrumbLinks != null)
						breadcrumb(out,breadcrumbLinks);
					%>
				</div>
				<div style="text-align:right;">
					<span style="white-space:nowrap;" id="nabble-user-header"></span>
					<script type="text/javascript">Nabble.userHeader();</script>
				</div>
			</div>
			<div style="clear:both;"></div>
			<%
			if (request.getAttribute("search") == null) {
				%>
				<script type="text/javascript">
					Nabble.deleteCookie("query");
					Nabble.deleteCookie("searchuser");
					Nabble.deleteCookie("searchterms");
				</script>
				<%
			}
	}

	public static void noHeader(HttpServletRequest request,HttpServletResponse response)
		throws IOException, ServletException
	{
		PrintWriter out = response.getWriter();
		%>
		<div class="nabble" id="nabble">
		<%
	}

	public static void editHeader(String firstText, String secondText, PrintWriter out) {
		%>
		<div class="second-font shaded-bg-color" style="font-size: 120%;padding: .4em;font-weight:bold">
			<%=firstText%> <span class="weak-color"><%=secondText == null? "" : "&ndash; " + secondText%></span>
		</div>
		<%
	}

	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 ) {
			%>
			<table class="footer-table shaded-bg-color">
				<tr>
					<td class="footer-left">
						<a href="<%=Jtp.homePage()%>" target="_top">Free Forum</a> by Nabble
					</td>
				</tr>
			</table>
			<%
		} else {
			%>
			<table class="footer-table shaded-bg-color">
				<tr>
					<td class="footer-left">
						<a href="<%=Jtp.homePage()%>" target="_top">Free Forum</a>
						by Nabble
					</td>
					<td class="footer-right">
						<a href="<%=Jtp.helpIndexUrl(request,response)%>">Help</a>
					</td>
				</tr>
			</table>
			<%
		}
		%>
		</div>
		<%
	}

	public static void noFooter(HttpServletRequest request,HttpServletResponse response)
		throws ServletException, IOException
	{
		PrintWriter out = response.getWriter();
		%>
		</div>
		<%
	}

	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
	{
		%><span class="weak-color" style="white-space:nowrap"><%
		for (String s : links) {
			%><%=s%><%
			boolean isLastString = s.equals(links[links.length-1]);
			if (!isLastString) {
				%>&nbsp;&rsaquo;&nbsp;<%
			}
		}
		%></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();
		%>
		<html>
		<head>
			<script src="<%=getJQueryPath()%>" type="text/javascript"></script>
			<script src="<%=getJavascriptPath(request)%>" type="text/javascript"></script>
			<% loadAnalytics(request,response); %>
			<script type="text/javascript">
				<%=Jtp.hideNull(script)%>
				var url = "<%=HtmlUtils.javascriptStringEncode(url)%>";

				<%=leaveEmbedding? "top." : ""%>location.replace(url);
			</script>
		</head>
		</html>
		<%
	}

	/* 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();
		%>
		<html>
			<head>
				<script src="<%=getJQueryPath()%>" type="text/javascript"></script>
				<script src="<%=getJavascriptPath(request)%>" type="text/javascript"></script>
				<script type="text/javascript">
					Nabble.restartEmbedding(<%=node.getId()%>,'<%=node.getSite().getBaseUrl()%>');
				</script>
			</head>
		</html>
		<%
	}

	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);
		}
		%>
		<table class="error-message" style="width:100%">
			<tr>
				<td style="padding: .4em; width: 40px;">
					<img src="/images/icon_alert.png" class="image32"/>
				</td>
				<td style="padding: .4em;">
					<strong><%=errorMsg%></strong>
					<%=prompt == null? "" : "<div style=\"padding-top:.3em\">" + prompt + "</div>"%>
				</td>
			</tr>
		</table>
		<%
	}

	public static void title(HttpServletRequest request,HttpServletResponse response,String s)
		throws ServletException, IOException
	{
		PrintWriter out = response.getWriter();
		Site site = Jtp.getSite(request);
		%>
		<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
		<title><%=site == null?"Nabble":site.getRootNode().getSubjectHtml()%> - <%=s%></title>
		<%
		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();
		%>
		<script type="text/javascript">
			if (Nabble.analytics) Nabble.analytics();
		</script>
		<%
	}

	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) ) {
			%><a href="<%=Jtp.path(post)%>"><%=postSubject%></a><%
		} else {
			%><%=topicSubject%></a> &nbsp; (<a href="<%=Jtp.path(post)%>"><%=postSubject%></a>)<%
		}
	}

	public static void profileHeading(HttpServletRequest request,PrintWriter out,User user,String heading)
	{
		%>
		<h1><%=heading%></h1>
		<%
	}

	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();
		}
		%>
		<div id="return-link" style="margin-top:1em">
			<% if (node != null) { %>
			&laquo;
			<a href="<%=Jtp.path(node)%>">Return to <%=node.getSubject()%></a>
			<% } %>
		</div>
		<%
	}

	public static void framedCss(PrintWriter out)
	{
		%>
		<style>
		body {
			margin: .8em;
		}
		</style>
		<%
	}

	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;
		%>
		<script type="text/javascript">
			if (!Nabble.isEmbedded) {
				var url = top.location.href;
				var cidPos = url.indexOf(';cid=');
				if (cidPos > 0) {
					url = url.substring(0, cidPos);
				}

				var posHtml = url.lastIndexOf('.html');
				var hash;
				<%
				/*
					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.
				*/
				%>
				if (url.indexOf('?') == -1 && posHtml > 0) {
					var posStart = url.lastIndexOf('-');
					hash = (posStart > 0)? url.substring(posStart, posHtml):'';
				} else {
					<%
					/*
						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".
					*/
					%>
					hash = url.replace('http://', '');
					hash = hash.substring(hash.indexOf('/')+1);
					hash = '+' + hash;
				}
				<%
				/*
					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.
				*/
				%>
				if (top.location.hash) {
					hash += '|' + top.location.hash.substring(1);
				}
				if (Nabble.analytics) Nabble.analytics();
				top.location.replace('<%=embeddingUrl%>' + (hash.length==1?'':'#nabble' + encodeURIComponent(hash)));
			}
		</script>
		<%
	}

	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();
		%>
		<html>
			<head>
				<META NAME="robots" CONTENT="noindex,nofollow">
				<% title(request, response, pageTitle); %>
				<style type="text/css">
					div.main-title {
						font-size:120%;
						font-weight:bold;
						margin:1em 0;
						padding: .2em;
					}
				</style>
				<script type="text/javascript">
					<%/* here I set the next url for the login UI */%>
					var loginNextUrl = '<%=node==null?"/":Jtp.url(node)%>';
				</script>
			</head>
			<body>
				<%
					if (node == null)
						minHeaderGlobal(request, response);
					else
						minHeader(request, response, node);
				%>
				<div class="shaded-bg-color rounded second-font main-title">
					<%=messageTitle%>
				</div>
				<%=message%>
				<% if (node != null) { %>
				<br><br>&laquo; <a href="<%=Jtp.url(node)%>">Back to <%=node.getSubjectHtml()%></a>
				<% } else { %>
				<br>
				<% } %>
				<br/><br/>
				<% footer(request,response); %>
				<% analytics(request,response); %>
			</body>
		</html>
		<%
	}

	public static void canonical(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException
	{
		String canonical = Jtp.getCanonicalUrl(request);
		if (canonical != null) {
			PrintWriter out = response.getWriter();
			%>
			<link rel="canonical" href="<%=canonical%>" />
			<%
		}
	}

	/** (To be used by scripts)
	 * http://tablesorter.com/docs/
	 */
	public static void tableSorter(PrintWriter out, String sortList)
		throws IOException, ServletException
	{
		%>
		<link rel="stylesheet" href="/util/tablesorter/style.css" type="text/css" />
		<script src="/util/tablesorter/jquery.tablesorter.min.js" type="text/javascript"></script>
		<script type="text/javascript">
			$(document).ready(function() {
				$("table.tablesorter").tablesorter(<%=sortList%>);
			});
		</script>
		<%
	}

	/** (To be used by scripts) */
	public static void countTableRows(PrintWriter out)
		throws IOException, ServletException
	{
		%>
		<div id="rows"></div>
		<script type="text/javascript">
			$(document).ready(function() {
				var count = $('tbody tr').size();
				$("#rows").html(count + " rows");
			});
		</script>
		<%
	}
}
%>