Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Whitelist generator.

git-svn-id: https://subsonic.svn.sourceforge.net/svnroot/subsonic/trunk@2425 24148959-8e0e-0410-9c20-a6ce11f09cd5
  • Loading branch information...
commit 60c7389e24fbb01f0f4fe5b5ae169eeb936190d7 1 parent 1092457
sindre_mehus authored
View
34 ...ic-backend/src/main/java/net/sourceforge/subsonic/backend/controller/MultiController.java
@@ -20,6 +20,7 @@
import java.io.PrintWriter;
import java.util.Calendar;
+import java.util.Date;
import java.util.Map;
import java.util.HashMap;
import java.util.List;
@@ -29,6 +30,7 @@
import javax.servlet.http.HttpServletResponse;
import net.sourceforge.subsonic.backend.dao.PaymentDao;
+import net.sourceforge.subsonic.backend.service.WhitelistGenerator;
import org.apache.log4j.Logger;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.springframework.web.servlet.ModelAndView;
@@ -52,17 +54,20 @@
private static final String SUBSONIC_VERSION = "4.5";
private static final String SUBSONIC_BETA_VERSION = "4.5.beta2";
+ private static final Date LICENSE_DATE_THRESHOLD;
+
private DaoHelper daoHelper;
+
private PaymentDao paymentDao;
+ private WhitelistGenerator whitelistGenerator;
- private static final long LICENSE_DATE_THRESHOLD;
static {
Calendar calendar = Calendar.getInstance();
calendar.clear();
calendar.set(Calendar.YEAR, 2010);
calendar.set(Calendar.MONTH, Calendar.JUNE);
calendar.set(Calendar.DAY_OF_MONTH, 19);
- LICENSE_DATE_THRESHOLD = calendar.getTime().getTime();
+ LICENSE_DATE_THRESHOLD = calendar.getTime();
}
public ModelAndView version(HttpServletRequest request, HttpServletResponse response) throws Exception {
@@ -133,6 +138,25 @@ public ModelAndView db(HttpServletRequest request, HttpServletResponse response)
return new ModelAndView("backend/db", "model", map);
}
+ public ModelAndView whitelist(HttpServletRequest request, HttpServletResponse response) throws Exception {
+
+ String password = ServletRequestUtils.getRequiredStringParameter(request, "p");
+ if (!password.equals(Util.getPassword("backendpwd.txt"))) {
+ response.sendError(HttpServletResponse.SC_FORBIDDEN);
+ return null;
+ }
+
+ Date newerThan = MultiController.LICENSE_DATE_THRESHOLD;
+ Integer days = ServletRequestUtils.getIntParameter(request, "days");
+ if (days != null) {
+ Calendar cal = Calendar.getInstance();
+ cal.add(Calendar.DATE, -days);
+ newerThan = cal.getTime();
+ }
+ whitelistGenerator.generate(newerThan);
+ return null;
+ }
+
private boolean isLicenseValid(String email, Long date) {
if (email == null || date == null) {
return false;
@@ -143,7 +167,7 @@ private boolean isLicenseValid(String email, Long date) {
}
// Always accept licenses that are older than 2010-06-19.
- if (date < LICENSE_DATE_THRESHOLD) {
+ if (date < LICENSE_DATE_THRESHOLD.getTime()) {
return true;
}
@@ -157,4 +181,8 @@ public void setDaoHelper(DaoHelper daoHelper) {
public void setPaymentDao(PaymentDao paymentDao) {
this.paymentDao = paymentDao;
}
+
+ public void setWhitelistGenerator(WhitelistGenerator whitelistGenerator) {
+ this.whitelistGenerator = whitelistGenerator;
+ }
}
View
34 subsonic-backend/src/main/java/net/sourceforge/subsonic/backend/dao/PaymentDao.java
@@ -1,6 +1,7 @@
package net.sourceforge.subsonic.backend.dao;
import net.sourceforge.subsonic.backend.domain.Payment;
+import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
@@ -20,7 +21,7 @@
private static final Logger LOG = Logger.getLogger(PaymentDao.class);
private static final String COLUMNS = "id, transaction_id, transaction_type, item, " +
"payment_type, payment_status, payment_amount, payment_currency, " +
- "payer_email, payer_first_name, payer_last_name, payer_country, " +
+ "payer_email, payer_email_lower, payer_first_name, payer_last_name, payer_country, " +
"processing_status, created, last_updated";
private RowMapper paymentRowMapper = new PaymentRowMapper();
@@ -47,8 +48,8 @@ public Payment getPaymentByEmail(String email) {
if (email == null) {
return null;
}
- String sql = "select " + COLUMNS + " from payment where payer_email=?";
- return queryOne(sql, paymentRowMapper, email);
+ String sql = "select " + COLUMNS + " from payment where payer_email_lower=?";
+ return queryOne(sql, paymentRowMapper, email.toLowerCase());
}
/**
@@ -70,8 +71,9 @@ public void createPayment(Payment payment) {
String sql = "insert into payment (" + COLUMNS + ") values (" + questionMarks(COLUMNS) + ")";
update(sql, null, payment.getTransactionId(), payment.getTransactionType(), payment.getItem(),
payment.getPaymentType(), payment.getPaymentStatus(), payment.getPaymentAmount(),
- payment.getPaymentCurrency(), payment.getPayerEmail(), payment.getPayerFirstName(), payment.getPayerLastName(),
- payment.getPayerCountry(), payment.getProcessingStatus().name(), payment.getCreated(), payment.getLastUpdated());
+ payment.getPaymentCurrency(), payment.getPayerEmail(), StringUtils.lowerCase(payment.getPayerEmail()),
+ payment.getPayerFirstName(), payment.getPayerLastName(), payment.getPayerCountry(),
+ payment.getProcessingStatus().name(), payment.getCreated(), payment.getLastUpdated());
LOG.info("Created " + payment);
}
@@ -82,33 +84,37 @@ public void createPayment(Payment payment) {
*/
public void updatePayment(Payment payment) {
String sql = "update payment set transaction_type=?, item=?, payment_type=?, payment_status=?, " +
- "payment_amount=?, payment_currency=?, payer_email=?, payer_first_name=?, payer_last_name=?, " +
+ "payment_amount=?, payment_currency=?, payer_email=?, payer_email_lower=?, payer_first_name=?, payer_last_name=?, " +
"payer_country=?, processing_status=?, created=?, last_updated=? where id=?";
update(sql, payment.getTransactionType(), payment.getItem(), payment.getPaymentType(), payment.getPaymentStatus(),
- payment.getPaymentAmount(), payment.getPaymentCurrency(), payment.getPayerEmail(), payment.getPayerFirstName(),
- payment.getPayerLastName(), payment.getPayerCountry(), payment.getProcessingStatus().name(), payment.getCreated(),
- payment.getLastUpdated(), payment.getId());
+ payment.getPaymentAmount(), payment.getPaymentCurrency(), payment.getPayerEmail(), StringUtils.lowerCase(payment.getPayerEmail()),
+ payment.getPayerFirstName(), payment.getPayerLastName(), payment.getPayerCountry(), payment.getProcessingStatus().name(),
+ payment.getCreated(), payment.getLastUpdated(), payment.getId());
LOG.info("Updated " + payment);
}
public boolean isBlacklisted(String email) {
String sql = "select 1 from blacklist where email=?";
- return queryOne(sql, listRowMapper, email) != null;
+ return queryOne(sql, listRowMapper, StringUtils.lowerCase(email)) != null;
}
public boolean isWhitelisted(String email) {
String sql = "select 1 from whitelist where email=?";
- return queryOne(sql, listRowMapper, email) != null;
+ return queryOne(sql, listRowMapper, StringUtils.lowerCase(email)) != null;
+ }
+
+ public void whitelist(String email) {
+ update("insert into whitelist(email) values (?)", StringUtils.lowerCase(email));
}
private static class PaymentRowMapper implements ParameterizedRowMapper<Payment> {
public Payment mapRow(ResultSet rs, int rowNum) throws SQLException {
return new Payment(rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5),
- rs.getString(6), rs.getInt(7), rs.getString(8), rs.getString(9), rs.getString(10),
- rs.getString(11), rs.getString(12), Payment.ProcessingStatus.valueOf(rs.getString(13)),
- rs.getTimestamp(14), rs.getTimestamp(15));
+ rs.getString(6), rs.getInt(7), rs.getString(8), rs.getString(9), rs.getString(11),
+ rs.getString(12), rs.getString(13), Payment.ProcessingStatus.valueOf(rs.getString(14)),
+ rs.getTimestamp(15), rs.getTimestamp(16));
}
}
}
View
9 subsonic-backend/src/main/java/net/sourceforge/subsonic/backend/dao/schema/Schema20.java
@@ -58,6 +58,15 @@ public void execute(JdbcTemplate template) {
LOG.info("Database table 'payment' was created successfully.");
}
+ if (!columnExists(template, "payer_email_lower", "payment")) {
+ LOG.info("Database column 'payment.payer_email_lower' not found. Creating it.");
+ template.execute("alter table payment " +
+ "add payer_email_lower varchar");
+ template.execute("update payment set payer_email_lower=lcase(payer_email)");
+ template.execute("create index idx_payment_payer_email_lower on payment(payer_email_lower)");
+ LOG.info("Database column 'payment.payer_email_lower' was added successfully.");
+ }
+
if (!tableExists(template, "whitelist")) {
LOG.info("Database table 'whitelist' not found. Creating it.");
template.execute("create cached table whitelist (" +
View
12 subsonic-backend/src/main/java/net/sourceforge/subsonic/backend/service/EmailSession.java
@@ -20,10 +20,12 @@
import net.sourceforge.subsonic.backend.Util;
+import javax.mail.Folder;
import javax.mail.Session;
import javax.mail.Message;
import javax.mail.Address;
import javax.mail.MessagingException;
+import javax.mail.Store;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
@@ -37,6 +39,8 @@
public class EmailSession {
private static final String SMTP_MAIL_SERVER = "smtp.gmail.com";
+ private static final String POP_MAIL_SERVER = "pop.gmail.com";
+ private static final String IMAP_MAIL_SERVER = "imap.gmail.com";
private static final String USER = "subsonic@activeobjects.no";
private Session session;
@@ -82,6 +86,14 @@ public void sendMessage(String from, List<String> to, List<String> cc, List<Stri
}
}
+ public Folder getFolder(String name) throws Exception {
+ Store store = session.getStore("imaps");
+ store.connect(IMAP_MAIL_SERVER, USER, password);
+ Folder folder = store.getFolder(name);
+ folder.open(Folder.READ_ONLY);
+ return folder;
+ }
+
private Address[] convertAddress(List<String> addresses) throws AddressException {
if (addresses == null) {
return null;
View
50 ...ic-backend/src/main/java/net/sourceforge/subsonic/backend/service/WhitelistGenerator.java
@@ -0,0 +1,50 @@
+package net.sourceforge.subsonic.backend.service;
+
+import net.sourceforge.subsonic.backend.dao.PaymentDao;
+import org.apache.log4j.Logger;
+
+import javax.mail.Folder;
+import javax.mail.Message;
+import java.util.Date;
+
+/**
+ * Creates a license whitelist.
+ *
+ * @author Sindre Mehus
+ * @version $Id$
+ */
+public class WhitelistGenerator {
+
+ private static final Logger LOG = Logger.getLogger(WhitelistGenerator.class);
+
+ private PaymentDao paymentDao;
+
+ public void generate(Date newerThan) throws Exception {
+ LOG.info("Starting whitelist update for emails newer than " + newerThan);
+
+ EmailSession session = new EmailSession();
+ Folder folder = session.getFolder("[Gmail]/Sent Mail");
+ int n = folder.getMessageCount();
+
+ for (int i = n; i >= 0; i--) {
+ Message message = folder.getMessage(i);
+ Date date = message.getSentDate();
+ String recipient = message.getRecipients(Message.RecipientType.TO)[0].toString();
+ if (date.before(newerThan)) {
+ break;
+ }
+ LOG.info(date + " " + recipient);
+
+ if (paymentDao.getPaymentByEmail(recipient) == null && !paymentDao.isWhitelisted(recipient)) {
+ paymentDao.whitelist(recipient);
+ LOG.info("WHITELISTED " + recipient);
+ }
+ }
+ folder.close(false);
+ LOG.info("Completed whitelist update.");
+ }
+
+ public void setPaymentDao(PaymentDao paymentDao) {
+ this.paymentDao = paymentDao;
+ }
+}
View
4 subsonic-backend/src/main/webapp/WEB-INF/applicationContext-backend.xml
@@ -18,4 +18,8 @@
<property name="paymentDao" ref="paymentDao"/>
</bean>
+ <bean id="whitelistGenerator" class="net.sourceforge.subsonic.backend.service.WhitelistGenerator">
+ <property name="paymentDao" ref="paymentDao"/>
+ </bean>
+
</beans>
View
2  subsonic-backend/src/main/webapp/WEB-INF/subsonic-backend-servlet.xml
@@ -7,6 +7,7 @@
<bean id="multiController" class="net.sourceforge.subsonic.backend.controller.MultiController">
<property name="daoHelper" ref="daoHelper"/>
<property name="paymentDao" ref="paymentDao"/>
+ <property name="whitelistGenerator" ref="whitelistGenerator"/>
</bean>
<bean id="redirectionController" class="net.sourceforge.subsonic.backend.controller.RedirectionController">
@@ -29,6 +30,7 @@
<prop key="/backend/db.view">multiController</prop>
<prop key="/backend/sendMail.view">multiController</prop>
<prop key="/backend/validateLicense.view">multiController</prop>
+ <prop key="/backend/whitelist.view">multiController</prop>
<prop key="/backend/ipn.view">ipnController</prop>
<prop key="/backend/redirect/register.view">redirectionManagementController</prop>
<prop key="/backend/redirect/unregister.view">redirectionManagementController</prop>
View
3  subsonic-main/TODO.TXT
@@ -23,11 +23,12 @@ Updated German translation, courtesy of deejay.
REST: include files in root folders.
Menu broken in "Settings > Shared media" when logged in as admin.
Stronger license check. Verify asynch with backend.
+Share on Google+
TODO IN 4.6
-----------------
-Share on Google+
+Show license details in About.
Support custom domain name in sharing (Settings > Advanced)
Fix permission issue on Win7.
Rename "offsetSeconds" to "timeOffset" in StreamController, and update API doc.
View
1  subsonic-main/src/main/java/net/sourceforge/subsonic/controller/DonateController.java
@@ -44,6 +44,7 @@ protected Object formBackingObject(HttpServletRequest request) throws Exception
command.setEmailAddress(settingsService.getLicenseEmail());
command.setLicenseDate(settingsService.getLicenseDate());
command.setLicenseValid(settingsService.isLicenseValid());
+ command.setLicense(settingsService.getLicenseCode());
command.setBrand(settingsService.getBrand());
return command;
Please sign in to comment.
Something went wrong with that request. Please try again.