raintpl3 icon indicating copy to clipboard operation
raintpl3 copied to clipboard

'Ternary' regex in parser.php wrong

Open lllopo opened this issue 11 years ago • 9 comments

Hi, the second ternary operator parser regex : {(.[^{?]?)?(.?):(.*?)} is wrong (parser.php, line 59). The problem is, that is also matches PHP code inserted by plugins that parsed the template with beforeParse. Example :

{$data.User.something}<a href="<?php echo static::$conf['base_url']; ?>users/{$data.User.otherthing}

The code above gets matched in total although it is not a ternary operator, because it has a '{' for a start a '}' for end and it has a '?' and ':' in it.

I believe this is a fundamental problem that need fixing, since a simple string like this :

{$startmeup} How are you ? Please, choose : one or two. {$endmenow}

will also get matched.

lllopo avatar Sep 27 '13 09:09 lllopo

Maybe this : {(.[^{}?]?)?(.[^}?]?):(.*?)} would work better ... not 100% sure though.

lllopo avatar Sep 27 '13 10:09 lllopo

Mhm, you can try it yourself and make a pull request if it would work :smile:

keskad avatar Sep 27 '13 10:09 keskad

I already tried it and it looks like working. I'll be using it in that form at least ... but I'm not sure it covers all the cases properly. Don't know Rain.tpl in details, don't have the time to test deeply either.

lllopo avatar Sep 27 '13 11:09 lllopo

Okey, so, you can also paste here some lines of changed code that we will be able to merge.

keskad avatar Sep 27 '13 11:09 keskad

Parser.php, line 59 should be :

'ternary' => array('({.[^{?]??.?:.?})', '/{(.[^{}?]?)?(.[^}?]?):(.?)}/'),

I think.

lllopo avatar Sep 27 '13 11:09 lllopo

Thank you.

keskad avatar Sep 27 '13 11:09 keskad

That regexp will not parse: {($var>1)?"{$var}s":""}

But, {$data.User.something} is not matched with {(.[^{?]*?)\?(.*?):(.*?)}. What is full source?

kargnas avatar Sep 27 '13 11:09 kargnas

@kargnas Updated the original issue post. Some markup was breaking it. Now full thing should be visible.

lllopo avatar Sep 27 '13 13:09 lllopo

Update : Parser.php, line 59 should probably be :

'ternary' => array('({.[^{}?]??.[^}?]?:.?})', '/{(.[^{}?]?)?(.[^}?]?):(.?)}/'),

Forgot to fix the other regex.

lllopo avatar Sep 30 '13 13:09 lllopo