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