Last active
October 1, 2015 10:15
-
-
Save mahedi2014/8c847aa309eb1957f7ec to your computer and use it in GitHub Desktop.
Password policy
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 | |
/** | |
* Password policy | |
* | |
* Created by Mahedi Azad. | |
* User: Mahedi Azad | |
* Date: 01-Oct-15 | |
* Time: 9:45 AM | |
*/ | |
class PasswordPolicy { | |
public $rules = array( | |
'lengthStatus' => false, //password length true for applicable | |
'minLength' => '5', | |
'maxLength' => '8', | |
'numberStatus' => false, //password number true for contain at last one number | |
'uppercaseStatus' => false, //password uppercase true for contain at last one uppercase | |
'lowercaseStatus' => false, //password lowercase true for contain at last one lowercase | |
'specialCharacterStatus' => false, //password special character true for contain at last one special character | |
'whiteSpaceStatus' => false, //password allow space | |
); | |
public function validation($str) { | |
$result['status'] = false; | |
if(empty($str)){ | |
$result['message'] = 'Password can not be empty'; | |
$result['status'] = false; | |
}elseif(($this->rules['lengthStatus'] == true) & ($this->lengthValidation($str, $this->rules['minLength'], $this->rules['maxLength'])== false)){ | |
$result['message'] = 'Your password must be '.$this->rules['minLength'].' to '.$this->rules['maxLength'].' characters'; | |
$result['status'] = false; | |
}elseif(($this->rules['numberStatus'] == true) & ($this->isContainNumber($str) == false)){ | |
$result['message'] = 'Your password must contain at least one number.'; | |
$result['status'] = false; | |
}elseif(($this->rules['uppercaseStatus'] == true) & ($this->isContainUppercase($str) == false)){ | |
$result['message'] = 'Your password must contain at least one uppercase letter.'; | |
$result['status'] = false; | |
}elseif(($this->rules['lowercaseStatus'] == true) & ($this->isContainLowercase($str) == false)){ | |
$result['message'] = 'Your password must contain at least one lowercase letter.'; | |
$result['status'] = false; | |
}elseif(($this->rules['specialCharacterStatus'] == true) & ($this->isContainSpecialCharacter($str) == false)){ | |
$result['message'] = 'Your password must contain at least one special character.'; | |
$result['status'] = false; | |
}elseif(($this->rules['whiteSpaceStatus'] == true) & ($this->isWhiteSpaceContain($str) == false)){ | |
$result['message'] = 'Space is not allow in password'; | |
$result['status'] = false; | |
}else{ | |
$result = true; | |
} | |
return $result; | |
} | |
/** | |
* Check is string contain space or not | |
* @param $str | |
* @return bool; true: if there is space | |
*/ | |
public function isWhiteSpaceContain($str) { | |
$str = preg_replace('/\s\s+/', ' ', $str ); | |
if (strpos($str,' ') | preg_match(' ',$str)) { | |
return false; | |
}else{ | |
return true; | |
} | |
} | |
/** | |
* Check string is contain minimum single uppercase | |
* | |
* @param $str | |
* @return bool; true for having uppercase | |
*/ | |
public function isContainUppercase($str){ | |
$pattern = '/[A-Z]/'; | |
if (preg_match($pattern, $str, $matches)) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
/** | |
* Check is string contain minimum one lowercase | |
* | |
* @param $str | |
* @return bool; true for having lowercase | |
*/ | |
public function isContainLowercase($str){ | |
$pattern = '/[a-z]/'; | |
if (preg_match($pattern, $str, $matches)) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
/** | |
* Check is string contain minimum one number | |
* | |
* @param $str | |
* @return bool; true for having number | |
*/ | |
public function isContainNumber($str){ | |
$pattern = '/[0-9]/'; | |
if (preg_match($pattern, $str, $matches)) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
/** | |
* Check is string contain minimum single special character | |
* | |
* @param $str contain ~!@#$%^&*()_+`{}[]|\<>,.? | |
* @return bool; true for having special character | |
*/ | |
public function isContainSpecialCharacter($str){ | |
$pattern = '/[!@#$%^&*()\\-_=+{};\:,<\.>~|"\']/'; | |
if (preg_match($pattern, $str, $matches)) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
/** | |
* Checking is string length in required length | |
* | |
* @param $str | |
* @param int $minL; minimum required length | |
* @param int $maxL; maximum required length | |
* @return bool | |
*/ | |
public function lengthValidation($str, $minL=5, $maxL=8){ | |
$length = strlen($str); | |
if (( $length >= $minL) & ($length <= $maxL )) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
} | |
/*************************************Uses**************************************/ | |
$pwObj = new PasswordPolicy(); | |
$pwObj->rules = array( | |
'lengthStatus' => ture, //password length true for applicable | |
'minLength' => '5', //minimum password length | |
'maxLength' => '8', //maximum password limit | |
'numberStatus' => true, //password number true for contain at last one number | |
'uppercaseStatus' => true, //password uppercase true for contain at last one uppercase | |
'lowercaseStatus' => true, //password lowercase true for contain at last one lowercase | |
'specialCharacterStatus' => true, //password special character true for contain at last one special character | |
'whiteSpaceStatus' => true, //password not allow space | |
); | |
var_dump($pwObj->validation('1 2A!ADd')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment