<?php
error_reporting(0);
$k=trim($_GET['k']); include("key.php");
$d=rawurldecode(trim($_GET['d'])); $n=rawurldecode(trim($_GET['n']));
if ($d==NULL) {$d="";} else {if ($d=="/") {$d="";}}
if ($n==NULL) {$n=preg_replace("~.*/([^/]*)~m","\\1",$d);
$d=preg_replace("~(.*)/[^/]*~m","\\1",$d);}
$rd=rawurlencode($d); $rn=rawurlencode($n);
$d=str_replace(".|htaccess",".htaccess",$d);
$n=str_replace(".|htaccess",".htaccess",$n);
$d=str_replace('$','$$',$d); $n=str_replace('$','$$',$n);
$tp=trim($_GET['tp']); $nm=trim($_GET['nm']);
$ch=trim($_GET['ch']); $pn=trim($_GET['pn']);
if ($nm<>NULL) {
 $repl=array("\\"=>"","/"=>"",":"=>"","*"=>"","?"=>"","\""=>"","<"=>"",">"=>"",
 "|"=>"","`"=>""," "=>"_");
 $nm=trim(strtr($nm,$repl));
 include("repl.php"); $nm=u2t($nm);
 if (($nm==".")||($nm=="..")) {$nm="";}
}

if ((($tp=="f")||($tp=="d"))&&($nm<>NULL)) {
 if (($ftp=@ftp_connect($sr))&&(@ftp_login($ftp,$lg,$ps))) {
  @ftp_pasv($ftp,true); $fnm="";
  $d=str_replace('$$','$',$d); $n=str_replace('$$','$',$n);
  if ($tp=="f") {
   $sfnm="free";
   if ($pn==1) {$sfnm="php";}
   elseif ($pn==2) {$sfnm="php_wml";}
   elseif ($pn==3) {$sfnm="php_htm";}
   elseif ($pn==4) {$sfnm="wml";}
   elseif ($pn==5) {$sfnm="htm";}
   if (strlen($ch)<>3) {$ch="644";}
   @ftp_put($ftp,"$d/$n/$nm","patterns/".$sfnm.".ptn",FTP_BINARY);
   $cmd="chmod 0$ch $d/$n/$nm"; @ftp_site($ftp,$cmd);
   @ftp_close($ftp);
   header("Location: $dftp/ftp.php?k=$k&d=$rd&n=$rn"); exit;
  } elseif ($tp=="d") {
   if (strlen($ch)<>3) {$ch="755";}
   @ftp_mkdir($ftp,"$d/$n/$nm");
   $cmd="chmod 0$ch $d/$n/$nm"; @ftp_site($ftp,$cmd);
   @ftp_close($ftp);
   header("Location: $dftp/ftp.php?k=$k&d=$rd&n=$rn"); exit;
  } else {header("Location: $dftp/ftp.php?k=$k&d=$rd&n=$rn"); exit;}
 } else {
include "includes/header.php"; 
echo '<div class="box_1">
Нет соединения с сервером</div>
<div class="title_page"><a href="index.php">Главная</a></div>';
 }
} elseif ($tp=="f") {
$num=@file_get_contents("allnumbd.dat");
$num++; if ($num>9999999) {$num=0;}
$f=@fopen("allnumbd.dat","w"); @fwrite($f,$num); @fclose($f);
include "includes/header.php";

echo '
<div class="box_2">
Путь: <b><a href="ftp.php?k='.$k.'&amp;d='.$rd.'&amp;n='.$rn.'">'.$d.'/'.$n.'</a></b><br/>
</div>
<div class="box_1">
<form action="create.php" method="get">
<b>Имя:</b>:<br/>
<input name="nm" type="text" value="new.php" maxlength="250"/><br/>
<b>CHMOD</b>:
<input name="ch" type="text" value="755" size="3" maxlength="3" format="*N"/><br/>
<b>Шаблон:</b><br/>
<select name="pn" value="0">
<option value="0">НЕТ</option>
<option value="1">PHP</option>
<option value="2">PHP_wml</option>
<option value="3">PHP_html</option>
<option value="4">WML</option>
<option value="5">HTML</option>
</select>
<input type="hidden" name="k" value="'.$k.'"/>
<input type="hidden" name="d" value="'.$rd.'"/>
<input type="hidden" name="n" value="'.$rn.'"/>
<input type="hidden" name="tp" value="f"/>
<input type="submit" value="Создать"><br/>
</form>
</div>';

} elseif ($tp=="d") {
$num=@file_get_contents("allnumbd.dat");
$num++; if ($num>9999999) {$num=0;}
$f=@fopen("allnumbd.dat","w"); @fwrite($f,$num); @fclose($f);
include "includes/header.php";

echo '
<div class="box_2">
Путь: <b><a href="ftp.php?k='.$k.'&amp;d='.$rd.'&amp;n='.$rn.'">'.$d.'/'.$n.'</a></b><br/>
</div>
<div class="box_1">
<form action="create.php" method="get">
<b>Имя</b>:<br/>
<input name="nm" type="text" value="new" maxlength="250"/><br/>
<b>CHMOD</b>:
<input name="ch" type="text" value="755" size="3" maxlength="3" format="*N"/><br/>
<input type="hidden" name="k" value="'.$k.'"/>
<input type="hidden" name="d" value="'.$rd.'"/>
<input type="hidden" name="n" value="'.$rn.'"/>
<input type="hidden" name="tp" value="d"/>
<input type="submit" value="Создать"><br/>
</form>
</div>';
} else {
include "includes/header.php";
echo '<div class="title_page">Создать:</div>
<div class="box_2">Путь: <b><a href="ftp.php?k='.$k.'&amp;d='.$rd.'&amp;n='.$rn.'">'.$d.'/'.$n.'</a></b></div>
<div class="box_1">&raquo; <a href="create.php?k='.$k.'&amp;d='.$rd.'&amp;n='.$rn.'&amp;tp=f">Файл</a></div>
<div class="box_2">&raquo; <a href="create.php?k='.$k.'&amp;d='.$rd.'&amp;n='.$rn.'&amp;tp=d">Папку</a></div>';
}
include "includes/footer.php";
?>