Search in sources :

Example 16 with Pattern

use of org.apache.oro.text.regex.Pattern in project jspwiki by apache.

the class SpamFilter method refreshBlacklists.

/**
 *  If the spam filter notices changes in the black list page, it will refresh them automatically.
 *
 *  @param context
 */
private void refreshBlacklists(WikiContext context) {
    try {
        boolean rebuild = false;
        // 
        // Rebuild, if the spam words page, the attachment or the IP ban page has changed since.
        // 
        WikiPage sourceSpam = context.getEngine().getPage(m_forbiddenWordsPage);
        if (sourceSpam != null) {
            if (m_spamPatterns == null || m_spamPatterns.isEmpty() || sourceSpam.getLastModified().after(m_lastRebuild)) {
                rebuild = true;
            }
        }
        Attachment att = context.getEngine().getAttachmentManager().getAttachmentInfo(context, m_blacklist);
        if (att != null) {
            if (m_spamPatterns == null || m_spamPatterns.isEmpty() || att.getLastModified().after(m_lastRebuild)) {
                rebuild = true;
            }
        }
        WikiPage sourceIPs = context.getEngine().getPage(m_forbiddenIPsPage);
        if (sourceIPs != null) {
            if (m_IPPatterns == null || m_IPPatterns.isEmpty() || sourceIPs.getLastModified().after(m_lastRebuild)) {
                rebuild = true;
            }
        }
        // 
        if (rebuild) {
            m_lastRebuild = new Date();
            m_spamPatterns = parseWordList(sourceSpam, (sourceSpam != null) ? (String) sourceSpam.getAttribute(LISTVAR) : null);
            log.info("Spam filter reloaded - recognizing " + m_spamPatterns.size() + " patterns from page " + m_forbiddenWordsPage);
            m_IPPatterns = parseWordList(sourceIPs, (sourceIPs != null) ? (String) sourceIPs.getAttribute(LISTIPVAR) : null);
            log.info("IP filter reloaded - recognizing " + m_IPPatterns.size() + " patterns from page " + m_forbiddenIPsPage);
            if (att != null) {
                InputStream in = context.getEngine().getAttachmentManager().getAttachmentStream(att);
                StringWriter out = new StringWriter();
                FileUtil.copyContents(new InputStreamReader(in, "UTF-8"), out);
                Collection<Pattern> blackList = parseBlacklist(out.toString());
                log.info("...recognizing additional " + blackList.size() + " patterns from blacklist " + m_blacklist);
                m_spamPatterns.addAll(blackList);
            }
        }
    } catch (IOException ex) {
        log.info("Unable to read attachment data, continuing...", ex);
    } catch (ProviderException ex) {
        log.info("Failed to read spam filter attachment, continuing...", ex);
    }
}
Also used : Pattern(org.apache.oro.text.regex.Pattern) StringWriter(java.io.StringWriter) InputStreamReader(java.io.InputStreamReader) ProviderException(org.apache.wiki.api.exceptions.ProviderException) InputStream(java.io.InputStream) WikiPage(org.apache.wiki.WikiPage) Attachment(org.apache.wiki.attachment.Attachment) IOException(java.io.IOException) Date(java.util.Date)

Example 17 with Pattern

use of org.apache.oro.text.regex.Pattern in project jspwiki by apache.

the class SpamFilter method checkPatternList.

/**
 *  Does a check against a known pattern list.
 *
 *  @param context
 *  @param content
 *  @param change
 *  @throws RedirectException
 */
