]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/formatting.php
Wordpress 2.3.3-scripts
[autoinstalls/wordpress.git] / wp-includes / formatting.php
1 <?php
2
3 function wptexturize($text) {
4         global $wp_cockneyreplace;
5         $next = true;
6         $output = '';
7         $curl = '';
8         $textarr = preg_split('/(<.*>)/Us', $text, -1, PREG_SPLIT_DELIM_CAPTURE);
9         $stop = count($textarr);
10
11         // if a plugin has provided an autocorrect array, use it
12         if ( isset($wp_cockneyreplace) ) {
13                 $cockney = array_keys($wp_cockneyreplace);
14                 $cockneyreplace = array_values($wp_cockneyreplace);
15         } else {
16                 $cockney = array("'tain't","'twere","'twas","'tis","'twill","'til","'bout","'nuff","'round","'cause");
17                 $cockneyreplace = array("&#8217;tain&#8217;t","&#8217;twere","&#8217;twas","&#8217;tis","&#8217;twill","&#8217;til","&#8217;bout","&#8217;nuff","&#8217;round","&#8217;cause");
18         }
19
20         $static_characters = array_merge(array('---', ' -- ', '--', 'xn&#8211;', '...', '``', '\'s', '\'\'', ' (tm)'), $cockney);
21         $static_replacements = array_merge(array('&#8212;', ' &#8212; ', '&#8211;', 'xn--', '&#8230;', '&#8220;', '&#8217;s', '&#8221;', ' &#8482;'), $cockneyreplace);
22
23         $dynamic_characters = array('/\'(\d\d(?:&#8217;|\')?s)/', '/(\s|\A|")\'/', '/(\d+)"/', '/(\d+)\'/', '/(\S)\'([^\'\s])/', '/(\s|\A)"(?!\s)/', '/"(\s|\S|\Z)/', '/\'([\s.]|\Z)/', '/(\d+)x(\d+)/');
24         $dynamic_replacements = array('&#8217;$1','$1&#8216;', '$1&#8243;', '$1&#8242;', '$1&#8217;$2', '$1&#8220;$2', '&#8221;$1', '&#8217;$1', '$1&#215;$2');
25
26         for ( $i = 0; $i < $stop; $i++ ) {
27                 $curl = $textarr[$i];
28
29                 if (isset($curl{0}) && '<' != $curl{0} && $next) { // If it's not a tag
30                         // static strings
31                         $curl = str_replace($static_characters, $static_replacements, $curl);
32                         // regular expressions
33                         $curl = preg_replace($dynamic_characters, $dynamic_replacements, $curl);
34                 } elseif (strpos($curl, '<code') !== false || strpos($curl, '<pre') !== false || strpos($curl, '<kbd') !== false || strpos($curl, '<style') !== false || strpos($curl, '<script') !== false) {
35                         $next = false;
36                 } else {
37                         $next = true;
38                 }
39
40                 $curl = preg_replace('/&([^#])(?![a-zA-Z1-4]{1,8};)/', '&#038;$1', $curl);
41                 $output .= $curl;
42         }
43
44         return $output;
45 }
46
47 // Accepts matches array from preg_replace_callback in wpautop()
48 // or a string
49 function clean_pre($matches) {
50         if ( is_array($matches) )
51                 $text = $matches[1] . $matches[2] . "</pre>";
52         else
53                 $text = $matches;
54
55         $text = str_replace('<br />', '', $text);
56         $text = str_replace('<p>', "\n", $text);
57         $text = str_replace('</p>', '', $text);
58
59         return $text;
60 }
61
62 function wpautop($pee, $br = 1) {
63         $pee = $pee . "\n"; // just to make things a little easier, pad the end
64         $pee = preg_replace('|<br />\s*<br />|', "\n\n", $pee);
65         // Space things out a little
66         $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)';
67         $pee = preg_replace('!(<' . $allblocks . '[^>]*>)!', "\n$1", $pee);
68         $pee = preg_replace('!(</' . $allblocks . '>)!', "$1\n\n", $pee);
69         $pee = str_replace(array("\r\n", "\r"), "\n", $pee); // cross-platform newlines
70         $pee = preg_replace("/\n\n+/", "\n\n", $pee); // take care of duplicates
71         $pee = preg_replace('/\n?(.+?)(?:\n\s*\n|\z)/s', "<p>$1</p>\n", $pee); // make paragraphs, including one at the end
72         $pee = preg_replace('|<p>\s*?</p>|', '', $pee); // under certain strange conditions it could create a P of entirely whitespace
73         $pee = preg_replace('!<p>([^<]+)\s*?(</(?:div|address|form)[^>]*>)!', "<p>$1</p>$2", $pee);
74         $pee = preg_replace( '|<p>|', "$1<p>", $pee );
75         $pee = preg_replace('!<p>\s*(</?' . $allblocks . '[^>]*>)\s*</p>!', "$1", $pee); // don't pee all over a tag
76         $pee = preg_replace("|<p>(<li.+?)</p>|", "$1", $pee); // problem with nested lists
77         $pee = preg_replace('|<p><blockquote([^>]*)>|i', "<blockquote$1><p>", $pee);
78         $pee = str_replace('</blockquote></p>', '</p></blockquote>', $pee);
79         $pee = preg_replace('!<p>\s*(</?' . $allblocks . '[^>]*>)!', "$1", $pee);
80         $pee = preg_replace('!(</?' . $allblocks . '[^>]*>)\s*</p>!', "$1", $pee);
81         if ($br) {
82                 $pee = preg_replace('/<(script|style).*?<\/\\1>/se', 'str_replace("\n", "<WPPreserveNewline />", "\\0")', $pee);
83                 $pee = preg_replace('|(?<!<br />)\s*\n|', "<br />\n", $pee); // optionally make line breaks
84                 $pee = str_replace('<WPPreserveNewline />', "\n", $pee);
85         }
86         $pee = preg_replace('!(</?' . $allblocks . '[^>]*>)\s*<br />!', "$1", $pee);
87         $pee = preg_replace('!<br />(\s*</?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)[^>]*>)!', '$1', $pee);
88         if (strpos($pee, '<pre') !== false)
89                 $pee = preg_replace_callback('!(<pre.*?>)(.*?)</pre>!is', 'clean_pre', $pee );
90         $pee = preg_replace( "|\n</p>$|", '</p>', $pee );
91
92         return $pee;
93 }
94
95
96 function seems_utf8($Str) { # by bmorel at ssi dot fr
97         for ($i=0; $i<strlen($Str); $i++) {
98                 if (ord($Str[$i]) < 0x80) continue; # 0bbbbbbb
99                 elseif ((ord($Str[$i]) & 0xE0) == 0xC0) $n=1; # 110bbbbb
100                 elseif ((ord($Str[$i]) & 0xF0) == 0xE0) $n=2; # 1110bbbb
101                 elseif ((ord($Str[$i]) & 0xF8) == 0xF0) $n=3; # 11110bbb
102                 elseif ((ord($Str[$i]) & 0xFC) == 0xF8) $n=4; # 111110bb
103                 elseif ((ord($Str[$i]) & 0xFE) == 0xFC) $n=5; # 1111110b
104                 else return false; # Does not match any model
105                 for ($j=0; $j<$n; $j++) { # n bytes matching 10bbbbbb follow ?
106                         if ((++$i == strlen($Str)) || ((ord($Str[$i]) & 0xC0) != 0x80))
107                         return false;
108                 }
109         }
110         return true;
111 }
112
113 function wp_specialchars( $text, $quotes = 0 ) {
114         // Like htmlspecialchars except don't double-encode HTML entities
115         $text = str_replace('&&', '&#038;&', $text);
116         $text = str_replace('&&', '&#038;&', $text);
117         $text = preg_replace('/&(?:$|([^#])(?![a-z1-4]{1,8};))/', '&#038;$1', $text);
118         $text = str_replace('<', '&lt;', $text);
119         $text = str_replace('>', '&gt;', $text);
120         if ( 'double' === $quotes ) {
121                 $text = str_replace('"', '&quot;', $text);
122         } elseif ( 'single' === $quotes ) {
123                 $text = str_replace("'", '&#039;', $text);
124         } elseif ( $quotes ) {
125                 $text = str_replace('"', '&quot;', $text);
126                 $text = str_replace("'", '&#039;', $text);
127         }
128         return $text;
129 }
130
131 function utf8_uri_encode( $utf8_string, $length = 0 ) {
132         $unicode = '';
133         $values = array();
134         $num_octets = 1;
135
136         for ($i = 0; $i < strlen( $utf8_string ); $i++ ) {
137
138                 $value = ord( $utf8_string[ $i ] );
139
140                 if ( $value < 128 ) {
141                         if ( $length && ( strlen($unicode) + 1 > $length ) )
142                                 break;
143                         $unicode .= chr($value);
144                 } else {
145                         if ( count( $values ) == 0 ) $num_octets = ( $value < 224 ) ? 2 : 3;
146
147                         $values[] = $value;
148
149                         if ( $length && ( (strlen($unicode) + ($num_octets * 3)) > $length ) )
150                                 break;
151                         if ( count( $values ) == $num_octets ) {
152                                 if ($num_octets == 3) {
153                                         $unicode .= '%' . dechex($values[0]) . '%' . dechex($values[1]) . '%' . dechex($values[2]);
154                                 } else {
155                                         $unicode .= '%' . dechex($values[0]) . '%' . dechex($values[1]);
156                                 }
157
158                                 $values = array();
159                                 $num_octets = 1;
160                         }
161                 }
162         }
163
164         return $unicode;
165 }
166
167 function remove_accents($string) {
168         if ( !preg_match('/[\x80-\xff]/', $string) )
169                 return $string;
170
171         if (seems_utf8($string)) {
172                 $chars = array(
173                 // Decompositions for Latin-1 Supplement
174                 chr(195).chr(128) => 'A', chr(195).chr(129) => 'A',
175                 chr(195).chr(130) => 'A', chr(195).chr(131) => 'A',
176                 chr(195).chr(132) => 'A', chr(195).chr(133) => 'A',
177                 chr(195).chr(135) => 'C', chr(195).chr(136) => 'E',
178                 chr(195).chr(137) => 'E', chr(195).chr(138) => 'E',
179                 chr(195).chr(139) => 'E', chr(195).chr(140) => 'I',
180                 chr(195).chr(141) => 'I', chr(195).chr(142) => 'I',
181                 chr(195).chr(143) => 'I', chr(195).chr(145) => 'N',
182                 chr(195).chr(146) => 'O', chr(195).chr(147) => 'O',
183                 chr(195).chr(148) => 'O', chr(195).chr(149) => 'O',
184                 chr(195).chr(150) => 'O', chr(195).chr(153) => 'U',
185                 chr(195).chr(154) => 'U', chr(195).chr(155) => 'U',
186                 chr(195).chr(156) => 'U', chr(195).chr(157) => 'Y',
187                 chr(195).chr(159) => 's', chr(195).chr(160) => 'a',
188                 chr(195).chr(161) => 'a', chr(195).chr(162) => 'a',
189                 chr(195).chr(163) => 'a', chr(195).chr(164) => 'a',
190                 chr(195).chr(165) => 'a', chr(195).chr(167) => 'c',
191                 chr(195).chr(168) => 'e', chr(195).chr(169) => 'e',
192                 chr(195).chr(170) => 'e', chr(195).chr(171) => 'e',
193                 chr(195).chr(172) => 'i', chr(195).chr(173) => 'i',
194                 chr(195).chr(174) => 'i', chr(195).chr(175) => 'i',
195                 chr(195).chr(177) => 'n', chr(195).chr(178) => 'o',
196                 chr(195).chr(179) => 'o', chr(195).chr(180) => 'o',
197                 chr(195).chr(181) => 'o', chr(195).chr(182) => 'o',
198                 chr(195).chr(182) => 'o', chr(195).chr(185) => 'u',
199                 chr(195).chr(186) => 'u', chr(195).chr(187) => 'u',
200                 chr(195).chr(188) => 'u', chr(195).chr(189) => 'y',
201                 chr(195).chr(191) => 'y',
202                 // Decompositions for Latin Extended-A
203                 chr(196).chr(128) => 'A', chr(196).chr(129) => 'a',
204                 chr(196).chr(130) => 'A', chr(196).chr(131) => 'a',
205                 chr(196).chr(132) => 'A', chr(196).chr(133) => 'a',
206                 chr(196).chr(134) => 'C', chr(196).chr(135) => 'c',
207                 chr(196).chr(136) => 'C', chr(196).chr(137) => 'c',
208                 chr(196).chr(138) => 'C', chr(196).chr(139) => 'c',
209                 chr(196).chr(140) => 'C', chr(196).chr(141) => 'c',
210                 chr(196).chr(142) => 'D', chr(196).chr(143) => 'd',
211                 chr(196).chr(144) => 'D', chr(196).chr(145) => 'd',
212                 chr(196).chr(146) => 'E', chr(196).chr(147) => 'e',
213                 chr(196).chr(148) => 'E', chr(196).chr(149) => 'e',
214                 chr(196).chr(150) => 'E', chr(196).chr(151) => 'e',
215                 chr(196).chr(152) => 'E', chr(196).chr(153) => 'e',
216                 chr(196).chr(154) => 'E', chr(196).chr(155) => 'e',
217                 chr(196).chr(156) => 'G', chr(196).chr(157) => 'g',
218                 chr(196).chr(158) => 'G', chr(196).chr(159) => 'g',
219                 chr(196).chr(160) => 'G', chr(196).chr(161) => 'g',
220                 chr(196).chr(162) => 'G', chr(196).chr(163) => 'g',
221                 chr(196).chr(164) => 'H', chr(196).chr(165) => 'h',
222                 chr(196).chr(166) => 'H', chr(196).chr(167) => 'h',
223                 chr(196).chr(168) => 'I', chr(196).chr(169) => 'i',
224                 chr(196).chr(170) => 'I', chr(196).chr(171) => 'i',
225                 chr(196).chr(172) => 'I', chr(196).chr(173) => 'i',
226                 chr(196).chr(174) => 'I', chr(196).chr(175) => 'i',
227                 chr(196).chr(176) => 'I', chr(196).chr(177) => 'i',
228                 chr(196).chr(178) => 'IJ',chr(196).chr(179) => 'ij',
229                 chr(196).chr(180) => 'J', chr(196).chr(181) => 'j',
230                 chr(196).chr(182) => 'K', chr(196).chr(183) => 'k',
231                 chr(196).chr(184) => 'k', chr(196).chr(185) => 'L',
232                 chr(196).chr(186) => 'l', chr(196).chr(187) => 'L',
233                 chr(196).chr(188) => 'l', chr(196).chr(189) => 'L',
234                 chr(196).chr(190) => 'l', chr(196).chr(191) => 'L',
235                 chr(197).chr(128) => 'l', chr(197).chr(129) => 'L',
236                 chr(197).chr(130) => 'l', chr(197).chr(131) => 'N',
237                 chr(197).chr(132) => 'n', chr(197).chr(133) => 'N',
238                 chr(197).chr(134) => 'n', chr(197).chr(135) => 'N',
239                 chr(197).chr(136) => 'n', chr(197).chr(137) => 'N',
240                 chr(197).chr(138) => 'n', chr(197).chr(139) => 'N',
241                 chr(197).chr(140) => 'O', chr(197).chr(141) => 'o',
242                 chr(197).chr(142) => 'O', chr(197).chr(143) => 'o',
243                 chr(197).chr(144) => 'O', chr(197).chr(145) => 'o',
244                 chr(197).chr(146) => 'OE',chr(197).chr(147) => 'oe',
245                 chr(197).chr(148) => 'R',chr(197).chr(149) => 'r',
246                 chr(197).chr(150) => 'R',chr(197).chr(151) => 'r',
247                 chr(197).chr(152) => 'R',chr(197).chr(153) => 'r',
248                 chr(197).chr(154) => 'S',chr(197).chr(155) => 's',
249                 chr(197).chr(156) => 'S',chr(197).chr(157) => 's',
250                 chr(197).chr(158) => 'S',chr(197).chr(159) => 's',
251                 chr(197).chr(160) => 'S', chr(197).chr(161) => 's',
252                 chr(197).chr(162) => 'T', chr(197).chr(163) => 't',
253                 chr(197).chr(164) => 'T', chr(197).chr(165) => 't',
254                 chr(197).chr(166) => 'T', chr(197).chr(167) => 't',
255                 chr(197).chr(168) => 'U', chr(197).chr(169) => 'u',
256                 chr(197).chr(170) => 'U', chr(197).chr(171) => 'u',
257                 chr(197).chr(172) => 'U', chr(197).chr(173) => 'u',
258                 chr(197).chr(174) => 'U', chr(197).chr(175) => 'u',
259                 chr(197).chr(176) => 'U', chr(197).chr(177) => 'u',
260                 chr(197).chr(178) => 'U', chr(197).chr(179) => 'u',
261                 chr(197).chr(180) => 'W', chr(197).chr(181) => 'w',
262                 chr(197).chr(182) => 'Y', chr(197).chr(183) => 'y',
263                 chr(197).chr(184) => 'Y', chr(197).chr(185) => 'Z',
264                 chr(197).chr(186) => 'z', chr(197).chr(187) => 'Z',
265                 chr(197).chr(188) => 'z', chr(197).chr(189) => 'Z',
266                 chr(197).chr(190) => 'z', chr(197).chr(191) => 's',
267                 // Euro Sign
268                 chr(226).chr(130).chr(172) => 'E',
269                 // GBP (Pound) Sign
270                 chr(194).chr(163) => '');
271
272                 $string = strtr($string, $chars);
273         } else {
274                 // Assume ISO-8859-1 if not UTF-8
275                 $chars['in'] = chr(128).chr(131).chr(138).chr(142).chr(154).chr(158)
276                         .chr(159).chr(162).chr(165).chr(181).chr(192).chr(193).chr(194)
277                         .chr(195).chr(196).chr(197).chr(199).chr(200).chr(201).chr(202)
278                         .chr(203).chr(204).chr(205).chr(206).chr(207).chr(209).chr(210)
279                         .chr(211).chr(212).chr(213).chr(214).chr(216).chr(217).chr(218)
280                         .chr(219).chr(220).chr(221).chr(224).chr(225).chr(226).chr(227)
281                         .chr(228).chr(229).chr(231).chr(232).chr(233).chr(234).chr(235)
282                         .chr(236).chr(237).chr(238).chr(239).chr(241).chr(242).chr(243)
283                         .chr(244).chr(245).chr(246).chr(248).chr(249).chr(250).chr(251)
284                         .chr(252).chr(253).chr(255);
285
286                 $chars['out'] = "EfSZszYcYuAAAAAACEEEEIIIINOOOOOOUUUUYaaaaaaceeeeiiiinoooooouuuuyy";
287
288                 $string = strtr($string, $chars['in'], $chars['out']);
289                 $double_chars['in'] = array(chr(140), chr(156), chr(198), chr(208), chr(222), chr(223), chr(230), chr(240), chr(254));
290                 $double_chars['out'] = array('OE', 'oe', 'AE', 'DH', 'TH', 'ss', 'ae', 'dh', 'th');
291                 $string = str_replace($double_chars['in'], $double_chars['out'], $string);
292         }
293
294         return $string;
295 }
296
297 function sanitize_file_name( $name ) { // Like sanitize_title, but with periods
298         $name = strtolower( $name );
299         $name = preg_replace('/&.+?;/', '', $name); // kill entities
300         $name = str_replace( '_', '-', $name );
301         $name = preg_replace('/[^a-z0-9\s-.]/', '', $name);
302         $name = preg_replace('/\s+/', '-', $name);
303         $name = preg_replace('|-+|', '-', $name);
304         $name = trim($name, '-');
305         return $name;
306 }
307
308 function sanitize_user( $username, $strict = false ) {
309         $raw_username = $username;
310         $username = strip_tags($username);
311         // Kill octets
312         $username = preg_replace('|%([a-fA-F0-9][a-fA-F0-9])|', '', $username);
313         $username = preg_replace('/&.+?;/', '', $username); // Kill entities
314
315         // If strict, reduce to ASCII for max portability.
316         if ( $strict )
317                 $username = preg_replace('|[^a-z0-9 _.\-@]|i', '', $username);
318
319         return apply_filters('sanitize_user', $username, $raw_username, $strict);
320 }
321
322 function sanitize_title($title, $fallback_title = '') {
323         $title = strip_tags($title);
324         $title = apply_filters('sanitize_title', $title);
325
326         if (empty($title)) {
327                 $title = $fallback_title;
328         }
329
330         return $title;
331 }
332
333 function sanitize_title_with_dashes($title) {
334         $title = strip_tags($title);
335         // Preserve escaped octets.
336         $title = preg_replace('|%([a-fA-F0-9][a-fA-F0-9])|', '---$1---', $title);
337         // Remove percent signs that are not part of an octet.
338         $title = str_replace('%', '', $title);
339         // Restore octets.
340         $title = preg_replace('|---([a-fA-F0-9][a-fA-F0-9])---|', '%$1', $title);
341
342         $title = remove_accents($title);
343         if (seems_utf8($title)) {
344                 if (function_exists('mb_strtolower')) {
345                         $title = mb_strtolower($title, 'UTF-8');
346                 }
347                 $title = utf8_uri_encode($title, 200);
348         }
349
350         $title = strtolower($title);
351         $title = preg_replace('/&.+?;/', '', $title); // kill entities
352         $title = preg_replace('/[^%a-z0-9 _-]/', '', $title);
353         $title = preg_replace('/\s+/', '-', $title);
354         $title = preg_replace('|-+|', '-', $title);
355         $title = trim($title, '-');
356
357         return $title;
358 }
359
360 function convert_chars($content, $flag = 'obsolete') {
361         // Translation of invalid Unicode references range to valid range
362         $wp_htmltranswinuni = array(
363         '&#128;' => '&#8364;', // the Euro sign
364         '&#129;' => '',
365         '&#130;' => '&#8218;', // these are Windows CP1252 specific characters
366         '&#131;' => '&#402;',  // they would look weird on non-Windows browsers
367         '&#132;' => '&#8222;',
368         '&#133;' => '&#8230;',
369         '&#134;' => '&#8224;',
370         '&#135;' => '&#8225;',
371         '&#136;' => '&#710;',
372         '&#137;' => '&#8240;',
373         '&#138;' => '&#352;',
374         '&#139;' => '&#8249;',
375         '&#140;' => '&#338;',
376         '&#141;' => '',
377         '&#142;' => '&#382;',
378         '&#143;' => '',
379         '&#144;' => '',
380         '&#145;' => '&#8216;',
381         '&#146;' => '&#8217;',
382         '&#147;' => '&#8220;',
383         '&#148;' => '&#8221;',
384         '&#149;' => '&#8226;',
385         '&#150;' => '&#8211;',
386         '&#151;' => '&#8212;',
387         '&#152;' => '&#732;',
388         '&#153;' => '&#8482;',
389         '&#154;' => '&#353;',
390         '&#155;' => '&#8250;',
391         '&#156;' => '&#339;',
392         '&#157;' => '',
393         '&#158;' => '',
394         '&#159;' => '&#376;'
395         );
396
397         // Remove metadata tags
398         $content = preg_replace('/<title>(.+?)<\/title>/','',$content);
399         $content = preg_replace('/<category>(.+?)<\/category>/','',$content);
400
401         // Converts lone & characters into &#38; (a.k.a. &amp;)
402         $content = preg_replace('/&([^#])(?![a-z1-4]{1,8};)/i', '&#038;$1', $content);
403
404         // Fix Word pasting
405         $content = strtr($content, $wp_htmltranswinuni);
406
407         // Just a little XHTML help
408         $content = str_replace('<br>', '<br />', $content);
409         $content = str_replace('<hr>', '<hr />', $content);
410
411         return $content;
412 }
413
414 function funky_javascript_fix($text) {
415         // Fixes for browsers' javascript bugs
416         global $is_macIE, $is_winIE;
417
418         if ( $is_winIE || $is_macIE )
419                 $text =  preg_replace("/\%u([0-9A-F]{4,4})/e",  "'&#'.base_convert('\\1',16,10).';'", $text);
420
421         return $text;
422 }
423
424 function balanceTags( $text, $force = false ) {
425         if ( !$force && get_option('use_balanceTags') == 0 )
426                 return $text;
427         return force_balance_tags( $text );
428 }
429
430 /*
431  force_balance_tags
432
433  Balances Tags of string using a modified stack.
434
435  @param text      Text to be balanced
436  @param force     Forces balancing, ignoring the value of the option
437  @return          Returns balanced text
438  @author          Leonard Lin (leonard@acm.org)
439  @version         v1.1
440  @date            November 4, 2001
441  @license         GPL v2.0
442  @notes
443  @changelog
444  ---  Modified by Scott Reilly (coffee2code) 02 Aug 2004
445         1.2  ***TODO*** Make better - change loop condition to $text
446         1.1  Fixed handling of append/stack pop order of end text
447              Added Cleaning Hooks
448         1.0  First Version
449 */
450 function force_balance_tags( $text ) {
451         $tagstack = array(); $stacksize = 0; $tagqueue = ''; $newtext = '';
452         $single_tags = array('br', 'hr', 'img', 'input'); //Known single-entity/self-closing tags
453         $nestable_tags = array('blockquote', 'div', 'span'); //Tags that can be immediately nested within themselves
454
455         # WP bug fix for comments - in case you REALLY meant to type '< !--'
456         $text = str_replace('< !--', '<    !--', $text);
457         # WP bug fix for LOVE <3 (and other situations with '<' before a number)
458         $text = preg_replace('#<([0-9]{1})#', '&lt;$1', $text);
459
460         while (preg_match("/<(\/?\w*)\s*([^>]*)>/",$text,$regex)) {
461                 $newtext .= $tagqueue;
462
463                 $i = strpos($text,$regex[0]);
464                 $l = strlen($regex[0]);
465
466                 // clear the shifter
467                 $tagqueue = '';
468                 // Pop or Push
469                 if ($regex[1][0] == "/") { // End Tag
470                         $tag = strtolower(substr($regex[1],1));
471                         // if too many closing tags
472                         if($stacksize <= 0) {
473                                 $tag = '';
474                                 //or close to be safe $tag = '/' . $tag;
475                         }
476                         // if stacktop value = tag close value then pop
477                         else if ($tagstack[$stacksize - 1] == $tag) { // found closing tag
478                                 $tag = '</' . $tag . '>'; // Close Tag
479                                 // Pop
480                                 array_pop ($tagstack);
481                                 $stacksize--;
482                         } else { // closing tag not at top, search for it
483                                 for ($j=$stacksize-1;$j>=0;$j--) {
484                                         if ($tagstack[$j] == $tag) {
485                                         // add tag to tagqueue
486                                                 for ($k=$stacksize-1;$k>=$j;$k--){
487                                                         $tagqueue .= '</' . array_pop ($tagstack) . '>';
488                                                         $stacksize--;
489                                                 }
490                                                 break;
491                                         }
492                                 }
493                                 $tag = '';
494                         }
495                 } else { // Begin Tag
496                         $tag = strtolower($regex[1]);
497
498                         // Tag Cleaning
499
500                         // If self-closing or '', don't do anything.
501                         if((substr($regex[2],-1) == '/') || ($tag == '')) {
502                         }
503                         // ElseIf it's a known single-entity tag but it doesn't close itself, do so
504                         elseif ( in_array($tag, $single_tags) ) {
505                                 $regex[2] .= '/';
506                         } else {        // Push the tag onto the stack
507                                 // If the top of the stack is the same as the tag we want to push, close previous tag
508                                 if (($stacksize > 0) && !in_array($tag, $nestable_tags) && ($tagstack[$stacksize - 1] == $tag)) {
509                                         $tagqueue = '</' . array_pop ($tagstack) . '>';
510                                         $stacksize--;
511                                 }
512                                 $stacksize = array_push ($tagstack, $tag);
513                         }
514
515                         // Attributes
516                         $attributes = $regex[2];
517                         if($attributes) {
518                                 $attributes = ' '.$attributes;
519                         }
520                         $tag = '<'.$tag.$attributes.'>';
521                         //If already queuing a close tag, then put this tag on, too
522                         if ($tagqueue) {
523                                 $tagqueue .= $tag;
524                                 $tag = '';
525                         }
526                 }
527                 $newtext .= substr($text,0,$i) . $tag;
528                 $text = substr($text,$i+$l);
529         }
530
531         // Clear Tag Queue
532         $newtext .= $tagqueue;
533
534         // Add Remaining text
535         $newtext .= $text;
536
537         // Empty Stack
538         while($x = array_pop($tagstack)) {
539                 $newtext .= '</' . $x . '>'; // Add remaining tags to close
540         }
541
542         // WP fix for the bug with HTML comments
543         $newtext = str_replace("< !--","<!--",$newtext);
544         $newtext = str_replace("<    !--","< !--",$newtext);
545
546         return $newtext;
547 }
548
549 function format_to_edit($content, $richedit = false) {
550         $content = apply_filters('format_to_edit', $content);
551         if (! $richedit )
552                 $content = htmlspecialchars($content);
553         return $content;
554 }
555
556 function format_to_post($content) {
557         global $wpdb;
558         $content = apply_filters('format_to_post', $content);
559         return $content;
560 }
561
562 function zeroise($number,$threshold) { // function to add leading zeros when necessary
563         return sprintf('%0'.$threshold.'s', $number);
564 }
565
566
567 function backslashit($string) {
568         $string = preg_replace('/^([0-9])/', '\\\\\\\\\1', $string);
569         $string = preg_replace('/([a-z])/i', '\\\\\1', $string);
570         return $string;
571 }
572
573 function trailingslashit($string) {
574         return untrailingslashit($string) . '/';
575 }
576
577 function untrailingslashit($string) {
578         return rtrim($string, '/');
579 }
580
581 function addslashes_gpc($gpc) {
582         global $wpdb;
583
584         if (get_magic_quotes_gpc()) {
585                 $gpc = stripslashes($gpc);
586         }
587
588         return $wpdb->escape($gpc);
589 }
590
591
592 function stripslashes_deep($value) {
593          $value = is_array($value) ?
594                  array_map('stripslashes_deep', $value) :
595                  stripslashes($value);
596
597          return $value;
598 }
599
600 function urlencode_deep($value) {
601          $value = is_array($value) ?
602                  array_map('urlencode_deep', $value) :
603                  urlencode($value);
604
605          return $value;
606 }
607
608 function antispambot($emailaddy, $mailto=0) {
609         $emailNOSPAMaddy = '';
610         srand ((float) microtime() * 1000000);
611         for ($i = 0; $i < strlen($emailaddy); $i = $i + 1) {
612                 $j = floor(rand(0, 1+$mailto));
613                 if ($j==0) {
614                         $emailNOSPAMaddy .= '&#'.ord(substr($emailaddy,$i,1)).';';
615                 } elseif ($j==1) {
616                         $emailNOSPAMaddy .= substr($emailaddy,$i,1);
617                 } elseif ($j==2) {
618                         $emailNOSPAMaddy .= '%'.zeroise(dechex(ord(substr($emailaddy, $i, 1))), 2);
619                 }
620         }
621         $emailNOSPAMaddy = str_replace('@','&#64;',$emailNOSPAMaddy);
622         return $emailNOSPAMaddy;
623 }
624
625 function _make_url_clickable_cb($matches) {
626         $url = $matches[2];
627         $url = clean_url($url);
628         if ( empty($url) )
629                 return $matches[0];
630         return $matches[1] . "<a href=\"$url\" rel=\"nofollow\">$url</a>";
631 }
632
633 function _make_web_ftp_clickable_cb($matches) {
634         $dest = $matches[2];
635         $dest = 'http://' . $dest;
636         $dest = clean_url($dest);
637         if ( empty($dest) )
638                 return $matches[0];
639
640         return $matches[1] . "<a href=\"$dest\" rel=\"nofollow\">$dest</a>";
641 }
642
643 function _make_email_clickable_cb($matches) {
644         $email = $matches[2] . '@' . $matches[3];
645         return $matches[1] . "<a href=\"mailto:$email\">$email</a>";
646 }
647
648 function make_clickable($ret) {
649         $ret = ' ' . $ret;
650         // in testing, using arrays here was found to be faster
651         $ret = preg_replace_callback('#([\s>])([\w]+?://[\w\#$%&~/.\-;:=,?@\[\]+]*)#is', '_make_url_clickable_cb', $ret);
652         $ret = preg_replace_callback('#([\s>])((www|ftp)\.[\w\#$%&~/.\-;:=,?@\[\]+]*)#is', '_make_web_ftp_clickable_cb', $ret);
653         $ret = preg_replace_callback('#([\s>])([.0-9a-z_+-]+)@(([0-9a-z-]+\.)+[0-9a-z]{2,})#i', '_make_email_clickable_cb', $ret);
654         // this one is not in an array because we need it to run last, for cleanup of accidental links within links
655         $ret = preg_replace("#(<a( [^>]+?>|>))<a [^>]+?>([^>]+?)</a></a>#i", "$1$3</a>", $ret);
656         $ret = trim($ret);
657         return $ret;
658 }
659
660 function wp_rel_nofollow( $text ) {
661         global $wpdb;
662         // This is a pre save filter, so text is already escaped.
663         $text = stripslashes($text);
664         $text = preg_replace_callback('|<a (.+?)>|i', 'wp_rel_nofollow_callback', $text);
665         $text = $wpdb->escape($text);
666         return $text;
667 }
668
669 function wp_rel_nofollow_callback( $matches ) {
670         $text = $matches[1];
671         $text = str_replace(array(' rel="nofollow"', " rel='nofollow'"), '', $text);
672         return "<a $text rel=\"nofollow\">";
673 }
674
675 function convert_smilies($text) {
676         global $wp_smiliessearch, $wp_smiliesreplace;
677     $output = '';
678         if (get_option('use_smilies')) {
679                 // HTML loop taken from texturize function, could possible be consolidated
680                 $textarr = preg_split("/(<.*>)/U", $text, -1, PREG_SPLIT_DELIM_CAPTURE); // capture the tags as well as in between
681                 $stop = count($textarr);// loop stuff
682                 for ($i = 0; $i < $stop; $i++) {
683                         $content = $textarr[$i];
684                         if ((strlen($content) > 0) && ('<' != $content{0})) { // If it's not a tag
685                                 $content = preg_replace($wp_smiliessearch, $wp_smiliesreplace, $content);
686                         }
687                         $output .= $content;
688                 }
689         } else {
690                 // return default text.
691                 $output = $text;
692         }
693         return $output;
694 }
695
696
697 function is_email($user_email) {
698         $chars = "/^([a-z0-9+_]|\\-|\\.)+@(([a-z0-9_]|\\-)+\\.)+[a-z]{2,6}\$/i";
699         if (strpos($user_email, '@') !== false && strpos($user_email, '.') !== false) {
700                 if (preg_match($chars, $user_email)) {
701                         return true;
702                 } else {
703                         return false;
704                 }
705         } else {
706                 return false;
707         }
708 }
709
710 // used by wp-mail to handle charsets in email subjects
711 function wp_iso_descrambler($string) {
712   /* this may only work with iso-8859-1, I'm afraid */
713   if (!preg_match('#\=\?(.+)\?Q\?(.+)\?\=#i', $string, $matches)) {
714     return $string;
715   } else {
716     $subject = str_replace('_', ' ', $matches[2]);
717     $subject = preg_replace('#\=([0-9a-f]{2})#ei', "chr(hexdec(strtolower('$1')))", $subject);
718     return $subject;
719   }
720 }
721
722
723 // give it a date, it will give you the same date as GMT
724 function get_gmt_from_date($string) {
725   // note: this only substracts $time_difference from the given date
726   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);
727   $string_time = gmmktime($matches[4], $matches[5], $matches[6], $matches[2], $matches[3], $matches[1]);
728   $string_gmt = gmdate('Y-m-d H:i:s', $string_time - get_option('gmt_offset') * 3600);
729   return $string_gmt;
730 }
731
732 // give it a GMT date, it will give you the same date with $time_difference added
733 function get_date_from_gmt($string) {
734   // note: this only adds $time_difference to the given date
735   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);
736   $string_time = gmmktime($matches[4], $matches[5], $matches[6], $matches[2], $matches[3], $matches[1]);
737   $string_localtime = gmdate('Y-m-d H:i:s', $string_time + get_option('gmt_offset')*3600);
738   return $string_localtime;
739 }
740
741 // computes an offset in seconds from an iso8601 timezone
742 function iso8601_timezone_to_offset($timezone) {
743   // $timezone is either 'Z' or '[+|-]hhmm'
744   if ($timezone == 'Z') {
745     $offset = 0;
746   } else {
747     $sign    = (substr($timezone, 0, 1) == '+') ? 1 : -1;
748     $hours   = intval(substr($timezone, 1, 2));
749     $minutes = intval(substr($timezone, 3, 4)) / 60;
750     $offset  = $sign * 3600 * ($hours + $minutes);
751   }
752   return $offset;
753 }
754
755 // converts an iso8601 date to MySQL DateTime format used by post_date[_gmt]
756 function iso8601_to_datetime($date_string, $timezone = USER) {
757   if ($timezone == GMT) {
758     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);
759     if (!empty($date_bits[7])) { // we have a timezone, so let's compute an offset
760       $offset = iso8601_timezone_to_offset($date_bits[7]);
761     } else { // we don't have a timezone, so we assume user local timezone (not server's!)
762       $offset = 3600 * get_option('gmt_offset');
763     }
764     $timestamp = gmmktime($date_bits[4], $date_bits[5], $date_bits[6], $date_bits[2], $date_bits[3], $date_bits[1]);
765     $timestamp -= $offset;
766     return gmdate('Y-m-d H:i:s', $timestamp);
767   } elseif ($timezone == USER) {
768     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);
769   }
770 }
771
772 function popuplinks($text) {
773         // Comment text in popup windows should be filtered through this.
774         // Right now it's a moderately dumb function, ideally it would detect whether
775         // a target or rel attribute was already there and adjust its actions accordingly.
776         $text = preg_replace('/<a (.+?)>/i', "<a $1 target='_blank' rel='external'>", $text);
777         return $text;
778 }
779
780 function sanitize_email($email) {
781         return preg_replace('/[^a-z0-9+_.@-]/i', '', $email);
782 }
783
784 function human_time_diff( $from, $to = '' ) {
785         if ( empty($to) )
786                 $to = time();
787         $diff = (int) abs($to - $from);
788         if ($diff <= 3600) {
789                 $mins = round($diff / 60);
790                 if ($mins <= 1) {
791                         $mins = 1;
792                 }
793                 $since = sprintf(__ngettext('%s min', '%s mins', $mins), $mins);
794         } else if (($diff <= 86400) && ($diff > 3600)) {
795                 $hours = round($diff / 3600);
796                 if ($hours <= 1) {
797                         $hour = 1;
798                 }
799                 $since = sprintf(__ngettext('%s hour', '%s hours', $hours), $hours);
800         } elseif ($diff >= 86400) {
801                 $days = round($diff / 86400);
802                 if ($days <= 1) {
803                         $days = 1;
804                 }
805                 $since = sprintf(__ngettext('%s day', '%s days', $days), $days);
806         }
807         return $since;
808 }
809
810 function wp_trim_excerpt($text) { // Fakes an excerpt if needed
811         global $post;
812         if ( '' == $text ) {
813                 $text = get_the_content('');
814                 $text = apply_filters('the_content', $text);
815                 $text = str_replace(']]>', ']]&gt;', $text);
816                 $text = strip_tags($text);
817                 $excerpt_length = 55;
818                 $words = explode(' ', $text, $excerpt_length + 1);
819                 if (count($words) > $excerpt_length) {
820                         array_pop($words);
821                         array_push($words, '[...]');
822                         $text = implode(' ', $words);
823                 }
824         }
825         return $text;
826 }
827
828 function ent2ncr($text) {
829         $to_ncr = array(
830                 '&quot;' => '&#34;',
831                 '&amp;' => '&#38;',
832                 '&frasl;' => '&#47;',
833                 '&lt;' => '&#60;',
834                 '&gt;' => '&#62;',
835                 '|' => '&#124;',
836                 '&nbsp;' => '&#160;',
837                 '&iexcl;' => '&#161;',
838                 '&cent;' => '&#162;',
839                 '&pound;' => '&#163;',
840                 '&curren;' => '&#164;',
841                 '&yen;' => '&#165;',
842                 '&brvbar;' => '&#166;',
843                 '&brkbar;' => '&#166;',
844                 '&sect;' => '&#167;',
845                 '&uml;' => '&#168;',
846                 '&die;' => '&#168;',
847                 '&copy;' => '&#169;',
848                 '&ordf;' => '&#170;',
849                 '&laquo;' => '&#171;',
850                 '&not;' => '&#172;',
851                 '&shy;' => '&#173;',
852                 '&reg;' => '&#174;',
853                 '&macr;' => '&#175;',
854                 '&hibar;' => '&#175;',
855                 '&deg;' => '&#176;',
856                 '&plusmn;' => '&#177;',
857                 '&sup2;' => '&#178;',
858                 '&sup3;' => '&#179;',
859                 '&acute;' => '&#180;',
860                 '&micro;' => '&#181;',
861                 '&para;' => '&#182;',
862                 '&middot;' => '&#183;',
863                 '&cedil;' => '&#184;',
864                 '&sup1;' => '&#185;',
865                 '&ordm;' => '&#186;',
866                 '&raquo;' => '&#187;',
867                 '&frac14;' => '&#188;',
868                 '&frac12;' => '&#189;',
869                 '&frac34;' => '&#190;',
870                 '&iquest;' => '&#191;',
871                 '&Agrave;' => '&#192;',
872                 '&Aacute;' => '&#193;',
873                 '&Acirc;' => '&#194;',
874                 '&Atilde;' => '&#195;',
875                 '&Auml;' => '&#196;',
876                 '&Aring;' => '&#197;',
877                 '&AElig;' => '&#198;',
878                 '&Ccedil;' => '&#199;',
879                 '&Egrave;' => '&#200;',
880                 '&Eacute;' => '&#201;',
881                 '&Ecirc;' => '&#202;',
882                 '&Euml;' => '&#203;',
883                 '&Igrave;' => '&#204;',
884                 '&Iacute;' => '&#205;',
885                 '&Icirc;' => '&#206;',
886                 '&Iuml;' => '&#207;',
887                 '&ETH;' => '&#208;',
888                 '&Ntilde;' => '&#209;',
889                 '&Ograve;' => '&#210;',
890                 '&Oacute;' => '&#211;',
891                 '&Ocirc;' => '&#212;',
892                 '&Otilde;' => '&#213;',
893                 '&Ouml;' => '&#214;',
894                 '&times;' => '&#215;',
895                 '&Oslash;' => '&#216;',
896                 '&Ugrave;' => '&#217;',
897                 '&Uacute;' => '&#218;',
898                 '&Ucirc;' => '&#219;',
899                 '&Uuml;' => '&#220;',
900                 '&Yacute;' => '&#221;',
901                 '&THORN;' => '&#222;',
902                 '&szlig;' => '&#223;',
903                 '&agrave;' => '&#224;',
904                 '&aacute;' => '&#225;',
905                 '&acirc;' => '&#226;',
906                 '&atilde;' => '&#227;',
907                 '&auml;' => '&#228;',
908                 '&aring;' => '&#229;',
909                 '&aelig;' => '&#230;',
910                 '&ccedil;' => '&#231;',
911                 '&egrave;' => '&#232;',
912                 '&eacute;' => '&#233;',
913                 '&ecirc;' => '&#234;',
914                 '&euml;' => '&#235;',
915                 '&igrave;' => '&#236;',
916                 '&iacute;' => '&#237;',
917                 '&icirc;' => '&#238;',
918                 '&iuml;' => '&#239;',
919                 '&eth;' => '&#240;',
920                 '&ntilde;' => '&#241;',
921                 '&ograve;' => '&#242;',
922                 '&oacute;' => '&#243;',
923                 '&ocirc;' => '&#244;',
924                 '&otilde;' => '&#245;',
925                 '&ouml;' => '&#246;',
926                 '&divide;' => '&#247;',
927                 '&oslash;' => '&#248;',
928                 '&ugrave;' => '&#249;',
929                 '&uacute;' => '&#250;',
930                 '&ucirc;' => '&#251;',
931                 '&uuml;' => '&#252;',
932                 '&yacute;' => '&#253;',
933                 '&thorn;' => '&#254;',
934                 '&yuml;' => '&#255;',
935                 '&OElig;' => '&#338;',
936                 '&oelig;' => '&#339;',
937                 '&Scaron;' => '&#352;',
938                 '&scaron;' => '&#353;',
939                 '&Yuml;' => '&#376;',
940                 '&fnof;' => '&#402;',
941                 '&circ;' => '&#710;',
942                 '&tilde;' => '&#732;',
943                 '&Alpha;' => '&#913;',
944                 '&Beta;' => '&#914;',
945                 '&Gamma;' => '&#915;',
946                 '&Delta;' => '&#916;',
947                 '&Epsilon;' => '&#917;',
948                 '&Zeta;' => '&#918;',
949                 '&Eta;' => '&#919;',
950                 '&Theta;' => '&#920;',
951                 '&Iota;' => '&#921;',
952                 '&Kappa;' => '&#922;',
953                 '&Lambda;' => '&#923;',
954                 '&Mu;' => '&#924;',
955                 '&Nu;' => '&#925;',
956                 '&Xi;' => '&#926;',
957                 '&Omicron;' => '&#927;',
958                 '&Pi;' => '&#928;',
959                 '&Rho;' => '&#929;',
960                 '&Sigma;' => '&#931;',
961                 '&Tau;' => '&#932;',
962                 '&Upsilon;' => '&#933;',
963                 '&Phi;' => '&#934;',
964                 '&Chi;' => '&#935;',
965                 '&Psi;' => '&#936;',
966                 '&Omega;' => '&#937;',
967                 '&alpha;' => '&#945;',
968                 '&beta;' => '&#946;',
969                 '&gamma;' => '&#947;',
970                 '&delta;' => '&#948;',
971                 '&epsilon;' => '&#949;',
972                 '&zeta;' => '&#950;',
973                 '&eta;' => '&#951;',
974                 '&theta;' => '&#952;',
975                 '&iota;' => '&#953;',
976                 '&kappa;' => '&#954;',
977                 '&lambda;' => '&#955;',
978                 '&mu;' => '&#956;',
979                 '&nu;' => '&#957;',
980                 '&xi;' => '&#958;',
981                 '&omicron;' => '&#959;',
982                 '&pi;' => '&#960;',
983                 '&rho;' => '&#961;',
984                 '&sigmaf;' => '&#962;',
985                 '&sigma;' => '&#963;',
986                 '&tau;' => '&#964;',
987                 '&upsilon;' => '&#965;',
988                 '&phi;' => '&#966;',
989                 '&chi;' => '&#967;',
990                 '&psi;' => '&#968;',
991                 '&omega;' => '&#969;',
992                 '&thetasym;' => '&#977;',
993                 '&upsih;' => '&#978;',
994                 '&piv;' => '&#982;',
995                 '&ensp;' => '&#8194;',
996                 '&emsp;' => '&#8195;',
997                 '&thinsp;' => '&#8201;',
998                 '&zwnj;' => '&#8204;',
999                 '&zwj;' => '&#8205;',
1000                 '&lrm;' => '&#8206;',
1001                 '&rlm;' => '&#8207;',
1002                 '&ndash;' => '&#8211;',
1003                 '&mdash;' => '&#8212;',
1004                 '&lsquo;' => '&#8216;',
1005                 '&rsquo;' => '&#8217;',
1006                 '&sbquo;' => '&#8218;',
1007                 '&ldquo;' => '&#8220;',
1008                 '&rdquo;' => '&#8221;',
1009                 '&bdquo;' => '&#8222;',
1010                 '&dagger;' => '&#8224;',
1011                 '&Dagger;' => '&#8225;',
1012                 '&bull;' => '&#8226;',
1013                 '&hellip;' => '&#8230;',
1014                 '&permil;' => '&#8240;',
1015                 '&prime;' => '&#8242;',
1016                 '&Prime;' => '&#8243;',
1017                 '&lsaquo;' => '&#8249;',
1018                 '&rsaquo;' => '&#8250;',
1019                 '&oline;' => '&#8254;',
1020                 '&frasl;' => '&#8260;',
1021                 '&euro;' => '&#8364;',
1022                 '&image;' => '&#8465;',
1023                 '&weierp;' => '&#8472;',
1024                 '&real;' => '&#8476;',
1025                 '&trade;' => '&#8482;',
1026                 '&alefsym;' => '&#8501;',
1027                 '&crarr;' => '&#8629;',
1028                 '&lArr;' => '&#8656;',
1029                 '&uArr;' => '&#8657;',
1030                 '&rArr;' => '&#8658;',
1031                 '&dArr;' => '&#8659;',
1032                 '&hArr;' => '&#8660;',
1033                 '&forall;' => '&#8704;',
1034                 '&part;' => '&#8706;',
1035                 '&exist;' => '&#8707;',
1036                 '&empty;' => '&#8709;',
1037                 '&nabla;' => '&#8711;',
1038                 '&isin;' => '&#8712;',
1039                 '&notin;' => '&#8713;',
1040                 '&ni;' => '&#8715;',
1041                 '&prod;' => '&#8719;',
1042                 '&sum;' => '&#8721;',
1043                 '&minus;' => '&#8722;',
1044                 '&lowast;' => '&#8727;',
1045                 '&radic;' => '&#8730;',
1046                 '&prop;' => '&#8733;',
1047                 '&infin;' => '&#8734;',
1048                 '&ang;' => '&#8736;',
1049                 '&and;' => '&#8743;',
1050                 '&or;' => '&#8744;',
1051                 '&cap;' => '&#8745;',
1052                 '&cup;' => '&#8746;',
1053                 '&int;' => '&#8747;',
1054                 '&there4;' => '&#8756;',
1055                 '&sim;' => '&#8764;',
1056                 '&cong;' => '&#8773;',
1057                 '&asymp;' => '&#8776;',
1058                 '&ne;' => '&#8800;',
1059                 '&equiv;' => '&#8801;',
1060                 '&le;' => '&#8804;',
1061                 '&ge;' => '&#8805;',
1062                 '&sub;' => '&#8834;',
1063                 '&sup;' => '&#8835;',
1064                 '&nsub;' => '&#8836;',
1065                 '&sube;' => '&#8838;',
1066                 '&supe;' => '&#8839;',
1067                 '&oplus;' => '&#8853;',
1068                 '&otimes;' => '&#8855;',
1069                 '&perp;' => '&#8869;',
1070                 '&sdot;' => '&#8901;',
1071                 '&lceil;' => '&#8968;',
1072                 '&rceil;' => '&#8969;',
1073                 '&lfloor;' => '&#8970;',
1074                 '&rfloor;' => '&#8971;',
1075                 '&lang;' => '&#9001;',
1076                 '&rang;' => '&#9002;',
1077                 '&larr;' => '&#8592;',
1078                 '&uarr;' => '&#8593;',
1079                 '&rarr;' => '&#8594;',
1080                 '&darr;' => '&#8595;',
1081                 '&harr;' => '&#8596;',
1082                 '&loz;' => '&#9674;',
1083                 '&spades;' => '&#9824;',
1084                 '&clubs;' => '&#9827;',
1085                 '&hearts;' => '&#9829;',
1086                 '&diams;' => '&#9830;'
1087         );
1088
1089         return str_replace( array_keys($to_ncr), array_values($to_ncr), $text );
1090 }
1091
1092 function wp_richedit_pre($text) {
1093         // Filtering a blank results in an annoying <br />\n
1094         if ( empty($text) ) return apply_filters('richedit_pre', '');
1095
1096         $output = $text;
1097         $output = convert_chars($output);
1098         $output = wpautop($output);
1099
1100         // These must be double-escaped or planets will collide.
1101         $output = str_replace('&lt;', '&amp;lt;', $output);
1102         $output = str_replace('&gt;', '&amp;gt;', $output);
1103
1104         return apply_filters('richedit_pre', $output);
1105 }
1106
1107 function clean_url( $url, $protocols = null, $context = 'display' ) {
1108         $original_url = $url;
1109
1110         if ('' == $url) return $url;
1111         $url = preg_replace('|[^a-z0-9-~+_.?#=!&;,/:%@]|i', '', $url);
1112         $strip = array('%0d', '%0a');
1113         $url = str_replace($strip, '', $url);
1114         $url = str_replace(';//', '://', $url);
1115         /* If the URL doesn't appear to contain a scheme, we
1116          * presume it needs http:// appended (unless a relative
1117          * link starting with / or a php file).
1118         */
1119         if ( strpos($url, ':') === false &&
1120                 substr( $url, 0, 1 ) != '/' && !preg_match('/^[a-z0-9-]+?\.php/i', $url) )
1121                 $url = 'http://' . $url;
1122
1123         // Replace ampersands ony when displaying.
1124         if ( 'display' == $context )
1125                 $url = preg_replace('/&([^#])(?![a-z]{2,8};)/', '&#038;$1', $url);
1126
1127         if ( !is_array($protocols) )
1128                 $protocols = array('http', 'https', 'ftp', 'ftps', 'mailto', 'news', 'irc', 'gopher', 'nntp', 'feed', 'telnet');
1129         if ( wp_kses_bad_protocol( $url, $protocols ) != $url )
1130                 return '';
1131
1132         return apply_filters('clean_url', $url, $original_url, $context);
1133 }
1134
1135 function sanitize_url( $url, $protocols = null ) {
1136         return clean_url( $url, $protocols, 'db');
1137 }
1138
1139 // Borrowed from the PHP Manual user notes. Convert entities, while
1140 // preserving already-encoded entities:
1141 function htmlentities2($myHTML) {
1142         $translation_table=get_html_translation_table (HTML_ENTITIES,ENT_QUOTES);
1143         $translation_table[chr(38)] = '&';
1144         return preg_replace("/&(?![A-Za-z]{0,4}\w{2,3};|#[0-9]{2,3};)/","&amp;" , strtr($myHTML, $translation_table));
1145 }
1146
1147 // Escape single quotes, specialchar double quotes, and fix line endings.
1148 function js_escape($text) {
1149         $safe_text = wp_specialchars($text, 'double');
1150         $safe_text = preg_replace('/&#(x)?0*(?(1)27|39);?/i', "'", stripslashes($safe_text));
1151         $safe_text = preg_replace("/\r?\n/", "\\n", addslashes($safe_text));
1152         return apply_filters('js_escape', $safe_text, $text);
1153 }
1154
1155 // Escaping for HTML attributes
1156 function attribute_escape($text) {
1157         $safe_text = wp_specialchars($text, true);
1158         return apply_filters('attribute_escape', $safe_text, $text);
1159 }
1160
1161 function wp_make_link_relative( $link ) {
1162         return preg_replace('|https?://[^/]+(/.*)|i', '$1', $link );
1163 }
1164
1165 function sanitize_option($option, $value) { // Remember to call stripslashes!
1166
1167         switch ($option) {
1168                 case 'admin_email':
1169                         $value = sanitize_email($value);
1170                         break;
1171
1172                 case 'default_post_edit_rows':
1173                 case 'mailserver_port':
1174                 case 'comment_max_links':
1175                 case 'page_on_front':
1176                 case 'rss_excerpt_length':
1177                 case 'default_category':
1178                 case 'default_email_category':
1179                 case 'default_link_category':
1180                         $value = abs((int) $value);
1181                         break;
1182
1183                 case 'posts_per_page':
1184                 case 'posts_per_rss':
1185                         $value = (int) $value;
1186                         if ( empty($value) ) $value = 1;
1187                         if ( $value < -1 ) $value = abs($value);
1188                         break;
1189
1190                 case 'default_ping_status':
1191                 case 'default_comment_status':
1192                         // Options that if not there have 0 value but need to be something like "closed"
1193                         if ( $value == '0' || $value == '')
1194                                 $value = 'closed';
1195                         break;
1196
1197                 case 'blogdescription':
1198                 case 'blogname':
1199                         $value = addslashes($value);
1200                         $value = wp_filter_post_kses( $value ); // calls stripslashes then addslashes
1201                         $value = stripslashes($value);
1202                         $value = wp_specialchars( $value );
1203                         break;
1204
1205                 case 'blog_charset':
1206                         $value = preg_replace('/[^a-zA-Z0-9_-]/', '', $value); // strips slashes
1207                         break;
1208
1209                 case 'date_format':
1210                 case 'time_format':
1211                 case 'mailserver_url':
1212                 case 'mailserver_login':
1213                 case 'mailserver_pass':
1214                 case 'ping_sites':
1215                 case 'upload_path':
1216                         $value = strip_tags($value);
1217                         $value = addslashes($value);
1218                         $value = wp_filter_kses($value); // calls stripslashes then addslashes
1219                         $value = stripslashes($value);
1220                         break;
1221
1222                 case 'gmt_offset':
1223                         $value = preg_replace('/[^0-9:.-]/', '', $value); // strips slashes
1224                         break;
1225
1226                 case 'siteurl':
1227                 case 'home':
1228                         $value = stripslashes($value);
1229                         $value = clean_url($value);
1230                         break;
1231                 default :
1232                         $value = apply_filters("sanitize_option_{$option}", $value, $option);
1233                         break;
1234         }
1235
1236         return $value;
1237 }
1238
1239 function wp_parse_str( $string, &$array ) {
1240         parse_str( $string, $array );
1241         if ( get_magic_quotes_gpc() )
1242                 $array = stripslashes_deep( $array ); // parse_str() adds slashes if magicquotes is on.  See: http://php.net/parse_str
1243         $array = apply_filters( 'wp_parse_str', $array );
1244 }
1245
1246 // Convert lone less than signs.  KSES already converts lone greater than signs.
1247 function wp_pre_kses_less_than( $text ) {
1248         return preg_replace_callback('%<[^>]*?((?=<)|>|$)%', 'wp_pre_kses_less_than_callback', $text);
1249 }
1250
1251 function wp_pre_kses_less_than_callback( $matches ) {
1252         if ( false === strpos($matches[0], '>') )
1253                 return wp_specialchars($matches[0]);
1254         return $matches[0];
1255 }
1256
1257 ?>