UsernameAvailable.php 948 B

123456789101112131415161718192021222324252627282930
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. require_once 'HTML/QuickForm/Rule.php';
  4. /**
  5. * QuickForm rule to check if a username is available
  6. */
  7. class HTML_QuickForm_Rule_UsernameAvailable extends HTML_QuickForm_Rule
  8. {
  9. /**
  10. * Function to check if a username is available
  11. * @see HTML_QuickForm_Rule
  12. * @param string $username Wanted username
  13. * @param string $current_username
  14. * @return boolean True if username is available
  15. */
  16. function validate($username, $current_username = null) {
  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. }