note 82811 added to function.php-check-syntax
| From: | kevin at metalaxe dot com | Date: | Sat, 26 Apr 2008 22:01:08 +0000 |
| Subject: | note 82811 added to function.php-check-syntax | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-138834@lists.php.net to get a copy of this message | ||
Sorry, I'm retarded. I did a little more testing and found big issues. I've fixed them in
the following code. This example is now dependant on http://us3.php.net/manual/en/function.token-get-all.php#80335
for a bugfix workaround with token_get_all(); So be sure to include that function in your script
when you run this function.
<?php
/**
* Check Eval
* Performs a Syntax check without killing the parser (hopefully)
*
* @params string PHP to be evaluated
* @return array Parse error info or true for success
**/
function checkEval( $php )
{
$evalStr = 'return;';
# Get the string tokens
$crack = token_get_all( "<?php\n".$php );
# Drop our manually entered opening tag
array_shift( $crack );
token_fix( $crack );
# Check to see how we need to proceed
# prepare the string for parsing
if( isset( $crack[0][0] ) &&
(
$crack[0][0] === T_CONSTANT_ENCAPSED_STRING ||
$crack[0][0] === T_STRING ||
$crack[0][0] === T_OPEN_TAG
)
){
$evalStr .= '?>';
}
# Make sure syntax reporting is on...probably only need E_PARSE...but I'm anal
$oldErr = error_reporting( E_ALL | E_STRICT );
# Prevent output
ob_start();
eval( $evalStr.$php );
$evaluput = ob_get_clean();
# Reset error reporting
error_reporting( $oldErr );
# Only do this if there was output
if( $evaluput !== '' )
{
# Strip annoying html tags, in case html reporting is enabled in php.ini
$evaluput = strip_tags( $evaluput );
# Fancy Regex to pull the error that killed us
$pattern = '/Parse error:\s*syntax error,(.+?)\s+in\s+.+?\s*line\s+(\d+)/';
$parse_fail = (bool)preg_match( $pattern, $evaluput, $match );
# Parse error to report?
if( $parse_fail === true )
{
return array(
'line' => (int)$match[2],
'msg' => $match[1]
);
}
}
return true;
}
?>
----
Server IP: 208.69.120.35
Probable Submitter: 76.22.73.161
----
Manual Page -- http://www.php.net/manual/en/function.php-check-syntax.php
Edit -- https://master.php.net/note/edit/82811
Del: integrated -- https://master.php.net/note/delete/82811/integrated
Del: useless -- https://master.php.net/note/delete/82811/useless
Del: bad code -- https://master.php.net/note/delete/82811/bad+code
Del: spam -- https://master.php.net/note/delete/82811/spam
Del: non-english -- https://master.php.net/note/delete/82811/non-english
Del: in docs -- https://master.php.net/note/delete/82811/in+docs
Del: other reasons-- https://master.php.net/note/delete/82811
Reject -- https://master.php.net/note/reject/82811
Search -- https://master.php.net/manage/user-notes.php