<?php
ob_start();
$delete = $_GET['delete'];
$edit = $_GET['edit'];
$oldfile = $_POST['oldfile'];
if (isset($oldfile)){
    $newfilename = $_POST['upfilename'];
    $oldfilename = basename($oldfile);
    $pathway = str_replace($oldfilename,'',$oldfile);
    echo "Pathway is ".$pathway;
    $newfile = $pathway.$newfilename;
   if (rename($oldfile,$newfile)){
       echo "You successfully renamed ".$oldfile.' to '.$newfile.' . You are a winner with many friends!';
   }
        //str_replace($)../stanford/hugh/js/index.js
//get the new filename
//rename the old to the new
//tell our user what happened   
}


if(isset($edit)){
    $filename = basename($edit);
        //create a form
    //with text field 
    //put filename into value tag
    //put the path + filename in a hidden field
    ?>
<h3>You are editing: <?php echo $edit;?> </h3>
<form action = "cms.php" method="post">
Filename: <input type="text" name="upfilename" value="<?php echo $filename;?>"/>
<input type="hidden" name="oldfile" value="<?php echo $edit;?>"/>    
<input type="submit" name="submit" value="Change filename"/>    
</form>


<?php
}



if (isset($delete)){
    unlink($delete);
    echo "Record deleted!<br> ";
}
function readFolders($dir,$level=0){
    $level++;
    $indent = $level*20;
    
    $opendir = opendir($dir);//opens a folder for reading
$nonos = ['.','..','error_log','emails.txt'];
while ($file = readdir($opendir))
    { 
    $path = $dir.'/'.$file; 
    if (in_array($file,$nonos)) continue;
    //if something is a directory
    echo '<p style="margin-left:'.$indent.'px">';
    if (is_dir($path)){
     echo '<strong>'.$file.'</strong> <a href="">ZIP</a><br>';  
       readFolders($path,$level);        
    }
    else {
        if (getimagesize($path)){
            if (file_exists($path)){
     echo '<img src = '.$path.' height="50" />'."\n".'<br>'; 
            }
        // echo $file." is an image <br>";
            
        }
        else //not an image!!!!
        {
    echo '<a href="'.$file.'" target="blank">'.$file.'</a>  '.date('M d, Y',filemtime($path)).' <a href="cms.php?edit='.$path.'">EDIT</a> <a href="cms.php?delete='.$path.'" onclick="return confirm(\'Are you sure you want to delete '.$file.'?\');">DELETE</a>'."\n".'<br>';
        }
    }
    echo '</p>';
    }
}
readFolders('../stanford');




?>