/* * 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 . */ 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.text.SimpleDateFormat; import java.util.ArrayList; 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"); public static void pageHead(PrintWriter out, String title, String headers) { out.println(""); out.print(""); out.print(""); out.print(""); out.print(""); out.print(""); if (headers != null) { out.print(headers); } out.print("" + title + ""); out.print(""); out.print(""); out.println(""); out.flush(); out.println(""); } public static void pageNavigation(PrintWriter out, Locale loc, com.juick.User user, String search) { ResourceBundle rb = ResourceBundle.getBundle("Global", loc); out.println("
"); out.println(""); out.println("
"); } /* 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("
"); out.println("
\""
"); out.println("
"); out.println("

" + fullname + "

" + description + "

"); out.println("
"); out.println(); } */ public static void pageYandexAd(PrintWriter out, int stat_id) { out.println("
"); out.println(""); } public static void pageDialogTemplate(PrintWriter out, Locale locale) { ResourceBundle rb = ResourceBundle.getBundle("Global", locale); out.println("
"); out.println("
" + rb.getString("(Dialog) Link to message") + ":"); out.println("
"); out.println(rb.getString("(Dialog) Message number") + ":"); out.println("
"); out.println(rb.getString("(Dialog) Share") + ":"); out.println(""); out.println("
"); } public static void pageFooter(HttpServletRequest request, PrintWriter out, Locale loc, com.juick.User visitor, boolean sapeon) { ResourceBundle rb = ResourceBundle.getBundle("Global", loc); out.println("
"); out.println(" "); out.print("
juick.com © 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("
" + rb.getString("Sponsored by") + ": " + links); } } out.println("
"); out.println("
"); } public static void pageEnd(PrintWriter out) { out.println(""); } public static String formatTags(ArrayList tags, com.juick.User user) { String ret = ""; for (int i = 0; i < tags.size(); i++) { String tag = tags.get(i); tag = tag.replaceAll("<", "<"); tag = tag.replaceAll(">", ">"); try { ret += " *" + tag + ""; } 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 ""; } 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))([\\[\\{]|<)((?:ht|f)tps?://(?:www\\.)?([^\\/\\s\\\"\\)\\!]+)/?(?:[^\\]\\}](?", ">"); // http://juick.com/last?page=2 // http://juick.com/last?page=2 msg = msg.replaceAll("((?<=\\s)|(?<=\\A))((?:ht|f)tps?://(?:www\\.)?([^\\/\\s\\n\\\"]+)/?[^\\s\\n\\\"]*)", "$1$2"); // (http://juick.com/last?page=2) // (http://juick.com/last?page=2) 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" + url + "$5"); } m.appendTail(sb); msg = sb.toString(); return "
" + msg + "
"; } public static String formatMessage(String msg) { msg = msg.replaceAll("&", "&"); msg = msg.replaceAll("<", "<"); msg = msg.replaceAll(">", ">"); // -- // — msg = msg.replaceAll("((?<=\\s)|(?<=\\A))\\-\\-?((?=\\s)|(?=\\Z))", "$1—$2"); // http://juick.com/last?page=2 // juick.com msg = msg.replaceAll("((?<=\\s)|(?<=\\A))((?:ht|f)tps?://(?:www\\.)?([^\\/\\s\\n\\\"]+)/?[^\\s\\n\\\"]*)", "$1$3"); // #12345 // #12345 msg = msg.replaceAll("((?<=\\s)|(?<=\\A)|(?<=\\p{Punct}))#(\\d+)((?=\\s)|(?=\\Z)|(?=\\))|(?=\\.)|(?=\\,))", "$1#$2$3"); // #12345/65 // #12345/65 msg = msg.replaceAll("((?<=\\s)|(?<=\\A)|(?<=\\p{Punct}))#(\\d+)/(\\d+)((?=\\s)|(?=\\Z)|(?=\\p{Punct}))", "$1#$2/$3$4"); // *bold* // bold msg = msg.replaceAll("((?<=\\s)|(?<=\\A)|(?<=\\p{Punct}))\\*([^\\*\\n<>]+)\\*((?=\\s)|(?=\\Z)|(?=\\p{Punct}))", "$1$2$3"); // /italic/ // italic msg = msg.replaceAll("((?<=\\s)|(?<=\\A))/([^\\/\\n<>]+)/((?=\\s)|(?=\\Z)|(?=\\p{Punct}))", "$1$2$3"); // _underline_ // underline msg = msg.replaceAll("((?<=\\s)|(?<=\\A))_([^\\_\\n<>]+)_((?=\\s)|(?=\\Z)|(?=\\p{Punct}))", "$1$2$3"); // /12 // /12 msg = msg.replaceAll("((?<=\\s)|(?<=\\A))\\/(\\d+)((?=\\s)|(?=\\Z)|(?=\\p{Punct}))", "$1/$2$3"); // @username@jabber.org // @username@jabber.org msg = msg.replaceAll("((?<=\\s)|(?<=\\A))@([\\w\\-\\.]+@[\\w\\-\\.]+)((?=\\s)|(?=\\Z)|(?=\\p{Punct}))", "$1@$2$3"); // @username // @username msg = msg.replaceAll("((?<=\\s)|(?<=\\A))@([\\w\\-]{2,16})((?=\\s)|(?=\\Z)|(?=\\p{Punct}))", "$1@$2$3"); // (http://juick.com/last?page=2) // (juick.com) 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$4$5"); } m.appendTail(sb); msg = sb.toString(); // > citate msg = msg.replaceAll("(?:(?<=\\n)|(?<=\\A))> *(.*)?(\\n|(?=\\Z))", "
$1
"); msg = msg.replaceAll("
", "\n"); msg = msg.replaceAll("\n", "
\n"); return msg; } public static void printMessages(PrintWriter out, Connection sql, com.juick.User user, ArrayList mids, com.juick.User visitor, Locale locale, int YandexID, int ad_mid) { ResourceBundle rb = ResourceBundle.getBundle("Global", locale); ArrayList 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 blUIDs = new ArrayList(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("
  • "); out.println(""); } 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 = "" + tags + ""; } out.println("
  • "); out.println("
    \""
    "); out.println("
    "); out.println("
    "); out.println(" "); out.print("
    " + formatDate(msg.TimeAgo, msg.TimestampString, locale) + ""); if (msg.MID == ad_mid) { out.print(" · " + rb.getString("Sponsored") + ""); } out.println("
    "); out.print("
    " + txt + "
    "); if (msg.AttachmentType != null) { out.println("
    \"\"/
    "); } msg.ReadOnly |= blUIDs.contains(msg.User.UID); if (visitor != null && (msg.ReadOnly == false || visitor.UID == msg.User.UID)) { out.println("
    "); out.println("
    "); out.println("
    "); } if (msg.Replies > 0) { String repliesby = (msg.RepliesBy != null) ? msg.RepliesBy : "..."; out.println("
    " + formatReplies(msg.Replies, locale) + " " + rb.getString("(replies) by") + " " + repliesby + "
    "); } out.println("
    "); out.println("
  • "); } } }