PageRenderTime 35ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/buddypress/bp-forums/bbpress/bb-includes/functions.bb-formatting.php

https://bitbucket.org/codemen_iftekhar/codemen
PHP | 405 lines | 392 code | 8 blank | 5 comment | 4 complexity | 7a29c7fb6db61146ca69a80a82fab451 MD5 | raw file
  1. <?php
  2. function bb_autop($pee, $br = 1) { // Reduced to be faster
  3. $pee = $pee . "\n"; // just to make things a little easier, pad the end
  4. $pee = preg_replace('|<br />\s*<br />|', "\n\n", $pee);
  5. // Space things out a little
  6. $pee = preg_replace('!(<(?:ul|ol|li|blockquote|pre|p)[^>]*>)!', "\n$1", $pee);
  7. $pee = preg_replace('!(</(?:ul|ol|li|blockquote|pre|p)>)!', "$1\n", $pee);
  8. $pee = str_replace(array("\r\n", "\r"), "\n", $pee); // cross-platform newlines
  9. $pee = preg_replace("/\n\n+/", "\n\n", $pee); // take care of duplicates
  10. $pee = preg_replace('/\n?(.+?)(?:\n\s*\n|\z)/s', "<p>$1</p>\n", $pee); // make paragraphs, including one at the end
  11. $pee = preg_replace('|<p>\s*?</p>|', '', $pee); // under certain strange conditions it could create a P of entirely whitespace
  12. $pee = preg_replace('!<p>\s*(</?(?:ul|ol|li|blockquote|p)[^>]*>)\s*</p>!', "$1", $pee); // don't pee all over a tag
  13. $pee = preg_replace("|<p>(<li.+?)</p>|", "$1", $pee); // problem with nested lists
  14. $pee = preg_replace('|<p><blockquote([^>]*)>|i', "<blockquote$1><p>", $pee);
  15. $pee = str_replace('</blockquote></p>', '</p></blockquote>', $pee);
  16. $pee = preg_replace('!<p>\s*(</?(?:ul|ol|li|blockquote|p)[^>]*>)!', "$1", $pee);
  17. $pee = preg_replace('!(</?(?:ul|ol|li|blockquote|p)[^>]*>)\s*</p>!', "$1", $pee);
  18. if ($br) $pee = preg_replace('|(?<!<br />)\s*\n|', "<br />\n", $pee); // optionally make line breaks
  19. $pee = preg_replace('!(</?(?:ul|ol|li|blockquote|p)[^>]*>)\s*<br />!', "$1", $pee);
  20. $pee = preg_replace('!<br />(\s*</?(?:p|li|ul|ol)>)!', '$1', $pee);
  21. if ( false !== strpos( $pee, '<pre' ) )
  22. $pee = preg_replace_callback('!(<pre.*?>)(.*?)</pre>!is', 'clean_pre', $pee );
  23. return $pee;
  24. }
  25. function bb_encodeit( $matches ) {
  26. $text = trim($matches[2]);
  27. $text = htmlspecialchars($text, ENT_QUOTES);
  28. $text = str_replace(array("\r\n", "\r"), "\n", $text);
  29. $text = preg_replace("|\n\n\n+|", "\n\n", $text);
  30. $text = str_replace('&amp;amp;', '&amp;', $text);
  31. $text = str_replace('&amp;lt;', '&lt;', $text);
  32. $text = str_replace('&amp;gt;', '&gt;', $text);
  33. $text = "<code>$text</code>";
  34. if ( "`" != $matches[1] )
  35. $text = "<pre>$text</pre>";
  36. return $text;
  37. }
  38. function bb_decodeit( $matches ) {
  39. $text = $matches[2];
  40. $trans_table = array_flip(get_html_translation_table(HTML_ENTITIES));
  41. $text = strtr($text, $trans_table);
  42. $text = str_replace('<br />', '<coded_br />', $text);
  43. $text = str_replace('<p>', '<coded_p>', $text);
  44. $text = str_replace('</p>', '</coded_p>', $text);
  45. $text = str_replace(array('&#38;','&amp;'), '&', $text);
  46. $text = str_replace('&#39;', "'", $text);
  47. if ( '<pre><code>' == $matches[1] )
  48. $text = "\n$text\n";
  49. return "`$text`";
  50. }
  51. function bb_code_trick( $text ) {
  52. $text = str_replace(array("\r\n", "\r"), "\n", $text);
  53. $text = preg_replace_callback("|(`)(.*?)`|", 'bb_encodeit', $text);
  54. $text = preg_replace_callback("!(^|\n)`(.*?)`!s", 'bb_encodeit', $text);
  55. return $text;
  56. }
  57. function bb_code_trick_reverse( $text ) {
  58. $text = preg_replace_callback("!(<pre><code>|<code>)(.*?)(</code></pre>|</code>)!s", 'bb_decodeit', $text);
  59. $text = str_replace(array('<p>', '<br />'), '', $text);
  60. $text = str_replace('</p>', "\n", $text);
  61. $text = str_replace('<coded_br />', '<br />', $text);
  62. $text = str_replace('<coded_p>', '<p>', $text);
  63. $text = str_replace('</coded_p>', '</p>', $text);
  64. return $text;
  65. }
  66. function _bb_encode_bad_empty(&$text, $key, $preg) {
  67. if (strpos($text, '`') !== 0)
  68. $text = preg_replace("|&lt;($preg)\s*?/*?&gt;|i", '<$1 />', $text);
  69. }
  70. function _bb_encode_bad_normal(&$text, $key, $preg) {
  71. if (strpos($text, '`') !== 0)
  72. $text = preg_replace("|&lt;(/?$preg)&gt;|i", '<$1>', $text);
  73. }
  74. function bb_encode_bad( $text ) {
  75. $text = wp_specialchars( $text, ENT_NOQUOTES );
  76. $text = preg_split('@(`[^`]*`)@m', $text, -1, PREG_SPLIT_NO_EMPTY + PREG_SPLIT_DELIM_CAPTURE);
  77. $allowed = bb_allowed_tags();
  78. $empty = array( 'br' => true, 'hr' => true, 'img' => true, 'input' => true, 'param' => true, 'area' => true, 'col' => true, 'embed' => true );
  79. foreach ( $allowed as $tag => $args ) {
  80. $preg = $args ? "$tag(?:\s.*?)?" : $tag;
  81. if ( isset( $empty[$tag] ) )
  82. array_walk($text, '_bb_encode_bad_empty', $preg);
  83. else
  84. array_walk($text, '_bb_encode_bad_normal', $preg);
  85. }
  86. return join('', $text);
  87. }
  88. function bb_filter_kses($data) {
  89. $allowedtags = bb_allowed_tags();
  90. return wp_kses($data, $allowedtags);
  91. }
  92. function bb_allowed_tags() {
  93. $tags = array(
  94. 'a' => array(
  95. 'href' => array(),
  96. 'title' => array(),
  97. 'rel' => array()),
  98. 'blockquote' => array('cite' => array()),
  99. 'br' => array(),
  100. 'code' => array(),
  101. 'pre' => array(),
  102. 'em' => array(),
  103. 'strong' => array(),
  104. 'ul' => array(),
  105. 'ol' => array(),
  106. 'li' => array()
  107. );
  108. return apply_filters( 'bb_allowed_tags', $tags );
  109. }
  110. function bb_rel_nofollow( $text ) {
  111. return preg_replace_callback('|<a (.+?)>|i', 'bb_rel_nofollow_callback', $text);
  112. }
  113. function bb_rel_nofollow_callback( $matches ) {
  114. $text = $matches[1];
  115. $text = str_replace(array(' rel="nofollow"', " rel='nofollow'"), '', $text);
  116. return "<a $text rel=\"nofollow\">";
  117. }
  118. // Should be able to take both escaped and unescaped data
  119. function bb_trim_for_db( $string, $length ) {
  120. $_string = $string;
  121. if ( seems_utf8( $string ) ) {
  122. $string = bb_utf8_cut( $string, $length );
  123. // if we have slashes at the end, make sure we have a reasonable number of them
  124. if ( preg_match( '#[^\\\\](\\\\+)$#', $string, $matches ) ) {
  125. $end = stripslashes($matches[1]);
  126. $end = addslashes($end);
  127. $string = trim( $string, '\\' ) . $end;
  128. }
  129. }
  130. return apply_filters( 'bb_trim_for_db', $string, $_string, $length );
  131. }
  132. // Reduce utf8 string to $length in single byte character equivalents without breaking multibyte characters
  133. function bb_utf8_cut( $utf8_string, $length = 0 ) {
  134. if ( $length < 1 )
  135. return $utf8_string;
  136. $unicode = '';
  137. $chars = array();
  138. $num_octets = 1;
  139. for ($i = 0; $i < strlen( $utf8_string ); $i++ ) {
  140. $value = ord( $utf8_string[ $i ] );
  141. if ( 128 > $value ) {
  142. if ( strlen($unicode) + 1 > $length )
  143. break;
  144. $unicode .= $utf8_string[ $i ];
  145. } else {
  146. if ( count( $chars ) == 0 )
  147. $num_octets = ( 224 > $value ) ? 2 : 3;
  148. $chars[] = $utf8_string[ $i ];
  149. if ( strlen($unicode) + $num_octets > $length )
  150. break;
  151. if ( count( $chars ) == $num_octets ) {
  152. $unicode .= join('', $chars);
  153. $chars = array();
  154. $num_octets = 1;
  155. }
  156. }
  157. }
  158. return $unicode;
  159. }
  160. function bb_encoded_utf8_cut( $encoded, $length = 0 ) {
  161. if ( $length < 1 )
  162. return $encoded;
  163. $r = '';
  164. $values = preg_split( '/(%[0-9a-f]{2})/i', $encoded, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY );;
  165. for ($i = 0; $i < count( $values ); $i += $num_octets ) {
  166. $num_octets = 1;
  167. if ( '%' != $values[$i][0] ) {
  168. $r .= $values[$i];
  169. if ( $length && strlen($r) > $length )
  170. return substr($r, 0, $length);
  171. } else {
  172. $value = hexdec(substr($values[$i], 1));
  173. if ( 1 == $num_octets )
  174. $num_octets = ( 224 > $value ) ? 2 : 3;
  175. if ( $length && ( strlen($r) + $num_octets * 3 ) > $length )
  176. return $r;
  177. $r .= $values[$i] . $values[$i + 1];
  178. if ( 3 == $num_octets )
  179. $r .= $values[$i + 2];
  180. }
  181. }
  182. return $r;
  183. }
  184. function bb_pre_term_slug( $slug, $taxonomy = '', $term_id = 0 ) {
  185. return bb_sanitize_with_dashes( $slug, 200 );
  186. }
  187. function bb_trim_for_db_55( $string ) {
  188. return bb_trim_for_db( $string, 55 );
  189. }
  190. function bb_trim_for_db_150( $string ) {
  191. return bb_trim_for_db( $string, 150 );
  192. }
  193. function bb_slug_sanitize( $slug, $length = 255 ) {
  194. $_slug = $slug;
  195. return apply_filters( 'bb_slug_sanitize', bb_sanitize_with_dashes( $slug, $length ), $_slug, $length );
  196. }
  197. function bb_user_nicename_sanitize( $user_nicename, $length = 50 ) {
  198. $_user_nicename = $user_nicename;
  199. return apply_filters( 'bb_user_nicename_sanitize', bb_sanitize_with_dashes( $user_nicename, $length ), $_user_nicename, $length );
  200. }
  201. function bb_sanitize_with_dashes( $text, $length = 0 ) { // Multibyte aware
  202. $_text = $text;
  203. $text = trim($text);
  204. $text = strip_tags($text);
  205. // Preserve escaped octets.
  206. $text = preg_replace('|%([a-fA-F0-9][a-fA-F0-9])|', '---$1---', $text);
  207. // Remove percent signs that are not part of an octet.
  208. $text = str_replace('%', '', $text);
  209. // Restore octets.
  210. $text = preg_replace('|---([a-fA-F0-9][a-fA-F0-9])---|', '%$1', $text);
  211. $text = apply_filters( 'pre_sanitize_with_dashes', $text, $_text, $length );
  212. $text = strtolower($text);
  213. $text = preg_replace('/&(^\x80-\xff)+?;/', '', $text); // kill entities
  214. $text = preg_replace('/[^%a-z0-9\x80-\xff _-]/', '', $text);
  215. $text = trim($text);
  216. $text = preg_replace('/\s+/', '-', $text);
  217. $text = preg_replace(array('|-+|', '|_+|'), array('-', '_'), $text); // Kill the repeats
  218. return $text;
  219. }
  220. function bb_pre_sanitize_with_dashes_utf8( $text, $_text = '', $length = 0 ) {
  221. $text = remove_accents($text);
  222. if ( seems_utf8( $text ) ) {
  223. if ( function_exists('mb_strtolower') )
  224. $text = mb_strtolower($text, 'UTF-8');
  225. $text = utf8_uri_encode( $text, $length );
  226. }
  227. return $text;
  228. }
  229. function bb_show_topic_context( $term, $text ) {
  230. $text = strip_tags( $text );
  231. $term = preg_quote( $term );
  232. $text = preg_replace( "|.*?(.{0,80})$term(.{0,80}).*|is", "$1<strong>$term</strong>$2", $text, 1 );
  233. $text = substr( $text, 0, 210 );
  234. return $text;
  235. }
  236. function bb_post_text_context( $post_text ) {
  237. return bb_show_context( $GLOBALS['q'], $post_text );
  238. }
  239. function bb_show_context( $term, $text ) {
  240. $text = strip_shortcodes( $text );
  241. $text = strip_tags( $text );
  242. $term = preg_quote( $term );
  243. $text = preg_replace( "|.*?(.{0,80})$term(.{0,80}).*|is", "... $1<strong>$term</strong>$2 ...", $text, 1 );
  244. $text = substr( $text, 0, 210 );
  245. return $text;
  246. }
  247. function bb_fix_link( $link ) {
  248. if ( false === strpos($link, '.') ) // these are usually random words
  249. return '';
  250. $link = wp_kses_no_null( $link );
  251. return esc_url( $link );
  252. }
  253. function bb_sticky_label( $label ) {
  254. global $topic;
  255. if (bb_is_front()) {
  256. if ( '2' === $topic->topic_sticky ) {
  257. return sprintf(__('[sticky] %s'), $label);
  258. }
  259. } else {
  260. if ( '1' === $topic->topic_sticky || '2' === $topic->topic_sticky ) {
  261. return sprintf(__('[sticky] %s'), $label);
  262. }
  263. }
  264. return $label;
  265. }
  266. function bb_closed_label( $label ) {
  267. global $topic;
  268. if ( '0' === $topic->topic_open )
  269. return sprintf(__('[closed] %s'), $label);
  270. return $label;
  271. }
  272. function bb_make_link_view_all( $link ) {
  273. return esc_html( add_query_arg( 'view', 'all', $link ) );
  274. }
  275. function bb_gmtstrtotime( $string ) {
  276. if ( is_numeric($string) )
  277. return $string;
  278. if ( !is_string($string) )
  279. return -1;
  280. if ( stristr($string, 'utc') || stristr($string, 'gmt') || stristr($string, '+0000') )
  281. return strtotime($string);
  282. if ( -1 == $time = strtotime($string . ' +0000') )
  283. return strtotime($string);
  284. return $time;
  285. }
  286. /**
  287. * Converts a number of characters from a string.
  288. *
  289. * Metadata tags <<title>> and <<category>> are removed, <<br>> and <<hr>> are
  290. * converted into correct XHTML and Unicode characters are converted to the
  291. * valid range.
  292. *
  293. * @param string $content String of characters to be converted.
  294. *
  295. * @return string Converted string.
  296. */
  297. function bb_convert_chars( $content ) {
  298. // Translation of invalid Unicode references range to valid range
  299. $wp_htmltranswinuni = array(
  300. '&#128;' => '&#8364;', // the Euro sign
  301. '&#129;' => '',
  302. '&#130;' => '&#8218;', // these are Windows CP1252 specific characters
  303. '&#131;' => '&#402;', // they would look weird on non-Windows browsers
  304. '&#132;' => '&#8222;',
  305. '&#133;' => '&#8230;',
  306. '&#134;' => '&#8224;',
  307. '&#135;' => '&#8225;',
  308. '&#136;' => '&#710;',
  309. '&#137;' => '&#8240;',
  310. '&#138;' => '&#352;',
  311. '&#139;' => '&#8249;',
  312. '&#140;' => '&#338;',
  313. '&#141;' => '',
  314. '&#142;' => '&#382;',
  315. '&#143;' => '',
  316. '&#144;' => '',
  317. '&#145;' => '&#8216;',
  318. '&#146;' => '&#8217;',
  319. '&#147;' => '&#8220;',
  320. '&#148;' => '&#8221;',
  321. '&#149;' => '&#8226;',
  322. '&#150;' => '&#8211;',
  323. '&#151;' => '&#8212;',
  324. '&#152;' => '&#732;',
  325. '&#153;' => '&#8482;',
  326. '&#154;' => '&#353;',
  327. '&#155;' => '&#8250;',
  328. '&#156;' => '&#339;',
  329. '&#157;' => '',
  330. '&#158;' => '',
  331. '&#159;' => '&#376;'
  332. );
  333. // Remove metadata tags
  334. $content = preg_replace( '/<title>(.+?)<\/title>/', '', $content );
  335. $content = preg_replace( '/<category>(.+?)<\/category>/', '', $content );
  336. // Converts lone & characters into &#38; (a.k.a. &amp;)
  337. $content = preg_replace( '/&([^#])(?![a-z1-4]{1,8};)/i', '&#038;$1', $content );
  338. // Fix Word pasting
  339. $content = strtr( $content, $wp_htmltranswinuni );
  340. // Just a little XHTML help
  341. $content = str_replace( '<br>', '<br />', $content );
  342. $content = str_replace( '<hr>', '<hr />', $content );
  343. return $content;
  344. }