Skip to content

Instantly share code, notes, and snippets.

@romaninsh
Created March 10, 2015 14:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save romaninsh/7835eef2c71c9ab4ee97 to your computer and use it in GitHub Desktop.
Save romaninsh/7835eef2c71c9ab4ee97 to your computer and use it in GitHub Desktop.
Password Strength Checker for Agile Toolkit 4.1
<?php
// Add into a field to perform password strength checking
class StrengthChecker extends HtmlElement {
public $default_text='use secure password';
function init(){
parent::init();
$this->set($this->default_text);
$this->addClass('password-checker');
$field=$this->owner;
if(!($field instanceof Form_Field)){
throw $this->exception('You should use StrengthChecker by inserting it into password field');
}
$field->js('change')->univ()->ajaxec(
array($this->api->getDestinationURL(),
$this->name=>$field->js()->val()
));
$field->js(true)->univ()->autoChange(1000);
if(isset($_GET[$this->name]) || isset($_POST[$this->name])){
$p=$_POST[$this->name]?$_POST[$this->name]:$_GET[$this->name];
// Check
if($p)$out = $this->checkByCrackLib($_GET[$this->name]);
else $out='';
$this->setResponse($p, $out);
}
}
function setResponse($p, $out){
// redefine this in your own checker
// Set color code
$j=$this->showStrengLevel($out);
if(!$p){
$this->js(null,$j)->text($this->default_text)->execute();
}elseif($out != "OK"){
$this->js(null,$j)->text($out)->execute();
}else{
$this->js(null,$j)->text('Password is OK')->execute();
}
}
/* Set text which appears when password field is empty */
function setDefaultText($t){
return $this->set($this->default_text=$t);
}
function showStrengLevel($response){
$j=$this->js()->removeClass('low')->removeClass('high')->removeClass('medium');
if(!$response)return $j;
if($response=='it is WAY too short')return $this->js(null,$j)->addClass('low');
if($response=='OK')return $this->js(null,$j)->addClass('high');
return $this->js(null,$j)->addClass('medium');
}
function checkByCrackLib($password){
$cl=$this->api->getConfig('cracklib','/usr/sbin/cracklib-check');
if(file_exists($cl) && is_executable($cl)){
$cl=$this->add('System_ProcessIO')
->exec('/usr/sbin/cracklib-check')
->write_all($password)
;
$out=trim($cl->read_all());
$out=str_replace($password,'',$out);
$out=preg_replace('/^:\s*/','',$out);
return $out;
} else {
if(strlen($password)<4)return "it is WAY too short";
if(strlen($password)<6)return "it is too short";
return "OK";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment