12require_once(
"./Services/FileSystem/classes/class.ilFileData.php");
 
   13require_once(
"./Services/Utilities/classes/class.ilFileUtils.php");
 
   48        define(
'MAILPATH', 
'mail');
 
   49        parent::__construct();
 
   50        $this->mail_path = parent::getPath() . 
"/" . MAILPATH;
 
   52        $this->user_id = $a_user_id;
 
   65        if (is_writable($this->
getPath())) {
 
   66            if (mkdir($this->
getPath() . 
'/' . MAILPATH)) {
 
   67                if (chmod($this->
getPath() . 
'/' . MAILPATH, 0755)) {
 
   68                    $this->mail_path = $this->
getPath() . 
'/' . MAILPATH;
 
   89        $max_size = $this->
ilias->getSetting(
'mail_maxsize_attach', 
'');
 
   90        if (!strlen($max_size)) {
 
   94        return (
float) $this->
ilias->getSetting(
'mail_maxsize_attach', 0) * 1024;
 
  112        return $this->mail_path . 
'/' . $this->user_id . 
'_';
 
  132        $query = 
$ilDB->query(
"SELECT path FROM mail_attachment  
  133                                  WHERE mail_id = " . 
$ilDB->quote($a_mail_id, 
'integer') . 
"");
 
  137            $rel_path = 
$row->path;
 
  143            if (
$file[
'type'] == 
'file' && md5(
$file[
'entry']) == $a_filename) {
 
  146                    'filename' => 
$file[
'entry']
 
  171        $query = 
$ilDB->query(
"SELECT path FROM mail_attachment " .
 
  172            "WHERE mail_id = " . 
$ilDB->quote($a_mail_id, 
'integer') . 
"");
 
  178        if (file_exists(
$path)) {
 
  179            if (is_readable(
$path)) {
 
  195        if (is_array($a_attachments)) {
 
  196            foreach ($a_attachments as 
$file) {
 
  199                    return "ERROR: $this->getMailPath().'/'.$this->user_id.'_'.$file cannot be created";
 
  203            return "ARRAY REQUIRED";
 
  216        if (is_writable($this->mail_path) && is_readable($this->mail_path)) {
 
  219            $this->
ilias->raiseError(
"Mail directory is not readable/writable by webserver: " . $this->mail_path, $this->
ilias->error_obj->FATAL);
 
  229        return $this->getUnsentFiles();
 
  238    private function getUnsentFiles()
 
  242        $iter = 
new DirectoryIterator($this->mail_path);
 
  243        foreach ($iter as 
$file) {
 
  247            if (
$file->isFile()) {
 
  248                list($uid, 
$rest) = explode(
'_', 
$file->getFilename(), 2);
 
  249                if ($uid == $this->user_id) {
 
  252                        'size'     => 
$file->getSize(),
 
  253                        'ctime'    => 
$file->getCTime()
 
  279        if (!$fp = @fopen($abs_path, 
'w+')) {
 
  299        ilUtil::moveUploadedFile(
 
  314        @copy($a_abs_path, $this->
getMailPath() . 
"/" . $this->user_id . 
"_" . $a_new_name);
 
  330        if (file_exists($a_path)) {
 
  332            return \ilFileUtils::rename($a_path, $a_path . 
'.old');
 
  344        if (is_array($a_filenames)) {
 
  345            foreach ($a_filenames as 
$file) {
 
  361        if (file_exists($this->mail_path . 
'/' . basename($this->user_id . 
'_' . $a_filename))) {
 
  362            return unlink($this->mail_path . 
'/' . basename($this->user_id . 
'_' . $a_filename));
 
  381    public function saveFiles($a_mail_id, array $a_attachments)
 
  383        if (!is_numeric($a_mail_id) || $a_mail_id < 1) {
 
  384            throw new InvalidArgumentException(
'The passed mail_id must be a valid integer!');
 
  387        foreach ($a_attachments as $attachment) {
 
  388            $this->
saveFile($a_mail_id, $attachment);
 
  394        static $fsstorage_cache = array();
 
  396        if (!is_object($fsstorage_cache[$a_mail_id][$a_usr_id])) {
 
  397            include_once 
'Services/Mail/classes/class.ilFSStorageMail.php';
 
  398            $fsstorage_cache[$a_mail_id][$a_usr_id] = 
new ilFSStorageMail($a_mail_id, $a_usr_id);
 
  401        return $fsstorage_cache[$a_mail_id][$a_usr_id];
 
  411    public function saveFile($a_mail_id, $a_attachment)
 
  415        $storage_directory = $oStorage->getAbsolutePath();
 
  417        if (@!is_dir($storage_directory)) {
 
  422            $this->mail_path . 
'/' . $this->user_id . 
'_' . $a_attachment,
 
  423            $storage_directory . 
'/' . $a_attachment
 
  435            foreach ($a_files as 
$file) {
 
  436                if (!file_exists($this->mail_path . 
'/' . $this->user_id . 
'_' . 
$file)) {
 
  464                        INSERT INTO mail_attachment  
  465                        ( mail_id, path) VALUES (%s, %s)',
 
  466            array(
'integer', 
'text'),
 
  467            array($a_mail_id, $oStorage->getRelativePathExMailDirectory())
 
  480        $res = 
$ilDB->query(
"SELECT path FROM mail_attachment 
  481                                WHERE mail_id = " . 
$ilDB->quote($a_mail_id, 
'integer'));
 
  487            $res = 
$ilDB->query(
"SELECT COUNT(mail_id) count_mail_id FROM mail_attachment  
  488                                        WHERE path = " . 
$ilDB->quote(
$path, 
'text')) ;
 
  491                $cnt_mail_id = 
$row->count_mail_id;
 
  493            if ($cnt_mail_id == 1) {
 
  498        $res = $ilDB->manipulateF(
 
  499            "DELETE FROM mail_attachment  
  523        $umf = ini_get(
"upload_max_filesize");
 
  525        $pms = ini_get(
"post_max_size");
 
  528        $multiplier_a = array(
"K" => 1024, 
"M" => 1024 * 1024, 
"G" => 1024 * 1024 * 1024);
 
  533        if (count($umf_parts) == 2) {
 
  534            $umf = $umf_parts[0] * $multiplier_a[$umf_parts[1]];
 
  536        if (count($pms_parts) == 2) {
 
  537            $pms = $pms_parts[0] * $multiplier_a[$pms_parts[1]];
 
  541        $max_filesize = min($umf, $pms);
 
  543        if (!$max_filesize) {
 
  544            $max_filesize = max($umf, $pms);
 
  548        $this->mail_max_upload_file_size = $max_filesize;
 
  568        $q = 
"SELECT path " .
 
  569            "FROM mail_attachment ma " .
 
  570            "JOIN mail m ON ma.mail_id=m.mail_id " .
 
  571            "WHERE m.user_id = " . 
$DIC->database()->quote(
$user_id);
 
  572        $result_set = 
$DIC->database()->query($q);
 
  576            $attachment_path = $mail_data_dir . DIRECTORY_SEPARATOR . 
$row[
'path'];
 
  578            if ($attachment_size != -1) {
 
  579                $size += $attachment_size;
 
  583        return array(
'count'=>$count, 
'size'=>
$size);
 
  589    public function onUserDelete()
 
  598            $iter = 
new RegexIterator(
 
  600                '/^' . $this->user_id . 
'_/' 
  602            foreach ($iter as 
$file) {
 
  607                if (
$file->isFile()) {
 
  608                    @unlink(
$file->getPathname());
 
  611        } 
catch (Exception $e) {
 
  616                        SELECT DISTINCT(ma1.path) 
  617                        FROM mail_attachment ma1 
  619                                ON mail.mail_id = ma1.mail_id 
  620                        WHERE mail.user_id = %s 
  621                        AND (SELECT COUNT(tmp.path) FROM mail_attachment tmp WHERE tmp.path = ma1.path) = 1 
  626            array($this->user_id)
 
  631                $iter = 
new RecursiveIteratorIterator(
 
  632                    new RecursiveDirectoryIterator(
$path),
 
  633                    RecursiveIteratorIterator::CHILD_FIRST
 
  635                foreach ($iter as 
$file) {
 
  640                    if (
$file->isDir()) {
 
  641                        @rmdir(
$file->getPathname());
 
  643                        @unlink(
$file->getPathname());
 
  647            } 
catch (Exception $e) {
 
  659                                        WHERE mail.user_id = %s AND mail.mail_id = mail_attachment.mail_id 
  663            array($this->user_id)
 
An exception for terminatinating execution or to throw for unit testing.
This class handles all operations on files (attachments) in directory ilias_data/mail.
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>...
getAbsolutePath($fileName)
get absolute path of filename
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'
__construct($a_user_id=0)
Constructor call base constructors checks if directory is writable and sets the optional user_id.
getAttachmentPathByMD5Filename($a_filename, $a_mail_id)
get the path of a specific attachment
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
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 getDataDir()
get data directory (outside webspace)
static delDir($a_dir, $a_clean_only=false)
removes a dir and all its content (subdirs and files) recursively
static _sanitizeFilemame($a_filename)
static getDir($a_dir, $a_rec=false, $a_sub_dir="")
get directory
static dirsize($directory)
get size of a directory or a file.
redirection script todo: (a better solution should control the processing via a xml file)
if(!file_exists("$old.txt")) if( $old===$new) if(file_exists("$new.txt")) $file
foreach($_POST as $key=> $value) $res
echo;exit;}function LogoutNotification($SessionID){ global $ilDB;$q="SELECT session_id, data FROM usr_session WHERE expires > (\w+)\|/" PREG_SPLIT_NO_EMPTY PREG_SPLIT_DELIM_CAPTURE