<?php
//  次の行の""の間にパスワードを入力して保存してください
$pwd="";
//  このプログラム？の作者である三木邦弘は、これを利用した際にどのような
//  トラブルに巻き込まれても責任を負いません。ただ、これを他の人が利用したり、
//  勝手に配布したり、改良や改悪をするのは認めます。
$cpr="ver. 0.8 by K.Miki 2014/12/15";
//$cpr="ver. 0.7 by K.Miki 2012/12/12";
//$cpr="ver. 0.6 by K.Miki 2012/12/04";
//$cpr="ver. 0.5 by K.Miki 2012/11/30";
//$cpr="ver. 0.4 by K.Miki 2012/11/25";
//$cpr="ver. 0.3 by K.Miki 2012/03/25";
//$cpr="ver. 0.2 by K.Miki 2012/03/11";
//$cpr="ver. 0.1 by K.Miki 2012/03/10";
session_start();
function showmod($f){
   $perms=fileperms($f);
   if (($perms & 0xC000)==0xC000)     $info='s';    // ソケット
   elseif (($perms & 0xA000)==0xA000) $info='l';    // シンボリックリンク
   elseif (($perms & 0x8000)==0x8000) $info='-';    // 通常のファイル
   elseif (($perms & 0x6000)==0x6000) $info='b';    // ブロックスペシャルファイル
   elseif (($perms & 0x4000)==0x4000) $info='d';    // ディレクトリ
   elseif (($perms & 0x2000)==0x2000) $info='c';    // キャラクタスペシャルファイル
   elseif (($perms & 0x1000)==0x1000) $info='p';    // FIFO パイプ
   else                               $info='u';    // 不明
   $info .= (($perms & 0x0100) ? ' r' : ' -');
   $info .= (($perms & 0x0080) ? 'w' : '-');
   $info .= (($perms & 0x0040) ?
            (($perms & 0x0800) ? 's' : 'x' ) :
            (($perms & 0x0800) ? 'S' : '-'));
   $info .= (($perms & 0x0020) ? ' r' : ' -');
   $info .= (($perms & 0x0010) ? 'w' : '-');
   $info .= (($perms & 0x0008) ?
            (($perms & 0x0400) ? 's' : 'x' ) :
            (($perms & 0x0400) ? 'S' : '-'));
   $info .= (($perms & 0x0004) ? ' r' : ' -');
   $info .= (($perms & 0x0002) ? 'w' : '-');
   $info .= (($perms & 0x0001) ?
            (($perms & 0x0200) ? 't' : 'x' ) :
            (($perms & 0x0200) ? 'T' : '-'));
   return $info;
}
if ($_GET['mode']=="pass" && $_POST['pass']==$pwd) {
   $_SESSION['peditor']="OK";
}
if ($_GET['mode']=="quit") {
   session_unset();
   header("location: ".$_SERVER['SCRIPT_NAME']);
}
date_default_timezone_set ("Asia/Tokyo");
?>
<HTML>
<Head>
<META http-equiv=Content-Type content="text/html; charset=euc-jp">
<Title>Peditor (<?php if ($_GET['file']=="" || $_GET['mode']!="") echo "for PHP, by PHP"; else echo $_GET['file'];?>)</Title>
<Script language="JavaScript">
<?php echo "var pathp='",$_SERVER['SCRIPT_NAME'],"';\n"; ?>
function FileBtn(d){
   location.href=pathp+"?mode=file&dir="+d;
}

function DirOpenBtn(d,f){
   if (d=="./") d="";
   if (f=="../") {
      var i=j=0,k;
      f="";
      while ((k=d.indexOf('/',j+1))!=-1) {
         i=j;j=k;
      }
      if (i==0) d="./";
      else      d=d.substr(0,i+1);
   }
   location.href=pathp+"?mode=file&dir="+d+f;
}

function NewBtn(d){
   if (document.aaa.newname.value=="") 
      window.alert("新しく作成するファイル名を入力してから、新規作成ボタンをクリックしてください。");
   else
      location.href=pathp+"?file="+document.aaa.newname.value+"&dir="+d;
}

function NewDirBtn(d){
   if (document.aaa.newname.value=="") 
      window.alert("新しく作成するディレクトリ名を入力してから、新規作成ボタンをクリックしてください。");
   else
   location.href=pathp+"?mode=newdir&file="+document.aaa.newname.value+"&dir="+d;
}

