[ Index ]

PHP Cross Reference of phpBB-3.2.11-deutsch

title

Body

[close]

/phpbb/console/command/user/ -> add.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\config\config;
  17  use phpbb\console\command\command;
  18  use phpbb\db\driver\driver_interface;
  19  use phpbb\exception\runtime_exception;
  20  use phpbb\language\language;
  21  use phpbb\passwords\manager;
  22  use phpbb\user;
  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\Question;
  27  use Symfony\Component\Console\Style\SymfonyStyle;
  28  
  29  class add extends command
  30  {
  31      /** @var array Array of interactively acquired options */
  32      protected $data;
  33  
  34      /** @var driver_interface */
  35      protected $db;
  36  
  37      /** @var config */
  38      protected $config;
  39  
  40      /** @var language */
  41      protected $language;
  42  
  43      /** @var manager */
  44      protected $password_manager;
  45  
  46      /**
  47       * phpBB root path
  48       *
  49       * @var string
  50       */
  51      protected $phpbb_root_path;
  52  
  53      /**
  54       * PHP extension.
  55       *
  56       * @var string
  57       */
  58      protected $php_ext;
  59  
  60      /**
  61       * Construct method
  62       *
  63       * @param user             $user
  64       * @param driver_interface $db
  65       * @param config           $config
  66       * @param language         $language
  67       * @param manager          $password_manager
  68       * @param string           $phpbb_root_path
  69       * @param string           $php_ext
  70       */
  71  	public function __construct(user $user, driver_interface $db, config $config, language $language, manager $password_manager, $phpbb_root_path, $php_ext)
  72      {
  73          $this->db = $db;
  74          $this->config = $config;
  75          $this->language = $language;
  76          $this->password_manager = $password_manager;
  77          $this->phpbb_root_path = $phpbb_root_path;
  78          $this->php_ext = $php_ext;
  79  
  80          $this->language->add_lang('ucp');
  81          parent::__construct($user);
  82      }
  83  
  84      /**
  85       * Sets the command name and description
  86       *
  87       * @return null
  88       */
  89  	protected function configure()
  90      {
  91          $this
  92              ->setName('user:add')
  93              ->setDescription($this->language->lang('CLI_DESCRIPTION_USER_ADD'))
  94              ->setHelp($this->language->lang('CLI_HELP_USER_ADD'))
  95              ->addOption(
  96                  'username',
  97                  'U',
  98                  InputOption::VALUE_REQUIRED,
  99                  $this->language->lang('CLI_DESCRIPTION_USER_ADD_OPTION_USERNAME')
 100              )
 101              ->addOption(
 102                  'password',
 103                  'P',
 104                  InputOption::VALUE_REQUIRED,
 105                  $this->language->lang('CLI_DESCRIPTION_USER_ADD_OPTION_PASSWORD')
 106              )
 107              ->addOption(
 108                  'email',
 109                  'E',
 110                  InputOption::VALUE_REQUIRED,
 111                  $this->language->lang('CLI_DESCRIPTION_USER_ADD_OPTION_EMAIL')
 112              )
 113              ->addOption(
 114                  'send-email',
 115                  null,
 116                  InputOption::VALUE_NONE,
 117                  $this->language->lang('CLI_DESCRIPTION_USER_ADD_OPTION_NOTIFY')
 118              )
 119          ;
 120      }
 121  
 122      /**
 123       * Executes the command user:add
 124       *
 125       * Adds a new user to the database. If options are not provided, it will ask for the username, password and email.
 126       * User is added to the registered user group. Language and timezone default to $config settings.
 127       *
 128       * @param InputInterface  $input  The input stream used to get the options
 129       * @param OutputInterface $output The output stream, used to print messages
 130       *
 131       * @return int 0 if all is well, 1 if any errors occurred
 132       */
 133  	protected function execute(InputInterface $input, OutputInterface $output)
 134      {
 135          $io = new SymfonyStyle($input, $output);
 136  
 137          try
 138          {
 139              $this->validate_user_data();
 140              $group_id = $this->get_group_id();
 141          }
 142          catch (runtime_exception $e)
 143          {
 144              $io->error($e->getMessage());
 145              return 1;
 146          }
 147  
 148          $user_row = array(
 149              'username'      => $this->data['username'],
 150              'user_password' => $this->password_manager->hash($this->data['new_password']),
 151              'user_email'    => $this->data['email'],
 152              'group_id'      => $group_id,
 153              'user_timezone' => $this->config['board_timezone'],
 154              'user_lang'     => $this->config['default_lang'],
 155              'user_type'     => USER_NORMAL,
 156              'user_regdate'  => time(),
 157          );
 158  
 159          $user_id = (int) user_add($user_row);
 160  
 161          if (!$user_id)
 162          {
 163              $io->error($this->language->lang('AUTH_NO_PROFILE_CREATED'));
 164              return 1;
 165          }
 166  
 167          if ($input->getOption('send-email') && $this->config['email_enable'])
 168          {
 169              $this->send_activation_email($user_id);
 170          }
 171  
 172          $io->success($this->language->lang('CLI_USER_ADD_SUCCESS', $this->data['username']));
 173  
 174          return 0;
 175      }
 176  
 177      /**
 178       * Interacts with the user.
 179       *
 180       * @param InputInterface  $input  An InputInterface instance
 181       * @param OutputInterface $output An OutputInterface instance
 182       */
 183  	protected function interact(InputInterface $input, OutputInterface $output)
 184      {
 185          $helper = $this->getHelper('question');
 186  
 187          $this->data = array(
 188              'username'     => $input->getOption('username'),
 189              'new_password' => $input->getOption('password'),
 190              'email'        => $input->getOption('email'),
 191          );
 192  
 193          if (!$this->data['username'])
 194          {
 195              $question = new Question($this->ask_user('USERNAME'));
 196              $this->data['username'] = $helper->ask($input, $output, $question);
 197          }
 198  
 199          if (!$this->data['new_password'])
 200          {
 201              $question = new Question($this->ask_user('PASSWORD'));
 202              $question->setValidator(function ($value) use ($helper, $input, $output) {
 203                  $question = new Question($this->ask_user('CONFIRM_PASSWORD'));
 204                  $question->setHidden(true);
 205                  if ($helper->ask($input, $output, $question) != $value)
 206                  {
 207                      throw new runtime_exception($this->language->lang('NEW_PASSWORD_ERROR'));
 208                  }
 209                  return $value;
 210              });
 211              $question->setHidden(true);
 212              $question->setMaxAttempts(5);
 213  
 214              $this->data['new_password'] = $helper->ask($input, $output, $question);
 215          }
 216  
 217          if (!$this->data['email'])
 218          {
 219              $question = new Question($this->ask_user('EMAIL_ADDRESS'));
 220              $this->data['email'] = $helper->ask($input, $output, $question);
 221          }
 222      }
 223  
 224      /**
 225       * Validate the submitted user data
 226       *
 227       * @throws runtime_exception if any data fails validation
 228       * @return null
 229       */
 230  	protected function validate_user_data()
 231      {
 232          if (!function_exists('validate_data'))
 233          {
 234              require($this->phpbb_root_path . 'includes/functions_user.' . $this->php_ext);
 235          }
 236  
 237          $error = validate_data($this->data, array(
 238              'username'     => array(
 239                  array('string', false, $this->config['min_name_chars'], $this->config['max_name_chars']),
 240                  array('username', '')),
 241              'new_password' => array(
 242                  array('string', false, $this->config['min_pass_chars'], $this->config['max_pass_chars']),
 243                  array('password')),
 244              'email'        => array(
 245                  array('string', false, 6, 60),
 246                  array('user_email')),
 247          ));
 248  
 249          if ($error)
 250          {
 251              throw new runtime_exception(implode("\n", array_map(array($this->language, 'lang'), $error)));
 252          }
 253      }
 254  
 255      /**
 256       * Get the group id
 257       *
 258       * Go and find in the database the group_id corresponding to 'REGISTERED'
 259       *
 260       * @throws runtime_exception if the group id does not exist in database.
 261       * @return null
 262       */
 263  	protected function get_group_id()
 264      {
 265          $sql = 'SELECT group_id
 266              FROM ' . GROUPS_TABLE . "
 267              WHERE group_name = '" . $this->db->sql_escape('REGISTERED') . "'
 268                  AND group_type = " . GROUP_SPECIAL;
 269          $result = $this->db->sql_query($sql);
 270          $row = $this->db->sql_fetchrow($result);
 271          $this->db->sql_freeresult($result);
 272  
 273          if (!$row || !$row['group_id'])
 274          {
 275              throw new runtime_exception($this->language->lang('NO_GROUP'));
 276          }
 277  
 278          return $row['group_id'];
 279      }
 280  
 281      /**
 282       * Send account activation email
 283       *
 284       * @param int   $user_id The new user's id
 285       * @return null
 286       */
 287  	protected function send_activation_email($user_id)
 288      {
 289          switch ($this->config['require_activation'])
 290          {
 291              case USER_ACTIVATION_SELF:
 292                  $email_template = 'user_welcome_inactive';
 293                  $user_actkey = gen_rand_string(mt_rand(6, 10));
 294              break;
 295              case USER_ACTIVATION_ADMIN:
 296                  $email_template = 'admin_welcome_inactive';
 297                  $user_actkey = gen_rand_string(mt_rand(6, 10));
 298              break;
 299              default:
 300                  $email_template = 'user_welcome';
 301                  $user_actkey = '';
 302              break;
 303          }
 304  
 305          if (!class_exists('messenger'))
 306          {
 307              require($this->phpbb_root_path . 'includes/functions_messenger.' . $this->php_ext);
 308          }
 309  
 310          $messenger = new \messenger(false);
 311          $messenger->template($email_template, $this->user->lang_name);
 312          $messenger->to($this->data['email'], $this->data['username']);
 313          $messenger->anti_abuse_headers($this->config, $this->user);
 314          $messenger->assign_vars(array(
 315              'WELCOME_MSG' => htmlspecialchars_decode($this->language->lang('WELCOME_SUBJECT', $this->config['sitename'])),
 316              'USERNAME'    => htmlspecialchars_decode($this->data['username']),
 317              'PASSWORD'    => htmlspecialchars_decode($this->data['new_password']),
 318              'U_ACTIVATE'  => generate_board_url() . "/ucp.{$this->php_ext}?mode=activate&u=$user_id&k=$user_actkey")
 319          );
 320  
 321          $messenger->send(NOTIFY_EMAIL);
 322      }
 323  
 324      /**
 325       * Helper to translate questions to the user
 326       *
 327       * @param string $key The language key
 328       * @return string The language key translated with a colon and space appended
 329       */
 330  	protected function ask_user($key)
 331      {
 332          return $this->language->lang($key) . $this->language->lang('COLON') . ' ';
 333      }
 334  }


Generated: Wed Nov 11 20:33:01 2020 Cross-referenced by PHPXref 0.7.1