I'm writing an application that will allow approved developers to submit php code to our database to be run at a later time.
I'm attempting to implement a system where before their code is run it is checked for any issues.
I found the following code at http://bytes.com/topic/php/answers/4819-before-eval-how-can-one-test-string-see-if-valid-php-code
function checkPHP($string) {
$string = escapeshellcmd($string);
exec("php -r \"$string\"",$output,$exit);
if($exit==0) return TRUE;
else return FALSE;
}
/* tests */
$test = array ("print ('foo');",
"print (\"foo\");",
"pint ('foo');",
"print ('foo);",
"print ('foo','bar');"
);
for($i=0;$i<sizeof($test);$i++) {
print $test[$i];
if(checkPHP($test[$i])) {
print " is ok.<br />\n";
} else {
print " not ok.<br />\n";
}
}
When I use it I get:
print ('foo'); is ok.
print ("foo"); is ok.
pint ('foo'); is ok.
print ('foo); is ok.
print ('foo','bar'); is ok.
I'm running Apache 2.2 / PHP 5.3.8 Safe Mode off