From 70d4b9a5dc39a7165e469aa3b4f24d49e9a20b9e Mon Sep 17 00:00:00 2001
From: thomascube <thomas@roundcube.net>
Date: Fri, 08 Sep 2006 08:03:22 -0400
Subject: [PATCH] Fixed safe_mode issues

---
 program/include/session.inc    |    6 --
 program/steps/mail/upload.inc  |    4 +-
 program/include/main.inc       |   26 ++++++++-----
 program/steps/mail/compose.inc |    6 +-
 program/steps/mail/func.inc    |   28 -------------
 5 files changed, 23 insertions(+), 47 deletions(-)

diff --git a/program/include/main.inc b/program/include/main.inc
index 0d3cf82..c084588 100644
--- a/program/include/main.inc
+++ b/program/include/main.inc
@@ -823,19 +823,25 @@
   }
 
 
-// remove temp files of a session
-function rcmail_clear_session_temp($sess_id)
+// remove temp files older than two day
+function rcmail_temp_gc()
   {
-  global $CONFIG;
+  $tmp = unslashify($CONFIG['temp_dir']);
+  $expire = mktime() - 172800;  // expire in 48 hours
 
-  $temp_dir = slashify($CONFIG['temp_dir']);
-  $cache_dir = $temp_dir.$sess_id;
-
-  if (is_dir($cache_dir))
+  if ($dir = opendir($tmp))
     {
-    clear_directory($cache_dir);
-    rmdir($cache_dir);
-    }  
+    while (($fname = readdir($dir)) !== false)
+      {
+      if ($fname{0} == '.')
+        continue;
+
+      if (filemtime($tmp.'/'.$fname) < $expire)
+        @unlink($tmp.'/'.$fname);
+      }
+
+    closedir($dir);
+    }
   }
 
 
diff --git a/program/include/session.inc b/program/include/session.inc
index dc77147..6c4687e 100644
--- a/program/include/session.inc
+++ b/program/include/session.inc
@@ -105,7 +105,6 @@
               WHERE sess_id=?",
               $key);
 
-  rcmail_clear_session_temp($key);
   return TRUE;
   }
 
@@ -137,12 +136,9 @@
                 WHERE sess_id IN ('".join("','", $a_exp_sessions)."')");
     }
 
-  // remove session specific temp dirs
-  foreach ($a_exp_sessions as $key)
-    rcmail_clear_session_temp($key);
-
   // also run message cache GC
   rcmail_message_cache_gc();
+  rcmail_temp_gc();
 
   return TRUE;
   }
diff --git a/program/steps/mail/compose.inc b/program/steps/mail/compose.inc
index 14adb12..29fdddd 100644
--- a/program/steps/mail/compose.inc
+++ b/program/steps/mail/compose.inc
@@ -497,9 +497,9 @@
   
 function rcmail_write_compose_attachments(&$message)
   {
-  global $IMAP;
-    
-  $temp_dir = rcmail_create_compose_tempdir();
+  global $IMAP, $CONFIG;
+
+  $temp_dir = unslashify($CONFIG['temp_dir']);
 
   if (!is_array($_SESSION['compose']['attachments']))
     $_SESSION['compose']['attachments'] = array();
diff --git a/program/steps/mail/func.inc b/program/steps/mail/func.inc
index 827f131..b51ec4b 100644
--- a/program/steps/mail/func.inc
+++ b/program/steps/mail/func.inc
@@ -1384,42 +1384,16 @@
   }
 
 
-// create temp dir for attachments
-function rcmail_create_compose_tempdir()
-  {
-  global $CONFIG;
-  
-  if ($_SESSION['compose']['temp_dir'])
-    return $_SESSION['compose']['temp_dir'];
-  
-  if (!empty($CONFIG['temp_dir']))
-    $temp_dir = $CONFIG['temp_dir'].(!eregi('\/$', $CONFIG['temp_dir']) ? '/' : '').$_SESSION['compose']['id'];
-
-  // create temp-dir for uploaded attachments
-  if (!empty($CONFIG['temp_dir']) && is_writeable($CONFIG['temp_dir']))
-    {
-    mkdir($temp_dir);
-    $_SESSION['compose']['temp_dir'] = $temp_dir;
-    }
-
-  return $_SESSION['compose']['temp_dir'];
-  }
-
-
 // clear message composing settings
 function rcmail_compose_cleanup()
   {
   if (!isset($_SESSION['compose']))
     return;
-  
+
   // remove attachment files from temp dir
   if (is_array($_SESSION['compose']['attachments']))
     foreach ($_SESSION['compose']['attachments'] as $attachment)
       @unlink($attachment['path']);
-
-  // kill temp dir
-  if ($_SESSION['compose']['temp_dir'])
-    @rmdir($_SESSION['compose']['temp_dir']);
   
   unset($_SESSION['compose']);
   }
diff --git a/program/steps/mail/upload.inc b/program/steps/mail/upload.inc
index 850ccd0..cde4ed2 100644
--- a/program/steps/mail/upload.inc
+++ b/program/steps/mail/upload.inc
@@ -26,8 +26,8 @@
   }
 
 
-// create temp dir for file uploads
-$temp_dir = rcmail_create_compose_tempdir();
+// use common temp dir for file uploads
+$temp_dir = unslashify($CONFIG['temp_dir']);
 
 
 if (!is_array($_SESSION['compose']['attachments']))

--
Gitblit v1.9.1