| 1 |
<?php
|
| 2 |
// $Id: inactive_user.module,v 1.9 2009/06/11 19:27:33 deekayen Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* The inactive user module controls inactive users.
|
| 7 |
*
|
| 8 |
* The inactive user module sends mails to inactive users.
|
| 9 |
* The user can configure the time after Drupal sends mails.
|
| 10 |
*/
|
| 11 |
|
| 12 |
|
| 13 |
/**
|
| 14 |
* Implementation of hook_perm().
|
| 15 |
*/
|
| 16 |
function inactive_user_perm() {
|
| 17 |
return array('change inactive user settings');
|
| 18 |
}
|
| 19 |
|
| 20 |
/**
|
| 21 |
* Implementation of hook_user().
|
| 22 |
*/
|
| 23 |
function inactive_user_user($type, $edit, &$user) {
|
| 24 |
switch ($type) {
|
| 25 |
case 'delete':
|
| 26 |
db_query("DELETE FROM {inactive_users} WHERE uid = %d", $user->uid);
|
| 27 |
break;
|
| 28 |
}
|
| 29 |
}
|
| 30 |
|
| 31 |
/**
|
| 32 |
* Implementation of hook_menu().
|
| 33 |
*/
|
| 34 |
function inactive_user_menu() {
|
| 35 |
$items['admin/user/inactive-user'] = array(
|
| 36 |
'title' => 'Inactive users',
|
| 37 |
'description' => 'Set rules and contact templates for inactive users.',
|
| 38 |
'page callback' => 'drupal_get_form',
|
| 39 |
'page arguments' => array('inactive_user_custom_settings'),
|
| 40 |
'access arguments' => array('change inactive user settings'),
|
| 41 |
'type' => MENU_NORMAL_ITEM,
|
| 42 |
);
|
| 43 |
|
| 44 |
return $items;
|
| 45 |
}
|
| 46 |
|
| 47 |
/**
|
| 48 |
* Custom settings page: menu callback
|
| 49 |
*
|
| 50 |
* (we're using a custom callback to enable a nicer menu title,
|
| 51 |
* without underscore)
|
| 52 |
*/
|
| 53 |
function inactive_user_custom_settings() {
|
| 54 |
$period = array(0 => 'disabled') + drupal_map_assoc(array(604800, 1209600, 1814400, 2419200, 2592000, 7776000, 15552000, 23328000, 31536000, 47088000, 63072000), '_format_interval');
|
| 55 |
$warn_period = array(0 => 'disabled') + drupal_map_assoc(array(86400, 172800, 259200, 604800, 1209600, 1814400, 2592000), '_format_interval');
|
| 56 |
$mail_variables = ' %username, %useremail, %lastaccess, %period, %sitename, %siteurl';
|
| 57 |
|
| 58 |
// set administrator e-mail
|
| 59 |
$form['inactive_user_admin_email_fieldset'] = array(
|
| 60 |
'#type' => 'fieldset',
|
| 61 |
'#title' => t('Administrator e-mail'),
|
| 62 |
'#collapsible' => TRUE,
|
| 63 |
'#collapsed' => TRUE,
|
| 64 |
);
|
| 65 |
$form['inactive_user_admin_email_fieldset']['inactive_user_admin_email'] = array(
|
| 66 |
'#type' => 'textfield',
|
| 67 |
'#title' => t('E-mail addresses'),
|
| 68 |
'#default_value' => _inactive_user_admin_mail(),
|
| 69 |
'#description' => t('Supply a comma-separated list of e-mail addresses that will receive administrator alerts. Spaces between addresses are allowed.'),
|
| 70 |
'#maxlength' => 256,
|
| 71 |
'#required' => TRUE,
|
| 72 |
);
|
| 73 |
|
| 74 |
// inactive user notification
|
| 75 |
$form['inactive_user_notification'] = array(
|
| 76 |
'#type' => 'fieldset',
|
| 77 |
'#title' => t('Inactive user notification'),
|
| 78 |
'#collapsible' => TRUE,
|
| 79 |
'#collapsed' => TRUE,
|
| 80 |
);
|
| 81 |
$form['inactive_user_notification']['inactive_user_notify_admin'] = array(
|
| 82 |
'#type' => 'select',
|
| 83 |
'#title' => t('Notify administrator when a user hasn\'t logged in for more than'),
|
| 84 |
'#default_value' => variable_get('inactive_user_notify_admin', 0),
|
| 85 |
'#options' => $period,
|
| 86 |
'#description' => t('Generate an email to notify the site administrator that a user account hasn\'t been used for longer than the specified amount of time. Requires crontab.'),
|
| 87 |
);
|
| 88 |
$form['inactive_user_notification']['inactive_user_notify'] = array(
|
| 89 |
'#type' => 'select',
|
| 90 |
'#title' => t('Notify users when they haven\'t logged in for more than'),
|
| 91 |
'#default_value' => variable_get('inactive_user_notify', 0),
|
| 92 |
'#options' => $period,
|
| 93 |
'#description' => t('Generate an email to notify users when they haven\'t used their account for longer than the specified amount of time. Requires crontab.'),
|
| 94 |
);
|
| 95 |
$form['inactive_user_notification']['inactive_user_notify_text'] = array(
|
| 96 |
'#type' => 'textarea',
|
| 97 |
'#title' => t('Body of user notification e-mail'),
|
| 98 |
'#default_value' => variable_get('inactive_user_notify_text', _inactive_user_mail_text('notify_text')),
|
| 99 |
'#cols' => 70,
|
| 100 |
'#rows' => 10,
|
| 101 |
'#description' => t('Customize the body of the notification e-mail sent to the user.') .' '. t('Available variables are:') . $mail_variables,
|
| 102 |
'#required' => TRUE,
|
| 103 |
);
|
| 104 |
|
| 105 |
// automatically block inactive users
|
| 106 |
$form['block_inactive_user'] = array(
|
| 107 |
'#type' => 'fieldset',
|
| 108 |
'#title' => t('Automatically block inactive users'),
|
| 109 |
'#collapsible' => TRUE,
|
| 110 |
'#collapsed' => TRUE,
|
| 111 |
);
|
| 112 |
$form['block_inactive_user']['inactive_user_auto_block'] = array(
|
| 113 |
'#type' => 'select',
|
| 114 |
'#title' => t('Block users that haven\'t logged in for more than'),
|
| 115 |
'#default_value' => variable_get('inactive_user_auto_block', 0),
|
| 116 |
'#options' => $period,
|
| 117 |
'#description' => t('Automatically block user accounts that haven\'t been used in the specified amount of time. Requires crontab.'),
|
| 118 |
);
|
| 119 |
$form['block_inactive_user']['inactive_user_notify_block'] = array(
|
| 120 |
'#type' => 'checkbox',
|
| 121 |
'#title' => t('Notify user'),
|
| 122 |
'#default_value' => variable_get('inactive_user_notify_block', 0),
|
| 123 |
'#description' => t('Generate an email to notify a user that his/her account has been automatically blocked.'),
|
| 124 |
);
|
| 125 |
$form['block_inactive_user']['inactive_user_block_notify_text'] = array(
|
| 126 |
'#type' => 'textarea',
|
| 127 |
'#title' => t('Body of blocked user acount e-mail'),
|
| 128 |
'#default_value' => variable_get('inactive_user_block_notify_text', _inactive_user_mail_text('block_notify_text')),
|
| 129 |
'#cols' => 70,
|
| 130 |
'#rows' => 10,
|
| 131 |
'#description' => t('Customize the body of the notification e-mail sent to the user when their account has been blocked.') .' '. t('Available variables are:') . $mail_variables,
|
| 132 |
'#required' => TRUE,
|
| 133 |
);
|
| 134 |
$form['block_inactive_user']['inactive_user_notify_block_admin'] = array(
|
| 135 |
'#type' => 'checkbox',
|
| 136 |
'#title' => t('Notify administrator'),
|
| 137 |
'#default_value' => variable_get('inactive_user_notify_block_admin', 0),
|
| 138 |
'#description' => t('Generate an email to notify the site administrator when a user is automatically blocked.'),
|
| 139 |
);
|
| 140 |
$form['block_inactive_user']['inactive_user_auto_block_warn'] = array(
|
| 141 |
'#type' => 'select',
|
| 142 |
'#title' => t('Warn users before they are blocked'),
|
| 143 |
'#default_value' => variable_get('inactive_user_auto_block_warn', 0),
|
| 144 |
'#options' => $warn_period,
|
| 145 |
'#description' => t('Generate an email to notify a user that his/her account is about to be blocked.'),
|
| 146 |
);
|
| 147 |
$form['block_inactive_user']['inactive_user_block_warn_text'] = array(
|
| 148 |
'#type' => 'textarea',
|
| 149 |
'#title' => t('Body of user warning e-mail'),
|
| 150 |
'#default_value' => variable_get('inactive_user_block_warn_text', _inactive_user_mail_text('block_warn_text')),
|
| 151 |
'#cols' => 70,
|
| 152 |
'#rows' => 10,
|
| 153 |
'#description' => t('Customize the body of the notification e-mail sent to the user when their account is about to be blocked.') .' '. t('Available variables are:') . $mail_variables,
|
| 154 |
'#required' => TRUE,
|
| 155 |
);
|
| 156 |
|
| 157 |
// automatically delete inactive users
|
| 158 |
$form['delete_inactive_user'] = array(
|
| 159 |
'#type' => 'fieldset',
|
| 160 |
'#title' => t('Automatically delete inactive users'),
|
| 161 |
'#collapsible' => TRUE,
|
| 162 |
'#collapsed' => TRUE,
|
| 163 |
);
|
| 164 |
$form['delete_inactive_user']['inactive_user_auto_delete'] = array(
|
| 165 |
'#type' => 'select',
|
| 166 |
'#title' => t('Delete users that haven\'t logged in for more than'),
|
| 167 |
'#default_value' => variable_get('inactive_user_auto_delete', 0),
|
| 168 |
'#options' => $period,
|
| 169 |
'#description' => t('Automatically delete user accounts that haven\'t been used in the specified amount of time. Warning, user accounts are permanently deleted, with no ability to undo the action! Requires crontab.'),
|
| 170 |
);
|
| 171 |
$form['delete_inactive_user']['inactive_user_preserve_content'] = array(
|
| 172 |
'#type' => 'checkbox',
|
| 173 |
'#title' => t('Preserve users that own site content'),
|
| 174 |
'#default_value' => variable_get('inactive_user_preserve_content', 1),
|
| 175 |
'#description' => t('Select this option to never delete users that own site content. If you delete a user that owns content on the site, such as a user that created a node or left a comment, the content will no longer be available via the normal Drupal user interface. That is, if a user creates a node or leaves a comment, then the user is deleted, the node and/or comment will no longer be accesible even though it will still be in the database.'),
|
| 176 |
);
|
| 177 |
$form['delete_inactive_user']['inactive_user_notify_delete'] = array(
|
| 178 |
'#type' => 'checkbox',
|
| 179 |
'#title' => t('Notify user'),
|
| 180 |
'#default_value' => variable_get('inactive_user_notify_delete', 0),
|
| 181 |
'#description' => t('Generate an email to notify a user that his/her account has been automatically deleted.'),
|
| 182 |
);
|
| 183 |
$form['delete_inactive_user']['inactive_user_delete_notify_text'] = array(
|
| 184 |
'#type' => 'textarea',
|
| 185 |
'#title' => t('Body of deleted user account e-mail'),
|
| 186 |
'#default_value' => variable_get('inactive_user_delete_notify_text', _inactive_user_mail_text('delete_notify_text')),
|
| 187 |
'#cols' => 70,
|
| 188 |
'#rows' => 10,
|
| 189 |
'#description' => t('Customize the body of the notification e-mail sent to the user when their account has been deleted.') .' '. t('Available variables are:') . $mail_variables,
|
| 190 |
'#required' => TRUE,
|
| 191 |
);
|
| 192 |
$form['delete_inactive_user']['inactive_user_notify_delete_admin'] = array(
|
| 193 |
'#type' => 'checkbox',
|
| 194 |
'#title' => t('Notify administrator'),
|
| 195 |
'#default_value' => variable_get('inactive_user_notify_delete_admin', 0),
|
| 196 |
'#description' => t('Generate an email to notify the site administrator when a user is automatically deleted.'),
|
| 197 |
);
|
| 198 |
$form['delete_inactive_user']['inactive_user_auto_delete_warn'] = array(
|
| 199 |
'#type' => 'select',
|
| 200 |
'#title' => t('Warn users before they are deleted'),
|
| 201 |
'#default_value' => variable_get('inactive_user_auto_delete_warn', 0),
|
| 202 |
'#options' => $warn_period,
|
| 203 |
'#description' => t('Generate an email to notify a user that his/her account is about to be deleted.')
|
| 204 |
);
|
| 205 |
$form['delete_inactive_user']['inactive_user_delete_warn_text'] = array(
|
| 206 |
'#type' => 'textarea',
|
| 207 |
'#title' => t('Body of user warning e-mail'),
|
| 208 |
'#default_value' => variable_get('inactive_user_delete_warn_text', _inactive_user_mail_text('delete_warn_text')),
|
| 209 |
'#cols' => 70,
|
| 210 |
'#rows' => 10,
|
| 211 |
'#description' => t('Customize the body of the notification e-mail sent to the user when their account is about to be deleted.') .' '. t('Available variables are:') . $mail_variables,
|
| 212 |
'#required' => TRUE,
|
| 213 |
);
|
| 214 |
return system_settings_form($form);
|
| 215 |
}
|
| 216 |
|
| 217 |
function inactive_user_custom_settings_validate($form, &$form_state) {
|
| 218 |
$mails = explode(',', $edit['inactive_user_admin_email']);
|
| 219 |
foreach ($mails as $mail) {
|
| 220 |
if ($mail && !valid_email_address(trim($mail))) {
|
| 221 |
$invalid[] = $mail;
|
| 222 |
$count++;
|
| 223 |
}
|
| 224 |
}
|
| 225 |
if ($count == 1) {
|
| 226 |
form_set_error('inactive_user_admin_email', t('%mail is not a valid e-mail address', array('%mail' => $invalid[0])));
|
| 227 |
}
|
| 228 |
elseif ($count > 1) {
|
| 229 |
form_set_error('inactive_user_admin_email', t('The following e-mail addresses are invalid: %mail', array('%mail' => implode(', ', $invalid))));
|
| 230 |
}
|
| 231 |
}
|
| 232 |
|
| 233 |
/**
|
| 234 |
* Implementation of hook_cron().
|
| 235 |
*/
|
| 236 |
function inactive_user_cron() {
|
| 237 |
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.
|
| 238 |
variable_set('inactive_user_timestamp', time());
|
| 239 |
unset($user_list);
|
| 240 |
|
| 241 |
// reset notifications if recent user activity
|
| 242 |
$users = db_fetch_object(db_query('SELECT uid FROM {inactive_users} WHERE uid <> 1'));
|
| 243 |
if ($users) {
|
| 244 |
foreach ($users as $uid) {
|
| 245 |
$u = db_fetch_object(db_query('SELECT access, name FROM {users} WHERE uid = %d', $uid));
|
| 246 |
if ($u->access > time() - 604800) {
|
| 247 |
// user activity in last week, remove from inactivity table
|
| 248 |
db_query('DELETE FROM {inactive_users} WHERE uid = %d', $uid);
|
| 249 |
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'))));
|
| 250 |
}
|
| 251 |
}
|
| 252 |
}
|
| 253 |
|
| 254 |
// notify administrator of inactive user accounts
|
| 255 |
if ($notify_time = variable_get('inactive_user_notify_admin', 0)) {
|
| 256 |
$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);
|
| 257 |
while ($user = db_fetch_object($result)) {
|
| 258 |
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))) {
|
| 259 |
db_query('UPDATE {inactive_users} SET notified_admin = 1 WHERE uid = %d', $user->uid);
|
| 260 |
if (!db_affected_rows()) {
|
| 261 |
// must create a new row
|
| 262 |
@db_query('INSERT INTO {inactive_users} (uid, notified_admin) VALUES (%d, 1)', $user->uid);
|
| 263 |
}
|
| 264 |
$user_list .= "$user->name ($user->mail) last active on ". format_date($user->access, 'large') .".\n";
|
| 265 |
}
|
| 266 |
}
|
| 267 |
if ($user_list) {
|
| 268 |
_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);
|
| 269 |
unset($user_list);
|
| 270 |
}
|
| 271 |
}
|
| 272 |
|
| 273 |
// notify users that their account has been inactive
|
| 274 |
if ($notify_time = variable_get('inactive_user_notify', 0)) {
|
| 275 |
$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);
|
| 276 |
while ($user = db_fetch_object($result)) {
|
| 277 |
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))) {
|
| 278 |
db_query('UPDATE {inactive_users} SET notified_user = 1 WHERE uid = %d', $user->uid);
|
| 279 |
if (!db_affected_rows()) {
|
| 280 |
@db_query('INSERT INTO {inactive_users} (uid, notified_user) VALUES (%d, 1)', $user->uid);
|
| 281 |
}
|
| 282 |
_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);
|
| 283 |
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'))));
|
| 284 |
}
|
| 285 |
}
|
| 286 |
}
|
| 287 |
|
| 288 |
// warn users when they are about to be blocked
|
| 289 |
if (($warn_time = variable_get('inactive_user_auto_block_warn', 0)) &&
|
| 290 |
($block_time = variable_get('inactive_user_auto_block', 0))) {
|
| 291 |
$result = db_query('SELECT * FROM {users} WHERE ((access <> 0 AND login <> 0 AND access < (%d - %d - %d)) OR (login = 0 AND created < (%d - %d - %d))) AND status <> 0 AND uid <> 1', time(), $warn_time, $block_time, time(), $warn_time, $block_time);
|
| 292 |
while ($user = db_fetch_object($result)) {
|
| 293 |
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 - $block_time))) {
|
| 294 |
db_query('UPDATE {inactive_users} SET warned_user_block_timestamp = %d WHERE uid = %d', time() + $warn_time, $user->uid);
|
| 295 |
if (!db_affected_rows()) {
|
| 296 |
@db_query('INSERT INTO {inactive_users} (uid, warned_user_block_timestamp) VALUES (%d, %d)', $user->uid, time() + $warn_time);
|
| 297 |
}
|
| 298 |
_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);
|
| 299 |
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'))));
|
| 300 |
}
|
| 301 |
}
|
| 302 |
}
|
| 303 |
|
| 304 |
// automatically block users
|
| 305 |
if ($block_time = variable_get('inactive_user_auto_block', 0)) {
|
| 306 |
$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);
|
| 307 |
while ($user = db_fetch_object($result)) {
|
| 308 |
// don't block user yet if we sent a warning and it hasn't expired
|
| 309 |
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))) {
|
| 310 |
db_query('UPDATE {users} SET status = 0 WHERE uid = %d', $user->uid);
|
| 311 |
|
| 312 |
// notify user
|
| 313 |
if (variable_get('inactive_user_notify_block', 0)) {
|
| 314 |
if (!db_fetch_object(db_query('SELECT uid FROM {inactive_users} WHERE uid = %d AND notified_user_block = 1', $user->uid))) {
|
| 315 |
db_query('UPDATE {inactive_users} SET notified_user_block = 1 WHERE uid = %d', $user->uid);
|
| 316 |
if (!db_affected_rows()) {
|
| 317 |
@db_query('INSERT INTO {inactive_users} (uid, notified_user_block) VALUES (%d, 1)', $user->uid);
|
| 318 |
}
|
| 319 |
_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);
|
| 320 |
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'))));
|
| 321 |
}
|
| 322 |
}
|
| 323 |
|
| 324 |
// notify admin
|
| 325 |
if (variable_get('inactive_user_notify_block_admin', 0)) {
|
| 326 |
if (!db_fetch_object(db_query('SELECT uid FROM {inactive_users} WHERE uid = %d and notified_admin_block = 1', $user->uid))) {
|
| 327 |
db_query('UPDATE {inactive_users} SET notified_admin_block = 1 WHERE uid = %d', $user->uid);
|
| 328 |
if (!db_affected_rows()) {
|
| 329 |
@db_query('INSERT INTO {inactive_users} (uid, notified_admin_block) VALUES(%d, 1)', $user->uid);
|
| 330 |
}
|
| 331 |
$user_list .= "$user->name ($user->mail) last active on ". format_date($user->access, 'large') .".\n";
|
| 332 |
}
|
| 333 |
}
|
| 334 |
}
|
| 335 |
if ($user_list) {
|
| 336 |
_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);
|
| 337 |
unset($user_list);
|
| 338 |
}
|
| 339 |
}
|
| 340 |
}
|
| 341 |
|
| 342 |
// warn users when they are about to be deleted
|
| 343 |
if (($warn_time = variable_get('inactive_user_auto_delete_warn', 0)) &&
|
| 344 |
($delete_time = variable_get('inactive_user_auto_delete', 0))) {
|
| 345 |
$result = db_query('SELECT * FROM {users} WHERE ((access <> 0 AND login <> 0 AND access < (%d - %d - %d)) OR (login = 0 AND created < (%d - %d - %d))) AND uid <> 1', time(), $warn_time, $delete_time, time(), $warn_time, $delete_time);
|
| 346 |
while ($user = db_fetch_object($result)) {
|
| 347 |
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 - $delete_time))) {
|
| 348 |
if (variable_get('inactive_user_preserve_content', 1) && _inactive_user_with_content($user->uid)) {
|
| 349 |
$protected = 1;
|
| 350 |
}
|
| 351 |
else {
|
| 352 |
$protected = 0;
|
| 353 |
}
|
| 354 |
db_query('UPDATE {inactive_users} SET warned_user_delete_timestamp = %d AND protected = %d WHERE uid = %d', time() + $warn_time, $protected, $user->uid);
|
| 355 |
if (!db_affected_rows()) {
|
| 356 |
@db_query('INSERT INTO {inactive_users} (uid, warned_user_delete_timestamp, protected) VALUES (%d, %d, %d)', $user->uid, time() + $warn_time, $protected);
|
| 357 |
}
|
| 358 |
if (!$protected) {
|
| 359 |
_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);
|
| 360 |
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'))));
|
| 361 |
}
|
| 362 |
}
|
| 363 |
}
|
| 364 |
}
|
| 365 |
|
| 366 |
// automatically delete users
|
| 367 |
if ($delete_time = variable_get('inactive_user_auto_delete', 0)) {
|
| 368 |
$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);
|
| 369 |
while ($user = db_fetch_object($result)) {
|
| 370 |
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))) {
|
| 371 |
if (variable_get('inactive_user_preserve_content', 1) && _inactive_user_with_content($user->uid)) {
|
| 372 |
// this is a protected user, mark as such
|
| 373 |
db_query('UPDATE {inactive_users} SET protected = 1 WHERE uid = %d', $user->uid);
|
| 374 |
if (!db_affected_rows()) {
|
| 375 |
@db_query('INSERT INTO {inactive_users} (uid, protected) VALUES (%d, 1)', $user->uid, $protected);
|
| 376 |
}
|
| 377 |
}
|
| 378 |
else {
|
| 379 |
// delete the user
|
| 380 |
// not using user_delete() so we can send custom emails and watchdog
|
| 381 |
$array = (array) $user;
|
| 382 |
sess_destroy_uid($user->uid);
|
| 383 |
db_query("DELETE FROM {users} WHERE uid = %d", $user->uid);
|
| 384 |
db_query('DELETE FROM {users_roles} WHERE uid = %d', $user->uid);
|
| 385 |
db_query("DELETE FROM {authmap} WHERE uid = %d", $user->uid);
|
| 386 |
module_invoke_all('user', 'delete', $array, $user);
|
| 387 |
if (variable_get('inactive_user_notify_delete', 0)) {
|
| 388 |
_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);
|
| 389 |
}
|
| 390 |
if (variable_get('inactive_user_notify_delete_admin', 0)) {
|
| 391 |
$user_list .= "$user->name ($user->mail) last active on ". format_date($user->access, 'large') .".\n";
|
| 392 |
}
|
| 393 |
watchdog('user', 'user %user deleted due to inactivity', array('%user' => $user->name));
|
| 394 |
}
|
| 395 |
}
|
| 396 |
}
|
| 397 |
if ($user_list) {
|
| 398 |
_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);
|
| 399 |
unset($user_list);
|
| 400 |
}
|
| 401 |
}
|
| 402 |
}
|
| 403 |
}
|
| 404 |
|
| 405 |
/**
|
| 406 |
* Slighty modified from format_interval() in common.inc to include months.
|
| 407 |
*/
|
| 408 |
function _format_interval($timestamp, $granularity = 2) {
|
| 409 |
$units = array('1 year|@count years' => 31536000, '1 month|@count months' => 2592000, '1 week|@count weeks' => 604800, '1 day|@count days' => 86400, '1 hour|@count hours' => 3600, '1 min|@count min' => 60, '1 sec|@count sec' => 1);
|
| 410 |
foreach ($units as $key => $value) {
|
| 411 |
$key = explode('|', $key);
|
| 412 |
if ($timestamp >= $value) {
|
| 413 |
$output .= ($output ? ' ' : '') . format_plural(floor($timestamp / $value), $key[0], $key[1]);
|
| 414 |
$timestamp %= $value;
|
| 415 |
$granularity--;
|
| 416 |
}
|
| 417 |
|
| 418 |
if ($granularity == 0) {
|
| 419 |
break;
|
| 420 |
}
|
| 421 |
}
|
| 422 |
return ($output) ? $output : t('0 sec');
|
| 423 |
}
|
| 424 |
|
| 425 |
/**
|
| 426 |
* Get administrator e-mail address(es)
|
| 427 |
*/
|
| 428 |
function _inactive_user_admin_mail() {
|
| 429 |
$admin_mail = db_result(db_query('SELECT mail FROM {users} WHERE uid = 1'));
|
| 430 |
return variable_get('inactive_user_admin_email', variable_get('site_mail', $admin_mail));
|
| 431 |
}
|
| 432 |
|
| 433 |
function inactive_user_mail($key, &$message, $params) {
|
| 434 |
$message['subject'] = $params['subject'];
|
| 435 |
$message['body'][] = $params['message'];
|
| 436 |
}
|
| 437 |
|
| 438 |
/**
|
| 439 |
* Wrapper for user_mail.
|
| 440 |
*/
|
| 441 |
function _inactive_user_mail($subject, $message, $period, $user = NULL, $user_list = NULL) {
|
| 442 |
global $base_url;
|
| 443 |
if ($user_list) {
|
| 444 |
$to = _inactive_user_admin_mail();
|
| 445 |
$variables = array(
|
| 446 |
'%period' => _format_interval($period),
|
| 447 |
'%sitename' => variable_get('site_name', 'drupal'),
|
| 448 |
'%siteurl' => $base_url,
|
| 449 |
"%userlist" => $user_list
|
| 450 |
);
|
| 451 |
}
|
| 452 |
elseif (isset($user->uid)) {
|
| 453 |
$to = $user->mail;
|
| 454 |
$variables = array(
|
| 455 |
'%username' => $user->name,
|
| 456 |
'%useremail' => $user->mail,
|
| 457 |
'%lastaccess' => empty($user->access) ? t('never') : format_date($user->access, 'custom', 'M d, Y'),
|
| 458 |
'%period' => _format_interval($period),
|
| 459 |
'%sitename' => variable_get('site_name', 'drupal'),
|
| 460 |
'%siteurl' => $base_url
|
| 461 |
);
|
| 462 |
}
|
| 463 |
if ($to) {
|
| 464 |
$from = variable_get('site_mail', ini_get('sendmail_from'));
|
| 465 |
$headers = array(
|
| 466 |
'Reply-to' => $from,
|
| 467 |
'Return-path' => "<$from>",
|
| 468 |
'Errors-to' => $from
|
| 469 |
);
|
| 470 |
$recipients = explode(',', $to);
|
| 471 |
foreach ($recipients as $recipient) {
|
| 472 |
$recipient = trim($recipient);
|
| 473 |
$params = array(
|
| 474 |
'subject' => $subject,
|
| 475 |
'message' => strtr($message, $variables),
|
| 476 |
'headers' => $headers
|
| 477 |
);
|
| 478 |
$user = user_load(array('mail' => $recipient));
|
| 479 |
$language = user_preferred_language($account);
|
| 480 |
drupal_mail('inactive_user', 'inactive_user_notice', $recipient, $language, $params, $from, TRUE);
|
| 481 |
}
|
| 482 |
}
|
| 483 |
}
|
| 484 |
|
| 485 |
/**
|
| 486 |
* Some default e-mail notification strings.
|
| 487 |
*/
|
| 488 |
function _inactive_user_mail_text($message) {
|
| 489 |
switch ($message) {
|
| 490 |
case 'notify_text':
|
| 491 |
return t("Hello %username,\n\n We haven't seen you at %sitename since %lastaccess, and we miss you! Please come back and visit us soon at %siteurl.\n\nSincerely,\n %sitename team");
|
| 492 |
break;
|
| 493 |
case 'notify_admin_text':
|
| 494 |
return t("Hello,\n\n This automatic notification is to inform you that the following users haven't been seen on %sitename for more than %period:\n\n%userlist");
|
| 495 |
break;
|
| 496 |
case 'block_warn_text':
|
| 497 |
return t("Hello %username,\n\n We haven't seen you at %sitename since %lastaccess, and we miss you! This automatic message is to warn you that your account will be disabled in %period unless you come back and visit us before that time.\n\n Please visit us at %siteurl.\n\nSincerely,\n %sitename team");
|
| 498 |
break;
|
| 499 |
case 'block_notify_text':
|
| 500 |
return t("Hello %username,\n\n This automatic message is to notify you that your account on %sitename has been automatically disabled due to no activity for more than %period.\n\n Please visit us at %siteurl to have your account re-enabled.\n\nSincerely,\n %sitename team");
|
| 501 |
break;
|
| 502 |
case 'block_notify_admin_text':
|
| 503 |
return t("Hello,\n\n This automatic notification is to inform you that the following users have been automatically blocked due to inactivity on %sitename for more than %period:\n\n%userlist");
|
| 504 |
break;
|
| 505 |
case 'delete_warn_text':
|
| 506 |
return t("Hello %username,\n\n We haven't seen you at %sitename since %lastaccess, and we miss you! This automatic message is to warn you that your account will be completely removed in %period unless you come back and visit us before that time.\n\n Please visit us at %siteurl.\n\nSincerely,\n %sitename team");
|
| 507 |
break;
|
| 508 |
case 'delete_notify_text':
|
| 509 |
return t("Hello %username,\n\n This automatic message is to notify you that your account on %sitename has been automatically removed due to no activity for more than %period.\n\n Please visit us at %siteurl if you would like to create a new account.\n\nSincerely,\n %sitename team");
|
| 510 |
break;
|
| 511 |
case 'delete_notify_admin_text':
|
| 512 |
return t("Hello,\n\n This automatic notification is to inform you that the following users have been automatically deleted due to inactivity on %sitename for more than %period:\n\n%userlist");
|
| 513 |
break;
|
| 514 |
}
|
| 515 |
}
|
| 516 |
|
| 517 |
/**
|
| 518 |
* Returns 1 if the user has ever created a node or a comment.
|
| 519 |
*
|
| 520 |
* The settings of inactive_user.module allow to protect such
|
| 521 |
* users from deletion.
|
| 522 |
*/
|
| 523 |
function _inactive_user_with_content($uid) {
|
| 524 |
return (db_fetch_object(db_query('SELECT uid FROM {node} WHERE uid = %d', $uid)) || db_fetch_object(db_query('SELECT uid FROM {comments} WHERE uid = %d', $uid)));
|
| 525 |
}
|
| 526 |
|