From 77b51f03e3495fec8f0ed6489b884b1f13e6157d Mon Sep 17 00:00:00 2001
From: Robin Appelman <icewind@owncloud.com>
Date: Tue, 28 Feb 2012 11:16:19 +0100
Subject: [PATCH] add temporary file managment

---
 lib/base.php   |  3 +++
 lib/helper.php | 25 +++++++++++++++++++++++++
 2 files changed, 28 insertions(+)

diff --git a/lib/base.php b/lib/base.php
index 32dcfd7825..7783b4c638 100644
--- a/lib/base.php
+++ b/lib/base.php
@@ -313,6 +313,9 @@ class OC{
 		// Last part: connect some hooks
 		OC_HOOK::connect('OC_User', 'post_createUser', 'OC_Connector_Sabre_Principal', 'addPrincipal');
 		OC_HOOK::connect('OC_User', 'post_deleteUser', 'OC_Connector_Sabre_Principal', 'deletePrincipal');
+		
+		//make sure temporary files are cleaned up
+		register_shutdown_function(array('OC_Helper','cleanTmp'));
 	}
 }
 
diff --git a/lib/helper.php b/lib/helper.php
index 1ea0a55f46..3c76d38328 100644
--- a/lib/helper.php
+++ b/lib/helper.php
@@ -26,6 +26,7 @@
  */
 class OC_Helper {
 	private static $mimetypes=array();
+	private static $tmpFiles=array();
 	
 	/**
 	 * @brief Creates an url
@@ -415,4 +416,28 @@ class OC_Helper {
 		}
 		return $count;
 	}
+	
+	/**
+	 * create a temporary file with an unique filename
+	 * @param string postfix
+	 * @return string
+	 *
+	 * temporary files are automatically cleaned up after the script is finished
+	 */
+	public static function tmpFile($postfix=''){
+		$file=tempnam(get_temp_dir(),'OC_TMP_').$postfix;
+		self::$tmpFiles[]=$file;
+		return $file;
+	}
+	
+	/**
+	 * remove all files created by self::tmpFile
+	 */
+	public static function cleanTmp(){
+		foreach(self::$tmpFiles as $file){
+			if(file_exists($file)){
+				unlink($file);
+			}
+		}
+	}
 }
-- 
GitLab