001: /*
002: * Created on Feb 18, 2005
003: */
004: package org.openedit.blog.modules;
005:
006: import java.io.StringWriter;
007: import java.io.Writer;
008: import java.util.HashMap;
009: import java.util.List;
010: import java.util.Map;
011:
012: import org.apache.commons.logging.Log;
013: import org.apache.commons.logging.LogFactory;
014: import org.openedit.blog.archive.BlogArchive;
015: import org.openedit.repository.filesystem.StringItem;
016:
017: import com.openedit.OpenEditException;
018: import com.openedit.WebPageRequest;
019: import com.openedit.blog.Blog;
020: import com.openedit.blog.BlogCommentNotification;
021: import com.openedit.blog.BlogEntry;
022: import com.openedit.blog.Comment;
023: import com.openedit.modules.BaseModule;
024: import com.openedit.modules.admin.users.Question;
025: import com.openedit.modules.html.EditorSession;
026: import com.openedit.page.Page;
027: import com.openedit.users.User;
028: import com.openedit.users.filesystem.FileSystemUser;
029: import com.sun.syndication.feed.synd.SyndFeed;
030: import com.sun.syndication.feed.synd.SyndFeedImpl;
031: import com.sun.syndication.io.SyndFeedOutput;
032:
033: /**
034: * @author cburkey
035: *
036: */
037: public class BlogModule extends BaseModule {
038: protected Map fieldBlogs;
039: private static final Log log = LogFactory.getLog(BlogModule.class);
040: protected BlogCommentNotification fieldCommentNotification;
041:
042: public Blog getBlog(WebPageRequest req) throws OpenEditException {
043: Page inPath = req.getPage();
044: String home = inPath.get("bloghome");
045:
046: if (home == null) {
047: Blog blog = (Blog) req.getPageValue("blog"); //already loaded once
048: if (blog != null) {
049: return blog;
050: }
051: throw new OpenEditException(
052: "Need to define bloghome page property");
053: }
054: Blog blog = getBlog(home);
055: req.putPageValue("blog", blog);
056: req.putPageValue("bloghome", blog.getBlogHome());
057: return blog;
058: }
059:
060: public Blog getBlog(String home) throws OpenEditException {
061:
062: if (home.endsWith("/")) //This will probably not happen
063: {
064: home = home.substring(0, home.length() - 1);
065: }
066: Blog blog = (Blog) getBlogs().get(home);
067: BlogArchive archive = getArchive(home + "/index.html");
068: boolean changed = false;
069: if (blog == null) {
070: changed = true;
071: } else {
072: //check the time stamp on the link file
073: changed = archive.hasChanged(blog);
074: }
075:
076: if (changed) {
077: blog = new Blog();
078: blog.setBlogHome(home);
079: archive.loadBlog(blog);
080: getBlogs().put(home, blog);
081: }
082: return blog;
083: }
084:
085: public void writeBlogSettings(WebPageRequest inReq)
086: throws Exception {
087: Blog blog = getBlog(inReq);
088: Page settings = getPageManager().getPage(
089: blog.getBlogHome() + "/blogsettings.xml");
090: //read in some XML
091:
092: String parameter = inReq.getRequestParameter("blogtitle");
093: blog.setTitle(parameter);
094: parameter = inReq.getRequestParameter("bloghostname");
095: blog.setHostName(parameter);
096: parameter = inReq.getRequestParameter("blogauthor");
097: blog.setAuthor(parameter);
098: parameter = inReq.getRequestParameter("blogdescription");
099: blog.setDescription(parameter);
100: parameter = inReq.getRequestParameter("bloganonymous");
101: if (parameter == null) {
102: parameter = "false";
103: }
104: blog.setAllowAnonymous("true".equals(parameter));
105:
106: parameter = inReq.getRequestParameter("blogautopublishentries");
107: blog.setAutoPublishEntries(Boolean.parseBoolean(parameter));
108:
109: parameter = inReq
110: .getRequestParameter("blogautopublishcomments");
111: blog.setAutoPublishingComments(Boolean.parseBoolean(parameter));
112:
113: StringWriter writer = new StringWriter();
114: try {
115: new BlogArchive().saveBlog(blog, writer, settings
116: .getCharacterEncoding());
117: } finally {
118: writer.close();
119: }
120:
121: // lets write to a file
122: StringItem item = new StringItem(settings.getPath(), writer
123: .toString(), settings.getCharacterEncoding());
124: item.setMessage("Edited blog settings via admin interface");
125: item.setAuthor(inReq.getUser().getUserName());
126: settings.setContentItem(item);
127: getPageManager().putPage(settings);
128: }
129:
130: public void generateFeed(WebPageRequest req) throws Exception {
131: SyndFeed feed = new SyndFeedImpl();
132: String feedtype = req.getPage().get("feedtype");
133: if (feedtype == null) {
134: feedtype = "atom_0.3";
135: }
136: feed.setFeedType(feedtype);
137:
138: Blog blog = getBlog(req);
139:
140: feed.setTitle(blog.getTitle());
141: feed.setLink(blog.getHostName());
142: feed.setDescription(blog.getDescription());
143:
144: List recent = blog.getRecentEntries();
145: feed.setEntries(recent);
146:
147: Writer writer = new StringWriter();
148: SyndFeedOutput output = new SyndFeedOutput();
149: output.output(feed, writer);
150: writer.close();
151: req.putPageValue("feedresults", writer.toString());
152: }
153:
154: public Map getBlogs() {
155: if (fieldBlogs == null) {
156: fieldBlogs = new HashMap();
157: }
158: return fieldBlogs;
159: }
160:
161: public void setBlogs(Map inBlogs) {
162: fieldBlogs = inBlogs;
163: }
164:
165: /**
166: * @param inReq
167: */
168: public BlogEntry getEntry(WebPageRequest inReq)
169: throws OpenEditException {
170: Blog blog = getBlog(inReq);
171: String entryId = inReq.getRequestParameter("entryId");
172: BlogEntry entry = blog.getEntry(entryId);
173: inReq.putPageValue("entry", entry);
174: return entry;
175: }
176:
177: public void editEntry(WebPageRequest inReq) throws Exception {
178: Blog blog = getBlog(inReq);
179: String entryId = inReq.getRequestParameter("entryId");
180: BlogEntry entry = blog.getEntry(entryId);
181: inReq.putPageValue("blog", blog);
182: inReq.putSessionValue("entry", entry);
183: inReq.setRequestParameter("editPath", entry.getPath());
184: inReq.setRequestParameter("originalPath", blog.getBlogHome());
185:
186: }
187:
188: public void loadPermalink(WebPageRequest inReq) throws Exception {
189: //some page is being requested
190: Blog blog = getBlog(inReq);
191: String path = inReq.getPath();
192:
193: //get entry will check two places for the path.
194: BlogEntry entry = blog.getEntry(path);
195: if (entry != null) {
196: inReq.putPageValue("entry", entry);
197: } else {
198: log.error("Null entry" + path);
199: }
200: }
201:
202: // public void loadLink(WebPageRequest inReq) throws Exception
203: // {
204: // //some page is being requested
205: // String path = inReq.getPath();
206: // Blog blog = getBlog(inReq);
207: // String id = path.substring(blog.getArchiveRootDirectory().length(),path.length());
208: // id = blog.getBlogHome() + id;
209: // BlogEntry entry = blog.getEntry(id);
210: // inReq.putPageValue("entry",entry);
211: // }
212: public void loadAdminLink(WebPageRequest inReq) throws Exception {
213: //some page is being requested
214: String path = inReq.getPath();
215: Blog blog = getBlog(inReq);
216: path = "admin/" + path;
217: BlogEntry entry = blog.getEntry(path);
218: inReq.putPageValue("entry", entry);
219: }
220:
221: /**
222: * @param inReq
223: */
224: public void addNewEntry(WebPageRequest inReq) throws Exception {
225: Blog blog = getBlog(inReq);
226: BlogEntry entry = blog.createNewEntry(inReq.getUser());
227:
228: inReq.putPageValue("blog", blog);
229: inReq.putSessionValue("entry", entry);
230:
231: inReq.setRequestParameter("editPath", entry.getPath());
232: inReq.setRequestParameter("originalURL", blog.getBlogHome());
233:
234: }
235:
236: public void saveEntry(WebPageRequest inReq) throws Exception {
237: Blog blog = getBlog(inReq);
238: if (!blog.canEdit(inReq.getUser())) {
239: throw new OpenEditException("User cannot edit blog");
240: }
241:
242: BlogEntry entry = (BlogEntry) inReq.getSessionValue("entry");
243: if (entry == null) {
244: entry = blog.createNewEntry(inReq.getUser());
245: }
246:
247: //inReq.getPageStreamer().entry.getLink();
248:
249: String content = inReq.getRequestParameter("content");
250: if (content != null) {
251: EditorSession session = new EditorSession();
252: content = session.stripBody(content);
253: //atom does not like XHTML entities NEW VERSION FIXES THIS
254: //content.replaceAll("™","™");
255: //content.replaceAll("®","®");
256: //content.replaceAll("©","©");
257: //content.replaceAll(" "," ");
258:
259: //replace
260: /*
261: //<!ENTITY sharp "#">
262: <!ENTITY trade "™">
263: <!ENTITY reg "®">
264: <!ENTITY copy "©">
265: <!ENTITY nbsp " ">
266: */
267:
268: }
269:
270: String author = inReq.getRequestParameter("author");
271: entry.setAuthor(author);
272: String title = inReq.getRequestParameter("title");
273: entry.setTitle(title);
274: entry.setDescription(content);
275:
276: String[] properties = inReq.getRequestParameters("property");
277: if (properties != null) {
278: for (int i = 0; i < properties.length; i++) {
279: String value = inReq.getRequestParameter(properties[i]
280: + ".value");
281: if (value != null) {
282: entry.addProperty(properties[i], value);
283: }
284: }
285: }
286: entry.setVisible(blog.isAutoPublishEntries());
287:
288: BlogArchive archive = getArchive(inReq.getPath());
289:
290: archive.saveEntry(blog, entry);
291: archive.getEntryArchive().saveLinks(blog);
292: }
293:
294: public void removeEntry(WebPageRequest inReq) throws Exception {
295: Blog blog = getBlog(inReq);
296:
297: if (!blog.canEdit(inReq.getUser())) {
298: throw new OpenEditException("User cannot edit blog");
299: }
300:
301: String entryId = inReq.getRequestParameter("entryId");
302: BlogEntry entry = blog.getEntry(entryId);
303:
304: blog.removeEntry(entry);
305: BlogArchive archive = getArchive(blog.getBlogHome());
306: archive.getEntryArchive().saveLinks(blog);
307: }
308:
309: protected BlogArchive getArchive(String inPath)
310: throws OpenEditException {
311: Page path = getPageManager().getPage(inPath);
312: return getArchive(path);
313: }
314:
315: protected BlogArchive getArchive(Page inPath)
316: throws OpenEditException {
317: //get the bloghome archive id
318: String name = inPath.getProperty("blogarchivename");
319: if (name == null) {
320: name = "BlogArchive";
321: }
322: BlogArchive archive = (BlogArchive) getBeanFactory().getBean(
323: name);
324: return archive;
325: }
326:
327: /**
328: * @param inReq
329: */
330: public void addNewComment(WebPageRequest inReq) throws Exception {
331: String content = inReq.getRequestParameter("content");
332: String entryId = inReq.getRequestParameter("entryId");
333: if (content == null || entryId == null) {
334: return;
335: }
336: Blog blog = getBlog(inReq);
337: if (checkQuestion(inReq)) {
338: BlogEntry entry = blog.getEntry(entryId);
339:
340: String author = inReq.getRequestParameter("username");
341: User user = getUserManager().getUser(author);
342: if (user == null) {
343: if ("anonymous".equals(author)
344: && blog.getAllowAnonymous()) {
345: user = new FileSystemUser();
346: user.setUserName(author);
347: } else {
348: throw new OpenEditException(
349: "Anonymous user not allowed");
350: }
351: }
352: Comment comment = blog.createNewComment(user, content);
353: entry.addComment(comment);
354: BlogArchive archive = getArchive(inReq.getPage());
355: archive.getEntryArchive().getCommentArchive().saveComments(
356: blog, entry);
357: getCommentNotification().commentAdded(inReq, blog, entry,
358: comment);
359: }
360: //inReq.removeSessionValue("question");
361: }
362:
363: public boolean checkQuestion(WebPageRequest inReq)
364: throws OpenEditException {
365: if (inReq.getSessionValue("answer") != null
366: || inReq.getUser() != null) {
367: return true;
368: }
369:
370: String answer = inReq.getRequestParameter("answerid");
371: Blog blog = getBlog(inReq);
372: Question q = (Question) inReq.getSessionValue("question");
373: if (q == null || !q.checkAnswer(answer)) {
374: inReq.redirect(blog.getBlogHome() + "/questionerror.html");
375: // //inReq.putPageValue("error", "Answer did not match this session.");
376: return false;
377: }
378: inReq.putSessionValue("answer", answer);
379: return true;
380: }
381:
382: public void removeComment(WebPageRequest inReq) throws Exception {
383: Blog blog = getBlog(inReq);
384: String entryId = inReq.getRequestParameter("entryId");
385: BlogEntry entry = blog.getEntry(entryId);
386: String commentId = inReq.getRequestParameter("commentId");
387: Comment comment = entry.getComment(commentId);
388: User user = (User) inReq.getSessionValue("user");
389:
390: if (comment.canEdit(user)) {
391: entry.removeComment(comment);
392: BlogArchive archive = getArchive(inReq.getPage());
393: archive.getEntryArchive().getCommentArchive().saveComments(
394: blog, entry);
395: }
396: redirectToOrig(inReq);
397: }
398:
399: public void changeCommentVisibility(WebPageRequest inReq)
400: throws Exception {
401: String entryId = inReq.getRequestParameter("entryId");
402: if (entryId != null) {
403: Blog blog = getBlog(inReq);
404: String commentId = inReq.getRequestParameter("commentId");
405: BlogEntry entry = blog.getEntry(entryId);
406: Comment comment = entry.getComment(commentId);
407: if (comment != null) {
408: if (comment.canEdit(inReq.getUser())
409: || blog.canEdit(inReq.getUser())) {
410: comment.setVisible(!comment.isVisible());
411: BlogArchive archive = getArchive(inReq.getPage());
412: archive.getEntryArchive().getCommentArchive()
413: .saveComments(blog, entry);
414: }
415: }
416: redirectToOrig(inReq);
417: }
418: }
419:
420: private void redirectToOrig(WebPageRequest inReq) {
421: String origURL = inReq.getRequestParameter("origURL");
422: if (origURL != null && origURL.length() > 0) {
423: inReq.redirect(origURL);
424: }
425: }
426:
427: public void login(WebPageRequest inReq) throws Exception {
428: String username = inReq.getRequestParameter("username");
429: User user = getUserManager().getUser(username);
430: String password = inReq.getRequestParameter("password");
431: if (getUserManager().authenticate(user, password)) {
432: inReq.putSessionValue("user", user);
433: }
434: }
435:
436: /*
437: public void registerNewUser(WebPageRequest inReq) throws Exception
438: {
439: //loop over all the properties
440: String email = inReq.getRequestParameter("property-email");
441: User user = getUserManager().getUserByEmail(email);
442: if ( user != null)
443: {
444: inReq.putPageValue("oe-error","Duplicate user. email address already in system");
445: return;
446: }
447: String password = inReq.getRequestParameter("password1");
448: String password2 = inReq.getRequestParameter("password2");
449: if ( password == null || password2 == null || !password.equals(password2) || password.length() == 0)
450: {
451: inReq.putPageValue("oe-error","Passwords do not match");
452: return;
453: }
454: User newuser = getUserManager().createUser(null,password);
455: for (Iterator iter = inReq.getParameterMap().keySet().iterator(); iter.hasNext();)
456: {
457: String keyId = (String) iter.next();
458: if ( keyId.indexOf("property-") == 0)
459: {
460: newuser.put(keyId.substring("property-".length()),inReq.getRequestParameter(keyId));
461: }
462: }
463: getUserManager().saveUser(newuser);
464: inReq.putSessionValue("user",newuser);
465: String commentlink = inReq.getRequestParameter("loginokpage");
466: if ( commentlink != null && commentlink.length() > 0)
467: {
468: inReq.redirect(commentlink);
469: }
470: }
471: */
472: public void changeEntryVisibility(WebPageRequest inReq)
473: throws Exception {
474: Blog blog = getBlog(inReq);
475:
476: String entryId = inReq.getRequestParameter("entryId");
477: BlogEntry entry = blog.getEntry(entryId);
478:
479: if (!blog.canEdit(inReq.getUser())
480: && !entry.getUser().getUserName().equals(
481: inReq.getUserName())) {
482: throw new OpenEditException("User cannot edit blog");
483: }
484:
485: entry.setVisible(!entry.isVisible());
486: BlogArchive archive = getArchive(inReq.getPage());
487: archive.saveEntry(blog, entry);
488: }
489:
490: public BlogCommentNotification getCommentNotification() {
491: return fieldCommentNotification;
492: }
493:
494: public void setCommentNotification(
495: BlogCommentNotification inCommentNotification) {
496: fieldCommentNotification = inCommentNotification;
497: }
498: }
|