aboutsummaryrefslogtreecommitdiff
path: root/src/java/com/juick/http/www/PageTemplates.java
blob: 16a00d24456d74ae4d9ef68d2fb9a43cdaea8ceb (plain) (blame)
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
/*
 * 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.server.MessagesQueries;
import com.juick.server.UserQueries;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.http.HttpServletRequest;
import ru.sape.Sape;

/**
 *
 * @author Ugnich Anton
 */
public class PageTemplates {

    public static Sape sape = null;
    private static 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 SimpleDateFormat sdfJS = new SimpleDateFormat("yyyy,MM,dd,HH,mm,ss");
    private static String tagsHTML = null;

    public static void pageHead(PrintWriter out, String title, String headers) {
        out.println("<!DOCTYPE html>");
        out.print("<html>");
        out.print("<head>");
        out.print("<link rel=\"stylesheet\" href=\"/style.2014012700.css\"/>");
        out.print("<script type=\"text/javascript\" src=\"//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js\" defer=\"defer\"></script>");
        out.print("<script type=\"text/javascript\" src=\"/scripts.2014012700.js\" defer=\"defer\"></script>");
        if (headers != null) {
            out.print(headers);
        }
        out.print("<title>" + title + "</title>");
        out.print("<meta name=\"viewport\" content=\"width=device-width,initial-scale=1,user-scalable=no\"/>");
        out.print("<link rel=\"icon\" href=\"//i.juick.com/favicon.png\"/>");
        out.println("</head>");
        out.flush();
        out.println("<body>");
    }

    public static void pageNavigation(PrintWriter out, Locale loc, com.juick.User user, String search) {
        ResourceBundle rb = ResourceBundle.getBundle("Global", loc);
        out.println("<div id=\"hwrapper\">");
        out.println("<ul id=\"header\">");
        if (user != null) {
            out.println("  <li><a href=\"/?show=my\"><div class=\"head-ico\" id=\"hi-home\"></div><label>" + rb.getString("Home") + "</label></a></li>");
        } else {
            out.println("  <li><a href=\"/\"><div class=\"head-ico\" id=\"hi-logo\"></div></a></li>");
        }

        out.print("  <li id=\"search\"><form action=\"/\"><input type=\"text\" name=\"search\" class=\"text\" placeholder=\"" + rb.getString("Search") + "\"");
        if (search != null) {
            out.print(" value=\"" + Utils.encodeHTML(search) + "\"");
        }
        out.println("/></form></li>");

        if (user != null) {
            out.println("  <li class=\"right\"><a href=\"/logout\"><div class=\"head-ico\" id=\"hi-logout\"></div></a></li>");
            out.println("  <li class=\"right\"><a href=\"/settings\"><div class=\"head-ico\" id=\"hi-settings\"></div></a></li>");
            out.println("  <li class=\"right\"><a href=\"/" + user.UName + "\" class=\"wide\"><img src=\"//i.juick.com/as/" + user.UID + ".png\" alt=\"" + user.UName + "\"/><label>" + user.UName + "</label></a></li>");
            out.println("  <li class=\"right\"><a href=\"/post\" class=\"wide\"><div class=\"head-ico\" id=\"hi-post\"></div><label>" + rb.getString("Post") + "</label></a></li>");
        } else {
            out.println("  <li class=\"right\"><a href=\"/login\" class=\"wide\" rel=\"nofollow\">" + rb.getString("Login") + "</a></li>");
        }
        out.println("</ul>");
        out.println("</div>");
    }
    /*
    public static void pageUserTitle(PrintWriter out, Connection sql, Locale loc, com.juick.User user, com.juick.User visitor) {
    ResourceBundle rb = ResourceBundle.getBundle("User", loc);
    
    // Full name and description
    String fullname = null;
    String description = null;
    PreparedStatement stmt = null;
    ResultSet rs = null;
    try {
    stmt = sql.prepareStatement("SELECT fullname,descr FROM usersinfo WHERE user_id=?");
    stmt.setInt(1, user.UID);
    rs = stmt.executeQuery();
    if (rs.first()) {
    fullname = rs.getString(1) + " (" + user.UName + ")";
    description = rs.getString(2);
    }
    } catch (SQLException e) {
    System.err.println(e);
    } finally {
    Utils.finishSQL(rs, stmt);
    }
    if (fullname == null) {
    fullname = user.UName;
    }
    if (description == null) {
    description = "";
    }
    
    out.println("<div id=\"title\">");
    out.println("  <div id=\"title-av\"><a href=\"/" + user.UName + "/\"><img src=\"//i.juick.com/a/" + user.UID + ".png\" width=\"96\" height=\"96\" alt=\"" + user.UName + "\"/></a></div>");
    out.println("  <div id=\"title-stats\"><ul>");
    out.println("    <li><a href=\"/" + user.UName + "/friends\">" + rb.getString("(Stats) I read") + ": " + UserQueries.getStatsIRead(sql, user.UID) + "</a></li>");
    out.println("    <li><a href=\"/" + user.UName + "/readers\">" + rb.getString("(Stats) My readers") + ": " + UserQueries.getStatsMyReaders(sql, user.UID) + "</a></li>");
    out.println("    <li>" + rb.getString("(Stats) Messages") + ": " + UserQueries.getStatsMessages(sql, user.UID) + "</li>");
    out.println("    <li>" + rb.getString("(Stats) Replies") + ": " + UserQueries.getStatsReplies(sql, user.UID) + "</li>");
    out.println("  </ul></div>");
    out.println("  <div id=\"title-username\"><h1>" + fullname + "</h1><p>" + description + "</p></div>");
    out.println("</div>");
    out.println();
    }
     */

