Make WordPress Core

Changeset 846


Ignore:
Timestamp:
02/09/2004 06:57:02 AM (21 years ago)
Author:
saxmatt
Message:

Breaking out some of the formatting functions into a different file.

Location:
trunk/wp-includes
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/functions.php

    r845 r846  
    11<?php
     2
     3$curpath = dirname(__FILE__).'/';
     4
     5require($curpath . 'functions-formatting.php');
    26
    37if (!function_exists('_')) {
     
    1115        return ((float) $string);
    1216    }
    13 }
    14 
    15 /* functions... */
    16 
    17 /***** Formatting functions *****/
    18 function wptexturize($text) {
    19     $output = "";
    20     $textarr = preg_split("/(<.*>)/Us", $text, -1, PREG_SPLIT_DELIM_CAPTURE); // capture the tags as well as in between
    21     $stop = count($textarr); $next = true; // loop stuff
    22     for ($i = 0; $i < $stop; $i++) {
    23         $curl = $textarr[$i];
    24         if (!strstr($_SERVER['HTTP_USER_AGENT'], 'Gecko')) {
    25             $curl = str_replace('<q>', '&#8220;', $curl);
    26             $curl = str_replace('</q>', '&#8221;', $curl);
    27         }
    28         if (isset($curl{0}) && '<' != $curl{0} && $next) { // If it's not a tag
    29             $curl = str_replace('---', '&#8212;', $curl);
    30             $curl = str_replace('--', '&#8211;', $curl);
    31             $curl = str_replace("...", '&#8230;', $curl);
    32             $curl = str_replace('``', '&#8220;', $curl);
    33 
    34             // This is a hack, look at this more later. It works pretty well though.
    35             $cockney = array("'tain't","'twere","'twas","'tis","'twill","'til","'bout","'nuff","'round");
    36             $cockneyreplace = array("&#8217;tain&#8217;t","&#8217;twere","&#8217;twas","&#8217;tis","&#8217;twill","&#8217;til","&#8217;bout","&#8217;nuff","&#8217;round");
    37             $curl = str_replace($cockney, $cockneyreplace, $curl);
    38 
    39             $curl = preg_replace("/'s/", "&#8217;s", $curl);
    40             $curl = preg_replace("/'(\d\d(?:&#8217;|')?s)/", "&#8217;$1", $curl);
    41             $curl = preg_replace('/(\s|\A|")\'/', '$1&#8216;', $curl);
    42             $curl = preg_replace("/(\d+)\"/", "$1&Prime;", $curl);
    43             $curl = preg_replace("/(\d+)'/", "$1&prime;", $curl);
    44             $curl = preg_replace("/(\S)'([^'\s])/", "$1&#8217;$2", $curl);
    45             $curl = preg_replace('/"([\s.,!?;:&\']|\Z)/', '&#8221;$1', $curl);
    46             $curl = preg_replace('/(\s|\A)"/', '$1&#8220;', $curl);
    47             $curl = preg_replace("/'([\s.]|\Z)/", '&#8217;$1', $curl);
    48             $curl = preg_replace("/\(tm\)/i", '&#8482;', $curl);
    49             $curl = preg_replace("/\(c\)/i", '&#169;', $curl);
    50             $curl = preg_replace("/\(r\)/i", '&#174;', $curl);
    51             $curl = preg_replace('/&([^#])(?![a-z]{1,8};)/', '&#038;$1', $curl);
    52             $curl = str_replace("''", '&#8221;', $curl);
    53            
    54             $curl = preg_replace('/(d+)x(\d+)/', "$1&#215;$2", $curl);
    55 
    56         } elseif (strstr($curl, '<code') || strstr($curl, '<pre') || strstr($curl, '<kbd' || strstr($curl, '<style') || strstr($curl, '<script'))) {
    57             // strstr is fast
    58             $next = false;
    59         } else {
    60             $next = true;
    61         }
    62         $output .= $curl;
    63     }
    64     return $output;
    65 }
    66 
    67 function wpautop($pee, $br = 1) {
    68     $pee = $pee . "\n"; // just to make things a little easier, pad the end
    69     $pee = preg_replace('|<br />\s*<br />|', "\n\n", $pee);
    70     $pee = preg_replace('!(<(?:table|tr|td|th|div|ul|ol|li|pre|select|form|blockquote|p|h[1-6])[^>]*>)!', "\n$1", $pee); // Space things out a little
    71     $pee = preg_replace('!(</(?:table|tr|td|th|div|ul|ol|li|pre|select|form|blockquote|p|h[1-6])>)!', "$1\n", $pee); // Space things out a little
    72     $pee = preg_replace("/(\r\n|\r)/", "\n", $pee); // cross-platform newlines
    73     $pee = preg_replace("/\n\n+/", "\n\n", $pee); // take care of duplicates
    74     $pee = preg_replace('/\n?(.+?)(?:\n\s*\n|\z)/s', "\t<p>$1</p>\n", $pee); // make paragraphs, including one at the end
    75     $pee = preg_replace('|<p>\s*?</p>|', '', $pee); // under certain strange conditions it could create a P of entirely whitespace
    76     $pee = preg_replace('!<p>\s*(</?(?:table|tr|td|th|div|ul|ol|li|pre|select|form|blockquote|p|h[1-6])[^>]*>)\s*</p>!', "$1", $pee); // don't pee all over a tag
    77     $pee = preg_replace("|<p>(<li.+?)</p>|", "$1", $pee); // problem with nested lists
    78     $pee = preg_replace('|<p><blockquote([^>]*)>|i', "<blockquote$1><p>", $pee);
    79     $pee = str_replace('</blockquote></p>', '</p></blockquote>', $pee);
    80     $pee = preg_replace('!<p>\s*(</?(?:table|tr|td|th|div|ul|ol|li|pre|select|form|blockquote|p|h[1-6])[^>]*>)!', "$1", $pee);
    81     $pee = preg_replace('!(</?(?:table|tr|td|th|div|ul|ol|li|pre|select|form|blockquote|p|h[1-6])[^>]*>)\s*</p>!', "$1", $pee);
    82     if ($br) $pee = preg_replace('|(?<!<br />)\s*\n|', "<br />\n", $pee); // optionally make line breaks
    83     $pee = preg_replace('!(</?(?:table|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|select|form|blockquote|p|h[1-6])[^>]*>)\s*<br />!', "$1", $pee);
    84     $pee = preg_replace('!<br />(\s*</?(?:p|li|div|th|pre|td|ul|ol)>)!', '$1', $pee);
    85     $pee = preg_replace('/&([^#])(?![a-z]{1,8};)/', '&#038;$1', $pee);
    86    
    87     return $pee;
    88 }
    89 
    90 function sanitize_title($title) {
    91     $title = strtolower($title);
    92     $title = preg_replace('/&.+?;/', '', $title); // kill entities
    93     $title = preg_replace('/[^a-z0-9 -]/', '', $title);
    94     $title = preg_replace('/\s+/', ' ', $title);
    95     $title = trim($title);
    96     $title = str_replace(' ', '-', $title);
    97     $title = preg_replace('|-+|', '-', $title);
    98     return $title;
    9917}
    10018
     
    10523    $text = preg_replace('/<a (.+?)>/i', "<a $1 target='_blank' rel='external'>", $text);
    10624    return $text;
    107 }
    108 
    109 function autobrize($content) {
    110     $content = preg_replace("/<br>\n/", "\n", $content);
    111     $content = preg_replace("/<br \/>\n/", "\n", $content);
    112     $content = preg_replace("/(\015\012)|(\015)|(\012)/", "<br />\n", $content);
    113     return $content;
    114     }
    115 function unautobrize($content) {
    116     $content = preg_replace("/<br>\n/", "\n", $content);   //for PHP versions before 4.0.5
    117     $content = preg_replace("/<br \/>\n/", "\n", $content);
    118     return $content;
    119     }
    120 
    121 
    122 function format_to_edit($content) {
    123     global $autobr;
    124     $content = stripslashes($content);
    125     if ($autobr) { $content = unautobrize($content); }
    126     $content = htmlspecialchars($content);
    127     return $content;
    128     }
    129 function format_to_post($content) {
    130     global $post_autobr,$comment_autobr;
    131     $content = addslashes($content);
    132     if ($post_autobr || $comment_autobr) { $content = autobrize($content); }
    133     return $content;
    134     }
    135 
    136 
    137 function zeroise($number,$threshold) { // function to add leading zeros when necessary
    138     $l=strlen($number);
    139     if ($l<$threshold)
    140         for ($i=0; $i<($threshold-$l); $i=$i+1) { $number='0'.$number;  }
    141     return $number;
    142     }
    143 
    144 
    145 function backslashit($string) {
    146     $string = preg_replace('/([a-z])/i', '\\\\\1', $string);
    147     return $string;
    148 }
    149 
    150 function trailingslashit($string) {
    151     if ( '/' != substr($string, -1)) {
    152         $string .= '/';
    153     }
    154     return $string;
    15525}
    15626
     
    19060            break;
    19161    }
    192 }
    193 
    194 function addslashes_gpc($gpc) {
    195     if (!get_magic_quotes_gpc()) {
    196         $gpc = addslashes($gpc);
    197     }
    198     return $gpc;
    19962}
    20063
     
    21679    }
    21780
    218 
    219 
    22081function get_weekstartend($mysqlstring, $start_of_week) {
    22182    $my = substr($mysqlstring,0,4);
     
    23495    return $week;
    23596}
    236 
    237 function convert_chars($content,$flag='obsolete attribute left there for backwards compatibility') { // html/unicode entities output
    238 
    239     global $use_htmltrans, $wp_htmltrans, $wp_htmltranswinuni;
    240 
    241     // removes metadata tags
    242     $content = preg_replace('/<title>(.+?)<\/title>/','',$content);
    243     $content = preg_replace('/<category>(.+?)<\/category>/','',$content);
    244    
    245     if ($use_htmltrans) {
    246 
    247         // converts lone & characters into &#38; (a.k.a. &amp;)
    248         $content = preg_replace('/&[^#](?![a-z]*;)/ie', '"&#38;".substr("\0",1)', $content);
    249 
    250         // converts HTML-entities to their display values in order to convert them again later
    251         $content = preg_replace('/['.chr(127).'-'.chr(255).']/e', '"&#".ord(\'\0\').";"', $content );
    252         $content = strtr($content, $wp_htmltrans);
    253 
    254         // now converting: Windows CP1252 => Unicode (valid HTML)
    255         // (if you've ever pasted text from MSWord, you'll understand)
    256 
    257         $content = strtr($content, $wp_htmltranswinuni);
    258 
    259     }
    260 
    261     // you can delete these 2 lines if you don't like <br /> and <hr />
    262     $content = str_replace("<br>","<br />",$content);
    263     $content = str_replace("<hr>","<hr />",$content);
    264 
    265     return $content;
    266 
    267 }
    268 
    269 function convert_bbcode($content) {
    270     global $wp_bbcode, $use_bbcode;
    271     if ($use_bbcode) {
    272         $content = preg_replace($wp_bbcode["in"], $wp_bbcode["out"], $content);
    273     }
    274     $content = convert_bbcode_email($content);
    275     return $content;
    276 }
    277 
    278 function convert_bbcode_email($content) {
    279     global $use_bbcode;
    280     $bbcode_email["in"] = array(
    281         '#\[email](.+?)\[/email]#eis',
    282         '#\[email=(.+?)](.+?)\[/email]#eis'
    283     );
    284     $bbcode_email["out"] = array(
    285         "'<a href=\"mailto:'.antispambot('\\1').'\">'.antispambot('\\1').'</a>'",       // E-mail
    286         "'<a href=\"mailto:'.antispambot('\\1').'\">\\2</a>'"
    287     );
    288 
    289     $content = preg_replace($bbcode_email["in"], $bbcode_email["out"], $content);
    290     return $content;
    291 }
    292 
    293 function convert_gmcode($content) {
    294     global $wp_gmcode, $use_gmcode;
    295     if ($use_gmcode) {
    296         $content = preg_replace($wp_gmcode["in"], $wp_gmcode["out"], $content);
    297     }
    298     return $content;
    299 }
    300 
    301 function convert_smilies($text) {
    302     global $smilies_directory, $use_smilies;
    303     global $wp_smiliessearch, $wp_smiliesreplace;
    304     $output = '';
    305     if ($use_smilies) {
    306         // HTML loop taken from texturize function, could possible be consolidated
    307         $textarr = preg_split("/(<.*>)/U", $text, -1, PREG_SPLIT_DELIM_CAPTURE); // capture the tags as well as in between
    308         $stop = count($textarr);// loop stuff
    309         for ($i = 0; $i < $stop; $i++) {
    310             $content = $textarr[$i];
    311             if ((strlen($content) > 0) && ('<' != $content{0})) { // If it's not a tag
    312                 $content = str_replace($wp_smiliessearch, $wp_smiliesreplace, $content);
    313             }
    314             $output .= $content;
    315         }
    316     } else {
    317         // return default text.
    318         $output = $text;
    319     }
    320     return $output;
    321 }
    322 
    323 function antispambot($emailaddy, $mailto=0) {
    324     $emailNOSPAMaddy = '';
    325     srand ((float) microtime() * 1000000);
    326     for ($i = 0; $i < strlen($emailaddy); $i = $i + 1) {
    327         $j = floor(rand(0, 1+$mailto));
    328         if ($j==0) {
    329             $emailNOSPAMaddy .= '&#'.ord(substr($emailaddy,$i,1)).';';
    330         } elseif ($j==1) {
    331             $emailNOSPAMaddy .= substr($emailaddy,$i,1);
    332         } elseif ($j==2) {
    333             $emailNOSPAMaddy .= '%'.zeroise(dechex(ord(substr($emailaddy, $i, 1))), 2);
    334         }
    335     }
    336     $emailNOSPAMaddy = str_replace('@','&#64;',$emailNOSPAMaddy);
    337     return $emailNOSPAMaddy;
    338 }
    339 
    340 function make_clickable($text) { // original function: phpBB, extended here for AIM & ICQ
    341     $ret = " " . $text;
    342     $ret = preg_replace("#([\n ])([a-z]+?)://([^, <>{}\n\r]+)#i", "\\1<a href=\"\\2://\\3\" target=\"_blank\">\\2://\\3</a>", $ret);
    343     $ret = preg_replace("#([\n ])aim:([^,< \n\r]+)#i", "\\1<a href=\"aim:goim?screenname=\\2\\3&message=Hello\">\\2\\3</a>", $ret);
    344     $ret = preg_replace("#([\n ])icq:([^,< \n\r]+)#i", "\\1<a href=\"http://wwp.icq.com/scripts/search.dll?to=\\2\\3\">\\2\\3</a>", $ret);
    345     $ret = preg_replace("#([\n ])www\.([a-z0-9\-]+)\.([a-z0-9\-.\~]+)((?:/[^,< \n\r]*)?)#i", "\\1<a href=\"http://www.\\2.\\3\\4\" target=\"_blank\">www.\\2.\\3\\4</a>", $ret);
    346     $ret = preg_replace("#([\n ])([a-z0-9\-_.]+?)@([^,< \n\r]+)#i", "\\1<a href=\"mailto:\\2@\\3\">\\2@\\3</a>", $ret);
    347     $ret = substr($ret, 1);
    348     return $ret;
    349 }
    350 
    351 
    352 function is_email($user_email) {
    353     $chars = "/^([a-z0-9_]|\\-|\\.)+@(([a-z0-9_]|\\-)+\\.)+[a-z]{2,4}\$/i";
    354     if(strstr($user_email, '@') && strstr($user_email, '.')) {
    355         if (preg_match($chars, $user_email)) {
    356             return true;
    357         } else {
    358             return false;
    359         }
    360     } else {
    361         return false;
    362     }
    363 }
    364 
    365 
    366 function strip_all_but_one_link($text, $mylink) {
    367     $match_link = '#(<a.+?href.+?'.'>)(.+?)(</a>)#';
    368     preg_match_all($match_link, $text, $matches);
    369     $count = count($matches[0]);
    370     for ($i=0; $i<$count; $i++) {
    371         if (!strstr($matches[0][$i], $mylink)) {
    372             $text = str_replace($matches[0][$i], $matches[2][$i], $text);
    373         }
    374     }
    375     return $text;
    376 }
    377 
    378 
    379 /***** // Formatting functions *****/
    380 
    381 
    38297
    38398function get_lastpostdate() {
     
    1215930}
    1216931
    1217 /**
    1218  ** sanitise HTML attributes, remove frame/applet/*script/mouseovers,etc. tags
    1219  ** so that this kind of thing cannot be done:
    1220  ** This is how we can do <b onmouseover="alert('badbadbad')">bad stuff</b>!
    1221  **/
    1222 function sanitise_html_attributes($text) {
    1223     $text = preg_replace('#(([\s"\'])on[a-z]{1,}|style|class|id)="(.*?)"#i', '$1', $text);
    1224     $text = preg_replace('#(([\s"\'])on[a-z]{1,}|style|class|id)=\'(.*?)\'#i', '$1', $text);
    1225     $text = preg_replace('#(([\s"\'])on[a-z]{1,}|style|class|id)[ \t]*=[ \t]*([^ \t\>]*?)#i', '$1', $text);
    1226     $text = preg_replace('#([a-z]{1,})="(( |\t)*?)(javascript|vbscript|about):(.*?)"#i', '$1=""', $text);
    1227     $text = preg_replace('#([a-z]{1,})=\'(( |\t)*?)(javascript|vbscript|about):(.*?)\'#i', '$1=""', $text);
    1228     $text = preg_replace('#\<(\/{0,1})([a-z]{0,2})(frame|applet)(.*?)\>#i', '', $text);
    1229     return $text;
    1230 }
    1231 
    1232 /*
    1233  balanceTags
    1234  
    1235  Balances Tags of string using a modified stack.
    1236  
    1237  @param text      Text to be balanced
    1238  @return          Returns balanced text
    1239  @author          Leonard Lin (leonard@acm.org)
    1240  @version         v1.1
    1241  @date            November 4, 2001
    1242  @license         GPL v2.0
    1243  @notes           
    1244  @changelog       
    1245              1.2  ***TODO*** Make better - change loop condition to $text
    1246              1.1  Fixed handling of append/stack pop order of end text
    1247                   Added Cleaning Hooks
    1248              1.0  First Version
    1249 */
    1250 function balanceTags($text, $is_comment = 0) {
    1251     global $use_balanceTags;
    1252 
    1253     if ($is_comment) {
    1254         $text = sanitise_html_attributes($text);
    1255     }
    1256    
    1257     if ($use_balanceTags == 0) {
    1258         return $text;
    1259     }
    1260 
    1261     $tagstack = array();
    1262     $stacksize = 0;
    1263     $tagqueue = '';
    1264     $newtext = '';
    1265 
    1266     # b2 bug fix for comments - in case you REALLY meant to type '< !--'
    1267     $text = str_replace('< !--', '<    !--', $text);
    1268 
    1269     # b2 bug fix for LOVE <3 (and other situations with '<' before a number)
    1270     $text = preg_replace('#<([0-9]{1})#', '&lt;$1', $text);
    1271 
    1272 
    1273     while (preg_match("/<(\/?\w*)\s*([^>]*)>/",$text,$regex)) {
    1274         $newtext = $newtext . $tagqueue;
    1275 
    1276         $i = strpos($text,$regex[0]);
    1277         $l = strlen($tagqueue) + strlen($regex[0]);
    1278 
    1279         // clear the shifter
    1280         $tagqueue = '';
    1281 
    1282         // Pop or Push
    1283         if ($regex[1][0] == "/") { // End Tag
    1284             $tag = strtolower(substr($regex[1],1));
    1285 
    1286             // if too many closing tags
    1287             if($stacksize <= 0) {
    1288                 $tag = '';
    1289                 //or close to be safe $tag = '/' . $tag;
    1290             }
    1291             // if stacktop value = tag close value then pop
    1292             else if ($tagstack[$stacksize - 1] == $tag) { // found closing tag
    1293                 $tag = '</' . $tag . '>'; // Close Tag
    1294                 // Pop
    1295                 array_pop ($tagstack);
    1296                 $stacksize--;
    1297             } else { // closing tag not at top, search for it
    1298                 for ($j=$stacksize-1;$j>=0;$j--) {
    1299                     if ($tagstack[$j] == $tag) {
    1300                     // add tag to tagqueue
    1301                         for ($k=$stacksize-1;$k>=$j;$k--){
    1302                             $tagqueue .= '</' . array_pop ($tagstack) . '>';
    1303                             $stacksize--;
    1304                         }
    1305                         break;
    1306                     }
    1307                 }
    1308                 $tag = '';
    1309             }
    1310         } else { // Begin Tag
    1311             $tag = strtolower($regex[1]);
    1312 
    1313             // Tag Cleaning
    1314 
    1315             // Push if not img or br or hr
    1316             if($tag != 'br' && $tag != 'img' && $tag != 'hr') {
    1317                 $stacksize = array_push ($tagstack, $tag);
    1318             }
    1319 
    1320             // Attributes
    1321             // $attributes = $regex[2];
    1322             $attributes = $regex[2];
    1323             if($attributes) {
    1324                 $attributes = ' '.$attributes;
    1325             }
    1326 
    1327             $tag = '<'.$tag.$attributes.'>';
    1328         }
    1329 
    1330         $newtext .= substr($text,0,$i) . $tag;
    1331         $text = substr($text,$i+$l);
    1332     } 
    1333 
    1334     // Clear Tag Queue
    1335     $newtext = $newtext . $tagqueue;
    1336 
    1337     // Add Remaining text
    1338     $newtext .= $text;
    1339 
    1340     // Empty Stack
    1341     while($x = array_pop($tagstack)) {
    1342         $newtext = $newtext . '</' . $x . '>'; // Add remaining tags to close     
    1343     }
    1344 
    1345     # b2 fix for the bug with HTML comments
    1346     $newtext = str_replace("< !--","<!--",$newtext);
    1347     $newtext = str_replace("<    !--","< !--",$newtext);
    1348 
    1349     return $newtext;
    1350 }
    1351 
    1352932function doGeoUrlHeader($posts) {
    1353933    global $use_default_geourl,$default_geourl_lat,$default_geourl_lon;
Note: See TracChangeset for help on using the changeset viewer.