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$','^\.cvsignore$','^\.ftpquota$','^index\.html$','^null\.txt$','\.bak$','^.tmp'); // Default file filter (regex format) return $this; } /** * Set fileinfo mode * @param string $val * @return e_file */ public function setFileInfo($val='default') { $this->finfo = $val; return $this; } public function setFileFilter($filter) { $this->fileFilter = $filter; return $this; } /** * Clean and rename file name * @param $f array as returned by get_files(); * @param $rename boolean - set to true to rename file. */ public function cleanFileName($f,$rename=false) { $fullpath = $f['path'].$f['fname']; $newfile = preg_replace("/[^a-z0-9-\._]/", "-", strtolower($f['fname'])); $newpath = $f['path'].$newfile; if($rename == true) { if(!rename($fullpath,$newpath)) { $f['error'] = "Couldn't rename $fullpath to $newpath"; } } $f['fname'] = $newfile; return $f; } /** * Read files from given path * * @param string $path * @param string $fmask [optional] * @param string $omit [optional] * @param integer $recurse_level [optional] * @return array of file names/paths */ function get_files($path, $fmask = '', $omit='standard', $recurse_level = 0) { $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) { return $ret; } if(substr($path,-1) == '/') { $path = substr($path, 0, -1); } if(!is_dir($path) || !$handle = opendir($path)) { return $ret; } if (($omit == 'standard') || ($omit == '')) { $omit = array(); } else { if (!is_array($omit)) { $omit = array($omit); } } 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)) { $xx = $this->get_files($path.'/'.$file, $fmask, $omit, $recurse_level - 1); $ret = array_merge($ret,$xx); } } else { // 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; // Check against the generic file reject filter foreach($this->fileFilter as $rmask) { if(preg_match("#".$rmask."#", $file)) { $rejected = TRUE; break; // continue 2 may well work } } if($rejected == FALSE) { switch($this->mode) { case 'fname': $ret[] = $file; break; case 'path': $ret[] = $path."/"; break; case 'full': $ret[] = $path."/".$file; 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. $finfo['fname'] = $file; $ret[] = $finfo; break; } } } } } 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(); if($imgcheck && ($tmp = getimagesize($path_to_file))) { $finfo['img-width'] = $tmp[0]; $finfo['img-height'] = $tmp[1]; $finfo['mime'] = $tmp['mime']; } $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); return $finfo; } // Grab a remote file and save it in the /temp directory. requires CURL function getRemoteFile($remote_url, $local_file, $type='temp') { $path = ($type == 'media') ? e_MEDIA : e_TEMP; $fp = fopen($path.$local_file, 'w'); // media-directory is the root. $cp = curl_init($remote_url); curl_setopt($cp, CURLOPT_FILE, $fp); $buffer = curl_exec($cp); curl_close($cp); fclose($fp); return ($buffer) ? true : false; } /** * 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 */ function get_dirs($path, $fmask = '', $omit='standard') { $ret = array(); if(substr($path,-1) == '/') { $path = substr($path, 0, -1); } if(!$handle = opendir($path)) { return $ret; } if($omit == 'standard') { $omit = array(); } else { if (!is_array($omit)) { $omit = array($omit); } } 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))) { $ret[] = $file; } } return $ret; } /** * Delete a complete directory tree * @param string $dir * @return boolean success */ function rmtree($dir) { if (substr($dir, -1) != '/') { $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; } /** * 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 '.CORE_LAN_B; } if ($size < $kb) { return $size." ".CORE_LAN_B; } else if($size < $mb) { return round($size/$kb, 2)." ".CORE_LAN_KB; } else if($size < $gb) { return round($size/$mb, 2)." ".CORE_LAN_MB; } else if($size < $tb) { return round($size/$gb, 2)." ".CORE_LAN_GB; } else { return round($size/$tb, 2)." ".CORE_LAN_TB; } } /** Recursive Chmod function. * @param string path to folder * @param string perms for files * @param string perms for directories * @example chmod_R('mydir', 0644, 0755); */ function chmod($path, $filemode=0644, $dirmode=0755) { if (is_dir($path) ) { if (!chmod($path, $dirmode)) { $dirmode_str=decoct($dirmode); print "Failed applying filemode '$dirmode_str' on directory '$path'\n"; print " `-> the directory '$path' will be skipped from recursive chmod\n"; return; } $dh = opendir($path); while (($file = readdir($dh)) !== false) { if($file != '.' && $file != '..') // skip self and parent pointing directories { $fullpath = $path.'/'.$file; $this->chmod($fullpath, $filemode,$dirmode); } } closedir($dh); } else { if (is_link($path)) { print "link '$path' is skipped\n"; return; } if (!chmod($path, $filemode)) { $filemode_str=decoct($filemode); print "Failed applying filemode '$filemode_str' on file '$path'\n"; return; } } } }