]> WPIA git - gigi.git/blob - src/org/cacert/gigi/pages/main/Signup.java
ToS: replacement of CCA/ToS in Signup
[gigi.git] / src / org / cacert / gigi / pages / main / Signup.java
1 package org.cacert.gigi.pages.main;
2
3 import java.io.IOException;
4 import java.io.PrintWriter;
5 import java.sql.SQLException;
6 import java.util.HashMap;
7 import java.util.Map;
8
9 import javax.servlet.http.HttpServletRequest;
10
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;
25
26 public class Signup extends Form {
27
28     Name buildupName = new Name("", "", "", "");
29
30     String email = "";
31
32     private Template t;
33
34     boolean general = true, country = true, regional = true, radius = true;
35
36     public Signup(HttpServletRequest hsr) {
37         super(hsr);
38         t = new Template(Signup.class.getResource("Signup.templ"));
39     }
40
41     DateSelector myDoB = new DateSelector("day", "month", "year");
42
43     @Override
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);
59     }
60
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");
68         }
69         if (r.getParameter("lname") != null) {
70             lname = r.getParameter("lname");
71         }
72         if (r.getParameter("mname") != null) {
73             mname = r.getParameter("mname");
74         }
75         if (r.getParameter("suffix") != null) {
76             suffix = r.getParameter("suffix");
77         }
78         if (r.getParameter("email") != null) {
79             email = r.getParameter("email");
80         }
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"));
86         try {
87             myDoB.update(r);
88         } catch (GigiApiException e) {
89         }
90     }
91
92     @Override
93     public synchronized boolean submit(PrintWriter out, HttpServletRequest req) {
94         update(req);
95         if (buildupName.getLname().trim().equals("")) {
96             outputError(out, req, "Last name were blank.");
97         }
98         if ( !myDoB.isValid()) {
99             outputError(out, req, "Invalid date of birth");
100         }
101         if ( !"1".equals(req.getParameter("tos_agree"))) {
102             outputError(out, req, "Acceptance of the ToS is required to continue.");
103         }
104         if (email.equals("")) {
105             outputError(out, req, "Email Address was blank");
106         }
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");
113         }
114         int pwpoints = PasswordStrengthChecker.checkpw(pw1, buildupName, email);
115         if (pwpoints < 3) {
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.");
117         }
118         if (isFailed(out)) {
119             return false;
120         }
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.");
128             }
129         }
130         try (GigiPreparedStatement q3 = new GigiPreparedStatement("SELECT `domain` FROM `baddomains` WHERE `domain`=RIGHT(?, LENGTH(`domain`))")) {
131             q3.setString(1, email);
132
133             GigiResultSet r3 = q3.executeQuery();
134             if (r3.next()) {
135                 String domain = r3.getString(1);
136                 outputError(out, req, "We don't allow signups from people using email addresses from %s", domain);
137             }
138         }
139         String mailResult = EmailProvider.FAIL;
140         try {
141             mailResult = HTMLEncoder.encodeHTML(EmailProvider.getInstance().checkEmailServer(0, email));
142         } catch (IOException e) {
143         }
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.");
147             } else {
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");
149             }
150             if (mailResult.equals(EmailProvider.FAIL)) {
151                 outputError(out, req, "Failed to make a connection to the mail server");
152             } else {
153                 outputErrorPlain(out, mailResult);
154             }
155         }
156
157         if (isFailed(out)) {
158             return false;
159         }
160         if (RegisterPage.RATE_LIMIT.isLimitExceeded(req.getRemoteAddr())) {
161             outputError(out, req, "Rate Limit Exceeded");
162             return false;
163         }
164         try {
165             run(req, pw1);
166         } catch (SQLException e) {
167             e.printStackTrace();
168         } catch (GigiApiException e) {
169             e.format(out, Page.getLanguage(req));
170             return false;
171         }
172         return true;
173     }
174
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());
177
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);
184             ps.execute();
185         }
186         Notary.writeUserAgreement(u, "ToS", "account creation", "", true, 0);
187
188     }
189 }