PageRenderTime 66ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

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

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