<?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);}
if ($n==NULL) {$n=preg_replace("~.*/([^/]*)~m","\\1",$d); $d=preg_replace("~(.*)/[^/]*~m","\\1",$d);}
$url=trim(stripslashes($_POST['url'])); if ($url=="<http://>") {$url=NULL;}
$rd=rawurlencode($d); $rn=rawurlencode($n);
$d=str_replace('$','$$',$d); $n=str_replace('$','$$',$n);
$d=str_replace(".|htaccess",".htaccess",$d);
$n=str_replace(".|htaccess",".htaccess",$n);

include "includes/header.php"; 

if ($url<>NULL) {
 if (($ftp=@ftp_connect($sr))&&(@ftp_login($ftp,$lg,$ps))) {
  @ftp_pasv($ftp,true);
  @preg_match_all("~<([^*>]*)[*]?([^*>]*)[*]?([acm]{1})?>~m",$url,$arr);
  if ($arr<>NULL) {include("repl.php");
   $cn=count($arr[0]); $str=""; $pp=""; $imp=0; $addl=0;
   if ($cn>25) {$cn=25;}
   for ($i=0;$i<$cn;$i++) {
    $curl=$arr[1][$i];
    if (strpos($curl,'"')!==false) {
     if (substr_count($curl,'"')==1) {
      $pp=preg_replace("~^([^\"]*).*~","\\1",$curl);
      if ($pp[strlen($pp)-1]=="/") {$pp[strlen($pp)-1]="";}
     }
     $curl=str_replace('"','',$curl);

    } elseif ($pp<>NULL) {
     if ((strlen($curl)<5)||(@substr($curl,0,5)<>"http:")) {
      if ($curl[0]=="/") {$curl=substr($curl,1);} $curl=trim($pp)."/".trim($curl);
     }
    }
    if ($arr[2][$i]==NULL) {
     $sv=preg_replace("~.*/([^/]*)~m","\\1",$curl);
     if ($sv==NULL) {$sv="index.wml";}
    } else {$sv=$arr[2][$i];}
    $repl=array("\\"=>"",":"=>"","*"=>"","?"=>"","\""=>"","<"=>"",">"=>"","|"=>"");
    $sv=trim(strtr($sv,$repl));
    $sv=u2t($sv);

    if ($n[strlen($n)-1]=="/") {$n[strlen($n)-1]="";}
    if ($sv[0]=="/") {$svf=$sv;} else {$svf=$d."/".$n."/".$sv;}
    if (strpos($curl,"http://")===0) {$fsz=NULL;
     $host=preg_replace("~.*://([^/]*).*~","\\1",$curl,1);
     $path=preg_replace("~.*://[^/]*(/.*)~","\\1",$curl,1);
     if (($host<>$curl)&&($path<>$curl)) {
      $f=@fsockopen($host,80,$e,$e,7);
      @fputs($f,"HEAD $path HTTP/1.1\r\nHost: $host\r\nConnection: close\r\nContent-Type: application/x-www-form-urlencoded\r\n\r\n");
      $header="";
      while (!feof($f)) {$header.=@fgets($f,1024);}
      @fclose($f);
      if (strpos($header,"Content-Length:")!==false) {
       eregi("Content-Length: ([0-9]*)",$header,$mss);
       $fsz=trim($mss[1]);
      }
     }
     if (($fsz==NULL)||($fsz<=5242880)) {
      if (copy($curl,"data/$k.bk")) {
       if (filesize("data/$k.bk")<=5242880) {
        if (ftp_put($ftp,$svf,"data/$k.bk",FTP_BINARY)) {
         $imp++;
         $ac=NULL;
         if ($arr[3][$i]<>NULL) {
          if ($arr[3][$i]=="m") {$ac="cut";}
          else if ($arr[3][$i]=="c") {$ac="copy";}
          else if ($arr[3][$i]=="a") {$ac="arh";}
         }
         if ($ac<>NULL) {$addl++; $str.="$svf|f|$ac\r\n";}
        }
       } else {$st.="Файл ".htmlspecialchars(str_replace('$','$$',$curl),ENT_QUOTES)." не закачан, размер более 5мб.<br/>\r\n";}
      } else {$st.="Файл ".htmlspecialchars(str_replace('$','$$',$curl),ENT_QUOTES)." не импортирован.<br/>\r\n";}
     } else {$st.="Файл ".htmlspecialchars(str_replace('$','$$',$curl),ENT_QUOTES)." не закачан, размер более 5мб.<br/>\r\n";}
    } else {$st.="Файл ".htmlspecialchars(str_replace('$','$$',$curl),ENT_QUOTES)." не импортирован.<br/>\r\n";}
    @unlink("data/$k.bk");
   }
   if ($str<>NULL) {
    $flist=@file("data/$k.act");
    if ($flist<>NULL) {
     for ($j=0;$j<count($flist);$j++) {if ($j==100) {break;} $str.=$flist[$j];}
    }
    $f=@fopen("data/$k.act","w"); @fwrite($f,$str); @fclose($f);
   }
   if ($cn<>NULL) {
    if ($st<>NULL) {$st.="\r\n";}
    $st.="Импортировано файлов: $imp/$cn\r\n";
    if ($addl<>NULL) {$st.="<br/>- - -<br/>Добавлено файлов в список: $addl<br/><a href=\"list.php?k=$k&amp;d=$rd&amp;n=$rn\">Список</a>\r\n";}
    $st.="<br/>- - -<br/>";
   }
  } else {$st="Ошибка импорта.";}
  @ftp_close($ftp);
$num=@file_get_contents("allnumbd.dat"); $nar=NULL;
$num++; $nar=$num; if ($num>99999999) {$num=0;}
$f=@fopen("allnumbd.dat","w"); @fwrite($f,$num); @fclose($f);
if ($pp<>NULL) {$purl="&lt;".htmlspecialchars(trim($pp))."&gt;";} else {$purl="&lt;http://&gt;";}

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">
'.$st.'
<form action="import.php?k='.$k.'&amp;d='.$rd.'&amp;n='.$rn.'" method="post">
URL:<br/><input name="url" type="text" value="'.$purl.'"/><br/>
<input type="submit" value="Импортировать"><br/>
</form>
</div>';

 } else {
echo '<div class="box_1">
Нет соединения с сервером</div>
<div class="title_page"><a href="index.php">Главная</a></div>';
 }
} else {
$num=@file_get_contents("allnumbd.dat"); $nar=NULL;
$num++; $nar=$num; if ($num>99999999) {$num=0;}
$f=@fopen("allnumbd.dat","w"); @fwrite($f,$num); @fclose($f);

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="import.php?k='.$k.'&amp;d='.$rd.'&amp;n='.$rn.'" method="post">
URL:<br/><input name="url" type="text" value="&lt;http://&gt;"/><br/>
<input type="submit" value="Импортировать"><br/>
</form>
</div>';
}
include "includes/footer.php";
?>