1
22
23 package com.liferay.portlet.messageboards.util;
24
25 import com.liferay.portal.kernel.language.LanguageUtil;
26 import com.liferay.portal.kernel.portlet.LiferayWindowState;
27 import com.liferay.portal.kernel.util.GetterUtil;
28 import com.liferay.portal.kernel.util.Http;
29 import com.liferay.portal.kernel.util.LocaleUtil;
30 import com.liferay.portal.kernel.util.ParamUtil;
31 import com.liferay.portal.kernel.util.StringPool;
32 import com.liferay.portal.kernel.util.StringUtil;
33 import com.liferay.portal.kernel.util.Validator;
34 import com.liferay.portal.model.Group;
35 import com.liferay.portal.model.Organization;
36 import com.liferay.portal.model.Role;
37 import com.liferay.portal.model.UserGroup;
38 import com.liferay.portal.service.GroupLocalServiceUtil;
39 import com.liferay.portal.service.OrganizationLocalServiceUtil;
40 import com.liferay.portal.service.RoleLocalServiceUtil;
41 import com.liferay.portal.service.UserGroupLocalServiceUtil;
42 import com.liferay.portal.service.UserGroupRoleLocalServiceUtil;
43 import com.liferay.portal.service.UserLocalServiceUtil;
44 import com.liferay.portal.theme.ThemeDisplay;
45 import com.liferay.portal.util.ContentUtil;
46 import com.liferay.portal.util.PropsValues;
47 import com.liferay.portlet.messageboards.model.MBBan;
48 import com.liferay.portlet.messageboards.model.MBCategory;
49 import com.liferay.portlet.messageboards.model.MBMessage;
50 import com.liferay.portlet.messageboards.model.MBStatsUser;
51 import com.liferay.portlet.messageboards.model.impl.MBCategoryImpl;
52 import com.liferay.portlet.messageboards.service.MBCategoryLocalServiceUtil;
53 import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
54 import com.liferay.util.LocalizationUtil;
55 import com.liferay.util.mail.JavaMailUtil;
56
57 import java.io.InputStream;
58
59 import java.util.Calendar;
60 import java.util.Date;
61
62 import javax.mail.BodyPart;
63 import javax.mail.Message;
64 import javax.mail.Part;
65 import javax.mail.internet.MimeMessage;
66 import javax.mail.internet.MimeMultipart;
67
68 import javax.portlet.PortletPreferences;
69 import javax.portlet.PortletURL;
70 import javax.portlet.RenderRequest;
71 import javax.portlet.RenderResponse;
72
73 import javax.servlet.jsp.PageContext;
74
75 import org.apache.commons.logging.Log;
76 import org.apache.commons.logging.LogFactory;
77
78
84 public class MBUtil {
85
86 public static final String POP_PORTLET_PREFIX = "mb.";
87
88 public static final int POP_SERVER_SUBDOMAIN_LENGTH =
89 PropsValues.POP_SERVER_SUBDOMAIN.length();
90
91 public static void collectMultipartContent(
92 MimeMultipart multipart, MBMailMessage collector)
93 throws Exception {
94
95 for (int i = 0; i < multipart.getCount(); i++) {
96 BodyPart part = multipart.getBodyPart(i);
97
98 collectPartContent(part, collector);
99 }
100 }
101
102 public static void collectPartContent(Part part, MBMailMessage collector)
103 throws Exception {
104
105 Object partContent = part.getContent();
106
107 String contentType = part.getContentType().toLowerCase();
108
109 if ((part.getDisposition() != null) &&
110 (part.getDisposition().equalsIgnoreCase(MimeMessage.ATTACHMENT))) {
111
112 if (_log.isDebugEnabled()) {
113 _log.debug("Processing attachment");
114 }
115
116 byte[] bytes = null;
117
118 if (partContent instanceof String) {
119 bytes = ((String)partContent).getBytes();
120 }
121 else if (partContent instanceof InputStream) {
122 bytes = JavaMailUtil.getBytes(part);
123 }
124
125 collector.addFile(part.getFileName(), bytes);
126 }
127 else {
128 if (partContent instanceof MimeMultipart) {
129 collectMultipartContent((MimeMultipart)partContent, collector);
130 }
131 else if (partContent instanceof String) {
132 if (contentType.startsWith("text/html")) {
133 collector.setHtmlBody((String)partContent);
134 }
135 else {
136 collector.setPlainBody((String)partContent);
137 }
138 }
139 }
140 }
141
142 public static String getBreadcrumbs(
143 long categoryId, long messageId, PageContext pageContext,
144 RenderRequest renderRequest, RenderResponse renderResponse)
145 throws Exception {
146
147 if (messageId > 0) {
148 MBMessage message = MBMessageLocalServiceUtil.getMessage(messageId);
149
150 return getBreadcrumbs(
151 null, message, pageContext, renderRequest, renderResponse);
152 }
153 else {
154 MBCategory category = null;
155
156 try {
157 if ((categoryId > 0) &&
158 (categoryId != MBCategoryImpl.DEFAULT_PARENT_CATEGORY_ID)) {
159
160 category = MBCategoryLocalServiceUtil.getCategory(
161 categoryId);
162 }
163 }
164 catch (Exception e) {
165 _log.error("Unable to retrieve category " + categoryId, e);
166 }
167
168 return getBreadcrumbs(
169 category, null, pageContext, renderRequest, renderResponse);
170 }
171 }
172
173 public static String getBreadcrumbs(
174 MBCategory category, MBMessage message, PageContext pageContext,
175 RenderRequest renderRequest, RenderResponse renderResponse)
176 throws Exception {
177
178 String strutsAction = ParamUtil.getString(
179 renderRequest, "struts_action");
180
181 boolean selectCategory = strutsAction.equals(
182 "/message_boards/select_category");
183
184 if ((message != null) && (category == null)) {
185 category = message.getCategory();
186 }
187
188 PortletURL categoriesURL = renderResponse.createRenderURL();
189
190 if (selectCategory) {
191 categoriesURL.setWindowState(LiferayWindowState.POP_UP);
192
193 categoriesURL.setParameter(
194 "struts_action", "/message_boards/select_category");
195 }
196 else {
197
199 categoriesURL.setParameter("struts_action", "/message_boards/view");
200 categoriesURL.setParameter(
201 "categoryId",
202 String.valueOf(MBCategoryImpl.DEFAULT_PARENT_CATEGORY_ID));
203 }
204
205 String categoriesLink =
206 "<a href=\"" + categoriesURL.toString() + "\">" +
207 LanguageUtil.get(pageContext, "categories") + "</a>";
208
209 if (category == null) {
210 return categoriesLink;
211 }
212
213 String breadcrumbs = StringPool.BLANK;
214
215 for (int i = 0;; i++) {
216 category = category.toEscapedModel();
217
218 PortletURL portletURL = renderResponse.createRenderURL();
219
220 if (selectCategory) {
221 portletURL.setWindowState(LiferayWindowState.POP_UP);
222
223 portletURL.setParameter(
224 "struts_action", "/message_boards/select_category");
225 portletURL.setParameter(
226 "categoryId", String.valueOf(category.getCategoryId()));
227 }
228 else {
229
231 portletURL.setParameter(
232 "struts_action", "/message_boards/view");
233 portletURL.setParameter(
234 "categoryId", String.valueOf(category.getCategoryId()));
235 }
236
237 String categoryLink =
238 "<a href=\"" + portletURL.toString() + "\">" +
239 category.getName() + "</a>";
240
241 if (i == 0) {
242 breadcrumbs = categoryLink;
243 }
244 else {
245 breadcrumbs = categoryLink + " » " + breadcrumbs;
246 }
247
248 if (category.isRoot()) {
249 break;
250 }
251
252 category = MBCategoryLocalServiceUtil.getCategory(
253 category.getParentCategoryId());
254 }
255
256 breadcrumbs = categoriesLink + " » " + breadcrumbs;
257
258 if (message != null) {
259 message = message.toEscapedModel();
260
261 PortletURL messageURL = renderResponse.createRenderURL();
262
263
265 messageURL.setParameter(
266 "struts_action", "/message_boards/view_message");
267 messageURL.setParameter(
268 "messageId", String.valueOf(message.getMessageId()));
269
270 String messageLink =
271 "<a href=\"" + messageURL.toString() + "\">" +
272 message.getSubject() + "</a>";
273
274 breadcrumbs = breadcrumbs + " » " + messageLink;
275 }
276
277 return breadcrumbs;
278 }
279
280 public static String getEmailFromAddress(PortletPreferences prefs) {
281 String emailFromAddress = PropsValues.MESSAGE_BOARDS_EMAIL_FROM_ADDRESS;
282
283 return prefs.getValue("email-from-address", emailFromAddress);
284 }
285
286 public static String getEmailFromName(PortletPreferences prefs) {
287 String emailFromName = PropsValues.MESSAGE_BOARDS_EMAIL_FROM_NAME;
288
289 return prefs.getValue("email-from-name", emailFromName);
290 }
291
292 public static boolean getEmailHtmlFormat(PortletPreferences prefs) {
293 String emailHtmlFormat = prefs.getValue(
294 "email-html-format", StringPool.BLANK);
295
296 if (Validator.isNotNull(emailHtmlFormat)) {
297 return GetterUtil.getBoolean(emailHtmlFormat);
298 }
299 else {
300 return PropsValues.MESSAGE_BOARDS_EMAIL_HTML_FORMAT;
301 }
302 }
303
304 public static boolean getEmailMessageAddedEnabled(
305 PortletPreferences prefs) {
306
307 String emailMessageAddedEnabled = prefs.getValue(
308 "email-message-added-enabled", StringPool.BLANK);
309
310 if (Validator.isNotNull(emailMessageAddedEnabled)) {
311 return GetterUtil.getBoolean(emailMessageAddedEnabled);
312 }
313 else {
314 return PropsValues.MESSAGE_BOARDS_EMAIL_MESSAGE_ADDED_ENABLED;
315 }
316 }
317
318 public static String getEmailMessageAddedBody(PortletPreferences prefs) {
319 String emailMessageAddedBody = prefs.getValue(
320 "email-message-added-body", StringPool.BLANK);
321
322 if (Validator.isNotNull(emailMessageAddedBody)) {
323 return emailMessageAddedBody;
324 }
325 else {
326 return ContentUtil.get(
327 PropsValues.MESSAGE_BOARDS_EMAIL_MESSAGE_ADDED_BODY);
328 }
329 }
330
331 public static String getEmailMessageAddedSignature(
332 PortletPreferences prefs) {
333
334 String emailMessageAddedSignature = prefs.getValue(
335 "email-message-added-signature", StringPool.BLANK);
336
337 if (Validator.isNotNull(emailMessageAddedSignature)) {
338 return emailMessageAddedSignature;
339 }
340 else {
341 return ContentUtil.get(
342 PropsValues.MESSAGE_BOARDS_EMAIL_MESSAGE_ADDED_SIGNATURE);
343 }
344 }
345
346 public static String getEmailMessageAddedSubjectPrefix(
347 PortletPreferences prefs) {
348
349 String emailMessageAddedSubjectPrefix = prefs.getValue(
350 "email-message-added-subject-prefix", StringPool.BLANK);
351
352 if (Validator.isNotNull(emailMessageAddedSubjectPrefix)) {
353 return emailMessageAddedSubjectPrefix;
354 }
355 else {
356 return ContentUtil.get(
357 PropsValues.MESSAGE_BOARDS_EMAIL_MESSAGE_ADDED_SUBJECT_PREFIX);
358 }
359 }
360
361 public static boolean getEmailMessageUpdatedEnabled(
362 PortletPreferences prefs) {
363
364 String emailMessageUpdatedEnabled = prefs.getValue(
365 "email-message-updated-enabled", StringPool.BLANK);
366
367 if (Validator.isNotNull(emailMessageUpdatedEnabled)) {
368 return GetterUtil.getBoolean(emailMessageUpdatedEnabled);
369 }
370 else {
371 return PropsValues.MESSAGE_BOARDS_EMAIL_MESSAGE_UPDATED_ENABLED;
372 }
373 }
374
375 public static String getEmailMessageUpdatedBody(PortletPreferences prefs) {
376 String emailMessageUpdatedBody = prefs.getValue(
377 "email-message-updated-body", StringPool.BLANK);
378
379 if (Validator.isNotNull(emailMessageUpdatedBody)) {
380 return emailMessageUpdatedBody;
381 }
382 else {
383 return ContentUtil.get(
384 PropsValues.MESSAGE_BOARDS_EMAIL_MESSAGE_UPDATED_BODY);
385 }
386 }
387
388 public static String getEmailMessageUpdatedSignature(
389 PortletPreferences prefs) {
390
391 String emailMessageUpdatedSignature = prefs.getValue(
392 "email-message-updated-signature", StringPool.BLANK);
393
394 if (Validator.isNotNull(emailMessageUpdatedSignature)) {
395 return emailMessageUpdatedSignature;
396 }
397 else {
398 return ContentUtil.get(
399 PropsValues.MESSAGE_BOARDS_EMAIL_MESSAGE_UPDATED_SIGNATURE);
400 }
401 }
402
403 public static String getEmailMessageUpdatedSubjectPrefix(
404 PortletPreferences prefs) {
405
406 String emailMessageUpdatedSubject = prefs.getValue(
407 "email-message-updated-subject-prefix", StringPool.BLANK);
408
409 if (Validator.isNotNull(emailMessageUpdatedSubject)) {
410 return emailMessageUpdatedSubject;
411 }
412 else {
413 return ContentUtil.get(
414 PropsValues.
415 MESSAGE_BOARDS_EMAIL_MESSAGE_UPDATED_SUBJECT_PREFIX);
416 }
417 }
418
419 public static String getMailId(String mx, long categoryId, long messageId) {
420 StringBuilder sb = new StringBuilder();
421
422 sb.append(StringPool.LESS_THAN);
423 sb.append(POP_PORTLET_PREFIX);
424 sb.append(categoryId);
425 sb.append(StringPool.PERIOD);
426 sb.append(messageId);
427 sb.append(StringPool.AT);
428 sb.append(PropsValues.POP_SERVER_SUBDOMAIN);
429 sb.append(StringPool.PERIOD);
430 sb.append(mx);
431 sb.append(StringPool.GREATER_THAN);
432
433 return sb.toString();
434 }
435
436 public static String getMailingListAddress(
437 long categoryId, long messageId, String mx,
438 String defaultMailingListAddress) {
439
440 if (POP_SERVER_SUBDOMAIN_LENGTH <= 0) {
441 return defaultMailingListAddress;
442 }
443
444 StringBuilder sb = new StringBuilder();
445
446 sb.append(POP_PORTLET_PREFIX);
447 sb.append(categoryId);
448 sb.append(StringPool.PERIOD);
449 sb.append(messageId);
450 sb.append(StringPool.AT);
451 sb.append(PropsValues.POP_SERVER_SUBDOMAIN);
452 sb.append(StringPool.PERIOD);
453 sb.append(mx);
454
455 return sb.toString();
456 }
457
458 public static long getMessageId(String mailId) {
459 int x = mailId.indexOf(StringPool.LESS_THAN) + 1;
460 int y = mailId.indexOf(StringPool.AT);
461
462 long messageId = 0;
463
464 if ((x > 0 ) && (y != -1)) {
465 String temp = mailId.substring(x, y);
466
467 int z = temp.lastIndexOf(StringPool.PERIOD);
468
469 if (z != -1) {
470 messageId = GetterUtil.getLong(temp.substring(z + 1));
471 }
472 }
473
474 return messageId;
475 }
476
477 public static long getParentMessageId(Message message) throws Exception {
478 long parentMessageId = -1;
479
480 String parentHeader = getParentMessageIdString(message);
481
482 if (parentHeader != null) {
483 if (_log.isDebugEnabled()) {
484 _log.debug("Parent header " + parentHeader);
485 }
486
487 parentMessageId = getMessageId(parentHeader);
488
489 if (_log.isDebugEnabled()) {
490 _log.debug("Previous message id " + parentMessageId);
491 }
492 }
493
494 return parentMessageId;
495 }
496
497 public static String getParentMessageIdString(Message message)
498 throws Exception {
499
500
505 String parentHeader = null;
506
507 String[] references = message.getHeader("References");
508
509 if ((references != null) && (references.length > 0)) {
510 parentHeader = references[0].substring(
511 references[0].lastIndexOf("<"));
512 }
513
514 if (parentHeader == null) {
515 String[] inReplyToHeaders = message.getHeader("In-Reply-To");
516
517 if ((inReplyToHeaders != null) && (inReplyToHeaders.length > 0)) {
518 parentHeader = inReplyToHeaders[0];
519 }
520 }
521
522 return parentHeader;
523 }
524
525 public static String[] getThreadPriority(
526 PortletPreferences prefs, String languageId, double value,
527 ThemeDisplay themeDisplay)
528 throws Exception {
529
530 String[] priorities = LocalizationUtil.getPrefsValues(
531 prefs, "priorities", languageId);
532
533 String[] priorityPair = _findThreadPriority(
534 value, themeDisplay, priorities);
535
536 if (priorityPair == null) {
537 String defaultLanguageId = LocaleUtil.toLanguageId(
538 LocaleUtil.getDefault());
539
540 priorities = LocalizationUtil.getPrefsValues(
541 prefs, "priorities", defaultLanguageId);
542
543 priorityPair = _findThreadPriority(value, themeDisplay, priorities);
544 }
545
546 return priorityPair;
547 }
548
549 public static Date getUnbanDate(MBBan ban, int expireInterval) {
550 Date banDate = ban.getCreateDate();
551
552 Calendar cal = Calendar.getInstance();
553
554 cal.setTime(banDate);
555
556 cal.add(Calendar.DATE, expireInterval);
557
558 return cal.getTime();
559 }
560
561 public static String getUserRank(
562 PortletPreferences prefs, String languageId, int posts)
563 throws Exception {
564
565 String rank = StringPool.BLANK;
566
567 String[] ranks = LocalizationUtil.getPrefsValues(
568 prefs, "ranks", languageId);
569
570 for (int i = 0; i < ranks.length; i++) {
571 String[] kvp = StringUtil.split(ranks[i], StringPool.EQUAL);
572
573 String kvpName = kvp[0];
574 int kvpPosts = GetterUtil.getInteger(kvp[1]);
575
576 if (posts >= kvpPosts) {
577 rank = kvpName;
578 }
579 else {
580 break;
581 }
582 }
583
584 return rank;
585 }
586
587 public static String getUserRank(
588 PortletPreferences prefs, String languageId, MBStatsUser statsUser)
589 throws Exception {
590
591 String rank = StringPool.BLANK;
592
593 Group group = GroupLocalServiceUtil.getGroup(
594 statsUser.getGroupId());
595
596 long companyId = group.getCompanyId();
597
598 String[] ranks = LocalizationUtil.getPrefsValues(
599 prefs, "ranks", languageId);
600
601 for (int i = 0; i < ranks.length; i++) {
602 String[] kvp = StringUtil.split(ranks[i], StringPool.EQUAL);
603
604 String curRank = kvp[0];
605 String curRankValue = kvp[1];
606
607 String[] curRankValueKvp = StringUtil.split(
608 curRankValue, StringPool.COLON);
609
610 if (curRankValueKvp.length <= 1) {
611 int kvpPosts = GetterUtil.getInteger(curRankValue);
612
613 if (statsUser.getMessageCount() >= kvpPosts) {
614 rank = curRank;
615 }
616
617 continue;
618 }
619
620 String entityType = curRankValueKvp[0];
621 String entityValue = curRankValueKvp[1];
622
623 try {
624 if (_isEntityRank(
625 companyId, statsUser, entityType, entityValue)) {
626
627 return curRank;
628 }
629 }
630 catch (Exception e) {
631 if (_log.isWarnEnabled()) {
632 _log.warn(e);
633 }
634 }
635 }
636
637 return rank;
638 }
639
640 public static boolean hasMailIdHeader(Message message) throws Exception {
641 String[] messageIds = message.getHeader("Message-ID");
642
643 if (messageIds == null) {
644 return false;
645 }
646
647 for (String messageId : messageIds) {
648 if (messageId.contains(PropsValues.POP_SERVER_SUBDOMAIN)) {
649 return true;
650 }
651 }
652
653 return false;
654 }
655
656 public static boolean isAllowAnonymousPosting(PortletPreferences prefs) {
657 String allowAnonymousPosting = prefs.getValue(
658 "allow-anonymous-posting", StringPool.BLANK);
659
660 if (Validator.isNotNull(allowAnonymousPosting)) {
661 return GetterUtil.getBoolean(allowAnonymousPosting);
662 }
663 else {
664 return PropsValues.MESSAGE_BOARDS_ANONYMOUS_POSTING_ENABLED;
665 }
666 }
667
668 private static String[] _findThreadPriority(
669 double value, ThemeDisplay themeDisplay, String[] priorities) {
670
671 for (int i = 0; i < priorities.length; i++) {
672 String[] priority = StringUtil.split(priorities[i]);
673
674 try {
675 String priorityName = priority[0];
676 String priorityImage = priority[1];
677 double priorityValue = GetterUtil.getDouble(priority[2]);
678
679 if (value == priorityValue) {
680 if (!priorityImage.startsWith(Http.HTTP)) {
681 priorityImage =
682 themeDisplay.getPathThemeImages() + priorityImage;
683 }
684
685 return new String[] {priorityName, priorityImage};
686 }
687 }
688 catch (Exception e) {
689 _log.error("Unable to determine thread priority", e);
690 }
691 }
692
693 return null;
694 }
695
696 private static boolean _isEntityRank(
697 long companyId, MBStatsUser statsUser, String entityType,
698 String entityValue)
699 throws Exception {
700
701 long groupId = statsUser.getGroupId();
702 long userId = statsUser.getUserId();
703
704 if (entityType.equals("community-role") ||
705 entityType.equals("organization-role")) {
706
707 Role role = RoleLocalServiceUtil.getRole(companyId, entityValue);
708
709 if (UserGroupRoleLocalServiceUtil.hasUserGroupRole(
710 userId, groupId, role.getRoleId())) {
711
712 return true;
713 }
714 }
715 else if (entityType.equals("organization")) {
716 Organization organization =
717 OrganizationLocalServiceUtil.getOrganization(
718 companyId, entityValue);
719
720 if (OrganizationLocalServiceUtil.hasUserOrganization(
721 userId, organization.getOrganizationId())) {
722
723 return true;
724 }
725 }
726 else if (entityType.equals("regular-role")) {
727 if (RoleLocalServiceUtil.hasUserRole(
728 userId, companyId, entityValue, true)) {
729
730 return true;
731 }
732 }
733 else if (entityType.equals("user-group")) {
734 UserGroup userGroup = UserGroupLocalServiceUtil.getUserGroup(
735 companyId, entityValue);
736
737 if (UserLocalServiceUtil.hasUserGroupUser(
738 userGroup.getUserGroupId(), userId)) {
739
740 return true;
741 }
742 }
743
744 return false;
745 }
746
747 private static Log _log = LogFactory.getLog(MBUtil.class);
748
749 }