8 Replies - 1647 Views - Last Post: 18 September 2011 - 04:38 AM Rate Topic: -----

#1 aaron1178  Icon User is offline

  • Dovakiin, Dragonborn
  • member icon

Reputation: 169
  • View blog
  • Posts: 1,298
  • Joined: 22-October 08

eval with php <?php and ?>

Posted 17 September 2011 - 11:08 PM

Hey,
I am creating an online sandbox editor for php, and all is good so far, until I used more than I structure of php code.

$string = explode("<?php", $page);

    if(count($string) > 1)
    {
        echo $string[0];
        
        $string = explode("?>", $string[1]);
        
        eval($string[0]);
        
        if(count($string) > 1)
        {
            echo $string[1];
        }
    }
    else
    {
        echo $page;
    }



This only cycles through 1 set of <?php and ?>, how would I go about having x number of <?php and ?> ?

Use a foreach loop?

Thanks,

Is This A Good Question/Topic? 0
  • +

Replies To: eval with php <?php and ?>

#2 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3719
  • View blog
  • Posts: 5,990
  • Joined: 08-June 10

Re: eval with php <?php and ?>

Posted 17 September 2011 - 11:38 PM

Personally I would try going with a regular expression replacement. PHP has a handy way to allow you to execute replacement strings as PHP code, which makes stuff like this possible:
/** Used in the regexp to eval and capture the output of the PHP block */
function eval_rt($str) {
    ob_start();
    eval($str);
    return ob_get_clean();
}

// The 'e' modifier in this pattern makes the preg_replace call
// execute the replacement string as PHP code and use the value
// returned from that as the replacement string.
$pattern = '/<\?php(.*?)\?>/sme';

// This replacement string calls the function defined above to eval
// the contents of the PHP block captured by the pattern and return
// the resulting output.
$replacement = 'eval_rt("$1");';

// Which leaves us with...
$out = preg_replace($pattern, 'eval_rt("$1");', $phpstr);
echo $out;


Was This Post Helpful? 0
  • +
  • -

#3 aaron1178  Icon User is offline

  • Dovakiin, Dragonborn
  • member icon

Reputation: 169
  • View blog
  • Posts: 1,298
  • Joined: 22-October 08

Re: eval with php <?php and ?>

Posted 18 September 2011 - 12:32 AM

Thanks for that, but I get these errors:

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\wamp\www\core\functions.fnc(15) : regexp code on line 4

Fatal error: preg_replace() [function.preg-replace]: Failed evaluating code: eval_rt"blah blah
Was This Post Helpful? 0
  • +
  • -

#4 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3719
  • View blog
  • Posts: 5,990
  • Joined: 08-June 10

Re: eval with php <?php and ?>

Posted 18 September 2011 - 01:39 AM

Hmm. Turns out the quotes in the code are being escaped.

Try this:
<?php
$phpstr = <<<'STR'
<h1>Hello, <?php echo "World"; ?>!</h1>
<?php
for ($i = 0; $i < 3; ++$i) {
    echo "Line #{$i}<br>";
}
?>
STR;

function eval_rt($str) {
	// The preg_replace function apparently adds slashes to the code
	// so it won't mess up the replacement string.
	$str = stripslashes($str);
    ob_start();
    eval($str);
    return ob_get_clean();
}

$pattern = '/<\?php(.*?)\?>/sme';
$replacement = 'eval_rt(\'$1\');';

echo preg_replace($pattern, $replacement, $phpstr);
?>


Was This Post Helpful? 1
  • +
  • -

#5 aaron1178  Icon User is offline

  • Dovakiin, Dragonborn
  • member icon

Reputation: 169
  • View blog
  • Posts: 1,298
  • Joined: 22-October 08

Re: eval with php <?php and ?>

Posted 18 September 2011 - 02:50 AM

Hahah Thanks Atli, you have fixed my problem :)

Or so I thought?

Parse error: syntax error, unexpected $end in C:\wamp\www\core\functions.fnc(12) : eval()'d code on line 48

Line 48

?>

This post has been edited by aaron1178: 18 September 2011 - 02:52 AM

Was This Post Helpful? 0
  • +
  • -

#6 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,227
  • Joined: 08-June 10

Re: eval with php <?php and ?>

Posted 18 September 2011 - 02:59 AM

View Postaaron1178, on 18 September 2011 - 11:50 AM, said:

Parse error: syntax error, unexpected $end in

a missing } usually
Was This Post Helpful? 0
  • +
  • -

#7 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3719
  • View blog
  • Posts: 5,990
  • Joined: 08-June 10

Re: eval with php <?php and ?>

Posted 18 September 2011 - 03:00 AM

Why is there a ?> in the code you are sending through eval? The regexp should only take what is in between the PHP code tags, not the tags themselves.

