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.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.util.Validator;
021    import com.liferay.portal.kernel.workflow.WorkflowConstants;
022    import com.liferay.portal.service.UserLocalServiceUtil;
023    import com.liferay.portal.util.Portal;
024    import com.liferay.portlet.blogs.linkback.LinkbackConsumer;
025    import com.liferay.portlet.blogs.linkback.LinkbackConsumerUtil;
026    import com.liferay.portlet.blogs.model.BlogsEntry;
027    import com.liferay.portlet.blogs.service.BlogsEntryLocalServiceUtil;
028    import com.liferay.portlet.messageboards.model.MBDiscussion;
029    import com.liferay.portlet.messageboards.model.MBMessage;
030    import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
031    
032    import java.util.List;
033    
034    /**
035     * <p>
036     * This class looks at every blog comment to see if it is a trackback and
037     * verifies that the source URL is a valid URL. Do not run this unless you want
038     * to do this.
039     * </p>
040     *
041     * @author Alexander Chow
042     */
043    public class VerifyBlogsTrackbacks extends VerifyProcess {
044    
045            @Override
046            protected void doVerify() throws Exception {
047                    List<MBDiscussion> mbDiscussions =
048                            MBMessageLocalServiceUtil.getDiscussions(
049                                    BlogsEntry.class.getName());
050    
051                    for (MBDiscussion mbDiscussion : mbDiscussions) {
052                            try {
053                                    BlogsEntry entry = BlogsEntryLocalServiceUtil.getBlogsEntry(
054                                            mbDiscussion.getClassPK());
055    
056                                    List<MBMessage> mbMessages =
057                                            MBMessageLocalServiceUtil.getThreadMessages(
058                                                    mbDiscussion.getThreadId(),
059                                                    WorkflowConstants.STATUS_APPROVED);
060    
061                                    for (MBMessage mbMessage : mbMessages) {
062                                            _verifyPost(entry, mbMessage);
063                                    }
064                            }
065                            catch (Exception e) {
066                                    _log.error(e, e);
067                            }
068                    }
069            }
070    
071            private void _verifyPost(BlogsEntry entry, MBMessage mbMessage)
072                    throws PortalException {
073    
074                    String entryURL =
075                            Portal.FRIENDLY_URL_SEPARATOR + "blogs/" + entry.getUrlTitle();
076                    String body = mbMessage.getBody();
077                    String url = null;
078    
079                    int start = body.indexOf("[url=");
080    
081                    if (start > -1) {
082                            start += "[url=".length();
083    
084                            int end = body.indexOf("]", start);
085    
086                            if (end > -1) {
087                                    url = body.substring(start, end);
088                            }
089                    }
090    
091                    if (Validator.isNotNull(url)) {
092                            long defaultUserId = UserLocalServiceUtil.getDefaultUserId(
093                                    mbMessage.getCompanyId());
094    
095                            if (mbMessage.getUserId() == defaultUserId) {
096                                    _linkbackConsumer.verifyTrackback(
097                                            mbMessage.getMessageId(), url, entryURL);
098                            }
099                    }
100            }
101    
102            private static final Log _log = LogFactoryUtil.getLog(
103                    VerifyBlogsTrackbacks.class);
104    
105            private final LinkbackConsumer _linkbackConsumer =
106                    LinkbackConsumerUtil.getLinkbackConsumer();
107    
108    }