1 package org.cacert.gigi;
3 import java.io.IOException;
4 import java.sql.PreparedStatement;
5 import java.sql.ResultSet;
6 import java.sql.SQLException;
8 import org.cacert.gigi.database.DatabaseConnection;
9 import org.cacert.gigi.email.EmailProvider;
10 import org.cacert.gigi.util.ServerConstants;
12 public class EmailAddress {
13 private String address;
16 private String hash = null;
18 private EmailAddress(int id) throws SQLException {
19 PreparedStatement ps = DatabaseConnection.getInstance().prepare(
20 "SELECT memid, email, hash FROM `email` WHERE id=? AND deleted=0");
23 ResultSet rs = ps.executeQuery();
25 throw new IllegalArgumentException("Invalid email id " + id);
28 owner = User.getById(rs.getInt(1));
29 address = rs.getString(2);
30 hash = rs.getString(3);
34 public EmailAddress(String address, User owner, String hash) {
35 this.address = address;
40 public void insert(Language l) {
42 throw new IllegalStateException("already inserted.");
45 PreparedStatement ps = DatabaseConnection.getInstance().prepare(
46 "INSERT INTO `email` SET memid=?, hash=?, email=?");
47 ps.setInt(1, owner.getId());
48 ps.setString(2, hash);
49 ps.setString(3, address);
51 id = DatabaseConnection.lastInsertId(ps);
52 StringBuffer body = new StringBuffer();
54 .getTranslation("Thanks for signing up with CAcert.org, below is the link you need to open to verify your account. Once your account is verified you will be able to start issuing certificates till your hearts' content!"));
55 body.append("\n\nhttps://");
56 body.append(ServerConstants.getWwwHostNamePort());
57 body.append("/verify?type=email&id=");
59 body.append("&hash=");
62 body.append(l.getTranslation("Best regards"));
64 body.append(l.getTranslation("CAcert.org Support!"));
65 EmailProvider.getInstance().sendmail(address, "[CAcert.org] " + l.getTranslation("Mail Probe"),
66 body.toString(), "support@cacert.org", null, null, null, null, false);
67 } catch (SQLException e) {
69 } catch (IOException e) {
78 public String getAddress() {
82 public synchronized void verify(String hash) throws GigiApiException {
83 if (this.hash.equals(hash)) {
86 PreparedStatement ps = DatabaseConnection.getInstance()
87 .prepare("UPDATE `email` SET hash='' WHERE id=?");
92 // Verify user with that primary email
93 PreparedStatement ps2 = DatabaseConnection.getInstance().prepare(
94 "update `users` set `verified`='1' where `id`=? and `email`=? and `verified`='0'");
95 ps2.setInt(1, owner.getId());
96 ps2.setString(2, address);
98 } catch (SQLException e) {
99 throw new GigiApiException(e);
103 throw new GigiApiException("Email verification hash is invalid.");
107 public static EmailAddress getById(int id) throws IllegalArgumentException {
110 EmailAddress e = new EmailAddress(id);
112 } catch (SQLException e) {
113 throw new IllegalArgumentException(e);