<?php
/**
 *	Fileman - Classe para manipulacao de arquivo e diretórios.
 *            Recomendada para edição de sites e para uso em webserver pessoal
 *  
 *  Autor: Lucas Brito Arruda (llbra) - http://llbra.tk -  [email protected]
 *
 *  $Id: llbra.php v 0.1.0 Initiated: ? Finished: ? Published: 05/02/2004 (mm/dd/yyyy) 
 *		 - Interface based on fileman.php (originaly by llbra) $
 */
/* USANDO O llbra LOGIN (3.0) PARA PROTEGER A INTERFACE E SÓ PERMITIR O ADMINISTRADOR 1 (ADMIN MASTER) ACESSAR
include "protect.php";
$nome = $_COOKIE['llbra_nome'];
include "usuarios/$nome.php";
if ($admin != 1){ exit("Acess Denied"); }
*/
function pegavar($varname,$METHOD="POST"){
	if ($METHOD == "POST"){
		$log = trim(htmlspecialchars($_POST[$varname]));	
	}else 
	if ($METHOD == "GET"){
		$log = trim(htmlspecialchars($_GET[$varname]));
	}else{
		$log = trim(htmlspecialchars($_REQUEST[$varname]));
	}
	return $log;		
}
// ROUTINE
$act = pegavar("action");
$file = pegavar("file");
$dir = pegavar("dir");
// EXCEPTIONS
if ($act == "f_write"){	
	if (!$file){ 
		$file = "$dir/";
	}
}
if ($act == "upload"){ $file = 1; }
if ($act == "f_dir"){ $file = $dir; }
		
