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