You are not logged in.
- Topics: Active | Unanswered
#26 2008-05-16 20:44:11
- damaxxed
- Member

- From: Germany
- Registered: 2008-05-16
- Posts: 353
Re: Hook bug
Isn't it complicated to write an algorithm that checks whether the compiler is in php-mode at the end of a script, regarding the difficulties with closing tags in strings?
Offline
#27 2008-05-16 20:51:23
- Smartys
- Former Developer
- Registered: 2008-04-27
- Posts: 3,116
- Website
Re: Hook bug
What Bekko said.
Moved to FluxBB Discussion, since this is really more of a "how should we deal with certain necessary coding standards" debate. ![]()
Offline
#28 2008-05-16 20:54:59
- damaxxed
- Member

- From: Germany
- Registered: 2008-05-16
- Posts: 353
Re: Hook bug
If you dislike my first idea (forbid if '?>' detected), what about this piece of code?
<?php
file_put_contents( $exported_extension_code_file, $ext_code );
file_put_contents( $exported_extension_code_file2, $ext_code . '<?php' );
if( ! php_check_syntax( $exported_extension_code_file ) || php_check_syntax( $exported_extension_code_file2 ) )
message('Not in PHP-Mode at EOF!');//edit: Not possible
The function is only available up to PHP 5.0.4 and executes the content..
Last edited by damaxxed (2008-05-16 21:00:35)
Offline
#29 2008-05-16 21:06:16
- eliot
- Member
- Registered: 2008-05-09
- Posts: 171
Re: Hook bug
nevermind, you said the same thing in your edit.
Last edited by eliot (2008-05-16 21:06:50)
Offline
#30 2008-05-16 23:24:18
- eliot
- Member
- Registered: 2008-05-09
- Posts: 171
Re: Hook bug
I think this does it
function strip_text($text,$r)
{
$text = ereg_replace($r,"",$text);
return $text;
}
function in_php_mode($hook)
{
$hook = addcslashes($hook,"$");
$hook_temp = strip_text($hook,"(\"([^\\\"]|\\.)*\"|'([^\\']|\\.)*')");
$a = strrpos($hook_temp,"<?php");
if($a=== false || is_null($a))
$a=-1;
$b = strrpos($hook_temp,"<? ");
if($b===false || is_null($b))
$b=-1;
$c=strrpos($hook_temp,"?>") ;
if($c===false || is_null($c))
$c=-1;
if($a == -1 && $b == -1 && $c==-1 )
{
return true;
}
if($a>-1)
{
if($c==-1)
return false;
else
return $a > $c;
}
else if($c>-1)
{
return false;
}
}and you can call
if (!in_php_mode($hook))
{
//bad stuff;
}
else
{
//good stuff;
}I'll test it some more, and I'm sure there's optimizations that could be done, but as a proof of concept, it'll do.
Offline
#31 2008-05-16 23:33:14
- damaxxed
- Member

- From: Germany
- Registered: 2008-05-16
- Posts: 353
Re: Hook bug
doesn't work with here-document-syntax:
var_dump( in_php_mode( 'echo <<<END test ?> asdfsdf
END;' ) );bool(false)
Offline
#32 2008-05-16 23:36:53
- eliot
- Member
- Registered: 2008-05-09
- Posts: 171
Re: Hook bug
doesn't work with here-document-syntax:
var_dump( in_php_mode( 'echo <<<END test ?> asdfsdf END;' ) );bool(false)
Ok, I'll have to brush up on what that is (I'm an asp and c# person by nature
) and I should be able to fix that.
Offline
#33 2008-05-17 03:23:36
- Smartys
- Former Developer
- Registered: 2008-04-27
- Posts: 3,116
- Website
Re: Hook bug
damaxxed: I don't even understand that code ![]()
Offline
#34 2008-05-17 03:33:34
- Smartys
- Former Developer
- Registered: 2008-04-27
- Posts: 3,116
- Website
Re: Hook bug
http://www.php.net/tokenizer
Great news everyone!
Offline
#35 2008-05-17 09:06:05
- Vovochka
- Member
- From: Ukraine
- Registered: 2008-05-10
- Posts: 165
- Website
Re: Hook bug
I use it in me mod ![]()
http://fluxbb.org/forums/topic/292/ext-compress-cache/
If you're going to use token_get_all()
Why don't you also compress hooks code before inserting them in db?
Last edited by Vovochka (2008-05-17 09:06:48)
Offline
#36 2008-05-17 11:10:43
- eliot
- Member
- Registered: 2008-05-09
- Posts: 171
Re: Hook bug
http://www.php.net/tokenizer
Great news everyone!
ok, that's almost like cheating ![]()
This works then:
function in_php_mode($hook)
{
//since we start in php mode, add the open tag to the hook
$mode = 'in';
foreach(@token_get_all("<?php ".$hook) as $c)
{
if(is_array($c))
{
if(token_name($c[0])=="T_OPEN_TAG")
$mode = "in";
if(token_name($c[0])=="T_CLOSE_TAG")
$mode = "out";
}
}
return $mode=='in' ;
}Offline
#37 2008-05-17 11:17:31
- Smartys
- Former Developer
- Registered: 2008-04-27
- Posts: 3,116
- Website
Re: Hook bug
eliot: I was actually going to cheat even more than that, if I can ![]()
I think all I have to do is pop off the last element of token_get_all and make sure it's not T_INLINE_HTML.
Vovochka: Because, as your extension proves, there are minimal performance gains from doing so.
http://en.wikipedia.org/wiki/Optimizati … o_optimize
Offline
#38 2008-05-17 11:24:48
- eliot
- Member
- Registered: 2008-05-09
- Posts: 171
Re: Hook bug
eliot: I was actually going to cheat even more than that, if I can
I think all I have to do is pop off the last element of token_get_all and make sure it's not T_INLINE_HTML.
Yes, even better. Cheater. ![]()
Offline
#39 2008-05-17 14:55:10
- Smartys
- Former Developer
- Registered: 2008-04-27
- Posts: 3,116
- Website
Re: Hook bug
And added in r172
Offline
