ILIAS  release_8 Revision v8.24
class.ilFileDataForumDrafts.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
27{
28 private int $obj_id;
29 private int $draft_id;
30 private string $drafts_path;
34
35 public function __construct(int $obj_id, int $draft_id)
36 {
37 global $DIC;
38 $this->main_tpl = $DIC->ui()->mainTemplate();
39
40 $this->lng = $DIC->language();
41 $this->error = $DIC['ilErr'];
42
43 $this->obj_id = $obj_id;
44 $this->draft_id = $draft_id;
45
47 $this->drafts_path = $this->getPath() . '/forum/drafts';
48
49 if (!$this->checkForumDraftsPath()) {
50 $this->initDirectory();
51 }
52 }
53
54 public function getObjId(): int
55 {
56 return $this->obj_id;
57 }
58
59 public function setObjId(int $obj_id): void
60 {
61 $this->obj_id = $obj_id;
62 }
63
64 public function getDraftId(): int
65 {
66 return $this->draft_id;
67 }
68
69 public function setDraftId(int $draft_id): void
70 {
71 $this->draft_id = $draft_id;
72 }
73
74 public function getDraftsPath(): string
75 {
76 return $this->drafts_path;
77 }
78
82 public function getFiles(): array
83 {
84 $files = [];
85
86 foreach (new DirectoryIterator($this->getDraftsPath() . '/' . $this->getDraftId()) as $file) {
88 if ($file->isDir()) {
89 continue;
90 }
91
92 $files[] = [
93 'path' => $file->getPathname(),
94 'md5' => md5($file->getFilename()),
95 'name' => $file->getFilename(),
96 'size' => $file->getSize(),
97 'ctime' => date('Y-m-d H:i:s', $file->getCTime())
98 ];
99 }
100
101 return $files;
102 }
103
107 public function getFilesOfPost(): array
108 {
109 $files = [];
110
111 foreach (new DirectoryIterator($this->getDraftsPath() . '/' . $this->getDraftId()) as $file) {
116 if ($file->isDir()) {
117 continue;
118 }
119
120 $files[$file->getFilename()] = [
121 'path' => $file->getPathname(),
122 'md5' => md5($file->getFilename()),
123 'name' => $file->getFilename(),
124 'size' => $file->getSize(),
125 'ctime' => date('Y-m-d H:i:s', $file->getCTime())
126 ];
127 }
128
129 return $files;
130 }
131
132 public function moveFilesOfDraft(string $forum_path, int $new_post_id): bool
133 {
134 foreach ($this->getFilesOfPost() as $file) {
135 copy(
136 $file['path'],
137 $forum_path . '/' . $this->obj_id . '_' . $new_post_id . '_' . $file['name']
138 );
139 }
140
141 return true;
142 }
143
144 public function delete(): bool
145 {
146 ilFileUtils::delDir($this->getDraftsPath() . '/' . $this->getDraftId());
147 return true;
148 }
149
150 public function storeUploadedFile(array $files): bool
151 {
152 if (isset($files['name']) && is_array($files['name'])) {
153 foreach ($files['name'] as $index => $name) {
154 $name = rtrim($name, '/');
156 $temp_name = $files['tmp_name'][$index];
157 $error = $files['error'][$index];
158
159 if ($filename !== '' && $temp_name !== '' && (int) $error === 0) {
160 $path = $this->getDraftsPath() . '/' . $this->getDraftId() . '/' . $filename;
161
162 $this->rotateFiles($path);
164 }
165 }
166
167 return true;
168 }
169
170 if (isset($files['name']) && is_string($files['name'])) {
171 $files['name'] = rtrim($files['name'], '/');
173 $temp_name = $files['tmp_name'];
174
175 $path = $this->getDraftsPath() . '/' . $this->getDraftId() . '/' . $filename;
176
177 $this->rotateFiles($path);
179
180 return true;
181 }
182
183 return false;
184 }
185
186 public function unlinkFile(string $a_filename): bool
187 {
188 if (is_file($this->getDraftsPath() . '/' . $this->getDraftId() . '/' . $a_filename)) {
189 return unlink($this->getDraftsPath() . '/' . $this->getDraftId() . '/' . $a_filename);
190 }
191
192 return false;
193 }
194
199 public function getFileDataByMD5Filename(string $hashedFilename): ?array
200 {
201 $files = ilFileUtils::getDir($this->getDraftsPath() . '/' . $this->getDraftId());
202 foreach ($files as $file) {
203 if ($file['type'] === 'file' && md5($file['entry']) === $hashedFilename) {
204 return [
205 'path' => $this->getDraftsPath() . '/' . $this->getDraftId() . '/' . $file['entry'],
206 'filename' => $file['entry'],
207 'clean_filename' => $file['entry']
208 ];
209 }
210 }
211
212 return null;
213 }
214
219 public function unlinkFilesByMD5Filenames($hashedFilenameOrFilenames): bool
220 {
221 $files = ilFileUtils::getDir($this->getDraftsPath() . '/' . $this->getDraftId());
222 if (is_array($hashedFilenameOrFilenames)) {
223 foreach ($files as $file) {
224 if ($file['type'] === 'file' && in_array(md5($file['entry']), $hashedFilenameOrFilenames, true)) {
225 unlink($this->getDraftsPath() . '/' . $this->getDraftId() . '/' . $file['entry']);
226 }
227 }
228
229 return true;
230 }
231
232 foreach ($files as $file) {
233 if ($file['type'] === 'file' && md5($file['entry']) === $hashedFilenameOrFilenames) {
234 return unlink($this->getDraftsPath() . '/' . $this->getDraftId() . '/' . $file['entry']);
235 }
236 }
237
238 return false;
239 }
240
241 public function checkForumDraftsPath(): bool
242 {
243 if (!is_dir($this->getDraftsPath() . '/' . $this->getDraftId())) {
244 return false;
245 }
246 $this->checkReadWrite();
247
248 return true;
249 }
250
251 private function checkReadWrite(): void
252 {
253 if (
254 !is_writable($this->getDraftsPath() . '/' . $this->getDraftId()) ||
255 !is_readable($this->getDraftsPath() . '/' . $this->getDraftId())
256 ) {
257 $this->error->raiseError('Forum directory is not readable/writable by webserver', $this->error->FATAL);
258 }
259 }
260
261 private function initDirectory(): void
262 {
263 if (is_writable($this->getPath()) && ilFileUtils::makeDirParents($this->getDraftsPath() . '/' . $this->getDraftId()) && chmod(
264 $this->getDraftsPath() . '/' . $this->getDraftId(),
265 0755
266 )) {
267 // Empty, whyever @nmatuschek?
268 }
269 }
270
271 private function rotateFiles(string $a_path): void
272 {
273 if (is_file($a_path)) {
274 $this->rotateFiles($a_path . '.old');
275 ilFileUtils::rename($a_path, $a_path . '.old');
276 }
277 }
278
279 public function deliverFile(string $file): void
280 {
281 if (($path = $this->getFileDataByMD5Filename($file)) !== null) {
282 ilFileDelivery::deliverFileLegacy($path['path'], $path['clean_filename']);
283 } else {
284 $this->main_tpl->setOnScreenMessage('failure', $this->lng->txt('error_reading_file'), true);
285 }
286 }
287
288 public function deliverZipFile(): bool
289 {
290 global $DIC;
291
292 $zip_file = $this->createZipFile();
293 if (!$zip_file) {
294 $this->main_tpl->setOnScreenMessage('failure', $this->lng->txt('error_reading_file'), true);
295 return false;
296 }
297
299 ilFileDelivery::deliverFileLegacy($zip_file, $post->getPostSubject() . '.zip', '', false, true, false);
300 ilFileUtils::delDir($this->getDraftsPath() . '/drafts_zip/' . $this->getDraftId());
301 $DIC->http()->close();
302 return true; // never
303 }
304
305 public function createZipFile(): ?string
306 {
307 $filesOfDraft = $this->getFilesOfPost();
308 ilFileUtils::makeDirParents($this->getDraftsPath() . '/drafts_zip/' . $this->getDraftId());
309 $tmp_dir = $this->getDraftsPath() . '/drafts_zip/' . $this->getDraftId();
310
311 if (count($filesOfDraft)) {
312 ksort($filesOfDraft);
313
314 foreach ($filesOfDraft as $file) {
315 copy($file['path'], $tmp_dir . '/' . $file['name']);
316 }
317 }
318
319 $zip_file = null;
320 if (ilFileUtils::zip($tmp_dir, $this->getDraftsPath() . '/drafts_zip/' . $this->getDraftId() . '.zip')) {
321 $zip_file = $this->getDraftsPath() . '/drafts_zip/' . $this->getDraftId() . '.zip';
322 }
323
324 return $zip_file;
325 }
326}
$filename
Definition: buildRTE.php:78
error(string $a_errmsg)
Error Handling & global info handling uses PEAR error class.
This class handles all operations on files for the drafts of a forum object.
getFileDataByMD5Filename(string $hashedFilename)
ilGlobalTemplateInterface $main_tpl
moveFilesOfDraft(string $forum_path, int $new_post_id)
unlinkFilesByMD5Filenames($hashedFilenameOrFilenames)
__construct(int $obj_id, int $draft_id)
static deliverFileLegacy(string $a_file, ?string $a_filename=null, ?string $a_mime=null, ?bool $isInline=false, ?bool $removeAfterDelivery=false, ?bool $a_exit_after=true)
static makeDirParents(string $a_dir)
Create a new directory and all parent directories.
static getDir(string $a_dir, bool $a_rec=false, ?string $a_sub_dir="")
get directory
static zip(string $a_dir, string $a_file, bool $compress_content=false)
zips given directory/file into given zip.file
static rename(string $a_source, string $a_target)
static delDir(string $a_dir, bool $a_clean_only=false)
removes a dir and all its content (subdirs and files) recursively
static _sanitizeFilemame(string $a_filename)
static moveUploadedFile(string $a_file, string $a_name, string $a_target, bool $a_raise_errors=true, string $a_mode="move_uploaded")
move uploaded file
string $path
static newInstanceByDraftId(int $draft_id)
language handling
global $DIC
Definition: feed.php:28
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$post
Definition: ltitoken.php:49
if($format !==null) $name
Definition: metadata.php:247
$index
Definition: metadata.php:145
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc