/*
* 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.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.ParseException;
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.S");
private static SimpleDateFormat sdfSimple = new SimpleDateFormat("d MMM");
private static SimpleDateFormat sdfFull = new SimpleDateFormat("d MMM yyyy");
public static void pageHead(PrintWriter out, String title, String headers) {
out.println("");
out.println("");
out.println("
");
out.println(" ");
out.println(" " + title + " ");
out.println(" ");
out.println(" ");
out.println(" ");
out.println(" ");
out.println(" ");
if (headers != null) {
out.println(headers);
}
out.println("");
out.println();
out.println("");
}
public static void pageNavigation(PrintWriter out, Locale loc, com.juick.User user) {
ResourceBundle rb = ResourceBundle.getBundle("Global", loc);
out.println("");
out.println("");
out.println("
");
}
public static void pageFilters(PrintWriter out, Locale loc, com.juick.User user) {
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 pageFooter(HttpServletRequest request, PrintWriter out, Locale loc, com.juick.User visitor) {
ResourceBundle rb = ResourceBundle.getBundle("Global", loc);
out.println("
");
}
public static String formatTags(String tags) {
String ret = "";
String tagsarr[] = tags.split(" ");
for (int i = 0; i < tagsarr.length; i++) {
String tag = tagsarr[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 (ParseException e) {
return fulldate;
}
}
}
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\\\"\\)\\!]+)/?(?:[^\\]\\}](?", ">");
// --
// —
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, ArrayList mids, Locale locale) {
ResourceBundle rb = ResourceBundle.getBundle("Global", locale);
PreparedStatement stmt = null;
ResultSet rs = null;
try {
stmt = sql.prepareStatement("SELECT STRAIGHT_JOIN messages.message_id,messages.user_id,users.nick,messages_txt.tags,messages.readonly,messages.privacy,messages_txt.txt,TIMESTAMPDIFF(MINUTE,messages.ts,NOW()),messages.ts,messages.replies,messages_txt.repliesby,messages.attach,messages.place_id,places.name,messages.lat,messages.lon FROM ((messages INNER JOIN messages_txt ON messages.message_id=messages_txt.message_id) INNER JOIN users ON messages.user_id=users.id) LEFT JOIN places ON messages.place_id=places.place_id WHERE messages.message_id IN (" + Utils.convertArray2String(mids) + ") ORDER BY messages.message_id DESC");
rs = stmt.executeQuery();
rs.beforeFirst();
while (rs.next()) {
int mid = rs.getInt(1);
int uid = rs.getInt(2);
String uname = rs.getString(3);
String tags = rs.getString(4);
String txt = rs.getString(7);
// timediff
// timestamp
// replies
// 11 repliesby
// attach
// pid
// pname
// lat
// lon
boolean cancomment = true;
tags = (tags != null) ? formatTags(tags) : "";
if (rs.getInt(5) == 1) {
tags += " *readonly";
cancomment = false;
}
switch (rs.getInt(6)) {
case 2:
tags += " *public";
break;
case -1:
tags += " *friends";
break;
case -2:
tags += " *private";
break;
}
txt = formatMessage(txt);
out.println(" ");
out.println(" ");
out.println(" ");
out.println(" ");
out.println(" " + txt + "
");
if (rs.getString(12) != null) {
if (rs.getString(12).equals("jpg")) {
out.println(" ");
} else {
out.println(" ");
}
}
if (rs.getInt(10) > 0) {
String repliesby = rs.getString(11);
if (repliesby == null) {
repliesby = "...";
}
out.println(" ");
} else if (cancomment) {
out.println(" ");
}
out.println(" ");
}
} catch (SQLException e) {
System.err.println(e);
} finally {
Utils.finishSQL(rs, stmt);
}
}
/*
public static void printPhotos(PrintWriter out, Connection sql, ArrayList mids, Locale locale) {
ResourceBundle rb = ResourceBundle.getBundle("Global", locale);
out.println("");
out.println(" ");
PreparedStatement stmt = null;
ResultSet rs = null;
try {
stmt = sql.prepareStatement("SELECT STRAIGHT_JOIN messages.message_id,messages.user_id,users.nick,messages_txt.tags,messages.readonly,messages.privacy,messages_txt.txt,TIMESTAMPDIFF(MINUTE,messages.ts,NOW()),messages.ts,messages.replies,messages_txt.repliesby,messages.attach,messages.place_id,places.name,messages.lat,messages.lon FROM ((messages INNER JOIN messages_txt ON messages.message_id=messages_txt.message_id) INNER JOIN users ON messages.user_id=users.id) LEFT JOIN places ON messages.place_id=places.place_id WHERE messages.message_id IN (" + Utils.convertArray2String(mids) + ") ORDER BY messages.message_id DESC");
rs = stmt.executeQuery();
rs.beforeFirst();
int cnt = 0;
while (rs.next()) {
int mid = rs.getInt(1);
int uid = rs.getInt(2);
String uname = rs.getString(3);
String tags = rs.getString(4);
String txt = rs.getString(7);
txt = formatMessage(txt);
if (cnt % 2 == 0 && cnt > 0) {
out.println(" ");
out.println(" ");
}
out.print(" ");
out.println("");
out.println("
");
out.println("
" + formatDate(rs.getInt(8), rs.getString(9), locale) + "
");
out.println("
@" + uname + "
");
if (rs.getInt(10) > 0) {
out.println(" ");
}
out.println("
" + txt + "
");
out.println("
");
cnt++;
}
} catch (SQLException e) {
System.err.println(e);
} finally {
Utils.finishSQL(rs, stmt);
}
out.println(" ");
out.println("
");
}
*/
}