Location: PHPKode > scripts > File Editor > file-editor-class.php
<?php 

class file_editor{


private $textarea_form;
private $accepted_ext=array();
private $textarea_style;
private $folder;
private $href_file;
private $get_var;
private $show_like;
private $style;
private $display_types = array("select", "link", "text_only", "list", "list_link");


private function is_empty_dir($dir){
//this function checks if the given folder exists.

    if ($dh = @opendir($dir))
    {
        closedir($dh);
        return true;
    }
    else return false; // no such dir, not a dir, not readable
}


private function show_files($name, $display_type){
//this function outputs the list of files in the folder "name" and the files will be listed like "display_type"
	$dir_handle = @opendir($name);
	$start = "";
	$finish = "";
	
	if ($display_type=="select"){
$start =<<<begin

<form name="class_file_editor" method="get" action="{$this -> href_file}">
<select {$this -> style} name="{$this -> get_var}">
<option value="" disabled="disabled" selected="selected">Folder: $name</option>

begin;

			//this gets the query string from url and transforms it in input fields
			parse_str($_SERVER["QUERY_STRING"],$variabileledinlink);
			$variabileurl="";$variabileform="";
			$startl=1;
			
			foreach($variabileledinlink as $key => $vdl){

			if($key== $this -> get_var){
					$variabileurl.="";
					}else{
					
					$startl == 1 ? $and="" : $and="&amp;";
					$variabileurl.=$and.$key.'='.$vdl;
					$variabileform.='<input type="hidden" name="'.$key.'" value="'.$vdl.'" />'."\n";
					
			}
			$startl=$startl+1;
			}
			//this gets the query string from url and transforms it in input fields
			
			

			$finish='</select>'."\n".$variabileform.'<input type="button" value="Edit" onclick="this.form.submit();"/>'."\n".'<input type="button" value="&lt;&lt;" onclick="window.location.href=\''.$this -> href_file .'\'"/></form>'."\n";
			$s = '<option value="';
			$f1 = '">';
			$f2 = "</option> "."\n";
	
	
	}
	
	if ($display_type=="text_only"){
		$start = '<p>';
		$finish= '</p>';
		
		$s='<span '.$this -> style .'>';
		$f1='</span> ';
		$f2="";
	}
	
	
	if ($display_type=="list"){
		$start = '<ul>';
		$finish= '</ul>';
		
		$s='<li '.$this -> style .'>';
		$f1='</li> ';
		$f2="";
	}
	
	if ($display_type=="list_link"){
		$start = '<ul>';
		$finish= '</ul>';
		
		if(strpos($this -> href_file , "?")){$delimiter="&amp;";
				}else{
				$delimiter="?";
			}
		
		
		$s='<li><a '.$this -> style .' href="'.$this -> href_file .$delimiter.$this -> get_var .'=';
		$f1='">';
		$f2='</a></li> ';
	}
	
	if ($display_type=="link"){
		$start = '<p>';
		$finish= '</p>';
		
		if(strpos($this -> href_file , "?")){$delimiter="&amp;";
				}else{
					$delimiter="?";
			}
		
		
		
		$s='<a '.$this -> style .' href="'.$this -> href_file .$delimiter.$this -> get_var .'=';
		$f1='">';
		$f2='</a> ';
	}
		
	$files = $start;
	while ($file = readdir($dir_handle)) {
	
		if($file == "." || $file == ".." || $file == "index.php" || $this -> is_empty_dir($this -> folder .'/'.$file) || $this -> check_ext($file) == FALSE )
			{continue;}
		
		
		
		if(strstr($display_type, "link") || strstr($display_type, "select")){
				$files .= $s.$file.$f1.$file.$f2;
				}else{
				$files .= $s.$file.$f1.$f2;
		}
	}
		
	closedir($dir_handle); 
	$files .= $finish;

	return $files;
}





private function show_error($err){
//this function outputs error messages

$errors = array("0" => "Nu exista directorul: ".$this -> folder ,//This folder doesn't exist: 
				"1" => "Fisierul nu a fost gasit.", //The file wasn't found
				"2" => "Alege un fisier! Extensii acceptate: ".implode(", ",$this -> accepted_ext), //Choose a file Accepted extensions
				"3" => "Acesta este un director, nu un fisier.", //This is a folder, not a file.
				"4" => "Fisierele cu aceasta extensie au fost restrictionate."); //This type of file has been restricted

if (array_key_exists($err,$errors)){
	$show = $errors[$err];
	}else{
	$show = "Eroare necunoscuta."; //Unknown error.
}
		
$err = '<div style="color: #000000;font-weight:bold;background-color: #EBEBEB;font-family:Arial; font-size:10px;border: 4px solid;margin: 10px 0px;padding:10px 10px 10px 10px;">'.$show.'</div>';

return $err;
}



public function only_these_ext($ext_array){
//this function sets accepted file extensions - * for ALL
if(empty($ext_array)){
			$this -> accepted_ext = array("html", "htm");
		}else{
			$this -> accepted_ext = $ext_array;
}

}



public function check_ext($file){
//this function checks if the extensions of the file is accepted
$file_part=pathinfo($file);
$ext=$file_part['extension'];

if (empty($this -> accepted_ext)){
		$this -> accepted_ext = array("html", "htm");
}


if(in_array("*", $this -> accepted_ext)){
		return TRUE;
		}else{

		if ( in_array($ext, $this -> accepted_ext)){

				return TRUE;
				}else{
				
				return FALSE;
		}
}

}




public function set_style($style_format, $href, $href_var){
/*
this function sets css style for the list of files 

EX.:
style_format= array("style" => "color: blue; font-size:12px;"); other attributes can be used ... onmouseover, onclick, class etc.

href = "some-page.php?action=file_editor"   - the page where this class will be used including the query if it exists
or

href= "file-edit.php"

href_var = "filename" - the variable that will carry the filename
*/

	$this -> href_file= $href;
	$this -> get_var= $href_var;

	foreach ($style_format as $attr => $value){
		$this -> style .=" ".$attr." = ".'"'.$value.'"';
	}
}


private function show_textarea(){
//this function outputs the form and textarea
if(!isset($_GET[$this -> get_var])){$filename="";}else{$filename=$_GET[$this -> get_var];}

if($filename!="" && file_exists($this -> folder .'/'.$filename)) {


		if ($this -> is_empty_dir($this -> folder .'/'.$filename)){
				return $this -> show_error("3");
				
				}else{
		
				if ($this -> check_ext($filename)){
				
				
					$file_content=file_get_contents($this -> folder .'/'.$filename);
					$file_content= $this -> replace_html($file_content, TRUE);
					
					$show_textarea='<form '.$this -> textarea_form .' method="post">'."\n".'<input type="text" size="'.strlen($filename).'" name="filename" value="'.$filename.'" /><br/>'."\n".'<textarea '.$this -> textarea_style .'>'."\n".$file_content."\n".'</textarea><br/><input type="submit" name="submit" onclick="return confirm(\'Sigur vrei sa salvezi?\');"/></form>'."\n";

					return $show_textarea;
					}else{
					return $this -> show_error("4");
		
				}
		
				
		}
}
elseif($filename==""){return $this -> show_error("2");}
	else{
	return $this -> show_error("1");

}


}



public function replace_html($string, $true){
//this function replace < and > for XHTML validation 

// array cu caracterele de inlocuit
		$search 	= array("<",">");
// array cu caracterele cu care se inlocuiesc caracterele de mai sus
		$replace 	= array("&lt;","&gt;");
if ($true==TRUE){		
		$clean=str_replace($search, $replace, $string);
		}else{
		$clean=str_replace($replace, $search, $string);
}

return $clean;		
}









public function textarea_settings($form, $style, $output_it){
/*

form=array("name"=>"formname", "action" => "do-this.php?action=save_file"); textarea form settings
style=array("name" => "textare_name", "rows" => "20", "cols"=>"85"); textarea settings
output_it=TRUE; this needs to be true in order to output it

*/
	
	foreach ($form as $attr => $value){
			$this -> textarea_form .=" ".$attr." = ".'"'.$value.'"';
		}

	foreach ($style as $attr => $value){
			$this -> textarea_style .=" ".$attr." = ".'"'.$value.'"';
		}

	if ($output_it== TRUE) {return $this -> show_textarea();}
		
}



public function set_folder($name, $display_type){
//this function sets the way the files of the folder named "name" are listed
// display_type can be select, text_only, link, list, list_link 	

	
	$this -> folder = $name;
	
	if (in_array($display_type, $this -> display_types)){
				$this -> show_like = $display_type;
				}else{
				$this -> show_like = "text_only";
	}
		
	if ($this -> is_empty_dir($this -> folder)){
		return $this -> show_files($this -> folder, $this -> show_like); // list files	
		}else{
		return $this -> show_error("0"); //error - no such folder (directory)	
		
	}
		
}






}
?>
Return current item: File Editor