]> WPIA git - gigi.git/blob - src/club/wpia/gigi/pages/main/Signup.java
b6585cb071a63c4b5a59c9e4be770a209707f857
[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.passwords.PasswordStrengthChecker;
27 import club.wpia.gigi.util.CalendarUtil;
28 import club.wpia.gigi.util.HTMLEncoder;
29 import club.wpia.gigi.util.Notary;
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 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>(outerVars);
53         vars.put("name", ni);
54         vars.put("dob", myDoB);
55         vars.put("email", HTMLEncoder.encodeHTML(email));
56         vars.put("helpOnNames", new SprintfCommand("Help on Names {0}in the knowledge base{1}", Arrays.asList("!(/kb/names", "!'</a>")));
57         vars.put("csrf", getCSRFToken());
58         vars.put("dobmin", User.MINIMUM_AGE + "");
59         vars.put("countryCode", cs);
60         t.output(out, l, vars);
61     }
62
63     private void update(HttpServletRequest r) throws GigiApiException {
64         if (r.getParameter("email") != null) {
65             email = r.getParameter("email");
66         }
67         GigiApiException problems = new GigiApiException();
68         try {
69             ni.update(r);
70         } catch (GigiApiException e) {
71             problems.mergeInto(e);
72         }
73         try {
74             myDoB.update(r);
75         } catch (GigiApiException e) {
76             problems.mergeInto(e);
77         }
78
79         cs.update(r);
80
81         if ( !problems.isEmpty()) {
82             throw problems;
83         }
84
85     }
86
87     @Override
88     public synchronized SubmissionResult submit(HttpServletRequest req) throws GigiApiException {
89         if (RegisterPage.RATE_LIMIT.isLimitExceeded(req.getRemoteAddr())) {
90             throw new RateLimitException();
91         }
92
93         GigiApiException ga = new GigiApiException();
94         try {
95             update(req);
96         } catch (GigiApiException e) {
97             ga.mergeInto(e);
98         }
99         try {
100             ni.getNameParts();
101         } catch (GigiApiException e) {
102             ga.mergeInto(e);
103         }
104
105         if ( !myDoB.isValid()) {
106             ga.mergeInto(new GigiApiException("Invalid date of birth"));
107         }
108
109         if ( !CalendarUtil.isOfAge(myDoB.getDate(), User.MINIMUM_AGE)) {
110             ga.mergeInto(new GigiApiException("Entered date of birth is below the restricted age requirements."));
111         }
112
113         if (CalendarUtil.isYearsInFuture(myDoB.getDate().end(), User.MAXIMUM_PLAUSIBLE_AGE)) {
114             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."));
115         }
116
117         if ( !"1".equals(req.getParameter("tos_agree"))) {
118             ga.mergeInto(new GigiApiException("Acceptance of the ToS is required to continue."));
119         }
120         if (email.equals("")) {
121             ga.mergeInto(new GigiApiException("Email Address was blank"));
122         }
123         String pw1 = req.getParameter("pword1");
124         String pw2 = req.getParameter("pword2");
125         if (pw1 == null || pw1.equals("")) {
126             ga.mergeInto(new GigiApiException("Passwords were blank"));
127         } else if ( !pw1.equals(pw2)) {
128             ga.mergeInto(new GigiApiException("Passwords don't match"));
129         }
130         if ( !ga.isEmpty()) {
131             throw ga;
132         }
133         GigiApiException gaPassword = new PasswordStrengthChecker().checkPassword(pw1, ni.getNamePartsPlain(), email);
134         if (gaPassword != null) {
135             throw gaPassword;
136         }
137         GigiApiException ga2 = new GigiApiException();
138         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")) {
139             q1.setString(1, email);
140             q2.setString(1, email);
141             GigiResultSet r1 = q1.executeQuery();
142             GigiResultSet r2 = q2.executeQuery();
143             if (r1.next() || r2.next()) {
144                 ga2.mergeInto(new GigiApiException("This email address is currently valid in the system."));
145             }
146         }
147         try (GigiPreparedStatement q3 = new GigiPreparedStatement("SELECT `domain` FROM `baddomains` WHERE `domain`=RIGHT(?, LENGTH(`domain`))")) {
148             q3.setString(1, email);
149
150             GigiResultSet r3 = q3.executeQuery();
151             if (r3.next()) {
152                 String domain = r3.getString(1);
153                 ga2.mergeInto(new GigiApiException(SprintfCommand.createSimple("We don't allow signups from people using email addresses from {0}.", domain)));
154             }
155         }
156         String mailResult = EmailProvider.FAIL;
157         try {
158             mailResult = HTMLEncoder.encodeHTML(EmailProvider.getInstance().checkEmailServer(0, email));
159         } catch (IOException e) {
160         }
161         if ( !mailResult.equals(EmailProvider.OK)) {
162             if (mailResult.startsWith("4")) {
163                 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."));
164             } else {
165                 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"));
166             }
167             if (mailResult.equals(EmailProvider.FAIL)) {
168                 ga2.mergeInto(new GigiApiException("Failed to make a connection to the mail server"));
169             } else {
170                 ga2.mergeInto(new GigiApiException(new PlainOutputable(mailResult)));
171             }
172         }
173
174         if ( !ga2.isEmpty()) {
175             throw ga2;
176         }
177         run(req, pw1);
178         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!"));
179     }
180
181     private void run(HttpServletRequest req, String password) throws GigiApiException {
182         User u = new User(email, password, myDoB.getDate(), Page.getLanguage(req).getLocale(), cs.getCountry(), ni.getNameParts());
183         Notary.writeUserAgreement(u, "ToS", "account creation", "", true, 0);
184     }
185
186 }