PageRenderTime 71ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

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

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