Przeglądaj źródła

better tmpdir autodetection ?

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@15601 44c647ce-9c0f-0410-b52a-842ac1e357ba
fab 16 lat temu
rodzic
commit
1b7a98a07f
1 zmienionych plików z 63 dodań i 22 usunięć
  1. 63 22
      library/Zend/Cache/Backend.php

+ 63 - 22
library/Zend/Cache/Backend.php

@@ -139,34 +139,75 @@ class Zend_Cache_Backend
     {
         return true;
     }
-
+   
     /**
-     * Return a system-wide tmp directory
+     * Determine system TMP directory and detect if we have read access
+     *
+     * inspired from Zend_File_Transfer_Adapter_Abstract 
      *
-     * @return string System-wide tmp directory
+     * @return string
+     * @throws Zend_Cache_Exception if unable to determine directory
      */
-    static function getTmpDir()
+    public function getTmpDir()
     {
-        if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
-            // windows...
-            foreach (array($_ENV, $_SERVER) as $tab) {
-                foreach (array('TEMP', 'TMP', 'windir', 'SystemRoot') as $key) {
-                    if (isset($tab[$key])) {
-                        $result = $tab[$key];
-                        if (($key == 'windir') or ($key == 'SystemRoot')) {
-                            $result = $result . '\\temp';
-                        }
-                        return $result;
+    	$tmpdir = array();
+        foreach (array($_ENV, $_SERVER) as $tab) {
+        	foreach (array('TMPDIR', 'TEMP', 'TMP', 'windir', 'SystemRoot') as $key) {
+        		if (isset($tab[$key])) {
+        			if (($key == 'windir') or ($key == 'SystemRoot')) {
+                        $dir = realpath($tab[$key] . '\\temp');
+                    } else {
+                    	$dir = realpath($tab[$key]);
                     }
-                }
-            }
-            return '\\temp';
-        } else {
-            // unix...
-            if (isset($_ENV['TMPDIR']))    return $_ENV['TMPDIR'];
-            if (isset($_SERVER['TMPDIR'])) return $_SERVER['TMPDIR'];
-            return '/tmp';
+        			if ($this->_isGoodTmpDir($dir)) {
+        				return $dir;
+        			}
+        		}
+        	}
+        }
+        $upload = ini_get('upload_tmp_dir');
+        if ($upload) {
+            $dir = realpath($upload);
+        	if ($this->_isGoodTmpDir($dir)) {
+        		return $dir;
+        	}
+        }
+        if (function_exists('sys_get_temp_dir')) {
+            $dir = sys_get_temp_dir();
+        	if ($this->_isGoodTmpDir($dir)) {
+        		return $dir;
+        	}
+        }
+        // Attemp to detect by creating a temporary file
+        $tempFile = tempnam(md5(uniqid(rand(), TRUE)), '');
+        if ($tempFile) {
+        	$dir = realpath(dirname($tempFile));
+            unlink($tempFile);
+            return $dir;
         }
+        if ($this->_isGoodTmpDir('/tmp')) {
+        	return '/tmp';
+        }
+        if ($this->_isGoodTmpDir('\\temp')) {
+        	return '\\temp';
+        }
+        Zend_Cache::throwException('Could not determine temp directory, please specify a cache_dir manually');
+    }
+    
+    /**
+     * Verify if the given temporary directory is readable and writable
+     * 
+     * @param $dir temporary directory
+     * @return boolean true if the directory is ok
+     */
+    protected function _isGoodTmpDir($dir)
+    {
+    	if (is_readable($dir)) {
+	    	if (is_writable($dir)) {
+	    		return true;
+	    	}
+    	}
+    	return false;
     }
 
     /**