    public static void pageYandexAd(PrintWriter out, int stat_id) {
        out.println("<div id=\"yandex_ad\"></div>");
        out.println("<script type=\"text/javascript\">");
        out.println("(function(w, d, n, s, t) {");
        out.println("w[n] = w[n] || [];");
        out.println("w[n].push(function() {");
        out.println("Ya.Direct.insertInto(84715, \"yandex_ad\", {");
        out.println("stat_id: " + stat_id + ",");
        out.println("site_charset: \"utf-8\",");
        out.println("ad_format: \"direct\",");
        out.println("font_size: 1,");
        out.println("type: \"vertical\",");
        out.println("limit: 1,");
        out.println("title_font_size: 2,");
        out.println("site_bg_color: \"EEEEE5\",");
        out.println("title_color: \"006699\",");
        out.println("url_color: \"000000\",");
        out.println("text_color: \"000000\",");
        out.println("hover_color: \"006699\"");
        out.println("});");
        out.println("});");
        out.println("t = d.documentElement.firstChild;");
        out.println("s = d.createElement(\"script\");");
        out.println("s.type = \"text/javascript\";");
        out.println("s.src = \"http://an.yandex.ru/system/context.js\";");
        out.println("s.setAttribute(\"async\", \"true\");");
        out.println("t.insertBefore(s, t.firstChild);");
        out.println("})(window, document, \"yandex_context_callbacks\");");
        out.println("</script>");
    }

    public static void pageHomeColumn(PrintWriter out, Connection sql, com.juick.User visitor, Locale locale) {
        ResourceBundle rb = ResourceBundle.getBundle("Blogs", locale);

        if (tagsHTML == null) {
            tagsHTML = PageTemplates.getPopularTags(sql, 60);
        }

        out.println("<div id=\"column\" class=\"abs\">");
        out.println("  <ul>");
        if (visitor != null) {
            out.println("    <li><a href=\"/?show=my\">" + rb.getString("My feed") + "</a></li>");
            out.println("    <li><a href=\"/pm/inbox\">" + rb.getString("Private") + "</a> · <a href=\"/pm/sent\">" + rb.getString("Private sent") + "</a></li>");
            out.println("    <li><a href=\"/?show=discuss\">" + rb.getString("Discussions") + "</a></li>");
            out.println("    <li><a href=\"/?show=recommended\">" + rb.getString("Recommended") + "</a></li>");
        }
        out.println("    <li class=\"margtop\"><a href=\"/\">" + rb.getString("Popular") + "</a></li>");
        out.println("    <li><a href=\"/?show=all\">" + rb.getString("All messages") + "</a></li>");
        out.println("  </ul>");
        out.println("  <hr/>");
        out.println("  <p>" + tagsHTML + "</p>");
        out.println("</div>");
    }

