diff --git a/htroot/Blog.java b/htroot/Blog.java index 06598635a..41cbcd21d 100644 --- a/htroot/Blog.java +++ b/htroot/Blog.java @@ -1,394 +1,390 @@ -// Blog.java -// ----------------------- -// part of YACY -// (C) by Michael Peter Christen; mc@anomic.de -// first published on http://www.anomic.de -// Frankfurt, Germany, 2004 -// -// This File is contributed by Jan Sandbrink -// Contains contributions from Marc Nause [MN] -// last change: 06.05.2006 -// -// This program is free software; you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation; either version 2 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 General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program; if not, write to the Free Software -// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -// -// Using this software in any meaning (reading, learning, copying, compiling, -// running) means that you agree that the Author(s) is (are) not responsible -// for cost, loss of data or any harm that may be caused directly or indirectly -// by usage of this softare or this documentation. The usage of this software -// is on your own risk. The installation and usage (starting/running) of this -// software may allow other people or application to access your computer and -// any attached devices and is highly dependent on the configuration of the -// software which must be done by the user of the software; the author(s) is -// (are) also not responsible for proper configuration and usage of the -// software, even if provoked by documentation provided together with -// the software. -// -// Any changes to this file according to the GPL as documented in the file -// gpl.txt aside this file in the shipment you received can be done to the -// lines that follows this copyright notice here, but changes must not be -// done inside the copyright notive above. A re-distribution must contain -// the intact and unchanged copyright notice. -// Contributions and changes to the program code must be marked as such. - -// You must compile this file with -// javac -classpath .:../classes Blog.java -// if the shell's current path is HTROOT - -import java.io.IOException; -import java.io.UnsupportedEncodingException; -import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.Date; -import java.util.HashMap; -import java.util.Iterator; - -import de.anomic.data.blogBoard; -import de.anomic.data.userDB; -import de.anomic.http.httpHeader; -import de.anomic.http.httpc; -import de.anomic.plasma.plasmaSwitchboard; -import de.anomic.server.serverObjects; -import de.anomic.server.serverSwitch; -import de.anomic.server.logging.serverLog; -import de.anomic.yacy.yacyCore; -import de.anomic.yacy.yacyNewsPool; -import de.anomic.yacy.yacyNewsRecord; - -public class Blog { - - private static final String DEFAULT_PAGE = "blog_default"; - - private static SimpleDateFormat SimpleFormatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); - // TODO: make userdefined date/time-strings (localisation) - - public static String dateString(Date date) { - return SimpleFormatter.format(date); - } - - public static serverObjects respond(httpHeader header, serverObjects post, serverSwitch env) { - final plasmaSwitchboard switchboard = (plasmaSwitchboard) env; - final serverObjects prop = new serverObjects(); - blogBoard.entry page = null; - - final boolean authenticated = switchboard.adminAuthenticated(header) >= 2; - final int display = ((post == null) || (!authenticated)) ? 0 : post.getInt("display", 0); - prop.put("display", display); - - boolean hasRights = switchboard.verifyAuthentication(header, true); - final boolean xml = ((String)header.get(httpHeader.CONNECTION_PROP_PATH)).endsWith(".xml"); - final String address = yacyCore.seedDB.mySeed().getPublicAddress(); - - if(hasRights) { - prop.put("mode_admin", "1"); - } else { - prop.put("mode_admin", "0"); - } - - if (post == null) { - prop.putHTML("peername", yacyCore.seedDB.mySeed().getName()); - prop.put("address", address); - return putBlogDefault(prop, switchboard, 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 = switchboard.userDB.proxyAuth((String)header.get("Authorization", "xxxxxx")); - if(userentry != null && userentry.hasRight(userDB.Entry.BLOG_RIGHT)){ - hasRights=true; - } else if(post.containsKey("login")) { - //opens login window if login link is clicked - contrib [MN] - prop.put("AUTHENTICATE","admin log-in"); - } - } - - String pagename = post.get("page", DEFAULT_PAGE); - final String ip = (String)header.get(httpHeader.CONNECTION_PROP_CLIENTIP, "127.0.0.1"); - - String StrAuthor = post.get("author", ""); - - if (StrAuthor.equals("anonymous")) { - StrAuthor = switchboard.blogDB.guessAuthor(ip); - - if (StrAuthor == null || StrAuthor.length() == 0) { - if (de.anomic.yacy.yacyCore.seedDB.mySeed() == null) { - StrAuthor = "anonymous"; - } else { - StrAuthor = de.anomic.yacy.yacyCore.seedDB.mySeed().get("Name", "anonymous"); - } - } - } - - byte[] author; - try { - author = StrAuthor.getBytes("UTF-8"); - } catch (UnsupportedEncodingException e) { - author = StrAuthor.getBytes(); - } - - if(hasRights && post.containsKey("delete") && post.get("delete").equals("sure")) { - page = switchboard.blogDB.read(pagename); - final Iterator i = page.comments().iterator(); - while(i.hasNext()) { - switchboard.blogCommentDB.delete(i.next()); - } - switchboard.blogDB.delete(pagename); - pagename = DEFAULT_PAGE; - } - - if (post.containsKey("discard")) { - pagename = DEFAULT_PAGE; - } - - if (post.containsKey("submit") && (hasRights)) { - // store a new/edited blog-entry - byte[] content; - try { - content = post.get("content", "").getBytes("UTF-8"); - } catch (UnsupportedEncodingException e) { - content = post.get("content", "").getBytes(); - } - - Date date = null; - ArrayList comments = null; - - //set name for new entry or date for old entry - if(pagename.equals(DEFAULT_PAGE)) { - pagename = String.valueOf(System.currentTimeMillis()); - } else { - page = switchboard.blogDB.read(pagename); - comments = page.comments(); - date = page.date(); - } - final String commentMode = post.get("commentMode", "1"); - final String StrSubject = post.get("subject", ""); - byte[] subject; - try { - subject = StrSubject.getBytes("UTF-8"); - } catch (UnsupportedEncodingException e) { - subject = StrSubject.getBytes(); - } - - switchboard.blogDB.write(switchboard.blogDB.newEntry(pagename, subject, author, ip, date, content, comments, commentMode)); - - // create a news message - final HashMap map = new HashMap(); - map.put("page", pagename); - map.put("subject", StrSubject.replace(',', ' ')); - map.put("author", StrAuthor.replace(',', ' ')); - yacyCore.newsPool.publishMyNews(yacyNewsRecord.newRecord(yacyNewsPool.CATEGORY_BLOG_ADD, map)); - } - - page = switchboard.blogDB.read(pagename); //maybe "if(page == null)" - - if (post.containsKey("edit")) { - //edit an entry - if(hasRights) { - try { - prop.put("mode", "1"); //edit - prop.put("mode_commentMode", page.getCommentMode()); - prop.putHTML("mode_author", new String(page.author(),"UTF-8"), xml); - prop.put("mode_pageid", page.key()); - prop.putHTML("mode_subject", new String(page.subject(), "UTF-8"), xml); - prop.put("mode_page-code", new String(page.page(), "UTF-8")); - } catch (UnsupportedEncodingException e) {} - } - else { - prop.put("mode", "3"); //access denied (no rights) - } - } - else if(post.containsKey("preview")) { - //preview the page - if(hasRights) { - prop.put("mode", "2");//preview - prop.put("mode_commentMode", post.getInt("commentMode", 1)); - prop.putHTML("mode_pageid", pagename, xml); - try { - prop.putHTML("mode_author", new String(author, "UTF-8"), xml); - } catch (UnsupportedEncodingException e) { - prop.putHTML("mode_author", new String(author), xml); - } - prop.putHTML("mode_subject", post.get("subject",""), xml); - prop.put("mode_date", dateString(new Date())); - prop.putWiki("mode_page", post.get("content", "")); - prop.putHTML("mode_page-code", post.get("content", ""), xml); - } - else { - prop.put("mode", "3"); //access denied (no rights) - } - } - else if(post.get("delete", "").equals("try")) { - if(hasRights) { - prop.put("mode", "4"); - prop.put("mode_pageid", pagename); - try { - prop.putHTML("mode_author",new String(page.author(), "UTF-8"), xml); - } catch (UnsupportedEncodingException e) { - prop.putHTML("mode_author",new String(page.author()), xml); - } - try { - prop.putHTML("mode_subject",new String(page.subject(),"UTF-8"), xml); - } catch (UnsupportedEncodingException e) { - prop.putHTML("mode_subject",new String(page.subject()), xml); - } - } - else prop.put("mode", "3"); //access denied (no rights) - } - else if (post.containsKey("import")) { - prop.put("mode", "5"); - prop.put("mode_state", "0"); - } - else if (post.containsKey("xmlfile")) { - prop.put("mode", "5"); - if(switchboard.blogDB.importXML(post.get("xmlfile$file"))) { - prop.put("mode_state", "1"); - } - else { - prop.put("mode_state", "2"); - } - } - else { - // show blog-entry/entries - prop.put("mode", "0"); //viewing - if(pagename.equals(DEFAULT_PAGE)) { - // XXX: where are "peername" and "address" used in the template? - // XXX: "clientname" is already set to the peername, no need for a new setting - prop.putHTML("peername", yacyCore.seedDB.mySeed().getName(), xml); - prop.put("address", address); - //index all entries - putBlogDefault(prop, switchboard, address, start, num, hasRights, xml); - } - else { - //only show 1 entry - prop.put("mode_entries", "1"); - putBlogEntry(prop, page, address, 0, hasRights, xml); - } - } - - // return rewrite properties - return prop; - } - - private static serverObjects putBlogDefault( - final serverObjects prop, - final plasmaSwitchboard switchboard, - final String address, - int start, - int num, - final boolean hasRights, - final boolean xml) - { - try { - final Iterator i = switchboard.blogDB.keys(false); - String pageid; - int count = 0; //counts how many entries are shown to the user - if(xml) num = 0; - final int nextstart = start+num; //indicates the starting offset for next results - int prevstart = start-num; //indicates the starting offset for previous results - boolean prev = false; //indicates if there were previous comments to the ones that are dispalyed - if (start > 0) prev = true; - while(i.hasNext() && (num == 0 || num > count)) { - pageid = i.next(); - if(0 < start--) continue; - putBlogEntry( - prop, - switchboard.blogDB.read(pageid), - address, - count++, - hasRights, - xml); - } - prop.put("mode_entries", count); - - if(i.hasNext()) { - prop.put("mode_moreentries", "1"); //more entries are availible - prop.put("mode_moreentries_start", nextstart); - prop.put("mode_moreentries_num", num); - } else { - prop.put("moreentries", "0"); - } - - if(prev) { - prop.put("mode_preventries", "1"); - if (prevstart < 0) prevstart = 0; - prop.put("mode_preventries_start", prevstart); - prop.put("mode_preventries_num", num); - } else prop.put("mode_preventries", "0"); - - - } catch (IOException e) { serverLog.logSevere("BLOG", "Error reading blog-DB", e); } - return prop; - } - - private static serverObjects putBlogEntry( - final serverObjects prop, - final blogBoard.entry entry, - final String address, - final int number, - final boolean hasRights, - final boolean xml) - { - // subject - try { - prop.putHTML("mode_entries_" + number + "_subject", new String(entry.subject(),"UTF-8"), xml); - } catch (UnsupportedEncodingException e) { - prop.putHTML("mode_entries_" + number + "_subject", new String(entry.subject()), xml); - } - - // author - try { - prop.putHTML("mode_entries_" + number + "_author", new String(entry.author(),"UTF-8"), xml); - } catch (UnsupportedEncodingException e) { - prop.putHTML("mode_entries_" + number + "_author", new String(entry.author()), xml); - } - - // comments - if(entry.getCommentMode() == 0) { - prop.put("mode_entries_" + number + "_commentsactive", "0"); - } else { - prop.put("mode_entries_" + number + "_commentsactive", "1"); - prop.put("mode_entries_" + number + "_commentsactive_pageid", entry.key()); - prop.put("mode_entries_" + number + "_commentsactive_address", address); - try { - prop.put("mode_entries_" + number + "_commentsactive_comments", new String(entry.commentsSize(),"UTF-8")); - } catch (UnsupportedEncodingException e) { - prop.put("mode_entries_" + number + "_commentsactive_comments", new String(entry.commentsSize())); - } - } - - prop.put("mode_entries_" + number + "_date", dateString(entry.date())); - prop.put("mode_entries_" + number + "_rfc822date", httpc.dateString(entry.date())); - prop.put("mode_entries_" + number + "_pageid", entry.key()); - prop.put("mode_entries_" + number + "_address", address); - prop.put("mode_entries_" + number + "_ip", entry.ip()); - - if(xml) { - prop.put("mode_entries_" + number + "_page", entry.page()); - prop.put("mode_entries_" + number + "_timestamp", entry.timestamp()); - } else { - prop.putWiki("mode_entries_" + number + "_page", entry.page()); - } - - if(hasRights) { - prop.put("mode_entries_" + number + "_admin", "1"); - prop.put("mode_entries_" + number + "_admin_pageid",entry.key()); - } else { - prop.put("mode_entries_" + number + "_admin", "0"); - } - - return prop; - } -} +// Blog.java +// ----------------------- +// part of YACY +// (C) by Michael Peter Christen; mc@anomic.de +// first published on http://www.anomic.de +// Frankfurt, Germany, 2004 +// +// This File is contributed by Jan Sandbrink +// Contains contributions from Marc Nause [MN] +// last change: 06.05.2006 +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 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 General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// Using this software in any meaning (reading, learning, copying, compiling, +// running) means that you agree that the Author(s) is (are) not responsible +// for cost, loss of data or any harm that may be caused directly or indirectly +// by usage of this softare or this documentation. The usage of this software +// is on your own risk. The installation and usage (starting/running) of this +// software may allow other people or application to access your computer and +// any attached devices and is highly dependent on the configuration of the +// software which must be done by the user of the software; the author(s) is +// (are) also not responsible for proper configuration and usage of the +// software, even if provoked by documentation provided together with +// the software. +// +// Any changes to this file according to the GPL as documented in the file +// gpl.txt aside this file in the shipment you received can be done to the +// lines that follows this copyright notice here, but changes must not be +// done inside the copyright notive above. A re-distribution must contain +// the intact and unchanged copyright notice. +// Contributions and changes to the program code must be marked as such. + +// You must compile this file with +// javac -classpath .:../classes Blog.java +// if the shell's current path is HTROOT + +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Date; +import java.util.HashMap; +import java.util.Iterator; + +import de.anomic.data.blogBoard; +import de.anomic.data.userDB; +import de.anomic.http.httpHeader; +import de.anomic.http.httpc; +import de.anomic.plasma.plasmaSwitchboard; +import de.anomic.server.serverObjects; +import de.anomic.server.serverSwitch; +import de.anomic.server.logging.serverLog; +import de.anomic.yacy.yacyCore; +import de.anomic.yacy.yacyNewsPool; +import de.anomic.yacy.yacyNewsRecord; + +public class Blog { + + private static final String DEFAULT_PAGE = "blog_default"; + + private static SimpleDateFormat SimpleFormatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); + // TODO: make userdefined date/time-strings (localisation) + + public static String dateString(Date date) { + return SimpleFormatter.format(date); + } + + public static serverObjects respond(httpHeader header, serverObjects post, serverSwitch env) { + final plasmaSwitchboard switchboard = (plasmaSwitchboard) env; + final serverObjects prop = new serverObjects(); + blogBoard.entry page = null; + + final boolean authenticated = switchboard.adminAuthenticated(header) >= 2; + final int display = ((post == null) || (!authenticated)) ? 0 : post.getInt("display", 0); + prop.put("display", display); + + boolean hasRights = switchboard.verifyAuthentication(header, true); + final boolean xml = ((String)header.get(httpHeader.CONNECTION_PROP_PATH)).endsWith(".xml"); + final String address = yacyCore.seedDB.mySeed().getPublicAddress(); + + if(hasRights) { + prop.put("mode_admin", "1"); + } else { + prop.put("mode_admin", "0"); + } + + if (post == null) { + prop.putHTML("peername", yacyCore.seedDB.mySeed().getName()); + prop.put("address", address); + return putBlogDefault(prop, switchboard, 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 = switchboard.userDB.proxyAuth((String)header.get("Authorization", "xxxxxx")); + if(userentry != null && userentry.hasRight(userDB.Entry.BLOG_RIGHT)){ + hasRights=true; + } else if(post.containsKey("login")) { + //opens login window if login link is clicked - contrib [MN] + prop.put("AUTHENTICATE","admin log-in"); + } + } + + String pagename = post.get("page", DEFAULT_PAGE); + final String ip = (String)header.get(httpHeader.CONNECTION_PROP_CLIENTIP, "127.0.0.1"); + + String StrAuthor = post.get("author", ""); + + if (StrAuthor.equals("anonymous")) { + StrAuthor = switchboard.blogDB.guessAuthor(ip); + + if (StrAuthor == null || StrAuthor.length() == 0) { + if (de.anomic.yacy.yacyCore.seedDB.mySeed() == null) { + StrAuthor = "anonymous"; + } else { + StrAuthor = de.anomic.yacy.yacyCore.seedDB.mySeed().get("Name", "anonymous"); + } + } + } + + byte[] author; + try { + author = StrAuthor.getBytes("UTF-8"); + } catch (UnsupportedEncodingException e) { + author = StrAuthor.getBytes(); + } + + if(hasRights && post.containsKey("delete") && post.get("delete").equals("sure")) { + page = switchboard.blogDB.read(pagename); + final Iterator i = page.comments().iterator(); + while(i.hasNext()) { + switchboard.blogCommentDB.delete(i.next()); + } + switchboard.blogDB.delete(pagename); + pagename = DEFAULT_PAGE; + } + + if (post.containsKey("discard")) { + pagename = DEFAULT_PAGE; + } + + if (post.containsKey("submit") && (hasRights)) { + // store a new/edited blog-entry + byte[] content; + try { + content = post.get("content", "").getBytes("UTF-8"); + } catch (UnsupportedEncodingException e) { + content = post.get("content", "").getBytes(); + } + + Date date = null; + ArrayList comments = null; + + //set name for new entry or date for old entry + if(pagename.equals(DEFAULT_PAGE)) { + pagename = String.valueOf(System.currentTimeMillis()); + } else { + page = switchboard.blogDB.read(pagename); + comments = page.comments(); + date = page.date(); + } + final String commentMode = post.get("commentMode", "1"); + final String StrSubject = post.get("subject", ""); + byte[] subject; + try { + subject = StrSubject.getBytes("UTF-8"); + } catch (UnsupportedEncodingException e) { + subject = StrSubject.getBytes(); + } + + switchboard.blogDB.write(switchboard.blogDB.newEntry(pagename, subject, author, ip, date, content, comments, commentMode)); + + // create a news message + final HashMap map = new HashMap(); + map.put("page", pagename); + map.put("subject", StrSubject.replace(',', ' ')); + map.put("author", StrAuthor.replace(',', ' ')); + yacyCore.newsPool.publishMyNews(yacyNewsRecord.newRecord(yacyNewsPool.CATEGORY_BLOG_ADD, map)); + } + + page = switchboard.blogDB.read(pagename); //maybe "if(page == null)" + + if (post.containsKey("edit")) { + //edit an entry + if(hasRights) { + try { + prop.put("mode", "1"); //edit + prop.put("mode_commentMode", page.getCommentMode()); + prop.putHTML("mode_author", new String(page.author(),"UTF-8"), xml); + prop.put("mode_pageid", page.key()); + prop.putHTML("mode_subject", new String(page.subject(), "UTF-8"), xml); + prop.put("mode_page-code", new String(page.page(), "UTF-8")); + } catch (UnsupportedEncodingException e) {} + } + else { + prop.put("mode", "3"); //access denied (no rights) + } + } + else if(post.containsKey("preview")) { + //preview the page + if(hasRights) { + prop.put("mode", "2");//preview + prop.put("mode_commentMode", post.getInt("commentMode", 1)); + prop.putHTML("mode_pageid", pagename, xml); + try { + prop.putHTML("mode_author", new String(author, "UTF-8"), xml); + } catch (UnsupportedEncodingException e) { + prop.putHTML("mode_author", new String(author), xml); + } + prop.putHTML("mode_subject", post.get("subject",""), xml); + prop.put("mode_date", dateString(new Date())); + prop.putWiki("mode_page", post.get("content", "")); + prop.putHTML("mode_page-code", post.get("content", ""), xml); + } + else { + prop.put("mode", "3"); //access denied (no rights) + } + } + else if(post.get("delete", "").equals("try")) { + if(hasRights) { + prop.put("mode", "4"); + prop.put("mode_pageid", pagename); + try { + prop.putHTML("mode_author",new String(page.author(), "UTF-8"), xml); + } catch (UnsupportedEncodingException e) { + prop.putHTML("mode_author",new String(page.author()), xml); + } + try { + prop.putHTML("mode_subject",new String(page.subject(),"UTF-8"), xml); + } catch (UnsupportedEncodingException e) { + prop.putHTML("mode_subject",new String(page.subject()), xml); + } + } + else prop.put("mode", "3"); //access denied (no rights) + } + else if (post.containsKey("import")) { + prop.put("mode", "5"); + prop.put("mode_state", "0"); + } + else if (post.containsKey("xmlfile")) { + prop.put("mode", "5"); + if(switchboard.blogDB.importXML(post.get("xmlfile$file"))) { + prop.put("mode_state", "1"); + } + else { + prop.put("mode_state", "2"); + } + } + else { + // show blog-entry/entries + prop.put("mode", "0"); //viewing + if(pagename.equals(DEFAULT_PAGE)) { + // XXX: where are "peername" and "address" used in the template? + // XXX: "clientname" is already set to the peername, no need for a new setting + prop.putHTML("peername", yacyCore.seedDB.mySeed().getName(), xml); + prop.put("address", address); + //index all entries + putBlogDefault(prop, switchboard, address, start, num, hasRights, xml); + } + else { + //only show 1 entry + prop.put("mode_entries", "1"); + putBlogEntry(prop, page, address, 0, hasRights, xml); + } + } + + // return rewrite properties + return prop; + } + + private static serverObjects putBlogDefault( + final serverObjects prop, + final plasmaSwitchboard switchboard, + final String address, + int start, + int num, + final boolean hasRights, + final boolean xml) + { + try { + final Iterator i = switchboard.blogDB.keys(false); + String pageid; + int count = 0; //counts how many entries are shown to the user + if(xml) num = 0; + final int nextstart = start+num; //indicates the starting offset for next results + int prevstart = start-num; //indicates the starting offset for previous results + boolean prev = false; //indicates if there were previous comments to the ones that are dispalyed + if (start > 0) prev = true; + while(i.hasNext() && (num == 0 || num > count)) { + pageid = i.next(); + if(0 < start--) continue; + putBlogEntry( + prop, + switchboard.blogDB.read(pageid), + address, + count++, + hasRights, + xml); + } + prop.put("mode_entries", count); + + if(i.hasNext()) { + prop.put("mode_moreentries", "1"); //more entries are availible + prop.put("mode_moreentries_start", nextstart); + prop.put("mode_moreentries_num", num); + } else { + prop.put("moreentries", "0"); + } + + if(prev) { + prop.put("mode_preventries", "1"); + if (prevstart < 0) prevstart = 0; + prop.put("mode_preventries_start", prevstart); + prop.put("mode_preventries_num", num); + } else prop.put("mode_preventries", "0"); + + + } catch (IOException e) { serverLog.logSevere("BLOG", "Error reading blog-DB", e); } + return prop; + } + + private static serverObjects putBlogEntry( + final serverObjects prop, + final blogBoard.entry entry, + final String address, + final int number, + final boolean hasRights, + final boolean xml) + { + // subject + try { + prop.putHTML("mode_entries_" + number + "_subject", new String(entry.subject(),"UTF-8"), xml); + } catch (UnsupportedEncodingException e) { + prop.putHTML("mode_entries_" + number + "_subject", new String(entry.subject()), xml); + } + + // author + try { + prop.putHTML("mode_entries_" + number + "_author", new String(entry.author(),"UTF-8"), xml); + } catch (UnsupportedEncodingException e) { + prop.putHTML("mode_entries_" + number + "_author", new String(entry.author()), xml); + } + + // comments + if(entry.getCommentMode() == 0) { + prop.put("mode_entries_" + number + "_commentsactive", "0"); + } else { + prop.put("mode_entries_" + number + "_commentsactive", "1"); + prop.put("mode_entries_" + number + "_commentsactive_pageid", entry.key()); + prop.put("mode_entries_" + number + "_commentsactive_address", address); + prop.put("mode_entries_" + number + "_commentsactive_comments", entry.commentsSize()); + } + + prop.put("mode_entries_" + number + "_date", dateString(entry.date())); + prop.put("mode_entries_" + number + "_rfc822date", httpc.dateString(entry.date())); + prop.put("mode_entries_" + number + "_pageid", entry.key()); + prop.put("mode_entries_" + number + "_address", address); + prop.put("mode_entries_" + number + "_ip", entry.ip()); + + if(xml) { + prop.put("mode_entries_" + number + "_page", entry.page()); + prop.put("mode_entries_" + number + "_timestamp", entry.timestamp()); + } else { + prop.putWiki("mode_entries_" + number + "_page", entry.page()); + } + + if(hasRights) { + prop.put("mode_entries_" + number + "_admin", "1"); + prop.put("mode_entries_" + number + "_admin_pageid",entry.key()); + } else { + prop.put("mode_entries_" + number + "_admin", "0"); + } + + return prop; + } +} diff --git a/htroot/BlogComments.java b/htroot/BlogComments.java index 6796ec62f..5da72e60b 100644 --- a/htroot/BlogComments.java +++ b/htroot/BlogComments.java @@ -1,391 +1,387 @@ -// Blog.java -// ----------------------- -// part of YACY -// (C) by Michael Peter Christen; mc@anomic.de -// first published on http://www.anomic.de -// Frankfurt, Germany, 2004 -// -// This File is contributed by Jan Sandbrink -// Contains contributions from Marc Nause [MN] -// last change: 06.05.2006 -// -// This program is free software; you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation; either version 2 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 General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program; if not, write to the Free Software -// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -// -// Using this software in any meaning (reading, learning, copying, compiling, -// running) means that you agree that the Author(s) is (are) not responsible -// for cost, loss of data or any harm that may be caused directly or indirectly -// by usage of this softare or this documentation. The usage of this software -// is on your own risk. The installation and usage (starting/running) of this -// software may allow other people or application to access your computer and -// any attached devices and is highly dependent on the configuration of the -// software which must be done by the user of the software; the author(s) is -// (are) also not responsible for proper configuration and usage of the -// software, even if provoked by documentation provided together with -// the software. -// -// Any changes to this file according to the GPL as documented in the file -// gpl.txt aside this file in the shipment you received can be done to the -// lines that follows this copyright notice here, but changes must not be -// done inside the copyright notive above. A re-distribution must contain -// the intact and unchanged copyright notice. -// Contributions and changes to the program code must be marked as such. - -// You must compile this file with -// javac -classpath .:../Classes Blacklist_p.java -// if the shell's current path is HTROOT - -import java.io.File; -import java.io.IOException; -import java.io.PrintWriter; -import java.io.UnsupportedEncodingException; -import java.text.SimpleDateFormat; -import java.util.Date; -import java.util.Iterator; - -import de.anomic.data.blogBoard; -import de.anomic.data.blogBoardComments; -import de.anomic.data.messageBoard; -import de.anomic.data.userDB; -import de.anomic.data.blogBoard.entry; -import de.anomic.http.httpHeader; -import de.anomic.plasma.plasmaSwitchboard; -import de.anomic.server.serverFileUtils; -import de.anomic.server.serverObjects; -import de.anomic.server.serverSwitch; -import de.anomic.server.logging.serverLog; -import de.anomic.yacy.yacyCore; - -public class BlogComments { - - private static SimpleDateFormat SimpleFormatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); - // TODO: make userdefined date/time-strings (localisation) - - public static String dateString(Date date) { - return SimpleFormatter.format(date); - } - - public static serverObjects respond(httpHeader header, serverObjects post, serverSwitch env) { - plasmaSwitchboard switchboard = (plasmaSwitchboard) env; - serverObjects prop = new serverObjects(); - blogBoard.entry page = null; - boolean hasRights = switchboard.verifyAuthentication(header, true); - - if (hasRights) prop.put("mode_admin", "1"); - else prop.put("mode_admin", "0"); - - if (post == null) { - post = new serverObjects(); - post.put("page", "blog_default"); - } - - if(!hasRights){ - userDB.Entry userentry = switchboard.userDB.proxyAuth((String)header.get("Authorization", "xxxxxx")); - if(userentry != null && userentry.hasRight(userDB.Entry.BLOG_RIGHT)){ - hasRights=true; - } - //opens login window if login link is clicked - contrib [MN] - else if(post.containsKey("login")){ - prop.put("AUTHENTICATE","admin log-in"); - } - } - - String pagename = post.get("page", "blog_default"); - String ip = post.get("CLIENTIP", "127.0.0.1"); - - String StrAuthor = post.get("author", "anonymous"); - - if (StrAuthor.equals("anonymous")) { - StrAuthor = switchboard.blogDB.guessAuthor(ip); - - if (StrAuthor == null || StrAuthor.length() == 0) { - if (de.anomic.yacy.yacyCore.seedDB.mySeed() == null) { - StrAuthor = "anonymous"; - } - else { - StrAuthor = de.anomic.yacy.yacyCore.seedDB.mySeed().get("Name", "anonymous"); - } - } - } - - byte[] author; - try { - author = StrAuthor.getBytes("UTF-8"); - } catch (UnsupportedEncodingException e) { - author = StrAuthor.getBytes(); - } - - page = switchboard.blogDB.read(pagename); //maybe "if(page == null)" - - // comments not allowed - if (page.getCommentMode() == 0) { - prop.put("mode_allow", 0); - } else { - prop.put("mode_allow", 1); - } - - if (post.containsKey("submit") && page.getCommentMode() != 0) { - // store a new/edited blog-entry - byte[] content; - if(!post.get("content", "").equals("")) - { - if(post.get("subject", "").equals("")) post.putHTML("subject", "no title"); - try { - content = post.get("content", "").getBytes("UTF-8"); - } catch (UnsupportedEncodingException e) { - content = post.get("content", "").getBytes(); - } - - Date date = null; - - //set name for new entry or date for old entry - String StrSubject = post.get("subject", ""); - byte[] subject; - try { - subject = StrSubject.getBytes("UTF-8"); - } catch (UnsupportedEncodingException e) { - subject = StrSubject.getBytes(); - } - String commentID = String.valueOf(System.currentTimeMillis()); - entry blogEntry = switchboard.blogDB.read(pagename); - blogEntry.addComment(commentID); - switchboard.blogDB.write(blogEntry); - switchboard.blogCommentDB.write(switchboard.blogCommentDB.newEntry(commentID, subject, author, ip, date, content)); - prop.put("LOCATION","BlogComments.html?page=" + pagename); - - messageBoard.entry msgEntry = null; - try { - switchboard.messageDB.write(msgEntry = switchboard.messageDB.newEntry( - "blogComment", - StrAuthor, - yacyCore.seedDB.mySeed().hash, - yacyCore.seedDB.mySeed().getName(), yacyCore.seedDB.mySeed().hash, - "new blog comment: " + new String(blogEntry.subject(),"UTF-8"), content)); - } catch (UnsupportedEncodingException e1) { - switchboard.messageDB.write(msgEntry = switchboard.messageDB.newEntry( - "blogComment", - StrAuthor, - yacyCore.seedDB.mySeed().hash, - yacyCore.seedDB.mySeed().getName(), yacyCore.seedDB.mySeed().hash, - "new blog comment: " + new String(blogEntry.subject()), content)); - } - - messageForwardingViaEmail(env, msgEntry); - - // finally write notification - File notifierSource = new File(switchboard.getRootPath(), switchboard.getConfig("htRootPath","htroot") + "/env/grafics/message.gif"); - File notifierDest = new File(switchboard.getConfigPath("htDocsPath", "DATA/HTDOCS"), "notifier.gif"); - try { - serverFileUtils.copy(notifierSource, notifierDest); - } catch (IOException e) { - serverLog.logSevere("MESSAGE", "NEW MESSAGE ARRIVED! (error: " + e.getMessage() + ")"); - - } - } - } - - if(hasRights && post.containsKey("delete") && post.containsKey("page") && post.containsKey("comment")) { - if(page.removeComment((String) post.get("comment"))) { - switchboard.blogCommentDB.delete((String) post.get("comment")); - } - } - - if(hasRights && post.containsKey("allow") && post.containsKey("page") && post.containsKey("comment")) { - blogBoardComments.CommentEntry entry = switchboard.blogCommentDB.read((String) post.get("comment")); - entry.allow(); - switchboard.blogCommentDB.write(entry); - } - - if(post.containsKey("preview") && page.getCommentMode() != 0) { - //preview the page - prop.put("mode", "1");//preview - prop.put("mode_pageid", pagename); - prop.put("mode_allow_pageid", pagename); - try { - prop.putHTML("mode_author", new String(author, "UTF-8")); - prop.putHTML("mode_allow_author", new String(author, "UTF-8")); - } catch (UnsupportedEncodingException e) { - prop.putHTML("mode_author", new String(author)); - prop.putHTML("mode_allow_author", new String(author)); - } - prop.putHTML("mode_subject", post.get("subject","")); - prop.put("mode_date", dateString(new Date())); - prop.putWiki("mode_page", post.get("content", "")); - prop.put("mode_page-code", post.get("content", "")); - } - else { - // show blog-entry/entries - prop.put("mode", "0"); //viewing - if(pagename.equals("blog_default")) { - prop.put("LOCATION","Blog.html"); - } - else { - //show 1 blog entry - prop.put("mode_pageid", page.key()); - prop.put("mode_allow_pageid", pagename); - try { - prop.putHTML("mode_subject", new String(page.subject(),"UTF-8")); - } catch (UnsupportedEncodingException e) { - prop.putHTML("mode_subject", new String(page.subject())); - } - try { - prop.putHTML("mode_author", new String(page.author(),"UTF-8")); - prop.putHTML("mode_allow_author", new String(author, "UTF-8")); - } catch (UnsupportedEncodingException e) { - prop.putHTML("mode_author", new String(page.author())); - prop.putHTML("mode_allow_author", new String(author)); - } - try { - prop.put("mode_comments", new String(page.commentsSize(),"UTF-8")); - } catch (UnsupportedEncodingException e) { - prop.put("mode_comments", new String(page.commentsSize())); - } - prop.put("mode_date", dateString(page.date())); - prop.putWiki("mode_page", page.page()); - if(hasRights) { - prop.put("mode_admin", "1"); - prop.put("mode_admin_pageid", page.key()); - } - //show all commments - try { - Iterator i = page.comments().iterator(); - int commentMode = page.getCommentMode(); - String pageid; - blogBoardComments.CommentEntry entry; - boolean xml = false; - if(post.containsKey("xml")) { - xml = true; - } - int count = 0; //counts how many entries are shown to the user - int start = post.getInt("start",0); //indicates from where entries should be shown - int num = post.getInt("num",10); //indicates how many entries should be shown - boolean prev = false; //indicates if there were previous comments to the ones that are dispalyed - if(xml) num = 0; - if (start < 1) start = 1; // dirrrty fix for incorrect comment count, need to find reason - if (start > 1) prev = true; - int nextstart = start+num; //indicates the starting offset for next results - int prevstart = start-num; //indicates the starting offset for previous results - while(i.hasNext() && count < num) { - - pageid = i.next(); - - if(start > 0) { - start--; - continue; - } - - entry = switchboard.blogCommentDB.read(pageid); - - if (commentMode == 2 && !hasRights && !entry.isAllowed()) - continue; - - prop.put("mode", "0"); - prop.put("mode_entries_"+count+"_pageid", entry.key()); - if(!xml) { - prop.putHTML("mode_entries_"+count+"_subject", new String(entry.subject(),"UTF-8")); - prop.putHTML("mode_entries_"+count+"_author", new String(entry.author(),"UTF-8")); - prop.putWiki("mode_entries_"+count+"_page", entry.page()); - } - else { - prop.putHTML("mode_entries_"+count+"_subject", new String(entry.subject(),"UTF-8")); - prop.putHTML("mode_entries_"+count+"_author", new String(entry.author(),"UTF-8")); - prop.put("mode_entries_"+count+"_page", entry.page()); - prop.put("mode_entries_"+count+"_timestamp", entry.timestamp()); - } - prop.put("mode_entries_"+count+"_date", dateString(entry.date())); - prop.put("mode_entries_"+count+"_ip", entry.ip()); - if(hasRights) { - prop.put("mode_entries_"+count+"_admin", "1"); - prop.put("mode_entries_"+count+"_admin_pageid", page.key()); - prop.put("mode_entries_"+count+"_admin_commentid", pageid); - if(!entry.isAllowed()) { - prop.put("mode_entries_"+count+"_admin_moderate", "1"); - prop.put("mode_entries_"+count+"_admin_moderate_pageid", page.key()); - prop.put("mode_entries_"+count+"_admin_moderate_commentid", pageid); - - } - } - else prop.put("mode_entries_"+count+"_admin", 0); - ++count; - } - prop.put("mode_entries", count); - if(i.hasNext()) { - prop.put("mode_moreentries", "1"); //more entries are availible - prop.put("mode_moreentries_start", nextstart); - prop.put("mode_moreentries_num", num); - prop.put("mode_moreentries_pageid", page.key()); - } - else prop.put("mode_moreentries", "0"); - if(prev) { - prop.put("mode_preventries", "1"); - if (prevstart < 0) prevstart = 0; - prop.put("mode_preventries_start", prevstart); - prop.put("mode_preventries_num", num); - prop.put("mode_preventries_pageid", page.key()); - } else prop.put("mode_preventries", "0"); - } catch (IOException e) { - - } - } - } - - // return rewrite properties - return prop; - } - - private static void messageForwardingViaEmail(serverSwitch env, messageBoard.entry msgEntry) { - try { - if (!Boolean.valueOf(env.getConfig("msgForwardingEnabled","false")).booleanValue()) return; - - // getting the recipient address - String sendMailTo = env.getConfig("msgForwardingTo","root@localhost").trim(); - - // getting the sendmail configuration - String sendMailStr = env.getConfig("msgForwardingCmd","/usr/bin/sendmail")+" "+sendMailTo; - String[] sendMail = sendMailStr.trim().split(" "); - - // building the message text - StringBuffer emailTxt = new StringBuffer(); - emailTxt.append("To: ") - .append(sendMailTo) - .append("\nFrom: ") - .append("yacy@") - .append(yacyCore.seedDB.mySeed().getName()) - .append("\nSubject: [YaCy] ") - .append(msgEntry.subject().replace('\n', ' ')) - .append("\nDate: ") - .append(msgEntry.date()) - .append("\n") - .append("\nMessage from: ") - .append(msgEntry.author()) - .append("/") - .append(msgEntry.authorHash()) - .append("\nMessage to: ") - .append(msgEntry.recipient()) - .append("/") - .append(msgEntry.recipientHash()) - .append("\nCategory: ") - .append(msgEntry.category()) - .append("\n===================================================================\n") - .append(new String(msgEntry.message())); - - Process process=Runtime.getRuntime().exec(sendMail); - PrintWriter email = new PrintWriter(process.getOutputStream()); - email.print(new String(emailTxt)); - email.close(); - } catch (Exception e) { - yacyCore.log.logWarning("message: message forwarding via email failed. ",e); - } - } -} +// Blog.java +// ----------------------- +// part of YACY +// (C) by Michael Peter Christen; mc@anomic.de +// first published on http://www.anomic.de +// Frankfurt, Germany, 2004 +// +// This File is contributed by Jan Sandbrink +// Contains contributions from Marc Nause [MN] +// last change: 06.05.2006 +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 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 General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// Using this software in any meaning (reading, learning, copying, compiling, +// running) means that you agree that the Author(s) is (are) not responsible +// for cost, loss of data or any harm that may be caused directly or indirectly +// by usage of this softare or this documentation. The usage of this software +// is on your own risk. The installation and usage (starting/running) of this +// software may allow other people or application to access your computer and +// any attached devices and is highly dependent on the configuration of the +// software which must be done by the user of the software; the author(s) is +// (are) also not responsible for proper configuration and usage of the +// software, even if provoked by documentation provided together with +// the software. +// +// Any changes to this file according to the GPL as documented in the file +// gpl.txt aside this file in the shipment you received can be done to the +// lines that follows this copyright notice here, but changes must not be +// done inside the copyright notive above. A re-distribution must contain +// the intact and unchanged copyright notice. +// Contributions and changes to the program code must be marked as such. + +// You must compile this file with +// javac -classpath .:../Classes Blacklist_p.java +// if the shell's current path is HTROOT + +import java.io.File; +import java.io.IOException; +import java.io.PrintWriter; +import java.io.UnsupportedEncodingException; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.Iterator; + +import de.anomic.data.blogBoard; +import de.anomic.data.blogBoardComments; +import de.anomic.data.messageBoard; +import de.anomic.data.userDB; +import de.anomic.data.blogBoard.entry; +import de.anomic.http.httpHeader; +import de.anomic.plasma.plasmaSwitchboard; +import de.anomic.server.serverFileUtils; +import de.anomic.server.serverObjects; +import de.anomic.server.serverSwitch; +import de.anomic.server.logging.serverLog; +import de.anomic.yacy.yacyCore; + +public class BlogComments { + + private static SimpleDateFormat SimpleFormatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); + // TODO: make userdefined date/time-strings (localisation) + + public static String dateString(Date date) { + return SimpleFormatter.format(date); + } + + public static serverObjects respond(httpHeader header, serverObjects post, serverSwitch env) { + plasmaSwitchboard switchboard = (plasmaSwitchboard) env; + serverObjects prop = new serverObjects(); + blogBoard.entry page = null; + boolean hasRights = switchboard.verifyAuthentication(header, true); + + if (hasRights) prop.put("mode_admin", "1"); + else prop.put("mode_admin", "0"); + + if (post == null) { + post = new serverObjects(); + post.put("page", "blog_default"); + } + + if(!hasRights){ + userDB.Entry userentry = switchboard.userDB.proxyAuth((String)header.get("Authorization", "xxxxxx")); + if(userentry != null && userentry.hasRight(userDB.Entry.BLOG_RIGHT)){ + hasRights=true; + } + //opens login window if login link is clicked - contrib [MN] + else if(post.containsKey("login")){ + prop.put("AUTHENTICATE","admin log-in"); + } + } + + String pagename = post.get("page", "blog_default"); + String ip = post.get("CLIENTIP", "127.0.0.1"); + + String StrAuthor = post.get("author", "anonymous"); + + if (StrAuthor.equals("anonymous")) { + StrAuthor = switchboard.blogDB.guessAuthor(ip); + + if (StrAuthor == null || StrAuthor.length() == 0) { + if (de.anomic.yacy.yacyCore.seedDB.mySeed() == null) { + StrAuthor = "anonymous"; + } + else { + StrAuthor = de.anomic.yacy.yacyCore.seedDB.mySeed().get("Name", "anonymous"); + } + } + } + + byte[] author; + try { + author = StrAuthor.getBytes("UTF-8"); + } catch (UnsupportedEncodingException e) { + author = StrAuthor.getBytes(); + } + + page = switchboard.blogDB.read(pagename); //maybe "if(page == null)" + + // comments not allowed + if (page.getCommentMode() == 0) { + prop.put("mode_allow", 0); + } else { + prop.put("mode_allow", 1); + } + + if (post.containsKey("submit") && page.getCommentMode() != 0) { + // store a new/edited blog-entry + byte[] content; + if(!post.get("content", "").equals("")) + { + if(post.get("subject", "").equals("")) post.putHTML("subject", "no title"); + try { + content = post.get("content", "").getBytes("UTF-8"); + } catch (UnsupportedEncodingException e) { + content = post.get("content", "").getBytes(); + } + + Date date = null; + + //set name for new entry or date for old entry + String StrSubject = post.get("subject", ""); + byte[] subject; + try { + subject = StrSubject.getBytes("UTF-8"); + } catch (UnsupportedEncodingException e) { + subject = StrSubject.getBytes(); + } + String commentID = String.valueOf(System.currentTimeMillis()); + entry blogEntry = switchboard.blogDB.read(pagename); + blogEntry.addComment(commentID); + switchboard.blogDB.write(blogEntry); + switchboard.blogCommentDB.write(switchboard.blogCommentDB.newEntry(commentID, subject, author, ip, date, content)); + prop.put("LOCATION","BlogComments.html?page=" + pagename); + + messageBoard.entry msgEntry = null; + try { + switchboard.messageDB.write(msgEntry = switchboard.messageDB.newEntry( + "blogComment", + StrAuthor, + yacyCore.seedDB.mySeed().hash, + yacyCore.seedDB.mySeed().getName(), yacyCore.seedDB.mySeed().hash, + "new blog comment: " + new String(blogEntry.subject(),"UTF-8"), content)); + } catch (UnsupportedEncodingException e1) { + switchboard.messageDB.write(msgEntry = switchboard.messageDB.newEntry( + "blogComment", + StrAuthor, + yacyCore.seedDB.mySeed().hash, + yacyCore.seedDB.mySeed().getName(), yacyCore.seedDB.mySeed().hash, + "new blog comment: " + new String(blogEntry.subject()), content)); + } + + messageForwardingViaEmail(env, msgEntry); + + // finally write notification + File notifierSource = new File(switchboard.getRootPath(), switchboard.getConfig("htRootPath","htroot") + "/env/grafics/message.gif"); + File notifierDest = new File(switchboard.getConfigPath("htDocsPath", "DATA/HTDOCS"), "notifier.gif"); + try { + serverFileUtils.copy(notifierSource, notifierDest); + } catch (IOException e) { + serverLog.logSevere("MESSAGE", "NEW MESSAGE ARRIVED! (error: " + e.getMessage() + ")"); + + } + } + } + + if(hasRights && post.containsKey("delete") && post.containsKey("page") && post.containsKey("comment")) { + if(page.removeComment((String) post.get("comment"))) { + switchboard.blogCommentDB.delete((String) post.get("comment")); + } + } + + if(hasRights && post.containsKey("allow") && post.containsKey("page") && post.containsKey("comment")) { + blogBoardComments.CommentEntry entry = switchboard.blogCommentDB.read((String) post.get("comment")); + entry.allow(); + switchboard.blogCommentDB.write(entry); + } + + if(post.containsKey("preview") && page.getCommentMode() != 0) { + //preview the page + prop.put("mode", "1");//preview + prop.put("mode_pageid", pagename); + prop.put("mode_allow_pageid", pagename); + try { + prop.putHTML("mode_author", new String(author, "UTF-8")); + prop.putHTML("mode_allow_author", new String(author, "UTF-8")); + } catch (UnsupportedEncodingException e) { + prop.putHTML("mode_author", new String(author)); + prop.putHTML("mode_allow_author", new String(author)); + } + prop.putHTML("mode_subject", post.get("subject","")); + prop.put("mode_date", dateString(new Date())); + prop.putWiki("mode_page", post.get("content", "")); + prop.put("mode_page-code", post.get("content", "")); + } + else { + // show blog-entry/entries + prop.put("mode", "0"); //viewing + if(pagename.equals("blog_default")) { + prop.put("LOCATION","Blog.html"); + } + else { + //show 1 blog entry + prop.put("mode_pageid", page.key()); + prop.put("mode_allow_pageid", pagename); + try { + prop.putHTML("mode_subject", new String(page.subject(),"UTF-8")); + } catch (UnsupportedEncodingException e) { + prop.putHTML("mode_subject", new String(page.subject())); + } + try { + prop.putHTML("mode_author", new String(page.author(),"UTF-8")); + prop.putHTML("mode_allow_author", new String(author, "UTF-8")); + } catch (UnsupportedEncodingException e) { + prop.putHTML("mode_author", new String(page.author())); + prop.putHTML("mode_allow_author", new String(author)); + } + prop.put("mode_comments", page.commentsSize()); + prop.put("mode_date", dateString(page.date())); + prop.putWiki("mode_page", page.page()); + if(hasRights) { + prop.put("mode_admin", "1"); + prop.put("mode_admin_pageid", page.key()); + } + //show all commments + try { + Iterator i = page.comments().iterator(); + int commentMode = page.getCommentMode(); + String pageid; + blogBoardComments.CommentEntry entry; + boolean xml = false; + if(post.containsKey("xml")) { + xml = true; + } + int count = 0; //counts how many entries are shown to the user + int start = post.getInt("start",0); //indicates from where entries should be shown + int num = post.getInt("num",10); //indicates how many entries should be shown + boolean prev = false; //indicates if there were previous comments to the ones that are dispalyed + if(xml) num = 0; + if (start < 0) start = 0; + if (start > 1) prev = true; + int nextstart = start+num; //indicates the starting offset for next results + int prevstart = start-num; //indicates the starting offset for previous results + while(i.hasNext() && count < num) { + + pageid = i.next(); + + if(start > 0) { + start--; + continue; + } + + entry = switchboard.blogCommentDB.read(pageid); + + if (commentMode == 2 && !hasRights && !entry.isAllowed()) + continue; + + prop.put("mode", "0"); + prop.put("mode_entries_"+count+"_pageid", entry.key()); + if(!xml) { + prop.putHTML("mode_entries_"+count+"_subject", new String(entry.subject(),"UTF-8")); + prop.putHTML("mode_entries_"+count+"_author", new String(entry.author(),"UTF-8")); + prop.putWiki("mode_entries_"+count+"_page", entry.page()); + } + else { + prop.putHTML("mode_entries_"+count+"_subject", new String(entry.subject(),"UTF-8")); + prop.putHTML("mode_entries_"+count+"_author", new String(entry.author(),"UTF-8")); + prop.put("mode_entries_"+count+"_page", entry.page()); + prop.put("mode_entries_"+count+"_timestamp", entry.timestamp()); + } + prop.put("mode_entries_"+count+"_date", dateString(entry.date())); + prop.put("mode_entries_"+count+"_ip", entry.ip()); + if(hasRights) { + prop.put("mode_entries_"+count+"_admin", "1"); + prop.put("mode_entries_"+count+"_admin_pageid", page.key()); + prop.put("mode_entries_"+count+"_admin_commentid", pageid); + if(!entry.isAllowed()) { + prop.put("mode_entries_"+count+"_admin_moderate", "1"); + prop.put("mode_entries_"+count+"_admin_moderate_pageid", page.key()); + prop.put("mode_entries_"+count+"_admin_moderate_commentid", pageid); + + } + } + else prop.put("mode_entries_"+count+"_admin", 0); + ++count; + } + prop.put("mode_entries", count); + if(i.hasNext()) { + prop.put("mode_moreentries", "1"); //more entries are availible + prop.put("mode_moreentries_start", nextstart); + prop.put("mode_moreentries_num", num); + prop.put("mode_moreentries_pageid", page.key()); + } + else prop.put("mode_moreentries", "0"); + if(prev) { + prop.put("mode_preventries", "1"); + if (prevstart < 0) prevstart = 0; + prop.put("mode_preventries_start", prevstart); + prop.put("mode_preventries_num", num); + prop.put("mode_preventries_pageid", page.key()); + } else prop.put("mode_preventries", "0"); + } catch (IOException e) { + + } + } + } + + // return rewrite properties + return prop; + } + + private static void messageForwardingViaEmail(serverSwitch env, messageBoard.entry msgEntry) { + try { + if (!Boolean.valueOf(env.getConfig("msgForwardingEnabled","false")).booleanValue()) return; + + // getting the recipient address + String sendMailTo = env.getConfig("msgForwardingTo","root@localhost").trim(); + + // getting the sendmail configuration + String sendMailStr = env.getConfig("msgForwardingCmd","/usr/bin/sendmail")+" "+sendMailTo; + String[] sendMail = sendMailStr.trim().split(" "); + + // building the message text + StringBuffer emailTxt = new StringBuffer(); + emailTxt.append("To: ") + .append(sendMailTo) + .append("\nFrom: ") + .append("yacy@") + .append(yacyCore.seedDB.mySeed().getName()) + .append("\nSubject: [YaCy] ") + .append(msgEntry.subject().replace('\n', ' ')) + .append("\nDate: ") + .append(msgEntry.date()) + .append("\n") + .append("\nMessage from: ") + .append(msgEntry.author()) + .append("/") + .append(msgEntry.authorHash()) + .append("\nMessage to: ") + .append(msgEntry.recipient()) + .append("/") + .append(msgEntry.recipientHash()) + .append("\nCategory: ") + .append(msgEntry.category()) + .append("\n===================================================================\n") + .append(new String(msgEntry.message())); + + Process process=Runtime.getRuntime().exec(sendMail); + PrintWriter email = new PrintWriter(process.getOutputStream()); + email.print(new String(emailTxt)); + email.close(); + } catch (Exception e) { + yacyCore.log.logWarning("message: message forwarding via email failed. ",e); + } + } +} diff --git a/source/de/anomic/data/blogBoard.java b/source/de/anomic/data/blogBoard.java index 14b6f1c68..6e6225ff9 100644 --- a/source/de/anomic/data/blogBoard.java +++ b/source/de/anomic/data/blogBoard.java @@ -1,348 +1,344 @@ -// wikiBoard.java -// ------------------------------------- -// (C) by Michael Peter Christen; mc@anomic.de -// first published on http://www.anomic.de -// Frankfurt, Germany, 2004 -// last major change: 20.07.2004 -// -// This program is free software; you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation; either version 2 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 General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program; if not, write to the Free Software -// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -// -// Using this software in any meaning (reading, learning, copying, compiling, -// running) means that you agree that the Author(s) is (are) not responsible -// for cost, loss of data or any harm that may be caused directly or indirectly -// by usage of this softare or this documentation. The usage of this software -// is on your own risk. The installation and usage (starting/running) of this -// software may allow other people or application to access your computer and -// any attached devices and is highly dependent on the configuration of the -// software which must be done by the user of the software; the author(s) is -// (are) also not responsible for proper configuration and usage of the -// software, even if provoked by documentation provided together with -// the software. -// -// Any changes to this file according to the GPL as documented in the file -// gpl.txt aside this file in the shipment you received can be done to the -// lines that follows this copyright notice here, but changes must not be -// done inside the copyright notive above. A re-distribution must contain -// the intact and unchanged copyright notice. -// Contributions and changes to the program code must be marked as such. - -// This file is contributed by Jan Sandbrink -// based on the Code of wikiBoard.java - -package de.anomic.data; - -import java.io.ByteArrayInputStream; -import java.io.File; -import java.io.IOException; -import java.io.UnsupportedEncodingException; -import java.text.ParseException; -import java.util.ArrayList; -import java.util.Date; -import java.util.HashMap; -import java.util.Iterator; - -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; -import javax.xml.parsers.ParserConfigurationException; - -import org.w3c.dom.Document; -import org.w3c.dom.Node; -import org.w3c.dom.NodeList; -import org.xml.sax.SAXException; - -import de.anomic.kelondro.kelondroBase64Order; -import de.anomic.kelondro.kelondroDyn; -import de.anomic.kelondro.kelondroMapObjects; -import de.anomic.kelondro.kelondroNaturalOrder; -import de.anomic.server.serverDate; - -public class blogBoard { - - public static final int keyLength = 64; - private static final int recordSize = 512; - - private kelondroMapObjects datbase = null; - - public blogBoard(File actpath, long preloadTime) { - new File(actpath.getParent()).mkdir(); - if (datbase == null) { - datbase = new kelondroMapObjects(new kelondroDyn(actpath, true, true, preloadTime, keyLength, recordSize, '_', kelondroNaturalOrder.naturalOrder, true, false, false), 500); - } - } - - public int size() { - return datbase.size(); - } - - public void close() { - datbase.close(); - } - - private static String normalize(String key) { - if (key == null) return "null"; - return key.trim().toLowerCase(); - } - - public static String webalize(String key) { - if (key == null) return "null"; - key = key.trim().toLowerCase(); - int p; - while ((p = key.indexOf(" ")) >= 0) - key = key.substring(0, p) + "%20" + key.substring(p +1); - return key; - } - - public String guessAuthor(String ip) { - return wikiBoard.guessAuthor(ip); - } - - public entry newEntry(String key, byte[] subject, byte[] author, String ip, Date date, byte[] page, ArrayList comments, String commentMode) { - return new entry(normalize(key), subject, author, ip, date, page, comments, commentMode); - } - - public class entry { - - String key; - HashMap record; - - public entry(String nkey, byte[] subject, byte[] author, String ip, Date date, byte[] page, ArrayList comments, String commentMode) { - record = new HashMap(); - key = nkey; - if (key.length() > keyLength) key = key.substring(0, keyLength); - if(date == null) date = new Date(); - record.put("date", serverDate.formatShortSecond(date)); - if (subject == null) record.put("subject",""); - else record.put("subject", kelondroBase64Order.enhancedCoder.encode(subject)); - if (author == null) record.put("author",""); - else record.put("author", kelondroBase64Order.enhancedCoder.encode(author)); - if ((ip == null) || (ip.length() == 0)) ip = ""; - record.put("ip", ip); - if (page == null) record.put("page", ""); - else record.put("page", kelondroBase64Order.enhancedCoder.encode(page)); - if (comments == null) record.put("comments", listManager.collection2string(new ArrayList())); - else record.put("comments", listManager.collection2string(comments)); - if (commentMode == null) record.put("commentMode", "1"); - else record.put("commentMode", commentMode); - - wikiBoard.setAuthor(ip, new String(author)); - //System.out.println("DEBUG: setting author " + author + " for ip = " + ip + ", authors = " + authors.toString()); - } - - private entry(String key, HashMap record) { - this.key = key; - this.record = record; - if (this.record.get("comments")==null) this.record.put("comments", listManager.collection2string(new ArrayList())); - if (this.record.get("commentMode")==null || this.record.get("commentMode").equals("")) this.record.put("commentMode", "1"); - } - - public String key() { - return key; - } - - public byte[] subject() { - String m = record.get("subject"); - if (m == null) return new byte[0]; - byte[] b = kelondroBase64Order.enhancedCoder.decode(m, "de.anomic.data.blogBoard.subject()"); - if (b == null) return "".getBytes(); - return b; - } - - public Date date() { - try { - String c = record.get("date"); - if (c == null) { - System.out.println("DEBUG - ERROR: date field missing in blogBoard"); - return new Date(); - } - return serverDate.parseShortSecond(c); - } catch (ParseException e) { - return new Date(); - } - } - - public String timestamp() { - String c = record.get("date"); - if (c == null) { - System.out.println("DEBUG - ERROR: date field missing in blogBoard"); - return serverDate.formatShortSecond(); - } - return c; - } - - public byte[] author() { - String m = record.get("author"); - if (m == null) return new byte[0]; - byte[] b = kelondroBase64Order.enhancedCoder.decode(m, "de.anomic.data.blogBoard.author()"); - if (b == null) return "".getBytes(); - return b; - } - - public byte[] commentsSize() { - ArrayList m = listManager.string2arraylist(record.get("comments")); - if (m == null) return new byte[0]; - byte[] b = Integer.toString(m.size()).getBytes(); - if (b == null) return "".getBytes(); - return b; - } - - public ArrayList comments() { - ArrayList m = listManager.string2arraylist(record.get("comments")); - if (m == null) return new ArrayList(); - return m; - } - - public String ip() { - String a = record.get("ip"); - if (a == null) return "127.0.0.1"; - return a; - } - - public byte[] page() { - String m = record.get("page"); - if (m == null) return new byte[0]; - byte[] b = kelondroBase64Order.enhancedCoder.decode(m, "de.anomic.data.blogBoard.page()"); - if (b == null) return "".getBytes(); - return b; - } - - public void addComment(String commentID) { - ArrayList comments = listManager.string2arraylist(record.get("comments")); - comments.add(commentID); - record.put("comments", listManager.collection2string(comments)); - } - - public boolean removeComment(String commentID) { - ArrayList comments = listManager.string2arraylist(record.get("comments")); - boolean success = comments.remove(commentID); - record.put("comments", listManager.collection2string(comments)); - return success; - } - - public int getCommentMode(){ - return Integer.parseInt(record.get("commentMode")); - } - } - - public String write(entry page) { - // writes a new page and returns key - try { - datbase.set(page.key, page.record); - return page.key; - } catch (IOException e) { - return null; - } - } - - public entry read(String key) { - return read(key, datbase); - } - - private entry read(String key, kelondroMapObjects base) { - key = normalize(key); - if (key.length() > keyLength) key = key.substring(0, keyLength); - HashMap record = base.getMap(key); - if (record == null) return newEntry(key, "".getBytes(), "anonymous".getBytes(), "127.0.0.1", new Date(), "".getBytes(), null, null); - return new entry(key, record); - } - - public boolean importXML(String input) { - DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); - try { - DocumentBuilder builder = factory.newDocumentBuilder(); - Document doc = builder.parse(new ByteArrayInputStream(input.getBytes())); - return parseXMLimport(doc); - } catch (ParserConfigurationException e) { - } catch (SAXException e) { - } catch (IOException e) {} - - return false; - } - - private boolean parseXMLimport(Document doc) { - if(!doc.getDocumentElement().getTagName().equals("blog")) - return false; - - NodeList items = doc.getDocumentElement().getElementsByTagName("item"); - if(items.getLength() == 0) - return false; - - for(int i=0;i keys(boolean up) throws IOException { - return datbase.keys(up, false); - } - -} +// wikiBoard.java +// ------------------------------------- +// (C) by Michael Peter Christen; mc@anomic.de +// first published on http://www.anomic.de +// Frankfurt, Germany, 2004 +// last major change: 20.07.2004 +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 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 General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// Using this software in any meaning (reading, learning, copying, compiling, +// running) means that you agree that the Author(s) is (are) not responsible +// for cost, loss of data or any harm that may be caused directly or indirectly +// by usage of this softare or this documentation. The usage of this software +// is on your own risk. The installation and usage (starting/running) of this +// software may allow other people or application to access your computer and +// any attached devices and is highly dependent on the configuration of the +// software which must be done by the user of the software; the author(s) is +// (are) also not responsible for proper configuration and usage of the +// software, even if provoked by documentation provided together with +// the software. +// +// Any changes to this file according to the GPL as documented in the file +// gpl.txt aside this file in the shipment you received can be done to the +// lines that follows this copyright notice here, but changes must not be +// done inside the copyright notive above. A re-distribution must contain +// the intact and unchanged copyright notice. +// Contributions and changes to the program code must be marked as such. + +// This file is contributed by Jan Sandbrink +// based on the Code of wikiBoard.java + +package de.anomic.data; + +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.text.ParseException; +import java.util.ArrayList; +import java.util.Date; +import java.util.HashMap; +import java.util.Iterator; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; + +import org.w3c.dom.Document; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.xml.sax.SAXException; + +import de.anomic.kelondro.kelondroBase64Order; +import de.anomic.kelondro.kelondroDyn; +import de.anomic.kelondro.kelondroMapObjects; +import de.anomic.kelondro.kelondroNaturalOrder; +import de.anomic.server.serverDate; + +public class blogBoard { + + public static final int keyLength = 64; + private static final int recordSize = 512; + + private kelondroMapObjects datbase = null; + + public blogBoard(File actpath, long preloadTime) { + new File(actpath.getParent()).mkdir(); + if (datbase == null) { + datbase = new kelondroMapObjects(new kelondroDyn(actpath, true, true, preloadTime, keyLength, recordSize, '_', kelondroNaturalOrder.naturalOrder, true, false, false), 500); + } + } + + public int size() { + return datbase.size(); + } + + public void close() { + datbase.close(); + } + + private static String normalize(String key) { + if (key == null) return "null"; + return key.trim().toLowerCase(); + } + + public static String webalize(String key) { + if (key == null) return "null"; + key = key.trim().toLowerCase(); + int p; + while ((p = key.indexOf(" ")) >= 0) + key = key.substring(0, p) + "%20" + key.substring(p +1); + return key; + } + + public String guessAuthor(String ip) { + return wikiBoard.guessAuthor(ip); + } + + public entry newEntry(String key, byte[] subject, byte[] author, String ip, Date date, byte[] page, ArrayList comments, String commentMode) { + return new entry(normalize(key), subject, author, ip, date, page, comments, commentMode); + } + + public class entry { + + String key; + HashMap record; + + public entry(String nkey, byte[] subject, byte[] author, String ip, Date date, byte[] page, ArrayList comments, String commentMode) { + record = new HashMap(); + key = nkey; + if (key.length() > keyLength) key = key.substring(0, keyLength); + if(date == null) date = new Date(); + record.put("date", serverDate.formatShortSecond(date)); + if (subject == null) record.put("subject",""); + else record.put("subject", kelondroBase64Order.enhancedCoder.encode(subject)); + if (author == null) record.put("author",""); + else record.put("author", kelondroBase64Order.enhancedCoder.encode(author)); + if ((ip == null) || (ip.length() == 0)) ip = ""; + record.put("ip", ip); + if (page == null) record.put("page", ""); + else record.put("page", kelondroBase64Order.enhancedCoder.encode(page)); + if (comments == null) record.put("comments", listManager.collection2string(new ArrayList())); + else record.put("comments", listManager.collection2string(comments)); + if (commentMode == null) record.put("commentMode", "1"); + else record.put("commentMode", commentMode); + + wikiBoard.setAuthor(ip, new String(author)); + //System.out.println("DEBUG: setting author " + author + " for ip = " + ip + ", authors = " + authors.toString()); + } + + private entry(String key, HashMap record) { + this.key = key; + this.record = record; + if (this.record.get("comments")==null) this.record.put("comments", listManager.collection2string(new ArrayList())); + if (this.record.get("commentMode")==null || this.record.get("commentMode").equals("")) this.record.put("commentMode", "1"); + } + + public String key() { + return key; + } + + public byte[] subject() { + String m = record.get("subject"); + if (m == null) return new byte[0]; + byte[] b = kelondroBase64Order.enhancedCoder.decode(m, "de.anomic.data.blogBoard.subject()"); + if (b == null) return "".getBytes(); + return b; + } + + public Date date() { + try { + String c = record.get("date"); + if (c == null) { + System.out.println("DEBUG - ERROR: date field missing in blogBoard"); + return new Date(); + } + return serverDate.parseShortSecond(c); + } catch (ParseException e) { + return new Date(); + } + } + + public String timestamp() { + String c = record.get("date"); + if (c == null) { + System.out.println("DEBUG - ERROR: date field missing in blogBoard"); + return serverDate.formatShortSecond(); + } + return c; + } + + public byte[] author() { + String m = record.get("author"); + if (m == null) return new byte[0]; + byte[] b = kelondroBase64Order.enhancedCoder.decode(m, "de.anomic.data.blogBoard.author()"); + if (b == null) return "".getBytes(); + return b; + } + + public int commentsSize() { + ArrayList m = listManager.string2arraylist(record.get("comments")); + return m.size(); + } + + public ArrayList comments() { + ArrayList m = listManager.string2arraylist(record.get("comments")); + return m; + } + + public String ip() { + String a = record.get("ip"); + if (a == null) return "127.0.0.1"; + return a; + } + + public byte[] page() { + String m = record.get("page"); + if (m == null) return new byte[0]; + byte[] b = kelondroBase64Order.enhancedCoder.decode(m, "de.anomic.data.blogBoard.page()"); + if (b == null) return "".getBytes(); + return b; + } + + public void addComment(String commentID) { + ArrayList comments = listManager.string2arraylist(record.get("comments")); + comments.add(commentID); + record.put("comments", listManager.collection2string(comments)); + } + + public boolean removeComment(String commentID) { + ArrayList comments = listManager.string2arraylist(record.get("comments")); + boolean success = comments.remove(commentID); + record.put("comments", listManager.collection2string(comments)); + return success; + } + + public int getCommentMode(){ + return Integer.parseInt(record.get("commentMode")); + } + } + + public String write(entry page) { + // writes a new page and returns key + try { + datbase.set(page.key, page.record); + return page.key; + } catch (IOException e) { + return null; + } + } + + public entry read(String key) { + return read(key, datbase); + } + + private entry read(String key, kelondroMapObjects base) { + key = normalize(key); + if (key.length() > keyLength) key = key.substring(0, keyLength); + HashMap record = base.getMap(key); + if (record == null) return newEntry(key, "".getBytes(), "anonymous".getBytes(), "127.0.0.1", new Date(), "".getBytes(), null, null); + return new entry(key, record); + } + + public boolean importXML(String input) { + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + try { + DocumentBuilder builder = factory.newDocumentBuilder(); + Document doc = builder.parse(new ByteArrayInputStream(input.getBytes())); + return parseXMLimport(doc); + } catch (ParserConfigurationException e) { + } catch (SAXException e) { + } catch (IOException e) {} + + return false; + } + + private boolean parseXMLimport(Document doc) { + if(!doc.getDocumentElement().getTagName().equals("blog")) + return false; + + NodeList items = doc.getDocumentElement().getElementsByTagName("item"); + if(items.getLength() == 0) + return false; + + for(int i=0;i keys(boolean up) throws IOException { + return datbase.keys(up, false); + } + +} diff --git a/source/de/anomic/data/listManager.java b/source/de/anomic/data/listManager.java index 32db94c6e..cd2ff0b50 100644 --- a/source/de/anomic/data/listManager.java +++ b/source/de/anomic/data/listManager.java @@ -327,7 +327,7 @@ public class listManager { public static ArrayList string2arraylist(String string){ ArrayList l; - if (string != null) { + if (string != null && string.length() > 0) { l = new ArrayList(Arrays.asList(string.split(","))); } else { l = new ArrayList();