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.HashMap;
12 import javax.servlet.http.HttpServletRequest;
14 import org.cacert.gigi.EmailAddress;
15 import org.cacert.gigi.User;
16 import org.cacert.gigi.database.DatabaseConnection;
17 import org.cacert.gigi.email.EmailProvider;
18 import org.cacert.gigi.localisation.Language;
19 import org.cacert.gigi.output.DateSelector;
20 import org.cacert.gigi.output.Form;
21 import org.cacert.gigi.output.template.Template;
22 import org.cacert.gigi.pages.Page;
23 import org.cacert.gigi.util.HTMLEncoder;
24 import org.cacert.gigi.util.Notary;
25 import org.cacert.gigi.util.PasswordStrengthChecker;
27 public class Signup extends Form {
29 private User buildup = new User();
33 boolean general = true, country = true, regional = true, radius = true;
35 public Signup(HttpServletRequest hsr) {
37 t = new Template(Signup.class.getResource("Signup.templ"));
41 buildup.setSuffix("");
43 buildup.setDob(new Date(0));
46 DateSelector myDoB = new DateSelector("day", "month", "year");
49 public void outputContent(PrintWriter out, Language l, Map<String, Object> outerVars) {
50 HashMap<String, Object> vars = new HashMap<String, Object>();
51 vars.put("fname", HTMLEncoder.encodeHTML(buildup.getFname()));
52 vars.put("mname", HTMLEncoder.encodeHTML(buildup.getMname()));
53 vars.put("lname", HTMLEncoder.encodeHTML(buildup.getLname()));
54 vars.put("suffix", HTMLEncoder.encodeHTML(buildup.getSuffix()));
55 vars.put("dob", myDoB);
56 vars.put("email", HTMLEncoder.encodeHTML(buildup.getEmail()));
57 vars.put("general", general ? " checked=\"checked\"" : "");
58 vars.put("country", country ? " checked=\"checked\"" : "");
59 vars.put("regional", regional ? " checked=\"checked\"" : "");
60 vars.put("radius", radius ? " checked=\"checked\"" : "");
61 vars.put("helpOnNames", String.format(l.getTranslation("Help on Names %sin the wiki%s"), "<a href=\"//wiki.cacert.org/FAQ/HowToEnterNamesInJoinForm\" target=\"_blank\">", "</a>"));
62 vars.put("csrf", getCSRFToken());
63 t.output(out, l, vars);
66 private void update(HttpServletRequest r) {
67 if (r.getParameter("fname") != null) {
68 buildup.setFname(r.getParameter("fname"));
70 if (r.getParameter("lname") != null) {
71 buildup.setLname(r.getParameter("lname"));
73 if (r.getParameter("mname") != null) {
74 buildup.setMname(r.getParameter("mname"));
76 if (r.getParameter("suffix") != null) {
77 buildup.setSuffix(r.getParameter("suffix"));
79 if (r.getParameter("email") != null) {
80 buildup.setEmail(r.getParameter("email"));
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"));
90 public synchronized boolean submit(PrintWriter out, HttpServletRequest req) {
92 if (buildup.getFname().equals("") || buildup.getLname().equals("")) {
93 outputError(out, req, "First and/or last names were blank.");
95 if ( !myDoB.isValid()) {
96 outputError(out, req, "Invalid date of birth");
98 if ( !"1".equals(req.getParameter("cca_agree"))) {
99 outputError(out, req, "You have to agree to the CAcert Community agreement.");
101 if (buildup.getEmail().equals("")) {
102 outputError(out, req, "Email Address was blank");
104 String pw1 = req.getParameter("pword1");
105 String pw2 = req.getParameter("pword2");
106 if (pw1 == null || pw1.equals("")) {
107 outputError(out, req, "Pass Phrases were blank");
108 } else if ( !pw1.equals(pw2)) {
109 outputError(out, req, "Pass Phrases don't match");
111 int pwpoints = PasswordStrengthChecker.checkpw(pw1, buildup);
113 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.");
119 PreparedStatement q1 = DatabaseConnection.getInstance().prepare("select * from `emails` where `email`=? and `deleted`=0");
120 PreparedStatement q2 = DatabaseConnection.getInstance().prepare("select * from `users` where `email`=? and `deleted`=0");
121 q1.setString(1, buildup.getEmail());
122 q2.setString(1, buildup.getEmail());
123 ResultSet r1 = q1.executeQuery();
124 ResultSet r2 = q2.executeQuery();
125 if (r1.next() || r2.next()) {
126 outputError(out, req, "This email address is currently valid in the system.");
130 PreparedStatement q3 = DatabaseConnection.getInstance().prepare("select `domain` from `baddomains` where `domain`=RIGHT(?, LENGTH(`domain`))");
131 q3.setString(1, buildup.getEmail());
133 ResultSet 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 } catch (SQLException e) {
141 outputError(out, req, "an internal error happened");
143 String mailResult = EmailProvider.FAIL;
145 mailResult = EmailProvider.getInstance().checkEmailServer(0, buildup.getEmail());
146 } catch (IOException e) {
148 if ( !mailResult.equals(EmailProvider.OK)) {
149 if (mailResult.startsWith("4")) {
150 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.");
152 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");
154 if (mailResult.equals(EmailProvider.FAIL)) {
155 outputError(out, req, "Failed to make a connection to the mail server");
157 outputErrorPlain(out, mailResult);
166 } catch (SQLException e) {
172 private void run(HttpServletRequest req, String password) throws SQLException {
174 DatabaseConnection.getInstance().beginTransaction();
176 buildup.setDob(myDoB.getDate());
177 buildup.insert(password);
178 int memid = buildup.getId();
179 EmailAddress ea = new EmailAddress(buildup.getEmail(), buildup);
180 ea.insert(Page.getLanguage(req));
182 PreparedStatement ps = DatabaseConnection.getInstance().prepare("insert into `alerts` set `memid`=?," + " `general`=?, `country`=?, `regional`=?, `radius`=?");
184 ps.setString(2, general ? "1" : "0");
185 ps.setString(3, country ? "1" : "0");
186 ps.setString(4, regional ? "1" : "0");
187 ps.setString(5, radius ? "1" : "0");
189 Notary.writeUserAgreement(memid, "CCA", "account creation", "", true, 0);
191 DatabaseConnection.getInstance().commitTransaction();
193 DatabaseConnection.getInstance().quitTransaction();