    public static String getPopularTags(Connection sql, int cnt) {
        String ret = "";
        com.juick.Tag tags[] = new com.juick.Tag[cnt];

        int maxUsageCnt = 0;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        try {
            stmt = sql.prepareStatement("SELECT tags.name AS name,COUNT(DISTINCT messages.user_id) AS cnt FROM (messages INNER JOIN messages_tags ON (messages.ts>TIMESTAMPADD(MONTH,-1,NOW()) AND messages.message_id=messages_tags.message_id)) INNER JOIN tags ON messages_tags.tag_id=tags.tag_id WHERE tags.tag_id NOT IN (SELECT tag_id FROM tags_ignore) GROUP BY tags.tag_id ORDER BY cnt DESC LIMIT ?");
            stmt.setInt(1, cnt);
            rs = stmt.executeQuery();
            rs.beforeFirst();
            cnt = 0;
            while (rs.next()) {
                tags[cnt] = new com.juick.Tag();
                tags[cnt].Name = rs.getString(1);
                tags[cnt].UsageCnt = rs.getInt(2);
                if (tags[cnt].UsageCnt > maxUsageCnt) {
                    maxUsageCnt = tags[cnt].UsageCnt;
                }
                cnt++;
            }
        } catch (SQLException e) {
            System.err.println(e);
        } finally {
            Utils.finishSQL(rs, stmt);
        }

        Arrays.sort(tags, 0, cnt);

        for (int i = 0; i < cnt; i++) {
            String tag = Utils.encodeHTML(tags[i].Name);
            try {
                tag = "<a href=\"/tag/" + URLEncoder.encode(tags[i].Name, "UTF-8") + "\">" + tag + "</a>";
            } catch (UnsupportedEncodingException e) {
            }

            if (tags[i].UsageCnt > maxUsageCnt / 3 * 2) {
                ret += "<big>" + tag + "</big> ";
            } else if (tags[i].UsageCnt > maxUsageCnt / 3) {
                ret += "<small>" + tag + "</small> ";
            } else {
                ret += tag + " ";
            }
        }

        return ret;
    }

    public static void pageDialogTemplate(PrintWriter out, Locale locale) {
        ResourceBundle rb = ResourceBundle.getBundle("Global", locale);
        out.println("<div id=\"dialogb\" onclick=\"$('#dialogb').hide();$('#dialogw').hide()\"></div>");
        out.println("<div id=\"dialogw\">" + rb.getString("(Dialog) Link to message") + ":");
        out.println("<div id=\"dialogl\" onclick=\"$(this).selectText()\"></div>");
        out.println(rb.getString("(Dialog) Message number") + ":");
        out.println("<div id=\"dialogn\" onclick=\"$(this).selectText()\"></div>");
        out.println(rb.getString("(Dialog) Share") + ":");
        out.println("<ul>");
        out.println("  <li><a id=\"sharefb\"></a></li>");
        out.println("  <li><a id=\"sharetw\"></a></li>");
        out.println("  <li><a id=\"sharevk\"></a></li>");
        out.println("  <li><a id=\"sharegp\"></a></li>");
        out.println("</ul>");
        out.println("</div>");
    }

    public static void pageFooter(HttpServletRequest request, PrintWriter out, Locale loc, com.juick.User visitor, boolean sapeon) {
        ResourceBundle rb = ResourceBundle.getBundle("Global", loc);
        out.println("<div id=\"fwrapper\"><div id=\"footer\">");
        out.println("  <div id=\"footer-right\"><a href=\"/help/ru/contacts\" rel=\"nofollow\">" + rb.getString("Contacts") + "</a> &#183; <a href=\"/help/\" rel=\"nofollow\">" + rb.getString("Help") + "</a> &#183; <a href=\"/help/ru/adv\" rel=\"nofollow\">" + rb.getString("Advertising") + "</a></div>");
        out.print("  <div id=\"footer-left\">juick.com &copy; 2008-2013");
        if (sapeon && sape != null && (visitor == null || visitor.UID == 1) && request.getQueryString() == null) {
            String links = sape.getPageLinks(request.getRequestURI(), request.getCookies()).render();
            if (links != null && !links.isEmpty()) {
                out.print("<br/>" + rb.getString("Sponsored by") + ": " + links);
            }
        }
        out.println("</div>");
        out.println("</div></div>");
    }

    public static void pageEnd(PrintWriter out) {
        out.println("</body></html>");
    }

    public static String formatTags(ArrayList<String> tags, com.juick.User user) {
        String ret = "";
        for (int i = 0; i < tags.size(); i++) {
            String tag = tags.get(i);
            tag = tag.replaceAll("<", "&lt;");
            tag = tag.replaceAll(">", "&gt;");
            try {
                ret += " *<a href=\"";
                if (user == null) {
                    ret += "/tag/";
                } else {
                    ret += "/" + user.UName + "/?tag=";
                }
                ret += URLEncoder.encode(tags.get(i), "utf-8") + "\">" + tag + "</a>";
            } catch (UnsupportedEncodingException e) {
            }
        }

        return ret;
    }