private void checkPatternList(WikiContext context, String content, Change change) throws RedirectException {
    // 
    if (m_spamPatterns == null || context.getPage().getName().equals(m_forbiddenWordsPage)) {
        return;
    }
    String ch = change.toString();
    if (context.getHttpRequest() != null) {
        ch += HttpUtil.getRemoteAddress(context.getHttpRequest());
    }
    for (Pattern p : m_spamPatterns) {
        if (m_matcher.contains(ch, p)) {
            // 
            // Spam filter has a match.
            // 
            String uid = log(context, REJECT, REASON_REGEXP + "(" + p.getPattern() + ")", ch);
            log.info("SPAM:Regexp (" + uid + "). Content matches the spam filter '" + p.getPattern() + "'");
            checkStrategy(context, REASON_REGEXP, "Herb says '" + p.getPattern() + "' is a bad spam word and I trust Herb! (Incident code " + uid + ")");
        }
    }
}
Also used : Pattern(org.apache.oro.text.regex.Pattern)

Example 18 with Pattern

use of org.apache.oro.text.regex.Pattern in project jspwiki by apache.

the class SpamFilter method checkIPList.

/**
 *  Does a check against a pattern list of IPs.
 *
 *  @param context
 *  @throws RedirectException
 */
private void checkIPList(WikiContext context) throws RedirectException {
    // 
    if (m_IPPatterns == null || context.getPage().getName().equals(m_forbiddenIPsPage)) {
        return;
    }
    String remoteIP = HttpUtil.getRemoteAddress(context.getHttpRequest());
    log.info("Attempting to match remoteIP " + remoteIP + " against " + m_IPPatterns.size() + " patterns");
    for (Pattern p : m_IPPatterns) {
        log.debug("Attempting to match remoteIP with " + p.getPattern());
        if (m_matcher.contains(remoteIP, p)) {
            // IP filter has a match.
            // 
            String uid = log(context, REJECT, REASON_IP_BANNED_PERMANENTLY + "(" + p.getPattern() + ")", remoteIP);
            log.info("SPAM:IPBanList (" + uid + "). remoteIP matches the IP filter '" + p.getPattern() + "'");
            checkStrategy(context, REASON_IP_BANNED_PERMANENTLY, "Herb says '" + p.getPattern() + "' is a banned IP and I trust Herb! (Incident code " + uid + ")");
        }
    }
}
Also used : Pattern(org.apache.oro.text.regex.Pattern)

Example 19 with Pattern

use of org.apache.oro.text.regex.Pattern in project ofbiz-framework by apache.

the class RegexpCondition method checkCondition.

@Override
public boolean checkCondition(MethodContext methodContext) throws MiniLangException {
    Object fieldVal = fieldFma.get(methodContext.getEnvMap());
    if (fieldVal == null) {
        fieldVal = "";
    } else if (!(fieldVal instanceof String)) {
        try {
            fieldVal = MiniLangUtil.convertType(fieldVal, String.class, methodContext.getLocale(), methodContext.getTimeZone(), null);
        } catch (Exception e) {
            throw new MiniLangRuntimeException(e, this);
        }
    }
    String regExp = exprFse.expandString(methodContext.getEnvMap());
    Pattern pattern = null;
    try {
        pattern = PatternFactory.createOrGetPerl5CompiledPattern(regExp, true);
    } catch (MalformedPatternException e) {
        Debug.logError(e, "Regular Expression [" + regExp + "] is mal-formed: " + e.toString(), module);
        throw new MiniLangRuntimeException(e, this);
    }
    PatternMatcher matcher = new Perl5Matcher();
    if (matcher.matches((String) fieldVal, pattern)) {
        // Debug.logInfo("The string [" + fieldVal + "] matched the pattern expr [" + pattern.getPattern() + "]", module);
        return true;
    } else {
        // Debug.logInfo("The string [" + fieldVal + "] did NOT match the pattern expr [" + pattern.getPattern() + "]", module);
        return false;
    }
}
Also used : Pattern(org.apache.oro.text.regex.Pattern) MiniLangRuntimeException(org.apache.ofbiz.minilang.MiniLangRuntimeException) Perl5Matcher(org.apache.oro.text.regex.Perl5Matcher) MalformedPatternException(org.apache.oro.text.regex.MalformedPatternException) PatternMatcher(org.apache.oro.text.regex.PatternMatcher) MalformedPatternException(org.apache.oro.text.regex.MalformedPatternException) MiniLangRuntimeException(org.apache.ofbiz.minilang.MiniLangRuntimeException) MiniLangException(org.apache.ofbiz.minilang.MiniLangException)

