1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
|
/*
* 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 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, response);
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("vk")) {
account = getVKNameByHash(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, "Новый пользователь", "");
PageTemplates.pageNavigation(out, visitor, null);
out.println("<section 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) == '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\"/>");
}
out.println(account + "</h1>");
out.println("<h2 class=\"signup-h2\">Связать с существующим аккаунтом Juick</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=\"Связать с этим аккаунтом\"/>");
} else {
out.println("<p>Имя пользователя: <input type=\"text\" name=\"username\"/></p>");
out.println("<p>Пароль: <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\">Создать новый аккаунт Juick</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>Имя пользователя: <input type=\"text\" name=\"username\" id=\"username\" onblur=\"checkUsername()\"/><br/><i>(От 2-х до 16-и латинских символов и/или цифр, дефис)</i></p>");
out.println("<p>Пароль: <input type=\"password\" name=\"password\"/><br/><i>(от 6-и до 32-х символов)</i></p>");
out.println("<p><input type=\"submit\" value=\" OK \"/></p>");
out.println("</form>");
out.println("</section>");
PageTemplates.pageFooter(request, out, 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, response);
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) == 'v' && setVKUser(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");
if (username == null || password == null || username.length() < 2 || username.length() > 16 || !username.matches("^[a-zA-Z0-9\\-]+$") || password.length() < 6 || password.length() > 32) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
return;
}
// CHECK USERNAME
uid = UserQueries.createUser(sql, username, password);
if (uid <= 0) {
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
return;
}
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;
}
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;
}
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 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;
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 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;
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;
}
}
|