/
BlogComments.java
327 lines (284 loc) · 14.1 KB
/
BlogComments.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
// BlogComments.java
// -----------------------
// part of YACY
// (C) by Michael Peter Christen; mc@yacy.net
// first published on http://www.anomic.de
// Frankfurt, Germany, 2004
//
// This File is contributed by Jan Sandbrink
//
//$LastChangedDate$
//$LastChangedRevision$
//$LastChangedBy$
//
// 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
// 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.util.Date;
import java.util.Iterator;
import net.yacy.cora.document.encoding.UTF8;
import net.yacy.cora.protocol.Domains;
import net.yacy.cora.protocol.HeaderFramework;
import net.yacy.cora.protocol.RequestHeader;
import net.yacy.cora.util.ConcurrentLog;
import net.yacy.data.BlogBoard;
import net.yacy.data.BlogBoardComments;
import net.yacy.data.MessageBoard;
import net.yacy.data.UserDB;
import net.yacy.data.BlogBoard.BlogEntry;
import net.yacy.peers.Network;
import net.yacy.peers.Seed;
import net.yacy.search.Switchboard;
import net.yacy.server.serverObjects;
import net.yacy.server.serverSwitch;
import com.google.common.io.Files;
public class BlogComments {
private static final String DEFAULT_PAGE = "blog_default";
public static String dateString(final Date date) {
return Blog.dateString(date);
}
public static serverObjects respond(final RequestHeader header, serverObjects post, final serverSwitch env) {
final Switchboard sb = (Switchboard) env;
final serverObjects prop = new serverObjects();
boolean hasRights = sb.verifyAuthentication(header);
prop.put("mode_admin", hasRights ? "1" : "0");
if (post == null) {
post = new serverObjects();
post.put("page", "blog_default");
}
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.authenticationRequired();
}
}
String pagename = post.get("page", DEFAULT_PAGE);
final String ip = post.get(HeaderFramework.CONNECTION_PROP_CLIENTIP, Domains.LOCALHOST);
String strAuthor = post.get("author", "anonymous");
if ("anonymous".equals(strAuthor)) {
strAuthor = sb.blogDB.guessAuthor(ip);
if (strAuthor == null || strAuthor.isEmpty()) {
if (sb.peers.mySeed() == null) {
strAuthor = "anonymous";
} else {
strAuthor = sb.peers.mySeed().get(Seed.NAME, "anonymous");
}
}
}
byte[] author;
author = UTF8.getBytes(strAuthor);
final BlogBoard.BlogEntry page = sb.blogDB.readBlogEntry(pagename); //maybe "if(page == null)"
final boolean pageExists = sb.blogDB.contains(pagename);
// comments not allowed
prop.put("mode_allow", (page.getCommentMode() == 0) ? 0 : 1);
if (post.containsKey("submit") && page.getCommentMode() != 0 && pageExists) {
// store a new/edited blog-entry
byte[] content;
if (!"".equals(post.get("content", ""))) {
if ("".equals(post.get("subject", ""))) {
post.putHTML("subject", "no title");
}
content = UTF8.getBytes(post.get("content", ""));
final Date date = null;
//set name for new entry or date for old entry
final String StrSubject = post.get("subject", "");
byte[] subject;
subject = UTF8.getBytes(StrSubject);
final String commentID = String.valueOf(System.currentTimeMillis());
final BlogEntry blogEntry = sb.blogDB.readBlogEntry(pagename);
blogEntry.addComment(commentID);
sb.blogDB.writeBlogEntry(blogEntry);
sb.blogCommentDB.write(sb.blogCommentDB.newEntry(commentID, subject, author, ip, date, content));
prop.putHTML(serverObjects.ACTION_LOCATION,"BlogComments.html?page=" + pagename);
MessageBoard.entry msgEntry = sb.messageDB.newEntry(
"blogComment",
strAuthor,
sb.peers.mySeed().hash,
sb.peers.mySeed().getName(), sb.peers.mySeed().hash,
"new blog comment: " + UTF8.String(blogEntry.getSubject()), content);
sb.messageDB.write(msgEntry);
messageForwardingViaEmail(sb, msgEntry);
// finally write notification
final File notifierSource = new File(sb.getAppPath(), sb.getConfig("htRootPath","htroot") + "/env/grafics/message.gif");
final File notifierDest = new File(sb.getDataPath("htDocsPath", "DATA/HTDOCS"), "notifier.gif");
try {
Files.copy(notifierSource, notifierDest);
} catch (final IOException e) {
ConcurrentLog.severe("MESSAGE", "NEW MESSAGE ARRIVED! (error: " + e.getMessage() + ")");
}
}
}
if (hasRights && post.containsKey("delete") && post.containsKey("page") &&
post.containsKey("comment") && page.removeComment(post.get("comment"))) {
sb.blogCommentDB.delete(post.get("comment"));
}
if (hasRights && post.containsKey("allow") && post.containsKey("page") && post.containsKey("comment")) {
final BlogBoardComments.CommentEntry entry = sb.blogCommentDB.read(post.get("comment"));
entry.allow();
sb.blogCommentDB.write(entry);
}
if (post.containsKey("preview") && page.getCommentMode() != 0) {
//preview the page
prop.put("mode", "1");//preview
prop.putHTML("mode_pageid", pagename);
prop.putHTML("mode_allow_pageid", pagename);
prop.putHTML("mode_author", UTF8.String(author));
prop.putHTML("mode_allow_author", UTF8.String(author));
prop.putHTML("mode_subject", post.get("subject",""));
prop.put("mode_date", dateString(new Date()));
prop.putWiki(sb.peers.mySeed().getClusterAddress(), "mode_page", post.get("content", ""));
prop.put("mode_page-code", post.get("content", ""));
} else {
// show blog-entry/entries
prop.put("mode", "0"); //viewing
if("blog_default".equals(pagename)) {
prop.put(serverObjects.ACTION_LOCATION,"Blog.html");
} else {
//show 1 blog entry
prop.put("mode_pageid", page.getKey());
prop.putHTML("mode_allow_pageid", pagename);
prop.putHTML("mode_subject", UTF8.String(page.getSubject()));
prop.putHTML("mode_author", UTF8.String(page.getAuthor()));
prop.putHTML("mode_allow_author", UTF8.String(author));
prop.put("mode_comments", page.getCommentsSize());
prop.put("mode_date", dateString(page.getDate()));
prop.putWiki(sb.peers.mySeed().getClusterAddress(), "mode_page", page.getPage());
if (hasRights) {
prop.put("mode_admin", "1");
prop.put("mode_admin_pageid", page.getKey());
}
final Iterator<String> i = page.getComments().iterator();
final int commentMode = page.getCommentMode();
String pageid;
BlogBoardComments.CommentEntry entry;
final boolean xml = post.containsKey("xml");
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
if (xml) {
num = 0;
}
if (start < 0) {
start = 0;
}
final 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 = sb.blogCommentDB.read(pageid);
if (commentMode == 2 && !hasRights && !entry.isAllowed()) {
continue;
}
prop.put("mode", "0");
prop.put("mode_entries_"+count+"_pageid", entry.getKey());
if (!xml) {
prop.putHTML("mode_entries_"+count+"_subject", UTF8.String(entry.getSubject()));
prop.putHTML("mode_entries_"+count+"_author", UTF8.String(entry.getAuthor()));
prop.putWiki(sb.peers.mySeed().getClusterAddress(), "mode_entries_"+count+"_page", entry.getPage());
} else {
prop.putHTML("mode_entries_"+count+"_subject", UTF8.String(entry.getSubject()));
prop.putHTML("mode_entries_"+count+"_author", UTF8.String(entry.getAuthor()));
prop.put("mode_entries_"+count+"_page", entry.getPage());
prop.put("mode_entries_"+count+"_timestamp", entry.getTimestamp());
}
prop.put("mode_entries_"+count+"_date", dateString(entry.getDate()));
prop.put("mode_entries_"+count+"_ip", entry.getIp());
if(hasRights) {
prop.put("mode_entries_"+count+"_admin", "1");
prop.put("mode_entries_"+count+"_admin_pageid", page.getKey());
prop.put("mode_entries_"+count+"_admin_commentid", pageid);
if(page.getCommentMode() == 2 && !entry.isAllowed()) {
prop.put("mode_entries_"+count+"_admin_moderate", "1");
prop.put("mode_entries_"+count+"_admin_moderate_pageid", page.getKey());
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.getKey());
}
else prop.put("mode_moreentries", "0");
if (start > 1) {
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.getKey());
} else prop.put("mode_preventries", "0");
}
}
// return rewrite properties
return prop;
}
private static void messageForwardingViaEmail(final Switchboard sb, final MessageBoard.entry msgEntry) {
try {
if (!sb.getConfigBool("msgForwardingEnabled",false)) {
return;
}
// get the recipient address
final String sendMailTo = sb.getConfig("msgForwardingTo","root@localhost").trim();
// get the sendmail configuration
final String sendMailStr = sb.getConfig("msgForwardingCmd","/usr/bin/sendmail")+" "+sendMailTo;
final String[] sendMail = sendMailStr.trim().split(" ");
// build the message text
final StringBuilder emailTxt = new StringBuilder();
emailTxt.append("To: ")
.append(sendMailTo)
.append("\nFrom: ")
.append("yacy@")
.append(sb.peers.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(UTF8.String(msgEntry.message()));
final Process process=Runtime.getRuntime().exec(sendMail);
final PrintWriter email = new PrintWriter(process.getOutputStream());
email.print(new String(emailTxt));
email.close();
} catch (final Exception e) {
Network.log.warn("message: message forwarding via email failed. ",e);
}
}
}