You are here

function inactive_user_cron in Inactive User 6

Same name and namespace in other branches
  1. 5 inactive_user.module \inactive_user_cron()
  2. 7 inactive_user.module \inactive_user_cron()

Implementation of hook_cron().

1 call to inactive_user_cron()
InactiveUserTest::checkInactiveAccounts in ./inactive_user.test
Perform inactivity validation

File

./inactive_user.module, line 237
The inactive user module controls inactive users.

Code

function inactive_user_cron() {
  if (time() - variable_get('inactive_user_timestamp', '0') >= 86100) {

    // Only check once every almost-day, so we slide around the clock and don't overload the server.
    variable_set('inactive_user_timestamp', time());
    $user_list = '';

    // reset notifications if recent user activity
    $users = db_fetch_object(db_query('SELECT uid FROM {inactive_users} WHERE uid <> 1'));
    if ($users) {
      foreach ($users as $uid) {
        $u = db_fetch_object(db_query('SELECT access, name FROM {users} WHERE uid = %d', $uid));
        if ($u->access > time() - 604800) {

          // user activity in last week, remove from inactivity table
          db_query('DELETE FROM {inactive_users} WHERE uid = %d', $uid);
          watchdog('user', 'recent user activity: %user removed from inactivity list', array(
            '%user' => $u->name,
          ), WATCHDOG_NOTICE, l(t('edit user'), "user/{$uid}/edit", array(
            'query' => array(
              'destination' => 'admin/user/user',
            ),
          )));
        }
      }
    }

    // notify administrator of inactive user accounts
    if ($notify_time = variable_get('inactive_user_notify_admin', 0)) {
      $result = db_query('SELECT uid, name, mail, access, created FROM {users} WHERE ((access <> 0 AND login <> 0 AND access < (%d - %d)) OR (login = 0 AND created < (%d - %d))) AND uid <> 1', time(), $notify_time, time(), $notify_time);
      while ($user = db_fetch_object($result)) {
        if ($user->uid && !db_fetch_object(db_query('SELECT uid FROM {inactive_users} WHERE uid = %d AND notified_admin = 1', $user->uid)) && $user->created < time() - $notify_time) {
          db_query('UPDATE {inactive_users} SET notified_admin = 1 WHERE uid = %d', $user->uid);
          if (!db_affected_rows()) {

            // must create a new row
            @db_query('INSERT INTO {inactive_users} (uid, notified_admin) VALUES (%d, 1)', $user->uid);
          }
          $user_list .= "{$user->name} ({$user->mail}) last active on " . format_date($user->access, 'large') . ".\n";
        }
      }
      if (isset($user_list)) {
        _inactive_user_mail(t('[@sitename] Inactive users', array(
          '@sitename' => variable_get('site_name', 'drupal'),
        )), _inactive_user_mail_text('notify_admin_text'), $notify_time, NULL, $user_list);
        unset($user_list);
      }
    }

    // notify users that their account has been inactive
    if ($notify_time = variable_get('inactive_user_notify', 0)) {
      $result = db_query('SELECT * FROM {users} WHERE ((access <> 0 AND login <> 0 AND access < (%d - %d)) OR (login = 0 AND created < (%d - %d))) AND status <> 0 AND uid <> 1', time(), $notify_time, time(), $notify_time);
      while ($user = db_fetch_object($result)) {
        if ($user->uid && !db_fetch_object(db_query('SELECT uid FROM {inactive_users} WHERE notified_user = 1 AND uid = %d', $user->uid)) && $user->created < time() - $notify_time) {
          db_query('UPDATE {inactive_users} SET notified_user = 1 WHERE uid = %d', $user->uid);
          if (!db_affected_rows()) {
            @db_query('INSERT INTO {inactive_users} (uid, notified_user) VALUES (%d, 1)', $user->uid);
          }
          _inactive_user_mail(t('[@sitename] Account inactivity', array(
            '@sitename' => variable_get('site_name', 'drupal'),
          )), variable_get('inactive_user_notify_text', _inactive_user_mail_text('notify_text')), $notify_time, $user, NULL);
          watchdog('user', 'user %user notified of inactivity', array(
            '%user' => $user->name,
          ), WATCHDOG_INFO, l(t('edit user'), "user/{$user->uid}/edit", array(
            'query' => array(
              'destination' => 'admin/user/user',
            ),
          )));
        }
      }
    }

    // warn users when they are about to be blocked
    if (($warn_time = variable_get('inactive_user_auto_block_warn', 0)) && ($block_time = variable_get('inactive_user_auto_block', 0))) {
      $result = db_query('SELECT * FROM {users} WHERE ((access <> 0 AND login <> 0 AND access < (%d - %d)) OR (login = 0 AND created < (%d - %d))) AND status <> 0 AND uid <> 1', time(), $warn_time, time(), $warn_time);
      while ($user = db_fetch_object($result)) {
        if ($user->uid && !db_fetch_object(db_query('SELECT uid FROM {inactive_users} WHERE uid = %d AND warned_user_block_timestamp > 0', $user->uid)) && $user->created < time() - $warn_time) {
          db_query('UPDATE {inactive_users} SET warned_user_block_timestamp = %d WHERE uid = %d', time() + $warn_time, $user->uid);
          if (!db_affected_rows()) {
            @db_query('INSERT INTO {inactive_users} (uid, warned_user_block_timestamp) VALUES (%d, %d)', $user->uid, time() + $warn_time);
          }
          _inactive_user_mail(t('[@sitename] Account inactivity', array(
            '@sitename' => variable_get('site_name', 'drupal'),
          )), variable_get('inactive_user_block_warn_text', _inactive_user_mail_text('block_warn_text')), $warn_time, $user, NULL);
          watchdog('user', 'user %user warned will be blocked due to inactivity', array(
            '%user' => $user->name,
          ), WATCHDOG_NOTICE, l(t('edit user'), "user/{$user->uid}/edit", array(
            'query' => array(
              'destination' => 'admin/user/user',
            ),
          )));
        }
      }
    }

    // automatically block users
    if ($block_time = variable_get('inactive_user_auto_block', 0)) {
      $result = db_query('SELECT * FROM {users} WHERE ((access <> 0 AND login <> 0 AND access < (%d - %d)) OR (login = 0 AND created < (%d - %d))) AND status <> 0 AND uid <> 1', time(), $block_time, time(), $block_time);
      while ($user = db_fetch_object($result)) {

        // don't block user yet if we sent a warning and it hasn't expired
        if ($user->uid && db_fetch_object(db_query('SELECT uid FROM {inactive_users} WHERE uid = %d AND warned_user_block_timestamp < %d', $user->uid, time())) && $user->created < time() - $block_time) {
          db_query('UPDATE {users} SET status = 0 WHERE uid = %d', $user->uid);

          // notify user
          if (variable_get('inactive_user_notify_block', 0)) {
            if (!db_fetch_object(db_query('SELECT uid FROM {inactive_users} WHERE uid = %d AND notified_user_block = 1', $user->uid))) {
              db_query('UPDATE {inactive_users} SET notified_user_block = 1 WHERE uid = %d', $user->uid);
              if (!db_affected_rows()) {
                @db_query('INSERT INTO {inactive_users} (uid, notified_user_block) VALUES (%d, 1)', $user->uid);
              }
              _inactive_user_mail(t('[@sitename] Account blocked due to inactivity', array(
                '@sitename' => variable_get('site_name', 'drupal'),
              )), variable_get('inactive_user_block_notify_text', _inactive_user_mail_text('block_notify_text')), $block_time, $user, NULL);
              watchdog('user', 'user %user blocked due to inactivity', array(
                '%user' => $user->name,
              ), WATCHDOG_NOTICE, l(t('edit user'), "user/{$user->uid}/edit", array(
                'query' => array(
                  'destination' => 'admin/user/user',
                ),
              )));
            }
          }

          // notify admin
          if (variable_get('inactive_user_notify_block_admin', 0)) {
            if (!db_fetch_object(db_query('SELECT uid FROM {inactive_users} WHERE uid = %d and notified_admin_block = 1', $user->uid))) {
              db_query('UPDATE {inactive_users} SET notified_admin_block = 1 WHERE uid = %d', $user->uid);
              if (!db_affected_rows()) {
                @db_query('INSERT INTO {inactive_users} (uid, notified_admin_block) VALUES(%d, 1)', $user->uid);
              }
              $user_list .= "{$user->name} ({$user->mail}) last active on " . format_date($user->access, 'large') . ".\n";
            }
          }
        }
        if ($user_list) {
          _inactive_user_mail(t('[@sitename] Blocked users', array(
            '@sitename' => variable_get('site_name', 'drupal'),
          )), _inactive_user_mail_text('block_notify_admin_text'), $block_time, NULL, $user_list);
          unset($user_list);
        }
      }
    }

    // warn users when they are about to be deleted
    if (($warn_time = variable_get('inactive_user_auto_delete_warn', 0)) && ($delete_time = variable_get('inactive_user_auto_delete', 0))) {
      $result = db_query('SELECT * FROM {users} WHERE ((access <> 0 AND login <> 0 AND access < (%d - %d)) OR (login = 0 AND created < (%d - %d))) AND uid <> 1', time(), $warn_time, time(), $warn_time);
      while ($user = db_fetch_object($result)) {
        if ($user->uid && !db_fetch_object(db_query('SELECT uid FROM {inactive_users} WHERE uid = %d AND warned_user_delete_timestamp > 0', $user->uid)) && $user->created < time() - $warn_time) {
          if (variable_get('inactive_user_preserve_content', 1) && _inactive_user_with_content($user->uid)) {
            $protected = 1;
          }
          else {
            $protected = 0;
          }
          db_query('UPDATE {inactive_users} SET warned_user_delete_timestamp = %d AND protected = %d WHERE uid = %d', time() + $warn_time, $protected, $user->uid);
          if (!db_affected_rows()) {
            @db_query('INSERT INTO {inactive_users} (uid, warned_user_delete_timestamp, protected) VALUES (%d, %d, %d)', $user->uid, time() + $warn_time, $protected);
          }
          if (!$protected) {
            _inactive_user_mail(t('[@sitename] Account inactivity', array(
              '@sitename' => variable_get('site_name', 'drupal'),
            )), variable_get('inactive_user_delete_warn_text', _inactive_user_mail_text('delete_warn_text')), $warn_time, $user, NULL);
            watchdog('user', 'user %user warned will be deleted due to inactivity', array(
              '%user' => $user->mail,
            ), WATCHDOG_NOTICE, l(t('edit user'), "user/{$user->uid}/edit", array(
              'query' => array(
                'destination' => 'admin/user/user',
              ),
            )));
          }
        }
      }
    }

    // automatically delete users
    if ($delete_time = variable_get('inactive_user_auto_delete', 0)) {
      $result = db_query('SELECT * FROM {users} WHERE ((access <> 0 AND login <> 0 AND access < (%d - %d)) OR (login = 0 AND created < (%d - %d))) AND uid <> 1', time(), $delete_time, time(), $delete_time);
      while ($user = db_fetch_object($result)) {
        if ($user->uid && (variable_get('inactive_user_auto_delete_warn', 0) && db_fetch_object(db_query('SELECT uid FROM {inactive_users} WHERE uid = %d AND warned_user_delete_timestamp < %d AND protected <> 1', $user->uid, time())) || !variable_get('inactive_user_auto_delete_warn', 0)) && $user->created < time() - $delete_time) {
          if (variable_get('inactive_user_preserve_content', 1) && _inactive_user_with_content($user->uid)) {

            // this is a protected user, mark as such
            db_query('UPDATE {inactive_users} SET protected = 1 WHERE uid = %d', $user->uid);
            if (!db_affected_rows()) {
              @db_query('INSERT INTO {inactive_users} (uid, protected) VALUES (%d, 1)', $user->uid, $protected);
            }
          }
          else {

            // delete the user
            // not using user_delete() so we can send custom emails and watchdog
            $array = (array) $user;
            sess_destroy_uid($user->uid);
            db_query("DELETE FROM {users} WHERE uid = %d", $user->uid);
            db_query('DELETE FROM {users_roles} WHERE uid = %d', $user->uid);
            db_query("DELETE FROM {authmap} WHERE uid = %d", $user->uid);
            module_invoke_all('user', 'delete', $array, $user);
            if (variable_get('inactive_user_notify_delete', 0)) {
              _inactive_user_mail(t('[@sitename] Account removed', array(
                '@sitename' => variable_get('site_name', 'drupal'),
              )), variable_get('inactive_user_delete_notify_text', _inactive_user_mail_text('delete_notify_text')), $delete_time, $user, NULL);
            }
            if (variable_get('inactive_user_notify_delete_admin', 0)) {
              $user_list .= "{$user->name} ({$user->mail}) last active on " . format_date($user->access, 'large') . ".\n";
            }
            watchdog('user', 'user %user deleted due to inactivity', array(
              '%user' => $user->name,
            ));
          }
        }
      }
      if ($user_list) {
        _inactive_user_mail(t('[@sitename] Deleted accounts', array(
          '@sitename' => variable_get('site_name', 'drupal'),
        )), _inactive_user_mail_text('delete_notify_admin_text'), $delete_time, NULL, $user_list);
        unset($user_list);
      }
    }
  }
}