]> scripts.mit.edu Git - autoinstalls/wordpress.git/blobdiff - wp-includes/functions-formatting.php
Wordpress 2.0.11
[autoinstalls/wordpress.git] / wp-includes / functions-formatting.php
index 7db8ea02f1ffc1fc14f367730308353ea286ea29..217e953ea3f43ef44e8f9ba8c10a0a3c2861b1f2 100644 (file)
@@ -1,6 +1,7 @@
 <?php
 
 function wptexturize($text) {
+       global $wp_cockneyreplace;
        $output = '';
        // Capture tags and everything inside them
        $textarr = preg_split("/(<.*>)/Us", $text, -1, PREG_SPLIT_DELIM_CAPTURE);
@@ -16,9 +17,15 @@ function wptexturize($text) {
                        $curl = str_replace('...', '&#8230;', $curl);
                        $curl = str_replace('``', '&#8220;', $curl);
 
-                       // This is a hack, look at this more later. It works pretty well though.
-                       $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 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");
+                       }
+
                        $curl = str_replace($cockney, $cockneyreplace, $curl);
 
                        $curl = preg_replace("/'s/", '&#8217;s', $curl);
@@ -72,7 +79,7 @@ function wpautop($pee, $br = 1) {
        $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('!<br />(\s*</?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)>)!', '$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; 
@@ -245,8 +252,10 @@ function remove_accents($string) {
                chr(197).chr(188) => 'z', chr(197).chr(189) => 'Z',
                chr(197).chr(190) => 'z', chr(197).chr(191) => 's',
                // Euro Sign
-               chr(226).chr(130).chr(172) => 'E');
-               
+               chr(226).chr(130).chr(172) => 'E',
+               // GBP (Pound) Sign
+               chr(194).chr(163) => '');
+
                $string = strtr($string, $chars);
        } else {
                // Assume ISO-8859-1 if not UTF-8
@@ -390,26 +399,27 @@ function funky_javascript_fix($text) {
 
 /*
  balanceTags
+
  Balances Tags of string using a modified stack.
+
  @param text      Text to be balanced
+ @param force     Forces balancing, ignoring the value of the option
  @return          Returns balanced text
  @author          Leonard Lin (leonard@acm.org)
  @version         v1.1
  @date            November 4, 2001
  @license         GPL v2.0
- @notes           
- @changelog       
+ @notes
+ @changelog
  ---  Modified by Scott Reilly (coffee2code) 02 Aug 2004
-             1.2  ***TODO*** Make better - change loop condition to $text
-             1.1  Fixed handling of append/stack pop order of end text
-                  Added Cleaning Hooks
-             1.0  First Version
+       1.2  ***TODO*** Make better - change loop condition to $text
+       1.1  Fixed handling of append/stack pop order of end text
+            Added Cleaning Hooks
+       1.0  First Version
 */
-function balanceTags($text, $is_comment = 0) {
-       
-       if ( get_option('use_balanceTags') == 0)
+function balanceTags($text, $force = false) {
+
+       if ( !$force && get_option('use_balanceTags') == 0 )
                return $text;
 
        $tagstack = array(); $stacksize = 0; $tagqueue = ''; $newtext = '';
@@ -431,7 +441,7 @@ function balanceTags($text, $is_comment = 0) {
                if ($regex[1][0] == "/") { // End Tag
                        $tag = strtolower(substr($regex[1],1));
                        // if too many closing tags
-                       if($stacksize <= 0) { 
+                       if($stacksize <= 0) {
                                $tag = '';
                                //or close to be safe $tag = '/' . $tag;
                        }
@@ -488,7 +498,7 @@ function balanceTags($text, $is_comment = 0) {
                }
                $newtext .= substr($text,0,$i) . $tag;
                $text = substr($text,$i+$l);
-       }  
+       }
 
        // Clear Tag Queue
        $newtext .= $tagqueue;
@@ -509,7 +519,7 @@ function balanceTags($text, $is_comment = 0) {
 }
 
 function force_balance_tags($text) {
-       return balanceTags($text, 0, true);
+       return balanceTags($text, true);
 }
 
 function format_to_edit($content, $richedit = false) {
@@ -582,16 +592,24 @@ function antispambot($emailaddy, $mailto=0) {
 
 function make_clickable($ret) {
        $ret = ' ' . $ret;
-       $ret = preg_replace("#(^|[\n ])([\w]+?://[\w\#$%&~/.\-;:=,?@\[\]+]*)#is", "$1<a href='$2' rel='nofollow'>$2</a>", $ret);
-       $ret = preg_replace("#(^|[\n ])((www|ftp)\.[\w\#$%&~/.\-;:=,?@\[\]+]*)#is", "$1<a href='http://$2' rel='nofollow'>$2</a>", $ret);
-       $ret = preg_replace("#(\s)([a-z0-9\-_.]+)@([^,< \n\r]+)#i", "$1<a href=\"mailto:$2@$3\">$2@$3</a>", $ret);
-       $ret = substr($ret, 1);
+       // 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);
+       // 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);
        return $ret;
 }
 
 function wp_rel_nofollow( $text ) {
-       $text = preg_replace('|<a (.+?)>|i', '<a $1 rel="nofollow">', $text);
+       $text = preg_replace('|<a (.+?)>|ie', "'<a ' . str_replace(' rel=\"nofollow\"','',stripslashes('$1')) . ' rel=\"nofollow\">'", $text);
        return $text;
 }
 
@@ -1027,10 +1045,37 @@ function wp_richedit_pre($text) {
        return apply_filters('richedit_pre', $output);
 }
 
+function clean_url( $url, $protocols = null ) {
+       if ('' == $url) return $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) )
+               $url = 'http://' . $url;
+       
+       $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'); 
+       if ( wp_kses_bad_protocol( $url, $protocols ) != $url )
+               return '';
+       return $url;
+}
+
 // Escape single quotes, specialchar double quotes, and fix line endings.
 function js_escape($text) {
-       $text = wp_specialchars($text, 'double');
-       $text = str_replace('&#039;', "'", $text);
-       return preg_replace("/\r?\n/", "\\n", addslashes($text));
+       $safe_text = wp_specialchars($text, 'double');
+       $safe_text = preg_replace('/&#(x)?0*(?(1)27|39);?/i', "'", stripslashes($safe_text));
+       $safe_text = preg_replace("/\r?\n/", "\\n", addslashes($safe_text));
+       return apply_filters('js_escape', $safe_text, $text);
 }
+
+// Escaping for HTML attributes
+function attribute_escape($text) {
+       $safe_text = wp_specialchars($text, true);
+       return apply_filters('attribute_escape', $safe_text, $text);
+}
+
 ?>