ILIAS  release_8 Revision v8.23
ilFileDataForum Class Reference

This class handles all operations on files for the forum object. More...

+ Inheritance diagram for ilFileDataForum:
+ Collaboration diagram for ilFileDataForum:

Public Member Functions

 __construct (int $a_obj_id=0, int $a_pos_id=0)
 
 getObjId ()
 
 getPosId ()
 
 setPosId (int $a_id)
 
 getForumPath ()
 
 getFilesOfPost ()
 
 ilClone (int $a_new_obj_id, int $a_new_pos_id)
 
 delete ()
 
 storeUploadedFile (array $files)
 
 unlinkFile (string $a_filename)
 
 getFileDataByMD5Filename (string $hashedFilename)
 
 unlinkFilesByMD5Filenames ($hashedFilenameOrFilenames)
 
 deliverFile (string $file)
 
 deliverZipFile ()
 
- Public Member Functions inherited from ilFileData
 __construct ()
 
 checkPath (string $a_path)
 
 getPath ()
 
- Public Member Functions inherited from ilFile
 deleteTrailingSlash (string $a_path)
 delete trailing slash of path variables More...
 

Protected Member Functions

 createZipFile ()
 

Private Member Functions

 checkForumPath ()
 
 checkReadWrite ()
 
 initDirectory ()
 
 rotateFiles (string $a_path)
 

Private Attributes

const FORUM_PATH = 'forum'
 
int $obj_id
 
int $pos_id
 
string $forum_path
 
ilErrorHandling $error
 
ilGlobalTemplateInterface $main_tpl
 

Additional Inherited Members

- Protected Attributes inherited from ilFile
string $path
 

Detailed Description

This class handles all operations on files for the forum object.

Author
Stefan Meyer meyer.nosp@m.@lei.nosp@m.fos.c.nosp@m.om

Definition at line 26 of file class.ilFileDataForum.php.

Constructor & Destructor Documentation

◆ __construct()

ilFileDataForum::__construct ( int  $a_obj_id = 0,
int  $a_pos_id = 0 
)

Definition at line 36 of file class.ilFileDataForum.php.

References $DIC, ILIAS\GlobalScreen\Provider\__construct(), checkForumPath(), ilFileData\getPath(), and initDirectory().

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  }
global $DIC
Definition: feed.php:28
__construct(Container $dic, ilPlugin $plugin)
+ Here is the call graph for this function:

Member Function Documentation

◆ checkForumPath()

ilFileDataForum::checkForumPath ( )
private

Definition at line 270 of file class.ilFileDataForum.php.

References checkReadWrite(), and getForumPath().

Referenced by __construct().

270  : bool
271  {
272  if (!is_dir($this->getForumPath())) {
273  return false;
274  }
275  $this->checkReadWrite();
276 
277  return true;
278  }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ checkReadWrite()

ilFileDataForum::checkReadWrite ( )
private

Definition at line 280 of file class.ilFileDataForum.php.

Referenced by checkForumPath().

280  : 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  }
+ Here is the caller graph for this function:

◆ createZipFile()

ilFileDataForum::createZipFile ( )
protected

Definition at line 333 of file class.ilFileDataForum.php.

References getFilesOfPost(), getForumPath(), getObjId(), getPosId(), ilFileUtils\makeDirParents(), and ilFileUtils\zip().

Referenced by deliverZipFile().

333  : ?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;
345  if (ilFileUtils::zip(
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  }
static makeDirParents(string $a_dir)
Create a new directory and all parent directories.
static zip(string $a_dir, string $a_file, bool $compress_content=false)
zips given directory/file into given zip.file
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ delete()

ilFileDataForum::delete ( )

Definition at line 168 of file class.ilFileDataForum.php.

References getForumPath(), and getObjId().

Referenced by ilObjForumGUI\publishDraftObject().

168  : 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  }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ deliverFile()

ilFileDataForum::deliverFile ( string  $file)

Definition at line 305 of file class.ilFileDataForum.php.

References $DIC, ilFile\$path, ilFileDelivery\deliverFileLegacy(), and getFileDataByMD5Filename().

305  : 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  }
getFileDataByMD5Filename(string $hashedFilename)
static deliverFileLegacy(string $a_file, ?string $a_filename=null, ?string $a_mime=null, ?bool $isInline=false, ?bool $removeAfterDelivery=false, ?bool $a_exit_after=true)
global $DIC
Definition: feed.php:28
string $path
+ Here is the call graph for this function:

◆ deliverZipFile()

ilFileDataForum::deliverZipFile ( )

Definition at line 316 of file class.ilFileDataForum.php.

References $DIC, $post, createZipFile(), ilFileUtils\delDir(), ilFileDelivery\deliverFileLegacy(), getForumPath(), getObjId(), and getPosId().

316  : 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  }
static deliverFileLegacy(string $a_file, ?string $a_filename=null, ?string $a_mime=null, ?bool $isInline=false, ?bool $removeAfterDelivery=false, ?bool $a_exit_after=true)
global $DIC
Definition: feed.php:28
static delDir(string $a_dir, bool $a_clean_only=false)
removes a dir and all its content (subdirs and files) recursively
$post
Definition: ltitoken.php:49
+ Here is the call graph for this function:

◆ getFileDataByMD5Filename()

ilFileDataForum::getFileDataByMD5Filename ( string  $hashedFilename)
Parameters
string$hashedFilename
Returns
array{path: string, filename: string, clean_filename: string}|null

Definition at line 228 of file class.ilFileDataForum.php.

References ilFileUtils\getDir().

Referenced by deliverFile().

