Jump to content

Archived

This topic is now archived and is closed to further replies.

horobey

execute PHP codes from within C function in PHP extension

Recommended Posts

I am coding PHP extension. I need to execute PHP code from withing C function in this extesion. PHP code is stored in the string like

 

char * str = \"<?PHP echo(\'test\'); ?>\";

 

now if I call zend_eval_string() it complains about <?PHP ?> tags. I would like the same functionality like zend_eval_string() but with possibility to pass whole PHP file to contained in the string.

 

Any ideas?

yuriy@horobey.com

 

PS really tired looking to solve this... :(

Share this post


Link to post
Share on other sites

I\'m guessing its safe to assume you tried it without the <?PHP ?> tags...?

Share this post


Link to post
Share on other sites

well then, therein lies your answer. Remove any <?PHP and ?> from input, then use the function.

Share this post


Link to post
Share on other sites

er, I don\'t really understand what you\'re getting at. Be more concrete, I don\'t see a problem with removing <?PHP and ?> internally.

Share this post


Link to post
Share on other sites


<?PHP

echo("PHP block 1<br>");

?>

this is not PHP<br>

<%

echo("php block 2<br>");

$v="this is not end %>";

%>

this is not PHP<br>

<script language="PHP">

$v=<<<EV

<?PHP

this is not PHP block

?>

EV;

</script>

<?

echo("<?PHP this is not PHP block?>");

?>

I think you can continue this sample. I mean it is difficult to split PHP file into valid PHP blocks

I think I need to study Zend code...

Share this post


Link to post
Share on other sites

no, that\'s pretty easy with a simple regex. But...I don\'t know what your extension is for, but if its just for you, a warning, don\'t use shorttags, script tags, or asp tags; most servers don\'t have them enabled. stick to <?php ?>.

Share this post


Link to post
Share on other sites

i should be able to process all valid PHP files.

my extension is something to process PHP before executing and if everything is OK then just execute the code.

Share this post


Link to post
Share on other sites

If you\'re processing the code, you should have absolutely no problem identifying code blocks... if you can\'t do that, then you really need to think about the way your processor works.

Share this post


Link to post
Share on other sites

my procwssor is in very early stage.

Right now forget it. Just lets concentrated on how to make Zend parse and execute the PHP file contained in the string?

Share this post


Link to post
Share on other sites

If the zend_eval_string function (which you want to use, im assuming its what does the work in zend, but I havn\'t looked and am at school now and cannot) (and which im hoping isn\'t like using eval(), heh ) doesn\'t like opening and closing tags, then you have to remove them. Separate your blocks, strip the tags, then feed them to zend_eval_string separatly. I havn\'t really looked at zend much, but daeken has. I don\'t really get what you mean by \"parse and execute the PHP file contained in the string\", but I\'m assuming you just want your extension to open and parse PHP from files...in which case im wondering why you\'re making the extension at all...

Share this post


Link to post
Share on other sites

In PHP when using eval() you can fix this problem by prepending a ?> to the string like so:

eval('?>' . $code);

Have you tried something like this?

Share this post


Link to post
Share on other sites

may be horobey want to encrypt some PHP string code, decrypt inside the extension and executing there, right, horobey ?

 

If it is so, his problem is same as me.

 

I want to evaluate php string within an extension, i use zend_eval_string(), success for simple php code like:

 

$str = "print \"hello, world!\";";

 

but, display error when zend_eval_string() parse some php code like:

 

$str = "function {print \"hello, world!\";}";

 

Any solution for this?

 

Please, help.

 

If the zend_eval_string function (which you want to use, im assuming its what does the work in zend, but I havn\'t looked and am at school now and cannot) (and which im hoping isn\'t like using eval(), heh ) doesn\'t like opening and closing tags, then you have to remove them. Separate your blocks, strip the tags, then feed them to zend_eval_string separatly. I havn\'t really looked at zend much, but daeken has. I don\'t really get what you mean by \"parse and execute the PHP file contained in the string\", but I\'m assuming you just want your extension to open and parse PHP from files...in which case im wondering why you\'re making the extension at all...                   

Share this post


Link to post
Share on other sites

  char *cBuffer;
  cBuffer = (char*)malloc(2048);
  memset(cBuffer, 0x00, 2048);

  strcpy(cBuffer, "echo 'this is some php text';");

  //execute decrypted php code via ZEND engine
  zend_try {
      zend_eval_string(cBuffer, NULL, "PHP Script");
      free(cBuffer);
  } zend_catch {
      free(cBuffer);
      php_error(E_ERROR, "Error occurred while executing PHP code, ERR - 101\0");
      RETURN_BOOL(0);
  } zend_end_try();

 

You could try this

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.