[ Index ]

PHP Cross Reference of phpBB-3.3.14-deutsch

title

Body

[close]

/phpbb/console/command/user/ -> delete_id.php (source)

   1  <?php
   2  /**
   3   *
   4   * This file is part of the phpBB Forum Software package.
   5   *
   6   * @copyright (c) phpBB Limited <https://www.phpbb.com>
   7   * @license GNU General Public License, version 2 (GPL-2.0)
   8   *
   9   * For full copyright and license information, please see
  10   * the docs/CREDITS.txt file.
  11   *
  12   */
  13  
  14  namespace phpbb\console\command\user;
  15  
  16  use phpbb\console\command\command;
  17  use phpbb\db\driver\driver_interface;
  18  use phpbb\language\language;
  19  use phpbb\log\log_interface;
  20  use phpbb\user;
  21  use phpbb\user_loader;
  22  use Symfony\Component\Console\Input\InputArgument;
  23  use Symfony\Component\Console\Input\InputInterface;
  24  use Symfony\Component\Console\Input\InputOption;
  25  use Symfony\Component\Console\Output\OutputInterface;
  26  use Symfony\Component\Console\Question\ConfirmationQuestion;
  27  use Symfony\Component\Console\Style\SymfonyStyle;
  28  
  29  class delete_id extends command
  30  {
  31      /** @var driver_interface */
  32      protected $db;
  33  
  34      /** @var language */
  35      protected $language;
  36  
  37      /** @var log_interface */
  38      protected $log;
  39  
  40      /** @var user_loader */
  41      protected $user_loader;
  42  
  43      /** @var string Bots table */
  44      protected $bots_table;
  45  
  46      /** @var string User group table */
  47      protected $user_group_table;
  48  
  49      /** @var string Users table */
  50      protected $users_table;
  51  
  52      /** @var string phpBB root path */
  53      protected $phpbb_root_path;
  54  
  55      /** @var string PHP extension */
  56      protected $php_ext;
  57  
  58      /**
  59       * Construct method
  60       *
  61       * @param driver_interface $db
  62       * @param language         $language
  63       * @param log_interface    $log
  64       * @param user             $user
  65       * @param user_loader      $user_loader
  66       * @param string           $bots_table
  67       * @param string           $user_group_table
  68       * @param string           $users_table
  69       * @param string           $phpbb_root_path
  70       * @param string           $php_ext
  71       */
  72  	public function __construct(driver_interface $db, language $language, log_interface $log, user $user, user_loader $user_loader,
  73                                  string $bots_table, string $user_group_table, string $users_table, string $phpbb_root_path, string $php_ext)
  74      {
  75          $this->db = $db;
  76          $this->language = $language;
  77          $this->log = $log;
  78          $this->user_loader = $user_loader;
  79          $this->bots_table = $bots_table;
  80          $this->user_group_table = $user_group_table;
  81          $this->users_table = $users_table;
  82          $this->phpbb_root_path = $phpbb_root_path;
  83          $this->php_ext = $php_ext;
  84  
  85          $this->language->add_lang('acp/users');
  86          parent::__construct($user);
  87      }
  88  
  89      /**
  90       * Sets the command name and description
  91       *
  92       * @return void
  93       */
  94  	protected function configure(): void
  95      {
  96          $this
  97              ->setName('user:delete_id')
  98              ->setDescription($this->language->lang('CLI_DESCRIPTION_USER_DELETE_ID'))
  99              ->addArgument(
 100                  'user_ids',
 101                  InputArgument::REQUIRED | InputArgument::IS_ARRAY,
 102                  $this->language->lang('CLI_DESCRIPTION_USER_DELETE_ID_OPTION_ID')
 103              )
 104              ->addOption(
 105                  'delete-posts',
 106                  null,
 107                  InputOption::VALUE_NONE,
 108                  $this->language->lang('CLI_DESCRIPTION_USER_DELETE_OPTION_POSTS')
 109              )
 110          ;
 111      }
 112  
 113      /**
 114       * Executes the command user:delete_ids
 115       *
 116       * Deletes a list of user ids from the database. An option to delete the users' posts
 117       * is available, by default posts will be retained.
 118       *
 119       * @param InputInterface  $input  The input stream used to get the options
 120       * @param OutputInterface $output The output stream, used to print messages
 121       *
 122       * @return int 0 if all is well, 1 if any errors occurred
 123       */
 124  	protected function execute(InputInterface $input, OutputInterface $output): int
 125      {
 126          $user_ids = $input->getArgument('user_ids');
 127          $mode = ($input->getOption('delete-posts')) ? 'remove' : 'retain';
 128          $deleted_users = 0;
 129          $io = new SymfonyStyle($input, $output);
 130  
 131          if (count($user_ids) > 0)
 132          {
 133              $this->user_loader->load_users($user_ids);
 134  
 135              $progress = $this->create_progress_bar(count($user_ids), $io, $output);
 136              $progress->setMessage($this->language->lang('CLI_USER_DELETE_ID_START'));
 137              $progress->start();
 138  
 139              foreach ($user_ids as $user_id)
 140              {
 141                  $user_row = $this->user_loader->get_user($user_id);
 142  
 143                  // Skip anonymous user
 144                  if ($user_row['user_id'] == ANONYMOUS)
 145                  {
 146                      $progress->advance();
 147                      continue;
 148                  }
 149                  else if ($user_row['user_type'] == USER_IGNORE)
 150                  {
 151                      $this->delete_bot_user($user_row);
 152                  }
 153                  else
 154                  {
 155                      if (!function_exists('user_delete'))
 156                      {
 157                          require($this->phpbb_root_path . 'includes/functions_user.' . $this->php_ext);
 158                      }
 159  
 160                      user_delete($mode, $user_row['user_id'], $user_row['username']);
 161  
 162                      $this->log->add('admin', ANONYMOUS, '', 'LOG_USER_DELETED', false, array($user_row['username']));
 163                  }
 164  
 165                  $progress->advance();
 166                  $deleted_users++;
 167              }
 168  
 169              $progress->finish();
 170  
 171              if ($deleted_users > 0)
 172              {
 173                  $io->success($this->language->lang('CLI_USER_DELETE_ID_SUCCESS'));
 174              }
 175          }
 176  
 177          if (!$deleted_users)
 178          {
 179              $io->note($this->language->lang('CLI_USER_DELETE_NONE'));
 180          }
 181  
 182          return 0;
 183      }
 184  
 185      /**
 186       * Interacts with the user.
 187       * Confirm they really want to delete the account...last chance!
 188       *
 189       * @param InputInterface  $input  An InputInterface instance
 190       * @param OutputInterface $output An OutputInterface instance
 191       */
 192  	protected function interact(InputInterface $input, OutputInterface $output): void
 193      {
 194          $helper = $this->getHelper('question');
 195  
 196          $user_ids = $input->getArgument('user_ids');
 197          if (count($user_ids) > 0)
 198          {
 199              $question = new ConfirmationQuestion(
 200                  $this->language->lang('CLI_USER_DELETE_ID_CONFIRM', implode(',', $user_ids)),
 201                  false
 202              );
 203  
 204              if (!$helper->ask($input, $output, $question))
 205              {
 206                  $input->setArgument('user_ids', []);
 207              }
 208          }
 209      }
 210  
 211      /**
 212       * Deletes a bot user
 213       *
 214       * @param array $user_row
 215       * @return void
 216       */
 217  	protected function delete_bot_user(array $user_row): void
 218      {
 219          $delete_tables = [$this->bots_table, $this->user_group_table, $this->users_table];
 220          foreach ($delete_tables as $table)
 221          {
 222              $sql = "DELETE FROM $table
 223                  WHERE user_id = " . (int) $user_row['user_id'];
 224              $this->db->sql_query($sql);
 225          }
 226      }
 227  }


Generated: Mon Nov 25 19:05:08 2024 Cross-referenced by PHPXref 0.7.1