// CHECK
if (!$act){ $stop .= "- Action is empty.<br>"; }
if (!$file){ $stop .= "- File has not been selected.<br>"; }
if (!$stop){
if ($act != "download" && $act != "nozipdl"){
echo "
<style>
textarea { width: 100%; height: 100%}
</style>";
}
	include "fileman.php";
	$man = new fileman();
	if ($act == "write"){
		
		$content = pegavar("f_content");
		$method  = pegavar("f_method");
		$line    = pegavar("f_line");
		$jump    = pegavar("f_jump");
		$f_file = pegavar("f_file");
		if($f_file != ""){ $file = $f_file; }
	
		$man->write_file($file,$content,$method,$line,$jump);
		echo "<a href=files.php>Back</a>";
		echo " - <a href=javascript:document.vi_form.submit()>Read</a>
		<form name=vi_form action='$PHP_SELF' method=post>
		<input type=hidden name=action value=s_read>
		<input type=hidden name=file value=$file></form>";
	
	}else
	if ($act == "del"){
	
		$line = pegavar("f_line");
		$all = pegavar("f_all");
		$man->del_file($file,$line,$all);
		echo "<a href=files.php>Back</a>";
		echo " - <a href=javascript:document.vi_form.submit()>Read</a>
		<form name=vi_form action='$PHP_SELF' method=post>
		<input type=hidden name=action value=s_read>
		<input type=hidden name=file value=$file></form>";
	}else
	if ($act == "read"){
		$start = pegavar("f_start");
		$end = pegavar("f_end");
		$key = pegavar("f_key");
		$ret = pegavar("f_ret");
		$high = pegavar("f_high");
		$url = pegavar("url");
		if($ret == 0){
			$man->read_file($file,$start,$end,$key,$ret,$high);
		}else{
			$return = $man->read_file($file,$start,$end,$key,$ret);
			echo "<form method=POST action=$url>";
			$ret = htmlspecialchars($return);
			$ret = nl2br($ret);								
			echo "<input type=hidden name=return value=\"$ret\">
			<input type=submit name=ok value=Enviar></form>";
			echo "<textarea name=return cols=100 rows=10>$return</textarea>";
		}
	}else
	if ($act == "s_read"){
		$man->read_file($file);
	
	}else
	if ($act == "find"){
		$content = pegavar("f_content");
		$pagin = pegavar("f_pagin");
		$ret = $man->find_file($file,stripslashes($content));
		
		$pagin = ereg_replace("X","$ret",$pagin);
		$pagin = ereg_replace("Y","'$content'",$pagin);
		echo $pagin;
		echo "<br><br>";
		echo "<a href=files.php>Back</a>";
	}else
	if ($act == "replace"){
		$content = pegavar("f_content");
		$line = pegavar("f_line");
		$man->replace_file($file,$content,$line);
		echo "<a href=files.php>Back</a>";
		echo " - <a href=javascript:document.vi_form.submit()>Read</a>
		<form name=vi_form action='$PHP_SELF' method=post>
		<input type=hidden name=action value=s_read>
		<input type=hidden name=file value=$file></form>";
	}else
	if ($act == "edit"){
		$content = pegavar("t_content");
		$f_file = pegavar("f_file");
		unlink($file);
		if ($f_file != ""){ $file = $f_file; }
		$man->write_file($file,$content,$method=0,$line="null",$jump=0);
		echo "<a href=files.php>Back</a>";
		echo " - <a href=javascript:document.vi_form.submit()>Read</a>
		<form name=vi_form action='$PHP_SELF' method=post>
		<input type=hidden name=action value=s_read>
		<input type=hidden name=file value=$file></form>";
	}else
	if ($act == "exe"){
		
		echo "<iframe id=ifr src=$file width=770 height=200></iframe>";
		echo "<br><br><a href='files.php'>Back</a>";
	}else
	if ($act == "download"){
		include "zip.lib.php";
		$zipfile = new zipfile();
		$data = $man->arr($file);
		$data = implode("",$data);
		
		$file = explode("/",$file);
		$file = end($file);
		$filename = explode(".",$file);
		$novalue = array_pop($filename);
		$filename = implode("_",$filename);
		$filename = $filename.".zip";
			header('Content-Type: application/x-zip');
			header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT');
			header("Content-Disposition: attachment; filename=".$filename);			
            header('Pragma: no-cache');
/*
			header('Content-Disposition: inline; filename="' . $filename . '"');
			header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
			header('Pragma: public');
*/
		$zipfile->addFile($data,$file);
		$data = $zipfile->file();
		echo $data;
		
	}else
	if ($act == "nozipdl"){	
		
		$data = $man->arr($file);
		$data = implode("",$data);
		
		$file = explode("/",$file);
		$file = end($file);
		/*
		$filename = explode(".",$file);
		$novalue = array_pop($filename);
		$filename = implode("_",$filename);
		*/
		$filename = $file;
			header('Content-Type: application/force-download');
			header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT');
			header("Content-Disposition: attachment; filename=".$filename);			
            header('Pragma: no-cache');
			
		echo $data;
	
	}else
	if ($act == "upload"){
	
		$dir = pegavar("dir");				
		
		/* SHOW CONTENT OF $_FILES
		
		foreach ($_FILES as $key1 => $value1) {
		   echo "\$_FILES[$key1] = $value1<br>\n";
		   foreach ($value1 as $key2 => $value2) {
			   echo "\$_FILES[$key1][$key2] = $value2<br>\n";
			   foreach($value2 as $key3 => $value3) {
				   echo "\$_FILES[$key1][$key2][$key3] = $value3<br>\n";
			   }
		   }
		}
		
		// OR
		echo "<pre>";
		print_r($_FILES);
		echo "</pre>";
		*/
		/* TESTING BAD CHAR REPLACEMENT - not implemented yet
		$badChar = array ("-"," ") ;
		$newChar = array ("_","_") ;
		*/
		
		for ( $i = 0 ; $i < count($_FILES['upfile']) ; $i++){
		/* REMOVING BAD CHARS - not implemented yet
			foreach ($badChar as $key => $oldChar){
						
				$_FILES['upfile']['name'] = ereg_replace($oldChar[$key], $newChar[$key], $_FILES['upfile']['name']);
			}	
		*/	
			if(!move_uploaded_file($_FILES['upfile']['tmp_name'][$i],$dir."/".$_FILES['upfile']['name'][$i])){
				$erro .= $FILES["upfile"]['name'][$i];
			}
			
		}
		if (!isset($erro)){
			echo "All uploads were sucesseful"; 
		}else{
			echo "The uploads below failed<br>$erro";
		}
		
		/*  FOR ONE FILE - just for example
		if(move_uploaded_file($_FILES['upfile']['tmp_name'],$dir."/".$_FILES['upfile']['name'])){
			echo "Upload sucessful<br><br>";
		}else{
			echo "Error during the upload process<br><br>";
		}
		
		echo "<a href=files.php>Back</a>";
		*/
	}else
	if ($act == "dir"){
		
		$action = pegavar("f_action");				
		if ($action == "make"){
		
			$name = pegavar("f_name_mk");
			$chmod = pegavar("f_chmod");			
			$name = "$file/$name";
			echo "<b>Dir: </b>".$name."<br>";			
			/*
			$brkName = explode("/",$name);
			for ( $x=0 ; $x<count($brkName) ; $x++){
				$fName .= $brkName[$x]."/";
				if (!is_dir($fName))
					if(!@mkdir($fName,$chmod))
						$erro .= "- $fName not created<br>";
					else
						$result .= "- Dir '$fName' created<br>";
			}
			$result = (!$result ? "Directory already exists!<br>" : $result) ;
			if (!$erro)
				echo "<b>Operation sucessful. Result:</b><br>".$result;
			else
				echo "<b>An error has occured during the process!</b><br>".$erro;
			
			*/
			function mkdirs($dirname) //mkdir
			{
			   $dir=split("/", $dirname);
			   for ($i=0;$i<count($dir);$i++)
			   {			   	   
				   $path.=$dir[$i]."/";
				   if (!is_dir($path))
				   @mkdir($path,0644);				   				   
				   @chmod($path,0644);
			   }
			   if (is_dir($dirname))
				   return 1;
			}
			
			echo mkdirs($name) ? "Operation sucessful. Directory created<br>" : "An error has ocurred.<br>";		
			echo "<a href=files.php>Back</a>";
									
		}else 
		if ($action == "del"){
		
			$name = pegavar("f_name_del");
			echo "<b>Dir: </b>".$name."<br>";
			function deldir($dir)
			{
			  $handle = opendir($dir);
			  while (false!==($FolderOrFile = readdir($handle)))
			  {
				 if($FolderOrFile != "." && $FolderOrFile != "..") 
				 {  
				   if(is_dir("$dir/$FolderOrFile")) 
				   { deldir("$dir/$FolderOrFile"); }  // recursive
				   else
				   { unlink("$dir/$FolderOrFile"); }
				 }  
			  }
			  closedir($handle);
			  if(rmdir($dir))
			  { $success = true; }
			  return $success;  
			}
			
			echo deldir($name)? "Operation sucessfull.<br>" : "An error has ocurred.<br>";	
			echo "<a href=files.php>Back</a>";
		
		}else
		if ($action == "rename"){
			
			$oldname = pegavar("f_name_old");
			$newname = pegavar("f_name_new");
			
			echo "<strong>Operation: </strong>".$oldname . " to " . $newname ."<br>" ;
			if (!@rename($oldname,$newname))
				echo "An error has ocurred during the rename.<br>";
			else
				echo "File/dir renamed.<br>";
			echo "<a href=files.php>Back</a>";
			
		}else{
			echo "Action unavaiable.<br>";
			echo "<a href=files.php>Back</a>";
		}			
		
	}else{
		echo "<form name=f_form id=f_form action='$PHP_SELF' method=post>
			<input type=hidden name=file value=$file>";
		if ($act == "f_write"){
			echo "<strong>WRITE</strong><BR><BR>";
			echo "<input type=hidden name=action value=write>";
			echo "<strong>File:</strong> $file<br>";
			if ($file == "$dir/"){ 
				echo "<strong>Define patch/file:</strong> <input type=text name=f_file value=$file><br>"; 
			}
			echo "<strong>Content:</strong><br>
			<textarea cols=100 rows=10 name=f_content></textarea><br>";
			echo "<strong>Method:</strong> ";
			echo "A+ (fim): <input type=radio name=f_method value=0 checked> 
			W+ (inicio): <input type=radio name=f_method value=1><br>";
			$lines = $man->arr($file,1);
			echo "<strong>Line to write: </strong>";
			echo "<select name=f_line>";
			echo "<option value=null>null</option>"; 
			for ($x=0;$x<$lines;$x++){ echo "<option value=$x>$x</option>"; }
			echo "</select><br>";
			echo "<strong>Jump:</strong> ";
			echo "Y: <input type=radio name=f_jump value=1> N: <input type=radio name=f_jump value=0 checked> <br>";
			echo "<strong>File content:</strong><br>";
			echo "<textarea name=t_content cols=100 rows=10>"
			.$man->read_file($file,$start=0,$end="null",$key=1,$ret=1,$high=0).
			"</textarea>";
	
		}else
		if ($act == "f_del"){
			echo "<input type=hidden name=action value=del>";
			echo "<strong>File:</strong> $file<br>";
			$lines = $man->arr($file,1);
			echo "<strong>Line to be deleted: </strong>";
			echo "<select name=f_line>";
			for ($x=0;$x<=$lines;$x++){ echo "<option value=$x>$x</option>"; }
			echo "</select><br>";
			echo "<strong>Delet hole file: </strong>";
			echo "Y <input type=radio name=f_all value=1> N <input type=radio name=f_all value=0 selected>";
			echo "<strong>File content:</strong><br>";
			echo "<textarea name=t_content cols=100 rows=10>"
			.$man->read_file($file,$start=0,$end="null",$key=1,$ret=1,$high=0).
			"</textarea>";			
	
		}else
		if ($act == "f_read"){
			echo "<strong>READ</strong> (You don´t need to set the values above, if you don´t want)<br><br><br>";
			echo "<input type=hidden name=action value=read>";
			echo "<strong>File:</strong> $file<br>";
			$lines = $man->arr($file,1);
			echo "<strong>Start line:</strong> ";
			echo "<select name=f_start>";
			for ($x=0;$x<$lines;$x++){ echo "<option value=$x>$x</option>"; }
			echo "</select><br>";
			echo "<strong>End line:</strong> ";
			echo "<select name=f_end>";
			echo "<option value=null>null</option>";
			for ($x=0;$x<$lines;$x++){ echo "<option value=$x>$x</option>"; }
			echo "</select><br>";
			echo "<strong>Line numbering:</strong> Y <input type=radio name=f_key value=1> 
			N <input type=radio name=f_key value=0 checked> (doesn´t work with highlight)<br>";
			echo '<strong>Return in var ($return):</strong> Y <input type=radio name=f_ret value=1> 
			N <input type=radio name=f_ret value=0 checked> - Url to send $return (post): 
			<input type=text name=url value=http://www.url.com/index.php><br>';
			echo '<strong>Highlight (php code):</strong> Y <input id=f_high1 type=radio name=f_high value=1> 
			N <input type=radio name=f_high value=0 checked> (format the rest with <pre>
			 - invalidate numbering if selected - doesn´t work whit return)<br>';
	
		}else
		if ($act == "f_find"){
			echo "<input type=hidden name=action value=find>";
			echo "<strong>FIND</strong><br><br>";
			echo "<strong>File: </strong>$file<br>";
			echo "<strong>Word: </strong>";
			echo "<input type=text name=f_content><br>";
			echo "<strong>Pagination : </strong>";
			echo "<input type=text name=f_pagin value=\"X result (s) for Y\"> 
			('X' will be replaced by the result number and 'Y' by the therm)";
	
		}else
		if ($act == "f_replace"){
			echo "<input type=hidden name=action value=replace>";
			echo "<strong>REPLACE</strong><br><br>";
			echo "<strong>Content:</strong><br>
			<textarea cols=100 rows=10 name=f_content></textarea><br>";
			echo "<strong>Line:</strong> ";
			echo "<select name=f_line>";
			$lines = $man->arr($file,1);
			for ($x=0;$x<$lines;$x++){ echo "<option value=$x>$x</option>"; }
			echo "</select><br>";
			echo "<strong>File content:</strong><br>";
			echo "<textarea name=t_content cols=100 rows=10>"
			.$man->read_file($file,$start=0,$end="null",$key=1,$ret=1,$high=0).
			"</textarea>";	
		}else
		if ($act == "f_edit"){
			echo "<strong>EDIT</strong><br><br>";
			echo "<input type=hidden name=action value=edit>";
			echo "<strong>File: </strong>$file<br>";
			echo "<strong>New patch/name:</strong>: <input type=text name=f_file value=$file>";
			echo "<textarea name=t_content cols=100 rows=30>"
			.$man->read_file($file,$start=0,$end="null",$key=0,$ret=1,$high=0).
			"</textarea>";
		}else
		if ($act == "f_dir"){
			echo "<strong>DIR</strong><br><br>";
			echo "<input type=hidden name=action value=dir>";
			echo "<strong>Dir: </strong>$file<br>";
			echo "<strong>Action: </strong><br>";
			echo "<input type=radio name='f_action' value=make> <strong>Make dir: </strong>
			Name <input type=text name='f_name_mk' value=$dir/> Chmod <input type=text name='f_chmod' value=0644><br>";
			echo "<input type=radio name='f_action' value=rename> <strong>Rename dir: </strong>
			Old Name <input type=text name='f_name_old' value=$dir> New name <input type=text name='f_name_new'><br>";
			echo "<input type=radio name=f_action value=del> <strong>Del dir: </strong>
			Name <input type=text name='f_name_del' value=$dir> - Caution! This will del all files inside.";
		}
		echo "<br><br><input type=submit name=submit value=ok>
			</form>";
	}
}else{
	echo "<strong>The following errors were found:</strong><br> $stop";
	echo "<br><br>";
	echo "<a href=files.php>Back</a>";
}
?> 
  |