001/* 002 Licensed to the Apache Software Foundation (ASF) under one 003 or more contributor license agreements. See the NOTICE file 004 distributed with this work for additional information 005 regarding copyright ownership. The ASF licenses this file 006 to you under the Apache License, Version 2.0 (the 007 "License"); you may not use this file except in compliance 008 with the License. You may obtain a copy of the License at 009 010 http://www.apache.org/licenses/LICENSE-2.0 011 012 Unless required by applicable law or agreed to in writing, 013 software distributed under the License is distributed on an 014 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 KIND, either express or implied. See the License for the 016 specific language governing permissions and limitations 017 under the License. 018 */ 019 020package org.apache.wiki.plugin; 021 022import org.apache.commons.lang3.StringUtils; 023import org.apache.wiki.api.core.Context; 024import org.apache.wiki.api.core.Page; 025import org.apache.wiki.api.exceptions.PluginException; 026import org.apache.wiki.api.plugin.Plugin; 027import org.apache.wiki.api.providers.WikiProvider; 028import org.apache.wiki.auth.AuthorizationManager; 029import org.apache.wiki.auth.permissions.PermissionFactory; 030import org.apache.wiki.pages.PageManager; 031import org.apache.wiki.render.RenderingManager; 032import org.apache.wiki.util.HttpUtil; 033import org.apache.wiki.util.TextUtil; 034import org.apache.wiki.variables.VariableManager; 035 036import java.security.Principal; 037import java.util.Map; 038import java.util.regex.Pattern; 039import java.util.regex.PatternSyntaxException; 040import org.apache.wiki.util.TimeLimitedRegex; 041 042/** 043 * The IfPlugin allows parts of a WikiPage to be executed conditionally, and is intended as a flexible way 044 * of customizing a page depending on certain conditions. Do not use it as a security mechanism to conditionally 045 * hide content from users (use page ACLs for that). 046 * 047 * You can also use shorthand "If" to run it. 048 * 049 * Parameters: 050 * <ul> 051 * <li><b>group</b> - A "|" -separated list of group names. 052 * <li><b>user</b> - A "|" -separated list of user names. 053 * <li><b>ip</b> - A "|" -separated list of ip addresses. 054 * <li><b>var</b> - A wiki variable 055 * <li><b>page</b> - A page name 056 * <li><b>contains</b> - A Perl5 regexp pattern 057 * <li><b>is</b> - A Perl5 regexp pattern 058 * <li><b>exists</b> - "true" or "false". 059 * </ul> 060 * 061 * <p>If any of them match, the body of the plugin is executed. You can 062 * negate the content by prefixing it with a "!". For example, to greet 063 * all admins, put the following in your LeftMenu:</p> 064 * <pre> 065 * [{If group='Admin' 066 * 067 * Hello, Admin, and your mighty powers!}] 068 * </pre> 069 * 070 * <p>In order to send a message to everybody except Jack use</p> 071 * <pre> 072 * [{If user='!Jack' 073 * 074 * %%warning 075 * Jack's surprise birthday party at eleven! 076 * %%}] 077 * </pre> 078 * 079 * <p>Note that you can't use "!Jack|!Jill", because for Jack, !Jill matches; 080 * and for Jill, !Jack matches. These are not regular expressions (though 081 * they might become so in the future).<p> 082 * 083 * <p>To check for page content, use</p> 084 * <pre> 085 * [{If page='TestPage' contains='xyzzy' 086 * 087 * Page contains the text "xyzzy"}] 088 * </pre> 089 * 090 * <p>The difference between "contains" and "is" is that "is" is always an exact match, 091 * whereas "contains" just checks if a pattern is available.</p> 092 * 093 * <p>To check for page existence, use</p> 094 * <pre> 095 * [{If page='TestPage' exists='true' 096 * 097 * Page "TestPage" exists.}] 098 * </pre> 099 * <p>With the same mechanism, it's also possible to test for the existence 100 * of a variable - just use "var" instead of "page".</p> 101 * 102 * <p>Another caveat is that the plugin body content is not counted 103 * towards ReferenceManager links. So any links do not appear on any reference 104 * lists. Depending on your position, this may be a good or a bad 105 * thing.</p> 106 * 107 * <h3>Calling Externally</h3> 108 * 109 * <p>The functional, decision-making part of this plugin may be called from 110 * other code (e.g., other plugins) since it is available as a static method 111 * {@link #ifInclude(Context,Map)}. Note that the plugin body may contain 112 * references to other plugins.</p> 113 * 114 * @since 2.6 115 */ 116public class IfPlugin implements Plugin { 117 118 /** The parameter name for setting the group to check. Value is <tt>{@value}</tt>. */ 119 public static final String PARAM_GROUP = "group"; 120 121 /** The parameter name for setting the user id to check. Value is <tt>{@value}</tt>. */ 122 public static final String PARAM_USER = "user"; 123 124 /** The parameter name for setting the ip address to check. Value is <tt>{@value}</tt>. */ 125 public static final String PARAM_IP = "ip"; 126 127 /** The parameter name for setting the page name to check. Value is <tt>{@value}</tt>. */ 128 public static final String PARAM_PAGE = "page"; 129 130 /** The parameter name for setting the contents of the page to check. Value is <tt>{@value}</tt>. */ 131 public static final String PARAM_CONTAINS = "contains"; 132 133 /** The parameter name for setting the variable name to check. Value is <tt>{@value}</tt>. */ 134 public static final String PARAM_VAR = "var"; 135 136 /** The parameter name for setting the exact content to check. Value is <tt>{@value}</tt>. */ 137 public static final String PARAM_IS = "is"; 138 139 /** The parameter name for checking whether a page/var exists. Value is <tt>{@value}</tt>. */ 140 public static final String PARAM_EXISTS = "exists"; 141 142 /** 143 * {@inheritDoc} 144 */ 145 @Override 146 public String execute( final Context context, final Map< String, String > params ) throws PluginException { 147 return ifInclude( context,params ) 148 ? context.getEngine().getManager( RenderingManager.class ).textToHTML( context, params.get( DefaultPluginManager.PARAM_BODY ) ) 149 : "" ; 150 } 151 152 153 /** 154 * Returns a boolean result based on processing the WikiContext and 155 * parameter Map as according to the rules stated in the IfPlugin 156 * documentation. 157 * As a static method this may be called by other classes. 158 * 159 * @param context The current WikiContext. 160 * @param params The parameter Map which contains key-value pairs. 161 * @throws PluginException If something goes wrong 162 * @return True, if the condition holds. 163 */ 164 public static boolean ifInclude( final Context context, final Map< String, String > params ) throws PluginException { 165 final String group = params.get( PARAM_GROUP ); 166 final String user = params.get( PARAM_USER ); 167 final String ip = params.get( PARAM_IP ); 168 final String page = params.get( PARAM_PAGE ); 169 final String contains = params.get( PARAM_CONTAINS ); 170 final String var = params.get( PARAM_VAR ); 171 final String is = params.get( PARAM_IS ); 172 final String exists = params.get( PARAM_EXISTS ); 173 174 boolean include = checkGroup( context, group ); 175 include |= checkUser(context, user); 176 include |= checkIP(context, ip); 177 178 if( page != null ) { 179 // Only read the target page's content if the requesting session is allowed to view it; 180 // otherwise behave exactly as if the content did not match, so ACL-protected content 181 // cannot be probed through the contains/is parameters. 182 String content = null; 183 final Page testedPage = context.getEngine().getManager( PageManager.class ).getPage( page ); 184 if( testedPage != null && context.getEngine().getManager( AuthorizationManager.class ) 185 .checkPermission( context.getWikiSession(), PermissionFactory.getPagePermission( testedPage, "view" ) ) ) { 186 content = context.getEngine().getManager( PageManager.class ).getPureText(page, WikiProvider.LATEST_VERSION).trim(); 187 } 188 include |= checkContains(content,contains); 189 include |= checkIs(content,is); 190 include |= checkExists(context,page,exists); 191 } 192 193 if( var != null ) { 194 final String content = context.getEngine().getManager( VariableManager.class ).getVariable(context, var); 195 include |= checkContains(content,contains); 196 include |= checkIs(content,is); 197 include |= checkVarExists(content,exists); 198 } 199 200 return include; 201 } 202 203 private static boolean checkExists( final Context context, final String page, final String exists ) { 204 if( exists == null ) { 205 return false; 206 } 207 return !context.getEngine().getManager( PageManager.class ).wikiPageExists( page ) ^ TextUtil.isPositive(exists); 208 } 209 210 private static boolean checkVarExists( final String varContent, final String exists ) { 211 if( exists == null ) { 212 return false; 213 } 214 return varContent == null ^ TextUtil.isPositive( exists ); 215 } 216 217 private static boolean checkGroup( final Context context, final String group ) { 218 if( group == null ) { 219 return false; 220 } 221 final String[] groupList = StringUtils.split(group,'|'); 222 boolean include = false; 223 224 for( final String grp : groupList ) { 225 String gname = grp; 226 boolean invert = false; 227 if( grp.startsWith( "!" ) ) { 228 if( grp.length() > 1 ) { 229 gname = grp.substring( 1 ); 230 } 231 invert = true; 232 } 233 234 final Principal g = context.getEngine().getManager( AuthorizationManager.class ).resolvePrincipal( gname ); 235 236 include |= context.getEngine().getManager( AuthorizationManager.class ).isUserInRole( context.getWikiSession(), g ) ^ invert; 237 } 238 return include; 239 } 240 241 private static boolean checkUser( final Context context, final String user ) { 242 if( user == null || context.getCurrentUser() == null ) { 243 return false; 244 } 245 246 final String[] list = StringUtils.split(user,'|'); 247 boolean include = false; 248 249 for( final String usr : list ) { 250 String userToCheck = usr; 251 boolean invert = false; 252 if( usr.startsWith( "!" ) ) { 253 invert = true; 254 // strip ! 255 if( user.length() > 1 ) { 256 userToCheck = usr.substring( 1 ); 257 } 258 } 259 260 include |= userToCheck.equals( context.getCurrentUser().getName() ) ^ invert; 261 } 262 return include; 263 } 264 265 // TODO: Add subnetwork matching, e.g. 10.0.0.0/8 266 private static boolean checkIP( final Context context, final String ipaddr ) { 267 if( ipaddr == null || context.getHttpRequest() == null ) { 268 return false; 269 } 270 271 final String[] list = StringUtils.split(ipaddr,'|'); 272 boolean include = false; 273 274 for( final String ip : list ) { 275 String ipaddrToCheck = ip; 276 boolean invert = false; 277 if( ip.startsWith( "!" ) ) { 278 invert = true; 279 // strip ! 280 if( ip.length() > 1 ) { 281 ipaddrToCheck = ip.substring( 1 ); 282 } 283 } 284 285 include |= ipaddrToCheck.equals( HttpUtil.getRemoteAddress( context.getHttpRequest() ) ) ^ invert; 286 } 287 return include; 288 } 289 290 private static boolean doMatch( final String content, final String pattern ) throws PluginException { 291 292 293 try { 294 // DOTALL gives '.' the same match-newlines behaviour as the previous Perl5Compiler.SINGLELINE_MASK. 295 // The match is time-boxed: pattern and page content are both author-controlled, so an unbounded 296 // backtracking match is a render-time denial of service (see TimeLimitedRegex). 297 final Pattern matchp = Pattern.compile( pattern, Pattern.DOTALL ); 298 return TimeLimitedRegex.matches( content, matchp ); 299 //final Pattern matchp = compiler.compile( pattern, Perl5Compiler.SINGLELINE_MASK ); 300 //return matcher.matches( content, matchp ); 301 } catch( final PatternSyntaxException e ) { 302 throw new PluginException( "Faulty pattern " + pattern ); 303 } 304 305 } 306 307 private static boolean checkContains( final String pagecontent, final String matchPattern ) throws PluginException { 308 if( pagecontent == null || matchPattern == null ) { 309 return false; 310 } 311 312 return doMatch( pagecontent, ".*"+matchPattern+".*" ); 313 } 314 315 private static boolean checkIs( final String content, final String matchPattern ) throws PluginException { 316 if( content == null || matchPattern == null ) { 317 return false; 318 } 319 return doMatch( content, "^" + matchPattern + "$"); 320 } 321 322}