DocMGR PHP Cross Reference Groupware Applications

Source: /lib/filefunctions.php - 378 lines - 8254 bytes - Summary - Text - Print

   1  <?php
   2  
   3  /*******************************************************************
   4    FILENAME: filefunctions.php
   5    PURPOSE:    holds custom file management functions
   6  *******************************************************************/
   7  
   8  function listDir($path,$rec=1,$filter=null) {
   9  
  10    $ret = array();
  11    
  12    $arr = scandir($path);
  13    $num = count($arr);
  14    
  15    for ($i=0;$i<$num;$i++) {
  16  
  17      if ($arr[$i]=="." || $arr[$i]=="..") continue;
  18  
  19      $obj = $path."/".$arr[$i];
  20  
  21      //if we have a filter, make sure the file type is in it (don't process directoreis)
  22      if (!is_dir($obj) && $filter) {
  23      
  24        $ext = fileExtension($obj);
  25        if ($ext) $ext = substr($ext,1);    //remove lead "."
  26        
  27        if (!in_array($ext,$filter)) continue;
  28  
  29      }
  30  
  31      //if we have a filter and it's not set to directory, then don't return directories
  32      if ($filter && is_dir($obj) && !in_array("directory",$filter)) {
  33        //do nothing
  34      } else {
  35        //add to the return array
  36        $ret[] = $obj;
  37      }
  38  
  39      //if recursive, look for more files       
  40      if (is_dir($obj) && $rec) $ret = array_merge($ret,listDir($obj,$rec,$filter));
  41  
  42  
  43    }
  44  
  45    return $ret;
  46  
  47  }
  48  
  49  /*******************************************************************
  50    FILENAME: emptyDir
  51    PURPOSE:    removes all files in a directory
  52  *******************************************************************/
  53  function emptyDir($path)
  54  {
  55  
  56    if (!is_dir($path)) return false;
  57  
  58    $files = listDir($path);
  59    
  60    if (count($files)==0) return false;
  61  
  62    //this makes sure the files in a directory are gone before we
  63    //try to delete the actual directory
  64    $files = array_reverse($files);
  65      
  66    foreach ($files AS $file) 
  67    {
  68      if (is_dir($file)) rmdir($file);
  69      else unlink($file);
  70    }
  71  
  72  }
  73  
  74  /*********************************************************
  75  //this function figures out the path to our files, based on whether or
  76  //not the filePath is relative
  77  *********************************************************/
  78  function getFilePath($filePath,$altFilePath) {
  79  
  80      //there is a leading slash, it's an absolute path
  81      if ($filePath[0]=="/") return $filePath;
  82      else {
  83  
  84          //return our relative path appended to the path
  85          //of the docmgr installation
  86          return $altFilePath."/".$filePath;
  87  
  88      }
  89  
  90  }
  91  /*******************************************************************
  92  //  this function spits out the files in chunks to 
  93  //  allow for larger files to be downloaded even though
  94  //  memory_limit is low.  I took this directly from
  95  //  the php website.
  96  *******************************************************************/
  97  
  98  function readfile_chunked ($filename) {
  99  
 100      $chunksize = 1*(1024*1024); // how many bytes per chunk (this is 1 mb)
 101  
 102      $buffer = null;
 103  
 104      if (!$handle = fopen($filename, 'rb')) return false;
 105  
 106      while (!feof($handle)) {
 107          $buffer = fread($handle, $chunksize);
 108          print $buffer;
 109      }
 110  
 111      return fclose($handle);
 112  
 113  }
 114  /*********************************************************
 115  *********************************************************/
 116  function fileExtension($file) {
 117  
 118      $pos = strrpos($file,".");
 119      if ($pos == "0") return false;
 120      else {
 121  
 122          $ext = strtolower(substr($file,$pos+1));
 123  
 124          return $ext;
 125  
 126      }
 127  }
 128  /*********************************************************
 129  *********************************************************/
 130  function fileIncludeType($ext) {
 131  
 132      $imageArray = array(    ".jpg",
 133                              ".png",
 134                              ".bmp",
 135                              ".gif",
 136                              ".tif",
 137                              ".tiff",
 138                              ".jpeg"
 139                              );
 140  
 141      $embedArray = array(    ".avi",
 142                              ".pdf",
 143                              ".mov",
 144                              ".doc"
 145                              );
 146  
 147  
 148  
 149      if (in_array($ext,$embedArray)) return "embed";
 150      elseif (in_array($ext,$imageArray)) return "image";
 151      else return "include";
 152  
 153  }
 154  
 155  
 156  /****************************************************************************
 157      return an array with all possible information regarding this extension
 158  ****************************************************************************/
 159  
 160  function return_file_type($filename) {
 161  
 162    $info = fileInfo($filename);
 163    return $info["custom_type"];
 164  }
 165  
 166  function return_file_mime($filename) {
 167  
 168    $info = fileInfo($filename);
 169    return $info["mime_type"];
 170  
 171  }
 172  
 173  //returns true if we can index the file, false if we can't
 174  function return_file_idxopt($filename) {
 175  
 176    $info = fileInfo($filename);
 177    return $info["prevent_index"];
 178      
 179  }
 180  
 181  
 182  function return_file_proper_name($filename) {
 183  
 184    $info = fileInfo($filename);
 185    return $info["proper_name"];
 186  
 187  }
 188  
 189  
 190  function return_file_info($filename,$filepath = null) {
 191  
 192    return fileInfo($filename);
 193  
 194  }
 195  
 196                      
 197  function return_file_extension($filename) {
 198  
 199    return fileExtension($filename);
 200  
 201  }
 202  
 203  /********************************
 204      new file info functions
 205  ********************************/
 206  function fileInfo($file) {
 207  
 208      if (!$_SESSION["extensions"]) {
 209      
 210          //get our extension config
 211          $xml = file_get_contents(SITE_PATH."/config/extensions.xml");
 212      
 213          $arr = XML::decode($xml);
 214          $_SESSION["extensions"] = $arr["object"];
 215  
 216    }
 217    
 218    $ext = fileExtension($file);
 219    $num = count($_SESSION["extensions"]);
 220  
 221    $fileinfo = array();
 222    
 223    for ($i=0;$i<$num;$i++) {
 224  
 225      //if this extension matches the passed one, stop
 226      if ($_SESSION["extensions"][$i]["extension"]==$ext) {
 227        $fileinfo = $_SESSION["extensions"][$i];
 228        break;
 229      }
 230      
 231    }
 232  
 233    return $fileinfo;
 234    
 235  }
 236  
 237  function displayFileSize($size,$floatsize = 2) {
 238  
 239    $kbTest = 1024;
 240    $mbTest = 1024*1024;
 241    $gbTest = 1024*1024*1024;
 242  
 243    //if greater than this size, return in kilobytes
 244    if ($size>=$gbTest) $size = number_format($size/$gbTest,$floatsize)." GB";
 245    elseif ($size>=$mbTest) $size = number_format($size/$mbTest,$floatsize)." MB";
 246    else $size = number_format($size/$kbTest,$floatsize)." KB";
 247    //else $size = $size." B";
 248  
 249    return $size;
 250  
 251  }
 252  
 253  /*********************************************************************
 254    FUNCTION:    extractFileName
 255    PURPOSE:    pulls a file's name from a full path
 256  *********************************************************************/
 257  function extractFileName($path) {
 258  
 259    return array_pop(explode("/",$path));
 260  
 261  } 
 262  
 263  /*******************************************************
 264    FUNCTION:    recurmkdir
 265    PURPOSE:    recursively makes the desired directory
 266              on the filesystem
 267  *******************************************************/
 268  function recurMkDir($path) 
 269  {
 270  
 271    $arr = explode("/",$path);
 272    $cur = null;
 273  
 274    foreach ($arr AS $dir)
 275    {
 276  
 277      $cur .= $dir."/";
 278      if (!file_exists("$cur")) mkdir("$cur");
 279  
 280    }
 281  
 282  }
 283  
 284  
 285  function listDirectory($directory,$extFilter=null,$nameFilter=null) 
 286  {
 287  
 288    $resultArray = array();
 289  
 290    $handle=@opendir($directory);
 291  
 292    while ($file = @readdir($handle)) 
 293    {
 294  
 295      //skip directory markers, hidden directorys, and joe leftovers
 296      if ($file!="." && $file!=".." && $file[0]!="." && substr($file,-1)!="~" && !strstr($file,"THUMB_")) 
 297      {
 298  
 299        //skip this one if the name filter isn't matched
 300        if ($nameFilter) 
 301        {
 302  
 303          $pos = strpos($file,trim($nameFilter));
 304  
 305          if ($pos===FALSE) continue;
 306  
 307        }
 308  
 309        //is our filter an array o
 310        if (is_array($extFilter)) 
 311        {
 312  
 313          //get the extension;
 314          $pos = strrpos($file,".");
 315          $ext = strtolower(substr($file,$pos));
 316  
 317          if (in_array($ext,$extFilter)) $resultArray[] = $file;
 318  
 319        }
 320        else 
 321        {
 322          $resultArray[] = $file;
 323        }
 324  
 325      }
 326  
 327    }
 328  
 329    @closedir($handle);
 330  
 331    return $resultArray;
 332  
 333  }
 334  
 335  function copyDir($src,$dest)
 336  {
 337  
 338      $list = listDir($src);
 339      
 340      //make our destination
 341      recurMkDir($dest);
 342      
 343      if (count($list) > 0)
 344      {
 345      
 346          foreach ($list AS $path)
 347          {
 348          
 349              //replace the path part of our object with the destination
 350              $destpath = str_replace($src,$dest,$path);
 351              
 352              //it's a directory
 353              if (is_dir($path))
 354              {
 355              
 356                  //now copy the contents of the directory
 357                  copyDir($path,$destpath);
 358              
 359              }
 360              //it's a file, just copy it
 361              else
 362              {
 363                copy($path,$destpath);
 364              }
 365          
 366          }
 367      
 368      }
 369  
 370  }
 371  
 372  /**
 373    because I'm stupid and didn't realize there was already a function that did this
 374    */
 375  function getFileName($path)
 376  {
 377    return basename($path);
 378  }

title

Description

title

Description

title

Description

title

title

Body