Source for file jResponseBinary.class.php

Documentation is available at jResponseBinary.class.php

  1. <?php
  2. /**
  3. @package     jelix
  4. @subpackage  core_response
  5. @author      Laurent Jouanneau
  6. @contributor Nicolas Lassalle <nicolas@beroot.org> (ticket #188), Julien Issler
  7. @copyright   2005-2007 Laurent Jouanneau
  8. @copyright   2007 Nicolas Lassalle
  9. @copyright   2009 Julien Issler
  10. @link        http://www.jelix.org
  11. @licence     GNU Lesser General Public Licence see LICENCE file or http://www.gnu.org/licenses/lgpl.html
  12. */
  13.  
  14.  
  15. /**
  16. * Response use to send a binary file to the browser
  17. @package  jelix
  18. @subpackage core_response
  19. */
  20.  
  21. final class jResponseBinary  extends jResponse {
  22.     /**
  23.     * @var string 
  24.     */
  25.     protected $_type = 'binary';
  26.  
  27.     /**
  28.      * The path of the file you want to send. Keep empty if you provide the content
  29.      * @var string 
  30.      */
  31.     public $fileName ='';
  32.     /**
  33.      * name of the file under which the content will be send to the user
  34.      * @var string 
  35.      */
  36.     public $outputFileName ='';
  37.  
  38.     /**
  39.      * the content you want to send. Keep empty if you indicate a filename
  40.      * @var string 
  41.      */
  42.     public $content = null;
  43.  
  44.     /**
  45.      * Says if the "save as" dialog appear or not to the user.
  46.      * if false, specify the mime type in $mimetype
  47.      * @var boolean 
  48.      */
  49.     public $doDownload = true;
  50.  
  51.     /**
  52.     * The mimeType of the current binary file.
  53.     * It will be sent in the header "Content-Type".
  54.     * @var string 
  55.     */
  56.     public $mimeType = 'application/octet-stream';
  57.  
  58.     /**
  59.      * send the content or the file to the browser.
  60.      * @return boolean    true it it's ok
  61.      */
  62.     public function output(){
  63.  
  64.         if($this->doDownload){
  65.             $this->mimeType = 'application/forcedownload';
  66.             if (!strlen($this->outputFileName)){
  67.                 $f explode ('/'str_replace ('\\''/'$this->fileName));
  68.                 $this->outputFileName = $f[count ($f)-1];
  69.             }
  70.         }
  71.  
  72.         if($this->hasErrors()) return false;
  73.  
  74.         $this->addHttpHeader("Content-Type",$this->mimeType$this->doDownload);
  75.  
  76.         if($this->doDownload)
  77.               $this->_downloadHeader();
  78.  
  79.         if($this->content === null){
  80.             if (is_readable ($this->fileName&& is_file ($this->fileName)){
  81.                 $this->_httpHeaders['Content-Length']=filesize ($this->fileName);
  82.                 $this->sendHttpHeaders();
  83.                 session_write_close();
  84.                 readfile ($this->fileName);
  85.                 flush();
  86.                 return true;
  87.             }else{
  88.                 throw new jException('jelix~errors.repbin.unknow.file' $this->fileName);
  89.                 return false;
  90.             }
  91.         }else{
  92.             $this->_httpHeaders['Content-Length']=strlen ($this->content);
  93.             $this->sendHttpHeaders();
  94.             session_write_close();
  95.             echo $this->content;
  96.             flush();
  97.             return true;
  98.         }
  99.     }
  100.  
  101.     /**
  102.      * set all headers to force download
  103.      */
  104.     protected function _downloadHeader(){
  105.         $this->addHttpHeader('Content-Disposition','attachment; filename="'.str_replace('"','\"',$this->outputFileName).'"'false);
  106.         $this->addHttpHeader('Content-Description','File Transfert'false);
  107.         $this->addHttpHeader('Content-Transfer-Encoding','binary'false);
  108.         $this->addHttpHeader('Pragma','public'false);
  109.         $this->addHttpHeader('Cache-Control','maxage=3600'false);
  110.         //$this->addHttpHeader('Cache-Control','no-store, no-cache, must-revalidate, post-check=0, pre-check=0', false);
  111.         //$this->addHttpHeader('Expires','0', false);
  112.     }
  113.  
  114.     /**
  115.      *
  116.      */
  117.     public function outputErrors(){
  118.         $this->clearHttpHeaders();
  119.         $this->_httpStatusCode ='500';
  120.         $this->_httpStatusMsg ='Internal Server Error';
  121.         $this->_httpHeaders["Content-Type"]='text/plain';
  122.         $this->sendHttpHeaders();
  123.         if($this->hasErrors()){
  124.             foreach$GLOBALS['gJCoord']->errorMessages  as $e){
  125.                echo '['.$e[0].' '.$e[1].'] '.$e[2]." \t".$e[3]." \t".$e[4]."\n";
  126.             }
  127.         }else{
  128.             echo "[unknown error]\n";
  129.         }
  130.     }
  131. }

Documentation generated on Thu, 22 Mar 2012 22:16:36 +0100 by phpDocumentor 1.4.3