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