Jump to content


Photo

execute PHP codes from within C function in PHP extension


  • Please log in to reply
16 replies to this topic

#1 horobey

horobey
  • New Members
  • Pip
  • Newbie
  • 6 posts
  • LocationUkraine

Posted 29 November 2003 - 12:02 PM

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... :(

#2 Derek

Derek
  • Members
  • PipPipPip
  • Advanced Member
  • 31 posts
  • LocationTennessee

Posted 03 December 2003 - 06:08 PM

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

{

	if (i_know_what_to_do_shut_up_i_dont_need_your_help_mode) {
   return  	} else {    return s  	}  }
ext/skeleton/create_stubs, lines 40-47. - PHP Core

#3 horobey

horobey
  • New Members
  • Pip
  • Newbie
  • 6 posts
  • LocationUkraine

Posted 03 December 2003 - 06:22 PM

yes, I\'ve tried.
without <?PHP ?> it works fine

#4 Derek

Derek
  • Members
  • PipPipPip
  • Advanced Member
  • 31 posts
  • LocationTennessee

Posted 03 December 2003 - 06:24 PM

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

{

	if (i_know_what_to_do_shut_up_i_dont_need_your_help_mode) {
   return  	} else {    return s  	}  }
ext/skeleton/create_stubs, lines 40-47. - PHP Core

#5 horobey

horobey
  • New Members
  • Pip
  • Newbie
  • 6 posts
  • LocationUkraine

Posted 03 December 2003 - 06:30 PM

i would like it to be parsed by stanfard zend engine

#6 Derek

Derek
  • Members
  • PipPipPip
  • Advanced Member
  • 31 posts
  • LocationTennessee

Posted 03 December 2003 - 06:41 PM

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.
function comment(s)

{

	if (i_know_what_to_do_shut_up_i_dont_need_your_help_mode) {
   return  	} else {    return s  	}  }
ext/skeleton/create_stubs, lines 40-47. - PHP Core

#7 horobey

horobey
  • New Members
  • Pip
  • Newbie
  • 6 posts
  • LocationUkraine

Posted 03 December 2003 - 08:17 PM

<?PHPecho("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<?PHPthis 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...

#8 Derek

Derek
  • Members
  • PipPipPip
  • Advanced Member
  • 31 posts
  • LocationTennessee

Posted 03 December 2003 - 08:53 PM

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 ?>.
function comment(s)

{

	if (i_know_what_to_do_shut_up_i_dont_need_your_help_mode) {
   return  	} else {    return s  	}  }
ext/skeleton/create_stubs, lines 40-47. - PHP Core

#9 horobey

horobey
  • New Members
  • Pip
  • Newbie
  • 6 posts
  • LocationUkraine

Posted 03 December 2003 - 09:08 PM

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.

#10 daeken

daeken
  • Members
  • PipPip
  • Member
  • 22 posts
  • LocationChambersburg, PA

Posted 04 December 2003 - 09:09 PM

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.
Black and white are all I see in my infancy.

#11 horobey

horobey
  • New Members
  • Pip
  • Newbie
  • 6 posts
  • LocationUkraine

Posted 04 December 2003 - 09:57 PM

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?

#12 Derek

Derek
  • Members
  • PipPipPip
  • Advanced Member
  • 31 posts
  • LocationTennessee

Posted 05 December 2003 - 01:25 PM

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...
function comment(s)

{

	if (i_know_what_to_do_shut_up_i_dont_need_your_help_mode) {
   return  	} else {    return s  	}  }
ext/skeleton/create_stubs, lines 40-47. - PHP Core

#13 Amaranth

Amaranth
  • New Members
  • Pip
  • Newbie
  • 1 posts
  • LocationSioux City, Iowa

Posted 03 January 2004 - 05:37 AM

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?
Assumptions are the mother of all screwups.

#14 daeken

daeken
  • Members
  • PipPip
  • Member
  • 22 posts
  • LocationChambersburg, PA

Posted 03 January 2004 - 05:44 AM

wow, i never knew about that. thanks amaranth :D
Black and white are all I see in my infancy.

#15 cyberjupie

cyberjupie
  • New Members
  • Pip
  • Newbie
  • 1 posts

Posted 18 June 2008 - 03:15 AM

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...                   



#16 Smystery

Smystery
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 21 October 2008 - 01:12 PM

I have same problem, Any solution  ???
There's no place like 127.0.0.1, There's no web like free pdf ebook

#17 o3d

o3d
  • Members
  • PipPipPip
  • Advanced Member
  • 138 posts
  • LocationSouth-Africa

Posted 24 October 2008 - 08:48 AM

  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
the more you know the more you know you don't know




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users