ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilMailAttachmentGUI.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2012 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4require_once "Services/Mail/classes/class.ilFormatMail.php";
5require_once "Services/Mail/classes/class.ilFileDataMail.php";
6
13{
17 private $tpl;
18
22 private $ctrl;
23
27 private $lng;
28
32 protected $user;
33
37 protected $toolbar;
38
42 private $umail;
43
47 private $mfile;
48
49 public function __construct()
50 {
51 global $DIC;
52
53 $this->tpl = $DIC->ui()->mainTemplate();
54 $this->ctrl = $DIC->ctrl();
55 $this->lng = $DIC->language();
56 $this->user = $DIC->user();
57 $this->toolbar = $DIC->toolbar();
58
59 $this->ctrl->saveParameter($this, 'mobj_id');
60
61 $this->umail = new ilFormatMail($DIC->user()->getId());
62 $this->mfile = new ilFileDataMail($DIC->user()->getId());
63 }
64
65 public function executeCommand()
66 {
67 $forward_class = $this->ctrl->getNextClass($this);
68 switch ($forward_class) {
69 default:
70 if (!($cmd = $this->ctrl->getCmd())) {
71 $cmd = 'showAttachments';
72 }
73
74 $this->$cmd();
75 break;
76 }
77 return true;
78 }
79
80 public function saveAttachments()
81 {
82 $files = array();
83
84 // Important: Do not check for uploaded files here, otherwise it is no more possible to remove files (please ignore bug reports like 10137)
85
86 $size_of_selected_files = 0;
87 if (is_array($_POST['filename']) && count($_POST['filename']) > 0) {
88 foreach ($_POST['filename'] as $file) {
89 if (file_exists($this->mfile->getMailPath() . '/' . basename($this->user->getId() . '_' . urldecode($file)))) {
90 $files[] = urldecode($file);
91 $size_of_selected_files += filesize($this->mfile->getMailPath() . '/' . basename($this->user->getId() . '_' . urldecode($file)));
92 }
93 }
94 }
95
96 if (
97 null !== $this->mfile->getAttachmentsTotalSizeLimit() &&
98 $files && $size_of_selected_files > $this->mfile->getAttachmentsTotalSizeLimit()
99 ) {
100 ilUtil::sendFailure($this->lng->txt('mail_max_size_attachments_total_error') . ' ' . ilUtil::formatSize($this->mfile->getAttachmentsTotalSizeLimit()));
101 $this->showAttachments();
102 return;
103 }
104
105 $this->umail->saveAttachments($files);
106
107 $this->ctrl->returnToParent($this);
108 }
109
110 public function cancelSaveAttachments()
111 {
112 $this->ctrl->setParameter($this, 'type', 'attach');
113 $this->ctrl->returnToParent($this);
114 }
115
116 public function deleteAttachments()
117 {
118 if (!isset($_POST['filename']) || !is_array($_POST['filename']) || !$_POST['filename']) {
119 ilUtil::sendFailure($this->lng->txt('mail_select_one_file'));
120 $this->showAttachments();
121 return;
122 }
123
124 $this->tpl->setTitle($this->lng->txt('mail'));
125
126 require_once 'Services/Utilities/classes/class.ilConfirmationGUI.php';
127 $confirmation = new ilConfirmationGUI();
128 $confirmation->setFormAction($this->ctrl->getFormAction($this, 'confirmDeleteAttachments'));
129 $confirmation->setConfirm($this->lng->txt('confirm'), 'confirmDeleteAttachments');
130 $confirmation->setCancel($this->lng->txt('cancel'), 'showAttachments');
131 $confirmation->setHeaderText($this->lng->txt('mail_sure_delete_file'));
132
133 foreach ($_POST['filename'] as $filename) {
134 $confirmation->addItem('filename[]', ilUtil::stripSlashes($filename), ilUtil::stripSlashes(urldecode($filename)));
135 }
136
137 $this->tpl->setContent($confirmation->getHtml());
138 $this->tpl->show();
139 }
140
141 public function confirmDeleteAttachments()
142 {
143 if (!isset($_POST['filename']) || !is_array($_POST['filename']) || !$_POST['filename']) {
144 ilUtil::sendInfo($this->lng->txt('mail_select_one_mail'));
145 $this->showAttachments();
146 return true;
147 }
148
149 $files = array();
150 foreach ($_POST['filename'] as $value) {
151 $files[] = urldecode($value);
152 }
153
154 if (strlen(($error = $this->mfile->unlinkFiles($files)))) {
155 ilUtil::sendFailure($this->lng->txt('mail_error_delete_file') . ' ' . $error);
156 } else {
157 $mailData = $this->umail->getSavedData();
158 if (is_array($mailData['attachments'])) {
159 $tmp = array();
160 for ($i = 0; $i < count($mailData['attachments']); $i++) {
161 if (!in_array($mailData['attachments'][$i], $files)) {
162 $tmp[] = $mailData['attachments'][$i];
163 }
164 }
165 $this->umail->saveAttachments($tmp);
166 }
167
168 ilUtil::sendSuccess($this->lng->txt('mail_files_deleted'));
169 }
170
171 $this->showAttachments();
172 }
173
177 protected function getToolbarForm()
178 {
179 require_once 'Services/Form/classes/class.ilPropertyFormGUI.php';
180 $form = new ilPropertyFormGUI();
181 $attachment = new ilFileInputGUI($this->lng->txt('upload'), 'userfile');
182 $attachment->setRequired(true);
183 $attachment->setSize(20);
184 $form->addItem($attachment);
185 return $form;
186 }
187
188 public function uploadFile()
189 {
190 if (strlen(trim($_FILES['userfile']['name']))) {
191 $form = $this->getToolbarForm();
192 if ($form->checkInput()) {
193 $this->mfile->storeUploadedFile($_FILES['userfile']);
194 ilUtil::sendSuccess($this->lng->txt('saved_successfully'));
195 } else {
196 if ($form->getItemByPostVar('userfile')->getAlert() != $this->lng->txt("form_msg_file_size_exceeds")) {
197 ilUtil::sendFailure($form->getItemByPostVar('userfile')->getAlert());
198 } else {
199 ilUtil::sendFailure($this->lng->txt('mail_maxsize_attachment_error') . ' ' . ilUtil::formatSize($this->mfile->getUploadLimit()));
200 }
201 }
202 } else {
203 ilUtil::sendFailure($this->lng->txt('mail_select_one_file'));
204 }
205
206 $this->showAttachments();
207 }
208
209 public function showAttachments()
210 {
211 $this->tpl->setTitle($this->lng->txt('mail'));
212
213 require_once 'Services/Form/classes/class.ilFileInputGUI.php';
214 $attachment = new ilFileInputGUI($this->lng->txt('upload'), 'userfile');
215 $attachment->setRequired(true);
216 $attachment->setSize(20);
217 $this->toolbar->setFormAction($this->ctrl->getFormAction($this, 'uploadFile'), true);
218 $this->toolbar->addInputItem($attachment);
219 $this->toolbar->addFormButton($this->lng->txt('upload'), 'uploadFile');
220
221 require_once 'Services/Mail/classes/class.ilMailAttachmentTableGUI.php';
222 $table = new ilMailAttachmentTableGUI($this, 'showAttachments');
223
224 $mailData = $this->umail->getSavedData();
225 $files = $this->mfile->getUserFilesData();
226 $data = array();
227 $counter = 0;
228 foreach ($files as $file) {
229 $checked = false;
230 if (is_array($mailData['attachments']) && in_array($file['name'], $mailData['attachments'])) {
231 $checked = true;
232 }
233
234 $data[$counter] = array(
235 'checked' => $checked,
236 'filename' => $file['name'],
237 'filesize' => (int) $file['size'],
238 'filecreatedate' => (int) $file['ctime']
239 );
240
241 ++$counter;
242 }
243 $table->setData($data);
244
245 $this->tpl->setContent($table->getHtml());
246 $this->tpl->show();
247 }
248}
user()
Definition: user.php:4
$filename
Definition: buildRTE.php:89
$_POST["username"]
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.
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.
$i
Definition: disco.tpl.php:19
$files
Definition: metarefresh.php:49
if(empty($password)) $table
Definition: pwgen.php:24
if(isset($_POST['submit'])) $form
global $DIC
Definition: saml.php:7
$data
Definition: bench.php:6