]> WPIA git - gigi.git/blob - src/org/cacert/gigi/pages/main/Signup.java
500598181b003dcca003e646da46cfa118b28d65
[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.sql.SQLException;
6 import java.util.HashMap;
7 import java.util.Map;
8
9 import javax.servlet.http.HttpServletRequest;
10
11 import org.cacert.gigi.GigiApiException;
12 import org.cacert.gigi.database.DatabaseConnection;
13 import org.cacert.gigi.database.GigiPreparedStatement;
14 import org.cacert.gigi.database.GigiResultSet;
15 import org.cacert.gigi.dbObjects.Name;
16 import org.cacert.gigi.dbObjects.User;
17 import org.cacert.gigi.email.EmailProvider;
18 import org.cacert.gigi.localisation.Language;
19 import org.cacert.gigi.output.DateSelector;
20 import org.cacert.gigi.output.template.Form;
21 import org.cacert.gigi.output.template.Template;
22 import org.cacert.gigi.pages.Page;
23 import org.cacert.gigi.util.HTMLEncoder;
24 import org.cacert.gigi.util.Notary;
25 import org.cacert.gigi.util.PasswordStrengthChecker;
26
27 public class Signup extends Form {
28
29     Name buildupName = new Name("", "", "", "");
30
31     String email = "";
32
33     private Template t;
34
35     boolean general = true, country = true, regional = true, radius = true;
36
37     public Signup(HttpServletRequest hsr) {
38         super(hsr);
39         t = new Template(Signup.class.getResource("Signup.templ"));
40     }
41
42     DateSelector myDoB = new DateSelector("day", "month", "year");
43
44     @Override
45     public void outputContent(PrintWriter out, Language l, Map<String, Object> outerVars) {
46         HashMap<String, Object> vars = new HashMap<String, Object>();
47         vars.put("fname", HTMLEncoder.encodeHTML(buildupName.getFname()));
48         vars.put("mname", HTMLEncoder.encodeHTML(buildupName.getMname()));
49         vars.put("lname", HTMLEncoder.encodeHTML(buildupName.getLname()));
50         vars.put("suffix", HTMLEncoder.encodeHTML(buildupName.getSuffix()));
51         vars.put("dob", myDoB);
52         vars.put("email", HTMLEncoder.encodeHTML(email));
53         vars.put("general", general ? " checked=\"checked\"" : "");
54         vars.put("country", country ? " checked=\"checked\"" : "");
55         vars.put("regional", regional ? " checked=\"checked\"" : "");
56         vars.put("radius", radius ? " checked=\"checked\"" : "");
57         vars.put("helpOnNames", String.format(l.getTranslation("Help on Names %sin the wiki%s"), "<a href=\"//wiki.cacert.org/FAQ/HowToEnterNamesInJoinForm\" target=\"_blank\">", "</a>"));
58         vars.put("csrf", getCSRFToken());
59         t.output(out, l, vars);
60     }
61
62     private void update(HttpServletRequest r) {
63         String fname = buildupName.getFname();
64         String lname = buildupName.getLname();
65         String mname = buildupName.getMname();
66         String suffix = buildupName.getSuffix();
67         if (r.getParameter("fname") != null) {
68             fname = r.getParameter("fname");
69         }
70         if (r.getParameter("lname") != null) {
71             lname = r.getParameter("lname");
72         }
73         if (r.getParameter("mname") != null) {
74             mname = r.getParameter("mname");
75         }
76         if (r.getParameter("suffix") != null) {
77             suffix = r.getParameter("suffix");
78         }
79         if (r.getParameter("email") != null) {
80             email = r.getParameter("email");
81         }
82         buildupName = new Name(fname, lname, mname, suffix);
83         general = "1".equals(r.getParameter("general"));
84         country = "1".equals(r.getParameter("country"));
85         regional = "1".equals(r.getParameter("regional"));
86         radius = "1".equals(r.getParameter("radius"));
87         try {
88             myDoB.update(r);
89         } catch (GigiApiException e) {
90         }
91     }
92
93     @Override
94     public synchronized boolean submit(PrintWriter out, HttpServletRequest req) {
95         update(req);
96         if (buildupName.getLname().trim().equals("")) {
97             outputError(out, req, "Last name were blank.");
98         }
99         if ( !myDoB.isValid()) {
100             outputError(out, req, "Invalid date of birth");
101         }
102         if ( !"1".equals(req.getParameter("cca_agree"))) {
103             outputError(out, req, "You have to agree to the CAcert Community agreement.");
104         }
105         if (email.equals("")) {
106             outputError(out, req, "Email Address was blank");
107         }
108         String pw1 = req.getParameter("pword1");
109         String pw2 = req.getParameter("pword2");
110         if (pw1 == null || pw1.equals("")) {
111             outputError(out, req, "Pass Phrases were blank");
112         } else if ( !pw1.equals(pw2)) {
113             outputError(out, req, "Pass Phrases don't match");
114         }
115         int pwpoints = PasswordStrengthChecker.checkpw(pw1, buildupName, email);
116         if (pwpoints < 3) {
117             outputError(out, req, "The Pass Phrase you submitted failed to contain enough" + " differing characters and/or contained words from" + " your name and/or email address.");
118         }
119         if (isFailed(out)) {
120             return false;
121         }
122         GigiPreparedStatement q1 = DatabaseConnection.getInstance().prepare("SELECT * FROM `emails` WHERE `email`=? AND `deleted` IS NULL");
123         GigiPreparedStatement q2 = DatabaseConnection.getInstance().prepare("SELECT * FROM `certOwners` INNER JOIN `users` ON `users`.`id`=`certOwners`.`id` WHERE `email`=? AND `deleted` IS NULL");
124         q1.setString(1, email);
125         q2.setString(1, email);
126         GigiResultSet r1 = q1.executeQuery();
127         GigiResultSet r2 = q2.executeQuery();
128         if (r1.next() || r2.next()) {
129             outputError(out, req, "This email address is currently valid in the system.");
130         }
131         r1.close();
132         r2.close();
133         GigiPreparedStatement q3 = DatabaseConnection.getInstance().prepare("SELECT `domain` FROM `baddomains` WHERE `domain`=RIGHT(?, LENGTH(`domain`))");
134         q3.setString(1, email);
135
136         GigiResultSet r3 = q3.executeQuery();
137         if (r3.next()) {
138             String domain = r3.getString(1);
139             outputError(out, req, "We don't allow signups from people using email addresses from %s", domain);
140         }
141         r3.close();
142         String mailResult = EmailProvider.FAIL;
143         try {
144             mailResult = HTMLEncoder.encodeHTML(EmailProvider.getInstance().checkEmailServer(0, email));
145         } catch (IOException e) {
146         }
147         if ( !mailResult.equals(EmailProvider.OK)) {
148             if (mailResult.startsWith("4")) {
149                 outputError(out, req, "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.");
150             } else {
151                 outputError(out, req, "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");
152             }
153             if (mailResult.equals(EmailProvider.FAIL)) {
154                 outputError(out, req, "Failed to make a connection to the mail server");
155             } else {
156                 outputErrorPlain(out, mailResult);
157             }
158         }
159
160         if (isFailed(out)) {
161             return false;
162         }
163         try {
164             run(req, pw1);
165         } catch (SQLException e) {
166             e.printStackTrace();
167         } catch (GigiApiException e) {
168             outputError(out, req, e.getMessage());
169             return false;
170         }
171         return true;
172     }
173
174     private void run(HttpServletRequest req, String password) throws SQLException, GigiApiException {
175         try {
176             DatabaseConnection.getInstance().beginTransaction();
177             User u = new User(email, password, buildupName, myDoB.getDate(), Page.getLanguage(req).getLocale());
178
179             GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO `alerts` SET `memid`=?," + " `general`=?, `country`=?, `regional`=?, `radius`=?");
180             ps.setInt(1, u.getId());
181             ps.setBoolean(2, general);
182             ps.setBoolean(3, country);
183             ps.setBoolean(4, regional);
184             ps.setBoolean(5, radius);
185             ps.execute();
186             Notary.writeUserAgreement(u, "CCA", "account creation", "", true, 0);
187
188             DatabaseConnection.getInstance().commitTransaction();
189         } finally {
190             DatabaseConnection.getInstance().quitTransaction();
191         }
192
193     }
194 }