ILIAS  release_8 Revision v8.24
class.ilFileDataForum.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
27{
28 private const FORUM_PATH = 'forum';
29
30 private int $obj_id;
31 private int $pos_id;
32 private string $forum_path;
35
36 public function __construct(int $a_obj_id = 0, int $a_pos_id = 0)
37 {
38 global $DIC;
39 $this->main_tpl = $DIC->ui()->mainTemplate();
40
41 $this->error = $DIC['ilErr'];
42
44 $this->forum_path = $this->getPath() . '/' . self::FORUM_PATH;
45
46 if (!$this->checkForumPath()) {
47 $this->initDirectory();
48 }
49 $this->obj_id = $a_obj_id;
50 $this->pos_id = $a_pos_id;
51 }
52
53 public function getObjId(): int
54 {
55 return $this->obj_id;
56 }
57
58 public function getPosId(): int
59 {
60 return $this->pos_id;
61 }
62
63 public function setPosId(int $a_id): void
64 {
65 $this->pos_id = $a_id;
66 }
67
68 public function getForumPath(): string
69 {
70 return $this->forum_path;
71 }
72
76 public function getFiles(): array
77 {
78 $directory_iterator = new DirectoryIterator($this->forum_path);
79 $filter_iterator = new RegexIterator($directory_iterator, "/^{$this->obj_id}_(.+)$/");
80
81 $files = [];
82 foreach ($filter_iterator as $file) {
84 if (!$file->isFile()) {
85 continue;
86 }
87
88 [$obj_id, $rest] = explode('_', $file->getFilename(), 2);
89 if ((int) $obj_id === $this->obj_id) {
90 $files[] = [
91 'path' => $file->getPathname(),
92 'md5' => md5($this->obj_id . '_' . $rest),
93 'name' => $rest,
94 'size' => $file->getSize(),
95 'ctime' => date('Y-m-d H:i:s', $file->getCTime())
96 ];
97 }
98 }
99
100 return $files;
101 }
102
106 public function getFilesOfPost(): array
107 {
108 $directoryIterator = new DirectoryIterator($this->forum_path);
109 $filterIterator = new RegexIterator($directoryIterator, "/^{$this->obj_id}_{$this->getPosId()}_(.+)$/");
110
111 $files = array();
112 foreach ($filterIterator as $file) {
113 if ($file->isFile()) {
114 list($obj_id, $pos_id, $rest) = explode('_', $file->getFilename(), 3);
115 $files[$rest] = array(
116 'path' => $file->getPathname(),
117 'md5' => md5($this->obj_id . '_' . $this->pos_id . '_' . $rest),
118 'name' => $rest,
119 'size' => $file->getSize(),
120 'ctime' => date('Y-m-d H:i:s', $file->getCTime())
121 );
122 }
123 }
124
125 return $files;
126 }
127
128 public function moveFilesOfPost(int $a_new_frm_id = 0): bool
129 {
130 if ($a_new_frm_id) {
131 $directory_iterator = new DirectoryIterator($this->forum_path);
132 $filter_iterator = new RegexIterator($directory_iterator, "/^{$this->obj_id}_(\d+)_(.+)$/");
133
134 foreach ($filter_iterator as $file) {
136 if (!$file->isFile()) {
137 continue;
138 }
139
140 [$obj_id, $pos_id, $rest] = explode('_', $file->getFilename(), 3);
141 if ((int) $obj_id !== $this->obj_id || (int) $pos_id !== $this->getPosId()) {
142 continue;
143 }
144
146 $file->getPathname(),
147 $this->forum_path . '/' . $a_new_frm_id . '_' . $this->pos_id . '_' . $rest
148 );
149 }
150
151 return true;
152 }
153
154 return false;
155 }
156
157 public function ilClone(int $a_new_obj_id, int $a_new_pos_id): bool
158 {
159 foreach ($this->getFilesOfPost() as $file) {
160 copy(
161 $this->getForumPath() . '/' . $this->obj_id . '_' . $this->pos_id . '_' . $file['name'],
162 $this->getForumPath() . '/' . $a_new_obj_id . '_' . $a_new_pos_id . '_' . $file['name']
163 );
164 }
165 return true;
166 }
167
168 public function delete(): bool
169 {
170 foreach ($this->getFiles() as $file) {
171 if (is_file($this->getForumPath() . '/' . $this->getObjId() . '_' . $file['name'])) {
172 unlink($this->getForumPath() . '/' . $this->getObjId() . '_' . $file['name']);
173 }
174 }
175
176 return true;
177 }
178
179 public function storeUploadedFile(array $files): bool
180 {
181 if (isset($files['name']) && is_array($files['name'])) {
182 foreach ($files['name'] as $index => $name) {
183 $name = rtrim($name, '/');
185 $temp_name = $files['tmp_name'][$index];
186 $error = $files['error'][$index];
187
188 if ($filename !== '' && $temp_name !== '' && (int) $error === 0) {
189 $path = $this->getForumPath() . '/' . $this->obj_id . '_' . $this->pos_id . '_' . $filename;
190
191 $this->rotateFiles($path);
193 }
194 }
195
196 return true;
197 }
198
199 if (isset($files['name']) && is_string($files['name'])) {
200 $files['name'] = rtrim($files['name'], '/');
202 $temp_name = $files['tmp_name'];
203
204 $path = $this->getForumPath() . '/' . $this->obj_id . '_' . $this->pos_id . '_' . $filename;
205
206 $this->rotateFiles($path);
208
209 return true;
210 }
211
212 return false;
213 }
214
215 public function unlinkFile(string $a_filename): bool
216 {
217 if (is_file($this->forum_path . '/' . $this->obj_id . '_' . $this->pos_id . '_' . $a_filename)) {
218 return unlink($this->forum_path . '/' . $this->obj_id . '_' . $this->pos_id . '_' . $a_filename);
219 }
220
221 return false;
222 }
223
228 public function getFileDataByMD5Filename(string $hashedFilename): ?array
229 {
230 $files = ilFileUtils::getDir($this->forum_path);
231 foreach ($files as $file) {
232 if ($file['type'] === 'file' && md5($file['entry']) === $hashedFilename) {
233 return [
234 'path' => $this->forum_path . '/' . $file['entry'],
235 'filename' => $file['entry'],
236 'clean_filename' => str_replace($this->obj_id . '_' . $this->pos_id . '_', '', $file['entry'])
237 ];
238 }
239 }
240
241 return null;
242 }
243
248 public function unlinkFilesByMD5Filenames($hashedFilenameOrFilenames): bool
249 {
250 $files = ilFileUtils::getDir($this->forum_path);
251 if (is_array($hashedFilenameOrFilenames)) {
252 foreach ($files as $file) {
253 if ($file['type'] === 'file' && in_array(md5($file['entry']), $hashedFilenameOrFilenames, true)) {
254 unlink($this->forum_path . '/' . $file['entry']);
255 }
256 }
257
258 return true;
259 }
260
261 foreach ($files as $file) {
262 if ($file['type'] === 'file' && md5($file['entry']) === $hashedFilenameOrFilenames) {
263 return unlink($this->forum_path . '/' . $file['entry']);
264 }
265 }
266
267 return false;
268 }
269
270 private function checkForumPath(): bool
271 {
272 if (!is_dir($this->getForumPath())) {
273 return false;
274 }
275 $this->checkReadWrite();
276
277 return true;
278 }
279
280 private function checkReadWrite(): void
281 {
282 if (!is_writable($this->forum_path) || !is_readable($this->forum_path)) {
283 $this->error->raiseError('Forum directory is not readable/writable by webserver', $this->error->FATAL);
284 }
285 }
286
287 private function initDirectory(): void
288 {
289 if (is_writable($this->getPath()) && mkdir($this->getPath() . '/' . self::FORUM_PATH) && chmod(
290 $this->getPath() . '/' . self::FORUM_PATH,
291 0755
292 )) {
293 $this->forum_path = $this->getPath() . '/' . self::FORUM_PATH;
294 }
295 }
296
297 private function rotateFiles(string $a_path): void
298 {
299 if (is_file($a_path)) {
300 $this->rotateFiles($a_path . '.old');
301 ilFileUtils::rename($a_path, $a_path . '.old');
302 }
303 }
304
305 public function deliverFile(string $file): void
306 {
307 global $DIC;
308
309 if (($path = $this->getFileDataByMD5Filename($file)) !== null) {
310 ilFileDelivery::deliverFileLegacy($path['path'], $path['clean_filename']);
311 } else {
312 $this->main_tpl->setOnScreenMessage('failure', $DIC->lanuage()->txt('error_reading_file'), true);
313 }
314 }
315
316 public function deliverZipFile(): bool
317 {
318 global $DIC;
319
320 $zip_file = $this->createZipFile();
321 if (!$zip_file) {
322 $this->main_tpl->setOnScreenMessage('failure', $DIC->language()->txt('error_reading_file'), true);
323 return false;
324 }
325
326 $post = new ilForumPost($this->getPosId());
327 ilFileDelivery::deliverFileLegacy($zip_file, $post->getSubject() . '.zip', '', false, true, false);
328 ilFileUtils::delDir($this->getForumPath() . '/zip/' . $this->getObjId() . '_' . $this->getPosId());
329 $DIC->http()->close();
330 return true; // never
331 }
332
333 protected function createZipFile(): ?string
334 {
335 $filesOfPost = $this->getFilesOfPost();
336 ksort($filesOfPost);
337
338 ilFileUtils::makeDirParents($this->getForumPath() . '/zip/' . $this->getObjId() . '_' . $this->getPosId());
339 $tmp_dir = $this->getForumPath() . '/zip/' . $this->getObjId() . '_' . $this->getPosId();
340 foreach ($filesOfPost as $file) {
341 copy($file['path'], $tmp_dir . '/' . $file['name']);
342 }
343
344 $zip_file = null;
346 $tmp_dir,
347 $this->getForumPath() . '/zip/' . $this->getObjId() . '_' . $this->getPosId() . '.zip'
348 )) {
349 $zip_file = $this->getForumPath() . '/zip/' . $this->getObjId() . '_' . $this->getPosId() . '.zip';
350 }
351
352 return $zip_file;
353 }
354}
$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 forum object.
__construct(int $a_obj_id=0, int $a_pos_id=0)
unlinkFilesByMD5Filenames($hashedFilenameOrFilenames)
storeUploadedFile(array $files)
unlinkFile(string $a_filename)
getFileDataByMD5Filename(string $hashedFilename)
ilErrorHandling $error
ilClone(int $a_new_obj_id, int $a_new_pos_id)
ilGlobalTemplateInterface $main_tpl
rotateFiles(string $a_path)
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
global $DIC
Definition: feed.php:28
$rest
Definition: goto.php:49
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