static function reply($data = array()) { Session::abort_save(); if ($data) { if (Input::instance()->get("output") == "html") { header("Content-type: text/html"); $html = preg_replace("#(^|[\n ])([\\w]+?://[\\w]+[^ \"\n\r\t<]*)#ise", "'\\1<a href=\"\\2\" >\\2</a>'", print_r($data, 1)); print "<pre>{$html}</pre>"; } else { header("Content-type: application/json"); print json_encode($data); } } }
public function print_proxy($type, $id) { // If its a request for the full size then make sure we are coming from an // authorized address if ($type == "full") { $remote_addr = ip2long($this->input->server("REMOTE_ADDR")); if ($remote_addr === false) { Kohana::show_404(); } $config = Kohana::config("addthis"); $authorized = false; foreach ($config["ranges"] as $ip_range) { $low = ip2long($ip_range["low"]); $high = ip2long($ip_range["high"]); $authorized = $low !== false && $high !== false && $low <= $remote_addr && $remote_addr <= $high; if ($authorized) { break; } } if (!$authorized) { Kohana::show_404(); } } $proxy = ORM::factory("addthis_proxy", array("uuid" => $id)); if (!$proxy->loaded || !$proxy->item->loaded) { Kohana::show_404(); } $file = $type == "full" ? $proxy->item->file_path() : $proxy->item->thumb_path(); if (!file_exists($file)) { kohana::show_404(); } // We don't need to save the session for this request Session::abort_save(); if (!TEST_MODE) { // Dump out the image header("Content-Type: {$proxy->item}->mime_type"); Kohana::close_buffers(false); $fd = fopen($file, "rb"); fpassthru($fd); fclose($fd); // If the request was for the image and not the thumb, then delete the proxy. if ($type == "full") { $proxy->delete(); } } $this->_clean_expired(); }
/** * Print out a cached entry. * @param string the combined entry type (either "javascript" or "css") * @param string the key (typically an md5 sum) */ private function _emit($type, $key) { $input = Input::instance(); // We don't need to save the session for this request Session::abort_save(); // Our data is immutable, so if they already have a copy then it needs no updating. if ($input->server("HTTP_IF_MODIFIED_SINCE")) { header('HTTP/1.0 304 Not Modified'); header("Expires: Tue, 19 Jan 2038 00:00:00 GMT"); header("Cache-Control: max-age=2678400"); header('Pragma: public'); Kohana::close_buffers(false); return ""; } if (empty($key)) { throw new Kohana_404_Exception(); } $cache = Cache::instance(); $use_gzip = function_exists("gzencode") && stripos($input->server("HTTP_ACCEPT_ENCODING"), "gzip") !== false && (int) ini_get("zlib.output_compression") === 0; if ($use_gzip && ($content = $cache->get("{$key}_gz"))) { header("Content-Encoding: gzip"); } else { // Fall back to non-gzipped if we have to $content = $cache->get($key); } if (empty($content)) { throw new Kohana_404_Exception(); } // $type is either 'javascript' or 'css' if ($type == "javascript") { header("Content-Type: application/javascript; charset=UTF-8"); } else { header("Content-Type: text/css; charset=UTF-8"); } header("Expires: Tue, 19 Jan 2038 00:00:00 GMT"); header("Cache-Control: max-age=2678400"); header('Pragma: public'); header("Last-Modified: " . gmdate("D, d M Y H:i:s T", time())); Kohana::close_buffers(false); print $content; }
public function print_proxy($type, $id) { $proxy = ORM::factory("digibug_proxy", array("uuid" => $id)); if (!$proxy->loaded || !$proxy->item->loaded) { Kohana::show_404(); } $file = $type == "full" ? $proxy->item->file_path() : $proxy->item->thumb_path(); if (!file_exists($file)) { kohana::show_404(); } // We don't need to save the session for this request Session::abort_save(); // Dump out the image header("Content-Type: {$proxy->item}->mime_type"); Kohana::close_buffers(false); $fd = fopen($file, "rb"); fpassthru($fd); fclose($fd); // If the request was for the image and not the thumb, then delete the proxy. if ($type == "full") { $proxy->delete(); } $this->_clean_expired(); }
public function __call($function, $args) { // request_uri: gallery3/var/trunk/albums/foo/bar.jpg $request_uri = rawurldecode(Input::instance()->server("REQUEST_URI")); $request_uri = preg_replace("/\\?.*/", "", $request_uri); // var_uri: gallery3/var/ $var_uri = url::file("var/"); // Make sure that the request is for a file inside var $offset = strpos(rawurldecode($request_uri), $var_uri); if ($offset !== 0) { throw new Kohana_404_Exception(); } $file_uri = substr($request_uri, strlen($var_uri)); // Make sure that we don't leave the var dir if (strpos($file_uri, "..") !== false) { throw new Kohana_404_Exception(); } list($type, $path) = explode("/", $file_uri, 2); if ($type != "resizes" && $type != "albums" && $type != "thumbs") { throw new Kohana_404_Exception(); } // If the last element is .album.jpg, pop that off since it's not a real item $path = preg_replace("|/.album.jpg\$|", "", $path); $encoded_path = array(); foreach (explode("/", $path) as $path_part) { $encoded_path[] = rawurlencode($path_part); } // We now have the relative path to the item. Search for it in the path cache // The patch cache is urlencoded so re-encode the path. (it was decoded earlier to // insure that the paths are normalized. $item = ORM::factory("item")->where("relative_path_cache", "=", implode("/", $encoded_path))->find(); if (!$item->loaded()) { // We didn't turn it up. It's possible that the relative_path_cache is out of date here. // There was fallback code, but bharat deleted it in 8f1bca74. If it turns out to be // necessary, it's easily resurrected. // 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 or .mp4 file would have been converted to a .jpg. // So try some alternate types: if (preg_match('/.jpg$/', $path)) { foreach (array("flv", "mp4") as $ext) { $movie_path = preg_replace('/.jpg$/', ".{$ext}", $path); $item = ORM::factory("item")->where("relative_path_cache", "=", $movie_path)->find(); if ($item->loaded()) { break; } } } } if (!$item->loaded()) { throw new Kohana_404_Exception(); } // Make sure we have access to the item if (!access::can("view", $item)) { throw new Kohana_404_Exception(); } // Make sure we have view_full access to the original if ($type == "albums" && !access::can("view_full", $item)) { throw new Kohana_404_Exception(); } // Don't try to load a directory if ($type == "albums" && $item->is_album()) { throw new Kohana_404_Exception(); } if ($type == "albums") { $file = $item->file_path(); } else { if ($type == "resizes") { $file = $item->resize_path(); } else { $file = $item->thumb_path(); } } if (!file_exists($file)) { throw new Kohana_404_Exception(); } header("Pragma:"); // Check that the content hasn't expired or it wasn't changed since cached expires::check(2592000, $item->updated); // We don't need to save the session for this request Session::abort_save(); expires::set(2592000, $item->updated); // 30 days // Dump out the image. If the item is a movie, then its thumbnail will be a JPG. if ($item->is_movie() && $type != "albums") { header("Content-type: image/jpeg"); } else { header("Content-Type: {$item->mime_type}"); } Kohana::close_buffers(false); $fd = fopen($file, "rb"); fpassthru($fd); fclose($fd); }
public function __call($function, $args) { // request_uri: http://example.com/gallery3/var/trunk/albums/foo/bar.jpg $request_uri = $this->input->server("REQUEST_URI"); $request_uri = preg_replace("/\\?.*/", "", $request_uri); // Unescape %7E (~), %20 ( ) and %27 (') // @todo: figure out why we have to do this and unescape everything appropriate $request_uri = str_replace(array("%7E", "%20", "%27"), array("~", " ", "'"), $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_uri = substr($request_uri, strlen($var_uri)); // Make sure that we don't leave the var dir if (strpos($file_uri, "..") !== false) { kohana::show_404(); } 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 $path = preg_replace("|/.album.jpg\$|", "", $path); // 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. It's possible that the relative_path_cache is out of date here. // There was fallback code, but bharat deleted it in 8f1bca74. If it turns out to be // necessary, it's easily resurrected. // 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 or .mp4 file would have been converted to a .jpg. // So try some alternate types: if (preg_match('/.jpg$/', $path)) { foreach (array("flv", "mp4") as $ext) { $movie_path = preg_replace('/.jpg$/', ".{$ext}", $path); $item = ORM::factory("item")->where("relative_path_cache", $movie_path)->find(); if ($item->loaded) { break; } } } } 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(); } // Make sure we have view_full access to the original if ($type == "albums" && !access::can("view_full", $item)) { kohana::show_404(); } // Don't try to load a directory if ($type == "albums" && $item->is_album()) { kohana::show_404(); } if (!file_exists($file)) { kohana::show_404(); } // We don't need to save the session for this request Session::abort_save(); // Dump out the image. If the item is a movie, then its thumbnail will be a JPG. if (in_array($item->mime_type, array("video/x-flv", "video/mp4"))) { header("Content-type: image/jpeg"); } else { header("Content-Type: {$item->mime_type}"); } Kohana::close_buffers(false); $fd = fopen($file, "rb"); fpassthru($fd); fclose($fd); }
public function __call($function, $args) { // request_uri: http://example.com/gallery3/var/trunk/albums/foo/bar.jpg $request_uri = $this->input->server("REQUEST_URI"); $request_uri = preg_replace("/\\?.*/", "", $request_uri); // Unescape %7E ("~") and %20 (" ") $request_uri = str_replace(array("%7E", "%20"), array("~", " "), $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_uri = substr($request_uri, strlen($var_uri)); // Make sure that we don't leave the var dir if (strpos($file_uri, "..") !== false) { kohana::show_404(); } 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 $path = preg_replace("|/.album.jpg\$|", "", $path); // 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->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(); } // Make sure we have view_full access to the original if ($type == "albums" && !access::can("view_full", $item)) { kohana::show_404(); } // Don't try to load a directory if ($type == "albums" && $item->is_album()) { kohana::show_404(); } if (!file_exists($file)) { kohana::show_404(); } // We don't need to save the session for this request Session::abort_save(); // Dump out the image header("Content-Type: {$item->mime_type}"); Kohana::close_buffers(false); $fd = fopen($file, "rb"); fpassthru($fd); fclose($fd); }