Password::generate_zoom_password( int $length = 6, bool $special_chars = false, bool $only_numeric = false )
Generates a random password drawn from the defined set of characters.
Contents
Parameters
- $length
-
(int) (Optional) The length of password to generate.
Default value: 6
- $special_chars
-
(bool) (Optional) Whether to include standard special characters.
Default value: false
- $only_numeric
-
(bool) (Optional) Whether to have only numeric values.
Default value: false
Return
(string) The random password.
Source
File: src/Tribe/Meetings/Zoom/Password.php
public function generate_zoom_password( $length = 6, $special_chars = false, $only_numeric = false ) {
// Build the chars pool.
$chars = '0123456789';
$chars .= ! $only_numeric ? 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' : '';
$chars .= ! $only_numeric && $special_chars ? '@-_*' : '';
$chars_pool = str_split( $chars );// Let's minimize the rounds to fully leverage `array_rand`.
$num_req = min( $length, count( $chars_pool ) );
$password = '';
$current_password_length = 0;
while ( $current_password_length < $length ) {
shuffle( $chars_pool );
$password .= implode( '', array_rand( array_flip( $chars_pool ), $num_req ) );
// Remove duplicates.
$password = preg_replace( '~([' . preg_quote( $chars, '~' ) . '])\1+~', '$1', $password );
$current_password_length = strlen( $password );
}
// Let's make sure the password length is the expected one.
$password = substr( $password, 0, $length );
return (string) $password;
}
Changelog
| Version | Description |
|---|---|
| 1.11.0 | - Modify password generation to always include 1 character from a required set. |
| 1.0.2 | Introduced. |