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