ILIAS  Release_4_0_x_branch Revision 61816
 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  {
74  return $this->mail_maxsize_attach_message;
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  // FIRST GET FILES OF USER IN BASE DIRECTORY
217  return $files = $this->getUnsentFiles();
218  }
219 
226  function getUnsentFiles()
227  {
228  $files = array();
229  $dp = opendir($this->mail_path);
230 
231  while($file = readdir($dp))
232  {
233  if(is_dir($file))
234  {
235  continue;
236  }
237  list($uid,$rest) = split('_',$file,2);
238  if($uid == $this->user_id)
239  {
240  if(!is_dir($this->mail_path.'/'.$file))
241  {
242  $files[] = array(
243  'name' => $rest,
244  'size' => filesize($this->mail_path.'/'.$file),
245  'ctime' => ilFormat::formatDate(date('Y-m-d H:i:s',filectime($this->mail_path.'/'.$file))));
246  }
247  }
248  }
249  closedir($dp);
250  return $files;
251  }
258  function storeUploadedFile($a_http_post_file)
259  {
260  if($a_http_post_file["size"] >= $this->mail_maxsize_attach)
261  {
262  return 1;
263  }
264 
265  if(isset($a_http_post_file) && $a_http_post_file['size'])
266  {
267  $a_http_post_file['name'] = ilUtil::_sanitizeFilemame($a_http_post_file['name']);
268 
269  // CHECK IF FILE WITH SAME NAME EXISTS
270  $this->rotateFiles($this->getMailPath().'/'.$this->user_id.'_'.$a_http_post_file['name']);
271 
272  ilUtil::moveUploadedFile($a_http_post_file['tmp_name'],
273  $a_http_post_file['name'], $this->getMailPath().'/'.$this->user_id.'_'.
274  $a_http_post_file['name']);
275 
276  //move_uploaded_file($a_http_post_file['tmp_name'],$this->getMailPath().'/'.$this->user_id.'_'.
277  // $a_http_post_file['name']);
278  return 0;
279  }
280  return 1;
281  }
282 
289  function copyAttachmentFile($a_abs_path,$a_new_name)
290  {
291  @copy($a_abs_path,$this->getMailPath()."/".$this->user_id."_".$a_new_name);
292 
293  return true;
294  }
295 
296 
297 
305  function rotateFiles($a_path)
306  {
307  if(file_exists($a_path))
308  {
309  $this->rotateFiles($a_path.".old");
310  return rename($a_path,$a_path.'.old');
311  }
312  return true;
313  }
320  function unlinkFiles($a_filenames)
321  {
322  if(is_array($a_filenames))
323  {
324  foreach($a_filenames as $file)
325  {
326  if(!$this->unlinkFile($file))
327  {
328  return $file;
329  }
330  }
331  }
332  return '';
333  }
340  function unlinkFile($a_filename)
341  {
342  if(file_exists($this->mail_path.'/'.basename($this->user_id.'_'.$a_filename)))
343  {
344  return unlink($this->mail_path.'/'.basename($this->user_id.'_'.$a_filename));
345  }
346  }
353  function getAbsolutePath($a_path)
354  {
355  return $this->mail_path.'/'.$this->user_id.'_'.$a_path;
356  }
357 
365  function saveFiles($a_mail_id,$a_attachments)
366  {
367  if(!$a_mail_id)
368  {
369  return "INTERNAL HERE ERROR: No valid mail_id given";
370  }
371  if(is_array($a_attachments))
372  {
373  foreach($a_attachments as $attachment)
374  {
375  if(!$this->saveFile($a_mail_id,$attachment))
376  {
377  return $attachment;
378  }
379  }
380  }
381  else
382  {
383  return "ARRAY REQUIRED";
384  }
385  return '';
386  }
387 
388  public static function getStorage($a_mail_id, $a_usr_id)
389  {
390  static $fsstorage_cache = array();
391 
392  if(!is_object($fsstorage_cache[$a_mail_id][$a_usr_id]))
393  {
394  include_once 'Services/Mail/classes/class.ilFSStorageMail.php';
395  $fsstorage_cache[$a_mail_id][$a_usr_id] = new ilFSStorageMail($a_mail_id, $a_usr_id);
396  }
397 
398  return $fsstorage_cache[$a_mail_id][$a_usr_id];
399  }
400 
408  function saveFile($a_mail_id,$a_attachment)
409  {
410  $oStorage = self::getStorage($a_mail_id, $this->user_id);
411  $oStorage->create();
412  $storage_directory = $oStorage->getAbsolutePath();
413 
414  if(@!is_dir($storage_directory))
415  {
416  return false;
417  }
418 
419  return copy($this->mail_path.'/'.$this->user_id.'_'.$a_attachment,
420  $storage_directory.'/'.$a_attachment);
421  }
428  function checkFilesExist($a_files)
429  {
430  if($a_files)
431  {
432  foreach($a_files as $file)
433  {
434  if(!file_exists($this->mail_path.'/'.$this->user_id.'_'.$file))
435  {
436  return false;
437  }
438  }
439  return true;
440  }
441  return true;
442  }
450  function assignAttachmentsToDirectory($a_mail_id,$a_sent_mail_id)
451  {
452  global $ilDB;
453 
454 /* $query = "INSERT INTO mail_attachment ".
455  "SET mail_id = ".$ilDB->quote($a_mail_id).", ".
456  "path = ".$ilDB->quote($this->user_id."_".$a_sent_mail_id)." ";
457  $res = $this->ilias->db->query($query);
458 */
459 
460  $oStorage = self::getStorage($a_sent_mail_id, $this->user_id);
461  $res = $ilDB->manipulateF('
462  INSERT INTO mail_attachment
463  ( mail_id, path) VALUES (%s, %s)',
464  array('integer', 'text'),
465  array($a_mail_id, $oStorage->getRelativePathExMailDirectory())
466  );
467 
468  }
475  function deassignAttachmentFromDirectory($a_mail_id)
476  {
477  global $ilDB;
478  // IF IT'S THE LAST MAIL CONTAINING THESE ATTACHMENTS => DELETE ATTACHMENTS
479  $res = $ilDB->query("SELECT path FROM mail_attachment
480  WHERE mail_id = ".$ilDB->quote($a_mail_id,'integer'));
481 
482  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
483  {
484  $path = $row->path;
485  }
486  if($path)
487  {
488  $res = $ilDB->query("SELECT COUNT(mail_id) count_mail_id FROM mail_attachment
489  WHERE path = ".$ilDB->quote($path,'text')) ;
490 
491  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
492  {
493  $cnt_mail_id = $row->count_mail_id;
494  }
495  if($cnt_mail_id == 1)
496  {
498  }
499  }
500 
501  $res = $ilDB->manipulateF("DELETE FROM mail_attachment
502  WHERE mail_id = %s",
503  array('integer'),
504  array($a_mail_id));
505  return true;
506  }
507 
508  function __deleteAttachmentDirectory($a_rel_path)
509  {
510  ilUtil::delDir($this->mail_path."/".$a_rel_path);
511 
512  return true;
513  }
515  {
516 
517  $this->mail_maxsize_attach = $this->ilias->getSetting("mail_maxsize_attach") ?
518  $this->ilias->getSetting("mail_maxsize_attach") * 1024 : 1024 * 1024 * 64;
519 
520  $this->mail_maxsize_attach_message = $this->ilias->getSetting("mail_maxsize_attach") ?
521  $this->ilias->getSetting("mail_maxsize_attach") :
522  ini_get("upload_max_filesize");
523  }
524 
534  {
535  // XXX - This method is extremely slow. We should
536  // use a cache to speed it up, for example, we should
537  // store the disk space used in table mail_attachment.
538  global $ilDB, $lng;
539 
540  $mail_data_dir = ilUtil::getDataDir('filesystem').DIRECTORY_SEPARATOR."mail";
541 
542  $q = "SELECT path ".
543  "FROM mail_attachment ma ".
544  "JOIN mail m ON ma.mail_id=m.mail_id ".
545  "WHERE m.user_id = ".$ilDB->quote($user_id);
546  $result_set = $ilDB->query($q);
547  $size = 0;
548  $count = 0;
549  while($row = $result_set->fetchRow(DB_FETCHMODE_ASSOC))
550  {
551  $attachment_path = $mail_data_dir.DIRECTORY_SEPARATOR.$row['path'];
552  $attachment_size = ilUtil::dirsize($attachment_path);
553  if ($attachment_size != -1)
554  {
555  $size += $attachment_size;
556  }
557  $count++;
558  }
559  return array('count'=>$count, 'size'=>$size);
560  }
561 }
562 ?>