From 88f3693c53e70ad064ccc1466ddf27cae98db49a Mon Sep 17 00:00:00 2001
From: Ugnich Anton
Date: Sat, 8 Mar 2014 16:26:50 +0700
Subject: iPod contest
---
src/java/com/juick/http/www/PageTemplates.java | 35 +++++++++++++++++++++++++-
src/java/com/juick/http/www/SignUp.java | 30 ++++++++++++++++++++++
src/java/com/juick/http/www/User.java | 29 +++++++++++++++++++++
src/java/com/juick/http/www/UserThread.java | 4 +++
4 files changed, 97 insertions(+), 1 deletion(-)
(limited to 'src/java/com/juick')
diff --git a/src/java/com/juick/http/www/PageTemplates.java b/src/java/com/juick/http/www/PageTemplates.java
index c76ded41..f9cce99e 100644
--- a/src/java/com/juick/http/www/PageTemplates.java
+++ b/src/java/com/juick/http/www/PageTemplates.java
@@ -52,7 +52,7 @@ public class PageTemplates {
out.println("");
out.print("");
out.print("
");
- out.print("Ссылка на сообщение:");
diff --git a/src/java/com/juick/http/www/SignUp.java b/src/java/com/juick/http/www/SignUp.java
index 2fbe2e2d..cf6679ce 100644
--- a/src/java/com/juick/http/www/SignUp.java
+++ b/src/java/com/juick/http/www/SignUp.java
@@ -172,6 +172,19 @@ public class SignUp {
return;
}
+ int ref = 0;
+ String sRef = Utils.getCookie(request, "ref");
+ if (sRef != null) {
+ try {
+ ref = Integer.parseInt(sRef);
+ } catch (Exception e) {
+ }
+ }
+
+ if (ref > 0) {
+ setUserRef(sql, uid, ref);
+ }
+
visitor = null;
}
@@ -185,6 +198,23 @@ public class SignUp {
response.sendRedirect("/");
}
+ private boolean setUserRef(Connection sql, int uid, int ref) {
+ boolean ret = false;
+ PreparedStatement stmt = null;
+ try {
+ stmt = sql.prepareStatement("INSERT INTO users_refs(user_id,ref) VALUES (?,?)");
+ stmt.setInt(1, uid);
+ stmt.setInt(2, ref);
+ stmt.executeUpdate();
+ ret = true;
+ } catch (SQLException e) {
+ System.err.println(e);
+ } finally {
+ Utils.finishSQL(null, stmt);
+ }
+ return ret;
+ }
+
private String getFacebookNameByHash(Connection sql, String hash) {
String ret = null;
diff --git a/src/java/com/juick/http/www/User.java b/src/java/com/juick/http/www/User.java
index 90a558cc..b517af8e 100644
--- a/src/java/com/juick/http/www/User.java
+++ b/src/java/com/juick/http/www/User.java
@@ -31,6 +31,7 @@ import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import javax.servlet.ServletException;
+import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@@ -105,6 +106,10 @@ public class User {
return;
}
+ if (visitor == null) {
+ pageUserRefCookie(request, response, user.UID);
+ }
+
response.setContentType("text/html; charset=UTF-8");
PrintWriter out = response.getWriter();
try {
@@ -151,6 +156,10 @@ public class User {
protected void doGetTags(Connection sql, HttpServletRequest request, HttpServletResponse response, com.juick.User user) throws ServletException, IOException {
com.juick.User visitor = Utils.getVisitorUser(sql, request, response);
+ if (visitor == null) {
+ pageUserRefCookie(request, response, user.UID);
+ }
+
response.setContentType("text/html; charset=UTF-8");
PrintWriter out = response.getWriter();
try {
@@ -180,6 +189,10 @@ public class User {
protected void doGetFriends(Connection sql, HttpServletRequest request, HttpServletResponse response, com.juick.User user) throws ServletException, IOException {
com.juick.User visitor = Utils.getVisitorUser(sql, request, response);
+ if (visitor == null) {
+ pageUserRefCookie(request, response, user.UID);
+ }
+
response.setContentType("text/html; charset=UTF-8");
PrintWriter out = response.getWriter();
try {
@@ -232,6 +245,10 @@ public class User {
protected void doGetReaders(Connection sql, HttpServletRequest request, HttpServletResponse response, com.juick.User user) throws ServletException, IOException {
com.juick.User visitor = Utils.getVisitorUser(sql, request, response);
+ if (visitor == null) {
+ pageUserRefCookie(request, response, user.UID);
+ }
+
response.setContentType("text/html; charset=UTF-8");
PrintWriter out = response.getWriter();
try {
@@ -281,6 +298,18 @@ public class User {
}
}
+ public static void pageUserRefCookie(HttpServletRequest request, HttpServletResponse response, int uid) {
+ String hReferer = request.getHeader("Referer");
+ String ref = Utils.getCookie(request, "ref");
+
+ if (ref == null && (hReferer == null || !(hReferer.startsWith("http://juick.com/") || hReferer.startsWith("https://juick.com/")))) {
+ Cookie c = new Cookie("ref", Integer.toString(uid));
+ c.setMaxAge(7 * 24 * 60 * 60);
+ c.setPath("/");
+ response.addCookie(c);
+ }
+ }
+
public static void pageUserColumn(PrintWriter out, Connection sql, com.juick.User user, com.juick.User visitor) {
out.println("