1
0
mirror of https://github.com/e107inc/e107.git synced 2025-01-18 05:09:05 +01:00
php-e107/e107_handlers/file_class.php

441 lines
10 KiB
PHP
Raw Normal View History

2006-12-02 04:36:16 +00:00
<?php
/*
2009-11-17 10:46:35 +00:00
* e107 website system
*
* Copyright (C) 2008-2010 e107 Inc (e107.org)
2009-11-17 10:46:35 +00:00
* Released under the terms and conditions of the
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt)
*
* $URL$
* $Id$
*/
/**
* File/folder manipulation handler
2009-11-17 10:46:35 +00:00
*
* @package e107
* @subpackage e107_handlers
* @version $Id$
* @author e107 Inc.
2009-11-17 10:46:35 +00:00
*/
2006-12-02 04:36:16 +00:00
if (!defined('e107_INIT')) { exit; }
/*
Class to return a list of files, with options to specify a filename matching string and exclude specified directories.
get_files() is the usual entry point.
$path - start directory (doesn't matter whether it has a trailing '/' or not - its stripped)
$fmask - regex expression of file names to match (empty string matches all). Omit the start and end delimiters - '#' is added here.
If the first character is '~', this becomes a list of files to exclude (the '~' is stripped)
Note that 'special' characters such as '.' must be escaped by the caller
There is a standard list of files which are always excluded (not affected by the leading '~')
The regex is case-sensitive.
$omit - specifies directories to exclude, in addition to the standard list. Does an exact, case-sensitive match.
'standard' or empty string - uses the standard exclude list
Otherwise a single directory name, or an array of names.
$recurse_level - number of directory levels to search.
2010-01-16 14:17:10 +00:00
If the standard file or directory filter is unacceptable in a special application, the relevant variable can be set to an empty array (emphasis - ARRAY).
setDefaults() restores the defaults - preferable to setting using a 'fixed' string. Can be called prior to using the class without knowledge of what went before.
get_dirs() returns a list of the directories in a specified directory (no recursion) - similar critera to get_files()
rmtree() attempts to remove a complete directory tree, including the files it contains
2010-01-16 14:17:10 +00:00
Note:
Directory filters look for an exact match (i.e. regex not supported)
Behaviour is slightly different to previous version:
$omit used to be applied to just files (so would recurse down a tree even if no files match) - now used for directories
The default file and directory filters are always applied (unless modified between instantiation/set defaults and call)
*/
2006-12-02 04:36:16 +00:00
class e_file
{
/**
* Array of directory names to ignore (in addition to any set by caller)
* @var array
*/
public $dirFilter;
/**
* Array of file names to ignore (in addition to any set by caller)
* @var array
*/
public $fileFilter;
/**
* Defines what array format should return get_files() method
* If one of 'fname', 'path', 'full' - numerical array.
* If default - associative array (depends on $finfo value).
*
* @see get_files()
* @var string one of the following: default (BC) | fname | path | full
*/
public $mode = 'default';
/**
* Defines what info should gatter get_files method.
* Works only in 'default' mode.
*
* @var string default (BC) | image | file | all
*/
public $finfo = 'default';
/**
* Constructor
*/
2010-01-16 14:17:10 +00:00
function __construct()
{
$this->setDefaults();
}
/**
* Set default parameters
* @return e_file
*/
function setDefaults()
{
$this->dirFilter = array('/', 'CVS', '.svn'); // Default directory filter (exact matches only)
$this->fileFilter = array('^thumbs\.db$','^Thumbs\.db$','.*\._$','^\.htaccess$','^index\.html$','^null\.txt$','\.bak$','^.tmp'); // Default file filter (regex format)
return $this;
}
2010-01-16 14:17:10 +00:00
/**
* Set fileinfo mode
* @param string $val
* @return e_file
*/
public function setFileInfo($val='default')
{
2010-01-16 14:17:10 +00:00
$this->finfo = $val;
return $this;
}
2010-01-16 14:17:10 +00:00
2009-11-05 08:07:53 +00:00
/**
2010-01-16 14:17:10 +00:00
* Read files from given path
*
* @param string $path
* @param string $fmask [optional]
* @param string $omit [optional]
* @param integer $recurse_level [optional]
2009-12-13 21:52:32 +00:00
* @return array of file names/paths
2009-11-05 08:07:53 +00:00
*/
function get_files($path, $fmask = '', $omit='standard', $recurse_level = 0)
2006-12-02 04:36:16 +00:00
{
$ret = array();
$invert = FALSE;
if (substr($fmask,0,1) == '~')
{
$invert = TRUE; // Invert selection - exclude files which match selection
$fmask = substr($fmask,1);
}
if($recurse_level < 0)
2006-12-02 04:36:16 +00:00
{
return $ret;
}
if(substr($path,-1) == '/')
{
$path = substr($path, 0, -1);
}
2011-04-27 07:41:31 +00:00
if(!is_dir($path) || !$handle = opendir($path))
2006-12-02 04:36:16 +00:00
{
return $ret;
}
if (($omit == 'standard') || ($omit == ''))
2006-12-02 04:36:16 +00:00
{
$omit = array();
2006-12-02 04:36:16 +00:00
}
else
{
if (!is_array($omit))
2006-12-02 04:36:16 +00:00
{
$omit = array($omit);
2006-12-02 04:36:16 +00:00
}
}
while (false !== ($file = readdir($handle)))
{
if(is_dir($path.'/'.$file))
{ // Its a directory - recurse into it unless a filtered directory or required depth achieved
// Must always check for '.' and '..'
if(($file != '.') && ($file != '..') && !in_array($file, $this->dirFilter) && !in_array($file, $omit) && ($recurse_level > 0))
2006-12-02 04:36:16 +00:00
{
$xx = $this->get_files($path.'/'.$file, $fmask, $omit, $recurse_level - 1);
2006-12-02 04:36:16 +00:00
$ret = array_merge($ret,$xx);
}
}
else
2006-12-02 04:36:16 +00:00
{
// Now check against standard reject list and caller-specified list
if (($fmask == '') || ($invert != preg_match("#".$fmask."#", $file)))
{ // File passes caller's filter here
$rejected = FALSE;
2006-12-02 04:36:16 +00:00
// Check against the generic file reject filter
foreach($this->fileFilter as $rmask)
2006-12-02 04:36:16 +00:00
{
if(preg_match("#".$rmask."#", $file))
{
$rejected = TRUE;
break; // continue 2 may well work
}
}
if($rejected == FALSE)
2010-01-16 14:17:10 +00:00
{
switch($this->mode)
{
case 'fname':
$ret[] = $file;
break;
2010-01-16 14:17:10 +00:00
case 'path':
$ret[] = $path."/";
2010-01-16 14:17:10 +00:00
break;
case 'full':
$ret[] = $path."/".$file;
2010-01-16 14:17:10 +00:00
break;
case 'all':
default:
if('default' != $this->finfo)
{
$finfo = $this->get_file_info($path."/".$file, ('file' != $this->finfo)); // -> 'all' & 'image'
}
$finfo['path'] = $path."/"; // important: leave this slash here and update other file instead.
2010-01-16 14:17:10 +00:00
$finfo['fname'] = $file;
$ret[] = $finfo;
break;
2010-01-16 14:17:10 +00:00
}
2006-12-02 04:36:16 +00:00
}
}
2010-01-16 14:17:10 +00:00
}
2006-12-02 04:36:16 +00:00
}
return $ret;
}
/**
* Collect file information
* @param string $path_to_file
* @param boolean $imgcheck
* @return array
*/
function get_file_info($path_to_file, $imgcheck = true)
{
$finfo = array();
2010-01-16 14:17:10 +00:00
if($imgcheck && ($tmp = getimagesize($path_to_file)))
{
$finfo['img-width'] = $tmp[0];
$finfo['img-height'] = $tmp[1];
$finfo['mime'] = $tmp['mime'];
}
2010-01-16 14:17:10 +00:00
$tmp = stat($path_to_file);
if($tmp)
{
$finfo['fsize'] = $tmp['size'];
$finfo['modified'] = $tmp['mtime'];
}
// associative array elements: dirname, basename, extension, filename
$finfo['pathinfo'] = pathinfo($path_to_file);
2010-01-16 14:17:10 +00:00
return $finfo;
}
/**
* Get a list of directories matching $fmask, omitting any in the $omit array - same calling syntax as get_files()
* N.B. - no recursion - just looks in the specified directory.
* @param string $path
* @param strig $fmask
* @param string $omit
* @return array
*/
2006-12-02 04:36:16 +00:00
function get_dirs($path, $fmask = '', $omit='standard')
{
$ret = array();
if(substr($path,-1) == '/')
{
$path = substr($path, 0, -1);
}
if(!$handle = opendir($path))
{
return $ret;
}
2006-12-02 04:36:16 +00:00
if($omit == 'standard')
{
$omit = array();
2006-12-02 04:36:16 +00:00
}
else
{
if (!is_array($omit))
2006-12-02 04:36:16 +00:00
{
$omit = array($omit);
2006-12-02 04:36:16 +00:00
}
}
while (false !== ($file = readdir($handle)))
{
if(is_dir($path.'/'.$file) && ($file != '.') && ($file != '..') && !in_array($file, $this->dirFilter) && !in_array($file, $omit) && ($fmask == '' || preg_match("#".$fmask."#", $file)))
2006-12-02 04:36:16 +00:00
{
$ret[] = $file;
2006-12-02 04:36:16 +00:00
}
}
return $ret;
}
/**
* Delete a complete directory tree
* @param string $dir
* @return boolean success
*/
2006-12-02 04:36:16 +00:00
function rmtree($dir)
{
if (substr($dir, -1) != '/')
2006-12-02 04:36:16 +00:00
{
$dir .= '/';
}
if ($handle = opendir($dir))
{
while ($obj = readdir($handle))
{
if ($obj != '.' && $obj != '..')
{
if (is_dir($dir.$obj))
{
if (!$this->rmtree($dir.$obj))
{
return false;
}
}
elseif (is_file($dir.$obj))
{
if (!unlink($dir.$obj))
{
return false;
}
}
}
}
closedir($handle);
if (!@rmdir($dir))
{
return false;
}
return true;
}
return false;
}
/**
* Parse a file size string (e.g. 16M) and compute the simple numeric value.
*
* @param string $source - input string which may include 'multiplier' characters such as 'M' or 'G'. Converted to 'decoded value'
* @param int $compare - a 'compare' value
* @param string $action - values (gt|lt)
*
* @return int file size value.
* If the decoded value evaluates to zero, returns the value of $compare
* If $action == 'gt', return the larger of the decoded value and $compare
* If $action == 'lt', return the smaller of the decoded value and $compare
*/
function file_size_decode($source, $compare = 0, $action = '')
{
$source = trim($source);
if (strtolower(substr($source, -1, 1)) == 'b')
$source = substr($source, 0, -1); // Trim a trailing byte indicator
$mult = 1;
if (strlen($source) && (strtoupper(substr($source, -1, 1)) == 'B'))
$source = substr($source, 0, -1);
if (!$source || is_numeric($source))
{
$val = $source;
}
else
{
$val = substr($source, 0, -1);
switch (substr($source, -1, 1))
{
case 'T':
$val = $val * 1024;
case 'G':
$val = $val * 1024;
case 'M':
$val = $val * 1024;
case 'K':
case 'k':
$val = $val * 1024;
break;
}
}
if ($val == 0)
return $compare;
switch ($action)
{
case 'lt':
return min($val, $compare);
case 'gt':
return max($val, $compare);
default:
return $val;
}
return 0;
}
2006-12-02 04:36:16 +00:00
2011-05-03 08:46:34 +00:00
/**
* Parse bytes to human readable format
* Former Download page function
* @param mixed $size file size in bytes or file path if $retrieve is true
* @param boolean $retrieve defines the type of $size
*
* @return string formatted size
*/
function file_size_encode($size, $retrieve = false)
{
if($retrieve)
{
$size = filesize($size);
}
$kb = 1024;
$mb = 1024 * $kb;
$gb = 1024 * $mb;
$tb = 1024 * $gb;
if(!$size)
{
return '0&nbsp;'.CORE_LAN_B;
}
if ($size < $kb)
{
return $size."&nbsp;".CORE_LAN_B;
}
else if($size < $mb)
{
return round($size/$kb, 2)."&nbsp;".CORE_LAN_KB;
}
else if($size < $gb)
{
return round($size/$mb, 2)."&nbsp;".CORE_LAN_MB;
}
else if($size < $tb)
{
return round($size/$gb, 2)."&nbsp;".CORE_LAN_GB;
}
else
{
return round($size/$tb, 2)."&nbsp;".CORE_LAN_TB;
}
}
2006-12-02 04:36:16 +00:00
}