Changeset 2877

Show
Ignore:
Timestamp:
08/01/08 01:43:09 (4 months ago)
Author:
bchoate
Message:

Tidying (-pbp).

Files:

Legend:

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

    r2846 r2877  
    33# GNU General Public License, version 2. 
    44# 
    5 # $Id
     5# $Id: App.pm 2846 2008-07-28 02:46:54Z fumiakiy
    66 
    77package MT::App; 
     
    1717 
    1818my $COOKIE_NAME = 'mt_user'; 
    19 sub COMMENTER_COOKIE_NAME () { "mt_commenter"
     19sub COMMENTER_COOKIE_NAME () {"mt_commenter"
    2020use vars qw( %Global_actions ); 
    2121 
     
    3737 
    3838sub core_list_filters { 
    39     {} 
     39    {}; 
    4040} 
    4141 
    4242sub core_widgets { 
    43     {} 
     43    {}; 
    4444} 
    4545 
    4646sub core_blog_stats_tabs { 
    47     {} 
     47    {}; 
    4848} 
    4949 
    5050sub core_search_apis { 
    51     {} 
     51    {}; 
    5252} 
    5353 
    5454sub __massage_page_action { 
    55     my ($app, $action, $type) = @_; 
     55    my ( $app, $action, $type ) = @_; 
    5656    return if exists $action->{__massaged}; 
    5757 
     
    5959    my $plugin = $action->{plugin}; 
    6060 
    61     if (my $link = $action->{link}) { 
     61    if ( my $link = $action->{link} ) { 
    6262        my $envelope = $plugin->envelope; 
    6363        $link .= '?' unless $link =~ m.\?.; 
     
    6666        $page .= $envelope . '/' if $envelope; 
    6767        $page .= $link; 
    68         my $has_params = ($page =~ m/\?/
    69             && ($page !~ m!(&|;|\?)$!); 
    70         $action->{page} = $page; 
     68        my $has_params = ( $page =~ m/\?/
     69            && ( $page !~ m!(&|;|\?)$! ); 
     70        $action->{page}            = $page; 
    7171        $action->{page_has_params} = $has_params; 
    72     } elsif ($action->{mode} || $action->{dialog}) { 
     72    } 
     73    elsif ( $action->{mode} || $action->{dialog} ) { 
    7374        if ( $app->user->is_superuser ) { 
    7475            $action->{allowed} = 1; 
     
    8081                foreach my $p (@p) { 
    8182                    my $perm = 'can_' . $p; 
    82                     $action->{allowed} = 1, last if ( $perms && $perms->$perm() ); 
     83                    $action->{allowed} = 1, last 
     84                        if ( $perms && $perms->$perm() ); 
    8385                } 
    8486            } 
     
    9294        elsif ( $action->{dialog} ) { 
    9395            if ( $action->{args} ) { 
    94                 my @args = map { $_ . '=' . $action->{args}->{$_} } keys %{ $action->{args} }; 
     96                my @args = map { $_ . '=' . $action->{args}->{$_} } 
     97                    keys %{ $action->{args} }; 
    9598                $action->{dialog_args} .= join '&', @args; 
    9699            } 
    97100        } 
    98     } else { 
    99         $action->{page} = $app->uri(mode => 'page_action', args => { action_name => $action->{key}, '_type' => $type } ); 
     101    } 
     102    else { 
     103        $action->{page} = $app->uri( 
     104            mode => 'page_action', 
     105            args => { action_name => $action->{key}, '_type' => $type } 
     106        ); 
    100107        $action->{page_has_params} = 1; 
    101108    } 
    102109    $action->{core} = $plugin->isa('MT::Plugin') ? 0 : 1; 
    103     $action->{order} = -10000 + ($action->{order} || 0) if $action->{core}; 
     110    $action->{order} = -10000 + ( $action->{order} || 0 ) if $action->{core}; 
    104111    $action->{label} = $action->{link_text} if exists $action->{link_text}; 
    105     if ($plugin && !ref($action->{label})) { 
     112    if ( $plugin && !ref( $action->{label} ) ) { 
    106113        my $label = $action->{label}; 
    107114        if ($plugin) { 
    108115            $action->{label} = sub { $plugin->translate($label) }; 
    109         } else { 
     116        } 
     117        else { 
    110118            $action->{label} = sub { $app->translate($label) }; 
    111119        } 
     
    116124 
    117125sub filter_conditional_list { 
    118     my ($app, $list, @param) = @_; 
     126    my ( $app, $list, @param ) = @_; 
    119127 
    120128    # $list may either be an array of things or a hashref of things 
    121129 
    122130    my $perms = $app->permissions; 
    123     my $user = $app->user; 
    124     my $admin = ($user && $user->is_superuser()
    125         || ($perms && $perms->blog_id && $perms->has('administer_blog')); 
     131    my $user = $app->user; 
     132    my $admin = ( $user && $user->is_superuser()
     133        || ( $perms && $perms->blog_id && $perms->has('administer_blog') ); 
    126134    my $system_perms = $user->permissions(0) unless $perms && $perms->blog_id; 
    127135 
     
    129137        my ($item) = @_; 
    130138        if ( $system_perms 
    131           && (my $sp = $item->{system_permission}) ) { 
     139            && ( my $sp = $item->{system_permission} ) ) 
     140        { 
    132141            my $allowed = 0; 
    133142            my @sp = split /,/, $sp; 
     
    135144                my $perm = 'can_' . $sp; 
    136145                $allowed = 1, last 
    137                     if $admin || ($system_perms && $system_perms->can($perm) && $system_perms->$perm()); 
     146                    if $admin 
     147                        || (   $system_perms 
     148                            && $system_perms->can($perm) 
     149                            && $system_perms->$perm() ); 
    138150            } 
    139151            return 0 unless $allowed; 
    140152        } 
    141         if (my $p = $item->{permission}) { 
     153        if ( my $p = $item->{permission} ) { 
    142154            my $allowed = 0; 
    143155            my @p = split /,/, $p; 
     
    145157                my $perm = 'can_' . $p; 
    146158                $allowed = 1, last 
    147                     if $admin || ($perms && $perms->can($perm) && $perms->$perm()); 
     159                    if $admin 
     160                        || (   $perms 
     161                            && $perms->can($perm) 
     162                            && $perms->$perm() ); 
    148163            } 
    149164            return 0 unless $allowed; 
    150165        } 
    151         if (my $cond = $item->{condition}) { 
    152             if (!ref($cond)) { 
     166        if ( my $cond = $item->{condition} ) { 
     167            if ( !ref($cond) ) { 
    153168                $cond = $item->{condition} = $app->handler_to_coderef($cond); 
    154169            } 
     
    158173    }; 
    159174 
    160     if (ref $list eq 'ARRAY') { 
     175    if ( ref $list eq 'ARRAY' ) { 
    161176        my @list; 
    162177        if (@$list) { 
     178 
    163179            # translate the link text here... 
    164180            foreach my $item (@$list) { 
     
    167183        } 
    168184        return \@list; 
    169     } elsif (ref $list eq 'HASH') { 
     185    } 
     186    elsif ( ref $list eq 'HASH' ) { 
    170187        my %list; 
    171188        if (%$list) { 
     189 
    172190            # translate the link text here... 
    173             foreach my $item (keys %$list) { 
     191            foreach my $item ( keys %$list ) { 
    174192                $list{$item} = $list->{$item} 
    175                     if $test->($list->{$item}); 
     193                    if $test->( $list->{$item} ); 
    176194            } 
    177195        } 
     
    183201sub page_actions { 
    184202    my $app = shift; 
    185     my ($type, @param) = @_; 
    186     my $actions = $app->registry("page_actions", $type) or return; 
    187     foreach my $a (keys %$actions) { 
     203    my ( $type, @param ) = @_; 
     204    my $actions = $app->registry( "page_actions", $type ) or return; 
     205    foreach my $a ( keys %$actions ) { 
    188206        $actions->{$a}{key} = $a; 
    189         __massage_page_action($app, $actions->{$a}, $type); 
     207        __massage_page_action( $app, $actions->{$a}, $type ); 
    190208    } 
    191209    my @actions = values %$actions; 
    192     $actions = $app->filter_conditional_list(\@actions, @param); 
     210    $actions = $app->filter_conditional_list( \@actions, @param ); 
    193211    no warnings; 
    194212    @$actions = sort { $a->{order} <=> $b->{order} } @$actions; 
     
    198216sub list_actions { 
    199217    my $app = shift; 
    200     my ($type, @param) = @_; 
    201  
    202     my $actions = $app->registry("list_actions", $type) or return; 
     218    my ( $type, @param ) = @_; 
     219 
     220    my $actions = $app->registry( "list_actions", $type ) or return; 
    203221    my @actions; 
    204     foreach my $a (keys %$actions) { 
    205         $actions->{$a}{key} = $a; 
     222    foreach my $a ( keys %$actions ) { 
     223        $actions->{$a}{key} = $a; 
    206224        $actions->{$a}{core} = 1 
    207             unless UNIVERSAL::isa($actions->{$a}{plugin}, 'MT::Plugin'); 
     225            unless UNIVERSAL::isa( $actions->{$a}{plugin}, 'MT::Plugin' ); 
    208226        if ( exists $actions->{$a}{continue_prompt_handler} ) { 
    209             my $code = $app->handler_to_coderef($actions->{$a}{continue_prompt_handler}); 
     227            my $code = $app->handler_to_coderef( 
     228                $actions->{$a}{continue_prompt_handler} ); 
    210229            $actions->{$a}{continue_prompt} = $code->() 
    211230                if 'CODE' eq ref($code); 
     
    213232        push @actions, $actions->{$a}; 
    214233    } 
    215     $actions = $app->filter_conditional_list(\@actions, @param); 
     234    $actions = $app->filter_conditional_list( \@actions, @param ); 
    216235    no warnings; 
    217236    @$actions = sort { $a->{order} <=> $b->{order} } @$actions; 
     
    221240sub list_filters { 
    222241    my $app = shift; 
    223     my ($type, @param) = @_; 
    224  
    225     my $filters = $app->registry("list_filters", $type) or return; 
     242    my ( $type, @param ) = @_; 
     243 
     244    my $filters = $app->registry( "list_filters", $type ) or return; 
    226245    my @filters; 
    227     foreach my $a (keys %$filters) { 
     246    foreach my $a ( keys %$filters ) { 
    228247        $filters->{$a}{key} = $a; 
    229         next if $a =~ m/^_/; # not shown... 
     248        next if $a =~ m/^_/;    # not shown... 
    230249        push @filters, $filters->{$a}; 
    231250    } 
    232     $filters = $app->filter_conditional_list(\@filters, @param); 
     251    $filters = $app->filter_conditional_list( \@filters, @param ); 
    233252    no warnings; 
    234253    @$filters = sort { $a->{order} <=> $b->{order} } @$filters; 
     
    238257sub search_apis { 
    239258    my $app = shift; 
    240     my ($view, @param) = @_; 
     259    my ( $view, @param ) = @_; 
    241260 
    242261    my $apis = $app->registry("search_apis") or return; 
    243262    my @apis; 
    244     foreach my $a (keys %$apis) { 
     263    foreach my $a ( keys %$apis ) { 
    245264        next if $apis->{$a}->{view} && $apis->{$a}->{view} ne $view; 
    246         $apis->{$a}{key} = $a; 
     265        $apis->{$a}{key} = $a; 
    247266        $apis->{$a}{core} = 1 
    248             unless UNIVERSAL::isa($apis->{$a}{plugin}, 'MT::Plugin'); 
     267            unless UNIVERSAL::isa( $apis->{$a}{plugin}, 'MT::Plugin' ); 
    249268        push @apis, $apis->{$a}; 
    250269    } 
    251     $apis = $app->filter_conditional_list(\@apis, @param); 
     270    $apis = $app->filter_conditional_list( \@apis, @param ); 
    252271    no warnings; 
    253272    @$apis = sort { $a->{order} <=> $b->{order} } @$apis; 
     
    260279 
    261280    my $type = $opt->{type} || $opt->{Type} || $app->param('_type'); 
    262     my $tmpl = $opt->{template} 
    263       || $opt->{Template} 
    264       || 'list_' . $type . '.tmpl'; 
     281    my $tmpl  
     282        = $opt->{template} 
     283        || $opt->{Template} 
     284        || 'list_' . $type . '.tmpl'; 
    265285    my $iter_method = $opt->{iterator} || $opt->{Iterator} || 'load_iter'; 
    266286    my $param       = $opt->{params}   || $opt->{Params}   || {}; 
    267287    $param->{listing_screen} = 1; 
    268288    my $add_pseudo_new_user = delete $param->{pseudo_new_user} 
    269       if exists $param->{pseudo_new_user}; 
    270     my $hasher  = $opt->{code}    || $opt->{Code}; 
    271     my $terms   = $opt->{terms}   || $opt->{Terms} || {}; 
    272     my $args    = $opt->{args}    || $opt->{Args} || {}; 
    273     my $no_html = $opt->{no_html} || $opt->{NoHTML}; 
     289        if exists $param->{pseudo_new_user}; 
     290    my $hasher  = $opt->{code}     || $opt->{Code}; 
     291    my $terms    = $opt->{terms}    || $opt->{Terms} || {}; 
     292    my $args    = $opt->{args}     || $opt->{Args} || {}; 
     293    my $no_html = $opt->{no_html} || $opt->{NoHTML}; 
    274294    my $no_limit = $opt->{no_limit} || 0; 
    275     my $json    = $opt->{json}    || $app->param('json'); 
    276     my $pre_build = $opt->{pre_build} if ref($opt->{pre_build}) eq 'CODE'; 
     295    my $json    = $opt->{json}     || $app->param('json'); 
     296    my $pre_build = $opt->{pre_build} if ref( $opt->{pre_build} ) eq 'CODE'; 
    277297    $param->{json} = 1 if $json; 
    278298 
     
    289309    if ( my $search = $app->param('search') ) { 
    290310        $app->param( 'do_search', 1 ); 
    291         if ($app->can('do_search_replace')) { 
     311        if ( $app->can('do_search_replace') ) { 
    292312            my $search_param = $app->do_search_replace(); 
    293313            if ($hasher) { 
     
    306326    } 
    307327    else { 
     328 
    308329        # handle filter options 
    309330        my $filter_key = $app->param('filter_key'); 
     
    311332            $filter_key = 'default'; 
    312333        } 
    313         if ( $filter_key ) { 
     334        if ($filter_key) { 
    314335 
    315336            # set filter based on type 
     
    318339                if ( my $code = $filter->{code} || $filter->{handler} ) { 
    319340                    if ( ref($code) ne 'CODE' ) { 
    320                         $code = $filter->{code} = 
    321                           $app->handler_to_coderef($code); 
     341                        $code = $filter->{code} 
     342                            = $app->handler_to_coderef($code); 
    322343                    } 
    323344                    if ( ref($code) eq 'CODE' ) { 
     
    333354            && ( my $val = $app->param('filter_val') ) ) 
    334355        { 
    335             if ( 
    336                 ( 
    337                        ( $filter_col eq 'normalizedtag' ) 
     356            if ((      ( $filter_col eq 'normalizedtag' ) 
    338357                    || ( $filter_col eq 'exacttag' ) 
    339358                ) 
    340359                && ( $class->isa('MT::Taggable') ) 
    341              
     360               
    342361            { 
    343362                my $normalize   = ( $filter_col eq 'normalizedtag' ); 
     
    349368                if ($normalize) { 
    350369                    push @filter_tags, MT::Tag->normalize($_) 
    351                       foreach @filter_vals; 
     370                        foreach @filter_vals; 
    352371                } 
    353                 my @tags = $tag_class->load( { name => [@filter_tags] }, 
    354                     { binary => { name => 1 } } ); 
     372                my @tags = $tag_class->load( 
     373                    { name   => [@filter_tags] }, 
     374                    { binary => { name => 1 } } 
     375                ); 
    355376                my @tag_ids; 
    356377                foreach (@tags) { 
     
    365386                $args->{'join'} = $ot_class->join_on( 
    366387                    'object_id', 
    367                     { 
    368                         tag_id            => \@tag_ids, 
     388                    {   tag_id            => \@tag_ids, 
    369389                        object_datasource => $class->datasource 
    370390                    }, 
     
    373393            } 
    374394            elsif ( !exists( $terms->{$filter_col} ) ) { 
    375                 if ($class->has_column($filter_col)) { 
     395                if ( $class->has_column($filter_col) ) { 
    376396                    $terms->{$filter_col} = $val; 
    377397                } 
     
    396416 
    397417        $args->{sort} = 'id' 
    398           unless exists $args->{sort};    # must always provide sort column 
    399  
    400         $app->run_callbacks( 'app_pre_listing_'.$app->mode, 
    401                              $app, $terms, $args, $param, \$hasher ); 
    402  
    403         my $iter = 
    404           ref($iter_method) eq 'CODE' 
    405           ? $iter_method 
    406           : ( $class->$iter_method( $terms, $args ) 
    407               or return $app->error( $class->errstr ) ); 
     418            unless exists $args->{sort};    # must always provide sort column 
     419 
     420        $app->run_callbacks( 'app_pre_listing_' . $app->mode, 
     421            $app, $terms, $args, $param, \$hasher ); 
     422 
     423        my $iter 
     424            = ref($iter_method) eq 'CODE' 
     425            ? $iter_method 
     426            : ( $class->$iter_method( $terms, $args ) 
     427                or return $app->error( $class->errstr ) ); 
    408428        my @data; 
    409429        while ( my $obj = $iter->() ) { 
    410430            my $row = $obj->column_values(); 
    411431            $hasher->( $obj, $row ) if $hasher; 
     432 
    412433            #$app->run_callbacks( 'app_listing_'.$app->mode, 
    413434            #                     $app, $obj, $row ); 
    414435            push @data, $row; 
    415             last if (scalar @data == $limit) && (!$no_limit); 
     436            last if ( scalar @data == $limit ) && ( !$no_limit ); 
    416437        } 
    417438 
     
    432453        $param->{pager_json} = $json ? $pager : JSON::objToJson($pager); 
    433454 
    434     # pager.rows (number of rows shown) 
    435     # pager.listTotal (total number of rows in datasource) 
    436     # pager.offset (offset currently used) 
    437     # pager.chronological (boolean, whether the listing is chronological or not) 
     455  # pager.rows (number of rows shown) 
     456  # pager.listTotal (total number of rows in datasource) 
     457  # pager.offset (offset currently used) 
     458  # pager.chronological (boolean, whether the listing is chronological or not) 
    438459    } 
    439460 
     
    470491            return $param; 
    471492        } 
    472         if (ref $tmpl) { 
    473             $tmpl->param( $param ); 
     493        if ( ref $tmpl ) { 
     494            $tmpl->param($param); 
    474495            return $tmpl; 
    475         } else { 
     496        } 
     497        else { 
    476498            return $app->load_tmpl( $tmpl, $param ); 
    477499        } 
     
    485507    $app->{no_print_body} = 1; 
    486508    require JSON; 
    487     $app->print(JSON::objToJson( { error => undef, result => $result })); 
     509    $app->print( JSON::objToJson( { error => undef, result => $result } ) ); 
    488510    return undef; 
    489511} 
     
    495517    $app->{no_print_body} = 1; 
    496518    require JSON; 
    497     $app->print(JSON::objToJson( { error => $error } )); 
     519    $app->print( JSON::objToJson( { error => $error } ) ); 
    498520    return undef; 
    499521} 
     
    525547sub send_http_header { 
    526548    my $app = shift; 
    527     my($type) = @_; 
     549    my ($type) = @_; 
    528550    $type ||= $app->{response_content_type} || 'text/html'; 
    529     if (my $charset = $app->charset) { 
     551    if ( my $charset = $app->charset ) { 
    530552        $type .= "; charset=$charset" 
    531             if $type =~ m!^text/! && $type !~ /\bcharset\b/; 
    532     } 
    533     if ($ENV{MOD_PERL}) { 
    534         if ($app->{response_message}) { 
    535             $app->{apache}->status_line(($app->response_code || 200) 
    536                                         . ($app->{response_message} ? ' ' . $app->{response_message} : '')); 
    537         } else { 
    538             $app->{apache}->status($app->response_code || 200); 
     553            if ( $type =~ m!^text/! || $type =~ m!\+xml$! ) 
     554            && $type !~ /\bcharset\b/; 
     555    } 
     556    if ( $ENV{MOD_PERL} ) { 
     557        if ( $app->{response_message} ) { 
     558            $app->{apache}->status_line( 
     559                ( $app->response_code || 200 ) 
     560                . ( $app->{response_message} 
     561                    ? ' ' . $app->{response_message} 
     562                    : '' 
     563                ) 
     564            ); 
     565        } 
     566        else { 
     567            $app->{apache}->status( $app->response_code || 200 ); 
    539568        } 
    540569        $app->{apache}->send_http_header($type); 
    541         if ($MT::DebugMode & 128) { 
    542             print "Status: " . ($app->response_code || 200) 
    543                 . ($app->{response_message} ? ' ' . $app->{response_message} : '') 
     570        if ( $MT::DebugMode & 128 ) { 
     571            print "Status: " 
     572                . ( $app->response_code || 200 ) 
     573                . ( $app->{response_message} 
     574                ? ' ' . $app->{response_message} 
     575                : '' ) 
    544576                . "\n"; 
    545577            print "Content-Type: $type\n\n"; 
    546578        } 
    547     } else { 
    548         $app->{cgi_headers}{-status} = ($app->response_code || 200) 
    549                                      . ($app->{response_message} ? ' ' . $app->{response_message} : ''); 
     579    } 
     580    else { 
     581        $app->{cgi_headers}{-status} 
     582            = ( $app->response_code || 200 ) 
     583            . ( 
     584            $app->{response_message} ? ' ' . $app->{response_message} : '' ); 
    550585        $app->{cgi_headers}{-type} = $type; 
    551         $app->print($app->{query}->header(%{ $app->{cgi_headers} })); 
     586        $app->print( $app->{query}->header( %{ $app->{cgi_headers} } ) ); 
    552587    } 
    553588} 
     
    555590sub print { 
    556591    my $app = shift; 
    557     if ($ENV{MOD_PERL}) { 
     592    if ( $ENV{MOD_PERL} ) { 
    558593        $app->{apache}->print(@_); 
    559     } else { 
     594    } 
     595    else { 
    560596        CORE::print(@_); 
    561597    } 
    562     if ($MT::DebugMode & 128) { 
     598    if ( $MT::DebugMode & 128 ) { 
    563599        CORE::print STDERR @_; 
    564600    } 
     
    567603sub handler ($$) { 
    568604    my $class = shift; 
    569     my($r) = @_; 
     605    my ($r) = @_; 
    570606    require Apache::Constants; 
    571     if (lc($r->dir_config('Filter') || '') eq 'on') { 
     607    if ( lc( $r->dir_config('Filter') || '' ) eq 'on' ) { 
    572608        $r = $r->filter_register; 
    573609    } 
    574610    my $config_file = $r->dir_config('MTConfig'); 
    575     my $mt_dir = $r->dir_config('MTHome'); 
    576     my %params = (Config => $config_file, ApacheObject => $r, 
    577                   ( $mt_dir ? ( Directory => $mt_dir ) : () )); 
    578     my $app = $class->new( %params ) 
     611    my $mt_dir      = $r->dir_config('MTHome'); 
     612    my %params      = ( 
     613        Config       => $config_file, 
     614        ApacheObject => $r, 
     615        ( $mt_dir ? ( Directory => $mt_dir ) : () ) 
     616    ); 
     617    my $app = $class->new(%params) 
    579618        or die $class->errstr; 
    580619 
     
    583622 
    584623    my $cfg = $app->config; 
    585     if (my @extra = $r->dir_config('MTSetVar')) { 
     624    if ( my @extra = $r->dir_config('MTSetVar') ) { 
    586625        for my $d (@extra) { 
    587             my($var, $val) = $d =~ /^\s*(\S+)\s+(.+)$/; 
    588             $cfg->set($var, $val); 
     626            my ( $var, $val ) = $d =~ /^\s*(\S+)\s+(.+)$/; 
     627            $cfg->set( $var, $val ); 
    589628        } 
    590629    } 
     
    602641 
    603642sub init { 
    604     my $app = shift; 
     643    my $app   = shift; 
    605644    my %param = @_; 
    606645    $app->{apache} = $param{ApacheObject} if exists $param{ApacheObject}; 
    607646    $app->SUPER::init(%param) or return; 
    608     $app->{vtbl} = { }; 
    609     $app->{is_admin} = 0; 
    610     $app->{template_dir} = 'cms'; #$app->id; 
    611     $app->{user_class} = 'MT::Author'; 
     647    $app->{vtbl}                 = {}; 
     648    $app->{is_admin}             = 0; 
     649    $app->{template_dir}         = 'cms';          #$app->id; 
     650    $app->{user_class}           = 'MT::Author'; 
    612651    $app->{plugin_template_path} = 'tmpl'; 
    613     $app->run_callbacks('init_app', $app, @_); 
    614     if ($MT::DebugMode & 4) { 
     652    $app->run_callbacks( 'init_app', $app, @_ ); 
     653 
     654    if ( $MT::DebugMode & 4 ) { 
     655 
    615656        # SQL profiling 
    616657        my $dbh = MT::Object->driver->r_handle; 
     
    618659        $dbh->{Profile} = DBI::Profile->new(); 
    619660    } 
    620     if ($MT::DebugMode & 128) { 
    621         MT->add_callback('pre_run', 1, $app, sub { $app->pre_run_debug }); 
    622         MT->add_callback('takedown', 1, $app, sub { $app->post_run_debug }); 
     661    if ( $MT::DebugMode & 128 ) { 
     662        MT->add_callback( 'pre_run',  1, $app, sub { $app->pre_run_debug } ); 
     663        MT->add_callback( 'takedown', 1, $app, sub { $app->post_run_debug } ); 
    623664    } 
    624665    $app->{vtbl} = $app->registry("methods"); 
     
    629670sub pre_run_debug { 
    630671    my $app = shift; 
    631     if ($MT::DebugMode & 128) { 
     672    if ( $MT::DebugMode & 128 ) { 
    632673        print STDERR "=====START: $$===========================\n"; 
    633674        print STDERR "Package: " . ref($app) . "\n"; 
     
    639680            foreach my $key (@param) { 
    640681                my @val = $app->param($key); 
    641                 print STDERR "\t" . $key . ": " . $_ . "\n" 
    642                     for @val; 
     682                print STDERR "\t" . $key . ": " . $_ . "\n" for @val; 
    643683            } 
    644684        } 
     
    648688 
    649689sub post_run_debug { 
    650     if ($MT::DebugMode & 128) { 
     690    if ( $MT::DebugMode & 128 ) { 
    651691        print STDERR "\n=====END: $$=============================\n"; 
    652692    } 
     
    655695sub run_callbacks { 
    656696    my $app = shift; 
    657     my ($meth, @param) = @_; 
    658     $meth = (ref($app)||$app) . '::' . $meth unless $meth =~ m/::/; 
    659     return $app->SUPER::run_callbacks($meth, @param); 
     697    my ( $meth, @param ) = @_; 
     698    $meth = ( ref($app) || $app ) . '::' . $meth unless $meth =~ m/::/; 
     699    return $app->SUPER::run_callbacks( $meth, @param ); 
    660700} 
    661701 
     
    663703    my $app = shift; 
    664704    $app->SUPER::init_callbacks(@_); 
    665     MT->add_callback('post_save', 0, $app, \&_cb_mark_blog ); 
    666     MT->add_callback('MT::Blog::post_remove', 0, $app, \&_cb_unmark_blog ); 
    667     MT->add_callback('pre_build', 9, $app, sub { $app->touch_blogs() } ); 
    668     MT->add_callback('new_user_provisioning', 5, $app, \&_cb_user_provisioning); 
     705    MT->add_callback( 'post_save',             0, $app, \&_cb_mark_blog ); 
     706    MT->add_callback( 'MT::Blog::post_remove', 0, $app, \&_cb_unmark_blog ); 
     707    MT->add_callback( 'pre_build', 9, $app, sub { $app->touch_blogs() } ); 
     708    MT->add_callback( 'new_user_provisioning', 5, $app, 
     709        \&_cb_user_provisioning ); 
    669710} 
    670711 
    671712sub init_request { 
    672     my $app = shift; 
     713    my $app   = shift; 
    673714    my %param = @_; 
    674715 
     
    676717 
    677718    if ($MT::DebugMode) { 
    678         if ($MT::DebugMode & 4) {  # SQL profile reporting is enabled 
     719        if ( $MT::DebugMode & 4 ) {    # SQL profile reporting is enabled 
    679720            my $h = MT::Object->driver->r_handle; 
    680             if (my $Profile = $h->{Profile}) { # if DBI profiling is enabled 
    681                 $Profile->{Data} = {}; # reset the profile data 
     721            if ( my $Profile = $h->{Profile} ) { # if DBI profiling is enabled 
     722                $Profile->{Data} = {};           # reset the profile data 
    682723            } 
    683724        } 
     
    686727    } 
    687728 
    688     if ($app->{request_read_config}) { 
    689         $app->init_config(\%param) or return; 
     729    if ( $app->{request_read_config} ) { 
     730        $app->init_config( \%param ) or return; 
    690731        $app->{request_read_config} = 0; 
    691732    } 
     
    699740    delete $app->{$_} foreach @req_vars; 
    700741    $app->user(undef); 
    701     if ($ENV{MOD_PERL}) { 
     742    if ( $ENV{MOD_PERL} ) { 
    702743        require Apache::Request; 
    703744        $app->{apache} = $param{ApacheObject} || Apache->request; 
    704         $app->{query} = Apache::Request->instance($app->{apache}, 
    705             POST_MAX => $app->config->CGIMaxUpload); 
    706     } else { 
    707         if ($param{CGIObject}) { 
     745        $app->{query} = Apache::Request->instance( $app->{apache}, 
     746            POST_MAX => $app->config->CGIMaxUpload ); 
     747    } 
     748    else { 
     749        if ( $param{CGIObject} ) { 
    708750            $app->{query} = $param{CGIObject}; 
    709751            require CGI; 
    710752            $CGI::POST_MAX = $app->config->CGIMaxUpload; 
    711         } else { 
    712             if (my $path_info = $ENV{PATH_INFO}) { 
    713                 if ($path_info =~ m/\.cgi$/) { 
     753        } 
     754        else { 
     755            if ( my $path_info = $ENV{PATH_INFO} ) { 
     756                if ( $path_info =~ m/\.cgi$/ ) { 
     757 
    714758                    # some CGI environments (notably 'sbox') leaves PATH_INFO 
    715759                    # defined which interferes with CGI.pm determining the 
     
    730774    ## Initialize the MT::Request singleton for this particular request. 
    731775    $app->request->reset(); 
    732     $app->request('App-Class', ref $app); 
    733  
    734     $app->run_callbacks(ref($app) . '::init_request', $app, @_); 
     776    $app->request( 'App-Class', ref $app ); 
     777 
     778    $app->run_callbacks( ref($app) . '::init_request', $app, @_ ); 
    735779 
    736780    $app->{init_request} = 1; 
     
    739783sub init_query { 
    740784    my $app = shift; 
    741     my $q = $app->{query}; 
     785    my $q   = $app->{query}; 
     786 
    742787    # CGI.pm has this terrible flaw in that if a POST is in effect, 
    743788    # it totally ignores any query parameters. 
    744     if ($app->request_method eq 'POST') { 
    745         if (! $ENV{MOD_PERL}) { 
     789    if ( $app->request_method eq 'POST' ) { 
     790        if ( !$ENV{MOD_PERL} ) { 
    746791            my $query_string = $ENV{'QUERY_STRING'} 
    747792                if defined $ENV{'QUERY_STRING'}; 
    748793            $query_string ||= $ENV{'REDIRECT_QUERY_STRING'} 
    749794                if defined $ENV{'REDIRECT_QUERY_STRING'}; 
    750             if (defined($query_string) and $query_string ne '') { 
     795            if ( defined($query_string) and $query_string ne '' ) { 
    751796                $q->parse_params($query_string); 
    752797            } 
     
    755800} 
    756801 
     802{ 
     803    my $has_encode; 
     804 
     805    sub validate_request_params { 
     806        my $app = shift; 
     807 
     808        $has_encode = eval { require Encode; 1 } ? 1 : 0 
     809            unless defined $has_encode; 
     810        return 1 unless $has_encode; 
     811 
     812        my $q = $app->param; 
     813 
     814        # validate all parameter data matches the expected character set. 
     815        my @p       = $q->param(); 
     816        my $charset = $app->charset; 
     817        require Encode; 
     818        $charset = 'UTF-8' if $charset =~ m/utf-?8/i; 
     819        foreach my $p (@p) { 
     820            if ( $p =~ m/[^\x20-\x7E]/ ) { 
     821 
     822                # non-ASCII parameter name 
     823                return $app->errtrans("Invalid request"); 
     824            } 
     825 
     826            my @d = $q->param($p); 
     827            foreach my $d (@d) { 
     828                next 
     829                    if ( !defined $d ) 
     830                    || ( $d eq '' ) 
     831                    || ( $d !~ m/[^\x20-\x7E]/ ); 
     832                eval { Encode::decode( $charset, $d, 1 ); }; 
     833                return $app->errtrans( 
     834                    "Invalid request: corrupt character data for character set [_1]", 
     835                    $charset 
     836                ) if $@; 
     837            } 
     838        } 
     839 
     840        return 1; 
     841    } 
     842} 
     843 
    757844sub registry { 
    758845    my $app = shift; 
    759     my $ar = $app->SUPER::registry("applications", $app->id, @_); 
    760     my $gr = $app->SUPER::registry(@_) if @_; 
     846    my $ar = $app->SUPER::registry( "applications", $app->id, @_ ); 
     847    my $gr = $app->SUPER::registry(@_) if @_; 
    761848    if ($ar) { 
    762         MT::__merge_hash($ar, $gr); 
     849        MT::__merge_hash( $ar, $gr ); 
    763850        return $ar; 
    764851    } 
     
    767854 
    768855sub _cb_unmark_blog { 
    769     my ($eh, $obj) = @_; 
     856    my ( $eh, $obj ) = @_; 
    770857    my $mt_req = MT->instance->request; 
    771     if (my $blogs_touched = $mt_req->stash('blogs_touched')) { 
    772         delete $blogs_touched->{$obj->id}; 
    773         $mt_req->stash('blogs_touched', $blogs_touched); 
     858    if ( my $blogs_touched = $mt_req->stash('blogs_touched') ) { 
     859        delete $blogs_touched->{ $obj->id }; 
     860        $mt_req->stash( 'blogs_touched', $blogs_touched ); 
    774861    } 
    775862} 
    776863 
    777864sub _cb_mark_blog { 
    778     my ($eh, $obj) = @_; 
     865    my ( $eh, $obj ) = @_; 
    779866    my $obj_type = ref $obj; 
    780867 
    781     if ($obj_type eq 'MT::Author') { 
     868    if ( $obj_type eq 'MT::Author' ) { 
    782869        require MT::Touch; 
    783         MT::Touch->touch(0, 'author'); 
     870        MT::Touch->touch( 0, 'author' ); 
    784871        return; 
    785872    } 
    786873 
    787     return if ($obj_type eq 'MT::Log' || $obj_type eq 'MT::Session' || 
    788             $obj_type eq 'MT::Touch' || 
    789                (($obj_type ne 'MT::Blog') && !$obj->has_column('blog_id'))); 
     874    return 
     875        if ( $obj_type eq 'MT::Log' 
     876        || $obj_type eq 'MT::Session' 
     877        || $obj_type eq 'MT::Touch' 
     878        || ( ( $obj_type ne 'MT::Blog' ) && !$obj->has_column('blog_id') ) ); 
    790879    my $mt_req = MT->instance->request; 
    791880    my $blogs_touched = $mt_req->stash('blogs_touched') || {}; 
     
    793882    # Issue MT::Touch touches for specific types we track 
    794883    my $type = $obj->datasource; 
    795     if ($obj->properties->{class_column}) { 
     884    if ( $obj->properties->{class_column} ) { 
    796885        $type = $obj->class_type; 
    797886    } 
    798     if ($type !~ m/^(entry|comment|page|folder|category|tbping|asset|author|template)$/) { 
     887    if ( $type 
     888        !~ m/^(entry|comment|page|folder|category|tbping|asset|author|template)$/ 
     889        ) 
     890    { 
    799891        undef $type; 
    800892    } 
    801893 
    802     if ($obj_type eq 'MT::Blog') { 
    803         delete $blogs_touched->{$obj->id}; 
    804     } else { 
    805         if ($obj->blog_id) { 
    806             my $th = $blogs_touched->{$obj->blog_id} ||= {}; 
     894    if ( $obj_type eq 'MT::Blog' ) { 
     895        delete $blogs_touched->{ $obj->id }; 
     896    } 
     897    else { 
     898        if ( $obj->blog_id ) { 
     899            my $th = $blogs_touched->{ $obj->blog_id } ||= {}; 
    807900            $th->{$type} = 1 if $type; 
    808901        } 
    809902    } 
    810     $mt_req->stash('blogs_touched', $blogs_touched); 
     903    $mt_req->stash( 'blogs_touched', $blogs_touched ); 
    811904} 
    812905 
    813906sub _cb_user_provisioning { 
    814     my ($cb, $user) = @_; 
     907    my ( $cb, $user ) = @_; 
    815908 
    816909    # Supply user with what they need... 
     
    820913    my $new_blog; 
    821914    my $blog_name = $user->nickname || MT->translate("First Weblog"); 
    822     if (my $blog_id = MT->config('NewUserTemplateBlogId')) { 
     915    if ( my $blog_id = MT->config('NewUserTemplateBlogId') ) { 
    823916        my $blog = MT::Blog->load($blog_id); 
    824         if (!$blog) { 
    825             MT->log({ 
    826                 message => MT->translate("Error loading blog #[_1] for user provisioning. Check your NewUserTemplateBlogId setting.", $blog_id), 
    827                 level => MT::Log::ERROR(), 
    828             }); 
     917        if ( !$blog ) { 
     918            MT->log( 
     919                {   message => MT->translate( 
     920                        "Error loading blog #[_1] for user provisioning. Check your NewUserTemplateBlogId setting.", 
     921                        $blog_id 
     922                    ), 
     923                    level => MT::Log::ERROR(), 
     924                } 
     925            ); 
    829926            return; 
    830927        } 
    831         $new_blog = $blog->clone({ 
    832             Children => 1, 
    833             Classes => { 'MT::Permission' => 0, 'MT::Association' => 0 }, 
    834             BlogName => $blog_name, 
    835         }); 
    836         if (!$new_blog) { 
    837             MT->log({ 
    838                 message => MT->translate("Error provisioning blog for new user '[_1]' using template blog #[_2].", $user->id, $blog->id), 
    839                 level => MT::Log::ERROR(), 
    840             }); 
     928        $new_blog = $blog->clone( 
     929            {   Children => 1, 
     930                Classes  => { 'MT::Permission' => 0, 'MT::Association' => 0 }, 
     931                BlogName => $blog_name, 
     932&n