15require_once(
"./Services/FileSystem/classes/class.ilFileData.php");
 
   16require_once(
"./Services/Utilities/classes/class.ilFileUtils.php");
 
   62        if (!defined(
'MAILPATH')) {
 
   63            define(
'MAILPATH', 
'mail');
 
   66        $this->mail_path = parent::getPath() . 
"/" . 
MAILPATH;
 
   68        $this->user_id = $a_user_id;
 
   70        $this->db = 
$DIC->database();
 
   71        $this->tmpDirectory = 
$DIC->filesystem()->temp();
 
   72        $this->storageDirectory = 
$DIC->filesystem()->storage();
 
   85        if (is_writable($this->
getPath())) {
 
  109        $max_size = $this->
ilias->getSetting(
'mail_maxsize_attach', 
'');
 
  110        if (!strlen($max_size)) {
 
  114        return (
float) $this->
ilias->getSetting(
'mail_maxsize_attach', 0) * 1024;
 
  132        return $this->mail_path . 
'/' . $this->user_id . 
'_';
 
  143        $res = $this->db->queryF(
 
  144            "SELECT path FROM mail_attachment WHERE mail_id = %s",
 
  149        if (1 !== (
int) $this->db->numRows(
$res)) {
 
  150            throw new \OutOfBoundsException();
 
  153        $row = $this->db->fetchAssoc(
$res);
 
  155        $relativePath = $row[
'path'];
 
  159        foreach ($files as $file) {
 
  160            if ($file[
'type'] === 
'file' && md5($file[
'entry']) === $md5FileHash) {
 
  162                    'path' => $this->
getMailPath() . 
'/' . $relativePath . 
'/' . $file[
'entry'],
 
  163                    'filename' => $file[
'entry']
 
  168        throw new \OutOfBoundsException();
 
  177        $query = $this->db->query(
 
  178            "SELECT path FROM mail_attachment WHERE mail_id = " . $this->db->quote($mailId, 
'integer')
 
  181        while ($row = $this->db->fetchObject(
$query)) {
 
  198        if (file_exists(
$path) && is_readable(
$path)) {
 
  213        if (is_array($a_attachments)) {
 
  214            foreach ($a_attachments as $file) {
 
  216                if (!copy(
$path, $this->
getMailPath() . 
'/' . $this->user_id . 
'_' . $file)) {
 
  217                    return "ERROR: $this->getMailPath().'/'.$this->user_id.'_'.$file cannot be created";
 
  221            return "ARRAY REQUIRED";
 
  234        if (is_writable($this->mail_path) && is_readable($this->mail_path)) {
 
  237            $this->
ilias->raiseError(
"Mail directory is not readable/writable by webserver: " . $this->mail_path, $this->
ilias->error_obj->FATAL);
 
  245        return $this->getUnsentFiles();
 
  251    private function getUnsentFiles() : array
 
  255        $iter = 
new RegexIterator(
new DirectoryIterator($this->mail_path), 
"/^{$this->user_id}_(.+)$/");
 
  256        foreach ($iter as $file) {
 
  258            if (!$file->isFile()) {
 
  262            list($uid, 
$rest) = explode(
'_', $file->getFilename(), 2);
 
  263            if ($uid === (
string) $this->user_id) {
 
  266                    'size' => $file->getSize(),
 
  267                    'ctime' => $file->getCTime(),
 
  292        if (!$fp = @fopen($abs_path, 
'w+')) {
 
  295        if (@fwrite($fp, $a_content) === 
false) {
 
  315            $this->
getMailPath() . 
'/' . $this->user_id . 
'_' . $file[
'name']
 
  327        @copy($a_abs_path, $this->
getMailPath() . 
"/" . $this->user_id . 
"_" . $a_new_name);
 
  343        if (file_exists($a_path)) {
 
  345            return \ilFileUtils::rename($a_path, $a_path . 
'.old');
 
  357        if (is_array($a_filenames)) {
 
  358            foreach ($a_filenames as $file) {
 
  374        if (file_exists($this->mail_path . 
'/' . basename($this->user_id . 
'_' . $a_filename))) {
 
  375            return unlink($this->mail_path . 
'/' . basename($this->user_id . 
'_' . $a_filename));
 
  394    public function saveFiles($a_mail_id, array $a_attachments)
 
  396        if (!is_numeric($a_mail_id) || $a_mail_id < 1) {
 
  397            throw new InvalidArgumentException(
'The passed mail_id must be a valid integer!');
 
  400        foreach ($a_attachments as $attachment) {
 
  401            $this->
saveFile($a_mail_id, $attachment);
 
  412        static $fsstorage_cache = array();
 
  414        if (!is_object($fsstorage_cache[$a_mail_id][$a_usr_id])) {
 
  415            include_once 
'Services/Mail/classes/class.ilFSStorageMail.php';
 
  416            $fsstorage_cache[$a_mail_id][$a_usr_id] = 
new ilFSStorageMail($a_mail_id, $a_usr_id);
 
  419        return $fsstorage_cache[$a_mail_id][$a_usr_id];
 
  429    public function saveFile($a_mail_id, $a_attachment)
 
  433        $storage_directory = $oStorage->getAbsolutePath();
 
  435        if (@!is_dir($storage_directory)) {
 
  440            $this->mail_path . 
'/' . $this->user_id . 
'_' . $a_attachment,
 
  441            $storage_directory . 
'/' . $a_attachment
 
  453            foreach ($a_files as $file) {
 
  454                if (!file_exists($this->mail_path . 
'/' . $this->user_id . 
'_' . $file)) {
 
  482                        INSERT INTO mail_attachment  
  483                        ( mail_id, path) VALUES (%s, %s)',
 
  484            array(
'integer', 
'text'),
 
  485            array($a_mail_id, $oStorage->getRelativePathExMailDirectory())
 
  498        $res = 
$ilDB->query(
"SELECT path FROM mail_attachment 
  499                                WHERE mail_id = " . 
$ilDB->quote($a_mail_id, 
'integer'));
 
  505            $res = 
$ilDB->query(
"SELECT COUNT(mail_id) count_mail_id FROM mail_attachment  
  506                                        WHERE path = " . 
$ilDB->quote(
$path, 
'text')) ;
 
  509                $cnt_mail_id = $row->count_mail_id;
 
  511            if ($cnt_mail_id == 1) {
 
  516        $res = $ilDB->manipulateF(
 
  517            "DELETE FROM mail_attachment  
  541        $umf = ini_get(
"upload_max_filesize");
 
  543        $pms = ini_get(
"post_max_size");
 
  546        $multiplier_a = array(
"K" => 1024, 
"M" => 1024 * 1024, 
"G" => 1024 * 1024 * 1024);
 
  548        $umf_parts = preg_split(
"/(\d+)([K|G|M])/", $umf, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
 
  549        $pms_parts = preg_split(
"/(\d+)([K|G|M])/", $pms, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
 
  551        if (count($umf_parts) == 2) {
 
  552            $umf = $umf_parts[0] * $multiplier_a[$umf_parts[1]];
 
  554        if (count($pms_parts) == 2) {
 
  555            $pms = $pms_parts[0] * $multiplier_a[$pms_parts[1]];
 
  559        $max_filesize = min($umf, $pms);
 
  561        if (!$max_filesize) {
 
  562            $max_filesize = max($umf, $pms);
 
  566        $this->mail_max_upload_file_size = $max_filesize;
 
  586        $q = 
"SELECT path " .
 
  587            "FROM mail_attachment ma " .
 
  588            "JOIN mail m ON ma.mail_id=m.mail_id " .
 
  589            "WHERE m.user_id = " . 
$DIC->database()->quote(
$user_id);
 
  590        $result_set = 
$DIC->database()->query($q);
 
  594            $attachment_path = $mail_data_dir . DIRECTORY_SEPARATOR . $row[
'path'];
 
  596            if ($attachment_size != -1) {
 
  597                $size += $attachment_size;
 
  601        return array(
'count' => $count, 
'size' => 
$size);
 
  607    public function onUserDelete()
 
  616            $iter = 
new RegexIterator(
 
  618                '/^' . $this->user_id . 
'_/' 
  620            foreach ($iter as $file) {
 
  625                if ($file->isFile()) {
 
  626                    @unlink($file->getPathname());
 
  629        } 
catch (Exception 
$e) {
 
  634                        SELECT DISTINCT(ma1.path) 
  635                        FROM mail_attachment ma1 
  637                                ON mail.mail_id = ma1.mail_id 
  638                        WHERE mail.user_id = %s 
  639                        AND (SELECT COUNT(tmp.path) FROM mail_attachment tmp WHERE tmp.path = ma1.path) = 1 
  644            array($this->user_id)
 
  649                $iter = 
new RecursiveIteratorIterator(
 
  650                    new RecursiveDirectoryIterator(
$path),
 
  651                    RecursiveIteratorIterator::CHILD_FIRST
 
  653                foreach ($iter as $file) {
 
  658                    if ($file->isDir()) {
 
  659                        @rmdir($file->getPathname());
 
  661                        @unlink($file->getPathname());
 
  665            } 
catch (Exception 
$e) {
 
  677                                        WHERE mail.user_id = %s AND mail.mail_id = mail_attachment.mail_id 
  681            array($this->user_id)
 
  700            if (0 === strlen(
$path)) {
 
  701                throw new \ilException(
'mail_download_zip_no_attachments');
 
  706        if (0 === strlen($downloadFilename)) {
 
  707            $downloadFilename = 
'attachments';
 
  711        $relativeProcessingDirectory = basename($processingDirectory);
 
  713        $absoluteZipDirectory = $processingDirectory . 
'/' . $downloadFilename;
 
  714        $relativeZipDirectory = $relativeProcessingDirectory . 
'/' . $downloadFilename;
 
  716        $this->tmpDirectory->createDir($relativeZipDirectory);
 
  718        foreach ($files as $fileName) {
 
  730            if (!$this->storageDirectory->has(
$source)) {
 
  734            $target = $relativeZipDirectory . 
'/' . $fileName;
 
  736            $stream = $this->storageDirectory->readStream(
$source);
 
  737            $this->tmpDirectory->writeStream($target, $stream);
 
  740        $pathToZipFile = $processingDirectory . 
'/' . $downloadFilename . 
'.zip';
 
  743        $this->tmpDirectory->deleteDir($relativeZipDirectory);
 
  745        $delivery = new \ilFileDelivery($processingDirectory . 
'/' . $downloadFilename . 
'.zip');
 
  748        $delivery->setConvertFileNameToAsci(
true);
 
  750        $delivery->setDeleteFile(
true);
 
  752        $delivery->deliver();
 
An exception for terminatinating execution or to throw for unit testing.
getAbsoluteAttachmentPoolPathByFilename(string $fileName)
Resolves a path for a passed filename in regards of a user's mail attachment pool,...
copyAttachmentFile($a_abs_path, $a_new_name)
Copy files in mail directory.
getAbsoluteAttachmentPoolPathPrefix()
checkReadWrite()
check if directory is writable overwritten method from base class @access private
saveFile($a_mail_id, $a_attachment)
save attachment file in a specific mail directory .../mail/<calculated_path>/mail_<mail_id>_<user_id>...
deliverAttachmentsAsZip(string $basename, int $mailId, $files=[], $isDraft=false)
getAttachmentPath($a_filename, $a_mail_id)
get the path of a specific attachment
unlinkFiles($a_filenames)
unlink files: expects an array of filenames e.g.
assignAttachmentsToDirectory($a_mail_id, $a_sent_mail_id)
assign attachments to mail directory
static getStorage($a_mail_id, $a_usr_id)
static _lookupDiskUsageOfUser($user_id)
Returns the number of bytes used on the harddisk for mail attachments, by the user with the specified...
initDirectory()
init directory overwritten method @access public
adoptAttachments($a_attachments, $a_mail_id)
adopt attachments (in case of forwarding a mail)
unlinkFile($a_filename)
unlink one uploaded file expects a filename e.g 'foo'
getAttachmentPathAndFilenameByMd5Hash(string $md5FileHash, int $mailId)
__construct($a_user_id=0)
Constructor call base constructors checks if directory is writable and sets the optional user_id.
storeAsAttachment($a_filename, $a_content)
Store content as attachment.
rotateFiles($a_path)
rotate files with same name recursive method
getMailPath()
get mail path @access public
$mail_max_upload_file_size
initAttachmentMaxUploadSize()
getAttachmentsTotalSizeLimit()
checkFilesExist($a_files)
check if files exist
getAttachmentPathByMailId(int $mailId)
deassignAttachmentFromDirectory($a_mail_id)
dassign attachments from mail directory
saveFiles($a_mail_id, array $a_attachments)
Saves all attachment files in a specific mail directory .../mail/<calculated_path>/mail_<mail_id>_<us...
__deleteAttachmentDirectory($a_rel_path)
This class handles all operations on files in directory /ilias_data/.
getPath()
get Path @access public
static getValidFilename($a_filename)
Get valid filename.
static getDataDir()
get data directory (outside webspace)
static moveUploadedFile($a_file, $a_name, $a_target, $a_raise_errors=true, $a_mode="move_uploaded")
move uploaded file
static delDir($a_dir, $a_clean_only=false)
removes a dir and all its content (subdirs and files) recursively
static _sanitizeFilemame($a_filename)
static zip($a_dir, $a_file, $compress_content=false)
zips given directory/file into given zip.file
static getDir($a_dir, $a_rec=false, $a_sub_dir="")
get directory
static ilTempnam($a_temp_path=null)
Returns a unique and non existing Path for e temporary file or directory.
static getASCIIFilename($a_filename)
convert utf8 to ascii filename
static dirsize($directory)
get size of a directory or a file.
Interface Filesystem The filesystem interface provides the public interface for the Filesystem servic...
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
redirection script todo: (a better solution should control the processing via a xml file)
foreach($_POST as $key=> $value) $res