PDA

View Full Version : PHP Returning eval



JonnyH
October 15th, 2007, 12:31
I'm basically writing this script which when entered <&#37;CONTENT%> on the HTML get what it needs etc. Here's the code:


$tpl = preg_replace("/<%CONTENT%>/si", $this->c_eval($this->e_array($this->content())), $tpl);

The three functions:


//Get's any content/plugin requested by user
function content()
{
if ($this->service == "0")
{
switch ($_GET['page'])
{

default:
if (!$_GET['page'])
{
header("Location:" . $this->config("default"));
} else
{
if (file_exists("style/" . $this->style . "/content_top.tpl") && file_exists("style/" .
$this->style . "/content_bottom.tpl"))
{
$cont = "<div id=\"contentcontainer\">";
$cont .= file_get_contents("style/" . $this->style . "/content_top.tpl");
} else
{
#echo "Hello";
$cont = array();
return $cont;
}
$page = strtolower($_GET['page']);
if (!file_exists("plugins/" . $page . "/main.php"))
{
$this->name("Doesn't Exist");
$cont .= "Sorry that page currently doesn't exist.";
} else
{
$cont .= file_get_contents("plugins/" . $page . "/main.php");
}
$cont .= file_get_contents("style/" . $this->style . "/content_bottom.tpl");
$cont .= "</div>";
$return[] = $cont;
return $return;
}
break;
}
} else
{
$cont[] = "<div id=\"contentcontainer\">";
$cont[] .= file_get_contents("style/" . $this->style . "/content_top.tpl");
$cont[] .= "<h2>Service Mode</h2>";
$cont[] .= "<title>" . $this->sitename . " &raquo; Service Mode</title>";
$cont[] .= "The website currently is in Service Mode.";
$cont[] .= file_get_contents("style/" . $this->style . "/content_bottom.tpl");
$cont[] .= "</div>";
return $cont;
}
}


//Eval's any php code and returns it
function c_eval($var)
{
eval('?>' . $var . '<?php ');
}


//Sets it out for the HTML return
function e_array($array)
{
foreach ($array as $value)
{
$bar .= $value;
}
return $bar;
}

The code works but doesn't return it. I need it to eval the code and return it to the content. If it doesn't, it looks like this:

http://kwix-host.com/site/

krakjoe
October 15th, 2007, 12:46
function _eval( $code )
{
return eval( sprintf( "return &#37;s;", $code ) );
}

JonnyH
October 15th, 2007, 12:52
That gives me this error message:

Parse error: syntax error, unexpected '<' in : eval()'d code on line 1

I changed it to:

<?
return eval(sprintf("return &#37;s;", '?>'.$code.'<?php '));
?>

and it returns blank.

krakjoe
October 15th, 2007, 13:11
why did you change it ?? and why do you keep putting <?php in the call to eval() ?? the function I posted will return the result of the evaluation ...

JonnyH
October 15th, 2007, 13:16
No it doesn't ;)

Parse error: syntax error, unexpected '<' in : eval()'d code on line 1

krakjoe
October 15th, 2007, 13:19
yes it does, your changes broke the function, I'm hardly likely to post code that doesn't work now am I ...



<?php
function eval_one( $code )
{
return eval( sprintf( "return &#37;s;", $code ) );
}
echo eval_one( "8 * 9" );
?>

JonnyH
October 15th, 2007, 13:24
I changed it back to the original code and I got that. Why should I use:
echo eval_one( "8 * 9" );?

That code didn't work.

krakjoe
October 15th, 2007, 13:48
<?php
function eval_one( $code )
{
return eval( sprintf( "return &#37;s;", $code ) );
}
echo eval_one( "8 * 9" );
?>


Is to show you that the function works as you wanted it too ( will evaluate "8 * 9" as php, returning 72 ), there are no calls to echo inside the function declaration and so clearly the function is returning the result of the evaluation, you shouldn't include echo eval_one( "8 * 9" ); in your code at all ....

JonnyH
October 15th, 2007, 15:24
Thanks for the explanation. I've now used the exact same code as you've provided me and I get this error which is at the top:
http://kwix-host.com/site/

krakjoe
October 15th, 2007, 16:03
I don't think eval is all you need to use here, you must be passing mixed html and php, you need to parse the content properly before your eval function handles it ... these are the reasons most people use ready made template engines such as smarty ....

I'm outa time for tonight, and didn't get that far ... but something along the lines of



<?php
function _eval( $code )
{
ob_start( );
eval($code);
$code = ob_get_contents( );
ob_end_clean( );
return $code ;
}
function getResult( $from )
{
if( preg_match_all( "~<\?(php)?(.*?)\?>~si", $from, $php ) )
{
foreach( $php[0] as $id => $code )
{
$from = str_replace( $code, _eval( $php[2][$id] ), $from );
}
}
return $from ;
}

echo getResult( "<html><?" . "php \$i = 0; while( \$i < 10 ){ echo 'Help'; \$i++; } ?" . ">" );
?>


will work for the majority of your templates, I assume .... not sure though ....

JonnyH
October 15th, 2007, 16:26
I intergrated that in and it works perfectly with my '?>' and '<?php ' don't ask me how I just saw it on a tutorial once. Thanks Joe.

krakjoe
October 16th, 2007, 04:03
Glad you got it sorted, I ran into these problems one time, like I said; instead of trying to solve it I just learnt how to use smarty seeing as they already solved all the said problems ..... nice to know there is a solution though because smarty can be quite big and bloats small applications ...

Hamed
October 23rd, 2007, 21:01
eval('$var = "' . $var . '";');

I think eval should use like this.