= 6 && $length <= 9 ) { $strength++; } //check if length is 10-15 chars if ( $length >= 10 && $length <= 15 ) { $strength += 2; } //check if length greater than 15 chars if ( $length > 15 ) { $strength += 3; } $duplicate_chars = 1; $consecutive_chars = 1; $char_arr = str_split( strtolower( $password ) ); $prev_char_int = ord( $char_arr[0] ); foreach ( $char_arr as $char ) { $curr_char_int = ord( $char ); $char_int_diff = abs( $prev_char_int - $curr_char_int ); if ( $char_int_diff == 0 ) { //Duplicate $duplicate_chars++; } else if ( $char_int_diff == 1 || $char_int_diff == -1 ) { //Consecutive $consecutive_chars++; } $prev_char_int = $curr_char_int; } $duplicate_percent = ( ( $duplicate_chars / strlen( $password ) ) * 100 ); $consecutive_percent = ( ( $consecutive_chars / strlen( $password ) ) * 100 ); if ( $duplicate_percent <= 25 ) { $strength++; } if ( $consecutive_percent <= 25 ) { $strength++; } //get the numbers in the password preg_match_all( '/[0-9]/', $password, $numbers ); //Prevent the addition of a single number to the beginning/end of the password from increasing the strength. if ( is_numeric( substr( $password, 0, 1 ) ) == true ) { array_pop( $numbers[0] ); } if ( is_numeric( substr( $password, -1, 1 ) ) == true ) { array_pop( $numbers[0] ); } $strength += ( count( $numbers[0] ) * 2 ); //check for special chars preg_match_all( '/[|!@#$%&*\/=?,;.:\-_+~^\\\]/', $password, $specialchars ); $strength += ( count( $specialchars[0] ) * 3 ); //get the number of unique chars $chars = str_split( $password ); $num_unique_chars = count( array_unique( $chars ) ); $unique_percent = ( ( $num_unique_chars / strlen( $password ) ) * 100 ); $strength += ( $num_unique_chars * 2 ); //If the password consists of duplicate or consecutive chars, make it the lowest strength. //This should help prevent 12345, or abcde passwords. if ( $unique_percent <= 20 ) { $strength = 1; } if ( $duplicate_percent >= 50 ) { $strength = 1; } if ( $consecutive_percent >= 60 ) { $strength = 1; } Debug::Text( 'Duplicate: Chars: ' . $duplicate_chars . ' Percent: ' . $duplicate_percent . ' Consec: Chars: ' . $consecutive_chars . ' Percent: ' . $consecutive_percent . ' Unique: Chars: ' . $num_unique_chars . ' Percent: ' . $unique_percent, __FILE__, __LINE__, __METHOD__, 10 ); //Check for dictionary word, if its just a dictionary word make it the lowest strength. if ( function_exists( 'pspell_new' ) ) { //If no aspell dictionary is installed, you might see: WARNING(2): pspell_new(): PSPELL couldn't open the dictionary. reason: No word lists can be found for the language "en". // On Centos this can fixed by: yum install aspell-en $pspell_config = @pspell_config_create( 'en' ); $pspell_link = @pspell_new_config( $pspell_config ); if ( $pspell_link != false ) { if ( pspell_check( $pspell_link, $password ) !== false ) { Debug::Text( 'Matches dictionary word exactly: ' . $password, __FILE__, __LINE__, __METHOD__, 10 ); $strength = 1; } if ( pspell_check( $pspell_link, substr( $password, 1 ) ) !== false ) { Debug::Text( 'Matches dictionary word after 1st char is dropped: ' . $password, __FILE__, __LINE__, __METHOD__, 10 ); $strength = 1; } if ( pspell_check( $pspell_link, substr( $password, 0, -1 ) ) !== false ) { Debug::Text( 'Matches dictionary word after last char is dropped: ' . $password, __FILE__, __LINE__, __METHOD__, 10 ); $strength = 1; } if ( pspell_check( $pspell_link, substr( substr( $password, 1 ), 0, -1 ) ) !== false ) { Debug::Text( 'Matches dictionary word after first and last char is dropped: ' . $password, __FILE__, __LINE__, __METHOD__, 10 ); $strength = 1; } } else { Debug::Text( 'WARNING: pspell extension is installed but not functioning, is a dictionary installed?', __FILE__, __LINE__, __METHOD__, 10 ); } } else { Debug::Text( 'WARNING: pspell extension is not enabled...', __FILE__, __LINE__, __METHOD__, 10 ); } //strength is a number 1-10; $strength = $strength > 99 ? 99 : $strength; $strength = floor( ( ( $strength / 10 ) + 1 ) ); Debug::Text( 'Strength: ' . $strength, __FILE__, __LINE__, __METHOD__, 10 ); return $strength; } } ?>