Can you show us the rest of the code you are passing through the eval?
Was This Post Helpful? 0
  • +
  • -

#8 aaron1178  Icon User is offline

  • Dovakiin, Dragonborn
  • member icon

Reputation: 169
  • View blog
  • Posts: 1,298
  • Joined: 22-October 08

Re: eval with php <?php and ?>

Posted 18 September 2011 - 03:47 AM

<?php
require_once 'core/core.core';

if(isset($_GET["module"]) && isset($_GET["page"]))
{
    if($_GET["page"] != "admin")
    {
        echo "<h2>Hacking attempt</h2><p>You IP has been logged and staff have been notified!";
        hackingAttempt($_SERVER["REQUEST_URI"], $_SERVER["REMOTE_ADDR"], $uLin);
        $l->Lfooter();
        exit();
    }
    
    switch($_GET["module"])
    {
        case "createPage"; createPage(); break;
        default: break;
    }
}

function createPage()
{
    global $db, $l, $f;
    if(isset($_POST["cp"]))
    {
        
        if(empty($_POST["pageName"]) || empty($_POST["pageTitle"]) || empty($_POST["pageContent"]))
        {
            echo "<h2>Missing Field</h2><p>You need to fill in all fields!";
            $l->Lfooter();
            exit();
        }
        
        $name = $db->clean($_POST["pageName"]);
        $title = $db->clean($_POST["pageTitle"]);
        $content = $db->clean($_POST["pageContent"]);
        
        $sql = "INSERT INTO `pages`(`page_name`,`page_title`,`page_content`,`page_author`) VALUES('$name','$title','$content','{$f['username']}')";
        
        $db->query($sql);
        
        echo "<h2>Page Created</h2><p>Your page has been created! To view it, please click on <a href='/index.php?page=$name'>this link</a>";
        
        $l->Lfooter();
        exit();
    }
?>

    <h2>Create Page</h2>
    <p>
        Here you can create a page. Pages are stored into a database and can accept php code directly.
        Please note, to use php, both start and end delimeters to properly store the page.
    </p>
    <p>
        <div style="text-align:right;line-height: 30px;padding-right: 300px;">
            <form method="post" action="/index.php?page=admin&module=createPage">
        Page Name: <input type="text" name="pageName" class="text" /><br />
        Page Title: <input type="text" name="pageTitle" class="text" /><br />
        Page Content: <textarea name="pageContent" rows="10" cols="40"/></textarea><br />
        <input type="submit" id="submit" name="cp" value="Create Page" />
            </form>
        
        </div>
    </p>
<?php
    exit();

}

echo "<h2>Hacking attempt</h2><p>You IP has been logged and staff have been notified!";
hackingAttempt($_SERVER["REQUEST_URI"], $_SERVER["REMOTE_ADDR"], $uLin);
$l->Lfooter();
exit();
?>




I am pretty sure that it is me using $db->. As another page that uses it displays this:

query("UPDATE `developers` SET `lastActivity`=unix_timestamp() WHERE `id`='{$f['id']}' LIMIT 1"); $f = 0; $_SESSION["logged_in"] = 0; $_SEESION["user_id"] = 0; session_destroy(); echo "
Developer Sign Out
You have signed out! Please wait to be redirected, or click this link
"; echo "";



as if it hasn't been eval'd
Was This Post Helpful? 0
  • +
  • -

#9 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3719
  • View blog
  • Posts: 5,990
  • Joined: 08-June 10

Re: eval with php <?php and ?>

Posted 18 September 2011 - 04:38 AM

Ahh I see. Based on your first example, I assumed you wanted each PHP code block evaluated on it's own rather than a part of a larger document.

The problem with your current code is that the PHP blocks in the code string you are feeding it are not independent of each other. You open a IF statement in the first block, but do not close it until the last block. This means that, technically, the first block has a syntax error, and if you evaluate it on it's own, PHP will stop with a parse error. In order for PHP to properly parse this, the page must be evaluated as a whole.

So, the solution is to feed the entire page to the eval command, from where the first <?php occurs until the last ?>. Within the string you feed eval(), you can leave PHP mode by closing the PHP block using the normal ?>. PHP will then process the string that follows as raw output, until it finds another <?php.

To do that, make the regexp greedy again.
// Non-greedy, like I used before:
$pattern = '/<\?php(.*?)\?>/sme';

// Greedy, which will match everything between the first <?php until
// the last ?>, including any and all instances of the two.
$pattern = '/<\?php(.*)\?>/sme';


The only problem I can see is that in order for this to work, the page MUST close the last PHP block. If you leave it open, as PHP normally allows, the regexp will not grab all the code and it will cause a parse error.



By the way, I just have to ask. Why are you doing it this way? Is there a reason why are you using eval() instead of putting the code into a file and using include? If you did that, PHP would handle all this for you. Evaluating code this way usually just causes problems.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1