Verzeichnisstruktur phpBB-3.2.0
- Veröffentlicht
- 06.01.2017
So funktioniert es
|
Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück |
Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis. |
|
(Beispiel Datei-Icons)
|
Auf das Icon klicken um den Quellcode anzuzeigen |
add.php
001 <?php
002 /**
003 *
004 * This file is part of the phpBB Forum Software package.
005 *
006 * @copyright (c) phpBB Limited <https://www.phpbb.com>
007 * @license GNU General Public License, version 2 (GPL-2.0)
008 *
009 * For full copyright and license information, please see
010 * the docs/CREDITS.txt file.
011 *
012 */
013
014 namespace phpbb\console\command\user;
015
016 use phpbb\config\config;
017 use phpbb\console\command\command;
018 use phpbb\db\driver\driver_interface;
019 use phpbb\exception\runtime_exception;
020 use phpbb\language\language;
021 use phpbb\passwords\manager;
022 use phpbb\user;
023 use Symfony\Component\Console\Input\InputInterface;
024 use Symfony\Component\Console\Input\InputOption;
025 use Symfony\Component\Console\Output\OutputInterface;
026 use Symfony\Component\Console\Question\Question;
027 use Symfony\Component\Console\Style\SymfonyStyle;
028
029 class add extends command
030 {
031 /** @var array Array of interactively acquired options */
032 protected $data;
033
034 /** @var driver_interface */
035 protected $db;
036
037 /** @var config */
038 protected $config;
039
040 /** @var language */
041 protected $language;
042
043 /** @var manager */
044 protected $password_manager;
045
046 /**
047 * phpBB root path
048 *
049 * @var string
050 */
051 protected $phpbb_root_path;
052
053 /**
054 * PHP extension.
055 *
056 * @var string
057 */
058 protected $php_ext;
059
060 /**
061 * Construct method
062 *
063 * @param user $user
064 * @param driver_interface $db
065 * @param config $config
066 * @param language $language
067 * @param manager $password_manager
068 * @param string $phpbb_root_path
069 * @param string $php_ext
070 */
071 public function __construct(user $user, driver_interface $db, config $config, language $language, manager $password_manager, $phpbb_root_path, $php_ext)
072 {
073 $this->db = $db;
074 $this->config = $config;
075 $this->language = $language;
076 $this->password_manager = $password_manager;
077 $this->phpbb_root_path = $phpbb_root_path;
078 $this->php_ext = $php_ext;
079
080 $this->language->add_lang('ucp');
081 parent::__construct($user);
082 }
083
084 /**
085 * Sets the command name and description
086 *
087 * @return null
088 */
089 protected function configure()
090 {
091 $this
092 ->setName('user:add')
093 ->setDescription($this->language->lang('CLI_DESCRIPTION_USER_ADD'))
094 ->setHelp($this->language->lang('CLI_HELP_USER_ADD'))
095 ->addOption(
096 'username',
097 'U',
098 InputOption::VALUE_REQUIRED,
099 $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 }
335