]> scripts.mit.edu Git - autoinstalls/wordpress.git/blobdiff - wp-includes/formatting.php
Wordpress 2.3.2
[autoinstalls/wordpress.git] / wp-includes / formatting.php
similarity index 71%
rename from wp-includes/functions-formatting.php
rename to wp-includes/formatting.php
index 217e953ea3f43ef44e8f9ba8c10a0a3c2861b1f2..76f41d8523e595cace436a4dacd803dfbf2a67cc 100644 (file)
@@ -2,62 +2,60 @@
 
 function wptexturize($text) {
        global $wp_cockneyreplace;
+       $next = true;
        $output = '';
-       // Capture tags and everything inside them
-       $textarr = preg_split("/(<.*>)/Us", $text, -1, PREG_SPLIT_DELIM_CAPTURE);
-       $stop = count($textarr); $next = true; // loop stuff
-       for ($i = 0; $i < $stop; $i++) {
-               $curl = $textarr[$i];
+       $curl = '';
+       $textarr = preg_split('/(<.*>)/Us', $text, -1, PREG_SPLIT_DELIM_CAPTURE);
+       $stop = count($textarr);
+
+       // if a plugin has provided an autocorrect array, use it
+       if ( isset($wp_cockneyreplace) ) {
+               $cockney = array_keys($wp_cockneyreplace);
+               $cockneyreplace = array_values($wp_cockneyreplace);
+       } else {
+               $cockney = array("'tain't","'twere","'twas","'tis","'twill","'til","'bout","'nuff","'round","'cause");
+               $cockneyreplace = array("&#8217;tain&#8217;t","&#8217;twere","&#8217;twas","&#8217;tis","&#8217;twill","&#8217;til","&#8217;bout","&#8217;nuff","&#8217;round","&#8217;cause");
+       }
 
-               if (isset($curl{0}) && '<' != $curl{0} && $next) { // If it's not a tag
-                       $curl = str_replace('---', '&#8212;', $curl);
-                       $curl = str_replace(' -- ', ' &#8212; ', $curl);
-                       $curl = str_replace('--', '&#8211;', $curl);
-                       $curl = str_replace('xn&#8211;', 'xn--', $curl);
-                       $curl = str_replace('...', '&#8230;', $curl);
-                       $curl = str_replace('``', '&#8220;', $curl);
-
-                       // if a plugin has provided an autocorrect array, use it
-                       if ( isset($wp_cockneyreplace) ) {
-                               $cockney = array_keys($wp_cockneyreplace);
-                               $cockney_replace = array_values($wp_cockneyreplace);
-                       } else {
-                               $cockney = array("'tain't","'twere","'twas","'tis","'twill","'til","'bout","'nuff","'round","'cause");
-                               $cockneyreplace = array("&#8217;tain&#8217;t","&#8217;twere","&#8217;twas","&#8217;tis","&#8217;twill","&#8217;til","&#8217;bout","&#8217;nuff","&#8217;round","&#8217;cause");
-                       }
+       $static_characters = array_merge(array('---', ' -- ', '--', 'xn&#8211;', '...', '``', '\'s', '\'\'', ' (tm)'), $cockney);
+       $static_replacements = array_merge(array('&#8212;', ' &#8212; ', '&#8211;', 'xn--', '&#8230;', '&#8220;', '&#8217;s', '&#8221;', ' &#8482;'), $cockneyreplace);
+
+       $dynamic_characters = array('/\'(\d\d(?:&#8217;|\')?s)/', '/(\s|\A|")\'/', '/(\d+)"/', '/(\d+)\'/', '/(\S)\'([^\'\s])/', '/(\s|\A)"(?!\s)/', '/"(\s|\S|\Z)/', '/\'([\s.]|\Z)/', '/(\d+)x(\d+)/');
+       $dynamic_replacements = array('&#8217;$1','$1&#8216;', '$1&#8243;', '$1&#8242;', '$1&#8217;$2', '$1&#8220;$2', '&#8221;$1', '&#8217;$1', '$1&#215;$2');
+
+       for ( $i = 0; $i < $stop; $i++ ) {
+               $curl = $textarr[$i];
 
-                       $curl = str_replace($cockney, $cockneyreplace, $curl);
-
-                       $curl = preg_replace("/'s/", '&#8217;s', $curl);
-                       $curl = preg_replace("/'(\d\d(?:&#8217;|')?s)/", "&#8217;$1", $curl);
-                       $curl = preg_replace('/(\s|\A|")\'/', '$1&#8216;', $curl);
-                       $curl = preg_replace('/(\d+)"/', '$1&#8243;', $curl);
-                       $curl = preg_replace("/(\d+)'/", '$1&#8242;', $curl);
-                       $curl = preg_replace("/(\S)'([^'\s])/", "$1&#8217;$2", $curl);
-                       $curl = preg_replace('/(\s|\A)"(?!\s)/', '$1&#8220;$2', $curl);
-                       $curl = preg_replace('/"(\s|\S|\Z)/', '&#8221;$1', $curl);
-                       $curl = preg_replace("/'([\s.]|\Z)/", '&#8217;$1', $curl);
-                       $curl = preg_replace("/ \(tm\)/i", ' &#8482;', $curl);
-                       $curl = str_replace("''", '&#8221;', $curl);
-                       
-                       $curl = preg_replace('/(\d+)x(\d+)/', "$1&#215;$2", $curl);
-
-               } elseif (strstr($curl, '<code') || strstr($curl, '<pre') || strstr($curl, '<kbd' || strstr($curl, '<style') || strstr($curl, '<script'))) {
-                       // strstr is fast
+               if (isset($curl{0}) && '<' != $curl{0} && $next) { // If it's not a tag
+                       // static strings
+                       $curl = str_replace($static_characters, $static_replacements, $curl);
+                       // regular expressions
+                       $curl = preg_replace($dynamic_characters, $dynamic_replacements, $curl);
+               } elseif (strpos($curl, '<code') !== false || strpos($curl, '<pre') !== false || strpos($curl, '<kbd') !== false || strpos($curl, '<style') !== false || strpos($curl, '<script') !== false) {
                        $next = false;
                } else {
                        $next = true;
                }
+
                $curl = preg_replace('/&([^#])(?![a-zA-Z1-4]{1,8};)/', '&#038;$1', $curl);
                $output .= $curl;
        }
-       return $output;
+
+       return $output;
 }
 
-function clean_pre($text) {
+// Accepts matches array from preg_replace_callback in wpautop()
+// or a string
+function clean_pre($matches) {
+       if ( is_array($matches) )
+               $text = $matches[1] . $matches[2] . "</pre>";
+       else
+               $text = $matches;
+
        $text = str_replace('<br />', '', $text);
        $text = str_replace('<p>', "\n", $text);
        $text = str_replace('</p>', '', $text);
+
        return $text;
 }
 
@@ -65,24 +63,33 @@ function wpautop($pee, $br = 1) {
        $pee = $pee . "\n"; // just to make things a little easier, pad the end
        $pee = preg_replace('|<br />\s*<br />|', "\n\n", $pee);
        // Space things out a little
-       $pee = preg_replace('!(<(?:table|thead|tfoot|caption|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|select|form|blockquote|address|math|p|h[1-6])[^>]*>)!', "\n$1", $pee); 
-       $pee = preg_replace('!(</(?:table|thead|tfoot|caption|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|select|form|blockquote|address|math|p|h[1-6])>)!', "$1\n\n", $pee);
-       $pee = str_replace(array("\r\n", "\r"), "\n", $pee); // cross-platform newlines 
+       $allblocks = '(?:table|thead|tfoot|caption|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)';
+       $pee = preg_replace('!(<' . $allblocks . '[^>]*>)!', "\n$1", $pee);
+       $pee = preg_replace('!(</' . $allblocks . '>)!', "$1\n\n", $pee);
+       $pee = str_replace(array("\r\n", "\r"), "\n", $pee); // cross-platform newlines
        $pee = preg_replace("/\n\n+/", "\n\n", $pee); // take care of duplicates
-       $pee = preg_replace('/\n?(.+?)(?:\n\s*\n|\z)/s', "<p>$1</p>\n", $pee); // make paragraphs, including one at the end 
-       $pee = preg_replace('|<p>\s*?</p>|', '', $pee); // under certain strange conditions it could create a P of entirely whitespace 
-       $pee = preg_replace('!<p>\s*(</?(?:table|thead|tfoot|caption|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|hr|pre|select|form|blockquote|address|math|p|h[1-6])[^>]*>)\s*</p>!', "$1", $pee); // don't pee all over a tag
+       $pee = preg_replace('/\n?(.+?)(?:\n\s*\n|\z)/s', "<p>$1</p>\n", $pee); // make paragraphs, including one at the end
+       $pee = preg_replace('|<p>\s*?</p>|', '', $pee); // under certain strange conditions it could create a P of entirely whitespace
+       $pee = preg_replace('!<p>([^<]+)\s*?(</(?:div|address|form)[^>]*>)!', "<p>$1</p>$2", $pee);
+       $pee = preg_replace( '|<p>|', "$1<p>", $pee );
+       $pee = preg_replace('!<p>\s*(</?' . $allblocks . '[^>]*>)\s*</p>!', "$1", $pee); // don't pee all over a tag
        $pee = preg_replace("|<p>(<li.+?)</p>|", "$1", $pee); // problem with nested lists
        $pee = preg_replace('|<p><blockquote([^>]*)>|i', "<blockquote$1><p>", $pee);
        $pee = str_replace('</blockquote></p>', '</p></blockquote>', $pee);
-       $pee = preg_replace('!<p>\s*(</?(?:table|thead|tfoot|caption|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|hr|pre|select|form|blockquote|address|math|p|h[1-6])[^>]*>)!', "$1", $pee);
-       $pee = preg_replace('!(</?(?:table|thead|tfoot|caption|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|select|form|blockquote|address|math|p|h[1-6])[^>]*>)\s*</p>!', "$1", $pee); 
-       if ($br) $pee = preg_replace('|(?<!<br />)\s*\n|', "<br />\n", $pee); // optionally make line breaks
-       $pee = preg_replace('!(</?(?:table|thead|tfoot|caption|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|select|form|blockquote|address|math|p|h[1-6])[^>]*>)\s*<br />!', "$1", $pee);
+       $pee = preg_replace('!<p>\s*(</?' . $allblocks . '[^>]*>)!', "$1", $pee);
+       $pee = preg_replace('!(</?' . $allblocks . '[^>]*>)\s*</p>!', "$1", $pee);
+       if ($br) {
+               $pee = preg_replace('/<(script|style).*?<\/\\1>/se', 'str_replace("\n", "<WPPreserveNewline />", "\\0")', $pee);
+               $pee = preg_replace('|(?<!<br />)\s*\n|', "<br />\n", $pee); // optionally make line breaks
+               $pee = str_replace('<WPPreserveNewline />', "\n", $pee);
+       }
+       $pee = preg_replace('!(</?' . $allblocks . '[^>]*>)\s*<br />!', "$1", $pee);
        $pee = preg_replace('!<br />(\s*</?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)[^>]*>)!', '$1', $pee);
-       $pee = preg_replace('!(<pre.*?>)(.*?)</pre>!ise', " stripslashes('$1') .  stripslashes(clean_pre('$2'))  . '</pre>' ", $pee);
-       
-       return $pee; 
+       if (strpos($pee, '<pre') !== false)
+               $pee = preg_replace_callback('!(<pre.*?>)(.*?)</pre>!is', 'clean_pre', $pee );
+       $pee = preg_replace( "|\n</p>$|", '</p>', $pee );
+
+       return $pee;
 }
 
 
@@ -105,7 +112,9 @@ function seems_utf8($Str) { # by bmorel at ssi dot fr
 
 function wp_specialchars( $text, $quotes = 0 ) {
        // Like htmlspecialchars except don't double-encode HTML entities
-       $text = preg_replace('/&([^#])(?![a-z1-4]{1,8};)/', '&#038;$1', $text);
+       $text = str_replace('&&', '&#038;&', $text);
+       $text = str_replace('&&', '&#038;&', $text);
+       $text = preg_replace('/&(?:$|([^#])(?![a-z1-4]{1,8};))/', '&#038;$1', $text);
        $text = str_replace('<', '&lt;', $text);
        $text = str_replace('>', '&gt;', $text);
        if ( 'double' === $quotes ) {
@@ -119,36 +128,40 @@ function wp_specialchars( $text, $quotes = 0 ) {
        return $text;
 }
 
-function utf8_uri_encode( $utf8_string ) {
-  $unicode = '';        
-  $values = array();
-  $num_octets = 1;
-        
-  for ($i = 0; $i < strlen( $utf8_string ); $i++ ) {
-
-    $value = ord( $utf8_string[ $i ] );
-            
-    if ( $value < 128 ) {
-      $unicode .= chr($value);
-    } else {
-      if ( count( $values ) == 0 ) $num_octets = ( $value < 224 ) ? 2 : 3;
-                
-      $values[] = $value;
-      
-      if ( count( $values ) == $num_octets ) {
-       if ($num_octets == 3) {
-         $unicode .= '%' . dechex($values[0]) . '%' . dechex($values[1]) . '%' . dechex($values[2]);
-       } else {
-         $unicode .= '%' . dechex($values[0]) . '%' . dechex($values[1]);
-       }
-
+function utf8_uri_encode( $utf8_string, $length = 0 ) {
+       $unicode = '';
        $values = array();
        $num_octets = 1;
-      }
-    }
-  }
 
-  return $unicode;    
+       for ($i = 0; $i < strlen( $utf8_string ); $i++ ) {
+
+               $value = ord( $utf8_string[ $i ] );
+
+               if ( $value < 128 ) {
+                       if ( $length && ( strlen($unicode) + 1 > $length ) )
+                               break;
+                       $unicode .= chr($value);
+               } else {
+                       if ( count( $values ) == 0 ) $num_octets = ( $value < 224 ) ? 2 : 3;
+
+                       $values[] = $value;
+
+                       if ( $length && ( (strlen($unicode) + ($num_octets * 3)) > $length ) )
+                               break;
+                       if ( count( $values ) == $num_octets ) {
+                               if ($num_octets == 3) {
+                                       $unicode .= '%' . dechex($values[0]) . '%' . dechex($values[1]) . '%' . dechex($values[2]);
+                               } else {
+                                       $unicode .= '%' . dechex($values[0]) . '%' . dechex($values[1]);
+                               }
+
+                               $values = array();
+                               $num_octets = 1;
+                       }
+               }
+       }
+
+       return $unicode;
 }
 
 function remove_accents($string) {
@@ -281,6 +294,17 @@ function remove_accents($string) {
        return $string;
 }
 
+function sanitize_file_name( $name ) { // Like sanitize_title, but with periods
+       $name = strtolower( $name );
+       $name = preg_replace('/&.+?;/', '', $name); // kill entities
+       $name = str_replace( '_', '-', $name );
+       $name = preg_replace('/[^a-z0-9\s-.]/', '', $name);
+       $name = preg_replace('/\s+/', '-', $name);
+       $name = preg_replace('|-+|', '-', $name);
+       $name = trim($name, '-');
+       return $name;
+}
+
 function sanitize_user( $username, $strict = false ) {
        $raw_username = $username;
        $username = strip_tags($username);
@@ -320,7 +344,7 @@ function sanitize_title_with_dashes($title) {
                if (function_exists('mb_strtolower')) {
                        $title = mb_strtolower($title, 'UTF-8');
                }
-               $title = utf8_uri_encode($title);
+               $title = utf8_uri_encode($title, 200);
        }
 
        $title = strtolower($title);
@@ -333,7 +357,7 @@ function sanitize_title_with_dashes($title) {
        return $title;
 }
 
-function convert_chars($content, $flag = 'obsolete') { 
+function convert_chars($content, $flag = 'obsolete') {
        // Translation of invalid Unicode references range to valid range
        $wp_htmltranswinuni = array(
        '&#128;' => '&#8364;', // the Euro sign
@@ -390,15 +414,21 @@ function convert_chars($content, $flag = 'obsolete') {
 function funky_javascript_fix($text) {
        // Fixes for browsers' javascript bugs
        global $is_macIE, $is_winIE;
-       
+
        if ( $is_winIE || $is_macIE )
                $text =  preg_replace("/\%u([0-9A-F]{4,4})/e",  "'&#'.base_convert('\\1',16,10).';'", $text);
-       
+
        return $text;
 }
 
+function balanceTags( $text, $force = false ) {
+       if ( !$force && get_option('use_balanceTags') == 0 )
+               return $text;
+       return force_balance_tags( $text );
+}
+
 /*
balanceTags
force_balance_tags
 
  Balances Tags of string using a modified stack.
 
@@ -417,12 +447,10 @@ function funky_javascript_fix($text) {
             Added Cleaning Hooks
        1.0  First Version
 */
-function balanceTags($text, $force = false) {
-
-       if ( !$force && get_option('use_balanceTags') == 0 )
-               return $text;
-
+function force_balance_tags( $text ) {
        $tagstack = array(); $stacksize = 0; $tagqueue = ''; $newtext = '';
+       $single_tags = array('br', 'hr', 'img', 'input'); //Known single-entity/self-closing tags
+       $nestable_tags = array('blockquote', 'div', 'span'); //Tags that can be immediately nested within themselves
 
        # WP bug fix for comments - in case you REALLY meant to type '< !--'
        $text = str_replace('< !--', '<    !--', $text);
@@ -473,11 +501,11 @@ function balanceTags($text, $force = false) {
                        if((substr($regex[2],-1) == '/') || ($tag == '')) {
                        }
                        // ElseIf it's a known single-entity tag but it doesn't close itself, do so
-                       elseif ($tag == 'br' || $tag == 'img' || $tag == 'hr' || $tag == 'input') {
+                       elseif ( in_array($tag, $single_tags) ) {
                                $regex[2] .= '/';
                        } else {        // Push the tag onto the stack
                                // If the top of the stack is the same as the tag we want to push, close previous tag
-                               if (($stacksize > 0) && ($tag != 'div') && ($tagstack[$stacksize - 1] == $tag)) {
+                               if (($stacksize > 0) && !in_array($tag, $nestable_tags) && ($tagstack[$stacksize - 1] == $tag)) {
                                        $tagqueue = '</' . array_pop ($tagstack) . '>';
                                        $stacksize--;
                                }
@@ -518,10 +546,6 @@ function balanceTags($text, $force = false) {
        return $newtext;
 }
 
-function force_balance_tags($text) {
-       return balanceTags($text, true);
-}
-
 function format_to_edit($content, $richedit = false) {
        $content = apply_filters('format_to_edit', $content);
        if (! $richedit )
@@ -547,10 +571,11 @@ function backslashit($string) {
 }
 
 function trailingslashit($string) {
-    if ( '/' != substr($string, -1)) {
-        $string .= '/';
-    }
-    return $string;
+       return untrailingslashit($string) . '/';
+}
+
+function untrailingslashit($string) {
+       return rtrim($string, '/');
 }
 
 function addslashes_gpc($gpc) {
@@ -564,13 +589,20 @@ function addslashes_gpc($gpc) {
 }
 
 
-function stripslashes_deep($value)
-{
-   $value = is_array($value) ?
-               array_map('stripslashes_deep', $value) :
-               stripslashes($value);
+function stripslashes_deep($value) {
+        $value = is_array($value) ?
+                array_map('stripslashes_deep', $value) :
+                stripslashes($value);
 
-   return $value;
+        return $value;
+}
+
+function urlencode_deep($value) {
+        $value = is_array($value) ?
+                array_map('urlencode_deep', $value) :
+                urlencode($value);
+
+        return $value;
 }
 
 function antispambot($emailaddy, $mailto=0) {
@@ -590,18 +622,35 @@ function antispambot($emailaddy, $mailto=0) {
        return $emailNOSPAMaddy;
 }
 
+function _make_url_clickable_cb($matches) {
+       $url = $matches[2];
+       $url = clean_url($url);
+       if ( empty($url) )
+               return $matches[0];
+       return $matches[1] . "<a href=\"$url\" rel=\"nofollow\">$url</a>";
+}
+
+function _make_web_ftp_clickable_cb($matches) {
+       $dest = $matches[2];
+       $dest = 'http://' . $dest;
+       $dest = clean_url($dest);
+       if ( empty($dest) )
+               return $matches[0];
+
+       return $matches[1] . "<a href=\"$dest\" rel=\"nofollow\">$dest</a>";
+}
+
+function _make_email_clickable_cb($matches) {
+       $email = $matches[2] . '@' . $matches[3];
+       return $matches[1] . "<a href=\"mailto:$email\">$email</a>";
+}
+
 function make_clickable($ret) {
        $ret = ' ' . $ret;
        // in testing, using arrays here was found to be faster
-       $ret = preg_replace(
-               array(
-                       '#([\s>])([\w]+?://[\w\#$%&~/.\-;:=,?@\[\]+]*)#is',
-                       '#([\s>])((www|ftp)\.[\w\#$%&~/.\-;:=,?@\[\]+]*)#is',
-                       '#([\s>])([a-z0-9\-_.]+)@([^,< \n\r]+)#i'),
-               array(
-                       '$1<a href="$2" rel="nofollow">$2</a>',
-                       '$1<a href="http://$2" rel="nofollow">$2</a>',
-                       '$1<a href="mailto:$2@$3">$2@$3</a>'),$ret);
+       $ret = preg_replace_callback('#([\s>])([\w]+?://[\w\#$%&~/.\-;:=,?@\[\]+]*)#is', '_make_url_clickable_cb', $ret);
+       $ret = preg_replace_callback('#([\s>])((www|ftp)\.[\w\#$%&~/.\-;:=,?@\[\]+]*)#is', '_make_web_ftp_clickable_cb', $ret);
+       $ret = preg_replace_callback('#([\s>])([.0-9a-z_+-]+)@(([0-9a-z-]+\.)+[0-9a-z]{2,})#i', '_make_email_clickable_cb', $ret);
        // this one is not in an array because we need it to run last, for cleanup of accidental links within links
        $ret = preg_replace("#(<a( [^>]+?>|>))<a [^>]+?>([^>]+?)</a></a>#i", "$1$3</a>", $ret);
        $ret = trim($ret);
@@ -609,21 +658,31 @@ function make_clickable($ret) {
 }
 
 function wp_rel_nofollow( $text ) {
-       $text = preg_replace('|<a (.+?)>|ie', "'<a ' . str_replace(' rel=\"nofollow\"','',stripslashes('$1')) . ' rel=\"nofollow\">'", $text);
+       global $wpdb;
+       // This is a pre save filter, so text is already escaped.
+       $text = stripslashes($text);
+       $text = preg_replace_callback('|<a (.+?)>|i', 'wp_rel_nofollow_callback', $text);
+       $text = $wpdb->escape($text);
        return $text;
 }
 
+function wp_rel_nofollow_callback( $matches ) {
+       $text = $matches[1];
+       $text = str_replace(array(' rel="nofollow"', " rel='nofollow'"), '', $text);
+       return "<a $text rel=\"nofollow\">";
+}
+
 function convert_smilies($text) {
        global $wp_smiliessearch, $wp_smiliesreplace;
     $output = '';
-       if (get_settings('use_smilies')) {
+       if (get_option('use_smilies')) {
                // HTML loop taken from texturize function, could possible be consolidated
                $textarr = preg_split("/(<.*>)/U", $text, -1, PREG_SPLIT_DELIM_CAPTURE); // capture the tags as well as in between
                $stop = count($textarr);// loop stuff
                for ($i = 0; $i < $stop; $i++) {
                        $content = $textarr[$i];
                        if ((strlen($content) > 0) && ('<' != $content{0})) { // If it's not a tag
-                               $content = str_replace($wp_smiliessearch, $wp_smiliesreplace, $content);
+                               $content = preg_replace($wp_smiliessearch, $wp_smiliesreplace, $content);
                        }
                        $output .= $content;
                }
@@ -637,7 +696,7 @@ function convert_smilies($text) {
 
 function is_email($user_email) {
        $chars = "/^([a-z0-9+_]|\\-|\\.)+@(([a-z0-9_]|\\-)+\\.)+[a-z]{2,6}\$/i";
-       if(strstr($user_email, '@') && strstr($user_email, '.')) {
+       if (strpos($user_email, '@') !== false && strpos($user_email, '.') !== false) {
                if (preg_match($chars, $user_email)) {
                        return true;
                } else {
@@ -666,7 +725,7 @@ function get_gmt_from_date($string) {
   // note: this only substracts $time_difference from the given date
   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);
   $string_time = gmmktime($matches[4], $matches[5], $matches[6], $matches[2], $matches[3], $matches[1]);
-  $string_gmt = gmdate('Y-m-d H:i:s', $string_time - get_settings('gmt_offset') * 3600);
+  $string_gmt = gmdate('Y-m-d H:i:s', $string_time - get_option('gmt_offset') * 3600);
   return $string_gmt;
 }
 
@@ -675,7 +734,7 @@ function get_date_from_gmt($string) {
   // note: this only adds $time_difference to the given date
   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);
   $string_time = gmmktime($matches[4], $matches[5], $matches[6], $matches[2], $matches[3], $matches[1]);
-  $string_localtime = gmdate('Y-m-d H:i:s', $string_time + get_settings('gmt_offset')*3600);
+  $string_localtime = gmdate('Y-m-d H:i:s', $string_time + get_option('gmt_offset')*3600);
   return $string_localtime;
 }
 
@@ -700,7 +759,7 @@ function iso8601_to_datetime($date_string, $timezone = USER) {
     if (!empty($date_bits[7])) { // we have a timezone, so let's compute an offset
       $offset = iso8601_timezone_to_offset($date_bits[7]);
     } else { // we don't have a timezone, so we assume user local timezone (not server's!)
-      $offset = 3600 * get_settings('gmt_offset');
+      $offset = 3600 * get_option('gmt_offset');
     }
     $timestamp = gmmktime($date_bits[4], $date_bits[5], $date_bits[6], $date_bits[2], $date_bits[3], $date_bits[1]);
     $timestamp -= $offset;
@@ -722,28 +781,28 @@ function sanitize_email($email) {
        return preg_replace('/[^a-z0-9+_.@-]/i', '', $email);
 }
 
-function human_time_diff( $from, $to = '' ) {     
+function human_time_diff( $from, $to = '' ) {
        if ( empty($to) )
                $to = time();
        $diff = (int) abs($to - $from);
        if ($diff <= 3600) {
                $mins = round($diff / 60);
-               if ($mins <= 1)
-                       $since = __('1 min');
-               else
-                       $since = sprintf( __('%s mins'), $mins);
+               if ($mins <= 1) {
+                       $mins = 1;
+               }
+               $since = sprintf(__ngettext('%s min', '%s mins', $mins), $mins);
        } else if (($diff <= 86400) && ($diff > 3600)) {
                $hours = round($diff / 3600);
-               if ($hours <= 1)
-                       $since = __('1 hour');
-               else 
-                       $since = sprintf( __('%s hours'), $hours );
+               if ($hours <= 1) {
+                       $hour = 1;
+               }
+               $since = sprintf(__ngettext('%s hour', '%s hours', $hours), $hours);
        } elseif ($diff >= 86400) {
                $days = round($diff / 86400);
-               if ($days <= 1)
-                       $since = __('1 day');
-               else
-                       $since = sprintf( __('%s days'), $days );
+               if ($days <= 1) {
+                       $days = 1;
+               }
+               $since = sprintf(__ngettext('%s day', '%s days', $days), $days);
        }
        return $since;
 }
@@ -751,7 +810,7 @@ function human_time_diff( $from, $to = '' ) {
 function wp_trim_excerpt($text) { // Fakes an excerpt if needed
        global $post;
        if ( '' == $text ) {
-               $text = $post->post_content;
+               $text = get_the_content('');
                $text = apply_filters('the_content', $text);
                $text = str_replace(']]>', ']]&gt;', $text);
                $text = strip_tags($text);
@@ -1045,23 +1104,44 @@ function wp_richedit_pre($text) {
        return apply_filters('richedit_pre', $output);
 }
 
-function clean_url( $url, $protocols = null ) {
+function clean_url( $url, $protocols = null, $context = 'display' ) {
+       $original_url = $url;
+
        if ('' == $url) return $url;
-       $url = preg_replace('|[^a-z0-9-~+_.?#=!&;,/:%]|i', '', $url);
+       $url = preg_replace('|[^a-z0-9-~+_.?#=!&;,/:%@]|i', '', $url);
        $strip = array('%0d', '%0a');
        $url = str_replace($strip, '', $url);
        $url = str_replace(';//', '://', $url);
-       // Append http unless a relative link starting with / or a php file.
-       if ( strpos($url, '://') === false &&
-               substr( $url, 0, 1 ) != '/' && !preg_match('/^[a-z0-9]+?\.php/i', $url) )
+       /* If the URL doesn't appear to contain a scheme, we
+        * presume it needs http:// appended (unless a relative
+        * link starting with / or a php file).
+       */
+       if ( strpos($url, ':') === false &&
+               substr( $url, 0, 1 ) != '/' && !preg_match('/^[a-z0-9-]+?\.php/i', $url) )
                $url = 'http://' . $url;
-       
-       $url = preg_replace('/&([^#])(?![a-z]{2,8};)/', '&#038;$1', $url);
+
+       // Replace ampersands ony when displaying.
+       if ( 'display' == $context )
+               $url = preg_replace('/&([^#])(?![a-z]{2,8};)/', '&#038;$1', $url);
+
        if ( !is_array($protocols) )
-               $protocols = array('http', 'https', 'ftp', 'ftps', 'mailto', 'news', 'irc', 'gopher', 'nntp', 'feed', 'telnet'); 
+               $protocols = array('http', 'https', 'ftp', 'ftps', 'mailto', 'news', 'irc', 'gopher', 'nntp', 'feed', 'telnet');
        if ( wp_kses_bad_protocol( $url, $protocols ) != $url )
                return '';
-       return $url;
+
+       return apply_filters('clean_url', $url, $original_url, $context);
+}
+
+function sanitize_url( $url, $protocols = null ) {
+       return clean_url( $url, $protocols, 'db');
+}
+
+// Borrowed from the PHP Manual user notes. Convert entities, while
+// preserving already-encoded entities:
+function htmlentities2($myHTML) {
+       $translation_table=get_html_translation_table (HTML_ENTITIES,ENT_QUOTES);
+       $translation_table[chr(38)] = '&';
+       return preg_replace("/&(?![A-Za-z]{0,4}\w{2,3};|#[0-9]{2,3};)/","&amp;" , strtr($myHTML, $translation_table));
 }
 
 // Escape single quotes, specialchar double quotes, and fix line endings.
@@ -1078,4 +1158,100 @@ function attribute_escape($text) {
        return apply_filters('attribute_escape', $safe_text, $text);
 }
 
+function wp_make_link_relative( $link ) {
+       return preg_replace('|https?://[^/]+(/.*)|i', '$1', $link );
+}
+
+function sanitize_option($option, $value) { // Remember to call stripslashes!
+
+       switch ($option) {
+               case 'admin_email':
+                       $value = sanitize_email($value);
+                       break;
+
+               case 'default_post_edit_rows':
+               case 'mailserver_port':
+               case 'comment_max_links':
+               case 'page_on_front':
+               case 'rss_excerpt_length':
+               case 'default_category':
+               case 'default_email_category':
+               case 'default_link_category':
+                       $value = abs((int) $value);
+                       break;
+
+               case 'posts_per_page':
+               case 'posts_per_rss':
+                       $value = (int) $value;
+                       if ( empty($value) ) $value = 1;
+                       if ( $value < -1 ) $value = abs($value);
+                       break;
+
+               case 'default_ping_status':
+               case 'default_comment_status':
+                       // Options that if not there have 0 value but need to be something like "closed"
+                       if ( $value == '0' || $value == '')
+                               $value = 'closed';
+                       break;
+
+               case 'blogdescription':
+               case 'blogname':
+                       $value = addslashes($value);
+                       $value = wp_filter_post_kses( $value ); // calls stripslashes then addslashes
+                       $value = stripslashes($value);
+                       $value = wp_specialchars( $value );
+                       break;
+
+               case 'blog_charset':
+                       $value = preg_replace('/[^a-zA-Z0-9_-]/', '', $value); // strips slashes
+                       break;
+
+               case 'date_format':
+               case 'time_format':
+               case 'mailserver_url':
+               case 'mailserver_login':
+               case 'mailserver_pass':
+               case 'ping_sites':
+               case 'upload_path':
+                       $value = strip_tags($value);
+                       $value = addslashes($value);
+                       $value = wp_filter_kses($value); // calls stripslashes then addslashes
+                       $value = stripslashes($value);
+                       break;
+
+               case 'gmt_offset':
+                       $value = preg_replace('/[^0-9:.-]/', '', $value); // strips slashes
+                       break;
+
+               case 'siteurl':
+               case 'home':
+                       $value = stripslashes($value);
+                       $value = clean_url($value);
+                       break;
+               default :
+                       $value = apply_filters("sanitize_option_{$option}", $value, $option);
+                       break;
+       }
+
+       return $value;
+}
+
+function wp_parse_str( $string, &$array ) {
+       parse_str( $string, $array );
+       if ( get_magic_quotes_gpc() )
+               $array = stripslashes_deep( $array ); // parse_str() adds slashes if magicquotes is on.  See: http://php.net/parse_str
+       $array = apply_filters( 'wp_parse_str', $array );
+}
+
+// Convert lone less than signs.  KSES already converts lone greater than signs.
+function wp_pre_kses_less_than( $text ) {
+       return preg_replace_callback('%<[^>]*?((?=<)|>|$)%', 'wp_pre_kses_less_than_callback', $text);
+}
+
+function wp_pre_kses_less_than_callback( $matches ) {
+       if ( false === strpos($matches[0], '>') )
+               return wp_specialchars($matches[0]);
+       return $matches[0];
+}
+
 ?>