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