2008-05-22 20:05:53 +00:00
|
|
|
<?php
|
|
|
|
class VariableModule extends ScannerModule {
|
|
|
|
private $assigned_variables = array();
|
|
|
|
private $captured = array();
|
|
|
|
|
|
|
|
function VariableModule() {
|
|
|
|
$this->ScannerModule();
|
|
|
|
}
|
|
|
|
function parserCallback( $object ) {
|
|
|
|
$pattern = '/\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/';
|
|
|
|
$matches = array();
|
2008-05-23 21:13:36 +00:00
|
|
|
$variable = preg_match($pattern, $object['name'], $matches) > 0 ? $matches[0] : false;
|
2008-05-22 20:05:53 +00:00
|
|
|
$scope = "{$object['in_class']}::{$object['in_function']}";
|
|
|
|
if (!isset($this->assigned_variables[$scope] ) )
|
|
|
|
$this->assigned_variables[$scope] = array();
|
|
|
|
if ($object['type'] == PHPPARSER_ASSIGNMENT) {
|
2008-05-23 21:13:36 +00:00
|
|
|
list($var, $value) = explode('=', $object['name']);
|
|
|
|
if ($variable == $var) {
|
|
|
|
// Regular variable assignment
|
|
|
|
//$this->fault($object, 0, "Assignment: {$object['name']}");
|
|
|
|
$this->assigned_variables[$scope][] = $var;
|
|
|
|
} else {
|
|
|
|
// Array index assignment
|
|
|
|
//$this->fault($object, 0, "Array index assignment: [{$variable}] {$object['name']}");
|
|
|
|
if (
|
|
|
|
!in_array($variable, $this->assigned_variables[$scope])
|
|
|
|
&& (empty($object['in_class']) && $variable == '$this')
|
|
|
|
) {
|
|
|
|
$this->fault($object, FAULT_MINOR, "Array key assignment on previously undefined variable: $var");
|
|
|
|
}
|
|
|
|
$this->assigned_variables[$scope][] = $variable;
|
|
|
|
}
|
2008-05-22 20:05:53 +00:00
|
|
|
}
|
|
|
|
if (
|
|
|
|
$object['type'] == PHPPARSER_VARIABLE
|
|
|
|
// Cannot yet accurately scan the global scope, so functions only
|
|
|
|
&& !empty($object['in_function'])
|
2008-05-23 21:13:36 +00:00
|
|
|
&& !in_array($variable, $this->assigned_variables[$scope])
|
|
|
|
&& !in_array($variable, array(
|
2008-05-22 20:05:53 +00:00
|
|
|
// Superglobals are exempt, obviously
|
|
|
|
'$GLOBALS', '$_SERVER', '$_GET', '$_POST', '$_FILES', '$_COOKIE', '$_SESSION', '$_REQUEST', '$_ENV'
|
|
|
|
))
|
2008-05-23 21:13:36 +00:00
|
|
|
&& (empty($object['in_class']) && $variable == '$this')
|
2008-05-22 20:05:53 +00:00
|
|
|
) {
|
2008-05-23 21:13:36 +00:00
|
|
|
$this->fault($object, FAULT_MEDIUM, "Undefined Variable: $variable");
|
2008-05-22 20:05:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
addModule( new VariableModule() );
|
|
|
|
?>
|