public static String dateString(final Date date) {
return SimpleFormatter.format(date);
}
public static serverObjects respond(final RequestHeader header, final serverObjects post, final serverSwitch env) {
final Switchboard sb = (Switchboard) env;
final serverObjects prop = new serverObjects();
BlogBoard.BlogEntry page = null;
boolean hasRights = sb.verifyAuthentication(header, true);
//final int display = (hasRights || post == null) ? 1 : post.getInt("display", 0);
//prop.put("display", display);
prop.put("display", 1); // Fixed to 1
final boolean xml = (header.get(HeaderFramework.CONNECTION_PROP_PATH)).endsWith(".xml");
final String address = sb.peers.mySeed().getPublicAddress();
if(hasRights) {
prop.put("mode_admin", "1");
} else {
prop.put("mode_admin", "0");
}
if (post == null) {
prop.putHTML("peername", sb.peers.mySeed().getName());
prop.put("address", address);
return putBlogDefault(prop, sb, address, 0, 10, hasRights, xml);
}
final int start = post.getInt("start",0); //indicates from where entries should be shown
final int num = post.getInt("num",10); //indicates how many entries should be shown
if (!hasRights) {
final UserDB.Entry userentry = sb.userDB.proxyAuth(header.get(RequestHeader.AUTHORIZATION, "xxxxxx"));
if (userentry != null && userentry.hasRight(UserDB.AccessRight.BLOG_RIGHT)) {
hasRights=true;
} else if (post.containsKey("login")) {
//opens login window if login link is clicked
prop.put("AUTHENTICATE","admin log-in");
}
}
String pagename = post.get("page", DEFAULT_PAGE);
final String ip = header.get(HeaderFramework.CONNECTION_PROP_CLIENTIP, "127.0.0.1");
String StrAuthor = post.get("author", "");
if ("anonymous".equals(StrAuthor)) {
StrAuthor = sb.blogDB.guessAuthor(ip);
if (StrAuthor == null || StrAuthor.length() == 0) {
if (sb.peers.mySeed() == null) {
StrAuthor = "anonymous";
} else {
StrAuthor = sb.peers.mySeed().get("Name", "anonymous");
}
}
}
byte[] author;
author = UTF8.getBytes(StrAuthor);
if (hasRights && post.containsKey("delete") && "sure".equals(post.get("delete"))) {
page = sb.blogDB.readBlogEntry(pagename);
for (final String comment : page.getComments()) {
sb.blogCommentDB.delete(comment);
}
sb.blogDB.deleteBlogEntry(pagename);
pagename = DEFAULT_PAGE;
}
if (post.containsKey("discard")) {
pagename = DEFAULT_PAGE;
}
if (post.containsKey("submit") && hasRights) {
// store a new/edited blog-entry
byte[] content;
content = UTF8.getBytes(post.get("content", ""));
final Date date;
List<String> comments = null;
//set name for new entry or date for old entry
if (DEFAULT_PAGE.equals(pagename)) {
pagename = String.valueOf(System.currentTimeMillis());
date = null;
} else {
page = sb.blogDB.readBlogEntry(pagename);
comments = page.getComments();
date = page.getDate();
}
final String commentMode = post.get("commentMode", "2");
final String StrSubject = post.get("subject", "");
byte[] subject;
subject = UTF8.getBytes(StrSubject);
sb.blogDB.writeBlogEntry(sb.blogDB.newEntry(pagename, subject, author, ip, date, content, comments, commentMode));
// create a news message
if (!sb.isRobinsonMode()) {
final Map<String, String> map = new HashMap<String, String>();
map.put("page", pagename);
map.put("subject", StrSubject.replace(',', ' '));
map.put("author", StrAuthor.replace(',', ' '));
sb.peers.newsPool.publishMyNews(sb.peers.mySeed(), yacyNewsPool.CATEGORY_BLOG_ADD, map);