function SaveBtn(x){
   document.aaa.action=pathp+"?"+x+document.aaa.fname.value;
   document.aaa.submit();
}

function TestBtn(x){
   var w=window.open(x,"PeditorTest");
   w.focus();
}

function OpenBtn(d,f){
   if (d=="./") d="";
   location.href=pathp+"?file="+f+"&dir="+d;
}

function DeleteBtn(d,f){
   if (d=="./") d="";
   if (window.confirm(d+f+"を削除して良いのですか？"))
      location.href=pathp+"?mode=delete&file="+f+"&dir="+d;
}

function DeleteDirBtn(d,f){
   if (d=="./") d="";
   if (window.confirm(d+f+"を削除して良いのですか？"))
      location.href=pathp+"?mode=deletedir&file="+f+"&dir="+d;
}

function OpenNewBtn(d){
   var w=window.open(pathp+"?mode=file&dir="+d,"","");

   w.focus();
}

var lastmode="";

function ModeBtn(d,f){
   var nmode=window.prompt("新しいモード (ex. rwxrw-r-x)",lastmode);
   var m=0;

   if (nmode==null) return;
   lastmode=nmode;
   if (nmode.length!=9) {
      window.alert(nmode+"は長さが変です。");
      ModeBtn(d,f);
      return;
   }
   if (nmode.substr(0,1)=='r') m=0400;
   else if (nmode.substr(0,1)!='-') {
      window.alert(nmode+"は最初の字が変です。");
      ModeBtn(d,f);
      return;
   }
   if (nmode.substr(1,1)=='w') m+=0200;
   else if (nmode.substr(1,1)!='-') {
      window.alert(nmode+"は2番目の字が変です。");
      ModeBtn(d,f);
      return;
   }
   if (nmode.substr(2,1)=='x') m+=0100;
   else if (nmode.substr(2,1)!='-') {
      window.alert(nmode+"は3番目の字が変です。");
      ModeBtn(d,f);
      return;
   }
   if (nmode.substr(3,1)=='r') m+=0040;
   else if (nmode.substr(3,1)!='-') {
      window.alert(nmode+"は4番目の字が変です。");
      ModeBtn(d,f);
      return;
   }
   if (nmode.substr(4,1)=='w') m+=0020;
   else if (nmode.substr(4,1)!='-') {
      window.alert(nmode+"は5番目の字が変です。");
      ModeBtn(d,f);
      return;
   }
   if (nmode.substr(5,1)=='x') m+=0010;
   else if (nmode.substr(5,1)!='-') {
      window.alert(nmode+"は6番目の字が変です。");
      ModeBtn(d,f);
      return;
   }
   if (nmode.substr(6,1)=='r') m+=0004;
   else if (nmode.substr(6,1)!='-') {
      window.alert(nmode+"は7番目の字が変です。");
      ModeBtn(d,f);
      return;
   }
   if (nmode.substr(7,1)=='w') m+=0002;
   else if (nmode.substr(7,1)!='-') {
      window.alert(nmode+"は8番目の字が変です。");
      ModeBtn(d,f);
      return;
   }
   if (nmode.substr(8,1)=='x') m+=0001;
   else if (nmode.substr(8,1)!='-') {
      window.alert(nmode+"は9番目の字が変です。");
      ModeBtn(d,f);
      return;
   }

   location.href=pathp+"?mode=chgmod&file="+f+"&dir="+d+"&new="+m;
}

function RenameBtn(d,f){
   var nname=window.prompt("新しいファイル名");
   if (nname!=null)
      location.href=pathp+"?mode=rename&file="+f+"&dir="+d+"&new="+nname;
}

function RenameDirBtn(d,f){
   var nname=window.prompt("新しいディレクトリ名");
   if (nname!=null)
      location.href=pathp+"?mode=rename&file="+f+"&dir="+d+"&new="+nname;
}

function QuitBtn(){
   location.href=pathp+"?mode=quit";
}

function upload(d){
   document.aaa.encoding="multipart/form-data";
   document.aaa.enctype="multipart/form-data";
   document.aaa.action=pathp+"?mode=upload&dir="+d;
   document.aaa.submit();
}

