Location: PHPKode > projects > Whiteboard > whiteboard1.0.3/program/include/filesysteminclude.php
<?php
/* Whiteboard Courseware System v1.0.3
   Copyright (C) 2002-2003 Todd Templeton

   This program is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by the Free
   Software Foundation; either version 2 of the License, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful, but WITHOUT
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
   more details.

   You should have received a copy of the GNU General Public License along with
   this program; if not, write to the Free Software Foundation, Inc., 59 Temple
   Place, Suite 330, Boston, MA 02111-1307 USA

   Contact info: my paper address changes often, but you can contact me at
   hide@address.com
*/


/* makes a directory
   returns true/false
   $dir is full path to parent directory
   $subdir is new subdirectory of $dir
*/
function makedirectory ($dir, $subdir) {
  if (!@mkdir ($dir . "/" . $subdir, 0770)) {
    @chmod ($dir, 0770);
    if (!@mkdir ($dir . "/" . $subdir, 0770))
      return false;
  }
  return true;
}


/* copies a file
   returns true/false
   $source is full path to source file
   $destination is full path to destination file
*/
function copyfile ($source, $destination) {
  if (!file_exists ($source))
    return false;
  if (!@copy ($source, $destination))
    return false;
  return true;
}


/* moves a file by copying and then deleting
   returns true/false
   $source is full path to source file
   $destination is full path to destination file
*/
function movefile ($source, $destination) {
  if (!copyfile ($source, $destination))
    return false;
  if (!deletefile ($source)) {
    deletefile ($destination);
    return false;
  }
  return true;
}


/* deletes a file
   returns true/false
   $filepath is full path to file
*/
function deletefile ($filepath) {
  if (!@unlink ($filepath)) {
    @chmod ($filepath, 0660);
    if (!@unlink ($filepath))
      return false;
  }
  return true;
}


/* recursive portion of dircleanup ()
   see dircleanup ()
*/
function dircleanuprec ($path, $regex, $mindepth, $maxdepth, $depth) {
  if (!is_bool ($maxdepth) && ($depth > $maxdepth))
    return true;
  if (!($dir = @opendir ($path)))
    return false;
  rewinddir ($dir);
  while ($file = readdir ($dir)) {
    $filepath = $path . "/" . $file;
    if (($file == ".") || ($file == ".."))
      continue;
    if (($depth >= $mindepth) && preg_match ("/" . $regex . "/", $file)) {
      @exec (RMCOMMAND . " -rf " . escapeshellarg ($filepath));
      if (file_exists ($filepath))
	return false;
    }
    else if (is_dir ($filepath)) {
      if (!dircleanuprec ($filepath, $regex, $mindepth, $maxdepth, $depth + 1))
        return false;
    }
  }
  closedir ($dir);
  return true;
}


/* recursively deletes files and directories from a directory tree
   returns true/false (returns false on error)
   $path is a full directory path
   $regex is a Perl-style regular expression that indicates what to delete
   $mindepth is minimum depth from $path to start deleting files (files inside
      $path are at depth 0)
   $maxdepth is maximum depth at which to search for files to delete
   does not check for symlink loops
   ignores "." and ".."
*/
function dircleanup ($path, $regex, $mindepth, $maxdepth) {
  return dircleanuprec ($path, $regex, $mindepth, $maxdepth, 0);
}


/* appends to a file
   returns true/false
   $filepath is full path to file
   $data is data to append
*/
function appendfile ($filepath, $data) {
  if (!$filepath)
    return true;
  if (!($file = @fopen ($filepath, "a")))
    return false;
  if (@fwrite ($file, $data) == -1) {
    @fclose ($file);
    return false;
  }
  @fclose ($file);
  return true;
}


