Subversion Repositories php_utils

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

  1. <?php
  2.  
  3. /*
  4.  * VtsBrowserDownload.class.php
  5.  * Copyright 2021 Daniel Marschall, ViaThinkSoft
  6.  * Revision: 2021-05-21
  7.  *
  8.  * Licensed under the Apache License, Version 2.0 (the "License");
  9.  * you may not use this file except in compliance with the License.
  10.  * You may obtain a copy of the License at
  11.  *
  12.  *     http://www.apache.org/licenses/LICENSE-2.0
  13.  *
  14.  * Unless required by applicable law or agreed to in writing, software
  15.  * distributed under the License is distributed on an "AS IS" BASIS,
  16.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17.  * See the License for the specific language governing permissions and
  18.  * limitations under the License.
  19.  */
  20.  
  21. class VtsBrowserDownload {
  22.  
  23.         private static function wellKnownInlineFile($file_extension) {
  24.                 // Windows Firefox: Browser decided wheater to display or download by looking at the mime type (inline disposition with explicit filename works)
  25.                 // Windows Chrome:  Browser decided wheater to display or download by looking at the mime type (inline disposition with explicit filename DOES NOT WORK)
  26.  
  27.                 //$file_extension = strtolower($file_extension);
  28.                 //$array_listen = array('txt', 'mp3', 'wav', 'mid', 'ogg', 'pdf', 'avi', 'mov', 'mp4', 'mpeg', 'mpg', 'swf', 'gif', 'jpg', 'jpeg', 'png');
  29.                 //return in_array($file_extension, $array_listen);
  30.  
  31.                 return true;
  32.         }
  33.  
  34.         private static function getMimeType($file_extension) {
  35.                 $file_extension = strtolower($file_extension);
  36.                 return VtsFileTypeDetect::getMimeType('dummy.'.$file_extension);
  37.         }
  38.  
  39.         public static function output_file($file, $mime_type='', $inline_mode=2/*2=auto*/) {
  40.                 // Partitally taken from:
  41.                 // - https://stackoverflow.com/a/13821992/488539
  42.                 // - https://stackoverflow.com/a/32885706/488539
  43.  
  44.                 if (connection_status() != 0) return false;
  45.  
  46.                 $file_extension = pathinfo($file, PATHINFO_EXTENSION);
  47.  
  48.                 if(!is_readable($file)) throw new Exception('File not found or inaccessible!');
  49.                 $size = filesize($file);
  50.                 $name = rawurldecode(basename($file));
  51.  
  52.                 if ($mime_type == '') {
  53.                         $mime_type = self::getMimeType($file_extension);
  54.                         if (!$mime_type) $mime_type='application/force-download';
  55.                 }
  56.  
  57.  
  58.  
  59.                 while (ob_get_level() > 0) @ob_end_clean();
  60.  
  61.                 switch ($inline_mode) {
  62.  
  63.                         case 0:
  64.                                 $disposition = 'attachment';
  65.                                 break;
  66.  
  67.                         case 1:
  68.                                 $disposition = 'inline';
  69.                                 break;
  70.  
  71.                         case 2:
  72.                                 $disposition = self::wellKnownInlineFile($file_extension) ? 'inline' : 'attachment';
  73.                                 break;
  74.  
  75.                         default:
  76.                                 throw new Exception('Invalid value for inline_mode');
  77.                 }
  78.  
  79.                 if(ini_get('zlib.output_compression')){
  80.                         ini_set('zlib.output_compression', 'Off');
  81.                 }
  82.                 header('Content-Type: ' . $mime_type);
  83.  
  84.                 $ua = isset($_SERVER['HTTP_USER_AGENT']) ? strtoupper($_SERVER['HTTP_USER_AGENT']) : '';
  85.                 if (strstr($ua, 'MSIE')) {
  86.                         $name_msie = preg_replace('/\./', '%2e', $name, substr_count($name, '.') - 1);
  87.                         header('Content-Disposition: '.$disposition.';filename="'.$name_msie.'"');
  88.                 } else if (strstr($ua, 'FIREFOX')) {
  89.                         // TODO: Implement "encodeRFC5987ValueChars" described at https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent ?
  90.                         header('Content-Disposition: '.$disposition.';filename*="UTF-8\'\''.utf8_encode($name).'"');
  91.                 } else {
  92.                         // Note: There is possibly a bug in Google Chrome: https://stackoverflow.com/questions/61866508/chrome-ignores-content-disposition-filename
  93.                         header('Content-Disposition: '.$disposition.';filename="'.$name.'"');
  94.                 }
  95.  
  96.                 header('Content-Transfer-Encoding: binary');
  97.                 header('Accept-Ranges: bytes');
  98.                 header('Cache-Control: public');
  99.  
  100.                 if (isset($_SERVER['HTTP_RANGE'])) {
  101.                         list($a, $range) = explode("=",$_SERVER['HTTP_RANGE'],2);
  102.                         list($range) = explode(",",$range,2);
  103.                         list($range, $range_end) = explode("-", $range);
  104.                         $range=intval($range);
  105.                         if(!$range_end) {
  106.                                 $range_end=$size-1;
  107.                         } else {
  108.                                 $range_end=intval($range_end);
  109.                         }
  110.  
  111.                         $new_length = $range_end-$range+1;
  112.                         header("HTTP/1.1 206 Partial Content");
  113.                         header("Content-Length: $new_length");
  114.                         header("Content-Range: bytes $range-$range_end/$size");
  115.                 } else {
  116.                         $range = 0;
  117.                         $etag = md5_file($file);
  118.                         header("Etag: $etag");
  119.                         if (isset($_SERVER['HTTP_IF_NONE_MATCH']) && (trim($_SERVER['HTTP_IF_NONE_MATCH']) == $etag)) {
  120.                                 header("HTTP/1.1 304 Not Modified");
  121.                                 return true;
  122.                         }
  123.  
  124.                         $new_length=$size;
  125.                         header("Content-Length: ".$size);
  126.                         header('Content-MD5: '.$etag); // RFC 2616 clause 14.15
  127.                 }
  128.  
  129.                 set_time_limit(0);
  130.  
  131.                 $chunksize = 1*(1024*1024);
  132.                 $bytes_send = 0;
  133.                 if ($file = fopen($file, 'r')) {
  134.                         if(isset($_SERVER['HTTP_RANGE']))
  135.                         fseek($file, $range);
  136.  
  137.                         while(!feof($file) &&
  138.                               (!connection_aborted()) &&  // connection_status() == 0
  139.                               ($bytes_send<$new_length))
  140.                         {
  141.                                 $buffer = fread($file, $chunksize);
  142.                                 echo($buffer);
  143.                                 flush();
  144.                                 $bytes_send += strlen($buffer);
  145.                         }
  146.                         fclose($file);
  147.                 } else {
  148.                         throw new Exception("Cannot open file $file");
  149.                 }
  150.                 return((connection_status() == 0) and !connection_aborted());
  151.         }
  152.  
  153. }
  154.