Skip to:
Content

bbPress.org


Ignore:
Timestamp:
06/07/2007 07:04:40 AM (17 years ago)
Author:
mdawaffe
Message:

varchar(255) for slugs, trim multibyte data before inserting in db. Fixes #655

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/bb-includes/formatting-functions.php

    r839 r846  
    122122}
    123123
     124function bb_trim_for_db( $string, $length ) {
     125    if ( seems_utf8( $string ) )
     126        $_string = bb_utf8_cut( $string, $length );
     127    return apply_filters( 'bb_trim_for_db', $_string, $string, $length );
     128}
     129
    124130// Reduce utf8 string to $length in single byte character equivalents without breaking multibyte characters
    125 function bb_utf8_cut( $utf8_string, $length ) {
     131function bb_utf8_cut( $utf8_string, $length = 0 ) {
     132    if ( $length < 1 )
     133        return $utf8_string;
     134
    126135    $unicode = '';
    127136    $chars = array();
     
    154163}
    155164
    156 function bb_tag_sanitize( $tag ) {
     165function bb_encoded_utf8_cut( $encoded, $length = 0 ) {
     166    if ( $length < 1 )
     167        return $encoded;
     168
     169    $r = '';
     170    $values = preg_split( '/(%[0-9a-f]{2})/i', $encoded, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY );;
     171
     172    for ($i = 0; $i < count( $values ); $i += $num_octets ) {
     173        $num_octets = 1;
     174        if ( '%' != $values[$i][0] ) {
     175            $r .= $values[$i];
     176            if ( $length && strlen($r) > $length )
     177                return substr($r, 0, $length);
     178        } else {
     179            $value = hexdec(substr($values[$i], 1));
     180
     181            if ( 1 == $num_octets )
     182                $num_octets = $value < 224 ? 2 : 3;
     183
     184            if ( $length && ( strlen($r) + $num_octets * 3 ) > $length )
     185                return $r;
     186
     187            $r .= $values[$i] . $values[$i + 1];
     188            if ( 3 == $num_octets )
     189                $r .= $values[$i + 2];
     190        }
     191    }
     192
     193    return $r;
     194}
     195
     196function bb_tag_sanitize( $tag, $length = 200 ) {
    157197    $_tag = $tag;
    158     return apply_filters( 'bb_tag_sanitize', bb_sanitize_with_dashes( $tag ), $_tag );
    159 }
    160 
    161 function bb_slug_sanitize( $slug ) {
     198    return apply_filters( 'bb_tag_sanitize', bb_sanitize_with_dashes( $tag, $length ), $_tag, $length );
     199}
     200
     201function bb_slug_sanitize( $slug, $length = 255 ) {
    162202    $_slug = $slug;
    163     return apply_filters( 'bb_slug_sanitize', sanitize_with_dashes( $slug ), $_slug );
    164 }
    165 
    166 function bb_sanitize_with_dashes( $text, $length = 200 ) { // Multibyte aware
     203    return apply_filters( 'bb_slug_sanitize', bb_sanitize_with_dashes( $slug, $length ), $_slug, $length );
     204}
     205
     206function bb_sanitize_with_dashes( $text, $length = 0 ) { // Multibyte aware
    167207    $_text = $text;
    168208    $text = trim($text);
    169209    $text = strip_tags($text);
    170 
    171210    // Preserve escaped octets.
    172211    $text = preg_replace('|%([a-fA-F0-9][a-fA-F0-9])|', '---$1---', $text);
     
    176215    $text = preg_replace('|---([a-fA-F0-9][a-fA-F0-9])---|', '%$1', $text);
    177216
    178     $text = apply_filters( 'pre_sanitize_with_dashes', $text, $_text );
     217    $text = apply_filters( 'pre_sanitize_with_dashes', $text, $_text, $length );
    179218
    180219    $text = strtolower($text);
     
    187226}
    188227
    189 function bb_pre_sanitize_with_dashes_utf8( $text ) {
     228function bb_pre_sanitize_with_dashes_utf8( $text, $_text = '', $length = 0 ) {
    190229    $text = remove_accents($text);
    191230
     
    193232        if ( function_exists('mb_strtolower') )
    194233            $text = mb_strtolower($text, 'UTF-8');
    195         $text = utf8_uri_encode( $text );
    196     }
     234        $text = utf8_uri_encode( $text, $length );
     235    }
     236
    197237    return $text;
    198238}
Note: See TracChangeset for help on using the changeset viewer.