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
332
333
334
335
336
337
338
339
340
341
342
343
344
|
/*
* Juick
* Copyright (C) 2008-2011, 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.Tag;
import com.juick.server.MessagesQueries;
import com.juick.server.TagQueries;
import com.juick.server.UserQueries;
import org.springframework.jdbc.core.JdbcTemplate;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.List;
/**
*
* @author Ugnich Anton
*/
public class User {
protected void doGetBlog(JdbcTemplate sql, JdbcTemplate sqlSearch, HttpServletRequest request, HttpServletResponse response, com.juick.User user) throws ServletException, IOException {
com.juick.User visitor = Utils.getVisitorUser(sql, request, response);
List<Integer> mids;
String paramShow = request.getParameter("show");
com.juick.Tag paramTag = null;
String paramTagStr = request.getParameter("tag");
if (paramTagStr != null) {
if (paramTagStr.length() < 64) {
paramTag = TagQueries.getTag(sql, paramTagStr, false);
}
if (paramTag == null) {
Errors.doGet404(sql, request, response);
return;
} else if (!paramTag.Name.equals(paramTagStr)) {
String url = "/" + user.getUName() + "/?tag=" + URLEncoder.encode(paramTag.Name, "UTF-8");
Utils.sendPermanentRedirect(response, url);
return;
}
}
int paramBefore = 0;
String paramBeforeStr = request.getParameter("before");
if (paramBeforeStr != null) {
try {
paramBefore = Integer.parseInt(paramBeforeStr);
} catch (NumberFormatException e) {
}
}
String paramSearch = request.getParameter("search");
if (paramSearch != null && paramSearch.length() > 64) {
paramSearch = null;
}
int privacy = 0;
if (visitor != null) {
if (user.getUID() == visitor.getUID() || visitor.getUID() == 1) {
privacy = -3;
} else if (UserQueries.isInWL(sql, user.getUID(), visitor.getUID())) {
privacy = -2;
}
}
String title;
if (paramShow == null) {
if (paramTag != null) {
title = "Блог " + user.getUName() + ": *" + Utils.encodeHTML(paramTag.Name);
mids = MessagesQueries.getUserTag(sql, user.getUID(), paramTag.TID, privacy, paramBefore);
} else if (paramSearch != null) {
title = "Блог " + user.getUName() + ": " + Utils.encodeHTML(paramSearch);
mids = MessagesQueries.getUserSearch(sql, sqlSearch, user.getUID(), Utils.encodeSphinx(paramSearch), privacy, paramBefore);
} else {
title = "Блог " + user.getUName();
mids = MessagesQueries.getUserBlog(sql, user.getUID(), privacy, paramBefore);
}
} else if (paramShow.equals("recomm")) {
title = "Рекомендации " + user.getUName();
mids = MessagesQueries.getUserRecommendations(sql, user.getUID(), paramBefore);
} else if (paramShow.equals("photos")) {
title = "Фотографии " + user.getUName();
mids = MessagesQueries.getUserPhotos(sql, user.getUID(), privacy, paramBefore);
} else {
Errors.doGet404(sql, request, response);
return;
}
if (visitor == null) {
pageUserRefCookie(request, response, user.getUID());
}
response.setContentType("text/html; charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
String head = "<link rel=\"alternate\" type=\"application/rss+xml\" title=\"@" + user.getUName() + "\" href=\"//rss.juick.com/" + user.getUName() + "/blog\"/>";
if (paramTag != null && TagQueries.getTagNoIndex(sql, paramTag.TID)) {
head += "<meta name=\"robots\" content=\"noindex,nofollow\"/>";
} else if (paramBefore > 0 || paramShow != null) {
head += "<meta name=\"robots\" content=\"noindex\"/>";
}
PageTemplates.pageHead(out, title, head);
PageTemplates.pageNavigation(out, visitor, null);
pageUserColumn(out, sql, user, visitor);
if (mids.size() > 0) {
out.println("<section id=\"content\">");
if (paramTag != null) {
out.println("<p class=\"page\"><a href=\"/tag/" + URLEncoder.encode(paramTag.Name, "UTF-8") + "\">← Все записи с тегом <b>" + Utils.encodeHTML(paramTag.Name) + "</b></a></p>");
}
PageTemplates.printMessages(out, sql, user, mids, visitor, visitor == null ? 4 : 5, 0);
if (mids.size() >= 20) {
String nextpage = "?before=" + mids.get(mids.size() - 1);
if (paramShow != null) {
nextpage += "&show=" + paramShow;
}
if (paramTag != null) {
nextpage += "&tag=" + URLEncoder.encode(paramTag.Name, "UTF-8");
}
if (paramSearch != null) {
nextpage += "&search=" + URLEncoder.encode(paramSearch, "UTF-8");
}
out.println("<p class=\"page\"><a href=\"" + nextpage + "\" rel=\"prev\">Читать дальше →</a></p>");
}
out.println("</section>");
}
PageTemplates.pageFooter(request, out, visitor, true);
PageTemplates.pageEnd(out);
}
}
protected void doGetTags(JdbcTemplate 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.getUID());
}
response.setContentType("text/html; charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
String head = "<meta name=\"robots\" content=\"noindex,nofollow\"/>";
PageTemplates.pageHead(out, "Теги " + user.getUName(), head);
PageTemplates.pageNavigation(out, visitor, null);
pageUserColumn(out, sql, user, visitor);
out.println("<section id=\"content\">");
out.println("<p>" + pageUserTags(sql, user, visitor, 0) + "</p>");
out.println("</section>");
PageTemplates.pageFooter(request, out, visitor, false);
PageTemplates.pageEnd(out);
}
}
protected void doGetFriends(JdbcTemplate 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.getUID());
}
response.setContentType("text/html; charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
String head = "<meta name=\"robots\" content=\"noindex\"/>";
PageTemplates.pageHead(out, "Подписки " + user.getUName(), head);
PageTemplates.pageNavigation(out, visitor, null);
pageUserColumn(out, sql, user, visitor);
out.println("<section id=\"content\">");
out.println("<table class=\"users\"><tr>");
List<com.juick.User> friends = UserQueries.getUserFriends(sql, user.getUID());
for (int i = 0; i < friends.size(); i++) {
if (i % 3 == 0 && i > 0) {
out.print("</tr><tr>");
}
out.print("<td><a href=\"/" + friends.get(i).getUName()
+ "/\"><img src=\"//i.juick.com/as/" + friends.get(i).getUID() + ".png\"/>"
+ friends.get(i).getUName() + "</a></td>");
}
out.println("</tr></table>");
out.println("</section>");
PageTemplates.pageFooter(request, out, visitor, false);
PageTemplates.pageEnd(out);
}
}
protected void doGetReaders(JdbcTemplate 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.getUID());
}
response.setContentType("text/html; charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
String head = "<meta name=\"robots\" content=\"noindex\"/>";
PageTemplates.pageHead(out, "Читатели " + user.getUName(), head);
PageTemplates.pageNavigation(out, visitor, null);
pageUserColumn(out, sql, user, visitor);
out.println("<section id=\"content\">");
out.println("<table class=\"users\"><tr>");
List<com.juick.User> readers = UserQueries.getUserReaders(sql, user.getUID());
for (int i = 0; i < readers.size(); i++) {
if (i % 3 == 0 && i > 0) {
out.print("</tr><tr>");
}
out.print("<td><a href=\"/" + readers.get(i).getUName()
+ "/\"><img src=\"//i.juick.com/as/" + readers.get(i).getUID() + ".png\"/>"
+ readers.get(i).getUName() + "</a></td>");
}
out.println("</tr></table>");
out.println("</section>");
PageTemplates.pageFooter(request, out, visitor, false);
PageTemplates.pageEnd(out);
}
}
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, JdbcTemplate sql, com.juick.User user, com.juick.User visitor) {
out.println("<aside id=\"column\">");
out.println(" <div id=\"ctitle\"><a href=\"./\"><img src=\"//i.juick.com/as/" + user.getUID() + ".png\" alt=\"\"/>" + user.getUName() + "</a></div>");
if (visitor != null && visitor.getUID() > 0 && visitor.getUID() != user.getUID()) {
out.println(" <ul id=\"ctoolbar\">");
if (UserQueries.isSubscribed(sql, visitor.getUID(), user.getUID())) {
out.println(" <li><a href=\"/post?body=U+%40" + user.getUName() + "\" title=\"Подписан\"><div style=\"background-position: -48px 0\"></div></a></li>");
} else {
out.println(" <li><a href=\"/post?body=S+%40" + user.getUName() + "\" title=\"Подписаться\"><div style=\"background-position: -16px 0\"></div></a></li>");
}
if (UserQueries.isInBL(sql, visitor.getUID(), user.getUID())) {
out.println(" <li><a href=\"/post?body=BL+%40" + user.getUName() + "\" title=\"Разблокировать\"><div style=\"background-position: -96px 0\"></div></a></li>");
} else {
out.println(" <li><a href=\"/post?body=BL+%40" + user.getUName() + "\" title=\"Заблокировать\"><div style=\"background-position: -80px 0\"></div></a></li>");
}
if (!UserQueries.isInBLAny(sql, user.getUID(), visitor.getUID())) {
out.println(" <li><a href=\"/pm/sent?uname=" + user.getUName() + "\" title=\"Написать приватное сообщение\"><div style=\"background-position: -112px 0\"></div></a></li>");
}
out.println(" </ul>");
} else {
out.println(" <hr/>");
}
out.println(" <ul>");
out.println(" <li><a href=\"./\">Блог</a></li>");
out.println(" <li><a href=\"./?show=recomm\" rel=\"nofollow\">Рекомендации</a></li>");
out.println(" <li><a href=\"./?show=photos\" rel=\"nofollow\">Фотографии</a></li>");
out.println(" </ul>");
out.println(" <hr/>");
out.println(" <form action=\"./\">");
out.println(" <p><input type=\"text\" name=\"search\" class=\"inp\" placeholder=\"Поиск\"/></p>");
out.println(" </form>");
out.println(" <p class=\"tags\">" + pageUserTags(sql, user, visitor, 20) + "<a href=\"./tags\" rel=\"nofollow\">...</a></p>");
out.println(" <hr/>");
out.println(" <div id=\"ustats\"><ul>");
out.println(" <li><a href=\"./friends\">Я читаю: " + UserQueries.getStatsIRead(sql, user.getUID()) + "</a></li>");
out.println(" <li><a href=\"./readers\">Мои подписчики: " + UserQueries.getStatsMyReaders(sql, user.getUID()) + "</a></li>");
out.println(" <li>Сообщений: " + UserQueries.getStatsMessages(sql, user.getUID()) + "</li>");
out.println(" <li>Комментариев: " + UserQueries.getStatsReplies(sql, user.getUID()) + "</li>");
out.println(" </ul>");
List<com.juick.User> iread = UserQueries.getUserReadLeastPopular(sql, user.getUID(), 8);
if (!iread.isEmpty()) {
out.println("<table class=\"iread\"><tr>");
for (int i = 0; i < iread.size(); i++) {
if (i == 4) {
out.println("</tr><tr>");
}
com.juick.User u = iread.get(i);
out.println("<td><a href=\"/" + u.getUName() + "/\"><img src=\"//i.juick.com/a/" + u.getUID() + ".png\" alt=\"" + u.getUName() + "\"/></a></td>");
}
out.println("</tr></table>");
}
out.println(" </div>");
out.println("</aside>");
}
public static String pageUserTags(JdbcTemplate sql, com.juick.User user, com.juick.User visitor, int cnt) {
List<Tag> tags = TagQueries.getUserTagsAll(sql, user.getUID());
int maxUsageCnt = tags.stream().map(t -> t.UsageCnt).max(Integer::max).get();
String ret = "";
int count = Math.min(tags.size(), cnt);
for (int i = 0; i < count; i++) {
String tag = Utils.encodeHTML(tags.get(i).Name);
try {
tag = "<a href=\"./?tag=" + URLEncoder.encode(tags.get(i).Name, "UTF-8") + "\" title=\""
+ tags.get(i).UsageCnt + "\" rel=\"nofollow\">" + tag + "</a>";
} catch (UnsupportedEncodingException e) {
}
if (tags.get(i).UsageCnt > maxUsageCnt / 3 * 2) {
ret += "<big>" + tag + "</big> ";
} else if (tags.get(i).UsageCnt > maxUsageCnt / 3) {
ret += "<small>" + tag + "</small> ";
} else {
ret += tag + " ";
}
}
return ret;
}
}
|