MyBB Community Forums

Full Version: PHP in Templates and Template Conditionals
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
(2009-05-24, 10:03 AM)faviouz Wrote: [ -> ]From what I read, this allows me to use other stuff like {$header} or {$forums} and so on. Is it?
Yes, this will keep all the functionality of MyBB templates.

(2009-05-24, 10:03 AM)faviouz Wrote: [ -> ]And also run PHP codes in templates right?
Yes.

(2009-05-24, 10:03 AM)faviouz Wrote: [ -> ]But where do I upload this? /inc/plugins/? And then, I upload it.. and.. it's done? Am I ready to use it or do I have to do something else?
After uploading it, you need to activate it in the AdminCP. Then just edit the templates as you wish.
Thanks Yumi this is a good plugin. Wink
It would be great, and for security, if you could use just one function, to include short snippets (or whatever they are called) form a certain folder.

So if you want to have this script inside:

if($var1=$var2)
     {
     blub
     }
else
     {
      bläh
     }

you can't write it in your Template, but in a certain folder, and include it at a position of your coice via special tag (like {%filename})

So you have to have access to the webspace to include this php-snippets, and not just access to the ACP
Interesting idea, but does make things less convenient (and possibly slower). Probably easier just to disable other admins access to the templates section, unless you feel they need to access the templates though.

I'll consider it though - thanks for the suggestion.
Great plug in, very useful. It helped me a lot with some modifications I needed to make to a mybb forum.
Hey, I'm sorry if I'm being stupid here, but.. How do I add it? I'm not really that good with this sort of thing.

- Pingo
does this work with 1.4.x version
(2009-06-12, 09:43 PM)Pingo Wrote: [ -> ]Hey, I'm sorry if I'm being stupid here, but.. How do I add it? I'm not really that good with this sort of thing.
Add what?
To install the plugin, you do it like other MyBB plugins - upload the PHP file to inc/plugins/ and activate from the AdminCP.

(2009-06-13, 04:28 AM)ancraz Wrote: [ -> ]does this work with 1.4.x version
Yes.
Thanks for this!
Hey everyone!

I am having trouble using php and conditionals in my templates. It used to work like a charm, but for some reason it stopped working. I am running MyBB 1.2.14 on www.maroonlife.com.

I am trying to add the following code to my postbit template:
<!--Testing Share--->
<tr align="right" valign="bottom">
<span class="smalltext">
<div align="right" id="sharing">
<img src="http://static.ak.fbcdn.net/images/share/facebook_share_icon.gif">&nbsp;<a href="http://www.facebook.com/share.php?u=<func urlencode>{$mybb->settings['bburl']}/showthread.php?tid={$tid}</func>" target="_blank">Share on Facebook</a>
&nbsp;
<img border=0 src="http://planetzope.org/static/icon_delicious.gif" alt="" width="16" height="16">&nbsp;<a href="http://del.icio.us/post?v=2&url=<func urlencode>{$mybb->settings['bburl']}/showthread.php?tid={$tid}</func>&title=<func urlencode>{$post['subject']}</func>&title="Post to del.icio.us">Post to del.icio.us</a> 
&nbsp;
<img src="http://digg.com/img/badges/16x16-digg-guy.png" width="16" height="16" alt="Digg!" />&nbsp;<a href="http://digg.com/submit?url=<func urlencode>{$mybb->settings['bburl']}/showthread.php?tid={$tid}</func>&title=<func urlencode>{$post['subject']}</func>&bodytext=<func urlencode>{$post['message']}</func>"  TARGET="_blank">Digg This!</a>
&nbsp;
<img src="http://x.myspace.com/images/myspace_logo_16.gif"  alt="Post to MySpace!" />&nbsp;<a href="http://www.myspace.com/Modules/PostTo/Pages/?t=<func urlencode>{$post['subject']}</func>&c=<func urlencode>{$post['message']}</func>&u=<func urlencode>{$mybb->settings['bburl']}/showthread.php?tid={$tid}</func>" TARGET="_blank">Post to MySpace</a>
&nbsp;
<img src="{$mybb->settings['bburl']}/{$theme['imgdir']}/send.gif" alt="" />&nbsp;<a href="{$mybb->settings['bburl']}/sendthread.php?tid={$tid}">Email This!</a>

