]> WPIA git - gigi.git/blob - src/org/cacert/gigi/pages/main/Signup.java
0fc04d8e9f9144e3202b956ef009bc74a3cab31b
[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.util.HashMap;
6 import java.util.Map;
7
8 import javax.servlet.http.HttpServletRequest;
9
10 import org.cacert.gigi.GigiApiException;
11 import org.cacert.gigi.database.GigiPreparedStatement;
12 import org.cacert.gigi.database.GigiResultSet;
13 import org.cacert.gigi.dbObjects.Name;
14 import org.cacert.gigi.dbObjects.User;
15 import org.cacert.gigi.email.EmailProvider;
16 import org.cacert.gigi.localisation.Language;
17 import org.cacert.gigi.output.DateSelector;
18 import org.cacert.gigi.output.template.Form;
19 import org.cacert.gigi.output.template.PlainOutputable;
20 import org.cacert.gigi.output.template.SprintfCommand;
21 import org.cacert.gigi.output.template.Template;
22 import org.cacert.gigi.pages.Page;
23 import org.cacert.gigi.util.CalendarUtil;
24 import org.cacert.gigi.util.HTMLEncoder;
25 import org.cacert.gigi.util.Notary;
26 import org.cacert.gigi.util.PasswordStrengthChecker;
27 import org.cacert.gigi.util.RateLimit.RateLimitException;
28
29 public class Signup extends Form {
30
31     Name buildupName = new Name("", "", "", "");
32
33     String email = "";
34
35     private static final Template t = new Template(Signup.class.getResource("Signup.templ"));
36
37     boolean general = true, country = true, regional = true, radius = true;
38
39     public Signup(HttpServletRequest hsr) {
40         super(hsr);
41
42     }
43
44     DateSelector myDoB = new DateSelector("day", "month", "year");
45
46     @Override
47     public void outputContent(PrintWriter out, Language l, Map<String, Object> outerVars) {
48         HashMap<String, Object> vars = new HashMap<String, Object>();
49         vars.put("fname", HTMLEncoder.encodeHTML(buildupName.getFname()));
50         vars.put("mname", HTMLEncoder.encodeHTML(buildupName.getMname()));
51         vars.put("lname", HTMLEncoder.encodeHTML(buildupName.getLname()));
52         vars.put("suffix", HTMLEncoder.encodeHTML(buildupName.getSuffix()));
53         vars.put("dob", myDoB);
54         vars.put("email", HTMLEncoder.encodeHTML(email));
55         vars.put("general", general ? " checked=\"checked\"" : "");
56         vars.put("country", country ? " checked=\"checked\"" : "");
57         vars.put("regional", regional ? " checked=\"checked\"" : "");
58         vars.put("radius", radius ? " checked=\"checked\"" : "");
59         vars.put("helpOnNames", String.format(l.getTranslation("Help on Names %sin the wiki%s"), "<a href=\"//wiki.cacert.org/FAQ/HowToEnterNamesInJoinForm\" target=\"_blank\">", "</a>"));
60         vars.put("csrf", getCSRFToken());
61         vars.put("dobmin", User.MINIMUM_AGE + "");
62         t.output(out, l, vars);
63     }
64
65     private void update(HttpServletRequest r) {
66         String fname = buildupName.getFname();
67         String lname = buildupName.getLname();
68         String mname = buildupName.getMname();
69         String suffix = buildupName.getSuffix();
70         if (r.getParameter("fname") != null) {
71             fname = r.getParameter("fname");
72         }
73         if (r.getParameter("lname") != null) {
74             lname = r.getParameter("lname");
75         }
76         if (r.getParameter("mname") != null) {
77             mname = r.getParameter("mname");
78         }
79         if (r.getParameter("suffix") != null) {
80             suffix = r.getParameter("suffix");
81         }
82         if (r.getParameter("email") != null) {
83             email = r.getParameter("email");
84         }
85         buildupName = new Name(fname, lname, mname, suffix);
86         general = "1".equals(r.getParameter("general"));
87         country = "1".equals(r.getParameter("country"));
88         regional = "1".equals(r.getParameter("regional"));
89         radius = "1".equals(r.getParameter("radius"));
90         try {
91             myDoB.update(r);
92         } catch (GigiApiException e) {
93         }
94     }
95
96     @Override
97     public synchronized boolean submit(PrintWriter out, HttpServletRequest req) throws GigiApiException {
98         if (RegisterPage.RATE_LIMIT.isLimitExceeded(req.getRemoteAddr())) {
99             throw new RateLimitException();
100         }
101
102         update(req);
103         GigiApiException ga = new GigiApiException();
104         if (buildupName.getLname().trim().equals("")) {
105             ga.mergeInto(new GigiApiException("Last name were blank."));
106         }
107         if ( !myDoB.isValid()) {
108             ga.mergeInto(new GigiApiException("Invalid date of birth"));
109         }
110
111         if ( !CalendarUtil.isOfAge(myDoB.getDate(), User.MINIMUM_AGE)) {
112             ga.mergeInto(new GigiApiException("Entered dated of birth is below the restricted age requirements."));
113         }
114
115         if ( !"1".equals(req.getParameter("tos_agree"))) {
116             ga.mergeInto(new GigiApiException("Acceptance of the ToS is required to continue."));
117         }
118         if (email.equals("")) {
119             ga.mergeInto(new GigiApiException("Email Address was blank"));
120         }
121         String pw1 = req.getParameter("pword1");
122         String pw2 = req.getParameter("pword2");
123         if (pw1 == null || pw1.equals("")) {
124             ga.mergeInto(new GigiApiException("Pass Phrases were blank"));
125         } else if ( !pw1.equals(pw2)) {
126             ga.mergeInto(new GigiApiException("Pass Phrases don't match"));
127         }
128         int pwpoints = PasswordStrengthChecker.checkpw(pw1, buildupName, email);
129         if (pwpoints < 3) {
130             ga.mergeInto(new GigiApiException("The Pass Phrase you submitted failed to contain enough" + " differing characters and/or contained words from" + " your name and/or email address."));
131         }
132         if ( !ga.isEmpty()) {
133             throw ga;
134         }
135         GigiApiException ga2 = new GigiApiException();
136         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")) {
137             q1.setString(1, email);
138             q2.setString(1, email);
139             GigiResultSet r1 = q1.executeQuery();
140             GigiResultSet r2 = q2.executeQuery();
141             if (r1.next() || r2.next()) {
142                 ga2.mergeInto(new GigiApiException("This email address is currently valid in the system."));
143             }
144         }
145         try (GigiPreparedStatement q3 = new GigiPreparedStatement("SELECT `domain` FROM `baddomains` WHERE `domain`=RIGHT(?, LENGTH(`domain`))")) {
146             q3.setString(1, email);
147
148             GigiResultSet r3 = q3.executeQuery();
149             if (r3.next()) {
150                 String domain = r3.getString(1);
151                 ga2.mergeInto(new GigiApiException(SprintfCommand.createSimple("We don't allow signups from people using email addresses from {0}.", domain)));
152             }
153         }
154         String mailResult = EmailProvider.FAIL;
155         try {
156             mailResult = HTMLEncoder.encodeHTML(EmailProvider.getInstance().checkEmailServer(0, email));
157         } catch (IOException e) {
158         }
159         if ( !mailResult.equals(EmailProvider.OK)) {
160             if (mailResult.startsWith("4")) {
161                 ga2.mergeInto(new GigiApiException("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."));
162             } else {
163                 ga2.mergeInto(new GigiApiException("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"));
164             }
165             if (mailResult.equals(EmailProvider.FAIL)) {
166                 ga2.mergeInto(new GigiApiException("Failed to make a connection to the mail server"));
167             } else {
168                 ga2.mergeInto(new GigiApiException(new PlainOutputable(mailResult)));
169             }
170         }
171
172         if ( !ga2.isEmpty()) {
173             throw ga2;
174         }
175         run(req, pw1);
176         return true;
177     }
178
179     private void run(HttpServletRequest req, String password) throws GigiApiException {
180         User u = new User(email, password, buildupName, myDoB.getDate(), Page.getLanguage(req).getLocale());
181
182         try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `alerts` SET `memid`=?," + " `general`=?, `country`=?, `regional`=?, `radius`=?")) {
183             ps.setInt(1, u.getId());
184             ps.setBoolean(2, general);
185             ps.setBoolean(3, country);
186             ps.setBoolean(4, regional);
187             ps.setBoolean(5, radius);
188             ps.execute();
189         }
190         Notary.writeUserAgreement(u, "ToS", "account creation", "", true, 0);
191
192     }
193 }