UsernameAvailable.php 1.0 KB

12345678910111213141516171819202122232425262728293031323334
  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. *
  11. * @see HTML_QuickForm_Rule
  12. *
  13. * @param string $username Wanted username
  14. * @param string $current_username
  15. *
  16. * @return bool True if username is available
  17. */
  18. public function validate($username, $current_username = null)
  19. {
  20. $user_table = Database::get_main_table(TABLE_MAIN_USER);
  21. $username = Database::escape_string($username);
  22. $current_username = Database::escape_string($current_username);
  23. $sql = "SELECT * FROM $user_table WHERE username = '$username'";
  24. if (!is_null($current_username)) {
  25. $sql .= " AND username != '$current_username'";
  26. }
  27. $res = Database::query($sql);
  28. $number = Database::num_rows($res);
  29. return $number == 0;
  30. }
  31. }