friendly_register.module 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /**
  3. * @file
  4. * Primary logic for the friendly_register module that allows for checking of username and email.
  5. */
  6. define('FRIENDLY_REGISTER_MAX_HITS', 300);
  7. define('FRIENDLY_REGISTER_EXPIRES', 86400);
  8. /**
  9. * Implements hook_menu().
  10. */
  11. function friendly_register_menu() {
  12. $items['ajax/check-email'] = array(
  13. 'page callback' => 'friendly_register_check_email',
  14. 'page arguments' => array(2),
  15. 'access callback' => TRUE,
  16. 'type' => MENU_CALLBACK,
  17. );
  18. $items['ajax/check-user'] = array(
  19. 'page callback' => 'friendly_register_check_user',
  20. 'page arguments' => array(2),
  21. 'access callback' => TRUE,
  22. 'type' => MENU_CALLBACK,
  23. );
  24. return $items;
  25. }
  26. /**
  27. * Implements hook_permission().
  28. */
  29. function friendly_register_permission() {
  30. return array(
  31. 'ignore flood' => array(
  32. 'title' => t('Ignore Flood Checking'),
  33. 'description' => t('Allows users to have unlimited checks against friendly register.'),
  34. 'restrict access' => FALSE,
  35. ),
  36. );
  37. }
  38. /**
  39. * Implements hook_cron().
  40. */
  41. function friendly_register_cron() {
  42. $expires = REQUEST_TIME - FRIENDLY_REGISTER_EXPIRES;
  43. db_delete('friendly_register_flood')
  44. ->condition('lasthit', $expires, '<=')
  45. ->execute();
  46. }
  47. /**
  48. * Implements hook_FORM_form_alter().
  49. */
  50. function friendly_register_form_user_register_form_alter(&$form, &$form_state, $form_id) {
  51. $path = drupal_get_path('module', 'friendly_register');
  52. drupal_add_js($path . '/js/friendly_register.js', 'file');
  53. drupal_add_css($path . '/css/friendly_register.css');
  54. }
  55. function friendly_register_check_email($address) {
  56. // Check if it is a valid email address. No need to check if it is not
  57. if (valid_email_address($address)) {
  58. drupal_json_output(_friendly_register_check_field('mail', $address));
  59. }
  60. else {
  61. // This flag will return incomplete so the user doesn't see an error
  62. // if they are just starting to enter in their email address
  63. drupal_json_output(array('available' => 'incomplete'));
  64. }
  65. }
  66. function friendly_register_check_user($username) {
  67. drupal_json_output(_friendly_register_check_field('name', $username));
  68. }
  69. function _friendly_register_check_field($field_name, $value) {
  70. if (_friendly_register_check_flood()) {
  71. $result = db_query("SELECT uid FROM {users} WHERE " . $field_name . " = :value", array(':value' => $value))->fetchField();
  72. return array('available' => ($result == NULL));
  73. }
  74. else {
  75. return array('flood' => TRUE);
  76. }
  77. }
  78. function _friendly_register_check_flood() {
  79. if (user_access('ignore flood')) {
  80. return TRUE;
  81. }
  82. else {
  83. $ip = ip_address();
  84. $q = 'SELECT hits FROM {friendly_register_flood} WHERE ip = :ip';
  85. $hits = db_query($q, array(':ip' => $ip))->fetchField();
  86. if ($hits == NULL) {
  87. $id = db_insert('friendly_register_flood')
  88. ->fields(array(
  89. 'ip' => $ip,
  90. 'hits' => 1,
  91. 'lasthit' => REQUEST_TIME,
  92. ))->execute();
  93. }
  94. else {
  95. db_update('friendly_register_flood')
  96. ->expression('hits', 'hits + 1')
  97. ->expression('lasthit', REQUEST_TIME)
  98. ->condition('ip', $ip)
  99. ->execute();
  100. }
  101. return $hits < FRIENDLY_REGISTER_MAX_HITS;
  102. }
  103. }