<?php
   session_start();
   if(!isSet($_SESSION["kasutajanimi"])){
      header("Location: sisenemisvorm.php");
      exit();
   }
   include("fckeditor/fckeditor.php");   
   function tekstifaililoetelu(){
      $h = opendir("../lehesisud");
      $puhver=array();
      while (false!== ($failinimi=readdir($h)) ){ //Kui failinime lugemine õnnestus
       if($failinimi!="." and $failinimi!=".."){
        array_push($puhver, $failinimi);
       }
      }
      return $puhver;
   }
   function loeFail($failinimi){
     if(!preg_match("/^[a-z]+\.txt$/", $failinimi)){
        echo "Vigane failinimi";
        exit();
     }
     $f=fopen("../lehesisud/".$failinimi, "r");
     $t="";
     while($rida=fgets($f, 1024)){
        $t.=$rida;
     }
     fclose($f);
     return $t;
   }
   
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
                      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
     <title>Karu pirukapood - haldus</title>
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  </head>
  <body>
   <form action="salvestusleht.php" method="post">
    <?php if(!isSet($_REQUEST["muudetav"])){ ?>
     <div id="menyy">
        <div style="text-align: right;">
          <a href="lahku.php">lahku</a>
        </div>
        <h2>Tere tulemast, <?=$_SESSION["kasutajanimi"] ?></h2>
        <ul>
          <?php
           // echo "Tere, $_SERVER[PHP_AUTH_USER]";
            $loetelu=tekstifaililoetelu();
            foreach($loetelu as $failinimi){
              echo "<li><a href='haldusleht.php?muudetav=$failinimi'>$failinimi</a></li>\n";
            }
          ?>
        </ul>
     </div>
    <?php } ?>
    <div id="sisu">
        <?php
          if(isSet($_REQUEST["muudetav"])){
             echo "<h2>Muudetakse: $_REQUEST[muudetav]</h2>";
             echo "<input type='hidden' name='salvestusfail' 
                value='$_REQUEST[muudetav]' />";
             $r=new FCKeditor("failisisu");
             $r->BasePath="fckeditor/";
             $r->Value=loeFail($_REQUEST["muudetav"]);
             $r->Create();
             /*
             echo "<textarea name='failisisu' style='width: 50%; height: 300px'>";
             echo htmlspecialchars(loeFail($_REQUEST["muudetav"]));
             echo "</textarea> ";
             */
             echo "<br />
                 <input type='submit' name='salvestusnupp' value='salvesta' />
                 <input type='submit' name='katkestusnupp' value='katkesta' />
             ";
          }
        ?>    
    </div>
   </form>
  </body>
</html>