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 returns a JSON object of it
  83.      * 
  84.      * @param string $pstrFilePath directory to read
  85.      *
  86.      * @return string JSON string
  87.      */
  88.     public function download($pstrFilePath)
  89.     {
  90.         $strFilePath $this->m_strRootPath.'/'.$pstrFilePath;
  91.  
  92.         if (!$this->m_strRootPath || !is_dir($this->m_strRootPath|| !is_file($strFilePath)) 
  93.         {
  94.             return;
  95.         }
  96.  
  97.         header("Content-type: application/force-download");
  98.         header("Content-Transfer-Encoding: Binary");
  99.         header("Content-length: ".filesize($strFilePath));
  100.         header("Content-disposition: attachment; filename=\"".basename($strFilePath)."\"");
  101.         readfile($strFilePath);
  102.  
  103.         return;
  104.     }
  105.  
  106.     /**
  107.     * Get file JSON.
  108.     * 
  109.     * @param string $pstrPath           Path of the file
  110.     * @param string $pstrFile           Name of the file
  111.     * @param array  $pobjArrayResources Array of resources
  112.     * 
  113.     * @return string JSON
  114.     */
  115.     public function getFileJSON($pstrPath$pstrFile$pobjArrayResources)
  116.     {
  117.         if (substr($pstrPath-1!= '/'
  118.         {
  119.             $pstrPath .= '/';
  120.         }
  121.  
  122.         $objFileDescription array(
  123.             'text'     => $pstrFile,
  124.             'disabled' => false,
  125.             'leaf'     => (is_dir($pstrPath.$pstrFile)?false:true),
  126.             'qtip'     => (is_file($pstrPath.$pstrFile)?'Size: '.filesize($pstrPath.$pstrFile).' bytes':'')
  127.         );
  128.  
  129.         if (is_dir($pstrPath.$pstrFile)) 
  130.         {
  131.             $objFileDescription['cls''folder';
  132.         }
  133.         else if (strrpos($pstrFile'.')) 
  134.         {
  135.             $objFileDescription['iconCls''file-'.strtolower(substr($pstrFilestrrpos($pstrFile'.')+1));
  136.         }
  137.  
  138.         if (isset($pobjArrayResources[$pstrFile])) 
  139.         {
  140.             $objFileDescription['osmresource'$pobjArrayResources[$pstrFile];
  141.         }
  142.  
  143.         return json_encode($objFileDescription);
  144.     }
  145.  
  146.     /**
  147.     * Get file list.
  148.     * 
  149.     * @param string $pstrFilePath Path of the file
  150.     * 
  151.     * @return array 
  152.     */
  153.     public function getFileList($pstrFilePath)
  154.     {
  155.         $strFilePath $this->m_strRootPath.'/'.$pstrFilePath;
  156.  
  157.         $objArrayFileList array();
  158.  
  159.         if (!is_dir($strFilePath)) 
  160.         {
  161.             return $objArrayFileList;
  162.         }
  163.  
  164.         $objDirectory dir($strFilePath);
  165.         while (false !== ($strEntry $objDirectory->read())) 
  166.         {
  167.             if (substr($strEntry01!= '.'
  168.             {
  169.                 $objArrayFileList[$strEntry;
  170.             }
  171.         }
  172.         $objDirectory->close();
  173.  
  174.         return $objArrayFileList;
  175.     }
  176.  
  177.     /**
  178.     * Get the root path.
  179.     * 
  180.     * @return string 
  181.     */
  182.     private function getRootPath()
  183.     {
  184.         return $this->m_strRootPath;
  185.     }
  186.  
  187.     /**
  188.     * Set the root path.
  189.     *
  190.     * @param string $pstrRootPath The root path to set.
  191.     *
  192.     * @return void 
  193.     */
  194.     private function setRootPath($pstrRootPath)
  195.     {
  196.         if (substr($pstrRootPath-1== '/'
  197.         {
  198.             $pstrRootPath substr($pstrRootPath0-1);
  199.         }
  200.  
  201.         $this->m_strRootPath = $pstrRootPath;
  202.  
  203.         return;
  204.     }
  205.  
  206.     // #### UPLOAD ####
  207.  
  208.     /**
  209.     * Returns current uploaded item
  210.     *
  211.     * @return array 
  212.     */
  213.     public function getUploadedItem()
  214.     {
  215.         return $_FILES['x-filename'];
  216.     }
  217.  
  218.     /**
  219.     * Upload a file using current uploaded item.
  220.     *
  221.     * @param string $pstrBasePath The bae path of the file to upload.
  222.     *
  223.     * @return string 
  224.     */
  225.     public function upload($pstrBasePath)
  226.     {
  227.         $objResponse false;
  228.  
  229.         $strBasePath realpath($this->getRootPath().'/'.$pstrBasePath);
  230.  
  231.         if (!$this->m_strRootPath || !is_dir($this->m_strRootPath|| !is_dir($strBasePath)) 
  232.         {
  233.             return;
  234.         }
  235.  
  236.         $objArrayUploadedItem $this->getUploadedItem();
  237.         $strFilePath $strBasePath.'/'.$objArrayUploadedItem['name'];
  238.  
  239.         // do not upload if file already exists
  240.         if (is_file($strFilePath))
  241.         {
  242.             $objResponse array(
  243.                 'success' => false,
  244.                 'errors' => array("message" => "File already exists")
  245.             );
  246.         }
  247.  
  248.         // UPLOAD_ERR_? checkup
  249.         if ($objResponse === false
  250.             && $objArrayUploadedItem['error'!== UPLOAD_ERR_OK
  251.         {
  252.             $objResponse array(
  253.                 'success' => false,
  254.                 'errors' => array(
  255.                     "message" => $this->getFileUploadErrorMessage(
  256.                         $objArrayUploadedItem['error']
  257.                     )
  258.                 )
  259.             );
  260.         }
  261.  
  262.         // move_uploaded_file
  263.         if ($objResponse === false)
  264.         {
  265.             $bMoved move_uploaded_file(
  266.                 $objArrayUploadedItem['tmp_name'],
  267.                 $strFilePath
  268.             );
  269.  
  270.             if ($bMoved)
  271.             {
  272.                 $objResponse array('success' => true);
  273.             }
  274.             else
  275.             {
  276.                 $objResponse array(
  277.                     'success' => false,
  278.                     'errors' => array("message" => "Could not upload file")
  279.                 );
  280.             }
  281.         }
  282.  
  283.         return json_encode($objResponse);
  284.     }
  285.  
  286.     /**
  287.     * Create a new directory
  288.     *
  289.     * @param string $pstrPath The path of the directory to create
  290.     *
  291.     * @return string 
  292.     */
  293.     public function createDirectory($pstrPath)
  294.     {
  295.         $strPath $this->getRootPath().'/'.$pstrPath;
  296.  
  297.         if (mkdir($strPath))
  298.         {
  299.             $objResponse array('success' => true);
  300.         }
  301.         else
  302.         {
  303.             $objResponse array(
  304.                 'success' => false,
  305.                 'errors' => array("error" => "Could not create directory")
  306.             );
  307.         }
  308.  
  309.         return json_encode($objResponse);
  310.     }
  311.  
  312.     public function getFileUploadErrorMessage($piErrorCode)
  313.     {
  314.         $strMessage '';
  315.  
  316.         switch ($piErrorCode{
  317.             case UPLOAD_ERR_INI_SIZE:
  318.                 $strMessage 'The uploaded file exceeds the upload_max_filesize directive in php.ini';
  319.                 break;
  320.             case UPLOAD_ERR_FORM_SIZE:
  321.                 $strMessage 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form';
  322.                 break;
  323.             case UPLOAD_ERR_PARTIAL:
  324.                 $strMessage 'The uploaded file was only partially uploaded';
  325.                 break;
  326.             case UPLOAD_ERR_NO_FILE:
  327.                 $strMessage 'No file was uploaded';
  328.                 break;
  329.             case UPLOAD_ERR_NO_TMP_DIR:
  330.                 $strMessage 'Missing a temporary folder';
  331.                 break;
  332.             case UPLOAD_ERR_CANT_WRITE:
  333.                 $strMessage 'Failed to write file to disk';
  334.                 break;
  335.             case UPLOAD_ERR_EXTENSION:
  336.                 $strMessage 'File upload stopped by extension';
  337.                 break;
  338.             default:
  339.                 $strMessage 'Unknown upload error';
  340.         }
  341.  
  342.         return $strMessage;
  343.     }
  344. }
  345.  
  346. ?>

Documentation generated on Thu, 19 Jan 2012 00:08:33 +0400 by phpDocumentor 1.4.1