You are here

function user_email_verification_cron in User email verification 7

Same name and namespace in other branches
  1. 8 user_email_verification.module \user_email_verification_cron()

Implements hook_cron().

File

./user_email_verification.module, line 151
This module allows you to have e-mail verification and in meanwhile allowing the users to type their own passwords. If they do not verify their accounts in a certain time interval the user will be blocked.

Code

function user_email_verification_cron() {
  $interval = variable_get('user_email_verification_validate_interval', 86400);
  $num_reminders = variable_get('user_email_verification_num_reminders', 0);
  $reminder_interval = $interval / ($num_reminders + 1);
  $skip_roles = variable_get('user_email_verification_roles', array());
  $skip_roles = array_filter($skip_roles);

  // Select those that need to be blocked.
  $query = db_select('user_email_verification', 'uev');
  $query
    ->join('users', 'u', 'uev.uid = u.uid');
  if (!empty($skip_roles)) {
    $query
      ->leftJoin('users_roles', 'ur', 'ur.uid = uev.uid');
    $query
      ->distinct('uev.uid');
    $or = db_or()
      ->condition('ur.rid', array_keys($skip_roles), 'NOT IN')
      ->isNull('ur.rid');

    // normal registered users don't have an entry in the users_roles table.
    $query
      ->condition($or);
  }
  $result = $query
    ->fields('u', array(
    'uid',
  ))
    ->condition('uev.verified', 0, '=')
    ->condition('u.uid', 1, '>')
    ->condition('uev.last_reminder', REQUEST_TIME - $reminder_interval, '<')
    ->condition('uev.reminders', $num_reminders, '>=')
    ->execute();
  $queue = DrupalQueue::get('user_email_verification_tasks');
  foreach ($result as $account) {
    $queue
      ->createItem($account->uid);
  }

  // Select those that need to be sent a reminder.
  $query = db_select('user_email_verification', 'uev');
  $query
    ->join('users', 'u', 'uev.uid = u.uid');
  if (!empty($skip_roles)) {
    $query
      ->leftJoin('users_roles', 'ur', 'ur.uid = uev.uid');
    $query
      ->distinct('uev.uid');
    $or = db_or()
      ->condition('ur.rid', array_keys($skip_roles), 'NOT IN')
      ->isNull('ur.rid');

    // normal registered users don't have an entry in the users_roles table.
    $query
      ->condition($or);
  }
  $result = $query
    ->fields('u', array(
    'uid',
  ))
    ->condition('uev.verified', 0, '=')
    ->condition('u.uid', 1, '>')
    ->condition('uev.last_reminder', REQUEST_TIME - $reminder_interval, '<')
    ->condition('uev.reminders', $num_reminders, '<')
    ->execute();
  $queue = DrupalQueue::get('user_email_verification_reminders');
  foreach ($result as $account) {
    $queue
      ->createItem($account->uid);
  }
  if (variable_get('user_email_verification_extended_enable', 0)) {

    // Delete accounts which have not verified their e-mail addresses
    // within extended time period. Similar to blocking users, but doesn't
    // care about reminder settings.
    $extended_interval = variable_get('user_email_verification_extended_validate_interval', 86400);
    $skip_roles = variable_get('user_email_verification_roles', array());
    $skip_roles = array_filter($skip_roles);

    // Select those that need to be blocked.
    $query = db_select('user_email_verification', 'uev');
    $query
      ->join('users', 'u', 'uev.uid = u.uid');
    if (!empty($skip_roles)) {
      $query
        ->leftJoin('users_roles', 'ur', 'ur.uid = uev.uid');
      $query
        ->distinct('uev.uid');
      $or = db_or()
        ->condition('ur.rid', array_keys($skip_roles), 'NOT IN')
        ->isNull('ur.rid');

      // normal registered users don't have an entry in the users_roles table.
      $query
        ->condition($or);
    }
    $result = $query
      ->fields('u', array(
      'uid',
    ))
      ->condition('uev.verified', 0, '=')
      ->condition('u.uid', 1, '>')
      ->condition('uev.last_reminder', REQUEST_TIME - $extended_interval, '<')
      ->execute();
    $queue = DrupalQueue::get('user_email_verification_extended');
    foreach ($result as $account) {
      $queue
        ->createItem($account->uid);
    }
  }
}