• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

classes/class.ilFileDataMail.php

Go to the documentation of this file.
00001 <?php
00002 /*
00003         +-----------------------------------------------------------------------------+
00004         | ILIAS open source                                                           |
00005         +-----------------------------------------------------------------------------+
00006         | Copyright (c) 1998-2001 ILIAS open source, University of Cologne            |
00007         |                                                                             |
00008         | This program is free software; you can redistribute it and/or               |
00009         | modify it under the terms of the GNU General Public License                 |
00010         | as published by the Free Software Foundation; either version 2              |
00011         | of the License, or (at your option) any later version.                      |
00012         |                                                                             |
00013         | This program is distributed in the hope that it will be useful,             |
00014         | but WITHOUT ANY WARRANTY; without even the implied warranty of              |
00015         | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               |
00016         | GNU General Public License for more details.                                |
00017         |                                                                             |
00018         | You should have received a copy of the GNU General Public License           |
00019         | along with this program; if not, write to the Free Software                 |
00020         | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. |
00021         +-----------------------------------------------------------------------------+
00022 */
00023 
00024 
00033 require_once("classes/class.ilFileData.php");
00034                                 
00035 class ilFileDataMail extends ilFileData
00036 {
00042         var $user_id;
00043 
00049         var $mail_path;
00050 
00051         var $mail_maxsize_attach;
00052 
00060         function ilFileDataMail($a_user_id = 0)
00061         {
00062                 define('MAILPATH','mail');
00063                 parent::ilFileData();
00064                 $this->mail_path = parent::getPath()."/".MAILPATH;
00065                 $this->checkReadWrite();
00066                 $this->user_id = $a_user_id;
00067 
00068 
00069                 $this->__initAttachmentMaxSize();
00070         }
00071 
00078         function initDirectory()
00079         {
00080                 if(is_writable($this->getPath()))
00081                 {
00082                         if(mkdir($this->getPath().'/'.MAILPATH))
00083                         {
00084                                 if(chmod($this->getPath().'/'.MAILPATH,0755))
00085                                 {
00086                                         $this->mail_path = $this->getPath().'/'.MAILPATH;
00087                                         return true;
00088                                 }
00089                         } 
00090                 }
00091                 return false;
00092         }
00093         function getUploadLimit()
00094         {
00095                 return $this->mail_maxsize_attach_message;
00096         }
00097 
00103         function getMailPath()
00104         {
00105                 return $this->mail_path;
00106         }
00107 
00115         function getAttachmentPath($a_filename,$a_mail_id)
00116         {
00117                 $query = "SELECT path FROM mail_attachment ".
00118                         "WHERE mail_id = '".$a_mail_id."'";
00119                 
00120                 $row = $this->ilias->db->getRow($query,DB_FETCHMODE_OBJECT);
00121                 $path = $this->getMailPath().'/'.$row->path.'/'.$a_filename;
00122 
00123                 if(file_exists($path))
00124                 {
00125                         if(is_readable($path))
00126                         {
00127                                 return $path;
00128                         }
00129                         return '';
00130                 }
00131                 return '';
00132         }
00140         function adoptAttachments($a_attachments,$a_mail_id)
00141         {
00142                 if(is_array($a_attachments))
00143                 {
00144                         foreach($a_attachments as $file)
00145                         {
00146                                 $path = $this->getAttachmentPath($file,$a_mail_id);
00147                                 if(!copy($path,$this->getMailPath().'/'.$this->user_id.'_'.$file))
00148                                 {
00149                                         return "ERROR: $this->getMailPath().'/'.$this->user_id.'_'.$file cannot be created";
00150                                 }
00151                         }
00152                 }
00153                 else
00154                 {
00155                         return "ARRAY REQUIRED";
00156                 }
00157                 return '';
00158         }
00159 
00166         function checkReadWrite()
00167         {
00168                 if(is_writable($this->mail_path) && is_readable($this->mail_path))
00169                 {
00170                         return true;
00171                 }
00172                 else
00173                 {
00174                         $this->ilias->raiseError("Mail directory is not readable/writable by webserver",$this->ilias->error_obj->FATAL);
00175                 }
00176         }
00182         function getUserFilesData()
00183         {
00184                 // FIRST GET FILES OF USER IN BASE DIRECTORY
00185                 return $files = $this->getUnsentFiles();
00186         }
00187 
00194         function getUnsentFiles()
00195         {
00196                 $files = array();
00197                 $dp = opendir($this->mail_path);
00198 
00199                 while($file = readdir($dp))
00200                 {
00201                         if(is_dir($file))
00202                         {
00203                                 continue;
00204                         }
00205                         list($uid,$rest) = split('_',$file,2);
00206                         if($uid == $this->user_id)
00207                         {
00208                                 if(!is_dir($this->mail_path.'/'.$file))
00209                                 {
00210                                         $files[] = array(
00211                                                 'name'     => $rest,
00212                                                 'size'     => filesize($this->mail_path.'/'.$file),
00213                                                 'ctime'    => ilFormat::formatDate(date('Y-m-d H:i:s',filectime($this->mail_path.'/'.$file))));
00214                                 }
00215                         }
00216                 }
00217                 closedir($dp);
00218                 return $files;
00219         }
00226         function storeUploadedFile($a_http_post_file)
00227         {
00228                 if($a_http_post_file["size"] >= $this->mail_maxsize_attach)
00229                 {
00230                         return 1;
00231                 }
00232 
00233                 if(isset($a_http_post_file) && $a_http_post_file['size'])
00234                 {
00235                         // CHECK IF FILE WITH SAME NAME EXISTS
00236                         $this->rotateFiles($this->getMailPath().'/'.$this->user_id.'_'.$a_http_post_file['name']);
00237                         
00238                         ilUtil::moveUploadedFile($a_http_post_file['tmp_name'],
00239                                 $a_http_post_file['name'], $this->getMailPath().'/'.$this->user_id.'_'.
00240                                 $a_http_post_file['name']);
00241 
00242                         //move_uploaded_file($a_http_post_file['tmp_name'],$this->getMailPath().'/'.$this->user_id.'_'.
00243                         //                                 $a_http_post_file['name']);
00244                         return 0;
00245                 }
00246                 return 1;
00247         }
00248 
00255         function copyAttachmentFile($a_abs_path,$a_new_name)
00256         {
00257                 @copy($a_abs_path,$this->getMailPath()."/".$this->user_id."_".$a_new_name);
00258                 
00259                 return true;
00260         }
00261                 
00262 
00263 
00271         function rotateFiles($a_path)
00272         {
00273                 if(file_exists($a_path))
00274                 {
00275                         $this->rotateFiles($a_path.".old");
00276                         return rename($a_path,$a_path.'.old');
00277                 }
00278                 return true;
00279         }
00286         function unlinkFiles($a_filenames)
00287         {
00288                 if(is_array($a_filenames))
00289                 {
00290                         foreach($a_filenames as $file)
00291                         {
00292                                 if(!$this->unlinkFile($file))
00293                                 {
00294                                         return $file;
00295                                 }
00296                         }
00297                 }
00298                 return '';
00299         }
00306         function unlinkFile($a_filename)
00307         {
00308                 if(file_exists($this->mail_path.'/'.$this->user_id.'_'.$a_filename))
00309                 {
00310                         return unlink($this->mail_path.'/'.$this->user_id.'_'.$a_filename);
00311                 }
00312         }
00319         function getAbsolutePath($a_path)
00320         {
00321                 return $this->mail_path.'/'.$this->user_id.'_'.$a_path;
00322         }
00323 
00331         function saveFiles($a_mail_id,$a_attachments)
00332         {
00333                 if(!$a_mail_id)
00334                 {
00335                         return "INTERNAL HERE ERROR: No valid mail_id given";
00336                 }
00337                 if(is_array($a_attachments))
00338                 {
00339                         foreach($a_attachments as $attachment)
00340                         {
00341                                 if(!$this->saveFile($a_mail_id,$attachment))
00342                                 {
00343                                         return $attachment;
00344                                 }
00345                         }
00346                 }
00347                 else
00348                 {
00349                         return "ARRAY REQUIRED";
00350                 }
00351                 return '';
00352         }
00360         function saveFile($a_mail_id,$a_attachment)
00361         {
00362                 if(!is_dir($this->mail_path.'/'.$this->user_id.'_'.$a_mail_id))
00363                 {
00364                         if(mkdir($this->mail_path.'/'.$this->user_id.'_'.$a_mail_id))
00365                         {
00366                                 chmod($this->mail_path.'/'.$this->user_id.'_'.$a_mail_id,0755);
00367                         }
00368                         else
00369                         {
00370                                 return false;
00371                         }
00372                 }
00373                 return copy($this->mail_path.'/'.$this->user_id.'_'.$a_attachment,
00374                                         $this->mail_path.'/'.$this->user_id.'_'.$a_mail_id.'/'.$a_attachment);
00375         }
00382         function checkFilesExist($a_files)
00383         {
00384                 if($a_files)
00385                 {
00386                         foreach($a_files as $file)
00387                         {
00388                                 if(!file_exists($this->mail_path.'/'.$this->user_id.'_'.$file))
00389                                 {
00390                                         return false;
00391                                 }
00392                         }
00393                         return true;
00394                 }
00395                 return true;
00396         }
00404         function assignAttachmentsToDirectory($a_mail_id,$a_sent_mail_id)
00405         {
00406                 $query = "INSERT INTO mail_attachment ".
00407                         "SET mail_id = '".$a_mail_id."', ".
00408                         "path = '".$this->user_id."_".$a_sent_mail_id."'";
00409                 $res = $this->ilias->db->query($query);
00410         }
00417         function deassignAttachmentFromDirectory($a_mail_id)
00418         {
00419                 // IF IT'S THE LAST MAIL CONTAINING THESE ATTACHMENTS => DELETE ATTACHMENTS
00420                 $query = "SELECT path FROM mail_attachment ".
00421                         "WHERE mail_id = '".$a_mail_id."'";
00422 
00423                 $res = $this->ilias->db->query($query);
00424                 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00425                 {
00426                         $path = $row->path;
00427                 }
00428                 if($path)
00429                 {
00430                         $query = "SELECT COUNT(mail_id) AS count_mail_id FROM mail_attachment ".
00431                                 "WHERE path = '".$path."'";
00432 
00433                         $res = $this->ilias->db->query($query);
00434                         while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00435                         {
00436                                 $cnt_mail_id = $row->count_mail_id;
00437                         }
00438                         if($cnt_mail_id == 1)
00439                         {
00440                                 $this->__deleteAttachmentDirectory($path);
00441                         }
00442                 }
00443 
00444                 $query = "DELETE FROM mail_attachment ".
00445                         "WHERE mail_id = '".$a_mail_id."'";
00446                 $res = $this->ilias->db->query($query);
00447                 return true;
00448         }
00449 
00450         function __deleteAttachmentDirectory($a_rel_path)
00451         {
00452                 $dp = opendir($this->mail_path."/".$a_rel_path);
00453 
00454                 while($file = readdir($dp))
00455                 {
00456                         if($file == '.' or $file == '..')
00457                         {
00458                                 continue;
00459                         }
00460                         if(is_dir($file))
00461                         {
00462                                 $this->__deleteAttachmentDirectory($file);
00463                         }
00464                         unlink($this->mail_path."/".$a_rel_path."/".$file);
00465                 }
00466                 rmdir($this->mail_path."/".$a_rel_path);
00467                 closedir($dp);
00468 
00469                 return true;
00470         }
00471         function __initAttachmentMaxSize()
00472         {
00473 
00474                 $this->mail_maxsize_attach = $this->ilias->getSetting("mail_maxsize_attach") ? 
00475                         $this->ilias->getSetting("mail_maxsize_attach") * 1024 : 1024 * 1024 * 64;
00476 
00477                 $this->mail_maxsize_attach_message = $this->ilias->getSetting("mail_maxsize_attach") ? 
00478                         $this->ilias->getSetting("mail_maxsize_attach") :
00479                         ini_get("upload_max_filesize");
00480         }
00481 }
00482 ?>

Generated on Fri Dec 13 2013 09:06:33 for ILIAS Release_3_4_x_branch .rev 46804 by  doxygen 1.7.1