HTTP_Download
[ class tree: HTTP_Download ] [ index: HTTP_Download ] [ all elements ]

Source for file Download.php

Documentation is available at Download.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3.  
  4. /**
  5.  * HTTP::Download
  6.  * 
  7.  * PHP versions 4 and 5
  8.  *
  9.  * @category   HTTP
  10.  * @package    HTTP_Download
  11.  * @author     Michael Wallner <mike@php.net>
  12.  * @copyright  2003-2005 Michael Wallner
  13.  * @license    BSD, revised
  14.  * @version    CVS: $Id: Download.php,v 1.79 2007/10/05 07:02:03 mike Exp $
  15.  * @link       http://pear.php.net/package/HTTP_Download
  16.  */
  17.  
  18. // {{{ includes
  19. /**
  20.  * Requires PEAR
  21.  */
  22. require_once 'PEAR.php';
  23.  
  24. /**
  25.  * Requires HTTP_Header
  26.  */
  27. require_once 'HTTP/Header.php';
  28. // }}}
  29.  
  30. // {{{ constants
  31. /**#@+ Use with HTTP_Download::setContentDisposition() **/
  32. /**
  33.  * Send data as attachment
  34.  */
  35. define('HTTP_DOWNLOAD_ATTACHMENT''attachment');
  36. /**
  37.  * Send data inline
  38.  */
  39. define('HTTP_DOWNLOAD_INLINE''inline');
  40. /**#@-**/
  41.  
  42. /**#@+ Use with HTTP_Download::sendArchive() **/
  43. /**
  44.  * Send as uncompressed tar archive
  45.  */
  46. define('HTTP_DOWNLOAD_TAR''TAR');
  47. /**
  48.  * Send as gzipped tar archive
  49.  */
  50. define('HTTP_DOWNLOAD_TGZ''TGZ');
  51. /**
  52.  * Send as bzip2 compressed tar archive
  53.  */
  54. define('HTTP_DOWNLOAD_BZ2''BZ2');
  55. /**
  56.  * Send as zip archive
  57.  */
  58. define('HTTP_DOWNLOAD_ZIP''ZIP');
  59. /**#@-**/
  60.  
  61. /**#@+
  62.  * Error constants
  63.  */
  64. define('HTTP_DOWNLOAD_E_HEADERS_SENT',          -1);
  65. define('HTTP_DOWNLOAD_E_NO_EXT_ZLIB',           -2);
  66. define('HTTP_DOWNLOAD_E_NO_EXT_MMAGIC',         -3);
  67. define('HTTP_DOWNLOAD_E_INVALID_FILE',          -4);
  68. define('HTTP_DOWNLOAD_E_INVALID_PARAM',         -5);
  69. define('HTTP_DOWNLOAD_E_INVALID_RESOURCE',      -6);
  70. define('HTTP_DOWNLOAD_E_INVALID_REQUEST',       -7);
  71. define('HTTP_DOWNLOAD_E_INVALID_CONTENT_TYPE',  -8);
  72. define('HTTP_DOWNLOAD_E_INVALID_ARCHIVE_TYPE',  -9);
  73. /**#@-**/
  74. // }}}
  75.  
  76. /** 
  77.  * Send HTTP Downloads/Responses.
  78.  *
  79.  * With this package you can handle (hidden) downloads.
  80.  * It supports partial downloads, resuming and sending
  81.  * raw data ie. from database BLOBs.
  82.  * 
  83.  * <i>ATTENTION:</i>
  84.  * You shouldn't use this package together with ob_gzhandler or
  85.  * zlib.output_compression enabled in your php.ini, especially
  86.  * if you want to send already gzipped data!
  87.  * 
  88.  * @access   public
  89.  * @version  $Revision: 1.79 $
  90.  */
  91. class HTTP_Download
  92. {
  93.     // {{{ protected member variables
  94.     /**
  95.      * Path to file for download
  96.      *
  97.      * @see     HTTP_Download::setFile()
  98.      * @access  protected
  99.      * @var     string 
  100.      */
  101.     var $file '';
  102.     
  103.     /**
  104.      * Data for download
  105.      *
  106.      * @see     HTTP_Download::setData()
  107.      * @access  protected
  108.      * @var     string 
  109.      */
  110.     var $data = null;
  111.     
  112.     /**
  113.      * Resource handle for download
  114.      *
  115.      * @see     HTTP_Download::setResource()
  116.      * @access  protected
  117.      * @var     int 
  118.      */
  119.     var $handle = null;
  120.     
  121.     /**
  122.      * Whether to gzip the download
  123.      *
  124.      * @access  protected
  125.      * @var     bool 
  126.      */
  127.     var $gzip = false;
  128.     
  129.     /**
  130.      * Whether to allow caching of the download on the clients side
  131.      * 
  132.      * @access  protected
  133.      * @var     bool 
  134.      */
  135.     var $cache = true;
  136.     
  137.     /**
  138.      * Size of download
  139.      *
  140.      * @access  protected
  141.      * @var     int 
  142.      */
  143.     var $size = 0;
  144.     
  145.     /**
  146.      * Last modified
  147.      *
  148.      * @access  protected
  149.      * @var     int 
  150.      */
  151.     var $lastModified = 0;
  152.     
  153.     /**
  154.      * HTTP headers
  155.      *
  156.      * @access  protected
  157.      * @var     array 
  158.      */
  159.     var $headers   = array(
  160.         'Content-Type'  => 'application/x-octetstream',
  161.         'Pragma'        => 'cache',
  162.         'Cache-Control' => 'public, must-revalidate, max-age=0',
  163.         'Accept-Ranges' => 'bytes',
  164.         'X-Sent-By'     => 'PEAR::HTTP::Download'
  165.     );
  166.  
  167.     /**
  168.      * HTTP_Header
  169.      * 
  170.      * @access  protected
  171.      * @var     object 
  172.      */
  173.     var $HTTP = null;
  174.     
  175.     /**
  176.      * ETag
  177.      * 
  178.      * @access  protected
  179.      * @var     string 
  180.      */
  181.     var $etag '';
  182.     
  183.     /**
  184.      * Buffer Size
  185.      * 
  186.      * @access  protected
  187.      * @var     int 
  188.      */
  189.     var $bufferSize = 2097152;
  190.     
  191.     /**
  192.      * Throttle Delay
  193.      * 
  194.      * @access  protected
  195.      * @var     float 
  196.      */
  197.     var $throttleDelay = 0;
  198.     
  199.     /**
  200.      * Sent Bytes
  201.      * 
  202.      * @access  public
  203.      * @var     int 
  204.      */
  205.     var $sentBytes = 0;
  206.     // }}}
  207.     
  208.     // {{{ constructor
  209.     /**
  210.      * Constructor
  211.      *
  212.      * Set supplied parameters.
  213.      * 
  214.      * @access  public
  215.      * @param   array   $params     associative array of parameters
  216.      * 
  217.      *           <b>one of:</b>
  218.      *                   o 'file'                => path to file for download
  219.      *                   o 'data'                => raw data for download
  220.      *                   o 'resource'            => resource handle for download
  221.      *  <br/>
  222.      *           <b>and any of:</b>
  223.      *                   o 'cache'               => whether to allow cs caching
  224.      *                   o 'gzip'                => whether to gzip the download
  225.      *                   o 'lastmodified'        => unix timestamp
  226.      *                   o 'contenttype'         => content type of download
  227.      *                   o 'contentdisposition'  => content disposition
  228.      *                   o 'buffersize'          => amount of bytes to buffer
  229.      *                   o 'throttledelay'       => amount of secs to sleep
  230.      *                   o 'cachecontrol'        => cache privacy and validity
  231.      * 
  232.      *  <br />
  233.      *  'Content-Disposition' is not HTTP compliant, but most browsers
  234.      *  follow this header, so it was borrowed from MIME standard.
  235.      * 
  236.      *  It looks like this: <br />
  237.      *  "Content-Disposition: attachment; filename=example.tgz".
  238.      * 
  239.      * @see HTTP_Download::setContentDisposition()
  240.      */
  241.     function HTTP_Download($params = array())
  242.     {
  243.         $this->HTTP &new HTTP_Header;
  244.         $this->setParams($params);
  245.     }
  246.     // }}}
  247.     
  248.     // {{{ public methods
  249.     /**
  250.      * Set parameters
  251.      * 
  252.      * Set supplied parameters through its accessor methods.
  253.      *
  254.      * @access  public
  255.      * @return  mixed   Returns true on success or PEAR_Error on failure.
  256.      * @param   array   $params     associative array of parameters
  257.      * 
  258.      * @see     HTTP_Download::HTTP_Download()
  259.      */
  260.     function setParams($params)
  261.     {
  262.         foreach((array) $params as $param => $value){
  263.             $method 'set'$param;
  264.             
  265.             if (!method_exists($this$method)) {
  266.                 return PEAR::raiseError(
  267.                     "Method '$method' doesn't exist.",
  268.                     HTTP_DOWNLOAD_E_INVALID_PARAM
  269.                 );
  270.             }
  271.             
  272.             $e call_user_func_array(array(&$this$method)(array) $value);
  273.             
  274.             if (PEAR::isError($e)) {
  275.                 return $e;
  276.             }
  277.         }
  278.         return true;
  279.     }
  280.     
  281.     /**
  282.      * Set path to file for download
  283.      *
  284.      * The Last-Modified header will be set to files filemtime(), actually.
  285.      * Returns PEAR_Error (HTTP_DOWNLOAD_E_INVALID_FILE) if file doesn't exist.
  286.      * Sends HTTP 404 status if $send_404 is set to true.
  287.      * 
  288.      * @access  public
  289.      * @return  mixed   Returns true on success or PEAR_Error on failure.
  290.      * @param   string  $file       path to file for download
  291.      * @param   bool    $send_404   whether to send HTTP/404 if
  292.      *                               the file wasn't found
  293.      */
  294.     function setFile($file$send_404 = true)
  295.     {
  296.         $file realpath($file);
  297.         if (!is_file($file)) {
  298.             if ($send_404{
  299.                 $this->HTTP->sendStatusCode(404);
  300.             }
  301.             return PEAR::raiseError(
  302.                 "File '$file' not found.",
  303.                 HTTP_DOWNLOAD_E_INVALID_FILE
  304.             );
  305.         }
  306.         $this->setLastModified(filemtime($file));
  307.         $this->file $file;
  308.         $this->size filesize($file);
  309.         return true;
  310.     }
  311.     
  312.     /**
  313.      * Set data for download
  314.      *
  315.      * Set $data to null if you want to unset this.
  316.      * 
  317.      * @access  public
  318.      * @return  void 
  319.      * @param   $data   raw data to send
  320.      */
  321.     function setData($data = null)
  322.     {
  323.         $this->data $data;
  324.         $this->size strlen($data);
  325.     }
  326.     
  327.     /**
  328.      * Set resource for download
  329.      *
  330.      * The resource handle supplied will be closed after sending the download.
  331.      * Returns a PEAR_Error (HTTP_DOWNLOAD_E_INVALID_RESOURCE) if $handle
  332.      * is no valid resource. Set $handle to null if you want to unset this.
  333.      * 
  334.      * @access  public
  335.      * @return  mixed   Returns true on success or PEAR_Error on failure.
  336.      * @param   int     $handle     resource handle
  337.      */
  338.     function setResource($handle = null)
  339.     {
  340.         if (!isset($handle)) {
  341.             $this->handle = null;
  342.             $this->size = 0;
  343.             return true;
  344.         }
  345.         
  346.         if (is_resource($handle)) {
  347.             $this->handle $handle;
  348.             $filestats    fstat($handle);
  349.             $this->size   $filestats['size'];
  350.             return true;
  351.         }
  352.  
  353.         return PEAR::raiseError(
  354.             "Handle '$handle' is no valid resource.",
  355.             HTTP_DOWNLOAD_E_INVALID_RESOURCE
  356.         );
  357.     }
  358.     
  359.     /**
  360.      * Whether to gzip the download
  361.      *
  362.      * Returns a PEAR_Error (HTTP_DOWNLOAD_E_NO_EXT_ZLIB)
  363.      * if ext/zlib is not available/loadable.
  364.      * 
  365.      * @access  public
  366.      * @return  mixed   Returns true on success or PEAR_Error on failure.
  367.      * @param   bool    $gzip   whether to gzip the download
  368.      */
  369.     function setGzip($gzip = false)
  370.     {
  371.         if ($gzip && !PEAR::loadExtension('zlib')){
  372.             return PEAR::raiseError(
  373.                 'GZIP compression (ext/zlib) not available.',
  374.                 HTTP_DOWNLOAD_E_NO_EXT_ZLIB
  375.             );
  376.         }
  377.         $this->gzip = (bool) $gzip;
  378.         return true;
  379.     }
  380.  
  381.     /**
  382.      * Whether to allow caching
  383.      * 
  384.      * If set to true (default) we'll send some headers that are commonly
  385.      * used for caching purposes like ETag, Cache-Control and Last-Modified.
  386.      * 
  387.      * If caching is disabled, we'll send the download no matter if it
  388.      * would actually be cached at the client side.
  389.      *
  390.      * @access  public
  391.      * @return  void 
  392.      * @param   bool    $cache  whether to allow caching
  393.      */
  394.     function setCache($cache = true)
  395.     {
  396.         $this->cache = (bool) $cache;
  397.     }
  398.     
  399.     /**
  400.      * Whether to allow proxies to cache
  401.      * 
  402.      * If set to 'private' proxies shouldn't cache the response.
  403.      * This setting defaults to 'public' and affects only cached responses.
  404.      * 
  405.      * @access  public
  406.      * @return  bool 
  407.      * @param   string  $cache  private or public
  408.      * @param   int     $maxage maximum age of the client cache entry
  409.      */
  410.     function setCacheControl($cache 'public'$maxage = 0)
  411.     {
  412.         switch ($cache = strToLower($cache))
  413.         {
  414.             case 'private':
  415.             case 'public':
  416.                 $this->headers['Cache-Control'
  417.                     $cache .', must-revalidate, max-age='abs($maxage);
  418.                 return true;
  419.             break;
  420.         }
  421.         return false;
  422.     }
  423.     
  424.     /**
  425.      * Set ETag
  426.      * 
  427.      * Sets a user-defined ETag for cache-validation.  The ETag is usually
  428.      * generated by HTTP_Download through its payload information.
  429.      * 
  430.      * @access  public
  431.      * @return  void 
  432.      * @param   string  $etag Entity tag used for strong cache validation.
  433.      */
  434.     function setETag($etag = null)
  435.     {
  436.         $this->etag = (string) $etag;
  437.     }
  438.     
  439.     /**
  440.      * Set Size of Buffer
  441.      * 
  442.      * The amount of bytes specified as buffer size is the maximum amount
  443.      * of data read at once from resources or files.  The default size is 2M
  444.      * (2097152 bytes).  Be aware that if you enable gzip compression and
  445.      * you set a very low buffer size that the actual file size may grow
  446.      * due to added gzip headers for each sent chunk of the specified size.
  447.      * 
  448.      * Returns PEAR_Error (HTTP_DOWNLOAD_E_INVALID_PARAM) if $size is not
  449.      * greater than 0 bytes.
  450.      * 
  451.      * @access  public
  452.      * @return  mixed   Returns true on success or PEAR_Error on failure.
  453.      * @param   int     $bytes Amount of bytes to use as buffer.
  454.      */
  455.     function setBufferSize($bytes = 2097152)
  456.     {
  457.         if (0 >= $bytes{
  458.             return PEAR::raiseError(
  459.                 'Buffer size must be greater than 0 bytes ('$bytes .' given)',
  460.                 HTTP_DOWNLOAD_E_INVALID_PARAM);
  461.         }
  462.         $this->bufferSize abs($bytes);
  463.         return true;
  464.     }
  465.     
  466.     /**
  467.      * Set Throttle Delay
  468.      * 
  469.      * Set the amount of seconds to sleep after each chunck that has been
  470.      * sent.  One can implement some sort of throttle through adjusting the
  471.      * buffer size and the throttle delay.  With the following settings
  472.      * HTTP_Download will sleep a second after each 25 K of data sent.
  473.      * 
  474.      * <code>
  475.      *  Array(
  476.      *      'throttledelay' => 1,
  477.      *      'buffersize'    => 1024 * 25,
  478.      *  )
  479.      * </code>
  480.      * 
  481.      * Just be aware that if gzipp'ing is enabled, decreasing the chunk size
  482.      * too much leads to proportionally increased network traffic due to added
  483.      * gzip header and bottom bytes around each chunk.
  484.      * 
  485.      * @access  public
  486.      * @return  void 
  487.      * @param   float   $seconds    Amount of seconds to sleep after each
  488.      *                               chunk that has been sent.
  489.      */
  490.     function setThrottleDelay($seconds = 0)
  491.     {
  492.         $this->throttleDelay abs($seconds* 1000;
  493.     }
  494.     
  495.     /**
  496.      * Set "Last-Modified"
  497.      *
  498.      * This is usually determined by filemtime() in HTTP_Download::setFile()
  499.      * If you set raw data for download with HTTP_Download::setData() and you
  500.      * want do send an appropiate "Last-Modified" header, you should call this
  501.      * method.
  502.      * 
  503.      * @access  public
  504.      * @return  void 
  505.      * @param   int     unix timestamp
  506.      */
  507.     function setLastModified($last_modified)
  508.     {
  509.         $this->lastModified $this->headers['Last-Modified'= (int) $last_modified;
  510.     }
  511.     
  512.     /**
  513.      * Set Content-Disposition header
  514.      * 
  515.      * @see HTTP_Download::HTTP_Download
  516.      *
  517.      * @access  public
  518.      * @return  void 
  519.      * @param   string  $disposition    whether to send the download
  520.      *                                   inline or as attachment
  521.      * @param   string  $file_name      the filename to display in
  522.      *                                   the browser's download window
  523.      * 
  524.      *  <b>Example:</b>
  525.      *  <code>
  526.      *  $HTTP_Download->setContentDisposition(
  527.      *    HTTP_DOWNLOAD_ATTACHMENT,
  528.      *    'download.tgz'
  529.      *  );
  530.      *  </code>
  531.      */
  532.     function setContentDisposition$disposition    = HTTP_DOWNLOAD_ATTACHMENT
  533.                                     $file_name      = null)
  534.     {
  535.         $cd $disposition;
  536.         if (isset($file_name)) {
  537.             $cd .= '; filename="' $file_name '"';
  538.         elseif ($this->file{
  539.             $cd .= '; filename="' basename($this->file'"';
  540.         }
  541.         $this->headers['Content-Disposition'$cd;
  542.     }
  543.     
  544.     /**
  545.      * Set content type of the download
  546.      *
  547.      * Default content type of the download will be 'application/x-octetstream'.
  548.      * Returns PEAR_Error (HTTP_DOWNLOAD_E_INVALID_CONTENT_TYPE) if
  549.      * $content_type doesn't seem to be valid.
  550.      * 
  551.      * @access  public
  552.      * @return  mixed   Returns true on success or PEAR_Error on failure.
  553.      * @param   string  $content_type   content type of file for download
  554.      */
  555.     function setContentType($content_type 'application/x-octetstream')
  556.     {
  557.         if (!preg_match('/^[a-z]+\w*\/[a-z]+[\w.;= -]*$/'$content_type)) {
  558.             return PEAR::raiseError(
  559.                 "Invalid content type '$content_type' supplied.",
  560.                 HTTP_DOWNLOAD_E_INVALID_CONTENT_TYPE
  561.             );
  562.         }
  563.         $this->headers['Content-Type'$content_type;
  564.         return true;
  565.     }
  566.     
  567.     /**
  568.      * Guess content type of file
  569.      * 
  570.      * First we try to use PEAR::MIME_Type, if installed, to detect the content
  571.      * type, else we check if ext/mime_magic is loaded and properly configured.
  572.      *
  573.      * Returns PEAR_Error if:
  574.      *      o if PEAR::MIME_Type failed to detect a proper content type
  575.      *        (HTTP_DOWNLOAD_E_INVALID_CONTENT_TYPE)
  576.      *      o ext/magic.mime is not installed, or not properly configured
  577.      *        (HTTP_DOWNLOAD_E_NO_EXT_MMAGIC)
  578.      *      o mime_content_type() couldn't guess content type or returned
  579.      *        a content type considered to be bogus by setContentType()
  580.      *        (HTTP_DOWNLOAD_E_INVALID_CONTENT_TYPE)
  581.      * 
  582.      * @access  public
  583.      * @return  mixed   Returns true on success or PEAR_Error on failure.
  584.      */
  585.     function guessContentType()
  586.     {
  587.         if (class_exists('MIME_Type'|| @include_once 'MIME/Type.php'{
  588.             if (PEAR::isError($mime_type = MIME_Type::autoDetect($this->file))) {
  589.                 return PEAR::raiseError($mime_type->getMessage(),
  590.                     HTTP_DOWNLOAD_E_INVALID_CONTENT_TYPE);
  591.             }
  592.             return $this->setContentType($mime_type);
  593.         }
  594.         if (!function_exists('mime_content_type')) {
  595.             return PEAR::raiseError(
  596.                 'This feature requires ext/mime_magic!',
  597.                 HTTP_DOWNLOAD_E_NO_EXT_MMAGIC
  598.             );
  599.         }
  600.         if (!is_file(ini_get('mime_magic.magicfile'))) {
  601.             return PEAR::raiseError(
  602.                 'ext/mime_magic is loaded but not properly configured!',
  603.                 HTTP_DOWNLOAD_E_NO_EXT_MMAGIC
  604.             );
  605.         }
  606.         if (!$content_type @mime_content_type($this->file)) {
  607.             return PEAR::raiseError(
  608.                 'Couldn\'t guess content type with mime_content_type().',
  609.                 HTTP_DOWNLOAD_E_INVALID_CONTENT_TYPE
  610.             );
  611.         }
  612.         return $this->setContentType($content_type);
  613.     }
  614.  
  615.     /**
  616.      * Send
  617.      *
  618.      * Returns PEAR_Error if:
  619.      *   o HTTP headers were already sent (HTTP_DOWNLOAD_E_HEADERS_SENT)
  620.      *   o HTTP Range was invalid (HTTP_DOWNLOAD_E_INVALID_REQUEST)
  621.      * 
  622.      * @access  public
  623.      * @return  mixed   Returns true on success or PEAR_Error on failure.
  624.      * @param   bool    $autoSetContentDisposition Whether to set the
  625.      *                   Content-Disposition header if it isn't already.
  626.      */
  627.     function send($autoSetContentDisposition = true)
  628.     {
  629.         if (headers_sent()) {
  630.             return PEAR::raiseError(
  631.                 'Headers already sent.',
  632.                 HTTP_DOWNLOAD_E_HEADERS_SENT
  633.             );
  634.         }
  635.         
  636.         if (!ini_get('safe_mode')) {
  637.             @set_time_limit(0);
  638.         }
  639.         
  640.         if ($autoSetContentDisposition && 
  641.             !isset($this->headers['Content-Disposition'])) {
  642.             $this->setContentDisposition();
  643.         }
  644.         
  645.         if ($this->cache{
  646.             $this->headers['ETag'$this->generateETag();
  647.             if ($this->isCached()) {
  648.                 $this->HTTP->sendStatusCode(304);
  649.                 $this->sendHeaders();
  650.                 return true;
  651.             }
  652.         else {
  653.             unset($this->headers['Last-Modified']);
  654.         }
  655.         
  656.         if (ob_get_level()) {
  657.             while (@ob_end_clean());
  658.         }
  659.         
  660.         if ($this->gzip{
  661.             @ob_start('ob_gzhandler');
  662.         else {
  663.             ob_start();
  664.         }
  665.         
  666.         $this->sentBytes = 0;
  667.         
  668.         if ($this->isRangeRequest()) {
  669.             $this->HTTP->sendStatusCode(206);
  670.             $chunks $this->getChunks();
  671.         else {
  672.             $this->HTTP->sendStatusCode(200);
  673.             $chunks = array(array(0$this->size));
  674.             if (!$this->gzip && count(ob_list_handlers()) < 2{
  675.                 $this->headers['Content-Length'$this->size;
  676.             }
  677.         }
  678.  
  679.         if (PEAR::isError($e $this->sendChunks($chunks))) {
  680.             ob_end_clean();
  681.    &nbs