UsernameAvailable.php 912 B

123456789101112131415161718192021222324252627282930
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * QuickForm rule to check if a username is available
  5. */
  6. class HTML_QuickForm_Rule_UsernameAvailable extends HTML_QuickForm_Rule
  7. {
  8. /**
  9. * Function to check if a username is available
  10. * @see HTML_QuickForm_Rule
  11. * @param string $username Wanted username
  12. * @param string $current_username
  13. * @return boolean True if username is available
  14. */
  15. function validate($username, $current_username = null)
  16. {
  17. $user_table = Database::get_main_table(TABLE_MAIN_USER);
  18. $username = Database::escape_string($username);
  19. $current_username = Database::escape_string($current_username);
  20. $sql = "SELECT * FROM $user_table WHERE username = '$username'";
  21. if (!is_null($current_username)) {
  22. $sql .= " AND username != '$current_username'";
  23. }
  24. $res = Database::query($sql);
  25. $number = Database::num_rows($res);
  26. return $number == 0;
  27. }
  28. }