1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.messageboards.util;
24  
25  import com.liferay.portal.kernel.language.LanguageUtil;
26  import com.liferay.portal.kernel.log.Log;
27  import com.liferay.portal.kernel.log.LogFactoryUtil;
28  import com.liferay.portal.kernel.portlet.LiferayWindowState;
29  import com.liferay.portal.kernel.util.GetterUtil;
30  import com.liferay.portal.kernel.util.Http;
31  import com.liferay.portal.kernel.util.LocaleUtil;
32  import com.liferay.portal.kernel.util.ParamUtil;
33  import com.liferay.portal.kernel.util.StringPool;
34  import com.liferay.portal.kernel.util.StringUtil;
35  import com.liferay.portal.kernel.util.Validator;
36  import com.liferay.portal.model.Group;
37  import com.liferay.portal.model.Organization;
38  import com.liferay.portal.model.Role;
39  import com.liferay.portal.model.UserGroup;
40  import com.liferay.portal.service.GroupLocalServiceUtil;
41  import com.liferay.portal.service.OrganizationLocalServiceUtil;
42  import com.liferay.portal.service.RoleLocalServiceUtil;
43  import com.liferay.portal.service.UserGroupLocalServiceUtil;
44  import com.liferay.portal.service.UserGroupRoleLocalServiceUtil;
45  import com.liferay.portal.service.UserLocalServiceUtil;
46  import com.liferay.portal.theme.ThemeDisplay;
47  import com.liferay.portal.util.ContentUtil;
48  import com.liferay.portal.util.PropsValues;
49  import com.liferay.portlet.messageboards.model.MBBan;
50  import com.liferay.portlet.messageboards.model.MBCategory;
51  import com.liferay.portlet.messageboards.model.MBMailingList;
52  import com.liferay.portlet.messageboards.model.MBMessage;
53  import com.liferay.portlet.messageboards.model.MBStatsUser;
54  import com.liferay.portlet.messageboards.model.impl.MBCategoryImpl;
55  import com.liferay.portlet.messageboards.service.MBCategoryLocalServiceUtil;
56  import com.liferay.portlet.messageboards.service.MBMailingListLocalServiceUtil;
57  import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
58  import com.liferay.util.LocalizationUtil;
59  import com.liferay.util.mail.JavaMailUtil;
60  
61  import java.io.InputStream;
62  
63  import java.util.Calendar;
64  import java.util.Date;
65  
66  import javax.mail.BodyPart;
67  import javax.mail.Message;
68  import javax.mail.Part;
69  import javax.mail.internet.MimeMessage;
70  import javax.mail.internet.MimeMultipart;
71  
72  import javax.portlet.PortletPreferences;
73  import javax.portlet.PortletURL;
74  import javax.portlet.RenderRequest;
75  import javax.portlet.RenderResponse;
76  
77  import javax.servlet.jsp.PageContext;
78  
79  /**
80   * <a href="MBUtil.java.html"><b><i>View Source</i></b></a>
81   *
82   * @author Brian Wing Shun Chan
83   */
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             //categoriesURL.setWindowState(WindowState.MAXIMIZED);
198 
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 "<span class=\"first last\">" + categoriesLink + "</span>";
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                 //portletURL.setWindowState(WindowState.MAXIMIZED);
230 
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                 if (message != null) {
243                     breadcrumbs += categoryLink;
244                 }
245                 else {
246                     breadcrumbs =
247                         "<span class=\"last\">" + categoryLink + "</span>";
248                 }
249             }
250             else {
251                 breadcrumbs = categoryLink + " &raquo; " + breadcrumbs;
252             }
253 
254             if (category.isRoot()) {
255                 break;
256             }
257 
258             category = MBCategoryLocalServiceUtil.getCategory(
259                 category.getParentCategoryId());
260         }
261 
262         breadcrumbs =
263             "<span class=\"first\">" + categoriesLink + " &raquo; </span>" +
264                 breadcrumbs;
265 
266         if (message != null) {
267             message = message.toEscapedModel();
268 
269             PortletURL messageURL = renderResponse.createRenderURL();
270 
271             //messageURL.setWindowState(WindowState.MAXIMIZED);
272 
273             messageURL.setParameter(
274                 "struts_action", "/message_boards/view_message");
275             messageURL.setParameter(
276                 "messageId", String.valueOf(message.getMessageId()));
277 
278             String messageLink =
279                 "<span class=\"last\"><a href=\"" + messageURL.toString() +
280                     "\">" + message.getSubject() + "</a></span>";
281 
282             breadcrumbs = breadcrumbs + " &raquo; " + messageLink;
283         }
284 
285         return breadcrumbs;
286     }
287 
288     public static String getEmailFromAddress(PortletPreferences preferences) {
289         String emailFromAddress = PropsValues.MESSAGE_BOARDS_EMAIL_FROM_ADDRESS;
290 
291         return preferences.getValue("email-from-address", emailFromAddress);
292     }
293 
294     public static String getEmailFromName(PortletPreferences preferences) {
295         String emailFromName = PropsValues.MESSAGE_BOARDS_EMAIL_FROM_NAME;
296 
297         return preferences.getValue("email-from-name", emailFromName);
298     }
299 
300     public static boolean getEmailHtmlFormat(PortletPreferences preferences) {
301         String emailHtmlFormat = preferences.getValue(
302             "email-html-format", StringPool.BLANK);
303 
304         if (Validator.isNotNull(emailHtmlFormat)) {
305             return GetterUtil.getBoolean(emailHtmlFormat);
306         }
307         else {
308             return PropsValues.MESSAGE_BOARDS_EMAIL_HTML_FORMAT;
309         }
310     }
311 
312     public static boolean getEmailMessageAddedEnabled(
313         PortletPreferences preferences) {
314 
315         String emailMessageAddedEnabled = preferences.getValue(
316             "email-message-added-enabled", StringPool.BLANK);
317 
318         if (Validator.isNotNull(emailMessageAddedEnabled)) {
319             return GetterUtil.getBoolean(emailMessageAddedEnabled);
320         }
321         else {
322             return PropsValues.MESSAGE_BOARDS_EMAIL_MESSAGE_ADDED_ENABLED;
323         }
324     }
325 
326     public static String getEmailMessageAddedBody(
327         PortletPreferences preferences) {
328 
329         String emailMessageAddedBody = preferences.getValue(
330             "email-message-added-body", StringPool.BLANK);
331 
332         if (Validator.isNotNull(emailMessageAddedBody)) {
333             return emailMessageAddedBody;
334         }
335         else {
336             return ContentUtil.get(
337                 PropsValues.MESSAGE_BOARDS_EMAIL_MESSAGE_ADDED_BODY);
338         }
339     }
340 
341     public static String getEmailMessageAddedSignature(
342         PortletPreferences preferences) {
343 
344         String emailMessageAddedSignature = preferences.getValue(
345             "email-message-added-signature", StringPool.BLANK);
346 
347         if (Validator.isNotNull(emailMessageAddedSignature)) {
348             return emailMessageAddedSignature;
349         }
350         else {
351             return ContentUtil.get(
352                 PropsValues.MESSAGE_BOARDS_EMAIL_MESSAGE_ADDED_SIGNATURE);
353         }
354     }
355 
356     public static String getEmailMessageAddedSubjectPrefix(
357         PortletPreferences preferences) {
358 
359         String emailMessageAddedSubjectPrefix = preferences.getValue(
360             "email-message-added-subject-prefix", StringPool.BLANK);
361 
362         if (Validator.isNotNull(emailMessageAddedSubjectPrefix)) {
363             return emailMessageAddedSubjectPrefix;
364         }
365         else {
366             return ContentUtil.get(
367                 PropsValues.MESSAGE_BOARDS_EMAIL_MESSAGE_ADDED_SUBJECT_PREFIX);
368         }
369     }
370 
371     public static boolean getEmailMessageUpdatedEnabled(
372         PortletPreferences preferences) {
373 
374         String emailMessageUpdatedEnabled = preferences.getValue(
375             "email-message-updated-enabled", StringPool.BLANK);
376 
377         if (Validator.isNotNull(emailMessageUpdatedEnabled)) {
378             return GetterUtil.getBoolean(emailMessageUpdatedEnabled);
379         }
380         else {
381             return PropsValues.MESSAGE_BOARDS_EMAIL_MESSAGE_UPDATED_ENABLED;
382         }
383     }
384 
385     public static String getEmailMessageUpdatedBody(
386         PortletPreferences preferences) {
387 
388         String emailMessageUpdatedBody = preferences.getValue(
389             "email-message-updated-body", StringPool.BLANK);
390 
391         if (Validator.isNotNull(emailMessageUpdatedBody)) {
392             return emailMessageUpdatedBody;
393         }
394         else {
395             return ContentUtil.get(
396                 PropsValues.MESSAGE_BOARDS_EMAIL_MESSAGE_UPDATED_BODY);
397         }
398     }
399 
400     public static String getEmailMessageUpdatedSignature(
401         PortletPreferences preferences) {
402 
403         String emailMessageUpdatedSignature = preferences.getValue(
404             "email-message-updated-signature", StringPool.BLANK);
405 
406         if (Validator.isNotNull(emailMessageUpdatedSignature)) {
407             return emailMessageUpdatedSignature;
408         }
409         else {
410             return ContentUtil.get(
411                 PropsValues.MESSAGE_BOARDS_EMAIL_MESSAGE_UPDATED_SIGNATURE);
412         }
413     }
414 
415     public static String getEmailMessageUpdatedSubjectPrefix(
416         PortletPreferences preferences) {
417 
418         String emailMessageUpdatedSubject = preferences.getValue(
419             "email-message-updated-subject-prefix", StringPool.BLANK);
420 
421         if (Validator.isNotNull(emailMessageUpdatedSubject)) {
422             return emailMessageUpdatedSubject;
423         }
424         else {
425             return ContentUtil.get(
426                 PropsValues.
427                     MESSAGE_BOARDS_EMAIL_MESSAGE_UPDATED_SUBJECT_PREFIX);
428         }
429     }
430 
431     public static String getMailId(String mx, long categoryId, long messageId) {
432         StringBuilder sb = new StringBuilder();
433 
434         sb.append(StringPool.LESS_THAN);
435         sb.append(POP_PORTLET_PREFIX);
436         sb.append(categoryId);
437         sb.append(StringPool.PERIOD);
438         sb.append(messageId);
439         sb.append(StringPool.AT);
440 
441         if (Validator.isNotNull(PropsValues.POP_SERVER_SUBDOMAIN)) {
442             sb.append(PropsValues.POP_SERVER_SUBDOMAIN);
443             sb.append(StringPool.PERIOD);
444         }
445 
446         sb.append(mx);
447         sb.append(StringPool.GREATER_THAN);
448 
449         return sb.toString();
450     }
451 
452     public static String getMailingListAddress(
453         long categoryId, long messageId, String mx,
454         String defaultMailingListAddress) {
455 
456         if (POP_SERVER_SUBDOMAIN_LENGTH <= 0) {
457             String mailingListAddress = defaultMailingListAddress;
458 
459             try {
460                 MBMailingList mailingList =
461                     MBMailingListLocalServiceUtil.getCategoryMailingList(
462                         categoryId);
463 
464                 if (mailingList.isActive()) {
465                     mailingListAddress = mailingList.getEmailAddress();
466                 }
467             }
468             catch (Exception e) {
469             }
470 
471             return mailingListAddress;
472         }
473 
474         StringBuilder sb = new StringBuilder();
475 
476         sb.append(POP_PORTLET_PREFIX);
477         sb.append(categoryId);
478         sb.append(StringPool.PERIOD);
479         sb.append(messageId);
480         sb.append(StringPool.AT);
481         sb.append(PropsValues.POP_SERVER_SUBDOMAIN);
482         sb.append(StringPool.PERIOD);
483         sb.append(mx);
484 
485         return sb.toString();
486     }
487 
488     public static long getMessageId(String mailId) {
489         int x = mailId.indexOf(StringPool.LESS_THAN) + 1;
490         int y = mailId.indexOf(StringPool.AT);
491 
492         long messageId = 0;
493 
494         if ((x > 0 ) && (y != -1)) {
495             String temp = mailId.substring(x, y);
496 
497             int z = temp.lastIndexOf(StringPool.PERIOD);
498 
499             if (z != -1) {
500                 messageId = GetterUtil.getLong(temp.substring(z + 1));
501             }
502         }
503 
504         return messageId;
505     }
506 
507     public static long getParentMessageId(Message message) throws Exception {
508         long parentMessageId = -1;
509 
510         String parentHeader = getParentMessageIdString(message);
511 
512         if (parentHeader != null) {
513             if (_log.isDebugEnabled()) {
514                 _log.debug("Parent header " + parentHeader);
515             }
516 
517             parentMessageId = getMessageId(parentHeader);
518 
519             if (_log.isDebugEnabled()) {
520                 _log.debug("Previous message id " + parentMessageId);
521             }
522         }
523 
524         return parentMessageId;
525     }
526 
527     public static String getParentMessageIdString(Message message)
528         throws Exception {
529 
530         // If the previous block failed, try to get the parent message ID from
531         // the "References" header as explained in
532         // http://cr.yp.to/immhf/thread.html. Some mail clients such as Yahoo!
533         // Mail use the "In-Reply-To" header, so we check that as well.
534 
535         String parentHeader = null;
536 
537         String[] references = message.getHeader("References");
538 
539         if ((references != null) && (references.length > 0)) {
540             String reference = references[0];
541 
542             int x = reference.lastIndexOf("<mb.");
543 
544             if (x > -1) {
545                 int y = reference.indexOf(">", x);
546 
547                 parentHeader = reference.substring(x, y);
548             }
549         }
550 
551         if (parentHeader == null) {
552             String[] inReplyToHeaders = message.getHeader("In-Reply-To");
553 
554             if ((inReplyToHeaders != null) && (inReplyToHeaders.length > 0)) {
555                 parentHeader = inReplyToHeaders[0];
556             }
557         }
558 
559         if (Validator.isNull(parentHeader) ||
560             !parentHeader.startsWith(POP_PORTLET_PREFIX, 1)) {
561 
562             parentHeader = _getParentMessageIdFromSubject(message);
563         }
564 
565         return parentHeader;
566     }
567 
568     public static String getSubjectWithoutMessageId(Message message)
569         throws Exception {
570 
571         String subject = message.getSubject();
572 
573         String parentMessageId = _getParentMessageIdFromSubject(message);
574 
575         if (Validator.isNotNull(parentMessageId)) {
576             int pos = subject.indexOf(parentMessageId);
577 
578             if (pos != -1) {
579                 subject = subject.substring(0, pos);
580             }
581         }
582 
583         return subject;
584     }
585 
586     public static String[] getThreadPriority(
587             PortletPreferences preferences, String languageId, double value,
588             ThemeDisplay themeDisplay)
589         throws Exception {
590 
591         String[] priorities = LocalizationUtil.getPreferencesValues(
592             preferences, "priorities", languageId);
593 
594         String[] priorityPair = _findThreadPriority(
595             value, themeDisplay, priorities);
596 
597         if (priorityPair == null) {
598             String defaultLanguageId = LocaleUtil.toLanguageId(
599                 LocaleUtil.getDefault());
600 
601             priorities = LocalizationUtil.getPreferencesValues(
602                 preferences, "priorities", defaultLanguageId);
603 
604             priorityPair = _findThreadPriority(value, themeDisplay, priorities);
605         }
606 
607         return priorityPair;
608     }
609 
610     public static Date getUnbanDate(MBBan ban, int expireInterval) {
611         Date banDate = ban.getCreateDate();
612 
613         Calendar cal = Calendar.getInstance();
614 
615         cal.setTime(banDate);
616 
617         cal.add(Calendar.DATE, expireInterval);
618 
619         return cal.getTime();
620     }
621 
622     public static String getUserRank(
623             PortletPreferences preferences, String languageId, int posts)
624         throws Exception {
625 
626         String rank = StringPool.BLANK;
627 
628         String[] ranks = LocalizationUtil.getPreferencesValues(
629             preferences, "ranks", languageId);
630 
631         for (int i = 0; i < ranks.length; i++) {
632             String[] kvp = StringUtil.split(ranks[i], StringPool.EQUAL);
633 
634             String kvpName = kvp[0];
635             int kvpPosts = GetterUtil.getInteger(kvp[1]);
636 
637             if (posts >= kvpPosts) {
638                 rank = kvpName;
639             }
640             else {
641                 break;
642             }
643         }
644 
645         return rank;
646     }
647 
648     public static String[] getUserRank(
649             PortletPreferences preferences, String languageId,
650             MBStatsUser statsUser)
651         throws Exception {
652 
653         String[] rank = {StringPool.BLANK, StringPool.BLANK};
654 
655         int maxPosts = 0;
656 
657         Group group = GroupLocalServiceUtil.getGroup(
658             statsUser.getGroupId());
659 
660         long companyId = group.getCompanyId();
661 
662         String[] ranks = LocalizationUtil.getPreferencesValues(
663             preferences, "ranks", languageId);
664 
665         for (int i = 0; i < ranks.length; i++) {
666             String[] kvp = StringUtil.split(ranks[i], StringPool.EQUAL);
667 
668             String curRank = kvp[0];
669             String curRankValue = kvp[1];
670 
671             String[] curRankValueKvp = StringUtil.split(
672                 curRankValue, StringPool.COLON);
673 
674             if (curRankValueKvp.length <= 1) {
675                 int posts = GetterUtil.getInteger(curRankValue);
676 
677                 if ((posts <= statsUser.getMessageCount()) &&
678                     (posts >= maxPosts)) {
679 
680                     rank[0] = curRank;
681                     maxPosts = posts;
682                 }
683 
684             }
685             else {
686                 String entityType = curRankValueKvp[0];
687                 String entityValue = curRankValueKvp[1];
688 
689                 try {
690                     if (_isEntityRank(
691                             companyId, statsUser, entityType, entityValue)) {
692 
693                         rank[1] = curRank;
694 
695                         break;
696                     }
697                 }
698                 catch (Exception e) {
699                     if (_log.isWarnEnabled()) {
700                         _log.warn(e);
701                     }
702                 }
703             }
704         }
705 
706         return rank;
707     }
708 
709     public static boolean hasMailIdHeader(Message message) throws Exception {
710         String[] messageIds = message.getHeader("Message-ID");
711 
712         if (messageIds == null) {
713             return false;
714         }
715 
716         for (String messageId : messageIds) {
717             if (Validator.isNotNull(PropsValues.POP_SERVER_SUBDOMAIN) &&
718                 messageId.contains(PropsValues.POP_SERVER_SUBDOMAIN)) {
719 
720                 return true;
721             }
722         }
723 
724         return false;
725     }
726 
727     public static boolean isAllowAnonymousPosting(
728         PortletPreferences preferences) {
729 
730         String allowAnonymousPosting = preferences.getValue(
731             "allow-anonymous-posting", StringPool.BLANK);
732 
733         if (Validator.isNotNull(allowAnonymousPosting)) {
734             return GetterUtil.getBoolean(allowAnonymousPosting);
735         }
736         else {
737             return PropsValues.MESSAGE_BOARDS_ANONYMOUS_POSTING_ENABLED;
738         }
739     }
740 
741     private static String[] _findThreadPriority(
742         double value, ThemeDisplay themeDisplay, String[] priorities) {
743 
744         for (int i = 0; i < priorities.length; i++) {
745             String[] priority = StringUtil.split(priorities[i]);
746 
747             try {
748                 String priorityName = priority[0];
749                 String priorityImage = priority[1];
750                 double priorityValue = GetterUtil.getDouble(priority[2]);
751 
752                 if (value == priorityValue) {
753                     if (!priorityImage.startsWith(Http.HTTP)) {
754                         priorityImage =
755                             themeDisplay.getPathThemeImages() + priorityImage;
756                     }
757 
758                     return new String[] {priorityName, priorityImage};
759                 }
760             }
761             catch (Exception e) {
762                 _log.error("Unable to determine thread priority", e);
763             }
764         }
765 
766         return null;
767     }
768 
769     private static String _getParentMessageIdFromSubject(Message message)
770         throws Exception {
771 
772         String parentMessageId = null;
773 
774         String subject = StringUtil.reverse(message.getSubject());
775 
776         int pos = subject.indexOf(StringPool.LESS_THAN);
777 
778         if (pos != -1) {
779             parentMessageId = StringUtil.reverse(subject.substring(0, pos + 1));
780         }
781 
782         return parentMessageId;
783     }
784 
785     private static boolean _isEntityRank(
786             long companyId, MBStatsUser statsUser, String entityType,
787             String entityValue)
788         throws Exception {
789 
790         long groupId = statsUser.getGroupId();
791         long userId = statsUser.getUserId();
792 
793         if (entityType.equals("community-role") ||
794             entityType.equals("organization-role")) {
795 
796             Role role = RoleLocalServiceUtil.getRole(companyId, entityValue);
797 
798             if (UserGroupRoleLocalServiceUtil.hasUserGroupRole(
799                     userId, groupId, role.getRoleId(), true)) {
800 
801                 return true;
802             }
803         }
804         else if (entityType.equals("organization")) {
805             Organization organization =
806                 OrganizationLocalServiceUtil.getOrganization(
807                     companyId, entityValue);
808 
809             if (OrganizationLocalServiceUtil.hasUserOrganization(
810                     userId, organization.getOrganizationId(), false, true,
811                     false)) {
812 
813                 return true;
814             }
815         }
816         else if (entityType.equals("regular-role")) {
817             if (RoleLocalServiceUtil.hasUserRole(
818                     userId, companyId, entityValue, true)) {
819 
820                 return true;
821             }
822         }
823         else if (entityType.equals("user-group")) {
824             UserGroup userGroup = UserGroupLocalServiceUtil.getUserGroup(
825                 companyId, entityValue);
826 
827             if (UserLocalServiceUtil.hasUserGroupUser(
828                     userGroup.getUserGroupId(), userId)) {
829 
830                 return true;
831             }
832         }
833 
834         return false;
835     }
836 
837     private static Log _log = LogFactoryUtil.getLog(MBUtil.class);
838 
839 }