debug > 8) mtrace("creating dir {$path}
");
$oldMask = umask(0);
if(!filesystem_is_dir($path)) $result = @mkdir($CFG->dataroot . '/' . $path, 0777);
umask($oldMask);
return $result;
}
else {
$parts = explode('/', $path);
$pathTo = '';
for($i = 0; $i < count($parts) && $result; $i++){
$pathTo .= '/' . $parts[$i];
$result = filesystem_create_dir($pathTo, 0);
}
return $result;
}
}
/**
* tests if path is a dir. A simple wrapper to is_dir
* @param relativepath the path from dataroot
*/
function filesystem_is_dir($relativepath){
global $CFG;
if ($CFG->debug > 8) mtrace("is dir $relativepath
");
return is_dir($CFG->dataroot . '/' . $relativepath);
}
/**
* checks if file (or dir) exists. A simple wrapper to file_exists
* @param relativepath the path from dataroot
*/
function filesystem_file_exists($relativepath){
global $CFG;
if ($CFG->debug > 8) mtrace("file exists $relativepath
");
return file_exists($CFG->dataroot . '/' . $relativepath);
}
/**
* scans for entries within a directory
* @param relativepath the path from dataroot
* @param hiddens shows or hide hidden files
* @param what selects only dirs, files or both
* @return an array of entries wich are local names in path
*/
function filesystem_scan_dir($relativepath, $hiddens = 0, $what = 0){
global $CFG;
if ($CFG->debug > 8) mtrace("scanning $relativepath
");
$dir = opendir($CFG->dataroot . '/' . $relativepath);
$entries = array();
while ($anEntry = readdir($dir)){
if ($what == FS_ONLY_DIRS){
$subpath = $relativepath . '/' . $anEntry;
$subpath = preg_replace("/^\//", "", $subpath);
if (!filesystem_is_dir($subpath)) continue ;
}
if ($what == FS_NO_DIRS){
$subpath = $relativepath . '/' . $anEntry;
$subpath = preg_replace("/^\//", "", $subpath);
if (filesystem_is_dir($subpath)) continue ;
}
if ($hiddens) {
if (($anEntry != '.') && ($anEntry != '..')) $entries[] = $anEntry;
}
else {
if (!preg_match("/^\./", $anEntry)) $entries[] = $anEntry;
}
}
closedir($dir);
return $entries;
}
/**
* clears and removes an entire dir
* @param relativepath the path from dataroot
* @param fulldelete if true, removes the dir root either
* @return an array of entries wich are local names in path
*/
function filesystem_clear_dir($relativepath, $fullDelete = false) {
global $CFG;
if ($CFG->debug > 8) mtrace("clearing dir $relativepath
");
$exists = filesystem_is_dir($relativepath);
if (!$exists && !$fullDelete) {
return filesystem_create_dir($relativepath);
}
if (!$exists && $fullDelete) {
return true;
}
$files = filesystem_scan_dir($relativepath, FS_SHOW_HIDDEN, FS_ALL_ENTRIES);
foreach($files as $aFile) {
if ($aFile == "." || $aFile == "..") continue ;
if (filesystem_is_dir("{$relativepath}/{$aFile}")){
filesystem_clear_dir("{$relativepath}/{$aFile}", FS_FULL_DELETE);
// fs_removeDir("{$relativepath}/{$aFile}");
}
else
filesystem_delete_file("{$relativepath}/{$aFile}");
}
if (file_exists($CFG->dataroot . '/' . $relativepath) && $fullDelete) return filesystem_remove_dir($relativepath);
return false;
}
/**
* copies recursively a subtree from a location to another
* @param source the source path from dataroot
* @param dest the dest path from dataroot
* @return void
*/
function filesystem_copy_tree($source, $dest) {
global $CFG;
if ($CFG->debug > 8) mtrace("copying tree $source to $dest
");
if (file_exists($dest) && !filesystem_is_dir($dest)) {
return;
}
if (!filesystem_is_dir($dest)) {
filesystem_create_dir($dest, FS_RECURSIVE);
}
$files = array();
$files = filesystem_scan_dir( $source );
foreach($files as $aFile) {
if ($aFile == '.' || $aFile == '..') next;
if (filesystem_is_dir("{$source}/{$aFile}")) {
filesystem_create_dir("{$dest}/{$aFile}", FS_NON_RECURSIVE);
if (count(filesystem_is_dir("{$source}/{$aFile}")) != 0) {
filesystem_copy_tree("{$source}/{$aFile}", "{$dest}/{$aFile}");
}
}
else {
filesystem_copy_file("{$source}/{$aFile}", "{$dest}/{$aFile}");
}
}
}
/**
* stores a file content in the file system, creating on the way directories if needed
* @param relativepath the path from dataroot
* @param data the data to store in
*/
function filesystem_store_file($relativepath, $data) {
global $CFG;
if ($CFG->debug > 8) mtrace("storing $relativepath
");
$parts = pathinfo($relativepath);
if (!filesystem_is_dir($parts['dirname'])) filesystem_create_dir($parts['dirname']);
$FILE = fopen($CFG->dataroot . '/' . $relativepath, "w");
if (!$FILE) return false;
fwrite ($FILE, $data);
fclose($FILE);
return true;
}
/**
* reads a file content and returns scalar string
* @param relativepath the path from dataroot
* @return the data as a string
*/
function filesystem_read_a_file($relativepath) {
global $CFG;
if ($CFG->debug > 8) mtrace("reading $relativepath
");
$fullPath = $CFG->dataroot . '/' . $relativepath;
if (file_exists($fullPath)){
$FILE = file($fullPath);
return implode('', $FILE);
}
return false;
}
/**
* deletes a file. Simple wrapper to unlink
* @param relativepath the path from dataroot
* @return the data as a string
*/
function filesystem_delete_file($relativepath){
global $CFG;
if ($CFG->debug > 8) mtrace("deleting file $relativepath
");
if (filesystem_file_exists($relativepath) && !filesystem_is_dir($relativepath))
return unlink($CFG->dataroot . '/' . $relativepath);
return false;
}
/**
* removes an empty dir. Simple wrapper to rmdir
* @param relativepath the path from dataroot
*/
function filesystem_remove_dir($relativepath){
global $CFG;
if ($CFG->debug > 8) mtrace("deleting dir $relativepath
");
if (filesystem_file_exists($relativepath))
return rmdir($CFG->dataroot . '/' . $relativepath);
}
/**
* renames a file. Simple wrapper to rename
* @param relativepath the path from dataroot
*/
function filesystem_move_file($source,$dest){
global $CFG;
if (filesystem_file_exists($source)){
if ($CFG->debug > 8) mtrace("moving file/dir $source to $dest
");
return rename($CFG->dataroot . '/' . $source, $CFG->dataroot . '/' . $dest);
}
return false;
}
/**
* copy a file creating all path on the way if needed
* @param source the source path from dataroot
* @param dest the dest path from dataroot
*/
function filesystem_copy_file($source, $dest) {
global $CFG;
if ($CFG->debug > 8) mtrace("copying file $source to $dest
");
if (!filesystem_file_exists($source)) return -1;
$parts = pathinfo($dest);
if (!filesystem_is_dir($parts['dirname'])) filesystem_create_dir($parts['dirname']);
return copy($CFG->dataroot . '/' . $source, $CFG->dataroot . '/' . $dest);
}
/**
* gets a filtered list of files
* @param path the path from dataroot
* @param filemask the filemask for filtering
*/
function filesystem_get_file_list($path, $filemask = "*.*") {
global $CFG;
if (preg_match("/(.*)\/$/", $path, $matches)) $path = $matches[1];
$files = glob($CFG->dataroot . "{$path}/{$filemask}");
return $files;
}
/**
TODO should be recoded
function getFileIcon($mimetype){
switch($mimetype) {
case 'dir': return ''; break;
case 'application/x-word': return ''; break;
case 'application/x-excel': return ''; break;
case 'application/x-powerpoint': return ''; break;
case 'application/x-pdf': return ''; break;
case 'application/x-compressed': return ''; break;
case 'application/x-audio': return ''; break;
case 'application/x-photoshop': return ''; break;
case 'text/html': return ''; break;
case 'text/diml': return ''; break;
case 'text/raw': return ''; break;
case 'image/gif': return ''; break;
case 'image/jpg': return ''; break;
case 'image/png': return ''; break;
case 'image/bmp': return ''; break;
case 'application/binary': return ''; break;
default : return '';
}
}
function getMimeType($extension){
global $_MIMES;
if (!array_key_exists(strtoupper($extension), $_MIMES)) return '';
return $_MIMES[strtoupper($extension)];
}
*/
?>