<?php
	if ( !defined('IN_MFA') )
		die("Hacking attempt");
	
$user->DieNotValid();
if(!$user->has_right("edit")) message_die("No Access",$lang['norights']);

if(is_locked_for_me($user->user_root_dir.$_GET['object'])) message_die("Invalid Object","This file is changed by another user at the moment");

$validated = validateObject($user->user_root_dir.$_GET['object']);
if(gettype($validated) != "boolean") message_die("Invalid Object",$validated);

$display = new file_info($user->user_root_dir.$_GET['object']);
//ABSPEICHERN DES QUELLTEXTES
if(isSet($_POST['txtcoding']))
{ 
  $display->open("w");
  fwrite($display->file_resource, stripslashes($_POST['txtcoding']));
  $display->close();
  
  if(LOG_USED == 1)
  {
		$display->add_action("file changed");
		write_in_log("file '".ROOT_DIR."/".$display->file_path."' changed");
	}
	//datei wurde jetzt evtl mit Inhalt gefüllt
	$display = new file_info($user->user_root_dir."".$_GET['object']);
}

if($user->editor_setting == "codepress")
{	
	//activate codePress
	$show_after_header .= "<form action=\"index.php?dir=".$_GET['dir']."\" method=\"post\">
 	<strong><input type=submit value=\"".$lang['back']."\"></strong>
	</form>";
	$show_after_header .= "<button onclick=\"saveData()\"><strong>".$lang['save']."</strong></button><br>";
	$show_after_header .= "<code id=\"codepress\" title=\"".$_GET['object']."\" class=\"cp\" style=\"width:100%;height:700px\"></code>";
	$randnum = md5(uniqid(rand(), true));
	$show_after_header .="<script src=\"includes/codepress/codepress.js\" type=\"text/javascript\" id=\"cp-script\" lang=\"en-us\"></script>";
		$show_after_header.= "<p>Editor powered by <a class='small' href='http://codepress.fermads.net/'>http://codepress.fermads.net/</a></p>";

	$show_after_header .= "<form name =\"frmcoding\" action='index.php?dir=".$_GET['dir']."&mode=edit&object=".$_GET['object']."' method='POST'>";
	$show_after_header .= "<textarea id ='txtcoding' name='txtcoding' style='display:none;'>";
	$show_after_header .= "</textarea></form>";
}
else
{
  $show_after_header .= "<p>";
	$show_after_header .= "<form action=\"index.php?dir=".$_GET['dir']."\" method=\"post\">
 	<strong><input type=submit value=\"".$lang['back']."\"></strong>
	</form>";
  
  $display->open("r");
    
  $show_after_header .= "<form action='index.php?dir=".$_GET['dir']."&mode=edit&object=".$_GET['object']."' method='POST'>
   <input type='submit' name='subedit' value='".$lang['save']."'><br>";
  $show_after_header .= "<textarea name='txtcoding' style='width:100%;height:500px'>";
  
  $line = 1;
  if($display->file_size != 0)
  {
    while(! feof($display->file_resource))
    {
  		$content = fgets($display->file_resource, 1024);
  		$show_after_header .= htmlspecialchars($content);
  	
  		$line++;
  	}
  }
  $show_after_header .= "</textarea><br>
  <input type='submit' name='subedit' value='".$lang['save']."'>
  </form>";
  
  $display->close();
  $show_after_header .= "</p>";
}
?>