[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-forums/bbpress/bb-includes/ -> functions.bb-formatting.php (source)

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


Generated: Thu Dec 7 01:01:35 2017 Cross-referenced by PHPXref 0.7.1