logger = $logger;
$this->_mBlnMagicQuotes = (bool) get_magic_quotes_gpc();
}
/**
* Enter description here...
*
* @param unknown_type $preset_file
*/
public function loadPresets( $preset_file = '' )
{
$this->_mArrSignaturePresets = ( '' != $preset_file && is_file($preset_file) && is_readable($preset_file) )
? include($preset_file)
: include(dirname(__FILE__).'/include/NanoSanitizer.presets.inc.php');
}
/**
* Enter description here...
*
* @param string $signature_preset
*/
public function setSignaturePreset( $signature_preset = '' )
{
$this->logger->rawAdd('setting signature preset...');
if( NanoUtil::isNotEmptyString($signature_preset) && array_key_exists($signature_preset,$this->_mArrSignaturePresets) )
{
$this->_mStrSignaturePreset = $signature_preset;
$this->logger->add('done.');
}
else
{
$this->_throw( __METHOD__,
' signatur preset ('.$signature_preset.') {'.
gettype($signature_preset).'} NOT valid!' );
}
}
/**
* Sets the signature array with paramters for later verification
*
* @param array $signature_array
*/
public function setSignatures( $signatures_array )
{
$this->logger->rawAdd('setting signatures...');
if( NanoUtil::isNotEmptyArray($signatures_array) )
{
$this->_mArrSignatures = $signatures_array;
$this->_mIntCountSignature = count($signatures_array);
$this->logger->add('done.');
}
else
{
$this->_throw( __METHOD__,
' signatur array ('.implode('|',$signatures_array).
'is NOT valid!' );
}
}
/**
* Set variables which will be sanitized
*
* @param array $variables_array
*/
public function setUnSecureData( $unsecure_variables_array )
{
$this->logger->rawAdd('setting unsecure variable array...');
if( is_array($unsecure_variables_array) && count( $unsecure_variables_array ) > 0 )
{
$this->logger->add('done.');
$this->_mArrUnSecureVariables = $unsecure_variables_array;
$this->_mIntCountUnSecureVariables = count($unsecure_variables_array);
}
else
{
$this->_throw( __METHOD__, ' unsecure variables array is NOT valid!' );
}
}
/**
* sets reporting of variable unequality
*
* @param boolean $bln_switch
*/
public function setErrorReporting( $bln_switch )
{
if( is_bool($bln_switch) )
{
$this->_mBlnReportErrors = $bln_switch;
}
}
/**
* sets reporting of variable unequality
*
* @param boolean $bln_switch
*/
public function setStopOnError( $bln_switch )
{
if( is_bool($bln_switch) )
{
$this->_mBlnStopOnError = $bln_switch;
}
}
/**
* Executes sanitization
*
*/
public function executeSanitization()
{
$this->logger->rawAdd('checking all parameters...');
if( false == $this->_areParemetersValid() )
{
$this->_throw( __METHOD__,
'Signature / Input Variables Error !!! [S:'.
$this->_mIntCountSignature.'|I:'.
$this->_mIntCountUnSecureVariables.']' );
}
$this->logger->add('valid!');
$this->logger->add('
iterating over all sigantures...');
foreach ($this->_mArrSignatures as $varname => $signature)
{
$this->logger->rawAdd('
checking variable '.$varname.' is required but not present...');
if( $this->_isRequiredVariableNotPresent($varname,$signature) )
{
$this->_throw( __METHOD__,
'Variable ['.$varname.'] is required, but NOT present!' );
}
$this->logger->add('OK. {'.(($this->_isVariableRequired($signature))?'required':'NOT required').'}');
$this->logger->rawAdd('checking variable exists in unsecure array...');
if( array_key_exists($varname,$this->_mArrUnSecureVariables) )
{
$this->logger->add('exists!');
$variable_container = trim($this->_mArrUnSecureVariables[$varname]);
$this->logger->rawAdd('searching for preset in signature...');
// -------------------------------------------------------------
// PRESET:
// is preset in signature ?
// set preset data to siganture
if( true === $this->_isSignaturePresetValid($signature) )
{
$this->logger->add('found! {'.NanoUtil::getParam($signature,'preset').'}');
$this->logger->rawAdd('setting signsture preset data to signature...');
$signature = $this->_getSignaturePresetData($signature);
$this->logger->add('done.');
}
else
{
$this->logger->add('NOT found, using given signature.');
}
$this->logger->rawAdd('checking signature given type...');
// -------------------------------------------------------------
// TYPE:
// apply type (integer, string, array,...) to variable
// { brute force mode }
if( $this->_isSignatureVarTypeValid($signature) )
{
$this->logger->rawAdd('valid. setting type ['.$signature['type'].']...');
settype( $variable_container, $signature['type'] );
$this->logger->add('done.');
}
$this->logger->rawAdd('searching for (filter) methods in signature...');
// -------------------------------------------------------------
// METHODS:
// apply method(s) to variable
if( $this->_isSignatureMethodsValid($signature) )
{
$this->logger->add('found.');
// check if is not an array
if( !is_array($signature['methods']) )
{
// put methodname into array for later iteration
$signature['methods'] = array($signature['methods']);
}
$this->logger->add('iterating over given filter methods...');
// iterate over method array
foreach ( $signature['methods'] as $method_data )
{
$this->logger->rawAdd('executing method '.$method_data['name'].'...');
if( method_exists( $this, $method_data['name'] ) )
{
// execute method with variable as parameter
$variable_container = $this->$method_data['name']($variable_container,$method_data['limits']);
}
else
{
$logger_message = 'method ['.$method_data['name'].'] NOT found!!!';
if( true == $this->_mBlnStopOnError )
{
$this->_throw( __METHOD__,'method ['.$method_data['name'].'] NOT found!!!');
}
else { $this->logger->add('method ['.$method_data['name'].'] NOT found!!!'); }
}
$this->logger->add('done.');
}
}
else
{
$this->logger->add('NOT found. (nothing to do)');
}
// -------------------------------------------------------------
// REPORTING (report variable change / stop if has changed)
if( true == $this->_mBlnReportErrors)
{
// -------------------------------------------------------------
// VERIFICATION (is variable changed in value (after sanitization)
if( $this->_isVariableChanged($varname,$variable_container) )
{
$this->logger->add('NOT equal');
$message = '~~ Variable '.$varname.' '. /* $logger */
/* $logger */ 'seems not to be same after sanitization '.
/* $logger */ '