UsernameAvailable.php 986 B

1234567891011121314151617181920212223242526272829
  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. $user_table = Database::get_main_table(TABLE_MAIN_USER);
  17. $username = Database::escape_string($username);
  18. $current_username = Database::escape_string($current_username);
  19. $sql = "SELECT * FROM $user_table WHERE username = '$username'";
  20. if (!is_null($current_username)) {
  21. $sql .= " AND username != '$current_username'";
  22. }
  23. $res = Database::query($sql);
  24. $number = Database::num_rows($res);
  25. return $number == 0;
  26. }
  27. }