Created
August 15, 2014 12:15
-
-
Save pvolyntsev/536bc699a85c2e555029 to your computer and use it in GitHub Desktop.
PHP userfriendly password generator (not very strong but really userfriendly and easy to remember) (using Yii framework)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @file protected/components/FriendlyPassword.php | |
*/ | |
/** | |
* Class FriendlyPassword | |
* | |
* Generates passwords like that: random_static_word + random_number + random_dynamic_word | |
* | |
* Note: it is a component for web application based on Yii framework. | |
* Ask me if you need solution for other framework or in plain PHP. | |
* | |
* @author [email protected] | |
* @link http://copist.ru/ | |
* | |
* @usage $password = \Yii::app()->passgen->generate(); | |
*/ | |
class FriendlyPassword extends \CComponent | |
{ | |
public $staticWords = array(); | |
public $randomNumberMin = 11; | |
public $randomNumberMax = 999; | |
public $template = '{number}{static}{number}{dynamic}{number}'; | |
public function init() { } | |
public function generate() | |
{ | |
$self = $this; | |
$password = preg_replace_callback('/(?:\{(static|number|dynamic)\})/', function($matches) use($self) { | |
switch($matches[1]) | |
{ | |
case 'number': | |
return $self->getNumber(); | |
case 'static': | |
return $self->getStaticWord(); | |
case 'dynamic': | |
return $self->getDynamicWord(); | |
default: | |
return $matches[0]; | |
} | |
}, $this->template); | |
return $password; | |
} | |
/** | |
* Returns random word from static words list | |
* @return mixed | |
*/ | |
public function getStaticWord() | |
{ | |
return $this->staticWords[rand(0, count($this->staticWords)-1)]; | |
} | |
/** | |
* Returns random number | |
* @return int | |
*/ | |
public function getNumber() | |
{ | |
return rand($this->randomNumberMin, $this->randomNumberMax); | |
} | |
/** | |
* Returns random word from database | |
* @return mixed | |
*/ | |
public function getDynamicWord() | |
{ | |
// one random word of length between 5 and 9 symbols in lowercase | |
$queryString = <<<SQL | |
SELECT LOWER(t.`tag`) | |
FROM `tag` t | |
WHERE t.`tag` REGEXP '^[[:alpha:]]{5,9}$' | |
ORDER BY RAND() | |
LIMIT 1 | |
SQL; | |
$queryCommand = \Yii::app()->db->createCommand($queryString); | |
return $queryCommand->queryScalar(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @file protected/config/main.php | |
* | |
* Main configuration file for Yii application | |
*/ | |
return array( | |
'components' => array( | |
// ... | |
'db' => array( | |
// ... database connection required | |
), | |
'passgen' => array( | |
'class' => 'application.components.FriendlyPassword', | |
'staticWords' => include('../passgen_static_words.php'), | |
), | |
// ... | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @ file protected/config/passgen_static_words.php | |
* | |
* Static words for passwords generator | |
*/ | |
return array( | |
'correct', | |
'agly', | |
'pretty', | |
'good', | |
'bad', | |
// etc. | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DROP TABLE IF EXISTS `tag`; | |
CREATE TABLE `tag` ( | |
`tag_id` INT(11) NOT NULL AUTO_INCREMENT, | |
`tag` VARCHAR(255) NOT NULL, | |
PRIMARY KEY (`tag_id`), | |
UNIQUE INDEX `uk_tag_tag` (`tag`) | |
) COLLATE='utf8_general_ci' ENGINE=InnoDB AUTO_INCREMENT=0; | |
-- table `tag` dump | |
INSERT INTO `tag` (`tag`) VALUES ('battery'), ('horse'), ('staple'), ('private'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment