FRED™  3.0
FRED™: Framework for Rapid and Easy Development
Integrity.php
Go to the documentation of this file.
1 <?php
2 
4 
5 class Integrity extends \Rsi\Fred\Security\Server{
6 
7  public $root = null; //!< Root directory for check.
8  public $filter = '/\\.php$/i'; //!< Pattern for files to check.
9  public $log = null; //!< File to store statistics (in temp dir when empty).
10  public $mode = 0666;
11 
12  public function check(){
13  $log = $this->component('log');
14  $errors = null;
15  if(!$this->root){
16  if($this->root = \Rsi\Http::docRoot()) $log->warning('Using default root: ' . $this->root,__FILE__,__LINE__);
17  else $log->error('No root set',__FILE__,__LINE__);
18  }
19  if($this->root){
20  $errors = [];
21  if(!$this->log) $this->log = \Rsi\File::tempDir() . 'integrity.log';
22  $files = \Rsi\File::unserialize($this->log,[]);
23  foreach(\Rsi\File::find($this->root,[\Rsi\File::FIND_FILTER_TYPE => \Rsi\File::FIND_TYPE_FILE,\Rsi\File::FIND_FILTER_NAME . '//' => $this->filter],true) as $filename => $info){
24  $hash = md5(file_get_contents($filename));
25  $error = null;
26  if(!array_key_exists($filename,$files)) $error = 'New file';
27  elseif($files[$filename] != $hash) $error = 'Changed file';
28  if($error){
29  $log->error($error . ': ' . $filename,__FILE__,__LINE__);
30  $errors[$filename] = $error;
31  $files[$filename] = $hash;
32  }
33  }
34  \Rsi\File::serialize($this->log,$files,$this->mode);
35  }
36  return $errors;
37  }
38 
39 }
$log
File to store statistics (in temp dir when empty).
Definition: Integrity.php:9
$filter
Pattern for files to check.
Definition: Integrity.php:8
$root
Root directory for check.
Definition: Integrity.php:7
component($name)
Get a component (local or default).
Definition: Component.php:80