The PHP Include function - question for hack

Get help with installation and running phpwcms here. Please do not post bug reports or feature requests here.
Post Reply
irishsk
Posts: 5
Joined: Thu 29. Jan 2004, 02:31

The PHP Include function - question for hack

Post by irishsk »

I am working on using the same capability of the php include funtction to create a hack that will allow me to insert php code into the system rather than including a seperate file.

I think the include for the seperate file is great, but it is a pain if I need to use small snippets of code in php to have to include an entire seperate file just to do so.

It would be nice if I could just use the power of <? date() ?> every now and then. I know there are replacement tags for the date, but only in certain formats. Sometimes, small php snippets can be worth the time.

So, here is what I am trying.

I noticed that the replacement tag uses this function

include_ext_php("$1");

What I am trying to do, is re-create this code to have a replacement tag that is {CODE: <? some code ?>} .

I tried to use the buffer ability like this, but am having an issue.


if( ! ( strpos($content["all"],'{PHP:')===false ) ) {
// includes external PHP script and returns the content
$content["all"] = preg_replace('/\{PHP:(.*?)\}/e', 'include_ext_php("$1");', $content["all"]);
}



function run_php_code($code) {
// just uses the code in the replacement tag area.
$php_content = "";
ob_start();
$code_to_run = $code;
$php_content = ob_get_contents();
ob_end_clean();
return $php_content;
}


I figured that the $code variable could just be reset inside the buffer. It isn't producing anything.

So my questions are:

what is the "$1" used for in the function reference?

How do I achieve this? Is it an issue that I need to change in the regular expression?


Your help is greatly appreciated, the system is wonderful by the way.

Thanks,

Sean
irishsk
Posts: 5
Joined: Thu 29. Jan 2004, 02:31

Where I am currently

Post by irishsk »

If anyone is interested, there have been a few views, I could reaslly use help solving this.

Here is where the code has been heading.

Code: Select all


if( ! ( strpos($content["all"],'{CODE:')===false ) ) {
	// includes external PHP script and returns the content
	$content["all"] = preg_replace('/\{CODE:(.*?)\}/e', 'run_php_code("$1");', $content["all"]);
}






function run_php_code($code) {
	// includes an external PHP script file and returns
	// the result as string from buffered include content
	$code_php_content = "";
	ob_start();
	$code_to_run = eval("$code");
	//serialize("$code_to_run");
	$code_php_content = ob_get_contents();
	ob_end_clean();
	return $code_php_content;
}





Maybe this will help, not sure.

I am getting this error now though, so its getting somewhere.





Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /var/www/html/include/inc_front/front.func.inc.php(835) : eval()'d code on line 1

Parse error: parse error in /var/www/html/include/inc_front/front.func.inc.php(835) : eval()'d code on line 1
irishsk
Posts: 5
Joined: Thu 29. Jan 2004, 02:31

Pretty much solved

Post by irishsk »

Here is the code


This goes in inc_front/content.func.inc

Code: Select all


// use internal PHP code (also normal HTML snippets)
if( ! ( strpos($content["all"],'{CODE:')===false ) ) {
	// uses internal php code snippet, no <? or ?>
	$content["all"] = preg_replace('/\{CODE:(.*?)\}/e', 'run_php_code("$1");', $content["all"]);
}

this goes in inc_front/front.func.inc

Code: Select all


function run_php_code($code) {
	// takes internal PHP code and runs it as PHP
	// the result as string from buffered eval content
	$code_php_content = "";
	ob_start();
	$code_to_run = eval("$code");
	$code_php_content = ob_get_contents();
	ob_end_clean();
	return $code_php_content;
	
}


Hope I get some feedback.
irishsk
Posts: 5
Joined: Thu 29. Jan 2004, 02:31

One Issue

Post by irishsk »

You can't use single quotes, it destroys the native phpwcms parsing apparently.

I am hoping this doesn't totally screw me on other things later.
Yousuf
Posts: 1
Joined: Wed 10. Dec 2003, 21:33

Post by Yousuf »

Hi,

I think i needed this MOD.

Could you please tell me more about it and how to use it? and if i could use it pars code from another php CMS?

Thank you for the effort.
Post Reply