    public static String formatDate(int minsago, String fulldate, Locale loc) {
        if (minsago < 1) {
            return "now";
        } else if (minsago < 60) {
            return minsago + " minute" + ((minsago % 10 == 1) ? "" : "s") + " ago";
        } else if (minsago < 1440) {
            int hours = (minsago / 60);
            return hours + " hour" + ((hours % 10 == 1) ? "" : "s") + " ago";
        } else if (minsago < 20160) {
            int days = (minsago / 1440);
            return days + " day" + ((days % 10 == 1) ? "" : "s") + " ago";
        } else {
            try {
                Date pDate = sdfSQL.parse(fulldate);
                Calendar c = Calendar.getInstance();
                int curyear = c.get(Calendar.YEAR);
                c.setTime(pDate);
                if (c.get(Calendar.YEAR) == curyear) {
                    return sdfSimple.format(pDate);
                } else {
                    return sdfFull.format(pDate);
                }
            } catch (Exception e) {
                return fulldate;
            }
        }
    }

    public static String formatJSLocalTime(String ts) {
        try {
            Date date = sdfSQL.parse(ts);
            return "<script type=\"text/javascript\">"
                    + "var d=new Date(" + date.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>";
        } catch (Exception e) {
            System.err.print(e);
        }
        return "";
    }

    public static String formatReplies(int replies, Locale loc) {
        return replies + " repl" + (replies % 10 == 1 ? "y" : "ies");
    }
    private static Pattern regexLinks2 = Pattern.compile("((?<=\\s)|(?<=\\A))([\\[\\{]|&lt;)((?:ht|f)tps?://(?:www\\.)?([^\\/\\s\\\"\\)\\!]+)/?(?:[^\\]\\}](?<!&gt;))*)([\\]\\}]|&gt;)");

    public static String formatMessageCode(String msg) {
        msg = msg.replaceAll("&", "&amp;");
        msg = msg.replaceAll("<", "&lt;");
        msg = msg.replaceAll(">", "&gt;");

        // http://juick.com/last?page=2
        // <a href="http://juick.com/last?page=2" rel="nofollow">http://juick.com/last?page=2</a>
        msg = msg.replaceAll("((?<=\\s)|(?<=\\A))((?:ht|f)tps?://(?:www\\.)?([^\\/\\s\\n\\\"]+)/?[^\\s\\n\\\"]*)", "$1<a href=\"$2\" rel=\"nofollow\">$2</a>");

        // (http://juick.com/last?page=2)
        // (<a href="http://juick.com/last?page=2" rel="nofollow">http://juick.com/last?page=2</a>)
        Matcher m = regexLinks2.matcher(msg);
        StringBuffer sb = new StringBuffer();
        while (m.find()) {
            String url = m.group(3).replace(" ", "%20").replaceAll("\\s+", "");
            m.appendReplacement(sb, "$1$2<a href=\"" + url + "\" rel=\"nofollow\">" + url + "</a>$5");
        }
        m.appendTail(sb);
        msg = sb.toString();

        return "<pre>" + msg + "</pre>";
    }