</div></span></tr>
<!---End Testing Share-->

Here is a copy of my postbit template:
<tr>
<td class="{$altbg}" width="15%" valign="top" style="white-space: nowrap; text-align: center;"><a name="pid{$post['pid']}" id="pid{$post['pid']}"></a>
{$post['user_details']}
</td>
<td class="{$altbg}" width="85%" valign="top">
<table width="100%">
<tr><td>{$post['posturl']}{$post['icon']}<span class="smalltext"><strong> {$post['subject']}</strong></span>
<br />
<div id="pid_{$post['pid']}">
<p>
{$post['message']}
</p>
</div>
{$post['attachments']}
{$post['signature']}
<div style="text-align: right; vertical-align: bottom;">
{$post['editedmsg']}
{$post['iplogged']}
</div>
<!--Testing Share--->
<tr align="right" valign="bottom">
<span class="smalltext">
<div align="right" id="sharing">
<img src="http://static.ak.fbcdn.net/images/share/facebook_share_icon.gif">&nbsp;<a href="http://www.facebook.com/share.php?u=<func urlencode>{$mybb->settings['bburl']}/showthread.php?tid={$tid}</func>" target="_blank">Share on Facebook</a>
&nbsp;
<img border=0 src="http://planetzope.org/static/icon_delicious.gif" alt="" width="16" height="16">&nbsp;<a href="http://del.icio.us/post?v=2&url=<func urlencode>{$mybb->settings['bburl']}/showthread.php?tid={$tid}</func>&title=<func urlencode>{$post['subject']}</func>&title="Post to del.icio.us">Post to del.icio.us</a> 
&nbsp;
<img src="http://digg.com/img/badges/16x16-digg-guy.png" width="16" height="16" alt="Digg!" />&nbsp;<a href="http://digg.com/submit?url=<func urlencode>{$mybb->settings['bburl']}/showthread.php?tid={$tid}</func>&title=<func urlencode>{$post['subject']}</func>&bodytext=<func urlencode>{$post['message']}</func>"  TARGET="_blank">Digg This!</a>
&nbsp;
<img src="http://x.myspace.com/images/myspace_logo_16.gif"  alt="Post to MySpace!" />&nbsp;<a href="http://www.myspace.com/Modules/PostTo/Pages/?t=<func urlencode>{$post['subject']}</func>&c=<func urlencode>{$post['message']}</func>&u=<func urlencode>{$mybb->settings['bburl']}/showthread.php?tid={$tid}</func>" TARGET="_blank">Post to MySpace</a>
&nbsp;
<img src="{$mybb->settings['bburl']}/{$theme['imgdir']}/send.gif" alt="" />&nbsp;<a href="{$mybb->settings['bburl']}/sendthread.php?tid={$tid}">Email This!</a>

</div></span></tr>
<!---End Testing Share-->
</td></tr>
</table>
</td>
</tr>
<tr>
<td class="{$altbg}" height="18" style="white-space: nowrap; text-align: center;"><span class="smalltext">{$post['postdate']} {$post['posttime']}</span></td>
<td class="{$altbg}" width="100%" valign="middle" height="18">
	<table width="100%" border="0" cellpadding="0" cellspacing="0">
	<tr valign="bottom">
		<td align="left" ><span class="smalltext">{$post['button_email']}{$post['button_pm']}{$post['button_www']}{$post['button_find']}</span></td>
		<td align="right"><span class="smalltext">{$post['button_edit']}{$post['button_quickdelete']}{$post['button_quote']}{$post['button_multiquote']}{$post['button_report']}</span></td>
	</tr>
	</table>
</td>
</tr>
{$seperator}


Here is a picture of one of the posts.
[attachment=14490]

After deactivating and reactivating multiple times, I tried to use the simple example:
<?php echo "Hi from PHP"; ?>

No luck though, it did not work. So I deactivated it and redownloaded it and reactivated it, still no luck.

Here is the code for the plugin:
<?php

