Source for file FileTree.php

Documentation is available at FileTree.php

  1. <?php
  2. /**
  3. * Class to read the disc and build a JSON array from the files.
  4. * This is based on documentation found at:
  5. * http://aariadne.com/filetree/
  6. *
  7. * PHP versions 5
  8. @category  PHP
  9. @package   GeoPrisma
  10. @author    Julien-Samuel Lacroix
  11. @copyright 2010, Mapgears inc
  12. @license   http://www.geoprisma.org/license BSD License
  13. @link      http://www.geoprisma.org
  14. */
  15.  
  16. /**
  17. * Class to read the disc and build a JSON array from the files.
  18. * This is based on documentation found at:
  19. * http://aariadne.com/filetree/
  20. @category   PHP
  21. @package    GeoPrisma
  22. @subpackage Proxy
  23. @author     Julien-Samuel Lacroix
  24. */   
  25. {
  26.     var $m_strRootPath = '';
  27.  
  28.     /**
  29.      * Build a FileTree
  30.      * 
  31.      * @param string $pstrRootPath based path of the listing
  32.      *
  33.      * @return void 
  34.      */
  35.     public function __construct($pstrRootPath="")
  36.     {
  37.         if ($pstrRootPath && is_dir($pstrRootPath)) 
  38.         {
  39.             $this->setRootPath($pstrRootPath);
  40.         }
  41.     }
  42.  
  43.     /**
  44.      * Read the content of a file path and returns a JSON object of it
  45.      * 
  46.      * @param string $pstrFilePath       Directory to read
  47.      * @param array  $pobjArrayFileList  Array of files
  48.      * @param array  $pobjArrayResources Array of resources
  49.      *
  50.      * @return string JSON
  51.      */
  52.     public function get(
  53.         $pstrFilePath,
  54.         $pobjArrayFileList=array(),
  55.         $pobjArrayResources=array()
  56.     {
  57.         $strFilePath $this->m_strRootPath.'/'.$pstrFilePath;
  58.  
  59.         if (!$this->m_strRootPath || !is_dir($this->m_strRootPath|| is_file($strFilePath|| !is_dir($strFilePath)) 
  60.         {
  61.             return '[]';
  62.         }
  63.  
  64.         if (count($pobjArrayFileList<= 0
  65.         {
  66.             $pobjArrayFileList $this->getFileList($strFilePath);
  67.         }
  68.  
  69.         $objArrayDirectoryToJSON array();
  70.         foreach ($pobjArrayFileList as $strEntry)
  71.         {
  72.             if (substr($strEntry01!= '.'
  73.             {
  74.                 $objArrayDirectoryToJSON[$this->getFileJSON($strFilePath$strEntry$pobjArrayResources);
  75.             }
  76.         }
  77.  
  78.         return '['.implode(','$objArrayDirectoryToJSON).']';
  79.     }
  80.  
  81.     /**
  82.      * Read the content of a file path and return it to the browser.
  83.      * 
  84.      * @param string $pstrFilePath    Path of the file to download
  85.      * @param bool   $pbForceDownload Whether to force downloading the file or
  86.      *                                 not
  87.      *
  88.      * @return void 
  89.      */
  90.     public function download($pstrFilePath$pbForceDownload true)
  91.     {
  92.         $strFilePath $this->m_strRootPath.'/'.$pstrFilePath;
  93.  
  94.         if (!$this->m_strRootPath || !is_dir($this->m_strRootPath|| !is_file($strFilePath)) 
  95.         {
  96.             return;
  97.         }
  98.         if ($pbForceDownload !== false
  99.         {
  100.             header("Content-type: application/force-download");
  101.             header("Content-disposition: attachment; filename=\"".basename($strFilePath)."\"");
  102.         }
  103.         else
  104.         {
  105.             $objArrayPathParts pathinfo($strFilePath)
  106.             $strExt strtolower($objArrayPathParts["extension"])
  107.             // Determine Content Type
  108.             switch ($strExt
  109.                 case "pdf":
  110.                     $strCType="application/pdf";
  111.                     break
  112.                 case "docx":
  113.                 case "doc":
  114.                     $strCType="application/msword";
  115.                     break
  116.                 case "xlsx"
  117.                 case "xls":
  118.                     $strCType="application/vnd.ms-excel";
  119.                     break
  120.                 case "pptx":
  121.                 case "ppt":
  122.                     $strCType="application/vnd.ms-powerpoint";
  123.                     break
  124.                 case "gif":
  125.                     $strCType="image/gif";
  126.                     break
  127.                 case "png":
  128.                     $strCType="image/png";
  129.                     break
  130.                 case "jpeg"
  131.                 case "jpg":
  132.                     $strCType="image/jpg";
  133.                     break
  134.                 default$strCType="application/force-download"
  135.             
  136.             header("Content-type: $strCType");
  137.             header("Content-disposition: inline; filename=\"".basename($strFilePath)."\"");
  138.         }
  139.         header("Content-Transfer-Encoding: Binary");
  140.         header("Content-length: ".filesize($strFilePath));
  141.         readfile($strFilePath);
  142.  
  143.         return;
  144.     }
  145.  
  146.     /**
  147.     * Get file JSON.
  148.     * 
  149.     * @param string $pstrPath           Path of the file
  150.     * @param string $pstrFile           Name of the file
  151.     * @param array  $pobjArrayResources Array of resources
  152.     * 
  153.     * @return string JSON
  154.     */
  155.     public function getFileJSON($pstrPath$pstrFile$pobjArrayResources)
  156.     {
  157.         if (substr($pstrPath-1!= '/'
  158.         {
  159.             $pstrPath .= '/';
  160.         }
  161.  
  162.         $objFileDescription array(
  163.             'text'     => $pstrFile,
  164.             'disabled' => false,
  165.             'leaf'     => (is_dir($pstrPath.$pstrFile)?false:true),
  166.             'qtip'     => (is_file($pstrPath.$pstrFile)?'Size: '.filesize($pstrPath.$pstrFile).' bytes':'')
  167.         );
  168.  
  169.         if (is_dir($pstrPath.$pstrFile)) 
  170.         {
  171.             $objFileDescription['cls''folder';
  172.         }
  173.         else if (strrpos($pstrFile'.')) 
  174.         {
  175.             $objFileDescription['iconCls''file-'.strtolower(substr($pstrFilestrrpos($pstrFile'.')+1));
  176.         }
  177.  
  178.         if (isset($pobjArrayResources[$pstrFile])) 
  179.         {
  180.             $objFileDescription['osmresource'$pobjArrayResources[$pstrFile];
  181.         }
  182.  
  183.         return json_encode($objFileDescription);
  184.     }
  185.  
  186.     /**
  187.     * Get file list.
  188.     * 
  189.     * @param string $pstrFilePath Path of the file
  190.     * 
  191.     * @return array 
  192.     */
  193.     public function getFileList($pstrFilePath)
  194.     {
  195.         $strFilePath $this->m_strRootPath.'/'.$pstrFilePath;
  196.  
  197.         $objArrayFileList array();
  198.  
  199.         if (!is_dir($strFilePath)) 
  200.         {
  201.             return $objArrayFileList;
  202.         }
  203.  
  204.         $objDirectory dir($strFilePath);
  205.         while (false !== ($strEntry $objDirectory->read())) 
  206.         {
  207.             if (substr($strEntry01!= '.'
  208.             {
  209.                 $objArrayFileList[$strEntry;
  210.             }
  211.         }
  212.         $objDirectory->close();
  213.  
  214.         return $objArrayFileList;
  215.     }
  216.  
  217.     /**
  218.     * Get the root path.
  219.     * 
  220.     * @return string 
  221.     */
  222.     private function getRootPath()
  223.     {
  224.         return $this->m_strRootPath;
  225.     }
  226.  
  227.     /**
  228.     * Set the root path.
  229.     *
  230.     * @param string $pstrRootPath The root path to set.
  231.     *
  232.     * @return void 
  233.     */
  234.     private function setRootPath($pstrRootPath)
  235.     {
  236.         if (substr($pstrRootPath-1== '/'
  237.         {
  238.             $pstrRootPath substr($pstrRootPath0-1);
  239.         }
  240.  
  241.         $this->m_strRootPath = $pstrRootPath;
  242.  
  243.         return;
  244.     }
  245.  
  246.     // #### UPLOAD ####
  247.  
  248.     /**
  249.     * Returns current uploaded item
  250.     *
  251.     * @return array 
  252.     */
  253.     public function getUploadedItem()
  254.     {
  255.         return $_FILES['x-filename'];
  256.     }
  257.  
  258.     /**
  259.     * Upload a file using current uploaded item.
  260.     *
  261.     * @param string $pstrBasePath The bae path of the file to upload.
  262.     *
  263.     * @return string 
  264.     */
  265.     public function upload($pstrBasePath)
  266.     {
  267.         $objResponse false;
  268.  
  269.         $strBasePath realpath($this->getRootPath().'/'.$pstrBasePath);
  270.  
  271.         if (!$this->m_strRootPath || !is_dir($this->m_strRootPath|| !is_dir($strBasePath)) 
  272.         {
  273.             return;
  274.         }
  275.  
  276.         $objArrayUploadedItem $this->getUploadedItem();
  277.         $strFilePath $strBasePath.'/'.$objArrayUploadedItem['name'];
  278.  
  279.         // do not upload if file already exists
  280.         if (is_file($strFilePath)) 
  281.         {
  282.             $objResponse array(
  283.                 'success' => false,
  284.                 'errors' => array("message" => "File already exists")
  285.             );
  286.         }
  287.  
  288.         // UPLOAD_ERR_? checkup
  289.         if ($objResponse === false
  290.             && $objArrayUploadedItem['error'!== UPLOAD_ERR_OK
  291.         
  292.         {
  293.             $objResponse array(
  294.                 'success' => false,
  295.                 'errors' => array(
  296.                     "message" => $this->getFileUploadErrorMessage(
  297.                         $objArrayUploadedItem['error']
  298.                     )
  299.                 )
  300.             );
  301.         }
  302.  
  303.         // move_uploaded_file
  304.         if ($objResponse === false
  305.         {
  306.             $bMoved move_uploaded_file(
  307.                 $objArrayUploadedItem['tmp_name'],
  308.                 $strFilePath
  309.             );
  310.  
  311.             if ($bMoved
  312.             {
  313.                 $objResponse array('success' => true);
  314.             }
  315.             else
  316.             {
  317.                 $objResponse array(
  318.                     'success' => false,
  319.                     'errors' => array("message" => "Could not upload file")
  320.                 );
  321.             }
  322.         }
  323.  
  324.         return json_encode($objResponse);
  325.     }
  326.  
  327.     /**
  328.     * Create a new directory
  329.     *
  330.     * @param string $pstrPath The path of the directory to create
  331.     *
  332.     * @return string 
  333.     */
  334.     public function createDirectory($pstrPath)
  335.     {
  336.         $strPath $this->getRootPath().'/'.$pstrPath;
  337.  
  338.         if (mkdir($strPath)) 
  339.         {
  340.             $objResponse array('success' => true);
  341.         }
  342.         else
  343.         {
  344.             $objResponse array(
  345.                 'success' => false,
  346.                 'errors' => array("error" => "Could not create directory")
  347.             );
  348.         }
  349.  
  350.         return json_encode($objResponse);
  351.     }
  352.  
  353.     /**
  354.     * Return the upload error message using the error code.
  355.     *
  356.     * @param integer $piErrorCode The error code returned
  357.     *
  358.     * @return string 
  359.     */
  360.     public function getFileUploadErrorMessage($piErrorCode)
  361.     {
  362.         $strMessage '';
  363.  
  364.         switch ($piErrorCode{
  365.             case UPLOAD_ERR_INI_SIZE:
  366.                 $strMessage 'The uploaded file exceeds the upload_max_filesize directive in php.ini';
  367.                 break;
  368.             case UPLOAD_ERR_FORM_SIZE:
  369.                 $strMessage 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form';
  370.                 break;
  371.             case UPLOAD_ERR_PARTIAL:
  372.                 $strMessage 'The uploaded file was only partially uploaded';
  373.                 break;
  374.             case UPLOAD_ERR_NO_FILE:
  375.                 $strMessage 'No file was uploaded';
  376.                 break;
  377.             case UPLOAD_ERR_NO_TMP_DIR:
  378.                 $strMessage 'Missing a temporary folder';
  379.                 break;
  380.             case UPLOAD_ERR_CANT_WRITE:
  381.                 $strMessage 'Failed to write file to disk';
  382.                 break;
  383.             case UPLOAD_ERR_EXTENSION:
  384.                 $strMessage 'File upload stopped by extension';
  385.                 break;
  386.             default:
  387.                 $strMessage 'Unknown upload error';
  388.         }
  389.  
  390.         return $strMessage;
  391.     }
  392. }
  393.  
  394. ?>

Documentation generated on Mon, 20 Feb 2012 13:46:18 -0500 by phpDocumentor 1.4.1