UsernameAvailable.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. // $Id: UsernameAvailable.php 10190 2006-11-24 00:23:20Z pcool $
  3. /*
  4. ==============================================================================
  5. Dokeos - elearning and course management software
  6. Copyright (c) 2004-2005 Dokeos S.A.
  7. Copyright (c) Bart Mollet, Hogeschool Gent
  8. For a full list of contributors, see "credits.txt".
  9. The full license can be read in "license.txt".
  10. This program is free software; you can redistribute it and/or
  11. modify it under the terms of the GNU General Public License
  12. as published by the Free Software Foundation; either version 2
  13. of the License, or (at your option) any later version.
  14. See the GNU General Public License for more details.
  15. Contact address: Dokeos, 44 rue des palais, B-1030 Brussels, Belgium
  16. Mail: info@dokeos.com
  17. ==============================================================================
  18. */
  19. require_once 'HTML/QuickForm/Rule.php';
  20. /**
  21. * QuickForm rule to check if a username is available
  22. */
  23. class HTML_QuickForm_Rule_UsernameAvailable extends HTML_QuickForm_Rule
  24. {
  25. /**
  26. * Function to check if a username is available
  27. * @see HTML_QuickForm_Rule
  28. * @param string $username Wanted username
  29. * @param string $current_username
  30. * @return boolean True if username is available
  31. */
  32. function validate($username, $current_username = null) {
  33. $user_table = Database::get_main_table(TABLE_MAIN_USER);
  34. $username = Database::escape_string($username);
  35. $current_username = Database::escape_string($current_username);
  36. $sql = "SELECT * FROM $user_table WHERE username = '$username'";
  37. if (!is_null($current_username)) {
  38. $sql .= " AND username != '$current_username'";
  39. }
  40. $res = Database::query($sql);
  41. $number = Database::num_rows($res);
  42. return $number == 0;
  43. }
  44. }