added fs_is_directorycomponent() and fs_is_hidden_file()

This commit is contained in:
azett 2022-06-24 21:03:19 +02:00
parent 7447db1b21
commit 28b7066d82

View File

@ -1,23 +1,21 @@
<?php <?php
/** /**
* Filesystem lib * Filesystem lib
* provides basic filesystem handling functions. * provides basic filesystem handling functions.
* *
* @author NoWhereMan <nowhereman@phreaker.net> * @author NoWhereMan <nowhereman@phreaker.net>
*/ */
class fs_filelister { class fs_filelister {
var $_list = array(); var $_list = array();
var $_directory = null;
var $_directory = null;
// constructor // constructor
function __construct($directory = null) { function __construct($directory = null) {
if ($directory) $this->_directory = $directory; if ($directory)
$this->_directory = $directory;
$this->_listFiles($this->_directory); $this->_listFiles($this->_directory);
} }
@ -28,20 +26,18 @@
} }
function _exitingDir($directory, $file) { function _exitingDir($directory, $file) {
} }
function _listFiles($directory) { function _listFiles($directory) {
// Try to open the directory // Try to open the directory
if (!file_exists($directory)) return array(); if (!file_exists($directory))
return array();
if ($dir = opendir($directory)) { if ($dir = opendir($directory)) {
// Add the files // Add the files
while ($file = readdir($dir)) { while ($file = readdir($dir)) {
if (!fs_is_directorycomponent($file)) {
if ($file != '.' && $file != '..') {
$action = $this->_checkFile($directory, $file); $action = $this->_checkFile($directory, $file);
// $action == 0: ok, go on // $action == 0: ok, go on
@ -49,25 +45,25 @@
// $action == 2: exit function // $action == 2: exit function
switch ($action) { switch ($action) {
case (1): { case (1):
{
$this->_listFiles("$directory/$file"); $this->_listFiles("$directory/$file");
$this->_exitingDir($directory, $file); $this->_exitingDir($directory, $file);
break; break;
} }
case (2): { case (2):
{
return false; return false;
} }
} }
} }
} }
// Finish off the function // Finish off the function
closedir($dir); closedir($dir);
return true; return true;
} } else
else return false; return false;
} }
function getList() { function getList() {
@ -84,6 +80,7 @@
} }
class fs_pathlister extends fs_filelister { class fs_pathlister extends fs_filelister {
function _checkFile($directory, $file) { function _checkFile($directory, $file) {
$f = "$directory/$file"; $f = "$directory/$file";
if (!is_dir($f)) if (!is_dir($f))
@ -102,14 +99,11 @@
// $id = lang_id($filename); // $id = lang_id($filename);
$files [] = $filename; $files [] = $filename;
} }
} }
sort($files); sort($files);
return $files; return $files;
} }
/** /**
* function fs_mkdir * function fs_mkdir
* *
@ -118,29 +112,31 @@
* <p>Recursively creates dirs.</p> * <p>Recursively creates dirs.</p>
* <p>Returns true on success, else false</p> * <p>Returns true on success, else false</p>
* *
* @param string $path Directory or directories to create * @param string $path
* @param int $mode octal mode value; same as UNIX chmod; defaults to 0777 (rwrwrw); * Directory or directories to create
* @param int $mode
* octal mode value; same as UNIX chmod; defaults to 0777 (rwrwrw);
* @return bool * @return bool
* *
* @todo cleanup & check bool return value * @todo cleanup & check bool return value
* *
*/ */
function fs_mkdir($dir, $mode = DIR_PERMISSIONS) { function fs_mkdir($dir, $mode = DIR_PERMISSIONS) {
if (is_dir($dir) || (@mkdir($dir,$mode))) {@chmod($dir, $mode); return TRUE;} if (is_dir($dir) || (@mkdir($dir, $mode))) {
if (!fs_mkdir(dirname($dir),$mode)) return FALSE; @chmod($dir, $mode);
return TRUE;
}
if (!fs_mkdir(dirname($dir), $mode))
return FALSE;
return (@mkdir($dir, $mode) && @chmod($dir, $mode)); return (@mkdir($dir, $mode) && @chmod($dir, $mode));
} }
/** /**
* function fs_delete * function fs_delete
* *
* Deletes a file and recursively deletes dirs, if they're empty * Deletes a file and recursively deletes dirs, if they're empty
*
*/ */
function fs_delete($path) { function fs_delete($path) {
if (file_exists($path)) { if (file_exists($path)) {
$fsuccess = unlink($path); $fsuccess = unlink($path);
@ -150,12 +146,10 @@
$path = dirname($path); $path = dirname($path);
$dsuccess = @rmdir($path); $dsuccess = @rmdir($path);
} }
// unlink can return both 0 and false -__-' // unlink can return both 0 and false -__-'
return ($fsuccess); return ($fsuccess);
} }
// in our particular implementation // in our particular implementation
@ -164,8 +158,6 @@
// so that we can anyway track it back // so that we can anyway track it back
return 2; return 2;
} }
/** /**
@ -174,14 +166,15 @@
* Perform a recursive reset of file permission in the given $path * Perform a recursive reset of file permission in the given $path
* and its subdirectories to 0777 * and its subdirectories to 0777
* *
* @param $fpath dir path * @param $fpath dir
* path
* @return bool * @return bool
* *
*/ */
class fs_chmodder extends fs_filelister { class fs_chmodder extends fs_filelister {
var $_chmod_dir; var $_chmod_dir;
var $_chmod_file; var $_chmod_file;
function __construct($directory, $ch_file = FILE_PERMISSIONS, $ch_dir = DIR_PERMISSIONS) { function __construct($directory, $ch_file = FILE_PERMISSIONS, $ch_dir = DIR_PERMISSIONS) {
@ -201,6 +194,7 @@
return $retval; return $retval;
} }
} }
function fs_chmod_recursive($fpath = FP_CONTENT) { function fs_chmod_recursive($fpath = FP_CONTENT) {
@ -208,47 +202,48 @@
return $obj->getList(); return $obj->getList();
} }
/** /**
* recursive deletion * recursive deletion
* deletes all files and directories recursively in the given $path * deletes all files and directories recursively in the given $path
* @param $fpath dir path *
* @param $fpath dir
* path
* @return bool * @return bool
*/ */
/*class fs_deleter extends fs_filelister {
function fs_deleter($directory) {
$this->_directory = $directory;
parent::__construct();
}
function _checkFile($directory, $file) {
$path = "$directory/$file";
/* /*
* class fs_deleter extends fs_filelister {
*
* function fs_deleter($directory) {
* $this->_directory = $directory;
* parent::__construct();
* }
*
* function _checkFile($directory, $file) {
*
* $path = "$directory/$file";
*
* /*
* open dir handle prevents directory deletion of php5 (and probably win) * open dir handle prevents directory deletion of php5 (and probably win)
* thanks to cimangi <cimangi (at) yahoo (dot) it> for noticing and * thanks to cimangi <cimangi (at) yahoo (dot) it> for noticing and
* giving a possible solution: * giving a possible solution:
* *
* filenames are cached and then deleted * filenames are cached and then deleted
// * //
*
if ( is_dir($path) ) { * if ( is_dir($path) ) {
return 1; * return 1;
} elseif ( file_exists($path) ) { * } elseif ( file_exists($path) ) {
array_push($this->_list, $path); * array_push($this->_list, $path);
return 0; * return 0;
} else { * } else {
return 2; * return 2;
} * }
*
} * }
*
} * }
*
*/ */
/* /*
@ -258,7 +253,6 @@
* *
* paths are now cached and then deleted * paths are now cached and then deleted
*/ */
function fs_delete_recursive($path) { function fs_delete_recursive($path) {
if (file_exists($path)) { if (file_exists($path)) {
@ -272,17 +266,11 @@
$elem; $elem;
fs_delete($elem); fs_delete($elem);
} }
} }
return true; return true;
} }
function fs_copy($source, $dest) { function fs_copy($source, $dest) {
if ($contents = io_load_file($source)) { if ($contents = io_load_file($source)) {
return io_write_file($dest, $contents); return io_write_file($dest, $contents);
@ -290,4 +278,24 @@
return false; return false;
} }
/**
* Checks if the file with the given name is a directory component ('.' or '..').
*
* @param string $filename
* the file name
* @return boolean <code>true</code> if the file is a directory component; otherwise <code>false</code>
*/
function fs_is_directorycomponent($filename) {
return strlen($filename) > 0 && ($filename === '.' || $filename === '..');
}
/**
* Checks if the file with the given name is a hidden file (i.e., starts with a '.').
*
* @param string $filename
* the file name
* @return boolean <code>true</code> if the file is a hidden file; otherwise <code>false</code>
*/
function fs_is_hidden_file($filename) {
return strlen($filename) > 0 && substr($filename, 0, 1) === '.';
}