Source for file pcf_directory.phpinc

Documentation is available at pcf_directory.phpinc

  1. <?php
  2. /**
  3.   * Common Directory Functions
  4.   *
  5.   * @project    Open CSP-Management
  6.   * @package    common
  7.   * @category   directory
  8.   *
  9.   * @author     Peter Krebs (pk)<p.krebs@lvus.at>
  10.   * @copyright  (c) 2002-2003 by Peter Krebs and Landesverlag Unternehmensservice
  11.   * @license    http://opensource.org/licenses/gpl-license.php GNU Public License
  12.   *
  13.   * @version $Id: pcf_directory.phpinc,v 1.11 2008/11/06 08:41:58 pitlinz Exp $
  14.   */
  15.  
  16. if (!function_exists('pcf_checkFileName'))
  17. {
  18.     require __OCSP_PHPINCPATH__."common"._OCSP_DIRSEP_."pcf_file.phpinc";
  19. }
  20.     
  21.  
  22. /**
  23.   * returns an array of directories like a tree
  24.   *
  25.   * Example:
  26.   *
  27.   * 1: /home
  28.   * 2: /home/user1
  29.   * 3: /home/user1/public_html
  30.   * 4: /home/user2
  31.   * 5: /home/user2/mail
  32.   * ....
  33.   *
  34.   * this function calls itself recursive until $level==0
  35.   * or no subdirectories exists
  36.   *
  37.   * this function is slow with a large directory tree
  38.   *
  39.   * take care start point ($aDir) is set correct
  40.   * to avoid:
  41.   *     - open_basedir restrictions
  42.   *     - safe_mode    restrictions
  43.   *     - not getting the whole dir structure as result
  44.   *
  45.   *
  46.   * @param  string  $aDir       start directory
  47.   * @param  array   &$dirArr    return array
  48.   * @param  bool    $goDown     add subdirectory subdirs to array
  49.   * @param  string  $path       path to add to the dirArray Value
  50.   * @param  string  $excludes   do not add this directories and subdirectories
  51.   * @param  int     $levels     max number of going into subdir
  52.   *
  53.   ***/
  54. function pcf_dirArray($aDir,&$dirArr,$goDown=TRUE,$path="",$excludes="",$levels=10{
  55.     // echo "pcf_dirArray: $aDir<br />";
  56.     if (($levels 0&& (file_exists($aDir))) {
  57.         if (empty($path)) $dirArr[]="/";
  58.         if ($dirHandle opendir($aDir)) {
  59.             while ($file readdir($dirHandle)) {
  60.                 if (($file != "."&& ($file != "..")) {
  61.                     if (is_dir($aDir."/".$file)) {
  62.                         if (empty($excludes|| (!strstr($excludes,$file))) {
  63.                             $dirArr[]=$path."/".$file;
  64.                             if ($goDown)
  65.                                 pcf_dirArray($aDir."/".$file,$dirArr,$goDown,$path."/".$file,$excludes,($levels-1));
  66.                         }
  67.                     }
  68.                 }
  69.             }
  70.             closedir($dirHandle);
  71.         }
  72.     }
  73. }
  74.  
  75.  
  76. /**
  77.   * returns an array with files of a specified type in a directory
  78.   *
  79.   * possible types:
  80.   * - F   = FILE
  81.   * - D   = DIRECTORY
  82.   * . !D  = WITHOUT DIRECTORIES (pk-04-08-18)
  83.   * - L   = LINK
  84.   * - A   = ALL
  85.   *
  86.   * Combination of types are also allowed
  87.   * FL returns files and links
  88.   *
  89.   * @param string $dirName (the directory)
  90.   * @param string $type (file type(s))
  91.   * @param boolean $debug 
  92.   * @param array $blackList 
  93.   *
  94.   * @return array 
  95.   *
  96.   * @version pk-04-08-19
  97.   * @version pk-07-02-28 handle last slash
  98.   * @version pk-07-09-04 follow links in FL an DL
  99.   * @version pk-07-10-03 hide files ending with ~
  100.   * @version pk-07-11-12 $blackList added
  101.   *
  102.   */
  103. function pcf_getDirList($dirName,$type="F",$debug=FALSE,$blackList=array()) {
  104.     if ($debugechoDebug(__FILE__,"<p><b>pcf_getDirList($dirName,$type,...)</b><p>");
  105.     $retArr array();
  106.     
  107.     if (!is_array($blackList))
  108.     {
  109.         $blackList=array();
  110.     }
  111.  
  112.     /* <pk-04-08-18> */
  113.     if ($type == "!D"{
  114.         $type="FL";
  115.     }
  116.     /* </pk-04-08-18> */
  117.     $dirName=trim($dirName);
  118.     if (!empty($dirName)) {
  119.         if (substr($dirName,-1!= "/"$dirName.="/";
  120.         if ($debugecho "<p>Directory$dirName</p>";
  121.         if ($debug{
  122.             $dirHandle opendir($dirName);
  123.         else {
  124.             $dirHandle @opendir($dirName);
  125.         }
  126.         if ($dirHandle{
  127.             while ($file @readdir($dirHandle)) {
  128.                 $file trim($file);
  129.                 if ($debugecho "<p>File$file</p>";
  130.                 if ((!in_array($file,$blackList)) && ($file != "."&& ($file != ".."&& (substr($file,-1!= '~'))
  131.                 {
  132.                     if ((strstr($type,"F")) && (is_file($dirName.$file))) {
  133.                         $retArr[]=$file;
  134.                     else if ((strstr($type,"F"&& strstr($type,"L")) && (is_link($dirName.$file))) // <pk-07-09-04>
  135.                         $str_file=readlink($dirName.$file);
  136.                         while(is_link($str_file)) $str_file=readlink($str_file);
  137.                         if (is_file($str_file)) {
  138.                             $retArr[]=$file;
  139.                         }
  140.                     else if ((strstr($type,"D")) && (is_dir($dirName.$file))){
  141.                         $retArr[]=$file;
  142.                     else if ((strstr($type,"D"&& strstr($type,"L")) && (pcf_is_dir($dirName.$file))) // <pk-07-09-04>
  143.                         $retArr[]=$file;
  144.                     else if ((strstr($type,"L")) && (is_link($dirName.$file))){
  145.                         $retArr[]=$file;
  146.                     else if ((strstr($type,"A"))) {
  147.                         $retArr[]=$file;
  148.                     }
  149.                 }
  150.             }
  151.             closedir($dirHandle);
  152.         }
  153.         return $retArr;
  154.     else {
  155.         return FALSE;
  156.     }
  157. }
  158.  
  159.  
  160. /**
  161.   * checks if a directory exists and create it if necessary
  162.   *
  163.   * returns 1       if the directory exists
  164.   * returns 2       if there is a symlink with the name
  165.   * returns 3       if a new Directory is created
  166.   * returns False   if the directory does not exists or could not be created
  167.   *
  168.   * @param string $aDir       the directory
  169.   * @param boolean $createDir  create the dir if not exists
  170.   * @param octal $rMask      fileMask of the new directory if one is created
  171.   * @param boolean $debug 
  172.   * @return int 
  173.   *
  174.   * @version pk-07-11-13 check if the link source is a directory
  175.   * @version pk-08-06-11
  176.   */
  177. function pcf_checkDir($aDir,$createDir=False,$rMask=0770,$debug=False{
  178.     if (file_exists($aDir)) 
  179.     {
  180.         if (is_dir($aDir)) {
  181.             return 1;
  182.         else if (pcf_is_dir($aDir)) {           
  183.             return  2;
  184.         else {
  185.             return False;
  186.         }
  187.     }
  188.  
  189.     if (!$createDir{
  190.         return False;
  191.     }
  192.  
  193.     if (pcf_mkdirs($aDir$rMask)) {
  194.         return 3;
  195.     else {
  196.         if ($debugechoDebugLine(__FILE__,__LINE__,"<p>Could not create$aDir</p><hr>\n",3)// <pk-06-02-08 />
  197.         return False;
  198.     }
  199. }
  200.  
  201. /**
  202.   * replaces some chars in $dirname which can cause problems
  203.   *
  204.   * @uses pcf_checkFileName() for each directory name
  205.   *
  206.   * @param string $dirName 
  207.   * @param bool $isPath $dirName is a Path with
  208.   * @param char $pathSep 
  209.   *
  210.   * @return string 
  211.   *
  212.   * @todo   this function only works with linux servers for windows you can also have a drive letter like "c:"
  213.   *
  214.   * @version pk-07.02-28
  215.   *
  216.   * @todo   withpath is quick and durty coded
  217.   *
  218.   ***/
  219. function pcf_checkDirName($dirName,$isPath=False,$pathSep=_OCSP_DIRSEP_{
  220.     if ($isPath{
  221.         $dirName trim($dirName);
  222.         
  223.         $s_ret=((substr($dirName,0,1)==$pathSep$pathSep "");
  224.         if ($a_path=explode($pathSep,$dirName)) {
  225.             foreach($a_path as $s_dir{
  226.                 if (!empty($s_dir)) $s_ret.=pcf_checkFileName($s_dir).$pathSep;
  227.             }
  228.             
  229.             return $s_ret;
  230.         }
  231.         return $dirName;
  232.     else {
  233.         return pcf_checkFileName($dirName);
  234.     }
  235. }
  236.  
  237. /**
  238.   * sets $dirArr with files, symlinks and dirs for a file tree
  239.   *
  240.   * @param  array   $dirArr 
  241.   * @param  string  $startDir 
  242.   * @param  string  $subDir 
  243.   * @param  string  $filter 
  244.   * @param  string  $debug 
  245.   *
  246.   ***/
  247. function pcf_getDirTree(&$dirArr,$startDir,$subDir="",$filter="",$debug=FALSE{
  248.  
  249.     if ($fileList=pcf_getDirList($startDir,"FL",$debug)) {
  250.         reset($fileList);
  251.         while(list($key,$val)=each($fileList)) {
  252.             $dirArr[]=$subDir.$val;
  253.         }
  254.     }
  255.  
  256.     if ($subDirs=pcf_getDirList($startDir,"D",$debug)) {
  257.         reset($subDirs);
  258.         while(list($key,$val)=each($subDirs)) {
  259.             getDirTree($dirArr,$startDir."/".$val,$subDir."$val/",$filter,$debug);
  260.         }
  261.     }
  262.  
  263. }
  264.  
  265. /**
  266.   * creates a direcotry
  267.   *
  268.   * @param  string  $strPath 
  269.   * @param  octal   $mode       create mode
  270.   *
  271.   * @return bool 
  272.   ***/
  273. function pcf_mkdir($strPath,$mode=0775{
  274.     if (file_exists($strPath)) {
  275.         return TRUE;
  276.     }
  277.     $old_umask umask(0);
  278.     $ret=@mkdir($strPath,$mode);
  279.     umask($old_umask);
  280.     return $ret;
  281. }
  282.  
  283. /**
  284.   * remove a directory and everything that's in it.
  285.   *
  286.   * @param string $dirname 
  287.   * @param boolean $debug 
  288.   * @since pk-07-06-17
  289.   */
  290. function pcf_rmdir($dirname,$debug=FALSE{
  291.     if ($debugechoDebugLine(__FILE__,__LINE__,"<strong>pcf_rmdir(</strong>$dirname<strong>)</strong>");
  292.  
  293.     if ($dirHandle opendir($dirname)) {
  294.         chdir($dirname);
  295.         while ($file readdir($dirHandle)) {
  296.             if ($file == '.' || $file == '..'continue;
  297.             if (is_dir($file)) pcf_rmdir($file);
  298.             else unlink($file);
  299.         }
  300.         rmdir($dirname);
  301.         closedir($dirHandle);
  302.     }
  303. }
  304.  
  305. /**
  306.   * creates a direcotry with parentdir if not exists
  307.   *
  308.   * @uses mkdir in bindir
  309.   *
  310.   * @param  string  $strPath 
  311.   * @param  octal   $mode       create mode
  312.   * @param  bool    $debug      (pk-03-12-19)
  313.   *
  314.   * @return bool 
  315.   *
  316.   ***/
  317. function pcf_mkdirs($strPath$mode=0775$debug=FALSE{
  318.     /*
  319.     $debug=TRUE;
  320.     if ($debug) echo "<p><b>pcf_mkdirs($strPath, $mode, $debug)</b></p>";
  321.     $projPath=pcf_checkDirName(str_replace($_SERVER['PROJECTDIR'],"",$strPath),TRUE);
  322.     $ret=system($GLOBALS['PROJECT']['BINPATH']."mkdir ".$projPath,$ok);
  323.     if ($debug) {echo "<p>".$GLOBALS['PROJECT']['BINPATH']."mkdir ".$projPath."<hr>returned: ".$ok."<hr>lastLine: $ret";}
  324.     return $ok;
  325.  
  326.     */
  327.  
  328.     $strPath=trim($strPath);
  329.     if (empty($strPath))     return TRUE;
  330.     if (substr($strPath,-1== "/"$strPath=substr($strPath,0,-1);
  331.     if (is_dir($strPath))    return TRUE;
  332.  
  333.     $pStrPath dirname($strPath);
  334.     if ((!is_dir($pStrPath)) && (!pcf_mkdirs($pStrPath,$mode,$debug))) return FALSE;
  335.     return pcf_mkdir($strPath);
  336. }
  337.  
  338.  
  339. /**
  340.  * checks if a file is a directory
  341.  * 
  342.  * @param string $path 
  343.  * 
  344.  * @return boolean 
  345.  * 
  346.  * @since pk-08-05-18
  347.  */
  348. function pcf_is_dir($path)
  349. {
  350.     if (is_dir($path))
  351.     {
  352.         return True;
  353.     else if (is_link($path&& ($path=readlink($path))) {
  354.         return pcf_is_dir($path);
  355.     else {
  356.         return False;
  357.     }
  358.     
  359. }
  360. ?>

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