Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

deluser.sh 4.1KB

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