<?php
/**
 * FileSystem functions library
 *
 * @version:    0.1
 * @author:    alex 'alex_ez' yaroshevich <qfox@ya.ru>
 * @link:    http://src.qfox.ru/
 * @date:    17 nov 2007
 *
 */

class fs
{

static function 
ls$dir )
{
    
$dir str_replace'//''/'str_replace'\\''/'$dir.'/' ) );
    
$d dir$dir );
    
$files = array( 'file' => array( ), 'dir' => array( ) );
    
$path $d ->path;

    while( 
false !== ( $entry $d->read( ) ) )
    if( 
$entry != '.' && $entry != '..' )
    {
        
$file = array( );
        
$type filetype$path.$entry );
        
$file'name' ] = $entry;
        
$file'path' ] = realpath$path.$entry );
        
        
$ext substr$entrystrrpos$entry'.' ) + );
        
$file'ext' ] = $ext;
        
        
$_fstat stat$path.$entry );
        foreach( array( 
'size','atime','ctime','mtime' ) as $key )
            
$file$key ] = $_fstat$key ];
        
        
$files$type ][ ] = $file;
    }
    
    return 
$files;
}

static 
$___depth 0;
static 
$___ext2type = array(
    
'art' => 'wmc/articul',
    
'ind' => 'wmc/index',
    
'name' => 'wmc/names',
    
'struct' => 'wmc/struct',
    
'jpg' => 'image/jpeg',
    
'jpeg' => 'image/jpeg'
    
);
static function 
tree$dir$needContent false$ext null )
{
    
self::$___depth ++;
    
    
$_r = array( );
    
$files ls$dir );
    
//$_r[ '_' ] = array( 'count' => count( $files ), 'depth' => self::$___depth - 1 );
    
    
$dirs =& $files'dir' ];
    unset( 
$files'dir' ] );
    
    foreach( 
$dirs as $subdir )
    {
        
$_r$subdir'name' ] ] = filetree$subdir'path' ], $needContent$ext );
    }
    
    foreach( 
$files'file' ] as $file )
    {
        
$_pathinfo pathinfo$file'path' ] );
        
$_ext __$_pathinfo'extension' ], '' );
        if( !
is_null$ext ) && $_ext != $ext ) continue;
        
        
$file'ext' ] = $_ext;
        
        
$_type __self::$___ext2type$_ext ], 'text/unknown' );
        
$file'type' ] = $_type;
        list( 
$_typens$_typename ) = explode'/'$_type );
        
        
//if( $needContent && $_ext == 'ind' )
        
$_cont = ( $_ext == '' && $file['name'] == 'lists' ) ? parseListsFile$file['path'] ) : parseFile$_ext$file'path' ] );
        
$file'content' ] = ( $_typens == 'image' )
            ? 
"<img src='" str_replace( array( 'C:\\www\\ei\\''\\' ), '/'$file['path'] ) . "' />"
            
$_cont;
        
        
$_r$file'name' ] ] = $file;
    }
    
    
self::$___depth --;
    return 
$_r;
}

}

?>