ILIAS  Release_4_2_x_branch Revision 61807
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilFileDataMail.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 
12 require_once("classes/class.ilFileData.php");
13 
15 {
21  var $user_id;
22 
29 
31 
39  function ilFileDataMail($a_user_id = 0)
40  {
41  define('MAILPATH','mail');
43  $this->mail_path = parent::getPath()."/".MAILPATH;
44  $this->checkReadWrite();
45  $this->user_id = $a_user_id;
46 
47 
48  $this->__initAttachmentMaxSize();
49  }
50 
57  function initDirectory()
58  {
59  if(is_writable($this->getPath()))
60  {
61  if(mkdir($this->getPath().'/'.MAILPATH))
62  {
63  if(chmod($this->getPath().'/'.MAILPATH,0755))
64  {
65  $this->mail_path = $this->getPath().'/'.MAILPATH;
66  return true;
67  }
68  }
69  }
70  return false;
71  }
72  function getUploadLimit()
73  {
75  }
76 
82  function getMailPath()
83  {
84  return $this->mail_path;
85  }
86 
94  public function getAttachmentPathByMD5Filename($a_filename,$a_mail_id)
95  {
96  global $ilDB;
97 
98 /* $query = "SELECT path FROM mail_attachment ".
99  "WHERE mail_id = ".$ilDB->quote($a_mail_id)."";
100 
101  $row = $this->ilias->db->getRow($query,DB_FETCHMODE_OBJECT);
102  $path = $this->getMailPath().'/'.$row->path;
103 */
104  $query = $ilDB->query("SELECT path FROM mail_attachment
105  WHERE mail_id = ".$ilDB->quote($a_mail_id,'integer')."");
106 
107  $rel_path = "";
108  while($row = $ilDB->fetchObject($query))
109  {
110  $rel_path = $row->path;
111  $path = $this->getMailPath().'/'.$row->path;
112 
113  }
114 
116  foreach((array)$files as $file)
117  {
118  if($file['type'] == 'file' && md5($file['entry']) == $a_filename)
119  {
120  return array(
121  'path' => $this->getMailPath().'/'.$rel_path.'/'.$file['entry'],
122  'filename' => $file['entry']
123  );
124  }
125  }
126  return '';
127  }
128 
129 
137  function getAttachmentPath($a_filename,$a_mail_id)
138  {
139  global $ilDB;
140 
141 /* $query = "SELECT path FROM mail_attachment ".
142  "WHERE mail_id = ".$ilDB->quote($a_mail_id)."";
143 
144  $row = $this->ilias->db->getRow($query,DB_FETCHMODE_OBJECT);
145  $path = $this->getMailPath().'/'.$row->path.'/'.$a_filename;
146 */
147  $query = $ilDB->query("SELECT path FROM mail_attachment ".
148  "WHERE mail_id = ".$ilDB->quote($a_mail_id, 'integer')."");
149 
150  while($row = $ilDB->fetchObject($query))
151  {
152  $path = $this->getMailPath().'/'.$row->path.'/'.$a_filename;
153  }
154 
155  if(file_exists($path))
156  {
157  if(is_readable($path))
158  {
159  return $path;
160  }
161  return '';
162  }
163  return '';
164  }
172  function adoptAttachments($a_attachments,$a_mail_id)
173  {
174  if(is_array($a_attachments))
175  {
176  foreach($a_attachments as $file)
177  {
178  $path = $this->getAttachmentPath($file,$a_mail_id);
179  if(!copy($path,$this->getMailPath().'/'.$this->user_id.'_'.$file))
180  {
181  return "ERROR: $this->getMailPath().'/'.$this->user_id.'_'.$file cannot be created";
182  }
183  }
184  }
185  else
186  {
187  return "ARRAY REQUIRED";
188  }
189  return '';
190  }
191 
198  function checkReadWrite()
199  {
200  if(is_writable($this->mail_path) && is_readable($this->mail_path))
201  {
202  return true;
203  }
204  else
205  {
206  $this->ilias->raiseError("Mail directory is not readable/writable by webserver: ".$this->mail_path,$this->ilias->error_obj->FATAL);
207  }
208  }
214  function getUserFilesData()
215  {
216  return $files = $this->getUnsentFiles();
217  }
218 
225  function getUnsentFiles()
226  {
227  $files = array();
228  $dp = opendir($this->mail_path);
229 
230  while($file = readdir($dp))
231  {
232  if(is_dir($file))
233  {
234  continue;
235  }
236  list($uid,$rest) = explode('_',$file,2);
237  if($uid == $this->user_id)
238  {
239  if(!is_dir($this->mail_path.'/'.$file))
240  {
241  $files[] = array(
242  'name' => $rest,
243  'size' => filesize($this->mail_path.'/'.$file),
244  'ctime' => filectime($this->mail_path.'/'.$file)
245  );
246  }
247  }
248  }
249  closedir($dp);
250  return $files;
251  }
252 
259  public function storeAsAttachment($a_filename,$a_content)
260  {
261  if(strlen($a_content) >= $this->mail_maxsize_attach)
262  {
263  return 1;
264  }
265  $name = ilUtil::_sanitizeFilemame($a_filename);
266  $this->rotateFiles($this->getMailPath().'/'.$this->user_id.'_'.$name);
267 
268  $abs_path = $this->getMailPath().'/'.$this->user_id.'_'.$name;
269 
270  if(!$fp = @fopen($abs_path,'w+'))
271  {
272  return false;
273  }
274  if(@fwrite($fp,$a_content) === false)
275  {
276  @fclose($fp);
277  return false;
278  }
279  @fclose($fp);
280  return true;
281  }
282 
283 
290  function storeUploadedFile($a_http_post_file)
291  {
292  if($a_http_post_file["size"] >= $this->mail_maxsize_attach)
293  {
294  return 1;
295  }
296 
297  if(isset($a_http_post_file) && $a_http_post_file['size'])
298  {
299  $a_http_post_file['name'] = ilUtil::_sanitizeFilemame($a_http_post_file['name']);
300 
301  // CHECK IF FILE WITH SAME NAME EXISTS
302  $this->rotateFiles($this->getMailPath().'/'.$this->user_id.'_'.$a_http_post_file['name']);
303 
304  ilUtil::moveUploadedFile($a_http_post_file['tmp_name'],
305  $a_http_post_file['name'], $this->getMailPath().'/'.$this->user_id.'_'.
306  $a_http_post_file['name']);
307 
308  //move_uploaded_file($a_http_post_file['tmp_name'],$this->getMailPath().'/'.$this->user_id.'_'.
309  // $a_http_post_file['name']);
310  return 0;
311  }
312  return 1;
313  }
314 
321  function copyAttachmentFile($a_abs_path,$a_new_name)
322  {
323  @copy($a_abs_path,$this->getMailPath()."/".$this->user_id."_".$a_new_name);
324 
325  return true;
326  }
327 
328 
329 
337  function rotateFiles($a_path)
338  {
339  if(file_exists($a_path))
340  {
341  $this->rotateFiles($a_path.".old");
342  return rename($a_path,$a_path.'.old');
343  }
344  return true;
345  }
352  function unlinkFiles($a_filenames)
353  {
354  if(is_array($a_filenames))
355  {
356  foreach($a_filenames as $file)
357  {
358  if(!$this->unlinkFile($file))
359  {
360  return $file;
361  }
362  }
363  }
364  return '';
365  }
372  function unlinkFile($a_filename)
373  {
374  if(file_exists($this->mail_path.'/'.basename($this->user_id.'_'.$a_filename)))
375  {
376  return unlink($this->mail_path.'/'.basename($this->user_id.'_'.$a_filename));
377  }
378  }
385  function getAbsolutePath($a_path)
386  {
387  return $this->mail_path.'/'.$this->user_id.'_'.$a_path;
388  }
389 
397  function saveFiles($a_mail_id,$a_attachments)
398  {
399  if(!$a_mail_id)
400  {
401  return "INTERNAL HERE ERROR: No valid mail_id given";
402  }
403  if(is_array($a_attachments))
404  {
405  foreach($a_attachments as $attachment)
406  {
407  if(!$this->saveFile($a_mail_id,$attachment))
408  {
409  return $attachment;
410  }
411  }
412  }
413  else
414  {
415  return "ARRAY REQUIRED";
416  }
417  return '';
418  }
419 
420  public static function getStorage($a_mail_id, $a_usr_id)
421  {
422  static $fsstorage_cache = array();
423 
424  if(!is_object($fsstorage_cache[$a_mail_id][$a_usr_id]))
425  {
426  include_once 'Services/Mail/classes/class.ilFSStorageMail.php';
427  $fsstorage_cache[$a_mail_id][$a_usr_id] = new ilFSStorageMail($a_mail_id, $a_usr_id);
428  }
429 
430  return $fsstorage_cache[$a_mail_id][$a_usr_id];
431  }
432 
440  function saveFile($a_mail_id,$a_attachment)
441  {
442  $oStorage = self::getStorage($a_mail_id, $this->user_id);
443  $oStorage->create();
444  $storage_directory = $oStorage->getAbsolutePath();
445 
446  if(@!is_dir($storage_directory))
447  {
448  return false;
449  }
450 
451  return copy($this->mail_path.'/'.$this->user_id.'_'.$a_attachment,
452  $storage_directory.'/'.$a_attachment);
453  }
460  function checkFilesExist($a_files)
461  {
462  if($a_files)
463  {
464  foreach($a_files as $file)
465  {
466  if(!file_exists($this->mail_path.'/'.$this->user_id.'_'.$file))
467  {
468  return false;
469  }
470  }
471  return true;
472  }
473  return true;
474  }
482  function assignAttachmentsToDirectory($a_mail_id,$a_sent_mail_id)
483  {
484  global $ilDB;
485 
486 /* $query = "INSERT INTO mail_attachment ".
487  "SET mail_id = ".$ilDB->quote($a_mail_id).", ".
488  "path = ".$ilDB->quote($this->user_id."_".$a_sent_mail_id)." ";
489  $res = $this->ilias->db->query($query);
490 */
491 
492  $oStorage = self::getStorage($a_sent_mail_id, $this->user_id);
493  $res = $ilDB->manipulateF('
494  INSERT INTO mail_attachment
495  ( mail_id, path) VALUES (%s, %s)',
496  array('integer', 'text'),
497  array($a_mail_id, $oStorage->getRelativePathExMailDirectory())
498  );
499 
500  }
507  function deassignAttachmentFromDirectory($a_mail_id)
508  {
509  global $ilDB;
510  // IF IT'S THE LAST MAIL CONTAINING THESE ATTACHMENTS => DELETE ATTACHMENTS
511  $res = $ilDB->query("SELECT path FROM mail_attachment
512  WHERE mail_id = ".$ilDB->quote($a_mail_id,'integer'));
513 
514  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
515  {
516  $path = $row->path;
517  }
518  if($path)
519  {
520  $res = $ilDB->query("SELECT COUNT(mail_id) count_mail_id FROM mail_attachment
521  WHERE path = ".$ilDB->quote($path,'text')) ;
522 
523  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
524  {
525  $cnt_mail_id = $row->count_mail_id;
526  }
527  if($cnt_mail_id == 1)
528  {
530  }
531  }
532 
533  $res = $ilDB->manipulateF("DELETE FROM mail_attachment
534  WHERE mail_id = %s",
535  array('integer'),
536  array($a_mail_id));
537  return true;
538  }
539 
540  function __deleteAttachmentDirectory($a_rel_path)
541  {
542  ilUtil::delDir($this->mail_path."/".$a_rel_path);
543 
544  return true;
545  }
547  {
550  // Copy of ilFileInputGUI: begin
551  // get the value for the maximal uploadable filesize from the php.ini (if available)
552  $umf = ini_get("upload_max_filesize");
553  // get the value for the maximal post data from the php.ini (if available)
554  $pms = ini_get("post_max_size");
555 
556  //convert from short-string representation to "real" bytes
557  $multiplier_a=array("K"=>1024, "M"=>1024*1024, "G"=>1024*1024*1024);
558 
559  $umf_parts=preg_split("/(\d+)([K|G|M])/", $umf, -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);
560  $pms_parts=preg_split("/(\d+)([K|G|M])/", $pms, -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);
561 
562  if (count($umf_parts) == 2) { $umf = $umf_parts[0]*$multiplier_a[$umf_parts[1]]; }
563  if (count($pms_parts) == 2) { $pms = $pms_parts[0]*$multiplier_a[$pms_parts[1]]; }
564 
565  // use the smaller one as limit
566  $max_filesize = min($umf, $pms);
567 
568  if (!$max_filesize) $max_filesize = max($umf, $pms);
569  // Copy of ilFileInputGUI: end
570 
571  $mail_system_limitation_in_byte = (float)$this->ilias->getSetting('mail_maxsize_attach', 0) * 1024;
572  if(!$mail_system_limitation_in_byte)
573  {
574  $mail_system_limitation_in_byte = $max_filesize;
575  }
576 
577  $mail_system_limitation_in_byte = min($mail_system_limitation_in_byte, $max_filesize);
578 
579  $this->mail_maxsize_attach = $mail_system_limitation_in_byte;
580  }
581 
591  {
592  // XXX - This method is extremely slow. We should
593  // use a cache to speed it up, for example, we should
594  // store the disk space used in table mail_attachment.
595  global $ilDB, $lng;
596 
597  $mail_data_dir = ilUtil::getDataDir('filesystem').DIRECTORY_SEPARATOR."mail";
598 
599  $q = "SELECT path ".
600  "FROM mail_attachment ma ".
601  "JOIN mail m ON ma.mail_id=m.mail_id ".
602  "WHERE m.user_id = ".$ilDB->quote($user_id);
603  $result_set = $ilDB->query($q);
604  $size = 0;
605  $count = 0;
606  while($row = $result_set->fetchRow(DB_FETCHMODE_ASSOC))
607  {
608  $attachment_path = $mail_data_dir.DIRECTORY_SEPARATOR.$row['path'];
609  $attachment_size = ilUtil::dirsize($attachment_path);
610  if ($attachment_size != -1)
611  {
612  $size += $attachment_size;
613  }
614  $count++;
615  }
616  return array('count'=>$count, 'size'=>$size);
617  }
618 }
619 ?>