228  : ?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  }
static getDir(string $a_dir, bool $a_rec=false, ?string $a_sub_dir="")
get directory
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getFilesOfPost()

ilFileDataForum::getFilesOfPost ( )
Returns
array<string, array{path: string, md5: string, name: string, size: int, ctime: string}>

Definition at line 106 of file class.ilFileDataForum.php.

References $obj_id, $pos_id, $rest, getPosId(), ILIAS\Repository\int(), and ilFileUtils\rename().

Referenced by createZipFile(), ilClone(), ilForumNotificationDataProvider\readAttachments(), and ilObjForumGUI\renderPostContent().

106  : 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  }
$rest
Definition: goto.php:49
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getForumPath()

ilFileDataForum::getForumPath ( )

Definition at line 68 of file class.ilFileDataForum.php.

References $forum_path, $obj_id, and $rest.

Referenced by checkForumPath(), createZipFile(), delete(), deliverZipFile(), ilClone(), and storeUploadedFile().

68  : string
69  {
70  return $this->forum_path;
71  }
+ Here is the caller graph for this function:

◆ getObjId()

ilFileDataForum::getObjId ( )

Definition at line 53 of file class.ilFileDataForum.php.

References $obj_id.

Referenced by createZipFile(), delete(), and deliverZipFile().

53  : int
54  {
55  return $this->obj_id;
56  }
+ Here is the caller graph for this function:

◆ getPosId()

ilFileDataForum::getPosId ( )

Definition at line 58 of file class.ilFileDataForum.php.

References $pos_id.

Referenced by createZipFile(), deliverZipFile(), and getFilesOfPost().

58  : int
59  {
60  return $this->pos_id;
61  }
+ Here is the caller graph for this function:

◆ ilClone()

ilFileDataForum::ilClone ( int  $a_new_obj_id,
int  $a_new_pos_id 
)

Definition at line 157 of file class.ilFileDataForum.php.

References getFilesOfPost(), and getForumPath().

Referenced by ilObjForum\update().

157  : 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  }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ initDirectory()

ilFileDataForum::initDirectory ( )
private

Definition at line 287 of file class.ilFileDataForum.php.

References ilFileData\getPath().

Referenced by __construct().

287  : 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  }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ rotateFiles()

ilFileDataForum::rotateFiles ( string  $a_path)
private

Definition at line 297 of file class.ilFileDataForum.php.

References ilFileUtils\rename().

Referenced by storeUploadedFile().

297  : void
298  {
299  if (is_file($a_path)) {
300  $this->rotateFiles($a_path . '.old');
301  ilFileUtils::rename($a_path, $a_path . '.old');
302  }
303  }
rotateFiles(string $a_path)
static rename(string $a_source, string $a_target)
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setPosId()

ilFileDataForum::setPosId ( int  $a_id)

Definition at line 63 of file class.ilFileDataForum.php.

63  : void
64  {
65  $this->pos_id = $a_id;
66  }

◆ storeUploadedFile()

ilFileDataForum::storeUploadedFile ( array  $files)

Definition at line 179 of file class.ilFileDataForum.php.

References $filename, $index, $name, ilFile\$path, ilFileUtils\_sanitizeFilemame(), getForumPath(), ilFileUtils\moveUploadedFile(), and rotateFiles().

Referenced by ilObjForumGUI\createThread(), ilObjForumGUI\publishDraftObject(), and ilObjForumGUI\savePostObject().

179  : 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'], '/');
201  $filename = ilFileUtils::_sanitizeFilemame($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  }
rotateFiles(string $a_path)
$index
Definition: metadata.php:145
if($format !==null) $name
Definition: metadata.php:247
static moveUploadedFile(string $a_file, string $a_name, string $a_target, bool $a_raise_errors=true, string $a_mode="move_uploaded")
move uploaded file
static _sanitizeFilemame(string $a_filename)
$filename
Definition: buildRTE.php:78
ilErrorHandling $error
string $path
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ unlinkFile()

ilFileDataForum::unlinkFile ( string  $a_filename)

Definition at line 215 of file class.ilFileDataForum.php.

215  : 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  }

◆ unlinkFilesByMD5Filenames()

ilFileDataForum::unlinkFilesByMD5Filenames (   $hashedFilenameOrFilenames)
Parameters
string|string[]$hashedFilenameOrFilenames
Returns
bool

Definition at line 248 of file class.ilFileDataForum.php.

References ilFileUtils\getDir().

248  : 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  }
static getDir(string $a_dir, bool $a_rec=false, ?string $a_sub_dir="")
get directory
+ Here is the call graph for this function:

Field Documentation

◆ $error

ilErrorHandling ilFileDataForum::$error
private

Definition at line 33 of file class.ilFileDataForum.php.

◆ $forum_path

string ilFileDataForum::$forum_path
private

Definition at line 32 of file class.ilFileDataForum.php.

Referenced by getForumPath().

◆ $main_tpl

ilGlobalTemplateInterface ilFileDataForum::$main_tpl
private

Definition at line 34 of file class.ilFileDataForum.php.

◆ $obj_id

int ilFileDataForum::$obj_id
private

Definition at line 30 of file class.ilFileDataForum.php.

Referenced by getFilesOfPost(), getForumPath(), and getObjId().

◆ $pos_id

int ilFileDataForum::$pos_id
private

Definition at line 31 of file class.ilFileDataForum.php.

Referenced by getFilesOfPost(), and getPosId().

◆ FORUM_PATH

const ilFileDataForum::FORUM_PATH = 'forum'
private

Definition at line 28 of file class.ilFileDataForum.php.


The documentation for this class was generated from the following file: