Below is a differential between the unpatched vulnerable code and the patched update, for reference.
--- a/sem-external-links/external-links-anchor-utils.php
+++ b/sem-external-links/external-links-anchor-utils.php
@@ -7,6 +7,10 @@
* Forked from Anchor-Utils
*/
+if ( ! defined( 'ABSPATH' ) ) {
+ exit;
+}
+
if ( @ini_get('pcre.backtrack_limit') <= 1000000 )
@ini_set('pcre.backtrack_limit', 1000000);
if ( @ini_get('pcre.recursion_limit') <= 250000 )
--- a/sem-external-links/sem-autolink-uri.php
+++ b/sem-external-links/sem-autolink-uri.php
@@ -15,6 +15,10 @@
This software is copyright Denis de Bernardy & Mike Koepke, and is distributed under the terms of the MIT and GPLv2 licenses.
**/
+if ( ! defined( 'ABSPATH' ) ) {
+ exit;
+}
+
/**
* autolink_uri
@@ -100,16 +104,63 @@
function filter($text) {
- if ( empty( $text ) )
+ if ( empty( $text ) || !is_string( $text ) )
return $text;
- global $escape_autolink_uri;
-
- $escape_autolink_uri = array();
-
- $text = sem_autolink_uri::escape($text);
-
- $text = preg_replace_callback("/
+ // Urls are looked for only in text between tags, never inside a tag or an
+ // attribute value, and not in text inside a, script, style, object, code, pre or
+ // textarea, before </head>, or inside [smart link] brackets.
+ $segments = sem_external_links_html::scan( $text );
+ $count = count( $segments );
+ $skip = array( 'a' => true, 'script' => true, 'style' => true, 'object' => true,
+ 'code' => true, 'pre' => true, 'textarea' => true );
+
+ $first = 0;
+ for ( $i = 0; $i < $count; $i++ ) {
+ if ( 'end' === $segments[$i]['type'] && 'head' === $segments[$i]['name'] ) {
+ $first = $i + 1;
+ break;
+ }
+ }
+
+ $out = '';
+ $depth = 0;
+ for ( $i = 0; $i < $count; $i++ ) {
+ $segment = $segments[$i];
+ $html = substr( $text, $segment['offset'], $segment['length'] );
+
+ if ( ( 'start' === $segment['type'] || 'end' === $segment['type'] ) && isset( $skip[$segment['name']] ) ) {
+ if ( 'start' === $segment['type'] )
+ $depth++;
+ elseif ( $depth > 0 )
+ $depth--;
+ } elseif ( 'text' === $segment['type'] && 0 === $depth && $i >= $first ) {
+ $html = $this->link_urls( $html );
+ }
+
+ $out .= $html;
+ }
+
+ return $out;
+ } # filter()
+
+
+ /**
+ * link_urls()
+ *
+ * Wraps the urls in one text segment with anchors, leaving [smart link] brackets alone.
+ *
+ * @param string $text
+ * @return string $text
+ **/
+
+ function link_urls($text) {
+ $parts = preg_split( '/([.+?])/', $text, -1, PREG_SPLIT_DELIM_CAPTURE );
+ if ( false === $parts )
+ return $text;
+
+ for ( $k = 0; $k < count( $parts ); $k += 2 ) {
+ $linked = preg_replace_callback("/
((?<!["']) # don't look inside quotes
(b
( # protocol or www.
@@ -131,12 +182,13 @@
)?
)
(?!["']))
- /ix", array($this, 'url_callback'), $text);
+ /ix", array($this, 'url_callback'), $parts[$k]);
+ if ( null !== $linked )
+ $parts[$k] = $linked;
+ }
- $text = sem_autolink_uri::unescape($text);
-
- return $text;
- } # filter()
+ return implode( '', $parts );
+ } # link_urls()
/**
@@ -155,88 +207,9 @@
$href = esc_url($href);
- return '<a href="' . $href . '">' . $url . '</a>';
+ return '<a href="' . $href . '">' . esc_html($url) . '</a>';
} # url_callback()
- /**
- * escape()
- *
- * @param string $text
- * @return string $text
- **/
-
- function escape($text) {
- global $escape_autolink_uri;
-
- if ( !isset($escape_autolink_uri) )
- $escape_autolink_uri = array();
-
- foreach ( array(
- 'head' => "/
- .*?
- <s*/s*heads*>
- /isx",
- 'blocks' => "/
- <s*(script|style|object|code|pre|textarea)(?:s.*?)?>
- .*?
- <s*/s*\1s*>
- /isx",
- 'smart_links' => "/
- [.+?]
- /x",
- 'meta' => "/
- <meta .*?>
- /isx",
- 'anchors' => "/
- <a .*?>.*?</a>
- /isx",
- 'tags' => "/
- <[^<>]+?(?:src|href|codebase|archive|usemap|data|data-.*|itemtype|xmlns|value|action|cite|background|placeholder|onclick)=[^<>]+?>
- /ix",
- ) as $regex ) {
- $t = preg_replace_callback($regex, array($this, 'escape_callback'), $text);
- if ( $t !== NULL )
- $text = $t;
- }
-
- return $text;
- } # escape()
-
-
- /**
- * escape_callback()
- *
- * @param array $match
- * @return string $tag_id
- **/
-
- function escape_callback($match) {
- global $escape_autolink_uri;
-
- $tag_id = "----escape_sem_autolink_uri:" . md5($match[0]) . "----";
- $escape_autolink_uri[$tag_id] = $match[0];
-
- return $tag_id;
- } # escape_callback()
-
-
- /**
- * unescape()
- *
- * @param string $text
- * @return string $text
- **/
-
- function unescape($text) {
- global $escape_autolink_uri;
-
- if ( !$escape_autolink_uri )
- return $text;
-
- $unescape = array_reverse($escape_autolink_uri);
-
- return str_replace(array_keys($unescape), array_values($unescape), $text);
- } # unescape()
} # sem_autolink_uri
$sem_autolink_uri = sem_autolink_uri::get_instance();
No newline at end of file
--- a/sem-external-links/sem-external-links-admin.php
+++ b/sem-external-links/sem-external-links-admin.php
@@ -5,6 +5,10 @@
* @package External Links
**/
+if ( ! defined( 'ABSPATH' ) ) {
+ exit;
+}
+
class external_links_admin {
/**
* Plugin instance.
@@ -74,7 +78,7 @@
**/
function save_options() {
- if ( !$_POST || !current_user_can('manage_options') )
+ if ( empty( $_POST ) || !current_user_can('manage_options') )
return;
check_admin_referer('sem_external_links');
@@ -83,7 +87,9 @@
'subdomains_local', 'exclude_code_blocks') as $var )
$$var = isset($_POST[$var]);
- $exclude_domains = stripslashes($_POST['exclude_domains']);
+ $exclude_domains = ( isset( $_POST['exclude_domains'] ) && is_string( $_POST['exclude_domains'] ) )
+ ? wp_kses( stripslashes_deep( $_POST['exclude_domains'] ), array() )
+ : '';
$domains = preg_split("/[s,]+/", $exclude_domains);
$exclude_domains = array();
@@ -109,7 +115,7 @@
echo "<div class="updated fade">n"
. "<p>"
. "<strong>"
- . __('Settings saved.', 'sem-external-links')
+ . esc_html__('Settings saved.', 'sem-external-links')
. "</strong>"
. "</p>n"
. "</div>n";
@@ -133,18 +139,18 @@
if ( $options['nofollow'] && ( function_exists('strip_nofollow') || class_exists('sem_dofollow') ) ) {
echo "<div class="error">n"
. "<p>"
- . __('Note: Your rel=nofollow preferences is being ignored because the dofollow plugin is enabled on your site.', 'sem-external-links')
+ . esc_html__('Note: Your rel=nofollow preferences is being ignored because the dofollow plugin is enabled on your site.', 'sem-external-links')
. "</p>n"
. "</div>n";
}
- echo '<h2>' . __('External Links Settings', 'sem-external-links') . '</h2>' . "n";
+ echo '<h2>' . esc_html__('External Links Settings', 'sem-external-links') . '</h2>' . "n";
echo '<table class="form-table">' . "n";
echo '<tr>' . "n"
. '<th scope="row">'
- . __('Apply Globally', 'sem-external-links')
+ . esc_html__('Apply Globally', 'sem-external-links')
. '</th>' . "n"
. '<td>'
. '<label>'
@@ -152,14 +158,14 @@
. checked($options['global'], true, false)
. ' />'
. ' '
- . __('Apply these settings to all outbound links on the site except those in scripts, styles and the html head section.', 'sem-external-links')
+ . esc_html__('Apply these settings to all outbound links on the site except those in scripts, styles and the html head section.', 'sem-external-links')
. '</label>'
. '</td>' . "n"
. '</tr>' . "n";
echo '<tr>' . "n"
. '<th scope="row">'
- . __('Apply to Text Widgets', 'sem-external-links')
+ . esc_html__('Apply to Text Widgets', 'sem-external-links')
. '</th>' . "n"
. '<td>'
. '<label>'
@@ -167,14 +173,14 @@
. checked($options['text_widgets'], true, false)
. ' />'
. ' '
- . __('Apply these settings to any text widgets in addition to post, page and comments content.', 'sem-external-links')
+ . esc_html__('Apply these settings to any text widgets in addition to post, page and comments content.', 'sem-external-links')
. '</label>'
. '</td>' . "n"
. '</tr>' . "n";
echo '<tr>' . "n"
. '<th scope="row">'
- . __('Treat Subdomains as Local', 'sem-external-links')
+ . esc_html__('Treat Subdomains as Local', 'sem-external-links')
. '</th>' . "n"
. '<td>'
. '<label>'
@@ -182,16 +188,16 @@
. checked($options['subdomains_local'], true, false)
. ' />'
. ' '
- . __('Treat any subdomains for this site as a local link.', 'sem-external-links')
+ . esc_html__('Treat any subdomains for this site as a local link.', 'sem-external-links')
. '</label>'
. '<br />' . "n"
- . '<i>' . __('Example: If your site is at domain.com and you also have store.domain.com, any link to store.domain.com will be treated as local.', 'sem-external-links') . '<i>'
+ . '<i>' . esc_html__('Example: If your site is at domain.com and you also have store.domain.com, any link to store.domain.com will be treated as local.', 'sem-external-links') . '<i>'
. '</td>' . "n"
. '</tr>' . "n";
echo '<tr>' . "n"
. '<th scope="row">'
- . __('Auto Convert Text Urls', 'sem-external-links')
+ . esc_html__('Auto Convert Text Urls', 'sem-external-links')
. '</th>' . "n"
. '<td>'
. '<label>'
@@ -199,18 +205,18 @@
. checked($options['autolinks'], true, false)
. ' />'
. ' '
- . __('Automatically converts text urls into clickable urls.', 'sem-external-links')
+ . esc_html__('Automatically converts text urls into clickable urls.', 'sem-external-links')
. '</label>'
. '<br />' . "n"
- . '<i>' . __('Note: If this option is enabled then if www.example.com is found in your text, it will be converted to an html <a> link."', 'sem-external-links')
+ . '<i>' . esc_html__('Note: If this option is enabled then if www.example.com is found in your text, it will be converted to an html <a> link."', 'sem-external-links')
. '<br />' . "n"
- . __('This conversion will occur first so external link treatment for nofollow, icon and target will be applied to this auto links.', 'sem-external-links') . '</i>'
+ . esc_html__('This conversion will occur first so external link treatment for nofollow, icon and target will be applied to this auto links.', 'sem-external-links') . '</i>'
. '</td>' . "n"
. '</tr>' . "n";
echo '<tr>' . "n"
. '<th scope="row">'
- . __('Add No Follow', 'sem-sem-external-links')
+ . esc_html__('Add No Follow', 'sem-external-links')
. '</th>' . "n"
. '<td>'
. '<label>'
@@ -218,16 +224,16 @@
. checked($options['nofollow'], true, false)
. ' />'
. ' '
- . __('Add a rel="nofollow" attribute to outbound links.', 'sem-external-links')
+ . esc_html__('Add a rel="nofollow" attribute to outbound links.', 'sem-external-links')
. '</label>'
. '<br />' . "n"
- . '<i>' . __('Note: You can override this behavior by adding the attribute rel="follow" to individual links.', 'sem-external-links')
+ . '<i>' . esc_html__('Note: You can override this behavior by adding the attribute rel="follow" to individual links.', 'sem-external-links')
. '</td>' . "n"
. '</tr>' . "n";
echo '<tr>' . "n"
. '<th scope="row">'
- . __('Add Icons', 'sem-external-links')
+ . esc_html__('Add Icons', 'sem-external-links')
. '</th>' . "n"
. '<td>'
. '<label>'
@@ -235,16 +241,16 @@
. checked($options['icon'], true, false)
. ' />'
. ' '
- . __('Mark outbound links with an icon.', 'sem-external-links')
+ . esc_html__('Mark outbound links with an icon.', 'sem-external-links')
. '</label>'
. '<br />' . "n"
- . '<i>' .__('Note: You can override this behavior by adding a class="no_icon" or "noicon" to individual links.', 'sem-external-links') . '</i>'
+ . '<i>' .esc_html__('Note: You can override this behavior by adding a class="no_icon" or "noicon" to individual links.', 'sem-external-links') . '</i>'
. '</td>' . "n"
. '</tr>' . "n";
echo '<tr>' . "n"
. '<th scope="row">'
- . __('Open in New Windows', 'sem-external-links')
+ . esc_html__('Open in New Windows', 'sem-external-links')
. '</th>' . "n"
. '<td>'
. '<label>'
@@ -252,16 +258,16 @@
. checked($options['target'], true, false)
. ' />'
. ' '
- . __('Open outbound links in new windows.', 'sem-external-links')
+ . esc_html__('Open outbound links in new windows.', 'sem-external-links')
. '</label>'
. '<br />' . "n"
- . '<i>' . __('Note: Some usability experts discourage this, claiming that <a href="http://www.useit.com/alertbox/9605.html">this can damage your visitors' trust</a> towards your site. Others highlight that computer-illiterate users do not always know how to use the back button, and encourage the practice for that reason.', 'sem-external-links') . '</i>'
+ . '<i>' . wp_kses( __('Note: Some usability experts discourage this, claiming that <a href="http://www.useit.com/alertbox/9605.html">this can damage your visitors' trust</a> towards your site. Others highlight that computer-illiterate users do not always know how to use the back button, and encourage the practice for that reason.', 'sem-external-links'), array( 'a' => array( 'href' => array() ) ) ) . '</i>'
. '</td>' . "n"
. '</tr>' . "n";
echo '<tr>' . "n"
. '<th scope="row">'
- . __('Exclude HTML Code Blocks', 'sem-external-links')
+ . esc_html__('Exclude HTML Code Blocks', 'sem-external-links')
. '</th>' . "n"
. '<td>'
. '<label>'
@@ -269,26 +275,26 @@
. checked($options['exclude_code_blocks'], true, false)
. ' />'
. ' '
- . __('Do not process links in html code blocks.', 'sem-external-links')
+ . esc_html__('Do not process links in html code blocks.', 'sem-external-links')
. '</label>'
. '<br />' . "n"
- . '<i>' . __('Links found inside either <code> or <pre> html tags will be ignored.', 'sem-external-links') . '</i>'
+ . '<i>' . esc_html__('Links found inside either <code> or <pre> html tags will be ignored.', 'sem-external-links') . '</i>'
. '</td>' . "n"
. '</tr>' . "n";
echo '<tr>' . "n"
. '<th scope="row">'
- . __('Domains to Exclude', 'sem-external-links')
+ . esc_html__('Domains to Exclude', 'sem-external-links')
. '</th>' . "n"
. '<td>'
. '<label>'
- . __('External site domains that should be excluded from processing:', 'sem-external-links')
+ . esc_html__('External site domains that should be excluded from processing:', 'sem-external-links')
. '<textarea name="exclude_domains" cols="58" rows="4" class="widefat">'
. esc_html($options['exclude_domains'])
. '</textarea>' . "n"
- . __('Domains and subdomains should be separated by a comma, space or carriage return. http://, https://, www. should not be included and will be stripped off.', 'sem-external-links')
+ . esc_html__('Domains and subdomains should be separated by a comma, space or carriage return. http://, https://, www. should not be included and will be stripped off.', 'sem-external-links')
. '</label> '
- . '<i>' .__('Example: domain.com, domain.net, sub.domain.com, somesite.com, external.org.', 'sem-external-links') . '</i>'
+ . '<i>' .esc_html__('Example: domain.com, domain.net, sub.domain.com, somesite.com, external.org.', 'sem-external-links') . '</i>'
. '<br />' . "n"
. '</td>'
. '</tr>' . "n";
@@ -297,7 +303,7 @@
echo '<p class="submit">'
. '<input type="submit"'
- . ' value="' . esc_attr(__('Save Changes', 'sem-external-links')) . '"'
+ . ' value="' . esc_attr__('Save Changes', 'sem-external-links') . '"'
. ' />'
. '</p>' . "n";
--- a/sem-external-links/sem-external-links-html.php
+++ b/sem-external-links/sem-external-links-html.php
@@ -0,0 +1,385 @@
+<?php
+/**
+ * sem_external_links_html
+ *
+ * A small HTML tokenizer shared by External Links and its autolink module.
+ *
+ * It splits markup into text, start tags, end tags and everything else (comments,
+ * doctypes, processing instructions and the contents of raw text elements such as
+ * script and style), following the HTML5 tokenizer's tag and attribute states, so that
+ * an attribute value is never mistaken for markup and markup is never mistaken for an
+ * attribute value. Callers edit only start tags it returns and only text segments it
+ * returns, which is what keeps their output well formed.
+ *
+ * Written for PHP 5.2 and WordPress 2.8: static methods only, and esc_attr() is the
+ * only WordPress function it calls.
+ *
+ * @package External Links
+ **/
+
+class sem_external_links_html {
+
+ /**
+ * scan()
+ *
+ * Splits $html into consecutive segments. Concatenating the bytes of every segment, in
+ * order, gives $html back. Each segment is an array:
+ *
+ * 'type' => 'text', 'start', 'end' or 'other'
+ * 'offset' => byte offset of the segment in $html
+ * 'length' => byte length of the segment
+ * 'name' => lowercase tag name for 'start' and 'end', '' otherwise
+ *
+ * and, for 'start' segments only:
+ *
+ * 'attrs' => list of attributes in source order, first occurrence of each name only
+ * (as HTML5 does). Each is array( 'name' => lowercase name,
+ * 'value' => value exactly as written (quotes removed, entities not
+ * decoded, '' when there is no value), 'quote' => '"', "'" or '',
+ * 'offset' => byte offset of the attribute in the tag,
+ * 'length' => byte length of the whole name=value token )
+ * 'close_at' => byte offset in the tag of its closing '>', or of the '/' of a
+ * closing '/>', where new attributes can be inserted
+ *
+ * Text segments are maximal: a '<' that does not start a tag stays in the text. Input
+ * the tokenizer cannot finish (an unterminated tag, quote or comment) becomes one
+ * 'other' segment running to the end, so callers never edit it.
+ *
+ * @param string $html
+ * @return array $segments
+ **/
+
+ static function scan( $html ) {
+ $segments = array();
+ $len = strlen( $html );
+ $pos = 0;
+ $text_start = 0;
+
+ while ( $pos < $len ) {
+ $lt = strpos( $html, '<', $pos );
+ if ( false === $lt )
+ break;
+
+ $token = sem_external_links_html::token_at( $html, $lt, $len );
+ if ( null === $token ) {
+ $pos = $lt + 1;
+ continue;
+ }
+
+ if ( $lt > $text_start )
+ $segments[] = sem_external_links_html::segment( 'text', $text_start, $lt - $text_start, '' );
+ $segments[] = $token;
+ $pos = $token['offset'] + $token['length'];
+ $text_start = $pos;
+
+ if ( 'start' !== $token['type'] )
+ continue;
+
+ if ( 'plaintext' === $token['name'] ) {
+ if ( $pos < $len )
+ $segments[] = sem_external_links_html::segment( 'other', $pos, $len - $pos, '' );
+ return $segments;
+ }
+
+ if ( !in_array( $token['name'], array( 'script', 'style', 'textarea', 'title', 'xmp', 'iframe', 'noembed', 'noframes', 'noscript' ) ) )
+ continue;
+
+ // raw text element: everything up to its own end tag is one 'other' segment
+ $end = sem_external_links_html::find_end_tag( $html, $token['name'], $pos, $len );
+ if ( false === $end ) {
+ if ( $pos < $len )
+ $segments[] = sem_external_links_html::segment( 'other', $pos, $len - $pos, '' );
+ return $segments;
+ }
+ if ( $end > $pos )
+ $segments[] = sem_external_links_html::segment( 'other', $pos, $end - $pos, '' );
+ $pos = $end;
+ $text_start = $end;
+ }
+
+ if ( $text_start < $len )
+ $segments[] = sem_external_links_html::segment( 'text', $text_start, $len - $text_start, '' );
+
+ return $segments;
+ } # scan()
+
+
+ /**
+ * rewrite_start_tag()
+ *
+ * Returns $tag with the attributes in $changes set. An attribute the tag already has
+ * is rewritten in place as name="value"; a missing one is added before the tag's
+ * closing '>' (or '/>'), in the order of $changes. Values are escaped with esc_attr(),
+ * which leaves existing character references alone.
+ *
+ * @param string $tag the start tag's bytes
+ * @param array $attrs the 'attrs' of its scan() segment
+ * @param int $close_at the 'close_at' of its scan() segment
+ * @param array $changes attribute name => new value
+ * @return string $tag
+ **/
+
+ static function rewrite_start_tag( $tag, $attrs, $close_at, $changes ) {
+ $present = array();
+ foreach ( $attrs as $attr )
+ $present[ $attr['name'] ] = true;
+
+ $insert = '';
+ foreach ( $changes as $name => $value ) {
+ if ( !isset( $present[ $name ] ) )
+ $insert .= ' ' . $name . '="' . esc_attr( $value ) . '"';
+ }
+
+ // close_at is after every attribute, so inserting there first leaves their offsets valid
+ if ( '' !== $insert )
+ $tag = substr_replace( $tag, $insert, $close_at, 0 );
+
+ // rewrite from the last attribute to the first so earlier offsets stay valid
+ for ( $i = count( $attrs ) - 1; $i >= 0; $i-- ) {
+ $name = $attrs[ $i ]['name'];
+ if ( isset( $changes[ $name ] ) )
+ $tag = substr_replace( $tag, $name . '="' . esc_attr( $changes[ $name ] ) . '"', $attrs[ $i ]['offset'], $attrs[ $i ]['length'] );
+ }
+
+ return $tag;
+ } # rewrite_start_tag()
+
+
+ /**
+ * segment()
+ *
+ * @param string $type
+ * @param int $offset
+ * @param int $length
+ * @param string $name
+ * @return array $segment
+ **/
+
+ static function segment( $type, $offset, $length, $name ) {
+ return array(
+ 'type' => $type,
+ 'offset' => $offset,
+ 'length' => $length,
+ 'name' => $name,
+ );
+ } # segment()
+
+
+ /**
+ * token_at()
+ *
+ * Reads the token that starts with the '<' at $lt, or returns null when that '<' is
+ * plain text (as HTML5 treats '< ', '<1', '<=' and a '<' at the very end).
+ *
+ * @param string $html
+ * @param int $lt
+ * @param int $len
+ * @return array|null $segment
+ **/
+
+ static function token_at( $html, $lt, $len ) {
+ if ( $lt + 1 >= $len )
+ return null;
+
+ $c = $html[ $lt + 1 ];
+
+ if ( sem_external_links_html::is_letter( $c ) )
+ return sem_external_links_html::read_tag( $html, $lt, $len, 'start' );
+
+ if ( '/' === $c ) {
+ if ( $lt + 2 >= $len )
+ return null;
+ if ( sem_external_links_html::is_letter( $html[ $lt + 2 ] ) )
+ return sem_external_links_html::read_tag( $html, $lt, $len, 'end' );
+ // '</>' is dropped by browsers, anything else is a bogus comment up to '>'
+ return sem_external_links_html::read_until( $html, $lt, $len, '>', $lt + 2 );
+ }
+
+ if ( '!' === $c ) {
+ if ( '<!--' === substr( $html, $lt, 4 ) ) {
+ // '<!-->' and '<!--->' are complete (empty) comments in HTML5
+ if ( '>' === substr( $html, $lt + 4, 1 ) )
+ return sem_external_links_html::segment( 'other', $lt, 5, '' );
+ if ( '->' === substr( $html, $lt + 4, 2 ) )
+ return sem_external_links_html::segment( 'other', $lt, 6, '' );
+ $end = strpos( $html, '-->', $lt + 4 );
+ $bang = strpos( $html, '--!>', $lt + 4 );
+ if ( false !== $bang && ( false === $end || $bang < $end ) )
+ return sem_external_links_html::segment( 'other', $lt, $bang + 4 - $lt, '' );
+ if ( false !== $end )
+ return sem_external_links_html::segment( 'other', $lt, $end + 3 - $lt, '' );
+ return sem_external_links_html::segment( 'other', $lt, $len - $lt, '' );
+ }
+ // doctype, CDATA outside foreign content and other '<!' markup: up to '>'
+ return sem_external_links_html::read_until( $html, $lt, $len, '>', $lt + 2 );
+ }
+
+ if ( '?' === $c )
+ return sem_external_links_html::read_until( $html, $lt, $len, '>', $lt + 2 );
+
+ return null;
+ } # token_at()
+
+
+ /**
+ * read_until()
+ *
+ * An 'other' segment from $lt through the first $char at or after $from, or to the end
+ * when there is none.
+ *
+ * @param string $html
+ * @param int $lt
+ * @param int $len
+ * @param string $char
+ * @param int $from
+ * @return array $segment
+ **/
+
+ static function read_until( $html, $lt, $len, $char, $from ) {
+ $end = strpos( $html, $char, $from );
+ if ( false === $end )
+ return sem_external_links_html::segment( 'other', $lt, $len - $lt, '' );
+ return sem_external_links_html::segment( 'other', $lt, $end + 1 - $lt, '' );
+ } # read_until()
+
+
+ /**
+ * read_tag()
+ *
+ * Reads a start or end tag beginning at $lt with the HTML5 tag name, attribute name and
+ * attribute value states. An end tag's attributes are read the same way (so a '>' inside
+ * their quotes does not end it) and then dropped, as HTML5 does.
+ *
+ * @param string $html
+ * @param int $lt
+ * @param int $len
+ * @param string $type 'start' or 'end'
+ * @return array $segment
+ **/
+
+ static function read_tag( $html, $lt, $len, $type ) {
+ $space = " tnrf";
+ $p = ( 'end' === $type ) ? $lt + 2 : $lt + 1;
+
+ $name_len = strcspn( $html, $space . '/>', $p );
+ $name = strtolower( substr( $html, $p, $name_len ) );
+ $p += $name_len;
+
+ $attrs = array();
+ $seen = array();
+
+ while ( true ) {
+ // before attribute name: skip spaces and stray '/'
+ $p += strspn( $html, $space . '/', $p );
+ if ( $p >= $len )
+ return sem_external_links_html::segment( 'other', $lt, $len - $lt, '' );
+
+ if ( '>' === $html[ $p ] )
+ break;
+
+ // attribute name; a leading '=' belongs to the name in HTML5
+ $attr_start = $p;
+ $attr_name_len = 1 + strcspn( $html, $space . '/>=', $p + 1 );
+ $attr_name = strtolower( substr( $html, $p, $attr_name_len ) );
+ $p += $attr_name_len;
+
+ // after attribute name
+ $p += strspn( $html, $space, $p );
+ if ( $p >= $len )
+ return sem_external_links_html::segment( 'other', $lt, $len - $lt, '' );
+
+ $value = '';
+ $quote = '';
+ if ( '=' === $html[ $p ] ) {
+ $p++;
+ $p += strspn( $html, $space, $p );
+ if ( $p >= $len )
+ return sem_external_links_html::segment( 'other', $lt, $len - $lt, '' );
+
+ $c = $html[ $p ];
+ if ( '"' === $c || "'" === $c ) {
+ $close = strpos( $html, $c, $p + 1 );
+ if ( false === $close )
+ return sem_external_links_html::segment( 'other', $lt, $len - $lt, '' );
+ $quote = $c;
+ $value = substr( $html, $p + 1, $close - $p - 1 );
+ $p = $close + 1;
+ } elseif ( '>' !== $c ) {
+ $value_len = strcspn( $html, $space . '>', $p );
+ $value = substr( $html, $p, $value_len );
+ $p += $value_len;
+ }
+ $attr_len = $p - $attr_start;
+ } else {
+ // no value: the token is the name alone, even if spaces follow it
+ $attr_len = $attr_name_len;
+ }
+
+ if ( !isset( $seen[ $attr_name ] ) ) {
+ $seen[ $attr_name ] = true;
+ $attrs[] = array(
+ 'name' => $attr_name,
+ 'value' => $value,
+ 'quote' => $quote,
+ 'offset' => $attr_start - $lt,
+ 'length' => $attr_len,
+ );
+ }
+ }
+
+ // $p is at the closing '>'
+ $segment = sem_external_links_html::segment( $type, $lt, $p + 1 - $lt, $name );
+ if ( 'start' === $type ) {
+ $close_at = $p - $lt;
+ // a '/' right before '>' that is not the end of an unquoted value marks '/>'
+ if ( '/' === $html[ $p - 1 ] ) {
+ $last = count( $attrs ) ? $attrs[ count( $attrs ) - 1 ] : null;
+ if ( null === $last || '' !== $last['quote'] || '' === $last['value'] || $last['offset'] + $last['length'] < $close_at )
+ $close_at--;
+ }
+ $segment['attrs'] = $attrs;
+ $segment['close_at'] = $close_at;
+ }
+
+ return $segment;
+ } # read_tag()
+
+
+ /**
+ * find_end_tag()
+ *
+ * Offset of the end tag that closes raw text element $name, searching from $from, or
+ * false. Like HTML5, it needs '</', the name in any case, then a space, '/' or '>'.
+ *
+ * @param string $html
+ * @param string $name
+ * @param int $from
+ * @param int $len
+ * @return int|false $offset
+ **/
+
+ static function find_end_tag( $html, $name, $from, $len ) {
+ $needle = '</' . $name;
+ $n = strlen( $needle );
+ while ( false !== ( $at = stripos( $html, $needle, $from ) ) ) {
+ if ( $at + $n >= $len )
+ return false;
+ if ( false !== strpos( " tnrf/>", $html[ $at + $n ] ) )
+ return $at;
+ $from = $at + 1;
+ }
+ return false;
+ } # find_end_tag()
+
+
+ /**
+ * is_letter()
+ *
+ * @param string $c one byte
+ * @return bool
+ **/
+
+ static function is_letter( $c ) {
+ return ( $c >= 'a' && $c <= 'z' ) || ( $c >= 'A' && $c <= 'Z' );
+ } # is_letter()
+} # sem_external_links_html
--- a/sem-external-links/sem-external-links-info.php
+++ b/sem-external-links/sem-external-links-info.php
@@ -1,4 +1,8 @@
<?php
+
+if ( ! defined( 'ABSPATH' ) ) {
+ exit;
+}
__('External Links', 'sem-external-links');
__('Marks outbound links as such, with various effects that are configurable under <a href="options-general.php?page=external-links">Settings / External Links</a>.', 'sem-external-links');
?>
No newline at end of file
--- a/sem-external-links/sem-external-links.php
+++ b/sem-external-links/sem-external-links.php
@@ -1,14 +1,13 @@
<?php
/*
Plugin Name: External Links
-Plugin URI: http://www.semiologic.com/software/external-links/
Description: Marks outbound links as such, with various effects that are configurable under <a href="options-general.php?page=external-links">Settings / External Links</a>.
-Version: 6.8.1
+Version: 6.8.2
Author: Denis de Bernardy & Mike Koepke
-Author URI: https://www.semiologic.com
Text Domain: sem-external-links
Domain Path: /lang
-License: Dual licensed under the MIT and GPLv2 licenses
+License: GPLv2 or later
+License URI: http://www.gnu.org/licenses/gpl-2.0.html
*/
/*
@@ -19,7 +18,13 @@
**/
-define('sem_external_links_version', '6.8.1');
+if ( ! defined( 'ABSPATH' ) ) {
+ exit;
+}
+
+define('sem_external_links_version', '6.8.2');
+
+require_once dirname( __FILE__ ) . '/sem-external-links-html.php';
/**
* external_links
@@ -84,7 +89,7 @@
{
load_plugin_textdomain(
$domain,
- FALSE,
+ false,
dirname(plugin_basename(__FILE__)) . '/lang'
);
}
@@ -181,47 +186,79 @@
**/
function process_content($text, $context = "global") {
+ if ( !is_string($text) )
+ return $text;
+
// short circuit if there's no anchors at all in the text
- if ( false === stripos($text, '<a ') )
+ if ( false === stripos($text, '<a') )
return($text);
- $escape_needed = array( 'global', 'content', 'widgets' );
- if ( in_array($context, $escape_needed ) ) {
- global $escape_anchor_filter;
- $escape_anchor_filter = array();
-
- $text = $this->escape($text, $context);
- }
-
- // find all occurrences of anchors and fill matches with links
- preg_match_all("/
- <s*as+
- ([^<>]+)
- >
- (.*?)
- <s*/s*as*>
- /isx", $text, $matches, PREG_SET_ORDER);
-
- $raw_links = array();
- $processed_links = array();
-
- foreach ($matches as $match)
- {
- $updated_link = $this->process_link($match);
- if ( $updated_link ) {
- $raw_links[] = $match[0];
- $processed_links[] = $updated_link;
+ // Only start tags found by the scanner are edited, each by attribute offsets, so an
+ // attribute value is never mistaken for markup. Script, style and textarea contents
+ // are never tags to the scanner; object (and pre and code when exclude_code_blocks
+ // is on) are skipped here; in the whole-page buffer, so is everything up to </head>.
+ $segments = sem_external_links_html::scan($text);
+ $count = count($segments);
+
+ $skip = array( 'object' => true );
+ if ( $this->opts['exclude_code_blocks'] ) {
+ $skip['pre'] = true;
+ $skip['code'] = true;
+ }
+
+ $first = 0;
+ if ( $context == 'global' ) {
+ for ( $i = 0; $i < $count; $i++ ) {
+ if ( 'end' === $segments[$i]['type'] && 'head' === $segments[$i]['name'] ) {
+ $first = $i + 1;
+ break;
+ }
}
}
- if ( !empty($raw_links) && !empty($processed_links) )
- $text = str_replace($raw_links, $processed_links, $text);
+ $out = '';
+ $done = 0;
+ $depth = 0;
+ for ( $i = $first; $i < $count; $i++ ) {
+ $segment = $segments[$i];
+ if ( 'start' !== $segment['type'] && 'end' !== $segment['type'] )
+ continue;
+
+ if ( isset($skip[$segment['name']]) ) {
+ if ( 'start' === $segment['type'] )
+ $depth++;
+ elseif ( $depth > 0 )
+ $depth--;
+ continue;
+ }
+
+ if ( $depth > 0 || 'start' !== $segment['type'] || 'a' !== $segment['name'] )
+ continue;
+
+ // the anchor's body runs to the next </a>; an anchor without one is left alone
+ $body_start = $segment['offset'] + $segment['length'];
+ $body_end = false;
+ for ( $j = $i + 1; $j < $count; $j++ ) {
+ if ( 'end' === $segments[$j]['type'] && 'a' === $segments[$j]['name'] ) {
+ $body_end = $segments[$j]['offset'];
+ break;
+ }
+ }
+ if ( false === $body_end )
+ continue;
- if ( in_array($context, $escape_needed ) ) {
- $text = $this->unescape($text);
+ $tag = substr($text, $segment['offset'], $segment['length']);
+ $updated_link = $this->process_link($tag, $segment, substr($text, $body_start, $body_end - $body_start));
+ if ( $updated_link ) {
+ $out .= substr($text, $done, $segment['offset'] - $done) . $updated_link;
+ $done = $body_start;
+ }
}
- return $text;
+ if ( 0 === $done )
+ return $text;
+
+ return $out . substr($text, $done);
} # process_content()
@@ -270,93 +307,17 @@
}
/**
- * escape()
- *
- * @param string $text
- * @param string $context
- * @return string $text
- **/
-
- function escape($text, $context) {
- global $escape_anchor_filter;
-
- if ( !isset($escape_anchor_filter) )
- $escape_anchor_filter = array();
-
- $exclusions = array();
-
- if ( $context == 'global' ) {
- $exclusions['head'] = "/
- .*?
- <s*/s*heads*>
- /isx";
- }
-
- $ignore_blocks = 'script|style|object|textarea';
- if ( $this->opts['exclude_code_blocks'] ) {
- $ignore_blocks .= '|pre|code';
- }
-
- $exclusions['blocks'] = "/
- <s*(" . $ignore_blocks . ")(?:s.*?)?>
- .*?
- <s*/s*\1s*>
- /isx";
-
- foreach ( $exclusions as $regex ) {
- $text = preg_replace_callback($regex, array($this, 'escape_callback'), $text);
- }
-
- return $text;
- } # escape()
-
-
- /**
- * escape_callback()
- *
- * @param array $match
- * @return string $text
- **/
-
- function escape_callback($match) {
- global $escape_anchor_filter;
-
- $tag_id = "----escape_sem_external_links:" . md5($match[0]) . "----";
- $escape_anchor_filter[$tag_id] = $match[0];
-
- return $tag_id;
- } # escape_callback()
-
-
- /**
- * unescape()
+ * process_link()
*
- * @param string $text
- * @return string $text
+ * @param string $tag the anchor's start tag
+ * @param array $segment its sem_external_links_html::scan() segment
+ * @param string $body the html between the start tag and its </a>
+ * @return string|false $tag the rewritten start tag, or false to leave it alone
**/
- function unescape($text) {
- global $escape_anchor_filter;
-
- if ( !$escape_anchor_filter )
- return $text;
-
- $unescape = array_reverse($escape_anchor_filter);
-
- return str_replace(array_keys($unescape), array_values($unescape), $text);
- } # unescape()
-
-
- /**
- * filter_callback()
- *
- * @param array $match
- * @return string $str
- **/
-
- function process_link($match) {
+ function process_link($tag, $segment, $body) {
# parse anchor
- $anchor = $this->parse_anchor($match);
+ $anchor = $this->parse_anchor($segment, $body);
if ( !$anchor )
return false;
@@ -365,7 +326,7 @@
$anchor = $this->filter_anchor( $anchor );
if ( $anchor )
- $anchor = $this->build_anchor($match[0], $anchor);
+ $anchor = $this->build_anchor($tag, $segment, $anchor);
return $anchor;
} # process_link()
@@ -374,20 +335,26 @@
/**
* parse_anchor()
*
- * @param array $match
+ * @param array $segment
+ * @param string $body
* @return array $anchor
**/
- function parse_anchor($match) {
+ function parse_anchor($segment, $body) {
$anchor = array();
- $anchor['attr'] = $this->parseAttributes( $match[1] );
+ $anchor['attr'] = array();
+ foreach ( $segment['attrs'] as $attr )
+ $anchor['attr'][$attr['name']] = $attr['value'];
+
+ # attribute values as written, to tell later which ones changed
+ $anchor['raw'] = $anchor['attr'];
- if ( !is_array($anchor['attr']) || empty($anchor['attr']['href']) # parser error or no link
- || trim($anchor['attr']['href']) != esc_attr($anchor['attr']['href'], null, 'db') ) # likely a script
+ if ( empty($anchor['attr']['href']) # no link
+ || trim($anchor['attr']['href']) != esc_attr($anchor['attr']['href']) ) # likely a script
return false;
foreach ( array('class', 'rel') as $attr ) {
- if ( !isset($anchor['attr'][$attr]) ) {
+ if ( !isset($anchor['attr'][$attr]) || '' === $anchor['attr'][$attr] ) {
$anchor['attr'][$attr] = array();
} else {
$anchor['attr'][$attr] = explode(' ', $anchor['attr'][$attr]);
@@ -395,7 +362,7 @@
}
}
- $anchor['body'] = $match[2];
+ $anchor['body'] = $body;
$anchor['attr']['href'] = @html_entity_decode($anchor['attr']['href'], ENT_COMPAT, get_option('blog_charset'));
@@ -406,14 +373,19 @@
/**
* build_anchor()
*
- * @param $link
+ * Sets class, rel and target on the start tag, touching only the attributes whose
+ * value changed, through sem_external_links_html::rewrite_start_tag().
+ *
+ * @param string $tag
+ * @param array $segment
* @param array $anchor
- * @return string $anchor
+ * @return string|false $tag
*/
- function build_anchor($link, $anchor) {
+ function build_anchor($tag, $segment, $anchor) {
$attrs = array( 'class', 'rel', 'target');
+ $changes = array();
foreach ( $attrs as $attr ) {
if ( isset($anchor['attr'][$attr]) ) {
@@ -427,59 +399,17 @@
$new_attr_value = $values;
}
- if ( $new_attr_value )
- $link = $this->update_attribute($link, $attr, $new_attr_value);
+ if ( $new_attr_value
+ && ( !isset($anchor['raw'][$attr]) || $anchor['raw'][$attr] !== $new_attr_value ) )
+ $changes[$attr] = $new_attr_value;
}
}
- return $link;
- } # build_anchor()
-
-
- /**
- * Updates attribute of an HTML tag.
- *
- * @param $html
- * @param $attr_name
- * @param $new_attr_value
- * @return string
- */
- function update_attribute($html, $attr_name, $new_attr_value) {
-
- $attr_value = false;
- $quote = false; // quotes to wrap attribute values
-
- preg_match('/(<a.*>)/isU', $html, $match);
-
- $link_str = $match[1];
- if ($link_str == "")
- return $html;
-
- $re = '/' . preg_quote($attr_name) . '=(['"])?((?(1).+?|[^s>]+))(?(1)1)/is';
- if (preg_match($re, $link_str, $matches)
- ) {
- // two possible ways to get existing attributes
- $attr_value = $matches[2];
-
- $quote = false !== stripos($html, $attr_name . "='") ? "'" : '"';
- }
-
- if ($attr_value)
- {
- //replace current attribute
- $html = str_ireplace("$attr_name=" . $quote . "$attr_value" . $quote,
- $attr_name . '="' . esc_attr($new_attr_value) . '"', $html);
- }
- else {
- // attribute does not currently exist, add it
- $pos = strpos( $html, '>' );
- if ($pos !== false) {
- $html = substr_replace( $html, " $attr_name="" . esc_attr($new_attr_value) . '">', $pos, strlen('>') );
- }
- }
+ if ( !$changes )
+ return false;
- return $html;
- } # update_attribute()
+ return sem_external_links_html::rewrite_start_tag($tag, $segment['attrs'], $segment['close_at'], $changes);
+ } # build_anchor()
/**
* filter_anchor()
@@ -558,29 +488,6 @@
return null;
} # filter_anchor()
-
- function parseAttributes($text) {
- $attributes = array();
- $pattern = '#(?(DEFINE)
- (?<name>[a-zA-Z][a-zA-Z0-9-:]*)
- (?<value_double>"[^"]+")
- (?<value_single>'[^']+')
- (?<value_none>[^s>]+)
- (?<value>((?&value_double)|(?&value_single)|(?&value_none)))
- )
- (?<n>(?&name))(=(?<v>(?&value)))?#xs';
-
- if (preg_match_all($pattern, $text, $matches, PREG_SET_ORDER)) {
- foreach ($matches as $match) {
- $attributes[$match['n']] = isset($match['v'])
- ? trim($match['v'], ''"')
- : null;
- }
- }
-
- return $attributes;
- }
-
/**
* is_external()
*
@@ -603,7 +510,8 @@
static $site_domain;
if ( !isset($site_domain) ) {
- $site_domain = home_url();
+ // home_url() arrived in WordPress 3.0; 2.8 and 2.9 only have the home option.
+ $site_domain = function_exists('home_url') ? home_url() : get_option('home');
$site_domain = parse_url($site_domain);
$site_domain = $site_domain['host'];
if ($site_domain == false)
--- a/sem-external-links/sem-follow_comment.php
+++ b/sem-external-links/sem-follow_comment.php
@@ -22,6 +22,10 @@
* Thomas Parisot <http://oncle-tom.net>
**/
+if ( ! defined( 'ABSPATH' ) ) {
+ exit;
+}
+
class sem_follow_comment {
/**