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.RandomToken;
11 import org.cacert.gigi.util.ServerConstants;
13 public class EmailAddress {
14 private String address;
17 private String hash = null;
19 private EmailAddress(int id) throws SQLException {
20 PreparedStatement ps = DatabaseConnection.getInstance().prepare(
21 "SELECT memid, email, hash FROM `email` WHERE id=? AND deleted=0");
24 ResultSet rs = ps.executeQuery();
26 throw new IllegalArgumentException("Invalid email id " + id);
29 owner = User.getById(rs.getInt(1));
30 address = rs.getString(2);
31 hash = rs.getString(3);
35 public EmailAddress(String address, User owner) {
36 this.address = address;
38 this.hash = RandomToken.generateToken(16);
41 public void insert(Language l) {
43 throw new IllegalStateException("already inserted.");
46 PreparedStatement ps = DatabaseConnection.getInstance().prepare(
47 "INSERT INTO `email` SET memid=?, hash=?, email=?");
48 ps.setInt(1, owner.getId());
49 ps.setString(2, hash);
50 ps.setString(3, address);
52 id = DatabaseConnection.lastInsertId(ps);
53 StringBuffer body = new StringBuffer();
55 .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!"));
56 body.append("\n\nhttps://");
57 body.append(ServerConstants.getWwwHostNamePort());
58 body.append("/verify?type=email&id=");
60 body.append("&hash=");
63 body.append(l.getTranslation("Best regards"));
65 body.append(l.getTranslation("CAcert.org Support!"));
66 EmailProvider.getInstance().sendmail(address, "[CAcert.org] " + l.getTranslation("Mail Probe"),
67 body.toString(), "support@cacert.org", null, null, null, null, false);
68 } catch (SQLException e) {
70 } catch (IOException e) {
79 public String getAddress() {
83 public synchronized void verify(String hash) throws GigiApiException {
84 if (this.hash.equals(hash)) {
87 PreparedStatement ps = DatabaseConnection.getInstance()
88 .prepare("UPDATE `email` SET hash='' WHERE id=?");
93 // Verify user with that primary email
94 PreparedStatement ps2 = DatabaseConnection.getInstance().prepare(
95 "update `users` set `verified`='1' where `id`=? and `email`=? and `verified`='0'");
96 ps2.setInt(1, owner.getId());
97 ps2.setString(2, address);
100 } catch (SQLException e) {
101 throw new GigiApiException(e);
105 throw new GigiApiException("Email verification hash is invalid.");
109 public static EmailAddress getById(int id) throws IllegalArgumentException {
112 EmailAddress e = new EmailAddress(id);
114 } catch (SQLException e) {
115 throw new IllegalArgumentException(e);
119 public boolean isVerified() {
120 return hash.isEmpty();