|
|
@@ -0,0 +1,162 @@ |
|
|
<?php |
|
|
|
|
|
/** |
|
|
* Generate a Password |
|
|
*/ |
|
|
class PassGen |
|
|
{ |
|
|
|
|
|
/** |
|
|
* Constructor |
|
|
*/ |
|
|
public function __construct() |
|
|
{ |
|
|
$basePath = basename(__DIR__); |
|
|
|
|
|
$uri = filter_input(INPUT_SERVER, 'REQUEST_URI'); |
|
|
$uri = trim(str_replace("/$basePath", "", $uri), '/'); |
|
|
|
|
|
if (empty($uri)) { |
|
|
echo $this->instructions(); |
|
|
return; |
|
|
} |
|
|
|
|
|
$options = explode('/', $uri); |
|
|
|
|
|
$length = null; |
|
|
$numbers = true; |
|
|
$uppercase = true; |
|
|
$lowercase = true; |
|
|
$othercharsArr = array(); |
|
|
|
|
|
foreach ($options as $option) { |
|
|
if (is_numeric($option) && is_null($length)) { |
|
|
$length = intval($option); |
|
|
continue; |
|
|
} |
|
|
if ($option === '!nums') { |
|
|
$numbers = false; |
|
|
continue; |
|
|
} |
|
|
if ($option === '!uc') { |
|
|
$uppercase = false; |
|
|
continue; |
|
|
} |
|
|
if ($option === '!lc') { |
|
|
$lowercase = false; |
|
|
continue; |
|
|
} |
|
|
array_push($othercharsArr, $option); |
|
|
} |
|
|
|
|
|
$otherchars = implode('', $othercharsArr); |
|
|
|
|
|
if (is_null($length)) { |
|
|
$length = 8; |
|
|
} |
|
|
|
|
|
echo self::GeneratePassword($length, $numbers, $uppercase, $lowercase, $otherchars); |
|
|
} |
|
|
|
|
|
/** |
|
|
* Print the usage instructions |
|
|
*/ |
|
|
protected function instructions() |
|
|
{ |
|
|
$host = filter_input(INPUT_SERVER, 'HTTP_HOST'); |
|
|
$basePath = basename(__DIR__); |
|
|
|
|
|
$chars = urlencode('!@#$%^&*()_+-='); |
|
|
|
|
|
return <<<EOI |
|
|
<pre> |
|
|
/** |
|
|
* Generate a password of the given length using a simple url |
|
|
* |
|
|
* Use any of the following options in any order to generate a password. |
|
|
* Ommitting all options will show this usage guide. |
|
|
* |
|
|
* http://$host/$basePath/[length]/!uc/!lc/!nums/[other] |
|
|
* [length] : length of password as a numeric value |
|
|
* !uc : no upper case characters |
|
|
* !lc : no lower case characters |
|
|
* !nums : no numbers |
|
|
* [other] : other allowed characters |
|
|
* |
|
|
* Samples: |
|
|
* <a href="http://$host/$basePath/12/{$this->encode('!@#$%^&*()_+-=')}">http://$host/$basePath/12/!@#$%^&*()_+-=</a> - 12 character including [a-Z0-9!@#$%^&*()_+-=] |
|
|
* <a href="http://$host/$basePath/20/!lc">http://$host/$basePath/12/!lc</a> - 20 character including [A-Z0-9] |
|
|
* <a href="http://$host/$basePath/64/!lc/!uc/ABCDEF">http://$host/$basePath/64/!lc/!uc/ABCDEF</a> - 64 character including [A-F0-9] |
|
|
* <a href="http://$host/$basePath/32/!lc/!uc/ABCDEF">http://$host/$basePath/32/!lc/!uc/ABCDEF</a> - 32 character including [A-F0-9] |
|
|
* <a href="http://$host/$basePath/64">http://$host/$basePath/64</a> - 64 character including [a-Z0-9] |
|
|
* <a href="http://$host/$basePath/32">http://$host/$basePath/32</a> - 32 character including [a-Z0-9] |
|
|
*/ |
|
|
</pre> |
|
|
EOI; |
|
|
} |
|
|
|
|
|
/** |
|
|
* local shortcut to urlencode; |
|
|
*/ |
|
|
protected function encode($str) |
|
|
{ |
|
|
return urlencode($str); |
|
|
} |
|
|
|
|
|
/** |
|
|
* Generate a random password. |
|
|
* |
|
|
* @param integer $length |
|
|
* @param boolean $numbers |
|
|
* @param boolean $uppercase |
|
|
* @param boolean $lowercase |
|
|
* @param string $otherchars |
|
|
* @return string |
|
|
*/ |
|
|
public static function GeneratePassword($length = 8, $numbers = true, $uppercase = true, $lowercase = true, $otherchars = '') { |
|
|
$charlist = ''; |
|
|
|
|
|
if ($numbers) { |
|
|
$charlist .= '0123456789'; |
|
|
} |
|
|
|
|
|
if ($uppercase) { |
|
|
$charlist .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
|
|
} |
|
|
|
|
|
if ($lowercase) { |
|
|
$charlist .= 'abcdefghijklmnopqrstuvwxyz'; |
|
|
} |
|
|
|
|
|
if ($otherchars) { |
|
|
$charlist .= $otherchars; |
|
|
} |
|
|
|
|
|
$generated = ''; |
|
|
|
|
|
if (is_numeric($length) && $length > 0) { |
|
|
$charlistLength = strlen($charlist); |
|
|
if ($charlistLength > 0) { |
|
|
for ($i = 0; $i < $length; $i++) { |
|
|
$randomIndex = rand(0, $charlistLength - 1); |
|
|
$generated .= substr($charlist, $randomIndex, 1); |
|
|
} |
|
|
return $generated; |
|
|
} else { |
|
|
trigger_error('Attempting to generate a password with no allowed characters.', E_USER_ERROR); |
|
|
} |
|
|
} else { |
|
|
trigger_error('Invalid length specified for password. Length must be a numeric value greater than 0.', E_USER_ERROR); |
|
|
} |
|
|
} |
|
|
|
|
|
/** |
|
|
* Destructor |
|
|
*/ |
|
|
public function __destruct() |
|
|
{ |
|
|
// insert code here. |
|
|
} |
|
|
} |
|
|
|
|
|
new PassGen; |