ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilMailAttachmentGUI.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
23 
30 {
33  private ilLanguage $lng;
34  private ilObjUser $user;
40 
41  public function __construct()
42  {
43  global $DIC;
44 
45  $this->tpl = $DIC->ui()->mainTemplate();
46  $this->ctrl = $DIC->ctrl();
47  $this->lng = $DIC->language();
48  $this->user = $DIC->user();
49  $this->toolbar = $DIC->toolbar();
50  $this->http = $DIC->http();
51  $this->refinery = $DIC->refinery();
52 
53  $this->ctrl->saveParameter($this, 'mobj_id');
54 
55  $this->umail = new ilFormatMail($DIC->user()->getId());
56  $this->mfile = new ilFileDataMail($DIC->user()->getId());
57  }
58 
59  public function executeCommand(): void
60  {
61  $forward_class = $this->ctrl->getNextClass($this);
62  switch ($forward_class) {
63  default:
64  if (!($cmd = $this->ctrl->getCmd())) {
65  $cmd = 'showAttachments';
66  }
67 
68  $this->$cmd();
69  break;
70  }
71  }
72 
73  public function saveAttachments(): void
74  {
75  $files = [];
76 
77  // Important: Do not check for uploaded files here,
78  // otherwise it is no more possible to remove files (please ignore bug reports like 10137)
79 
80  $sizeOfSelectedFiles = 0;
81  $filesOfRequest = [];
82  if ($this->http->wrapper()->post()->has('filename')) {
83  $filesOfRequest = $this->http->wrapper()->post()->retrieve(
84  'filename',
85  $this->refinery->kindlyTo()->listOf($this->refinery->kindlyTo()->string())
86  );
87  }
88 
89  foreach ($filesOfRequest as $file) {
90  if (is_file($this->mfile->getMailPath() . '/'
91  . basename($this->user->getId() . '_' . urldecode($file)))
92  ) {
93  $files[] = urldecode($file);
94  $sizeOfSelectedFiles += filesize(
95  $this->mfile->getMailPath() . '/' .
96  basename($this->user->getId() . '_' . urldecode($file))
97  );
98  }
99  }
100 
101  if (
102  $files !== [] &&
103  null !== $this->mfile->getAttachmentsTotalSizeLimit() &&
104  $sizeOfSelectedFiles > $this->mfile->getAttachmentsTotalSizeLimit()
105  ) {
106  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('mail_max_size_attachments_total_error') . ' ' .
107  ilUtil::formatSize((int) $this->mfile->getAttachmentsTotalSizeLimit()));
108  $this->showAttachments();
109  return;
110  }
111 
112  $this->umail->saveAttachments($files);
113 
114  $this->ctrl->returnToParent($this);
115  }
116 
117  public function cancelSaveAttachments(): void
118  {
119  $this->ctrl->setParameter($this, 'type', ilMailFormGUI::MAIL_FORM_TYPE_ATTACH);
120  $this->ctrl->returnToParent($this);
121  }
122 
123  public function deleteAttachments(): void
124  {
125  $files = [];
126  if ($this->http->wrapper()->post()->has('filename')) {
127  $files = $this->http->wrapper()->post()->retrieve(
128  'filename',
129  $this->refinery->kindlyTo()->listOf($this->refinery->kindlyTo()->string())
130  );
131  }
132  if ($files === []) {
133  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('mail_select_one_file'));
134  $this->showAttachments();
135  return;
136  }
137 
138  $this->tpl->setTitle($this->lng->txt('mail'));
139 
140  $confirmation = new ilConfirmationGUI();
141  $confirmation->setFormAction($this->ctrl->getFormAction($this, 'confirmDeleteAttachments'));
142  $confirmation->setConfirm($this->lng->txt('confirm'), 'confirmDeleteAttachments');
143  $confirmation->setCancel($this->lng->txt('cancel'), 'showAttachments');
144  $confirmation->setHeaderText($this->lng->txt('mail_sure_delete_file'));
145 
146  foreach ($files as $filename) {
147  $confirmation->addItem(
148  'filename[]',
149  ilUtil::stripSlashes($filename),
150  ilUtil::stripSlashes(urldecode($filename))
151  );
152  }
153 
154  $this->tpl->setContent($confirmation->getHTML());
155  $this->tpl->printToStdout();
156  }
157 
158  public function confirmDeleteAttachments(): void
159  {
160  $files = [];
161  if ($this->http->wrapper()->post()->has('filename')) {
162  $files = $this->http->wrapper()->post()->retrieve(
163  'filename',
164  $this->refinery->kindlyTo()->listOf($this->refinery->kindlyTo()->string())
165  );
166  }
167 
168  if ($files === []) {
169  $this->tpl->setOnScreenMessage('info', $this->lng->txt('mail_select_one_mail'));
170  $this->showAttachments();
171  return;
172  }
173 
174  $decodedFiles = [];
175  foreach ($files as $value) {
176  $decodedFiles[] = urldecode($value);
177  }
178 
179  $error = $this->mfile->unlinkFiles($decodedFiles);
180  if ($error !== '') {
181  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('mail_error_delete_file') . ' ' . $error);
182  } else {
183  $mail_data = $this->umail->retrieveFromStage();
184  if (is_array($mail_data['attachments'])) {
185  $tmp = [];
186  foreach ($mail_data['attachments'] as $attachment) {
187  if (!in_array($attachment, $decodedFiles, true)) {
188  $tmp[] = $attachment;
189  }
190  }
191  $this->umail->saveAttachments($tmp);
192  }
193 
194  $this->tpl->setOnScreenMessage('success', $this->lng->txt('mail_files_deleted'));
195  }
196 
197  $this->showAttachments();
198  }
199 
200  protected function getToolbarForm(): ilPropertyFormGUI
201  {
202  $form = new ilPropertyFormGUI();
203 
204  $attachment = new ilFileInputGUI($this->lng->txt('upload'), 'userfile');
205  $attachment->setRequired(true);
206  $attachment->setSize(20);
207  $form->addItem($attachment);
208 
209  return $form;
210  }
211 
212  public function uploadFile(): void
213  {
214  if (isset($_FILES['userfile']['name']) && trim($_FILES['userfile']['name']) !== '') {
215  $form = $this->getToolbarForm();
216  if ($form->checkInput()) {
217  $this->mfile->storeUploadedFile($_FILES['userfile']);
218  $this->tpl->setOnScreenMessage('success', $this->lng->txt('saved_successfully'));
219  } elseif ($form->getItemByPostVar('userfile')->getAlert() !==
220  $this->lng->txt("form_msg_file_size_exceeds")
221  ) {
222  $this->tpl->setOnScreenMessage('failure', $form->getItemByPostVar('userfile')->getAlert());
223  } else {
224  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('mail_maxsize_attachment_error') . ' ' .
225  ilUtil::formatSize($this->mfile->getUploadLimit()));
226  }
227  } else {
228  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('mail_select_one_file'));
229  }
230 
231  $this->showAttachments();
232  }
233 
234  public function showAttachments(): void
235  {
236  $this->tpl->setTitle($this->lng->txt('mail'));
237 
238  $attachment = new ilFileInputGUI($this->lng->txt('upload'), 'userfile');
239  $attachment->setRequired(true);
240  $attachment->setSize(20);
241  $this->toolbar->setFormAction($this->ctrl->getFormAction($this, 'uploadFile'), true);
242  $this->toolbar->addInputItem($attachment);
243  $this->toolbar->addFormButton($this->lng->txt('upload'), 'uploadFile');
244 
245  $table = new ilMailAttachmentTableGUI($this, 'showAttachments');
246 
247  $mail_data = $this->umail->retrieveFromStage();
248  $files = $this->mfile->getUserFilesData();
249  $data = [];
250  $counter = 0;
251  foreach ($files as $file) {
252  $checked = false;
253  if (is_array($mail_data['attachments']) && in_array($file['name'], $mail_data['attachments'], true)) {
254  $checked = true;
255  }
256 
257  $data[$counter] = [
258  'checked' => $checked,
259  'filename' => $file['name'],
260  'filesize' => (int) $file['size'],
261  'filecreatedate' => (int) $file['ctime'],
262  ];
263 
264  ++$counter;
265  }
266  $table->setData($data);
267 
268  $this->tpl->setContent($table->getHTML());
269  $this->tpl->printToStdout();
270  }
271 }
Interface GlobalHttpState.
This class handles all operations on files (attachments) in directory ilias_data/mail.
This class represents a file property in a property form.
static stripSlashes(string $a_str, bool $a_strip_html=true, string $a_allow="")
global $DIC
Definition: feed.php:28
static http()
Fetches the global http state from ILIAS.
static formatSize(int $size, string $a_mode='short', ?ilLanguage $a_lng=null)
Returns the specified file size value in a human friendly form.
setRequired(bool $a_required)
$filename
Definition: buildRTE.php:78
ilGlobalTemplateInterface $tpl
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...