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