Changeset 858


Ignore:
Timestamp:
11/23/08 00:47:55 (3 years ago)
Author:
ArthurClemens
Message:

Item244: improve code for checking nonwikiword flags; added code to apply this check for copying a topic as introduced with Item1873

Location:
trunk/core
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/core/lib/Foswiki/UI/Manage.pm

    r830 r858  
    6161    elsif ( $action eq 'restoreRevision' ) { 
    6262        _restoreRevision($session); 
     63    } 
     64    elsif ( $action eq 'copy' ) { 
     65        _copyTopic($session); 
    6366    } 
    6467    elsif ($action) { 
     
    297300    my $breakLock   = $query->param('breaklock'); 
    298301 
    299     my $confirm            = $query->param('confirm'); 
    300     my $doAllowNonWikiWord = $query->param('nonwikiword') || ''; 
    301     my $store              = $session->{store}; 
     302    my $confirm          = $query->param('confirm'); 
     303    my $nonWikiWordParam = $query->param('nonwikiword') || ''; 
     304    my $store            = $session->{store}; 
    302305 
    303306    $newTopic =~ s/\s//go; 
     
    324327    } 
    325328 
    326     if ( $newTopic && !Foswiki::isValidWikiWord($newTopic) ) { 
    327         unless ($doAllowNonWikiWord) { 
     329    if ($newTopic) { 
     330        if ( !_isValidTopicName( $newTopic, $nonWikiWordParam ) ) { 
    328331            throw Foswiki::OopsException( 
    329332                'attention', 
     
    334337            ); 
    335338        } 
    336  
    337         # Filter out dangerous characters (. and / may cause 
    338         # issues with pathnames 
    339         $newTopic =~ s![./]!_!g; 
    340         $newTopic =~ s/($Foswiki::cfg{NameFilter})//go; 
     339        $newTopic = _safeTopicName($newTopic); 
    341340    } 
    342341 
     
    408407            $session->{user} ); 
    409408        _newTopicScreen( $session, $oldWeb, $oldTopic, $newWeb, $newTopic, 
    410             $attachment, $confirm, $doAllowNonWikiWord ); 
     409            $attachment, $confirm, $nonWikiWordParam ); 
    411410        return; 
    412411    } 
     
    468467    #follow redirectto= 
    469468    $session->redirect( $new_url, undef, 1 ); 
     469} 
     470 
     471=pod 
     472 
     473---++ StaticMethod _isValidTopicName( $topic, $nonWikiWordParam ) -> $boolean 
     474 
     475Checks whether a topic name is valid. This may depend on the setting of session param 'nonwikiword'. 
     476 
     477Usage: 
     478        my $isValidName = _isValidTopicName( $newTopic, $query->param('nonwikiword') ); 
     479 
     480=cut 
     481 
     482sub _isValidTopicName { 
     483    my ( $topic, $nonWikiWordParam ) = @_; 
     484 
     485    my $nonWikiWord = $nonWikiWordParam || 0; 
     486    my $doAllowNonWikiWord = Foswiki::isTrue($nonWikiWord); 
     487 
     488    return 0 if !$topic; 
     489    return 0 if ( !Foswiki::isValidTopicName($topic) && !$doAllowNonWikiWord ); 
     490    return 1 if ( Foswiki::isValidTopicName($topic) ); 
     491 
     492    return 1; 
     493} 
     494 
     495=pod 
     496 
     497---++ StaticMethod _safeTopicName( $topic ) -> $topic 
     498 
     499Filter out dangerous characters . and / may cause issues with pathnames. 
     500         
     501=cut 
     502 
     503sub _safeTopicName { 
     504    my ($topic) = @_; 
     505 
     506    $topic =~ s/\s//go; 
     507    $topic = ucfirst $topic;    # Item3270 
     508    $topic =~ s![./]!_!g; 
     509    $topic =~ s/($Foswiki::cfg{NameFilter})//go; 
     510 
     511    return $topic; 
     512} 
     513 
     514=pod 
     515 
     516---++ StaticMethod _copyTopic() 
     517 
     518Copies a topic to new topic with name passed in query param 'newtopic'. 
     519Redirects to edit screen. 
     520 
     521=cut 
     522 
     523 
     524sub _copyTopic { 
     525    my ($session) = @_; 
     526 
     527    my $query = $session->{request}; 
     528    my $newTopic = $query->param('newtopic') || ''; 
     529 
     530    # topic must not be empty 
     531    if ( !$newTopic ) { 
     532        throw Foswiki::OopsException( 
     533            'attention', 
     534            web    => undef, 
     535            topic  => $newTopic, 
     536            def    => 'empty_topic_name', 
     537            params => undef 
     538        ); 
     539    } 
     540 
     541    my $oldWeb           = $session->{webName}; 
     542    my $oldTopic         = $session->{topicName}; 
     543    my $nonWikiWordParam = $query->param('nonwikiword') || ''; 
     544 
     545    if ($newTopic) { 
     546        # topic must be valid 
     547        if ( !_isValidTopicName( $newTopic, $nonWikiWordParam ) ) { 
     548            throw Foswiki::OopsException( 
     549                'attention', 
     550                web    => $oldWeb, 
     551                topic  => $oldTopic, 
     552                def    => 'not_wikiword', 
     553                params => [$newTopic] 
     554            ); 
     555        } 
     556        $newTopic = _safeTopicName($newTopic); 
     557    } 
     558     
     559    # untaint new topic name 
     560    use Foswiki::Sandbox; 
     561    $session->{topicName} = Foswiki::Sandbox::untaintUnchecked($newTopic); 
     562 
     563    require Foswiki::UI::Edit; 
     564    Foswiki::UI::Edit::edit($session); 
    470565} 
    471566 
     
    12251320 
    12261321    $tmpl = 
    1227       $session->handleCommonTags( $tmpl, $oldWeb, $Foswiki::cfg{HomeTopicName} ); 
     1322      $session->handleCommonTags( $tmpl, $oldWeb, 
     1323        $Foswiki::cfg{HomeTopicName} ); 
    12281324    $tmpl = 
    12291325      $session->renderer->getRenderedVersion( $tmpl, $oldWeb, 
     
    13411437                    \&Foswiki::Render::replaceTopicReferences, $options ); 
    13421438                $meta->forEachSelectedValue( 
    1343                     qw/^(FIELD|FORM|TOPICPARENT)$/,          undef, 
     1439                    qw/^(FIELD|FORM|TOPICPARENT)$/,            undef, 
    13441440                    \&Foswiki::Render::replaceTopicReferences, $options 
    13451441                ); 
     
    13861482                    \&Foswiki::Render::replaceWebReferences, $options ); 
    13871483                $meta->forEachSelectedValue( 
    1388                     qw/^(FIELD|FORM|TOPICPARENT)$/,        undef, 
     1484                    qw/^(FIELD|FORM|TOPICPARENT)$/,          undef, 
    13891485                    \&Foswiki::Render::replaceWebReferences, $options 
    13901486                ); 
  • trunk/core/templates/messages.tmpl

    r758 r858  
    8888%MAKETEXT{"Please go back in your browser and choose a topic name that is a [_1].WikiWord or check the allow non-Wiki Word box" args="%SYSTEMWEB%"}% 
    8989%TMPL:END% 
     90 
     91%TMPL:DEF{"empty_topic_name"}% 
     92---++ %MAKETEXT{"You must pass a topic name."}% 
     93%MAKETEXT{"The name of the topic must not be empty." args="%PARAM1%"}% 
     94 
     95%MAKETEXT{"Please go back in your browser and try again."}% 
     96%TMPL:END% 
     97 
    9098%TMPL:DEF{"rename_err"}% 
    9199%MAKETEXT{"During rename of topic [_1] to [_2] an error ([_3]) was found. Please notify your [_4] administrator."  args="<nop>%WEB%.<nop>%TOPIC%,<nop>%PARAM2%,%PARAM1%,%WIKITOOLNAME%"}% 
  • trunk/core/templates/oopsmore.tmpl

    r847 r858  
    2020%MAKETEXT{"Copy text and form data to a new topic (no attachments will be copied though)."}% 
    2121 
    22 <form action="%SCRIPTURL{edit}%/%WEB%/"> 
    23 %MAKETEXT{"Name of copy:"}% <input type="text" name="topic" class="twikiInputField" value="%TOPIC%Copy" size="30"> <input type="submit" class="twikiButton" value="Clone" /> <span class="twikiGrayText">%MAKETEXT{"You will be able to review the copied topic before saving"}%</span> 
     22<form action="%SCRIPTURL{manage}%/%WEB%/"> 
     23%MAKETEXT{"Name of copy:"}% <input type="text" name="newtopic" class="twikiInputField" value="%TOPIC%Copy" size="30"> <input type="submit" class="twikiButton" value="Clone" /> <span class="twikiGrayText">%MAKETEXT{"You will be able to review the copied topic before saving"}%</span> 
     24%TMPL:P{"nonwikiword"}% 
     25<input type="hidden" name="action" value="copy" /> 
    2426<input type="hidden" name="onlynewtopic" value="on" /> 
    2527<input type="hidden" name="templatetopic" value="%TOPIC%" /> 
Note: See TracChangeset for help on using the changeset viewer.