web/wp-includes/formatting.php
branchwordpress
changeset 109 03b0d1493584
child 132 4d4862461b8d
equal deleted inserted replaced
-1:000000000000 109:03b0d1493584
       
     1 <?php
       
     2 /**
       
     3  * Main Wordpress Formatting API.
       
     4  *
       
     5  * Handles many functions for formatting output.
       
     6  *
       
     7  * @package WordPress
       
     8  **/
       
     9 
       
    10 /**
       
    11  * Replaces common plain text characters into formatted entities
       
    12  *
       
    13  * As an example,
       
    14  * <code>
       
    15  * 'cause today's effort makes it worth tomorrow's "holiday"...
       
    16  * </code>
       
    17  * Becomes:
       
    18  * <code>
       
    19  * &#8217;cause today&#8217;s effort makes it worth tomorrow&#8217;s &#8220;holiday&#8221;&#8230;
       
    20  * </code>
       
    21  * Code within certain html blocks are skipped.
       
    22  *
       
    23  * @since 0.71
       
    24  * @uses $wp_cockneyreplace Array of formatted entities for certain common phrases
       
    25  *
       
    26  * @param string $text The text to be formatted
       
    27  * @return string The string replaced with html entities
       
    28  */
       
    29 function wptexturize($text) {
       
    30 	global $wp_cockneyreplace;
       
    31 	$output = '';
       
    32 	$curl = '';
       
    33 	$textarr = preg_split('/(<.*>|\[.*\])/Us', $text, -1, PREG_SPLIT_DELIM_CAPTURE);
       
    34 	$stop = count($textarr);
       
    35 	
       
    36 	/* translators: opening curly quote */
       
    37 	$opening_quote = _x('&#8220;', 'opening curly quote');
       
    38 	/* translators: closing curly quote */
       
    39 	$closing_quote = _x('&#8221;', 'closing curly quote');
       
    40 	
       
    41 	$no_texturize_tags = apply_filters('no_texturize_tags', array('pre', 'code', 'kbd', 'style', 'script', 'tt'));
       
    42 	$no_texturize_shortcodes = apply_filters('no_texturize_shortcodes', array('code'));
       
    43 	$no_texturize_tags_stack = array();
       
    44 	$no_texturize_shortcodes_stack = array();
       
    45 
       
    46 	// if a plugin has provided an autocorrect array, use it
       
    47 	if ( isset($wp_cockneyreplace) ) {
       
    48 		$cockney = array_keys($wp_cockneyreplace);
       
    49 		$cockneyreplace = array_values($wp_cockneyreplace);
       
    50 	} else {
       
    51 		$cockney = array("'tain't","'twere","'twas","'tis","'twill","'til","'bout","'nuff","'round","'cause");
       
    52 		$cockneyreplace = array("&#8217;tain&#8217;t","&#8217;twere","&#8217;twas","&#8217;tis","&#8217;twill","&#8217;til","&#8217;bout","&#8217;nuff","&#8217;round","&#8217;cause");
       
    53 	}
       
    54 
       
    55 	$static_characters = array_merge(array('---', ' -- ', '--', ' - ', 'xn&#8211;', '...', '``', '\'s', '\'\'', ' (tm)'), $cockney);
       
    56 	$static_replacements = array_merge(array('&#8212;', ' &#8212; ', '&#8211;', ' &#8211; ', 'xn--', '&#8230;', $opening_quote, '&#8217;s', $closing_quote, ' &#8482;'), $cockneyreplace);
       
    57 
       
    58 	$dynamic_characters = array('/\'(\d\d(?:&#8217;|\')?s)/', '/(\s|\A|")\'/', '/(\d+)"/', '/(\d+)\'/', '/(\S)\'([^\'\s])/', '/(\s|\A)"(?!\s)/', '/"(\s|\S|\Z)/', '/\'([\s.]|\Z)/', '/(\d+)x(\d+)/');
       
    59 	$dynamic_replacements = array('&#8217;$1','$1&#8216;', '$1&#8243;', '$1&#8242;', '$1&#8217;$2', '$1' . $opening_quote . '$2', $closing_quote . '$1', '&#8217;$1', '$1&#215;$2');
       
    60 
       
    61 	for ( $i = 0; $i < $stop; $i++ ) {
       
    62 		$curl = $textarr[$i];
       
    63 
       
    64 		if ( !empty($curl) && '<' != $curl{0} && '[' != $curl{0}
       
    65 				&& empty($no_texturize_shortcodes_stack) && empty($no_texturize_tags_stack)) { // If it's not a tag
       
    66 			// static strings
       
    67 			$curl = str_replace($static_characters, $static_replacements, $curl);
       
    68 			// regular expressions
       
    69 			$curl = preg_replace($dynamic_characters, $dynamic_replacements, $curl);
       
    70 		} else {
       
    71 			wptexturize_pushpop_element($curl, $no_texturize_tags_stack, $no_texturize_tags, '<', '>');
       
    72 			wptexturize_pushpop_element($curl, $no_texturize_shortcodes_stack, $no_texturize_shortcodes, '[', ']');
       
    73 		}
       
    74 
       
    75 		$curl = preg_replace('/&([^#])(?![a-zA-Z1-4]{1,8};)/', '&#038;$1', $curl);
       
    76 		$output .= $curl;
       
    77 	}
       
    78 
       
    79 	return $output;
       
    80 }
       
    81 
       
    82 function wptexturize_pushpop_element($text, &$stack, $disabled_elements, $opening = '<', $closing = '>') {
       
    83 	$o = preg_quote($opening, '/');
       
    84 	$c = preg_quote($closing, '/');
       
    85 	foreach($disabled_elements as $element) {
       
    86 		if (preg_match('/^'.$o.$element.'\b/', $text)) array_push($stack, $element);
       
    87 		if (preg_match('/^'.$o.'\/'.$element.$c.'/', $text)) {
       
    88 			$last = array_pop($stack);
       
    89 			// disable texturize until we find a closing tag of our type (e.g. <pre>)
       
    90 			// even if there was invalid nesting before that
       
    91 			// Example: in the case <pre>sadsadasd</code>"baba"</pre> "baba" won't be texturized
       
    92 			if ($last != $element) array_push($stack, $last);
       
    93 		}
       
    94 	}
       
    95 }
       
    96 
       
    97 /**
       
    98  * Accepts matches array from preg_replace_callback in wpautop() or a string.
       
    99  *
       
   100  * Ensures that the contents of a <<pre>>...<</pre>> HTML block are not
       
   101  * converted into paragraphs or line-breaks.
       
   102  *
       
   103  * @since 1.2.0
       
   104  *
       
   105  * @param array|string $matches The array or string
       
   106  * @return string The pre block without paragraph/line-break conversion.
       
   107  */
       
   108 function clean_pre($matches) {
       
   109 	if ( is_array($matches) )
       
   110 		$text = $matches[1] . $matches[2] . "</pre>";
       
   111 	else
       
   112 		$text = $matches;
       
   113 
       
   114 	$text = str_replace('<br />', '', $text);
       
   115 	$text = str_replace('<p>', "\n", $text);
       
   116 	$text = str_replace('</p>', '', $text);
       
   117 
       
   118 	return $text;
       
   119 }
       
   120 
       
   121 /**
       
   122  * Replaces double line-breaks with paragraph elements.
       
   123  *
       
   124  * A group of regex replaces used to identify text formatted with newlines and
       
   125  * replace double line-breaks with HTML paragraph tags. The remaining
       
   126  * line-breaks after conversion become <<br />> tags, unless $br is set to '0'
       
   127  * or 'false'.
       
   128  *
       
   129  * @since 0.71
       
   130  *
       
   131  * @param string $pee The text which has to be formatted.
       
   132  * @param int|bool $br Optional. If set, this will convert all remaining line-breaks after paragraphing. Default true.
       
   133  * @return string Text which has been converted into correct paragraph tags.
       
   134  */
       
   135 function wpautop($pee, $br = 1) {
       
   136 	if ( trim($pee) === '' )
       
   137 		return '';
       
   138 	$pee = $pee . "\n"; // just to make things a little easier, pad the end
       
   139 	$pee = preg_replace('|<br />\s*<br />|', "\n\n", $pee);
       
   140 	// Space things out a little
       
   141 	$allblocks = '(?:table|thead|tfoot|caption|col|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|select|form|map|area|blockquote|address|math|style|input|p|h[1-6]|hr)';
       
   142 	$pee = preg_replace('!(<' . $allblocks . '[^>]*>)!', "\n$1", $pee);
       
   143 	$pee = preg_replace('!(</' . $allblocks . '>)!', "$1\n\n", $pee);
       
   144 	$pee = str_replace(array("\r\n", "\r"), "\n", $pee); // cross-platform newlines
       
   145 	if ( strpos($pee, '<object') !== false ) {
       
   146 		$pee = preg_replace('|\s*<param([^>]*)>\s*|', "<param$1>", $pee); // no pee inside object/embed
       
   147 		$pee = preg_replace('|\s*</embed>\s*|', '</embed>', $pee);
       
   148 	}
       
   149 	$pee = preg_replace("/\n\n+/", "\n\n", $pee); // take care of duplicates
       
   150 	// make paragraphs, including one at the end
       
   151 	$pees = preg_split('/\n\s*\n/', $pee, -1, PREG_SPLIT_NO_EMPTY);
       
   152 	$pee = '';
       
   153 	foreach ( $pees as $tinkle )
       
   154 		$pee .= '<p>' . trim($tinkle, "\n") . "</p>\n";
       
   155 	$pee = preg_replace('|<p>\s*</p>|', '', $pee); // under certain strange conditions it could create a P of entirely whitespace
       
   156 	$pee = preg_replace('!<p>([^<]+)</(div|address|form)>!', "<p>$1</p></$2>", $pee);
       
   157 	$pee = preg_replace('!<p>\s*(</?' . $allblocks . '[^>]*>)\s*</p>!', "$1", $pee); // don't pee all over a tag
       
   158 	$pee = preg_replace("|<p>(<li.+?)</p>|", "$1", $pee); // problem with nested lists
       
   159 	$pee = preg_replace('|<p><blockquote([^>]*)>|i', "<blockquote$1><p>", $pee);
       
   160 	$pee = str_replace('</blockquote></p>', '</p></blockquote>', $pee);
       
   161 	$pee = preg_replace('!<p>\s*(</?' . $allblocks . '[^>]*>)!', "$1", $pee);
       
   162 	$pee = preg_replace('!(</?' . $allblocks . '[^>]*>)\s*</p>!', "$1", $pee);
       
   163 	if ($br) {
       
   164 		$pee = preg_replace_callback('/<(script|style).*?<\/\\1>/s', create_function('$matches', 'return str_replace("\n", "<WPPreserveNewline />", $matches[0]);'), $pee);
       
   165 		$pee = preg_replace('|(?<!<br />)\s*\n|', "<br />\n", $pee); // optionally make line breaks
       
   166 		$pee = str_replace('<WPPreserveNewline />', "\n", $pee);
       
   167 	}
       
   168 	$pee = preg_replace('!(</?' . $allblocks . '[^>]*>)\s*<br />!', "$1", $pee);
       
   169 	$pee = preg_replace('!<br />(\s*</?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)[^>]*>)!', '$1', $pee);
       
   170 	if (strpos($pee, '<pre') !== false)
       
   171 		$pee = preg_replace_callback('!(<pre[^>]*>)(.*?)</pre>!is', 'clean_pre', $pee );
       
   172 	$pee = preg_replace( "|\n</p>$|", '</p>', $pee );
       
   173 	$pee = preg_replace('/<p>\s*?(' . get_shortcode_regex() . ')\s*<\/p>/s', '$1', $pee); // don't auto-p wrap shortcodes that stand alone
       
   174 
       
   175 	return $pee;
       
   176 }
       
   177 
       
   178 /**
       
   179  * Checks to see if a string is utf8 encoded.
       
   180  *
       
   181  * NOTE: This function checks for 5-Byte sequences, UTF8
       
   182  *       has Bytes Sequences with a maximum length of 4.
       
   183  *
       
   184  * @author bmorel at ssi dot fr (modified)
       
   185  * @since 1.2.1
       
   186  *
       
   187  * @param string $str The string to be checked
       
   188  * @return bool True if $str fits a UTF-8 model, false otherwise.
       
   189  */
       
   190 function seems_utf8($str) {
       
   191 	$length = strlen($str);
       
   192 	for ($i=0; $i < $length; $i++) {
       
   193 		$c = ord($str[$i]);
       
   194 		if ($c < 0x80) $n = 0; # 0bbbbbbb
       
   195 		elseif (($c & 0xE0) == 0xC0) $n=1; # 110bbbbb
       
   196 		elseif (($c & 0xF0) == 0xE0) $n=2; # 1110bbbb
       
   197 		elseif (($c & 0xF8) == 0xF0) $n=3; # 11110bbb
       
   198 		elseif (($c & 0xFC) == 0xF8) $n=4; # 111110bb
       
   199 		elseif (($c & 0xFE) == 0xFC) $n=5; # 1111110b
       
   200 		else return false; # Does not match any model
       
   201 		for ($j=0; $j<$n; $j++) { # n bytes matching 10bbbbbb follow ?
       
   202 			if ((++$i == $length) || ((ord($str[$i]) & 0xC0) != 0x80))
       
   203 				return false;
       
   204 		}
       
   205 	}
       
   206 	return true;
       
   207 }
       
   208 
       
   209 /**
       
   210  * Converts a number of special characters into their HTML entities.
       
   211  *
       
   212  * Specifically deals with: &, <, >, ", and '.
       
   213  *
       
   214  * $quote_style can be set to ENT_COMPAT to encode " to
       
   215  * &quot;, or ENT_QUOTES to do both. Default is ENT_NOQUOTES where no quotes are encoded.
       
   216  *
       
   217  * @since 1.2.2
       
   218  *
       
   219  * @param string $string The text which is to be encoded.
       
   220  * @param mixed $quote_style Optional. Converts double quotes if set to ENT_COMPAT, both single and double if set to ENT_QUOTES or none if set to ENT_NOQUOTES. Also compatible with old values; converting single quotes if set to 'single', double if set to 'double' or both if otherwise set. Default is ENT_NOQUOTES.
       
   221  * @param string $charset Optional. The character encoding of the string. Default is false.
       
   222  * @param boolean $double_encode Optional. Whether or not to encode existing html entities. Default is false.
       
   223  * @return string The encoded text with HTML entities.
       
   224  */
       
   225 function _wp_specialchars( $string, $quote_style = ENT_NOQUOTES, $charset = false, $double_encode = false ) {
       
   226 	$string = (string) $string;
       
   227 
       
   228 	if ( 0 === strlen( $string ) ) {
       
   229 		return '';
       
   230 	}
       
   231 
       
   232 	// Don't bother if there are no specialchars - saves some processing
       
   233 	if ( !preg_match( '/[&<>"\']/', $string ) ) {
       
   234 		return $string;
       
   235 	}
       
   236 
       
   237 	// Account for the previous behaviour of the function when the $quote_style is not an accepted value
       
   238 	if ( empty( $quote_style ) ) {
       
   239 		$quote_style = ENT_NOQUOTES;
       
   240 	} elseif ( !in_array( $quote_style, array( 0, 2, 3, 'single', 'double' ), true ) ) {
       
   241 		$quote_style = ENT_QUOTES;
       
   242 	}
       
   243 
       
   244 	// Store the site charset as a static to avoid multiple calls to wp_load_alloptions()
       
   245 	if ( !$charset ) {
       
   246 		static $_charset;
       
   247 		if ( !isset( $_charset ) ) {
       
   248 			$alloptions = wp_load_alloptions();
       
   249 			$_charset = isset( $alloptions['blog_charset'] ) ? $alloptions['blog_charset'] : '';
       
   250 		}
       
   251 		$charset = $_charset;
       
   252 	}
       
   253 	if ( in_array( $charset, array( 'utf8', 'utf-8', 'UTF8' ) ) ) {
       
   254 		$charset = 'UTF-8';
       
   255 	}
       
   256 
       
   257 	$_quote_style = $quote_style;
       
   258 
       
   259 	if ( $quote_style === 'double' ) {
       
   260 		$quote_style = ENT_COMPAT;
       
   261 		$_quote_style = ENT_COMPAT;
       
   262 	} elseif ( $quote_style === 'single' ) {
       
   263 		$quote_style = ENT_NOQUOTES;
       
   264 	}
       
   265 
       
   266 	// Handle double encoding ourselves
       
   267 	if ( !$double_encode ) {
       
   268 		$string = wp_specialchars_decode( $string, $_quote_style );
       
   269 		$string = preg_replace( '/&(#?x?[0-9a-z]+);/i', '|wp_entity|$1|/wp_entity|', $string );
       
   270 	}
       
   271 
       
   272 	$string = @htmlspecialchars( $string, $quote_style, $charset );
       
   273 
       
   274 	// Handle double encoding ourselves
       
   275 	if ( !$double_encode ) {
       
   276 		$string = str_replace( array( '|wp_entity|', '|/wp_entity|' ), array( '&', ';' ), $string );
       
   277 	}
       
   278 
       
   279 	// Backwards compatibility
       
   280 	if ( 'single' === $_quote_style ) {
       
   281 		$string = str_replace( "'", '&#039;', $string );
       
   282 	}
       
   283 
       
   284 	return $string;
       
   285 }
       
   286 
       
   287 /**
       
   288  * Converts a number of HTML entities into their special characters.
       
   289  *
       
   290  * Specifically deals with: &, <, >, ", and '.
       
   291  *
       
   292  * $quote_style can be set to ENT_COMPAT to decode " entities,
       
   293  * or ENT_QUOTES to do both " and '. Default is ENT_NOQUOTES where no quotes are decoded.
       
   294  *
       
   295  * @since 2.8
       
   296  *
       
   297  * @param string $string The text which is to be decoded.
       
   298  * @param mixed $quote_style Optional. Converts double quotes if set to ENT_COMPAT, both single and double if set to ENT_QUOTES or none if set to ENT_NOQUOTES. Also compatible with old _wp_specialchars() values; converting single quotes if set to 'single', double if set to 'double' or both if otherwise set. Default is ENT_NOQUOTES.
       
   299  * @return string The decoded text without HTML entities.
       
   300  */
       
   301 function wp_specialchars_decode( $string, $quote_style = ENT_NOQUOTES ) {
       
   302 	$string = (string) $string;
       
   303 
       
   304 	if ( 0 === strlen( $string ) ) {
       
   305 		return '';
       
   306 	}
       
   307 
       
   308 	// Don't bother if there are no entities - saves a lot of processing
       
   309 	if ( strpos( $string, '&' ) === false ) {
       
   310 		return $string;
       
   311 	}
       
   312 
       
   313 	// Match the previous behaviour of _wp_specialchars() when the $quote_style is not an accepted value
       
   314 	if ( empty( $quote_style ) ) {
       
   315 		$quote_style = ENT_NOQUOTES;
       
   316 	} elseif ( !in_array( $quote_style, array( 0, 2, 3, 'single', 'double' ), true ) ) {
       
   317 		$quote_style = ENT_QUOTES;
       
   318 	}
       
   319 
       
   320 	// More complete than get_html_translation_table( HTML_SPECIALCHARS )
       
   321 	$single = array( '&#039;'  => '\'', '&#x27;' => '\'' );
       
   322 	$single_preg = array( '/&#0*39;/'  => '&#039;', '/&#x0*27;/i' => '&#x27;' );
       
   323 	$double = array( '&quot;' => '"', '&#034;'  => '"', '&#x22;' => '"' );
       
   324 	$double_preg = array( '/&#0*34;/'  => '&#034;', '/&#x0*22;/i' => '&#x22;' );
       
   325 	$others = array( '&lt;'   => '<', '&#060;'  => '<', '&gt;'   => '>', '&#062;'  => '>', '&amp;'  => '&', '&#038;'  => '&', '&#x26;' => '&' );
       
   326 	$others_preg = array( '/&#0*60;/'  => '&#060;', '/&#0*62;/'  => '&#062;', '/&#0*38;/'  => '&#038;', '/&#x0*26;/i' => '&#x26;' );
       
   327 
       
   328 	if ( $quote_style === ENT_QUOTES ) {
       
   329 		$translation = array_merge( $single, $double, $others );
       
   330 		$translation_preg = array_merge( $single_preg, $double_preg, $others_preg );
       
   331 	} elseif ( $quote_style === ENT_COMPAT || $quote_style === 'double' ) {
       
   332 		$translation = array_merge( $double, $others );
       
   333 		$translation_preg = array_merge( $double_preg, $others_preg );
       
   334 	} elseif ( $quote_style === 'single' ) {
       
   335 		$translation = array_merge( $single, $others );
       
   336 		$translation_preg = array_merge( $single_preg, $others_preg );
       
   337 	} elseif ( $quote_style === ENT_NOQUOTES ) {
       
   338 		$translation = $others;
       
   339 		$translation_preg = $others_preg;
       
   340 	}
       
   341 
       
   342 	// Remove zero padding on numeric entities
       
   343 	$string = preg_replace( array_keys( $translation_preg ), array_values( $translation_preg ), $string );
       
   344 
       
   345 	// Replace characters according to translation table
       
   346 	return strtr( $string, $translation );
       
   347 }
       
   348 
       
   349 /**
       
   350  * Checks for invalid UTF8 in a string.
       
   351  *
       
   352  * @since 2.8
       
   353  *
       
   354  * @param string $string The text which is to be checked.
       
   355  * @param boolean $strip Optional. Whether to attempt to strip out invalid UTF8. Default is false.
       
   356  * @return string The checked text.
       
   357  */
       
   358 function wp_check_invalid_utf8( $string, $strip = false ) {
       
   359 	$string = (string) $string;
       
   360 
       
   361 	if ( 0 === strlen( $string ) ) {
       
   362 		return '';
       
   363 	}
       
   364 
       
   365 	// Store the site charset as a static to avoid multiple calls to get_option()
       
   366 	static $is_utf8;
       
   367 	if ( !isset( $is_utf8 ) ) {
       
   368 		$is_utf8 = in_array( get_option( 'blog_charset' ), array( 'utf8', 'utf-8', 'UTF8', 'UTF-8' ) );
       
   369 	}
       
   370 	if ( !$is_utf8 ) {
       
   371 		return $string;
       
   372 	}
       
   373 
       
   374 	// Check for support for utf8 in the installed PCRE library once and store the result in a static
       
   375 	static $utf8_pcre;
       
   376 	if ( !isset( $utf8_pcre ) ) {
       
   377 		$utf8_pcre = @preg_match( '/^./u', 'a' );
       
   378 	}
       
   379 	// We can't demand utf8 in the PCRE installation, so just return the string in those cases
       
   380 	if ( !$utf8_pcre ) {
       
   381 		return $string;
       
   382 	}
       
   383 
       
   384 	// preg_match fails when it encounters invalid UTF8 in $string
       
   385 	if ( 1 === @preg_match( '/^./us', $string ) ) {
       
   386 		return $string;
       
   387 	}
       
   388 
       
   389 	// Attempt to strip the bad chars if requested (not recommended)
       
   390 	if ( $strip && function_exists( 'iconv' ) ) {
       
   391 		return iconv( 'utf-8', 'utf-8', $string );
       
   392 	}
       
   393 
       
   394 	return '';
       
   395 }
       
   396 
       
   397 /**
       
   398  * Encode the Unicode values to be used in the URI.
       
   399  *
       
   400  * @since 1.5.0
       
   401  *
       
   402  * @param string $utf8_string
       
   403  * @param int $length Max length of the string
       
   404  * @return string String with Unicode encoded for URI.
       
   405  */
       
   406 function utf8_uri_encode( $utf8_string, $length = 0 ) {
       
   407 	$unicode = '';
       
   408 	$values = array();
       
   409 	$num_octets = 1;
       
   410 	$unicode_length = 0;
       
   411 
       
   412 	$string_length = strlen( $utf8_string );
       
   413 	for ($i = 0; $i < $string_length; $i++ ) {
       
   414 
       
   415 		$value = ord( $utf8_string[ $i ] );
       
   416 
       
   417 		if ( $value < 128 ) {
       
   418 			if ( $length && ( $unicode_length >= $length ) )
       
   419 				break;
       
   420 			$unicode .= chr($value);
       
   421 			$unicode_length++;
       
   422 		} else {
       
   423 			if ( count( $values ) == 0 ) $num_octets = ( $value < 224 ) ? 2 : 3;
       
   424 
       
   425 			$values[] = $value;
       
   426 
       
   427 			if ( $length && ( $unicode_length + ($num_octets * 3) ) > $length )
       
   428 				break;
       
   429 			if ( count( $values ) == $num_octets ) {
       
   430 				if ($num_octets == 3) {
       
   431 					$unicode .= '%' . dechex($values[0]) . '%' . dechex($values[1]) . '%' . dechex($values[2]);
       
   432 					$unicode_length += 9;
       
   433 				} else {
       
   434 					$unicode .= '%' . dechex($values[0]) . '%' . dechex($values[1]);
       
   435 					$unicode_length += 6;
       
   436 				}
       
   437 
       
   438 				$values = array();
       
   439 				$num_octets = 1;
       
   440 			}
       
   441 		}
       
   442 	}
       
   443 
       
   444 	return $unicode;
       
   445 }
       
   446 
       
   447 /**
       
   448  * Converts all accent characters to ASCII characters.
       
   449  *
       
   450  * If there are no accent characters, then the string given is just returned.
       
   451  *
       
   452  * @since 1.2.1
       
   453  *
       
   454  * @param string $string Text that might have accent characters
       
   455  * @return string Filtered string with replaced "nice" characters.
       
   456  */
       
   457 function remove_accents($string) {
       
   458 	if ( !preg_match('/[\x80-\xff]/', $string) )
       
   459 		return $string;
       
   460 
       
   461 	if (seems_utf8($string)) {
       
   462 		$chars = array(
       
   463 		// Decompositions for Latin-1 Supplement
       
   464 		chr(195).chr(128) => 'A', chr(195).chr(129) => 'A',
       
   465 		chr(195).chr(130) => 'A', chr(195).chr(131) => 'A',
       
   466 		chr(195).chr(132) => 'A', chr(195).chr(133) => 'A',
       
   467 		chr(195).chr(135) => 'C', chr(195).chr(136) => 'E',
       
   468 		chr(195).chr(137) => 'E', chr(195).chr(138) => 'E',
       
   469 		chr(195).chr(139) => 'E', chr(195).chr(140) => 'I',
       
   470 		chr(195).chr(141) => 'I', chr(195).chr(142) => 'I',
       
   471 		chr(195).chr(143) => 'I', chr(195).chr(145) => 'N',
       
   472 		chr(195).chr(146) => 'O', chr(195).chr(147) => 'O',
       
   473 		chr(195).chr(148) => 'O', chr(195).chr(149) => 'O',
       
   474 		chr(195).chr(150) => 'O', chr(195).chr(153) => 'U',
       
   475 		chr(195).chr(154) => 'U', chr(195).chr(155) => 'U',
       
   476 		chr(195).chr(156) => 'U', chr(195).chr(157) => 'Y',
       
   477 		chr(195).chr(159) => 's', chr(195).chr(160) => 'a',
       
   478 		chr(195).chr(161) => 'a', chr(195).chr(162) => 'a',
       
   479 		chr(195).chr(163) => 'a', chr(195).chr(164) => 'a',
       
   480 		chr(195).chr(165) => 'a', chr(195).chr(167) => 'c',
       
   481 		chr(195).chr(168) => 'e', chr(195).chr(169) => 'e',
       
   482 		chr(195).chr(170) => 'e', chr(195).chr(171) => 'e',
       
   483 		chr(195).chr(172) => 'i', chr(195).chr(173) => 'i',
       
   484 		chr(195).chr(174) => 'i', chr(195).chr(175) => 'i',
       
   485 		chr(195).chr(177) => 'n', chr(195).chr(178) => 'o',
       
   486 		chr(195).chr(179) => 'o', chr(195).chr(180) => 'o',
       
   487 		chr(195).chr(181) => 'o', chr(195).chr(182) => 'o',
       
   488 		chr(195).chr(182) => 'o', chr(195).chr(185) => 'u',
       
   489 		chr(195).chr(186) => 'u', chr(195).chr(187) => 'u',
       
   490 		chr(195).chr(188) => 'u', chr(195).chr(189) => 'y',
       
   491 		chr(195).chr(191) => 'y',
       
   492 		// Decompositions for Latin Extended-A
       
   493 		chr(196).chr(128) => 'A', chr(196).chr(129) => 'a',
       
   494 		chr(196).chr(130) => 'A', chr(196).chr(131) => 'a',
       
   495 		chr(196).chr(132) => 'A', chr(196).chr(133) => 'a',
       
   496 		chr(196).chr(134) => 'C', chr(196).chr(135) => 'c',
       
   497 		chr(196).chr(136) => 'C', chr(196).chr(137) => 'c',
       
   498 		chr(196).chr(138) => 'C', chr(196).chr(139) => 'c',
       
   499 		chr(196).chr(140) => 'C', chr(196).chr(141) => 'c',
       
   500 		chr(196).chr(142) => 'D', chr(196).chr(143) => 'd',
       
   501 		chr(196).chr(144) => 'D', chr(196).chr(145) => 'd',
       
   502 		chr(196).chr(146) => 'E', chr(196).chr(147) => 'e',
       
   503 		chr(196).chr(148) => 'E', chr(196).chr(149) => 'e',
       
   504 		chr(196).chr(150) => 'E', chr(196).chr(151) => 'e',
       
   505 		chr(196).chr(152) => 'E', chr(196).chr(153) => 'e',
       
   506 		chr(196).chr(154) => 'E', chr(196).chr(155) => 'e',
       
   507 		chr(196).chr(156) => 'G', chr(196).chr(157) => 'g',
       
   508 		chr(196).chr(158) => 'G', chr(196).chr(159) => 'g',
       
   509 		chr(196).chr(160) => 'G', chr(196).chr(161) => 'g',
       
   510 		chr(196).chr(162) => 'G', chr(196).chr(163) => 'g',
       
   511 		chr(196).chr(164) => 'H', chr(196).chr(165) => 'h',
       
   512 		chr(196).chr(166) => 'H', chr(196).chr(167) => 'h',
       
   513 		chr(196).chr(168) => 'I', chr(196).chr(169) => 'i',
       
   514 		chr(196).chr(170) => 'I', chr(196).chr(171) => 'i',
       
   515 		chr(196).chr(172) => 'I', chr(196).chr(173) => 'i',
       
   516 		chr(196).chr(174) => 'I', chr(196).chr(175) => 'i',
       
   517 		chr(196).chr(176) => 'I', chr(196).chr(177) => 'i',
       
   518 		chr(196).chr(178) => 'IJ',chr(196).chr(179) => 'ij',
       
   519 		chr(196).chr(180) => 'J', chr(196).chr(181) => 'j',
       
   520 		chr(196).chr(182) => 'K', chr(196).chr(183) => 'k',
       
   521 		chr(196).chr(184) => 'k', chr(196).chr(185) => 'L',
       
   522 		chr(196).chr(186) => 'l', chr(196).chr(187) => 'L',
       
   523 		chr(196).chr(188) => 'l', chr(196).chr(189) => 'L',
       
   524 		chr(196).chr(190) => 'l', chr(196).chr(191) => 'L',
       
   525 		chr(197).chr(128) => 'l', chr(197).chr(129) => 'L',
       
   526 		chr(197).chr(130) => 'l', chr(197).chr(131) => 'N',
       
   527 		chr(197).chr(132) => 'n', chr(197).chr(133) => 'N',
       
   528 		chr(197).chr(134) => 'n', chr(197).chr(135) => 'N',
       
   529 		chr(197).chr(136) => 'n', chr(197).chr(137) => 'N',
       
   530 		chr(197).chr(138) => 'n', chr(197).chr(139) => 'N',
       
   531 		chr(197).chr(140) => 'O', chr(197).chr(141) => 'o',
       
   532 		chr(197).chr(142) => 'O', chr(197).chr(143) => 'o',
       
   533 		chr(197).chr(144) => 'O', chr(197).chr(145) => 'o',
       
   534 		chr(197).chr(146) => 'OE',chr(197).chr(147) => 'oe',
       
   535 		chr(197).chr(148) => 'R',chr(197).chr(149) => 'r',
       
   536 		chr(197).chr(150) => 'R',chr(197).chr(151) => 'r',
       
   537 		chr(197).chr(152) => 'R',chr(197).chr(153) => 'r',
       
   538 		chr(197).chr(154) => 'S',chr(197).chr(155) => 's',
       
   539 		chr(197).chr(156) => 'S',chr(197).chr(157) => 's',
       
   540 		chr(197).chr(158) => 'S',chr(197).chr(159) => 's',
       
   541 		chr(197).chr(160) => 'S', chr(197).chr(161) => 's',
       
   542 		chr(197).chr(162) => 'T', chr(197).chr(163) => 't',
       
   543 		chr(197).chr(164) => 'T', chr(197).chr(165) => 't',
       
   544 		chr(197).chr(166) => 'T', chr(197).chr(167) => 't',
       
   545 		chr(197).chr(168) => 'U', chr(197).chr(169) => 'u',
       
   546 		chr(197).chr(170) => 'U', chr(197).chr(171) => 'u',
       
   547 		chr(197).chr(172) => 'U', chr(197).chr(173) => 'u',
       
   548 		chr(197).chr(174) => 'U', chr(197).chr(175) => 'u',
       
   549 		chr(197).chr(176) => 'U', chr(197).chr(177) => 'u',
       
   550 		chr(197).chr(178) => 'U', chr(197).chr(179) => 'u',
       
   551 		chr(197).chr(180) => 'W', chr(197).chr(181) => 'w',
       
   552 		chr(197).chr(182) => 'Y', chr(197).chr(183) => 'y',
       
   553 		chr(197).chr(184) => 'Y', chr(197).chr(185) => 'Z',
       
   554 		chr(197).chr(186) => 'z', chr(197).chr(187) => 'Z',
       
   555 		chr(197).chr(188) => 'z', chr(197).chr(189) => 'Z',
       
   556 		chr(197).chr(190) => 'z', chr(197).chr(191) => 's',
       
   557 		// Euro Sign
       
   558 		chr(226).chr(130).chr(172) => 'E',
       
   559 		// GBP (Pound) Sign
       
   560 		chr(194).chr(163) => '');
       
   561 
       
   562 		$string = strtr($string, $chars);
       
   563 	} else {
       
   564 		// Assume ISO-8859-1 if not UTF-8
       
   565 		$chars['in'] = chr(128).chr(131).chr(138).chr(142).chr(154).chr(158)
       
   566 			.chr(159).chr(162).chr(165).chr(181).chr(192).chr(193).chr(194)
       
   567 			.chr(195).chr(196).chr(197).chr(199).chr(200).chr(201).chr(202)
       
   568 			.chr(203).chr(204).chr(205).chr(206).chr(207).chr(209).chr(210)
       
   569 			.chr(211).chr(212).chr(213).chr(214).chr(216).chr(217).chr(218)
       
   570 			.chr(219).chr(220).chr(221).chr(224).chr(225).chr(226).chr(227)
       
   571 			.chr(228).chr(229).chr(231).chr(232).chr(233).chr(234).chr(235)
       
   572 			.chr(236).chr(237).chr(238).chr(239).chr(241).chr(242).chr(243)
       
   573 			.chr(244).chr(245).chr(246).chr(248).chr(249).chr(250).chr(251)
       
   574 			.chr(252).chr(253).chr(255);
       
   575 
       
   576 		$chars['out'] = "EfSZszYcYuAAAAAACEEEEIIIINOOOOOOUUUUYaaaaaaceeeeiiiinoooooouuuuyy";
       
   577 
       
   578 		$string = strtr($string, $chars['in'], $chars['out']);
       
   579 		$double_chars['in'] = array(chr(140), chr(156), chr(198), chr(208), chr(222), chr(223), chr(230), chr(240), chr(254));
       
   580 		$double_chars['out'] = array('OE', 'oe', 'AE', 'DH', 'TH', 'ss', 'ae', 'dh', 'th');
       
   581 		$string = str_replace($double_chars['in'], $double_chars['out'], $string);
       
   582 	}
       
   583 
       
   584 	return $string;
       
   585 }
       
   586 
       
   587 /**
       
   588  * Sanitizes a filename replacing whitespace with dashes
       
   589  *
       
   590  * Removes special characters that are illegal in filenames on certain
       
   591  * operating systems and special characters requiring special escaping
       
   592  * to manipulate at the command line. Replaces spaces and consecutive
       
   593  * dashes with a single dash. Trim period, dash and underscore from beginning
       
   594  * and end of filename.
       
   595  *
       
   596  * @since 2.1.0
       
   597  *
       
   598  * @param string $filename The filename to be sanitized
       
   599  * @return string The sanitized filename
       
   600  */
       
   601 function sanitize_file_name( $filename ) {
       
   602 	$filename_raw = $filename;
       
   603 	$special_chars = array("?", "[", "]", "/", "\\", "=", "<", ">", ":", ";", ",", "'", "\"", "&", "$", "#", "*", "(", ")", "|", "~", "`", "!", "{", "}", chr(0));
       
   604 	$special_chars = apply_filters('sanitize_file_name_chars', $special_chars, $filename_raw);
       
   605 	$filename = str_replace($special_chars, '', $filename);
       
   606 	$filename = preg_replace('/[\s-]+/', '-', $filename);
       
   607 	$filename = trim($filename, '.-_');
       
   608 
       
   609 	// Split the filename into a base and extension[s]
       
   610 	$parts = explode('.', $filename);
       
   611 
       
   612 	// Return if only one extension
       
   613 	if ( count($parts) <= 2 )
       
   614 		return apply_filters('sanitize_file_name', $filename, $filename_raw);
       
   615 
       
   616 	// Process multiple extensions
       
   617 	$filename = array_shift($parts);
       
   618 	$extension = array_pop($parts);
       
   619 	$mimes = get_allowed_mime_types();
       
   620 
       
   621 	// Loop over any intermediate extensions.  Munge them with a trailing underscore if they are a 2 - 5 character
       
   622 	// long alpha string not in the extension whitelist.
       
   623 	foreach ( (array) $parts as $part) {
       
   624 		$filename .= '.' . $part;
       
   625 		
       
   626 		if ( preg_match("/^[a-zA-Z]{2,5}\d?$/", $part) ) {
       
   627 			$allowed = false;
       
   628 			foreach ( $mimes as $ext_preg => $mime_match ) {
       
   629 				$ext_preg = '!(^' . $ext_preg . ')$!i';
       
   630 				if ( preg_match( $ext_preg, $part ) ) {
       
   631 					$allowed = true;
       
   632 					break;
       
   633 				}
       
   634 			}
       
   635 			if ( !$allowed )
       
   636 				$filename .= '_';
       
   637 		}
       
   638 	}
       
   639 	$filename .= '.' . $extension;
       
   640 
       
   641 	return apply_filters('sanitize_file_name', $filename, $filename_raw);
       
   642 }
       
   643 
       
   644 /**
       
   645  * Sanitize username stripping out unsafe characters.
       
   646  *
       
   647  * If $strict is true, only alphanumeric characters (as well as _, space, ., -,
       
   648  * @) are returned.
       
   649  * Removes tags, octets, entities, and if strict is enabled, will remove all
       
   650  * non-ASCII characters. After sanitizing, it passes the username, raw username
       
   651  * (the username in the parameter), and the strict parameter as parameters for
       
   652  * the filter.
       
   653  *
       
   654  * @since 2.0.0
       
   655  * @uses apply_filters() Calls 'sanitize_user' hook on username, raw username,
       
   656  *		and $strict parameter.
       
   657  *
       
   658  * @param string $username The username to be sanitized.
       
   659  * @param bool $strict If set limits $username to specific characters. Default false.
       
   660  * @return string The sanitized username, after passing through filters.
       
   661  */
       
   662 function sanitize_user( $username, $strict = false ) {
       
   663 	$raw_username = $username;
       
   664 	$username = strip_tags($username);
       
   665 	// Kill octets
       
   666 	$username = preg_replace('|%([a-fA-F0-9][a-fA-F0-9])|', '', $username);
       
   667 	$username = preg_replace('/&.+?;/', '', $username); // Kill entities
       
   668 
       
   669 	// If strict, reduce to ASCII for max portability.
       
   670 	if ( $strict )
       
   671 		$username = preg_replace('|[^a-z0-9 _.\-@]|i', '', $username);
       
   672 
       
   673 	// Consolidate contiguous whitespace
       
   674 	$username = preg_replace('|\s+|', ' ', $username);
       
   675 
       
   676 	return apply_filters('sanitize_user', $username, $raw_username, $strict);
       
   677 }
       
   678 
       
   679 /**
       
   680  * Sanitizes title or use fallback title.
       
   681  *
       
   682  * Specifically, HTML and PHP tags are stripped. Further actions can be added
       
   683  * via the plugin API. If $title is empty and $fallback_title is set, the latter
       
   684  * will be used.
       
   685  *
       
   686  * @since 1.0.0
       
   687  *
       
   688  * @param string $title The string to be sanitized.
       
   689  * @param string $fallback_title Optional. A title to use if $title is empty.
       
   690  * @return string The sanitized string.
       
   691  */
       
   692 function sanitize_title($title, $fallback_title = '') {
       
   693 	$raw_title = $title;
       
   694 	$title = strip_tags($title);
       
   695 	$title = apply_filters('sanitize_title', $title, $raw_title);
       
   696 
       
   697 	if ( '' === $title || false === $title )
       
   698 		$title = $fallback_title;
       
   699 
       
   700 	return $title;
       
   701 }
       
   702 
       
   703 /**
       
   704  * Sanitizes title, replacing whitespace with dashes.
       
   705  *
       
   706  * Limits the output to alphanumeric characters, underscore (_) and dash (-).
       
   707  * Whitespace becomes a dash.
       
   708  *
       
   709  * @since 1.2.0
       
   710  *
       
   711  * @param string $title The title to be sanitized.
       
   712  * @return string The sanitized title.
       
   713  */
       
   714 function sanitize_title_with_dashes($title) {
       
   715 	$title = strip_tags($title);
       
   716 	// Preserve escaped octets.
       
   717 	$title = preg_replace('|%([a-fA-F0-9][a-fA-F0-9])|', '---$1---', $title);
       
   718 	// Remove percent signs that are not part of an octet.
       
   719 	$title = str_replace('%', '', $title);
       
   720 	// Restore octets.
       
   721 	$title = preg_replace('|---([a-fA-F0-9][a-fA-F0-9])---|', '%$1', $title);
       
   722 
       
   723 	$title = remove_accents($title);
       
   724 	if (seems_utf8($title)) {
       
   725 		if (function_exists('mb_strtolower')) {
       
   726 			$title = mb_strtolower($title, 'UTF-8');
       
   727 		}
       
   728 		$title = utf8_uri_encode($title, 200);
       
   729 	}
       
   730 
       
   731 	$title = strtolower($title);
       
   732 	$title = preg_replace('/&.+?;/', '', $title); // kill entities
       
   733 	$title = str_replace('.', '-', $title);
       
   734 	$title = preg_replace('/[^%a-z0-9 _-]/', '', $title);
       
   735 	$title = preg_replace('/\s+/', '-', $title);
       
   736 	$title = preg_replace('|-+|', '-', $title);
       
   737 	$title = trim($title, '-');
       
   738 
       
   739 	return $title;
       
   740 }
       
   741 
       
   742 /**
       
   743  * Ensures a string is a valid SQL order by clause.
       
   744  *
       
   745  * Accepts one or more columns, with or without ASC/DESC, and also accepts
       
   746  * RAND().
       
   747  *
       
   748  * @since 2.5.1
       
   749  *
       
   750  * @param string $orderby Order by string to be checked.
       
   751  * @return string|false Returns the order by clause if it is a match, false otherwise.
       
   752  */
       
   753 function sanitize_sql_orderby( $orderby ){
       
   754 	preg_match('/^\s*([a-z0-9_]+(\s+(ASC|DESC))?(\s*,\s*|\s*$))+|^\s*RAND\(\s*\)\s*$/i', $orderby, $obmatches);
       
   755 	if ( !$obmatches )
       
   756 		return false;
       
   757 	return $orderby;
       
   758 }
       
   759 
       
   760 /**
       
   761  * Santizes a html classname to ensure it only contains valid characters
       
   762  *
       
   763  * Strips the string down to A-Z,a-z,0-9,'-' if this results in an empty
       
   764  * string then it will return the alternative value supplied.
       
   765  *
       
   766  * @todo Expand to support the full range of CDATA that a class attribute can contain.
       
   767  *
       
   768  * @since 2.8.0
       
   769  *
       
   770  * @param string $class The classname to be sanitized
       
   771  * @param string $fallback The value to return if the sanitization end's up as an empty string.
       
   772  * @return string The sanitized value
       
   773  */
       
   774 function sanitize_html_class($class, $fallback){
       
   775 	//Strip out any % encoded octets
       
   776 	$sanitized = preg_replace('|%[a-fA-F0-9][a-fA-F0-9]|', '', $class);
       
   777 
       
   778 	//Limit to A-Z,a-z,0-9,'-'
       
   779 	$sanitized = preg_replace('/[^A-Za-z0-9-]/', '', $sanitized);
       
   780 
       
   781 	if ('' == $sanitized)
       
   782 		$sanitized = $fallback;
       
   783 
       
   784 	return apply_filters('sanitize_html_class',$sanitized, $class, $fallback);
       
   785 }
       
   786 
       
   787 /**
       
   788  * Converts a number of characters from a string.
       
   789  *
       
   790  * Metadata tags <<title>> and <<category>> are removed, <<br>> and <<hr>> are
       
   791  * converted into correct XHTML and Unicode characters are converted to the
       
   792  * valid range.
       
   793  *
       
   794  * @since 0.71
       
   795  *
       
   796  * @param string $content String of characters to be converted.
       
   797  * @param string $deprecated Not used.
       
   798  * @return string Converted string.
       
   799  */
       
   800 function convert_chars($content, $deprecated = '') {
       
   801 	// Translation of invalid Unicode references range to valid range
       
   802 	$wp_htmltranswinuni = array(
       
   803 	'&#128;' => '&#8364;', // the Euro sign
       
   804 	'&#129;' => '',
       
   805 	'&#130;' => '&#8218;', // these are Windows CP1252 specific characters
       
   806 	'&#131;' => '&#402;',  // they would look weird on non-Windows browsers
       
   807 	'&#132;' => '&#8222;',
       
   808 	'&#133;' => '&#8230;',
       
   809 	'&#134;' => '&#8224;',
       
   810 	'&#135;' => '&#8225;',
       
   811 	'&#136;' => '&#710;',
       
   812 	'&#137;' => '&#8240;',
       
   813 	'&#138;' => '&#352;',
       
   814 	'&#139;' => '&#8249;',
       
   815 	'&#140;' => '&#338;',
       
   816 	'&#141;' => '',
       
   817 	'&#142;' => '&#382;',
       
   818 	'&#143;' => '',
       
   819 	'&#144;' => '',
       
   820 	'&#145;' => '&#8216;',
       
   821 	'&#146;' => '&#8217;',
       
   822 	'&#147;' => '&#8220;',
       
   823 	'&#148;' => '&#8221;',
       
   824 	'&#149;' => '&#8226;',
       
   825 	'&#150;' => '&#8211;',
       
   826 	'&#151;' => '&#8212;',
       
   827 	'&#152;' => '&#732;',
       
   828 	'&#153;' => '&#8482;',
       
   829 	'&#154;' => '&#353;',
       
   830 	'&#155;' => '&#8250;',
       
   831 	'&#156;' => '&#339;',
       
   832 	'&#157;' => '',
       
   833 	'&#158;' => '',
       
   834 	'&#159;' => '&#376;'
       
   835 	);
       
   836 
       
   837 	// Remove metadata tags
       
   838 	$content = preg_replace('/<title>(.+?)<\/title>/','',$content);
       
   839 	$content = preg_replace('/<category>(.+?)<\/category>/','',$content);
       
   840 
       
   841 	// Converts lone & characters into &#38; (a.k.a. &amp;)
       
   842 	$content = preg_replace('/&([^#])(?![a-z1-4]{1,8};)/i', '&#038;$1', $content);
       
   843 
       
   844 	// Fix Word pasting
       
   845 	$content = strtr($content, $wp_htmltranswinuni);
       
   846 
       
   847 	// Just a little XHTML help
       
   848 	$content = str_replace('<br>', '<br />', $content);
       
   849 	$content = str_replace('<hr>', '<hr />', $content);
       
   850 
       
   851 	return $content;
       
   852 }
       
   853 
       
   854 /**
       
   855  * Callback used to change %uXXXX to &#YYY; syntax
       
   856  *
       
   857  * @since 2.8?
       
   858  *
       
   859  * @param array $matches Single Match
       
   860  * @return string An HTML entity
       
   861  */
       
   862 function funky_javascript_callback($matches) {
       
   863 	return "&#".base_convert($matches[1],16,10).";";
       
   864 }
       
   865 
       
   866 /**
       
   867  * Fixes javascript bugs in browsers.
       
   868  *
       
   869  * Converts unicode characters to HTML numbered entities.
       
   870  *
       
   871  * @since 1.5.0
       
   872  * @uses $is_macIE
       
   873  * @uses $is_winIE
       
   874  *
       
   875  * @param string $text Text to be made safe.
       
   876  * @return string Fixed text.
       
   877  */
       
   878 function funky_javascript_fix($text) {
       
   879 	// Fixes for browsers' javascript bugs
       
   880 	global $is_macIE, $is_winIE;
       
   881 
       
   882 	if ( $is_winIE || $is_macIE )
       
   883 		$text =  preg_replace_callback("/\%u([0-9A-F]{4,4})/",
       
   884 					       "funky_javascript_callback",
       
   885 					       $text);
       
   886 
       
   887 	return $text;
       
   888 }
       
   889 
       
   890 /**
       
   891  * Will only balance the tags if forced to and the option is set to balance tags.
       
   892  *
       
   893  * The option 'use_balanceTags' is used for whether the tags will be balanced.
       
   894  * Both the $force parameter and 'use_balanceTags' option will have to be true
       
   895  * before the tags will be balanced.
       
   896  *
       
   897  * @since 0.71
       
   898  *
       
   899  * @param string $text Text to be balanced
       
   900  * @param bool $force Forces balancing, ignoring the value of the option. Default false.
       
   901  * @return string Balanced text
       
   902  */
       
   903 function balanceTags( $text, $force = false ) {
       
   904 	if ( !$force && get_option('use_balanceTags') == 0 )
       
   905 		return $text;
       
   906 	return force_balance_tags( $text );
       
   907 }
       
   908 
       
   909 /**
       
   910  * Balances tags of string using a modified stack.
       
   911  *
       
   912  * @since 2.0.4
       
   913  *
       
   914  * @author Leonard Lin <leonard@acm.org>
       
   915  * @license GPL v2.0
       
   916  * @copyright November 4, 2001
       
   917  * @version 1.1
       
   918  * @todo Make better - change loop condition to $text in 1.2
       
   919  * @internal Modified by Scott Reilly (coffee2code) 02 Aug 2004
       
   920  *		1.1  Fixed handling of append/stack pop order of end text
       
   921  *			 Added Cleaning Hooks
       
   922  *		1.0  First Version
       
   923  *
       
   924  * @param string $text Text to be balanced.
       
   925  * @return string Balanced text.
       
   926  */
       
   927 function force_balance_tags( $text ) {
       
   928 	$tagstack = array(); $stacksize = 0; $tagqueue = ''; $newtext = '';
       
   929 	$single_tags = array('br', 'hr', 'img', 'input'); //Known single-entity/self-closing tags
       
   930 	$nestable_tags = array('blockquote', 'div', 'span'); //Tags that can be immediately nested within themselves
       
   931 
       
   932 	# WP bug fix for comments - in case you REALLY meant to type '< !--'
       
   933 	$text = str_replace('< !--', '<    !--', $text);
       
   934 	# WP bug fix for LOVE <3 (and other situations with '<' before a number)
       
   935 	$text = preg_replace('#<([0-9]{1})#', '&lt;$1', $text);
       
   936 
       
   937 	while (preg_match("/<(\/?\w*)\s*([^>]*)>/",$text,$regex)) {
       
   938 		$newtext .= $tagqueue;
       
   939 
       
   940 		$i = strpos($text,$regex[0]);
       
   941 		$l = strlen($regex[0]);
       
   942 
       
   943 		// clear the shifter
       
   944 		$tagqueue = '';
       
   945 		// Pop or Push
       
   946 		if ( isset($regex[1][0]) && '/' == $regex[1][0] ) { // End Tag
       
   947 			$tag = strtolower(substr($regex[1],1));
       
   948 			// if too many closing tags
       
   949 			if($stacksize <= 0) {
       
   950 				$tag = '';
       
   951 				//or close to be safe $tag = '/' . $tag;
       
   952 			}
       
   953 			// if stacktop value = tag close value then pop
       
   954 			else if ($tagstack[$stacksize - 1] == $tag) { // found closing tag
       
   955 				$tag = '</' . $tag . '>'; // Close Tag
       
   956 				// Pop
       
   957 				array_pop ($tagstack);
       
   958 				$stacksize--;
       
   959 			} else { // closing tag not at top, search for it
       
   960 				for ($j=$stacksize-1;$j>=0;$j--) {
       
   961 					if ($tagstack[$j] == $tag) {
       
   962 					// add tag to tagqueue
       
   963 						for ($k=$stacksize-1;$k>=$j;$k--){
       
   964 							$tagqueue .= '</' . array_pop ($tagstack) . '>';
       
   965 							$stacksize--;
       
   966 						}
       
   967 						break;
       
   968 					}
       
   969 				}
       
   970 				$tag = '';
       
   971 			}
       
   972 		} else { // Begin Tag
       
   973 			$tag = strtolower($regex[1]);
       
   974 
       
   975 			// Tag Cleaning
       
   976 
       
   977 			// If self-closing or '', don't do anything.
       
   978 			if((substr($regex[2],-1) == '/') || ($tag == '')) {
       
   979 			}
       
   980 			// ElseIf it's a known single-entity tag but it doesn't close itself, do so
       
   981 			elseif ( in_array($tag, $single_tags) ) {
       
   982 				$regex[2] .= '/';
       
   983 			} else {	// Push the tag onto the stack
       
   984 				// If the top of the stack is the same as the tag we want to push, close previous tag
       
   985 				if (($stacksize > 0) && !in_array($tag, $nestable_tags) && ($tagstack[$stacksize - 1] == $tag)) {
       
   986 					$tagqueue = '</' . array_pop ($tagstack) . '>';
       
   987 					$stacksize--;
       
   988 				}
       
   989 				$stacksize = array_push ($tagstack, $tag);
       
   990 			}
       
   991 
       
   992 			// Attributes
       
   993 			$attributes = $regex[2];
       
   994 			if($attributes) {
       
   995 				$attributes = ' '.$attributes;
       
   996 			}
       
   997 			$tag = '<'.$tag.$attributes.'>';
       
   998 			//If already queuing a close tag, then put this tag on, too
       
   999 			if ($tagqueue) {
       
  1000 				$tagqueue .= $tag;
       
  1001 				$tag = '';
       
  1002 			}
       
  1003 		}
       
  1004 		$newtext .= substr($text,0,$i) . $tag;
       
  1005 		$text = substr($text,$i+$l);
       
  1006 	}
       
  1007 
       
  1008 	// Clear Tag Queue
       
  1009 	$newtext .= $tagqueue;
       
  1010 
       
  1011 	// Add Remaining text
       
  1012 	$newtext .= $text;
       
  1013 
       
  1014 	// Empty Stack
       
  1015 	while($x = array_pop($tagstack)) {
       
  1016 		$newtext .= '</' . $x . '>'; // Add remaining tags to close
       
  1017 	}
       
  1018 
       
  1019 	// WP fix for the bug with HTML comments
       
  1020 	$newtext = str_replace("< !--","<!--",$newtext);
       
  1021 	$newtext = str_replace("<    !--","< !--",$newtext);
       
  1022 
       
  1023 	return $newtext;
       
  1024 }
       
  1025 
       
  1026 /**
       
  1027  * Acts on text which is about to be edited.
       
  1028  *
       
  1029  * Unless $richedit is set, it is simply a holder for the 'format_to_edit'
       
  1030  * filter. If $richedit is set true htmlspecialchars() will be run on the
       
  1031  * content, converting special characters to HTMl entities.
       
  1032  *
       
  1033  * @since 0.71
       
  1034  *
       
  1035  * @param string $content The text about to be edited.
       
  1036  * @param bool $richedit Whether or not the $content should pass through htmlspecialchars(). Default false.
       
  1037  * @return string The text after the filter (and possibly htmlspecialchars()) has been run.
       
  1038  */
       
  1039 function format_to_edit($content, $richedit = false) {
       
  1040 	$content = apply_filters('format_to_edit', $content);
       
  1041 	if (! $richedit )
       
  1042 		$content = htmlspecialchars($content);
       
  1043 	return $content;
       
  1044 }
       
  1045 
       
  1046 /**
       
  1047  * Holder for the 'format_to_post' filter.
       
  1048  *
       
  1049  * @since 0.71
       
  1050  *
       
  1051  * @param string $content The text to pass through the filter.
       
  1052  * @return string Text returned from the 'format_to_post' filter.
       
  1053  */
       
  1054 function format_to_post($content) {
       
  1055 	$content = apply_filters('format_to_post', $content);
       
  1056 	return $content;
       
  1057 }
       
  1058 
       
  1059 /**
       
  1060  * Add leading zeros when necessary.
       
  1061  *
       
  1062  * If you set the threshold to '4' and the number is '10', then you will get
       
  1063  * back '0010'. If you set the number to '4' and the number is '5000', then you
       
  1064  * will get back '5000'.
       
  1065  *
       
  1066  * Uses sprintf to append the amount of zeros based on the $threshold parameter
       
  1067  * and the size of the number. If the number is large enough, then no zeros will
       
  1068  * be appended.
       
  1069  *
       
  1070  * @since 0.71
       
  1071  *
       
  1072  * @param mixed $number Number to append zeros to if not greater than threshold.
       
  1073  * @param int $threshold Digit places number needs to be to not have zeros added.
       
  1074  * @return string Adds leading zeros to number if needed.
       
  1075  */
       
  1076 function zeroise($number, $threshold) {
       
  1077 	return sprintf('%0'.$threshold.'s', $number);
       
  1078 }
       
  1079 
       
  1080 /**
       
  1081  * Adds backslashes before letters and before a number at the start of a string.
       
  1082  *
       
  1083  * @since 0.71
       
  1084  *
       
  1085  * @param string $string Value to which backslashes will be added.
       
  1086  * @return string String with backslashes inserted.
       
  1087  */
       
  1088 function backslashit($string) {
       
  1089 	$string = preg_replace('/^([0-9])/', '\\\\\\\\\1', $string);
       
  1090 	$string = preg_replace('/([a-z])/i', '\\\\\1', $string);
       
  1091 	return $string;
       
  1092 }
       
  1093 
       
  1094 /**
       
  1095  * Appends a trailing slash.
       
  1096  *
       
  1097  * Will remove trailing slash if it exists already before adding a trailing
       
  1098  * slash. This prevents double slashing a string or path.
       
  1099  *
       
  1100  * The primary use of this is for paths and thus should be used for paths. It is
       
  1101  * not restricted to paths and offers no specific path support.
       
  1102  *
       
  1103  * @since 1.2.0
       
  1104  * @uses untrailingslashit() Unslashes string if it was slashed already.
       
  1105  *
       
  1106  * @param string $string What to add the trailing slash to.
       
  1107  * @return string String with trailing slash added.
       
  1108  */
       
  1109 function trailingslashit($string) {
       
  1110 	return untrailingslashit($string) . '/';
       
  1111 }
       
  1112 
       
  1113 /**
       
  1114  * Removes trailing slash if it exists.
       
  1115  *
       
  1116  * The primary use of this is for paths and thus should be used for paths. It is
       
  1117  * not restricted to paths and offers no specific path support.
       
  1118  *
       
  1119  * @since 2.2.0
       
  1120  *
       
  1121  * @param string $string What to remove the trailing slash from.
       
  1122  * @return string String without the trailing slash.
       
  1123  */
       
  1124 function untrailingslashit($string) {
       
  1125 	return rtrim($string, '/');
       
  1126 }
       
  1127 
       
  1128 /**
       
  1129  * Adds slashes to escape strings.
       
  1130  *
       
  1131  * Slashes will first be removed if magic_quotes_gpc is set, see {@link
       
  1132  * http://www.php.net/magic_quotes} for more details.
       
  1133  *
       
  1134  * @since 0.71
       
  1135  *
       
  1136  * @param string $gpc The string returned from HTTP request data.
       
  1137  * @return string Returns a string escaped with slashes.
       
  1138  */
       
  1139 function addslashes_gpc($gpc) {
       
  1140 	global $wpdb;
       
  1141 
       
  1142 	if (get_magic_quotes_gpc()) {
       
  1143 		$gpc = stripslashes($gpc);
       
  1144 	}
       
  1145 
       
  1146 	return $wpdb->escape($gpc);
       
  1147 }
       
  1148 
       
  1149 /**
       
  1150  * Navigates through an array and removes slashes from the values.
       
  1151  *
       
  1152  * If an array is passed, the array_map() function causes a callback to pass the
       
  1153  * value back to the function. The slashes from this value will removed.
       
  1154  *
       
  1155  * @since 2.0.0
       
  1156  *
       
  1157  * @param array|string $value The array or string to be striped.
       
  1158  * @return array|string Stripped array (or string in the callback).
       
  1159  */
       
  1160 function stripslashes_deep($value) {
       
  1161 	$value = is_array($value) ? array_map('stripslashes_deep', $value) : stripslashes($value);
       
  1162 	return $value;
       
  1163 }
       
  1164 
       
  1165 /**
       
  1166  * Navigates through an array and encodes the values to be used in a URL.
       
  1167  *
       
  1168  * Uses a callback to pass the value of the array back to the function as a
       
  1169  * string.
       
  1170  *
       
  1171  * @since 2.2.0
       
  1172  *
       
  1173  * @param array|string $value The array or string to be encoded.
       
  1174  * @return array|string $value The encoded array (or string from the callback).
       
  1175  */
       
  1176 function urlencode_deep($value) {
       
  1177 	$value = is_array($value) ? array_map('urlencode_deep', $value) : urlencode($value);
       
  1178 	return $value;
       
  1179 }
       
  1180 
       
  1181 /**
       
  1182  * Converts email addresses characters to HTML entities to block spam bots.
       
  1183  *
       
  1184  * @since 0.71
       
  1185  *
       
  1186  * @param string $emailaddy Email address.
       
  1187  * @param int $mailto Optional. Range from 0 to 1. Used for encoding.
       
  1188  * @return string Converted email address.
       
  1189  */
       
  1190 function antispambot($emailaddy, $mailto=0) {
       
  1191 	$emailNOSPAMaddy = '';
       
  1192 	srand ((float) microtime() * 1000000);
       
  1193 	for ($i = 0; $i < strlen($emailaddy); $i = $i + 1) {
       
  1194 		$j = floor(rand(0, 1+$mailto));
       
  1195 		if ($j==0) {
       
  1196 			$emailNOSPAMaddy .= '&#'.ord(substr($emailaddy,$i,1)).';';
       
  1197 		} elseif ($j==1) {
       
  1198 			$emailNOSPAMaddy .= substr($emailaddy,$i,1);
       
  1199 		} elseif ($j==2) {
       
  1200 			$emailNOSPAMaddy .= '%'.zeroise(dechex(ord(substr($emailaddy, $i, 1))), 2);
       
  1201 		}
       
  1202 	}
       
  1203 	$emailNOSPAMaddy = str_replace('@','&#64;',$emailNOSPAMaddy);
       
  1204 	return $emailNOSPAMaddy;
       
  1205 }
       
  1206 
       
  1207 /**
       
  1208  * Callback to convert URI match to HTML A element.
       
  1209  *
       
  1210  * This function was backported from 2.5.0 to 2.3.2. Regex callback for {@link
       
  1211  * make_clickable()}.
       
  1212  *
       
  1213  * @since 2.3.2
       
  1214  * @access private
       
  1215  *
       
  1216  * @param array $matches Single Regex Match.
       
  1217  * @return string HTML A element with URI address.
       
  1218  */
       
  1219 function _make_url_clickable_cb($matches) {
       
  1220 	$url = $matches[2];
       
  1221 	$url = esc_url($url);
       
  1222 	if ( empty($url) )
       
  1223 		return $matches[0];
       
  1224 	return $matches[1] . "<a href=\"$url\" rel=\"nofollow\">$url</a>";
       
  1225 }
       
  1226 
       
  1227 /**
       
  1228  * Callback to convert URL match to HTML A element.
       
  1229  *
       
  1230  * This function was backported from 2.5.0 to 2.3.2. Regex callback for {@link
       
  1231  * make_clickable()}.
       
  1232  *
       
  1233  * @since 2.3.2
       
  1234  * @access private
       
  1235  *
       
  1236  * @param array $matches Single Regex Match.
       
  1237  * @return string HTML A element with URL address.
       
  1238  */
       
  1239 function _make_web_ftp_clickable_cb($matches) {
       
  1240 	$ret = '';
       
  1241 	$dest = $matches[2];
       
  1242 	$dest = 'http://' . $dest;
       
  1243 	$dest = esc_url($dest);
       
  1244 	if ( empty($dest) )
       
  1245 		return $matches[0];
       
  1246 	// removed trailing [,;:] from URL
       
  1247 	if ( in_array(substr($dest, -1), array('.', ',', ';', ':')) === true ) {
       
  1248 		$ret = substr($dest, -1);
       
  1249 		$dest = substr($dest, 0, strlen($dest)-1);
       
  1250 	}
       
  1251 	return $matches[1] . "<a href=\"$dest\" rel=\"nofollow\">$dest</a>" . $ret;
       
  1252 }
       
  1253 
       
  1254 /**
       
  1255  * Callback to convert email address match to HTML A element.
       
  1256  *
       
  1257  * This function was backported from 2.5.0 to 2.3.2. Regex callback for {@link
       
  1258  * make_clickable()}.
       
  1259  *
       
  1260  * @since 2.3.2
       
  1261  * @access private
       
  1262  *
       
  1263  * @param array $matches Single Regex Match.
       
  1264  * @return string HTML A element with email address.
       
  1265  */
       
  1266 function _make_email_clickable_cb($matches) {
       
  1267 	$email = $matches[2] . '@' . $matches[3];
       
  1268 	return $matches[1] . "<a href=\"mailto:$email\">$email</a>";
       
  1269 }
       
  1270 
       
  1271 /**
       
  1272  * Convert plaintext URI to HTML links.
       
  1273  *
       
  1274  * Converts URI, www and ftp, and email addresses. Finishes by fixing links
       
  1275  * within links.
       
  1276  *
       
  1277  * @since 0.71
       
  1278  *
       
  1279  * @param string $ret Content to convert URIs.
       
  1280  * @return string Content with converted URIs.
       
  1281  */
       
  1282 function make_clickable($ret) {
       
  1283 	$ret = ' ' . $ret;
       
  1284 	// in testing, using arrays here was found to be faster
       
  1285 	$ret = preg_replace_callback('#(?<=[\s>])(\()?([\w]+?://(?:[\w\\x80-\\xff\#$%&~/\-=?@\[\](+]|[.,;:](?![\s<])|(?(1)\)(?![\s<])|\)))+)#is', '_make_url_clickable_cb', $ret);
       
  1286 	$ret = preg_replace_callback('#([\s>])((www|ftp)\.[\w\\x80-\\xff\#$%&~/.\-;:=,?@\[\]+]+)#is', '_make_web_ftp_clickable_cb', $ret);
       
  1287 	$ret = preg_replace_callback('#([\s>])([.0-9a-z_+-]+)@(([0-9a-z-]+\.)+[0-9a-z]{2,})#i', '_make_email_clickable_cb', $ret);
       
  1288 	// this one is not in an array because we need it to run last, for cleanup of accidental links within links
       
  1289 	$ret = preg_replace("#(<a( [^>]+?>|>))<a [^>]+?>([^>]+?)</a></a>#i", "$1$3</a>", $ret);
       
  1290 	$ret = trim($ret);
       
  1291 	return $ret;
       
  1292 }
       
  1293 
       
  1294 /**
       
  1295  * Adds rel nofollow string to all HTML A elements in content.
       
  1296  *
       
  1297  * @since 1.5.0
       
  1298  *
       
  1299  * @param string $text Content that may contain HTML A elements.
       
  1300  * @return string Converted content.
       
  1301  */
       
  1302 function wp_rel_nofollow( $text ) {
       
  1303 	global $wpdb;
       
  1304 	// This is a pre save filter, so text is already escaped.
       
  1305 	$text = stripslashes($text);
       
  1306 	$text = preg_replace_callback('|<a (.+?)>|i', 'wp_rel_nofollow_callback', $text);
       
  1307 	$text = $wpdb->escape($text);
       
  1308 	return $text;
       
  1309 }
       
  1310 
       
  1311 /**
       
  1312  * Callback to used to add rel=nofollow string to HTML A element.
       
  1313  *
       
  1314  * Will remove already existing rel="nofollow" and rel='nofollow' from the
       
  1315  * string to prevent from invalidating (X)HTML.
       
  1316  *
       
  1317  * @since 2.3.0
       
  1318  *
       
  1319  * @param array $matches Single Match
       
  1320  * @return string HTML A Element with rel nofollow.
       
  1321  */
       
  1322 function wp_rel_nofollow_callback( $matches ) {
       
  1323 	$text = $matches[1];
       
  1324 	$text = str_replace(array(' rel="nofollow"', " rel='nofollow'"), '', $text);
       
  1325 	return "<a $text rel=\"nofollow\">";
       
  1326 }
       
  1327 
       
  1328 
       
  1329 /**
       
  1330  * Convert one smiley code to the icon graphic file equivalent.
       
  1331  *
       
  1332  * Looks up one smiley code in the $wpsmiliestrans global array and returns an
       
  1333  * <img> string for that smiley.
       
  1334  *
       
  1335  * @global array $wpsmiliestrans
       
  1336  * @since 2.8.0
       
  1337  *
       
  1338  * @param string $smiley Smiley code to convert to image.
       
  1339  * @return string Image string for smiley.
       
  1340  */
       
  1341 function translate_smiley($smiley) {
       
  1342 	global $wpsmiliestrans;
       
  1343 
       
  1344 	if (count($smiley) == 0) {
       
  1345 		return '';
       
  1346 	}
       
  1347 
       
  1348 	$siteurl = get_option( 'siteurl' );
       
  1349 
       
  1350 	$smiley = trim(reset($smiley));
       
  1351 	$img = $wpsmiliestrans[$smiley];
       
  1352 	$smiley_masked = esc_attr($smiley);
       
  1353 
       
  1354 	return " <img src='$siteurl/wp-includes/images/smilies/$img' alt='$smiley_masked' class='wp-smiley' /> ";
       
  1355 }
       
  1356 
       
  1357 
       
  1358 /**
       
  1359  * Convert text equivalent of smilies to images.
       
  1360  *
       
  1361  * Will only convert smilies if the option 'use_smilies' is true and the global
       
  1362  * used in the function isn't empty.
       
  1363  *
       
  1364  * @since 0.71
       
  1365  * @uses $wp_smiliessearch
       
  1366  *
       
  1367  * @param string $text Content to convert smilies from text.
       
  1368  * @return string Converted content with text smilies replaced with images.
       
  1369  */
       
  1370 function convert_smilies($text) {
       
  1371 	global $wp_smiliessearch;
       
  1372 	$output = '';
       
  1373 	if ( get_option('use_smilies') && !empty($wp_smiliessearch) ) {
       
  1374 		// HTML loop taken from texturize function, could possible be consolidated
       
  1375 		$textarr = preg_split("/(<.*>)/U", $text, -1, PREG_SPLIT_DELIM_CAPTURE); // capture the tags as well as in between
       
  1376 		$stop = count($textarr);// loop stuff
       
  1377 		for ($i = 0; $i < $stop; $i++) {
       
  1378 			$content = $textarr[$i];
       
  1379 			if ((strlen($content) > 0) && ('<' != $content{0})) { // If it's not a tag
       
  1380 				$content = preg_replace_callback($wp_smiliessearch, 'translate_smiley', $content);
       
  1381 			}
       
  1382 			$output .= $content;
       
  1383 		}
       
  1384 	} else {
       
  1385 		// return default text.
       
  1386 		$output = $text;
       
  1387 	}
       
  1388 	return $output;
       
  1389 }
       
  1390 
       
  1391 /**
       
  1392  * Verifies that an email is valid.
       
  1393  *
       
  1394  * Does not grok i18n domains. Not RFC compliant.
       
  1395  *
       
  1396  * @since 0.71
       
  1397  *
       
  1398  * @param string $email Email address to verify.
       
  1399  * @param boolean $check_dns Whether to check the DNS for the domain using checkdnsrr().
       
  1400  * @return string|bool Either false or the valid email address.
       
  1401  */
       
  1402 function is_email( $email, $check_dns = false ) {
       
  1403 	// Test for the minimum length the email can be
       
  1404 	if ( strlen( $email ) < 3 ) {
       
  1405 		return apply_filters( 'is_email', false, $email, 'email_too_short' );
       
  1406 	}
       
  1407 
       
  1408 	// Test for an @ character after the first position
       
  1409 	if ( strpos( $email, '@', 1 ) === false ) {
       
  1410 		return apply_filters( 'is_email', false, $email, 'email_no_at' );
       
  1411 	}
       
  1412 
       
  1413 	// Split out the local and domain parts
       
  1414 	list( $local, $domain ) = explode( '@', $email, 2 );
       
  1415 
       
  1416 	// LOCAL PART
       
  1417 	// Test for invalid characters
       
  1418 	if ( !preg_match( '/^[a-zA-Z0-9!#$%&\'*+\/=?^_`{|}~\.-]+$/', $local ) ) {
       
  1419 		return apply_filters( 'is_email', false, $email, 'local_invalid_chars' );
       
  1420 	}
       
  1421 
       
  1422 	// DOMAIN PART
       
  1423 	// Test for sequences of periods
       
  1424 	if ( preg_match( '/\.{2,}/', $domain ) ) {
       
  1425 		return apply_filters( 'is_email', false, $email, 'domain_period_sequence' );
       
  1426 	}
       
  1427 
       
  1428 	// Test for leading and trailing periods and whitespace
       
  1429 	if ( trim( $domain, " \t\n\r\0\x0B." ) !== $domain ) {
       
  1430 		return apply_filters( 'is_email', false, $email, 'domain_period_limits' );
       
  1431 	}
       
  1432 
       
  1433 	// Split the domain into subs
       
  1434 	$subs = explode( '.', $domain );
       
  1435 
       
  1436 	// Assume the domain will have at least two subs
       
  1437 	if ( 2 > count( $subs ) ) {
       
  1438 		return apply_filters( 'is_email', false, $email, 'domain_no_periods' );
       
  1439 	}
       
  1440 
       
  1441 	// Loop through each sub
       
  1442 	foreach ( $subs as $sub ) {
       
  1443 		// Test for leading and trailing hyphens and whitespace
       
  1444 		if ( trim( $sub, " \t\n\r\0\x0B-" ) !== $sub ) {
       
  1445 			return apply_filters( 'is_email', false, $email, 'sub_hyphen_limits' );
       
  1446 		}
       
  1447 
       
  1448 		// Test for invalid characters
       
  1449 		if ( !preg_match('/^[a-z0-9-]+$/i', $sub ) ) {
       
  1450 			return apply_filters( 'is_email', false, $email, 'sub_invalid_chars' );
       
  1451 		}
       
  1452 	}
       
  1453 
       
  1454 	// DNS
       
  1455 	// Check the domain has a valid MX and A resource record
       
  1456 	if ( $check_dns && function_exists( 'checkdnsrr' ) && !( checkdnsrr( $domain . '.', 'MX' ) || checkdnsrr( $domain . '.', 'A' ) ) ) {
       
  1457 		return apply_filters( 'is_email', false, $email, 'dns_no_rr' );
       
  1458 	}
       
  1459 
       
  1460 	// Congratulations your email made it!
       
  1461 	return apply_filters( 'is_email', $email, $email, null );
       
  1462 }
       
  1463 
       
  1464 /**
       
  1465  * Convert to ASCII from email subjects.
       
  1466  *
       
  1467  * @since 1.2.0
       
  1468  * @usedby wp_mail() handles charsets in email subjects
       
  1469  *
       
  1470  * @param string $string Subject line
       
  1471  * @return string Converted string to ASCII
       
  1472  */
       
  1473 function wp_iso_descrambler($string) {
       
  1474 	/* this may only work with iso-8859-1, I'm afraid */
       
  1475 	if (!preg_match('#\=\?(.+)\?Q\?(.+)\?\=#i', $string, $matches)) {
       
  1476 		return $string;
       
  1477 	} else {
       
  1478 		$subject = str_replace('_', ' ', $matches[2]);
       
  1479 		$subject = preg_replace_callback('#\=([0-9a-f]{2})#i', create_function('$match', 'return chr(hexdec(strtolower($match[1])));'), $subject);
       
  1480 		return $subject;
       
  1481 	}
       
  1482 }
       
  1483 
       
  1484 /**
       
  1485  * Returns a date in the GMT equivalent.
       
  1486  *
       
  1487  * Requires and returns a date in the Y-m-d H:i:s format. Simply subtracts the
       
  1488  * value of the 'gmt_offset' option. Return format can be overridden using the
       
  1489  * $format parameter
       
  1490  *
       
  1491  * @since 1.2.0
       
  1492  *
       
  1493  * @uses get_option() to retrieve the the value of 'gmt_offset'.
       
  1494  * @param string $string The date to be converted.
       
  1495  * @param string $format The format string for the returned date (default is Y-m-d H:i:s)
       
  1496  * @return string GMT version of the date provided.
       
  1497  */
       
  1498 function get_gmt_from_date($string, $format = 'Y-m-d H:i:s') {
       
  1499 	preg_match('#([0-9]{1,4})-([0-9]{1,2})-([0-9]{1,2}) ([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})#', $string, $matches);
       
  1500 	$string_time = gmmktime($matches[4], $matches[5], $matches[6], $matches[2], $matches[3], $matches[1]);
       
  1501 	$string_gmt = gmdate($format, $string_time - get_option('gmt_offset') * 3600);
       
  1502 	return $string_gmt;
       
  1503 }
       
  1504 
       
  1505 /**
       
  1506  * Converts a GMT date into the correct format for the blog.
       
  1507  *
       
  1508  * Requires and returns in the Y-m-d H:i:s format. Simply adds the value of
       
  1509  * gmt_offset.Return format can be overridden using the $format parameter
       
  1510  *
       
  1511  * @since 1.2.0
       
  1512  *
       
  1513  * @param string $string The date to be converted.
       
  1514  * @param string $format The format string for the returned date (default is Y-m-d H:i:s)
       
  1515  * @return string Formatted date relative to the GMT offset.
       
  1516  */
       
  1517 function get_date_from_gmt($string, $format = 'Y-m-d H:i:s') {
       
  1518 	preg_match('#([0-9]{1,4})-([0-9]{1,2})-([0-9]{1,2}) ([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})#', $string, $matches);
       
  1519 	$string_time = gmmktime($matches[4], $matches[5], $matches[6], $matches[2], $matches[3], $matches[1]);
       
  1520 	$string_localtime = gmdate($format, $string_time + get_option('gmt_offset')*3600);
       
  1521 	return $string_localtime;
       
  1522 }
       
  1523 
       
  1524 /**
       
  1525  * Computes an offset in seconds from an iso8601 timezone.
       
  1526  *
       
  1527  * @since 1.5.0
       
  1528  *
       
  1529  * @param string $timezone Either 'Z' for 0 offset or '±hhmm'.
       
  1530  * @return int|float The offset in seconds.
       
  1531  */
       
  1532 function iso8601_timezone_to_offset($timezone) {
       
  1533 	// $timezone is either 'Z' or '[+|-]hhmm'
       
  1534 	if ($timezone == 'Z') {
       
  1535 		$offset = 0;
       
  1536 	} else {
       
  1537 		$sign    = (substr($timezone, 0, 1) == '+') ? 1 : -1;
       
  1538 		$hours   = intval(substr($timezone, 1, 2));
       
  1539 		$minutes = intval(substr($timezone, 3, 4)) / 60;
       
  1540 		$offset  = $sign * 3600 * ($hours + $minutes);
       
  1541 	}
       
  1542 	return $offset;
       
  1543 }
       
  1544 
       
  1545 /**
       
  1546  * Converts an iso8601 date to MySQL DateTime format used by post_date[_gmt].
       
  1547  *
       
  1548  * @since 1.5.0
       
  1549  *
       
  1550  * @param string $date_string Date and time in ISO 8601 format {@link http://en.wikipedia.org/wiki/ISO_8601}.
       
  1551  * @param string $timezone Optional. If set to GMT returns the time minus gmt_offset. Default is 'user'.
       
  1552  * @return string The date and time in MySQL DateTime format - Y-m-d H:i:s.
       
  1553  */
       
  1554 function iso8601_to_datetime($date_string, $timezone = 'user') {
       
  1555 	$timezone = strtolower($timezone);
       
  1556 
       
  1557 	if ($timezone == 'gmt') {
       
  1558 
       
  1559 		preg_match('#([0-9]{4})([0-9]{2})([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})(Z|[\+|\-][0-9]{2,4}){0,1}#', $date_string, $date_bits);
       
  1560 
       
  1561 		if (!empty($date_bits[7])) { // we have a timezone, so let's compute an offset
       
  1562 			$offset = iso8601_timezone_to_offset($date_bits[7]);
       
  1563 		} else { // we don't have a timezone, so we assume user local timezone (not server's!)
       
  1564 			$offset = 3600 * get_option('gmt_offset');
       
  1565 		}
       
  1566 
       
  1567 		$timestamp = gmmktime($date_bits[4], $date_bits[5], $date_bits[6], $date_bits[2], $date_bits[3], $date_bits[1]);
       
  1568 		$timestamp -= $offset;
       
  1569 
       
  1570 		return gmdate('Y-m-d H:i:s', $timestamp);
       
  1571 
       
  1572 	} else if ($timezone == 'user') {
       
  1573 		return preg_replace('#([0-9]{4})([0-9]{2})([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})(Z|[\+|\-][0-9]{2,4}){0,1}#', '$1-$2-$3 $4:$5:$6', $date_string);
       
  1574 	}
       
  1575 }
       
  1576 
       
  1577 /**
       
  1578  * Adds a element attributes to open links in new windows.
       
  1579  *
       
  1580  * Comment text in popup windows should be filtered through this. Right now it's
       
  1581  * a moderately dumb function, ideally it would detect whether a target or rel
       
  1582  * attribute was already there and adjust its actions accordingly.
       
  1583  *
       
  1584  * @since 0.71
       
  1585  *
       
  1586  * @param string $text Content to replace links to open in a new window.
       
  1587  * @return string Content that has filtered links.
       
  1588  */
       
  1589 function popuplinks($text) {
       
  1590 	$text = preg_replace('/<a (.+?)>/i', "<a $1 target='_blank' rel='external'>", $text);
       
  1591 	return $text;
       
  1592 }
       
  1593 
       
  1594 /**
       
  1595  * Strips out all characters that are not allowable in an email.
       
  1596  *
       
  1597  * @since 1.5.0
       
  1598  *
       
  1599  * @param string $email Email address to filter.
       
  1600  * @return string Filtered email address.
       
  1601  */
       
  1602 function sanitize_email( $email ) {
       
  1603 	// Test for the minimum length the email can be
       
  1604 	if ( strlen( $email ) < 3 ) {
       
  1605 		return apply_filters( 'sanitize_email', '', $email, 'email_too_short' );
       
  1606 	}
       
  1607 
       
  1608 	// Test for an @ character after the first position
       
  1609 	if ( strpos( $email, '@', 1 ) === false ) {
       
  1610 		return apply_filters( 'sanitize_email', '', $email, 'email_no_at' );
       
  1611 	}
       
  1612 
       
  1613 	// Split out the local and domain parts
       
  1614 	list( $local, $domain ) = explode( '@', $email, 2 );
       
  1615 
       
  1616 	// LOCAL PART
       
  1617 	// Test for invalid characters
       
  1618 	$local = preg_replace( '/[^a-zA-Z0-9!#$%&\'*+\/=?^_`{|}~\.-]/', '', $local );
       
  1619 	if ( '' === $local ) {
       
  1620 		return apply_filters( 'sanitize_email', '', $email, 'local_invalid_chars' );
       
  1621 	}
       
  1622 
       
  1623 	// DOMAIN PART
       
  1624 	// Test for sequences of periods
       
  1625 	$domain = preg_replace( '/\.{2,}/', '', $domain );
       
  1626 	if ( '' === $domain ) {
       
  1627 		return apply_filters( 'sanitize_email', '', $email, 'domain_period_sequence' );
       
  1628 	}
       
  1629 
       
  1630 	// Test for leading and trailing periods and whitespace
       
  1631 	$domain = trim( $domain, " \t\n\r\0\x0B." );
       
  1632 	if ( '' === $domain ) {
       
  1633 		return apply_filters( 'sanitize_email', '', $email, 'domain_period_limits' );
       
  1634 	}
       
  1635 
       
  1636 	// Split the domain into subs
       
  1637 	$subs = explode( '.', $domain );
       
  1638 
       
  1639 	// Assume the domain will have at least two subs
       
  1640 	if ( 2 > count( $subs ) ) {
       
  1641 		return apply_filters( 'sanitize_email', '', $email, 'domain_no_periods' );
       
  1642 	}
       
  1643 
       
  1644 	// Create an array that will contain valid subs
       
  1645 	$new_subs = array();
       
  1646 
       
  1647 	// Loop through each sub
       
  1648 	foreach ( $subs as $sub ) {
       
  1649 		// Test for leading and trailing hyphens
       
  1650 		$sub = trim( $sub, " \t\n\r\0\x0B-" );
       
  1651 
       
  1652 		// Test for invalid characters
       
  1653 		$sub = preg_replace( '/^[^a-z0-9-]+$/i', '', $sub );
       
  1654 
       
  1655 		// If there's anything left, add it to the valid subs
       
  1656 		if ( '' !== $sub ) {
       
  1657 			$new_subs[] = $sub;
       
  1658 		}
       
  1659 	}
       
  1660 
       
  1661 	// If there aren't 2 or more valid subs
       
  1662 	if ( 2 > count( $new_subs ) ) {
       
  1663 		return apply_filters( 'sanitize_email', '', $email, 'domain_no_valid_subs' );
       
  1664 	}
       
  1665 
       
  1666 	// Join valid subs into the new domain
       
  1667 	$domain = join( '.', $new_subs );
       
  1668 
       
  1669 	// Put the email back together
       
  1670 	$email = $local . '@' . $domain;
       
  1671 
       
  1672 	// Congratulations your email made it!
       
  1673 	return apply_filters( 'sanitize_email', $email, $email, null );
       
  1674 }
       
  1675 
       
  1676 /**
       
  1677  * Determines the difference between two timestamps.
       
  1678  *
       
  1679  * The difference is returned in a human readable format such as "1 hour",
       
  1680  * "5 mins", "2 days".
       
  1681  *
       
  1682  * @since 1.5.0
       
  1683  *
       
  1684  * @param int $from Unix timestamp from which the difference begins.
       
  1685  * @param int $to Optional. Unix timestamp to end the time difference. Default becomes time() if not set.
       
  1686  * @return string Human readable time difference.
       
  1687  */
       
  1688 function human_time_diff( $from, $to = '' ) {
       
  1689 	if ( empty($to) )
       
  1690 		$to = time();
       
  1691 	$diff = (int) abs($to - $from);
       
  1692 	if ($diff <= 3600) {
       
  1693 		$mins = round($diff / 60);
       
  1694 		if ($mins <= 1) {
       
  1695 			$mins = 1;
       
  1696 		}
       
  1697 		$since = sprintf(_n('%s min', '%s mins', $mins), $mins);
       
  1698 	} else if (($diff <= 86400) && ($diff > 3600)) {
       
  1699 		$hours = round($diff / 3600);
       
  1700 		if ($hours <= 1) {
       
  1701 			$hours = 1;
       
  1702 		}
       
  1703 		$since = sprintf(_n('%s hour', '%s hours', $hours), $hours);
       
  1704 	} elseif ($diff >= 86400) {
       
  1705 		$days = round($diff / 86400);
       
  1706 		if ($days <= 1) {
       
  1707 			$days = 1;
       
  1708 		}
       
  1709 		$since = sprintf(_n('%s day', '%s days', $days), $days);
       
  1710 	}
       
  1711 	return $since;
       
  1712 }
       
  1713 
       
  1714 /**
       
  1715  * Generates an excerpt from the content, if needed.
       
  1716  *
       
  1717  * The excerpt word amount will be 55 words and if the amount is greater than
       
  1718  * that, then the string '[...]' will be appended to the excerpt. If the string
       
  1719  * is less than 55 words, then the content will be returned as is.
       
  1720  *
       
  1721  * @since 1.5.0
       
  1722  *
       
  1723  * @param string $text The exerpt. If set to empty an excerpt is generated.
       
  1724  * @return string The excerpt.
       
  1725  */
       
  1726 function wp_trim_excerpt($text) {
       
  1727 	$raw_excerpt = $text;
       
  1728 	if ( '' == $text ) {
       
  1729 		$text = get_the_content('');
       
  1730 
       
  1731 		$text = strip_shortcodes( $text );
       
  1732 
       
  1733 		$text = apply_filters('the_content', $text);
       
  1734 		$text = str_replace(']]>', ']]&gt;', $text);
       
  1735 		$text = strip_tags($text);
       
  1736 		$excerpt_length = apply_filters('excerpt_length', 55);
       
  1737 		$words = explode(' ', $text, $excerpt_length + 1);
       
  1738 		if (count($words) > $excerpt_length) {
       
  1739 			array_pop($words);
       
  1740 			array_push($words, '[...]');
       
  1741 			$text = implode(' ', $words);
       
  1742 		}
       
  1743 	}
       
  1744 	return apply_filters('wp_trim_excerpt', $text, $raw_excerpt);
       
  1745 }
       
  1746 
       
  1747 /**
       
  1748  * Converts named entities into numbered entities.
       
  1749  *
       
  1750  * @since 1.5.1
       
  1751  *
       
  1752  * @param string $text The text within which entities will be converted.
       
  1753  * @return string Text with converted entities.
       
  1754  */
       
  1755 function ent2ncr($text) {
       
  1756 	$to_ncr = array(
       
  1757 		'&quot;' => '&#34;',
       
  1758 		'&amp;' => '&#38;',
       
  1759 		'&frasl;' => '&#47;',
       
  1760 		'&lt;' => '&#60;',
       
  1761 		'&gt;' => '&#62;',
       
  1762 		'|' => '&#124;',
       
  1763 		'&nbsp;' => '&#160;',
       
  1764 		'&iexcl;' => '&#161;',
       
  1765 		'&cent;' => '&#162;',
       
  1766 		'&pound;' => '&#163;',
       
  1767 		'&curren;' => '&#164;',
       
  1768 		'&yen;' => '&#165;',
       
  1769 		'&brvbar;' => '&#166;',
       
  1770 		'&brkbar;' => '&#166;',
       
  1771 		'&sect;' => '&#167;',
       
  1772 		'&uml;' => '&#168;',
       
  1773 		'&die;' => '&#168;',
       
  1774 		'&copy;' => '&#169;',
       
  1775 		'&ordf;' => '&#170;',
       
  1776 		'&laquo;' => '&#171;',
       
  1777 		'&not;' => '&#172;',
       
  1778 		'&shy;' => '&#173;',
       
  1779 		'&reg;' => '&#174;',
       
  1780 		'&macr;' => '&#175;',
       
  1781 		'&hibar;' => '&#175;',
       
  1782 		'&deg;' => '&#176;',
       
  1783 		'&plusmn;' => '&#177;',
       
  1784 		'&sup2;' => '&#178;',
       
  1785 		'&sup3;' => '&#179;',
       
  1786 		'&acute;' => '&#180;',
       
  1787 		'&micro;' => '&#181;',
       
  1788 		'&para;' => '&#182;',
       
  1789 		'&middot;' => '&#183;',
       
  1790 		'&cedil;' => '&#184;',
       
  1791 		'&sup1;' => '&#185;',
       
  1792 		'&ordm;' => '&#186;',
       
  1793 		'&raquo;' => '&#187;',
       
  1794 		'&frac14;' => '&#188;',
       
  1795 		'&frac12;' => '&#189;',
       
  1796 		'&frac34;' => '&#190;',
       
  1797 		'&iquest;' => '&#191;',
       
  1798 		'&Agrave;' => '&#192;',
       
  1799 		'&Aacute;' => '&#193;',
       
  1800 		'&Acirc;' => '&#194;',
       
  1801 		'&Atilde;' => '&#195;',
       
  1802 		'&Auml;' => '&#196;',
       
  1803 		'&Aring;' => '&#197;',
       
  1804 		'&AElig;' => '&#198;',
       
  1805 		'&Ccedil;' => '&#199;',
       
  1806 		'&Egrave;' => '&#200;',
       
  1807 		'&Eacute;' => '&#201;',
       
  1808 		'&Ecirc;' => '&#202;',
       
  1809 		'&Euml;' => '&#203;',
       
  1810 		'&Igrave;' => '&#204;',
       
  1811 		'&Iacute;' => '&#205;',
       
  1812 		'&Icirc;' => '&#206;',
       
  1813 		'&Iuml;' => '&#207;',
       
  1814 		'&ETH;' => '&#208;',
       
  1815 		'&Ntilde;' => '&#209;',
       
  1816 		'&Ograve;' => '&#210;',
       
  1817 		'&Oacute;' => '&#211;',
       
  1818 		'&Ocirc;' => '&#212;',
       
  1819 		'&Otilde;' => '&#213;',
       
  1820 		'&Ouml;' => '&#214;',
       
  1821 		'&times;' => '&#215;',
       
  1822 		'&Oslash;' => '&#216;',
       
  1823 		'&Ugrave;' => '&#217;',
       
  1824 		'&Uacute;' => '&#218;',
       
  1825 		'&Ucirc;' => '&#219;',
       
  1826 		'&Uuml;' => '&#220;',
       
  1827 		'&Yacute;' => '&#221;',
       
  1828 		'&THORN;' => '&#222;',
       
  1829 		'&szlig;' => '&#223;',
       
  1830 		'&agrave;' => '&#224;',
       
  1831 		'&aacute;' => '&#225;',
       
  1832 		'&acirc;' => '&#226;',
       
  1833 		'&atilde;' => '&#227;',
       
  1834 		'&auml;' => '&#228;',
       
  1835 		'&aring;' => '&#229;',
       
  1836 		'&aelig;' => '&#230;',
       
  1837 		'&ccedil;' => '&#231;',
       
  1838 		'&egrave;' => '&#232;',
       
  1839 		'&eacute;' => '&#233;',
       
  1840 		'&ecirc;' => '&#234;',
       
  1841 		'&euml;' => '&#235;',
       
  1842 		'&igrave;' => '&#236;',
       
  1843 		'&iacute;' => '&#237;',
       
  1844 		'&icirc;' => '&#238;',
       
  1845 		'&iuml;' => '&#239;',
       
  1846 		'&eth;' => '&#240;',
       
  1847 		'&ntilde;' => '&#241;',
       
  1848 		'&ograve;' => '&#242;',
       
  1849 		'&oacute;' => '&#243;',
       
  1850 		'&ocirc;' => '&#244;',
       
  1851 		'&otilde;' => '&#245;',
       
  1852 		'&ouml;' => '&#246;',
       
  1853 		'&divide;' => '&#247;',
       
  1854 		'&oslash;' => '&#248;',
       
  1855 		'&ugrave;' => '&#249;',
       
  1856 		'&uacute;' => '&#250;',
       
  1857 		'&ucirc;' => '&#251;',
       
  1858 		'&uuml;' => '&#252;',
       
  1859 		'&yacute;' => '&#253;',
       
  1860 		'&thorn;' => '&#254;',
       
  1861 		'&yuml;' => '&#255;',
       
  1862 		'&OElig;' => '&#338;',
       
  1863 		'&oelig;' => '&#339;',
       
  1864 		'&Scaron;' => '&#352;',
       
  1865 		'&scaron;' => '&#353;',
       
  1866 		'&Yuml;' => '&#376;',
       
  1867 		'&fnof;' => '&#402;',
       
  1868 		'&circ;' => '&#710;',
       
  1869 		'&tilde;' => '&#732;',
       
  1870 		'&Alpha;' => '&#913;',
       
  1871 		'&Beta;' => '&#914;',
       
  1872 		'&Gamma;' => '&#915;',
       
  1873 		'&Delta;' => '&#916;',
       
  1874 		'&Epsilon;' => '&#917;',
       
  1875 		'&Zeta;' => '&#918;',
       
  1876 		'&Eta;' => '&#919;',
       
  1877 		'&Theta;' => '&#920;',
       
  1878 		'&Iota;' => '&#921;',
       
  1879 		'&Kappa;' => '&#922;',
       
  1880 		'&Lambda;' => '&#923;',
       
  1881 		'&Mu;' => '&#924;',
       
  1882 		'&Nu;' => '&#925;',
       
  1883 		'&Xi;' => '&#926;',
       
  1884 		'&Omicron;' => '&#927;',
       
  1885 		'&Pi;' => '&#928;',
       
  1886 		'&Rho;' => '&#929;',
       
  1887 		'&Sigma;' => '&#931;',
       
  1888 		'&Tau;' => '&#932;',
       
  1889 		'&Upsilon;' => '&#933;',
       
  1890 		'&Phi;' => '&#934;',
       
  1891 		'&Chi;' => '&#935;',
       
  1892 		'&Psi;' => '&#936;',
       
  1893 		'&Omega;' => '&#937;',
       
  1894 		'&alpha;' => '&#945;',
       
  1895 		'&beta;' => '&#946;',
       
  1896 		'&gamma;' => '&#947;',
       
  1897 		'&delta;' => '&#948;',
       
  1898 		'&epsilon;' => '&#949;',
       
  1899 		'&zeta;' => '&#950;',
       
  1900 		'&eta;' => '&#951;',
       
  1901 		'&theta;' => '&#952;',
       
  1902 		'&iota;' => '&#953;',
       
  1903 		'&kappa;' => '&#954;',
       
  1904 		'&lambda;' => '&#955;',
       
  1905 		'&mu;' => '&#956;',
       
  1906 		'&nu;' => '&#957;',
       
  1907 		'&xi;' => '&#958;',
       
  1908 		'&omicron;' => '&#959;',
       
  1909 		'&pi;' => '&#960;',
       
  1910 		'&rho;' => '&#961;',
       
  1911 		'&sigmaf;' => '&#962;',
       
  1912 		'&sigma;' => '&#963;',
       
  1913 		'&tau;' => '&#964;',
       
  1914 		'&upsilon;' => '&#965;',
       
  1915 		'&phi;' => '&#966;',
       
  1916 		'&chi;' => '&#967;',
       
  1917 		'&psi;' => '&#968;',
       
  1918 		'&omega;' => '&#969;',
       
  1919 		'&thetasym;' => '&#977;',
       
  1920 		'&upsih;' => '&#978;',
       
  1921 		'&piv;' => '&#982;',
       
  1922 		'&ensp;' => '&#8194;',
       
  1923 		'&emsp;' => '&#8195;',
       
  1924 		'&thinsp;' => '&#8201;',
       
  1925 		'&zwnj;' => '&#8204;',
       
  1926 		'&zwj;' => '&#8205;',
       
  1927 		'&lrm;' => '&#8206;',
       
  1928 		'&rlm;' => '&#8207;',
       
  1929 		'&ndash;' => '&#8211;',
       
  1930 		'&mdash;' => '&#8212;',
       
  1931 		'&lsquo;' => '&#8216;',
       
  1932 		'&rsquo;' => '&#8217;',
       
  1933 		'&sbquo;' => '&#8218;',
       
  1934 		'&ldquo;' => '&#8220;',
       
  1935 		'&rdquo;' => '&#8221;',
       
  1936 		'&bdquo;' => '&#8222;',
       
  1937 		'&dagger;' => '&#8224;',
       
  1938 		'&Dagger;' => '&#8225;',
       
  1939 		'&bull;' => '&#8226;',
       
  1940 		'&hellip;' => '&#8230;',
       
  1941 		'&permil;' => '&#8240;',
       
  1942 		'&prime;' => '&#8242;',
       
  1943 		'&Prime;' => '&#8243;',
       
  1944 		'&lsaquo;' => '&#8249;',
       
  1945 		'&rsaquo;' => '&#8250;',
       
  1946 		'&oline;' => '&#8254;',
       
  1947 		'&frasl;' => '&#8260;',
       
  1948 		'&euro;' => '&#8364;',
       
  1949 		'&image;' => '&#8465;',
       
  1950 		'&weierp;' => '&#8472;',
       
  1951 		'&real;' => '&#8476;',
       
  1952 		'&trade;' => '&#8482;',
       
  1953 		'&alefsym;' => '&#8501;',
       
  1954 		'&crarr;' => '&#8629;',
       
  1955 		'&lArr;' => '&#8656;',
       
  1956 		'&uArr;' => '&#8657;',
       
  1957 		'&rArr;' => '&#8658;',
       
  1958 		'&dArr;' => '&#8659;',
       
  1959 		'&hArr;' => '&#8660;',
       
  1960 		'&forall;' => '&#8704;',
       
  1961 		'&part;' => '&#8706;',
       
  1962 		'&exist;' => '&#8707;',
       
  1963 		'&empty;' => '&#8709;',
       
  1964 		'&nabla;' => '&#8711;',
       
  1965 		'&isin;' => '&#8712;',
       
  1966 		'&notin;' => '&#8713;',
       
  1967 		'&ni;' => '&#8715;',
       
  1968 		'&prod;' => '&#8719;',
       
  1969 		'&sum;' => '&#8721;',
       
  1970 		'&minus;' => '&#8722;',
       
  1971 		'&lowast;' => '&#8727;',
       
  1972 		'&radic;' => '&#8730;',
       
  1973 		'&prop;' => '&#8733;',
       
  1974 		'&infin;' => '&#8734;',
       
  1975 		'&ang;' => '&#8736;',
       
  1976 		'&and;' => '&#8743;',
       
  1977 		'&or;' => '&#8744;',
       
  1978 		'&cap;' => '&#8745;',
       
  1979 		'&cup;' => '&#8746;',
       
  1980 		'&int;' => '&#8747;',
       
  1981 		'&there4;' => '&#8756;',
       
  1982 		'&sim;' => '&#8764;',
       
  1983 		'&cong;' => '&#8773;',
       
  1984 		'&asymp;' => '&#8776;',
       
  1985 		'&ne;' => '&#8800;',
       
  1986 		'&equiv;' => '&#8801;',
       
  1987 		'&le;' => '&#8804;',
       
  1988 		'&ge;' => '&#8805;',
       
  1989 		'&sub;' => '&#8834;',
       
  1990 		'&sup;' => '&#8835;',
       
  1991 		'&nsub;' => '&#8836;',
       
  1992 		'&sube;' => '&#8838;',
       
  1993 		'&supe;' => '&#8839;',
       
  1994 		'&oplus;' => '&#8853;',
       
  1995 		'&otimes;' => '&#8855;',
       
  1996 		'&perp;' => '&#8869;',
       
  1997 		'&sdot;' => '&#8901;',
       
  1998 		'&lceil;' => '&#8968;',
       
  1999 		'&rceil;' => '&#8969;',
       
  2000 		'&lfloor;' => '&#8970;',
       
  2001 		'&rfloor;' => '&#8971;',
       
  2002 		'&lang;' => '&#9001;',
       
  2003 		'&rang;' => '&#9002;',
       
  2004 		'&larr;' => '&#8592;',
       
  2005 		'&uarr;' => '&#8593;',
       
  2006 		'&rarr;' => '&#8594;',
       
  2007 		'&darr;' => '&#8595;',
       
  2008 		'&harr;' => '&#8596;',
       
  2009 		'&loz;' => '&#9674;',
       
  2010 		'&spades;' => '&#9824;',
       
  2011 		'&clubs;' => '&#9827;',
       
  2012 		'&hearts;' => '&#9829;',
       
  2013 		'&diams;' => '&#9830;'
       
  2014 	);
       
  2015 
       
  2016 	return str_replace( array_keys($to_ncr), array_values($to_ncr), $text );
       
  2017 }
       
  2018 
       
  2019 /**
       
  2020  * Formats text for the rich text editor.
       
  2021  *
       
  2022  * The filter 'richedit_pre' is applied here. If $text is empty the filter will
       
  2023  * be applied to an empty string.
       
  2024  *
       
  2025  * @since 2.0.0
       
  2026  *
       
  2027  * @param string $text The text to be formatted.
       
  2028  * @return string The formatted text after filter is applied.
       
  2029  */
       
  2030 function wp_richedit_pre($text) {
       
  2031 	// Filtering a blank results in an annoying <br />\n
       
  2032 	if ( empty($text) ) return apply_filters('richedit_pre', '');
       
  2033 
       
  2034 	$output = convert_chars($text);
       
  2035 	$output = wpautop($output);
       
  2036 	$output = htmlspecialchars($output, ENT_NOQUOTES);
       
  2037 
       
  2038 	return apply_filters('richedit_pre', $output);
       
  2039 }
       
  2040 
       
  2041 /**
       
  2042  * Formats text for the HTML editor.
       
  2043  *
       
  2044  * Unless $output is empty it will pass through htmlspecialchars before the
       
  2045  * 'htmledit_pre' filter is applied.
       
  2046  *
       
  2047  * @since 2.5.0
       
  2048  *
       
  2049  * @param string $output The text to be formatted.
       
  2050  * @return string Formatted text after filter applied.
       
  2051  */
       
  2052 function wp_htmledit_pre($output) {
       
  2053 	if ( !empty($output) )
       
  2054 		$output = htmlspecialchars($output, ENT_NOQUOTES); // convert only < > &
       
  2055 
       
  2056 	return apply_filters('htmledit_pre', $output);
       
  2057 }
       
  2058 
       
  2059 /**
       
  2060  * Checks and cleans a URL.
       
  2061  *
       
  2062  * A number of characters are removed from the URL. If the URL is for displaying
       
  2063  * (the default behaviour) amperstands are also replaced. The 'esc_url' filter
       
  2064  * is applied to the returned cleaned URL.
       
  2065  *
       
  2066  * @since 1.2.0
       
  2067  * @uses wp_kses_bad_protocol() To only permit protocols in the URL set
       
  2068  *		via $protocols or the common ones set in the function.
       
  2069  *
       
  2070  * @param string $url The URL to be cleaned.
       
  2071  * @param array $protocols Optional. An array of acceptable protocols.
       
  2072  *		Defaults to 'http', 'https', 'ftp', 'ftps', 'mailto', 'news', 'irc', 'gopher', 'nntp', 'feed', 'telnet' if not set.
       
  2073  * @param string $context Optional. How the URL will be used. Default is 'display'.
       
  2074  * @return string The cleaned $url after the 'cleaned_url' filter is applied.
       
  2075  */
       
  2076 function clean_url( $url, $protocols = null, $context = 'display' ) {
       
  2077 	$original_url = $url;
       
  2078 
       
  2079 	if ('' == $url) return $url;
       
  2080 	$url = preg_replace('|[^a-z0-9-~+_.?#=!&;,/:%@$\|*\'()\\x80-\\xff]|i', '', $url);
       
  2081 	$strip = array('%0d', '%0a', '%0D', '%0A');
       
  2082 	$url = _deep_replace($strip, $url);
       
  2083 	$url = str_replace(';//', '://', $url);
       
  2084 	/* If the URL doesn't appear to contain a scheme, we
       
  2085 	 * presume it needs http:// appended (unless a relative
       
  2086 	 * link starting with / or a php file).
       
  2087 	 */
       
  2088 	if ( strpos($url, ':') === false &&
       
  2089 		substr( $url, 0, 1 ) != '/' && substr( $url, 0, 1 ) != '#' && !preg_match('/^[a-z0-9-]+?\.php/i', $url) )
       
  2090 		$url = 'http://' . $url;
       
  2091 
       
  2092 	// Replace ampersands and single quotes only when displaying.
       
  2093 	if ( 'display' == $context ) {
       
  2094 		$url = preg_replace('/&([^#])(?![a-z]{2,8};)/', '&#038;$1', $url);
       
  2095 		$url = str_replace( "'", '&#039;', $url );
       
  2096 	}
       
  2097 
       
  2098 	if ( !is_array($protocols) )
       
  2099 		$protocols = array('http', 'https', 'ftp', 'ftps', 'mailto', 'news', 'irc', 'gopher', 'nntp', 'feed', 'telnet');
       
  2100 	if ( wp_kses_bad_protocol( $url, $protocols ) != $url )
       
  2101 		return '';
       
  2102 
       
  2103 	return apply_filters('clean_url', $url, $original_url, $context);
       
  2104 }
       
  2105 
       
  2106 /**
       
  2107  * Perform a deep string replace operation to ensure the values in $search are no longer present
       
  2108  * 
       
  2109  * Repeats the replacement operation until it no longer replaces anything so as to remove "nested" values
       
  2110  * e.g. $subject = '%0%0%0DDD', $search ='%0D', $result ='' rather than the '%0%0DD' that
       
  2111  * str_replace would return
       
  2112  * 
       
  2113  * @since 2.8.1
       
  2114  * @access private
       
  2115  * 
       
  2116  * @param string|array $search
       
  2117  * @param string $subject
       
  2118  * @return string The processed string
       
  2119  */
       
  2120 function _deep_replace($search, $subject){
       
  2121 	$found = true;
       
  2122 	while($found) {
       
  2123 		$found = false;
       
  2124 		foreach( (array) $search as $val ) {
       
  2125 			while(strpos($subject, $val) !== false) {
       
  2126 				$found = true;
       
  2127 				$subject = str_replace($val, '', $subject);
       
  2128 			}
       
  2129 		}
       
  2130 	}
       
  2131 	
       
  2132 	return $subject;
       
  2133 }
       
  2134 
       
  2135 /**
       
  2136  * Escapes data for use in a MySQL query
       
  2137  *
       
  2138  * This is just a handy shortcut for $wpdb->escape(), for completeness' sake
       
  2139  *
       
  2140  * @since 2.8.0
       
  2141  * @param string $sql Unescaped SQL data
       
  2142  * @return string The cleaned $sql
       
  2143  */
       
  2144 function esc_sql( $sql ) {
       
  2145 	global $wpdb;
       
  2146 	return $wpdb->escape( $sql );
       
  2147 }
       
  2148 
       
  2149 
       
  2150 /**
       
  2151  * Checks and cleans a URL.
       
  2152  *
       
  2153  * A number of characters are removed from the URL. If the URL is for displaying
       
  2154  * (the default behaviour) amperstands are also replaced. The 'esc_url' filter
       
  2155  * is applied to the returned cleaned URL.
       
  2156  *
       
  2157  * @since 2.8.0
       
  2158  * @uses esc_url()
       
  2159  * @uses wp_kses_bad_protocol() To only permit protocols in the URL set
       
  2160  *		via $protocols or the common ones set in the function.
       
  2161  *
       
  2162  * @param string $url The URL to be cleaned.
       
  2163  * @param array $protocols Optional. An array of acceptable protocols.
       
  2164  *		Defaults to 'http', 'https', 'ftp', 'ftps', 'mailto', 'news', 'irc', 'gopher', 'nntp', 'feed', 'telnet' if not set.
       
  2165  * @return string The cleaned $url after the 'cleaned_url' filter is applied.
       
  2166  */
       
  2167 function esc_url( $url, $protocols = null ) {
       
  2168 	return clean_url( $url, $protocols, 'display' );
       
  2169 }
       
  2170 
       
  2171 /**
       
  2172  * Performs esc_url() for database usage.
       
  2173  *
       
  2174  * @see esc_url()
       
  2175  * @see esc_url()
       
  2176  *
       
  2177  * @since 2.8.0
       
  2178  *
       
  2179  * @param string $url The URL to be cleaned.
       
  2180  * @param array $protocols An array of acceptable protocols.
       
  2181  * @return string The cleaned URL.
       
  2182  */
       
  2183 function esc_url_raw( $url, $protocols = null ) {
       
  2184 	return clean_url( $url, $protocols, 'db' );
       
  2185 }
       
  2186 
       
  2187 /**
       
  2188  * Performs esc_url() for database or redirect usage.
       
  2189  *
       
  2190  * @see esc_url()
       
  2191  * @deprecated 2.8.0
       
  2192  *
       
  2193  * @since 2.3.1
       
  2194  *
       
  2195  * @param string $url The URL to be cleaned.
       
  2196  * @param array $protocols An array of acceptable protocols.
       
  2197  * @return string The cleaned URL.
       
  2198  */
       
  2199 function sanitize_url( $url, $protocols = null ) {
       
  2200 	return clean_url( $url, $protocols, 'db' );
       
  2201 }
       
  2202 
       
  2203 /**
       
  2204  * Convert entities, while preserving already-encoded entities.
       
  2205  *
       
  2206  * @link http://www.php.net/htmlentities Borrowed from the PHP Manual user notes.
       
  2207  *
       
  2208  * @since 1.2.2
       
  2209  *
       
  2210  * @param string $myHTML The text to be converted.
       
  2211  * @return string Converted text.
       
  2212  */
       
  2213 function htmlentities2($myHTML) {
       
  2214 	$translation_table = get_html_translation_table( HTML_ENTITIES, ENT_QUOTES );
       
  2215 	$translation_table[chr(38)] = '&';
       
  2216 	return preg_replace( "/&(?![A-Za-z]{0,4}\w{2,3};|#[0-9]{2,3};)/", "&amp;", strtr($myHTML, $translation_table) );
       
  2217 }
       
  2218 
       
  2219 /**
       
  2220  * Escape single quotes, specialchar double quotes, and fix line endings.
       
  2221  *
       
  2222  * The filter 'js_escape' is also applied here.
       
  2223  *
       
  2224  * @since 2.8.0
       
  2225  *
       
  2226  * @param string $text The text to be escaped.
       
  2227  * @return string Escaped text.
       
  2228  */
       
  2229 function esc_js( $text ) {
       
  2230 	$safe_text = wp_check_invalid_utf8( $text );
       
  2231 	$safe_text = _wp_specialchars( $safe_text, ENT_COMPAT );
       
  2232 	$safe_text = preg_replace( '/&#(x)?0*(?(1)27|39);?/i', "'", stripslashes( $safe_text ) );
       
  2233 	$safe_text = preg_replace( "/\r?\n/", "\\n", addslashes( $safe_text ) );
       
  2234 	return apply_filters( 'js_escape', $safe_text, $text );
       
  2235 }
       
  2236 
       
  2237 /**
       
  2238  * Escape single quotes, specialchar double quotes, and fix line endings.
       
  2239  *
       
  2240  * The filter 'js_escape' is also applied by esc_js()
       
  2241  *
       
  2242  * @since 2.0.4
       
  2243  *
       
  2244  * @deprecated 2.8.0
       
  2245  * @see esc_js()
       
  2246  *
       
  2247  * @param string $text The text to be escaped.
       
  2248  * @return string Escaped text.
       
  2249  */
       
  2250 function js_escape( $text ) {
       
  2251 	return esc_js( $text );
       
  2252 }
       
  2253 
       
  2254 /**
       
  2255  * Escaping for HTML blocks.
       
  2256  *
       
  2257  * @since 2.8.0
       
  2258  *
       
  2259  * @param string $text
       
  2260  * @return string
       
  2261  */
       
  2262 function esc_html( $text ) {
       
  2263 	$safe_text = wp_check_invalid_utf8( $text );
       
  2264 	$safe_text = _wp_specialchars( $safe_text, ENT_QUOTES );
       
  2265 	return apply_filters( 'esc_html', $safe_text, $text );
       
  2266 	return $text;
       
  2267 }
       
  2268 
       
  2269 /**
       
  2270  * Escaping for HTML blocks
       
  2271  * @deprecated 2.8.0
       
  2272  * @see esc_html()
       
  2273  */
       
  2274 function wp_specialchars( $string, $quote_style = ENT_NOQUOTES, $charset = false, $double_encode = false ) {
       
  2275 	if ( func_num_args() > 1 ) { // Maintain backwards compat for people passing additional args
       
  2276 		$args = func_get_args();
       
  2277 		return call_user_func_array( '_wp_specialchars', $args );
       
  2278 	} else {
       
  2279 		return esc_html( $string );
       
  2280 	}
       
  2281 }
       
  2282 
       
  2283 /**
       
  2284  * Escaping for HTML attributes.
       
  2285  *
       
  2286  * @since 2.8.0
       
  2287  *
       
  2288  * @param string $text
       
  2289  * @return string
       
  2290  */
       
  2291 function esc_attr( $text ) {
       
  2292 	$safe_text = wp_check_invalid_utf8( $text );
       
  2293 	$safe_text = _wp_specialchars( $safe_text, ENT_QUOTES );
       
  2294 	return apply_filters( 'attribute_escape', $safe_text, $text );
       
  2295 }
       
  2296 
       
  2297 /**
       
  2298  * Escaping for HTML attributes.
       
  2299  *
       
  2300  * @since 2.0.6
       
  2301  *
       
  2302  * @deprecated 2.8.0
       
  2303  * @see esc_attr()
       
  2304  *
       
  2305  * @param string $text
       
  2306  * @return string
       
  2307  */
       
  2308 function attribute_escape( $text ) {
       
  2309 	return esc_attr( $text );
       
  2310 }
       
  2311 
       
  2312 /**
       
  2313  * Escape a HTML tag name.
       
  2314  *
       
  2315  * @since 2.5.0
       
  2316  *
       
  2317  * @param string $tag_name
       
  2318  * @return string
       
  2319  */
       
  2320 function tag_escape($tag_name) {
       
  2321 	$safe_tag = strtolower( preg_replace('/[^a-zA-Z_:]/', '', $tag_name) );
       
  2322 	return apply_filters('tag_escape', $safe_tag, $tag_name);
       
  2323 }
       
  2324 
       
  2325 /**
       
  2326  * Escapes text for SQL LIKE special characters % and _.
       
  2327  *
       
  2328  * @since 2.5.0
       
  2329  *
       
  2330  * @param string $text The text to be escaped.
       
  2331  * @return string text, safe for inclusion in LIKE query.
       
  2332  */
       
  2333 function like_escape($text) {
       
  2334 	return str_replace(array("%", "_"), array("\\%", "\\_"), $text);
       
  2335 }
       
  2336 
       
  2337 /**
       
  2338  * Convert full URL paths to absolute paths.
       
  2339  *
       
  2340  * Removes the http or https protocols and the domain. Keeps the path '/' at the
       
  2341  * beginning, so it isn't a true relative link, but from the web root base.
       
  2342  *
       
  2343  * @since 2.1.0
       
  2344  *
       
  2345  * @param string $link Full URL path.
       
  2346  * @return string Absolute path.
       
  2347  */
       
  2348 function wp_make_link_relative( $link ) {
       
  2349 	return preg_replace( '|https?://[^/]+(/.*)|i', '$1', $link );
       
  2350 }
       
  2351 
       
  2352 /**
       
  2353  * Sanitises various option values based on the nature of the option.
       
  2354  *
       
  2355  * This is basically a switch statement which will pass $value through a number
       
  2356  * of functions depending on the $option.
       
  2357  *
       
  2358  * @since 2.0.5
       
  2359  *
       
  2360  * @param string $option The name of the option.
       
  2361  * @param string $value The unsanitised value.
       
  2362  * @return string Sanitized value.
       
  2363  */
       
  2364 function sanitize_option($option, $value) {
       
  2365 
       
  2366 	switch ($option) {
       
  2367 		case 'admin_email':
       
  2368 			$value = sanitize_email($value);
       
  2369 			break;
       
  2370 
       
  2371 		case 'thumbnail_size_w':
       
  2372 		case 'thumbnail_size_h':
       
  2373 		case 'medium_size_w':
       
  2374 		case 'medium_size_h':
       
  2375 		case 'large_size_w':
       
  2376 		case 'large_size_h':
       
  2377 		case 'default_post_edit_rows':
       
  2378 		case 'mailserver_port':
       
  2379 		case 'comment_max_links':
       
  2380 		case 'page_on_front':
       
  2381 		case 'rss_excerpt_length':
       
  2382 		case 'default_category':
       
  2383 		case 'default_email_category':
       
  2384 		case 'default_link_category':
       
  2385 		case 'close_comments_days_old':
       
  2386 		case 'comments_per_page':
       
  2387 		case 'thread_comments_depth':
       
  2388 			$value = abs((int) $value);
       
  2389 			break;
       
  2390 
       
  2391 		case 'posts_per_page':
       
  2392 		case 'posts_per_rss':
       
  2393 			$value = (int) $value;
       
  2394 			if ( empty($value) ) $value = 1;
       
  2395 			if ( $value < -1 ) $value = abs($value);
       
  2396 			break;
       
  2397 
       
  2398 		case 'default_ping_status':
       
  2399 		case 'default_comment_status':
       
  2400 			// Options that if not there have 0 value but need to be something like "closed"
       
  2401 			if ( $value == '0' || $value == '')
       
  2402 				$value = 'closed';
       
  2403 			break;
       
  2404 
       
  2405 		case 'blogdescription':
       
  2406 		case 'blogname':
       
  2407 			$value = addslashes($value);
       
  2408 			$value = wp_filter_post_kses( $value ); // calls stripslashes then addslashes
       
  2409 			$value = stripslashes($value);
       
  2410 			$value = esc_html( $value );
       
  2411 			break;
       
  2412 
       
  2413 		case 'blog_charset':
       
  2414 			$value = preg_replace('/[^a-zA-Z0-9_-]/', '', $value); // strips slashes
       
  2415 			break;
       
  2416 
       
  2417 		case 'date_format':
       
  2418 		case 'time_format':
       
  2419 		case 'mailserver_url':
       
  2420 		case 'mailserver_login':
       
  2421 		case 'mailserver_pass':
       
  2422 		case 'ping_sites':
       
  2423 		case 'upload_path':
       
  2424 			$value = strip_tags($value);
       
  2425 			$value = addslashes($value);
       
  2426 			$value = wp_filter_kses($value); // calls stripslashes then addslashes
       
  2427 			$value = stripslashes($value);
       
  2428 			break;
       
  2429 
       
  2430 		case 'gmt_offset':
       
  2431 			$value = preg_replace('/[^0-9:.-]/', '', $value); // strips slashes
       
  2432 			break;
       
  2433 
       
  2434 		case 'siteurl':
       
  2435 		case 'home':
       
  2436 			$value = stripslashes($value);
       
  2437 			$value = esc_url($value);
       
  2438 			break;
       
  2439 		default :
       
  2440 			$value = apply_filters("sanitize_option_{$option}", $value, $option);
       
  2441 			break;
       
  2442 	}
       
  2443 
       
  2444 	return $value;
       
  2445 }
       
  2446 
       
  2447 /**
       
  2448  * Parses a string into variables to be stored in an array.
       
  2449  *
       
  2450  * Uses {@link http://www.php.net/parse_str parse_str()} and stripslashes if
       
  2451  * {@link http://www.php.net/magic_quotes magic_quotes_gpc} is on.
       
  2452  *
       
  2453  * @since 2.2.1
       
  2454  * @uses apply_filters() for the 'wp_parse_str' filter.
       
  2455  *
       
  2456  * @param string $string The string to be parsed.
       
  2457  * @param array $array Variables will be stored in this array.
       
  2458  */
       
  2459 function wp_parse_str( $string, &$array ) {
       
  2460 	parse_str( $string, $array );
       
  2461 	if ( get_magic_quotes_gpc() )
       
  2462 		$array = stripslashes_deep( $array );
       
  2463 	$array = apply_filters( 'wp_parse_str', $array );
       
  2464 }
       
  2465 
       
  2466 /**
       
  2467  * Convert lone less than signs.
       
  2468  *
       
  2469  * KSES already converts lone greater than signs.
       
  2470  *
       
  2471  * @uses wp_pre_kses_less_than_callback in the callback function.
       
  2472  * @since 2.3.0
       
  2473  *
       
  2474  * @param string $text Text to be converted.
       
  2475  * @return string Converted text.
       
  2476  */
       
  2477 function wp_pre_kses_less_than( $text ) {
       
  2478 	return preg_replace_callback('%<[^>]*?((?=<)|>|$)%', 'wp_pre_kses_less_than_callback', $text);
       
  2479 }
       
  2480 
       
  2481 /**
       
  2482  * Callback function used by preg_replace.
       
  2483  *
       
  2484  * @uses esc_html to format the $matches text.
       
  2485  * @since 2.3.0
       
  2486  *
       
  2487  * @param array $matches Populated by matches to preg_replace.
       
  2488  * @return string The text returned after esc_html if needed.
       
  2489  */
       
  2490 function wp_pre_kses_less_than_callback( $matches ) {
       
  2491 	if ( false === strpos($matches[0], '>') )
       
  2492 		return esc_html($matches[0]);
       
  2493 	return $matches[0];
       
  2494 }
       
  2495 
       
  2496 /**
       
  2497  * WordPress implementation of PHP sprintf() with filters.
       
  2498  *
       
  2499  * @since 2.5.0
       
  2500  * @link http://www.php.net/sprintf
       
  2501  *
       
  2502  * @param string $pattern The string which formatted args are inserted.
       
  2503  * @param mixed $args,... Arguments to be formatted into the $pattern string.
       
  2504  * @return string The formatted string.
       
  2505  */
       
  2506 function wp_sprintf( $pattern ) {
       
  2507 	$args = func_get_args( );
       
  2508 	$len = strlen($pattern);
       
  2509 	$start = 0;
       
  2510 	$result = '';
       
  2511 	$arg_index = 0;
       
  2512 	while ( $len > $start ) {
       
  2513 		// Last character: append and break
       
  2514 		if ( strlen($pattern) - 1 == $start ) {
       
  2515 			$result .= substr($pattern, -1);
       
  2516 			break;
       
  2517 		}
       
  2518 
       
  2519 		// Literal %: append and continue
       
  2520 		if ( substr($pattern, $start, 2) == '%%' ) {
       
  2521 			$start += 2;
       
  2522 			$result .= '%';
       
  2523 			continue;
       
  2524 		}
       
  2525 
       
  2526 		// Get fragment before next %
       
  2527 		$end = strpos($pattern, '%', $start + 1);
       
  2528 		if ( false === $end )
       
  2529 			$end = $len;
       
  2530 		$fragment = substr($pattern, $start, $end - $start);
       
  2531 
       
  2532 		// Fragment has a specifier
       
  2533 		if ( $pattern{$start} == '%' ) {
       
  2534 			// Find numbered arguments or take the next one in order
       
  2535 			if ( preg_match('/^%(\d+)\$/', $fragment, $matches) ) {
       
  2536 				$arg = isset($args[$matches[1]]) ? $args[$matches[1]] : '';
       
  2537 				$fragment = str_replace("%{$matches[1]}$", '%', $fragment);
       
  2538 			} else {
       
  2539 				++$arg_index;
       
  2540 				$arg = isset($args[$arg_index]) ? $args[$arg_index] : '';
       
  2541 			}
       
  2542 
       
  2543 			// Apply filters OR sprintf
       
  2544 			$_fragment = apply_filters( 'wp_sprintf', $fragment, $arg );
       
  2545 			if ( $_fragment != $fragment )
       
  2546 				$fragment = $_fragment;
       
  2547 			else
       
  2548 				$fragment = sprintf($fragment, strval($arg) );
       
  2549 		}
       
  2550 
       
  2551 		// Append to result and move to next fragment
       
  2552 		$result .= $fragment;
       
  2553 		$start = $end;
       
  2554 	}
       
  2555 	return $result;
       
  2556 }
       
  2557 
       
  2558 /**
       
  2559  * Localize list items before the rest of the content.
       
  2560  *
       
  2561  * The '%l' must be at the first characters can then contain the rest of the
       
  2562  * content. The list items will have ', ', ', and', and ' and ' added depending
       
  2563  * on the amount of list items in the $args parameter.
       
  2564  *
       
  2565  * @since 2.5.0
       
  2566  *
       
  2567  * @param string $pattern Content containing '%l' at the beginning.
       
  2568  * @param array $args List items to prepend to the content and replace '%l'.
       
  2569  * @return string Localized list items and rest of the content.
       
  2570  */
       
  2571 function wp_sprintf_l($pattern, $args) {
       
  2572 	// Not a match
       
  2573 	if ( substr($pattern, 0, 2) != '%l' )
       
  2574 		return $pattern;
       
  2575 
       
  2576 	// Nothing to work with
       
  2577 	if ( empty($args) )
       
  2578 		return '';
       
  2579 
       
  2580 	// Translate and filter the delimiter set (avoid ampersands and entities here)
       
  2581 	$l = apply_filters('wp_sprintf_l', array(
       
  2582 		/* translators: used between list items, there is a space after the coma */
       
  2583 		'between'          => __(', '),
       
  2584 		/* translators: used between list items, there is a space after the and */
       
  2585 		'between_last_two' => __(', and '),
       
  2586 		/* translators: used between only two list items, there is a space after the and */
       
  2587 		'between_only_two' => __(' and '),
       
  2588 		));
       
  2589 
       
  2590 	$args = (array) $args;
       
  2591 	$result = array_shift($args);
       
  2592 	if ( count($args) == 1 )
       
  2593 		$result .= $l['between_only_two'] . array_shift($args);
       
  2594 	// Loop when more than two args
       
  2595 	$i = count($args);
       
  2596 	while ( $i ) {
       
  2597 		$arg = array_shift($args);
       
  2598 		$i--;
       
  2599 		if ( 0 == $i )
       
  2600 			$result .= $l['between_last_two'] . $arg;
       
  2601 		else
       
  2602 			$result .= $l['between'] . $arg;
       
  2603 	}
       
  2604 	return $result . substr($pattern, 2);
       
  2605 }
       
  2606 
       
  2607 /**
       
  2608  * Safely extracts not more than the first $count characters from html string.
       
  2609  *
       
  2610  * UTF-8, tags and entities safe prefix extraction. Entities inside will *NOT*
       
  2611  * be counted as one character. For example &amp; will be counted as 4, &lt; as
       
  2612  * 3, etc.
       
  2613  *
       
  2614  * @since 2.5.0
       
  2615  *
       
  2616  * @param integer $str String to get the excerpt from.
       
  2617  * @param integer $count Maximum number of characters to take.
       
  2618  * @return string The excerpt.
       
  2619  */
       
  2620 function wp_html_excerpt( $str, $count ) {
       
  2621 	$str = strip_tags( $str );
       
  2622 	$str = mb_substr( $str, 0, $count );
       
  2623 	// remove part of an entity at the end
       
  2624 	$str = preg_replace( '/&[^;\s]{0,6}$/', '', $str );
       
  2625 	return $str;
       
  2626 }
       
  2627 
       
  2628 /**
       
  2629  * Add a Base url to relative links in passed content.
       
  2630  *
       
  2631  * By default it supports the 'src' and 'href' attributes. However this can be
       
  2632  * changed via the 3rd param.
       
  2633  *
       
  2634  * @since 2.7.0
       
  2635  *
       
  2636  * @param string $content String to search for links in.
       
  2637  * @param string $base The base URL to prefix to links.
       
  2638  * @param array $attrs The attributes which should be processed.
       
  2639  * @return string The processed content.
       
  2640  */
       
  2641 function links_add_base_url( $content, $base, $attrs = array('src', 'href') ) {
       
  2642 	$attrs = implode('|', (array)$attrs);
       
  2643 	return preg_replace_callback("!($attrs)=(['\"])(.+?)\\2!i",
       
  2644 			create_function('$m', 'return _links_add_base($m, "' . $base . '");'),
       
  2645 			$content);
       
  2646 }
       
  2647 
       
  2648 /**
       
  2649  * Callback to add a base url to relative links in passed content.
       
  2650  *
       
  2651  * @since 2.7.0
       
  2652  * @access private
       
  2653  *
       
  2654  * @param string $m The matched link.
       
  2655  * @param string $base The base URL to prefix to links.
       
  2656  * @return string The processed link.
       
  2657  */
       
  2658 function _links_add_base($m, $base) {
       
  2659 	//1 = attribute name  2 = quotation mark  3 = URL
       
  2660 	return $m[1] . '=' . $m[2] .
       
  2661 		(strpos($m[3], 'http://') === false ?
       
  2662 			path_join($base, $m[3]) :
       
  2663 			$m[3])
       
  2664 		. $m[2];
       
  2665 }
       
  2666 
       
  2667 /**
       
  2668  * Adds a Target attribute to all links in passed content.
       
  2669  *
       
  2670  * This function by default only applies to <a> tags, however this can be
       
  2671  * modified by the 3rd param.
       
  2672  *
       
  2673  * <b>NOTE:</b> Any current target attributed will be striped and replaced.
       
  2674  *
       
  2675  * @since 2.7.0
       
  2676  *
       
  2677  * @param string $content String to search for links in.
       
  2678  * @param string $target The Target to add to the links.
       
  2679  * @param array $tags An array of tags to apply to.
       
  2680  * @return string The processed content.
       
  2681  */
       
  2682 function links_add_target( $content, $target = '_blank', $tags = array('a') ) {
       
  2683 	$tags = implode('|', (array)$tags);
       
  2684 	return preg_replace_callback("!<($tags)(.+?)>!i",
       
  2685 			create_function('$m', 'return _links_add_target($m, "' . $target . '");'),
       
  2686 			$content);
       
  2687 }
       
  2688 /**
       
  2689  * Callback to add a target attribute to all links in passed content.
       
  2690  *
       
  2691  * @since 2.7.0
       
  2692  * @access private
       
  2693  *
       
  2694  * @param string $m The matched link.
       
  2695  * @param string $target The Target to add to the links.
       
  2696  * @return string The processed link.
       
  2697  */
       
  2698 function _links_add_target( $m, $target ) {
       
  2699 	$tag = $m[1];
       
  2700 	$link = preg_replace('|(target=[\'"](.*?)[\'"])|i', '', $m[2]);
       
  2701 	return '<' . $tag . $link . ' target="' . $target . '">';
       
  2702 }
       
  2703 
       
  2704 // normalize EOL characters and strip duplicate whitespace
       
  2705 function normalize_whitespace( $str ) {
       
  2706 	$str  = trim($str);
       
  2707 	$str  = str_replace("\r", "\n", $str);
       
  2708 	$str  = preg_replace( array( '/\n+/', '/[ \t]+/' ), array( "\n", ' ' ), $str );
       
  2709 	return $str;
       
  2710 }
       
  2711 
       
  2712 ?>