<!DOCTYPE html>
<html>
	<head>
	<meta charset="utf-8" />
<title>Рассылка новостей</title>
<script type='text/javascript'>
   function clo(clearsub)
    {
     window.opener.rel(clearsub);
     window.close();
    }
 </script>
</head>
<?php
//ini_set('display_errors', 1);
//error_reporting(E_ALL);
$mset=file('../../sg_cm/conf/set.php');
for($i=0; $i<count($mset); $i++) $mset[$i]=trim($mset[$i]);
if ($mset[4]=="X5" && is_file("../../res/imemail.inc.php")) {$incl="../../res/imemail.inc.php";}
elseif (is_file("../../sg_cm/include/email_libM.php")) {$incl="../../sg_cm/include/email_libM.php"; $mset[4]=="libM";}
elseif (is_file("../../res/imemail.inc.php")) {$incl="../../res/imemail.inc.php"; $mset[4]=="X5";}
else exit("Не найден скрипт отправки почты");
require($incl); 
require ('../../sg_cm/include/mailtemplater.php');
require ('../../sg_cm/root.php');

ignore_user_abort();
 set_time_limit(0);
if (isset($_GET['test_mail'])) $getTestmail=$_GET['test_mail']; else $getTestmail=''; 
echo "<div align=center><span style=\"color:#005E8A;\">Идёт отправка почты</span><br /><br /></div>";


$head_oll="Новости сайта ".rtrim($_SERVER['SERVER_NAME']."/".$sg_root,'/');
$head_oll1="";$head_oll2="";
$head_file=file("db/head.txt");
if(isset($head_file[0])){$head_file[0]=trim($head_file[0]);
if ($head_file[0]!='')$head_oll=$head_file[0];
if (isset($head_file[1]))$head_oll1=trim($head_file[1]);
if (isset($head_file[2]))$head_oll2=trim($head_file[2]);}


$conf=file("conf/sett.txt");
for($n=0;$n<count($conf);$n++)$conf[$n]=trim($conf[$n]);

$conf1=file("conf/sett1.txt");
for($n=0;$n<count($conf1);$n++)$conf1[$n]=trim($conf1[$n]);

$mail=array();if ($getTestmail=='no') {
$mail=file("db/mail.txt");
for($n=0;$n<count($mail);$n++)$mail[$n]=trim($mail[$n]);
$mail = array_unique($mail);}
else {$mail[0]=$getTestmail;}
$sub=array();
$sub1=array();
$sub=file("db/sub.txt");
foreach($sub as $line)
 {
   $line=trim($line);
   $text=file("db/news/$line");
   $text[0]=trim($text[0]);
   $sub1[$text[0]]=$line;
 }

krsort($sub1,SORT_NUMERIC);
$sub=array();
foreach($sub1 as $line) $sub[]=$line;

if(count($sub)!="" && count($mail)!="" && @$_GET['op']==1)
  {
     $t[0]=$head_oll;//заголовок
	 $t[3]=$head_oll2;//текст приветствия
	 $t[1]=$head_oll1;//подзаголовок
	 $i=0;$nnews=array();
     foreach($sub as $line)
       {   $nnews[$i]='';
       	 $file=file("db/news/$line");
       	 for($n=0;$n<count($file);$n++)$file[$n]=trim($file[$n]);
         $nnews[$i].="<div id=date_news><i>".date("d.m.Y H:i",$file[0])."</i></div><div id=head_news><strong>"; 
			
         if($conf[34]!="") { $conf34 = str_ireplace(".html", "/", $conf[34]);
			$conf34 = str_ireplace(".php", "/", $conf[34]); $nnews[$i].="<a href='".$conf34.$line.".html'>".$file[2]."</a>";}
         else $nnews[$i].= $file[2];
         $nnews[$i].="</strong></div><div id=resume_news>$file[3]</div><br />";
$i=$i+1;
       }

    $subj=$head_oll; 
    foreach($mail as $line) {
	$to=$line;
	$t[2]='<a href="'.$conf[34].'?unsub='.$to.'">Отменить подписку</a>';
	if ($mset[3]!='TXT') $mess=mailtemplater($nnews,$t);
	else $mess='';
	if ($mset[3]!='HTML')$mess_txt=mailtemplater_txt($nnews,$t);
	else $mess_txt='';
//echo $mess;
	
	usleep(35000); require("../../sg_cm/conf/email_set.php");	
	if ($getTestmail!='no') break;
} 
	if ($getTestmail=='no'){
    $f=fopen("db/sub.txt","w");
    fclose($f);
	}
     echo "<meta http-equiv=refresh content='0; url=sub.php?op=2&test_mail=".$getTestmail."'>";
     exit();
  }

if(@$_GET['op']==2)
  {  $sub=array();$clearsub=1;
	 $sub=file("db/sub.txt"); if (count($sub)) $clearsub=0;
     echo"<script type='text/javascript'>clo('".$clearsub."')</script>";
  }

if(@$_GET['op']==3)
  {
     echo "<meta http-equiv=refresh content='4; url=sub.php?op=1&test_mail=".$getTestmail."'>";
     exit();
  }


?>
</html>