Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

template validation

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Smarty Development
View previous topic :: View next topic  
Author Message
JBJHJM
Smarty Rookie


Joined: 14 Mar 2011
Posts: 12

PostPosted: Fri Jul 08, 2011 9:11 pm    Post subject: template validation Reply with quote

Hi everybody!
I'm implementing Smarty into an own CMS - working great so far - but there's one important thing I have to correct. I'll give you a short example:

-> an user edits a smarty template
-> it is saved into database
-> but it contains an error.

So it would be possible to catch that error somewhere but that's not what I want to do. So my question is does Smarty have some kind of function which can directly tell me if the template (saved in string variable) is valid or if it contains errors, before it is saved and used somewhere?
thx
Back to top
View user's profile Send private message
rodneyrehm
Administrator


Joined: 30 Mar 2007
Posts: 674
Location: Germany, border to Switzerland

PostPosted: Sat Jul 09, 2011 9:05 am    Post subject: Reply with quote

To validate a template just use the eval: resource:

Code:
try {
  $smarty->fetch('eval:'. $templateString);
  // everything is fine…
} catch(SmartyException $e) {
  // error occured
}
Back to top
View user's profile Send private message Visit poster's website
JBJHJM
Smarty Rookie


Joined: 14 Mar 2011
Posts: 12

PostPosted: Sat Jul 09, 2011 10:23 am    Post subject: Reply with quote

Thanks, just wanted to know if I have to use/catch an exception to validate or if there already is a 'template debugging' function.
Back to top
View user's profile Send private message
rodneyrehm
Administrator


Joined: 30 Mar 2007
Posts: 674
Location: Germany, border to Switzerland

PostPosted: Sat Jul 09, 2011 10:38 am    Post subject: Reply with quote

well, you could initialize the compiler directly - but you'd still have to work with a try/catch block.
Back to top
View user's profile Send private message Visit poster's website
Veselin
Smarty n00b


Joined: 20 May 2014
Posts: 4

PostPosted: Tue May 20, 2014 6:59 am    Post subject: Reply with quote

I've the same problem.I need to validate user input for it is a valid smarty template and I'm searching for solution 2 days already with no result...your solution do not work either.
Here is the code:

protected function CheckForInvalidSmartySymbols($syntax = 'asd{&&}') {
//$syntax = '{if $p && $q}1{/if}';
//$syntax = '{if 1 == 1}g {/if}';
//$syntax = '{foreach from=a item=$b}{/foreach}';
$templateFile = '/mnt/home/veselin/vescoTest.tpl';
file_put_contents($templateFile,$syntax);
$template = new NitroTemplate('file:'.$templateFile);
$smarty = $template->Engine;
try {
$smarty->fetch('eval:',$templateFile);
// everything is fine…
} catch(SmartyException $e) {
FB::log('smarty Error');
}

}


With this "eval" fetch I get this in the console:
"E_USER_WARNING: Smarty error: unable to read resource: "eval:" "

Without "eval" in the fetch I get this in the console:
"E_USER_ERROR: Smarty error: [in /mnt/home/veselin/vescoTest.tpl line 1]: syntax error: unrecognized tag: && "

But I can not catch this in the try/catch block

Please not the test case :
$syntax = '{foreach from=a item=$b}{/foreach}';
this breaks the entire application with a error in the console:
"syntax error, unexpected T_ENDFOREACH"

So my question is:
Is there any way to validate is what the user send as post is a valid smarty template before this get into the database,because if it's not later on this will be used and application will fail

Thanks in advance
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Tue May 20, 2014 10:16 am    Post subject: Reply with quote

It looks like that you are using Smarty 2.

Smarty2 does not throw exceptions, so you can't catch errors.
Also Smarty2 does not support the 'eval:' resource.

The examples given have been for Smarty3.
Back to top
View user's profile Send private message
Veselin
Smarty n00b


Joined: 20 May 2014
Posts: 4

PostPosted: Tue May 20, 2014 11:41 am    Post subject: Reply with quote

Ok
Can you give me solution in my case ,please

Thanks
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Tue May 20, 2014 12:11 pm    Post subject: Reply with quote

The workaround could to setup an error handler for E_USER_ERROR.

See http://php.net/manual/en/function.set-error-handler.php

In the error handler throw an exception which can be catched.

See example http://www.php.net/manual/en/class.errorexception.php
Back to top
View user's profile Send private message
Veselin
Smarty n00b


Joined: 20 May 2014
Posts: 4

PostPosted: Tue May 20, 2014 12:41 pm    Post subject: Reply with quote

If I have such a input :
$syntax = '{foreach from=a item=$b}{/foreach}';

the site will be down ,because of the syntax error smarty produce:

"syntax error, unexpected T_ENDFOREACH("

seems like you can not process syntax or fatal errors with error handlers
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Tue May 20, 2014 1:56 pm    Post subject: Reply with quote

That error message is strange. It's not generated by the compiler, but is a fatal PHP error.

Did you get some information where it did occur?
Back to top
View user's profile Send private message
Veselin
Smarty n00b


Joined: 20 May 2014
Posts: 4

PostPosted: Thu May 22, 2014 7:46 am    Post subject: Reply with quote

So,I think I found out a solution.Here is the code:
restore_error_handler();
error_reporting(E_ALL);
ini_set('display_errors', 1);
$syntax = $_POST['smartySyntax'];
CheckForInvalidSmartySymbols($syntax);
echo 'valid';
set_error_handler('MyErrorHandler');
function CheckForInvalidSmartySymbols($syntax) {
$time = time();
$templateFile = 'someTempTemplate.tpl'
file_put_contents($templateFile,$syntax);
$template = new NitroTemplate('file:'.$templateFile);
$smarty = $template->Engine;
$smarty->fetch($templateFile);
@unlink($templateFile);
}
function MyErrorHandler(){

}


This script is called in ajax mode.
The idea is :
Capsulate all error in customer handler that is a empty function (do nothing)
According to php documentation you can NOT customize parsing and fatal errors.
These are the errors smarty->fetch will produce if the tempalte is wrong.
If I receive something different from "valid" string...obviously smarty return parsing error ...the template is not valid...for me this validation should be good enough
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Smarty Development All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP