PageRenderTime 65ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

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

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