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                 mailingListAddress = mailingList.getEmailAddress();
465             }
466             catch (Exception e) {
467             }
468 
469             return mailingListAddress;
470         }
471 
472         StringBuilder sb = new StringBuilder();
473 
474         sb.append(POP_PORTLET_PREFIX);
475         sb.append(categoryId);
476         sb.append(StringPool.PERIOD);
477         sb.append(messageId);
478         sb.append(StringPool.AT);
479         sb.append(PropsValues.POP_SERVER_SUBDOMAIN);
480         sb.append(StringPool.PERIOD);
481         sb.append(mx);
482 
483         return sb.toString();
484     }
485 
486     public static long getMessageId(String mailId) {
487         int x = mailId.indexOf(StringPool.LESS_THAN) + 1;
488         int y = mailId.indexOf(StringPool.AT);
489 
490         long messageId = 0;
491 
492         if ((x > 0 ) && (y != -1)) {
493             String temp = mailId.substring(x, y);
494 
495             int z = temp.lastIndexOf(StringPool.PERIOD);
496 
497             if (z != -1) {
498                 messageId = GetterUtil.getLong(temp.substring(z + 1));
499             }
500         }
501 
502         return messageId;
503     }
504 
505     public static long getParentMessageId(Message message) throws Exception {
506         long parentMessageId = -1;
507 
508         String parentHeader = getParentMessageIdString(message);
509 
510         if (parentHeader != null) {
511             if (_log.isDebugEnabled()) {
512                 _log.debug("Parent header " + parentHeader);
513             }
514 
515             parentMessageId = getMessageId(parentHeader);
516 
517             if (_log.isDebugEnabled()) {
518                 _log.debug("Previous message id " + parentMessageId);
519             }
520         }
521 
522         return parentMessageId;
523     }
524 
525     public static String getParentMessageIdString(Message message)
526         throws Exception {
527 
528         // If the previous block failed, try to get the parent message ID from
529         // the "References" header as explained in
530         // http://cr.yp.to/immhf/thread.html. Some mail clients such as Yahoo!
531         // Mail use the "In-Reply-To" header, so we check that as well.
532 
533         String parentHeader = null;
534 
535         String[] references = message.getHeader("References");
536 
537         if ((references != null) && (references.length > 0)) {
538             String reference = references[0];
539 
540             int x = reference.lastIndexOf("<mb.");
541 
542             if (x > -1) {
543                 int y = reference.indexOf(">", x);
544 
545                 parentHeader = reference.substring(x, y);
546             }
547         }
548 
549         if (parentHeader == null) {
550             String[] inReplyToHeaders = message.getHeader("In-Reply-To");
551 
552             if ((inReplyToHeaders != null) && (inReplyToHeaders.length > 0)) {
553                 parentHeader = inReplyToHeaders[0];
554             }
555         }
556 
557         if (Validator.isNull(parentHeader) ||
558             !parentHeader.startsWith(POP_PORTLET_PREFIX, 1)) {
559 
560             parentHeader = _getParentMessageIdFromSubject(message);
561         }
562 
563         return parentHeader;
564     }
565 
566     public static String getSubjectWithoutMessageId(Message message)
567         throws Exception {
568 
569         String subject = message.getSubject();
570 
571         String parentMessageId = _getParentMessageIdFromSubject(message);
572 
573         if (Validator.isNotNull(parentMessageId)) {
574             int pos = subject.indexOf(parentMessageId);
575 
576             if (pos != -1) {
577                 subject = subject.substring(0, pos);
578             }
579         }
580 
581         return subject;
582     }
583 
584     public static String[] getThreadPriority(
585             PortletPreferences preferences, String languageId, double value,
586             ThemeDisplay themeDisplay)
587         throws Exception {
588 
589         String[] priorities = LocalizationUtil.getPreferencesValues(
590             preferences, "priorities", languageId);
591 
592         String[] priorityPair = _findThreadPriority(
593             value, themeDisplay, priorities);
594 
595         if (priorityPair == null) {
596             String defaultLanguageId = LocaleUtil.toLanguageId(
597                 LocaleUtil.getDefault());
598 
599             priorities = LocalizationUtil.getPreferencesValues(
600                 preferences, "priorities", defaultLanguageId);
601 
602             priorityPair = _findThreadPriority(value, themeDisplay, priorities);
603         }
604 
605         return priorityPair;
606     }
607 
608     public static Date getUnbanDate(MBBan ban, int expireInterval) {
609         Date banDate = ban.getCreateDate();
610 
611         Calendar cal = Calendar.getInstance();
612 
613         cal.setTime(banDate);
614 
615         cal.add(Calendar.DATE, expireInterval);
616 
617         return cal.getTime();
618     }
619 
620     public static String getUserRank(
621             PortletPreferences preferences, String languageId, int posts)
622         throws Exception {
623 
624         String rank = StringPool.BLANK;
625 
626         String[] ranks = LocalizationUtil.getPreferencesValues(
627             preferences, "ranks", languageId);
628 
629         for (int i = 0; i < ranks.length; i++) {
630             String[] kvp = StringUtil.split(ranks[i], StringPool.EQUAL);
631 
632             String kvpName = kvp[0];
633             int kvpPosts = GetterUtil.getInteger(kvp[1]);
634 
635             if (posts >= kvpPosts) {
636                 rank = kvpName;
637             }
638             else {
639                 break;
640             }
641         }
642 
643         return rank;
644     }
645 
646     public static String[] getUserRank(
647             PortletPreferences preferences, String languageId,
648             MBStatsUser statsUser)
649         throws Exception {
650 
651         String[] rank = {StringPool.BLANK, StringPool.BLANK};
652 
653         int maxPosts = 0;
654 
655         Group group = GroupLocalServiceUtil.getGroup(
656             statsUser.getGroupId());
657 
658         long companyId = group.getCompanyId();
659 
660         String[] ranks = LocalizationUtil.getPreferencesValues(
661             preferences, "ranks", languageId);
662 
663         for (int i = 0; i < ranks.length; i++) {
664             String[] kvp = StringUtil.split(ranks[i], StringPool.EQUAL);
665 
666             String curRank = kvp[0];
667             String curRankValue = kvp[1];
668 
669             String[] curRankValueKvp = StringUtil.split(
670                 curRankValue, StringPool.COLON);
671 
672             if (curRankValueKvp.length <= 1) {
673                 int posts = GetterUtil.getInteger(curRankValue);
674 
675                 if ((posts <= statsUser.getMessageCount()) &&
676                     (posts >= maxPosts)) {
677 
678                     rank[0] = curRank;
679                     maxPosts = posts;
680                 }
681 
682             }
683             else {
684                 String entityType = curRankValueKvp[0];
685                 String entityValue = curRankValueKvp[1];
686 
687                 try {
688                     if (_isEntityRank(
689                             companyId, statsUser, entityType, entityValue)) {
690 
691                         rank[1] = curRank;
692 
693                         break;
694                     }
695                 }
696                 catch (Exception e) {
697                     if (_log.isWarnEnabled()) {
698                         _log.warn(e);
699                     }
700                 }
701             }
702         }
703 
704         return rank;
705     }
706 
707     public static boolean hasMailIdHeader(Message message) throws Exception {
708         String[] messageIds = message.getHeader("Message-ID");
709 
710         if (messageIds == null) {
711             return false;
712         }
713 
714         for (String messageId : messageIds) {
715             if (Validator.isNotNull(PropsValues.POP_SERVER_SUBDOMAIN) &&
716                 messageId.contains(PropsValues.POP_SERVER_SUBDOMAIN)) {
717 
718                 return true;
719             }
720         }
721 
722         return false;
723     }
724 
725     public static boolean isAllowAnonymousPosting(
726         PortletPreferences preferences) {
727 
728         String allowAnonymousPosting = preferences.getValue(
729             "allow-anonymous-posting", StringPool.BLANK);
730 
731         if (Validator.isNotNull(allowAnonymousPosting)) {
732             return GetterUtil.getBoolean(allowAnonymousPosting);
733         }
734         else {
735             return PropsValues.MESSAGE_BOARDS_ANONYMOUS_POSTING_ENABLED;
736         }
737     }
738 
739     private static String[] _findThreadPriority(
740         double value, ThemeDisplay themeDisplay, String[] priorities) {
741 
742         for (int i = 0; i < priorities.length; i++) {
743             String[] priority = StringUtil.split(priorities[i]);
744 
745             try {
746                 String priorityName = priority[0];
747                 String priorityImage = priority[1];
748                 double priorityValue = GetterUtil.getDouble(priority[2]);
749 
750                 if (value == priorityValue) {
751                     if (!priorityImage.startsWith(Http.HTTP)) {
752                         priorityImage =
753                             themeDisplay.getPathThemeImages() + priorityImage;
754                     }
755 
756                     return new String[] {priorityName, priorityImage};
757                 }
758             }
759             catch (Exception e) {
760                 _log.error("Unable to determine thread priority", e);
761             }
762         }
763 
764         return null;
765     }
766 
767     private static String _getParentMessageIdFromSubject(Message message)
768         throws Exception {
769 
770         String parentMessageId = null;
771 
772         String subject = StringUtil.reverse(message.getSubject());
773 
774         int pos = subject.indexOf(StringPool.LESS_THAN);
775 
776         if (pos != -1) {
777             parentMessageId = StringUtil.reverse(subject.substring(0, pos + 1));
778         }
779 
780         return parentMessageId;
781     }
782 
783     private static boolean _isEntityRank(
784             long companyId, MBStatsUser statsUser, String entityType,
785             String entityValue)
786         throws Exception {
787 
788         long groupId = statsUser.getGroupId();
789         long userId = statsUser.getUserId();
790 
791         if (entityType.equals("community-role") ||
792             entityType.equals("organization-role")) {
793 
794             Role role = RoleLocalServiceUtil.getRole(companyId, entityValue);
795 
796             if (UserGroupRoleLocalServiceUtil.hasUserGroupRole(
797                     userId, groupId, role.getRoleId(), true)) {
798 
799                 return true;
800             }
801         }
802         else if (entityType.equals("organization")) {
803             Organization organization =
804                 OrganizationLocalServiceUtil.getOrganization(
805                     companyId, entityValue);
806 
807             if (OrganizationLocalServiceUtil.hasUserOrganization(
808                     userId, organization.getOrganizationId(), false, true,
809                     false)) {
810 
811                 return true;
812             }
813         }
814         else if (entityType.equals("regular-role")) {
815             if (RoleLocalServiceUtil.hasUserRole(
816                     userId, companyId, entityValue, true)) {
817 
818                 return true;
819             }
820         }
821         else if (entityType.equals("user-group")) {
822             UserGroup userGroup = UserGroupLocalServiceUtil.getUserGroup(
823                 companyId, entityValue);
824 
825             if (UserLocalServiceUtil.hasUserGroupUser(
826                     userGroup.getUserGroupId(), userId)) {
827 
828                 return true;
829             }
830         }
831 
832         return false;
833     }
834 
835     private static Log _log = LogFactoryUtil.getLog(MBUtil.class);
836 
837 }