ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
class.ilMailAttachmentGUI.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
29 
31 {
32  private readonly ilGlobalTemplateInterface $tpl;
33  private readonly ilLanguage $lng;
34  private readonly ilObjUser $user;
35  private readonly ilFormatMail $umail;
36  private readonly ilFileDataMail $mfile;
37  private readonly Refinery $refinery;
38  private readonly \ILIAS\UI\Factory $ui_factory;
39  private readonly \ILIAS\UI\Renderer $ui_renderer;
40  private readonly ilTabsGUI $tabs;
41  private AttachmentManagement $mode = AttachmentManagement::MANAGE;
42 
43  public function __construct()
44  {
45  global $DIC;
46 
48  $this->tpl = $DIC->ui()->mainTemplate();
49  $this->lng = $DIC->language();
50  $this->user = $DIC->user();
51  $this->tabs = $DIC->tabs();
52  $this->refinery = $DIC->refinery();
53  $this->ui_factory = $DIC->ui()->factory();
54  $this->ui_renderer = $DIC->ui()->renderer();
55 
56  $this->ctrl->saveParameter($this, 'mobj_id');
57 
58  $this->umail = new ilFormatMail($DIC->user()->getId());
59  $this->mfile = new ilFileDataMail($DIC->user()->getId());
60  }
61 
62  public function getUnsafeGetCommands(): array
63  {
64  return [
65  'handleTableActions',
66  ];
67  }
68 
69  public function getSafePostCommands(): array
70  {
71  return [];
72  }
73 
74  public function manage(): self
75  {
76  $this->mode = AttachmentManagement::MANAGE;
77  return $this;
78  }
79 
80  public function consume(): self
81  {
82  $this->mode = AttachmentManagement::CONSUME;
83  return $this;
84  }
85 
86  public function executeCommand(): void
87  {
88  if (!($cmd = $this->ctrl->getCmd())) {
89  $cmd = 'showAttachments';
90  }
91 
92  match ($cmd) {
93  AbstractCtrlAwareUploadHandler::CMD_UPLOAD,
94  AbstractCtrlAwareUploadHandler::CMD_INFO,
95  AbstractCtrlAwareUploadHandler::CMD_REMOVE => parent::executeCommand(),
96  default => $this->$cmd()
97  };
98  }
99 
100  public function saveAttachments(): void
101  {
102  $files = [];
103 
104  // Important: Do not check for uploaded files here,
105  // otherwise it is no more possible to remove files (please ignore bug reports like 10137)
106 
107  $sizeOfSelectedFiles = 0;
108  $filesOfRequest = $this->http->wrapper()->query()->retrieve(
109  'mail_attachments_filename',
110  $this->refinery->byTrying([
111  $this->refinery->kindlyTo()->listOf($this->refinery->kindlyTo()->string()),
112  $this->refinery->always([])
113  ])
114  );
115 
116  if ($filesOfRequest !== [] && $filesOfRequest[0] === 'ALL_OBJECTS') {
117  $filesOfRequest = array_map(static function (array $file): string {
118  return $file['name'];
119  }, $this->mfile->getUserFilesData());
120  }
121 
122  foreach ($filesOfRequest as $file) {
123  if (is_file($this->mfile->getMailPath() . '/' . basename($this->user->getId() . '_' . urldecode($file)))) {
124  $files[] = urldecode($file);
125  $sizeOfSelectedFiles += filesize(
126  $this->mfile->getMailPath() . '/' .
127  basename($this->user->getId() . '_' . urldecode($file))
128  );
129  }
130  }
131 
132  if ($files !== [] &&
133  null !== $this->mfile->getAttachmentsTotalSizeLimit() &&
134  $sizeOfSelectedFiles > $this->mfile->getAttachmentsTotalSizeLimit()) {
135  $this->tpl->setOnScreenMessage(
136  'failure',
137  $this->lng->txt('mail_max_size_attachments_total_error') . ' ' .
138  ilUtil::formatSize((int) $this->mfile->getAttachmentsTotalSizeLimit())
139  );
140  $this->showAttachments();
141  return;
142  }
143 
144  $this->umail->saveAttachments($files);
145 
146  $this->ctrl->returnToParent($this);
147  }
148 
149  public function cancelSaveAttachments(): void
150  {
151  $this->ctrl->setParameter($this, 'type', ilMailFormGUI::MAIL_FORM_TYPE_ATTACH);
152  $this->ctrl->returnToParent($this);
153  }
154 
155  public function deleteAttachments(): void
156  {
157  $files = $this->http->wrapper()->query()->retrieve(
158  'mail_attachments_filename',
159  $this->refinery->byTrying([
160  $this->refinery->kindlyTo()->listOf($this->refinery->kindlyTo()->string()),
161  $this->refinery->always([])
162  ])
163  );
164 
165  if ($files !== [] && $files[0] === 'ALL_OBJECTS') {
166  $files = array_map(static function (array $file): string {
167  return $file['name'];
168  }, $this->mfile->getUserFilesData());
169  }
170 
171  if ($files === []) {
172  $this->tpl->setOnScreenMessage('info', $this->lng->txt('select_one'), true);
173  $this->ctrl->redirect($this);
174  }
175 
176  $this->tpl->setTitle($this->lng->txt('mail'));
177 
178  $confirmation = new ilConfirmationGUI();
179  $confirmation->setFormAction($this->ctrl->getFormAction($this, 'confirmDeleteAttachments'));
180  $confirmation->setConfirm($this->lng->txt('confirm'), 'confirmDeleteAttachments');
181  $confirmation->setCancel($this->lng->txt('cancel'), 'showAttachments');
182  $confirmation->setHeaderText($this->lng->txt('mail_sure_delete_file'));
183 
184  foreach ($files as $filename) {
185  $confirmation->addItem(
186  'filename[]',
187  ilUtil::stripSlashes($filename),
188  ilUtil::stripSlashes(urldecode($filename))
189  );
190  }
191 
192  $this->tpl->setContent($confirmation->getHTML());
193  $this->tpl->printToStdout();
194  }
195 
196  public function confirmDeleteAttachments(): void
197  {
198  $files = $this->http->wrapper()->post()->retrieve(
199  'filename',
200  $this->refinery->byTrying([
201  $this->refinery->kindlyTo()->listOf($this->refinery->kindlyTo()->string()),
202  $this->refinery->always([])
203  ])
204  );
205 
206  if ($files === []) {
207  $this->tpl->setOnScreenMessage('info', $this->lng->txt('mail_select_one_mail'));
208  $this->showAttachments();
209  return;
210  }
211 
212  $decodedFiles = [];
213  foreach ($files as $value) {
214  $decodedFiles[] = urldecode($value);
215  }
216 
217  $error = $this->mfile->unlinkFiles($decodedFiles);
218  if ($error !== '') {
219  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('mail_error_delete_file') . ' ' . $error, true);
220  } else {
221  $mail_data = $this->umail->retrieveFromStage();
222  if (is_array($mail_data['attachments'])) {
223  $tmp = [];
224  foreach ($mail_data['attachments'] as $attachment) {
225  if (!in_array($attachment, $decodedFiles, true)) {
226  $tmp[] = $attachment;
227  }
228  }
229  $this->umail->saveAttachments($tmp);
230  }
231 
232  $this->tpl->setOnScreenMessage('success', $this->lng->txt('mail_files_deleted'), true);
233  }
234 
235  $this->ctrl->redirect($this);
236  }
237 
238  public function showAttachments(): void
239  {
240  $this->tpl->setTitle($this->lng->txt('mail'));
241 
242  if ($this->mode === AttachmentManagement::CONSUME) {
243  $this->tabs->clearTargets();
244  $this->tabs->setBackTarget(
245  $this->lng->txt('mail_manage_attachments_back_to_compose'),
246  $this->ctrl->getLinkTarget($this, 'cancelSaveAttachments')
247  );
248  }
249 
250  $components = [];
251  if ($this->mode === AttachmentManagement::MANAGE) {
252  $dropzone = $this->ui_factory
253  ->dropzone()
254  ->file()
255  ->standard(
256  $this->lng->txt('mail_manage_attachments'),
257  $this->lng->txt('mail_manage_attachments_drop_files_msg'),
258  '#',
259  $this->ui_factory->input()->field()->file(
260  $this,
261  $this->lng->txt('file')
262  )->withMaxFiles(42) // The answer to life, universe and the rest
263  )
264  ->withBulky(true)
265  ->withUploadButton(
266  $this->ui_factory->button()->shy(
267  $this->lng->txt('upload'),
268  '#'
269  )
270  );
271  $components[] = $dropzone;
272  }
273 
274  $mail_data = $this->umail->retrieveFromStage();
275  $files = $this->mfile->getUserFilesData();
276  $records = [];
277  $checked_items = [];
278  foreach ($files as $file) {
279  if (is_array($mail_data['attachments']) && in_array($file['name'], $mail_data['attachments'], true)) {
280  $checked_items[] = urlencode($file['name']);
281  }
282 
283  $records[] = [
284  'filename' => $file['name'],
285  'filesize' => (int) $file['size'],
286  'filecreatedate' => (int) $file['ctime'],
287  ];
288  }
289 
290  $table = new MailAttachmentTableGUI(
291  $this,
292  $this->user,
293  $records,
294  $this->ui_factory,
295  $this->ui_renderer,
296  $this->lng,
297  $this->ctrl,
298  $this->http->request(),
299  new ILIAS\Data\Factory(),
300  'handleTableActions',
302  );
303  $components[] = $table->get();
304 
305  $this->tpl->setContent($this->ui_renderer->render($components));
306 
307  if ($this->mode === AttachmentManagement::CONSUME) {
308  // The table above has to be rendered first, because it deselects all checkboxes
309  $this->tpl->addOnLoadCode('
310  const checked_items = ' . json_encode($checked_items, JSON_THROW_ON_ERROR) . ';
311  for (const item of checked_items) {
312  const checkbox = document.querySelector("input[type=\'checkbox\'][value=\'" + item + "\']");
313  if (checkbox) {
314  checkbox.checked = true;
315  }
316  }
317  ');
318  }
319 
320  $this->tpl->printToStdout();
321  }
322 
323  private function handleTableActions(): void
324  {
325  $query = $this->http->wrapper()->query();
326  if (!$query->has('mail_attachments_table_action')) {
327  return;
328  }
329 
330  $action = $query->retrieve('mail_attachments_table_action', $this->refinery->to()->string());
331  match ($action) {
332  'saveAttachments' => $this->saveAttachments(),
333  'deleteAttachments' => $this->deleteAttachments(),
334  default => $this->ctrl->redirect($this),
335  };
336  }
337 
338  protected function getUploadResult(): HandlerResult
339  {
340  $this->upload->process();
341  $array = $this->upload->getResults();
342  $result = end($array);
343 
344  if ($result instanceof UploadResult && $result->isOK()) {
345  $identifier = $this->mfile->storeUploadedFile($result);
346  $status = HandlerResult::STATUS_OK;
347  $message = $this->lng->txt('saved_successfully');
348  $this->tpl->setOnScreenMessage('success', $this->lng->txt('saved_successfully'), true);
349  } else {
350  $status = HandlerResult::STATUS_FAILED;
351  $identifier = '';
352  $message = $result->getStatus()->getMessage();
353  }
354 
355  return new BasicHandlerResult($this->getFileIdentifierParameterName(), $status, $identifier, $message);
356  }
357 
358  protected function getRemoveResult(string $identifier): HandlerResult
359  {
360  throw new DomainException('Not necessary for this handler');
361  }
362 
363  public function getInfoResult(string $identifier): ?FileInfoResult
364  {
365  throw new DomainException('Not necessary for this handler');
366  }
367 
368  public function getInfoForExistingFiles(array $file_ids): array
369  {
370  throw new DomainException('Not necessary for this handler');
371  }
372 
373  public function getFileIdentifierParameterName(): string
374  {
375  return 'userfile';
376  }
377 }
getUnsafeGetCommands()
This method must return a list of unsafe GET commands.
AttachmentManagement $mode
readonly ilFileDataMail $mfile
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
getSafePostCommands()
This method must return a list of safe POST commands.
This class handles all operations on files (attachments) in directory ilias_data/mail.
static stripSlashes(string $a_str, bool $a_strip_html=true, string $a_allow="")
readonly ILIAS UI Renderer $ui_renderer
$components
readonly ilFormatMail $umail
executeCommand()
Since this is a ilCtrl aware UploadHandler executeCommand MUST be implemented.
getInfoResult(string $identifier)
static http()
Fetches the global http state from ILIAS.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static formatSize(int $size, string $a_mode='short', ?ilLanguage $a_lng=null)
Returns the specified file size value in a human friendly form.
global $DIC
Definition: shib_login.php:25
$filename
Definition: buildRTE.php:78
final const MAIL_FORM_TYPE_ATTACH
getInfoForExistingFiles(array $file_ids)
__construct(Container $dic, ilPlugin $plugin)
readonly ILIAS UI Factory $ui_factory
Interface ilCtrlSecurityInterface provides ilCtrl security information.
getRemoveResult(string $identifier)
readonly ilGlobalTemplateInterface $tpl