PageRenderTime 59ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

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

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