<?php
error_reporting(E_ALL);
ob_start();
$delete = $_GET['delete'];
$edit = $_GET['edit'];
$oldfile = $_POST['oldfile'];

$foldertouploadto = $_POST['foldertouploadto'];
if (isset($foldertouploadto)){
    $doc = $_SERVER['DOCUMENT_ROOT'];
    $uploadto = str_replace("./","",$foldertouploadto); 
    $name=$_FILES['uploadtofolder']['name'];
    $uppath = $doc.'/newsletter/'.$uploadto.'/'.$name; 
   $bool =  move_uploaded_file($_FILES['uploadtofolder']['tmp_name'], $uppath);
    

    if (!$bool){
        echo $_FILES['uploadtofolder']['error'];
    }
}
//      <input type="hidden" name="foldertouploadto" value="'.$path.'"/> 
//        <input type ="file" name="uploadtofolder">




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!';
   }//closes rename if
        //str_replace($)../stanford/hugh/js/index.js
//get the new filename
//rename the old to the new
//tell our user what happened   
}//closes lin


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)){
        //this is the folder condition!!!!
        echo '<strong>'.$file.'</strong> 
        <form action="cms.php" method="post" enctype="multipart/form-data">
        <input type="hidden" name="foldertouploadto" value="'.$path.'"/>    

        <input type ="file" name="uploadtofolder">
        <input type="submit" name="submit" value="Upload"/>
        
       </form><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!!!!
        {
            
            //lets out the file size
            
            
          if (mktime()- filemtime($path) > 86400 * 365 * 1){
           $old = '<span style="color:#ff0000">WARNING: OLD FILE!</span>';
          }
            
     $filesize = filesize($path);    
    
              
    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>'.$old." ".$filesize."\n".'<br>';
              unset($old); 
        }
    }
    echo '</p>';
    }
}
readFolders('.');




?>