# TWiki Enterprise Collaboration Platform, http://TWiki.org/ # # Copyright (C) 1999-2018 Peter Thoeny, peter[at]thoeny.org # Copyright (C) 2006-2018 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 3 # 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. package TWiki; =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. ---+++!! Public Data members * =request= Pointer to the TWiki::Request * =response= Pointer to the TWiki::Respose * =context= Hash of context ids * moved: =loginManager= TWiki::LoginManager singleton (moved to TWiki::Users) * =plugins= TWiki::Plugins singleton * =prefs= TWiki::Prefs singleton * =remoteUser= Login ID when using ApacheLogin. Maintained for compatibility only, do not use. * =requestedWebName= Name of web found in URL path or =web= URL parameter * =sandbox= TWiki::Sandbox singleton * =scriptUrlPath= URL path to the current script. May be dynamically extracted from the URL path if {GetScriptUrlFromCgi}. Only required to support {GetScriptUrlFromCgi} and not consistently used. Avoid. * =security= TWiki::Access singleton * =SESSION_TAGS= Hash of TWiki variables whose value is specific to the current request. * =store= TWiki::Store singleton * =topicName= Name of topic found in URL path or =topic= URL parameter * =urlHost= Host part of the URL (including the protocol) determined during intialisation and defaulting to {DefaultUrlHost} * =user= Unique user ID of logged-in user * =users= TWiki::Users singleton * =webName= Name of web found in URL path, or =web= URL parameter, or {UsersWebName} =cut use strict; use Assert; use Error qw( :try ); use CGI; $CGI::LIST_CONTEXT_WARN = 0; use TWiki::Response; use TWiki::Request; use TWiki::Time; require 5.010001; # Perl 5.10.1 # 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 $percentSubstitute $twikiLibDir %regex %functionTags %contextFreeSyntax %restDispatch $VERSION $RELEASE $TRUE $FALSE $sandbox $engine $ifParser %scriptOnMaster %httpHiddenField ); # 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"; # Hack to substitute a % into a non-printable character so that a # search string can be passed from URLPARAM to SEARCH without variable # expansion, e.g. for a literal search. # (TWiki:Codev.NewModeSearchEncodingInENCODEandURLPARAM & Item7847) $percentSubstitute = "\x1a"; =pod ---++ StaticMethod getTWikiLibDir() -> $path 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; # TSA SMELL : Should not assume environment variables and get data from request 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 { require TWiki::Sandbox; # system command sandbox require 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 # The $VERSION is automatically expanded on checkin of this module $VERSION = '$Date: 2018-07-16 12:09:47 +0900 (Mon, 16 Jul 2018) $ $Rev: 30610 (2018-07-16) $ '; $RELEASE = 'TWiki-6.1.0'; $VERSION =~ s/^.*?\((.*)\).*: (\d+) .*?$/$RELEASE, $1, build $2/; # Default handlers for different %TAGS% %functionTags = ( ADDTOHEAD => \&ADDTOHEAD, ALLVARIABLES => \&ALLVARIABLES, ATTACHURL => \&ATTACHURL, ATTACHURLPATH => \&ATTACHURLPATH, BASETOPIC => \&BASETOPIC, BASEWEB => \&BASEWEB, CONTENTMODE => \&CONTENTMODE, CRYPTTOKEN => \&CRYPTTOKEN, DATE => \&DATE, DISKID => \&DISKID, DISPLAYTIME => \&DISPLAYTIME, EDITFORM => \&EDITFORM, EDITFORMFIELD => \&EDITFORMFIELD, ENCODE => \&ENCODE, ENTITY => \&ENTITY, ENV => \&ENV, FORM => \&FORM, FORMFIELD => \&FORMFIELD, GMTIME => \&GMTIME, GROUPS => \&GROUPS, HIDE => \&HIDE, HIDEINPRINT => \&HIDEINPRINT, HTTP_HOST => \&HTTP_HOST_deprecated, HTTP => \&HTTP, HTTPS => \&HTTPS, ICON => \&ICON, ICONURL => \&ICONURL, ICONURLPATH => \&ICONURLPATH, IF => \&IF, INCLUDE => \&INCLUDE, INCLUDINGTOPIC => \&INCLUDINGTOPIC, INCLUDINGWEB => \&INCLUDINGWEB, INTURLENCODE => \&INTURLENCODE_deprecated, LANGUAGES => \&LANGUAGES, MAKETEXT => \&MAKETEXT, MDREPO => \&MDREPO, META => \&META, METASEARCH => \&METASEARCH, NOP => \&NOP, PARENTTOPIC => \&PARENTTOPIC, PLUGINVERSION => \&PLUGINVERSION, PUBURL => \&PUBURL, PUBURLPATH => \&PUBURLPATH, QUERYPARAMS => \&QUERYPARAMS, QUERYSTRING => \&QUERYSTRING, RELATIVETOPICPATH => \&RELATIVETOPICPATH, REMOTE_ADDR => \&REMOTE_ADDR_deprecated, REMOTE_PORT => \&REMOTE_PORT_deprecated, REMOTE_USER => \&REMOTE_USER_deprecated, RENDERHEAD => \&RENDERHEAD, REVINFO => \&REVINFO, REVTITLE => \&REVTITLE, REVARG => \&REVARG, SCRIPTNAME => \&SCRIPTNAME, SCRIPTURL => \&SCRIPTURL, SCRIPTURLPATH => \&SCRIPTURLPATH, SEARCH => \&SEARCH, SEP => \&SEP, SERVERTIME => \&SERVERTIME, SPACEDTOPIC => \&SPACEDTOPIC_deprecated, SPACEOUT => \&SPACEOUT, 'TMPL:P' => \&TMPLP, TOPIC => \&TOPIC, TOPICLIST => \&TOPICLIST, TOPICTITLE => \&TOPICTITLE, TRASHWEB => \&TRASHWEB, URLENCODE => \&ENCODE, URLPARAM => \&URLPARAM, LANGUAGE => \&LANGUAGE, USERINFO => \&USERINFO, USERNAME => \&USERNAME_deprecated, VAR => \&VAR, WEB => \&WEB, WEBLIST => \&WEBLIST, WIKINAME => \&WIKINAME_deprecated, WIKIUSERNAME => \&WIKIUSERNAME_deprecated, WIKIWEBMASTER => \&WIKIWEBMASTER, WIKIWEBMASTERNAME => \&WIKIWEBMASTERNAME, # Constant tag strings _not_ dependent on config. These get nicely # optimised by the compiler. ENDSECTION => sub { '' }, WIKIVERSION => sub { $VERSION }, STARTSECTION => sub { '' }, STARTINCLUDE => sub { '' }, STOPINCLUDE => sub { '' }, ); $contextFreeSyntax{IF} = 1; 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 $functionTags{ALLOWLOGINNAME} = sub { $TWiki::cfg{Register}{AllowLoginName} || 0 }; $functionTags{AUTHREALM} = sub { $TWiki::cfg{AuthRealm} }; $functionTags{DEFAULTURLHOST} = sub { $TWiki::cfg{DefaultUrlHost} }; $functionTags{HOMETOPIC} = sub { $TWiki::cfg{HomeTopicName} }; $functionTags{LOCALSITEPREFS} = sub { $TWiki::cfg{LocalSitePreferences} }; $functionTags{NOFOLLOW} = sub { $TWiki::cfg{NoFollow} ? 'rel='.$TWiki::cfg{NoFollow} : '' }; $functionTags{NOTIFYTOPIC} = sub { $TWiki::cfg{NotifyTopicName} }; $functionTags{SCRIPTSUFFIX} = sub { $TWiki::cfg{ScriptSuffix} }; $functionTags{SITESTATISTICSTOPIC} = sub { $TWiki::cfg{Stats}{SiteStatsTopicName} }; $functionTags{STATISTICSTOPIC} = sub { $TWiki::cfg{Stats}{TopicName} }; $functionTags{SYSTEMWEB} = sub { $TWiki::cfg{SystemWebName} }; # $functionTags{TRASHWEB} = sub { $TWiki::cfg{TrashWebName} }; $functionTags{TWIKIADMINLOGIN} = sub { $TWiki::cfg{AdminUserLogin} }; $functionTags{USERSWEB} = sub { $TWiki::cfg{UsersWebName} }; $functionTags{WEBPREFSTOPIC} = sub { $TWiki::cfg{WebPrefsTopicName} }; $functionTags{WIKIPREFSTOPIC} = sub { $TWiki::cfg{SitePrefsTopicName} }; $functionTags{WIKIUSERSTOPIC} = sub { $TWiki::cfg{UsersTopicName} }; if ( $TWiki::cfg{UserSubwebs}{Enabled} ) { $functionTags{USERPREFSTOPIC} = sub { $TWiki::cfg{UserSubwebs}{UserPrefsTopicName} }; } # Compatibility synonyms, deprecated in 4.2 but still used throughout # the documentation. $functionTags{MAINWEB} = $functionTags{USERSWEB}; $functionTags{TWIKIWEB} = $functionTags{SYSTEMWEB}; # 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 LC_COLLATE ); # 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}); setlocale(&LC_COLLATE, $TWiki::cfg{Site}{Locale}); } $functionTags{CHARSET} = sub { $TWiki::cfg{Site}{CharSet} || 'iso-8859-1' }; # HTML 4.01 and XML refers to RFC 4646 with language specification. # RFC 4646 dictates the delimiter to be a hyphen rather than an underscore. my $lang = $TWiki::cfg{Site}{Lang}; unless ( $lang ) { if ( $TWiki::cfg{Site}{Locale} =~ m/^([a-z]+_[a-z]+)/i ) { $lang = $1; $lang =~ s/_/-/; } else { $lang = 'en-US'; } } $functionTags{LANG} = sub { $lang }; # 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. foreach my $lineno (sort{$a <=> $b}(keys %headings)) { my ($level, $line, $anchor) = ($levels{$lineno}, $headings{$lineno}, $anchors{$lineno}); $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; } my $args; $args->{class} = 'twikiToc'; $args->{style} = $style if( $style ); return CGI::div( $args, "$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 your configuration settings for {TemplateDir} and {TemplatePath}'; } $text =~ s/^\s+//s; $text =~ s/\s+$//s; 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(?:{(.*)})?%$/) { require TWiki::Attrs; 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(?:{(.*)})?%$/ ) { require TWiki::Attrs; 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, $web, $topic ) -> $text * =$text= - text to expand * =$user= - This is the user expanded in e.g. %USERNAME. Optional, defaults to logged-in user. * =$web= - name of web, optional * =$topic= - name of topic, optional 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, $theWeb, $theTopic ) = @_; $user ||= $this->{user}; $theWeb ||= $this->{SESSION_TAGS}{WEB} || $this->{SESSION_TAGS}{BASEWEB}; $theTopic ||= $this->{SESSION_TAGS}{TOPIC} || $this->{SESSION_TAGS}{BASETOPIC}; # 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 = _processTags( $this, $text, \&_expandTagOnTopicCreation, 16 ); # expand all variables for type="expandvariables" sections ( $ntext, $sections ) = parseSections( $text ); if( scalar( @$sections )) { $theWeb ||= $this->{session}->{webName}; $theTopic ||= $this->{session}->{topicName}; foreach my $s ( reverse @$sections ) { if( $s->{type} eq 'expandvariables' ) { my $etext = substr( $ntext, $s->{start}, $s->{end} - $s->{start} ); expandAllTags( $this, \$etext, $theTopic, $theWeb ); $ntext = substr( $ntext, 0, $s->{start}) . $etext . substr( $ntext, $s->{end}, length($ntext) ); } else { # put back non-expandvariables 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; } # 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 urlEncodeAttachment ( $text ) For attachments, URL-encode specially to 'freeze' any characters >127 in the site charset (e.g. ISO-8859-1 or KOI8-R), by doing URL encoding into native charset ($siteCharset) - used when generating attachment URLs, to enable the web server to serve attachments, including images, directly. This encoding is required to handle the cases of: - browsers that generate UTF-8 URLs automatically from site charset URLs - now quite common - web servers that directly serve attachments, using the site charset for filenames, and cannot convert UTF-8 URLs into site charset filenames The aim is to prevent the browser from converting a site charset URL in the web page to a UTF-8 URL, which is the default. Hence we 'freeze' the URL into the site character set through URL encoding. In two cases, no URL encoding is needed: For EBCDIC mainframes, we assume that site charset URLs will be translated (outbound and inbound) by the web server to/from an EBCDIC character set. For sites running in UTF-8, there's no need for TWiki to do anything since all URLs and attachment filenames are already in UTF-8. =cut sub urlEncodeAttachment { my( $text ) = @_; my $usingEBCDIC = ( 'A' eq chr(193) ); # Only true on EBCDIC mainframes if( (defined($TWiki::cfg{Site}{CharSet}) and $TWiki::cfg{Site}{CharSet} =~ /^utf-?8$/i ) or $usingEBCDIC ) { # Just let browser do UTF-8 URL encoding return $text; } # Freeze into site charset through URL encoding return urlEncode( $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. This URL-encoding handles all character encodings including ISO-8859-*, KOI8-R, EUC-* and UTF-8. This may not handle EBCDIC properly, as it generates an EBCDIC URL-encoded URL, but mainframe web servers seem to translate this outbound before it hits browser - see CGI::Util::escape for another approach. =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; $value =~ s/false//gi; return ( $value ) ? 1 : 0; } =pod ---++ StaticMethod topLevelWeb( $web ) -> top level web of $web If $web is a top level web, it returns $web. If $web is a subweb, it returns the top level web of $web. =cut sub topLevelWeb { my( $web ) = @_; return '' if ( !defined($web) ); $web =~ /^(\w*)/; return $1; } =pod ---++ StaticMethod spaceOutWikiWord( $word, $sep ) -> $string Spaces out a wiki word by inserting a string between each word component. Word component boundaries are transitions from lowercase to uppercase or numeric, from numeric to uppercase or lowercase, and from uppercase to numeric characters. Parameter $sep defines the separator between the word components, the default is a space. Example: "ABC2015ProjectCharter" results in "ABC 2015 Project Charter" =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}$regex{lowerAlpha}])/$1$sep$2/go; $word =~ s/([$regex{upperAlpha}])([$regex{numeric}])/$1$sep$2/go; return $word; } =pod ---++ ObjectMethod expandAllTags(\$text, $topic, $web, $meta) 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 =cut sub expandAllTags { my $this = shift; my $textRef = shift; # reference my ( $topic, $web, $meta ) = @_; $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%' $$textRef =~ 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. $$textRef = _processTags( $this, $$textRef, \&_expandTagOnTopicRendering, 16, $topic, $web, $meta, $textRef ); # 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 $tagFunction = shift; # my ( $topic, $web, $meta, $fullTextRef ) = @_; my $tell = 0; return '' if ( (!defined( $text )) || ($text eq '') ); #no tags to process return $text unless ($text =~ /(%)/); my $depth = shift; unless ( $depth ) { my $loc = ''; if ( defined($_[0]) && defined($_[1]) ) { $loc = " at $_[1].$_[0]" } my $mess = "Max recursive depth reached$loc: $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] while ( scalar( @queue )) { my $token = shift( @queue ); #print STDERR ' ' x $tell,"PROCESSING $token \n"; # each % sign either closes an existing stacked context, or # opens a new context. if ( $token eq '%' ) { #print STDERR ' ' x $tell,"CONSIDER $stackTop\n"; # 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"; $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"; # Call tag function. @_ is( $topic, $web, $meta, $fullTextRef ), # values may be undef. $meta and $text are passed along so that # they can be referenced by tag handlers. $fullTextRef is a # reference to the full text, it cannot be updated because text # is reconstructed via $stackTop. my $e = &$tagFunction( $this, $tag, $args, @_ ); if ( defined( $e )) { #print STDERR ' ' x $tell--,"EXPANDED $tag -> $e\n"; $stackTop = pop( @stack ); unless ($e =~ /(%)/) { #SMELL: this is a profiler speedup found by Sven on the last day of 4.2.1 #TODO: I don't think this parser should be in this section - re-analysis desired. #print STDERR "no tags to recurse\n"; $stackTop .= $e; next; } # Recursively expand tags in the expansion of $tag $stackTop .= _processTags($this, $e, $tagFunction, $depth-1, @_ ); } else { # expansion failed #print STDERR ' ' x $tell++,"EXPAND $tag FAILED\n"; # 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++; } } 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"; 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, $meta ) = @_; require TWiki::Attrs; my $opv = $this->{prefs}->getPreferencesValue( 'OVERRIDABLEPREDEFINEDVARIABLES'); $opv = 'all' unless ( defined($opv) ); # for backward compatibility unless ( $opv =~ /\ball\b/i ) { my %p = map { $_ => 1 } split(/[,\s]+/, $opv); if ( !$p{$tag} && defined( $functionTags{$tag} ) ) { return &{$functionTags{$tag}} ( $this, new TWiki::Attrs( $args, $contextFreeSyntax{$tag} ), @_ ); } } my $e = $this->{prefs}->getPreferencesValue( $tag ); if( defined( $e ) ) { if( $args ) { # Codev.ParameterizedVariables feature my $attrs = new TWiki::Attrs( $args, $contextFreeSyntax{$tag} ); # Not possible to define a _DEFAULT setting, so use DEFAULT: if( ! defined $attrs->{DEFAULT} && defined $attrs->{_DEFAULT} ) { $attrs->{DEFAULT} = $attrs->{_DEFAULT}; } while( my ( $key, $value ) = each( %$attrs ) ) { $e =~ s/%${key}(\{ *default="(.*?[^\\]?)" *})?%/_unescapeQuotes( $value )/ge; } } # In parameterized variables, expand %ALL_UNUSED_TAGS{ default="..." }% to defaults # FIXME: Quick hack; do proper variable parsing $e =~ s/%($regex{tagNameRegex})\{ *default="(.*?[^\\]?)" *}%/_unescapeQuotes( $2 )/ge; } else { $e = $this->{SESSION_TAGS}{$tag} unless( $args ); if( !defined( $e ) && defined( $functionTags{$tag} )) { $e = &{$functionTags{$tag}} ( $this, new TWiki::Attrs( $args, $contextFreeSyntax{$tag} ), @_ ); } } return $e; } sub _unescapeQuotes { my $text = shift; $text =~ s/\\(["'])/$1/g; return $text; } # 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]; # You may want to expand arbitrary tags on topic creation. # By prepending EOTC__ (EOTC stands for Expand On Topic Creation), you # can achieve that. if ( $_[0] =~ /^EOTC__(\w+)$/ ) { $_[0] = $1; return _expandTagOnTopicRendering( $this, @_ ); } # 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 _expandTagOnTopicRendering( $this, @_ ); } =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 ---++ ObjectMethod handleCommonTags( $text, $web, $topic, $meta ) -> $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. $meta may be undef when, for example, expanding templates, or one-off strings at a time when meta isn't available. =cut sub handleCommonTags { my( $this, $text, $theWeb, $theTopic, $meta ) = @_; ASSERT($theWeb) if DEBUG; ASSERT($theTopic) if DEBUG; return $text unless $text; my $verbatim={}; # Plugin Hook (for cache Plugins only) $this->{plugins}->dispatch( 'beforeCommonTagsHandler', $text, $theTopic, $theWeb, $meta ); #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; expandAllTags( $this, \$text, $theTopic, $theWeb, $meta ); $text = $this->renderer->takeOutBlocks( $text, 'verbatim', $verbatim); # Plugin Hook $this->{plugins}->dispatch( 'commonTagsHandler', $text, $theTopic, $theWeb, 0, $meta ); # process tags again because plugin hook may have added more in expandAllTags( $this, \$text, $theTopic, $theWeb, $meta ); $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. # We need to keep track of the 'TOC topics' here in order to ensure that each # of these topics is only processed once (this is due to the fact that the # renaming of ambiguous anchors has to work context-less and cannot recognize # whether a particular heading has been converted before)--alternatively, we # could just clear the 'anchorname memory' and keep reprocessing topics # (the latter solution is slower if th same TOC is included multiple times) # current solution: let _TOC() clear the hash which holds the anchornames $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}->dispatch( 'afterCommonTagsHandler', $text, $theTopic, $theWeb, $meta ); return $text; } =pod ---++ ObjectMethod ADDTOHEAD( $args ) 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. ---+++ =% ADDTOHEAD%= You can write =%ADDTOHEAD{...}%= in a topic or template. This variable accepts the following parameters: * =_DEFAULT= optional, id of the head block. Used to generate a comment in the output HTML. * =text= optional, text to use for the head block. Mutually exclusive with =topic=. * =topic= optional, full TWiki path name of a topic that contains the full text to use for the head block. Mutually exclusive with =text=. Example: =topic="%WEB%.MyTopic"=. * =requires= optional, comma-separated list of id's of other head blocks this one depends on. =% ADDTOHEAD%= expands in-place to the empty string, unless there is an error in which case the variable expands to an error string. Use =% RENDERHEAD%= to generate the sorted head tags. =cut sub ADDTOHEAD { my ($this, $args, $topic, $web) = @_; my $_DEFAULT = $args->{_DEFAULT}; my $text = $args->{text}; $topic = $args->{topic}; my $section = $args->{section} || ''; my $requires = $args->{requires}; if( defined $topic ) { ( $web, $topic ) = $this->normalizeWebTopicName( $web, $topic ); # generate TML only and delay expansion until this is rendered $text = '%INCLUDE{"' . $web . '.' . $topic . '"'; $text .= ' section="' . $section . '"' if( $section ); $text .= ' warn="off"}%'; } $text = $_DEFAULT unless defined $text; $text = '' unless defined $text; $this->addToHEAD($_DEFAULT, $text, $requires); return ''; } sub addToHEAD { my( $this, $tag, $header, $requires ) = @_; # Expand TWiki variables in the header $header = $this->handleCommonTags( $header, $this->{webName}, $this->{topicName} ); $this->{_SORTEDHEADS} ||= {}; $tag ||= ''; $requires ||= ''; my $debug = ''; # Resolve to references to build DAG my @requires; foreach my $req (split(/,\s*/, $requires)) { unless ($this->{_SORTEDHEADS}->{$req}) { $this->{_SORTEDHEADS}->{$req} = { tag => $req, requires => [], header => '', }; } push(@requires, $this->{_SORTEDHEADS}->{$req}); } my $record = $this->{_SORTEDHEADS}->{$tag}; unless ($record) { $record = { tag => $tag }; $this->{_SORTEDHEADS}->{$tag} = $record; } $record->{requires} = \@requires; $record->{header} = $header; # Temporary, for compatibility until %RENDERHEAD% is embedded # in the skins $this->{_HTMLHEADERS}{GENERATED_HEADERS} = _genHeaders($this); } sub _visit { my ($v, $visited, $list) = @_; return if $visited->{$v}; foreach my $r (@{$v->{requires}}) { _visit($r, $visited, $list); } push(@$list, $v); $visited->{$v} = 1; } sub _genHeaders { my ($this) = @_; return '' unless $this->{_SORTEDHEADS}; # Loop through the vertices of the graph, in any order, initiating # a depth-first search for any vertex that has not already been # visited by a previous search. The desired topological sorting is # the reverse postorder of these searches. That is, we can construct # the ordering as a list of vertices, by adding each vertex to the # start of the list at the time when the depth-first search is # processing that vertex and has returned from processing all children # of that vertex. Since each edge and vertex is visited once, the # algorithm runs in linear time. my %visited; my @total; foreach my $v (values %{$this->{_SORTEDHEADS}}) { _visit($v, \%visited, \@total); } return join( "\n", map { " $_->{header}" } @total ); } =pod ---+++ % RENDERHEAD% =%RENDERHEAD%= should be written where you want the sorted head tags to be generated. This will normally be in a template. The variable expands to a sorted list of the head blocks added up to the point the RENDERHEAD variable is expanded. Each expanded head block is preceded by an HTML comment that records the ID of the head block. Head blocks are sorted to satisfy all their =requires= constraints. The output order of blocks with no =requires= value is undefined. If cycles exist in the dependency order, the cycles will be broken but the resulting order of blocks in the cycle is undefined. =cut sub RENDERHEAD { my $this = shift; return _genHeaders($this); } =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 TWiki::Request( {} ); } if( $query->path_info() ne $pathInfo ) { $query->path_info( "/$0/" . $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->getDatadir ); } =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; } =pod ---++ StaticMethod suffixToMimeType( $filename ) -> $mimeType Returns the MIME type corresponding to the extension of the $filename based on the file specified by {MimeTypesFileName}. If there is no extension or the extension is not found in the {MimeTypesFileName} file, 'text/plain' is returned. =cut sub suffixToMimeType { my( $theFilename ) = @_; my $mimeType = 'text/plain'; if( $theFilename =~ /\.([^.]+)$/ ) { my $suffix = $1; my @types = grep{ s/^\s*([^\s]+).*?\s$suffix\s.*$/$1/i } map { $_.' ' } split( /[\n\r]/, readFile( $TWiki::cfg{MimeTypesFileName} ) ); $mimeType = $types[0] if( @types ); } return $mimeType; } =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 (="=) | | =$aquot= | Apostrophe quote (='=) | | =$percnt= | Percent sign (=%=) | | =$dollar= | Dollar sign (=$=) | | =$lt= | Less than sign (=<=) | | =$gt= | Greater than 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/\$aquot(\(\))?/\'/gos; # expand apostrophe quote $text =~ s/\$percnt(\(\))?/\%/gos; # expand percent $text =~ s/\$dollar(\(\))?/\$/gos; # expand dollar $text =~ s/\$lt\b(\(\))?/\/gos; # expand greater than sign return $text; } # 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 ''; } #------------------------------------------------------------------- # Tag Handlers #------------------------------------------------------------------- sub BASETOPIC { my $this = shift; return $this->{SESSION_TAGS}{BASETOPIC}; } sub BASEWEB { my ( $this, $params ) = @_; return _handleWebTag( $this->{SESSION_TAGS}{BASEWEB}, $params ); } sub INCLUDINGTOPIC { my $this = shift; return $this->{SESSION_TAGS}{INCLUDINGTOPIC}; } sub INCLUDINGWEB { my ( $this, $params ) = @_; return _handleWebTag( $this->{SESSION_TAGS}{INCLUDINGWEB}, $params ); } sub TOPIC { my $this = shift; return $this->{SESSION_TAGS}{TOPIC}; } sub WEB { my ( $this, $params ) = @_; return _handleWebTag( $this->{SESSION_TAGS}{WEB}, $params ); } sub _handleWebTag { my( $theWeb, $params ) = @_; my $format = $params->{format} || $params->{_DEFAULT}; if( $format ) { my $web = $theWeb; my @w = split( /[\/\.]/, $theWeb ); my $size = scalar( @w ); my $parents = ''; if( $size > 1 && $web =~ /^(.*)[\/\.]/ ) { $parents = $1; } $theWeb = $format; $theWeb =~ s/\$web/$web/go; $theWeb =~ s/\$parents?/$parents/go; $theWeb =~ s/\$current/$w[-1]/go; $theWeb =~ s/\$(item|last)\(0\)//go; $theWeb =~ s/\$item\(([0-9]+)\)/$1 > $size ? '' : $w[$1-1]/geo; $theWeb =~ s/\$last\(([0-9]+)\)/my @t = @w; join('\/', splice( @t, ($1 > $size ? -$size : -$1), 99))/geo; $theWeb =~ s/\$top\(([0-9]+)\)/my @t = @w; join( '\/', splice( @t, 0, $1 ) )/geo; $theWeb =~ s/\$top/$w[0]/go; $theWeb =~ s/\$list/join( ', ', @w)/geo; $theWeb =~ s/\$size/$size/go; } return $theWeb; } sub TOPICTITLE { my ( $this, $params, $topic, $web ) = @_; # optional $params->{topic} can be "TopicName" or "Web.TopicName" $topic = $params->{topic} || $params->{_DEFAULT} || $topic; # normalize web and topic name ( $web, $topic ) = $this->normalizeWebTopicName( $web, $topic ); my $text = $topic; if( $this->{store}->topicExists( $web, $topic )) { my $meta = $this->inContext( 'can_render_meta' ); if( $meta && $web eq $this->{SESSION_TAGS}{BASEWEB} && $topic eq $this->{SESSION_TAGS}{BASETOPIC} ) { # use meta data of base topic $text = $meta->topicTitle(); } else { # not base topic, need to read meta data to get topic title try { my $dummyText; ( $meta, $dummyText ) = $this->{store}->readTopic( $this->{session}->{user}, $web, $topic ); $text = $meta->topicTitle() if( $meta ); } catch TWiki::AccessControlException with { # Ignore access exceptions }; } } if( $params->{encode} ) { $text = $this->ENCODE( { _DEFAULT => $text, type => $params->{encode} } ); } return $text; } sub FORM { my ( $this, $params, $topic, $web ) = @_; my $cgiQuery = $this->{request}; my $cgiRev = $cgiQuery->param('rev') if( $cgiQuery ); $params->{rev} = $cgiRev unless( defined $params->{rev} ); return $this->renderer->renderFORM( $params, $topic, $web ); } sub FORMFIELD { my ( $this, $params, $topic, $web ) = @_; my $cgiQuery = $this->{request}; my $cgiRev = $cgiQuery->param('rev') if( $cgiQuery ); $params->{rev} = $cgiRev unless( defined $params->{rev} ); return $this->renderer->renderFORMFIELD( $params, $topic, $web ); } sub EDITFORM { my ( $this, $params, $topic, $web ) = @_; return $this->renderer->renderEDITFORM( $params, $topic, $web ); } sub EDITFORMFIELD { my ( $this, $params, $topic, $web ) = @_; return $this->renderer->renderEDITFORMFIELD( $params, $topic, $web ); } sub TMPLP { my( $this, $params ) = @_; return $this->templates->tmplP( $params ); } sub VAR { my( $this, $params, $intopic, $inweb ) = @_; my $key = $params->{_DEFAULT}; my $default = $params->{default}; $default = '' unless ( defined($default) ); return $default unless $key; my $ignoreNull = TWiki::Func::isTrue($params->{ignorenull}); my $web = $params->{web}; my $topic = $params->{topic}; my $val; # always return a value, even when the key isn't defined if ( $topic ) { ( $web, $topic ) = $this->normalizeWebTopicName( $web || $inweb, $topic ); $val = $this->{prefs}->getTopicPreferencesValue( $key, $web, $topic ); } elsif ( $web ) { # handle %USERSWEB%-type cases ( $web, $topic ) = $this->normalizeWebTopicName( $web, $intopic ); $val = $this->{prefs}->getWebPreferencesValue( $key, $web ); } else { $val = $this->{prefs}->getPreferencesValue($key); return $val if ( defined($val) && ($val ne '' || !$ignoreNull) ); $val = $this->{SESSION_TAGS}{$key}; } return $val if ( defined($val) && ($val ne '' || !$ignoreNull) ); return $default; } sub PLUGINVERSION { my( $this, $params ) = @_; $this->{plugins}->getPluginVersion( $params->{_DEFAULT} ); } sub IF { my ( $this, $params, $topic, $web, $meta ) = @_; unless( $ifParser ) { require TWiki::If::Parser; $ifParser = new TWiki::If::Parser(); } my $texpr = $params->{_DEFAULT}; my $expr; my $result; if ( defined($texpr) && ($texpr =~ /^\s*$/ || $texpr =~ /^\s*0\s*$/) ) { # shortcut for a null string or 0 condition - compatibility with # TWiki 4.1 and consistency with a "1" condition. $params->{else} = '' unless defined $params->{else}; return expandStandardEscapes( $params->{else} ); } # Recursion block. $this->{evaluating_if} ||= {}; # Block after 5 levels. if ($this->{evaluating_if}->{$texpr} && $this->{evaluating_if}->{$texpr} > 5) { delete $this->{evaluating_if}->{$texpr}; return ''; } $this->{evaluating_if}->{$texpr}++; try { $expr = $ifParser->parse( $texpr ); unless( $meta ) { require TWiki::Meta; $meta = new TWiki::Meta( $this, $web, $topic ); } if( $expr->evaluate( tom=>$meta, data=>$meta )) { $params->{then} = '' unless defined $params->{then}; $result = expandStandardEscapes( $params->{then} ); } else { $params->{else} = '' unless defined $params->{else}; $result = expandStandardEscapes( $params->{else} ); } } catch TWiki::Infix::Error with { my $e = shift; $result = $this->inlineAlert( 'alerts', 'generic', 'IF{', $params->stringify(), '}:', $e->{-text} ); } finally { delete $this->{evaluating_if}->{$texpr}; }; return $result; } sub HIDE { # return empty string return ''; } sub HIDEINPRINT { # enclose content in div to hide when printing my ( $this, $params ) = @_; return ' ' . $params->{_DEFAULT} . ''; } sub _fixHeadingOffset { my ( $prefix, $level, $offset ) = @_; $level += $offset; $level = 1 if( $level < 1); $level = 6 if( $level > 6); return $prefix . '+' x $level; } # 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 $attachment = $params->remove('attachment') || ''; my $pattern = $params->remove('pattern'); my $headingoffset = $params->remove('headingoffset') || ''; my $hidetoc = isTrue( $params->remove('hidetoc') ) || isTrue( $this->{prefs}->getPreferencesValue( 'TOC_HIDE_IF_INCLUDED' ) ); my $rev = $params->remove('rev'); my $section = $params->remove('section'); my $disableFixLinks = $params->remove('disablefixlinks') || ''; # no sense in considering an empty string as an unfindable section: undef $section if (defined($section) && $section eq ''); my $raw = $params->remove('raw') || ''; my $warn = $params->remove('warn') || $this->{prefs}->getPreferencesValue( 'INCLUDEWARNING' ); my $allowAnyType = isTrue( $params->remove('allowanytype') ); my $charSet = $params->remove('charset') || ''; if( $path =~ /^https?\:/ ) { # include web page return _includeUrl( $this, $path, $pattern, $includingWeb, $includingTopic, $raw, $params, $warn, $allowAnyType, $charSet ); } if ( $path eq '' && $attachment ne '' ) { $path = $includingTopic; } $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 _includeWarning( $this, $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 _includeWarning( $this, $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 = keys %{$this->{_INCLUDES}}; $key .= $args; if( $this->{_INCLUDES}->{$key} || $count > 99) { return _includeWarning( $this, $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; ( $meta, $text ) = $this->{store}->readTopic( undef, $includedWeb, $includedTopic, $rev ); # Simplify leading, and remove trailing, newlines. If we don't remove # trailing, it becomes impossible to %INCLUDE a topic into a table. $text =~ s/^[\r\n]+/\n/; $text =~ s/[\r\n]+$//; unless( ($includingTopic eq $includedTopic && $includingWeb eq $includedWeb) || # you may include itself, in which case permission check needs to be # omitted for efficiency $this->security->checkAccessPermission( 'VIEW', $this->{user}, $text, $meta, $includedTopic, $includedWeb ) ) { if( isTrue( $warn )) { return $this->inlineAlert( 'alerts', 'access_denied', "[[$includedWeb.$includedTopic]]" ); } # else fail silently return ''; } if ( $attachment ne '' ) { my $mimeType = suffixToMimeType($attachment); if( $allowAnyType || $mimeType =~ /^text\/(html|plain|css)/ ) { unless( $this->{store}->attachmentExists( $includedWeb, $includedTopic, $attachment )) { return _includeWarning( $this, $warn, 'bad_attachment', $attachment); } $text = $this->{store}->readAttachment( undef, $includedWeb, $includedTopic, $attachment, $rev ); } else { return _includeWarning( $this, $warn, 'bad_content', $mimeType ); } } if ( $charSet ) { my $siteCharset = $TWiki::cfg{Site}{CharSet} || 'iso-8859-1'; $this->_convertCharsets($charSet, $siteCharset, \$text); } return $text if ( $raw ); # remove everything before and after the default include block unless # a section is explicitly defined if( !$section ) { $text =~ s/.*?%STARTINCLUDE%//s; $text =~ s/%STOPINCLUDE%.*//s; } # handle sections my( $ntext, $sections ) = parseSections( $text ); my $interesting = ( defined $section ); if( $interesting || scalar( @$sections )) { # Rebuild the text from the interesting 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} ); $disableFixLinks = 1 if( $s->{disablefixlinks} ); $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 ); # Do not show TOC in included topic if hidetoc parameter or # TOC_HIDE_IF_INCLUDED preference setting has been set if( $hidetoc ) { $text =~ s/%TOC(?:{(.*?)})?%//g; } # Codev.IncludeParametersWithDefault feature: # Change %ALLTAGS{ default="..." }% to %ALLTAGS% and capture tags with defaults # FIXME: Quick hack; do proper variable parsing my $verbatim = {}; $text = $this->renderer->takeOutBlocks( $text, 'verbatim', $verbatim ); my $tagsWithDefault = undef; $text =~ s/(%)($regex{tagNameRegex})(\{ *default=")(.*?[^\\]?)(" *\})(%)/ $tagsWithDefault->{$2} = _unescapeQuotes( $4 ); "$1$2$6"/ge; $this->renderer->putBackBlocks( \$text, $verbatim, 'verbatim' ); foreach my $k ( keys %$params ) { next if( $k eq '_RAW' ); # copy params into session tags $this->{SESSION_TAGS}{$k} = $params->{$k}; # remove captured tag with default delete $tagsWithDefault->{$k}; } foreach my $k ( keys %$tagsWithDefault ) { # copy left over captured tags with default into session tags $this->{SESSION_TAGS}{$k} = $tagsWithDefault->{$k}; } expandAllTags( $this, \$text, $includedTopic, $includedWeb, $meta ); # 4th parameter tells plugin that its called for an included file $this->{plugins}->dispatch( 'commonTagsHandler', $text, $includedTopic, $includedWeb, 1, $meta ); # We have to expand tags again, because a plugin may have inserted additional # tags. expandAllTags( $this, \$text, $includedTopic, $includedWeb, $meta ); # If needed, fix all 'TopicNames' to 'Web.TopicNames' to get the # right context so that links continue to work properly if( $includedWeb ne $includingWeb && !$disableFixLinks ) { my $removed = {}; my $noautolink = isTrue( $this->{prefs}->getPreferencesValue( 'NOAUTOLINK' ) ); $text = $this->renderer->forEachLine( $text, \&_fixupIncludedTopic, { web => $includedWeb, force_noautolink => $noautolink, # TWikibug:Item7188 pre => 1, noautolink => 1} ); # handle tags again because of plugin hook expandAllTags( $this, \$text, $includedTopic, $includedWeb, $meta ); } if( $headingoffset =~ s/.*?([-+]?[0-9]).*/$1/ ) { $text =~ s/^(---*)(\++)/_fixHeadingOffset( $1, length( $2 ), $headingoffset )/gem; } $this->_includePostProcessing(\$text, $params); # restore the tags delete $this->{_INCLUDES}->{$key}; %{$this->{SESSION_TAGS}} = %saveTags; $this->{prefs}->restore( $prefsMark ); return $text; } sub _http { my( $this, $params, $https ) = @_; my $res; my $field = $params->{_DEFAULT}; if ( $field ) { my $f = lc $field; $f =~ s/_/-/g; return '' if $httpHiddenField{$f}; $res = $https ? $this->{request}->https( $field ) : $this->{request}->http( $field ); } $res = '' unless defined( $res ); return $res; } sub HTTP { return _http($_[0], $_[1], 0); } sub HTTPS { return _http($_[0], $_[1], 1); } #deprecated functionality, now implemented using %ENV% #move to compatibility plugin in TWiki5 sub HTTP_HOST_deprecated { return $_[0]->{request}->header('Host') || ''; } #deprecated functionality, now implemented using %ENV% #move to compatibility plugin in TWiki5 sub REMOTE_ADDR_deprecated { return $_[0]->{request}->remoteAddress() || ''; } #deprecated functionality, now implemented using %ENV% #move to compatibility plugin in TWiki5 sub REMOTE_PORT_deprecated { # CGI/1.1 (RFC 3875) doesn't specify REMOTE_PORT, # but some webservers implement it. However, since # it's not RFC compliant, TWiki should not rely on # it. So we get more portability. return ''; } #deprecated functionality, now implemented using %ENV% #move to compatibility plugin in TWiki5 sub REMOTE_USER_deprecated { return $_[0]->{request}->remoteUser() || ''; } # 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(), $TWiki::cfg{DefaultDateFormat}, $TWiki::cfg{DisplayTimeValues}); } 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->{request}; my $cgiRev = ''; $cgiRev = $cgiQuery->param('rev') if( $cgiQuery ); my $rev = $params->{rev} || $cgiRev || ''; return $this->renderer->renderRevisionInfo( $web, $topic, undef, $rev, $format ); } sub REVTITLE { my ( $this, $params, $theTopic, $theWeb ) = @_; my $request = $this->{request}; my $out = ''; if( $request ) { my $rev = $this->{store}->cleanUpRevID( $request->param( 'rev' ) ); $out = '(r'.$rev.')' if ($rev); } return $out; } sub REVARG { my ( $this, $params, $theTopic, $theWeb ) = @_; my $request = $this->{request}; my $out = ''; if( $request ) { my $rev = $this->{store}->cleanUpRevID( $request->param( 'rev' ) ); $out = '&rev='.$rev if ($rev); } return $out; } sub ENCODE { my( $this, $params ) = @_; my $type = $params->{type} || 'url'; my $extra = $params->{extra} || ''; my $text = $params->{_DEFAULT}; $text = '' unless( defined $text && $text ne '' ); my $newLine = $params->{newline}; if( defined $newLine ) { $newLine =~ s/\$br\b/\0-br-\0/go; $newLine =~ s/\$n\b/\0-n-\0/go; $text =~ s/\r?\n/$newLine/go; } my $encoded = _encode( $type, $text, expandStandardEscapes( $extra ) ); if( defined $newLine ) { $encoded =~ s/\0-br-\0/
/go; $encoded =~ s/\0-n-\0/\n/go; } return $encoded; } sub ENTITY { my( $this, $params ) = @_; my $text = $params->{_DEFAULT}; $text = '' unless( defined $text && $text ne '' ); return _encode( 'html', $text ); } sub _encode { my( $type, $text, $extra ) = @_; if ( $type =~ /^entit(y|ies)$/i ) { # entity encode return entityEncode( $text, $extra ); } elsif ( $type =~ /^html$/i ) { # entity encode, encode also space, newline and linefeed return entityEncode( $text, " \n\r" ); } elsif ( $type =~ /^quotes?$/i ) { # escape quotes with backslash (Item3383) $text =~ s/\"/\\"/go; return $text; } elsif ( $type =~ /^search$/i ) { # substitue % with \x1a (Item7847), also escape quotes with backslash $text =~ s/\"/\\"/go; $text =~ s/%/$percentSubstitute/go; return $text; } elsif ($type =~ /^url$/i) { # legacy $text =~ s/\r*\n\r*/
/g; return urlEncode( $text ); } elsif ( $type =~ /^(off|none)$/i ) { # no encoding return $text; } elsif ($type =~ /^moderate$/i) { # entity encode ' " < and > $text =~ s/([<>'"])/''.ord($1).';'/ge; return $text; } elsif ($type =~ /^csv$/i) { # escape for CSV use: Repeat ' and " $text =~ s/(['"])/$1$1/g; return $text; } elsif ($type =~ /^json$/i) { # escape for JSON string use: Double quotes, backslashes and non-printable chars $text =~ s/(["\\])/\\$1/go; $text =~ s/[\b]/\\b/go; $text =~ s/\f/\\f/go; $text =~ s/\n/\\n/go; $text =~ s/\r/\\r/go; $text =~ s/\t/\\t/go; $text =~ s/([\x00-\x1F])/sprintf( '\u%04x', ord($1) )/geo; return $text; } else { # safe or default # entity encode ' " < > and % $text =~ s/([<>%'"])/''.ord($1).';'/ge; return $text; } } sub ENV { my ($this, $params) = @_; my $key = $params->{_DEFAULT}; return '' unless $key && defined $TWiki::cfg{AccessibleENV} && $key =~ /$TWiki::cfg{AccessibleENV}/o; my $val; if ( $key =~ /^HTTPS?_(.*)/ ) { $val = $this->{request}->header($1); } elsif ( $key eq 'REQUEST_METHOD' ) { $val = $this->{request}->request_method; } elsif ( $key eq 'REMOTE_USER' ) { $val = $this->{request}->remoteUser; } elsif ( $key eq 'REMOTE_ADDR' ) { $val = $this->{request}->remoteAddress; } else { # TSA SMELL: TWiki::Request doesn't support # SERVER_\w+, REMOTE_HOST and REMOTE_IDENT. # Use %ENV as fallback, but for ones above # wil probably not behave as expected if # running with non-CGI engine. $val = $ENV{$key}; } return defined $val ? $val : 'not set'; } 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; try { $s = $this->search->searchWeb( %$params ); if( my $encode = $params->{encode} ) { $s = $this->ENCODE( { _DEFAULT => $s, type => $encode } ); } } catch Error::Simple with { my $message = (DEBUG) ? shift->stringify() : shift->{-text}; # Block recursions kicked off by the text being repeated in the # error message $message =~ s/%([A-Z]*[{%])/%$1/g; $s = $this->inlineAlert( 'alerts', 'bad_search', $message ); }; return $s; } sub WEBLIST { my( $this, $params ) = @_; my $format = $params->{_DEFAULT} || $params->{'format'} || '$name'; my $separator = expandStandardEscapes($params->{separator} || "\n"); my $web = $params->{web} || ''; my $webs = $params->{webs} || 'public'; my $exclude = $params->{exclude} || ''; my $selection = $params->{selection} || ''; $selection =~ s/\,/ /g; $selection = " $selection "; my $showWeb = $params->{subwebs} || ''; my $limit = $params->{limit} || '32000'; my $overlimit = $params->{overlimit} || ''; my $depth = $params->{depth}; my $reverse = isTrue($params->{reverse}); if ( defined($depth) ) { if ( $depth =~ /^(\d+)/ ) { $depth = $1; } else { $depth = undef; } } 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', $showWeb, $depth ) ); } elsif ( $aweb eq 'canmoveto' ) { push( @list, $this->{store}->getListOfWebs( 'user,public,allowed,canmoveto', $showWeb, $depth ) ); } elsif ( $aweb eq 'cancopyto' ) { push( @list, $this->{store}->getListOfWebs( 'user,public,allowed,cancopyto', $showWeb, $depth ) ); } elsif( $aweb eq 'webtemplate' ) { push( @list, $this->{store}->getListOfWebs( 'template,allowed', $showWeb, $depth )); } else { push( @list, $aweb ) if( $this->{store}->webExists( $aweb ) ); } } if( $exclude ) { # turn exclude into a regex: $exclude =~ s/,\s*/|/g; # change comma list to regex "or" $exclude =~ s/[^$regex{mixedAlphaNum}\_\.\/\*\|]//g; # filter out illegal chars $exclude =~ s/\*/.*/g; # change wildcard to regex } my @items; my $indent = CGI::span({class=>'twikiWebIndent'},''); my $i = 0; @list = reverse @list if ( $reverse ); foreach my $item ( @list ) { if( $exclude && $item =~ /^($exclude)$/ ) { next; } if( $i++ >= $limit ) { push( @items, $overlimit ) if $overlimit; last; } 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 $listindent = ' ' x (($item =~ tr:/::) - ($showWeb eq '' ? 0 : ($showWeb =~ tr:/::) + 1)); # $s =~ tr:/:: doesn't modify $s $line =~ s/\$listindent\b/$listindent/g; my $mark = ( $selection =~ / \Q$item\E / ) ? $marker : ''; $line =~ s/\$marker/$mark/g; $line = expandStandardEscapes($line); push( @items, $line ); } return join( $separator, @items); } sub TOPICLIST { my( $this, $params ) = @_; my $format = $params->{_DEFAULT} || $params->{'format'} || '$topic'; 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/\$topic\b/$item/g; $line =~ s/\$name\b/$item/g; # Undocumented, DO NOT REMOVE $line =~ s/\$qname/"$item"/g; # Undocumented, DO NOT REMOVE my $mark = ( $selection =~ / \Q$item\E / ) ? $marker : ''; $line =~ s/\$marker/$mark/g; $line = expandStandardEscapes( $line ); push( @items, $line ); } return join( $separator, @items); } sub QUERYSTRING { my $this = shift; my $qs = $this->{request}->queryString(); # Item7595: Sanitize QUERYSTRING to counter XSS exploits $qs =~ s/(['\/<>])/'%'.sprintf('%02x', ord($1))/ge; return $qs; } sub QUERYPARAMS { my ( $this, $params ) = @_; return '' unless $this->{request}; my $format = defined $params->{format} ? $params->{format} : '$name=$value'; my $separator = defined $params->{separator} ? $params->{separator} : "\n"; # Item6621: Deprecate encoding="", add encode="". Do NOT remove encoding=""! my $encoding = $params->{encode} || $params->{encoding} || ''; my @list; foreach my $name ( $this->{request}->param() ) { # clean parameter names of illegal characters $name =~ s/['"<>].*//; # Issues multi-valued parameters as separate hiddens if( $name ) { foreach my $value ( $this->{request}->param( $name ) ) { $value = '' unless defined $value; $value = _encode( $encoding, $value ) if( $encoding ); my $entry = $format; $entry =~ s/\$name/$name/g; $entry =~ s/\$value/$value/; push( @list, $entry ); } } } return expandStandardEscapes(join($separator, @list)); } sub URLPARAM { my( $this, $params ) = @_; my $param = $params->{_DEFAULT} || ''; my $newLine = $params->{newline}; my $encode = $params->{encode} || 'safe'; my $multiple = $params->{multiple}; my $format = $params->{format} || '$value'; my $separator = $params->{separator}; $separator="\n" unless (defined $separator); my $value; if( $this->{request} ) { if( TWiki::isTrue( $multiple )) { my @valueArray = $this->{request}->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->{request}->param( $param ); } } if( defined $value ) { $format =~ s/\$value/$value/go; $value = $format; if( defined $newLine ) { $newLine =~ s/\$br\b/\0-br-\0/go; $newLine =~ s/\$n\b/\0-n-\0/go; $value =~ s/\r?\n/$newLine/go; $value = _encode( $encode, $value ); $value =~ s/\0-br-\0/
/go; $value =~ s/\0-n-\0/\n/go; } else { $value = _encode( $encode, $value ); } } unless( defined $value && $value ne '' ) { $value = $params->{default}; $value = '' unless defined $value; } # Block expansion of %URLPARAM in the value to prevent recursion $value =~ s/%URLPARAM\{/%URLPARAM{/g; 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_deprecated { 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). # Move to compatibility module in TWiki5 sub SPACEDTOPIC_deprecated { 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 $iconName = $params->{_DEFAULT} || ''; my $format = $params->{format} || '$img'; my $default = $params->{default} || ''; return $this->formatIcon( $iconName, $format, $default ); } sub ICONURL { my( $this, $params ) = @_; my $iconName = ( $params->{_DEFAULT} || '' ); my $default = $params->{default} || ''; return $this->formatIcon( $iconName, '$url', $default ); } sub ICONURLPATH { my( $this, $params ) = @_; my $iconName = ( $params->{_DEFAULT} || '' ); my $default = $params->{default} || ''; return $this->formatIcon( $iconName, '$urlpath', $default ); } 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; my $min = 1; $str =~ s/~\[(\_(\d+))~\]/ $max = $2 if ($2 > $max); $min = $2 if ($2 < $min); "[$1]"/ge; $str =~ s/~\[(\*,\_(\d+),[^,]+(,([^,]+))?)~\]/ $max = $2 if ($2 > $max); $min = $2 if ($2 < $min); "[$1]"/ge; # Item7080: Sanitize MAKETEXT variable: return "MAKETEXT error: No more than 32 parameters are allowed" if( $max > 32 ); return "MAKETEXT error: Parameter 0 is not allowed" if( $min < 1 ); if( $TWiki::cfg{UserInterfaceInternationalisation} ) { eval { require Locale::Maketext; }; no warnings('numeric'); $str =~ s#\\#\\\\#g if( $@ || !$@ && $Locale::Maketext::VERSION < 1.23 ); } # 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 { return $_[0]->{request}->action; } sub scriptUrlSub { my ( $this, $params, $absolute ) = @_; my $script = $params->{_DEFAULT} || ''; my $web = $params->{web}; my $topic = $params->{topic}; $topic = '' if ( !defined($topic) ); my @optParams; if ( isTrue($params->{master}) ) { push(@optParams, '$master', 1); } my $url = $this->getScriptUrl($absolute, $script, $web, $topic, @optParams); if ( $web && !$topic ) { $url = substr($url, 0, -length($cfg{HomeTopicName})-1); } return $url; } sub SCRIPTURL { # my ( $this, $params, $topic, $web ) = @_; return scriptUrlSub($_[0], $_[1], 1); } sub SCRIPTURLPATH { # my ( $this, $params, $topic, $web ) = @_; return scriptUrlSub($_[0], $_[1], 0); } sub PUBURL { my $this = shift; return $this->getPubUrl(1); } sub PUBURLPATH { my $this = shift; return $this->getPubUrl(0); } sub getContentMode { my ( $this, $web ) = @_; if ( !defined($web) || $web eq '' || $web eq $this->{webName} ) { return $this->{contentMode}; } else { return ($this->modeAndMaster($web))[0]; } } sub webWritable { my ( $this, $web ) = @_; my $mode = $this->getContentMode($web); return ($mode eq 'slave' || $mode eq 'read-only') ? 0 : 1; } sub CONTENTMODE { #my ( $this, $params ) = @_; return $_[0]->getContentMode($_[1]->{web}); } sub ALLVARIABLES { return shift->{prefs}->stringify(); } sub META { my ( $this, $params, $topic, $web ) = @_; # TWikibug:Item6438: %META uses current web.topic scope, but base topic's meta data. # ==> Quirky spec for compatibility with pre 5.0 releases where base topic is used # by default instead of current topic because meta data is pulled from base topic. $web = $this->{SESSION_TAGS}{BASEWEB} || $web; $topic = $this->{SESSION_TAGS}{BASETOPIC} || $topic; my $meta = $this->inContext( 'can_render_meta' ); my $paramTopic = $params->{topic}; if( $paramTopic ) { ( $web, $topic ) = $this->normalizeWebTopicName( $web, $paramTopic ); try { my $dummyText; ( $meta, $dummyText ) = $this->{store}->readTopic( $this->{session}->{user}, $web, $topic ); } catch TWiki::AccessControlException with { # Ignore access exceptions return ''; }; } return '' unless $meta; my $result = ''; my $option = $params->{_DEFAULT} || ''; if( $option eq 'form' ) { # META:FORM and META:FIELD $result = $meta->renderFormForDisplay( $this->templates ); } elsif ( $option eq 'formfield' ) { # a formfield from within topic text $result = $meta->renderFormFieldForDisplay( $params->get('name'), '$value', $params ); } elsif( $option eq 'attachments' ) { # renders attachment tables $result = $this->attach->renderMetaData( $web, $topic, $meta, $params ); } elsif( $option eq 'moved' ) { $result = $this->renderer->renderMoved( $web, $topic, $meta, $params ); } elsif( $option eq 'parent' ) { $result = $this->renderer->renderParent( $web, $topic, $meta, $params ); } return expandStandardEscapes($result); } sub PARENTTOPIC { my ( $this, $params, $topic, $web ) = @_; my $metaParams = { _DEFAULT => 'parent', format => $params->{format} || '$topic', topic => $params->{topic} || "$web.$topic", dontrecurse => 'on', }; return $this->META( $metaParams, $topic, $web ); } # 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 ) = @_; $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 ) = @_; $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 ) = @_; $params->{format} = $this->{prefs}->getPreferencesValue( 'WIKIUSERNAME' ) || '$wikiusername'; return $this->USERINFO($params); } sub USERINFO { my ( $this, $params ) = @_; my $format = $params->{format} || '$username, $wikiusername, $emails'; my $user = $this->{user}; if( $params->{_DEFAULT} ) { $user = $params->{_DEFAULT}; return '' if !$user; # map wikiname to a login name $user = $this->{users}->getCanonicalUserID($user); return '' unless $user; return '' if( $TWiki::cfg{AntiSpam}{HideUserDetails} && !$this->{users}->isAdmin( $this->{user} ) && $user ne $this->{user} ); } return '' unless $user; my $info = $format; if ($info =~ /\$username/) { my $username = $this->{users}->getLoginName($user); $username = 'unknown' unless defined $username; $info =~ s/\$username/$username/g; } if ($info =~ /\$wikiname/) { my $wikiname = $this->{users}->getWikiName( $user ); $wikiname = 'UnknownUser' unless defined $wikiname; $info =~ s/\$wikiname/$wikiname/g; } if ($info =~ /\$wikiusername/) { my $wikiusername = $this->{users}->webDotWikiName($user); $wikiusername = "$TWiki::cfg{UsersWebName}.UnknownUser" unless defined $wikiusername; $info =~ s/\$wikiusername/$wikiusername/g; } if ($info =~ /\$emails/) { my $emails = join(', ', $this->{users}->getEmails($user)); $info =~ s/\$emails/$emails/g; } if ($info =~ /\$groups/) { my @groupNames; my $it = $this->{users}->eachMembership( $user ); while( $it->hasNext()) { my $group = $it->next(); push( @groupNames, $group); } my $groups = join(', ', @groupNames); $info =~ s/\$groups/$groups/g; } if ($info =~ /\$cUID/) { my $cUID = $user; $info =~ s/\$cUID/$cUID/g; } if ($info =~ /\$admin/) { my $admin = $this->{users}->isAdmin($user) ? 'true' : 'false'; $info =~ s/\$admin/$admin/g; } return $info; } sub GROUPS { my ( $this, $params ) = @_; my $format = $params->{format} || '| $grouplink | $members |'; my $separator = expandStandardEscapes( $params->{separator} || "\n" ); my $memberSeparator = expandStandardEscapes( $params->{memberseparator} || ", " ); my $memberFormat = $params->{memberformat} || '[[$wikiusername][$wikiname]]'; my $limit_output = $params->{memberlimit} || 32; $limit_output = 32000 if( $limit_output eq 'all' ); my $header = $params->{header}; $header = '| *' . $this->i18n->maketext( 'Group' ) . '* | *' . $this->i18n->maketext( 'Members' ) . '* |' unless( defined $header ); $header = '' if( $header eq 'none' ); $header = expandStandardEscapes( $header ); $header .= $separator unless( $header eq '' ); my $groups = $this->{users}->eachGroup(); my @table = (); while( $groups->hasNext() ) { my $group = $groups->next(); # Nop it to prevent wikiname expansion unless the topic exists. my $groupLink = " $group"; if( $this->{store}->topicExists( $TWiki::cfg{UsersWebName}, $group ) ) { $groupLink = '[['.$TWiki::cfg{UsersWebName}.".$group][$group]]"; } my $it = $this->{users}->eachGroupMember( $group ); my @members = (); my $i = 0; while( $it->hasNext() ) { $i++; last if( $i > $limit_output ); push( @members, $it->next() ); } @members = map { my $user = $_; $_ = $memberFormat; s/\$cuid/$user/go; s/\$wikiname/$this->{users}->getWikiName( $user )/geo; s/\$wikiusername/$this->{users}->webDotWikiName( $user )/geo; $_; } @members; @members = sort @members if ( isTrue($params->{sort}) ); my $members = join( $memberSeparator, @members ); $members .= $memberSeparator . '...' if( $i > $limit_output ); my $line = $format; $line =~ s/\$grouplink/$groupLink/go; $line =~ s/\$group/$group/go; $line =~ s/\$members/$members/go; $line = expandStandardEscapes( $line ); push( @table, $line ); } # add hardcoded AllUsersGroup my $line = $format; my $group = 'AllUsersGroup'; my $groupLink = '[['.$TWiki::cfg{UsersWebName}.".$group][$group]]"; my $members = $this->i18n->maketext( 'All users including unauthenticated users.' ); $line =~ s/\$grouplink/$groupLink/go; $line =~ s/\$group/$group/go; $line =~ s/\$members/$members/go; $line = expandStandardEscapes( $line ); push( @table, $line ); # add hardcoded AllAuthUsersGroup $line = $format; $group = 'AllAuthUsersGroup'; $groupLink = '[['.$TWiki::cfg{UsersWebName}.".$group][$group]]"; $members = $this->i18n->maketext( 'All authenticated users.' ); $line =~ s/\$grouplink/$groupLink/go; $line =~ s/\$group/$group/go; $line =~ s/\$members/$members/go; $line = expandStandardEscapes( $line ); push( @table, $line ); return $header . join( $separator, sort @table ); } sub CRYPTTOKEN { my ($this ) = @_; return $this->{users}->{loginManager}->createCryptToken(); } sub _getMdrepoField { my ($rec, $recId, $fieldName) = @_; if ( $fieldName eq '' ) { return $recId; } elsif ( $fieldName eq '_' ) { return join(" ", map { "$_=$rec->{$_}" } sort keys %$rec); } return $rec->{$fieldName} || ''; } sub _mdrepoFieldCond { my ($neg, $val, $ifMet) = @_; if ( $neg ) { return $val ? '' : $ifMet; } else { return $val ? $ifMet : ''; } } sub _mdrepoExpand { my ($rec, $id, $fmt, $selection, $marker) = @_; my $m = $id eq $selection ? $marker : ''; $fmt =~ s/\?(!?)(\w+)([!#%'\/:?@^`|~])(.*?)\3/_mdrepoFieldCond($1, $rec->{$2}, $4)/ge; $fmt =~ s/\$marker(\(\))?/$m/g; $fmt =~ s/\$_(\w*)(\(\))?/_getMdrepoField($rec, $id, $1)/ge; $fmt =~ s/\$question\(\)/\?/g; $fmt =~ s/\$question\b/\?/g; return expandStandardEscapes($fmt); } sub MDREPO { my ( $this, $params ) = @_; my $mdrepo = $this->{mdrepo}; return '' unless ( $mdrepo ); if ( my $web = $params->{web} ) { $web = topLevelWeb($web); my $rec = $mdrepo->getRec('webs', $web); unless ( $rec ) { return $params->{default} || ''; } my $format = $params->{_DEFAULT} || '$__'; return _mdrepoExpand($rec, $web, $format, ''); } my $table = $params->{_DEFAULT} || $params->{table} || ''; my $filter = $params->{filter} || ''; my $format = $params->{format} || '| $_ | $__ |'; my $separator = $params->{separator}; if ( defined($separator) ) { $separator = expandStandardEscapes($separator); } else { $separator = "\n"; } my $exclude = $params->{exclude} || ''; my $selection = $params->{selection} || ''; my $marker = $params->{marker} || 'selected'; my @excludes; if ( $exclude ) { for my $i ( split(/,\s*/, $exclude) ) { push(@excludes, qr/^$i$/); } } my @recIds = $mdrepo->getList($table); if ( $filter ) { @recIds = grep { $_ =~ /$filter/i } @recIds; } my @ents; RECID_LOOP: for my $i ( sort { lc $a cmp lc $b } @recIds ) { for my $e ( @excludes ) { next RECID_LOOP if ( $i =~ $e ); } my $rec = $mdrepo->getRec($table, $i); push(@ents, _mdrepoExpand($rec, $i, $format, $selection, $marker)); } join($separator, @ents); } sub DISKID { my ( $this, $params ) = @_; my $web = $params->{web} || $this->{webName}; return ($this->getStorageInfo($web))[2]; } sub trashWebName { my ( $this, %param ) = @_; if ( !$TWiki::cfg{MultipleDisks} ) { return $TWiki::cfg{TrashWebName}; } my $diskID; if ( defined($param{disk}) ) { $diskID = $param{disk}; } else { $diskID = ($this->getDiskInfo($param{web}))[2]; } my $name = $TWiki::cfg{TrashWebName}; $name .= 'x' . $diskID . 'x' if ( $diskID ); return $name; } sub TRASHWEB { my ( $this, $params, $topic, $web ) = @_; my $w = $params->{web} || $web; return $this->trashWebName(web => $w); } sub _wikiWebMaster { my ( $this, $params, $name ) = @_; my $web = $params->{web} || $this->{webName}; my $topic = $params->{topic} || $this->{topicName}; my $mapping = $this->{users}{mapping}; my $result = ''; if ( $mapping->can('wikiWebMaster') ) { $result = $mapping->wikiWebMaster($web, $topic, $name); } if ( $result ) { return $result; } else { return $name ? $TWiki::cfg{WebMasterName} : $TWiki::cfg{WebMasterEmail}; } } sub WIKIWEBMASTER { return _wikiWebMaster(@_[0, 1], 0); } sub WIKIWEBMASTERNAME { return _wikiWebMaster(@_[0, 1], 1); } 1;