2 namespace wcf\acp\form
;
3 use wcf\data\user\group\UserGroup
;
4 use wcf\data\user\UserAction
;
5 use wcf\data\user\UserEditor
;
6 use wcf\data\user\UserProfileAction
;
7 use wcf\form\AbstractForm
;
8 use wcf\system\database\util\PreparedStatementConditionBuilder
;
9 use wcf\system\exception\UserInputException
;
10 use wcf\system\language\LanguageFactory
;
12 use wcf\util\ArrayUtil
;
13 use wcf\util\StringUtil
;
14 use wcf\util\UserUtil
;
17 * Shows the user add form.
20 * @copyright 2001-2014 WoltLab GmbH
21 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
22 * @package com.woltlab.wcf
23 * @subpackage acp.form
24 * @category Community Framework
26 class UserAddForm
extends UserOptionListForm
{
28 * @see \wcf\page\AbstractPage::$activeMenuItem
30 public $activeMenuItem = 'wcf.acp.menu.link.user.add';
33 * @see \wcf\page\AbstractPage::$neededPermissions
35 public $neededPermissions = array('admin.user.canAddUser');
41 public $username = '';
50 * confirmed email address
53 public $confirmEmail = '';
59 public $password = '';
62 * confirmed user password
65 public $confirmPassword = '';
71 public $groupIDs = array();
77 public $languageID = 0;
83 public $visibleLanguages = array();
89 protected $userTitle = '';
95 public $signature = '';
101 public $signatureEnableSmilies = 1;
107 public $signatureEnableBBCodes = 1;
113 public $signatureEnableHtml = 0;
116 * true to disable this signature
119 public $disableSignature = 0;
125 public $disableSignatureReason = '';
128 * @see \wcf\form\IForm::readFormParameters()
130 public function readFormParameters() {
131 parent
::readFormParameters();
133 if (isset($_POST['username'])) $this->username
= StringUtil
::trim($_POST['username']);
134 if (isset($_POST['email'])) $this->email
= StringUtil
::trim($_POST['email']);
135 if (isset($_POST['confirmEmail'])) $this->confirmEmail
= StringUtil
::trim($_POST['confirmEmail']);
136 if (isset($_POST['password'])) $this->password
= $_POST['password'];
137 if (isset($_POST['confirmPassword'])) $this->confirmPassword
= $_POST['confirmPassword'];
138 if (isset($_POST['groupIDs']) && is_array($_POST['groupIDs'])) $this->groupIDs
= ArrayUtil
::toIntegerArray($_POST['groupIDs']);
139 if (isset($_POST['visibleLanguages']) && is_array($_POST['visibleLanguages'])) $this->visibleLanguages
= ArrayUtil
::toIntegerArray($_POST['visibleLanguages']);
140 if (isset($_POST['languageID'])) $this->languageID
= intval($_POST['languageID']);
141 if (isset($_POST['userTitle'])) $this->userTitle
= $_POST['userTitle'];
143 if (isset($_POST['signature'])) $this->signature
= StringUtil
::trim($_POST['signature']);
145 $this->signatureEnableBBCodes
= $this->signatureEnableSmilies
= 0;
146 if (!empty($_POST['signatureEnableBBCodes'])) $this->signatureEnableBBCodes
= 1;
147 if (!empty($_POST['signatureEnableSmilies'])) $this->signatureEnableSmilies
= 1;
148 if (!empty($_POST['signatureEnableHtml'])) $this->signatureEnableHtml
= 1;
150 if (WCF
::getSession()->getPermission('admin.user.canDisableSignature')) {
151 if (isset($_POST['disableSignatureReason'])) $this->disableSignatureReason
= StringUtil
::trim($_POST['disableSignatureReason']);
152 if (!empty($_POST['disableSignature'])) $this->disableSignature
= 1;
157 * @see \wcf\form\IForm::validate()
159 public function validate() {
160 // validate static user options
162 $this->validateUsername($this->username
);
164 catch (UserInputException
$e) {
165 $this->errorType
[$e->getField()] = $e->getType();
169 $this->validateEmail($this->email
, $this->confirmEmail
);
171 catch (UserInputException
$e) {
172 $this->errorType
[$e->getField()] = $e->getType();
176 $this->validatePassword($this->password
, $this->confirmPassword
);
178 catch (UserInputException
$e) {
179 $this->errorType
[$e->getField()] = $e->getType();
182 // validate user groups
183 if (!empty($this->groupIDs
)) {
184 $conditions = new PreparedStatementConditionBuilder();
185 $conditions->add("groupID IN (?)", array($this->groupIDs
));
186 $conditions->add("groupType NOT IN (?)", array(array(UserGroup
::GUESTS
, UserGroup
::EVERYONE
, UserGroup
::USERS
)));
188 $sql = "SELECT groupID
189 FROM wcf".WCF_N
."_user_group
191 $statement = WCF
::getDB()->prepareStatement($sql);
192 $statement->execute($conditions->getParameters());
193 $this->groupIDs
= array();
194 while ($row = $statement->fetchArray()) {
195 if (UserGroup
::isAccessibleGroup(array($row['groupID']))) {
196 $this->groupIDs
[] = $row['groupID'];
201 // validate user language
202 $language = LanguageFactory
::getInstance()->getLanguage($this->languageID
);
203 if ($language === null ||
!$language->languageID
) {
204 // use default language
205 $this->languageID
= LanguageFactory
::getInstance()->getDefaultLanguageID();
208 // validate visible languages
209 foreach ($this->visibleLanguages
as $key => $visibleLanguage) {
210 $language = LanguageFactory
::getInstance()->getLanguage($visibleLanguage);
211 if (!$language->languageID ||
!$language->hasContent
) {
212 unset($this->visibleLanguages
[$key]);
215 if (empty($this->visibleLanguages
) && ($language = LanguageFactory
::getInstance()->getLanguage($this->languageID
)) && $language->hasContent
) {
216 $this->visibleLanguages
[] = $this->languageID
;
219 // validate user title
221 if (mb_strlen($this->userTitle
) > USER_TITLE_MAX_LENGTH
) {
222 throw new UserInputException('userTitle', 'tooLong');
224 if (!StringUtil
::executeWordFilter($this->userTitle
, USER_FORBIDDEN_TITLES
)) {
225 throw new UserInputException('userTitle', 'forbidden');
228 catch (UserInputException
$e) {
229 $this->errorType
[$e->getField()] = $e->getType();
232 // validate dynamic options
237 * @see \wcf\form\IForm::save()
239 public function save() {
240 AbstractForm
::save();
243 $saveOptions = $this->optionHandler
->save();
244 $this->additionalFields
['languageID'] = $this->languageID
;
246 'data' => array_merge($this->additionalFields
, array(
247 'username' => $this->username
,
248 'email' => $this->email
,
249 'password' => $this->password
,
250 'userTitle' => $this->userTitle
,
251 'signature' => $this->signature
,
252 'signatureEnableBBCodes' => $this->signatureEnableBBCodes
,
253 'signatureEnableSmilies' => $this->signatureEnableSmilies
,
254 'signatureEnableHtml' => $this->signatureEnableHtml
256 'groups' => $this->groupIDs
,
257 'languages' => $this->visibleLanguages
,
258 'options' => $saveOptions
261 if (WCF
::getSession()->getPermission('admin.user.canDisableSignature')) {
262 $data['data']['disableSignature'] = $this->disableSignature
;
263 $data['data']['disableSignatureReason'] = $this->disableSignatureReason
;
266 $this->objectAction
= new UserAction(array(), 'create', $data);
267 $this->objectAction
->executeAction();
270 // show empty add form
271 WCF
::getTPL()->assign(array(
276 $this->username
= $this->email
= $this->confirmEmail
= $this->password
= $this->confirmPassword
= '';
277 $this->groupIDs
= array();
278 $this->languageID
= $this->getDefaultFormLanguageID();
279 $this->optionHandler
->resetOptionValues();
283 * Throws a UserInputException if the username is not unique or not valid.
285 * @param string $username
287 protected function validateUsername($username) {
288 if (empty($username)) {
289 throw new UserInputException('username');
292 // check for forbidden chars (e.g. the ",")
293 if (!UserUtil
::isValidUsername($username)) {
294 throw new UserInputException('username', 'notValid');
297 // Check if username exists already.
298 if (!UserUtil
::isAvailableUsername($username)) {
299 throw new UserInputException('username', 'notUnique');
304 * Throws a UserInputException if the email is not unique or not valid.
306 * @param string $email
307 * @param string $confirmEmail
309 protected function validateEmail($email, $confirmEmail) {
311 throw new UserInputException('email');
314 // check for valid email (one @ etc.)
315 if (!UserUtil
::isValidEmail($email)) {
316 throw new UserInputException('email', 'notValid');
319 // Check if email exists already.
320 if (!UserUtil
::isAvailableEmail($email)) {
321 throw new UserInputException('email', 'notUnique');
324 // check confirm input
325 if (mb_strtolower($email) != mb_strtolower($confirmEmail)) {
326 throw new UserInputException('confirmEmail', 'notEqual');
331 * Throws a UserInputException if the password is not valid.
333 * @param string $password
334 * @param string $confirmPassword
336 protected function validatePassword($password, $confirmPassword) {
337 if (empty($password)) {
338 throw new UserInputException('password');
341 // check confirm input
342 if ($password != $confirmPassword) {
343 throw new UserInputException('confirmPassword', 'notEqual');
348 * @see \wcf\page\IPage::readData()
350 public function readData() {
353 $this->readOptionTree();
357 * Reads option tree on page init.
359 protected function readOptionTree() {
360 $this->optionTree
= $this->optionHandler
->getOptionTree();
364 * @see \wcf\page\IPage::assignVariables()
366 public function assignVariables() {
367 parent
::assignVariables();
369 WCF
::getTPL()->assign(array(
370 'username' => $this->username
,
371 'email' => $this->email
,
372 'confirmEmail' => $this->confirmEmail
,
373 'password' => $this->password
,
374 'confirmPassword' => $this->confirmPassword
,
375 'groupIDs' => $this->groupIDs
,
376 'optionTree' => $this->optionTree
,
377 'availableGroups' => $this->getAvailableGroups(),
378 'availableLanguages' => LanguageFactory
::getInstance()->getLanguages(),
379 'languageID' => $this->languageID
,
380 'visibleLanguages' => $this->visibleLanguages
,
381 'availableContentLanguages' => LanguageFactory
::getInstance()->getContentLanguages(),
383 'userTitle' => $this->userTitle
,
384 'signature' => $this->signature
,
385 'signatureEnableBBCodes' => $this->signatureEnableBBCodes
,
386 'signatureEnableSmilies' => $this->signatureEnableSmilies
,
387 'signatureEnableHtml' => $this->signatureEnableHtml
,
388 'disableSignature' => $this->disableSignature
,
389 'disableSignatureReason' => $this->disableSignatureReason
394 * @see \wcf\page\IPage::show()
396 public function show() {
397 // get the default language id
398 $this->languageID
= $this->getDefaultFormLanguageID();