Skip to:
Content

Changeset 846


Ignore:
Timestamp:
06/07/07 07:04:40 (7 years ago)
Author:
mdawaffe
Message:

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

Location:
trunk
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/bb-admin/admin-functions.php

    r841 r846  
    422422    $forum_name = apply_filters( 'bb_pre_forum_name', stripslashes($forum_name) ); 
    423423    $forum_desc = apply_filters( 'bb_pre_forum_desc', stripslashes($forum_desc) ); 
     424    $forum_name = bb_trim_for_db( $forum_name, 150 ); 
    424425 
    425426    $forum_name = $bbdb->escape( $forum_name ); 
  • trunk/bb-admin/upgrade-functions.php

    r845 r846  
    1616    $bb_upgrade += bb_upgrade_190(); // Move topic_resolved to topicmeta 
    1717    $bb_upgrade += bb_upgrade_200(); // Indices 
     18    $bb_upgrade += bb_upgrade_210(); // Convert text slugs to varchar slugs 
    1819    require_once( BBPATH . 'bb-admin/upgrade-schema.php'); 
    1920    bb_make_db_current(); 
     
    269270} 
    270271 
     272function bb_upgrade_process_all_slugs() { 
     273    global $bbdb; 
     274    // Forums 
     275 
     276    $ids = (array) $bbdb->get_col("SELECT forum_id, forum_name FROM $bbdb->forums ORDER BY forum_order ASC" ); 
     277 
     278    $names = $bbdb->get_col('', 1); 
     279 
     280    $slugs = array(); 
     281    foreach ( $ids as $r => $id ) : 
     282        $slug = bb_slug_sanitize( $names[$r] ); 
     283        $slugs[$slug][] = $id; 
     284    endforeach; 
     285 
     286    foreach ( $slugs as $slug => $forum_ids ) : 
     287        foreach ( $forum_ids as $count => $forum_id ) : 
     288            if ( $count > 0 ) 
     289                $slug = bb_slug_increment( $slug, "-" . ( $count - 1 ) ); 
     290            $bbdb->query("UPDATE $bbdb->forums SET forum_slug = '$slug' WHERE forum_id = '$forum_id';"); 
     291        endforeach; 
     292    endforeach; 
     293    unset($ids, $names, $slugs, $r, $id, $slug, $forum_ids, $forum_id, $count); 
     294 
     295    // Topics 
     296 
     297    $ids = (array) $bbdb->get_col("SELECT topic_id, topic_title FROM $bbdb->topics ORDER BY topic_start_time ASC" ); 
     298 
     299    $names = $bbdb->get_col('', 1); 
     300 
     301    $slugs = array(); 
     302    foreach ( $ids as $r => $id ) : 
     303        $slug = bb_slug_sanitize( $names[$r] ); 
     304        $slugs[$slug][] = $id; 
     305    endforeach; 
     306 
     307    foreach ( $slugs as $slug => $topic_ids ) : 
     308        foreach ( $topic_ids as $count => $topic_id ) : 
     309            if ( $count > 0 ) 
     310                $slug = bb_slug_increment( $slug, "-" . ( $count - 1 ) ); 
     311            $bbdb->query("UPDATE $bbdb->topics SET topic_slug = '$slug' WHERE topic_id = '$topic_id';"); 
     312        endforeach; 
     313    endforeach; 
     314    unset($ids, $names, $slugs, $r, $id, $slug, $topic_ids, $topic_id, $count); 
     315} 
     316 
    271317// Reversibly break passwords of blocked users. 
    272318function bb_upgrade_160() { 
     
    359405} 
    360406 
    361 function bb_upgrade_1000() { 
    362     if ( ( $dbv = bb_get_option_from_db( 'bb_db_version' ) ) && $dbv >= 788 ) 
    363         return 0; 
     407// 210 converts text slugs to varchar(255) width slugs (upgrading from alpha version - fires before dbDelta) 
     408// 1000 Gives new slugs (upgrading from previous release - fires after dbDelta) 
     409function bb_upgrade_210() { 
     410    if ( ( $dbv = bb_get_option_from_db( 'bb_db_version' ) ) && $dbv >= 846 ) 
     411        return 0; 
     412 
     413    global $bbdb; 
     414 
     415    $bbdb->hide_errors(); 
     416    if ( !$ids = $bbdb->get_var("SELECT forum_slug FROM $bbdb->forums ORDER BY forum_order ASC LIMIT 1" ) ) 
     417        return; // Wait till after dbDelta 
     418    $bbdb->show_errors(); 
     419 
     420    bb_upgrade_process_all_slugs(); 
     421 
     422    bb_update_option( 'bb_db_version', 846 ); 
    364423     
    365     global $bbdb; 
    366      
    367     $forums = (array) $bbdb->get_results("SELECT forum_id, forum_name, forum_slug FROM $bbdb->forums ORDER BY forum_order ASC" ); 
    368     foreach ($forums  as $forum) { 
    369         $slug = bb_slug_sanitize(trim($forum->forum_name)); 
    370         $forum_slugs[$slug][] = $forum->forum_id; 
    371     } 
    372     foreach ($forum_slugs as $slug => $forums) { 
    373         foreach ($forums as $count => $forum_id) { 
    374             if ($count > 0) { 
    375                 $increment = '-' . ($count + 1); 
    376             } else { 
    377                 $increment = null; 
    378             } 
    379             $slug .= $increment; 
    380             $bbdb->query("UPDATE $bbdb->forums SET forum_slug = '$slug' WHERE forum_id = $forum_id;"); 
    381         } 
    382     } 
    383     unset($forums,$forum,$forum_slugs,$slug,$forum_id,$increment,$count); 
    384      
    385     $topics = (array) $bbdb->get_results("SELECT topic_id, topic_title, topic_slug FROM $bbdb->topics ORDER BY topic_start_time ASC" ); 
    386     foreach ($topics  as $topic) { 
    387         $slug = bb_slug_sanitize(trim($topic->topic_title)); 
    388         $topic_slugs[$slug][] = $topic->topic_id; 
    389     } 
    390     foreach ($topic_slugs as $slug => $topics) { 
    391         foreach ($topics as $count => $topic_id) { 
    392             if ($count > 0) { 
    393                 $increment = '-' . ($count + 1); 
    394             } else { 
    395                 $increment = null; 
    396             } 
    397             $slug .= $increment; 
    398             $bbdb->query("UPDATE $bbdb->topics SET topic_slug = '$slug' WHERE topic_id = $topic_id;"); 
    399         } 
    400     } 
    401     unset($topics,$topic,$topic_slugs,$slug,$topic_id,$increment,$count); 
    402      
    403     bb_update_option( 'bb_db_version', 788 ); 
     424    echo "Done adding slugs.<br />"; 
     425    return 1; 
     426} 
     427 
     428function bb_upgrade_1000() { // Give all topics and forums slugs 
     429    if ( ( $dbv = bb_get_option_from_db( 'bb_db_version' ) ) && $dbv >= 846 ) 
     430        return 0; 
     431 
     432    bb_upgrade_process_all_slugs(); 
     433 
     434    bb_update_option( 'bb_db_version', 846 ); 
    404435     
    405436    echo "Done adding slugs.<br />"; 
  • trunk/bb-admin/upgrade-schema.php

    r845 r846  
    55  forum_id int(10) NOT NULL auto_increment, 
    66  forum_name varchar(150)  NOT NULL default '', 
    7   forum_slug text  NOT NULL default '', 
     7  forum_slug varchar(255)  NOT NULL default '', 
    88  forum_desc text  NOT NULL, 
    99  forum_parent int(10) NOT NULL default '0', 
     
    3232  topic_id bigint(20) NOT NULL auto_increment, 
    3333  topic_title varchar(100) NOT NULL default '', 
    34   topic_slug text NOT NULL default '', 
     34  topic_slug varchar(255) NOT NULL default '', 
    3535  topic_poster bigint(20) NOT NULL default '0', 
    3636  topic_poster_name varchar(40) NOT NULL default 'Anonymous', 
  • trunk/bb-includes/default-filters.php

    r841 r846  
    3838add_filter('edit_text', 'trim', 15); 
    3939 
    40 add_filter('pre_create_tag', 'bb_pre_create_tag_utf8' ); 
    41  
    42 add_filter('pre_sanitize_with_dashes', 'bb_pre_sanitize_with_dashes_utf8' ); 
     40add_filter('pre_sanitize_with_dashes', 'bb_pre_sanitize_with_dashes_utf8', 10, 3 ); 
    4341 
    4442add_filter('get_user_link', 'bb_fix_link'); 
  • 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} 
  • trunk/bb-includes/functions.php

    r845 r846  
    11<?php 
    2  
    32/* INIT */ 
    43 
     
    173172    global $bbdb, $bb_cache; 
    174173    $title = apply_filters('pre_topic_title', $title, false); 
    175     $slug = bb_slug_sanitize($title); 
    176     $existing_slugs = $bbdb->get_col("SELECT topic_slug FROM $bbdb->topics WHERE topic_slug LIKE '$slug%'"); 
    177     if ($existing_slugs) { 
    178         $slug = bb_slug_increment($slug, $existing_slugs); 
    179     } 
     174    $title = bb_trim_for_db( $title, 150 ); 
     175    $slug  = $_slug = bb_slug_sanitize($title); 
     176    while ( $existing_slug = $bbdb->get_var("SELECT topic_slug FROM $bbdb->topics WHERE topic_slug = '$slug'") ) 
     177        $slug = bb_slug_increment($_slug, $existing_slug); 
     178 
    180179    $forum = (int) $forum; 
    181180    $now   = bb_current_time('mysql'); 
     
    748747    $tag = apply_filters( 'pre_create_tag', $tag ); 
    749748 
    750     $raw_tag = $tag; 
     749    $raw_tag = bb_trim_for_db( $tag, 50 ); 
    751750    $tag     = bb_tag_sanitize( $tag ); 
    752751 
     
    759758    do_action('bb_tag_created', $raw_tag, $bbdb->insert_id); 
    760759    return $bbdb->insert_id; 
    761 } 
    762  
    763 function bb_pre_create_tag_utf8( $tag ) { 
    764     if ( seems_utf8( $tag ) ) 
    765         $tag = bb_utf8_cut( $tag, 50 ); // Should match raw_tag column width in DB schema 
    766     return $tag; 
    767760} 
    768761 
     
    11891182        break; 
    11901183    case 'bb_db_version' : 
    1191         return '845'; // Don't filter 
     1184        return '846'; // Don't filter 
    11921185        break; 
    11931186    case 'html_type' : 
     
    15481541    $path = preg_replace("#$bbpath#",'',$path,1); 
    15491542    $url = explode('/',$path); 
    1550     return $url[$level]; 
     1543    return urldecode($url[$level]); 
    15511544} 
    15521545 
     
    15771570    else 
    15781571        $permalink = get_path(); 
     1572    $_original_piece = $permalink; 
    15791573 
    15801574    do_action( 'pre_permalink', $permalink ); 
     
    16111605            else 
    16121606                $permalink = get_path(); 
     1607            $_original_piece = $permalink; 
    16131608            if ( !$user = bb_get_user( $permalink ) ) 
    16141609                bb_die(__('User not found.')); 
     
    16371632            else 
    16381633                $permalink = get_path(); 
     1634            $_original_piece = $permalink; 
    16391635            if ( !$permalink ) 
    16401636                $permalink = get_tag_page_link(); 
     
    16491645            if ( isset($_GET['view']) ) 
    16501646                $permalink = $_GET['view']; 
    1651             else    $permalink = get_path(); 
     1647            else 
     1648                $permalink = get_path(); 
     1649            $_original_piece = $permalink; 
    16521650            global $view; 
    16531651            $view = $permalink; 
     
    16881686        echo "</td></tr>\n</table>"; 
    16891687    else : 
    1690         if ( $check != $uri ) { 
     1688        if ( $check != $uri && $check != str_replace(urlencode($_original_piece), $_original_piece, $uri) ) { 
    16911689            wp_redirect( $permalink ); 
    16921690            exit; 
     
    21332131/* Slugs */ 
    21342132 
    2135 function bb_slug_increment($slug, $all_slugs) { 
    2136     $all_slugs = preg_grep('/^' . $slug . '(\-[0-9]+)?$/', $all_slugs); 
    2137     if (!count($all_slugs)) { 
    2138         return $slug; 
    2139     } 
    2140      
    2141     natsort($all_slugs); 
    2142     $all_slugs = array_reverse($all_slugs); 
    2143     if ($slug == $all_slugs[0]) { 
    2144         $last_slug_number = 1; 
    2145     } else { 
    2146         $last_slug_number = (integer) str_replace($slug . '-', '', $all_slugs[0]); 
    2147     } 
    2148     return $slug . '-' . ($last_slug_number + 1); 
    2149 } 
    2150  
    2151 function bb_get_id_from_slug($table, $slug) { 
     2133function bb_slug_increment( $slug, $existing_slug, $slug_length = 255 ) { 
     2134    if ( preg_match('/^.*-([0-9]+)$/', $existing_slug, $m) ) 
     2135        $number = (int) $m[1] + 1; 
     2136    else 
     2137        $number = 1; 
     2138 
     2139    $r = bb_encoded_utf8_cut( $slug, $slug_length - 1 - strlen($number) ); 
     2140    return apply_filters( 'bb_slug_increment', "$r-$number", $slug, $existing_slug, $slug_length ); 
     2141} 
     2142 
     2143function bb_get_id_from_slug( $table, $slug, $slug_length = 255 ) { 
    21522144    global $bbdb; 
    21532145    $tablename = $table . 's'; 
    2154     $slug = bb_slug_sanitize($slug); 
    2155     $result = $bbdb->get_var("SELECT ${table}_id FROM {$bbdb->$tablename} WHERE ${table}_slug = '$slug'"); 
    2156     return $result; 
     2146    $r = false; 
     2147    // Look for new style equiv of old style slug 
     2148    $_slug = bb_slug_sanitize( $slug ); 
     2149    if ( strlen($_slug) > $slug_length && preg_match('/^.*-([0-9]+)$/', $_slug, $m) ) { 
     2150        $_slug = bb_encoded_utf8_cut( $_slug, $slug_length - 1 - strlen($number) ); 
     2151        $number = (int) $m[1]; 
     2152        $r = $bbdb->get_var("SELECT ${table}_id FROM {$bbdb->$tablename} WHERE ${table}_slug = '$_slug-$number'"); 
     2153    } 
     2154    if ( !$r ) { 
     2155        $_slug = bb_slug_sanitize($slug); 
     2156        $r = $bbdb->get_var("SELECT ${table}_id FROM {$bbdb->$tablename} WHERE ${table}_slug = '$_slug'"); 
     2157    } 
     2158    return $r; 
    21572159} 
    21582160 
Note: See TracChangeset for help on using the changeset viewer.