input->server("REQUEST_URI"); // var_uri: http://example.com/gallery3/var/ $var_uri = url::file("var/"); // Make sure that the request is for a file inside var $offset = strpos($request_uri, $var_uri); if ($offset === false) { kohana::show_404(); } $file = substr($request_uri, strlen($var_uri)); // Make sure that we don't leave the var dir if (strpos($file, "..") !== false) { kohana::show_404(); } // We only handle var/resizes and var/albums $paths = explode("/", $file); $type = array_shift($paths); if ($type != "resizes" && $type != "albums" && $type != "thumbs") { kohana::show_404(); } // Walk down from the root until we find the item that matches this path $item = ORM::factory("item", 1); while ($path = array_shift($paths)) { $item = ORM::factory("item") ->where("name", $path) ->where("level", $item->level + 1) ->where("parent_id", $item->id) ->find(); if (!$item->loaded) { kohana::show_404(); } // If the last element is _album.jpg then we're done. if (count($paths) == 1 && $paths[0] == "_album.jpg") { break; } } // Make sure we have access to the item if (!access::can("view", $item)) { kohana::show_404(); } if ($type == "albums") { if ($item->is_album()) { kohana::show_404(); } $path = $item->file_path(); } else if ($type == "resizes") { $path = $item->resize_path(); } else { $path = $item->thumb_path(); } if (!file_exists($path)) { kohana::show_404(); } // Dump out the image header("Content-Type: $item->mime_type"); Kohana::close_buffers(false); $fd = fopen($path, "rb"); fpassthru($fd); fclose($fd); } }