diff --git a/php/elFinder.class.php b/php/elFinder.class.php index e50dff8321..23de9b8be7 100644 --- a/php/elFinder.class.php +++ b/php/elFinder.class.php @@ -439,6 +439,11 @@ public function __construct($opts) { } set_error_handler('elFinder::phpErrorHandler', $errLevel); + // Associative array of files to delete at the end of script: ['temp file path' => true] + $GLOBALS['elFinderTempFiles'] = array(); + // regist Shutdown function + register_shutdown_function(array('elFinder', 'onShutdown')); + // convert PATH_INFO to GET query if (! empty($_SERVER['PATH_INFO'])) { $_ps = explode('/', trim($_SERVER['PATH_INFO'], '/')); @@ -563,8 +568,7 @@ public function __construct($opts) { foreach ($opts['bind'] as $cmd => $handlers) { $doRegist = (strpos($cmd, '*') !== false); if (! $doRegist) { - $_getcmd = create_function('$cmd', 'list($ret) = explode(\'.\', $cmd);return trim($ret);'); - $doRegist = ($_reqCmd && in_array($_reqCmd, array_map($_getcmd, explode(' ', $cmd)))); + $doRegist = ($_reqCmd && in_array($_reqCmd, array_map('self::getCmdOfBind', explode(' ', $cmd)))); } if ($doRegist) { // for backward compatibility @@ -709,8 +713,8 @@ public function bind($cmd, $handler) { list(, $sub) = array_pad(explode('.', $_cmd), 2, ''); if ($sub) { $sub = str_replace('\'', '\\\'', $sub); - $addSub = create_function('$cmd', 'return $cmd . \'.\' . trim(\'' . $sub . '\');'); - $cmds = array_merge($cmds, array_map($addSub, $allCmds)); + $subs = array_fill(0, count($allCmds), $sub); + $cmds = array_merge($cmds, array_map(array('elFinder', 'addSubToBindName'), $allCmds, $subs)); } else { $cmds = array_merge($cmds, $allCmds); } @@ -1440,7 +1444,7 @@ protected function zipdl($args) { if (($volume = $this->volume($targets[0])) !== false) { if ($dlres = $volume->zipdl($targets)) { $path = $dlres['path']; - register_shutdown_function(create_function('$f', 'connection_status() && is_file($f) && unlink($f);'), $path); + register_shutdown_function(array('elFinder', 'rmFileInDisconnected'), $path); if (count($targets) === 1) { $name = basename($volume->path($targets[0])); } else { @@ -1466,7 +1470,8 @@ protected function zipdl($args) { } $file = $targets[1]; $path = $volume->getTempPath().DIRECTORY_SEPARATOR.$file; - register_shutdown_function(create_function('$f', 'is_file($f) && unlink($f);'), $path); + // register auto delete on shutdown + $GLOBALS['elFinderTempFiles'][$path] = true; if (!is_readable($path)) { return array('error' => 'File not found', 'header' => $h404, 'raw' => true); } @@ -2419,23 +2424,6 @@ protected function upload($args) { $this->itemLock($target); - // regist Shutdown function - $GLOBALS['elFinderTempFiles'] = array(); -// if (version_compare(PHP_VERSION, '5.3.0', '>=')) { -// $shutdownfunc = function(){ // <- Parse error on PHP < 5.3 ;-( -// foreach(array_keys($GLOBALS['elFinderTempFiles']) as $f){ -// unlink($f); -// } -// }; -// } else { - $shutdownfunc = create_function('', ' - foreach(array_keys($GLOBALS[\'elFinderTempFiles\']) as $f){ - is_file($f) && unlink($f); - } - '); -// } - register_shutdown_function($shutdownfunc); - // file extentions table by MIME $extTable = array_flip(array_unique($volume->getMimeTable())); @@ -3892,4 +3880,65 @@ public static function curlExec($curl, $options = array(), $headers = array()) { return $result; } + /***************************************************************************/ + /* callbacks */ + /***************************************************************************/ + + /** + * Get command name of binded "commandName.subName" + * + * @param string $cmd + * @return string + */ + protected static function getCmdOfBind($cmd) { + list($ret) = explode('.', $cmd); + return trim($ret); + } + + /** + * Add subName to commandName + * + * @param string $cmd + * @param string $sub + * @return string + */ + protected static function addSubToBindName($cmd, $sub) { + return $cmd . '.' . trim($sub); + } + + /** + * Remove a file if connection is disconnected + * + * @param string $file + */ + public static function rmFileInDisconnected($file) { + (connection_aborted() || connection_status() !== CONNECTION_NORMAL) && is_file($file) && unlink($file); + } + + /** + * Call back function on shutdown + * - delete files in $GLOBALS['elFinderTempFiles'] + * + */ + public static function onShutdown() { + if (! empty($GLOBALS['elFinderTempFiles'])) { + foreach(array_keys($GLOBALS['elFinderTempFiles']) as $f){ + is_file($f) && unlink($f); + } + } + } + + /** + * Garbage collection with glob + * + * @param string $pattern + * @param integer $time + */ + public static function GlobGC($pattern, $time) { + $now = time(); + foreach(glob($pattern) as $file) { + (filemtime($file) < ($now - $time)) && unlink($file); + } + } + } // END class diff --git a/php/elFinderVolumeDriver.class.php b/php/elFinderVolumeDriver.class.php index 6a14fe21ad..203870e221 100644 --- a/php/elFinderVolumeDriver.class.php +++ b/php/elFinderVolumeDriver.class.php @@ -3021,14 +3021,7 @@ public function getContentUrl($hash, $options = array()) { } else if (!empty($options['temporary']) && $this->tmpLinkPath) { $name = 'temp_' . md5($hash); $path = $this->tmpLinkPath . DIRECTORY_SEPARATOR . $name; - $contents = $this->getContents($hash); - $gc = create_function('$p,$t', 'foreach(glob($p) as $f) { (filemtime($f) < (time() - $t)) && unlink($f); }'); - /*$gc = function($p,$t) { - foreach(glob($p) as $f) { - (filemtime($f) < (time() - $t)) && unlink($f); - } - };*/ - register_shutdown_function($gc, $this->tmpLinkPath . DIRECTORY_SEPARATOR . 'temp_*', elFinder::$tmpLinkLifeTime); + register_shutdown_function(array('elFinder', 'GlobGC'), $this->tmpLinkPath . DIRECTORY_SEPARATOR . 'temp_*', elFinder::$tmpLinkLifeTime); if (file_put_contents($path, $this->getContents($hash))) { return $this->tmpLinkUrl . '/' . $name; } @@ -3189,7 +3182,8 @@ public function ffmpegToImg($file, $stat, $self, $ss = null) { $name = basename($file); $path = dirname($file); $tmp = $path . DIRECTORY_SEPARATOR . md5($name); - $GLOBALS['elFinderTempFiles'][] = $tmp; // regist to remove at the end + // register auto delete on shutdown + $GLOBALS['elFinderTempFiles'][$tmp] = true; if (rename($file, $tmp)) { if ($ss === null) { // specific start time by file name (xxx^[sec].[extention] - video^3.mp4) @@ -3670,14 +3664,12 @@ protected function getTempFile($path = '') { } if ($tmpdir = $this->getTempPath()) { - if (!$rmfunc) { - $rmfunc = create_function('$f', 'is_file($f) && unlink($f);'); - } $name = tempnam($tmpdir, 'ELF'); if ($key) { $cache[$key] = $name; } - register_shutdown_function($rmfunc, $name); + // register auto delete on shutdown + $GLOBALS['elFinderTempFiles'][$name] = true; return $name; }