Code:
<?php
class brute_force
{
var $callback_break = false;
var $min = 0;
var $max = 0;
var $callback = "";
var $_error = "";
var $_chars = array();
function brute_force($callback, $min, $max, $chars="all")
{
$this->min = $min;
$this->max = $max;
$this->callback = $callback;
if(!is_int($this->min) || $this->min < 1)
{
$this->_error = "Invalid number for minimum characters";
return false;
}
elseif(!is_int($this->max) || $this->max < $this->min)
{
$this->_error = "Invalid number for maximum Characters";
return false;
}
elseif(!is_callable($this->callback))
{
$this->_error = "Callback function is not callable";
return false;
}
$c['lower'] = array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z");
$c['upper'] = array("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z");
$c['num'] = array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9");
if(is_array($chars))
{
$this->_chars = array_unique($chars);
sort($this->_chars);
}
else
switch(strtolower($chars))
{
case "lower":
case "upper":
case "num":
$this->_chars = $c[$chars];
break;
default:
case "alnum":
$this->_chars = array_merge($c['lower'], $c['upper'], $c['num']);
break;
case "lalnum":
$this->_chars = array_merge($c['lower'], $c['num']);
break;
case "ualnum":
$this->_chars = array_merge($c['upper'], $c['num'],$c['lower']);
break;
}
}
function errormsg()
{
if($this->_error == "")
return false;
else
return $this->_error;
}
function search()
{
if(!$this->errormsg())
{
$iteration = 0;
$flags = array();
$total_chars = count($this->_chars);
$lendone = "";
$total_strings = "";
$current_string = "";
$first_string = "";
for($i = 0; $i < ($this->max + 1); $i++)
$flags[$i] = -1;
for($i = 0; $i < $this->max; $i++)
$total_strings .= $this->_chars[$total_chars - 1];
for($i = 0; $i < $this->min; $i++)
$flags[$i] = $this->_chars[0];
$i = 0;
while($flags[$i] != -1)
{
$first_string .= $flags[$i];
$i++;
}
$start = true;
$iteration++;
if(!(call_user_func($this->callback, $first_string, $iteration) && $this->callback_break))
while(true)
{
for($i = 0; $i < ($this->max + 1); $i++)
if($flags[$i] == -1)
break;
$i--;
$lendone = 0;
while(!$lendone)
{
for($j = 0; $j < $total_chars; $j++)
if($flags[$i] == $this->_chars[$j])
break;
if($j == ($total_chars - 1))
{
$flags[$i] = $this->_chars[0];
$i--;
if($i < 0)
{
for($i = 0; $i < ($this->max + 1); $i++)
if($flags[$i] == -1)
break;
$flags[$i] = $this->_chars[0];
$flags[$i + 1] = -1;
$lendone = 1;
}
}
else
{
$flags[$i] = $this->_chars[$j + 1];
$lendone = 1;
}
}
$i = 0;
$current_string = "";
while($flags[$i] != -1)
{
$current_string = $current_string . $flags[$i];
$i++;
}
$iteration++;
if((call_user_func($this->callback, $current_string, $iteration) && $this->callback_break) || $current_string == $total_strings)
break;
}
}
else
return false;
}
}
?>