diff --git a/core/basepage.php b/core/basepage.php index 72c23554..13a26928 100644 --- a/core/basepage.php +++ b/core/basepage.php @@ -368,8 +368,7 @@ class BasePage header('Content-Disposition: ' . $this->disposition . '; filename=' . $this->filename); } - //https://gist.github.com/codler/3906826 - + // https://gist.github.com/codler/3906826 $size = filesize($this->file); // File size $length = $size; // Content length $start = 0; // Start byte @@ -379,7 +378,6 @@ class BasePage header('Accept-Ranges: bytes'); if (isset($_SERVER['HTTP_RANGE'])) { - $c_end = $end; list(, $range) = explode('=', $_SERVER['HTTP_RANGE'], 2); if (strpos($range, ',') !== false) { header('HTTP/1.1 416 Requested Range Not Satisfiable'); @@ -388,6 +386,7 @@ class BasePage } if ($range == '-') { $c_start = $size - (int)substr($range, 1); + $c_end = $end; } else { $range = explode('-', $range); $c_start = (int)$range[0]; @@ -407,29 +406,7 @@ class BasePage header("Content-Range: bytes $start-$end/$size"); header("Content-Length: " . $length); - - $fp = fopen($this->file, 'r'); - try { - fseek($fp, $start); - $buffer = 1024 * 64; - while (!feof($fp) && ($p = ftell($fp)) <= $end) { - if ($p + $buffer > $end) { - $buffer = $end - $p + 1; - } - set_time_limit(0); - echo fread($fp, $buffer); - flush(); - - // After flush, we can tell if the client browser has disconnected. - // This means we can start sending a large file, and if we detect they disappeared - // then we can just stop and not waste any more resources or bandwidth. - if (connection_status() != 0) { - break; - } - } - } finally { - fclose($fp); - } + stream_file($this->file, $start, $end); break; case PageMode::REDIRECT: if ($this->flash) { @@ -544,7 +521,8 @@ class BasePage EOD; } - protected function head_html(): string { + protected function head_html(): string + { $html_header_html = $this->get_all_html_headers(); return " @@ -555,7 +533,8 @@ EOD; "; } - protected function body_html(): string { + protected function body_html(): string + { $left_block_html = ""; $main_block_html = ""; $sub_block_html = ""; @@ -604,7 +583,8 @@ EOD; "; } - protected function footer_html(): string { + protected function footer_html(): string + { $debug = get_debug_info(); $contact_link = contact_link(); $contact = empty($contact_link) ? "" : "
Contact"; @@ -711,7 +691,6 @@ class NavLink return false; } - } function sort_nav_links(NavLink $a, NavLink $b) diff --git a/core/polyfills.php b/core/polyfills.php index b4177c02..3fab2e15 100644 --- a/core/polyfills.php +++ b/core/polyfills.php @@ -150,6 +150,32 @@ function list_files(string $base, string $_sub_dir=""): array return $file_list; } +function stream_file(string $file, int $start, int $end): void +{ + $fp = fopen($file, 'r'); + try { + set_time_limit(0); + fseek($fp, $start); + $buffer = 1024 * 64; + while (!feof($fp) && ($p = ftell($fp)) <= $end) { + if ($p + $buffer > $end) { + $buffer = $end - $p + 1; + } + echo fread($fp, $buffer); + flush(); + + // After flush, we can tell if the client browser has disconnected. + // This means we can start sending a large file, and if we detect they disappeared + // then we can just stop and not waste any more resources or bandwidth. + if (connection_status() != 0) { + break; + } + } + } finally { + fclose($fp); + } +} + if (!function_exists('http_parse_headers')) { #http://www.php.net/manual/en/function.http-parse-headers.php#112917 /** diff --git a/ext/handle_svg/main.php b/ext/handle_svg/main.php index 3569d2e8..2cb48578 100644 --- a/ext/handle_svg/main.php +++ b/ext/handle_svg/main.php @@ -47,8 +47,11 @@ class SVGFileHandler extends DataHandlerExtension { try { // Normally we require imagemagick, but for unit tests we can use a no-op engine - if(defined('UNITTEST')) create_image_thumb($hash, $type); - else create_image_thumb($hash, $type, MediaEngine::IMAGICK); + if (defined('UNITTEST')) { + create_image_thumb($hash, $type); + } else { + create_image_thumb($hash, $type, MediaEngine::IMAGICK); + } return true; } catch (MediaException $e) { log_warning("handle_svg", "Could not generate thumbnail. " . $e->getMessage()); diff --git a/themes/rule34v2/home.theme.php b/themes/rule34v2/home.theme.php index b44a96f0..9889991d 100644 --- a/themes/rule34v2/home.theme.php +++ b/themes/rule34v2/home.theme.php @@ -34,7 +34,7 @@ class CustomHomeTheme extends HomeTheme EOD -); + ); } public function build_body(string $sitename, string $main_links, string $main_text, string $contact_link, $num_comma, string $counter_text)