Add EmailLogListPage
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / data / TMessageQuickReplyGuestDialogAction.class.php
1 <?php
2
3 namespace wcf\data;
4
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;
9 use wcf\system\WCF;
10 use wcf\util\UserRegistrationUtil;
11 use wcf\util\UserUtil;
12
13 /**
14 * Provides methods related to the guest dialog of message quick reply.
15 *
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
20 * @since 3.0
21 */
22 trait TMessageQuickReplyGuestDialogAction
23 {
24 /**
25 * captcha object type used for guests or `null` if no captcha is used or available
26 * @var ObjectType
27 */
28 public $guestDialogCaptchaObjectType;
29
30 /**
31 * list of errors in the guest dialog with field as key and error type as value
32 * @var string[]
33 */
34 public $guestDialogErrors = [];
35
36 /**
37 * @see AbstractDatabaseObjectAction::$parameters
38 */
39 //protected $parameters = [];
40
41 /**
42 * Reads a string value and validates it.
43 *
44 * @param string $variableName
45 * @param bool $allowEmpty
46 * @param string $arrayIndex
47 * @see AbstractDatabaseObjectAction::readString()
48 */
49 abstract protected function readString($variableName, $allowEmpty = false, $arrayIndex = '');
50
51 /**
52 * Sets the guest dialog captcha.
53 *
54 * Needs to be called before all other methods in this trait.
55 */
56 protected function setGuestDialogCaptcha()
57 {
58 if (CAPTCHA_TYPE) {
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 . "'");
62 }
63
64 /** @var ICaptchaHandler $processor */
65 $processor = $this->guestDialogCaptchaObjectType->getProcessor();
66
67 if (!$processor->isAvailable()) {
68 $this->guestDialogCaptchaObjectType = null;
69 }
70 }
71 }
72
73 /**
74 * Validates the captcha in the guest dialog.
75 *
76 * @throws \BadMethodCallException
77 * @throws \LogicException
78 */
79 protected function validateGuestDialogCaptcha()
80 {
81 // only relevant for guests
82 if (WCF::getUser()->userID) {
83 throw new \BadMethodCallException("Guest dialogs are only relevant for guests");
84 }
85
86 if (CAPTCHA_TYPE && $this->guestDialogCaptchaObjectType) {
87 /** @var ICaptchaHandler $processor */
88 $processor = $this->guestDialogCaptchaObjectType->getProcessor();
89
90 if ($processor->isAvailable()) {
91 try {
92 $processor->readFormParameters();
93 $processor->validate();
94 } catch (UserInputException $e) {
95 $this->guestDialogErrors[$e->getField()] = $e->getType();
96 }
97 }
98 }
99 }
100
101 /**
102 * Validates the entered username in the guest dialog.
103 *
104 * @return string type of the validation error or empty if no error occurred
105 * @throws \BadMethodCallException
106 */
107 protected function validateGuestDialogUsername()
108 {
109 // only relevant for guests
110 if (WCF::getUser()->userID) {
111 throw new \BadMethodCallException("Guest dialogs are only relevant for guests");
112 }
113
114 try {
115 $this->readString('username', false, 'data');
116
117 if (!UserRegistrationUtil::isValidUsername($this->parameters['data']['username'])) {
118 throw new UserInputException('username', 'invalid');
119 }
120 if (!UserUtil::isAvailableUsername($this->parameters['data']['username'])) {
121 throw new UserInputException('username', 'notUnique');
122 }
123 } catch (UserInputException $e) {
124 $this->guestDialogErrors['username'] = $e->getType();
125 }
126 }
127 }