1 package org.cacert.gigi.pages.main;
3 import java.io.IOException;
4 import java.io.PrintWriter;
5 import java.sql.SQLException;
6 import java.util.HashMap;
9 import javax.servlet.http.HttpServletRequest;
11 import org.cacert.gigi.GigiApiException;
12 import org.cacert.gigi.database.GigiPreparedStatement;
13 import org.cacert.gigi.database.GigiResultSet;
14 import org.cacert.gigi.dbObjects.Name;
15 import org.cacert.gigi.dbObjects.User;
16 import org.cacert.gigi.email.EmailProvider;
17 import org.cacert.gigi.localisation.Language;
18 import org.cacert.gigi.output.DateSelector;
19 import org.cacert.gigi.output.template.Form;
20 import org.cacert.gigi.output.template.Template;
21 import org.cacert.gigi.pages.Page;
22 import org.cacert.gigi.util.HTMLEncoder;
23 import org.cacert.gigi.util.Notary;
24 import org.cacert.gigi.util.PasswordStrengthChecker;
26 public class Signup extends Form {
28 Name buildupName = new Name("", "", "", "");
34 boolean general = true, country = true, regional = true, radius = true;
36 public Signup(HttpServletRequest hsr) {
38 t = new Template(Signup.class.getResource("Signup.templ"));
41 DateSelector myDoB = new DateSelector("day", "month", "year");
44 public void outputContent(PrintWriter out, Language l, Map<String, Object> outerVars) {
45 HashMap<String, Object> vars = new HashMap<String, Object>();
46 vars.put("fname", HTMLEncoder.encodeHTML(buildupName.getFname()));
47 vars.put("mname", HTMLEncoder.encodeHTML(buildupName.getMname()));
48 vars.put("lname", HTMLEncoder.encodeHTML(buildupName.getLname()));
49 vars.put("suffix", HTMLEncoder.encodeHTML(buildupName.getSuffix()));
50 vars.put("dob", myDoB);
51 vars.put("email", HTMLEncoder.encodeHTML(email));
52 vars.put("general", general ? " checked=\"checked\"" : "");
53 vars.put("country", country ? " checked=\"checked\"" : "");
54 vars.put("regional", regional ? " checked=\"checked\"" : "");
55 vars.put("radius", radius ? " checked=\"checked\"" : "");
56 vars.put("helpOnNames", String.format(l.getTranslation("Help on Names %sin the wiki%s"), "<a href=\"//wiki.cacert.org/FAQ/HowToEnterNamesInJoinForm\" target=\"_blank\">", "</a>"));
57 vars.put("csrf", getCSRFToken());
58 t.output(out, l, vars);
61 private void update(HttpServletRequest r) {
62 String fname = buildupName.getFname();
63 String lname = buildupName.getLname();
64 String mname = buildupName.getMname();
65 String suffix = buildupName.getSuffix();
66 if (r.getParameter("fname") != null) {
67 fname = r.getParameter("fname");
69 if (r.getParameter("lname") != null) {
70 lname = r.getParameter("lname");
72 if (r.getParameter("mname") != null) {
73 mname = r.getParameter("mname");
75 if (r.getParameter("suffix") != null) {
76 suffix = r.getParameter("suffix");
78 if (r.getParameter("email") != null) {
79 email = r.getParameter("email");
81 buildupName = new Name(fname, lname, mname, suffix);
82 general = "1".equals(r.getParameter("general"));
83 country = "1".equals(r.getParameter("country"));
84 regional = "1".equals(r.getParameter("regional"));
85 radius = "1".equals(r.getParameter("radius"));
88 } catch (GigiApiException e) {
93 public synchronized boolean submit(PrintWriter out, HttpServletRequest req) {
95 if (buildupName.getLname().trim().equals("")) {
96 outputError(out, req, "Last name were blank.");
98 if ( !myDoB.isValid()) {
99 outputError(out, req, "Invalid date of birth");
101 if ( !"1".equals(req.getParameter("tos_agree"))) {
102 outputError(out, req, "Acceptance of the ToS is required to continue.");
104 if (email.equals("")) {
105 outputError(out, req, "Email Address was blank");
107 String pw1 = req.getParameter("pword1");
108 String pw2 = req.getParameter("pword2");
109 if (pw1 == null || pw1.equals("")) {
110 outputError(out, req, "Pass Phrases were blank");
111 } else if ( !pw1.equals(pw2)) {
112 outputError(out, req, "Pass Phrases don't match");
114 int pwpoints = PasswordStrengthChecker.checkpw(pw1, buildupName, email);
116 outputError(out, req, "The Pass Phrase you submitted failed to contain enough" + " differing characters and/or contained words from" + " your name and/or email address.");
121 try (GigiPreparedStatement q1 = new GigiPreparedStatement("SELECT * FROM `emails` WHERE `email`=? AND `deleted` IS NULL"); GigiPreparedStatement q2 = new GigiPreparedStatement("SELECT * FROM `certOwners` INNER JOIN `users` ON `users`.`id`=`certOwners`.`id` WHERE `email`=? AND `deleted` IS NULL")) {
122 q1.setString(1, email);
123 q2.setString(1, email);
124 GigiResultSet r1 = q1.executeQuery();
125 GigiResultSet r2 = q2.executeQuery();
126 if (r1.next() || r2.next()) {
127 outputError(out, req, "This email address is currently valid in the system.");
130 try (GigiPreparedStatement q3 = new GigiPreparedStatement("SELECT `domain` FROM `baddomains` WHERE `domain`=RIGHT(?, LENGTH(`domain`))")) {
131 q3.setString(1, email);
133 GigiResultSet r3 = q3.executeQuery();
135 String domain = r3.getString(1);
136 outputError(out, req, "We don't allow signups from people using email addresses from %s", domain);
139 String mailResult = EmailProvider.FAIL;
141 mailResult = HTMLEncoder.encodeHTML(EmailProvider.getInstance().checkEmailServer(0, email));
142 } catch (IOException e) {
144 if ( !mailResult.equals(EmailProvider.OK)) {
145 if (mailResult.startsWith("4")) {
146 outputError(out, req, "The mail server responsible for your domain indicated" + " a temporary failure. This may be due to anti-SPAM measures, such" + " as greylisting. Please try again in a few minutes.");
148 outputError(out, req, "Email Address given was invalid, or a test connection" + " couldn't be made to your server, or the server" + " rejected the email address as invalid");
150 if (mailResult.equals(EmailProvider.FAIL)) {
151 outputError(out, req, "Failed to make a connection to the mail server");
153 outputErrorPlain(out, mailResult);
160 if (RegisterPage.RATE_LIMIT.isLimitExceeded(req.getRemoteAddr())) {
161 outputError(out, req, "Rate Limit Exceeded");
166 } catch (SQLException e) {
168 } catch (GigiApiException e) {
169 e.format(out, Page.getLanguage(req));
175 private void run(HttpServletRequest req, String password) throws SQLException, GigiApiException {
176 User u = new User(email, password, buildupName, myDoB.getDate(), Page.getLanguage(req).getLocale());
178 try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `alerts` SET `memid`=?," + " `general`=?, `country`=?, `regional`=?, `radius`=?")) {
179 ps.setInt(1, u.getId());
180 ps.setBoolean(2, general);
181 ps.setBoolean(3, country);
182 ps.setBoolean(4, regional);
183 ps.setBoolean(5, radius);
186 Notary.writeUserAgreement(u, "ToS", "account creation", "", true, 0);