function GoBtn(){
   var elm=document.aaa.data;
   var buff=elm.value;
   var limit=buff.length;

   for (var j=document.aaa.goto.value-1,n=0;j>0;n++,j--) 
      while (n<limit && buff.charAt(n)!='\n') n++;
   elm.focus();
   if (elm.createTextRange) {
      var range = elm.createTextRange();
      range.move('character', n-document.aaa.goto.value+1);
      range.select();
   } else if (elm.setSelectionRange) {
     elm.setSelectionRange(n,n);
   }
}
</Script>
</Head>

<Body BGColor="#FFFFC0">
<?php
$file=$_GET['file'];
if ($pwd=="") {
   ?>
   <Script language="JavaScript">
   window.alert("パスワードが設定されていません。");
   </Script>
   <?php
   $sn=$_SERVER['SCRIPT_NAME'];
   $file=substr($sn,strrpos($sn,"/")+1);
} else if ($_SESSION['peditor']!="OK") {
   ?>
   <Form method="POST" action="<? echo $_SERVER['SCRIPT_NAME']; ?>?mode=pass" name="aaa">
   パスワード：<Input type="password" name="pass">　<Input type="submit" value="Start">
   </Form>
   <Script language="JavaScript">
      document.aaa.pass.focus();
   </Script>
   </Body>
   </HTML>
   <?php
   exit();
}
$dir=$_GET['dir'];
$mode=$_GET['mode'];
$flag=($mode=="file" || $mode=="delete" || $mode=="deletedir" ||
    $mode=="rename" || $mode=="newdir" || $mode=="chgmod" ||
    $mode=="upload" || $mode=="pass");
