<?php
//#******************************************************************************************************************************
if (isset ($_REQUEST['mt']) or isset ($_REQUEST['sz']))    //
//******************************************************************************************************************************
{$net=false;                              //
 $nus=" ".$_REQUEST['htm'];              // 
 if (isset ($_REQUEST['mt']))
  {$zel="apx"; 
   $zel2="In the 'archive' ";
   $zel3="archive";
}                                        // 
  else
  {$zel="tmp";
   $zel2="In 'my' ";
   $zel3="my";
}


$net=false;                              // 

// 
$ll = file($_frhtm);			         // 
foreach ($ll as $lli => $llin)           // 
{if (strpos($nus,",u".$lli.",")>0)       // 
{


if (strpos($llin,'eg_a(')>0)           //
{
$kudap1=getcwd ( ).'/pages/'.$nam1.'/contents/01,01,01_'.$zel.'/y/';
if (file_exists($kudap1))                //
{$os=$os. '<b style="color:red">'.$zel2.' there is already a diary! <br> The addition is canceled!
To add rename '. $zel3.' , create a new '. $zel3.' and repeat!</b><br>';
$net=true;
}
}
if (strpos($llin,"zk_a2(")>0)           //
{
$kudap1=getcwd ( ).'/pages/'.$nam1.'/contents/01,01,01_'.$zel.'/n/';
if (file_exists($kudap1))                //
{$os=$os. '<b style="color:red">'.$zel2.' already have a notebook!<br> The addition is canceled!
To add rename '. $zel3.' , create a new '. $zel3.' and repeat!</b><br>';
$net=true;
}}
}}                                      // 







//**************************************************
if ($net)           //
{}                  //
else
{//
  $kuda=getcwd ( ).'/pages/'.$nam1.'/contents/01,01,01_'.$zel.'/01,01,01_'.$zel.'/'.$zel.'.htm';
  if (file_exists($kuda))                 //
  {//
   $otkuda=$_frhtm;
   list($s1, $old1) = explode("/pages/",$otkuda);     
   list($s2, $new1) = explode("/pages/",$kuda);     
   $imnew = '01,01,01_'.$zel;     
   $l = file($kuda); 					   //
   $ww = fopen($kuda, 'w') or exit;        //
   $i=-1; 							       //
   $ii=-1; 							       //
   foreach ($l as $li => $lin)             //
   {fwrite($ww,$lin);++$i;}
   $sm="/".$nam1."/".$nam1."/";            //
   $ff=$otkuda;                            //
   $ll = file($ff);					       //
   if ($zel=="apx")                     //
   {$w5 = fopen($ff, 'w') or exit;}  	   //
   $mpic2="";
   foreach ($ll as $lli => $llin)           //
   {if (strpos($nus,",u".$lli.",")>0)       //
	{$sw=$llin;
	 $poz=strpos($sw,">");				    //
     $tag=explode(" ",substr($sw,0,$poz));  //
     $tagw="";						        //
     foreach ($tag as $k => $z)             //
     {if (substr($z,0,6)=="title=")         //
      {$tagw=$tagw.'title="'.(++$i).'" ';
	  }
	  else
	  {$tagw=$tagw.$z." ";}
      } 
      $sw=str_replace($old1,$new1,$sw);
      $sw1=$sw;                                 //
      $mpic=spic($sw1); 	    				//
	  $mpic2=$mpic2.$mpic;
	  if  ($mpic=='#')	                        //
	  {}
	  else
      {$nmm="/".$nam1."/"."contents/01,01,01_".$zel."/01,01,01_".$zel."/";  //
       $sw=str_replace($sm,$nmm,$sw);                                       //
	  }
$sw=str_replace($nam1."/y/eg.html","y/eg.html",$sw);
$sw=str_replace($nam1."/n/zk.html","n/zk.html",$sw);
fwrite($ww,$tagw.substr($sw,$poz)); //
//****************************************************************************************
// 
$ipap=="";
//
if (strpos($sw,' id=zk ')>0 ) 
{$ipap="n";}
//
if (strpos($sw,' id=eg ')>0 ) 
{$ipap="y";}
//
if (strpos($sw,' name=C')>0 ) 
{$ipap="tc";}
//
if (strpos($sw,' name=G')>0 ) 
{$ipap="tg";}
//
if (strpos($sw,' name=Q')>0 ) 
{$ipap="tq";}
//
if (strpos($sw,' name=V')>0 ) 
{$ipap="tv";}
//
if ($ipap!="" )                       //
{$otkudap1=$_pohtm."/".$ipap;
if  (file_exists($otkudap1))
{$kudap1=getcwd ( ).'/pages/'.$nam1.'/contents/01,01,01_'.$zel."/".$ipap;

if (file_exists($kudap1))            // 
{}
else                                  //
{mkdir($kudap1);}

if  (file_exists($otkudap1))
{$fes =scandir($otkudap1, 1);		  //
 foreach ($fes as & $val) 			  //
  {if ($val != "." & $val != "..") 	  //
   {if  (!strstr($val, '.'))          //
   {}
   else
{if (copy($otkudap1."/".$val,$kudap1."/".$val)) 
{
$fkor=$kudap1."/".$val;
$ksod=file_get_contents($fkor);                             //
$ksod=str_replace("../d~e~f","../../../../d~e~f",$ksod);    //
$w = fopen($fkor, 'w') or exit;      	                    //
fwrite($w,$ksod);                                           //
fclose($w);
}
else
{$os=$os. "<b>Copy error ".$z."</b>";}
}}}
}}}


//echo $kudap1."/".$val."<hr>";
//echo  substr(htmlspecialchars($sw, ENT_QUOTES),0,200);   
//

$fkor="";
if (file_exists($kudap1."/tc.htm"))               //
{$fkor=$kudap1."/tc.htm";}
else if (file_exists($kudap1."/tg.htm"))
{$fkor=$kudap1."/tg.htm";}
else if (file_exists($kudap1."/tq.htm"))
{$fkor=$kudap1."/tq.htm";}
//echo $fkor."<hr>";
if ($fkor!="")                                             //
{$kimg = file($fkor);				                       //
$ksod=file_get_contents ($fkor);
$ksod=str_replace("../d~e~f/","../../../../d~e~f/",$ksod);  //
$w = fopen($fkor, 'w') or exit;      	                    //
fwrite($w,$ksod);  
fclose($w);
}

}               //
else 
   {if ($zel=="apx")                               //
   {$sw11=$llin;
	$poz11=strpos($sw11,">");				          //
	$tag11=explode(" ",substr($sw11,0,$poz11));       //
    $tagw10="";
    foreach ($tag11 as $k11 => $z11)                  //
    {if (strpos(" ".$z11,"title=")>0)                 //
     {$tagw10=$tagw10.' title='.++$ii;
	 }
     else
     {$tagw10=$tagw10." ".$z11;}	 
	 } 
   fwrite($w5,$tagw10.substr($sw11,$poz11));          // 
	}
}}
if ($zel=="apx") {fclose($w5);}                     //
fclose($ww);
//

$fkor="";
if (file_exists($kudap1."/tc.htm"))               //
{$fkor=$kudap1."/tc.htm";}
else if (file_exists($kudap1."/tg.htm"))
{$fkor=$kudap1."/tg.htm";}
else if (file_exists($kudap1."/tq.htm"))
{$fkor=$kudap1."/tq.htm";}
//echo $fkor."<hr>";
if ($fkor!="")                                             //
{$kimg = file($fkor);				                       //
$ksod=file_get_contents ($fkor);
$ksod=str_replace("../d~e~f/","../../../../d~e~f/",$ksod);  //
$w = fopen($fkor, 'w') or exit;      	                    //
fwrite($w,$ksod);  
fclose($w);
}




// 
$kuda=getcwd ( ).'/pages/'.$nam1.'/contents/01,01,01_'.$zel.'/01,01,01_'.$zel;
$otkuda=$_prhtm;
//echo $otkuda."     ".$kuda;
//echo $mpic2."<hr>";

$fes =scandir($_prhtm, 1);			                  //
 foreach ($fes as & $val) 			                  //
  {if ($val != "." & $val != "..") 	                  //
   {if  (!strstr($val, '.'))                          //
   {}
   else
   {
if (strpos($mpic2,$val."#")>0)
  {
//echo $val."<br>";
if (copy($otkuda."/".$val,$kuda."/".$val)) 
     {}
	 else
     {$os=$os. "<b>Copy error ".$z."</b>";}
}}}}
$nus1=str_replace("u","",substr($nus,2));
if ($zel=="apx")
{$os=$os. '<b style="color:green">Marked objects:'.$nus1.' moved to the archive!</b>';
}
else
{$os=$os. '<b style="color:green">Marked objects: '.$nus1.' added to templates!</b>';
}}
else
{$os=$os. '<b style="color:red">For this page ARCHIVE, TEMPLATE IS NOT CREATED OR THIS IS AN EXTERNAL PAGE! Close the form, click the "Page name" and the button "Archive" ("Template"). </b>';
}}}