2008-12-16 23:07:33 +00:00
|
|
|
<?php defined("SYSPATH") or die("No direct script access.");
|
|
|
|
|
/**
|
|
|
|
|
* Gallery - a web based photo album viewer and editor
|
2012-02-27 09:46:39 -08:00
|
|
|
* Copyright (C) 2000-2012 Bharat Mediratta
|
2008-12-16 23:07:33 +00:00
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or (at
|
|
|
|
|
* your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
* General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
|
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
*/
|
|
|
|
|
/**
|
|
|
|
|
* Proxy access to files in var/albums and var/resizes, making sure that the session user has
|
|
|
|
|
* access to view these files.
|
|
|
|
|
*
|
|
|
|
|
* Security Philosophy: we do not use the information provided to find if the file exists on
|
|
|
|
|
* disk. We use this information only to locate the correct item in the database and then we
|
|
|
|
|
* *only* use information from the database to find and proxy the correct file. This way all user
|
|
|
|
|
* input is sanitized against the database before we perform any file I/O.
|
|
|
|
|
*/
|
|
|
|
|
class File_Proxy_Controller extends Controller {
|
2011-01-10 14:50:30 -08:00
|
|
|
const ALLOW_PRIVATE_GALLERY = true;
|
2009-03-06 02:36:04 +00:00
|
|
|
public function __call($function, $args) {
|
2011-01-11 17:54:33 -08:00
|
|
|
// request_uri: gallery3/var/albums/foo/bar.jpg?m=1234
|
2010-01-07 10:55:43 -08:00
|
|
|
$request_uri = rawurldecode(Input::instance()->server("REQUEST_URI"));
|
|
|
|
|
|
2011-01-11 17:54:33 -08:00
|
|
|
// get rid of query parameters
|
|
|
|
|
// request_uri: gallery3/var/albums/foo/bar.jpg
|
2009-05-21 05:54:59 +00:00
|
|
|
$request_uri = preg_replace("/\?.*/", "", $request_uri);
|
2008-12-16 23:07:33 +00:00
|
|
|
|
2010-01-07 10:55:43 -08:00
|
|
|
// var_uri: gallery3/var/
|
2008-12-16 23:07:33 +00:00
|
|
|
$var_uri = url::file("var/");
|
|
|
|
|
|
|
|
|
|
// Make sure that the request is for a file inside var
|
2010-01-07 10:55:43 -08:00
|
|
|
$offset = strpos(rawurldecode($request_uri), $var_uri);
|
|
|
|
|
if ($offset !== 0) {
|
2009-12-23 20:51:33 -08:00
|
|
|
throw new Kohana_404_Exception();
|
2008-12-16 23:07:33 +00:00
|
|
|
}
|
|
|
|
|
|
2011-01-11 17:54:33 -08:00
|
|
|
// file_uri: albums/foo/bar.jpg
|
2009-05-29 21:23:08 -07:00
|
|
|
$file_uri = substr($request_uri, strlen($var_uri));
|
2008-12-16 23:07:33 +00:00
|
|
|
|
2011-01-11 17:54:33 -08:00
|
|
|
// type: albums
|
|
|
|
|
// path: foo/bar.jpg
|
2009-05-29 21:23:08 -07:00
|
|
|
list ($type, $path) = explode("/", $file_uri, 2);
|
2008-12-17 04:45:35 +00:00
|
|
|
if ($type != "resizes" && $type != "albums" && $type != "thumbs") {
|
2009-12-23 20:51:33 -08:00
|
|
|
throw new Kohana_404_Exception();
|
2008-12-16 23:07:33 +00:00
|
|
|
}
|
|
|
|
|
|
2008-12-17 05:53:05 +00:00
|
|
|
// If the last element is .album.jpg, pop that off since it's not a real item
|
2009-05-29 21:23:08 -07:00
|
|
|
$path = preg_replace("|/.album.jpg$|", "", $path);
|
2009-07-21 12:26:16 -07:00
|
|
|
|
2010-12-21 19:36:23 -08:00
|
|
|
$item = item::find_by_path($path);
|
|
|
|
|
if (!$item->loaded()) {
|
|
|
|
|
// We didn't turn it up. If we're looking for a .jpg then it's it's possible that we're
|
|
|
|
|
// requesting the thumbnail for a movie. In that case, the .flv, .mp4 or .m4v file would
|
|
|
|
|
// have been converted to a .jpg. So try some alternate types:
|
2009-07-21 12:26:16 -07:00
|
|
|
if (preg_match('/.jpg$/', $path)) {
|
2010-07-04 21:53:57 +02:00
|
|
|
foreach (array("flv", "mp4", "m4v") as $ext) {
|
2010-12-21 19:36:23 -08:00
|
|
|
$movie_path = preg_replace('/.jpg$/', ".$ext", $path);
|
|
|
|
|
$item = item::find_by_path($movie_path);
|
2009-11-25 13:22:24 -08:00
|
|
|
if ($item->loaded()) {
|
2009-07-21 12:26:16 -07:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2008-12-16 23:07:33 +00:00
|
|
|
}
|
|
|
|
|
|
2009-11-25 13:22:24 -08:00
|
|
|
if (!$item->loaded()) {
|
2009-12-23 20:51:33 -08:00
|
|
|
throw new Kohana_404_Exception();
|
2008-12-17 05:53:05 +00:00
|
|
|
}
|
|
|
|
|
|
2008-12-16 23:07:33 +00:00
|
|
|
// Make sure we have access to the item
|
|
|
|
|
if (!access::can("view", $item)) {
|
2009-12-23 20:51:33 -08:00
|
|
|
throw new Kohana_404_Exception();
|
2008-12-16 23:07:33 +00:00
|
|
|
}
|
|
|
|
|
|
2008-12-31 00:18:24 +00:00
|
|
|
// Make sure we have view_full access to the original
|
|
|
|
|
if ($type == "albums" && !access::can("view_full", $item)) {
|
2009-12-23 20:51:33 -08:00
|
|
|
throw new Kohana_404_Exception();
|
2008-12-31 00:18:24 +00:00
|
|
|
}
|
|
|
|
|
|
2008-12-17 05:53:05 +00:00
|
|
|
// Don't try to load a directory
|
|
|
|
|
if ($type == "albums" && $item->is_album()) {
|
2009-12-23 20:51:33 -08:00
|
|
|
throw new Kohana_404_Exception();
|
2008-12-17 04:45:35 +00:00
|
|
|
}
|
|
|
|
|
|
2010-01-07 10:55:43 -08:00
|
|
|
if ($type == "albums") {
|
|
|
|
|
$file = $item->file_path();
|
|
|
|
|
} else if ($type == "resizes") {
|
|
|
|
|
$file = $item->resize_path();
|
|
|
|
|
} else {
|
|
|
|
|
$file = $item->thumb_path();
|
|
|
|
|
}
|
|
|
|
|
|
2009-05-29 21:23:08 -07:00
|
|
|
if (!file_exists($file)) {
|
2009-12-23 20:51:33 -08:00
|
|
|
throw new Kohana_404_Exception();
|
2008-12-16 23:07:33 +00:00
|
|
|
}
|
|
|
|
|
|
2010-08-11 21:02:57 -07:00
|
|
|
header("Content-Length: " . filesize($file));
|
|
|
|
|
|
2010-01-04 09:10:12 -08:00
|
|
|
header("Pragma:");
|
|
|
|
|
// Check that the content hasn't expired or it wasn't changed since cached
|
|
|
|
|
expires::check(2592000, $item->updated);
|
|
|
|
|
|
2009-06-30 20:51:02 -07:00
|
|
|
// We don't need to save the session for this request
|
2010-01-31 16:07:41 -08:00
|
|
|
Session::instance()->abort_save();
|
2009-06-30 20:51:02 -07:00
|
|
|
|
2010-01-04 09:10:12 -08:00
|
|
|
expires::set(2592000, $item->updated); // 30 days
|
2009-12-30 09:55:28 -08:00
|
|
|
|
2009-07-21 12:26:16 -07:00
|
|
|
// Dump out the image. If the item is a movie, then its thumbnail will be a JPG.
|
2009-12-01 13:37:07 -08:00
|
|
|
if ($item->is_movie() && $type != "albums") {
|
2010-08-09 22:51:14 -07:00
|
|
|
header("Content-Type: image/jpeg");
|
2009-07-21 12:26:16 -07:00
|
|
|
} else {
|
2009-08-20 12:24:05 +08:00
|
|
|
header("Content-Type: $item->mime_type");
|
2009-07-21 12:26:16 -07:00
|
|
|
}
|
2012-05-08 18:23:09 -07:00
|
|
|
|
|
|
|
|
// Don't use Kohana::close_buffers(false) here because that only closes all the buffers
|
|
|
|
|
// that Kohana started. We want to close *all* buffers at this point because otherwise we're
|
|
|
|
|
// going to buffer up whatever file we're proxying (and it may be very large). This may
|
|
|
|
|
// affect embedding or systems with PHP's output_buffering enabled.
|
|
|
|
|
while (ob_get_level()) {
|
|
|
|
|
ob_end_clean();
|
|
|
|
|
}
|
|
|
|
|
|
Use readfile() instead of fopen()/fpassthru()/fclose() for brevity.
I've done some tests on a 60M flv and found that there's no difference
in memory consumption with these three approaches:
public function test() {
Kohana::close_buffers(false);
$file = "/home/bharat/basketball.flv";
if ($fd = fopen($file, "rb")) {
while (true) {
$bits = fread($fd, 65535);
if (strlen($bits) == 0) {
break;
}
print $bits;
set_time_limit(30);
}
fclose($fd);
}
Kohana_Log::add("error","test: " . print_r(array(memory_get_peak_usage(true),memory_get_peak_usage(false)),1));
}
public function test2() {
Kohana::close_buffers(false);
$file = "/home/bharat/basketball.flv";
$fd = fopen($file, "rb");
fpassthru($fd);
fclose($fd);
Kohana_Log::add("error","test2: " . print_r(array(memory_get_peak_usage(true),memory_get_peak_usage(false)),1));
}
public function test3() {
Kohana::close_buffers(false);
$file = "/home/bharat/basketball.flv";
readfile($file);
Kohana_Log::add("error","test3: " . print_r(array(memory_get_peak_usage(true),memory_get_peak_usage(false)),1));
}
2010-07-31 11:51:18 -07:00
|
|
|
readfile($file);
|
2008-12-16 23:07:33 +00:00
|
|
|
}
|
|
|
|
|
}
|