__construct($path);
}
/**
* Main Constructor
*
* Builds the page header, footer, and contains the logic to decide what to do next
*/
function __construct($path="./") {
// get current file name, for all internal links
$this->filename = basename(__FILE__);
array_push($this->ignorelist, $this->filename);
// insert page header
$this->createHeader();
// condition: create initial file list?
if (isset($_GET['delete']) && !empty($_GET['delete'])) {
// a path has been set, escape and use
$path = basename($_GET['delete']);
$path = urldecode($path);
//$path = mysql_real_escape_string($path);
// condition : Step 2: seek deletion confirmation?
if (!isset($_GET['confirm']) || $_GET['confirm'] != 'aye') {
$this->createConfirmationStep($path);
// step 3: delete!
} else {
$this->createShowDelete($path);
}
// step 1: no files selected, create file list
} else {
echo '
(Careful how you use this, there\'s no undo!)
';
$this->createFileList($path);
echo '
';
}
// insert page footer
$this->createFooter();
}
/**
* Step 1: Create a list of all files within a specific directory
*
* @param string $path The server path to look for files in
* @return array $fileList Array of all files, with file/directory details
* @access public
*/
function createFileList($path) {
// condition : if the path isn't set, assume one
if (!isset($path)) {
$path = "./";
}
// temporary arrays to hold separate file and directory content
$filelist = array();
$directorylist = array();
// get the ignore list, in local scope
$ignorelist = $this->ignorelist;
// Open directory and read contents
if (is_dir($path)) {
// loop through the contents (PHP4 compat)
$dh = opendir($path);
while (false !== ($file = readdir($dh))) {
// skip over any files in the ignore list
if (!in_array($file, $ignorelist)) {
// condition : if it is a directory, add to dir list array
if (is_dir($path.$file)) {
$directorylist[] = array(
"path" => $path,
"file" => $file,
"filetype" => 'directory',
"date" => date("M d Y, H:i", filemtime($path.$file."")),
"filecount" => $this->countRelevantFiles($path.$file),
"filesize" => 0
);
// file, add to file array
} else {
$filelist[] = array(
"path" => $path,
"file" => $file,
"filetype" => $this->getFileType($path.$file) . " file",
"date" => date("M d Y, H:i", filemtime($path.$file."")),
"filecount" => 0,
"filesize" => $this->getFileSize(filesize($path.$file))
);
}
}
}
}
// merge file and directory lists
$finalList = array_merge($directorylist, $filelist);
// loop through each file
foreach ($finalList as $key => $value) {
// condition : add trailing slash for directories
$trailingslash = ($value['filetype'] == 'directory' ) ? '/' : '';
// condition : if it is a directory, display count of subfiles
if ($value['filetype'] == 'directory') {
$fileending = ($value['filecount'] == 1) ? 'item' : 'items';
$filedetails = ' (contains '.$value['filecount'].' '.$fileending.')';
// else, if it is a file, display file size
} else {
$filedetails = ' ('.$value['filesize'].')';
}
// create the html for each project
echo '
' . $value['file'] . ' /
' . $value['filetype'] . ' /
' . $filedetails . ' /
';
if ($value['filetype'] != "directory") {
echo '
file size: ' . $value['filesize'] . ' /
';
}
echo '
created: ' . $value['date'] . ' /
Delete
';
}
}
/**
* count the number of files in a directory, not including the list of ignorable files
*
* @param string $path The server path to look for files in
* @return int $count The number of relevant files
* @access private
*/
function countRelevantFiles($path, $count = 0) {
// open the directory
if (is_dir($path)) {
// loop through all files, checking if we should count the current one
$dh = opendir($path);
while (false !== ($file = readdir($dh))) {
if (!in_array($file, $this->ignorelist)) {
$count++;
if(is_dir($path."/".$file)) {
$count = $this->countRelevantFiles($path."/".$file, $count);
}
}
}
}
// return the result
return $count;
}
/**
* list all sub-files of a directory
*
* @param string $path The server path to look for files in
* @return void
* @access private
*/
function listFilesToDelete($path) {
// open the directory
if (is_dir($path)) {
// loop through all files, checking if we should count the current one
$dh = opendir($path);
while (false !== ($file = readdir($dh))) {
if (!in_array($file, $this->ignorelist)) {
echo ''.$path.'/'.$file.'';
if(is_dir($path."/".$file)) {
$this->listFilesToDelete($path."/".$file);
}
}
}
}
}
/**
* Delete files
*
* @param string $path The server path to delete
* @return void
* @access private
*/
function delete($path) {
// Simple delete for a file
if (is_file($path)) {
echo 'deleting file: ' . $path . '';
return unlink($path);
} else {
// Loop through the folder
$dir = dir($path);
while (false !== $entry = $dir->read()) {
// ignore specified files
if (in_array($entry, $this->ignorelist)) {
continue;
}
// deep delete
if (is_dir($path."/".$entry)) {
$this->delete($path."/".$entry);
} else {
echo 'removing file: ' . $path.'/'.$entry.'';
unlink($path."/".$entry);
}
}
// Clean up
$dir->close();
echo 'removing directory: '.$path.'';
return rmdir($path);
}
}
/**
* Create a nice readable filesize from the number of bytes in a file
*
* @param int $size the size in bytes
* @param string $retstring
*
* @return string the size in nice words
*/
function getFileSize($size, $retstring = null)
{
$sizes = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
if ($retstring === null) { $retstring = '%01.2f %s'; }
$lastsizestring = end($sizes);
foreach ($sizes as $sizestring) {
if ($size < 1024) { break; }
if ($sizestring != $lastsizestring) { $size /= 1024; }
}
if ($sizestring == $sizes[0]) { $retstring = '%01d %s'; } // Bytes aren't normally fractional
return sprintf($retstring, $size, $sizestring);
}
/**
* Function to find a file type for a given filename
*
* @param string $file filename/path
* @return string $extension file type
*/
function getFileType($file="") {
// get file name
$filearray = explode("/", $file);
$filename = array_pop($filearray);
// condition : if no file extension, return
if(strpos($filename, ".") === false) return false;
// get file extension
$filenamearray = explode(".", $filename);
$extension = $filenamearray[(count($filenamearray) - 1)];
return $extension;
}
/* Page Building Methods */
/**
* Create page header
*/
function createHeader(){
echo '
Deleter
File deleter
';
}
/**
* Create page footer
*/
function createFooter(){
echo '
';
}
/**
* Create confirmation step
*/
function createConfirmationStep($path){
echo '
« back to file list
Please confirm that you want to delete the following files:
Delete
- '.$path.'
';
$this->listFilesToDelete($path);
echo '
Delete
';
}
/**
* Show the files you're deleting
*/
function createShowDelete($path){
echo '
« back to file list
The following items have been removed:
';
$this->delete($path);
echo '
Deletion complete.
« back to file list
';
}
}
?>