Changeset 2890

Show
Ignore:
Timestamp:
08/04/08 11:28:44 (4 months ago)
Author:
fumiakiy
Message:

Handle automatic transcoding for ajax requests. BugId:80904

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/release-42/lib/MT/App.pm

    r2877 r2890  
    33# GNU General Public License, version 2. 
    44# 
    5 # $Id: App.pm 2846 2008-07-28 02:46:54Z fumiakiy
     5# $Id
    66 
    77package MT::App; 
     
    816816        my $charset = $app->charset; 
    817817        require Encode; 
     818        require MT::I18N::default; 
    818819        $charset = 'UTF-8' if $charset =~ m/utf-?8/i; 
     820        my $request_charset = $charset; 
     821        if ( my $content_type = $q->content_type() ) { 
     822            if ( $content_type =~ m/;[ ]+charset=(.+)/i ) { 
     823                $request_charset = lc $1; 
     824                $request_charset =~ s/^\s+|\s+$//gs; 
     825            } 
     826        } 
     827        my $transcode = $request_charset ne $charset ? 1 : 0; 
     828        my %params; 
    819829        foreach my $p (@p) { 
    820830            if ( $p =~ m/[^\x20-\x7E]/ ) { 
     
    825835 
    826836            my @d = $q->param($p); 
     837            my @param; 
    827838            foreach my $d (@d) { 
    828                 next 
    829                     if ( !defined $d ) 
    830                     || ( $d eq '' ) 
    831                     || ( $d !~ m/[^\x20-\x7E]/ ); 
     839                if ( ( !defined $d ) 
     840                  || ( $d eq '' ) 
     841                  || ( $d !~ m/[^\x20-\x7E]/ ) ) 
     842                { 
     843                    push @param, $d if $transcode; 
     844                    next; 
     845                } 
     846                $d = MT::I18N::default->encode_text_encode( $d, $request_charset, $charset ) 
     847                    if $transcode; 
     848                my $saved = $d; 
    832849                eval { Encode::decode( $charset, $d, 1 ); }; 
    833850                return $app->errtrans( 
     
    835852                    $charset 
    836853                ) if $@; 
    837             } 
    838         } 
     854                push @param, $saved if $transcode; 
     855            } 
     856            if ( $transcode && @param ) { 
     857                if ( 1 == scalar(@param) ) { 
     858                    $params{ $p } = $param[0]; 
     859                } 
     860                else { 
     861                    $params{ $p } = [ @param ]; 
     862                } 
     863            } 
     864        } 
     865        $app->param( $_, $params{ $_ } ) foreach keys %params; 
    839866 
    840867        return 1; 
  • branches/release-42/lib/MT/App/Trackback.pm

    r2877 r2890  
    3939 
    4040    # attempt to determine character set encoding based on 
    41     # 'charset' parameter or 'charset' in content-type header
     41    # 'charset' parameter
    4242    my $enc = $q->param('charset'); 
    43     unless ($enc) { 
    44         my $content_type = $q->content_type(); 
    45         if ( $content_type =~ m/;[ ]+charset=(.+)/i ) { 
    46             $enc = lc $1; 
    47             $enc =~ s/^\s+|\s+$//gs; 
    48         } 
    49     } 
    5043    local $app->{charset} = $enc if $enc; 
    5144    return $app->SUPER::validate_request_params(); 
     
    253246    } 
    254247 
    255     my ( $title, $excerpt, $url, $blog_name, $enc
     248    my ( $title, $excerpt, $url, $blog_name
    256249        = map scalar $q->param($_), 
    257         qw( title excerpt url blog_name charset); 
    258  
    259     unless ($enc) { 
    260         my $content_type = $q->content_type(); 
    261         if ( $content_type =~ m/;[ ]+charset=(.+)/i ) { 
    262             $enc = lc $1; 
    263             $enc =~ s/^\s+|\s+$//gs; 
    264         } 
    265     } 
     250        qw( title excerpt url blog_name); 
    266251 
    267252    no_utf8( $tb_id, $title, $excerpt, $url, $blog_name ); 
    268  
    269     # guess encoding as possible 
    270     $enc = MT::I18N::guess_encoding( $excerpt . $title . $blog_name ) 
    271         unless $enc; 
    272     ( $title, $excerpt, $blog_name ) 
    273         = map { encode_text( $_, $enc ) } ( $title, $excerpt, $blog_name ); 
    274253 
    275254    return $app->_response( 
  • branches/release-42/lib/MT/CMS/Category.pm

    r2657 r2890  
    276276 
    277277    my $label = $app->param('label'); 
    278     my $enc   = $app->config->PublishCharset; 
    279  
    280     # XMLHttpRequest always send text in UTF-8... right? 
    281     if ( 'utf-8' ne lc($enc) ) { 
    282         $label = MT::I18N::encode_text( $label, 'utf-8', $enc ); 
    283     } 
    284278    my $basename = $app->param('basename'); 
    285279    if ( !defined($label) || ( $label =~ m/^\s*$/ ) ) { 
  • branches/release-42/lib/MT/CMS/Common.pm

    r2502 r2890  
    450450            my $data = $sess_obj->thaw_data; 
    451451            if ($data) { 
    452  
    453                 # XMLHttpRequest always send text in UTF-8... right? 
    454                 if ( 'utf-8' eq lc($enc) ) { 
    455                     $q->param( $_, $data->{$_} ) for keys %$data; 
    456                 } 
    457                 else { 
    458                     foreach ( keys %$data ) { 
    459                         my $encoded = 
    460                           MT::I18N::encode_text( $data->{$_}, 'utf-8', $enc ); 
    461                         $q->param( $_, $encoded ); 
    462                     } 
    463                 } 
     452                $q->param( $_, $data->{$_} ) for keys %$data; 
    464453                $param{'recovered_object'} = 1; 
    465454            } 
  • branches/release-42/lib/MT/CMS/Search.pm

    r2809 r2890  
    386386    ## on it if it's there. 
    387387    if ( ($search || '') ne '' ) { 
    388         my $enc = $app->charset; 
    389         $search = MT::I18N::encode_text( $search, 'utf-8', $enc ) 
    390           if ( $enc !~ m/utf-?8/i ) 
    391           && ( 'dialog_grant_role' eq $app->param('__mode') ); 
    392388        $search = quotemeta($search) unless $is_regex; 
    393389        $search = '(?i)' . $search   unless $case; 
  • branches/release-42/lib/MT/CMS/Tag.pm

    r2454 r2890  
    156156    my $obj_class    = $app->model($obj_ds) or return; 
    157157    my $tag_name     = $app->param('tag') or return; 
    158     if ( 'utf-8' ne lc( $app->config->PublishCharset) ) { 
    159         $tag_name = MT::I18N::encode_text( $tag_name, 'utf-8', $app->config->PublishCharset ); 
    160     } 
     158 
    161159    my $tag_obj = 
    162160      $tag_class->load( { name => $tag_name }, { binary => { name => 1 } } ); 
  • branches/release-42/lib/MT/CMS/Tools.pm

    r2770 r2890  
    14361436    my $app    = shift; 
    14371437    my $format = $app->param('format'); 
    1438     my $text   = $app->param('text'); 
    1439     # XMLHttpRequest always send text in UTF-8... right? 
    1440     if ( defined $text ) { 
    1441         $text = encode_text($text, 'utf-8', $app->config->PublishCharset); 
    1442     }  
    1443     else { 
    1444         $text = '' ; 
    1445     } 
    1446     my $text_more = $app->param('text_more'); 
    1447     if ( defined $text_more ) { 
    1448         $text_more = encode_text($text_more, 'utf-8', $app->config->PublishCharset); 
    1449     }  
    1450     else { 
    1451         $text_more = '' ; 
    1452     } 
     1438    my $text   = $app->param('text') || ''; 
     1439    my $text_more = $app->param('text_more') || ''; 
    14531440    my $result = { 
    14541441        text      => $app->apply_text_filters( $text,      [$format] ), 
  • branches/release-42/mt-static/js/tc/client.js

    r1174 r2890  
    8989            contents = args.join('&'); 
    9090        } 
    91         c.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' ); 
     91        c.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8' ); 
    9292    } 
    9393    c.send( contents );