[PHP] Handling exit in eval

2011-03-14 Thread Thomas Björk
Is there any way to emulate an exit in eval without exiting the calling
script.

?php
$s = 'echo Shows; exit; echo Doesn\'t show; ';
eval($s);
echo Never comes here;
?


I would like to do something like this:
?php
$s = 'echo Shows; exit; echo Doesn\'t show; ';

function MakeItStop() {
  // Do something to make the eval stop
  // without halting the script
}

$s = str_replace('exit;', 'MakeItStop();', $s);

eval($s);
echo Never comes here;
?


A simple return would fix it IF the return isn't located within a
function.


Any good suggestions?

Thanks in advanced
Thomas Björk


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Handling exit in eval

2011-03-14 Thread DELiTH
Is there any way to emulate an exit in eval without exiting the calling
script.

?php
$s = 'echo Shows; exit; echo Doesn\'t show; ';
eval($s);
echo Never comes here;
?


I would like to do something like this:
?php
$s = 'echo Shows; exit; echo Doesn\'t show; ';

function MakeItStop() {
  // Do something to make the eval stop
  // without halting the script
}

$s = str_replace('exit;', 'MakeItStop();', $s);

eval($s);
echo Never comes here;
?


A simple return would fix it IF the return isn't located within a
function.


Any good suggestions?

Thanks in advanced
Thomas Björk

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Handling exit in eval

2011-03-14 Thread Richard Quadling
2011/3/14 Thomas Björk t...@bytecode.se:
 Is there any way to emulate an exit in eval without exiting the calling
 script.

 ?php
 $s = 'echo Shows; exit; echo Doesn\'t show; ';
 eval($s);
 echo Never comes here;
 ?


 I would like to do something like this:
 ?php
 $s = 'echo Shows; exit; echo Doesn\'t show; ';

 function MakeItStop() {
  // Do something to make the eval stop
  // without halting the script
 }

 $s = str_replace('exit;', 'MakeItStop();', $s);

 eval($s);
 echo Never comes here;
 ?


 A simple return would fix it IF the return isn't located within a
 function.


 Any good suggestions?

 Thanks in advanced
 Thomas Björk


 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php



Can you provide a more realistic example?

Just don't code anything after the last line of code that contains the
exit? Surely.

http://uk.php.net/manual/en/function.eval.php does say that ...

A return statement will immediately terminate the evaluation of the string .

So, there's your answer I suppose.

Replace exit with return.

-- 
Richard Quadling
Twitter : EE : Zend
@RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Handling exit in eval

2011-03-14 Thread Richard Quadling
2011/3/14 Richard Quadling rquadl...@gmail.com:

And where is my UNDO button.

Essentially, having exit in the eval code is no different to having it
in non-eval code. eval isn't a separate entity which can be
terminated.

So, code the eval code differently.

If you can provide some examples, maybe we can come up with a better solution.



-- 
Richard Quadling
Twitter : EE : Zend
@RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Handling exit in eval

2011-03-14 Thread DELiTH
Richard Quadling wrote:

 2011/3/14 Richard Quadling rquadl...@gmail.com:

 And where is my UNDO button.

 Essentially, having exit in the eval code is no different to having it
 in non-eval code. eval isn't a separate entity which can be
 terminated.

 So, code the eval code differently.

 If you can provide some examples, maybe we can come up with a better solution.




I'm developing a kind of sandbox where you will be able to run a script
without affecting your current context. The script is loaded and parsed
which gives me the opportunity to allow or deny functions and classes
used in the script.
The only problem I have so far is the exit function which, it if is
allowed, terminates the calling script as well as the eval'ed script.

By using a return the problem is solved as long as the user doesn't
write a function containing an exit.

Here is a simple script thet the user might write (probably as useful as
a Hello, world!.

?php
function DoSomethingOrExit($test) {
  if($test == 1) {
exit;
  }
  return;
}

DoSomethingOrExit(0);
echo Hello;
DoSomethingOrExit(1);
echo , world!;
exit;
echo Bye;
?

By replacing exit with return this code will echo Hello, world! before
returning without echoing Bye.


Another perfect solution would be if there was a way to trigger an error
within the eval'ed script that crashes it and returns to the calling
script. But then it just comes back to the entity-problem as you pointed
out.


/Thomas

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Handling exit in eval

2011-03-14 Thread DELiTH
Richard Quadling wrote:

 On 14 March 2011 12:14, DELiTH t...@bytecode.se wrote:
 Richard Quadling wrote:

 2011/3/14 Richard Quadling rquadl...@gmail.com:

 And where is my UNDO button.

 Essentially, having exit in the eval code is no different to having it
 in non-eval code. eval isn't a separate entity which can be
 terminated.

 So, code the eval code differently.

 If you can provide some examples, maybe we can come up with a better 
 solution.




 I'm developing a kind of sandbox where you will be able to run a script
 without affecting your current context. The script is loaded and parsed
 which gives me the opportunity to allow or deny functions and classes
 used in the script.
 The only problem I have so far is the exit function which, it if is
 allowed, terminates the calling script as well as the eval'ed script.

 By using a return the problem is solved as long as the user doesn't
 write a function containing an exit.

 Here is a simple script thet the user might write (probably as useful as
 a Hello, world!.

 ?php
 function DoSomethingOrExit($test) {
  if($test == 1) {
    exit;
  }
  return;
 }

 DoSomethingOrExit(0);
 echo Hello;
 DoSomethingOrExit(1);
 echo , world!;
 exit;
 echo Bye;
 ?

 By replacing exit with return this code will echo Hello, world! before
 returning without echoing Bye.


 Another perfect solution would be if there was a way to trigger an error
 within the eval'ed script that crashes it and returns to the calling
 script. But then it just comes back to the entity-problem as you pointed
 out.


 /Thomas

 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php



 Why not just invoke a separate instance of PHP using exec() ?

 Or take a look at runkit/runkit_Sandbox :
 http://uk.php.net/manual/en/book.runkit.php


exec() tends to allocate to much resources and Runkit_Sandbox is what
I'm trying to get away from =)
The Runkit_Sandbox requires that the server is configured to use it and
my intended solution will run on almost any server without any spcial
needs. (exec is also restricted from many hosting server which makes it
a poor choise.)

My little SandBox will run on almost any server and doesn't require any
special configuration. Sure, it will suck on resources but it will work
as an alternative =)

/Thomas

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php