Merge branch '3.0'
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / form / RegisterActivationForm.class.php
CommitLineData
320f4a6d
MW
1<?php
2namespace wcf\form;
320f4a6d 3use wcf\data\user\User;
cf0b9517 4use wcf\data\user\UserAction;
f084bc1c 5use wcf\system\event\EventHandler;
320f4a6d
MW
6use wcf\system\exception\IllegalLinkException;
7use wcf\system\exception\NamedUserException;
8use wcf\system\exception\UserInputException;
9use wcf\system\request\LinkHandler;
10use wcf\system\WCF;
11use wcf\util\HeaderUtil;
12use wcf\util\StringUtil;
13
14/**
15 * Shows the user activation form.
16 *
17 * @author Marcel Werk
c839bd49 18 * @copyright 2001-2018 WoltLab GmbH
320f4a6d 19 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
e71525e4 20 * @package WoltLabSuite\Core\Form
320f4a6d
MW
21 */
22class RegisterActivationForm extends AbstractForm {
23 /**
24 * username
25 * @var string
26 */
27 public $username = null;
28
29 /**
30 * activation code
31 * @var integer
32 */
33 public $activationCode = '';
34
35 /**
36 * User object
4e25add7 37 * @var User
320f4a6d
MW
38 */
39 public $user = null;
40
f084bc1c 41 /**
0fcfe5f6 42 * @inheritDoc
f084bc1c
MW
43 */
44 public function readParameters() {
45 parent::readParameters();
1a6e8c52 46
f084bc1c
MW
47 if (!empty($_GET['u'])) {
48 $userID = intval($_GET['u']);
49 $this->user = new User($userID);
50 if ($this->user->userID) $this->username = $this->user->username;
51 }
52 if (!empty($_GET['a'])) $this->activationCode = intval($_GET['a']);
53 }
54
320f4a6d 55 /**
0fcfe5f6 56 * @inheritDoc
320f4a6d
MW
57 */
58 public function readFormParameters() {
59 parent::readFormParameters();
60
f084bc1c
MW
61 if (isset($_POST['username'])) {
62 $this->username = StringUtil::trim($_POST['username']);
63 $this->user = User::getUserByUsername($this->username);
64 }
320f4a6d
MW
65 if (isset($_POST['activationCode'])) $this->activationCode = intval($_POST['activationCode']);
66 }
67
68 /**
0fcfe5f6 69 * @inheritDoc
320f4a6d
MW
70 */
71 public function validate() {
f084bc1c 72 EventHandler::getInstance()->fireAction($this, 'validate');
320f4a6d 73
f084bc1c
MW
74 // check given user name
75 if ($this->user === null || !$this->user->userID) {
320f4a6d
MW
76 throw new UserInputException('username', 'notFound');
77 }
78
79 // user is already enabled
80 if ($this->user->activationCode == 0) {
81 throw new NamedUserException(WCF::getLanguage()->get('wcf.user.registerActivation.error.userAlreadyEnabled'));
82 }
83
84 // check given activation code
85 if ($this->user->activationCode != $this->activationCode) {
063bbf46 86 throw new UserInputException('activationCode', 'invalid');
320f4a6d
MW
87 }
88 }
89
90 /**
0fcfe5f6 91 * @inheritDoc
320f4a6d
MW
92 */
93 public function save() {
94 parent::save();
95
96 // enable user
058cbd6a 97 $this->objectAction = new UserAction([$this->user], 'enable', ['skipNotification' => true]);
320f4a6d
MW
98 $this->objectAction->executeAction();
99 $this->saved();
100
101 // forward to index page
344a1ee7 102 HeaderUtil::delayedRedirect(LinkHandler::getInstance()->getLink(), WCF::getLanguage()->getDynamicVariable('wcf.user.registerActivation.success'), 10);
320f4a6d
MW
103 exit;
104 }
105
106 /**
0fcfe5f6 107 * @inheritDoc
320f4a6d
MW
108 */
109 public function assignVariables() {
110 parent::assignVariables();
111
058cbd6a 112 WCF::getTPL()->assign([
320f4a6d
MW
113 'username' => $this->username,
114 'activationCode' => $this->activationCode
058cbd6a 115 ]);
320f4a6d
MW
116 }
117
118 /**
0fcfe5f6 119 * @inheritDoc
320f4a6d
MW
120 */
121 public function show() {
122 if (REGISTER_ACTIVATION_METHOD != 1) {
123 throw new IllegalLinkException();
124 }
125
f084bc1c
MW
126 if (empty($_POST) && $this->user !== null && $this->activationCode != 0) {
127 $this->submit();
128 }
129
320f4a6d
MW
130 parent::show();
131 }
132}