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

Source for file FilesystemToolkit.class.php

Documentation is available at FilesystemToolkit.class.php

  1. <?php
  2. /**
  3.  * For including this file you have to define the constant "CLASSPATH".
  4.  * Because every include in the framework depends on the CLASSPATH definition.
  5.  * The CLASSPATH means the relative path to the folder that contains the
  6.  * framework GilliGan.
  7.  *
  8.  * @package filesystem
  9.  */
  10. if( !defined( "CLASSPATH" ) )
  11. {
  12.   echo "<h3>You have to define the constant CLASSPATH!</h3>\n";
  13.   echo "Example: define( 'CLASSPATH', '../path/to/classes/' );\n";
  14.   exit();
  15. }
  16.  
  17. /**
  18.  * Including various functions to manipulate arrays.
  19.  */
  20. require_once( CLASSPATH."core/Arrays.class.php" );
  21.  
  22. /**
  23.  * Constant FILESANDFOLDER describes that files and folder kan match this value.
  24.  * For example if you want to read both (file and folders) from a filesystem.
  25.  */
  26. define( "FILEORFOLDER", 0 );
  27. /**
  28.  * Constant FILES is defined to declare the type files in a filesystem.
  29.  */
  30. define( "FILE", 1 );
  31. /**
  32.  * Constant FOLDER is defined to declare the type folder in a filesystem.
  33.  */
  34. define( "FOLDER", 2 );
  35.  
  36.  
  37. /**
  38.  * Static Methods
  39.  *
  40.  * @package   filesystem
  41.  * @version   0.1.9
  42.  * @static
  43.  * @author    Daniel Plücken ( daniel@debakel.net )
  44.  * @license   http://www.gnu.org/copyleft/lesser.html
  45.  *             GNU Lesser General Public License
  46.  * @copyright Copyright (c) 2003 Daniel Plücken ( daniel@debakel.net )
  47.  *
  48.  *  This library is free software; you can redistribute it and/or
  49.  *  modify it under the terms of the GNU Lesser General Public
  50.  *  License as published by the Free Software Foundation; either
  51.  *  version 2.1 of the License.
  52.  *
  53.  *  This library is distributed in the hope that it will be useful,
  54.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  55.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  56.  *  GNU Lesser General Public License for more details.
  57.  *
  58.  *  You should have received a copy of the GNU Lesser General
  59.  *  Public License along with this library; if not, write to the
  60.  *  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  61.  *  Boston, MA 02111-1307 USA
  62.  */
  63. {
  64.    /**
  65.     * Removes the last slash in a path if it is on the last position.
  66.     *
  67.     * @version 1.0
  68.     * @since   0.1.0
  69.     * @author  Daniel Plücken <daniel@debakel.net>
  70.     * @access  public
  71.     * @static
  72.     * @param   string  $string  The string comprises the path and the filename
  73.     *                            and which should be slited.
  74.     * @return  string 
  75.     */
  76.    function getCleanPath( $string )
  77.    {
  78.       // perhaps?
  79.       // return preg_replace( "!(.+?)/ *$!", "$1", $string );
  80.       if( ( $strpos = strrpos( $string, '/' ) ) === strlen( $string ) - 1 )
  81.         return substr( $string, 0, $strpos );
  82.       else
  83.         return $string;
  84.    }
  85.    
  86.    
  87.    
  88.     /**
  89.      * Return human readable sizes.
  90.      *
  91.      * @author      Aidan Lister <aidan@php.net>
  92.      * @version     1.1
  93.       * @since        0.1.9
  94.      * @link        http://aidanlister.com/repos/v/function.size_readable.php
  95.      * @static
  96.      * @param       int    $size        Size
  97.      * @param       int    $unit        The maximum unit
  98.      * @param       int    $retstring   The return string format
  99.      * @param       int    $si          Whether to use SI prefixes
  100.      */
  101.     function size_readable( $size, $unit = null, $retstring = null, $si = true )
  102.     {
  103.         // Units
  104.         if ($si === true) {
  105.             $sizes = array('B', 'KB', 'MB', 'GB', 'TB', 'PB');
  106.             $mod   = 1000;
  107.         } else {
  108.             $sizes = array('B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB');
  109.             $mod   = 1024;
  110.         }
  111.         $ii = count($sizes) - 1;
  112.      
  113.         // Max unit
  114.         $unit = array_search($unit, $sizes);
  115.         if ($unit === null || $unit === false) {
  116.             $unit = $ii;
  117.         }
  118.      
  119.         // Return string
  120.         if ($retstring === null) {
  121.             $retstring = '%01.2f %s';
  122.         }
  123.      
  124.         // Loop
  125.         $i = 0;
  126.         while ($unit != $i && $size >= 1024 && $i < $ii) {
  127.             $size /= $mod;
  128.             $i++;
  129.         }
  130.      
  131.         return sprintf($retstring, $size, $sizes[$i]);
  132.     }
  133.    
  134.    
  135.    
  136.    /**
  137.     * Returns the suffix of a file. To be returned, the suffix has to have three
  138.     * or four digits and contains only letters (ASCII) and numbers.
  139.     *
  140.     * @version 1.0
  141.     * @since   0.1.85
  142.     * @author  Daniel Plücken <daniel@debakel.net>
  143.     * @access  public
  144.     * @static
  145.     * @param   string  $path The string containing the path to the file to
  146.     *                           checkout.
  147.     * @return  string 
  148.     */
  149.    function getFileSuffix( $path )
  150.    { return preg_replace( "!.+\.([a-z0-9]{3,4})$!i", "$1", $path ); }
  151.  
  152.  
  153.  
  154.    /**
  155.     * Separates the path and the filename of each other and returns the splited
  156.     * values in an array. The first entry [0] of the array will be the path and
  157.     * the second entry [1] will be the filename.
  158.     *
  159.     * @version 1.2
  160.     * @since   0.1.1
  161.     * @author  Daniel Plücken <daniel@debakel.net>
  162.     * @access  public
  163.     * @static
  164.     * @param   string  $string  The string comprises the path and the filename
  165.     *                            and which should be slited.
  166.     * @return  array 
  167.     */
  168.    function separatePathAndFile( $string )
  169.    {
  170.        $string = FilesystemToolkit::getCleanPath( $string );
  171.        $pos = strrpos( $string, "/" );
  172.        if( $pos === false )
  173.        {
  174.           $arr[0] = ".";
  175.           $arr[1] = $string;
  176.        }
  177.        else
  178.        {
  179.           $arr[0] = substr( $string, 0, $pos );
  180.           $arr[1] = substr( $string, $pos + 1 );
  181.        }
  182.        
  183.        return $arr;
  184.    }
  185.  
  186.  
  187.  
  188.    /**
  189.     * Extracs the direct path from one to another folder.
  190.     * The from-folder is absolut the target-folder is relative.
  191.     * For example you read out links from a htmlsource and you want to follow
  192.     * this links. You know the url of the file's htmlsource and where the links
  193.     * you read out relativly refer to. Then you just should give the url $from
  194.     * and the relative path $to to this function and it will return you the
  195.     * direct url to the links in the htmlsource.
  196.     *
  197.     * @version 1.2
  198.     * @since   0.1.4
  199.     * @author  Daniel Plücken <daniel@debakel.net>
  200.     * @access  public
  201.     * @static
  202.     * @param   string  $from 
  203.     * @param   string  $to 
  204.     * @return  string 
  205.     */
  206.   function getPurePathFromTo( $from, $to )
  207.   {
  208.      $from    = FilesystemToolkit::getCleanPath( $from );
  209.      $to      = FilesystemToolkit::getCleanPath( $to );
  210.      $fromArr = explode( "/", $from );
  211.      $toArr   = explode( "/", $to );
  212.      
  213.      while ( $toArr[0] == "." )
  214.        Arrays::deleteRecord( 0, $toArr );
  215.      
  216.      for ( $i = 0; $i < count( $toArr ); $i++ )
  217.         if ( $toArr[$i] == ".." )
  218.            Arrays::deleteRecord( count( $fromArr )-1, $fromArr );
  219.      
  220.      while ( $toArr[0] == ".." )
  221.         Arrays::deleteRecord( 0, $toArr );
  222.      
  223.      $frompath  = implode( "/", $fromArr );
  224.      $outputstr = ( !empty( $frompath )
  225.                     ? $frompath."/"
  226.                     : ""
  227.                   )
  228.                  .implode( "/", $toArr );
  229.      
  230.      return $outputstr;
  231.   }
  232.  
  233.  
  234.  
  235.   /**
  236.     * Loads the content of a given folder. Stores file- and foldernmaes in a two
  237.     * dimensional array like as follows:
  238.     *
  239.     *    $out[FILENUMBER]['name']
  240.     *    $out[FILENUMBER]['is_dir']
  241.     *    $out[FILENUMBER]['size'] (if flag $filesize is set)
  242.     *
  243.     * The output array will be sorted by the filename.
  244.     *
  245.     * @static
  246.     * @version 1.2
  247.     * @since   0.1.5
  248.     * @todo    Modifying the sort that files and folders will be grouped in the
  249.     *           out array
  250.     * @author  Daniel Plücken <daniel@debakel.net>
  251.     * @access  public
  252.     * @param   string  $dir 
  253.     * @param   string  $regExpMatchFiles 
  254.     * @param   string  $regExpMatchFolder 
  255.     * @param   boolean $filesize 
  256.     * @return  false|array
  257.     */
  258.   function loadFolderContent(
  259.                               $dir,
  260.                               $regExpMatchFiles  = "",
  261.                               $regExpMatchFolder = "",
  262.                               $filesize          = false
  263.                             )
  264.   {
  265.     if( !is_dir( $dir ) )
  266.       return false;
  267.     else
  268.     {
  269.          $out = array();
  270.          clearstatcache();
  271.          
  272.          $i = 0;
  273.          $folder = opendir( $dir );
  274.          # folder and files to separate arrays.
  275.          while ( ( $file = readdir( $folder ) ) !== false )
  276.          {
  277.             if (
  278.                  $file != "."
  279.               && $file != ".."
  280.                )
  281.             {
  282.                # collecting folder
  283.                if (
  284.                     is_dir( $dir."/".$file )
  285.                  && (
  286.                       empty( $regExpMatchFolder )
  287.                    || preg_match( $regExpMatchFolder, $file )
  288.                     )
  289.                   )
  290.                {
  291.                  $out[$i]["name"]   = $file;
  292.                  $out[$i]["is_dir"] = true;
  293.                  
  294.                  $i++;
  295.                }
  296.                else # collecting files
  297.                if (
  298.                     empty( $regExpMatchFiles )
  299.                  || preg_match( $regExpMatchFiles, $file )
  300.                   )
  301.                {
  302.                  $out[$i]["name"]   = $file;
  303.                  $out[$i]["is_dir"] = false;
  304.                  
  305.                  if ( $filesize )
  306.                     $out[$i]["size"] = filesize( $dir."/".$file );
  307.                  
  308.                  $i++;
  309.                }
  310.             }
  311.          }
  312.          closedir( $folder );
  313.          
  314.          if ( count( $out ) > 1 )
  315.             Arrays::sortSecondDimension( $out, "name" );
  316.          
  317.          return $out;
  318.     }
  319.   }
  320.  
  321.  
  322.  
  323.   /**
  324.    * Loads the content of a given file.
  325.    *
  326.    * @static
  327.    * @version 1.0
  328.    * @since   0.1.5
  329.    * @author  Daniel Plücken <daniel@debakel.net>
  330.    * @access  public
  331.    * @param   string  $path  Where the file can be found.
  332.    * @param   string  $file  The file's name whose content should be loaded.
  333.    * @return  string 
  334.    */
  335.   function loadFileContent()
  336.   {
  337.       $param = func_get_args();
  338.       
  339.       if ( empty( $param[1] ) )
  340.       {
  341.          $tmpArr = FilesystemToolkit
  342.                  ::separatePathAndFile( $param[0] );
  343.          $path = $tmpArr[0];
  344.          $file = $tmpArr[1];
  345.       }
  346.       else
  347.       {
  348.          $path = $param[0];
  349.          $file = $param[1];
  350.       }
  351.       
  352.       if ( file_exists( $path."/".$file ) )
  353.          $data = implode( "", ( file( $path."/".$file ) ) );
  354.       
  355.       return $data;
  356.   }
  357.  
  358.  
  359.  
  360.   /**
  361.    * Copies a directory recursively.
  362.    *
  363.    * @static
  364.    * @version 1.0
  365.    * @since   0.1.7
  366.    * @author  Daniel Plücken <daniel@debakel.net>
  367.    * @access  public
  368.    * @param   string  $source  The directory to copy.
  369.    * @param   string  $target  The target directory for copying.
  370.    * @return  string 
  371.    */
  372.   function copyDir( $source, $target )
  373.   {
  374.       $source = FilesystemToolkit::getCleanPath( $source );
  375.       $target = FilesystemToolkit::getCleanPath( $target );
  376.       
  377.       if ( !is_dir( $target ) )
  378.          mkdir( $target );
  379.       
  380.       $folder = opendir( $source );
  381.       while ( ( $file = readdir( $folder ) ) !== false )
  382.       {
  383.          if (
  384.               $file != "."
  385.            && $file != ".."
  386.             )
  387.          {
  388.             # recursive invokation
  389.             if ( is_dir( $source."/".$file ) )
  390.             {
  391.               if ( !is_dir( $target."/".$file ) )
  392.                  mkdir( $target."/".$file );
  393.               
  394.               FilesystemToolkit::copyDir(
  395.                                            $source."/".$file,
  396.                                            $target."/".$file 
  397.                                         );
  398.             }
  399.             else
  400.             if ( !file_exists( $target."/".$file ) )
  401.             {
  402.                // temporarly file content
  403.                $tfc = FilesystemToolkit::loadFileContent( $source."/".$file );
  404.                fwrite( fopen( $target."/".$file, 'w' ), $tfc );
  405.             }
  406.          }
  407.       }
  408.       closedir( $folder );
  409.   }
  410.  
  411.  
  412.  
  413.   /**
  414.    * Deletes a directory recursively.
  415.    *
  416.    * @static
  417.    * @version 1.0
  418.    * @since   0.1.6
  419.    * @author  Daniel Plücken <daniel@debakel.net>
  420.    * @access  public
  421.    * @param   string  $dir  The directory to delete.
  422.    * @return  void 
  423.    */
  424.   function deleteDir( $dir )
  425.   {
  426.       $dir = FilesystemToolkit::getCleanPath( $dir );
  427.       
  428.       $folder = opendir( $dir );
  429.       while ( ( $file = readdir( $folder ) ) !== false )
  430.       {
  431.          if (
  432.               $file != "."
  433.            && $file != ".."
  434.             )
  435.          {
  436.             # recursive invokation
  437.             if( is_dir( $dir."/".$file ) )
  438.               FilesystemToolkit::deleteDir( $dir."/".$file );
  439.             else
  440.               unlink( $dir."/".$file );
  441.          }
  442.       }
  443.       closedir( $folder );
  444.       rmdir( $dir );
  445.   }
  446.  
  447.  
  448.  
  449.   /**
  450.    * Deletes a directory recursively. This method is an alias for the method
  451.    * deleteDir()
  452.    *
  453.    * @static
  454.    * @version 1.0
  455.    * @since   0.1.6
  456.    * @author  Daniel Plücken <daniel@debakel.net>
  457.    * @access  public
  458.    * @param   string  $dir  The directory to delete.
  459.    * @return  string 
  460.    */
  461.   function removeDir( $dir )
  462.  
  463.  
  464.  
  465.   /**
  466.    * Rewrites a directory recursively to handle it like it is written by the
  467.    * server user.
  468.    *
  469.    * @static
  470.    * @version 1.0
  471.    * @since   0.1.7
  472.    * @author  Daniel Plücken <daniel@debakel.net>
  473.    * @access  public
  474.    * @param   string  $dir  The directory to rewrite.
  475.    * @return  string 
  476.    */
  477.   function rewriteDir( $dir )
  478.   {
  479.       $dir = FilesystemToolkit::getCleanPath( $dir );
  480.       
  481.       $folder = opendir( $dir );
  482.       while ( ( $file = readdir( $folder ) ) !== false )
  483.       {
  484.          if (
  485.               $file != "."
  486.            && $file != ".."
  487.             )
  488.          {
  489.             # recursive invokation
  490.             if ( is_dir( $dir."/".$file ) )
  491.                FilesystemToolkit::rewriteDir( $dir."/".$file );
  492.             else
  493.             {
  494.                // temporarly file content
  495.                $tfc = FilesystemToolkit::loadFileContent( $dir."/".$file );
  496.                unlink( $dir."/".$file );
  497.               
  498.                fwrite( fopen( $dir."/".$file, 'w' ), $tfc );
  499.             }
  500.          }
  501.       }
  502.       closedir( $folder );
  503.   }
  504.  
  505.  
  506.  
  507.   /**
  508.    * Checks whether an url can access.
  509.    *
  510.    * @static
  511.    * @version 1.0
  512.    * @since   0.1.8
  513.    * @author  Fabrizio (staff at bibivu dot com)
  514.    * @author  Daniel Plücken <daniel@debakel.net> (only write down here)
  515.    * @see     http://www.php.net/manual/en/function.file-exists.php
  516.    * @access  public
  517.    * @param   string  $url The url to proof of existence.
  518.    * @return  string 
  519.    */
  520.   function url_exists( $url )
  521.   {
  522.      $a_url = parse_url( $url );
  523.      if ( !isset( $a_url["port"] ) )
  524.         $a_url["port"] = 80;
  525.      
  526.      $errno = 0;
  527.      $errstr = "";
  528.      $timeout = 30;
  529.      if (
  530.           !isset( $a_url["host"] )
  531.        || $a_url["host"] == gethostbyname( $a_url["host"] )
  532.         )
  533.         return false;
  534.      else
  535.      {
  536.         $fid = fsockopen(
  537.                     $a_url["host"],
  538.                     $a_url["port"],
  539.                     $errno, $errstr,
  540.                     $timeout
  541.                         );
  542.         
  543.         if ( !$fid )
  544.            return false;
  545.         
  546.         $page  = isset( $a_url["path"] )
  547.                  ? $a_url["path"]
  548.                  : "";
  549.         $page .= isset( $a_url["query"] )
  550.                  ? "?".$a_url["query"]
  551.                  : "";
  552.         
  553.         fputs(
  554.                 $fid,
  555.                 "HEAD ".$page." HTTP/1.0\r\n"
  556.                ."Host: ".$a_url["host"]."\r\n\r\n"
  557.              );
  558.         $head = fread( $fid, 4096 );
  559.         fclose( $fid );
  560.         
  561.         return preg_match( '#^HTTP/.*\s+[200|302]+\s#i', $head );
  562.      }
  563.   }
  564. } // END of class FilesystemToolkit
  565. ?>

Documentation generated on Thu, 05 Jun 2008 19:11:36 +0200 by phpDocumentor 1.4.1