Example #1
0
function autolink_do($text, $sub, $limit, $tagfill, $auto_title, $force_prefix = null)
{
    $text_l = StrToLower($text);
    $cursor = 0;
    $loop = 1;
    $buffer = '';
    while ($cursor < strlen($text) && $loop) {
        $ok = 1;
        $matched = preg_match($sub, $text_l, $m, PREG_OFFSET_CAPTURE, $cursor);
        if (!$matched) {
            $loop = 0;
            $ok = 0;
        } else {
            $pos = $m[0][1];
            $sub_len = strlen($m[0][0]);
            $pre_hit = substr($text, $cursor, $pos - $cursor);
            $hit = substr($text, $pos, $sub_len);
            $pre = substr($text, 0, $pos);
            $post = substr($text, $pos + $sub_len);
            $fail_text = $pre_hit . $hit;
            $fail_len = strlen($fail_text);
            #
            # substring found - first check to see if we're inside a link tag already...
            #
            $bits = preg_split("!</a>!i", $pre);
            $last_bit = array_pop($bits);
            if (preg_match("!<a\\s!i", $last_bit)) {
                #echo "fail 1 at $cursor<br />\n";
                $ok = 0;
                $cursor += $fail_len;
                $buffer .= $fail_text;
            }
        }
        #
        # looks like a nice spot to autolink from - check the pre
        # to see if there was whitespace before this match
        #
        if ($ok) {
            if ($pre) {
                if (!preg_match('![\\s\\(\\[\\{>]$!s', $pre)) {
                    #echo "fail 2 at $cursor ($pre)<br />\n";
                    $ok = 0;
                    $cursor += $fail_len;
                    $buffer .= $fail_text;
                }
            }
        }
        #
        # we want to autolink here - find the extent of the url
        #
        if ($ok) {
            if (preg_match('/^([a-z0-9\\-\\.\\/\\-_%~!?=,:;&+*#@\\(\\)\\$]+)/i', $post, $matches)) {
                $url = $hit . $matches[1];
                $cursor += strlen($url) + strlen($pre_hit);
                $buffer .= $pre_hit;
                $url = html_entity_decode($url);
                #
                # remove trailing punctuation from url
                #
                while (preg_match('|[.,!;:?]$|', $url)) {
                    $url = substr($url, 0, strlen($url) - 1);
                    $cursor--;
                }
                foreach (array('()', '[]', '{}') as $pair) {
                    $o = substr($pair, 0, 1);
                    $c = substr($pair, 1, 1);
                    if (preg_match("!^(\\{$c}|^)[^\\{$o}]+\\{$c}\$!", $url)) {
                        $url = substr($url, 0, strlen($url) - 1);
                        $cursor--;
                    }
                }
                #
                # nice-i-fy url here
                #
                $link_url = $url;
                $display_url = $url;
                if ($force_prefix) {
                    $link_url = $force_prefix . $link_url;
                }
                if ($GLOBALS['autolink_options']['strip_protocols']) {
                    if (preg_match('!^(http|https)://!i', $display_url, $m)) {
                        $display_url = substr($display_url, strlen($m[1]) + 3);
                    }
                }
                $display_url = autolink_label($display_url, $limit);
                #
                # add the url
                #
                if ($display_url != $link_url && !preg_match('@title=@msi', $tagfill) && $auto_title) {
                    $display_quoted = preg_quote($display_url, '!');
                    if (!preg_match("!^(http|https)://{$display_quoted}\$!i", $link_url)) {
                        $tagfill .= ' title="' . $link_url . '"';
                    }
                }
                $link_url_enc = HtmlSpecialChars($link_url);
                $display_url_enc = HtmlSpecialChars($display_url);
                $buffer .= "<a href=\"{$link_url_enc}\"{$tagfill}>{$display_url_enc}</a>";
            } else {
                #echo "fail 3 at $cursor<br />\n";
                $ok = 0;
                $cursor += $fail_len;
                $buffer .= $fail_text;
            }
        }
    }
    #
    # add everything from the cursor to the end onto the buffer.
    #
    $buffer .= substr($text, $cursor);
    return $buffer;
}
Example #2
0
function autolink_do($text, $sub, $limit, $tagfill, $auto_title)
{
    $sub_len = strlen($sub);
    $text_l = StrToLower($text);
    $cursor = 0;
    $loop = 1;
    $buffer = '';
    while ($cursor < strlen($text) && $loop) {
        $ok = 1;
        $pos = strpos($text_l, $sub, $cursor);
        if ($pos === false) {
            $loop = 0;
            $ok = 0;
        } else {
            $pre_hit = substr($text, $cursor, $pos - $cursor);
            $hit = substr($text, $pos, $sub_len);
            $pre = substr($text, 0, $pos);
            $post = substr($text, $pos + $sub_len);
            $fail_text = $pre_hit . $hit;
            $fail_len = strlen($fail_text);
            #
            # substring found - first check to see if we're inside a link tag already...
            #
            $bits = preg_split("!</a>!i", $pre);
            $last_bit = array_pop($bits);
            if (preg_match("!<a\\s!i", $last_bit)) {
                #echo "fail 1 at $cursor<br />\n";
                $ok = 0;
                $cursor += $fail_len;
                $buffer .= $fail_text;
            }
        }
        #
        # looks like a nice spot to autolink from - check the pre
        # to see if there was whitespace before this match
        #
        if ($ok) {
            if ($pre) {
                if (!preg_match('![\\s\\(\\[\\{>]$!s', $pre)) {
                    #echo "fail 2 at $cursor ($pre)<br />\n";
                    $ok = 0;
                    $cursor += $fail_len;
                    $buffer .= $fail_text;
                }
            }
        }
        #
        # we want to autolink here - find the extent of the url
        #
        if ($ok) {
            if (preg_match('/^([a-z0-9\\-\\.\\/\\-_%~!?=,:;&+*#@\\(\\)\\$]+)/i', $post, $matches)) {
                $url = $hit . $matches[1];
                #
                # remove trailing punctuation from url
                #
                if (preg_match('|[.,!;:?]$|', $url)) {
                    $url = substr($url, 0, strlen($url) - 1);
                }
                foreach (array('()', '[]', '{}') as $pair) {
                    $o = substr($pair, 0, 1);
                    $c = substr($pair, 1, 1);
                    if (preg_match("!^(\\{$c}|^)[^\\{$o}]+\\{$c}\$!", $url)) {
                        $url = substr($url, 0, strlen($url) - 1);
                    }
                }
                #
                # commit
                #
                $cursor += strlen($url) + strlen($pre_hit);
                $buffer .= $pre_hit;
                #
                # nice-i-fy url here
                #
                $link_url = $url;
                $display_url = $url;
                if (preg_match('!^([a-z]+)://!i', $url, $m)) {
                    $display_url = substr($display_url, strlen($m[1]) + 3);
                } else {
                    $link_url = "http://{$link_url}";
                }
                $display_url = autolink_label($display_url, $limit);
                #
                # add the url
                #
                if ($display_url != $link_url && !preg_match('@title=@msi', $tagfill) && $auto_title) {
                    if ("http://{$display_url}" != $link_url && "https://{$display_url}" != $link_url && "ftp://{$display_url}" != $link_url) {
                        $tagfill .= ' title="' . $link_url . '"';
                    }
                }
                $buffer .= "<a href=\"{$link_url}\"{$tagfill}>{$display_url}</a>";
            } else {
                #echo "fail 3 at $cursor<br />\n";
                $ok = 0;
                $cursor += $fail_len;
                $buffer .= $fail_text;
            }
        }
    }
    #
    # add everything from the cursor to the end onto the buffer.
    #
    $buffer .= substr($text, $cursor);
    return $buffer;
}