1 | <?php |
---|
2 | |
---|
3 | function _genpass_default_entropy() { |
---|
4 | return 'abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789!#$%&()*+,-./:;<=>?@[]^_{|}~'; |
---|
5 | } |
---|
6 | |
---|
7 | /** |
---|
8 | * Generate a new password |
---|
9 | * Based on the original D6 user_password function (with more characters) |
---|
10 | * |
---|
11 | * @return a fresh password according to the settings made in /admin/user/settings |
---|
12 | * |
---|
13 | * @see genpass_form_alter() |
---|
14 | */ |
---|
15 | function genpass_password() { |
---|
16 | $pass = ''; |
---|
17 | $length = variable_get('genpass_length', 8); |
---|
18 | $allowable_characters = variable_get('genpass_entropy', _genpass_default_entropy()); |
---|
19 | |
---|
20 | // Zero-based count of characters in the allowable list: |
---|
21 | $len = strlen($allowable_characters) - 1; |
---|
22 | |
---|
23 | // Loop the number of times specified by $length. |
---|
24 | for ($i = 0; $i < $length; $i++) { |
---|
25 | // Each iteration, pick a random character from the |
---|
26 | // allowable string and append it to the password: |
---|
27 | $pass .= $allowable_characters[mt_rand(0, $len)]; |
---|
28 | } |
---|
29 | |
---|
30 | return $pass; |
---|
31 | } |
---|
32 | |
---|
33 | |
---|
34 | /** |
---|
35 | * helper function to find the password item in a form, since its position within |
---|
36 | * the form-array depends on the profile module (account-category) |
---|
37 | */ |
---|
38 | function &genpass_get_pass_item(&$form){ |
---|
39 | if(isset($form['account']['pass'])) return $form['account']['pass']; |
---|
40 | else return $form['pass']; |
---|
41 | } |
---|
42 | |
---|
43 | |
---|
44 | |
---|
45 | |
---|
46 | /** |
---|
47 | * Take care of new form handling (admin and registration forms) |
---|
48 | */ |
---|
49 | function genpass_form_alter(&$form, $form_state, $form_id) { |
---|
50 | switch($form_id) { |
---|
51 | |
---|
52 | case 'user_admin_settings': |
---|
53 | |
---|
54 | $form['registration']['genpass_mode'] = array( |
---|
55 | '#type' => 'radios', |
---|
56 | '#title' => t('Password handling'), |
---|
57 | '#default_value' => variable_get('genpass_mode', 0), |
---|
58 | '#options' => array( |
---|
59 | 0 => t('Users must enter a password on registration (default Drupal behaviour).'), |
---|
60 | 1 => t('Users may enter a password on registration. If not, a random password will be generated.'), |
---|
61 | 2 => t('Users cannot choose a password on registration. It will be generated.') |
---|
62 | ), |
---|
63 | '#description' => t('Choose a password handling mode for new users. Note that for manual user creation by an administrator the second option always applies.') |
---|
64 | ); |
---|
65 | $form['registration']['genpass_length'] = array( |
---|
66 | '#type' => 'textfield', |
---|
67 | '#title' => t('Generated password length'), |
---|
68 | '#default_value' => variable_get('genpass_length', 8), |
---|
69 | '#size' => 2, |
---|
70 | '#maxlength' => 2, |
---|
71 | '#description' => t('Set the length of generated passwords here. Allowed range: 5 to 32') |
---|
72 | ); |
---|
73 | $form['registration']['genpass_entropy'] = array( |
---|
74 | '#type' => 'textfield', |
---|
75 | '#title' => t('Generated password entropy'), |
---|
76 | '#default_value' => variable_get('genpass_entropy', _genpass_default_entropy()), |
---|
77 | '#description' => t('Give a list of possible characters for a generated password. Note that the list must contain at least X different characters where X is defined by the length you have given above.') |
---|
78 | ); |
---|
79 | $form['#validate'][] = 'genpass_user_admin_settings_validate'; |
---|
80 | $form['#submit'][] = 'genpass_user_admin_settings_submit'; |
---|
81 | break; |
---|
82 | |
---|
83 | case 'user_register': |
---|
84 | |
---|
85 | $pass_item =& genpass_get_pass_item($form); |
---|
86 | // alter the user-admin register (no mode decision) |
---|
87 | if ($form['destination']['#value'] == 'admin/user/user/create' && user_access('administer users')) { |
---|
88 | $pass_item['#required'] = FALSE; |
---|
89 | $form['#validate'][] = 'genpass_user_admin_register_validate'; |
---|
90 | } |
---|
91 | else { // alter the user-register form according to the mode |
---|
92 | switch(variable_get('genpass_mode',0)){ |
---|
93 | case 2: |
---|
94 | $pass_item['#type'] = 'hidden'; |
---|
95 | case 1: |
---|
96 | $pass_item['#required'] = FALSE; |
---|
97 | $form['#validate'][] = 'genpass_register_validate'; |
---|
98 | } |
---|
99 | } |
---|
100 | |
---|
101 | break; |
---|
102 | } |
---|
103 | |
---|
104 | } |
---|
105 | |
---|
106 | |
---|
107 | function genpass_user_admin_settings_validate($form, &$form_state) { |
---|
108 | $length = $form_state['values']['genpass_length']; |
---|
109 | if(!is_numeric($length) || $length < 5 || $length > 32){ |
---|
110 | form_set_error('genpass_length', t('The length of a generated password must be between 5 and 32')); return; |
---|
111 | } |
---|
112 | $entropy = $form_state['values']['genpass_entropy']; |
---|
113 | $chars = array_unique(preg_split('//', $entropy, -1, PREG_SPLIT_NO_EMPTY)); |
---|
114 | if(count($chars) < $length){ |
---|
115 | form_set_error('genpass_entropy', t('The list of possible characters is not long or unique enough')); return; |
---|
116 | } |
---|
117 | return $form; |
---|
118 | } |
---|
119 | |
---|
120 | |
---|
121 | function genpass_user_admin_settings_submit($form, &$form_state) { |
---|
122 | $mode = $form_state['values']['genpass_mode']; |
---|
123 | if($mode >= 0 && $mode < 3) variable_set('genpass_mode', $mode); |
---|
124 | |
---|
125 | $length = $form_state['values']['genpass_length']; |
---|
126 | if(is_numeric($length) && $length >= 5 && $length <= 32) variable_set('genpass_length', $length); |
---|
127 | |
---|
128 | $entropy = $form_state['values']['genpass_entropy']; |
---|
129 | $chars = array_unique(preg_split('//', $entropy, -1, PREG_SPLIT_NO_EMPTY)); |
---|
130 | if(count($chars) >= $length) variable_set('genpass_entropy',$entropy); |
---|
131 | } |
---|
132 | |
---|
133 | |
---|
134 | |
---|
135 | function genpass_register_validate($form, &$form_state) { |
---|
136 | if (empty($form_state['values']['pass']) && !form_get_errors() ) { |
---|
137 | $pass = genpass_password(); |
---|
138 | $pass_item =& genpass_get_pass_item($form); |
---|
139 | form_set_value($pass_item, $pass, $form_state); |
---|
140 | drupal_set_message(t('Generated password')); |
---|
141 | } |
---|
142 | return $form; |
---|
143 | } |
---|
144 | |
---|
145 | |
---|
146 | function genpass_user_admin_register_validate($form, &$form_state) { |
---|
147 | if (empty($form_state['values']['pass']) && !form_get_errors() ) { |
---|
148 | $pass = genpass_password(); |
---|
149 | $pass_item =& genpass_get_pass_item($form); |
---|
150 | form_set_value($pass_item, $pass, $form_state); |
---|
151 | drupal_set_message(t('Generated password is ').$pass); |
---|
152 | } |
---|
153 | return $form; |
---|
154 | } |
---|
155 | |
---|
156 | |
---|
157 | |
---|