aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/java/com/juick/http/www/Main.java23
-rw-r--r--src/java/com/juick/http/www/SignUp.java44
-rw-r--r--src/java/com/juick/http/www/VKontakteLogin.java154
3 files changed, 211 insertions, 10 deletions
diff --git a/src/java/com/juick/http/www/Main.java b/src/java/com/juick/http/www/Main.java
index baaa1cae..c84a2f8c 100644
--- a/src/java/com/juick/http/www/Main.java
+++ b/src/java/com/juick/http/www/Main.java
@@ -37,7 +37,7 @@ import ru.sape.Sape;
*/
@WebServlet(name = "Main", urlPatterns = {"/"})
public class Main extends HttpServlet implements XmppListener {
-
+
Connection sql;
Connection sqlSearch;
XmppConnection xmpp;
@@ -48,16 +48,17 @@ public class Main extends HttpServlet implements XmppListener {
UserThread pagesUserThread = new UserThread();
NewMessage pagesNewMessage = new NewMessage();
FacebookLogin loginFacebook = new FacebookLogin();
+ VKontakteLogin loginVK = new VKontakteLogin();
SignUp signup = new SignUp();
Settings settings = new Settings();
-
+
@Override
public void init() throws ServletException {
super.init();
try {
Properties conf = new Properties();
conf.load(new FileInputStream("/etc/juick/www.conf"));
-
+
Class.forName("com.mysql.jdbc.Driver");
sql = DriverManager.getConnection("jdbc:mysql://localhost/juick?autoReconnect=true&user=" + conf.getProperty("mysql_username", "") + "&password=" + conf.getProperty("mysql_password", ""));
sqlSearch = DriverManager.getConnection("jdbc:mysql://127.0.0.1:9306/juick?autoReconnect=true&characterEncoding=utf8&maxAllowedPacket=512000&relaxAutoCommit=true&user=root&password=");
@@ -66,15 +67,15 @@ public class Main extends HttpServlet implements XmppListener {
xmpp.addListener((XmppListener) this);
xmpp.start();
*/
-
+
blogs = new Blogs(sql);
-
+
PageTemplates.sape = new Sape(conf.getProperty("sape_user"), "juick.com", 2000, 3600);
} catch (Exception e) {
log(null, e);
}
}
-
+
@Override
public void destroy() {
super.destroy();
@@ -95,17 +96,17 @@ public class Main extends HttpServlet implements XmppListener {
}
}
}
-
+
@Override
public void onAuth(String resource) {
log("XMPP AUTH: " + resource);
}
-
+
@Override
public void onAuthFailed(String message) {
log("XMPP AUTH FAILED: " + message);
}
-
+
@Override
public void onConnectionFailed(String message) {
log("XMPP CONNECTION FAILED: " + message);
@@ -124,7 +125,7 @@ public class Main extends HttpServlet implements XmppListener {
request.setCharacterEncoding("UTF-8");
}
String uri = request.getRequestURI();
-
+
if (uri.equals("/")) {
blogs.doGet(sql, sqlSearch, request, response);
} else if (uri.equals("/post")) {
@@ -146,6 +147,8 @@ public class Main extends HttpServlet implements XmppListener {
settings.doGet(sql, request, response);
} else if (uri.equals("/_fblogin")) {
loginFacebook.doGet(sql, request, response);
+ } else if (uri.equals("/_vklogin")) {
+ loginVK.doGet(sql, request, response);
} else if (uri.equals("/signup")) {
signup.doGet(sql, request, response);
} else if (uri.equals("/help") || uri.equals("/help/")) {
diff --git a/src/java/com/juick/http/www/SignUp.java b/src/java/com/juick/http/www/SignUp.java
index 1afbebbe..46049451 100644
--- a/src/java/com/juick/http/www/SignUp.java
+++ b/src/java/com/juick/http/www/SignUp.java
@@ -52,6 +52,8 @@ public class SignUp {
String account = null;
if (type.equals("fb")) {
account = getFacebookNameByHash(sql, hash);
+ } else if (type.equals("vk")) {
+ account = getVKNameByHash(sql, hash);
} else if (type.equals("xmpp")) {
account = getJIDByHash(sql, hash);
}
@@ -72,6 +74,8 @@ public class SignUp {
out.print("<h1 class=\"signup-h1\">");
if (type.charAt(0) == 'f') {
out.print("<img src=\"//static.juick.com/settings/facebook.png\" alt=\"Facebook\"/>");
+ } else if (type.charAt(0) == 'v') {
+ out.print("<img src=\"//static.juick.com/settings/vk.png\" alt=\"VKontakte\"/>");
} else if (type.charAt(0) == 'x') {
out.print("<img src=\"//static.juick.com/settings/xmpp.png\" alt=\"XMPP\"/>");
}
@@ -153,6 +157,7 @@ public class SignUp {
}
if (!(type.charAt(0) == 'f' && setFacebookUser(sql, hash, uid))
+ && !(type.charAt(0) == 'v' && setVKUser(sql, hash, uid))
&& !(type.charAt(0) == 'x' && setJIDUser(sql, hash, uid))) {
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
return;
@@ -174,6 +179,7 @@ public class SignUp {
}
if (!(type.charAt(0) == 'f' && setFacebookUser(sql, hash, uid))
+ && !(type.charAt(0) == 'v' && setVKUser(sql, hash, uid))
&& !(type.charAt(0) == 'x' && setJIDUser(sql, hash, uid))) {
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
return;
@@ -230,6 +236,44 @@ public class SignUp {
return ret;
}
+ private String getVKNameByHash(Connection sql, String hash) {
+ String ret = null;
+
+ PreparedStatement stmt = null;
+ ResultSet rs = null;
+ try {
+ stmt = sql.prepareStatement("SELECT vk_name,vk_link FROM vk WHERE loginhash=?");
+ stmt.setString(1, hash);
+ rs = stmt.executeQuery();
+ if (rs.first()) {
+ ret = "<a href=\"http://vk.com/" + rs.getString(2) + "\" rel=\"nofollow\">" + rs.getString(1) + "</a>";
+ }
+ } catch (SQLException e) {
+ System.err.println(e);
+ } finally {
+ Utils.finishSQL(rs, stmt);
+ }
+
+ return ret;
+ }
+
+ private boolean setVKUser(Connection sql, String hash, int uid) {
+ boolean ret = false;
+ PreparedStatement stmt = null;
+ try {
+ stmt = sql.prepareStatement("UPDATE vk SET user_id=?,loginhash=NULL WHERE loginhash=?");
+ stmt.setInt(1, uid);
+ stmt.setString(2, hash);
+ stmt.executeUpdate();
+ ret = true;
+ } catch (SQLException e) {
+ System.err.println(e);
+ } finally {
+ Utils.finishSQL(null, stmt);
+ }
+ return ret;
+ }
+
private String getJIDByHash(Connection sql, String hash) {
String ret = null;
diff --git a/src/java/com/juick/http/www/VKontakteLogin.java b/src/java/com/juick/http/www/VKontakteLogin.java
new file mode 100644
index 00000000..e7252b57
--- /dev/null
+++ b/src/java/com/juick/http/www/VKontakteLogin.java
@@ -0,0 +1,154 @@
+/*
+ * Juick
+ * Copyright (C) 2008-2013, Ugnich Anton
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package com.juick.http.www;
+
+import com.juick.server.UserQueries;
+import java.io.IOException;
+import java.net.URLEncoder;
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.UUID;
+import javax.servlet.ServletException;
+import javax.servlet.http.Cookie;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import org.json.JSONException;
+import org.json.JSONObject;
+
+/**
+ *
+ * @author Ugnich Anton
+ */
+public class VKontakteLogin {
+
+ private static final String VK_APPID = "3531222";
+ private static final String VK_SECRET = "EhgW6VJrl02oFPO0buiJ";
+ private static final String VK_REDIRECT = "http://juick.com/_vklogin";
+
+ protected void doGet(Connection sql, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
+ String code = request.getParameter("code");
+ if (code == null || code.equals("")) {
+ response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
+ response.setHeader("Location", "https://oauth.vk.com/authorize?client_id=" + VK_APPID + "&redirect_uri=" + URLEncoder.encode(VK_REDIRECT, "utf-8") + "&scope=friends,wall,offline&response_type=code");
+ return;
+ }
+
+
+ String tokenjson = Utils.fetchURL("https://oauth.vk.com/access_token?client_id=" + VK_APPID + "&redirect_uri=" + URLEncoder.encode(VK_REDIRECT, "utf-8") + "&client_secret=" + VK_SECRET + "&code=" + URLEncoder.encode(code, "utf-8"));
+ if (tokenjson == null || tokenjson.isEmpty()) {
+ System.err.println("VK TOKEN EMPTY");
+ response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
+ return;
+ }
+ String token = null;
+ long vkID = 0;
+ try {
+ JSONObject json = new JSONObject(tokenjson);
+ token = json.getString("access_token");
+ vkID = json.getLong("user_id");
+ } catch (JSONException e) {
+ System.err.println("VK TOKEN EXCEPTION: " + e);
+ response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
+ return;
+ }
+ if (token == null || vkID == 0) {
+ System.err.println("VK TOKEN EMPTY: " + tokenjson);
+ response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
+ return;
+ }
+
+
+
+ String graph = Utils.fetchURL("https://api.vk.com/method/users.get?uids=" + vkID + "&fields=screen_name&access_token=" + token);
+ if (graph == null || graph.isEmpty()) {
+ System.err.println("VK GRAPH ERROR");
+ response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
+ return;
+ }
+
+ try {
+ JSONObject json = new JSONObject(graph);
+ String vkName = json.getString("first_name") + " " + json.getString("last_name");
+ String vkLink = json.getString("screen_name");
+
+ if (vkName == null || vkLink == null || vkName.isEmpty() || vkName.length() == 1 || vkLink.isEmpty()) {
+ throw new Exception();
+ }
+
+ int uid = getUIDbyVKID(sql, vkID);
+ if (uid > 0) {
+ Cookie c = new Cookie("hash", UserQueries.getHashByUID(sql, uid));
+ c.setMaxAge(50 * 24 * 60 * 60);
+ response.addCookie(c);
+ response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
+ response.setHeader("Location", "/");
+ } else {
+ String loginhash = UUID.randomUUID().toString();
+ if (!insertDB(sql, vkID, loginhash, token, vkName, vkLink)) {
+ throw new Exception();
+ }
+ response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
+ response.setHeader("Location", "/signup?type=vk&hash=" + loginhash);
+ }
+ } catch (Exception e) {
+ response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
+ return;
+ }
+ }
+
+ private int getUIDbyVKID(Connection sql, long vkID) {
+ int uid = 0;
+ PreparedStatement stmt = null;
+ ResultSet rs = null;
+ try {
+ stmt = sql.prepareStatement("SELECT user_id FROM vk WHERE vk_id=? AND user_id IS NOT NULL");
+ stmt.setLong(1, vkID);
+ rs = stmt.executeQuery();
+ if (rs.first()) {
+ uid = rs.getInt(1);
+ }
+ } catch (SQLException e) {
+ System.err.println(e);
+ } finally {
+ Utils.finishSQL(rs, stmt);
+ }
+ return uid;
+ }
+
+ private boolean insertDB(Connection sql, long vkID, String loginhash, String token, String vkName, String vkLink) {
+ boolean ret = false;
+ PreparedStatement stmt = null;
+ try {
+ stmt = sql.prepareStatement("INSERT INTO vk(vk_id,loginhash,access_token,vk_name,vk_link) VALUES (?,?,?,?,?)");
+ stmt.setLong(1, vkID);
+ stmt.setString(2, loginhash);
+ stmt.setString(3, token);
+ stmt.setString(4, vkName);
+ stmt.setString(5, vkLink);
+ stmt.executeUpdate();
+ ret = true;
+ } catch (SQLException e) {
+ System.err.println(e);
+ } finally {
+ Utils.finishSQL(null, stmt);
+ }
+ return ret;
+ }
+}