summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--modules/gallery/controllers/file_proxy.php69
-rw-r--r--modules/gallery/helpers/MY_url.php20
2 files changed, 47 insertions, 42 deletions
diff --git a/modules/gallery/controllers/file_proxy.php b/modules/gallery/controllers/file_proxy.php
index f3c5f109..2037ad98 100644
--- a/modules/gallery/controllers/file_proxy.php
+++ b/modules/gallery/controllers/file_proxy.php
@@ -41,56 +41,55 @@ class File_Proxy_Controller extends Controller {
kohana::show_404();
}
- $file = substr($request_uri, strlen($var_uri));
+ $file_uri = substr($request_uri, strlen($var_uri));
// Make sure that we don't leave the var dir
- if (strpos($file, "..") !== false) {
+ if (strpos($file_uri, "..") !== false) {
kohana::show_404();
}
- // We only handle var/resizes and var/albums
- $paths = explode("/", $file);
- $type = $paths[0];
+ list ($type, $path) = explode("/", $file_uri, 2);
if ($type != "resizes" && $type != "albums" && $type != "thumbs") {
kohana::show_404();
}
// If the last element is .album.jpg, pop that off since it's not a real item
- if ($paths[count($paths)-1] == ".album.jpg") {
- array_pop($paths);
- }
- if ($paths[count($paths)-1] == "") {
- array_pop($paths);
- }
+ $path = preg_replace("|/.album.jpg$|", "", $path);
- // Find all items that match the level and name, then iterate over those to find a match.
- // In most cases we'll get it in one. Note that for the level calculation, we just count the
- // size of $paths. $paths includes the type ("thumbs", etc) but it doesn't include the root,
- // so it's a wash.
- $count = count($paths);
- $compare_file = VARPATH . $file;
- $item = null;
- foreach (ORM::factory("item")
- ->where("name", $paths[$count - 1])
- ->where("level", $count)
- ->find_all() as $match) {
- if ($type == "albums") {
- $match_file = $match->file_path();
- } else if ($type == "resizes") {
- $match_file = $match->resize_path();
- } else {
- $match_file = $match->thumb_path();
- }
- if ($match_file == $compare_file) {
- $item = $match;
- break;
+ // We now have the relative path to the item. Search for it in the path cache
+ $item = ORM::factory("item")->where("relative_path_cache", $path)->find();
+ if (!$item->loaded) {
+ // We didn't turn it up. This may mean that the path cache is out of date, so look it up
+ // the hard way.
+ //
+ // Find all items that match the level and name, then iterate over those to find a match.
+ // In most cases we'll get it in one. Note that for the level calculation, we just count the
+ // size of $paths.
+ $paths = explode("/", $path);
+ $count = count($paths);
+ foreach (ORM::factory("item")
+ ->where("name", $paths[$count - 1])
+ ->where("level", $count + 1)
+ ->find_all() as $match) {
+ if ($match->relative_path() == $path) {
+ $item = $match;
+ break;
+ }
}
}
- if (!$item) {
+ if (!$item->loaded) {
kohana::show_404();
}
+ if ($type == "albums") {
+ $file = $item->file_path();
+ } else if ($type == "resizes") {
+ $file = $item->resize_path();
+ } else {
+ $file = $item->thumb_path();
+ }
+
// Make sure we have access to the item
if (!access::can("view", $item)) {
kohana::show_404();
@@ -106,14 +105,14 @@ class File_Proxy_Controller extends Controller {
kohana::show_404();
}
- if (!file_exists($match_file)) {
+ if (!file_exists($file)) {
kohana::show_404();
}
// Dump out the image
header("Content-Type: $item->mime_type");
Kohana::close_buffers(false);
- $fd = fopen($match_file, "rb");
+ $fd = fopen($file, "rb");
fpassthru($fd);
fclose($fd);
}
diff --git a/modules/gallery/helpers/MY_url.php b/modules/gallery/helpers/MY_url.php
index 5e8bfc9e..019e416f 100644
--- a/modules/gallery/helpers/MY_url.php
+++ b/modules/gallery/helpers/MY_url.php
@@ -38,13 +38,19 @@ class url extends url_Core {
return;
}
- $count = count(Router::$segments);
- foreach (ORM::factory("item")
- ->where("name", html_entity_decode(Router::$segments[$count - 1], ENT_QUOTES))
- ->where("level", $count + 1)
- ->find_all() as $match) {
- if ($match->relative_path() == html_entity_decode(Router::$current_uri, ENT_QUOTES)) {
- $item = $match;
+ $current_uri = html_entity_decode(Router::$current_uri, ENT_QUOTES);
+ $item = ORM::factory("item")->where("relative_path_cache", $current_uri)->find();
+ if (!$item->loaded) {
+ // It's possible that the relative path cache for the item we're looking for is out of date,
+ // so find it the hard way.
+ $count = count(Router::$segments);
+ foreach (ORM::factory("item")
+ ->where("name", html_entity_decode(Router::$segments[$count - 1], ENT_QUOTES))
+ ->where("level", $count + 1)
+ ->find_all() as $match) {
+ if ($match->relative_path() == $current_uri) {
+ $item = $match;
+ }
}
}