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
Marc A Offline
Junior Member
**
Posts: 8
Joined: Feb 2015
Post: #51
RE: Template Conditionals
I'm guessing we can't use str_replace with arrays for search and replace variables...?
03-18-2015 09:59 AM
Find all posts by this user Quote this message in a reply
ZiNgA BuRgA Offline
Fag
*******
Posts: 3,357
Joined: Jan 2008
Post: #52
RE: Template Conditionals
Well, have you tried?

My Blog
03-18-2015 05:03 PM
Find all posts by this user Quote this message in a reply
Marc A Offline
Junior Member
**
Posts: 8
Joined: Feb 2015
Post: #53
RE: Template Conditionals
Neither of these worked:

Code:
<func str_replace>array("PlayStation 4", "PlayStation 3", "Xbox One", "Xbox 360"), array("PS4", "PS3", "X1", "360"), $user['fid7']</func>

Code:
<?=str_replace(array("PlayStation 4", "PlayStation 3", "Xbox One", "Xbox 360"), array("PS4", "PS3", "X1", "360"), $user['fid7'])?>


Actually, I can't get any str_replace function to work.
(This post was last modified: 03-19-2015 06:27 AM by Marc A.)
03-19-2015 06:00 AM
Find all posts by this user Quote this message in a reply
ZiNgA BuRgA Offline
Fag
*******
Posts: 3,357
Joined: Jan 2008
Post: #54
RE: Template Conditionals
Your first example won't work, because the <func> shortcut only works with single argument functions.
However, your second example works fine for me.

Code:
<?=str_replace(array("PlayStation 4", "PlayStation 3", "Xbox One", "Xbox 360"), array("PS4", "PS3", "X1", "360"), "PlayStation 4")?>

Above prints "PS4"


My Blog
(This post was last modified: 03-20-2015 01:15 PM by ZiNgA BuRgA.)
03-20-2015 01:15 PM
Find all posts by this user Quote this message in a reply
Marc A Offline
Junior Member
**
Posts: 8
Joined: Feb 2015
Post: #55
RE: Template Conditionals
Strange. I have the above in my memberlist_user template and it outputs nothing. Any ideas?
03-20-2015 11:11 PM
Find all posts by this user Quote this message in a reply
ZiNgA BuRgA Offline
Fag
*******
Posts: 3,357
Joined: Jan 2008
Post: #56
RE: Template Conditionals
Install a completely fresh version of MyBB and see if you can reproduce the problem.  If so, post step by step instructions on how to make it fail.

My Blog
03-21-2015 09:43 AM
Find all posts by this user Quote this message in a reply
Marc A Offline
Junior Member
**
Posts: 8
Joined: Feb 2015
Post: #57
RE: Template Conditionals
(03-21-2015 09:43 AM)ZiNgA BuRgA Wrote:  Install a completely fresh version of MyBB and see if you can reproduce the problem.  If so, post step by step instructions on how to make it fail.
If I have time, I'll try this.

However, I just switched from Template Conditionals to PHP in Templates and Template Conditionals and the <?=str_replace line now works.
03-21-2015 11:47 PM
Find all posts by this user Quote this message in a reply
Sama34 Offline
Senior Member
****
Posts: 490
Joined: May 2011
Post: #58
RE: Template Conditionals
You were probably just missing the white-list txt file.

Support PM's will be ignored. Yipi
Plugins: Announcement Bars - Custom Reputation - Mark PM As Unread
03-22-2015 05:59 AM
Visit this user's website Find all posts by this user Quote this message in a reply
Marc A Offline
Junior Member
**
Posts: 8
Joined: Feb 2015
Post: #59
RE: Template Conditionals
(03-22-2015 05:59 AM)Sama34 Wrote:  You were probably just missing the white-list txt file.

Now I feel like an idiot.
03-22-2015 11:01 AM
Find all posts by this user Quote this message in a reply
tempo Offline
Junior Member
**
Posts: 30
Joined: Aug 2012
Post: #60
RE: Template Conditionals
hi,

i want to use this code into a template

PHP Code:
$q = $db->simple_select('tablename', 'column');
echo $db->fetch_field($q, 'column'); 


if i use "PHP in Templates / Complex Templates" everything is ok!

if i use "Template Conditionals" nothing is happen.

my question is: is it possible to make this code work for me using "Template Conditionals"?

07-08-2015 05:01 AM
Find all posts by this user Quote this message in a reply


Forum Jump: