Jump to content

PHP Smarty stop to work after upgrade to WHMCS 8


massa

Recommended Posts

Hello, 

after I make a upgrade to WHMCS 8 i have problem with templates which use PHP Smarty.

 

[29-Sep-2020 18:41:46 Europe/Sarajevo] [WHMCS Application] ERROR: Error: Call to a member function get_template_vars() on null in /home/xxxx/whmcsmd/templates_c/9696ac8c6e5322b1d8d49c7704c10ff2f1fa60a8_0.file.viewticket.tpl.php:49 Stack trace: #0 /home/xxxx/public_html/ap/vendor/smarty/smarty/libs/sysplugins/smarty_template_resource_base.php(123): content_5f73638ecb4d98_14662502(Object(Smarty_Internal_Template)) #1 /home/xxxx/public_html/ap/vendor/smarty/smarty/libs/sysplugins/smarty_template_compiled.php(114): Smarty_Template_Resource_Base->getRenderedTemplateCode(Object(Smarty_Internal_Template)) #2 /home/xxxx/public_html/ap/vendor/smarty/smarty/libs/sysplugins/smarty_internal_template.php(216): Smarty_Template_Compiled->render(Object(Smarty_Internal_Template)) #3 /home/xxxx/public_html/ap/vendor/smarty/smarty/libs/sysplugins/smarty_internal_templatebase.php(232): Smarty_Internal_Template->render(false, 0) #4 /home/xxxx/public_html/ap/vendor/smarty/smarty/libs/sysplugins/smarty_internal_templatebase.php(116): Smarty_Internal_TemplateBase->_execute(Object(Smarty_Internal_Template), NULL, NULL, NULL, 0) #5 /home/xxxx/public_html/ap/vendor/whmcs/whmcs-foundation/lib/Smarty.php(0): Smarty_Internal_TemplateBase->fetch('tpl/v...', NULL, NULL, NULL, false, true, false) #6 /home/xxxx/public_html/ap/vendor/whmcs/whmcs-foundation/lib/Admin.php(0): WHMCS\Smarty->fetch('tpl/v...') #7 /home/xxxx/public_html/ap/vendor/whmcs/whmcs-foundation/lib/Admin.php(0): WHMCS\Admin->output() #8 /home/xxxx/public_html/ap/chadm/supporttickets.php(2665): WHMCS\Admin->display() #9 {main} {"exception":"[object] (Error(code: 0): Call to a member function get_template_vars() on null at /home/xxxx/whmcsmd/templates_c/9696ac8c6e5322b1d8d49c7704c10ff2f1fa60a8_0.file.viewticket.tpl.php:49)"} []

 

Link to comment
Share on other sites

20 minutes ago, massa said:

So, how do I enable  PHP smarty to work as in WHMCS7?

do you mean Smarty, or are you trying to execute code within {php} tags ?

if it's Smarty itself, then O suspect your issue is more likely to be with the Smarty Security Policy than any changes to Smarty.

if it helps, I added the code below to my v8 dev configuration file and that at least allows me to access some values within Smarty as per v7.

$smarty_security_policy = array(
    'system' => array(
        'enabled_special_smarty_vars' => array(
            'cookies','session','server',
            ),
        ),
    ); 

if you're using {php] tags, then you'd probably need to pos tan example of the code - not that anyone should be using {php} tags still.

Link to comment
Share on other sites

This is my PHP code:

 

{php}
///////////////////////////////////////////////////////////////////////////
$ticketid = $template->get_template_vars('ticketid');
$subject = $template->getTemplateVars('subject');
$lastreply = $template->get_template_vars('lastreply');
$tidc = $template->get_template_vars('tid');

$res4 = WHMCS\Database\Capsule::table('tblticketowners')
			->select(WHMCS\Database\Capsule::raw('adminid, onhold'))
			->where('tid', '=', $ticketid)
			->first();
										
$owner = $res4->adminid;
$onhold = $res4->onhold;
$adminid = WHMCS\Session::get("adminid");

$res4 = WHMCS\Database\Capsule::table('tbladmins')
			->select(WHMCS\Database\Capsule::raw('firstname, lastname'))
			->where('id', '=', $owner)
			->first();

$ownerusername = $res4->firstname.' '.$res4->lastname;
if( count( $res4 ) == 0 ) 
	echo '<h2>#'.$tidc.' - '.$subject.' (Nedodjeljen)</h2>';
else
	echo '<h2>#'.$tidc.' - '.$subject.' (Preuzeo: '.$ownerusername.')</h2>';

echo '<div class="ticketlastreply">Last Reply: '.$lastreply.'</div>';


$res4 = WHMCS\Database\Capsule::table('tbltickets')
			->select(WHMCS\Database\Capsule::raw('status'))
			->where('id', '=', $ticketid)
			->first();
			
$tstatus = $res4->status;

echo "<script type='text/javascript'>
		function SubmitForm( action, tid, aid )
		{
			if( action == 'preuzmi' )
			{
				$.ajax({
					url: 'supportticketsupdateowner.php?action=checkassign&tid='+tid+'&aid='+aid,
					type: 'post',
					dataType: 'json',
					success: function(data) {
						if( data != '' )
						{
							var s = confirm( 'Ticket je već preuzet od strane administratora '+data+'! Ukoliko i dalje želite preuzeti tiket kliknite na OK, za odustati kliknite CANCEL.' );
							if( s == true )
							{
								var messagetext = document.getElementById('replymessage').value;
								var ownerid = document.getElementById('adminowner').value;
								window.location = 'supportticketsupdateowner.php?action=' + action + '&tid=' + tid + '&adminowner=' + ownerid + '&aid=' + aid + '&replymsg=' + messagetext;
							}
						}
						else
						{
							var messagetext = document.getElementById('replymessage').value;
							var ownerid = document.getElementById('adminowner').value;
							window.location = 'supportticketsupdateowner.php?action=' + action + '&tid=' + tid + '&adminowner=' + ownerid + '&aid=' + aid + '&replymsg=' + messagetext;
						}
					}
				});
			}
			else
			{
				var messagetext = document.getElementById('replymessage').value;
				var ownerid = document.getElementById('adminowner').value;
				window.location = 'supportticketsupdateowner.php?action=' + action + '&tid=' + tid + '&adminowner=' + ownerid + '&aid=' + aid + '&replymsg=' + messagetext;
			}
		}
		function SubmitForm2( action, tid, aid )
		{
			if( action == 'preuzmi' )
			{
				$.ajax({
					url: 'supportticketsupdateowner.php?action=checkassign&tid='+tid+'&aid='+aid,
					type: 'post',
					dataType: 'json',
					success: function(data) {
						if( data != '' )
						{
							var s = confirm( 'Ticket je već preuzet od strane administratora '+data+'! Ukoliko i dalje želite preuzeti tiket kliknite na OK, za odustati kliknite CANCEL.' );
							if( s == true )
							{
								var messagetext = document.getElementById('replymessage').value;
								var ownerid = document.getElementById('adminowner2').value;
								window.location = 'supportticketsupdateowner.php?action=' + action + '&tid=' + tid + '&adminowner=' + ownerid + '&aid=' + aid + '&replymsg=' + messagetext;
							}
						}
						else
						{
							var messagetext = document.getElementById('replymessage').value;
							var ownerid = document.getElementById('adminowner2').value;
							window.location = 'supportticketsupdateowner.php?action=' + action + '&tid=' + tid + '&adminowner=' + ownerid + '&aid=' + aid + '&replymsg=' + messagetext;
						}
					}
				});
			}
			else
			{
				var messagetext = document.getElementById('replymessage').value;
				var ownerid = document.getElementById('adminowner2').value;
				window.location = 'supportticketsupdateowner.php?action=' + action + '&tid=' + tid + '&adminowner=' + ownerid + '&aid=' + aid + '&replymsg=' + messagetext;
			}
		}
		</script>";


if( $owner == $adminid )
{
	if( $tstatus == "Closed" )
    {
        $onclickstring = "SubmitForm( 'otvori', ".$ticketid.", ".$adminid." )";
        echo '<input type="button" name="otvori" id="otvori" value="Otvori" onclick="'.$onclickstring.'" class="btn btn-small btn-primary"/>&nbsp;&nbsp;';
    }
    else
    {
    	$onclickstring = "SubmitForm( 'zatvori', ".$ticketid.", ".$adminid." )";
        echo '<input type="button" name="zatvori" id="zatvori" value="Zatvori" onclick="'.$onclickstring.'" class="btn btn-small btn-primary"/>&nbsp;&nbsp;';
    }
    
    if( $onhold == 1 )
    {
    	$onclickstring = "SubmitForm( 'otpusti', ".$ticketid.", ".$adminid." )";
    	echo '<input type="button" name="onhold" id="onhold" value="Otpusti" onclick="'.$onclickstring.'" class="btn btn-small btn-primary"/>&nbsp;&nbsp;';
    }
    else
    {
    	$onclickstring = "SubmitForm( 'zadrzi', ".$ticketid.", ".$adminid." )";
    	echo '<input type="button" name="onhold" id="onhold" value="Zadr&#382;i" onclick="'.$onclickstring.'" class="btn btn-small btn-primary"/>&nbsp;&nbsp;';
    }
}
else
{
	$onclickstring = "SubmitForm( 'preuzmi', ".$ticketid.", ".$adminid." )";
	echo '<input type="button" name="preuzmi" id="preuzmi" value="Preuzmi" onclick="'.$onclickstring.'" class="btn btn-small btn-primary"/>&nbsp;&nbsp;';
}


