1 package org.cacert.gigi.pages.main;
3 import java.io.IOException;
4 import java.io.PrintWriter;
6 import java.sql.PreparedStatement;
7 import java.sql.ResultSet;
8 import java.sql.SQLException;
9 import java.util.Enumeration;
10 import java.util.HashMap;
11 import java.util.Locale;
14 import javax.servlet.http.HttpServletRequest;
16 import org.cacert.gigi.EmailAddress;
17 import org.cacert.gigi.User;
18 import org.cacert.gigi.database.DatabaseConnection;
19 import org.cacert.gigi.email.EmailProvider;
20 import org.cacert.gigi.localisation.Language;
21 import org.cacert.gigi.output.DateSelector;
22 import org.cacert.gigi.output.Form;
23 import org.cacert.gigi.output.template.Template;
24 import org.cacert.gigi.pages.Page;
25 import org.cacert.gigi.util.HTMLEncoder;
26 import org.cacert.gigi.util.Notary;
27 import org.cacert.gigi.util.PasswordStrengthChecker;
29 public class Signup extends Form {
31 private User buildup = new User();
35 boolean general = true, country = true, regional = true, radius = true;
37 public Signup(HttpServletRequest hsr) {
39 t = new Template(Signup.class.getResource("Signup.templ"));
43 buildup.setSuffix("");
45 buildup.setDob(new Date(0));
48 DateSelector myDoB = new DateSelector("day", "month", "year");
51 public void outputContent(PrintWriter out, Language l, Map<String, Object> outerVars) {
52 HashMap<String, Object> vars = new HashMap<String, Object>();
53 vars.put("fname", HTMLEncoder.encodeHTML(buildup.getFname()));
54 vars.put("mname", HTMLEncoder.encodeHTML(buildup.getMname()));
55 vars.put("lname", HTMLEncoder.encodeHTML(buildup.getLname()));
56 vars.put("suffix", HTMLEncoder.encodeHTML(buildup.getSuffix()));
57 vars.put("dob", myDoB);
58 vars.put("email", HTMLEncoder.encodeHTML(buildup.getEmail()));
59 vars.put("general", general ? " checked=\"checked\"" : "");
60 vars.put("country", country ? " checked=\"checked\"" : "");
61 vars.put("regional", regional ? " checked=\"checked\"" : "");
62 vars.put("radius", radius ? " checked=\"checked\"" : "");
63 vars.put("helpOnNames", String.format(l.getTranslation("Help on Names %sin the wiki%s"), "<a href=\"//wiki.cacert.org/FAQ/HowToEnterNamesInJoinForm\" target=\"_blank\">", "</a>"));
64 vars.put("csrf", getCSRFToken());
65 t.output(out, l, vars);
68 private void update(HttpServletRequest r) {
69 if (r.getParameter("fname") != null) {
70 buildup.setFname(r.getParameter("fname"));
72 if (r.getParameter("lname") != null) {
73 buildup.setLname(r.getParameter("lname"));
75 if (r.getParameter("mname") != null) {
76 buildup.setMname(r.getParameter("mname"));
78 if (r.getParameter("suffix") != null) {
79 buildup.setSuffix(r.getParameter("suffix"));
81 if (r.getParameter("email") != null) {
82 buildup.setEmail(r.getParameter("email"));
84 general = "1".equals(r.getParameter("general"));
85 country = "1".equals(r.getParameter("country"));
86 regional = "1".equals(r.getParameter("regional"));
87 radius = "1".equals(r.getParameter("radius"));
92 public synchronized boolean submit(PrintWriter out, HttpServletRequest req) {
94 if (buildup.getFname().equals("") || buildup.getLname().equals("")) {
95 outputError(out, req, "First and/or last names were blank.");
97 if ( !myDoB.isValid()) {
98 outputError(out, req, "Invalid date of birth");
100 if ( !"1".equals(req.getParameter("cca_agree"))) {
101 outputError(out, req, "You have to agree to the CAcert Community agreement.");
103 if (buildup.getEmail().equals("")) {
104 outputError(out, req, "Email Address was blank");
106 String pw1 = req.getParameter("pword1");
107 String pw2 = req.getParameter("pword2");
108 if (pw1 == null || pw1.equals("")) {
109 outputError(out, req, "Pass Phrases were blank");
110 } else if ( !pw1.equals(pw2)) {
111 outputError(out, req, "Pass Phrases don't match");
113 int pwpoints = PasswordStrengthChecker.checkpw(pw1, buildup);
115 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 PreparedStatement q1 = DatabaseConnection.getInstance().prepare("select * from `emails` where `email`=? and `deleted`=0");
122 PreparedStatement q2 = DatabaseConnection.getInstance().prepare("select * from `users` where `email`=? and `deleted`=0");
123 q1.setString(1, buildup.getEmail());
124 q2.setString(1, buildup.getEmail());
125 ResultSet r1 = q1.executeQuery();
126 ResultSet r2 = q2.executeQuery();
127 if (r1.next() || r2.next()) {
128 outputError(out, req, "This email address is currently valid in the system.");
132 PreparedStatement q3 = DatabaseConnection.getInstance().prepare("select `domain` from `baddomains` where `domain`=RIGHT(?, LENGTH(`domain`))");
133 q3.setString(1, buildup.getEmail());
135 ResultSet r3 = q3.executeQuery();
137 String domain = r3.getString(1);
138 outputError(out, req, "We don't allow signups from people using email addresses from %s", domain);
141 } catch (SQLException e) {
143 outputError(out, req, "an internal error happened");
145 String mailResult = EmailProvider.FAIL;
147 mailResult = EmailProvider.getInstance().checkEmailServer(0, buildup.getEmail());
148 } catch (IOException e) {
150 if ( !mailResult.equals(EmailProvider.OK)) {
151 if (mailResult.startsWith("4")) {
152 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.");
154 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");
156 if (mailResult.equals(EmailProvider.FAIL)) {
157 outputError(out, req, "Failed to make a connection to the mail server");
159 outputErrorPlain(out, mailResult);
168 } catch (SQLException e) {
174 private void run(HttpServletRequest req, String password) throws SQLException {
176 DatabaseConnection.getInstance().beginTransaction();
177 Enumeration<Locale> locales = req.getLocales();
178 buildup.setPreferredLocale(Page.getLanguage(req).getLocale());
179 buildup.setDob(myDoB.getDate());
180 buildup.insert(password);
181 int memid = buildup.getId();
182 EmailAddress ea = new EmailAddress(buildup.getEmail(), buildup);
183 ea.insert(Page.getLanguage(req));
185 PreparedStatement ps = DatabaseConnection.getInstance().prepare("insert into `alerts` set `memid`=?," + " `general`=?, `country`=?, `regional`=?, `radius`=?");
187 ps.setString(2, general ? "1" : "0");
188 ps.setString(3, country ? "1" : "0");
189 ps.setString(4, regional ? "1" : "0");
190 ps.setString(5, radius ? "1" : "0");
192 Notary.writeUserAgreement(memid, "CCA", "account creation", "", true, 0);
194 DatabaseConnection.getInstance().commitTransaction();
196 DatabaseConnection.getInstance().quitTransaction();