[ Index ]

PHP Cross Reference of phpwcms V1.4.7 _r403 (01.11.10)

title

Body

[close]

/include/inc_lib/ -> functions.file.inc.php (source)

   1  <?php
   2  /*************************************************************************************
   3     Copyright notice
   4     
   5     (c) 2002-2010 Oliver Georgi (oliver@phpwcms.de) // All rights reserved.
   6   
   7     This script is part of PHPWCMS. The PHPWCMS web content management system is
   8     free software; you can redistribute it and/or modify it under the terms of
   9     the GNU General Public License as published by the Free Software Foundation;
  10     either version 2 of the License, or (at your option) any later version.
  11    
  12     The GNU General Public License can be found at http://www.gnu.org/copyleft/gpl.html
  13     A copy is found in the textfile GPL.txt and important notices to the license 
  14     from the author is found in LICENSE.txt distributed with these scripts.
  15    
  16     This script is distributed in the hope that it will be useful, but WITHOUT ANY 
  17     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  18     PARTICULAR PURPOSE.  See the GNU General Public License for more details.
  19   
  20     This copyright notice MUST APPEAR in all copies of the script!
  21  *************************************************************************************/
  22  
  23  
  24  function _getFileInfo($value, $limit='1', $mode='hash') {
  25  
  26      $sql = '';
  27  
  28      switch($mode) {
  29  
  30          case 'hash':    $sql  = "SELECT * FROM ".DB_PREPEND."phpwcms_file WHERE f_aktiv=1 AND ";
  31                          $sql .= "f_trash=0 AND f_public=1 AND ";
  32                          $sql .= "f_hash='".aporeplace($value)."'";
  33                          if( !FEUSER_LOGIN_STATUS ) {
  34                              $sql .= ' AND f_granted=0';
  35                          }
  36                          if($limit) {
  37                              $sql .= " LIMIT ".$limit;
  38                          }
  39                          break;
  40      }
  41  
  42      return _dbQuery($sql);
  43      
  44  }
  45  
  46  function dl_file_resume($file='', $fileinfo=array(), $onsuccess = false) {
  47  
  48      // function based on this one here:
  49      // http://www.php.net/manual/de/function.fread.php#63893
  50  
  51      //First, see if the file exists
  52      if (!is_file($file) && connection_status()) return false;
  53      
  54      //Gather relevent info about file
  55      $filename         = empty($fileinfo['realfname'])    ? basename($file) : $fileinfo['realfname'];
  56      $disposition    = empty($fileinfo['method'])    ? 'attachment' : $fileinfo['method'];
  57      
  58      if($disposition == 'attachment') {
  59      
  60          $fileinfo['mimetype'] = "application/force-download";
  61      
  62      }
  63      
  64      if(empty($fileinfo['mimetype']) && empty($fileinfo['extension'])) {
  65      
  66          $file_extension    = strtolower(substr(strrchr($filename,"."),1));
  67          $ctype            = isset($GLOBALS['phpwcms']['mime_types'][$file_extension]) ? $GLOBALS['phpwcms']['mime_types'][$file_extension] : 'application/force-download';
  68      
  69      } else {
  70      
  71          $ctype             = $fileinfo['mimetype'];
  72          $file_extension    = $fileinfo['extension'];
  73      
  74      }
  75  
  76      //Begin writing headers
  77      header('Cache-Control: ');
  78      header('Cache-Control: public');
  79      header('Pragma: ');
  80      
  81      //Use the switch-generated Content-Type
  82      header('Content-Type: '.$ctype);
  83      
  84      
  85      if (isset($_SERVER['HTTP_USER_AGENT']) && strstr($_SERVER['HTTP_USER_AGENT'], 'MSIE')) {
  86          // workaround for IE filename bug with multiple periods / multiple dots in filename
  87          // that adds square brackets to filename - eg. setup.abc.exe becomes setup[1].abc.exe
  88          $filename = preg_replace('/\./', '%2e', $filename, substr_count($filename, '.') - 1);
  89      }
  90      
  91      
  92      //header('Accept-Ranges: bytes');
  93      
  94      $size = filesize($file);
  95  
  96      
  97      header('Content-Length: '.$size);
  98      header('Content-Transfer-Encoding: binary'.LF);
  99      header('Content-Disposition: '.$disposition.'; filename="'.$filename.'"');
 100      
 101  
 102      //reset time limit for big files
 103      @set_time_limit(0);
 104  
 105      //open the file
 106      $fp = fopen($file, 'rb');
 107  
 108      //seek to start of missing part
 109      //fseek($fp, $range);
 110  
 111      //start buffered download
 112      while(!feof($fp) && !connection_status()){
 113      
 114          print(fread($fp, 1024*8));
 115          flush();
 116          //ob_flush();
 117      }
 118      fclose($fp);
 119  
 120      return ($onsuccess && !connection_status() && !connection_aborted()) ? true : false;
 121  }
 122  
 123  // http://www.thomthom.net/blog/2007/09/php-resumable-download-server/
 124  function rangeDownload($file) {
 125   
 126      $fp = @fopen($file, 'rb');
 127   
 128      $size   = filesize($file); // File size
 129      $length = $size;           // Content length
 130      $start  = 0;               // Start byte
 131      $end    = $size - 1;       // End byte
 132      // Now that we've gotten so far without errors we send the accept range header
 133      /* At the moment we only support single ranges.
 134       * Multiple ranges requires some more work to ensure it works correctly
 135       * and comply with the spesifications: http://www.w3.org/Protocols/rfc2616/rfc2616-sec19.html#sec19.2
 136       *
 137       * Multirange support annouces itself with:
 138       * header('Accept-Ranges: bytes');
 139       *
 140       * Multirange content must be sent with multipart/byteranges mediatype,
 141       * (mediatype = mimetype)
 142       * as well as a boundry header to indicate the various chunks of data.
 143       */
 144      header("Accept-Ranges: 0-$length");
 145      // header('Accept-Ranges: bytes');
 146      // multipart/byteranges
 147      // http://www.w3.org/Protocols/rfc2616/rfc2616-sec19.html#sec19.2
 148      if (isset($_SERVER['HTTP_RANGE'])) {
 149   
 150          $c_start = $start;
 151          $c_end   = $end;
 152          // Extract the range string
 153          list(, $range) = explode('=', $_SERVER['HTTP_RANGE'], 2);
 154          // Make sure the client hasn't sent us a multibyte range
 155          if (strpos($range, ',') !== false) {
 156   
 157              // (?) Shoud this be issued here, or should the first
 158              // range be used? Or should the header be ignored and
 159              // we output the whole content?
 160              header('HTTP/1.1 416 Requested Range Not Satisfiable');
 161              header("Content-Range: bytes $start-$end/$size");
 162              // (?) Echo some info to the client?
 163              exit;
 164          }
 165          // If the range starts with an '-' we start from the beginning
 166          // If not, we forward the file pointer
 167          // And make sure to get the end byte if spesified
 168          if ($range0 == '-') {
 169   
 170              // The n-number of the last bytes is requested
 171              $c_start = $size - substr($range, 1);
 172          }
 173          else {
 174   
 175              $range  = explode('-', $range);
 176              $c_start = $range[0];
 177              $c_end   = (isset($range[1]) && is_numeric($range[1])) ? $range[1] : $size;
 178          }
 179          /* Check the range and make sure it's treated according to the specs.
 180           * http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
 181           */
 182          // End bytes can not be larger than $end.
 183          $c_end = ($c_end > $end) ? $end : $c_end;
 184          // Validate the requested range and return an error if it's not correct.
 185          if ($c_start > $c_end || $c_start > $size - 1 || $c_end >= $size) {
 186   
 187              header('HTTP/1.1 416 Requested Range Not Satisfiable');
 188              header("Content-Range: bytes $start-$end/$size");
 189              // (?) Echo some info to the client?
 190              exit;
 191          }
 192          $start  = $c_start;
 193          $end    = $c_end;
 194          $length = $end - $start + 1; // Calculate new content length
 195          fseek($fp, $start);
 196          header('HTTP/1.1 206 Partial Content');
 197      }
 198      // Notify the client the byte range we'll be outputting
 199      header("Content-Range: bytes $start-$end/$size");
 200      header("Content-Length: $length");
 201   
 202      // Start buffered download
 203      $buffer = 1024 * 8;
 204      while(!feof($fp) && ($p = ftell($fp)) <= $end) {
 205   
 206          if ($p + $buffer > $end) {
 207   
 208              // In case we're only outputtin a chunk, make sure we don't
 209              // read past the length
 210              $buffer = $end - $p + 1;
 211          }
 212          set_time_limit(0); // Reset time limit for big files
 213          echo fread($fp, $buffer);
 214          flush(); // Free up memory. Otherwise large files will trigger PHP's memory limit.
 215      }
 216   
 217      fclose($fp);
 218   
 219  }
 220  
 221  
 222  
 223  ?>


Generated: Tue Nov 16 22:51:00 2010 Cross-referenced by PHPXref 0.7