ILIAS  Release_3_10_x_branch Revision 61812
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilFileDataMail.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2001 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +-----------------------------------------------------------------------------+
22 */
23 
24 
32 require_once("classes/class.ilFileData.php");
33 
35 {
41  var $user_id;
42 
49 
51 
59  function ilFileDataMail($a_user_id = 0)
60  {
61  define('MAILPATH','mail');
63  $this->mail_path = parent::getPath()."/".MAILPATH;
64  $this->checkReadWrite();
65  $this->user_id = $a_user_id;
66 
67 
68  $this->__initAttachmentMaxSize();
69  }
70 
77  function initDirectory()
78  {
79  if(is_writable($this->getPath()))
80  {
81  if(mkdir($this->getPath().'/'.MAILPATH))
82  {
83  if(chmod($this->getPath().'/'.MAILPATH,0755))
84  {
85  $this->mail_path = $this->getPath().'/'.MAILPATH;
86  return true;
87  }
88  }
89  }
90  return false;
91  }
92  function getUploadLimit()
93  {
94  return $this->mail_maxsize_attach_message;
95  }
96 
102  function getMailPath()
103  {
104  return $this->mail_path;
105  }
106 
114  public function getAttachmentPathByMD5Filename($a_filename,$a_mail_id)
115  {
116  global $ilDB;
117 
118  $query = "SELECT path FROM mail_attachment ".
119  "WHERE mail_id = ".$ilDB->quote($a_mail_id)."";
120 
121  $row = $this->ilias->db->getRow($query,DB_FETCHMODE_OBJECT);
122  $path = $this->getMailPath().'/'.$row->path;
123 
125  foreach((array)$files as $file)
126  {
127  if($file['type'] == 'file' && md5($file['entry']) == $a_filename)
128  {
129  return array(
130  'path' => $this->getMailPath().'/'.$row->path.'/'.$file['entry'],
131  'filename' => $file['entry']
132  );
133  }
134  }
135  return '';
136  }
137 
138 
146  function getAttachmentPath($a_filename,$a_mail_id)
147  {
148  global $ilDB;
149 
150  $query = "SELECT path FROM mail_attachment ".
151  "WHERE mail_id = ".$ilDB->quote($a_mail_id)."";
152 
153  $row = $this->ilias->db->getRow($query,DB_FETCHMODE_OBJECT);
154  $path = $this->getMailPath().'/'.$row->path.'/'.$a_filename;
155 
156  if(file_exists($path))
157  {
158  if(is_readable($path))
159  {
160  return $path;
161  }
162  return '';
163  }
164  return '';
165  }
173  function adoptAttachments($a_attachments,$a_mail_id)
174  {
175  if(is_array($a_attachments))
176  {
177  foreach($a_attachments as $file)
178  {
179  $path = $this->getAttachmentPath($file,$a_mail_id);
180  if(!copy($path,$this->getMailPath().'/'.$this->user_id.'_'.$file))
181  {
182  return "ERROR: $this->getMailPath().'/'.$this->user_id.'_'.$file cannot be created";
183  }
184  }
185  }
186  else
187  {
188  return "ARRAY REQUIRED";
189  }
190  return '';
191  }
192 
199  function checkReadWrite()
200  {
201  if(is_writable($this->mail_path) && is_readable($this->mail_path))
202  {
203  return true;
204  }
205  else
206  {
207  $this->ilias->raiseError("Mail directory is not readable/writable by webserver",$this->ilias->error_obj->FATAL);
208  }
209  }
215  function getUserFilesData()
216  {
217  // FIRST GET FILES OF USER IN BASE DIRECTORY
218  return $files = $this->getUnsentFiles();
219  }
220 
227  function getUnsentFiles()
228  {
229  $files = array();
230  $dp = opendir($this->mail_path);
231 
232  while($file = readdir($dp))
233  {
234  if(is_dir($file))
235  {
236  continue;
237  }
238  list($uid,$rest) = split('_',$file,2);
239  if($uid == $this->user_id)
240  {
241  if(!is_dir($this->mail_path.'/'.$file))
242  {
243  $files[] = array(
244  'name' => $rest,
245  'size' => filesize($this->mail_path.'/'.$file),
246  'ctime' => ilFormat::formatDate(date('Y-m-d H:i:s',filectime($this->mail_path.'/'.$file))));
247  }
248  }
249  }
250  closedir($dp);
251  return $files;
252  }
259  function storeUploadedFile($a_http_post_file)
260  {
261  if($a_http_post_file["size"] >= $this->mail_maxsize_attach)
262  {
263  return 1;
264  }
265 
266  if(isset($a_http_post_file) && $a_http_post_file['size'])
267  {
268  $a_http_post_file['name'] = ilUtil::_sanitizeFilemame($a_http_post_file['name']);
269 
270  // CHECK IF FILE WITH SAME NAME EXISTS
271  $this->rotateFiles($this->getMailPath().'/'.$this->user_id.'_'.$a_http_post_file['name']);
272 
273  ilUtil::moveUploadedFile($a_http_post_file['tmp_name'],
274  $a_http_post_file['name'], $this->getMailPath().'/'.$this->user_id.'_'.
275  $a_http_post_file['name']);
276 
277  //move_uploaded_file($a_http_post_file['tmp_name'],$this->getMailPath().'/'.$this->user_id.'_'.
278  // $a_http_post_file['name']);
279  return 0;
280  }
281  return 1;
282  }
283 
290  function copyAttachmentFile($a_abs_path,$a_new_name)
291  {
292  @copy($a_abs_path,$this->getMailPath()."/".$this->user_id."_".$a_new_name);
293 
294  return true;
295  }
296 
297 
298 
306  function rotateFiles($a_path)
307  {
308  if(file_exists($a_path))
309  {
310  $this->rotateFiles($a_path.".old");
311  return rename($a_path,$a_path.'.old');
312  }
313  return true;
314  }
321  function unlinkFiles($a_filenames)
322  {
323  if(is_array($a_filenames))
324  {
325  foreach($a_filenames as $file)
326  {
327  if(!$this->unlinkFile($file))
328  {
329  return $file;
330  }
331  }
332  }
333  return '';
334  }
341  function unlinkFile($a_filename)
342  {
343  if(file_exists($this->mail_path.'/'.basename($this->user_id.'_'.$a_filename)))
344  {
345  return unlink($this->mail_path.'/'.basename($this->user_id.'_'.$a_filename));
346  }
347  }
354  function getAbsolutePath($a_path)
355  {
356  return $this->mail_path.'/'.$this->user_id.'_'.$a_path;
357  }
358 
366  function saveFiles($a_mail_id,$a_attachments)
367  {
368  if(!$a_mail_id)
369  {
370  return "INTERNAL HERE ERROR: No valid mail_id given";
371  }
372  if(is_array($a_attachments))
373  {
374  foreach($a_attachments as $attachment)
375  {
376  if(!$this->saveFile($a_mail_id,$attachment))
377  {
378  return $attachment;
379  }
380  }
381  }
382  else
383  {
384  return "ARRAY REQUIRED";
385  }
386  return '';
387  }
395  function saveFile($a_mail_id,$a_attachment)
396  {
397  if(!is_dir($this->mail_path.'/'.$this->user_id.'_'.$a_mail_id))
398  {
399  if(mkdir($this->mail_path.'/'.$this->user_id.'_'.$a_mail_id))
400  {
401  chmod($this->mail_path.'/'.$this->user_id.'_'.$a_mail_id,0755);
402  }
403  else
404  {
405  return false;
406  }
407  }
408  return copy($this->mail_path.'/'.$this->user_id.'_'.$a_attachment,
409  $this->mail_path.'/'.$this->user_id.'_'.$a_mail_id.'/'.$a_attachment);
410  }
417  function checkFilesExist($a_files)
418  {
419  if($a_files)
420  {
421  foreach($a_files as $file)
422  {
423  if(!file_exists($this->mail_path.'/'.$this->user_id.'_'.$file))
424  {
425  return false;
426  }
427  }
428  return true;
429  }
430  return true;
431  }
439  function assignAttachmentsToDirectory($a_mail_id,$a_sent_mail_id)
440  {
441  global $ilDB;
442 
443  $query = "INSERT INTO mail_attachment ".
444  "SET mail_id = ".$ilDB->quote($a_mail_id).", ".
445  "path = ".$ilDB->quote($this->user_id."_".$a_sent_mail_id)." ";
446  $res = $this->ilias->db->query($query);
447  }
454  function deassignAttachmentFromDirectory($a_mail_id)
455  {
456  global $ilDB;
457  // IF IT'S THE LAST MAIL CONTAINING THESE ATTACHMENTS => DELETE ATTACHMENTS
458  $query = "SELECT path FROM mail_attachment ".
459  "WHERE mail_id = ".$ilDB->quote($a_mail_id)." ";
460 
461  $res = $this->ilias->db->query($query);
462  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
463  {
464  $path = $row->path;
465  }
466  if($path)
467  {
468  $query = "SELECT COUNT(mail_id) AS count_mail_id FROM mail_attachment ".
469  "WHERE path = ".$ilDB->quote($path)." ";
470 
471  $res = $this->ilias->db->query($query);
472  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
473  {
474  $cnt_mail_id = $row->count_mail_id;
475  }
476  if($cnt_mail_id == 1)
477  {
479  }
480  }
481 
482  $query = "DELETE FROM mail_attachment ".
483  "WHERE mail_id = ".$ilDB->quote($a_mail_id)." ";
484  $res = $this->ilias->db->query($query);
485  return true;
486  }
487 
488  function __deleteAttachmentDirectory($a_rel_path)
489  {
490  if(!@$dp = opendir($this->mail_path."/".$a_rel_path))
491  {
492  return false;
493  }
494 
495  while($file = @readdir($dp))
496  {
497  if($file == '.' or $file == '..')
498  {
499  continue;
500  }
501  if(is_dir($file))
502  {
504  }
505  unlink($this->mail_path."/".$a_rel_path."/".$file);
506  }
507  @rmdir($this->mail_path."/".$a_rel_path);
508  closedir($dp);
509 
510  return true;
511  }
513  {
514 
515  $this->mail_maxsize_attach = $this->ilias->getSetting("mail_maxsize_attach") ?
516  $this->ilias->getSetting("mail_maxsize_attach") * 1024 : 1024 * 1024 * 64;
517 
518  $this->mail_maxsize_attach_message = $this->ilias->getSetting("mail_maxsize_attach") ?
519  $this->ilias->getSetting("mail_maxsize_attach") :
520  ini_get("upload_max_filesize");
521  }
522 
523  //BEGIN DiskQuota: Get used disk space
529  public static function _getDiskSpaceUsedBy($user_id, $as_string = false)
530  {
531  // XXX - This method is extremely slow. We should
532  // use a cache to speed it up, for example, we should
533  // store the disk space used in table mail_attachment.
534  global $ilDB, $lng;
535 
536  $mail_data_dir = ilUtil::getDataDir('filesystem').DIRECTORY_SEPARATOR."mail";
537 
538  $q = "SELECT path ".
539  "FROM mail_attachment AS ma ".
540  "JOIN mail AS m ON ma.mail_id=m.mail_id ".
541  "WHERE m.user_id = ".$ilDB->quote($user_id);
542  $result_set = $ilDB->query($q);
543  $size = 0;
544  $count = 0;
545  while($row = $result_set->fetchRow(DB_FETCHMODE_ASSOC))
546  {
547  $attachment_path = $mail_data_dir.DIRECTORY_SEPARATOR.$row['path'];
548  $attachment_size = ilUtil::dirsize($attachment_path);
549  if ($attachment_size != -1)
550  {
551  $size += $attachment_size;
552  }
553  $count++;
554  }
555  include_once("Modules/File/classes/class.ilObjFileAccess.php");
556  return ($as_string) ?
557  $count.' '.$lng->txt('mail_attachments').', '.ilObjFileAccess::_sizeToString($size) :
558  $size;
559  }
560  //END DiskQuota: Get used disk space
561 }
562 ?>