function showfpara($f){
   echo "<Td>",showmod($f),"</Td>";
   echo "<Td>",date ("Y/m/d H:i:s",filemtime($f)),"</Td>";
   echo "<Td align='right'>",number_format(filesize($f)),"</Td>";
}
?>
<Form name="aaa" method="POST">
<Table>
<Tr valign="top">
<Td><Input type="button" value="ファイル" onClick="FileBtn('<?php echo $dir; ?>')">　</Td>
<Td><Input name="fname" value="<?php echo $dir,$file; ?>">　</Td>
<?php if (!$flag) { ?>
<Td><Input type="button" value="テスト" onClick="TestBtn('<?php echo $dir,$file; ?>')">　</Td>
<Td><Input type="button" value="保存" onClick="SaveBtn('mode=save&dir=<?php echo $dir; ?>&file=')">　</Td>
<?php } ?>
<?php if (!$flag) { ?>
<Td>　<Input name="goto" size="3">　<Input type="button" value="Go" onClick="GoBtn()">　</Td>
<?php }?>
<Td>　<Input type="button" value="別Windowを開く" onClick="OpenNewBtn('<?php echo $dir; ?>')">　</Td>
<Td><B>peditor</B> <?php echo $cpr; ?>　</Td>
<Td><Input type="button" value="終了" onClick="QuitBtn()">　</Td>
</Tr>
</Table>
<?php
if ($flag) {
   if ($mode=="delete" && file_exists($dir.$file)) unlink($dir.$file);
   if ($mode=="deletedir" && file_exists($dir.$file)) 
      rmdir($dir.$file);
   if ($mode=="rename" && file_exists($dir.$file)) 
      rename($dir.$file,$dir.$_GET['new']);
   if ($mode=="newdir" && !file_exists($dir.$file)) {
      mkdir($dir.$file);
      chmod($dir.$file,0755);
   }
   if ($mode=="chgmod" && file_exists($dir.$file))
      chmod($dir.$file,$_GET['new']);
   if ($mode=="upload" && is_uploaded_file($_FILES['ufile']['tmp_name'])) {
      $name=$_FILES["ufile"]["name"];
      move_uploaded_file($_FILES["ufile"]["tmp_name"],$dir.$name);
      chmod($dir.$name,0644);
   }
   ?>
   <Br>
   <Table style="Background: white">
   <Tr style="background: aqua"><Td><Input name="newname"></Td>
   <Td colspan="2"><Input type="button" value="新規作成 (ファイル)" onClick="NewBtn('<?php echo $dir; ?>')"></Td>
   <Td colspan="6"><Input type="button" value="新規作成 (ディレクトリ)" onClick="NewDirBtn('<?php echo $dir; ?>')"></Td>
   </Tr>
   <Tr style="background: aqua">
   <Td colspan="2"><Input name="ufile" type="file"></Td><Td colspan="7">
   <Input type="button" value="ファイルをアップロード" onClick="upload('<?php echo $dir; ?>')">
   </Td></Tr>
   <?php
   if ($dir=="") $dir="./";
   $dh=opendir($dir);
   for ($n=0;($tmp=readdir($dh))!==false;) {
      if ($tmp==".." && $dir=="./") continue;
      $f[$n++]=$tmp;
   }
   for ($i=0;$i<$n;$i++)
      for ($j=$i+1;$j<$n;$j++)
         if ($f[$i]>$f[$j]) {
            $tmp=$f[$i];$f[$i]=$f[$j];$f[$j]=$tmp;
         }
   for ($i=0;$i<$n;$i++) {
      if ($i%2==0) echo "<Tr>";
      else         echo '<Tr style="background: aqua">';
      echo "<Td>$f[$i]</Td>";
      showfpara($dir.$f[$i]);
      if ($f[$i]==".") {
         echo "<Td></Td><Td></Td><Td></Td>";
         echo '<Td><Input type="button" value="モード変更" onClick="ModeBtn(',
              "'",$dir,"','",$f[$i],"')\"></Td>";
         echo "<Td></Td></Tr>\n";
      } else if (filetype($dir.$f[$i])=="dir") {
         echo '<Td><Input type="button" value="開く" onClick="DirOpenBtn(',
              "'",$dir,"','",$f[$i],"/')\"></Td>";
         echo "<Td></Td>";
         echo '<Td><Input type="button" value="名前変更" onClick="RenameBtn(',
              "'",$dir,"','",$f[$i],"')\"></Td>";
         echo '<Td><Input type="button" value="モード変更" onClick="ModeBtn(',
              "'",$dir,"','",$f[$i],"')\"></Td>";
         echo '<Td><Input type="button" value="削除" onClick="DeleteDirBtn(',
              "'",$dir,"','",$f[$i],"')\"></Td></Tr>\n";
      } else {
         echo '<Td><Input type="button" value="編集" onClick="OpenBtn(',
              "'",$dir,"','",$f[$i],"')\"></Td>";
         echo '<Td><Input type="button" value="テスト" onClick="TestBtn(',
              "'",$dir.$f[$i],"')\"></Td>";
         echo '<Td><Input type="button" value="名前変更" onClick="RenameBtn(',
              "'",$dir,"','",$f[$i],"')\"></Td>";
         echo '<Td><Input type="button" value="モード変更" onClick="ModeBtn(',
              "'",$dir,"','",$f[$i],"')\"></Td>";
         echo '<Td><Input type="button" value="削除" onClick="DeleteBtn(',
              "'",$dir,"','",$f[$i],"')\"></Td></Tr>\n";
      }
   }
   closedir($dh);
   ?>
   </Table>
   <Script language="JavaScript">
   document.aaa.fname.value="<?php echo $dir; ?>";
   </Script>
   <?php
} else if ($_GET['mode']=="save") {
   if (file_exists($dir.$file.".bak")) unlink($dir.$file.".bak");
   if (file_exists($dir.$file)) rename($dir.$file,$dir.$file.".bak");
//   $data=str_replace("\r\n","\n",stripslashes($_POST['data']));
$data=str_replace("\r\n","\n",$_POST['data']);
   $f=fopen($dir.$file,"w");
   fwrite($f,$data,strlen($data));
   fclose($f);
   chmod($dir.$file,0644);
   ?>
   <Script language="JavaScript">
   window.alert("ファイルを保存しました。");
   location.href=pathp+"?file=<?php echo $file; ?>&dir=<?php echo $dir; ?>";
   </Script>
   <?php
} else {
   echo '<Textarea rows="15" cols="80" name="data">';
      echo str_replace(array("&","<",">",'"'),array("&amp;","&lt;","&gt;","&quot;"),file_get_contents($dir.$file));
   if (file_exists($dir.$file)) 
      echo htmlspecialchars(file_get_contents($dir.$file));
   echo "</Textarea>\n";
   ?>
   </Form>
   <Script language="JavaScript">
   if (window.navigator.appName=="Microsoft Internet Explorer"){
      document.aaa.data.style.width=document.body.clientWidth-20;
      document.aaa.data.style.height=document.body.clientHeight-60;
   } else {
      document.aaa.data.style.width=window.innerWidth-40;
      document.aaa.data.style.height=window.innerHeight-60;
   }
   </Script>
<?php
}
?>
</Body>
</HTML>
                  