19 declare(strict_types=1);
37 if (!defined(
'MAILPATH')) {
38 define(
'MAILPATH',
'mail');
42 $this->
ilias = $DIC[
'ilias'];
43 $this->db = $DIC->database();
44 $this->tmp_directory = $DIC->filesystem()->temp();
45 $this->storage_directory = $DIC->filesystem()->storage();
53 if (is_writable($this->
getPath())
70 $max_size = $this->
ilias->getSetting(
'mail_maxsize_attach',
'');
71 if ($max_size ===
'') {
75 return (
float) $this->
ilias->getSetting(
'mail_maxsize_attach',
'0') * 1024;
85 return $this->mail_path .
'/' . $this->user_id .
'_';
94 $res = $this->db->queryF(
95 'SELECT path FROM mail_attachment WHERE mail_id = %s',
100 if ($this->db->numRows(
$res) !== 1) {
104 $row = $this->db->fetchAssoc(
$res);
106 $relative_path = $row[
'path'];
110 foreach ($files as $file) {
111 if ($file[
'type'] ===
'file' && md5($file[
'entry']) === $md5FileHash) {
113 'path' => $this->
getMailPath() .
'/' . $relative_path .
'/' . $file[
'entry'],
114 'filename' => $file[
'entry'],
125 $query = $this->db->query(
126 'SELECT path FROM mail_attachment WHERE mail_id = ' . $this->db->quote($mail_id,
'integer')
129 while ($row = $this->db->fetchObject($query)) {
140 if (is_readable(
$path)) {
152 foreach ($a_attachments as $file) {
154 if (!copy(
$path, $this->
getMailPath() .
'/' . $this->user_id .
'_' . $file)) {
155 return 'ERROR: ' . $this->
getMailPath() .
'/' . $this->user_id .
'_' . $file .
' cannot be created';
164 if (is_writable($this->mail_path) && is_readable($this->mail_path)) {
168 $this->
ilias->raiseError(
169 'Mail directory is not readable/writable by webserver: ' .
171 $this->
ilias->error_obj->FATAL
182 return $this->getUnsentFiles();
188 private function getUnsentFiles(): array
192 $iter =
new RegexIterator(
new DirectoryIterator($this->mail_path),
"/^{$this->user_id}_(.+)$/");
193 foreach ($iter as $file) {
195 if (!$file->isFile()) {
199 [$uid, $rest] = explode(
'_', $file->getFilename(), 2);
203 'size' => $file->getSize(),
204 'ctime' => $file->getCTime(),
215 throw new DomainException(
217 'Mail upload limit reached for user with id %s',
226 $abs_path = $this->
getMailPath() .
'/' . $this->user_id .
'_' . $name;
228 $fp = fopen($abs_path,
'wb+');
229 if (!is_resource($fp)) {
232 'Could not read file: %s',
238 if (fwrite($fp, $a_content) ===
false) {
242 'Could not write file: %s',
272 @copy($a_abs_path, $this->
getMailPath() .
'/' . $this->user_id .
'_' . $a_new_name);
279 if (is_file($a_path)) {
292 foreach ($a_filenames as $file) {
303 if (is_file($this->mail_path .
'/' . basename($this->user_id .
'_' . $a_filename))) {
304 return unlink($this->mail_path .
'/' . basename($this->user_id .
'_' . $a_filename));
323 public function saveFiles(
int $a_mail_id, array $a_attachments): void
325 if (!is_numeric($a_mail_id) || $a_mail_id < 1) {
329 foreach ($a_attachments as $attachment) {
330 $this->
saveFile($a_mail_id, $attachment);
336 static $fsstorage_cache = [];
338 $fsstorage_cache[$a_mail_id][$a_usr_id] =
new ilFSStorageMail($a_mail_id, $a_usr_id);
340 return $fsstorage_cache[$a_mail_id][$a_usr_id];
346 public function saveFile(
int $a_mail_id,
string $a_attachment): bool
348 $storage = self::getStorage($a_mail_id, $this->user_id);
350 $storage_directory = $storage->getAbsolutePath();
352 if (!is_dir($storage_directory)) {
357 $this->mail_path .
'/' . $this->user_id .
'_' . $a_attachment,
358 $storage_directory .
'/' . $a_attachment
367 if ($a_files !== []) {
368 foreach ($a_files as $file) {
369 if (!is_file($this->mail_path .
'/' . $this->user_id .
'_' . $file)) {
380 $storage = self::getStorage($a_sent_mail_id, $this->user_id);
381 $this->db->manipulateF(
383 INSERT INTO mail_attachment 384 ( mail_id, path) VALUES (%s, %s)',
386 [$a_mail_id, $storage->getRelativePathExMailDirectory()]
392 $res = $this->db->query(
393 'SELECT path FROM mail_attachment WHERE mail_id = ' . $this->db->quote($a_mail_id,
'integer')
397 while ($row = $this->db->fetchObject(
$res)) {
398 $path = (string) $row->path;
402 $res = $this->db->query(
403 'SELECT COUNT(mail_id) count_mail_id FROM mail_attachment WHERE path = ' .
404 $this->db->quote(
$path,
'text')
408 while ($row = $this->db->fetchObject(
$res)) {
409 $cnt_mail_id = (
int) $row->count_mail_id;
412 if ($cnt_mail_id === 1) {
417 $this->db->manipulateF(
418 'DELETE FROM mail_attachment WHERE mail_id = %s',
437 $umf = ini_get(
'upload_max_filesize');
439 $pms = ini_get(
'post_max_size');
442 $multiplier_a = [
'K' => 1024,
'M' => 1024 * 1024,
'G' => 1024 * 1024 * 1024];
444 $umf_parts = preg_split(
448 PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY
450 $pms_parts = preg_split(
454 PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY
457 if ((is_countable($umf_parts) ? count($umf_parts) : 0) === 2) {
458 $umf = (float) $umf_parts[0] * $multiplier_a[$umf_parts[1]];
460 if ((is_countable($pms_parts) ? count($pms_parts) : 0) === 2) {
461 $pms = (float) $pms_parts[0] * $multiplier_a[$pms_parts[1]];
465 $max_filesize = min($umf, $pms);
467 if (!$max_filesize) {
468 $max_filesize = max($umf, $pms);
471 $this->mail_max_upload_file_size = (
int) $max_filesize;
474 public function onUserDelete():
void 478 $iter =
new RegexIterator(
480 '/^' . $this->user_id .
'_/' 482 foreach ($iter as $file) {
484 if ($file->isFile()) {
485 @unlink($file->getPathname());
493 SELECT DISTINCT(ma1.path) 494 FROM mail_attachment ma1 496 ON mail.mail_id = ma1.mail_id 497 WHERE mail.user_id = %s 498 AND (SELECT COUNT(tmp.path) FROM mail_attachment tmp WHERE tmp.path = ma1.path) = 1 500 $res = $this->db->queryF(
505 while ($row = $this->db->fetchAssoc(
$res)) {
510 RecursiveIteratorIterator::CHILD_FIRST
512 foreach ($iter as $file) {
514 if ($file->isDir()) {
515 @rmdir($file->getPathname());
517 @unlink($file->getPathname());
526 $this->db->manipulateF(
533 WHERE mail.user_id = %s AND mail.mail_id = mail_attachment.mail_id 548 bool $is_draft =
false 559 if ($download_filename ===
'') {
560 $download_filename =
'attachments';
564 $relative_processing_directory = basename($processing_directory);
566 $absolute_zip_directory = $processing_directory .
'/' . $download_filename;
567 $relative_zip_directory = $relative_processing_directory .
'/' . $download_filename;
569 $this->tmp_directory->createDir($relative_zip_directory);
573 $source = str_replace(
582 $source = str_replace(
'//',
'/', $source);
583 if (!$this->storage_directory->has($source)) {
587 $target = $relative_zip_directory .
'/' .
$filename;
589 $stream = $this->storage_directory->readStream($source);
590 $this->tmp_directory->writeStream($target, $stream);
593 $path_to_zip_file = $processing_directory .
'/' . $download_filename .
'.zip';
596 $this->tmp_directory->deleteDir($relative_zip_directory);
599 $processing_directory .
'/' . $download_filename .
'.zip',
deliverAttachmentsAsZip(string $basename, int $mail_id, array $files=[], bool $is_draft=false)
getAttachmentsTotalSizeLimit()
getAttachmentPathAndFilenameByMd5Hash(string $md5FileHash, int $mail_id)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
storeAsAttachment(string $a_filename, string $a_content)
Filesystem $storage_directory
Filesystem $tmp_directory
Interface Observer Contains several chained tasks and infos about them.
adoptAttachments(array $a_attachments, int $a_mail_id)
getAbsoluteAttachmentPoolPathByFilename(string $filename)
Resolves a path for a passed filename in regards of a user's mail attachment pool, meaning attachments not being sent.
static getValidFilename(string $a_filename)
copyAttachmentFile(string $a_abs_path, string $a_new_name)
storeUploadedFile(UploadResult $result)
static getASCIIFilename(string $a_filename)
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
static getStorage(int $a_mail_id, int $a_usr_id)
unlinkFile(string $a_filename)
static deliverFileAttached(string $path_to_file, ?string $download_file_name=null, ?string $mime_type=null, bool $delete_file=false)
assignAttachmentsToDirectory(int $a_mail_id, int $a_sent_mail_id)
rotateFiles(string $a_path)
deassignAttachmentFromDirectory(int $a_mail_id)
static delDir(string $a_dir, bool $a_clean_only=false)
removes a dir and all its content (subdirs and files) recursively
unlinkFiles(array $a_filenames)
Class ilObjForumAdministration.
getAttachmentPathByMailId(int $mail_id)
static getDir(string $a_dir, bool $a_rec=false, ?string $a_sub_dir="")
get directory
static moveUploadedFile(string $a_file, string $a_name, string $a_target, bool $a_raise_errors=true, string $a_mode="move_uploaded")
move uploaded file
getAbsoluteAttachmentPoolPathPrefix()
static _sanitizeFilemame(string $a_filename)
static ilTempnam(?string $a_temp_path=null)
Returns a unique and non existing Path for e temporary file or directory.
int $mail_max_upload_file_size
__construct(Container $dic, ilPlugin $plugin)
static zip(string $a_dir, string $a_file, bool $compress_content=false)
__construct(public int $user_id=0)
static rename(string $a_source, string $a_target)
saveFiles(int $a_mail_id, array $a_attachments)
Saves all attachment files in a specific mail directory .../mail/<calculated_path>/mail_<mail_id>_<us...
getAttachmentPath(string $a_filename, int $a_mail_id)
initAttachmentMaxUploadSize()
checkFilesExist(array $a_files)
saveFile(int $a_mail_id, string $a_attachment)
Save attachment file in a specific mail directory .../mail/<calculated_path>/mail_<mail_id>_<user_id>...
deleteAttachmentDirectory(string $a_rel_path)