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