note 82809 added to function.php-check-syntax
| From: | kevin at metalaxe dot com | Date: | Sat, 26 Apr 2008 21:22:35 +0000 |
| Subject: | note 82809 added to function.php-check-syntax | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-138832@lists.php.net to get a copy of this message | ||
2: kevin at metalaxe dot com
Hm.. i think your function didnt work...
Yeah, I noticed this. I could do a couple of things, but I played with this a little less half-assed
today. Give this new version a shot, see output below. I tried to even parse classes, but it seems
that classes are being processed a lot differently and blow FATAL errors during the parsing phase...
Anyone have any ideas around this?
<?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'.$php );
# Drop our manually entered opening tag
array_shift( $crack );
# Check to see how we need to proceed
# prepare the string for parsing
if( isset( $crack[0] ) && $crack[0][1] === T_CONSTANT_ENCAPSED_STRING | T_STRING |
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;
}
?>
Test Case:
<?php
require('syntaxcheck.php');
//$axis = axis_kernel::init( __FILE__ );
ini_set( 'display_errors', true );
error_reporting( E_ALL | E_STRICT );
$php[] = 'asfvsd <?php echo \'aaa\'; ?> asvsdgb';
$php[] = '<'."?php echo 'something';?> asvsdgb";
$php[] = "echo 'something';?> asvsdgb";
$php[] = "if( PHP_VERSION ) { echo PHP_VERSION; }";
$php[] = $temp = <<<EOF
abstract class bob {
public \$property = 'string';
public \$property2 = true;
public \$property3 = array();
abstract public static final function __construct(){}
}
EOF;
$php[] = '<?php '.$temp;
print '<pre>';
foreach( $php as $code )
{
print 'Test php'."\n\n";
print htmlspecialchars( $code );
print "\n\n".'Results:'."\n";
var_dump( checkEval( $code ) );
print "\n\n";
}
exit();
?>
Output:
Test php
asfvsd <?php echo 'aaa'; ?> asvsdgb
Results:
bool(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/82809
Del: integrated -- https://master.php.net/note/delete/82809/integrated
Del: useless -- https://master.php.net/note/delete/82809/useless
Del: bad code -- https://master.php.net/note/delete/82809/bad+code
Del: spam -- https://master.php.net/note/delete/82809/spam
Del: non-english -- https://master.php.net/note/delete/82809/non-english
Del: in docs -- https://master.php.net/note/delete/82809/in+docs
Del: other reasons-- https://master.php.net/note/delete/82809
Reject -- https://master.php.net/note/reject/82809
Search -- https://master.php.net/manage/user-notes.php