<?php
class Password
{
/**
* @var integer desired length of password, default: null
*/
protected $length;
/**
* @var array $numbers available numbers (0-9), default: []
*/
protected $numbers = [];
/**
* @var array $uppercase available uppercase characters (A-Z), default: []
*/
protected $uppercase = [];
/**
* @var array available lowercase characters (a-z), default: []
*/
protected $lowercase = [];
/**
* @var array available symbols (! @ # $ % ^ * - _ + = ?), default: []
*/
protected $symbols = [];
/**
* @var array characters to exclude from available characters - supersedes include, default: []
*/
protected $exclude = [];
/**
* @var array characters to include from available characters - overridden by exclude, default: []
*/
protected $include = [];
/**
* @var int amount of times to repeat the specified characters, default: 1
*/
protected $repeat = 1;
/**
* @var array list of characters to exclude when symbols is selected by default
*/
protected $extended = ['/', '\\', '(', ')', '<', '>', '\'', '"', '[', ']', '{', '}'];
/**
* @var array default values
*/
protected $options = [
'numbers' => true,
'uppercase' => true,
'lowercase' => false,
'symbols' => false,
'extended' => false,
'exclude' => [],
'include' => [],
'repeat' => 1
];
/**
* @var bool generate as html, default: false
*/
protected $html = false;
/**
* generates a password of the specified length using desired options
* attempts to prevent repeat characters unless repeat is greater than 1 or the specified length is greater than available characters
* automatically adjusts repeat based on desired length and desired characters to ensure desired length is reached
* @param integer $length desired length of password
* @param bool $numbers include numbers (0-9) in available characters, default: true
* @param bool $uppercase include uppercase characters (A-Z) in available characters, default: true
* @param bool $lowercase include lowercase characters (a-z) in available characters, default: false
* @param bool $symbols include symbols (! @ # $ % ^ * - _ + = ?) in available characters, default: false
* @param bool $extended include obscure symbols (, ), <, >, /, \, ", ', in available characters default: false
* @param array $exclude characters to exclude from available characters - supersedes include, default: array()
* @param array $include characters to include from available characters - overridden by exclude, default: array()
* @param integer $repeat how many times to repeat the specified characters, default: 1
* @return \ISEL\Auth\Password
*/
public function __construct($length, $numbers = null, $uppercase = null, $lowercase = null, $symbols = null, $extended = null, array $exclude = null, array $include = null, $repeat = null)
{
$this->setLength($length);
$this->setOptions([
'numbers' => $numbers,
'uppercase' => $uppercase,
'lowercase' => $lowercase,
'symbols' => $symbols,
'extended' => $extended,
'exclude' => $exclude,
'include' => $include,
'repeat' => $repeat
]);
}
/**
* generates a password of the specified length using desired options
* attempts to prevent repeat characters unless repeat is greater than 1 or the specified length is greater than available characters
* automatically adjusts repeat based on desired length and desired characters to ensure desired length is reached
* @param integer $length desired length of password
* @param bool $numbers include numbers (0-9) in available characters, default: true
* @param bool $uppercase include uppercase characters (A-Z) in available characters, default: true
* @param bool $lowercase include lowercase characters (a-z) in available characters, default: false
* @param bool $symbols include symbols (! @ # $ % ^ * - _ + = ?) in available characters, default: false
* @param bool $extended include obscure symbols (, ), <, >, /, \, ", ', in available characters default: false
* @param array $exclude characters to exclude from available characters - supersedes include, default: array()
* @param array $include characters to include from available characters - overridden by exclude, default: array()
* @param integer $repeat how many times to repeat the specified characters, default: 1
* @return string
*/
public static function factory($length, $numbers = null, $uppercase = null, $lowercase = null, $symbols = null, $extended = null, array $exclude = null, array $include = null, $repeat = null)
{
return new self($length, $numbers, $uppercase, $lowercase, $symbols, $extended, $exclude, $include, $repeat);
}
/**
* @param array $options set options using an array of values, and use defaults
*/
public function setOptions(array $options)
{
foreach ($this->options as $option => $default) {
$method = 'set' . ucfirst($option);
$value = (true === isset($options[$option]) && null !== $options[$option] ? $options[$option] : $default);
$this->$method($value);
}
}
/**
* convert password string to html friendly string
* @param $password
* @return string
*/
public static function passwordToHtml($password)
{
return htmlentities($password, ENT_HTML5 | ENT_QUOTES, false);
}
/**
* convert html entity string to literal string
* @param $password
* @return string
*/
public static function passwordFromHtml($password)
{
return html_entity_decode($password, ENT_HTML5 | ENT_QUOTES);
}
/**
* convert password string to html friendly string
* @param null $password
* @return string
*/
public function toHtml($password = null)
{
return self::passwordToHtml($password ? : $this->generate());
}
/**
* takes an html entity string and converts it to literal characters
* @param $password
* @return string
*/
public function fromHtml($password)
{
return self::passwordFromHtml($password);
}
/**
* output the password based on specified settings
* automatically adjusts based on supplied characters and desired length
* @return string
*/
public function output()
{
$characters = $this->getAvailableCharacters();
$string = substr(str_shuffle(str_repeat($characters, (ceil($this->length / strlen($characters)) * $this->repeat))), 0, $this->length);
if (true === empty($string)) {
throw new \RuntimeException('No string generated, check excluded characters.');
}
if (true === $this->html) {
$string = self::passwordToHtml($string);
}
return $string;
}
/**
* @return string
*/
public function getAvailableCharacters()
{
return implode('', array_diff(array_merge($this->include, $this->lowercase, $this->uppercase, $this->numbers, $this->symbols), array_merge($this->exclude, $this->extended)));
}
/**
* set Exclude
* @param array $exclude
* @return $this
*/
public function setExclude(array $exclude)
{
$this->exclude = $exclude;
return $this;
}
/**
* set Html
* @param boolean $html
* @return $this
*/
public function setHtml($html)
{
$this->html = $html;
return $this;
}
/**
* set Include
* @param array $include
* @return $this
*/
public function setInclude(array $include)
{
$this->include = $include;
return $this;
}
/**
* set Length
* @param int $length
* @return $this
*/
public function setLength($length)
{
$this->length = (int) $length;
return $this;
}
/**
* set Lowercase
* @param boolean $lowercase
* @return $this
*/
public function setLowercase($lowercase, $start = 'a', $end = 'z')
{
$this->lowercase = (true === (bool) $lowercase ? range(strtolower($start), strtolower($end)) : []);
return $this;
}
/**
* set Numbers
* @param boolean $numbers
* @return $this
*/
public function setNumbers($numbers, $start = 0, $end = 9)
{
$this->numbers = (true === (bool) $numbers ? range((int) $start, (int) $end) : []);
return $this;
}
/**
* set Repeat
* @param int $repeat
* @return $this
*/
public function setRepeat($repeat)
{
$this->repeat = (int) $repeat;
return $this;
}
/**
* set Symbols
* @param boolean $symbols include symbols
* @return $this
*/
public function setSymbols($symbols)
{
$this->symbols = (true === (bool) $symbols ? array_merge(range('!', '/'), range(':', '@'), range('[', '`')) : []);
return $this;
}
/**
* set Extended
* @param bool $extended include extended symbols, default: false
* @return $this
*/
public function setExtended($extended)
{
$this->extended = (false === (bool) $extended ? ['/', '\\', '(', ')', '<', '>', '\'', '"', '[', ']', '{', '}'] : []);
return $this;
}
/**
* set Uppercase
* @param boolean $uppercase
* @param string $start starting character, default: A
* @param string $end ending character, default: Z
* @return $this
*/
public function setUppercase($uppercase, $start = 'A', $end = 'Z')
{
$this->uppercase = (true === (bool) $uppercase ? range(strtoupper($start), strtoupper($end)) : []);
return $this;
}
/**
* @return string
*/
public function __toString()
{
return $this->output();
}
}
$temppasword = Password::factory(20490)->output();
echo $temppasword;
Abusive script
This script was stopped while abusing our resources
preferences:
43.19 ms | 419 KiB | 6 Q