15require_once(
"./Services/FileSystem/classes/class.ilFileData.php");
 
   16require_once(
"./Services/Utilities/classes/class.ilFileUtils.php");
 
   62        define(
'MAILPATH', 
'mail');
 
   64        $this->mail_path = parent::getPath() . 
"/" . MAILPATH;
 
   66        $this->user_id = $a_user_id;
 
   68        $this->db = 
$DIC->database();
 
   69        $this->tmpDirectory = 
$DIC->filesystem()->temp();
 
   70        $this->storageDirectory = 
$DIC->filesystem()->storage();
 
   83        if (is_writable($this->
getPath())) {
 
   84            if (mkdir($this->
getPath() . 
'/' . MAILPATH)) {
 
   85                if (chmod($this->
getPath() . 
'/' . MAILPATH, 0755)) {
 
   86                    $this->mail_path = $this->
getPath() . 
'/' . MAILPATH;
 
  107        $max_size = $this->
ilias->getSetting(
'mail_maxsize_attach', 
'');
 
  108        if (!strlen($max_size)) {
 
  112        return (
float) $this->
ilias->getSetting(
'mail_maxsize_attach', 0) * 1024;
 
  130        return $this->mail_path . 
'/' . $this->user_id . 
'_';
 
  141        $res = $this->db->queryF(
 
  142            "SELECT path FROM mail_attachment WHERE mail_id = %s",
 
  147        if (1 !== (
int) $this->db->numRows(
$res)) {
 
  148            throw new \OutOfBoundsException();
 
  151        $row = $this->db->fetchAssoc(
$res);
 
  153        $relativePath = $row[
'path'];
 
  157        foreach ($files as $file) {
 
  158            if ($file[
'type'] === 
'file' && md5($file[
'entry']) === $md5FileHash) {
 
  160                    'path' => $this->
getMailPath() . 
'/' . $relativePath . 
'/' . $file[
'entry'],
 
  161                    'filename' => $file[
'entry']
 
  166        throw new \OutOfBoundsException();
 
  175        $query = $this->db->query(
 
  176            "SELECT path FROM mail_attachment WHERE mail_id = " . $this->db->quote($mailId, 
'integer')
 
  179        while ($row = $this->db->fetchObject(
$query)) {
 
  196        if (file_exists(
$path) && is_readable(
$path)) {
 
  211        if (is_array($a_attachments)) {
 
  212            foreach ($a_attachments as $file) {
 
  214                if (!copy(
$path, $this->
getMailPath() . 
'/' . $this->user_id . 
'_' . $file)) {
 
  215                    return "ERROR: $this->getMailPath().'/'.$this->user_id.'_'.$file cannot be created";
 
  219            return "ARRAY REQUIRED";
 
  232        if (is_writable($this->mail_path) && is_readable($this->mail_path)) {
 
  235            $this->
ilias->raiseError(
"Mail directory is not readable/writable by webserver: " . $this->mail_path, $this->
ilias->error_obj->FATAL);
 
  245        return $this->getUnsentFiles();
 
  254    private function getUnsentFiles()
 
  258        $iter = 
new DirectoryIterator($this->mail_path);
 
  259        foreach ($iter as $file) {
 
  263            if ($file->isFile()) {
 
  264                list($uid, 
$rest) = explode(
'_', $file->getFilename(), 2);
 
  265                if ($uid == $this->user_id) {
 
  268                        'size' => $file->getSize(),
 
  269                        'ctime' => $file->getCTime()
 
  295        if (!$fp = @fopen($abs_path, 
'w+')) {
 
  318            $this->
getMailPath() . 
'/' . $this->user_id . 
'_' . $file[
'name']
 
  330        @copy($a_abs_path, $this->
getMailPath() . 
"/" . $this->user_id . 
"_" . $a_new_name);
 
  346        if (file_exists($a_path)) {
 
  348            return \ilFileUtils::rename($a_path, $a_path . 
'.old');
 
  360        if (is_array($a_filenames)) {
 
  361            foreach ($a_filenames as $file) {
 
  377        if (file_exists($this->mail_path . 
'/' . basename($this->user_id . 
'_' . $a_filename))) {
 
  378            return unlink($this->mail_path . 
'/' . basename($this->user_id . 
'_' . $a_filename));
 
  397    public function saveFiles($a_mail_id, array $a_attachments)
 
  399        if (!is_numeric($a_mail_id) || $a_mail_id < 1) {
 
  400            throw new InvalidArgumentException(
'The passed mail_id must be a valid integer!');
 
  403        foreach ($a_attachments as $attachment) {
 
  404            $this->
saveFile($a_mail_id, $attachment);
 
  415        static $fsstorage_cache = array();
 
  417        if (!is_object($fsstorage_cache[$a_mail_id][$a_usr_id])) {
 
  418            include_once 
'Services/Mail/classes/class.ilFSStorageMail.php';
 
  419            $fsstorage_cache[$a_mail_id][$a_usr_id] = 
new ilFSStorageMail($a_mail_id, $a_usr_id);
 
  422        return $fsstorage_cache[$a_mail_id][$a_usr_id];
 
  432    public function saveFile($a_mail_id, $a_attachment)
 
  436        $storage_directory = $oStorage->getAbsolutePath();
 
  438        if (@!is_dir($storage_directory)) {
 
  443            $this->mail_path . 
'/' . $this->user_id . 
'_' . $a_attachment,
 
  444            $storage_directory . 
'/' . $a_attachment
 
  456            foreach ($a_files as $file) {
 
  457                if (!file_exists($this->mail_path . 
'/' . $this->user_id . 
'_' . $file)) {
 
  485                        INSERT INTO mail_attachment  
  486                        ( mail_id, path) VALUES (%s, %s)',
 
  487            array(
'integer', 
'text'),
 
  488            array($a_mail_id, $oStorage->getRelativePathExMailDirectory())
 
  501        $res = 
$ilDB->query(
"SELECT path FROM mail_attachment 
  502                                WHERE mail_id = " . 
$ilDB->quote($a_mail_id, 
'integer'));
 
  508            $res = 
$ilDB->query(
"SELECT COUNT(mail_id) count_mail_id FROM mail_attachment  
  509                                        WHERE path = " . 
$ilDB->quote(
$path, 
'text')) ;
 
  512                $cnt_mail_id = $row->count_mail_id;
 
  514            if ($cnt_mail_id == 1) {
 
  519        $res = $ilDB->manipulateF(
 
  520            "DELETE FROM mail_attachment  
  544        $umf = ini_get(
"upload_max_filesize");
 
  546        $pms = ini_get(
"post_max_size");
 
  549        $multiplier_a = array(
"K" => 1024, 
"M" => 1024 * 1024, 
"G" => 1024 * 1024 * 1024);
 
  551        $umf_parts = preg_split(
"/(\d+)([K|G|M])/", $umf, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
 
  552        $pms_parts = preg_split(
"/(\d+)([K|G|M])/", $pms, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
 
  554        if (count($umf_parts) == 2) {
 
  555            $umf = $umf_parts[0] * $multiplier_a[$umf_parts[1]];
 
  557        if (count($pms_parts) == 2) {
 
  558            $pms = $pms_parts[0] * $multiplier_a[$pms_parts[1]];
 
  562        $max_filesize = min($umf, $pms);
 
  564        if (!$max_filesize) {
 
  565            $max_filesize = max($umf, $pms);
 
  569        $this->mail_max_upload_file_size = $max_filesize;
 
  589        $q = 
"SELECT path " .
 
  590            "FROM mail_attachment ma " .
 
  591            "JOIN mail m ON ma.mail_id=m.mail_id " .
 
  592            "WHERE m.user_id = " . 
$DIC->database()->quote(
$user_id);
 
  593        $result_set = 
$DIC->database()->query($q);
 
  597            $attachment_path = $mail_data_dir . DIRECTORY_SEPARATOR . $row[
'path'];
 
  599            if ($attachment_size != -1) {
 
  600                $size += $attachment_size;
 
  604        return array(
'count' => $count, 
'size' => 
$size);
 
  610    public function onUserDelete()
 
  619            $iter = 
new RegexIterator(
 
  621                '/^' . $this->user_id . 
'_/' 
  623            foreach ($iter as $file) {
 
  628                if ($file->isFile()) {
 
  629                    @unlink($file->getPathname());
 
  632        } 
catch (Exception 
$e) {
 
  637                        SELECT DISTINCT(ma1.path) 
  638                        FROM mail_attachment ma1 
  640                                ON mail.mail_id = ma1.mail_id 
  641                        WHERE mail.user_id = %s 
  642                        AND (SELECT COUNT(tmp.path) FROM mail_attachment tmp WHERE tmp.path = ma1.path) = 1 
  647            array($this->user_id)
 
  652                $iter = 
new RecursiveIteratorIterator(
 
  653                    new RecursiveDirectoryIterator(
$path),
 
  654                    RecursiveIteratorIterator::CHILD_FIRST
 
  656                foreach ($iter as $file) {
 
  661                    if ($file->isDir()) {
 
  662                        @rmdir($file->getPathname());
 
  664                        @unlink($file->getPathname());
 
  668            } 
catch (Exception 
$e) {
 
  680                                        WHERE mail.user_id = %s AND mail.mail_id = mail_attachment.mail_id 
  684            array($this->user_id)
 
  703            if (0 === strlen(
$path)) {
 
  704                throw new \ilException(
'mail_download_zip_no_attachments');
 
  709        if (0 === strlen($downloadFilename)) {
 
  710            $downloadFilename = 
'attachments';
 
  714        $relativeProcessingDirectory = basename($processingDirectory);
 
  716        $absoluteZipDirectory = $processingDirectory . 
'/' . $downloadFilename;
 
  717        $relativeZipDirectory = $relativeProcessingDirectory . 
'/' . $downloadFilename;
 
  719        $this->tmpDirectory->createDir($relativeZipDirectory);
 
  721        foreach ($files as $fileName) {
 
  733            if (!$this->storageDirectory->has(
$source)) {
 
  737            $target = $relativeZipDirectory . 
'/' . $fileName;
 
  739            $stream = $this->storageDirectory->readStream(
$source);
 
  740            $this->tmpDirectory->writeStream($target, $stream);
 
  743        $pathToZipFile = $processingDirectory . 
'/' . $downloadFilename . 
'.zip';
 
  746        $this->tmpDirectory->deleteDir($relativeZipDirectory);
 
  748        $delivery = new \ilFileDelivery($processingDirectory . 
'/' . $downloadFilename . 
'.zip');
 
  751        $delivery->setConvertFileNameToAsci(
true);
 
  753        $delivery->setDeleteFile(
true);
 
  755        $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()
getUserFilesData()
get all attachments of a specific user @access public
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.
__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