001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portlet.messageboards.util;
016    
017    import com.liferay.portal.kernel.exception.SystemException;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.portlet.LiferayWindowState;
021    import com.liferay.portal.kernel.util.CharPool;
022    import com.liferay.portal.kernel.util.GetterUtil;
023    import com.liferay.portal.kernel.util.Http;
024    import com.liferay.portal.kernel.util.LocaleUtil;
025    import com.liferay.portal.kernel.util.LocalizationUtil;
026    import com.liferay.portal.kernel.util.ParamUtil;
027    import com.liferay.portal.kernel.util.PropsUtil;
028    import com.liferay.portal.kernel.util.StringBundler;
029    import com.liferay.portal.kernel.util.StringPool;
030    import com.liferay.portal.kernel.util.StringUtil;
031    import com.liferay.portal.kernel.util.Validator;
032    import com.liferay.portal.model.Group;
033    import com.liferay.portal.model.Organization;
034    import com.liferay.portal.model.Role;
035    import com.liferay.portal.model.UserGroup;
036    import com.liferay.portal.security.permission.ActionKeys;
037    import com.liferay.portal.security.permission.PermissionChecker;
038    import com.liferay.portal.service.GroupLocalServiceUtil;
039    import com.liferay.portal.service.OrganizationLocalServiceUtil;
040    import com.liferay.portal.service.RoleLocalServiceUtil;
041    import com.liferay.portal.service.UserGroupLocalServiceUtil;
042    import com.liferay.portal.service.UserGroupRoleLocalServiceUtil;
043    import com.liferay.portal.service.UserLocalServiceUtil;
044    import com.liferay.portal.theme.ThemeDisplay;
045    import com.liferay.portal.util.PortalUtil;
046    import com.liferay.portal.util.PropsValues;
047    import com.liferay.portal.util.WebKeys;
048    import com.liferay.portlet.messageboards.model.MBBan;
049    import com.liferay.portlet.messageboards.model.MBCategory;
050    import com.liferay.portlet.messageboards.model.MBCategoryConstants;
051    import com.liferay.portlet.messageboards.model.MBMailingList;
052    import com.liferay.portlet.messageboards.model.MBMessage;
053    import com.liferay.portlet.messageboards.model.MBMessageConstants;
054    import com.liferay.portlet.messageboards.model.MBStatsUser;
055    import com.liferay.portlet.messageboards.service.MBCategoryLocalServiceUtil;
056    import com.liferay.portlet.messageboards.service.MBMailingListLocalServiceUtil;
057    import com.liferay.portlet.messageboards.service.permission.MBMessagePermission;
058    import com.liferay.util.ContentUtil;
059    import com.liferay.util.mail.JavaMailUtil;
060    
061    import java.io.InputStream;
062    
063    import java.util.Calendar;
064    import java.util.Collections;
065    import java.util.Date;
066    import java.util.List;
067    
068    import javax.mail.BodyPart;
069    import javax.mail.Message;
070    import javax.mail.Part;
071    import javax.mail.internet.MimeMessage;
072    import javax.mail.internet.MimeMultipart;
073    
074    import javax.portlet.PortletPreferences;
075    import javax.portlet.PortletURL;
076    import javax.portlet.RenderResponse;
077    
078    import javax.servlet.http.HttpServletRequest;
079    
080    /**
081     * @author Brian Wing Shun Chan
082     */
083    public class MBUtil {
084    
085            public static final String BB_CODE_EDITOR_WYSIWYG_IMPL_KEY =
086                    "editor.wysiwyg.portal-web.docroot.html.portlet.message_boards." +
087                            "edit_message.bb_code.jsp";
088    
089            public static final String MESSAGE_POP_PORTLET_PREFIX = "mb_message.";
090    
091            public static void addPortletBreadcrumbEntries(
092                            long categoryId, HttpServletRequest request,
093                            RenderResponse renderResponse)
094                    throws Exception {
095    
096                    if ((categoryId == MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) ||
097                            (categoryId == MBCategoryConstants.DISCUSSION_CATEGORY_ID)) {
098    
099                            return;
100                    }
101    
102                    MBCategory category = MBCategoryLocalServiceUtil.getCategory(
103                            categoryId);
104    
105                    addPortletBreadcrumbEntries(category, request, renderResponse);
106            }
107    
108            public static void addPortletBreadcrumbEntries(
109                            MBCategory category, HttpServletRequest request,
110                            RenderResponse renderResponse)
111                    throws Exception {
112    
113                    String strutsAction = ParamUtil.getString(request, "struts_action");
114    
115                    PortletURL portletURL = renderResponse.createRenderURL();
116    
117                    if (strutsAction.equals("/message_boards/select_category") ||
118                            strutsAction.equals("/message_boards_admin/select_category")) {
119    
120                            ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
121                                    WebKeys.THEME_DISPLAY);
122    
123                            portletURL.setWindowState(LiferayWindowState.POP_UP);
124    
125                            portletURL.setParameter(
126                                    "struts_action", "/message_boards/select_category");
127    
128                            PortalUtil.addPortletBreadcrumbEntry(
129                                    request, themeDisplay.translate("categories"),
130                                    portletURL.toString());
131                    }
132                    else {
133                            portletURL.setParameter("struts_action", "/message_boards/view");
134                    }
135    
136                    List<MBCategory> ancestorCategories = category.getAncestors();
137    
138                    Collections.reverse(ancestorCategories);
139    
140                    for (MBCategory curCategory : ancestorCategories) {
141                            portletURL.setParameter(
142                                    "mbCategoryId", String.valueOf(curCategory.getCategoryId()));
143    
144                            PortalUtil.addPortletBreadcrumbEntry(
145                                    request, curCategory.getName(), portletURL.toString());
146                    }
147    
148                    portletURL.setParameter(
149                            "mbCategoryId", String.valueOf(category.getCategoryId()));
150    
151                    PortalUtil.addPortletBreadcrumbEntry(
152                            request, category.getName(), portletURL.toString());
153            }
154    
155            public static void addPortletBreadcrumbEntries(
156                            MBMessage message, HttpServletRequest request,
157                            RenderResponse renderResponse)
158                    throws Exception {
159    
160                    if ((message.getCategoryId() ==
161                                    MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) ||
162                            (message.getCategoryId() ==
163                                    MBCategoryConstants.DISCUSSION_CATEGORY_ID)) {
164    
165                            return;
166                    }
167    
168                    MBCategory category = message.getCategory();
169    
170                    addPortletBreadcrumbEntries(category, request, renderResponse);
171    
172                    PortletURL portletURL = renderResponse.createRenderURL();
173    
174                    portletURL.setParameter(
175                            "struts_action", "/message_boards/view_message");
176                    portletURL.setParameter(
177                            "messageId", String.valueOf(message.getMessageId()));
178    
179                    PortalUtil.addPortletBreadcrumbEntry(
180                            request, message.getSubject(), portletURL.toString());
181            }
182    
183            public static void collectMultipartContent(
184                            MimeMultipart multipart, MBMailMessage collector)
185                    throws Exception {
186    
187                    for (int i = 0; i < multipart.getCount(); i++) {
188                            BodyPart part = multipart.getBodyPart(i);
189    
190                            collectPartContent(part, collector);
191                    }
192            }
193    
194            public static void collectPartContent(
195                            Part part, MBMailMessage mbMailMessage)
196                    throws Exception {
197    
198                    Object partContent = part.getContent();
199    
200                    String contentType = part.getContentType().toLowerCase();
201    
202                    if ((part.getDisposition() != null) &&
203                            part.getDisposition().equalsIgnoreCase(MimeMessage.ATTACHMENT)) {
204    
205                            if (_log.isDebugEnabled()) {
206                                    _log.debug("Processing attachment");
207                            }
208    
209                            byte[] bytes = null;
210    
211                            if (partContent instanceof String) {
212                                    bytes = ((String)partContent).getBytes();
213                            }
214                            else if (partContent instanceof InputStream) {
215                                    bytes = JavaMailUtil.getBytes(part);
216                            }
217    
218                            mbMailMessage.addBytes(part.getFileName(), bytes);
219                    }
220                    else {
221                            if (partContent instanceof MimeMultipart) {
222                                    MimeMultipart mimeMultipart = (MimeMultipart)partContent;
223    
224                                    collectMultipartContent(mimeMultipart, mbMailMessage);
225                            }
226                            else if (partContent instanceof String) {
227                                    if (contentType.startsWith("text/html")) {
228                                            mbMailMessage.setHtmlBody((String)partContent);
229                                    }
230                                    else {
231                                            mbMailMessage.setPlainBody((String)partContent);
232                                    }
233                            }
234                    }
235            }
236    
237            public static long getCategoryId(
238                    HttpServletRequest request, MBCategory category) {
239    
240                    long categoryId = MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID;
241    
242                    if (category != null) {
243                            categoryId = category.getCategoryId();
244                    }
245    
246                    categoryId = ParamUtil.getLong(request, "mbCategoryId", categoryId);
247    
248                    return categoryId;
249            }
250    
251            public static long getCategoryId(
252                    HttpServletRequest request, MBMessage message) {
253    
254                    long categoryId = MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID;
255    
256                    if (message != null) {
257                            categoryId = message.getCategoryId();
258                    }
259    
260                    categoryId = ParamUtil.getLong(request, "mbCategoryId", categoryId);
261    
262                    return categoryId;
263            }
264    
265            public static String getEmailFromAddress(
266                            PortletPreferences preferences, long companyId)
267                    throws SystemException {
268    
269                    return PortalUtil.getEmailFromAddress(
270                            preferences, companyId,
271                            PropsValues.MESSAGE_BOARDS_EMAIL_FROM_ADDRESS);
272            }
273    
274            public static String getEmailFromName(
275                            PortletPreferences preferences, long companyId)
276                    throws SystemException {
277    
278                    return PortalUtil.getEmailFromName(
279                            preferences, companyId, PropsValues.MESSAGE_BOARDS_EMAIL_FROM_NAME);
280            }
281    
282            public static boolean getEmailHtmlFormat(PortletPreferences preferences) {
283                    String emailHtmlFormat = preferences.getValue(
284                            "emailHtmlFormat", StringPool.BLANK);
285    
286                    if (Validator.isNotNull(emailHtmlFormat)) {
287                            return GetterUtil.getBoolean(emailHtmlFormat);
288                    }
289                    else {
290                            return PropsValues.MESSAGE_BOARDS_EMAIL_HTML_FORMAT;
291                    }
292            }
293    
294            public static String getEmailMessageAddedBody(
295                    PortletPreferences preferences) {
296    
297                    String emailMessageAddedBody = preferences.getValue(
298                            "emailMessageAddedBody", StringPool.BLANK);
299    
300                    if (Validator.isNotNull(emailMessageAddedBody)) {
301                            return emailMessageAddedBody;
302                    }
303                    else {
304                            return ContentUtil.get(
305                                    PropsValues.MESSAGE_BOARDS_EMAIL_MESSAGE_ADDED_BODY);
306                    }
307            }
308    
309            public static boolean getEmailMessageAddedEnabled(
310                    PortletPreferences preferences) {
311    
312                    String emailMessageAddedEnabled = preferences.getValue(
313                            "emailMessageAddedEnabled", StringPool.BLANK);
314    
315                    if (Validator.isNotNull(emailMessageAddedEnabled)) {
316                            return GetterUtil.getBoolean(emailMessageAddedEnabled);
317                    }
318                    else {
319                            return PropsValues.MESSAGE_BOARDS_EMAIL_MESSAGE_ADDED_ENABLED;
320                    }
321            }
322    
323            public static String getEmailMessageAddedSignature(
324                    PortletPreferences preferences) {
325    
326                    String emailMessageAddedSignature = preferences.getValue(
327                            "emailMessageAddedSignature", StringPool.BLANK);
328    
329                    if (Validator.isNotNull(emailMessageAddedSignature)) {
330                            return emailMessageAddedSignature;
331                    }
332                    else {
333                            return ContentUtil.get(
334                                    PropsValues.MESSAGE_BOARDS_EMAIL_MESSAGE_ADDED_SIGNATURE);
335                    }
336            }
337    
338            public static String getEmailMessageAddedSubjectPrefix(
339                    PortletPreferences preferences) {
340    
341                    String emailMessageAddedSubjectPrefix = preferences.getValue(
342                            "emailMessageAddedSubjectPrefix", StringPool.BLANK);
343    
344                    if (Validator.isNotNull(emailMessageAddedSubjectPrefix)) {
345                            return emailMessageAddedSubjectPrefix;
346                    }
347                    else {
348                            return ContentUtil.get(
349                                    PropsValues.MESSAGE_BOARDS_EMAIL_MESSAGE_ADDED_SUBJECT_PREFIX);
350                    }
351            }
352    
353            public static String getEmailMessageUpdatedBody(
354                    PortletPreferences preferences) {
355    
356                    String emailMessageUpdatedBody = preferences.getValue(
357                            "emailMessageUpdatedBody", StringPool.BLANK);
358    
359                    if (Validator.isNotNull(emailMessageUpdatedBody)) {
360                            return emailMessageUpdatedBody;
361                    }
362                    else {
363                            return ContentUtil.get(
364                                    PropsValues.MESSAGE_BOARDS_EMAIL_MESSAGE_UPDATED_BODY);
365                    }
366            }
367    
368            public static boolean getEmailMessageUpdatedEnabled(
369                    PortletPreferences preferences) {
370    
371                    String emailMessageUpdatedEnabled = preferences.getValue(
372                            "emailMessageUpdatedEnabled", StringPool.BLANK);
373    
374                    if (Validator.isNotNull(emailMessageUpdatedEnabled)) {
375                            return GetterUtil.getBoolean(emailMessageUpdatedEnabled);
376                    }
377                    else {
378                            return PropsValues.MESSAGE_BOARDS_EMAIL_MESSAGE_UPDATED_ENABLED;
379                    }
380            }
381    
382            public static String getEmailMessageUpdatedSignature(
383                    PortletPreferences preferences) {
384    
385                    String emailMessageUpdatedSignature = preferences.getValue(
386                            "emailMessageUpdatedSignature", StringPool.BLANK);
387    
388                    if (Validator.isNotNull(emailMessageUpdatedSignature)) {
389                            return emailMessageUpdatedSignature;
390                    }
391                    else {
392                            return ContentUtil.get(
393                                    PropsValues.MESSAGE_BOARDS_EMAIL_MESSAGE_UPDATED_SIGNATURE);
394                    }
395            }
396    
397            public static String getEmailMessageUpdatedSubjectPrefix(
398                    PortletPreferences preferences) {
399    
400                    String emailMessageUpdatedSubject = preferences.getValue(
401                            "emailMessageUpdatedSubjectPrefix", StringPool.BLANK);
402    
403                    if (Validator.isNotNull(emailMessageUpdatedSubject)) {
404                            return emailMessageUpdatedSubject;
405                    }
406                    else {
407                            return ContentUtil.get(
408                                    PropsValues.
409                                            MESSAGE_BOARDS_EMAIL_MESSAGE_UPDATED_SUBJECT_PREFIX);
410                    }
411            }
412    
413            public static String getMailingListAddress(
414                    long groupId, long categoryId, long messageId, String mx,
415                    String defaultMailingListAddress) {
416    
417                    if (PropsValues.POP_SERVER_SUBDOMAIN.length() <= 0) {
418                            String mailingListAddress = defaultMailingListAddress;
419    
420                            try {
421                                    MBMailingList mailingList =
422                                            MBMailingListLocalServiceUtil.getCategoryMailingList(
423                                                    groupId, categoryId);
424    
425                                    if (mailingList.isActive()) {
426                                            mailingListAddress = mailingList.getEmailAddress();
427                                    }
428                            }
429                            catch (Exception e) {
430                            }
431    
432                            return mailingListAddress;
433                    }
434    
435                    StringBundler sb = new StringBundler(8);
436    
437                    sb.append(MESSAGE_POP_PORTLET_PREFIX);
438                    sb.append(categoryId);
439                    sb.append(StringPool.PERIOD);
440                    sb.append(messageId);
441                    sb.append(StringPool.AT);
442                    sb.append(PropsValues.POP_SERVER_SUBDOMAIN);
443                    sb.append(StringPool.PERIOD);
444                    sb.append(mx);
445    
446                    return sb.toString();
447            }
448    
449            public static String getMessageFormat(PortletPreferences preferences) {
450                    String messageFormat = preferences.getValue(
451                            "messageFormat", MBMessageConstants.DEFAULT_FORMAT);
452    
453                    String editorImpl = PropsUtil.get(BB_CODE_EDITOR_WYSIWYG_IMPL_KEY);
454    
455                    if (messageFormat.equals("bbcode") &&
456                            !(editorImpl.equals("bbcode") ||
457                              editorImpl.equals("ckeditor_bbcode"))) {
458    
459                            messageFormat = "html";
460                    }
461    
462                    return messageFormat;
463            }
464    
465            public static long getMessageId(String mailId) {
466                    int x = mailId.indexOf(CharPool.LESS_THAN) + 1;
467                    int y = mailId.indexOf(CharPool.AT);
468    
469                    long messageId = 0;
470    
471                    if ((x > 0 ) && (y != -1)) {
472                            String temp = mailId.substring(x, y);
473    
474                            int z = temp.lastIndexOf(CharPool.PERIOD);
475    
476                            if (z != -1) {
477                                    messageId = GetterUtil.getLong(temp.substring(z + 1));
478                            }
479                    }
480    
481                    return messageId;
482            }
483    
484            public static long getParentMessageId(Message message) throws Exception {
485                    long parentMessageId = -1;
486    
487                    String parentHeader = getParentMessageIdString(message);
488    
489                    if (parentHeader != null) {
490                            if (_log.isDebugEnabled()) {
491                                    _log.debug("Parent header " + parentHeader);
492                            }
493    
494                            parentMessageId = getMessageId(parentHeader);
495    
496                            if (_log.isDebugEnabled()) {
497                                    _log.debug("Previous message id " + parentMessageId);
498                            }
499                    }
500    
501                    return parentMessageId;
502            }
503    
504            public static String getParentMessageIdString(Message message)
505                    throws Exception {
506    
507                    // If the previous block failed, try to get the parent message ID from
508                    // the "References" header as explained in
509                    // http://cr.yp.to/immhf/thread.html. Some mail clients such as Yahoo!
510                    // Mail use the "In-Reply-To" header, so we check that as well.
511    
512                    String parentHeader = null;
513    
514                    String[] references = message.getHeader("References");
515    
516                    if ((references != null) && (references.length > 0)) {
517                            String reference = references[0];
518    
519                            int x = reference.lastIndexOf("<mb.");
520    
521                            if (x > -1) {
522                                    int y = reference.indexOf(">", x);
523    
524                                    parentHeader = reference.substring(x, y);
525                            }
526                    }
527    
528                    if (parentHeader == null) {
529                            String[] inReplyToHeaders = message.getHeader("In-Reply-To");
530    
531                            if ((inReplyToHeaders != null) && (inReplyToHeaders.length > 0)) {
532                                    parentHeader = inReplyToHeaders[0];
533                            }
534                    }
535    
536                    if (Validator.isNull(parentHeader) ||
537                            !parentHeader.startsWith(MESSAGE_POP_PORTLET_PREFIX, 1)) {
538    
539                            parentHeader = _getParentMessageIdFromSubject(message);
540                    }
541    
542                    return parentHeader;
543            }
544    
545            public static String getSubjectWithoutMessageId(Message message)
546                    throws Exception {
547    
548                    String subject = message.getSubject();
549    
550                    String parentMessageId = _getParentMessageIdFromSubject(message);
551    
552                    if (Validator.isNotNull(parentMessageId)) {
553                            int pos = subject.indexOf(parentMessageId);
554    
555                            if (pos != -1) {
556                                    subject = subject.substring(0, pos);
557                            }
558                    }
559    
560                    return subject;
561            }
562    
563            public static String[] getThreadPriority(
564                            PortletPreferences preferences, String languageId, double value,
565                            ThemeDisplay themeDisplay)
566                    throws Exception {
567    
568                    String[] priorities = LocalizationUtil.getPreferencesValues(
569                            preferences, "priorities", languageId);
570    
571                    String[] priorityPair = _findThreadPriority(
572                            value, themeDisplay, priorities);
573    
574                    if (priorityPair == null) {
575                            String defaultLanguageId = LocaleUtil.toLanguageId(
576                                    LocaleUtil.getDefault());
577    
578                            priorities = LocalizationUtil.getPreferencesValues(
579                                    preferences, "priorities", defaultLanguageId);
580    
581                            priorityPair = _findThreadPriority(value, themeDisplay, priorities);
582                    }
583    
584                    return priorityPair;
585            }
586    
587            public static Date getUnbanDate(MBBan ban, int expireInterval) {
588                    Date banDate = ban.getCreateDate();
589    
590                    Calendar cal = Calendar.getInstance();
591    
592                    cal.setTime(banDate);
593    
594                    cal.add(Calendar.DATE, expireInterval);
595    
596                    return cal.getTime();
597            }
598    
599            public static String getUserRank(
600                            PortletPreferences preferences, String languageId, int posts)
601                    throws Exception {
602    
603                    String rank = StringPool.BLANK;
604    
605                    String[] ranks = LocalizationUtil.getPreferencesValues(
606                            preferences, "ranks", languageId);
607    
608                    for (int i = 0; i < ranks.length; i++) {
609                            String[] kvp = StringUtil.split(ranks[i], CharPool.EQUAL);
610    
611                            String kvpName = kvp[0];
612                            int kvpPosts = GetterUtil.getInteger(kvp[1]);
613    
614                            if (posts >= kvpPosts) {
615                                    rank = kvpName;
616                            }
617                            else {
618                                    break;
619                            }
620                    }
621    
622                    return rank;
623            }
624    
625            public static String[] getUserRank(
626                            PortletPreferences preferences, String languageId,
627                            MBStatsUser statsUser)
628                    throws Exception {
629    
630                    String[] rank = {StringPool.BLANK, StringPool.BLANK};
631    
632                    int maxPosts = 0;
633    
634                    Group group = GroupLocalServiceUtil.getGroup(statsUser.getGroupId());
635    
636                    long companyId = group.getCompanyId();
637    
638                    String[] ranks = LocalizationUtil.getPreferencesValues(
639                            preferences, "ranks", languageId);
640    
641                    for (int i = 0; i < ranks.length; i++) {
642                            String[] kvp = StringUtil.split(ranks[i], CharPool.EQUAL);
643    
644                            String curRank = kvp[0];
645                            String curRankValue = kvp[1];
646    
647                            String[] curRankValueKvp = StringUtil.split(
648                                    curRankValue, CharPool.COLON);
649    
650                            if (curRankValueKvp.length <= 1) {
651                                    int posts = GetterUtil.getInteger(curRankValue);
652    
653                                    if ((posts <= statsUser.getMessageCount()) &&
654                                            (posts >= maxPosts)) {
655    
656                                            rank[0] = curRank;
657                                            maxPosts = posts;
658                                    }
659    
660                            }
661                            else {
662                                    String entityType = curRankValueKvp[0];
663                                    String entityValue = curRankValueKvp[1];
664    
665                                    try {
666                                            if (_isEntityRank(
667                                                            companyId, statsUser, entityType, entityValue)) {
668    
669                                                    rank[1] = curRank;
670    
671                                                    break;
672                                            }
673                                    }
674                                    catch (Exception e) {
675                                            if (_log.isWarnEnabled()) {
676                                                    _log.warn(e);
677                                            }
678                                    }
679                            }
680                    }
681    
682                    return rank;
683            }
684    
685            public static boolean hasMailIdHeader(Message message) throws Exception {
686                    String[] messageIds = message.getHeader("Message-ID");
687    
688                    if (messageIds == null) {
689                            return false;
690                    }
691    
692                    for (String messageId : messageIds) {
693                            if (Validator.isNotNull(PropsValues.POP_SERVER_SUBDOMAIN) &&
694                                    messageId.contains(PropsValues.POP_SERVER_SUBDOMAIN)) {
695    
696                                    return true;
697                            }
698                    }
699    
700                    return false;
701            }
702    
703            public static boolean isAllowAnonymousPosting(
704                    PortletPreferences preferences) {
705    
706                    return GetterUtil.getBoolean(
707                            preferences.getValue("allowAnonymousPosting", null),
708                            PropsValues.MESSAGE_BOARDS_ANONYMOUS_POSTING_ENABLED);
709            }
710    
711            public static boolean isViewableMessage(
712                            ThemeDisplay themeDisplay, MBMessage message)
713                    throws Exception {
714    
715                    return isViewableMessage(themeDisplay, message, message);
716            }
717    
718            public static boolean isViewableMessage(
719                            ThemeDisplay themeDisplay, MBMessage message,
720                            MBMessage parentMessage)
721                    throws Exception {
722    
723                    PermissionChecker permissionChecker =
724                            themeDisplay.getPermissionChecker();
725    
726                    if (!MBMessagePermission.contains(
727                                    permissionChecker, parentMessage, ActionKeys.VIEW)) {
728    
729                            return false;
730                    }
731    
732                    if ((message.getMessageId() != parentMessage.getMessageId()) &&
733                            !MBMessagePermission.contains(
734                                    permissionChecker, message, ActionKeys.VIEW)) {
735    
736                            return false;
737                    }
738    
739                    if (!message.isApproved() &&
740                            !Validator.equals(message.getUserId(), themeDisplay.getUserId()) &&
741                            !permissionChecker.isGroupAdmin(themeDisplay.getScopeGroupId())) {
742    
743                            return false;
744                    }
745    
746                    return true;
747            }
748    
749            private static String[] _findThreadPriority(
750                    double value, ThemeDisplay themeDisplay, String[] priorities) {
751    
752                    for (int i = 0; i < priorities.length; i++) {
753                            String[] priority = StringUtil.split(priorities[i]);
754    
755                            try {
756                                    String priorityName = priority[0];
757                                    String priorityImage = priority[1];
758                                    double priorityValue = GetterUtil.getDouble(priority[2]);
759    
760                                    if (value == priorityValue) {
761                                            if (!priorityImage.startsWith(Http.HTTP)) {
762                                                    priorityImage =
763                                                            themeDisplay.getPathThemeImages() + priorityImage;
764                                            }
765    
766                                            return new String[] {priorityName, priorityImage};
767                                    }
768                            }
769                            catch (Exception e) {
770                                    _log.error("Unable to determine thread priority", e);
771                            }
772                    }
773    
774                    return null;
775            }
776    
777            private static String _getParentMessageIdFromSubject(Message message)
778                    throws Exception {
779    
780                    if (message.getSubject() == null) {
781                            return null;
782                    }
783    
784                    String parentMessageId = null;
785    
786                    String subject = StringUtil.reverse(message.getSubject());
787    
788                    int pos = subject.indexOf(CharPool.LESS_THAN);
789    
790                    if (pos != -1) {
791                            parentMessageId = StringUtil.reverse(subject.substring(0, pos + 1));
792                    }
793    
794                    return parentMessageId;
795            }
796    
797            private static boolean _isEntityRank(
798                            long companyId, MBStatsUser statsUser, String entityType,
799                            String entityValue)
800                    throws Exception {
801    
802                    long groupId = statsUser.getGroupId();
803                    long userId = statsUser.getUserId();
804    
805                    if (entityType.equals("organization-role") ||
806                            entityType.equals("site-role")) {
807    
808                            Role role = RoleLocalServiceUtil.getRole(companyId, entityValue);
809    
810                            if (UserGroupRoleLocalServiceUtil.hasUserGroupRole(
811                                            userId, groupId, role.getRoleId(), true)) {
812    
813                                    return true;
814                            }
815                    }
816                    else if (entityType.equals("organization")) {
817                            Organization organization =
818                                    OrganizationLocalServiceUtil.getOrganization(
819                                            companyId, entityValue);
820    
821                            if (OrganizationLocalServiceUtil.hasUserOrganization(
822                                            userId, organization.getOrganizationId(), false, false)) {
823    
824                                    return true;
825                            }
826                    }
827                    else if (entityType.equals("regular-role")) {
828                            if (RoleLocalServiceUtil.hasUserRole(
829                                            userId, companyId, entityValue, true)) {
830    
831                                    return true;
832                            }
833                    }
834                    else if (entityType.equals("user-group")) {
835                            UserGroup userGroup = UserGroupLocalServiceUtil.getUserGroup(
836                                    companyId, entityValue);
837    
838                            if (UserLocalServiceUtil.hasUserGroupUser(
839                                            userGroup.getUserGroupId(), userId)) {
840    
841                                    return true;
842                            }
843                    }
844    
845                    return false;
846            }
847    
848            private static Log _log = LogFactoryUtil.getLog(MBUtil.class);
849    
850    }