2 namespace wcf\acp\form
;
3 use wcf\data\user\group\UserGroup
;
4 use wcf\data\user\UserAction
;
5 use wcf\form\AbstractForm
;
6 use wcf\system\database\util\PreparedStatementConditionBuilder
;
7 use wcf\system\exception\UserInputException
;
8 use wcf\system\language\LanguageFactory
;
10 use wcf\util\ArrayUtil
;
11 use wcf\util\StringUtil
;
12 use wcf\util\UserUtil
;
15 * Shows the user add form.
18 * @copyright 2001-2014 WoltLab GmbH
19 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
20 * @package com.woltlab.wcf
21 * @subpackage acp.form
22 * @category Community Framework
24 class UserAddForm
extends UserOptionListForm
{
26 * @see \wcf\page\AbstractPage::$activeMenuItem
28 public $activeMenuItem = 'wcf.acp.menu.link.user.add';
31 * @see \wcf\page\AbstractPage::$neededPermissions
33 public $neededPermissions = array('admin.user.canAddUser');
39 public $username = '';
48 * confirmed email address
51 public $confirmEmail = '';
57 public $password = '';
60 * confirmed user password
63 public $confirmPassword = '';
69 public $groupIDs = array();
75 public $languageID = 0;
81 public $visibleLanguages = array();
87 protected $userTitle = '';
93 public $signature = '';
99 public $signatureEnableSmilies = 1;
105 public $signatureEnableBBCodes = 1;
111 public $signatureEnableHtml = 0;
114 * true to disable this signature
117 public $disableSignature = 0;
123 public $disableSignatureReason = '';
126 * date when the signature will be enabled again
129 public $disableSignatureExpires = '';
132 * @see \wcf\form\IForm::readFormParameters()
134 public function readFormParameters() {
135 parent
::readFormParameters();
137 if (isset($_POST['username'])) $this->username
= StringUtil
::trim($_POST['username']);
138 if (isset($_POST['email'])) $this->email
= StringUtil
::trim($_POST['email']);
139 if (isset($_POST['confirmEmail'])) $this->confirmEmail
= StringUtil
::trim($_POST['confirmEmail']);
140 if (isset($_POST['password'])) $this->password
= $_POST['password'];
141 if (isset($_POST['confirmPassword'])) $this->confirmPassword
= $_POST['confirmPassword'];
142 if (isset($_POST['groupIDs']) && is_array($_POST['groupIDs'])) $this->groupIDs
= ArrayUtil
::toIntegerArray($_POST['groupIDs']);
143 if (isset($_POST['visibleLanguages']) && is_array($_POST['visibleLanguages'])) $this->visibleLanguages
= ArrayUtil
::toIntegerArray($_POST['visibleLanguages']);
144 if (isset($_POST['languageID'])) $this->languageID
= intval($_POST['languageID']);
145 if (isset($_POST['userTitle'])) $this->userTitle
= $_POST['userTitle'];
147 if (isset($_POST['signature'])) $this->signature
= StringUtil
::trim($_POST['signature']);
149 $this->signatureEnableBBCodes
= $this->signatureEnableSmilies
= 0;
150 if (!empty($_POST['signatureEnableBBCodes'])) $this->signatureEnableBBCodes
= 1;
151 if (!empty($_POST['signatureEnableSmilies'])) $this->signatureEnableSmilies
= 1;
152 if (!empty($_POST['signatureEnableHtml'])) $this->signatureEnableHtml
= 1;
154 if (WCF
::getSession()->getPermission('admin.user.canDisableSignature')) {
155 if (isset($_POST['disableSignatureReason'])) $this->disableSignatureReason
= StringUtil
::trim($_POST['disableSignatureReason']);
156 if (!empty($_POST['disableSignature'])) $this->disableSignature
= 1;
157 if ($this->disableSignature
&& !isset($_POST['disableSignatureNeverExpires'])) {
158 if (isset($_POST['disableSignatureExpires'])) $this->disableSignatureExpires
= StringUtil
::trim($_POST['disableSignatureExpires']);
161 $this->disableSignatureExpires
= '';
167 * @see \wcf\form\IForm::validate()
169 public function validate() {
170 // validate static user options
172 $this->validateUsername($this->username
);
174 catch (UserInputException
$e) {
175 $this->errorType
[$e->getField()] = $e->getType();
179 $this->validateEmail($this->email
, $this->confirmEmail
);
181 catch (UserInputException
$e) {
182 $this->errorType
[$e->getField()] = $e->getType();
186 $this->validatePassword($this->password
, $this->confirmPassword
);
188 catch (UserInputException
$e) {
189 $this->errorType
[$e->getField()] = $e->getType();
192 // validate user groups
193 if (!empty($this->groupIDs
)) {
194 $conditions = new PreparedStatementConditionBuilder();
195 $conditions->add("groupID IN (?)", array($this->groupIDs
));
196 $conditions->add("groupType NOT IN (?)", array(array(UserGroup
::GUESTS
, UserGroup
::EVERYONE
, UserGroup
::USERS
)));
198 $sql = "SELECT groupID
199 FROM wcf".WCF_N
."_user_group
201 $statement = WCF
::getDB()->prepareStatement($sql);
202 $statement->execute($conditions->getParameters());
203 $this->groupIDs
= array();
204 while ($row = $statement->fetchArray()) {
205 if (UserGroup
::isAccessibleGroup(array($row['groupID']))) {
206 $this->groupIDs
[] = $row['groupID'];
211 // validate user language
212 $language = LanguageFactory
::getInstance()->getLanguage($this->languageID
);
213 if ($language === null ||
!$language->languageID
) {
214 // use default language
215 $this->languageID
= LanguageFactory
::getInstance()->getDefaultLanguageID();
218 // validate visible languages
219 foreach ($this->visibleLanguages
as $key => $visibleLanguage) {
220 $language = LanguageFactory
::getInstance()->getLanguage($visibleLanguage);
221 if (!$language->languageID ||
!$language->hasContent
) {
222 unset($this->visibleLanguages
[$key]);
225 if (empty($this->visibleLanguages
) && ($language = LanguageFactory
::getInstance()->getLanguage($this->languageID
)) && $language->hasContent
) {
226 $this->visibleLanguages
[] = $this->languageID
;
229 // validate user title
231 if (mb_strlen($this->userTitle
) > USER_TITLE_MAX_LENGTH
) {
232 throw new UserInputException('userTitle', 'tooLong');
234 if (!StringUtil
::executeWordFilter($this->userTitle
, USER_FORBIDDEN_TITLES
)) {
235 throw new UserInputException('userTitle', 'forbidden');
238 catch (UserInputException
$e) {
239 $this->errorType
[$e->getField()] = $e->getType();
242 // validate dynamic options
247 * @see \wcf\form\IForm::save()
249 public function save() {
250 AbstractForm
::save();
253 $saveOptions = $this->optionHandler
->save();
254 $this->additionalFields
['languageID'] = $this->languageID
;
256 'data' => array_merge($this->additionalFields
, array(
257 'username' => $this->username
,
258 'email' => $this->email
,
259 'password' => $this->password
,
260 'userTitle' => $this->userTitle
,
261 'signature' => $this->signature
,
262 'signatureEnableBBCodes' => $this->signatureEnableBBCodes
,
263 'signatureEnableSmilies' => $this->signatureEnableSmilies
,
264 'signatureEnableHtml' => $this->signatureEnableHtml
266 'groups' => $this->groupIDs
,
267 'languageIDs' => $this->visibleLanguages
,
268 'options' => $saveOptions
271 if (WCF
::getSession()->getPermission('admin.user.canDisableSignature')) {
272 $disableSignatureExpires = 0;
273 if ($this->disableSignatureExpires
) {
274 $disableSignatureExpires = strtotime($this->disableSignatureExpires
);
277 $data['data']['disableSignature'] = $this->disableSignature
;
278 $data['data']['disableSignatureReason'] = $this->disableSignatureReason
;
279 $data['data']['disableSignatureExpires'] = $disableSignatureExpires;
282 $this->objectAction
= new UserAction(array(), 'create', $data);
283 $this->objectAction
->executeAction();
286 // show empty add form
287 WCF
::getTPL()->assign(array(
292 $this->signatureEnableHtml
= $this->disableSignature
= 0;
293 $this->signatureEnableSmilies
= $this->signatureEnableBBCodes
= 1;
294 $this->username
= $this->email
= $this->confirmEmail
= $this->password
= $this->confirmPassword
= $this->userTitle
= '';
295 $this->signature
= $this->disableSignatureReason
= $this->disableSignatureExpires
= '';
296 $this->groupIDs
= array();
297 $this->languageID
= $this->getDefaultFormLanguageID();
298 $this->optionHandler
->resetOptionValues();
302 * Throws a UserInputException if the username is not unique or not valid.
304 * @param string $username
306 protected function validateUsername($username) {
307 if (empty($username)) {
308 throw new UserInputException('username');
311 // check for forbidden chars (e.g. the ",")
312 if (!UserUtil
::isValidUsername($username)) {
313 throw new UserInputException('username', 'notValid');
316 // Check if username exists already.
317 if (!UserUtil
::isAvailableUsername($username)) {
318 throw new UserInputException('username', 'notUnique');
323 * Throws a UserInputException if the email is not unique or not valid.
325 * @param string $email
326 * @param string $confirmEmail
328 protected function validateEmail($email, $confirmEmail) {
330 throw new UserInputException('email');
333 // check for valid email (one @ etc.)
334 if (!UserUtil
::isValidEmail($email)) {
335 throw new UserInputException('email', 'notValid');
338 // Check if email exists already.
339 if (!UserUtil
::isAvailableEmail($email)) {
340 throw new UserInputException('email', 'notUnique');
343 // check confirm input
344 if (mb_strtolower($email) != mb_strtolower($confirmEmail)) {
345 throw new UserInputException('confirmEmail', 'notEqual');
350 * Throws a UserInputException if the password is not valid.
352 * @param string $password
353 * @param string $confirmPassword
355 protected function validatePassword($password, $confirmPassword) {
356 if (empty($password)) {
357 throw new UserInputException('password');
360 // check confirm input
361 if ($password != $confirmPassword) {
362 throw new UserInputException('confirmPassword', 'notEqual');
367 * @see \wcf\page\IPage::readData()
369 public function readData() {
372 $this->readOptionTree();
376 * Reads option tree on page init.
378 protected function readOptionTree() {
379 $this->optionTree
= $this->optionHandler
->getOptionTree();
383 * @see \wcf\page\IPage::assignVariables()
385 public function assignVariables() {
386 parent
::assignVariables();
388 WCF
::getTPL()->assign(array(
389 'username' => $this->username
,
390 'email' => $this->email
,
391 'confirmEmail' => $this->confirmEmail
,
392 'password' => $this->password
,
393 'confirmPassword' => $this->confirmPassword
,
394 'groupIDs' => $this->groupIDs
,
395 'optionTree' => $this->optionTree
,
396 'availableGroups' => $this->getAvailableGroups(),
397 'availableLanguages' => LanguageFactory
::getInstance()->getLanguages(),
398 'languageID' => $this->languageID
,
399 'visibleLanguages' => $this->visibleLanguages
,
400 'availableContentLanguages' => LanguageFactory
::getInstance()->getContentLanguages(),
402 'userTitle' => $this->userTitle
,
403 'signature' => $this->signature
,
404 'signatureEnableBBCodes' => $this->signatureEnableBBCodes
,
405 'signatureEnableSmilies' => $this->signatureEnableSmilies
,
406 'signatureEnableHtml' => $this->signatureEnableHtml
,
407 'disableSignature' => $this->disableSignature
,
408 'disableSignatureReason' => $this->disableSignatureReason
,
409 'disableSignatureExpires' => $this->disableSignatureExpires
414 * @see \wcf\page\IPage::show()
416 public function show() {
417 // get the default language id
418 $this->languageID
= $this->getDefaultFormLanguageID();