001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.verify;
016    
017    import com.liferay.blogs.kernel.model.BlogsEntry;
018    import com.liferay.blogs.kernel.service.BlogsEntryLocalServiceUtil;
019    import com.liferay.message.boards.kernel.model.MBDiscussion;
020    import com.liferay.message.boards.kernel.model.MBMessage;
021    import com.liferay.message.boards.kernel.service.MBMessageLocalServiceUtil;
022    import com.liferay.portal.kernel.exception.PortalException;
023    import com.liferay.portal.kernel.log.Log;
024    import com.liferay.portal.kernel.log.LogFactoryUtil;
025    import com.liferay.portal.kernel.service.UserLocalServiceUtil;
026    import com.liferay.portal.kernel.util.LoggingTimer;
027    import com.liferay.portal.kernel.util.Portal;
028    import com.liferay.portal.kernel.util.Validator;
029    import com.liferay.portal.kernel.workflow.WorkflowConstants;
030    import com.liferay.portlet.blogs.linkback.LinkbackConsumer;
031    import com.liferay.portlet.blogs.linkback.LinkbackConsumerUtil;
032    
033    import java.util.List;
034    
035    /**
036     * <p>
037     * This class looks at every blog comment to see if it is a trackback and
038     * verifies that the source URL is a valid URL. Do not run this unless you want
039     * to do this.
040     * </p>
041     *
042     * @author Alexander Chow
043     */
044    public class VerifyBlogsTrackbacks extends VerifyProcess {
045    
046            @Override
047            protected void doVerify() throws Exception {
048                    verifyMBDiscussions();
049            }
050    
051            protected void verifyMBDiscussions() {
052                    try (LoggingTimer loggingTimer = new LoggingTimer()) {
053                            List<MBDiscussion> mbDiscussions =
054                                    MBMessageLocalServiceUtil.getDiscussions(
055                                            BlogsEntry.class.getName());
056    
057                            for (MBDiscussion mbDiscussion : mbDiscussions) {
058                                    try {
059                                            BlogsEntry entry = BlogsEntryLocalServiceUtil.getBlogsEntry(
060                                                    mbDiscussion.getClassPK());
061    
062                                            List<MBMessage> mbMessages =
063                                                    MBMessageLocalServiceUtil.getThreadMessages(
064                                                            mbDiscussion.getThreadId(),
065                                                            WorkflowConstants.STATUS_APPROVED);
066    
067                                            for (MBMessage mbMessage : mbMessages) {
068                                                    _verifyPost(entry, mbMessage);
069                                            }
070                                    }
071                                    catch (Exception e) {
072                                            _log.error(e, e);
073                                    }
074                            }
075                    }
076            }
077    
078            private void _verifyPost(BlogsEntry entry, MBMessage mbMessage)
079                    throws PortalException {
080    
081                    String entryURL =
082                            Portal.FRIENDLY_URL_SEPARATOR + "blogs/" + entry.getUrlTitle();
083                    String body = mbMessage.getBody();
084                    String url = null;
085    
086                    int start = body.indexOf("[url=");
087    
088                    if (start > -1) {
089                            start += "[url=".length();
090    
091                            int end = body.indexOf("]", start);
092    
093                            if (end > -1) {
094                                    url = body.substring(start, end);
095                            }
096                    }
097    
098                    if (Validator.isNotNull(url)) {
099                            long defaultUserId = UserLocalServiceUtil.getDefaultUserId(
100                                    mbMessage.getCompanyId());
101    
102                            if (mbMessage.getUserId() == defaultUserId) {
103                                    _linkbackConsumer.verifyTrackback(
104                                            mbMessage.getMessageId(), url, entryURL);
105                            }
106                    }
107            }
108    
109            private static final Log _log = LogFactoryUtil.getLog(
110                    VerifyBlogsTrackbacks.class);
111    
112            private final LinkbackConsumer _linkbackConsumer =
113                    LinkbackConsumerUtil.getLinkbackConsumer();
114    
115    }