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/controllers/quick.php | 4 +-- modules/gallery/helpers/system.php | 25 ++++++++++++++ modules/gallery/tests/Mock_Built_In.php | 39 ++++++++++++++++++++++ modules/gallery/tests/System_Helper_Test.php | 49 ++++++++++++++++++++++++++++ 4 files changed, 115 insertions(+), 2 deletions(-) create mode 100644 modules/gallery/tests/Mock_Built_In.php create mode 100644 modules/gallery/tests/System_Helper_Test.php (limited to 'modules') diff --git a/modules/gallery/controllers/quick.php b/modules/gallery/controllers/quick.php index da4768fd..ce52cb8d 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 = tempnam(TMPPATH, "rotate") . "." . - pathinfo($item->file_path(), PATHINFO_EXTENSION); + $tmpfile = system::tempnam(TMPPATH, "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 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 diff --git a/modules/gallery/tests/Mock_Built_In.php b/modules/gallery/tests/Mock_Built_In.php new file mode 100644 index 00000000..b02e5ecf --- /dev/null +++ b/modules/gallery/tests/Mock_Built_In.php @@ -0,0 +1,39 @@ +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 new file mode 100644 index 00000000..734f98ac --- /dev/null +++ b/modules/gallery/tests/System_Helper_Test.php @@ -0,0 +1,49 @@ +assert_true(file_exists($filename), "File not created"); + unlink($filename); + } + + 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 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') 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 68370b92f5f6fa68744655f8c68b4b0ca59bf4fd Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Mon, 2 May 2011 21:36:17 -0700 Subject: Map the G2 album highlight thumbnail derivative id to the G3 album's thumbnail. Fixes #1729. --- modules/g2_import/helpers/g2_import.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'modules') diff --git a/modules/g2_import/helpers/g2_import.php b/modules/g2_import/helpers/g2_import.php index 3606c7ef..c79a8d36 100644 --- a/modules/g2_import/helpers/g2_import.php +++ b/modules/g2_import/helpers/g2_import.php @@ -560,7 +560,7 @@ class g2_import_Core { $table = g2(GalleryCoreApi::fetchThumbnailsByItemIds(array($g2_album_id))); if (isset($table[$g2_album_id])) { // Backtrack the source id to an item - $g2_source = $table[$g2_album_id]; + $orig_g2_source = $g2_source = $table[$g2_album_id]; while (GalleryUtilities::isA($g2_source, "GalleryDerivative")) { $g2_source = g2(GalleryCoreApi::loadEntitiesById($g2_source->getDerivativeSourceId())); } @@ -584,6 +584,11 @@ class g2_import_Core { array("name" => $g3_album->name)), $e); } + + self::set_map( + $orig_g2_source->getId(), $g3_album->id, + "thumbnail", + self::g2_url(array("view" => "core.DownloadItem", "itemId" => $orig_g2_source->getId()))); } } } -- cgit v1.2.3 From 229bfc5c7c760c53d1357503fd61bf9a165acf6e Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Mon, 2 May 2011 21:37:04 -0700 Subject: Track and redirect core.DownloadItem requests properly. This can happen if the G2 was imported with rewrite on, so the g2_url in the g2_map table has a shortened url, but then rewrite is disabled and the .htaccess mod_rewrite rules are sending over a &g2_view=core.DownloadItem request. Fixes #1728. --- modules/g2_import/controllers/g2.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'modules') diff --git a/modules/g2_import/controllers/g2.php b/modules/g2_import/controllers/g2.php index 6e60bed0..90984e84 100644 --- a/modules/g2_import/controllers/g2.php +++ b/modules/g2_import/controllers/g2.php @@ -41,7 +41,9 @@ class G2_Controller extends Controller { // (bbcode, embedding) people are using the id style URLs although URL rewriting is enabled. $where = array(array("g2_id", "=", $id)); $view = $input->get("g2_view"); - if ($view) { + if ($view == "core.DownloadItem") { + $where[] = array("resource_type", "IN", array("file", "resize", "thumbnail", "full")); + } else if ($view) { $where[] = array("g2_url", "like", "%g2_view=$view%"); } // else: Assuming that the first search hit is sufficiently good. } else if ($path) { -- 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') 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') 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 From 5bae21864f54a03b557ab349cf97ba5f1d4276dc Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Thu, 5 May 2011 14:52:47 -0700 Subject: Follow-on to 6f916e49d5b431c2c1961a13d1a61fef8c02d628 -- don't make database calls if Gallery isn't installed, else we fail to bounce the user to the installer on fresh packages. #1637. --- modules/gallery/config/locale.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'modules') diff --git a/modules/gallery/config/locale.php b/modules/gallery/config/locale.php index 13de9098..bce7fb49 100644 --- a/modules/gallery/config/locale.php +++ b/modules/gallery/config/locale.php @@ -32,7 +32,12 @@ $config['language'] = array('en_US', 'English_United States'); * Locale timezone. Set in 'Advanced' settings, falling back to the server's zone. * @see http://php.net/timezones */ -$config['timezone'] = module::get_var("gallery", "timezone", date_default_timezone_get()); +if (file_exists(VARPATH . "database.php")) { + $config['timezone'] = module::get_var("gallery", "timezone", date_default_timezone_get()); +} else { + // Gallery3 is not installed yet -- don't make module::get_var() calls. + $config['timezone'] = date_default_timezone_get(); +} // i18n settings -- cgit v1.2.3 From 57f7e42a128848d73ad2a7ac0bf9df2fee6ba8b8 Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Sun, 8 May 2011 11:42:40 -0700 Subject: Add the item id to the print_proxy line so that we have a little more info about what the original was, and extend the timeout to 90 days from 10. Fixes #1733. --- modules/digibug/controllers/digibug.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'modules') diff --git a/modules/digibug/controllers/digibug.php b/modules/digibug/controllers/digibug.php index 4acb6513..672afe57 100644 --- a/modules/digibug/controllers/digibug.php +++ b/modules/digibug/controllers/digibug.php @@ -33,8 +33,8 @@ class Digibug_Controller extends Controller { $proxy->uuid = random::hash(); $proxy->item_id = $item->id; $proxy->save(); - $full_url = url::abs_site("digibug/print_proxy/full/$proxy->uuid"); - $thumb_url = url::abs_site("digibug/print_proxy/thumb/$proxy->uuid"); + $full_url = url::abs_site("digibug/print_proxy/full/$proxy->uuid/$item->id"); + $thumb_url = url::abs_site("digibug/print_proxy/thumb/$proxy->uuid/$item->id"); } $v = new View("digibug_form.html"); @@ -114,7 +114,7 @@ class Digibug_Controller extends Controller { private function _clean_expired() { db::build() ->delete("digibug_proxies") - ->where("request_date", "<=", db::expr("(CURDATE() - INTERVAL 10 DAY)")) + ->where("request_date", "<=", db::expr("(CURDATE() - INTERVAL 90 DAY)")) ->limit(20) ->execute(); } -- cgit v1.2.3