5 use wcf\data\
object\type\ObjectType
;
6 use wcf\system\captcha\CaptchaHandler
;
7 use wcf\system\captcha\ICaptchaHandler
;
8 use wcf\system\exception\UserInputException
;
10 use wcf\util\UserRegistrationUtil
;
11 use wcf\util\UserUtil
;
14 * Provides methods related to the guest dialog of message quick reply.
16 * @author Matthias Schmudt
17 * @copyright 2001-2019 WoltLab GmbH
18 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
19 * @package WoltLabSuite\Core\Data
22 trait TMessageQuickReplyGuestDialogAction
25 * captcha object type used for guests or `null` if no captcha is used or available
28 public $guestDialogCaptchaObjectType;
31 * list of errors in the guest dialog with field as key and error type as value
34 public $guestDialogErrors = [];
37 * @see AbstractDatabaseObjectAction::$parameters
39 //protected $parameters = [];
42 * Reads a string value and validates it.
44 * @param string $variableName
45 * @param bool $allowEmpty
46 * @param string $arrayIndex
47 * @see AbstractDatabaseObjectAction::readString()
49 abstract protected function readString($variableName, $allowEmpty = false, $arrayIndex = '');
52 * Sets the guest dialog captcha.
54 * Needs to be called before all other methods in this trait.
56 protected function setGuestDialogCaptcha()
59 $this->guestDialogCaptchaObjectType
= CaptchaHandler
::getInstance()->getObjectTypeByName(CAPTCHA_TYPE
);
60 if ($this->guestDialogCaptchaObjectType
=== null) {
61 throw new \
LogicException("Unknown captcha object type with name '" . CAPTCHA_TYPE
. "'");
64 /** @var ICaptchaHandler $processor */
65 $processor = $this->guestDialogCaptchaObjectType
->getProcessor();
67 if (!$processor->isAvailable()) {
68 $this->guestDialogCaptchaObjectType
= null;
74 * Validates the captcha in the guest dialog.
76 * @throws \BadMethodCallException
77 * @throws \LogicException
79 protected function validateGuestDialogCaptcha()
81 // only relevant for guests
82 if (WCF
::getUser()->userID
) {
83 throw new \
BadMethodCallException("Guest dialogs are only relevant for guests");
86 if (CAPTCHA_TYPE
&& $this->guestDialogCaptchaObjectType
) {
87 /** @var ICaptchaHandler $processor */
88 $processor = $this->guestDialogCaptchaObjectType
->getProcessor();
90 if ($processor->isAvailable()) {
92 $processor->readFormParameters();
93 $processor->validate();
94 } catch (UserInputException
$e) {
95 $this->guestDialogErrors
[$e->getField()] = $e->getType();
102 * Validates the entered username in the guest dialog.
104 * @return string type of the validation error or empty if no error occurred
105 * @throws \BadMethodCallException
107 protected function validateGuestDialogUsername()
109 // only relevant for guests
110 if (WCF
::getUser()->userID
) {
111 throw new \
BadMethodCallException("Guest dialogs are only relevant for guests");
115 $this->readString('username', false, 'data');
117 if (!UserRegistrationUtil
::isValidUsername($this->parameters
['data']['username'])) {
118 throw new UserInputException('username', 'invalid');
120 if (!UserUtil
::isAvailableUsername($this->parameters
['data']['username'])) {
121 throw new UserInputException('username', 'notUnique');
123 } catch (UserInputException
$e) {
124 $this->guestDialogErrors
['username'] = $e->getType();