    public static String formatMessage(String msg) {
        msg = msg.replaceAll("&", "&amp;");
        msg = msg.replaceAll("<", "&lt;");
        msg = msg.replaceAll(">", "&gt;");

        // --
        // &mdash;
        msg = msg.replaceAll("((?<=\\s)|(?<=\\A))\\-\\-?((?=\\s)|(?=\\Z))", "$1&mdash;$2");

        // http://juick.com/last?page=2
        // <a href="http://juick.com/last?page=2" rel="nofollow">juick.com</a>
        msg = msg.replaceAll("((?<=\\s)|(?<=\\A))((?:ht|f)tps?://(?:www\\.)?([^\\/\\s\\n\\\"]+)/?[^\\s\\n\\\"]*)", "$1<a href=\"$2\" rel=\"nofollow\">$3</a>");

        // #12345
        // <a href="http://juick.com/12345">#12345</a>
        msg = msg.replaceAll("((?<=\\s)|(?<=\\A)|(?<=\\p{Punct}))#(\\d+)((?=\\s)|(?=\\Z)|(?=\\))|(?=\\.)|(?=\\,))", "$1<a href=\"http://juick.com/$2\">#$2</a>$3");

        // #12345/65
        // <a href="http://juick.com/12345#65">#12345/65</a>
        msg = msg.replaceAll("((?<=\\s)|(?<=\\A)|(?<=\\p{Punct}))#(\\d+)/(\\d+)((?=\\s)|(?=\\Z)|(?=\\p{Punct}))", "$1<a href=\"http://juick.com/$2#$3\">#$2/$3</a>$4");

        // *bold*
        // <b>bold</b>
        msg = msg.replaceAll("((?<=\\s)|(?<=\\A)|(?<=\\p{Punct}))\\*([^\\*\\n<>]+)\\*((?=\\s)|(?=\\Z)|(?=\\p{Punct}))", "$1<b>$2</b>$3");

        // /italic/
        // <i>italic</i>
        msg = msg.replaceAll("((?<=\\s)|(?<=\\A))/([^\\/\\n<>]+)/((?=\\s)|(?=\\Z)|(?=\\p{Punct}))", "$1<i>$2</i>$3");

        // _underline_
        // <span class="u">underline</span>
        msg = msg.replaceAll("((?<=\\s)|(?<=\\A))_([^\\_\\n<>]+)_((?=\\s)|(?=\\Z)|(?=\\p{Punct}))", "$1<span class=\"u\">$2</span>$3");

        // /12
        // <a href="#12">/12</a>
        msg = msg.replaceAll("((?<=\\s)|(?<=\\A))\\/(\\d+)((?=\\s)|(?=\\Z)|(?=\\p{Punct}))", "$1<a href=\"#$2\">/$2</a>$3");

        // @username@jabber.org
        // <a href="http://juick.com/username@jabber.org/">@username@jabber.org</a>
        msg = msg.replaceAll("((?<=\\s)|(?<=\\A))@([\\w\\-\\.]+@[\\w\\-\\.]+)((?=\\s)|(?=\\Z)|(?=\\p{Punct}))", "$1<a href=\"http://juick.com/$2/\">@$2</a>$3");

        // @username
        // <a href="http://juick.com/username/">@username</a>
        msg = msg.replaceAll("((?<=\\s)|(?<=\\A))@([\\w\\-]{2,16})((?=\\s)|(?=\\Z)|(?=\\p{Punct}))", "$1<a href=\"http://juick.com/$2/\">@$2</a>$3");

        // (http://juick.com/last?page=2)
        // (<a href="http://juick.com/last?page=2" rel="nofollow">juick.com</a>)
        Matcher m = regexLinks2.matcher(msg);
        StringBuffer sb = new StringBuffer();
        while (m.find()) {
            String url = m.group(3).replace(" ", "%20").replaceAll("\\s+", "");
            m.appendReplacement(sb, "$1$2<a href=\"" + url + "\" rel=\"nofollow\">$4</a>$5");
        }
        m.appendTail(sb);
        msg = sb.toString();

        // > citate
        msg = msg.replaceAll("(?:(?<=\\n)|(?<=\\A))&gt; *(.*)?(\\n|(?=\\Z))", "<blockquote>$1</blockquote>");
        msg = msg.replaceAll("</blockquote><blockquote>", "\n");

        msg = msg.replaceAll("\n", "<br/>\n");
        return msg;
    }

