diff options
Diffstat (limited to 'src/java/com/juick/http/www/SignUp.java')
-rw-r--r-- | src/java/com/juick/http/www/SignUp.java | 270 |
1 files changed, 270 insertions, 0 deletions
diff --git a/src/java/com/juick/http/www/SignUp.java b/src/java/com/juick/http/www/SignUp.java new file mode 100644 index 00000000..1afbebbe --- /dev/null +++ b/src/java/com/juick/http/www/SignUp.java @@ -0,0 +1,270 @@ +/* + * 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.io.PrintWriter; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.Locale; +import java.util.ResourceBundle; +import javax.servlet.ServletException; +import javax.servlet.http.Cookie; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +/** + * + * @author Ugnich Anton + */ +public class SignUp { + + protected void doGet(Connection sql, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + com.juick.User visitor = Utils.getVisitorUser(sql, request); + Locale locale = request.getLocale(); + ResourceBundle rb = ResourceBundle.getBundle("SignUp", locale); + + String type = request.getParameter("type"); + String hash = request.getParameter("hash"); + if (type == null || type.isEmpty() || hash == null || hash.isEmpty() || hash.length() > 36 || !type.matches("^[a-zA-Z0-9\\-]+$") || !hash.matches("^[a-zA-Z0-9\\-]+$")) { + response.sendError(HttpServletResponse.SC_BAD_REQUEST); + return; + } + + String account = null; + if (type.equals("fb")) { + account = getFacebookNameByHash(sql, hash); + } else if (type.equals("xmpp")) { + account = getJIDByHash(sql, hash); + } + if (account == null) { + response.sendError(HttpServletResponse.SC_BAD_REQUEST); + return; + } + + response.setContentType("text/html; charset=UTF-8"); + PrintWriter out = response.getWriter(); + try { + PageTemplates.pageHead(out, rb.getString("Unknown account"), ""); + PageTemplates.pageNavigation(out, locale, visitor, null); + out.println("<div id=\"topwrapper\">"); + out.println("<div id=\"wrapper\">"); + out.println("<div id=\"content\">"); + + 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) == 'x') { + out.print("<img src=\"//static.juick.com/settings/xmpp.png\" alt=\"XMPP\"/>"); + } + out.println(account + "</h1>"); + + out.println("<h2 class=\"signup-h2\">" + rb.getString("Link with existing") + "</h2>"); + out.println("<form action=\"/signup\" method=\"post\">"); + out.println("<input type=\"hidden\" name=\"action\" value=\"link\"/>"); + out.println("<input type=\"hidden\" name=\"type\" value=\"" + type + "\"/>"); + out.println("<input type=\"hidden\" name=\"hash\" value=\"" + hash + "\"/>"); + if (visitor != null) { + out.println("<input type=\"submit\" value=\"" + rb.getString("Link with this") + "\"/>"); + } else { + out.println("<p>" + rb.getString("Username") + ": <input type=\"text\" name=\"username\"/></p>"); + out.println("<p>" + rb.getString("Password") + ": <input type=\"password\" name=\"password\"/></p>"); + out.println("<p><input type=\"submit\" value=\" OK \"/></p>"); + } + out.println("</form>"); + + out.println("<hr class=\"signup-hr\"/>"); + + out.println("<h2 class=\"signup-h2\">" + rb.getString("Create new") + "</h2>"); + out.println("<form action=\"/signup\" method=\"post\">"); + out.println("<input type=\"hidden\" name=\"action\" value=\"new\"/>"); + out.println("<input type=\"hidden\" name=\"type\" value=\"" + type + "\"/>"); + out.println("<input type=\"hidden\" name=\"hash\" value=\"" + hash + "\"/>"); + out.println("<p>" + rb.getString("Username") + ": <input type=\"text\" name=\"username\" id=\"username\" onblur=\"checkUsername()\"/><br/><i>" + rb.getString("Username restrictions") + "</i></p>"); + out.println("<p>" + rb.getString("Password") + ": <input type=\"password\" name=\"password\"/><br/><i>" + rb.getString("Password restrictions") + "</i></p>"); + out.println("<p>" + rb.getString("Language") + ": <select name=\"lang\">"); + String langs[] = rb.getString("Languages").split(","); + String langcodes[] = rb.getString("Language codes").split(","); + for (int i = 0; i < langs.length; i++) { + out.println(" <option value=\"" + langcodes[i] + "\">" + langs[i] + "</option>"); + } + out.println("</select></p>"); + out.println("<p><input type=\"submit\" value=\" OK \"/></p>"); + out.println("</form>"); + + out.println("</div>"); + out.println("</div>"); + out.println("</div>"); // topwrapper + + PageTemplates.pageFooter(request, out, locale, visitor, false); + PageTemplates.pageEnd(out); + } finally { + out.close(); + } + } + + protected void doPost(Connection sql, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + com.juick.User visitor = Utils.getVisitorUser(sql, request); + int uid = 0; + + String type = request.getParameter("type"); + String hash = request.getParameter("hash"); + if (type == null || type.isEmpty() || hash == null || hash.isEmpty() || hash.length() > 36 || !type.matches("^[a-zA-Z0-9\\-]+$") || !hash.matches("^[a-zA-Z0-9\\-]+$")) { + response.sendError(HttpServletResponse.SC_BAD_REQUEST); + return; + } + + String action = request.getParameter("action"); + if (action.charAt(0) == 'l') { + + if (visitor == null) { + String username = request.getParameter("username"); + String password = request.getParameter("password"); + if (username == null || password == null || username.length() > 32 || password.isEmpty()) { + response.sendError(HttpServletResponse.SC_BAD_REQUEST); + return; + } + uid = com.juick.server.UserQueries.checkPassword(sql, username, password); + } else { + uid = visitor.UID; + } + + if (uid == 0) { + response.sendError(HttpServletResponse.SC_FORBIDDEN); + return; + } + + if (!(type.charAt(0) == 'f' && setFacebookUser(sql, hash, uid)) + && !(type.charAt(0) == 'x' && setJIDUser(sql, hash, uid))) { + response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); + return; + } + + } else { // Create new account + String username = request.getParameter("username"); + String password = request.getParameter("password"); + String lang = request.getParameter("lang"); + if (username == null || password == null || lang == null || username.length() < 2 || username.length() > 16 || !username.matches("^[a-zA-Z0-9\\-]+$") || password.length() < 6 || password.length() > 32 || lang.length() != 2) { + response.sendError(HttpServletResponse.SC_BAD_REQUEST); + return; + } + + uid = UserQueries.createUser(sql, username, password, lang); + if (uid == 0) { + response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); + return; + } + + if (!(type.charAt(0) == 'f' && setFacebookUser(sql, hash, uid)) + && !(type.charAt(0) == 'x' && setJIDUser(sql, hash, uid))) { + response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); + return; + } + + visitor = null; + } + + if (visitor == null) { + hash = com.juick.server.UserQueries.getHashByUID(sql, uid); + Cookie c = new Cookie("hash", hash); + c.setMaxAge(365 * 24 * 60 * 60); + response.addCookie(c); + } + + response.sendRedirect("/"); + } + + private String getFacebookNameByHash(Connection sql, String hash) { + String ret = null; + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT fb_name,fb_link FROM facebook WHERE loginhash=?"); + stmt.setString(1, hash); + rs = stmt.executeQuery(); + if (rs.first()) { + ret = "<a href=\"" + 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 setFacebookUser(Connection sql, String hash, int uid) { + boolean ret = false; + PreparedStatement stmt = null; + try { + stmt = sql.prepareStatement("UPDATE facebook 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; + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT jid FROM jids WHERE loginhash=?"); + stmt.setString(1, hash); + rs = stmt.executeQuery(); + if (rs.first()) { + ret = rs.getString(1); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + + return ret; + } + + private boolean setJIDUser(Connection sql, String hash, int uid) { + boolean ret = false; + PreparedStatement stmt = null; + try { + stmt = sql.prepareStatement("UPDATE jids 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; + } +} |