@@ -341,8 +341,6 @@ public function generateTelegramCode()
341
341
{
342
342
$ this ->telegram_code = md5 (time ().$ this ->username );
343
343
}
344
-
345
-
346
344
347
345
/**
348
346
* Find a user by email
@@ -407,20 +405,30 @@ public static function getUsersByStatus($status_id)
407
405
}
408
406
409
407
/**
410
- * Generating a new password
411
- * @param int $length - new password length
412
- * @return string new password
408
+ * Generates a random password.
409
+ *
410
+ * @param int $length The desired length of the password. Default is 10.
411
+ * @param bool $use_special Whether to include special characters in the password. Default is false.
412
+ * @return string The generated password.
413
413
*/
414
- public static function genPassword ($ length = 10 )
414
+ public static function genPassword ($ length = 10 , $ use_special = false )
415
415
{
416
- $ chars = "qazxswedcvfrtgbnhyujmkiolp1234567890QAZXSWEDCVFRTGBNHYUJMKIOLP!@#$%&*? " ;
417
- $ length = intval ($ length );
418
- $ size = strlen ($ chars ) - 1 ;
419
- $ password = "" ;
420
- while ($ length --) {
421
- $ password .= $ chars [rand (0 , $ size )];
422
- }
416
+ $ lowercase = range ('a ' , 'z ' );
417
+ $ uppercase = range ('A ' , 'Z ' );
418
+ $ digits = range (0 , 9 );
423
419
420
+ // If special characters are to be used, create an array of special characters
421
+ $ special = $ use_special ? ['! ' , '@ ' , '# ' , '$ ' , '% ' , '^ ' , '& ' , '* ' ] : [];
422
+
423
+ // Merge all character arrays into one array for password generation
424
+ $ chars = array_merge ($ lowercase , $ uppercase , $ digits , $ special );
425
+ $ password = '' ;
426
+
427
+ // Loop to generate each character of the password
428
+ for ($ i = 0 ; $ i < intval ($ length ); $ i ++) {
429
+ $ password .= $ chars [random_int (0 , count ($ chars ) - 1 )];
430
+ }
431
+
424
432
return $ password ;
425
433
}
426
434
0 commit comments