/*
 *  Known issue: in PHP evaluation, "?>" may not match properly if used in strings
 */

if(!defined('IN_MYBB'))
	die('This file cannot be accessed directly.');

if(!defined('IN_ADMINCP'))
{
	// gain control of $templates object
	
	eval('
		class phptpl_templates extends '.get_class($GLOBALS['templates']).'
		{
			function phptpl_templates(&$oldtpl)
			{
				$vars = get_object_vars($oldtpl);
				foreach($vars as $var => $val)
					$this->$var = $val;
				
				$this->def_htmlcomments = $GLOBALS[\'mybb\']->settings[\'tplhtmlcomments\'];
				$this->def_htmlcomments = (($this->def_htmlcomments == \'yes\' || $this->def_htmlcomments == 1) ? 1:0);
			}
			function get($title, $eslashes=1, $htmlcomments=1)
			{
				if($eslashes && $this->def_htmlcomments == $htmlcomments)
				{
					if(!isset($this->parsed_cache[$title]))
					{
						$this->parsed_cache[$title] = parent::get($title, $eslashes, $htmlcomments);
						// parse the template
						phptpl_parsetpl($this->parsed_cache[$title]);
					}
					
					return $this->parsed_cache[$title];
				}
				else
					return parent::get($title, $eslashes, $htmlcomments);
			}
		}
	');
	$GLOBALS['templates'] = new phptpl_templates($GLOBALS['templates']);
}

function phptpl_info()
{
	return array(
		'name'			=> 'PHP and Template Conditionals',
		'description'	=> 'Allows you to use conditionals and PHP code in templates.',
		'website'		=> 'http://mybbhacks.zingaburga.com/',
		'author'		=> 'ZiNgA BuRgA',
		'authorsite'	=> 'http://zingaburga.com/',
		'version'		=> '1.3',
		'compatibility'	=> '1*',
		'guid'			=> ''
	);
}

function phptpl_parsetpl(&$ourtpl)
{
	$ourtpl = preg_replace(array(
		'#\<\?.+?(\?\>)#se', // '#\<\?.*?(\?\>|$)#se',
		'#\<if (.*?) then\>#sie',
		'#\<elseif (.*?) then\>#sie',
		'#\<else( /)?\>#i',
		'#\</if\>#i',
		'#\<func (htmlspecialchars|htmlspecialchars_uni|intval|file_get_contents|floatval|urlencode|rawurlencode|addslashes|stripslashes|trim|crc32|ltrim|rtrim|md5|nl2br|sha1|strrev|strtoupper|strtolower|my_strtoupper|my_strtolower|alt_trow|get_friendly_size|filesize|strlen|my_strlen|my_wordwrap|random_str|unicode_chr)\>#i',
		'#\</func\>#i'
	), array(
		'phptpl_evalphp(\'$0\', \'$1\')',
		'\'".phptpl_iif(\'.phptpl_unescape_string(\'$1\').\',"\'',
		'\'",\'.phptpl_unescape_string(\'$1\').\',"\'',
		'","',
		'")."',
		'".$1("',
		'")."'
	), $ourtpl);
}


// unescapes the slashes added by $templates->get(), plus addslashes() during preg_replace()
function phptpl_unescape_string($str)
{
	return strtr($str, array('\\\\"' => '"', '\\\\' => '\\'));
}

function phptpl_evalphp($str, $end)
{
	return '".eval(\'ob_start(); ?>'
		.strtr(phptpl_unescape_string($str), array('\'' => '\\\'', '\\' => '\\\\'))
		.($end?'':'?>').'<?php return ob_get_clean();\')."';
}

function phptpl_iif($condition, $true)
{
	$args = func_get_args();
	for($i=1, $c=count($args); $i<$c; $i+=2)
		if($args[$i-1]) return $args[$i];
	return (isset($args[$i-1]) ? $args[$i-1] : '');
}
?>
Any help will be greatly appreciated.

Thanks,
jnd52
I fixed it. Sigh. Blush

Apparently, I had to set

Board Settings->
Server and Optimization Options ->
Output template start/end comments?
to yes.

Working like a charm again. Woot!
Pages: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22