session_write_close();
require_once("common.php");
require_once("loggedinuser.php");
require_once("contenthandler.php");
session_start();
if (isset($_GET['file'])) {
  $targetFile = $_GET['file'];
  if (!isVarGood($targetFile, false)) {
    die("Form variables are incomplete and/or invalid!");
  }
  $targetFile = str_replace("\\", "", $targetFile);
  $targetFile = str_replace("..", "", $targetFile);
  global $CMS_CONTENT_FS_PATH;
  if (!is_readable($CMS_CONTENT_FS_PATH . $targetFile)) {
    die("Cannot read file " . $targetFile);
  }
  $hFile = fopen($CMS_CONTENT_FS_PATH . $targetFile, "r");
  $content = fread($hFile, filesize($CMS_CONTENT_FS_PATH . $targetFile));
  fclose($hFile);
  $beginPos = strpos($content, "");
  $endPos = strpos($content, "");
  if (($beginPos === false) || ($endPos === false)) {
    die("Cannot read file " . $targetFile);
  } else
    $beginPos += 19;
  $contentLen = $endPos - $beginPos;
  $content = dehtmlize(substr($content, $beginPos, $contentLen));
?>
   } else if (isset($_POST['content'])) {
     $deleteme = $_POST['deleteme'];
     $content = $_POST['content'];
     $file = $_POST['file'];
     if (!isVarGood($content, false) || !isVarGood($file, false) ||
        !isVarGood($deleteme, false)) {
        die("Form variables incomplete and/or invalid!");
     }
     $ip = null;
     if (isset($_SESSION['LoggedInUser'])) {
        $loggedInUser = $_SESSION['LoggedInUser'];
        $ip = $loggedInUser->getCurrentIP();
     } else {
        $loggedInUser = null;
        $ip = $_SERVER['REMOTE_ADDR'];
     }
                                                                                
     if ($deleteme === "1") {
        global $CMS_CONTENT_URL_PATH;
        if ($loggedInUser == null) {
           die("What, you think you can delete this file even though you're not logged in?  Pssh!");
        }
        require_once("contenthandler.php");
        ContentHandler::deleteFile($loggedInUser, $file);
        print "File deleted successfully.  Back to Litgloss.";
     } else {
        $ip = null;
        if (isset($_SESSION['LoggedInUser'])) {
           $loggedInUser = $_SESSION['LoggedInUser'];
           $ip = $loggedInUser->getCurrentIP();
        } else {
           $loggedInUser = null;
           $ip = $_SERVER['REMOTE_ADDR'];
        }
        require_once("contenthandler.php");
        $ret = ContentHandler::submitEdit($loggedInUser, $ip, $file, $content);
        if ($ret == ContentHandler::$CONTENT_POSTED)
          echo "Changes have been successfully applied to the page.";
        else if ($ret == ContentHandler::$CONTENT_QUEUED)
          echo "Changes have been queued for approval by an administrator.";
        else if ($ret == ContentHandler::$CONTENT_FAILURE)
          echo "Editing of content has FAILED!";
      }
   }
?>