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
The PHP Include function - question for hack
Where I am currently
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.
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
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
Pretty much solved
Here is the code
This goes in inc_front/content.func.inc
this goes in inc_front/front.func.inc
Hope I get some feedback.
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"]);
}
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.