]> WPIA git - gigi.git/blob - src/org/cacert/gigi/pages/wot/AssurePage.java
[EMPTY] Formatting with configured formatter.
[gigi.git] / src / org / cacert / gigi / pages / wot / AssurePage.java
1 package org.cacert.gigi.pages.wot;
2
3 import java.io.IOException;
4 import java.io.InputStreamReader;
5 import java.io.PrintWriter;
6 import java.sql.PreparedStatement;
7 import java.sql.ResultSet;
8 import java.sql.SQLException;
9 import java.util.HashMap;
10
11 import javax.servlet.http.HttpServletRequest;
12 import javax.servlet.http.HttpServletResponse;
13 import javax.servlet.http.HttpSession;
14
15 import org.cacert.gigi.User;
16 import org.cacert.gigi.database.DatabaseConnection;
17 import org.cacert.gigi.output.DateSelector;
18 import org.cacert.gigi.output.Template;
19 import org.cacert.gigi.output.Form.CSRFError;
20 import org.cacert.gigi.pages.LoginPage;
21 import org.cacert.gigi.pages.Page;
22 import org.cacert.gigi.util.Notary;
23 import org.cacert.gigi.util.Notary.AssuranceResult;
24
25 public class AssurePage extends Page {
26         public static final String PATH = "/wot/assure";
27         public static final String SESSION = "/wot/assure/FORM";
28         DateSelector ds = new DateSelector("day", "month", "year");
29         Template t;
30
31         public AssurePage() {
32                 super("Assure someone");
33                 t = new Template(new InputStreamReader(AssuranceForm.class.getResourceAsStream("AssureeSearch.templ")));
34
35         }
36
37         @Override
38         public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
39
40                 PrintWriter out = resp.getWriter();
41                 String pi = req.getPathInfo().substring(PATH.length());
42                 if (pi.length() > 1) {
43                         User myself = LoginPage.getUser(req);
44                         int mid = Integer.parseInt(pi.substring(1));
45                         AssuranceResult check = Notary.checkAssuranceIsPossible(myself, new User(mid));
46                         if (check != AssuranceResult.ASSURANCE_SUCCEDED) {
47                                 out.println(translate(req, check.getMessage()));
48                                 return;
49                         }
50                         HttpSession hs = req.getSession();
51                         AssuranceForm form = (AssuranceForm) hs.getAttribute(SESSION);
52                         if (form == null || form.assuree.getId() != mid) {
53                                 form = new AssuranceForm(mid);
54                                 hs.setAttribute(SESSION, form);
55                         }
56
57                         form.output(out, getLanguage(req), new HashMap<String, Object>());
58                         ;
59                 } else {
60                         HashMap<String, Object> vars = new HashMap<String, Object>();
61                         vars.put("DoB", ds);
62                         t.output(out, getLanguage(req), vars);
63                 }
64         }
65
66         @Override
67         public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
68                 PrintWriter out = resp.getWriter();
69                 String pi = req.getPathInfo().substring(PATH.length());
70                 if (pi.length() > 1) {
71                         User myself = LoginPage.getUser(req);
72                         int mid = Integer.parseInt(pi.substring(1));
73                         if (mid == myself.getId()) {
74                                 out.println("Cannot assure myself.");
75                                 return;
76                         }
77
78                         AssuranceForm form = (AssuranceForm) req.getSession().getAttribute(SESSION);
79                         if (form == null) {
80                                 out.println("No form found. This is an Error. Fill in the form again.");
81                                 return;
82                         }
83                         try {
84                                 form.submit(out, req);
85                         } catch (CSRFError e) {
86                                 resp.sendError(500, "CSRF Failed");
87                                 out.println(translate(req, "CSRF Token failed."));
88                         }
89
90                         return;
91                 }
92
93                 System.out.println("searching for");
94                 ResultSet rs = null;
95                 try {
96                         PreparedStatement ps = DatabaseConnection.getInstance().prepare(
97                                 "SELECT id, verified FROM users WHERE email=? AND dob=? AND deleted=0");
98                         ps.setString(1, req.getParameter("email"));
99                         String day = req.getParameter("year") + "-" + req.getParameter("month") + "-" + req.getParameter("day");
100                         ps.setString(2, day);
101                         rs = ps.executeQuery();
102                         int id = 0;
103                         if (rs.next()) {
104                                 id = rs.getInt(1);
105                                 int verified = rs.getInt(2);
106                                 if (rs.next()) {
107                                         out.println("Error, ambigous user. Please contact support@cacert.org.");
108                                 } else {
109                                         if (verified == 0) {
110                                                 out.println(translate(req, "User is not yet verified. Please try again in 24 hours!"));
111                                         }
112                                         resp.sendRedirect(PATH + "/" + id);
113                                 }
114                         } else {
115                                 out.print("<div class='formError'>");
116
117                                 out.println(translate(req, "I'm sorry, there was no email and date of birth matching"
118                                         + " what you entered in the system. Please double check" + " your information."));
119                                 out.print("</div>");
120                         }
121
122                         rs.close();
123                 } catch (SQLException e) {
124                         e.printStackTrace();
125                 } finally {
126                         try {
127                                 if (rs != null) {
128                                         rs.close();
129                                 }
130                         } catch (SQLException e) {
131                                 e.printStackTrace();
132                         }
133                 }
134         }
135 }