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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
|
/*
* 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.www;
import com.juick.Message;
import com.juick.Tag;
import com.juick.server.MessagesQueries;
import com.juick.server.TagQueries;
import com.juick.server.UserQueries;
import org.apache.commons.lang3.StringEscapeUtils;
import com.mitchellbosecke.pebble.PebbleEngine;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.util.StringUtils;
import ru.sape.Sape;
import javax.servlet.http.HttpServletRequest;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
/**
*
* @author Ugnich Anton
*/
public class PageTemplates {
private static final Logger logger = Logger.getLogger(PageTemplates.class.getName());
public static Sape sape = null;
protected static final SimpleDateFormat sdfSQL = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private static SimpleDateFormat sdfSimple = new SimpleDateFormat("d MMM");
private static SimpleDateFormat sdfFull = new SimpleDateFormat("d MMM yyyy");
private static String tagsHTML = null;
public static void pageHead(PrintWriter out, com.juick.User visitor, String title, String headers) {
out.println("<!DOCTYPE html>");
out.print("<html>");
out.print("<head>");
out.println("<meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\">");
out.print("<link rel=\"stylesheet\" href=\"/style.css?v=2\"/>");
out.print("<script type=\"text/javascript\" src=\"/scripts.js\"></script>");
if (headers != null) {
out.print(headers);
}
out.print("<title>" + title + "</title>");
out.println("<meta name=\"viewport\" content=\"width=device-width,initial-scale=1,user-scalable=no\"/>");
out.println("<link rel=\"icon\" href=\"//i.juick.com/favicon.png\"/>");
out.println("<!--[if lt IE 9 & (!IEMobile 7)]>");
out.println("<script src=\"//cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv.min.js\"></script>");
out.println("<![endif]-->");
out.println("</head>");
out.flush();
if (visitor.getUID() > 0) {
out.println("<body id=\"body\" data-hash=\"" + visitor.getAuthHash() + "\">");
} else {
out.println("<body id=\"body\">");
}
}
public static void pageNavigation(PrintWriter out, com.juick.User visitor, String search) {
out.println("<header>");
out.println(" <div id=\"logo\"><a href=\"/\">Juick</a></div>");
out.print(" <nav id=\"global\"><ul>");
out.print("<li><a href=\"/\">Популярные</a></li>");
out.print("<li><a href=\"/?show=all\" rel=\"nofollow\">Все сообщения</a></li>");
out.print("<li><a href=\"/?show=photos\" rel=\"nofollow\">Фотографии</a></li>");
out.println("</ul></nav>");
out.print(" <div id=\"search\"><form action=\"/\"><input type=\"text\" name=\"search\" class=\"text\" placeholder=\"Поиск\"");
if (search != null) {
out.print(" value=\"" + StringEscapeUtils.escapeHtml4(search) + "\"");
}
out.println("/></form></div>");
out.println(" <section id=\"headdiv\">");
if (visitor.getUID() > 0) {
out.print(" <nav id=\"user\"><ul>");
out.print("<li><a href=\"/?show=my\">Моя лента</a></li>");
out.print("<li><a href=\"/pm/inbox\">Приватные</a></li>");
out.print("<li><a href=\"/?show=discuss\">Обсуждения</a></li>");
out.print("<li><a href=\"/?show=recommended\">Рекомендации</a></li>");
out.println("</ul></nav>");
out.print(" <nav id=\"actions\"><ul>");
out.print("<li><a href=\"/#post\">Написать</a></li>");
out.print("<li><a href=\"/" + visitor.getUName() + "\">@" + visitor.getUName() + "</a></li>");
out.print("<li><a href=\"/logout\">Выйти</a></li>");
out.println("</ul></nav>");
} else {
out.println("<p>Чтобы добавлять сообщения и комментарии, <a href=\"#\" class=\"a-login\">представьтесь</a>.</p>");
}
out.println(" </section>");
out.println("</header>");
}
public static void pageHomeColumn(PrintWriter out, JdbcTemplate sql, com.juick.User visitor) {
pageHomeColumn(out, sql, visitor, false);
}
public static void pageHomeColumn(PrintWriter out, JdbcTemplate sql, com.juick.User visitor, boolean showAdv) {
if (tagsHTML == null) {
tagsHTML = PageTemplates.formatPopularTags(sql, 80);
}
out.println("<aside id=\"column\">");
out.print(" <p class=\"tags\">" + tagsHTML);
if (showAdv) {
out.print(" <a href=\"http://ru.wix.com/\">конструктор сайтов</a>");
}
out.println("</p>");
// if (visitor != null) {
// printContestRating(out, sql);
// }
out.println("</aside>");
}
public static String formatPopularTags(JdbcTemplate sql, int cnt) {
List<String> popularTags = TagQueries.getPopularTags(sql).stream()
.map(t -> "<a href=\"/tag/" + URLEncoder.encode(t) + "\">" + StringEscapeUtils.escapeHtml4(t) + "</a>").collect(Collectors.toList());
return StringUtils.collectionToDelimitedString(popularTags, " ");
}
public static void pageFooter(HttpServletRequest request, PrintWriter out, com.juick.User visitor, boolean sapeon) {
out.println("<div id=\"footer\">");
out.println(" <div id=\"footer-right\"><a href=\"/settings\" rel=\"nofollow\">Настройки</a> · <a href=\"/help/ru/contacts\" rel=\"nofollow\">Контакты</a> · <a href=\"/help/\" rel=\"nofollow\">Справка</a> · <a href=\"/help/ru/adv\" rel=\"nofollow\">Реклама</a></div>");
out.print(" <div id=\"footer-social\">");
out.print("<a href=\"https://twitter.com/Juick\" rel=\"nofollow\" class=\"ico32-twi\">Twitter</a>");
out.print("<a href=\"https://vk.com/juick\" rel=\"nofollow\" class=\"ico32-vk\">ВКонтакте</a>");
out.print("<a href=\"https://www.facebook.com/JuickCom\" rel=\"nofollow\" class=\"ico32-fb\">Facebook</a>");
out.println("</div>");
out.print(" <div id=\"footer-left\">juick.com © 2008-2016");
String queryString = request.getQueryString();
String requestURI = request.getRequestURI();
if (sapeon && sape != null && (visitor.getUID() == 0 || visitor.getUID() == 1) && queryString == null) {
String links = sape.getPageLinks(requestURI, request.getCookies()).render();
if (links != null && !links.isEmpty()) {
out.print("<br/>Спонсоры: " + links);
}
}
out.println("</div>");
out.println("</div>");
out.println("<script>");
out.println("(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){");
out.println("(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),");
out.println("m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)");
out.println("})(window,document,'script','//www.google-analytics.com/analytics.js','ga');");
out.println("ga('create','UA-385578-4','juick.com');");
out.println("ga('require','displayfeatures');");
out.println("ga('send','pageview');");
if (sapeon) {
out.println("var _acic={dataProvider:10};");
out.println("(function(){");
out.println("var e=document.createElement('script');e.type='text/javascript';e.async=true;e.src='//www2.aci'+'nt.net/aci.js';");
out.println("var t=document.getElementsByTagName('script')[0];t.parentNode.insertBefore(e,t);");
out.println("})();");
}
out.println("</script>");
}
public static void pageEnd(PrintWriter out) {
out.println("</body></html>");
}
public static String formatTags(List<Tag> tags) {
String ret = "";
for (Tag tag : tags) {
String tagName = StringEscapeUtils.escapeHtml4(tag.getName());
try {
ret += "<a href=\"/tag/" + URLEncoder.encode(tag.getName(), "utf-8") + "\"";
if (tag.UsageCnt < 2) {
ret += " rel=\"nofollow\"";
}
ret += ">" + tagName + "</a>";
} catch (UnsupportedEncodingException e) {
}
}
return ret;
}
public static String formatDate(int minutes, Date fulldate) {
if (minutes < 1) {
return "сейчас";
} else if (minutes < 60) {
String unit;
int ld = minutes % 10;
if ((minutes < 10 || minutes > 20) && ld == 1) {
unit = "минуту";
} else if ((minutes < 10 || minutes > 20) && ld > 1 && ld < 5) {
unit = "минуты";
} else {
unit = "минут";
}
return minutes + " " + unit + " назад";
} else if (minutes < 1440) {
int hours = (minutes / 60);
String unit;
int ld = hours % 10;
if ((hours < 10 || hours > 20) && ld == 1) {
unit = "час";
} else if ((hours < 10 || hours > 20) && ld > 1 && ld < 5) {
unit = "часа";
} else {
unit = "часов";
}
return hours + " " + unit + " назад";
} else if (minutes < 20160) {
int days = (minutes / 1440);
String unit;
int ld = days % 10;
if ((days < 10 || days > 20) && ld == 1) {
unit = "день";
} else if ((days < 10 || days > 20) && ld > 1 && ld < 5) {
unit = "дня";
} else {
unit = "дней";
}
return days + " " + unit + " назад";
} else {
String ret = sdfFull.format(fulldate);
synchronized (sdfSQL) {
try {
Calendar c = Calendar.getInstance();
int curyear = c.get(Calendar.YEAR);
c.setTime(fulldate);
if (c.get(Calendar.YEAR) == curyear) {
ret = sdfSimple.format(fulldate);
} else {
ret = sdfFull.format(fulldate);
}
} catch (Exception e) {
logger.log(Level.SEVERE, "PARSE EXCEPTION: " + fulldate);
}
}
return ret;
}
}
public static String formatJSLocalTime(Date ts) {
return "<script type=\"text/javascript\">"
+ "var d=new Date(" + ts.getTime() + ");"
+ "document.write((d.getDate()<10?'0':'')+d.getDate()+'.'+(d.getMonth()<9?'0':'')+(d.getMonth()+1)+'.'+d.getFullYear()+' '+(d.getHours()<10?'0':'')+d.getHours()+':'+(d.getMinutes()<10?'0':'')+d.getMinutes());"
+ "</script>";
}
public static String formatReplies(int replies) {
int ld = replies % 10;
int lh = replies % 100;
if ((lh < 10 || lh > 20) && ld == 1) {
return replies + " ответ";
} else if ((lh < 10 || lh > 20) && ld > 1 && ld < 5) {
return replies + " ответа";
} else {
return replies + " ответов";
}
}
public static void printMessages(PrintWriter out, JdbcTemplate sql, com.juick.User user, List<Integer> mids, com.juick.User visitor, int YandexID, int ad_mid) {
List<com.juick.Message> msgs = MessagesQueries.getMessages(sql, mids);
for (int i = 0; i < msgs.size(); i++) {
com.juick.Message msg = msgs.get(i);
if (msg.getMID() == ad_mid) {
msgs.remove(i);
msgs.add(0, msg);
break;
}
}
List<Integer> blUIDs = new ArrayList<Integer>(20);
if (visitor != null) {
for (Message msg : msgs) {
blUIDs.add(msg.getUser().getUID());
}
blUIDs = UserQueries.checkBL(sql, visitor.getUID(), blUIDs);
}
for (Message msg : msgs) {
List<Tag> tags = MessagesQueries.getMessageTags(sql, msg.getMID());
String tagsStr = formatTags(tags);
if (msg.ReadOnly) {
tagsStr += "<a>readonly</a>";
}
if (msg.Privacy < 0) {
tagsStr += "<a>friends</a>";
}
if (msg.getMID() == ad_mid) {
tagsStr += "<a>реклама</a>";
}
String txt;
if (!msg.Tags.isEmpty() && msg.Tags.contains("code")) {
txt = Utils.formatMessageCode(msg.getText());
} else {
txt = Utils.formatMessage(msg.getText());
}
out.println("<article data-mid=\"" + msg.getMID() + "\">");
out.println(" <aside><a href=\"/" + msg.getUser().getUName() + "/\"><img src=\"//i.juick.com/a/" + msg.getUser().getUID() + ".png\" alt=\"" + msg.getUser().getUName() + "\"/></a></aside>");
out.println(" <header class=\"u\">");
out.println(" <header class=\"t\"><a href=\"/" + msg.getUser().getUName() + "/" + msg.getMID() + "\"><time datetime=\"" + sdfSQL.format(msg.getDate()) + "Z\" title=\"" + sdfSQL.format(msg.getDate()) + " GMT\">" + formatDate(msg.TimeAgo, msg.getDate()) + "</time></a></header>");
out.println(" @<a href=\"/" + msg.getUser().getUName() + "/\">" + msg.getUser().getUName() + "</a>:");
out.println(" <div class=\"tags\">" + tagsStr + "</div>");
out.println(" </header>");
if (msg.AttachmentType != null) {
String fname = msg.getMID() + "." + msg.AttachmentType;
out.println(" <p class=\"ir\"><a href=\"//i.juick.com/photos-512/" + fname + "\" data-fname=\"" + fname + "\"><img src=\"//i.juick.com/photos-512/" + fname + "\" alt=\"\"/></a></p>");
}
out.println(" <p>" + txt + "</p>");
if (msg.AttachmentType != null) {
out.println(" <div class=\"irbr\"></div>");
}
out.print(" <nav class=\"l\">");
msg.ReadOnly |= blUIDs.contains(msg.getUser().getUID());
if (visitor.getUID() == 0) {
out.print("<a href=\"#\" class=\"a-login\">Мне нравится</a>");
} else {
out.print("<a href=\"/post?body=!+%23" + msg.getMID() + "\" class=\"a-like\">Мне нравится</a>");
}
if (visitor.getUID() == 0 && !msg.ReadOnly) {
out.print("<a href=\"/"+ msg.getMID() + "\" class=\"a-login\">Комментировать</a> ");
} else if (visitor.getUID() > 0 && (!msg.ReadOnly || visitor.getUID() == msg.getUser().getUID())) {
out.print("<a class=\"a-comment\" href=\"/" + msg.getMID() + "\">Комментировать</a> ");
}
if (visitor.getUID() > 0 && msg.Privacy < 0 && msg.getUser().getUID() == visitor.getUID()) {
out.print(" <a href=\"#\" class=\"a-privacy\">Открыть доступ</a>");
}
if (visitor.getUID() > 0 && visitor.getUID() == 3694) {
out.print(" <a href=\"#\" class=\"a-popular-plus\">+</a>");
out.print(" <a href=\"#\" class=\"a-popular-minus\">-</a>");
out.print(" <a href=\"#\" class=\"a-popular-delete\">x</a>");
}
out.println("</nav>");
out.print(" <nav class=\"s\">");
if (msg.Likes > 0) {
out.print("<a href=\"/" + msg.getUser().getUName() + "/" + msg.getMID() + "\" class=\"likes\"><i data-icon=\"ei-heart\" data-size=\"s\"></i> " + msg.Likes + "</a>");
}
if (msg.Replies > 0) {
out.print("<a href=\"/" + msg.getUser().getUName() + "/" + msg.getMID() + "\" class=\"replies\"><i data-icon=\"ei-comment\" data-size=\"s\"></i> " + msg.Replies + "</a>");
}
out.println("</nav>");
out.print("</article>");
}
}
}
|