PageRenderTime 61ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/2.3.3/wp-includes/formatting.php

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