| 1 |
<?php
|
| 2 |
|
| 3 |
// TODO: consider moving from own table to a variable_set()/get()
|
| 4 |
|
| 5 |
function user_maintenance_help($section) {
|
| 6 |
switch($section) {
|
| 7 |
case 'admin/modules#description':
|
| 8 |
return t('Deletes users who self-registered but never logged in.');
|
| 9 |
}
|
| 10 |
}
|
| 11 |
|
| 12 |
function user_maintenance_user($op, &$edit, &$user, $category = NULL) {
|
| 13 |
if ($op == 'insert') {
|
| 14 |
if (arg(0) != 'admin' && $user->uid > 1) {
|
| 15 |
db_query('INSERT INTO {user_maintenance_ids}(user_id) VALUES (%d)', $user->uid);
|
| 16 |
}
|
| 17 |
}
|
| 18 |
elseif ($op == 'delete') {
|
| 19 |
db_query('DELETE FROM {user_maintenance_ids} WHERE user_id = %d', $user->uid);
|
| 20 |
}
|
| 21 |
}
|
| 22 |
|
| 23 |
function user_maintenance_settings() {
|
| 24 |
$delays = drupal_map_assoc(array(4*7*24*60*60, 3*7*24*60*60, 2*7*24*60*60, 7*24*60*60, 4*24*60*60, 2*24*60*60, 24*60*60), "format_interval");
|
| 25 |
$delays['-1'] = t('Never delete');
|
| 26 |
$form['user_maintenance_delete_delay'] = array(
|
| 27 |
'#type' => 'select',
|
| 28 |
'#title' => t('Delete users who never logged in after'),
|
| 29 |
'#default_value' => variable_get('user_maintenance_delete_delay', -1),
|
| 30 |
'#options' => $delays,
|
| 31 |
'#description' => t('After this time users who registered but never logged in will be deleted. Accounts created by admin are never deleted'),
|
| 32 |
);
|
| 33 |
return $form;
|
| 34 |
}
|
| 35 |
|
| 36 |
/**
|
| 37 |
* Deletes users who:
|
| 38 |
* 1. never logged in
|
| 39 |
* 2. registered X days ago (configurable)
|
| 40 |
* 3. do exist in {user_maintenance_ids}
|
| 41 |
*
|
| 42 |
* {user_maintenance_ids} contains ids of users who self-registered (not
|
| 43 |
* created by admin) and have uid > 1.
|
| 44 |
*/
|
| 45 |
function user_maintenance_cron() {
|
| 46 |
$delay = variable_get('user_maintenance_delete_delay', -1);
|
| 47 |
if ($delay == -1) {
|
| 48 |
return;
|
| 49 |
}
|
| 50 |
|
| 51 |
$result = db_query('SELECT uid, name, login FROM {users}, {user_maintenance_ids} WHERE uid = user_id AND created <= %d', time() - $delay);
|
| 52 |
$my_uids = $user_uids = $user_names = array();
|
| 53 |
while ($user = db_fetch_array($result)) {
|
| 54 |
if ($user['login']) { // User has logged in after registering
|
| 55 |
$my_uids[] = $user['uid'];
|
| 56 |
} else {
|
| 57 |
$my_uids[] = $user_uids[] = $user['uid'];
|
| 58 |
$user_names[] = $user['name'];
|
| 59 |
}
|
| 60 |
}
|
| 61 |
|
| 62 |
if (count($my_uids)) {
|
| 63 |
db_query('DELETE FROM {user_maintenance_ids} WHERE user_id IN ('. join(', ', $my_uids) .')');
|
| 64 |
}
|
| 65 |
|
| 66 |
if (count($user_uids)) {
|
| 67 |
$users = array();
|
| 68 |
foreach ($user_uids as $uid) {
|
| 69 |
$users[] = user_load(array('uid' => $uid));
|
| 70 |
}
|
| 71 |
$user_uids = join(', ', $user_uids);
|
| 72 |
db_query('DELETE FROM {users} WHERE uid IN ('. $user_uids .')');
|
| 73 |
db_query('DELETE FROM {sessions} WHERE uid IN ('. $user_uids .')'); // Probably not needed, but should not break anything
|
| 74 |
db_query('DELETE FROM {users_roles} WHERE uid IN ('. $user_uids .')');
|
| 75 |
db_query('DELETE FROM {authmap} WHERE uid IN ('. $user_uids .')');
|
| 76 |
watchdog('user', t('User maintenance: deleted never logged in users: %users', array('%users' => theme('placeholder', join(', ', $user_names)))));
|
| 77 |
foreach ($users as $user) {
|
| 78 |
module_invoke_all('user', 'delete', (array) $user, $user);
|
| 79 |
}
|
| 80 |
}
|
| 81 |
}
|
| 82 |
|