|
1 |
| -<?php |
2 |
| - |
3 |
| -/* |
4 |
| - * Plugin Name: Minimum Password Strength |
5 |
| - * Description: Enforce a specific password strength. Uses the same strength calculations as the WordPress password strength meter |
6 |
| - * Version: 1.2.0 |
7 |
| - * Plugin URI: http://wordpress.org/extend/plugins/minimum-password-strength/ |
8 |
| - * Author: Will Anderson and Tony Ferrell |
9 |
| - * Author URI: http://codeawhile.com/ |
10 |
| - */ |
11 |
| - |
12 |
| - class Minimum_Password_Strength { |
13 |
| - |
14 |
| - const STRENGTH_KEY = 'minimum_password_strength'; |
15 |
| - const PASS_LENGTH = 4; |
16 |
| - const SHORT_PASS = 1; |
17 |
| - const BAD_PASS = 2; |
18 |
| - const GOOD_PASS = 3; |
19 |
| - const STRONG_PASS = 4; |
20 |
| - const MISMATCH = 5; |
21 |
| - const DEFAULT_REQUIRED_STRENGTH = self::GOOD_PASS; |
22 |
| - |
23 |
| - public static $strengths = array( |
24 |
| - 2 => 'Weak', |
25 |
| - 3 => 'Medium', |
26 |
| - 4 => 'Strong', |
27 |
| - ); |
28 |
| - |
29 |
| - public static function start() { |
30 |
| - add_action( 'user_profile_update_errors', array( __CLASS__, 'check_password_strength' ) ); |
31 |
| - add_action( 'admin_menu', array( __CLASS__, 'add_menu' ) ); |
32 |
| - add_action( 'validate_password_reset', array( __CLASS__, 'check_password_strength' ) ); |
33 |
| - } |
34 |
| - |
35 |
| - public static function check_password_strength( $errors ) { |
36 |
| - $password1 = isset( $_POST['pass1'] ) ? $_POST['pass1'] : ''; |
37 |
| - $password2 = isset( $_POST['pass2'] ) ? $_POST['pass2'] : ''; |
38 |
| - if ( isset( $_POST['user_id'] ) ) { |
39 |
| - // Editing user profile page |
40 |
| - $user_id = intval( $_POST['user_id'] ); |
41 |
| - $user = get_userdata( $user_id ); |
42 |
| - $username = $user->user_login; |
43 |
| - } else { |
44 |
| - // Creating a new user |
45 |
| - $username = $_POST['user_login']; |
46 |
| - } |
47 |
| - |
48 |
| - if ( empty( $password1 ) && empty( $password2 ) ) { |
49 |
| - return; |
50 |
| - } |
51 |
| - |
52 |
| - $strength = self::get_password_strength( $username, $password1, $password2 ); |
53 |
| - |
54 |
| - $required_strength = get_option( self::STRENGTH_KEY, 3 ); |
55 |
| - |
56 |
| - if ( self::MISMATCH == $strength ) { |
57 |
| - $errors->add( 'mismatched-password', 'The passwords you entered do not match', array( 'form-field' => 'pass1' ) ); |
58 |
| - } elseif ( $strength < $required_strength ) { |
59 |
| - $errors->add( 'weak-password', sprintf( __( 'You must choose a "%s" password', 'minimum-password-strength' ), self::$strengths[$required_strength] ), array( 'form-field' => 'pass1' ) ); |
60 |
| - } |
61 |
| - } |
62 |
| - |
63 |
| - public static function add_menu() { |
64 |
| - add_options_page( __( 'Minimum Password Strength', 'minimum-password-strength' ), __( 'Password Strength', 'minimum-password-strength' ), 'manage_options', __FILE__, array( __CLASS__, 'show_settings_page' ) ); |
65 |
| - } |
66 |
| - |
67 |
| - public static function show_settings_page() { |
68 |
| - if ( isset( $_POST['submit'] ) && isset( $_POST['_wpnonce'] ) && |
69 |
| - wp_verify_nonce( $_POST['_wpnonce'], 'update_minimum_password_strength' ) ) { |
70 |
| - $strength = intval( $_POST['strength'] ); |
71 |
| - update_option( self::STRENGTH_KEY, $strength ); |
72 |
| - } |
73 |
| - |
74 |
| - $required_strength = self::get_required_strength(); |
75 |
| - $options = self::$strengths; |
76 |
| - |
77 |
| - include plugin_dir_path( __FILE__ ) . 'views/settings.php'; |
78 |
| - } |
79 |
| - |
80 |
| - public static function get_required_strength() { |
81 |
| - return get_option( self::STRENGTH_KEY, self::DEFAULT_REQUIRED_STRENGTH ); |
82 |
| - } |
83 |
| - |
84 |
| - public static function get_password_strength( $username, $password1, $password2 ) { |
85 |
| - $symbolSize = 0; |
86 |
| - |
87 |
| - // password 1 != password 2 |
88 |
| - if ( $password1 != $password2 ) |
89 |
| - return self::MISMATCH; |
90 |
| - |
91 |
| - //password < self::PASS_LENGTH |
92 |
| - if ( strlen( $password1 ) < self::PASS_LENGTH ) |
93 |
| - return self::SHORT_PASS; |
94 |
| - |
95 |
| - //password1 == username |
96 |
| - if ( strtolower( $password1 ) == strtolower( $username ) ) |
97 |
| - return self::BAD_PASS; |
98 |
| - |
99 |
| - if ( preg_match( '/[0-9]/', $password1 ) ) |
100 |
| - $symbolSize += 10; |
101 |
| - if ( preg_match( '/[a-z]/', $password1 ) ) |
102 |
| - $symbolSize += 26; |
103 |
| - if ( preg_match( '/[A-Z]/', $password1 ) ) |
104 |
| - $symbolSize += 26; |
105 |
| - if ( preg_match( '/[^a-zA-Z0-9]/', $password1 ) ) |
106 |
| - $symbolSize += 31; |
107 |
| - |
108 |
| - $natLog = log( pow( $symbolSize, strlen( $password1 ) ) ); |
109 |
| - $score = $natLog / log( 2 ); |
110 |
| - |
111 |
| - if ( $score < 40 ) |
112 |
| - return self::BAD_PASS; |
113 |
| - |
114 |
| - if ( $score < 56 ) |
115 |
| - return self::GOOD_PASS; |
116 |
| - |
117 |
| - return self::STRONG_PASS; |
118 |
| - } |
119 |
| - } |
120 |
| - |
121 |
| - Minimum_Password_Strength::start(); |
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * Plugin Name: Minimum Password Strength |
| 5 | + * Description: Enforce a specific password strength. Hides the option to ignore weak passwords. |
| 6 | + * Version: 2.0.0 |
| 7 | + * Plugin URI: http://wordpress.org/extend/plugins/minimum-password-strength/ |
| 8 | + * Author: Will Anderson, Tony Ferrell and Ryan Hellyer |
| 9 | + * Author URI: http://codeawhile.com/ |
| 10 | + */ |
| 11 | + |
| 12 | + |
| 13 | +add_action( 'admin_enqueue_scripts', 'minimum_password_strength' ); |
| 14 | +/** |
| 15 | + * Hiding the "Confirm use of weak password" checkbox from view. |
| 16 | + */ |
| 17 | +function minimum_password_strength() { |
| 18 | + wp_add_inline_style( 'admin-menu', '.pw-weak {display: none !important;}' ); |
| 19 | +} |
0 commit comments