commit | author | age
|
6fe5dd
|
1 |
#!/usr/bin/env php |
TB |
2 |
<?php |
|
3 |
|
|
4 |
/* |
|
5 |
+-----------------------------------------------------------------------+ |
|
6 |
| bin/deluser.sh | |
|
7 |
| | |
|
8 |
| This file is part of the Roundcube Webmail client | |
|
9 |
| Copyright (C) 2014, The Roundcube Dev Team | |
|
10 |
| | |
|
11 |
| Licensed under the GNU General Public License version 3 or | |
|
12 |
| any later version with exceptions for skins & plugins. | |
|
13 |
| See the README file for a full license statement. | |
|
14 |
| | |
|
15 |
| PURPOSE: | |
|
16 |
| Utility script to remove all data related to a certain user | |
|
17 |
| from the local database. | |
|
18 |
+-----------------------------------------------------------------------+ |
|
19 |
| Author: Thomas Bruederli <thomas@roundcube.net> | |
|
20 |
+-----------------------------------------------------------------------+ |
|
21 |
*/ |
|
22 |
|
|
23 |
define('INSTALL_PATH', realpath(dirname(__FILE__) . '/..') . '/' ); |
|
24 |
|
|
25 |
require_once INSTALL_PATH . 'program/include/clisetup.php'; |
|
26 |
|
|
27 |
function print_usage() |
|
28 |
{ |
|
29 |
print "Usage: deluser.sh [--host=mail_host] username\n"; |
|
30 |
print "--host=HOST The IMAP hostname or IP the given user is related to\n"; |
|
31 |
} |
|
32 |
|
|
33 |
function _die($msg, $usage=false) |
|
34 |
{ |
|
35 |
fputs(STDERR, $msg . "\n"); |
|
36 |
if ($usage) print_usage(); |
|
37 |
exit(1); |
|
38 |
} |
|
39 |
|
|
40 |
$rcmail = rcmail::get_instance(); |
|
41 |
|
|
42 |
// get arguments |
|
43 |
$args = rcube_utils::get_opt(array('h' => 'host')); |
|
44 |
$username = trim($args[0]); |
|
45 |
|
|
46 |
if (empty($username)) { |
|
47 |
_die("Missing required parameters", true); |
|
48 |
} |
|
49 |
|
|
50 |
if (empty($args['host'])) { |
|
51 |
$hosts = $rcmail->config->get('default_host', ''); |
|
52 |
if (is_string($hosts)) { |
|
53 |
$args['host'] = $hosts; |
|
54 |
} |
|
55 |
else if (is_array($hosts) && count($hosts) == 1) { |
|
56 |
$args['host'] = reset($hosts); |
|
57 |
} |
|
58 |
else { |
|
59 |
_die("Specify a host name", true); |
|
60 |
} |
5b6d09
|
61 |
|
TB |
62 |
// host can be a URL like tls://192.168.12.44 |
|
63 |
$host_url = parse_url($args['host']); |
|
64 |
if ($host_url['host']) { |
|
65 |
$args['host'] = $host_url['host']; |
|
66 |
} |
6fe5dd
|
67 |
} |
TB |
68 |
|
|
69 |
// connect to DB |
|
70 |
$db = $rcmail->get_dbh(); |
|
71 |
$db->db_connect('w'); |
|
72 |
|
|
73 |
if (!$db->is_connected() || $db->is_error()) { |
|
74 |
_die("No DB connection\n" . $db->is_error()); |
|
75 |
} |
|
76 |
|
|
77 |
// find user in loca database |
|
78 |
$user = rcube_user::query($username, $args['host']); |
|
79 |
|
|
80 |
if (!$user) { |
|
81 |
die("User not found.\n"); |
|
82 |
} |
|
83 |
|
|
84 |
// let plugins cleanup their own user-related data |
|
85 |
$plugin = $rcmail->plugins->exec_hook('user_delete', array('user' => $user, 'username' => $username, 'host' => $args['host'])); |
|
86 |
|
|
87 |
if ($plugin['abort']) { |
|
88 |
_die("User deletion aborted by plugin"); |
|
89 |
} |
|
90 |
|
|
91 |
// deleting the user record should be sufficient due to ON DELETE CASCADE foreign key references |
|
92 |
// but not all database backends actually support this so let's do it by hand |
|
93 |
foreach (array('identities','contacts','contactgroups','dictionaries','cache','cache_index','cache_messages','cache_thread','searches','users') as $table) { |
|
94 |
$db->query('DELETE FROM ' . $db->table_name($table) . ' WHERE user_id=?', $user->ID); |
|
95 |
} |
|
96 |
|
|
97 |
if ($db->is_error()) { |
|
98 |
_die("DB error occurred: " . $db->is_error()); |
|
99 |
} |
|
100 |
else { |
|
101 |
echo "Successfully deleted user $user->ID\n"; |
|
102 |
} |
|
103 |
|