Template Conditionals
Author Message
This is essentially a more restrictive version of my PHP in Templates plugin.  The restrictions aim to make this a "safe" plugin to use, that is, doesn't allow arbitrary PHP execution, but still gives the benefits of template conditionals.

You may notice that this still uses the "phptpl" name, and thus, is incompatible with the PHP in Templates plugin.  Both plugins are very similar though.  The differences between this and the other plugin are:
  • Admins cannot enter PHP code using <?php ?> tags
  • Conditionals in <if> and <elseif> tags are checked to ensure that they are "safe" (see below)
  • file_get_contents function has been removed from the allowable <func ...>...</func> shortcuts
  • There's a new <?=...?> tag to print out the result of a "safe" PHP expression; although this is a tag, only PHP expressions may exist inside (do not terminate expressions with a semicolon), so you cannot nest other tags inside this
    Example (prints 123654321):

    HTML Code
    123<?=substr("987654321", 3)?>

  • There's also a new <setvar name>...</setvar> tag which can set variables; for safety reasons, these are actually stored in a $tplvars array.  Examples:
    (just prints some text)

    HTML Code
    <setvar uselesstext>"some text"</setvar>
    {$tplvars['uselesstext']}

    (prints out the username of the user with UID of 2)

    HTML Code
    <setvar user2>get_user(2)</setvar>
    <func htmlspecialchars_uni>{$tplvars['user2']['username']}</func>


v1.0-1.3 of this plugin is based off v1.7 of PHP in Templates.
As of v1.8, PHP 5.3 or later is required.
This plugin can be used with the Admin Security plugin.

"Safe expressions"
This plugin implements "safe expression" checking; essentially, this does impose a bit of a performance hit, but, on the other hand, tries to ensure no "bad PHP" gets executed.
For more information on what I consider to be a "safe expression", see my blog post here.
For the purposes of this plugin, all valid PHP expressions are allowed, as long as they don't infringe on any of the following conditions:
  • no assignment/modification operators (=, +=, |=, ++ etc) allowed
  • no statements such as include, exit, eval etc are allowed
  • no special constants such as PHP_OS, PHP_LIBDIR etc are allowed
  • backtick (`) operator not allowed
  • heredoc type strings not allowed (takes too much effort to handle) - use double quoted strings instead
  • double quoted strings may not contain the "{" character (takes too much effort to handle) - use string concatenation instead
  • array and object typecasting not allowed
  • no variable functions or method calls allowed
  • single line comments (//, #) not allowed
  • only some functions are allowed - see inc/plugins/phptpl_allowed_funcs.txt for a list of allowed functions
(This post was last modified: 06-26-2023 10:42 PM by ZiNgA BuRgA.)
Find all posts by this user
Quote this message in a reply
Download: tplcond-1.9.7z (6.35 KB)
Plugin Version: 1.9
Last Updated: 06-26-2023, 10:42 PM

Downloads: 5,373
MyBB Compatibility: 1.2.x, 1.4.x, 1.6.x, 1.8.x
Plugin License: GPLv3
Uploader: ZiNgA BuRgA
ZiNgA BuRgA Offline
Fag
*******
Posts: 3,357
Joined: Jan 2008
Post: #31
RE: Template Conditionals
str_replace isn't a valid function allowable inside the <func> tag
(03-14-2010 10:12 AM)ZiNgA BuRgA Wrote:  The functions available, with the shortcut, are:
htmlspecialchars, htmlspecialchars_uni, intval, file_get_contents, floatval, urlencode, rawurlencode, addslashes, stripslashes, trim, crc32, ltrim, rtrim, chop, md5, nl2br, strrev, strtoupper, strtolower, my_strtoupper, my_strtolower, alt_trow, get_friendly_size, filesize, strlen, my_strlen, my_wordwrap, random_str, unicode_chr, bin2hex, str_rot13, str_shuffle, strip_tags, ucfirst, ucwords, basename, dirname, unhtmlentities

Try

Code:
<?=str_replace("\n", ', ', $post['fid6'])?>


My Blog
09-03-2012 09:43 AM
Find all posts by this user Quote this message in a reply
Victor Offline
Junior Member
**
Posts: 6
Joined: Aug 2012
Post: #32
RE: Template Conditionals
Simple 2 words: Thank you.
09-04-2012 05:39 AM
Find all posts by this user Quote this message in a reply
ZiNgA BuRgA Offline
Fag
*******
Posts: 3,357
Joined: Jan 2008
Post: #33
RE: Template Conditionals
Updated to v1.7 - fixes a possible function call exploit.

My Blog
11-18-2012 08:17 PM
Find all posts by this user Quote this message in a reply
ahstanford Offline
Junior Member
**
Posts: 5
Joined: Apr 2010
Post: #34
RE: Template Conditionals
How would I get the following to work?

Code:
<if {$mybb->user['avatar']} != '' then> 
          <div class="avatar" style="background-image:url('{$mybb->user['avatar']}');"></div> 
</if>

12-12-2012 06:04 AM
Find all posts by this user Quote this message in a reply
RateU Offline
Administrator
*******
Posts: 2,350
Joined: Mar 2010
Post: #35
RE: Template Conditionals
Something like this:

Code:
<if $mybb->user['avatar'] != '' then><div class="avatar" style="background-image:url('{$mybb->user['avatar']}');"></div></if>


12-12-2012 06:08 AM
Find all posts by this user Quote this message in a reply
ahstanford Offline
Junior Member
**
Posts: 5
Joined: Apr 2010
Post: #36
RE: Template Conditionals
(12-12-2012 06:08 AM)RateU Wrote:  Something like this:

Code:
<if $mybb->user['avatar'] != '' then><div class="avatar" style="background-image:url('{$mybb->user['avatar']}');"></div></if>


Ah, thanks. Just had to drop those curly brackets.
12-12-2012 07:02 AM
Find all posts by this user Quote this message in a reply
charafweb Offline
Junior Member
**
Posts: 12
Joined: Jan 2013
Post: #37
RE: Template Conditionals
Hi,
Is this plugin doesn't slow down the board?
Thanks
01-31-2013 03:34 AM
Find all posts by this user Quote this message in a reply
ZiNgA BuRgA Offline
Fag
*******
Posts: 3,357
Joined: Jan 2008
Post: #38
RE: Template Conditionals
What?
Define "slow down the board".

My Blog
01-31-2013 09:25 AM
Find all posts by this user Quote this message in a reply
charafweb Offline
Junior Member
**
Posts: 12
Joined: Jan 2013
Post: #39
RE: Template Conditionals
(01-31-2013 09:25 AM)ZiNgA BuRgA Wrote:  What?
Define "slow down the board".

what I mean: is this plugin does not affect (negatively) site performance (by adding a lot of work to CPU) when used many times in templates? (I hope I'm clear now)

sorry for my bad english.
01-31-2013 07:18 PM
Find all posts by this user Quote this message in a reply
ZiNgA BuRgA Offline
Fag
*******
Posts: 3,357
Joined: Jan 2008
Post: #40
RE: Template Conditionals
It depends on what you mean by "a lot of work" (PHP script execution time? additional CPU cycles? memory usage?), as well as what you mean by "used many times" (ten instances? a hundred instances? what templates? what code?).

Pretty much all plugins will have a negative effect on site performance.  It's up to you whether you think the trade off is worth it.

My Blog
02-01-2013 09:29 AM
Find all posts by this user Quote this message in a reply


Forum Jump: