<?php
require_once"../template/start.php";
require_once"../template/regglobals.php";
require_once"../template/config.php";
require_once"../template/functions.php";
require_once"../template/antidos.php";
require_once"../template/cookies.php";
require_once"../template/gzip.php";
require_once"../template/header.php";
require_once"../template/referer.php";
include_once"../themes/$config_themes/index.php";
include_once"../template/isset.php";

$lib_name=file_get_contents("$did/name.dat");
$text2 = @file("../local/profil/$log.prof"); 
if ($text2!=""){
$udata = explode(":||:",$text2[0]);
$provlog=trim($udata[0]);
$provpar=trim($udata[1]);}

$date=date("d.m.y в H:i:s");

$title = htmlspecialchars(stripslashes(trim($title)));
$fid = htmlspecialchars(stripslashes(trim($fid)));
$did = htmlspecialchars(stripslashes(trim($did)));

$str1=strlen($title);
$str2=strlen($fid);
if($str1>100 || $str2>200000){ header ("Location: add.php?isset=ljbigtext&".SID);  exit();}

if($str2<200 || $str1<3){ header ("Location: add.php?isset=ljsmalltext&".SID);  exit();}  
if($title=="") {
header ("Location: add.php?isset=ljnotitle");  exit();}

if($did=="") {
header ("Location: add.php?isset=ljnokat");  exit();}

if($fid=="") {
header ("Location: add.php?isset=ljnotext");  exit();}

if($log=="") {
header ("Location: add.php?isset=inputoff");  exit();}
///Антифлуд
if($config_floodstime > 0){
if(flooder($ip, $php_self) == TRUE ){header ("Location: index.php?isset=antiflood&".SID); exit;}
$flood_file = fopen(BASEDIR."local/flood.dat", "a+");
flock ($flood_file,LOCK_EX);
fputs ($flood_file, time()."|$ip|$php_self|\r\n");
fflush($flood_file);
flock ($flood_file,LOCK_UN);
fclose($flood_file);}

$ufile = file(BASEDIR."local/profil/$log.prof"); 
$udata = explode(":||:",$ufile[0]);
$udata[41]=round($udata[41]+20);//// 20 монет за статью
$udata[36]=round($udata[36]+5);/////////// 5 балов за статью
for ($u=0; $u<$config_userprofkey; $u++){
$utext.=$udata[$u].':||:';}

if($udata[0]!="" && $udata[1]!="" && $udata[4]!="" && $utext!=""){
$fp=fopen(BASEDIR."local/profil/$log.prof","a+");
flock($fp,LOCK_EX);   
ftruncate($fp,0);                                                                 
fputs($fp,$utext);
fflush($fp);
flock($fp,LOCK_UN);
fclose($fp);  
unset($utext);
}

$dir = opendir ("$did"); 
   while ($file = readdir ($dir)) 
   {if (( $file != ".") && ($file != "..") && ($file != "name.dat") && !ereg (".dat$", "$file"))
   {$a[]=$file;}}  
   closedir ($dir);  
   sort($a);
   $shet_file = count($a);  
   $newname = $shet_file+1;
   $newname = $newname.'.txt';


$fp=fopen("$did/$log.$did.$newname","w"); ///добавлен $did
flock ($fp,LOCK_EX);
fputs($fp,"$title\r\n\r\n $fid");
fflush ($fp);
flock ($fp,LOCK_UN);
fclose($fp);
chmod($fp, 0666);
chmod("$did/$log.$did.$newname", 0666); ///$did 

$f = file("../local/gazeta/$log.dat"); 
$u = explode(":||:",$f[0]);
$u[0]++;
$t=$u[0].':||:'.$log.':||:'.$date;

$fp = fopen("../local/gazeta/$log.dat","w");
flock ($fp,LOCK_EX);
fputs($fp,$t);
fflush ($fp);
flock ($fp,LOCK_UN);
fclose($fp);
chmod($fp, 0666);
chmod("../local/gazeta/$log.dat", 0666);
/////////////////////////////
/////////////////////////////
$f = file("../local/gazeta/$log.$did.$newname.dat"); 
$u = explode("|",$f[0]);

$t=''.$date.'|'.$lib_name.'|'.$log.'|'.$title.'|';
$t=no_br($t);

$file=file("../local/gazeta/$log.$did.$newname.dat");
$fp=fopen("../local/gazeta/$log.$did.$newname.dat","a+");
flock ($fp,LOCK_EX);
ftruncate ($fp,0);
fputs ($fp,"$t\r\n");
fflush ($fp);
flock ($fp,LOCK_UN);
fclose($fp);
chmod ($fp, 0777);
chmod ("../local/gazeta/$log.$did.$newname.dat", 0777);    
//////////////////////////////////////////////////////////////////

$f1 = file("../local/gazeta/$log.dat"); 
$u1 = explode(":||:",$f1[0]);


echo '
<b>'.$log.', Вы добавили статью...</b><br/>
<b>Раздел:</b> '.$lib_name.'<br>
<b>Это ваша:</b> '.$u1[0].'-я запись ('.$date.')<br/>
<b>Название:</b> '.$title.'<br/>
</div>';

echo '<img src="img/bsearch.png" alt=""> <a href="lj.php?action=count&amp;did='.$did.'&amp;fid='.$log.'.'.$did.'.'.$newname.'">Читать</a><br />';

echo '<img src="img/edit.png" alt=""> <a href="add.php?'.SID.'">Добавить статью</a><br>'; 
echo '<img src="img/folder_open.png" alt=""> <a href="index.php?'.SID.'">Журнал</a><br>';
echo '<img src="../images/img/homepage.gif" alt=""> <a href="../index.php?'.SID.'">На главную</a><br>';

include_once"../themes/$config_themes/foot.php";
?>