diff src/nabble/view/web/more/ForumStart.jtp @ 0:7ecd1a4ef557

add content
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 21 Mar 2019 19:15:52 -0600
parents
children 03e68185c2f5
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/nabble/view/web/more/ForumStart.jtp	Thu Mar 21 19:15:52 2019 -0600
@@ -0,0 +1,354 @@
+<%
+package nabble.view.web.more;
+
+import fschmidt.db.DbDatabase;
+import fschmidt.util.mail.MailAddress;
+import fschmidt.util.servlet.CanonicalUrl;
+import nabble.model.Db;
+import nabble.model.Message;
+import nabble.model.ModelException;
+import nabble.model.ModelHome;
+import nabble.model.Site;
+import nabble.model.User;
+import nabble.naml.compiler.Template;
+import nabble.naml.compiler.TemplatePrintWriter;
+import nabble.naml.namespaces.BasicNamespace;
+import nabble.view.lib.Jtp;
+import nabble.view.lib.NewSiteMail;
+import nabble.view.lib.Permissions;
+import nabble.view.lib.Shared;
+import nabble.view.lib.UrlMappable;
+import nabble.view.lib.Recaptcha;
+import nabble.view.web.app.Languages;
+import nabble.view.web.template.NabbleNamespace;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.io.StringWriter;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+
+public final class ForumStart extends HttpServlet implements UrlMappable, CanonicalUrl {
+
+	private static final Pattern URL_PATTERN = Pattern.compile("/free-(forum|gallery|newspaper|blog|mailing-list)\\.html$");
+
+	public static String url(String what) {
+		return Jtp.defaultContextUrl() + path(what);
+	}
+
+	public static String path(String what) {
+		return "/free-" + what + ".html";
+	}
+
+	public String getCanonicalUrl(HttpServletRequest request) {
+		return url( request.getParameter("what") );
+	}
+
+	public Map<String,String[]> getParameterMapFromUrl(HttpServletRequest request,String mappedUrl) {
+		Matcher m = URL_PATTERN.matcher(mappedUrl);
+		if( !m.find() )
+			throw new RuntimeException();
+		Map<String,String[]> params = new HashMap<String,String[]>();
+		String what = m.group(1);
+		params.put("what",new String[]{what});
+		return params;
+	}
+
+	public Pattern getUrlPattern() {
+		return URL_PATTERN;
+	}
+
+	protected void service(HttpServletRequest request,HttpServletResponse response)
+		throws ServletException, IOException
+	{
+		build(request, response, Collections.<String,String>emptyMap(), Collections.<String,String>emptyMap());
+	}
+
+	private static void build(HttpServletRequest request,HttpServletResponse response, Map<String,String> values, Map<String,String> errors)
+		throws ServletException, IOException
+	{
+		PrintWriter out = response.getWriter();
+		String what = request.getParameter("what");
+		if (what == null)
+			what = "Forum";
+		else if ("mailing-list".equals(what))
+			what = "Mailing List";
+		else
+			what = Jtp.capitalize(what);
+
+		String imgName = what.toLowerCase();
+		if ("Mailing List".equals(what))
+			imgName = "mailing-list";
+
+		%>
+		<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
+		<html>
+			<head>
+				<% Shared.head(request,response); %>
+				<title>Nabble - Free <%=what%> Setup</title>
+				<meta name="robots" content="noindex"/>
+				<META NAME="description" CONTENT="Setting up a free <%=what.toLowerCase()%> on Nabble is quick and easy. Fill in one simple form and you are done.">
+				<META NAME="keywords" CONTENT="free <%=what.toLowerCase()%>, hosted <%=what.toLowerCase()%>, simple, embeddable <%=what.toLowerCase()%>, customizable">
+				<% /*Shared.canonical(request, response);*/ %>
+				<style type="text/css">
+					div.center-content {
+						margin:0px auto;
+						margin-bottom: 3em;
+					}
+					td.column1 {
+						text-align:right;
+						width:7em;
+						white-space:nowrap;
+					}
+					input[type=text],input[type=password] {
+						padding:.4em 0;
+					}
+					div.field-title {
+						margin-top:.3em;
+					}
+					.important { font-weight:bold }
+					label { vertical-align:-15%; }
+				</style>
+				<script type="text/javascript">
+					$(document).ready(function() {
+						$('#username').focus();
+					});
+
+					function singleFormSubmit(f) {
+						if (f.done)
+							return false;
+						f.done = true;
+						$('#submit-btn').hide();
+						var $div = $('#wait-message');
+						function loading1() { $div.fadeTo(300,0.3,loading2);  };
+						function loading2() { $div.fadeTo(300,1,loading1);  };
+						loading1();
+						return true;
+					};
+				</script>
+				<%= Recaptcha.JS %>
+			</head>
+			<body style="text-align:center">
+				<% Shared.minHeaderGlobal(request,response); %>
+
+				<div class="center-content">
+					<img src="/images/logo_nabble_home.png" border="0" alt="Nabble - free forums for everyone"/><br />
+					<h1 style="color:#979797">Start Your <%=what%></h1>
+
+					<% if (errors.size() > 0) { %>
+						<div class="error-message important" style="margin:1em;padding:.5em 0">
+							<% String generic = errors.get("generic"); %>
+							<%=generic != null? generic : errors.size() > 0? "Please check the errors below" : ""%>
+						</div>
+					<% } %>
+
+					<form action="/more/ForumStart$Save.jtp" method="post" accept-charset="UTF-8" onsubmit="return singleFormSubmit(this)">
+						<input type="hidden" name="type" value="<%=what.toLowerCase().replace(" ","")%>" />
+						<input type="hidden" name="what" value="<%=what%>" />
+
+						<div style="text-align:left;width:50em;margin:0 auto">
+							<div style="border-bottom:2px solid #eeeeee;padding:1em">
+								<div class="weak-color" style="width:12em;text-align:center;float:left">
+									<div style="font-weight:bold">Account</div>
+									<img src="/images/account.png" width="84" height="45"/>
+									<div style="margin-top:1em;font-size:80%">
+										You will receive an email with a link to activate your account
+									</div>
+								</div>
+								<table>
+									<tr>
+										<td class="column1"><div class="second-font field-title">User Name</div></td>
+										<td><input type="text" id="username" size="35" maxlength="30" name="username" value="<%=Jtp.hideNull(values.get("username"))%>" /></td>
+										<td class="important"><%=errors.containsKey("username")? errors.get("username"):""%></td>
+									</tr>
+									<tr>
+										<td class="column1"><div class="second-font field-title">Email</div></td>
+										<td><input type="text" size="35" maxlength="60" name="email" value="<%=Jtp.hideNull(values.get("email"))%>"/></td>
+										<td class="important"><%=errors.containsKey("email")? errors.get("email"):""%></td>
+									</tr>
+									<tr>
+										<td class="column1"><div class="second-font field-title">Password</div></td>
+										<td><input type="password" size="35" maxlength="15" name="password" value="<%=Jtp.hideNull(values.get("password"))%>"/></td>
+										<td class="important"><%=errors.containsKey("password")? errors.get("password"):""%></td>
+									</tr>
+									<tr>
+										<td class="column1"><input type="checkbox" id="terms" name="terms" value="y" <%="y".equals(values.get("terms"))?"checked":""%> /></td>
+										<td colspan=2><label for="terms">I have read and I agree to Nabble's <a href="<%=Jtp.termsUrl(true)%>">Terms of Use</a>.</label></td>
+									</tr>
+								</table>
+							</div>
+
+							<div style="padding:1em;overflow:hidden">
+								<div class="weak-color" style="width:12em;text-align:center;float:left;height:15em">
+									<div style="font-weight:bold"><%=what%></div>
+									<img src="/images/homepage/<%=imgName%>.png" alt="Free <%=what.toLowerCase()%>">
+								</div>
+								<table>
+									<tr>
+										<td class="column1"><div class="second-font field-title">Language</div></td>
+										<td>
+											<select name="lang">
+												<% for( Map.Entry<String,String> entry : Languages.languages.entrySet() ) { %>
+												<% String lang = request.getParameter("lang"); %>
+												<% boolean isEnglish = entry.getKey().equals("none"); %>
+												<% boolean isSelected = (lang == null && isEnglish) || entry.getKey().equals(lang); %>
+												<option value="<%=entry.getKey()%>" <%=isSelected?"selected=\"true\"":""%>><%=entry.getValue()%></option>
+												<% } %>
+											</select>
+										</td>
+										<td></td>
+									</tr>
+									<tr>
+										<td colspan="3" style="height:.6em"></td>
+									</tr>
+									<tr>
+										<td class="column1"><div class="second-font field-title"><%=what%> Name</div></td>
+										<td><input type="text" name="subject" size="30" maxlength="80" value="<%=Jtp.hideNull(values.get("subject"))%>"/></td>
+										<td class="important"><%=errors.containsKey("subject")? errors.get("subject"):""%></td>
+									</tr>
+									<tr>
+										<td colspan=3 style="padding:.4em 0 0 .6em">
+											<div class="second-font field-title">Description &nbsp;<span class="weak-color" style="font-weight:normal;">(optional)</span></div>
+											<textarea rows="7" name="message" wrap="SOFT" style="width:28em"><%=Jtp.hideNull(values.get("message"))%></textarea>
+											<br><%= Recaptcha.DIV %>
+										</td>
+									</tr>
+								</table>
+							</div>
+							<div class="weak-color" style="margin-top:.5em;text-align:center;">
+								<input id="submit-btn" type="submit" name="save" value="Create <%=what%>" style="padding:.5em .8em;font-size:110%;font-weight:bold"/>
+								<div id="wait-message" class="important invisible" style="margin:.1em 0">Creating <%=what%>... Please wait</div>
+							</div>
+						</div>
+                    </form>
+				</div>
+
+				<% Shared.footer(request,response); %>
+				<% Shared.analytics(request,response); %>
+			</body>
+		</html>
+		<%
+	}
+
+	public static class Save extends HttpServlet {
+
+		private static String get(String name, HttpServletRequest request) {
+			String s = request.getParameter(name);
+			return s == null? null : s.trim();
+		}
+
+		protected void service(HttpServletRequest request, HttpServletResponse response)
+				throws ServletException, IOException
+		{
+			String username = get("username", request);
+			String email = get("email", request);
+			String password = get("password", request);
+			boolean agreed = "y".equals(get("terms", request));
+			String subject = get("subject", request);
+			String message = get("message", request);
+
+			Map<String,String> errors = new HashMap<String,String>();
+			if (username == null || username.trim().length() == 0)
+				errors.put("username", "required");
+			if (email == null || email.length() == 0)
+				errors.put("email", "required");
+			else if (!new MailAddress(email).isValid())
+				errors.put("email", "invalid email");
+			if (password == null || password.length() < 4)
+				errors.put("password", "too short");
+			if (!agreed)
+				errors.put("generic", "You must agree to the Terms and Conditions");
+			if (subject == null || subject.length() == 0)
+				errors.put("subject", "required");
+
+			String type = get("type", request);
+			type = "newspaper".equals(type)? "news" : type;
+
+			String extraMessage = "";
+			if ("mailinglist".equals(type)) {
+				type = "forum";
+				StringBuilder m = new StringBuilder();
+				m.append("\n\nMailing List Options\n");
+				m.append("Click \"Options > Subscribe via email\" to subscribe to this mailing list;\n");
+				m.append("Click \"Options > Post by email...\" to get the email address of this mailing list;\n");
+				m.append("You can post messages via email or through the forum interface below;\n");
+				m.append("All web posts and emails are archived here.");
+				extraMessage = m.toString();
+			}
+
+			if (errors.isEmpty()) {
+				DbDatabase db = Db.dbGlobal();
+				db.beginTransaction();
+				try {
+					Recaptcha.check(request);
+					Site site = ModelHome.newSite(type,subject, message + extraMessage, Message.Format.TEXT, email, username);
+					Permissions.addToGroup( (User)site.getRootNode().getOwner(), Permissions.ADMINISTRATORS_GROUP );
+					String key = site.newRegistration(email,password,username,"/");
+					db.commitTransaction();
+
+					// Track spam activities by IP
+					ModelHome.setRemoteAddr(site, Jtp.getClientIpAddr(request));
+
+					site = site.getGoodCopy();
+
+					String lang = request.getParameter("lang");
+					if (!"none".equals(lang)) {
+						site.setModuleEnabled(lang, true);
+						site = site.getGoodCopy();
+					}
+
+					sendRegisterMail(site, email, key);
+					NewSiteMail.send(site, request, response);
+					response.sendRedirect(site.getBaseUrl()+"/more/ForumStart$Redirection.jtp");
+					return;
+				} catch(ModelException e) {
+					errors.put("generic", e.getMessage());
+				} finally {
+					db.endTransaction();
+				}
+			}
+
+			Map<String,String> values = new HashMap<String,String>();
+			values.put("username", username);
+			values.put("email", email);
+			values.put("password", password);
+			values.put("terms", agreed?"y":"");
+			values.put("subject", subject);
+			values.put("message", message);
+			build(request, response, values, errors);
+		}
+	}
+
+	/** Sets cookies in the site domain  */
+	public static class Redirection extends HttpServlet {
+
+		protected void service(HttpServletRequest request, HttpServletResponse response)
+				throws ServletException, IOException
+		{
+			Site site = Jtp.getSite(request);
+			Shared.javascriptRedirect(request, response, Jtp.url(site.getRootNode()), "Nabble.setVar('appnotice','true');");
+		}
+	}
+
+	public static void sendRegisterMail(Site site, String email, String key) {
+		Map<String,Object> args = new HashMap<String,Object>();
+		args.put("email",email);
+		args.put("next_url","/");
+		args.put("key",key);
+		Template template = site.getTemplate( "send_registration_email",
+			BasicNamespace.class, NabbleNamespace.class
+		);
+		template.run( TemplatePrintWriter.NULL, args,
+			new BasicNamespace(template), new NabbleNamespace(site)
+		);
+	}
+}
+%>