0
|
1 #!/usr/bin/env php
|
|
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(__DIR__ . '/..') . '/' );
|
|
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 = rcube::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 }
|
|
61
|
|
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 }
|
|
67 }
|
|
68
|
|
69 // connect to DB
|
|
70 $db = $rcmail->get_dbh();
|
|
71 $db->db_connect('w');
|
|
72 $transaction = false;
|
|
73
|
|
74 if (!$db->is_connected() || $db->is_error()) {
|
|
75 _die("No DB connection\n" . $db->is_error());
|
|
76 }
|
|
77
|
|
78 // find user in loca database
|
|
79 $user = rcube_user::query($username, $args['host']);
|
|
80
|
|
81 if (!$user) {
|
|
82 die("User not found.\n");
|
|
83 }
|
|
84
|
|
85 // inform plugins about approaching user deletion
|
|
86 $plugin = $rcmail->plugins->exec_hook('user_delete_prepare', array('user' => $user, 'username' => $username, 'host' => $args['host']));
|
|
87
|
|
88 // let plugins cleanup their own user-related data
|
|
89 if (!$plugin['abort']) {
|
|
90 $transaction = $db->startTransaction();
|
|
91 $plugin = $rcmail->plugins->exec_hook('user_delete', $plugin);
|
|
92 }
|
|
93
|
|
94 if ($plugin['abort']) {
|
|
95 if ($transaction) {
|
|
96 $db->rollbackTransaction();
|
|
97 }
|
|
98 _die("User deletion aborted by plugin");
|
|
99 }
|
|
100
|
|
101 // deleting the user record should be sufficient due to ON DELETE CASCADE foreign key references
|
|
102 // but not all database backends actually support this so let's do it by hand
|
|
103 foreach (array('identities','contacts','contactgroups','dictionary','cache','cache_index','cache_messages','cache_thread','searches','users') as $table) {
|
|
104 $db->query('DELETE FROM ' . $db->table_name($table, true) . ' WHERE `user_id` = ?', $user->ID);
|
|
105 }
|
|
106
|
|
107 if ($db->is_error()) {
|
|
108 $rcmail->plugins->exec_hook('user_delete_rollback', $plugin);
|
|
109 _die("DB error occurred: " . $db->is_error());
|
|
110 }
|
|
111 else {
|
|
112 // inform plugins about executed user deletion
|
|
113 $plugin = $rcmail->plugins->exec_hook('user_delete_commit', $plugin);
|
|
114
|
|
115 if ($plugin['abort']) {
|
|
116 unset($plugin['abort']);
|
|
117 $db->rollbackTransaction();
|
|
118 $rcmail->plugins->exec_hook('user_delete_rollback', $plugin);
|
|
119 }
|
|
120 else {
|
|
121 $db->endTransaction();
|
|
122 echo "Successfully deleted user $user->ID\n";
|
|
123 }
|
|
124 }
|
|
125
|