• Main Page
  • Related Pages
  • Modules
  • 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 
00032 require_once("classes/class.ilFileData.php");
00033                                 
00034 class ilFileDataMail extends ilFileData
00035 {
00041         var $user_id;
00042 
00048         var $mail_path;
00049 
00050         var $mail_maxsize_attach;
00051 
00059         function ilFileDataMail($a_user_id = 0)
00060         {
00061                 define('MAILPATH','mail');
00062                 parent::ilFileData();
00063                 $this->mail_path = parent::getPath()."/".MAILPATH;
00064                 $this->checkReadWrite();
00065                 $this->user_id = $a_user_id;
00066 
00067 
00068                 $this->__initAttachmentMaxSize();
00069         }
00070 
00077         function initDirectory()
00078         {
00079                 if(is_writable($this->getPath()))
00080                 {
00081                         if(mkdir($this->getPath().'/'.MAILPATH))
00082                         {
00083                                 if(chmod($this->getPath().'/'.MAILPATH,0755))
00084                                 {
00085                                         $this->mail_path = $this->getPath().'/'.MAILPATH;
00086                                         return true;
00087                                 }
00088                         } 
00089                 }
00090                 return false;
00091         }
00092         function getUploadLimit()
00093         {
00094                 return $this->mail_maxsize_attach_message;
00095         }
00096 
00102         function getMailPath()
00103         {
00104                 return $this->mail_path;
00105         }
00106 
00114         function getAttachmentPath($a_filename,$a_mail_id)
00115         {
00116                 global $ilDB;
00117                 
00118                 $query = "SELECT path FROM mail_attachment ".
00119                         "WHERE mail_id = ".$ilDB->quote($a_mail_id)."";
00120                 
00121                 $row = $this->ilias->db->getRow($query,DB_FETCHMODE_OBJECT);
00122                 $path = $this->getMailPath().'/'.$row->path.'/'.$a_filename;
00123 
00124                 if(file_exists($path))
00125                 {
00126                         if(is_readable($path))
00127                         {
00128                                 return $path;
00129                         }
00130                         return '';
00131                 }
00132                 return '';
00133         }
00141         function adoptAttachments($a_attachments,$a_mail_id)
00142         {
00143                 if(is_array($a_attachments))
00144                 {
00145                         foreach($a_attachments as $file)
00146                         {
00147                                 $path = $this->getAttachmentPath($file,$a_mail_id);
00148                                 if(!copy($path,$this->getMailPath().'/'.$this->user_id.'_'.$file))
00149                                 {
00150                                         return "ERROR: $this->getMailPath().'/'.$this->user_id.'_'.$file cannot be created";
00151                                 }
00152                         }
00153                 }
00154                 else
00155                 {
00156                         return "ARRAY REQUIRED";
00157                 }
00158                 return '';
00159         }
00160 
00167         function checkReadWrite()
00168         {
00169                 if(is_writable($this->mail_path) && is_readable($this->mail_path))
00170                 {
00171                         return true;
00172                 }
00173                 else
00174                 {
00175                         $this->ilias->raiseError("Mail directory is not readable/writable by webserver",$this->ilias->error_obj->FATAL);
00176                 }
00177         }
00183         function getUserFilesData()
00184         {
00185                 // FIRST GET FILES OF USER IN BASE DIRECTORY
00186                 return $files = $this->getUnsentFiles();
00187         }
00188 
00195         function getUnsentFiles()
00196         {
00197                 $files = array();
00198                 $dp = opendir($this->mail_path);
00199 
00200                 while($file = readdir($dp))
00201                 {
00202                         if(is_dir($file))
00203                         {
00204                                 continue;
00205                         }
00206                         list($uid,$rest) = split('_',$file,2);
00207                         if($uid == $this->user_id)
00208                         {
00209                                 if(!is_dir($this->mail_path.'/'.$file))
00210                                 {
00211                                         $files[] = array(
00212                                                 'name'     => $rest,
00213                                                 'size'     => filesize($this->mail_path.'/'.$file),
00214                                                 'ctime'    => ilFormat::formatDate(date('Y-m-d H:i:s',filectime($this->mail_path.'/'.$file))));
00215                                 }
00216                         }
00217                 }
00218                 closedir($dp);
00219                 return $files;
00220         }
00227         function storeUploadedFile($a_http_post_file)
00228         {
00229                 if($a_http_post_file["size"] >= $this->mail_maxsize_attach)
00230                 {
00231                         return 1;
00232                 }
00233 
00234                 if(isset($a_http_post_file) && $a_http_post_file['size'])
00235                 {
00236                         $a_http_post_file['name'] = ilUtil::_sanitizeFilemame($a_http_post_file['name']);
00237                 
00238                         // CHECK IF FILE WITH SAME NAME EXISTS
00239                         $this->rotateFiles($this->getMailPath().'/'.$this->user_id.'_'.$a_http_post_file['name']);
00240                         
00241                         ilUtil::moveUploadedFile($a_http_post_file['tmp_name'],
00242                                 $a_http_post_file['name'], $this->getMailPath().'/'.$this->user_id.'_'.
00243                                 $a_http_post_file['name']);
00244 
00245                         //move_uploaded_file($a_http_post_file['tmp_name'],$this->getMailPath().'/'.$this->user_id.'_'.
00246                         //                                 $a_http_post_file['name']);
00247                         return 0;
00248                 }
00249                 return 1;
00250         }
00251 
00258         function copyAttachmentFile($a_abs_path,$a_new_name)
00259         {
00260                 @copy($a_abs_path,$this->getMailPath()."/".$this->user_id."_".$a_new_name);
00261                 
00262                 return true;
00263         }
00264                 
00265 
00266 
00274         function rotateFiles($a_path)
00275         {
00276                 if(file_exists($a_path))
00277                 {
00278                         $this->rotateFiles($a_path.".old");
00279                         return rename($a_path,$a_path.'.old');
00280                 }
00281                 return true;
00282         }
00289         function unlinkFiles($a_filenames)
00290         {
00291                 if(is_array($a_filenames))
00292                 {
00293                         foreach($a_filenames as $file)
00294                         {
00295                                 if(!$this->unlinkFile($file))
00296                                 {
00297                                         return $file;
00298                                 }
00299                         }
00300                 }
00301                 return '';
00302         }
00309         function unlinkFile($a_filename)
00310         {
00311                 if(file_exists($this->mail_path.'/'.basename($this->user_id.'_'.$a_filename)))
00312                 {
00313                         return unlink($this->mail_path.'/'.basename($this->user_id.'_'.$a_filename));
00314                 }
00315         }
00322         function getAbsolutePath($a_path)
00323         {
00324                 return $this->mail_path.'/'.$this->user_id.'_'.$a_path;
00325         }
00326 
00334         function saveFiles($a_mail_id,$a_attachments)
00335         {
00336                 if(!$a_mail_id)
00337                 {
00338                         return "INTERNAL HERE ERROR: No valid mail_id given";
00339                 }
00340                 if(is_array($a_attachments))
00341                 {
00342                         foreach($a_attachments as $attachment)
00343                         {
00344                                 if(!$this->saveFile($a_mail_id,$attachment))
00345                                 {
00346                                         return $attachment;
00347                                 }
00348                         }
00349                 }
00350                 else
00351                 {
00352                         return "ARRAY REQUIRED";
00353                 }
00354                 return '';
00355         }
00363         function saveFile($a_mail_id,$a_attachment)
00364         {
00365                 if(!is_dir($this->mail_path.'/'.$this->user_id.'_'.$a_mail_id))
00366                 {
00367                         if(mkdir($this->mail_path.'/'.$this->user_id.'_'.$a_mail_id))
00368                         {
00369                                 chmod($this->mail_path.'/'.$this->user_id.'_'.$a_mail_id,0755);
00370                         }
00371                         else
00372                         {
00373                                 return false;
00374                         }
00375                 }
00376                 return copy($this->mail_path.'/'.$this->user_id.'_'.$a_attachment,
00377                                         $this->mail_path.'/'.$this->user_id.'_'.$a_mail_id.'/'.$a_attachment);
00378         }
00385         function checkFilesExist($a_files)
00386         {
00387                 if($a_files)
00388                 {
00389                         foreach($a_files as $file)
00390                         {
00391                                 if(!file_exists($this->mail_path.'/'.$this->user_id.'_'.$file))
00392                                 {
00393                                         return false;
00394                                 }
00395                         }
00396                         return true;
00397                 }
00398                 return true;
00399         }
00407         function assignAttachmentsToDirectory($a_mail_id,$a_sent_mail_id)
00408         {
00409                 global $ilDB;
00410                 
00411                 $query = "INSERT INTO mail_attachment ".
00412                         "SET mail_id = ".$ilDB->quote($a_mail_id).", ".
00413                         "path = ".$ilDB->quote($this->user_id."_".$a_sent_mail_id)." ";
00414                 $res = $this->ilias->db->query($query);
00415         }
00422         function deassignAttachmentFromDirectory($a_mail_id)
00423         {
00424                 global $ilDB;
00425                 // IF IT'S THE LAST MAIL CONTAINING THESE ATTACHMENTS => DELETE ATTACHMENTS
00426                 $query = "SELECT path FROM mail_attachment ".
00427                         "WHERE mail_id = ".$ilDB->quote($a_mail_id)." ";
00428 
00429                 $res = $this->ilias->db->query($query);
00430                 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00431                 {
00432                         $path = $row->path;
00433                 }
00434                 if($path)
00435                 {
00436                         $query = "SELECT COUNT(mail_id) AS count_mail_id FROM mail_attachment ".
00437                                 "WHERE path = ".$ilDB->quote($path)." ";
00438 
00439                         $res = $this->ilias->db->query($query);
00440                         while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00441                         {
00442                                 $cnt_mail_id = $row->count_mail_id;
00443                         }
00444                         if($cnt_mail_id == 1)
00445                         {
00446                                 $this->__deleteAttachmentDirectory($path);
00447                         }
00448                 }
00449 
00450                 $query = "DELETE FROM mail_attachment ".
00451                         "WHERE mail_id = ".$ilDB->quote($a_mail_id)." ";
00452                 $res = $this->ilias->db->query($query);
00453                 return true;
00454         }
00455 
00456         function __deleteAttachmentDirectory($a_rel_path)
00457         {
00458                 if(!@$dp = opendir($this->mail_path."/".$a_rel_path))
00459                 {
00460                         return false;
00461                 }
00462 
00463                 while($file = @readdir($dp))
00464                 {
00465                         if($file == '.' or $file == '..')
00466                         {
00467                                 continue;
00468                         }
00469                         if(is_dir($file))
00470                         {
00471                                 $this->__deleteAttachmentDirectory($file);
00472                         }
00473                         unlink($this->mail_path."/".$a_rel_path."/".$file);
00474                 }
00475                 @rmdir($this->mail_path."/".$a_rel_path);
00476                 closedir($dp);
00477 
00478                 return true;
00479         }
00480         function __initAttachmentMaxSize()
00481         {
00482 
00483                 $this->mail_maxsize_attach = $this->ilias->getSetting("mail_maxsize_attach") ? 
00484                         $this->ilias->getSetting("mail_maxsize_attach") * 1024 : 1024 * 1024 * 64;
00485 
00486                 $this->mail_maxsize_attach_message = $this->ilias->getSetting("mail_maxsize_attach") ? 
00487                         $this->ilias->getSetting("mail_maxsize_attach") :
00488                         ini_get("upload_max_filesize");
00489         }
00490 }
00491 ?>

Generated on Fri Dec 13 2013 17:56:47 for ILIAS Release_3_9_x_branch .rev 46835 by  doxygen 1.7.1