<?
/**
* @name Security class
* @author Я ацкий одмин, йа счас в серверной кота через шредер за 25 секнд пропустил o_0
* @copyright 2009 кот, админ, шредер.
* @uses This class can help you to find risky files
*/
class Security
{
/**
*
* @var String|false
* @uses Mail of admin
*/
private $admin_mail = false;
/**
*
* @var Array
* @uses Risky files
*/
private $risky = array ();
/**
*
* @var Array
* @uses What to search. Optional: if it not called, script will check - "executable" or not.
*/
private $search = array ();
/**
*
* @var Array
* @uses All files in directory
*/
private $files = array ();
/**
*
* @var Array
* @uses Temp var
*/
private $temp_dir = array ();
/**
*
* @param String
* @uses Activate and create structure
*/
public function __construct ($start_folder = '/')
{
$this->create_structure ($start_folder);
}
/**
*
* @param String
* @return Create structure
*/
private function create_structure ($start_folder)
{
if (is_dir ($start_folder))
{
if (class_exists ('RecursveDirectoryIteratorIterator') && class_exists ('RecursiveDirectoryIterator'))
{
$this->files = new RecursveDirectoryIteratorIterator(new RecursiveDirectoryIterator($start_folder));
}
else
{
$this->scan_dir ($start_folder, true);
while (count ($this->temp_dir))
{
$dir = reset ($this->temp_dir);
if (empty ($dir))
{
break;
}
else
{
$this->scan_dir ($dir);
}
}
}
}
else
{
throw new Exception("$start_folder is not directory");
}
}
/**
*
* @param $dir - String
* @param $first - true|false
* @uses Create structure of $dir
*/
private function scan_dir ($dir, $first = false)
{
$array = scandir ($dir);
array_splice($array, 0, 2);
if (!$first)
{
unset ($this->temp_dir [array_search ($dir, $this->temp_dir)]);
}
if (count ($array) > 1)
{
foreach ($array as $value)
{
$a = substr ($dir, - 1);
$temp = (substr ($dir, - 1)==='/' ? $dir : $dir.'/').$value;
if (is_dir ($temp))
{
$this->temp_dir[] = $temp;
}
else if (is_file ($temp))
{
$this->files[] = $temp;
}
}
}
}
/**
*
* @param Array - What to search
*/
public function search ()
{
$this->search = array_unique (array_merge (func_get_args (), $this->search));
}
/**
* @uses Main action. Finder.
* @return Risky files|false
*/
public function scan ()
{
if (count ($this->search))
{
$gc = true;
}
foreach ($this->files as $value)
{
if (is_executable ($value))
{
$this->risky['executable'][] = $value;
}
if ($gc)
{
$temp = file_get_contents ($value);
foreach ($this->search as $val)
{
if (stristr ($temp, $val))
{
$this->risky[$val][] = $value;
}
}
$temp = null;
}
}
if (count ($this->risky))
{
if ($this->admin_mail)
{
$this->send_mail ();
}
return $this->risky;
}
else
{
return false;
}
}
/**
*
* @param String
* @uses Set receiver of the mail.
*/
public function mail_alert ($mail)
{
$this->admin_mail = $mail;
}
/**
*
* @uses Mail sender
*/
private function send_mail ()
{
$count = count($this->risky, 0);
mail($this->admin_mail, "Founded risky files", "It is PHP Security class.\nNow was found $count risky files.\nPlease check its.");
}
}
|