ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilChatroomAuthInputGUI.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
30{
31 private const string NAME_AUTH_PROP_1 = 'key';
32 private const string NAME_AUTH_PROP_2 = 'secret';
33 private const array DEFAULT_SHAPE = [
34 self::NAME_AUTH_PROP_1 => '',
35 self::NAME_AUTH_PROP_2 => ''
36 ];
38 protected array $ctrl_path = [];
39 protected int $size = 10;
41 protected array $values = self::DEFAULT_SHAPE;
42 protected bool $isReadOnly = false;
43
44 public function __construct(string $title, string $httpPostVariableName, protected \ILIAS\HTTP\Services $http)
45 {
46 parent::__construct($title, $httpPostVariableName);
47 }
48
49 public function setIsReadOnly(bool $isReadOnly): void
50 {
51 $this->isReadOnly = $isReadOnly;
52 }
53
54 protected function getRandomValues(): void
55 {
56 $response = new stdClass();
57
58 $response->{self::NAME_AUTH_PROP_1} = $this->uuidV4();
59 $response->{self::NAME_AUTH_PROP_2} = $this->uuidV4();
60
61 $responseStream = \ILIAS\Filesystem\Stream\Streams::ofString(json_encode($response, JSON_THROW_ON_ERROR));
62 $this->http->saveResponse(
63 $this->http->response()
64 ->withBody($responseStream)
65 ->withHeader(ResponseHeader::CONTENT_TYPE, 'application/json')
66 );
67 $this->http->sendResponse();
68 $this->http->close();
69 }
70
71 private function uuidV4(): string
72 {
73 return sprintf(
74 '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
75 // 32 bits for "time_low"
76 random_int(0, 0xffff),
77 random_int(0, 0xffff),
78 // 16 bits for "time_mid"
79 random_int(0, 0xffff),
80 // 16 bits for "time_high_and_version",
81 // four most significant bits holds version number 4
82 random_int(0, 0x0fff) | 0x4000,
83 // 16 bits, 8 bits for "clk_seq_hi_res",
84 // 8 bits for "clk_seq_low",
85 // two most significant bits holds zero and one for variant DCE1.1
86 random_int(0, 0x3fff) | 0x8000,
87 // 48 bits for "node"
88 random_int(0, 0xffff),
89 random_int(0, 0xffff),
90 random_int(0, 0xffff)
91 );
92 }
93
97 public function setCtrlPath(array $ctrl_path): void
98 {
99 $this->ctrl_path = $ctrl_path;
100 }
101
102 public function setValueByArray(array $a_values): void
103 {
104 $this->values = [
105 self::NAME_AUTH_PROP_1 => $a_values[$this->getPostVar()][self::NAME_AUTH_PROP_1] ?? '',
106 self::NAME_AUTH_PROP_2 => $a_values[$this->getPostVar()][self::NAME_AUTH_PROP_2] ?? ''
107 ];
108
109 foreach ($this->getSubItems() as $item) {
110 $item->setValueByArray($a_values);
111 }
112 }
113
114 public function checkInput(): bool
115 {
116 $post = $this->http->request()->getParsedBody()[$this->getPostVar()] ?? [];
117
118 if ($this->getRequired() && 2 > count(array_filter(array_map('trim', $post)))) {
119 $this->setAlert($this->lng->txt('msg_input_is_required'));
120 return false;
121 }
122
123 return $this->checkSubItemsInput();
124 }
125
129 public function getInput(): array
130 {
131 $input = self::DEFAULT_SHAPE;
132
133 $as_sanizited_string = $this->refinery->custom()->transformation(function (string $value): string {
134 return $this->stripSlashesAddSpaceFallback($value);
135 });
136
137 $null_to_empty_string = $this->refinery->custom()->transformation(static function ($value): string {
138 if ($value === null) {
139 return '';
140 }
141
142 throw new ilException('Expected null in transformation');
143 });
144
145 $sanizite_as_string = $this->refinery->in()->series([
146 $this->refinery->byTrying([
147 $this->refinery->kindlyTo()->string(),
148 $null_to_empty_string
149 ]),
150 $as_sanizited_string
151 ]);
152
153 if ($this->http->wrapper()->post()->has($this->getPostVar())) {
154 $input = $this->http->wrapper()->post()->retrieve(
155 $this->getPostVar(),
156 $this->refinery->kindlyTo()->recordOf([
157 self::NAME_AUTH_PROP_1 => $sanizite_as_string,
158 self::NAME_AUTH_PROP_2 => $sanizite_as_string
159 ])
160 );
161 }
162
163 return $input;
164 }
165
166 public function insert(ilTemplate $a_tpl): void
167 {
168 $a_tpl->setCurrentBlock('prop_generic');
169 $a_tpl->setVariable('PROP_GENERIC', $this->render());
170 $a_tpl->parseCurrentBlock();
171 }
172
173 public function render(): string
174 {
175 global $DIC;
176
177 $tpl = new ilTemplate('tpl.chatroom_auth_input.html', true, true, 'components/ILIAS/Chatroom');
178
179 for ($i = 1, $iMax = count($this->values); $i <= $iMax; $i++) {
180 $const = 'NAME_AUTH_PROP_' . $i;
181 $const_val = constant('self::' . $const);
182
183 $tpl->setVariable('TXT_AUTH_PROP_' . $i, $DIC->language()->txt('chatroom_auth_' . $const_val));
184 $tpl->setVariable('ID_AUTH_PROP_' . $i, $const_val);
185 $tpl->setVariable('NAME_AUTH_PROP_' . $i, $const_val);
186 $tpl->setVariable('VALUE_AUTH_PROP_' . $i, $this->values[$const_val]);
187 }
188
189 if (!$this->isReadOnly && !$this->getDisabled()) {
190 for ($i = 1, $iMax = count($this->values); $i <= $iMax; $i++) {
191 $const = 'NAME_AUTH_PROP_' . $i;
192 $const_val = constant('self::' . $const);
193
194 $tpl->setVariable('ID_AUTH_PROP_' . $i . '_BTN', $const_val);
195 }
196
197 $DIC->ctrl()->setParameterByClass('ilformpropertydispatchgui', 'postvar', $this->getPostVar());
198 $tpl->setVariable(
199 'URL',
200 $DIC->ctrl()->getLinkTargetByClass($this->ctrl_path, 'getRandomValues', '', true)
201 );
202 $tpl->setVariable('ID_BTN', $this->getFieldId() . '_btn');
203 $tpl->setVariable('TXT_BTN', $DIC->language()->txt('chatroom_auth_btn_txt'));
204 }
205
206 $tpl->setVariable('POST_VAR', $this->getPostVar());
207 $tpl->setVariable('SIZE', $this->getSize());
208
209 if ($this->getDisabled()) {
210 $tpl->setVariable('DISABLED', ' disabled="disabled"');
211 }
212
213 return $tpl->get();
214 }
215
216 public function getSize(): int
217 {
218 return $this->size;
219 }
220
221 public function setSize(int $size): void
222 {
223 $this->size = $size;
224 }
225}
setVariable($variable, $value='')
Sets a variable value.
Definition: IT.php:544
static ofString(string $string)
Creates a new stream with an initial value.
Definition: Streams.php:41
Class ilChatroomAuthInputGUI.
checkInput()
Check input, strip slashes etc.
__construct(string $title, string $httpPostVariableName, protected \ILIAS\HTTP\Services $http)
Base class for ILIAS Exception handling.
This class represents a property that may include a sub form.
special template class to simplify handling of ITX/PEAR
setCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
parseCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
Interface ResponseHeader.
$post
Definition: ltitoken.php:46
static http()
Fetches the global http state from ILIAS.
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.
global $DIC
Definition: shib_login.php:26
$response
Definition: xapitoken.php:93