ILIAS  release_7 Revision v7.30-3-g800a261c036
class.ilMailAttachmentGUI.php
Go to the documentation of this file.
1<?php declare(strict_types=1);
2/* Copyright (c) 1998-2012 ILIAS open source, Extended GPL, see docs/LICENSE */
3
10{
12 private $tpl;
13
15 private $ctrl;
16
18 private $lng;
19
21 private $user;
22
24 private $toolbar;
25
27 private $umail;
28
30 private $mfile;
31
33 private $request;
34
38 public function __construct()
39 {
40 global $DIC;
41
42 $this->tpl = $DIC->ui()->mainTemplate();
43 $this->ctrl = $DIC->ctrl();
44 $this->lng = $DIC->language();
45 $this->user = $DIC->user();
46 $this->toolbar = $DIC->toolbar();
47 $this->request = $DIC->http()->request();
48
49 $this->ctrl->saveParameter($this, 'mobj_id');
50
51 $this->umail = new ilFormatMail($DIC->user()->getId());
52 $this->mfile = new ilFileDataMail($DIC->user()->getId());
53 }
54
55 public function executeCommand() : void
56 {
57 $forward_class = $this->ctrl->getNextClass($this);
58 switch ($forward_class) {
59 default:
60 if (!($cmd = $this->ctrl->getCmd())) {
61 $cmd = 'showAttachments';
62 }
63
64 $this->$cmd();
65 break;
66 }
67 }
68
69 public function saveAttachments() : void
70 {
71 $files = [];
72
73 // Important: Do not check for uploaded files here, otherwise it is no more possible to remove files (please ignore bug reports like 10137)
74
75 $sizeOfSelectedFiles = 0;
76 $filesOfRequest = (array) ($this->request->getParsedBody()['filename'] ?? []);
77 foreach ($filesOfRequest as $file) {
78 if (file_exists($this->mfile->getMailPath() . '/' . basename($this->user->getId() . '_' . urldecode($file)))) {
79 $files[] = urldecode($file);
80 $sizeOfSelectedFiles += filesize($this->mfile->getMailPath() . '/' . basename($this->user->getId() . '_' . urldecode($file)));
81 }
82 }
83
84 if (
85 null !== $this->mfile->getAttachmentsTotalSizeLimit() &&
86 $files && $sizeOfSelectedFiles > $this->mfile->getAttachmentsTotalSizeLimit()
87 ) {
88 ilUtil::sendFailure($this->lng->txt('mail_max_size_attachments_total_error') . ' ' . ilUtil::formatSize($this->mfile->getAttachmentsTotalSizeLimit()));
89 $this->showAttachments();
90 return;
91 }
92
93 $this->umail->saveAttachments($files);
94
95 $this->ctrl->returnToParent($this);
96 }
97
98 public function cancelSaveAttachments() : void
99 {
100 $this->ctrl->setParameter($this, 'type', 'attach');
101 $this->ctrl->returnToParent($this);
102 }
103
104 public function deleteAttachments() : void
105 {
106 $files = (array) ($this->request->getParsedBody()['filename'] ?? []);
107 if (0 === count($files)) {
108 ilUtil::sendFailure($this->lng->txt('mail_select_one_file'));
109 $this->showAttachments();
110 return;
111 }
112
113 $this->tpl->setTitle($this->lng->txt('mail'));
114
115 $confirmation = new ilConfirmationGUI();
116 $confirmation->setFormAction($this->ctrl->getFormAction($this, 'confirmDeleteAttachments'));
117 $confirmation->setConfirm($this->lng->txt('confirm'), 'confirmDeleteAttachments');
118 $confirmation->setCancel($this->lng->txt('cancel'), 'showAttachments');
119 $confirmation->setHeaderText($this->lng->txt('mail_sure_delete_file'));
120
121 foreach ($files as $filename) {
122 $confirmation->addItem(
123 'filename[]',
126 );
127 }
128
129 $this->tpl->setContent($confirmation->getHtml());
130 $this->tpl->printToStdout();
131 }
132
133 public function confirmDeleteAttachments() : void
134 {
135 $files = (array) ($this->request->getParsedBody()['filename'] ?? []);
136 if (0 === count($files)) {
137 ilUtil::sendInfo($this->lng->txt('mail_select_one_mail'));
138 $this->showAttachments();
139 return;
140 }
141
142 $decodedFiles = [];
143 foreach ($files as $value) {
144 $decodedFiles[] = urldecode($value);
145 }
146
147 $error = $this->mfile->unlinkFiles($decodedFiles);
148 if (strlen($error) > 0) {
149 ilUtil::sendFailure($this->lng->txt('mail_error_delete_file') . ' ' . $error);
150 } else {
151 $mailData = $this->umail->getSavedData();
152 if (is_array($mailData['attachments'])) {
153 $tmp = array();
154 for ($i = 0; $i < count($mailData['attachments']); $i++) {
155 if (!in_array($mailData['attachments'][$i], $decodedFiles)) {
156 $tmp[] = $mailData['attachments'][$i];
157 }
158 }
159 $this->umail->saveAttachments($tmp);
160 }
161
162 ilUtil::sendSuccess($this->lng->txt('mail_files_deleted'));
163 }
164
165 $this->showAttachments();
166 }
167
171 protected function getToolbarForm() : ilPropertyFormGUI
172 {
173 $form = new ilPropertyFormGUI();
174
175 $attachment = new ilFileInputGUI($this->lng->txt('upload'), 'userfile');
176 $attachment->setRequired(true);
177 $attachment->setSize(20);
178 $form->addItem($attachment);
179
180 return $form;
181 }
182
183 public function uploadFile() : void
184 {
185 if (strlen(trim($_FILES['userfile']['name']))) {
186 $form = $this->getToolbarForm();
187 if ($form->checkInput()) {
188 $this->mfile->storeUploadedFile($_FILES['userfile']);
189 ilUtil::sendSuccess($this->lng->txt('saved_successfully'));
190 } elseif ($form->getItemByPostVar('userfile')->getAlert() !== $this->lng->txt("form_msg_file_size_exceeds")) {
191 ilUtil::sendFailure($form->getItemByPostVar('userfile')->getAlert());
192 } else {
193 ilUtil::sendFailure($this->lng->txt('mail_maxsize_attachment_error') . ' ' . ilUtil::formatSize($this->mfile->getUploadLimit()));
194 }
195 } else {
196 ilUtil::sendFailure($this->lng->txt('mail_select_one_file'));
197 }
198
199 $this->showAttachments();
200 }
201
202 public function showAttachments() : void
203 {
204 $this->tpl->setTitle($this->lng->txt('mail'));
205
206 $attachment = new ilFileInputGUI($this->lng->txt('upload'), 'userfile');
207 $attachment->setRequired(true);
208 $attachment->setSize(20);
209 $this->toolbar->setFormAction($this->ctrl->getFormAction($this, 'uploadFile'), true);
210 $this->toolbar->addInputItem($attachment);
211 $this->toolbar->addFormButton($this->lng->txt('upload'), 'uploadFile');
212
213 $table = new ilMailAttachmentTableGUI($this, 'showAttachments');
214
215 $mailData = $this->umail->getSavedData();
216 $files = $this->mfile->getUserFilesData();
217 $data = array();
218 $counter = 0;
219 foreach ($files as $file) {
220 $checked = false;
221 if (is_array($mailData['attachments']) && in_array($file['name'], $mailData['attachments'])) {
222 $checked = true;
223 }
224
225 $data[$counter] = array(
226 'checked' => $checked,
227 'filename' => $file['name'],
228 'filesize' => (int) $file['size'],
229 'filecreatedate' => (int) $file['ctime']
230 );
231
232 ++$counter;
233 }
234 $table->setData($data);
235
236 $this->tpl->setContent($table->getHtml());
237 $this->tpl->printToStdout();
238 }
239}
user()
Definition: user.php:4
$filename
Definition: buildRTE.php:89
An exception for terminatinating execution or to throw for unit testing.
Confirmation screen class.
Class ilFileDataMail.
This class represents a file property in a property form.
Class UserMail this class handles user mails.
__construct()
ilMailAttachmentGUI constructor.
This class represents a property form user interface.
static sendFailure($a_info="", $a_keep=false)
Send Failure Message to Screen.
static formatSize($size, $a_mode='short', $a_lng=null)
Returns the specified file size value in a human friendly form.
static stripSlashes($a_str, $a_strip_html=true, $a_allow="")
strip slashes if magic qoutes is enabled
static sendInfo($a_info="", $a_keep=false)
Send Info Message to Screen.
global $DIC
Definition: goto.php:24
$i
Definition: metadata.php:24
$data
Definition: storeScorm.php:23