From 5c9a3b3f39f6ff0d5c84c2cf283d27eaebe2e66e Mon Sep 17 00:00:00 2001 From: Chad Parry Date: Sat, 23 Apr 2011 21:19:47 -0600 Subject: Create a tempnam substitute that safely creates files with a given extension. --- modules/gallery/helpers/system.php | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'modules/gallery/helpers/system.php') diff --git a/modules/gallery/helpers/system.php b/modules/gallery/helpers/system.php index c39c7227..31ecafa7 100644 --- a/modules/gallery/helpers/system.php +++ b/modules/gallery/helpers/system.php @@ -40,4 +40,29 @@ class system_Core { } return null; } + + /** + * Create a file with a unique file name. + * This helper is similar to the built-in tempnam, except that it supports an optional postfix. + */ + static function tempnam($dir = TMPPATH, $prefix = "", $postfix = "") { + return self::_tempnam($dir, $prefix, $postfix, "tempnam"); + } + + // This helper provides a dependency-injected implementation of tempnam. + static function _tempnam($dir, $prefix, $postfix, $builtin) { + $success = false; + do { + $basename = call_user_func($builtin, $dir, $prefix); + if (!$basename) { + return false; + } + $filename = $basename . $postfix; + $success = !file_exists($filename) && @rename($basename, $filename); + if (!$success) { + @unlink($basename); + } + } while (!$success); + return $filename; + } } \ No newline at end of file -- cgit v1.2.3 From 0e844766baf3b3875cbb2d84579626e05e879420 Mon Sep 17 00:00:00 2001 From: Chad Parry Date: Sat, 30 Apr 2011 16:40:55 -0600 Subject: Change the signature of system::tempnam to something more appropriate for Gallery. --- modules/gallery/controllers/quick.php | 4 ++-- modules/gallery/helpers/system.php | 13 ++++++++----- modules/gallery/tests/System_Helper_Test.php | 5 +++-- 3 files changed, 13 insertions(+), 9 deletions(-) (limited to 'modules/gallery/helpers/system.php') diff --git a/modules/gallery/controllers/quick.php b/modules/gallery/controllers/quick.php index ce52cb8d..b6576ec0 100644 --- a/modules/gallery/controllers/quick.php +++ b/modules/gallery/controllers/quick.php @@ -36,8 +36,8 @@ class Quick_Controller extends Controller { } if ($degrees) { - $tmpfile = system::tempnam(TMPPATH, "rotate", - "." . pathinfo($item->file_path(), PATHINFO_EXTENSION)); + $tmpfile = system::temp_filename("rotate", + pathinfo($item->file_path(), PATHINFO_EXTENSION)); gallery_graphics::rotate($item->file_path(), $tmpfile, array("degrees" => $degrees), $item); $item->set_data_file($tmpfile); $item->save(); diff --git a/modules/gallery/helpers/system.php b/modules/gallery/helpers/system.php index 31ecafa7..9815d588 100644 --- a/modules/gallery/helpers/system.php +++ b/modules/gallery/helpers/system.php @@ -43,15 +43,18 @@ class system_Core { /** * Create a file with a unique file name. - * This helper is similar to the built-in tempnam, except that it supports an optional postfix. + * This helper is similar to the built-in tempnam. + * It allows the caller to specify a prefix and an extension. + * It always places the file in TMPPATH. */ - static function tempnam($dir = TMPPATH, $prefix = "", $postfix = "") { - return self::_tempnam($dir, $prefix, $postfix, "tempnam"); + static function temp_filename($prefix = "", $extension = "") { + return self::_tempnam(TMPPATH, $prefix, ".$extension", "tempnam"); } - // This helper provides a dependency-injected implementation of tempnam. + /** + * This helper provides a dependency-injected implementation of tempnam. + */ static function _tempnam($dir, $prefix, $postfix, $builtin) { - $success = false; do { $basename = call_user_func($builtin, $dir, $prefix); if (!$basename) { diff --git a/modules/gallery/tests/System_Helper_Test.php b/modules/gallery/tests/System_Helper_Test.php index 734f98ac..dfe5d9ab 100644 --- a/modules/gallery/tests/System_Helper_Test.php +++ b/modules/gallery/tests/System_Helper_Test.php @@ -18,10 +18,11 @@ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ class System_Helper_Test extends Gallery_Unit_Test_Case { - public function tempnam_random_test() { - $filename = system::tempnam(TMPPATH, "file", ".ext"); + public function temp_filename_random_test() { + $filename = system::temp_filename("file", "ext"); $this->assert_true(file_exists($filename), "File not created"); unlink($filename); + $this->assert_pattern($filename, "|/file.*\\.ext$|"); } public function tempnam_collision_test() { -- cgit v1.2.3 From d2331bf43457a8d33491921f106879f087438171 Mon Sep 17 00:00:00 2001 From: Chad Parry Date: Wed, 4 May 2011 17:48:25 -0600 Subject: Simplified the temp_filename implementation and removed the mocks. --- modules/gallery/helpers/system.php | 15 +++-------- modules/gallery/tests/Mock_Built_In.php | 39 ---------------------------- modules/gallery/tests/System_Helper_Test.php | 25 +----------------- 3 files changed, 5 insertions(+), 74 deletions(-) delete mode 100644 modules/gallery/tests/Mock_Built_In.php (limited to 'modules/gallery/helpers/system.php') diff --git a/modules/gallery/helpers/system.php b/modules/gallery/helpers/system.php index 9815d588..e7e58a70 100644 --- a/modules/gallery/helpers/system.php +++ b/modules/gallery/helpers/system.php @@ -47,20 +47,13 @@ class system_Core { * It allows the caller to specify a prefix and an extension. * It always places the file in TMPPATH. */ - static function temp_filename($prefix = "", $extension = "") { - return self::_tempnam(TMPPATH, $prefix, ".$extension", "tempnam"); - } - - /** - * This helper provides a dependency-injected implementation of tempnam. - */ - static function _tempnam($dir, $prefix, $postfix, $builtin) { + static function temp_filename($prefix="", $extension="") { do { - $basename = call_user_func($builtin, $dir, $prefix); + $basename = tempnam(TMPPATH, $prefix); if (!$basename) { return false; } - $filename = $basename . $postfix; + $filename = "$basename.$extension"; $success = !file_exists($filename) && @rename($basename, $filename); if (!$success) { @unlink($basename); @@ -68,4 +61,4 @@ class system_Core { } while (!$success); return $filename; } -} \ No newline at end of file +} diff --git a/modules/gallery/tests/Mock_Built_In.php b/modules/gallery/tests/Mock_Built_In.php deleted file mode 100644 index b02e5ecf..00000000 --- a/modules/gallery/tests/Mock_Built_In.php +++ /dev/null @@ -1,39 +0,0 @@ -nonces = func_get_args(); - } - - function _tempnam($dir, $prefix) { - if (empty($this->nonces)) - return false; - $filename = "$dir/$prefix" . array_shift($this->nonces); - if (!touch($filename)) - return false; - return $filename; - } -} diff --git a/modules/gallery/tests/System_Helper_Test.php b/modules/gallery/tests/System_Helper_Test.php index dfe5d9ab..3d56c516 100644 --- a/modules/gallery/tests/System_Helper_Test.php +++ b/modules/gallery/tests/System_Helper_Test.php @@ -18,33 +18,10 @@ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ class System_Helper_Test extends Gallery_Unit_Test_Case { - public function temp_filename_random_test() { + public function temp_filename_test() { $filename = system::temp_filename("file", "ext"); $this->assert_true(file_exists($filename), "File not created"); unlink($filename); $this->assert_pattern($filename, "|/file.*\\.ext$|"); } - - public function tempnam_collision_test() { - require_once('Mock_Built_In.php'); - $existing = TMPPATH . "/file1.ext"; - $available = TMPPATH . "/file2.ext"; - touch($existing); - $filename = system::_tempnam(TMPPATH, "file", ".ext", - array(new Mock_Built_In("1", "2"), "_tempnam")); - unlink($existing); - $this->assert_true(file_exists($filename), "File not created"); - unlink($filename); - $this->assert_equal($available, $filename, "Incorrect filename created"); - } - - public function tempnam_abort_test() { - require_once('Mock_Built_In.php'); - $filename = system::_tempnam(TMPPATH, "file", ".ext", - array(new Mock_Built_In(), "_tempnam")); - if ($filename) { - @unlink($filename); - } - $this->assert_false($filename, "Operation not aborted"); - } } -- cgit v1.2.3 From 46da011bf69bbc4e45757feda8f0d28e91e7fb6a Mon Sep 17 00:00:00 2001 From: Chad Parry Date: Wed, 4 May 2011 17:51:00 -0600 Subject: Remove a newline I accidentally introduced. --- modules/gallery/helpers/system.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'modules/gallery/helpers/system.php') diff --git a/modules/gallery/helpers/system.php b/modules/gallery/helpers/system.php index e7e58a70..4110b4ed 100644 --- a/modules/gallery/helpers/system.php +++ b/modules/gallery/helpers/system.php @@ -61,4 +61,4 @@ class system_Core { } while (!$success); return $filename; } -} +} \ No newline at end of file -- cgit v1.2.3