'friendly_register_check_email', 'page arguments' => array(2), 'access callback' => TRUE, 'type' => MENU_CALLBACK, ); $items['ajax/check-user'] = array( 'page callback' => 'friendly_register_check_user', 'page arguments' => array(2), 'access callback' => TRUE, 'type' => MENU_CALLBACK, ); return $items; } /** * Implements hook_permission(). */ function friendly_register_permission() { return array( 'ignore flood' => array( 'title' => t('Ignore Flood Checking'), 'description' => t('Allows users to have unlimited checks against friendly register.'), 'restrict access' => FALSE, ), ); } /** * Implements hook_cron(). */ function friendly_register_cron() { $expires = REQUEST_TIME - FRIENDLY_REGISTER_EXPIRES; db_delete('friendly_register_flood') ->condition('lasthit', $expires, '<=') ->execute(); } /** * Implements hook_FORM_form_alter(). */ function friendly_register_form_user_register_form_alter(&$form, &$form_state, $form_id) { $path = drupal_get_path('module', 'friendly_register'); drupal_add_js($path . '/js/friendly_register.js', 'file'); drupal_add_css($path . '/css/friendly_register.css'); } function friendly_register_check_email($address) { // Check if it is a valid email address. No need to check if it is not if (valid_email_address($address)) { drupal_json_output(_friendly_register_check_field('mail', $address)); } else { // This flag will return incomplete so the user doesn't see an error // if they are just starting to enter in their email address drupal_json_output(array('available' => 'incomplete')); } } function friendly_register_check_user($username) { drupal_json_output(_friendly_register_check_field('name', $username)); } function _friendly_register_check_field($field_name, $value) { if (_friendly_register_check_flood()) { $result = db_query("SELECT uid FROM {users} WHERE " . $field_name . " = :value", array(':value' => $value))->fetchField(); return array('available' => ($result == NULL)); } else { return array('flood' => TRUE); } } function _friendly_register_check_flood() { if (user_access('ignore flood')) { return TRUE; } else { $ip = ip_address(); $q = 'SELECT hits FROM {friendly_register_flood} WHERE ip = :ip'; $hits = db_query($q, array(':ip' => $ip))->fetchField(); if ($hits == NULL) { $id = db_insert('friendly_register_flood') ->fields(array( 'ip' => $ip, 'hits' => 1, 'lasthit' => REQUEST_TIME, ))->execute(); } else { db_update('friendly_register_flood') ->expression('hits', 'hits + 1') ->expression('lasthit', REQUEST_TIME) ->condition('ip', $ip) ->execute(); } return $hits < FRIENDLY_REGISTER_MAX_HITS; } }