Example 20 with Pattern

use of org.apache.oro.text.regex.Pattern in project ofbiz-framework by apache.

the class UrlRegexpTransform method seoUrl.

/**
 * Transform a url according to seo pattern regular expressions.
 *
 * @param url
 *            , String to do the seo transform
 * @param isAnon
 *            , boolean to indicate whether it's an anonymous visit.
 *
 * @return String, the transformed url.
 */
public static String seoUrl(String url, boolean isAnon) {
    Perl5Matcher matcher = new Perl5Matcher();
    if (SeoConfigUtil.checkUseUrlRegexp() && matcher.matches(url, SeoConfigUtil.getGeneralRegexpPattern())) {
        Iterator<String> keys = SeoConfigUtil.getSeoPatterns().keySet().iterator();
        boolean foundMatch = false;
        while (keys.hasNext()) {
            String key = keys.next();
            Pattern pattern = SeoConfigUtil.getSeoPatterns().get(key);
            if (pattern.getPattern().contains(";jsessionid=")) {
                if (isAnon) {
                    if (SeoConfigUtil.isJSessionIdAnonEnabled()) {
                        continue;
                    }
                } else {
                    if (SeoConfigUtil.isJSessionIdUserEnabled()) {
                        continue;
                    }
                    boolean foundException = false;
                    for (int i = 0; i < SeoConfigUtil.getUserExceptionPatterns().size(); i++) {
                        if (matcher.matches(url, SeoConfigUtil.getUserExceptionPatterns().get(i))) {
                            foundException = true;
                            break;
                        }
                    }
                    if (foundException) {
                        continue;
                    }
                }
            }
            String replacement = SeoConfigUtil.getSeoReplacements().get(key);
            if (matcher.matches(url, pattern)) {
                for (int i = 1; i < matcher.getMatch().groups(); i++) {
                    replacement = replacement.replaceAll("\\$" + i, matcher.getMatch().group(i));
                }
                // break if found any matcher
                url = replacement;
                foundMatch = true;
                break;
            }
        }
        if (!foundMatch) {
            if (Debug.verboseOn()) {
                Debug.logVerbose("Can NOT find a seo transform pattern for this url: " + url, module);
            }
        }
    }
    return url;
}
Also used : Pattern(org.apache.oro.text.regex.Pattern) Perl5Matcher(org.apache.oro.text.regex.Perl5Matcher)

Aggregations

Pattern (org.apache.oro.text.regex.Pattern)70 Perl5Matcher (org.apache.oro.text.regex.Perl5Matcher)48 MalformedPatternException (org.apache.oro.text.regex.MalformedPatternException)27 Perl5Compiler (org.apache.oro.text.regex.Perl5Compiler)24 MatchResult (org.apache.oro.text.regex.MatchResult)17 PatternMatcherInput (org.apache.oro.text.regex.PatternMatcherInput)14 ArrayList (java.util.ArrayList)12 PatternMatcher (org.apache.oro.text.regex.PatternMatcher)9 PatternCompiler (org.apache.oro.text.regex.PatternCompiler)7 IOException (java.io.IOException)6 MalformedCachePatternException (org.apache.oro.text.MalformedCachePatternException)6 MalformedURLException (java.net.MalformedURLException)4 BufferedReader (java.io.BufferedReader)3 FileInputStream (java.io.FileInputStream)3 InputStreamReader (java.io.InputStreamReader)3 Perl5Substitution (org.apache.oro.text.regex.Perl5Substitution)3 EOFException (java.io.EOFException)2 File (java.io.File)2 FileNotFoundException (java.io.FileNotFoundException)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2