Source for file OCSP_FILE.phpclass

Documentation is available at OCSP_FILE.phpclass

  1. <?php
  2. /**
  3.   * openCSP class file OCSP_FILE.phpclass
  4.   *
  5.   * @project Open CSP-Management
  6.   * @package common
  7.   *
  8.   * @author Peter Krebs <pitlinz@users.sourceforge.net>
  9.   * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  10.   *
  11.   * @since pk-27.08.2008
  12.   * @version $Id: OCSP_FILE.phpclass,v 1.3 2008/11/11 07:04:42 pitlinz Exp $
  13.   */
  14.  
  15.     // ---------------------------------------------------------
  16.     // requirements
  17.     // ---------------------------------------------------------
  18.  
  19.     pcf_require_class('OCSP_DIRECTORY',"common/");
  20.     
  21. /**
  22.   * openCSP class OCSP_FILE
  23.   *
  24.   * @project Open CSP-Management
  25.   * @package default
  26.   *
  27.   * @author Peter Krebs <pitlinz@users.sourceforge.net>
  28.   * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  29.   *
  30.   * @since pk-27.08.2008
  31.   * @version $Id: OCSP_FILE.phpclass,v 1.3 2008/11/11 07:04:42 pitlinz Exp $
  32.   */
  33. class OCSP_FILE
  34. {
  35.     // ---------------------------------------------------------------------------
  36.     // constants
  37.     // ---------------------------------------------------------------------------
  38.     
  39.     /**
  40.      * @constant string CLASS_SRC_FILE
  41.      */
  42.     const CLASS_SRC_FILE = __FILE__;
  43.     
  44.     // ---------------------------------------------------------------------------
  45.     // class (static)
  46.     // ---------------------------------------------------------------------------
  47.     
  48.     /*** class vars ------------------------------------------------------ */
  49.     
  50.     /*** class methods --------------------------------------------------- */
  51.  
  52.     // checks
  53.     
  54.     /**
  55.      * checks if $aFile is a file
  56.      * 
  57.      * follows symlinks when checking
  58.      *
  59.      * @return boolean 
  60.      */
  61.     public static function isFile($aFile)
  62.     {
  63.         if (empty($aFile))
  64.         {
  65.             return False;
  66.         }
  67.         
  68.         if (is_file($aFile))
  69.         {
  70.             return True;
  71.         else if (is_link($aFile&& ($aFile=readlink($aFile))) {
  72.             return self::isFile($aFile);
  73.         else {
  74.             return False;
  75.         }        
  76.     }
  77.     
  78.     /**
  79.       * returns a filename where some special char are converted
  80.       * unknown chars are replaced with _
  81.       *
  82.       * @param string $fileName 
  83.       * @param string $replChar 
  84.       *
  85.       * @return string 
  86.       *
  87.       * @since pk-08-11-05 $replChar added
  88.       ***/
  89.     public static function checkFileName($fileName,$replChar="_"
  90.     {
  91.         $fileName str_replace("Ä","Ae",$fileName);
  92.         $fileName str_replace("ä","ae",$fileName);
  93.         $fileName str_replace("Ö","Oe",$fileName);
  94.         $fileName str_replace("ö","oe",$fileName);
  95.         $fileName str_replace("Ü","Ue",$fileName);
  96.         $fileName str_replace("ü","ue",$fileName);
  97.         $fileName str_replace("ß","ss",$fileName);
  98.         $fileName str_replace("€","EURO",$fileName);
  99.     
  100.         return preg_replace('/[^a-z0-9_\-\.]/i'$replChar$fileName);
  101.     }
  102.     
  103.     /**
  104.      * returns if a filename is an absolute path
  105.      *
  106.      * @param string $aFile 
  107.      * @return boolean 
  108.      */
  109.     public static function isAbsoultePath($aFile)
  110.     {
  111.         return (substr(trim($aFile),0,1== "/")
  112.     }
  113.     
  114.     /**
  115.      * checks slashes in $aFile
  116.      *
  117.      * @param string $aFile 
  118.      * 
  119.      * @return string 
  120.      */
  121.     public static function checkFilePath($aFile)
  122.     {
  123.         $str_ret (self::isAbsoultePath($aFile_OCSP_DIRSEP_ "");
  124.         $arr_path explode(_OCSP_DIRSEP_,$aFile);
  125.         foreach($arr_path as $str_part)
  126.         {
  127.             if (!empty($str_part))
  128.             {
  129.                 $str_ret .= $str_part _OCSP_DIRSEP_;
  130.             }
  131.         }
  132.         return substr($str_ret,0,-1);
  133.     }
  134.     
  135.     // ---------------------------------------------------------------------------
  136.     // factory
  137.     // ---------------------------------------------------------------------------    
  138.     
  139.     /**
  140.      * factories a file object from an file upload
  141.      *
  142.      * @param array $upload ($_FILES['UPLOAD INPUT NAME'])
  143.      * @param string $destDir (destination directory)
  144.      * @param boolean $overwrite (replace existing file?)
  145.      * @param boolean $debug 
  146.      * 
  147.      * @return OCSP_FILE; 
  148.      */
  149.     public static function factoryFromUpload($upload,$destDir,$overwrite=True,$debug=False)
  150.     {
  151.         if ($debugechoDebugMethod(__FILE__,"static","OCSP_FILE::factoryFromUpload()",print_r($upload,True));
  152.         
  153.         if (!OCSP_DIRECTORY::isDir($destDir))
  154.         {
  155.             if ($debugechoDebugLine(__FILE__,__LINE__,"Error destionation does not exits: " $destDir);
  156.             return Null;
  157.         }
  158.         if (substr($destDir,-1!= _OCSP_DIRSEP_)
  159.         {
  160.             $destDir .= _OCSP_DIRSEP_;
  161.         }
  162.         
  163.         if ($upload['error'== UPLOAD_ERR_OK
  164.         {
  165.             $str_name self::checkFileName($upload['name']);
  166.             if (!$overwrite && self::isFile($destDir $str_name))
  167.             {
  168.                 if ($debugechoDebugLine(__FILE__,__LINE__,"Error File Exists: " $destDir $str_name);
  169.                 return Null;
  170.             }            
  171.             if ($debugechoDebugLine(__FILE__,__LINE__,"Dest: " $destDir $str_name);
  172.             move_uploaded_file($upload['tmp_name']$destDir $str_name);
  173.             if (self::isFile($destDir $str_name))
  174.             {
  175.                 return new OCSP_FILE($destDir $str_name);
  176.             else if ($debug{
  177.                 echoDebugLine(__FILE__,__LINE__,"File not found: ".$destDir $str_name)
  178.             }
  179.         else {
  180.             if ($debugechoDebugLine(__FILE__,__LINE__,"Error: " $upload['error']);
  181.         }
  182.     }
  183.     
  184.     
  185.     // ---------------------------------------------------------------------------
  186.     // object vars
  187.     // ---------------------------------------------------------------------------
  188.     
  189.     /*** compostion --------------------------------------------------- */
  190.     
  191.     /*** attributes  -------------------------------------------------- */
  192.  
  193.     /**
  194.      * the file (absolut path in the system)
  195.      *
  196.      * @var string $myFilePath 
  197.      */
  198.     protected $myFilePath = "";
  199.     
  200.     /**
  201.      * the file name (relative path / value set)
  202.      * 
  203.      * @var string $myFileName 
  204.      */
  205.     protected $myFileName = "";
  206.     
  207.     /**
  208.      * does the file exists?
  209.      * 
  210.      * @var boolean $fileExists 
  211.      */
  212.     protected $fileExists = Null;
  213.     
  214.     /**
  215.      * image informations from getimagesize
  216.      *
  217.      * @var array $imagesize 
  218.      */
  219.     protected $imagesize = Null;
  220.     
  221.     // ---------------------------------------------------------------------------
  222.     // factory / construct
  223.     // ---------------------------------------------------------------------------
  224.     
  225.     /**
  226.      * constructor
  227.      *
  228.      * @param string $aFile 
  229.      */
  230.     public function __construct($aFile="")
  231.     {
  232.         if (!empty($aFile))
  233.         {
  234.             $this->setFile($aFile);
  235.         }
  236.     }
  237.     
  238.     // ---------------------------------------------------------------------------
  239.     // getter / setter
  240.     // ---------------------------------------------------------------------------    
  241.  
  242.     /**
  243.      * sets myFilePath
  244.      *
  245.      * @param string $aFile 
  246.      * 
  247.      * @return boolean (if the file exists)
  248.      */
  249.     public function setFile($aFile)
  250.     {
  251.         $this->myFileName = $aFile;
  252.         $this->myFilePath = self::checkFilePath($aFile);
  253.         
  254.         while (!($this->fileExists  = self::isFile($this->myFilePath)))
  255.         {            
  256.             // ensure the loop is terminated with a break or return
  257.             
  258.             $this->myFilePath = self::checkFilePath(__OCSP_PROJECTPATH__ $aFile);
  259.             if ($this->fileExists  = self::isFile($this->myFilePath))
  260.             {
  261.                 break;
  262.             }
  263.  
  264.             $this->myFilePath = self::checkFilePath($_SERVER['DOCUMENT_ROOT'$aFile);
  265.             if ($this->fileExists = self::isFile($this->myFilePath))
  266.             {
  267.                 break;
  268.             }
  269.             
  270.             $this->myFilePath = self::checkFilePath(OCSP_CONF::getInstance()->getValue('TEMPLATEPATH'$aFile);
  271.             if ($this->fileExists = self::isFile($this->myFilePath))
  272.             {
  273.                 break;
  274.             }
  275.             
  276.             $this->myFilePath = "";
  277.             return $this->fileExists;
  278.         }
  279.         
  280.         
  281.         if ($this->fileExists)
  282.         {
  283.             $this->imagesize = @getimagesize($this->myFilePath);
  284.         }        
  285.         
  286.         return $this->fileExists;
  287.     }
  288.     
  289.     /**
  290.      * returns the file
  291.      *
  292.      * @return string 
  293.      */
  294.     public function getFile()
  295.     {
  296.         return $this->myFileName;    
  297.     }
  298.     
  299.     /**
  300.      * returns absoulute file path
  301.      *
  302.      * @return string 
  303.      */
  304.     public function getFilePath()
  305.     {
  306.         return $this->myFilePath;    
  307.     }    
  308.     
  309.     /**
  310.      * returns the file size in bytes
  311.      *
  312.      * @return int 
  313.      */
  314.     public function getSize()
  315.     {
  316.         return filesize($this->myFilePath);
  317.     }
  318.     
  319.     /**
  320.      * sets the file form an upload
  321.      *
  322.      * @param array $upload 
  323.      * @param string $destDir 
  324.      * 
  325.      * @return boolean 
  326.      */
  327.     public function setFromUplaod($upload,$destDir,$overwrite=False,$debug=False)
  328.     {
  329.         if ($debugechoDebugMethod(__FILE__,get_class($this),"OCSP_FILE::getFromUplaod()",print_r($upload,True));
  330.         
  331.         if (!OCSP_DIRECTORY::isDir($destDir))
  332.         {
  333.             if ($debugechoDebugLine(__FILE__,__LINE__,"Error destionation does not exits: " $destDir);
  334.             return Null;
  335.         }
  336.         if (substr($destDir,-1!= _OCSP_DIRSEP_)
  337.         {
  338.             $destDir .= _OCSP_DIRSEP_;
  339.         }
  340.         
  341.         if ($upload['error'== UPLOAD_ERR_OK
  342.         {
  343.             $str_name self::checkFileName($upload['name']);
  344.             if (!$overwrite && self::isFile($destDir $str_name))
  345.             {
  346.                 if ($debugechoDebugLine(__FILE__,__LINE__,"Error File Exists: " $destDir $str_name);
  347.                 return false;
  348.             }            
  349.             if ($debugechoDebugLine(__FILE__,__LINE__,"Dest: " $destDir $str_name);
  350.             move_uploaded_file($upload['tmp_name']$destDir $str_name);
  351.             return $this->setFile($destDir $str_name);
  352.         else {
  353.             if ($debugechoDebugLine(__FILE__,__LINE__,"Error: " $upload['error']);
  354.         }    
  355.         return False;    
  356.     }
  357.  
  358.     // ------------------------------------------------------------------------
  359.     // mime types
  360.     // ------------------------------------------------------------------------
  361.     
  362.     /**
  363.      * returns the mimetype of
  364.      *
  365.      * @todo get mime type from T_MED_MIMETYPES
  366.      * 
  367.      * @return string 
  368.      */
  369.     public function getMimeType()
  370.     {
  371.         if ($arr_name explode('.',basename($this->myFilePath)))
  372.         {
  373.             switch(strtolower($arr_name[sizeof($arr_name)-1]))
  374.             {
  375.                 // scripts ----------------------
  376.                  
  377.                 case "js":
  378.                     return "text/javascript";
  379.                 case "json":
  380.                     return "application/jsonrequest";
  381.                     
  382.                 // text --------------------------
  383.                 
  384.                 case "css":
  385.                     return "text/css";
  386.                 case "html":
  387.                     return "text/html";
  388.                 case "txt":
  389.                     return "text/text";
  390.                     
  391.                 // images
  392.                     
  393.                 case "jpg":
  394.                 case "jpeg":
  395.                     return "image/jpeg";
  396.                 case "gif":
  397.                     return "image/gif";
  398.                 case "png":
  399.                     return "image/png";
  400.                     
  401.                 default:
  402.                     return "application/octet-stream";
  403.             }
  404.         }
  405.     }
  406.     
  407.     /**
  408.      * returns if the file is an image
  409.      *
  410.      * @return boolean 
  411.      */
  412.     public function isImage()
  413.     {
  414.         if (!isset($this->imagesize['mime']|| empty($this->imagesize['mime']))
  415.         {
  416.             return False;
  417.         else {
  418.             return True;
  419.         }
  420.     }
  421.     
  422.     /**
  423.      * returns the imagesize array
  424.      *
  425.      * @return array 
  426.      */
  427.     public function getImagesize()
  428.     {
  429.         return $this->imagesize;
  430.     }
  431.     
  432.     // ---------------------------------------------------------------------------
  433.     // output
  434.     // ---------------------------------------------------------------------------    
  435.     
  436.     /**
  437.      * passes the file as is to the browser
  438.      *
  439.      */
  440.     public function passthru()
  441.     {
  442.         if (self::isFile($this->myFilePath))
  443.         {
  444.             header("Content-Type: " $this->getMimeType());
  445.             $res_fp=fopen($this->myFilePath"rb");
  446.             fpassthru($res_fp);
  447.             fclose($res_fp);            
  448.         else {
  449.             header("Status: 404 Not Found");
  450.         }
  451.     }
  452.     
  453.     // ---------------------------------------------------------------------------
  454.     // file operations
  455.     // ---------------------------------------------------------------------------    
  456.     
  457.     /**
  458.      * deletes the file
  459.      *
  460.      */
  461.     public function delete()
  462.     {
  463.         if (self::isFile($this->myFilePath))
  464.         {
  465.             unlink($this->myFilePath);
  466.             $this->myFilePath="";
  467.         }
  468.     }
  469. }
  470.  
  471. ?>

Documentation generated on Thu, 08 Jan 2009 17:45:38 +0100 by phpDocumentor 1.4.0a2