Example #1
0
/**
 * Add a javascript ban action icon after each url in the given content
 *
 * @param string Comment content
 * @return string the content with a ban icon after each url if the user has spamblacklist permission, the incoming content otherwise
 */
function add_ban_icons($content)
{
    global $current_User;
    if (!$current_User->check_perm('spamblacklist', 'edit')) {
        return $content;
    }
    $atags = get_atags($content);
    $imgtags = get_imgtags($content);
    $urls = get_urls($content);
    $result = '';
    $from = 0;
    // current processing position
    $length = 0;
    // current url or tag length
    $i = 0;
    // url counter
    $j = 0;
    // "a" tag counter
    $k = 0;
    // "img" tag counter
    while (isset($urls[$i])) {
        // there is unprocessed url
        $url = $urls[$i];
        if (validate_url($url, 'posting', false)) {
            // skip not valid urls
            $i++;
            continue;
        }
        while (isset($imgtags[$k]) && strpos($content, $imgtags[$k]) < $from) {
            // skipp already passed img tags
            $k++;
        }
        $pos = strpos($content, $url, $from);
        $length = evo_strlen($url);
        $i++;
        // check img tags
        if (isset($imgtags[$k]) && strpos($imgtags[$k], $url) !== false && $pos > strpos($content, $imgtags[$k], $from)) {
            // current url is inside the img tag, we need to skip this url.
            $result .= substr($content, $from, $pos + $length - $from);
            $from = $pos + $length;
            $k++;
            continue;
        }
        // check a tags
        if (isset($atags[$j])) {
            // there is unprocessed "a" tag
            $tag = $atags[$j];
            if (($urlpos = strpos($tag, $url)) !== false && $pos > strpos($content, $tag, $from)) {
                // the url is inside the current tag, we have to add ban icon after the tag
                $pos = strpos($content, $tag, $from);
                $length = strlen($tag);
                while (isset($urls[$i]) && ($urlpos = strpos($tag, $urls[$i], $urlpos + 1)) !== false) {
                    // skip all other urls from this tag
                    $i++;
                }
                $j++;
            }
        }
        // add processed part and ban icon to result and set current position
        $result .= substr($content, $from, $pos + $length - $from);
        $from = $pos + $length;
        $result .= add_jsban($url);
    }
    // add the end of the content to the result
    $result .= substr($content, $from, strlen($content) - $from);
    return $result;
}
Example #2
0
/**
 * Callback function to add a javascript ban action icon after each url in the given content
 *
 * @param string Comment content
 * @return string the content with a ban icon after each url if the user has spamblacklist permission, the incoming content otherwise
 */
function add_ban_icons_callback($content)
{
    $atags = get_atags($content);
    $imgtags = get_imgtags($content);
    $urls = get_urls($content);
    $result = '';
    $from = 0;
    // current processing position
    $length = 0;
    // current url or tag length
    $i = 0;
    // url counter
    $j = 0;
    // "a" tag counter
    $k = 0;
    // "img" tag counter
    // Remove the duplicated <a> tags from array
    $atags_fixed = array();
    foreach ($atags as $atag) {
        if (preg_match('#href="([^"]+)"#', $atag, $matches) && !isset($atags_fixed[$matches[1]])) {
            $atags_fixed[$matches[1]] = $atag;
        }
    }
    $atags = array();
    foreach ($atags_fixed as $atag) {
        $atags[] = $atag;
    }
    $used_urls = array();
    while (isset($urls[$i])) {
        // there is unprocessed url
        $url = $urls[$i];
        if (validate_url($url, 'posting', false)) {
            // skip not valid urls
            $i++;
            continue;
        }
        if (in_array($url, $used_urls)) {
            // skip already passed url
            $i++;
            continue;
        }
        $used_urls[] = $url;
        while (isset($imgtags[$k]) && strpos($content, $imgtags[$k]) < $from) {
            // skip already passed img tags
            $k++;
        }
        $pos = strpos($content, $url, $from);
        $length = utf8_strlen($url);
        $i++;
        // check img tags
        if (isset($imgtags[$k]) && strpos($imgtags[$k], $url) !== false && $pos > strpos($content, $imgtags[$k], $from)) {
            // current url is inside the img tag, we need to skip this url.
            $result .= substr($content, $from, $pos + $length - $from);
            $from = $pos + $length;
            $k++;
            continue;
        }
        // check a tags
        if (isset($atags[$j])) {
            // there is unprocessed "a" tag
            $tag = $atags[$j];
            if (($urlpos = strpos($tag, $url)) !== false && $pos > strpos($content, $tag, $from)) {
                // the url is inside the current tag, we have to add ban icon after the tag
                $pos = strpos($content, $tag, $from);
                $length = strlen($tag);
                while (isset($urls[$i]) && ($urlpos = strpos($tag, $urls[$i], $urlpos + 1)) !== false) {
                    // skip all other urls from this tag
                    $i++;
                }
                $j++;
            }
        }
        // add processed part and ban icon to result and set current position
        $result .= substr($content, $from, $pos + $length - $from);
        $from = $pos + $length;
        $result .= add_jsban($url);
    }
    // add the end of the content to the result
    $result .= substr($content, $from, strlen($content) - $from);
    return $result;
}