Not Solved PHP in Templates and Template Conditionals
#40
Not Solved
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.
   

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!
Reply


Messages In This Thread
RE: PHP in Templates and Template Conditionals - by jnd52 - 2009-06-28, 08:40 AM
RE: Template conditionals - by expat - 2015-01-31, 05:28 AM

Forum Jump:


Users browsing this thread: 93 Guest(s)