    public static void printMessages(PrintWriter out, Connection sql, com.juick.User user, ArrayList<Integer> mids, com.juick.User visitor, Locale locale, int YandexID, int ad_mid) {
        ResourceBundle rb = ResourceBundle.getBundle("Global", locale);

        ArrayList<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.MID == ad_mid) {
                msgs.remove(i);
                msgs.add(0, msg);
                break;
            }
        }

        ArrayList<Integer> blUIDs = new ArrayList<Integer>(20);
        if (visitor != null) {
            for (int i = 0; i < msgs.size(); i++) {
                blUIDs.add(msgs.get(i).User.UID);
            }
            blUIDs = UserQueries.checkBL(sql, visitor.UID, blUIDs);
        }

        for (int i = 0; i < msgs.size(); i++) {

            if (i == 1 && YandexID > 0 && ad_mid == 0) {
                out.println("  <li class=\"ads\"><div id=\"yandex_ad\"></div></li>");
                out.println("<script type=\"text/javascript\">");
                out.println("(function(w, d, n, s, t) {");
                out.println("w[n] = w[n] || [];");
                out.println("w[n].push(function() {");
                out.println("Ya.Direct.insertInto(84715, \"yandex_ad\", {");
                out.println("stat_id: " + YandexID + ",");
                out.println("site_charset: \"utf-8\",");
                out.println("ad_format: \"direct\",");
                out.println("font_size: 1,");
                out.println("type: \"flat\",");
                out.println("limit: 1,");
                out.println("title_font_size: 2,");
                out.println("site_bg_color: \"FFFFFF\",");
                out.println("title_color: \"006699\",");
                out.println("url_color: \"000000\",");
                out.println("text_color: \"000000\",");
                out.println("hover_color: \"006699\"");
                out.println("});");
                out.println("});");
                out.println("t = d.documentElement.firstChild;");
                out.println("s = d.createElement(\"script\");");
                out.println("s.type = \"text/javascript\";");
                out.println("s.src = \"http://an.yandex.ru/system/context.js\";");
                out.println("s.setAttribute(\"async\", \"true\");");
                out.println("t.insertBefore(s, t.firstChild);");
                out.println("})(window, document, \"yandex_context_callbacks\");");
                out.println("</script>");
            }

            com.juick.Message msg = msgs.get(i);

            String tags = msg.Tags.isEmpty() ? "" : formatTags(msg.Tags, msg.User);
            if (msg.ReadOnly) {
                tags += " *readonly";
            }
            switch (msg.Privacy) {
                case 2:
                    tags += " *public";
                    break;
                case -1:
                    tags += " *friends";
                    break;
                case -2:
                    tags += " *friends";
                    break;
                case -3:
                    tags += " *private";
                    break;
            }

            String txt;
            if (!msg.Tags.isEmpty() && msg.Tags.contains("code")) {
                txt = formatMessageCode(msg.Text);
            } else {
                txt = formatMessage(msg.Text);
            }

            if (!tags.isEmpty()) {
                tags = "<span class=\"msg-tags\">" + tags + "</span>";
            }

            out.println("  <li id=\"msg-" + msg.MID + "\" class=\"msg\">");
            out.println("    <div class=\"msg-avatar\"><a href=\"/" + msg.User.UName + "/\"><img src=\"//i.juick.com/a/" + msg.User.UID + ".png\" alt=\"" + msg.User.UName + "\"/></a></div>");
            out.println("    <div class=\"msg-cont\">");
            out.println("    <div class=\"msg-menu\"><a href=\"#\" onclick=\"showMessageLinksDialog(" + msg.MID + "); return false\"></a></div>");
            out.println("    <div class=\"msg-header\"><a href=\"/" + msg.User.UName + "/\">@" + msg.User.UName + "</a>:" + tags + "</div>");
            out.print("    <div class=\"msg-ts\"><a href=\"/" + msg.User.UName + "/" + msg.MID + "\" title=\"" + msg.TimestampString + " GMT\">" + formatDate(msg.TimeAgo, msg.TimestampString, locale) + "</a>");
            if (msg.MID == ad_mid) {
                out.print(" &#183; <a href=\"/help/ru/adv\">" + rb.getString("Sponsored") + "</a>");
            }
            out.println("</div>");

            out.print("    <div class=\"msg-txt\">" + txt + "</div>");

            if (msg.AttachmentType != null) {
                out.println("    <div class=\"msg-media\"><a href=\"//i.juick.com/photos-1024/" + msg.MID + "." + msg.AttachmentType + "\"><img src=\"//i.juick.com/photos-512/" + msg.MID + "." + msg.AttachmentType + "\" alt=\"\"/></a></div>");
            }

            msg.ReadOnly |= blUIDs.contains(msg.User.UID);

            if (visitor != null && (msg.ReadOnly == false || visitor.UID == msg.User.UID)) {
                out.println("    <form action=\"/post\" method=\"POST\" enctype=\"multipart/form-data\"><input type=\"hidden\" name=\"mid\" value=\"" + msg.MID + "\"/>");
                out.println("      <div class=\"msg-comment\"><div class=\"ta-wrapper\"><textarea name=\"body\" rows=\"1\" class=\"reply\" placeholder=\"" + rb.getString("Add a comment") + "\" onkeypress=\"postformListener(this.form,event)\"></textarea></div></div>");
                out.println("    </form>");
            }
            if (msg.Replies > 0) {
                String repliesby = (msg.RepliesBy != null) ? msg.RepliesBy : "...";
                out.println("    <div class=\"msg-comments\"><a href=\"/" + msg.User.UName + "/" + msg.MID + "\">" + formatReplies(msg.Replies, locale) + "</a> " + rb.getString("(replies) by") + " " + repliesby + "</div>");
            }
            out.println("    </div>");
            out.println("  </li>");
        }
    }
}