<?php

    //TODO: 
    // sort by file name, date, size
    // rename, copy, move, delete
    // logon id
    
    
    // Username/Password:
    
    //    Username: mp1
    //    Password: Tuesday56
        

	include "/home/academi1/public_html/library.php";

//	include "/home/academi1/public_html/common_code/common_library.txt";


        // code to implement a file-manager HTML screen functions


        // click on a file to download it, except .php files which would be executed
        
        // click on 'Up one level' to go to a folder higher up the directory tree. Doesn't go above the domain name root folder
        
        // click on 'Select File' and 'Upload' to upload a new file. Doesn't overwrite existing files.
        
        
        // NOTES: a URL parameter such as current_folder needs to be encoded with urlencode but a <a href='' value should not be encoded
        

    session_start();

    
    echo "<style>";
    
	write_common_styles();

    echo "</style>";
    
    

        
        //  /home/academi1/public_html/melbpara
        
    $pathroot = val( $_GET, "pathroot" );

    if ($pathroot != "")
        $_SESSION['pathroot'] = $pathroot;
    else
        $pathroot = val( $_SESSION, "pathroot" );

    if ($pathroot == "")
        $pathroot = "/home/academi1/public_html/melbournepara/server1";
        
        
        
        //  /home/academi1/public_html/melbpara/MP Server
        
    $dont_go_above = val( $_GET, "dont_go_above" );

    if ($dont_go_above != "")
        $_SESSION['dont_go_above'] = $dont_go_above;
    else
        $dont_go_above = val( $_SESSION, "dont_go_above" );

    if ($dont_go_above == "")
        $dont_go_above = "/home/academi1/public_html/melbournepara/server1/MP Server";


        // https://melbpara.com.au

    $home_url = val( $_GET, "home_url" );

    if ($home_url != "")
        $_SESSION['home_url'] = $home_url;
    else
        $home_url = val( $_SESSION, "home_url" );

    if ($home_url == "")
        $home_url = "https://server1.melbournepara.com";
        

//echo "pathroot: ".$pathroot."<br>";
//echo "dont_go_above: ".$dont_go_above."<br>";
//echo "home_url: ".$home_url."<br>";


    $current_folder = val( $_GET, "current_folder" );


    if ($current_folder == "")
        $current_folder = $dont_go_above;


    $_SESSION['current_folder'] = $current_folder;

//echo "current_folder: ".$current_folder."<br>";
    
    
    $files = glob( $current_folder."/*" );


    $folder_above = substr( $current_folder, 0, strrpos( $current_folder, "/" ));


//echo "<br>Current folder: ".$current_folder."<br>";
//echo "<br>Folder above: ".$folder_above."<br>";


    echo "<br>";
    
    if ($current_folder == $pathroot)
        $current_folder_display = "/";
    else
        $current_folder_display = substr( $current_folder, strlen($pathroot)+1, strlen($current_folder) - strlen($pathroot) - 1 );


    echo "<table width='100%'><tr><td width='80%'>";

    echo "<span style='font-family: verdana; color: #00a000; font-size:18pt'>Current Folder: ".$current_folder_display."</span>";
    
    echo "</td><td>";
    
	show_green_button( "Home", 80, "/folder_view.php", "current_folder", $dont_go_above );

    echo "</td></tr></table>";

    
    echo "<br><br><br>";
    

        // file upload function
        
	echo "<form name='form' enctype='multipart/form-data' action='".url_encode( "folder_view_upload_go.php" )."' method='POST'>";

	echo black( "Select file to upload&nbsp;&nbsp;" )."<input type='file' name='file' id='file' size='60'>";

	show_green_button_submit( "Upload", 80 );



        // folders

    echo "<br><br><br><br>";


        // don't allow browsing above the domain name root, this would work but the files cannot be downloaded as URL's
        
    if ($current_folder != $dont_go_above)
    {
        $new_folder = urlencode( $folder_above );

        echo "<a href='folder_view.php?current_folder=".$new_folder."' style='text-decoration: none; font-family: verdana; font-weight: bold; font-size: 10pt; color: #cf0000;'>Up one level</a><br>";
    }
    

    $found_one_folder = false;
    
    for ($i=0; $i < count($files); $i++)
    {
        if (is_dir( $files[$i] ))
            $found_one_folder = true;
    }
    
    if ($found_one_folder)
    {
        echo "<br><br>";
        echo "<span style='text-decoration: none; font-family: verdana; font-weight: bold; font-size: 10pt; font-style: italic; color: #8B4513;'>Folders</span><br><br>";
    }

    echo "<table>";
    
    $found_one_file = false;
    
    for ($i=0; $i < count($files); $i++)
    {
        if (is_dir( $files[$i] ))
        {
            $filename = substr( $files[$i], strlen( $current_folder )+1, strlen($files[$i]) - strlen( $current_folder ) - 1 );
            
            $new_folder = urlencode( $files[$i] );

            echo "<tr><td><a href='folder_view.php?current_folder=".$new_folder."' style='text-decoration: none; font-family: verdana; font-size: 10pt; color: #8B4513;'>".$filename."</a></td><td></td><td></td><td></td></tr>";
        }
        else
            $found_one_file = true;
    }


    echo "<tr><td>&nbsp;</td></tr>";
    echo "<tr><td>&nbsp;</td></tr>";


    if ($found_one_file)
    {
        echo "<tr><td style='text-decoration: none; font-family: verdana; font-weight: bold; font-style: italic; font-size: 10pt; color: #cf0000;'>Files</td></tr>";
    
        echo "<tr><td>&nbsp;</td></tr>";
    
    
        for ($i=0; $i < count($files); $i++)
        {
            if (! is_dir( $files[$i] ))
            {
                $filename = substr( $files[$i], strlen( $current_folder )+1, strlen($files[$i]) - strlen( $current_folder ) - 1 );
                
    //            echo "<span style='font-family: verdana; font-size: 10pt; color: #cf0000;'>".$filename."</span><br>";
    
                    // don't let the user accidently run php files by clicking on them
                    

                $filename1 = "/".substr( $files[$i], strlen($pathroot)+1, strlen($files[$i]) - strlen($pathroot)-1 );

                if (strpos( $files[$i], ".php" ) === false)            
                    echo "<tr><td><a href=\"".$home_url.$filename1."\" style='text-decoration: none; font-family: verdana; font-size: 10pt; color: #cf0000;'>".$filename."</a><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td><td align='right' style='font-family: verdana; font-size: 10pt; color: #cf0000;'>".date( "j M Y H:i:s", filemtime( $files[$i] ) )."</td><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td><td align='right' style='font-family: verdana; font-size: 10pt; color: #cf0000;'>".number_format( filesize( $files[$i] ) )."</td></tr>";
                else
                    echo "<tr><td><span style='text-decoration: none; font-style: italic; font-family: verdana; font-size: 10pt; color: #cf0000;'>".$filename."</span><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td><td align='right' style='font-family: verdana; font-size: 10pt; color: #cf0000;'>".date( "j M Y H:i:s", filemtime( $files[$i] ) )."</td><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td><td align='right' style='font-family: verdana; font-size: 10pt; color: #cf0000;'>".number_format( filesize( $files[$i] ) )."</td></tr>";
            }
        }
    }
    
    
    echo "</table>";

	echo "</form>";
	
	if ((! $found_one_folder) && (! $found_one_file))
        echo "<span style='text-decoration: none; font-family: verdana; font-weight: bold; font-size: 10pt; font-style: italic; color: #8B4513;'>No folders or files</span><br><br>";
    
?>
