1 package club.wpia.gigi.pages.main;
3 import java.io.IOException;
4 import java.io.PrintWriter;
5 import java.util.Arrays;
6 import java.util.HashMap;
9 import javax.servlet.http.HttpServletRequest;
11 import club.wpia.gigi.Gigi;
12 import club.wpia.gigi.GigiApiException;
13 import club.wpia.gigi.database.GigiPreparedStatement;
14 import club.wpia.gigi.database.GigiResultSet;
15 import club.wpia.gigi.dbObjects.User;
16 import club.wpia.gigi.email.EmailProvider;
17 import club.wpia.gigi.localisation.Language;
18 import club.wpia.gigi.output.CountrySelector;
19 import club.wpia.gigi.output.DateSelector;
20 import club.wpia.gigi.output.NameInput;
21 import club.wpia.gigi.output.template.Form;
22 import club.wpia.gigi.output.template.PlainOutputable;
23 import club.wpia.gigi.output.template.SprintfCommand;
24 import club.wpia.gigi.output.template.Template;
25 import club.wpia.gigi.output.template.TranslateCommand;
26 import club.wpia.gigi.pages.Page;
27 import club.wpia.gigi.passwords.PasswordStrengthChecker;
28 import club.wpia.gigi.util.CalendarUtil;
29 import club.wpia.gigi.util.HTMLEncoder;
30 import club.wpia.gigi.util.Notary;
31 import club.wpia.gigi.util.RateLimit.RateLimitException;
33 public class Signup extends Form {
37 private String email = "";
39 private static final Template t = new Template(Signup.class.getResource("Signup.templ"));
41 private CountrySelector cs;
43 public Signup(HttpServletRequest hsr) {
46 cs = new CountrySelector("residenceCountry", true);
49 private DateSelector myDoB = new DateSelector("day", "month", "year");
52 public void outputContent(PrintWriter out, Language l, Map<String, Object> outerVars) {
53 HashMap<String, Object> vars = new HashMap<String, Object>(outerVars);
55 vars.put("dob", myDoB);
56 vars.put("email", HTMLEncoder.encodeHTML(email));
57 vars.put("helpOnNames", new SprintfCommand("Help on Names {0}in the knowledge base{1}", Arrays.asList("!(/kb/names", "!'</a>")));
58 vars.put("csrf", getCSRFToken());
59 vars.put("dobmin", User.MINIMUM_AGE + "");
60 vars.put("countryCode", cs);
61 t.output(out, l, vars);
64 private void update(HttpServletRequest r) throws GigiApiException {
65 if (r.getParameter("email") != null) {
66 email = r.getParameter("email");
68 GigiApiException problems = new GigiApiException();
71 } catch (GigiApiException e) {
72 problems.mergeInto(e);
76 } catch (GigiApiException e) {
77 problems.mergeInto(e);
82 if ( !problems.isEmpty()) {
89 public synchronized SubmissionResult submit(HttpServletRequest req) throws GigiApiException {
90 if (RegisterPage.RATE_LIMIT.isLimitExceeded(req.getRemoteAddr())) {
91 throw new RateLimitException();
94 GigiApiException ga = new GigiApiException();
97 } catch (GigiApiException e) {
102 } catch (GigiApiException e) {
106 if ( !myDoB.isValid()) {
107 ga.mergeInto(new GigiApiException("Invalid date of birth"));
110 if ( !CalendarUtil.isOfAge(myDoB.getDate(), User.MINIMUM_AGE)) {
111 ga.mergeInto(new GigiApiException("Entered date of birth is below the restricted age requirements."));
114 if (CalendarUtil.isYearsInFuture(myDoB.getDate().end(), User.MAXIMUM_PLAUSIBLE_AGE)) {
115 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."));
118 if ( !"1".equals(req.getParameter("tos_agree"))) {
119 ga.mergeInto(new GigiApiException("Acceptance of the ToS is required to continue."));
121 if (email.equals("")) {
122 ga.mergeInto(new GigiApiException("Email Address was blank"));
124 String pw1 = req.getParameter("pword1");
125 String pw2 = req.getParameter("pword2");
126 if (pw1 == null || pw1.equals("")) {
127 ga.mergeInto(new GigiApiException("Passwords were blank"));
128 } else if ( !pw1.equals(pw2)) {
129 ga.mergeInto(new GigiApiException("Passwords don't match"));
131 if ( !ga.isEmpty()) {
134 GigiApiException gaPassword = Gigi.getPasswordChecker().checkPassword(pw1, ni.getNamePartsPlain(), email);
135 if (gaPassword != null) {
138 GigiApiException ga2 = new GigiApiException();
139 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")) {
140 q1.setString(1, email);
141 q2.setString(1, email);
142 GigiResultSet r1 = q1.executeQuery();
143 GigiResultSet r2 = q2.executeQuery();
144 if (r1.next() || r2.next()) {
145 ga2.mergeInto(new GigiApiException("This email address is currently valid in the system."));
148 try (GigiPreparedStatement q3 = new GigiPreparedStatement("SELECT `domain` FROM `baddomains` WHERE `domain`=RIGHT(?, LENGTH(`domain`))")) {
149 q3.setString(1, email);
151 GigiResultSet r3 = q3.executeQuery();
153 String domain = r3.getString(1);
154 ga2.mergeInto(new GigiApiException(SprintfCommand.createSimple("We don't allow signups from people using email addresses from {0}.", domain)));
157 String mailResult = EmailProvider.FAIL;
159 mailResult = HTMLEncoder.encodeHTML(EmailProvider.getInstance().checkEmailServer(0, email));
160 } catch (IOException e) {
162 if ( !mailResult.equals(EmailProvider.OK)) {
163 if (mailResult.startsWith("4")) {
164 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."));
166 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"));
168 if (mailResult.equals(EmailProvider.FAIL)) {
169 ga2.mergeInto(new GigiApiException("Failed to make a connection to the mail server"));
171 ga2.mergeInto(new GigiApiException(new PlainOutputable(mailResult)));
175 if ( !ga2.isEmpty()) {
179 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!"));
182 private void run(HttpServletRequest req, String password) throws GigiApiException {
183 User u = new User(email, password, myDoB.getDate(), Page.getLanguage(req).getLocale(), cs.getCountry(), ni.getNameParts());
184 Notary.writeUserAgreement(u, "ToS", "account creation", "", true, 0);