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