echo '<select name="adminowner" id="adminowner" style="width:250px; height:34px; top:2px; position:relative;">';

$res4 = WHMCS\Database\Capsule::table('tbladmins')
			->select(WHMCS\Database\Capsule::raw('id, firstname, lastname'))
			->where('username', '<>', 'apiuser')
			->get();
			
foreach( $res4 as $adminobj )
{
	$ownerid = $adminobj->id;
	$firstname = $adminobj->firstname;
	$lastname = $adminobj->lastname;
	
	if( $adminid == $ownerid )
        echo '<option selected value="'.$ownerid.'">'.$firstname.' '.$lastname.'</option>';
    else
        echo '<option value="'.$ownerid.'">'.$firstname.' '.$lastname.'</option>';
}
echo '</select>&nbsp;&nbsp;';

$onclickstring = "SubmitForm( 'dodijeli', ".$ticketid." )";
echo '<input type="button" name="dodijeli" id="dodijeli" value="Dodijeli" onclick="'.$onclickstring.'" class="btn btn-small btn-primary"/> ';
///////////////////////////////////////////////////////////////////////////
{/php}

and also:

 {if $smarty.session.cart.promo}
       <span>Promotional Code: {$smarty.session.cart.promo}</span>
       <span>Discount: {$currency.prefix}{$carttotals.rawdiscount}{$currency.suffix}</span>
       <span class="amt">{$currency.prefix}{$carttotals.rawtotal}{$currency.suffix}</span>
       <span>Total Due Today with promo <b>{$smarty.session.cart.promo}</b></span>
   {/if}

 

Edited by massa
Link to comment
Share on other sites

13 minutes ago, massa said:

and also:

i'll tackle the easy one first... this should work in v8.0.1 assuming the Smarty security policy is in place...

oKianxY.png

i'm pretty sure $smarty.session.cart didn't exist in v8.0.0 (from my limited testing) - but the above screenshot is from v8.0.1

1 hour ago, massa said:

This is my PHP code:

my first thought seeing the code - this really needs to be an action hook. 😲

even if that's limited to just pulling the variables from the template, running the SQL queries and returning the results back to the template as Smarty variables for use in the JS - that's a step forward if nothing else and would stop the above issue occurring.

with the queries moved to a hook, you could do replace the rest of the PHP code with pure Smarty without ever using {php}.

with regards to the actual error - i'm seeing the same thing... but I don't know if this is WHMCS removing the feature from the update, if it's a bug or whether there is an updated technique for accessing Smarty variables in {php}.

Link to comment
Share on other sites

  • WHMCS Support Manager

Hi all,

The exposure of the template object via the legacy variable $template available within the deprecated PHP blocks context is only available if template_object is itemized in the enabled_special_smarty_vars array.

Details on this array are documented here: https://docs.whmcs.com/Smarty_Security_Policy#Supported_Policy_Settings_and_Values

 

If you expect to access the Smarty object within our PHP block, you will need to update your security policy. If you do not do this, code $template inside a PHP block will be undefined and error messages will state it is NULL

Here is an example of a working policy and template snippet that demonstrates this:

// minimum policy inside configuration.php
$smarty_security_policy = array(
    'system' => array(
        'disabled_tags' => [],
        'enabled_special_smarty_vars' => array(
            'template_object',
        ),
    ),
);

 

<!-- in the page's template or header.tpl -->
{php}
    echo 'PHP Block: ';
    echo $template->tpl_vars['template'];
    echo "<br/>";
{/php}
Template Variable: {$template}<br/>

Use of $this inside a PHP block is not an applicable work around.

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated