Aleksander Machniak
2016-05-22 77b5d7ee304a688a2eb115ce04b460b43c0dd700
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
0ea079 23 define('INSTALL_PATH', realpath(__DIR__ . '/..') . '/' );
6fe5dd 24
TB 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
1b39d9 40 $rcmail = rcube::get_instance();
6fe5dd 41
TB 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');
68b005 72 $transaction = false;
6fe5dd 73
TB 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
68b005 85 // inform plugins about approaching user deletion
TB 86 $plugin = $rcmail->plugins->exec_hook('user_delete_prepare', array('user' => $user, 'username' => $username, 'host' => $args['host']));
87
6fe5dd 88 // let plugins cleanup their own user-related data
68b005 89 if (!$plugin['abort']) {
TB 90     $transaction = $db->startTransaction();
91     $plugin = $rcmail->plugins->exec_hook('user_delete', $plugin);
92 }
6fe5dd 93
TB 94 if ($plugin['abort']) {
68b005 95     if ($transaction) {
TB 96         $db->rollbackTransaction();
97     }
6fe5dd 98     _die("User deletion aborted by plugin");
TB 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
68b005 103 foreach (array('identities','contacts','contactgroups','dictionary','cache','cache_index','cache_messages','cache_thread','searches','users') as $table) {
34a090 104     $db->query('DELETE FROM ' . $db->table_name($table, true) . ' WHERE `user_id` = ?', $user->ID);
6fe5dd 105 }
TB 106
107 if ($db->is_error()) {
68b005 108     $rcmail->plugins->exec_hook('user_delete_rollback', $plugin);
6fe5dd 109     _die("DB error occurred: " . $db->is_error());
TB 110 }
111 else {
68b005 112     // inform plugins about executed user deletion
TB 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     }
6fe5dd 124 }
TB 125