# TWiki Enterprise Collaboration Platform, http://TWiki.org/ # # Copyright (C) 1999-2007 Peter Thoeny, peter@thoeny.org # and TWiki Contributors. All Rights Reserved. TWiki Contributors # are listed in the AUTHORS file in the root of this distribution. # NOTE: Please extend that file, not this notice. # # Additional copyrights apply to some or all of the code in this # file as follows: # # Based on parts of Ward Cunninghams original Wiki and JosWiki. # Copyright (C) 1998 Markus Peter - SPiN GmbH (warpi@spin.de) # Some changes by Dave Harris (drh@bhresearch.co.uk) incorporated # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. For # more details read LICENSE in the root of this distribution. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. # # As per the GPL, removal of this notice is prohibited. =pod ---+ package TWiki TWiki operates by creating a singleton object (known as the Session object) that acts as a point of reference for all the different modules in the system. This package is the class for this singleton, and also contains the vast bulk of the basic constants and the per- site configuration mechanisms. Global variables are avoided wherever possible to avoid problems with CGI accelerators such as mod_perl. =cut package TWiki; use strict; use Assert; use Error qw( :try ); require 5.005; # For regex objects and internationalisation # Site configuration constants use vars qw( %cfg ); # Uncomment this and the __END__ to enable AutoLoader #use AutoLoader 'AUTOLOAD'; # You then need to autosplit TWiki.pm: # cd lib # perl -e 'use AutoSplit; autosplit("TWiki.pm", "auto")' # Other computed constants use vars qw( $TranslationToken $twikiLibDir %regex %constantTags %functionTags %contextFreeSyntax %restDispatch $VERSION $RELEASE $TRUE $FALSE $sharedSandbox ); # Token character that must not occur in any normal text - converted # to a flag character if it ever does occur (very unlikely) # TWiki uses $TranslationToken to mark points in the text. This is # normally \0, which is not a useful character in any 8-bit character # set we can find, nor in UTF-8. But if you *do* encounter problems # with it, the workaround is to change $TranslationToken to something # longer that is unlikely to occur in your text - for example # muRfleFli5ble8leep (do *not* use punctuation characters or whitspace # in the string!) # See Codev.NationalCharTokenClash for more. $TranslationToken= "\0"; =pod ---++ StaticMethod getTWikiLibDir() -> $path STATIC method. Returns the full path of the directory containing TWiki.pm =cut sub getTWikiLibDir { if( $twikiLibDir ) { return $twikiLibDir; } # FIXME: Should just use $INC{"TWiki.pm"} to get path used to load this # module. my $dir = ''; foreach $dir ( @INC ) { if( $dir && -e "$dir/TWiki.pm" ) { $twikiLibDir = $dir; last; } } # fix path relative to location of called script if( $twikiLibDir =~ /^\./ ) { print STDERR "WARNING: TWiki lib path $twikiLibDir is relative; you should make it absolute, otherwise some scripts may not run from the command line."; my $bin; if( $ENV{SCRIPT_FILENAME} && $ENV{SCRIPT_FILENAME} =~ /^(.+)\/[^\/]+$/ ) { # CGI script name $bin = $1; } elsif ( $0 =~ /^(.*)\/.*?$/ ) { # program name $bin = $1; } else { # last ditch; relative to current directory. require Cwd; import Cwd qw( cwd ); $bin = cwd(); } $twikiLibDir = "$bin/$twikiLibDir/"; # normalize "/../" and "/./" while ( $twikiLibDir =~ s|([\\/])[^\\/]+[\\/]\.\.[\\/]|$1| ) { }; $twikiLibDir =~ s|([\\/])\.[\\/]|$1|g; } $twikiLibDir =~ s|([\\/])[\\/]*|$1|g; # reduce "//" to "/" $twikiLibDir =~ s|[\\/]$||; # cut trailing "/" return $twikiLibDir; } BEGIN { use TWiki::Sandbox; # system command sandbox use TWiki::Configure::Load; # read configuration files $TRUE = 1; $FALSE = 0; if( DEBUG ) { # If ASSERTs are on, then warnings are errors. Paranoid, # but the only way to be sure we eliminate them all. # Look out also for $cfg{WarningsAreErrors}, below, which # is another way to install this handler without enabling # ASSERTs # ASSERTS are turned on by defining the environment variable # TWIKI_ASSERTS. If ASSERTs are off, this is assumed to be a # production environment, and no stack traces or paths are # output to the browser. $SIG{'__WARN__'} = sub { die @_ }; $Error::Debug = 1; # verbose stack traces, please } else { $Error::Debug = 0; # no verbose stack traces } # DO NOT CHANGE THE FORMAT OF $VERSION # automatically expanded on checkin of this module $VERSION = '$Date: 2007-01-16 05:04:44 +0100 (Tue, 16 Jan 2007) $ $Rev: 12567 $ '; $RELEASE = 'TWiki-4.1.0'; $VERSION =~ s/^.*?\((.*)\).*: (\d+) .*?$/$RELEASE, $1, build $2/; # Default handlers for different %TAGS% %functionTags = ( ALLVARIABLES => \&_ALLVARIABLES, ATTACHURL => \&_ATTACHURL, ATTACHURLPATH => \&_ATTACHURLPATH, DATE => \&_DATE, DISPLAYTIME => \&_DISPLAYTIME, ENCODE => \&_ENCODE, FORMFIELD => \&_FORMFIELD, GMTIME => \&_GMTIME, GROUPS => \&_GROUPS, HTTP_HOST => \&_HTTP_HOST, HTTP => \&_HTTP, HTTPS => \&_HTTPS, ICON => \&_ICON, ICONURL => \&_ICONURL, ICONURLPATH => \&_ICONURLPATH, IF => \&_IF, INCLUDE => \&_INCLUDE, INTURLENCODE => \&_INTURLENCODE, LANGUAGES => \&_LANGUAGES, MAKETEXT => \&_MAKETEXT, META => \&_META, METASEARCH => \&_METASEARCH, NOP => \&_NOP, PLUGINVERSION => \&_PLUGINVERSION, PUBURL => \&_PUBURL, PUBURLPATH => \&_PUBURLPATH, QUERYPARAMS => \&_QUERYPARAMS, QUERYSTRING => \&_QUERYSTRING, RELATIVETOPICPATH => \&_RELATIVETOPICPATH, REMOTE_ADDR => \&_REMOTE_ADDR, REMOTE_PORT => \&_REMOTE_PORT, REMOTE_USER => \&_REMOTE_USER, REVINFO => \&_REVINFO, SCRIPTNAME => \&_SCRIPTNAME, SCRIPTURL => \&_SCRIPTURL, SCRIPTURLPATH => \&_SCRIPTURLPATH, SEARCH => \&_SEARCH, SEP => \&_SEP, SERVERTIME => \&_SERVERTIME, SPACEDTOPIC => \&_SPACEDTOPIC, # deprecated, use SPACEOUT SPACEOUT => \&_SPACEOUT, 'TMPL:P' => \&_TMPLP, TOPICLIST => \&_TOPICLIST, URLENCODE => \&_ENCODE, URLPARAM => \&_URLPARAM, LANGUAGE => \&_LANGUAGE, USERINFO => \&_USERINFO, USERNAME => \&_USERNAME_deprecated, VAR => \&_VAR, WEBLIST => \&_WEBLIST, WIKINAME => \&_WIKINAME_deprecated, WIKIUSERNAME => \&_WIKIUSERNAME_deprecated ); $contextFreeSyntax{IF} = 1; # Constant tag strings _not_ dependent on config %constantTags = ( ENDSECTION => '', WIKIVERSION => $VERSION, STARTSECTION => '', STARTINCLUDE => '', STOPINCLUDE => '', ); unless( ( $TWiki::cfg{DetailedOS} = $^O ) ) { require Config; $TWiki::cfg{DetailedOS} = $Config::Config{'osname'}; } $TWiki::cfg{OS} = 'UNIX'; if ($TWiki::cfg{DetailedOS} =~ /darwin/i) { # MacOS X $TWiki::cfg{OS} = 'UNIX'; } elsif ($TWiki::cfg{DetailedOS} =~ /Win/i) { $TWiki::cfg{OS} = 'WINDOWS'; } elsif ($TWiki::cfg{DetailedOS} =~ /vms/i) { $TWiki::cfg{OS} = 'VMS'; } elsif ($TWiki::cfg{DetailedOS} =~ /bsdos/i) { $TWiki::cfg{OS} = 'UNIX'; } elsif ($TWiki::cfg{DetailedOS} =~ /dos/i) { $TWiki::cfg{OS} = 'DOS'; } elsif ($TWiki::cfg{DetailedOS} =~ /^MacOS$/i) { # MacOS 9 or earlier $TWiki::cfg{OS} = 'MACINTOSH'; } elsif ($TWiki::cfg{DetailedOS} =~ /os2/i) { $TWiki::cfg{OS} = 'OS2'; } # Validate and untaint Apache's SERVER_NAME Environment variable # for use in referencing virtualhost-based paths for separate data/ and templates/ instances, etc if ( $ENV{SERVER_NAME} && $ENV{SERVER_NAME} =~ /^(([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,6})$/ ) { $ENV{SERVER_NAME} = TWiki::Sandbox::untaintUnchecked( $ENV{SERVER_NAME} ); } # readConfig is defined in TWiki::Configure::Load to allow overriding it TWiki::Configure::Load::readConfig(); if( $TWiki::cfg{WarningsAreErrors} ) { # Note: Warnings are always errors if ASSERTs are enabled $SIG{'__WARN__'} = sub { die @_ }; } if( $TWiki::cfg{UseLocale} ) { require locale; import locale(); } # Constant tags dependent on the config $constantTags{AUTHREALM} = $TWiki::cfg{AuthRealm}; $constantTags{HOMETOPIC} = $TWiki::cfg{HomeTopicName}; $constantTags{MAINWEB} = $TWiki::cfg{UsersWebName}; $constantTags{TRASHWEB} = $TWiki::cfg{TrashWebName}; $constantTags{NOTIFYTOPIC} = $TWiki::cfg{NotifyTopicName}; $constantTags{SCRIPTSUFFIX} = $TWiki::cfg{ScriptSuffix}; $constantTags{LOCALSITEPREFS} = $TWiki::cfg{LocalSitePreferences}; $constantTags{STATISTICSTOPIC} = $TWiki::cfg{Stats}{TopicName}; $constantTags{TWIKIWEB} = $TWiki::cfg{SystemWebName}; $constantTags{WEBPREFSTOPIC} = $TWiki::cfg{WebPrefsTopicName}; $constantTags{DEFAULTURLHOST} = $TWiki::cfg{DefaultUrlHost}; $constantTags{WIKIPREFSTOPIC} = $TWiki::cfg{SitePrefsTopicName}; $constantTags{WIKIUSERSTOPIC} = $TWiki::cfg{UsersTopicName}; $constantTags{WIKIWEBMASTER} = $TWiki::cfg{WebMasterEmail}; $constantTags{WIKIWEBMASTERNAME} = $TWiki::cfg{WebMasterName}; if( $TWiki::cfg{NoFollow} ) { $constantTags{NOFOLLOW} = 'rel='.$TWiki::cfg{NoFollow}; } $constantTags{ALLOWLOGINNAME} = $TWiki::cfg{Register}{AllowLoginName} || 0; # locale setup # # # Note that 'use locale' must be done in BEGIN block for regexes and # sorting to # work properly, although regexes can still work without this in # 'non-locale regexes' mode. if ( $TWiki::cfg{UseLocale} ) { # Set environment variables for grep $ENV{LC_CTYPE} = $TWiki::cfg{Site}{Locale}; # Load POSIX for I18N support. require POSIX; import POSIX qw( locale_h LC_CTYPE ); # SMELL: mod_perl compatibility note: If TWiki is running under Apache, # won't this play with the Apache process's locale settings too? # What effects would this have? setlocale(&LC_CTYPE, $TWiki::cfg{Site}{Locale}); } $constantTags{CHARSET} = $TWiki::cfg{Site}{CharSet}; $constantTags{SHORTLANG} = $TWiki::cfg{Site}{Lang}; $constantTags{LANG} = $TWiki::cfg{Site}{FullLang}; # Set up pre-compiled regexes for use in rendering. All regexes with # unchanging variables in match should use the '/o' option. # In the regex hash, all precompiled REs have "Regex" at the # end of the name. Anything else is a string, either intended # for use as a character class, or as a sub-expression in # another compiled RE. # Build up character class components for use in regexes. # Depends on locale mode and Perl version, and finally on # whether locale-based regexes are turned off. if ( not $TWiki::cfg{UseLocale} or $] < 5.006 or not $TWiki::cfg{Site}{LocaleRegexes} ) { # No locales needed/working, or Perl 5.005, so just use # any additional national characters defined in TWiki.cfg $regex{upperAlpha} = 'A-Z'.$TWiki::cfg{UpperNational}; $regex{lowerAlpha} = 'a-z'.$TWiki::cfg{LowerNational}; $regex{numeric} = '\d'; $regex{mixedAlpha} = $regex{upperAlpha}.$regex{lowerAlpha}; } else { # Perl 5.006 or higher with working locales $regex{upperAlpha} = '[:upper:]'; $regex{lowerAlpha} = '[:lower:]'; $regex{numeric} = '[:digit:]'; $regex{mixedAlpha} = '[:alpha:]'; } $regex{mixedAlphaNum} = $regex{mixedAlpha}.$regex{numeric}; $regex{lowerAlphaNum} = $regex{lowerAlpha}.$regex{numeric}; $regex{upperAlphaNum} = $regex{upperAlpha}.$regex{numeric}; # Compile regexes for efficiency and ease of use # Note: qr// locks in regex modes (i.e. '-xism' here) - see Friedl # book at http://regex.info/. $regex{linkProtocolPattern} = $TWiki::cfg{LinkProtocolPattern}; # Header patterns based on '+++'. The '###' are reserved for numbered # headers # '---++ Header', '---## Header' $regex{headerPatternDa} = qr/^---+(\++|\#+)(.*)$/m; # '
is archaic. # SMELL: use forEachLine foreach my $line ( split( /\r?\n/, $text ) ) { my $level; if ( $line =~ m/$regex{headerPatternDa}/o ) { $line = $2; $level = length $1; } elsif ( $line =~ m/$regex{headerPatternHt}/io ) { $line = $2; $level = $1; } else { next; } if( $line && $level <= $depth ) { # cut TOC exclude '---+ heading !! exclude this bit' $line =~ s/\s*$regex{headerPatternNoTOC}.+$//go; next unless $line; my $anchor = $this->{renderer}->makeAnchorName( $line ); $highest = $level if( $level < $highest ); my $tabs = "\t" x $level; # Remove *bold*, _italic_ and =fixed= formatting $line =~ s/(^|[\s\(])\*([^\s]+?|[^\s].*?[^\s])\*($|[\s\,\.\;\:\!\?\)])/$1$2$3/g; $line =~ s/(^|[\s\(])_+([^\s]+?|[^\s].*?[^\s])_+($|[\s\,\.\;\:\!\?\)])/$1$2$3/g; $line =~ s/(^|[\s\(])=+([^\s]+?|[^\s].*?[^\s])=+($|[\s\,\.\;\:\!\?\)])/$1$2$3/g; # Prevent WikiLinks $line =~ s/\[\[.*?\]\[(.*?)\]\]/$1/g; # '[[...][...]]' $line =~ s/\[\[(.*?)\]\]/$1/ge; # '[[...]]' $line =~ s/([\s\(])($regex{webNameRegex})\.($regex{wikiWordRegex})/$1$3/go; # 'Web.TopicName' $line =~ s/([\s\(])($regex{wikiWordRegex})/$1 $2/go; # 'TopicName' $line =~ s/([\s\(])($regex{abbrevRegex})/$1 $2/go; # 'TLA' $line =~ s/([\s\-\*\(])([$regex{mixedAlphaNum}]+\:)/$1 $2/go; # 'Site:page' Interwiki link # Prevent manual links $line =~ s/<[\/]?a\b[^>]*>//gi; # create linked bullet item, using a relative link to anchor my $target = $isSameTopic ? _make_params(0, '#'=>$anchor,@qparams) : $this->getScriptUrl(0,'view',$web,$topic,'#'=>$anchor,@qparams); $line = $tabs.'* ' . CGI::a({href=>$target},$line); $result .= "\n".$line; } } if( $result ) { if( $highest > 1 ) { # left shift TOC $highest--; $result =~ s/^\t{$highest}//gm; } return CGI::div( { class=>'twikiToc' }, "$title$result\n" ); } else { return ''; } } =pod ---++ ObjectMethod inlineAlert($template, $def, ... ) -> $string Format an error for inline inclusion in rendered output. The message string is obtained from the template 'oops'.$template, and the DEF $def is selected. The parameters (...) are used to populate %PARAM1%..%PARAMn% =cut sub inlineAlert { my $this = shift; my $template = shift; my $def = shift; my $text = $this->{templates}->readTemplate( 'oops'.$template, $this->getSkin() ); if( $text ) { my $blah = $this->{templates}->expandTemplate( $def ); $text =~ s/%INSTANTIATE%/$blah/; # web and topic can be anything; they are not used $text = $this->handleCommonTags( $text, $this->{webName}, $this->{topicName} ); my $n = 1; while( defined( my $param = shift )) { $text =~ s/%PARAM$n%/$param/g; $n++; } } else { $text = CGI::h1('TWiki Installation Error') . 'Template "'.$template.'" not found.'.CGI::p() . 'Check the configuration setting for {TemplateDir}'; } return $text; } =pod ---++ StaticMethod parseSections($text) -> ($string,$sectionlistref) Generic parser for sections within a topic. Sections are delimited by STARTSECTION and ENDSECTION, which may be nested, overlapped or otherwise abused. The parser builds an array of sections, which is ordered by the order of the STARTSECTION within the topic. It also removes all the SECTION tags from the text, and returns the text and the array of sections. Each section is a =TWiki::Attrs= object, which contains the attributes {type, name, start, end} where start and end are character offsets in the string *after all section tags have been removed*. All sections are required to be uniquely named; if a section is unnamed, it will be given a generated name. Sections may overlap or nest. See test/unit/Fn_SECTION.pm for detailed testcases that round out the spec. =cut sub parseSections { #my( $text _ = @_; my %sections; my @list = (); my $seq = 0; my $ntext = ''; my $offset = 0; foreach my $bit (split(/(%(?:START|END)SECTION(?:{.*?})?%)/, $_[0] )) { if( $bit =~ /^%STARTSECTION(?:{(.*)})?%$/) { my $attrs = new TWiki::Attrs( $1 ); $attrs->{type} ||= 'section'; $attrs->{name} = $attrs->{_DEFAULT} || $attrs->{name} || '_SECTION'.$seq++; delete $attrs->{_DEFAULT}; my $id = $attrs->{type}.':'.$attrs->{name}; if( $sections{$id} ) { # error, this named section already defined, ignore next; } # close open unnamed sections of the same type foreach my $s ( @list ) { if( $s->{end} < 0 && $s->{type} eq $attrs->{type} && $s->{name} =~ /^_SECTION\d+$/ ) { $s->{end} = $offset; } } $attrs->{start} = $offset; $attrs->{end} = -1; # open section $sections{$id} = $attrs; push( @list, $attrs ); } elsif( $bit =~ /^%ENDSECTION(?:{(.*)})?%$/ ) { my $attrs = new TWiki::Attrs( $1 ); $attrs->{type} ||= 'section'; $attrs->{name} = $attrs->{_DEFAULT} || $attrs->{name} || ''; delete $attrs->{_DEFAULT}; unless( $attrs->{name} ) { # find the last open unnamed section of this type foreach my $s ( reverse @list ) { if( $s->{end} == -1 && $s->{type} eq $attrs->{type} && $s->{name} =~ /^_SECTION\d+$/ ) { $attrs->{name} = $s->{name}; last; } } # ignore it if no matching START found next unless $attrs->{name}; } my $id = $attrs->{type}.':'.$attrs->{name}; if( !$sections{$id} || $sections{$id}->{end} >= 0 ) { # error, no such open section, ignore next; } $sections{$id}->{end} = $offset; } else { $ntext .= $bit; $offset = length( $ntext ); } } # close open sections foreach my $s ( @list ) { $s->{end} = $offset if $s->{end} < 0; } return( $ntext, \@list ); } =pod ---++ ObjectMethod expandVariablesOnTopicCreation ( $text, $user ) -> $text * =$text= - text to expand * =$user= - reference to user object. This is the user expanded in e.g. %USERNAME. Optional, defaults to logged-in user. Expand limited set of variables during topic creation. These are variables expected in templates that must be statically expanded in new content. # SMELL: no plugin handler =cut sub expandVariablesOnTopicCreation { my ( $this, $text, $user ) = @_; ASSERT($this->isa( 'TWiki')) if DEBUG; $user ||= $this->{user}; ASSERT($user->isa( 'TWiki::User')) if DEBUG; # Chop out templateonly sections my( $ntext, $sections ) = parseSections( $text ); if( scalar( @$sections )) { # Note that if named templateonly sections overlap, the behaviour is undefined. foreach my $s ( reverse @$sections ) { if( $s->{type} eq 'templateonly' ) { $ntext = substr($ntext, 0, $s->{start}). substr($ntext, $s->{end}, length($ntext)); } else { # put back non-templateonly sections my $start = $s->remove('start'); my $end = $s->remove('end'); $ntext = substr($ntext, 0, $start). '%STARTSECTION{'.$s->stringify().'}%'. substr($ntext, $start, $end - $start). '%ENDSECTION{'.$s->stringify().'}%'. substr($ntext, $end, length($ntext)); } } $text = $ntext; } # Make sure func works, for registered tag handlers $TWiki::Plugins::SESSION = $this; # Note: it may look dangerous to override the user this way, but # it's actually quite safe, because only a subset of tags are # expanded during topic creation. if the set of tags expanded is # extended, then the impact has to be considered. my $safe = $this->{user}; $this->{user} = $user; $text = $this->_processTags( $text, \&_expandTagOnTopicCreation, 16 ); # kill markers used to prevent variable expansion $text =~ s/%NOP%//g; $this->{user} = $safe; return $text; } =pod ---++ StaticMethod entityEncode( $text, $extras ) -> $encodedText Escape special characters to HTML numeric entities. This is *not* a generic encoding, it is tuned specifically for use in TWiki. HTML4.0 spec: "Certain characters in HTML are reserved for use as markup and must be escaped to appear literally. The "<" character may be represented with an entity, <. Similarly, ">" is escaped as >, and "&" is escaped as &. If an attribute value contains a double quotation mark and is delimited by double quotation marks, then the quote should be escaped as ". Other entities exist for special characters that cannot easily be entered with some keyboards..." This method encodes HTML special and any non-printable ascii characters (except for \n and \r) using numeric entities. FURTHER this method also encodes characters that are special in TWiki meta-language. $extras is an optional param that may be used to include *additional* characters in the set of encoded characters. It should be a string containing the additional chars. =cut sub entityEncode { my( $text, $extra) = @_; $extra ||= ''; # encode all non-printable 7-bit chars (< \x1f), # except \n (\xa) and \r (\xd) # encode HTML special characters '>', '<', '&', ''' and '"'. # encode TML special characters '%', '|', '[', ']', '@', '_', # '*', and '=' $text =~ s/([[\x01-\x09\x0b\x0c\x0e-\x1f"%&'*<=>@[_\|$extra])/''.ord($1).';'/ge; return $text; } =pod ---++ StaticMethod entityDecode ( $encodedText ) -> $text Decodes all numeric entities (e.g. {). _Does not_ decode named entities such as & (use HTML::Entities for that) =cut sub entityDecode { my $text = shift; $text =~ s/(\d+);/chr($1)/ge; return $text; } =pod ---++ StaticMethod urlEncode( $string ) -> encoded string Encode by converting characters that are illegal in URLs to their %NN equivalents. This method is used for encoding strings that must be embedded _verbatim_ in URLs; it cannot be applied to URLs themselves, as it escapes reserved characters such as = and ?. RFC 1738, Dec. '94: > ...Only alphanumerics [0-9a-zA-Z], the special characters $-_.+!*'(), and reserved characters used for their reserved purposes may be used unencoded within a URL. Reserved characters are $&+,/:;=?@ - these are _also_ encoded by this method. SMELL: For non-ISO-8859-1 $TWiki::cfg{Site}{CharSet}, need to convert to UTF-8 before URL encoding. This encoding only supports 8-bit character codes. =cut sub urlEncode { my $text = shift; $text =~ s/([^0-9a-zA-Z-_.:~!*'()\/%])/'%'.sprintf('%02x',ord($1))/ge; return $text; } =pod ---++ StaticMethod urlDecode( $string ) -> decoded string Reverses the encoding done in urlEncode. =cut sub urlDecode { my $text = shift; $text =~ s/%([\da-f]{2})/chr(hex($1))/gei; return $text; } =pod ---++ StaticMethod isTrue( $value, $default ) -> $boolean Returns 1 if =$value= is true, and 0 otherwise. "true" means set to something with a Perl true value, with the special cases that "off", "false" and "no" (case insensitive) are forced to false. Leading and trailing spaces in =$value= are ignored. If the value is undef, then =$default= is returned. If =$default= is not specified it is taken as 0. =cut sub isTrue { my( $value, $default ) = @_; $default ||= 0; return $default unless defined( $value ); $value =~ s/^\s*(.*?)\s*$/$1/gi; $value =~ s/off//gi; $value =~ s/no//gi; return ( $value ) ? 1 : 0; } =pod ---++ StaticMethod spaceOutWikiWord( $word, $sep ) -> $string Spaces out a wiki word by inserting a string (default: one space) between each word component. With parameter $sep any string may be used as separator between the word components; if $sep is undefined it defaults to a space. =cut sub spaceOutWikiWord { my $word = shift || ''; my $sep = shift || ' '; $word =~ s/([$regex{lowerAlpha}])([$regex{upperAlpha}$regex{numeric}]+)/$1$sep$2/go; $word =~ s/([$regex{numeric}])([$regex{upperAlpha}])/$1$sep$2/go; return $word; } # Expands variables by replacing the variables with their # values. Some example variables: %TOPIC%, % SCRIPTURL%, # % WIKINAME%, etc. # $web and $incs are passed in for recursive include expansion. They can # safely be undef. # The rules for tag expansion are: # 1 Tags are expanded left to right, in the order they are encountered. # 1 Tags are recursively expanded as soon as they are encountered - the algorithm is inherently single-pass # 1 A tag is not "encountered" until the matching }% has been seen, by which time all tags in parameters will have been expanded # 1 Tag expansions that create new tags recursively are limited to a set number of hierarchical levels of expansion # # Formerly known as handleInternalTags, but renamed when it was rewritten # because the old name clashes with the namespace of handlers. sub _expandAllTags { my $this = shift; my $text = shift; # reference my ( $topic, $web ) = @_; $web =~ s#\.#/#go; # push current context my $memTopic = $this->{SESSION_TAGS}{TOPIC}; my $memWeb = $this->{SESSION_TAGS}{WEB}; $this->{SESSION_TAGS}{TOPIC} = $topic; $this->{SESSION_TAGS}{WEB} = $web; # Escape ' !%VARIABLE%' $$text =~ s/(?<=\s)!%($regex{tagNameRegex})/%$1/g; # Make sure func works, for registered tag handlers $TWiki::Plugins::SESSION = $this; # NOTE TO DEBUGGERS # The depth parameter in the following call controls the maximum number # of levels of expansion. If it is set to 1 then only tags in the # topic will be expanded; tags that they in turn generate will be # left unexpanded. If it is set to 2 then the expansion will stop after # the first recursive inclusion, and so on. This is incredible useful # when debugging. The default is set to 16 # to match the original limit on search expansion, though this of # course applies to _all_ tags and not just search. $$text = $this->_processTags( $$text, \&_expandTagOnTopicRendering, 16, @_ ); # restore previous context $this->{SESSION_TAGS}{TOPIC} = $memTopic; $this->{SESSION_TAGS}{WEB} = $memWeb; } # Process TWiki %TAGS{}% by parsing the input tokenised into # % separated sections. The parser is a simple stack-based parse, # sufficient to ensure nesting of tags is correct, but no more # than that. # $depth limits the number of recursive expansion steps that # can be performed on expanded tags. sub _processTags { my $this = shift; my $text = shift; my $tagf = shift; return '' unless defined( $text ); my $depth = shift; # my( $topic, $web ) = @_; unless ( $depth ) { my $mess = "Max recursive depth reached: $text"; $this->writeWarning( $mess ); # prevent recursive expansion that just has been detected # from happening in the error message $text =~ s/%(.*?)%/$1/go; return $text; } my $verbatim = {}; $text = $this->{renderer}->takeOutBlocks( $text, 'verbatim', $verbatim); # See Item1442 #my $percent = ($TranslationToken x 3).'%'.($TranslationToken x 3); my @queue = split( /(%)/, $text ); my @stack; my $stackTop = ''; # the top stack entry. Done this way instead of # referring to the top of the stack for efficiency. This var # should be considered to be $stack[$#stack] #my $tell = 1; # uncomment all tell lines set this to 1 to print debugging while ( scalar( @queue )) { my $token = shift( @queue ); #print STDERR ' ' x $tell,"PROCESSING $token \n" if $tell; # each % sign either closes an existing stacked context, or # opens a new context. if ( $token eq '%' ) { #print STDERR ' ' x $tell,"CONSIDER $stackTop\n" if $tell; # If this is a closing }%, try to rejoin the previous # tokens until we get to a valid tag construct. This is # a bit of a hack, but it's hard to think of a better # way to do this without a full parse that takes % signs # in tag parameters into account. if ( $stackTop =~ /}$/s ) { while ( scalar( @stack) && $stackTop !~ /^%($regex{tagNameRegex}){.*}$/so ) { my $top = $stackTop; #print STDERR ' ' x $tell,"COLLAPSE $top \n" if $tell; $stackTop = pop( @stack ) . $top; } } # /s so you can have newlines in parameters if ( $stackTop =~ m/^%(($regex{tagNameRegex})(?:{(.*)})?)$/so ) { my( $expr, $tag, $args ) = ( $1, $2, $3 ); #print STDERR ' ' x $tell,"POP $tag\n" if $tell; my $e = &$tagf( $this, $tag, $args, @_ ); if ( defined( $e )) { #print STDERR ' ' x $tell--,"EXPANDED $tag -> $e\n" if $tell; $stackTop = pop( @stack ); # Recursively expand tags in the expansion of $tag $stackTop .= $this->_processTags($e, $tagf, $depth-1, @_ ); } else { # expansion failed #print STDERR ' ' x $tell++,"EXPAND $tag FAILED\n" if $tell; # To handle %NOP # correctly, we have to handle the %VAR% case differently # to the %VAR{}% case when a variable expansion fails. # This is so that recursively define variables e.g. # %A%B%D% expand correctly, but at the same time we ensure # that a mismatched }% can't accidentally close a context # that was left open when a tag expansion failed. # However Cairo didn't do this, so for compatibility # we have to accept that %NOP can never be fixed. if it # could, then we could uncomment the following: #if( $stackTop =~ /}$/ ) { # # %VAR{...}% case # # We need to push the unexpanded expression back # # onto the stack, but we don't want it to match the # # tag expression again. So we protect the %'s # $stackTop = $percent.$expr.$percent; #} else { # %VAR% case. # In this case we *do* want to match the tag expression # again, as an embedded %VAR% may have expanded to # create a valid outer expression. This is directly # at odds with the %VAR{...}% case. push( @stack, $stackTop ); $stackTop = '%'; # open new context } } } else { push( @stack, $stackTop ); $stackTop = '%'; # push a new context #$tell++ if ( $tell ); } } else { $stackTop .= $token; } } # Run out of input. Gather up everything in the stack. while ( scalar( @stack )) { my $expr = $stackTop; $stackTop = pop( @stack ); $stackTop .= $expr; } #$stackTop =~ s/$percent/%/go; $this->{renderer}->putBackBlocks( \$stackTop, $verbatim, 'verbatim' ); #print STDERR "FINAL $stackTop\n" if $tell; return $stackTop; } # Handle expansion of a tag during topic rendering # $tag is the tag name # $args is the bit in the {} (if there are any) # $topic and $web should be passed for dynamic tags (not needed for # session or constant tags sub _expandTagOnTopicRendering { my $this = shift; my $tag = shift; my $args = shift; # my( $topic, $web ) = @_; my $e = $this->{prefs}->getPreferencesValue( $tag ); unless( defined( $e )) { $e = $this->{SESSION_TAGS}{$tag}; unless( defined( $e )) { $e = $constantTags{$tag}; } if( !defined( $e ) && defined( $functionTags{$tag} )) { $e = &{$functionTags{$tag}} ( $this, new TWiki::Attrs( $args, $contextFreeSyntax{$tag} ), @_ ); } } return $e; } # Handle expansion of a tag during new topic creation. When creating a # new topic from a template we only expand a subset of the available legal # tags, and we expand %NOP% differently. sub _expandTagOnTopicCreation { my $this = shift; # my( $tag, $args, $topic, $web ) = @_; # Required for Cairo compatibility. Ignore %NOP{...}% # %NOP% is *not* ignored until all variable expansion is complete, # otherwise them inside-out rule would remove it too early e.g. # %GM%NOP%TIME -> %GMTIME -> 12:00. So we ignore it here and scrape it # out later. We *have* to remove %NOP{...}% because it can foul up # brace-matching. return '' if $_[0] eq 'NOP' && defined $_[1]; # Only expand a subset of legal tags. Warning: $this->{user} may be # overridden during this call, when a new user topic is being created. # This is what we want to make sure new user templates are populated # correctly, but you need to think about this if you extend the set of # tags expanded here. return undef unless $_[0] =~ /^(URLPARAM|DATE|(SERVER|GM)TIME|(USER|WIKI)NAME|WIKIUSERNAME|USERINFO)$/; return $this->_expandTagOnTopicRendering( @_ ); } =pod ---++ ObjectMethod enterContext( $id, $val ) Add the context id $id into the set of active contexts. The $val can be anything you like, but should always evaluate to boolean TRUE. An example of the use of contexts is in the use of tag expansion. The commonTagsHandler in plugins is called every time tags need to be expanded, and the context of that expansion is signalled by the expanding module using a context id. So the forms module adds the context id "form" before invoking common tags expansion. Contexts are not just useful for tag expansion; they are also relevant when rendering. Contexts are intended for use mainly by plugins. Core modules can use $session->inContext( $id ) to determine if a context is active. =cut sub enterContext { my( $this, $id, $val ) = @_; $val ||= 1; $this->{context}->{$id} = $val; } =pod ---++ ObjectMethod leaveContext( $id ) Remove the context id $id from the set of active contexts. (see =enterContext= for more information on contexts) =cut sub leaveContext { my( $this, $id ) = @_; my $res = $this->{context}->{$id}; delete $this->{context}->{$id}; return $res; } =pod ---++ ObjectMethod inContext( $id ) Return the value for the given context id (see =enterContext= for more information on contexts) =cut sub inContext { my( $this, $id ) = @_; return $this->{context}->{$id}; } =pod ---++ StaticMethod registerTagHandler( $tag, $fnref ) STATIC Add a tag handler to the function tag handlers. * =$tag= name of the tag e.g. MYTAG * =$fnref= Function to execute. Will be passed ($session, \%params, $web, $topic ) =cut sub registerTagHandler { my ( $tag, $fnref, $syntax ) = @_; $functionTags{$tag} = \&$fnref; if( $syntax && $syntax eq 'context-free' ) { $contextFreeSyntax{$tag} = 1; } } =pod= ---++ StaticMethod registerRESTHandler( $subject, $verb, \&fn ) Adds a function to the dispatch table of the REST interface for a given subject. See TWikiScripts#rest for more info. * =$subject= - The subject under which the function will be registered. * =$verb= - The verb under which the function will be registered. * =\&fn= - Reference to the function. The handler function must be of the form: sub handler(\%session,$subject,$verb) -> $text where: * =\%session= - a reference to the TWiki session object (may be ignored) * =$subject= - The invoked subject (may be ignored) * =$verb= - The invoked verb (may be ignored) *Since:* TWiki::Plugins::VERSION 1.1 =cut= sub registerRESTHandler { my ( $subject, $verb, $fnref) = @_; $restDispatch{$subject}{$verb} = \&$fnref; } =pod= ---++ StaticMethod restDispatch( $subject, $verb) => \&fn Returns the handler function associated to the given $subject and $werb, or undef if none is found. *Since:* TWiki::Plugins::VERSION 1.1 =cut= sub restDispatch { my ( $subject, $verb) = @_; my $s=$restDispatch{$subject}; if (defined($s)) { return $restDispatch{$subject}{$verb}; } else { return undef; } } =pod ---++ ObjectMethod handleCommonTags( $text, $web, $topic ) -> $text Processes %VARIABLE%, and % TOC% syntax; also includes 'commonTagsHandler' plugin hook. Returns the text of the topic, after file inclusion, variable substitution, table-of-contents generation, and any plugin changes from commonTagsHandler. =cut sub handleCommonTags { my( $this, $text, $theWeb, $theTopic ) = @_; ASSERT($this->isa( 'TWiki')) if DEBUG; ASSERT($theWeb) if DEBUG; ASSERT($theTopic) if DEBUG; return $text unless $text; my $verbatim={}; # Plugin Hook (for cache Plugins only) $this->{plugins}->beforeCommonTagsHandler( $text, $theTopic, $theWeb ); #use a "global var", so included topics can extract and putback #their verbatim blocks safetly. $text = $this->{renderer}->takeOutBlocks( $text, 'verbatim', $verbatim); my $memW = $this->{SESSION_TAGS}{INCLUDINGWEB}; my $memT = $this->{SESSION_TAGS}{INCLUDINGTOPIC}; $this->{SESSION_TAGS}{INCLUDINGWEB} = $theWeb; $this->{SESSION_TAGS}{INCLUDINGTOPIC} = $theTopic; $this->_expandAllTags( \$text, $theTopic, $theWeb ); $text = $this->{renderer}->takeOutBlocks( $text, 'verbatim', $verbatim); # Plugin Hook $this->{plugins}->commonTagsHandler( $text, $theTopic, $theWeb, 0 ); # process tags again because plugin hook may have added more in $this->_expandAllTags( \$text, $theTopic, $theWeb ); $this->{SESSION_TAGS}{INCLUDINGWEB} = $memW; $this->{SESSION_TAGS}{INCLUDINGTOPIC} = $memT; # 'Special plugin tag' TOC hack, must be done after all other expansions # are complete, and has to reprocess the entire topic. $text =~ s/%TOC(?:{(.*?)})?%/$this->_TOC($text, $theTopic, $theWeb, $1)/ge; # Codev.FormattedSearchWithConditionalOutput: remove lines, # possibly introduced by SEARCHes with conditional CALC. This needs # to be done after CALC and before table rendering in order to join # table rows properly $text =~ s/^ \r?\n//gm; $this->{renderer}->putBackBlocks( \$text, $verbatim, 'verbatim' ); # TWiki Plugin Hook (for cache Plugins only) $this->{plugins}->afterCommonTagsHandler( $text, $theTopic, $theWeb ); return $text; } =pod ---++ ObjectMethod addToHEAD( $id, $html ) Add =$html= to the HEAD tag of the page currently being generated. Note that TWiki variables may be used in the HEAD. They will be expanded according to normal variable expansion rules. The 'id' is used to ensure that multiple adds of the same block of HTML don't result in it being added many times. =cut sub addToHEAD { my ($this,$tag,$header) = @_; ASSERT($this->isa( 'TWiki')) if DEBUG; $header = $this->handleCommonTags( $header, $this->{webName}, $this->{topicName} ); $this->{htmlHeaders}{$tag} = $header; } =pod ---++ StaticMethod initialize( $pathInfo, $remoteUser, $topic, $url, $query ) -> ($topicName, $webName, $scriptUrlPath, $userName, $dataDir) Return value: ( $topicName, $webName, $TWiki::cfg{ScriptUrlPath}, $userName, $TWiki::cfg{DataDir} ) Static method to construct a new singleton session instance. It creates a new TWiki and sets the Plugins $SESSION variable to point to it, so that TWiki::Func methods will work. This method is *DEPRECATED* but is maintained for script compatibility. Note that $theUrl, if specified, must be identical to $query->url() =cut sub initialize { my ( $pathInfo, $theRemoteUser, $topic, $theUrl, $query ) = @_; if( !$query ) { $query = new CGI( {} ); } if( $query->path_info() ne $pathInfo ) { $query->path_info( $pathInfo ); } if( $topic ) { $query->param( -name => 'topic', -value => '' ); } # can't do much if $theUrl is specified and it is inconsistent with # the query. We are trying to get to all parameters passed in the # query. if( $theUrl && $theUrl ne $query->url()) { die 'Sorry, this version of TWiki does not support the url parameter to TWiki::initialize being different to the url in the query'; } my $twiki = new TWiki( $theRemoteUser, $query ); # Force the new session into the plugins context. $TWiki::Plugins::SESSION = $twiki; return ( $twiki->{topicName}, $twiki->{webName}, $twiki->{scriptUrlPath}, $twiki->{userName}, $TWiki::cfg{DataDir} ); } =pod ---++ StaticMethod readFile( $filename ) -> $text Returns the entire contents of the given file, which can be specified in any format acceptable to the Perl open() function. Fast, but inherently unsafe. WARNING: Never, ever use this for accessing topics or attachments! Use the Store API for that. This is for global control files only, and should be used *only* if there is *absolutely no alternative*. =cut sub readFile { my $name = shift; open( IN_FILE, "<$name" ) || return ''; local $/ = undef; my $data = ; close( IN_FILE ); $data = '' unless( defined( $data )); return $data; } sub _FORMFIELD { my ( $this, $params, $topic, $web ) = @_; my $cgiQuery = $this->{cgiQuery}; my $cgiRev = $cgiQuery->param('rev') if( $cgiQuery ); $params->{rev} = $cgiRev; return $this->{renderer}->renderFORMFIELD( $params, $topic, $web ); } sub _TMPLP { my( $this, $params ) = @_; return $this->{templates}->tmplP( $params ); } sub _VAR { my( $this, $params, $topic, $inweb ) = @_; my $key = $params->{_DEFAULT}; my $web = $params->{web} || $inweb; # handle %MAINWEB%-type cases ( $web, $topic ) = $this->normalizeWebTopicName( $web, $topic ); # always return a value, even when the key isn't defined return $this->{prefs}->getWebPreferencesValue( $key, $web ) || ''; } sub _PLUGINVERSION { my( $this, $params ) = @_; $this->{plugins}->getPluginVersion( $params->{_DEFAULT} ); } my $ifFactory; sub _IF { my ( $this, $params ) = @_; unless( $ifFactory ) { require TWiki::If; $ifFactory = new TWiki::If(); } my $expr = $ifFactory->parse( $params->{_DEFAULT} ); return $this->inlineAlert( 'alerts', 'generic', 'IF{', $params->stringify(), '}:', $ifFactory->{error} ) unless $expr; if( $expr->evaluate( $this )) { return $params->{then} || ''; } else { return $params->{else} || ''; } } # generate an include warning # SMELL: varying number of parameters idiotic to handle for customized $warn sub _includeWarning { my $this = shift; my $warn = shift; my $message = shift; if( $warn eq 'on' ) { return $this->inlineAlert( 'alerts', $message, @_ ); } elsif( isTrue( $warn )) { # different inlineAlerts need different argument counts my $argument = ''; if ($message eq 'topic_not_found') { my ($web,$topic) = @_; $argument = "$web.$topic"; } else { $argument = shift; } $warn =~ s/\$topic/$argument/go if $argument; return $warn; } # else fail silently return ''; } # Processes a specific instance % INCLUDE{...}% syntax. # Returns the text to be inserted in place of the INCLUDE command. # $topic and $web should be for the immediate parent topic in the # include hierarchy. Works for both URLs and absolute server paths. sub _INCLUDE { my ( $this, $params, $includingTopic, $includingWeb ) = @_; # remember args for the key before mangling the params my $args = $params->stringify(); # Remove params, so they don't get expanded in the included page my $path = $params->remove('_DEFAULT') || ''; my $pattern = $params->remove('pattern'); my $rev = $params->remove('rev'); my $section = $params->remove('section'); my $raw = $params->remove('raw') || ''; my $warn = $params->remove('warn') || $this->{prefs}->getPreferencesValue( 'INCLUDEWARNING' ); if( $path =~ /^https?\:/ ) { # include web page return $this->_includeUrl( $path, $pattern, $includingWeb, $includingTopic, $raw, $params, $warn ); } $path =~ s/$TWiki::cfg{NameFilter}//go; # zap anything suspicious if( $TWiki::cfg{DenyDotDotInclude} ) { # Filter out '..' from filename, this is to # prevent includes of '../../file' $path =~ s/\.+/\./g; } else { # danger, could include .htpasswd with relative path $path =~ s/passwd//gi; # filter out passwd filename } # make sure we have something to include. If we don't do this, then # normalizeWebTopicName will default to WebHome. Item2209. unless( $path ) { # SMELL: could do with a different message here, but don't want to # add one right now because translators are already working return $this->_includeWarning( $warn, 'topic_not_found', '""','""' ); } my $text = ''; my $meta = ''; my $includedWeb; my $includedTopic = $path; $includedTopic =~ s/\.txt$//; # strip optional (undocumented) .txt ($includedWeb, $includedTopic) = $this->normalizeWebTopicName($includingWeb, $includedTopic); # See Codev.FailedIncludeWarning for the history. unless( $this->{store}->topicExists($includedWeb, $includedTopic)) { return $this->_includeWarning( $warn, 'topic_not_found', $includedWeb, $includedTopic ); } # prevent recursive includes. Note that the inclusion of a topic into # itself is not blocked; however subsequent attempts to include the # topic will fail. There is a hard block of 99 on any recursive include. my $key = $includingWeb.'.'.$includingTopic; my $count = grep( $key, keys %{$this->{includes}}); $key .= $args; if( $this->{includes}->{$key} || $count > 99) { return $this->_includeWarning( $warn, 'already_included', "$includedWeb.$includedTopic", '' ); } my %saveTags = %{$this->{SESSION_TAGS}}; my $prefsMark = $this->{prefs}->mark(); $this->{includes}->{$key} = 1; $this->{SESSION_TAGS}{INCLUDINGWEB} = $includingWeb; $this->{SESSION_TAGS}{INCLUDINGTOPIC} = $includingTopic; # copy params into session tags foreach my $k ( keys %$params ) { $this->{SESSION_TAGS}{$k} = $params->{$k}; } ( $meta, $text ) = $this->{store}->readTopic( undef, $includedWeb, $includedTopic, $rev ); unless( $this->{security}->checkAccessPermission( 'VIEW', $this->{user}, $text, $meta, $includedTopic, $includedWeb )) { if( isTrue( $warn )) { return $this->inlineAlert( 'alerts', 'access_denied', $includedTopic ); } # else fail silently return ''; } # remove everything before and after the selected include block if( !$section ) { $text =~ s/.*?%STARTINCLUDE%//s; $text =~ s/%STOPINCLUDE%.*//s; } # handle sections my( $ntext, $sections ) = parseSections( $text ); my $interesting = ( defined $section ); if( scalar( @$sections )) { # Rebuild the text from the sections $text = ''; foreach my $s ( @$sections ) { if( $section && $s->{type} eq 'section' && $s->{name} eq $section) { $text .= substr( $ntext, $s->{start}, $s->{end}-$s->{start} ); $interesting = 1; last; } elsif( $s->{type} eq 'include' && !$section ) { $text .= substr( $ntext, $s->{start}, $s->{end}-$s->{start} ); $interesting = 1; } } } # If there were no interesting sections, restore the whole text $text = $ntext unless $interesting; $text = applyPatternToIncludedText( $text, $pattern ) if( $pattern ); $this->_expandAllTags( \$text, $includedTopic, $includedWeb ); # 4th parameter tells plugin that its called for an included file $this->{plugins}->commonTagsHandler( $text, $includedTopic, $includedWeb, 1 ); # We have to expand tags again, because a plugin may have inserted additional # tags. $this->_expandAllTags( \$text, $includedTopic, $includedWeb ); # If needed, fix all 'TopicNames' to 'Web.TopicNames' to get the # right context so that links continue to work properly if( $includedWeb ne $includingWeb ) { my $removed = {}; # Must handle explicit [[]] before noautolink # '[[TopicName]]' to '[[Web.TopicName][TopicName]]' $text =~ s/\[\[([^\]]+)\]\]/&_fixIncludeLink( $includedWeb, $1 )/geo; # '[[TopicName][...]]' to '[[Web.TopicName][...]]' $text =~ s/\[\[([^\]]+)\]\[([^\]]+)\]\]/&_fixIncludeLink( $includedWeb, $1, $2 )/geo; unless( TWiki::isTrue( $this->{prefs}->getPreferencesValue('NOAUTOLINK')) ) { # Handle WikiWords $text = $this->{renderer}->takeOutBlocks( $text, 'noautolink', $removed ); } # 'TopicName' to 'Web.TopicName' $text =~ s/(^|[\s(])($regex{webNameRegex}\.$regex{wikiWordRegex})/$1$TranslationToken$2/go; $text =~ s/(^|[\s(])($regex{wikiWordRegex})/$1$includedWeb\.$2/go; $text =~ s/(^|[\s(])$TranslationToken/$1/go; $this->{renderer}->putBackBlocks( \$text, $removed, 'noautolink' ); } # handle tags again because of plugin hook $this->_expandAllTags( \$text, $includedTopic, $includedWeb ); # restore the tags delete $this->{includes}->{$key}; %{$this->{SESSION_TAGS}} = %saveTags; $this->{prefs}->restore( $prefsMark ); $text =~ s/^[\r\n]+/\n/; $text =~ s/[\r\n]+$/\n/; return $text; } sub _HTTP { my( $this, $params ) = @_; my $res; if( $params->{_DEFAULT} ) { $res = $this->{cgiQuery}->http( $params->{_DEFAULT} ); } $res = '' unless defined( $res ); return $res; } sub _HTTPS { my( $this, $params ) = @_; my $res; if( $params->{_DEFAULT} ) { $res = $this->{cgiQuery}->https( $params->{_DEFAULT} ); } $res = '' unless defined( $res ); return $res; } sub _HTTP_HOST { return $ENV{HTTP_HOST} || ''; } sub _REMOTE_ADDR { return $ENV{REMOTE_ADDR} || ''; } sub _REMOTE_PORT { return $ENV{REMOTE_PORT} || ''; } sub _REMOTE_USER { return $ENV{REMOTE_USER} || ''; } # Only does simple search for topicmoved at present, can be expanded when required # SMELL: this violates encapsulation of Store and Meta, by exporting # the assumption that meta-data is stored embedded inside topic # text. sub _METASEARCH { my( $this, $params ) = @_; return $this->{store}->searchMetaData( $params ); } sub _DATE { my $this = shift; return TWiki::Time::formatTime(time(), '$day $mon $year', 'gmtime'); } sub _GMTIME { my( $this, $params ) = @_; return TWiki::Time::formatTime( time(), $params->{_DEFAULT} || '', 'gmtime' ); } sub _SERVERTIME { my( $this, $params ) = @_; return TWiki::Time::formatTime( time(), $params->{_DEFAULT} || '', 'servertime' ); } sub _DISPLAYTIME { my( $this, $params ) = @_; return TWiki::Time::formatTime( time(), $params->{_DEFAULT} || '', $TWiki::cfg{DisplayTimeValues} ); } #| $web | web and | #| $topic | topic to display the name for | #| $formatString | twiki format string (like in search) | sub _REVINFO { my ( $this, $params, $theTopic, $theWeb ) = @_; my $format = $params->{_DEFAULT} || $params->{format}; my $web = $params->{web} || $theWeb; my $topic = $params->{topic} || $theTopic; my $cgiQuery = $this->{cgiQuery}; my $cgiRev = ''; $cgiRev = $cgiQuery->param('rev') if( $cgiQuery ); my $rev = $cgiRev || $params->{rev} || ''; return $this->{renderer}->renderRevisionInfo( $web, $topic, undef, $rev, $format ); } sub _ENCODE { my( $this, $params ) = @_; my $type = $params->{type} || ''; my $text = $params->{_DEFAULT} || ''; if ( $type =~ /^entit(y|ies)$/i ) { return entityEncode( $text ); } elsif ( $type =~ /^html$/i ) { return entityEncode( $text, "\n\r" ); } elsif ( $type =~ /^quotes?$/i ) { $text =~ s/\"/\\"/go; # escape quotes with backslash (Bugs:Item3383 fix) return $text; } else { $text =~ s/\r*\n\r*/
/; # Legacy. return urlEncode( $text ); } } sub _SEARCH { my ( $this, $params, $topic, $web ) = @_; # pass on all attrs, and add some more #$params->{_callback} = undef; $params->{inline} = 1; $params->{baseweb} = $web; $params->{basetopic} = $topic; $params->{search} = $params->{_DEFAULT} if( $params->{_DEFAULT} ); $params->{type} = $this->{prefs}->getPreferencesValue( 'SEARCHVARDEFAULTTYPE' ) unless( $params->{type} ); my $s = $this->{search}->searchWeb( %$params ); return $s; } sub _WEBLIST { my( $this, $params ) = @_; my $format = $params->{_DEFAULT} || $params->{'format'} || '$name'; $format ||= '$name'; my $separator = $params->{separator} || "\n"; $separator =~ s/\$n/\n/; my $web = $params->{web} || ''; my $webs = $params->{webs} || 'public'; my $selection = $params->{selection} || ''; $selection =~ s/\,/ /g; $selection = " $selection "; my $marker = $params->{marker} || 'selected="selected"'; $web =~ s#\.#/#go; my @list = (); my @webslist = split( /,\s*/, $webs ); foreach my $aweb ( @webslist ) { if( $aweb eq 'public' ) { push( @list, $this->{store}->getListOfWebs( 'user,public,allowed' ) ); } elsif( $aweb eq 'webtemplate' ) { push( @list, $this->{store}->getListOfWebs( 'template,allowed' )); } else{ push( @list, $aweb ) if( $this->{store}->webExists( $aweb ) ); } } my @items; my $indent = CGI::span({class=>'twikiWebIndent'},''); foreach my $item ( @list ) { my $line = $format; $line =~ s/\$web\b/$web/g; $line =~ s/\$name\b/$item/g; $line =~ s/\$qname/"$item"/g; my $indenteditem = $item; $indenteditem =~ s#/$##g; $indenteditem =~ s#\w+/#$indent#g; $line =~ s/\$indentedname/$indenteditem/g; my $mark = ( $selection =~ / \Q$item\E / ) ? $marker : ''; $line =~ s/\$marker/$mark/g; push(@items, $line); } return join( $separator, @items); } sub _TOPICLIST { my( $this, $params ) = @_; my $format = $params->{_DEFAULT} || $params->{'format'} || '$name'; $format ||= '$name'; my $separator = $params->{separator} || "\n"; $separator =~ s/\$n/\n/; my $web = $params->{web} || $this->{webName}; my $selection = $params->{selection} || ''; $selection =~ s/\,/ /g; $selection = " $selection "; my $marker = $params->{marker} || 'selected="selected"'; $web =~ s#\.#/#go; return '' if $web ne $this->{webName} && $this->{prefs}->getWebPreferencesValue( 'NOSEARCHALL', $web ); my @items; foreach my $item ( $this->{store}->getTopicNames( $web ) ) { my $line = $format; $line =~ s/\$web\b/$web/g; $line =~ s/\$name\b/$item/g; $line =~ s/\$qname/"$item"/g; my $mark = ( $selection =~ / \Q$item\E / ) ? $marker : ''; $line =~ s/\$marker/$mark/g; push( @items, $line ); } return join( $separator, @items ); } sub _QUERYSTRING { my $this = shift; return $this->{cgiQuery}->query_string(); } sub _QUERYPARAMS { my ( $this, $params ) = @_; return '' unless $this->{cgiQuery}; my $format = defined $params->{format} ? $params->{format} : '$name=$value'; my $separator = defined $params->{separator} ? $params->{separator} : "\n"; my @list; foreach my $name ( $this->{cgiQuery}->param() ) { # Issues multi-valued parameters as separate hiddens my $value = $this->{cgiQuery}->param( $name ); my $entry = $format; $entry =~ s/\$name/$name/g; $entry =~ s/\$value/$value/; push(@list, $entry); } return expandStandardEscapes(join($separator, @list)); } =pod ---++ StaticMethod expandStandardEscapes($str) -> $unescapedStr Expands standard escapes used in parameter values to block evaluation. The following escapes are handled: | *Escape:* | *Expands To:* | | =$n= or =$n()= | New line. Use =$n()= if followed by alphanumeric character, e.g. write =Foo$n()Bar= instead of =Foo$nBar= | | =$nop= or =$nop()= | Is a "no operation". | | =$quot= | Double quote (="=) | | =$percnt= | Percent sign (=%=) | | =$dollar= | Dollar sign (=$=) | =cut sub expandStandardEscapes { my $text = shift; $text =~ s/\$n\(\)/\n/gos; # expand '$n()' to new line $text =~ s/\$n([^$regex{mixedAlpha}]|$)/\n$1/gos; # expand '$n' to new line $text =~ s/\$nop(\(\))?//gos; # remove filler, useful for nested search $text =~ s/\$quot(\(\))?/\"/gos; # expand double quote $text =~ s/\$percnt(\(\))?/\%/gos; # expand percent $text =~ s/\$dollar(\(\))?/\$/gos; # expand dollar return $text; } sub _URLPARAM { my( $this, $params ) = @_; my $param = $params->{_DEFAULT} || ''; my $newLine = $params->{newline} || ''; my $encode = $params->{encode}; my $multiple = $params->{multiple}; my $separator = $params->{separator} || "\n"; my $value = ''; if( $this->{cgiQuery} ) { if( TWiki::isTrue( $multiple )) { my @valueArray = $this->{cgiQuery}->param( $param ); if( @valueArray ) { # join multiple values properly unless( $multiple =~ m/^on$/i ) { my $item = ''; @valueArray = map { $item = $_; $_ = $multiple; $_ .= $item unless( s/\$item/$item/go ); $_ } @valueArray; } $value = join ( $separator, @valueArray ); } } else { $value = $this->{cgiQuery}->param( $param ); $value = '' unless( defined $value ); } } $value =~ s/\r?\n/$newLine/go if( $newLine ); if ( $encode ) { if ( $encode =~ /^entit(y|ies)$/i ) { $value = entityEncode( $value ); } elsif ( $encode =~ /^quotes?$/i ) { $value =~ s/\"/\\"/go; # escape quotes with backslash (Bugs:Item3383 fix) } else { $value =~ s/\r*\n\r*/
/; # Legacy $value = urlEncode( $value ); } } unless( $value ) { $value = $params->{default} || ''; } return $value; } # This routine was introduced to URL encode Mozilla UTF-8 POST URLs in the # TWiki Feb2003 release - encoding is no longer needed since UTF-URLs are now # directly supported, but it is provided for backward compatibility with # skins that may still be using the deprecated %INTURLENCODE%. sub _INTURLENCODE { my( $this, $params ) = @_; # Just strip double quotes, no URL encoding - Mozilla UTF-8 URLs # directly supported now return $params->{_DEFAULT} || ''; } # This routine is deprecated as of DakarRelease, # and is maintained only for backward compatibility. # Spacing of WikiWords is now done with %SPACEOUT% # (and the private routine _SPACEOUT). sub _SPACEDTOPIC { my ( $this, $params, $theTopic ) = @_; my $topic = spaceOutWikiWord( $theTopic ); $topic =~ s/ / */g; return urlEncode( $topic ); } sub _SPACEOUT { my ( $this, $params ) = @_; my $spaceOutTopic = $params->{_DEFAULT}; my $sep = $params->{'separator'}; $spaceOutTopic = spaceOutWikiWord( $spaceOutTopic, $sep ); return $spaceOutTopic; } sub _ICON { my( $this, $params ) = @_; my $file = $params->{_DEFAULT} || ''; # Try to map the file name to see if there is a matching filetype image # If no mapping could be found, use the file name that was passed my $iconFileName = $this->mapToIconFileName( $file, $file ); return CGI::img( { src => $this->getIconUrl( 0, $iconFileName ), width => 16, height=>16, align => 'top', alt => $iconFileName, border => 0 }); } sub _ICONURL { my( $this, $params ) = @_; my $file = ( $params->{_DEFAULT} || '' ); return $this->getIconUrl( 1, $file ); } sub _ICONURLPATH { my( $this, $params ) = @_; my $file = ( $params->{_DEFAULT} || '' ); return $this->getIconUrl( 0, $file ); } sub _RELATIVETOPICPATH { my ( $this, $params, $theTopic, $web ) = @_; my $topic = $params->{_DEFAULT}; return '' unless $topic; my $theRelativePath; # if there is no dot in $topic, no web has been specified if ( index( $topic, '.' ) == -1 ) { # add local web $theRelativePath = $web . '/' . $topic; } else { $theRelativePath = $topic; #including dot } # replace dot by slash is not necessary; TWiki.MyTopic is a valid url # add ../ if not already present to make a relative file reference if ( $theRelativePath !~ m!^../! ) { $theRelativePath = "../$theRelativePath"; } return $theRelativePath; } sub _ATTACHURLPATH { my ( $this, $params, $topic, $web ) = @_; return $this->getPubUrl(0, $web, $topic); } sub _ATTACHURL { my ( $this, $params, $topic, $web ) = @_; return $this->getPubUrl(1, $web, $topic); } sub _LANGUAGE { my $this = shift; return $this->{i18n}->language(); } sub _LANGUAGES { my ( $this , $params ) = @_; my $format = $params->{format} || " * \$langname"; my $separator = $params->{separator} || "\n"; $separator =~ s/\\n/\n/g; my $selection = $params->{selection} || ''; $selection =~ s/\,/ /g; $selection = " $selection "; my $marker = $params->{marker} || 'selected="selected"'; # $languages is a hash reference: my $languages = $this->{i18n}->enabled_languages(); my @tags = sort(keys(%{$languages})); my $result = ''; my $i = 0; foreach my $lang (@tags) { my $item = $format; my $name = ${$languages}{$lang}; $item =~ s/\$langname/$name/g; $item =~ s/\$langtag/$lang/g; my $mark = ( $selection =~ / \Q$lang\E / ) ? $marker : ''; $item =~ s/\$marker/$mark/g; $result .= $separator if $i; $result .= $item; $i++; } return $result; } sub _MAKETEXT { my( $this, $params ) = @_; my $str = $params->{_DEFAULT} || $params->{string} || ""; return "" unless $str; # escape everything: $str =~ s/\[/~[/g; $str =~ s/\]/~]/g; # restore already escaped stuff: $str =~ s/~~\[/~[/g; $str =~ s/~~\]/~]/g; # unescape parameters and calculate highest parameter number: my $max = 0; $str =~ s/~\[(\_(\d+))~\]/ $max = $2 if ($2 > $max); "[$1]"/ge; $str =~ s/~\[(\*,\_(\d+),[^,]+(,([^,]+))?)~\]/ $max = $2 if ($2 > $max); "[$1]"/ge; # get the args to be interpolated. my $argsStr = $params->{args} || ""; my @args = split (/\s*,\s*/, $argsStr) ; # fill omitted args with zeros while ((scalar @args) < $max) { push(@args, 0); } # do the magic: my $result = $this->{i18n}->maketext($str, @args); # replace accesskeys: $result =~ s#(^|[^&])&([a-zA-Z])#$1$2#g; # replace escaped amperstands: $result =~ s/&&/\&/g; return $result; } sub _SCRIPTNAME { #my ( $this, $params, $theTopic, $theWeb ) = @_; # try SCRIPT_FILENAME my $value = $ENV{SCRIPT_FILENAME}; if( $value ) { $value =~ s!.*/([^/]+)$!$1!o; return $value; } # try SCRIPT_URL (won't work with url rewriting) $value = $ENV{SCRIPT_URL}; if( $value ) { # e.g. '/cgi-bin/view.cgi/TWiki/WebHome' # cut URL path to get 'view.cgi/TWiki/WebHome' $value =~ s|^$TWiki::cfg{ScriptUrlPath}/?||o; # cut extended path to get 'view.cgi' $value =~ s|/.*$||; return $value; } # no joy return ''; } sub _SCRIPTURL { my ( $this, $params, $topic, $web ) = @_; my $script = $params->{_DEFAULT} || ''; return $this->getScriptUrl( 1, $script ); } sub _SCRIPTURLPATH { my ( $this, $params, $topic, $web ) = @_; my $script = $params->{_DEFAULT} || ''; return $this->getScriptUrl( 0, $script ); } sub _PUBURL { my $this = shift; return $this->getPubUrl(1); } sub _PUBURLPATH { my $this = shift; return $this->getPubUrl(0); } sub _ALLVARIABLES { return shift->{prefs}->stringify(); } sub _META { my ( $this, $params, $topic, $web ) = @_; my $meta = $this->inContext( 'can_render_meta' ); return '' unless $meta; my $option = $params->{_DEFAULT}; if( $option eq 'form' ) { # META:FORM and META:FIELD return TWiki::Form::renderForDisplay( $this->{templates}, $meta ); } elsif ( $option eq 'formfield' ) { # a formfield from within topic text return $this->{renderer}->renderFormField( $meta, $params ); } elsif( $option eq 'attachments' ) { # renders attachment tables return $this->{attach}->renderMetaData( $web, $topic, $meta, $params ); } elsif( $option eq 'moved' ) { return $this->{renderer}->renderMoved( $web, $topic, $meta, $params ); } elsif( $option eq 'parent' ) { return $this->{renderer}->renderParent( $web, $topic, $meta, $params ); } return ''; } # Remove NOP tag in template topics but show content. Used in template # _topics_ (not templates, per se, but topics used as templates for new # topics) sub _NOP { my ( $this, $params, $topic, $web ) = @_; return '' unless $params->{_RAW}; return $params->{_RAW}; } # Shortcut to %TMPL:P{"sep"}% sub _SEP { my $this = shift; return $this->{templates}->expandTemplate('sep'); } #deprecated functionality, now implemented using %USERINFO% #move to compatibility plugin in TWiki5 sub _WIKINAME_deprecated { my ( $this, $params ) = @_; ASSERT($this->isa( 'TWiki')) if DEBUG; $params->{format} = $this->{prefs}->getPreferencesValue( 'WIKINAME' ) || '$wikiname'; return $this->_USERINFO($params); } #deprecated functionality, now implemented using %USERINFO% #move to compatibility plugin in TWiki5 sub _USERNAME_deprecated { my ( $this, $params ) = @_; ASSERT($this->isa( 'TWiki')) if DEBUG; $params->{format} = $this->{prefs}->getPreferencesValue( 'USERNAME' ) || '$username'; return $this->_USERINFO($params); } #deprecated functionality, now implemented using %USERINFO% #move to compatibility plugin in TWiki5 sub _WIKIUSERNAME_deprecated { my ( $this, $params ) = @_; ASSERT($this->isa( 'TWiki')) if DEBUG; $params->{format} = $this->{prefs}->getPreferencesValue( 'WIKIUSERNAME' ) || '$wikiusername'; return $this->_USERINFO($params); } sub _USERINFO { my ( $this, $params ) = @_; my $format = $params->{format} || '$username, $wikiusername, $emails'; my $userDebug = $params->{'userdebug'} || ''; my $user = $this->{user}; if( $params->{_DEFAULT} ) { $user = $this->{users}->findUser( $params->{_DEFAULT}, undef, 1 ); return '' if !$user; return '' if( $TWiki::cfg{AntiSpam}{HideUserDetails} && !$this->{user}->isAdmin() && $user != $this->{user} ); } my $info = $format; if ($info =~ /\$username/) { my $username = $user->login(); $info =~ s/\$username/$username/g; } if ($info =~ /\$wikiname/) { my $wikiname = $user->wikiName(); $info =~ s/\$wikiname/$wikiname/g; } if ($info =~ /\$wikiusername/) { my $wikiusername = $user->webDotWikiName(); $info =~ s/\$wikiusername/$wikiusername/g; } if ($info =~ /\$emails/) { my $emails = join(', ', $user->emails()); $info =~ s/\$emails/$emails/g; } if ($info =~ /\$groups/) { my @groupNames = map {$_->webDotWikiName();} $user->getGroups(); my $groups = join(', ', @groupNames); $groups .= ' isAdmin()' if $user->isAdmin(); $info =~ s/\$groups/$groups/g; } #don't give out userlists to non-admins if ($userDebug ne '' && $user->isAdmin()) { my $users = ''; $users .= "\n\nLoaded Users: ".join(" \n", map {$_->webDotWikiName()} @{$this->{users}->getAllLoadedUsers()}); $users .= "\n\nALL Users: ".join(" \n", map {$_->webDotWikiName()} @{$this->{users}->getAllUsers()}); $info .= $users; } return $info; } sub _GROUPS { my ( $this, $params ) = @_; my @groupNames = map { '| [['.$_->webDotWikiName(). ']['.$_->wikiName().']] |'. join(', ', map { '[['.$_->webDotWikiName().']['.$_->wikiName().']]' } @{$_->groupMembers()}). ' |'; } sort {$a->wikiName() cmp $b->wikiName()} @{$this->{users}->getAllGroups()}; return '| *Group* | *Members* |'."\n".join("\n", @groupNames); } 1;