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