/* finds total size of all files in directory
   returns number of bytes, returns false if $dirpath is unreadable
   $dirpath is a full directory path
   $recursive (bool) determines whether to recursively include the size of
      all subdirectories of $dirpath as far down as it has permission to go
   does not check for symlink loops
*/
function dirsize ($dirpath, $recursive) {
  if (!($dir = @opendir ($dirpath)))
    return false;
  $size = 0;
  while ($file = readdir ($dir)) {
    if (($file == ".") || ($file == ".."))
      continue;
    if (is_dir ($dirpath . "/" . $file) || is_link ($dirpath . "/" . $file)) {
      if ($recursive)
	$retval = dirsize ($dirpath . "/" . $file, true);
      else
	$retval = 0;
    }
    else
      $retval = @filesize ($dirpath . "/" . $file);
    if (is_int ($retval))
      $size += $retval;
  }
  @closedir ($dir);
  return $size;
}


/* retrieve the contents of a directory
   returns flat array containing file names as values, returns false on error
   $dirpath is a full path
   $type is the type of files to return (-1 for directories only, 0 for no
      directories, 1 for all)
   $expression is a Perl-style regular expression that file names must match
      (bool to accept all file names)
   $sort (bool) determines whether to do a normal string sort on the file names
   "." and ".." are ignored
*/
function dircontents ($dirpath, $type, $expression, $sort) {
  if (!($dir = @opendir ($dirpath)))
    return false;
  $contents = false;
  while ($file = readdir ($dir)) {
    if (($file == ".") || ($file == ".."))
      continue;
    if ($type == 0) {
      if (is_dir ($dirpath . "/" . $file))
        continue;
    }
    if ($type == -1) {
      if (!is_dir ($dirpath . "/" . $file))
        continue;
    }
    if (is_bool ($expression) || preg_match ($expression, $file))
      $contents[] = $file;
  }
  @closedir ($dir);
  if ((count ($contents) > 1) && $sort)
    sort ($contents);
  return $contents;
}


/*NOTE: not used*/
/* determine if directory is empty
   returns true/false (returns false on error)
   $dirpath is a full directory path
   $ignore is a Perl-style regular expression indicating file names to ignore
   ignores "." and ".."
*/
function dirisempty ($dirpath, $ignore) {
  if (!($dir = @opendir ($dirpath)))
    return true;
  while ($file = readdir ($dir)) {
    if (($file != ".") && ($file != "..")) {
      if (is_bool ($ignore) || !preg_match ($ignore, $file)) {
        @closedir ($dir);
        return false;
      }
    }
  }
  @closedir ($dir);
  return true;
}


/* uses UNIX file command with "brief" option to determine the type of a file
   returns a file type string, returns false on error
   $file is a full file path
*/
function fileinfo ($file) {
  $retval = exec (FILECOMMAND . " -b " . escapeshellarg ($file));
  if (preg_match ("/\Acan't stat /", $retval))
    return false;
  $retval = preg_replace ("/,\Z/", "", $retval);
  return $retval;
}


/* uses UNIX file command with MIME type option to determine the MIME type of a
     file
   returns a MIME type string, returns false on error
   $file is a full file path
*/
function filemimetype ($file) {
  /*use UNIX file command with "brief" option*/
  $retval = @exec (FILECOMMAND . " -b -i " . escapeshellarg ($file));
  if (preg_match ("/\Acan't stat /", $retval))
    return false;
  $retval = strtolower ($retval);
  /*at least PDF comes out as PDF document*/
  $retval = preg_replace ("/\A(\S+)\s+document(?:\s+|\Z)/", "application/\\1", $retval);
  return $retval;
}


/* determine if a file is a text file
   returns true/false (returns false on error)
   $file is a full file path
*/
function istextfile ($file) {
  if (!($filetype = fileinfo ($file)))
    return false;
  /*"text" is a special word only used in description if file is a true text
    file
  */
  if (preg_match ("/(?:\s+|\A)text(?:,|;|.|\/|\s+|\Z)/", $filetype))
    return true;
  return false;
}
?>
Return current item: Whiteboard