<?php
///////////////////////////////////////////////////
//Авто-напоминание о забытых книгах
//2010 (C)
//Карпухин Павел
///////////////////////////////////////////////////

require_once "/var/www/library/config.php";
require_once "/var/www/library/utils/functions.php";
echo `clear`;
echo "Project Home library.\r\n";
echo "Mail notification started.\r\n\r\n";
$n = 0;
$usrs = "";

// переменные для формирования письма
$TEXT['nap_podp'] = "С Уважением, <br />";
$TEXT['nap_tema'] = "Задолженность (просьба вернуть книгу владельцу).";
$TEXT['nap_head1'] = "Здравствуйте, Уважаемый(ая) ";
$TEXT['nap_telo1'] = "Напоминаю Вам, что не далее чем ";
$TEXT['nap_telo2'] = "вы брали у меня книгу <br />&laquo;<b>";
$TEXT['nap_telo3'] = "</b>&raquo; и клятвенно обещали вернуть её не позднее <b>";
$TEXT['nap_telo4'] = "</b>.<br />Взгляните пожалуйста на календарь.<br />Да! Да! Время вышло!<br />Верните пожалуйста сей фолиант немедля!!!<br />";

$TEXT['month1'] = "января";
$TEXT['month2'] = "февраля";
$TEXT['month3'] = "марта";
$TEXT['month4'] = "апреля";
$TEXT['month5'] = "мая";
$TEXT['month6'] = "июня";
$TEXT['month7'] = "июля";
$TEXT['month8'] = "августа";
$TEXT['month9'] = "сентября";
$TEXT['month01'] = "января";
$TEXT['month02'] = "февраля";
$TEXT['month03'] = "марта";
$TEXT['month04'] = "апреля";
$TEXT['month05'] = "мая";
$TEXT['month06'] = "июня";
$TEXT['month07'] = "июля";
$TEXT['month08'] = "августа";
$TEXT['month09'] = "сентября";
$TEXT['month10'] = "октября";
$TEXT['month11'] = "ноября";
$TEXT['month12'] = "декабря";
$TEXT['year2'] = "года";

$TEXT['log-mail'] = "Напоминание о книге";
$TEXT['log-mail3'] = "E-mail сообщения посланы в количестве ";
$TEXT['log-mail4'] = " штук.<br />Напоминание получат пользователи:<br />";

// от чьего имени пишем
$address_vlad = "tel-spr@yandex.ru";
$famil_vlad = "Карпухин";
$name_vlad = "Павел";

$query_body = "SELECT
     tbl_book.book_name,
     tbl_book.book_id,
     tbl_book.napominalka,
     tbl_book.vidano,
     UNIX_TIMESTAMP(tbl_book.data_vidachi) AS data_vidachi,
     UNIX_TIMESTAMP(tbl_book.data_sdachi) AS data_sdachi,
     tbl_book.user_id,
     tbl_biblioteka.name AS bibl_name,
     tbl_logins.dom_id,
     tbl_logins.e_mail,
     tbl_logins.familiya,
     tbl_logins.name,
     tbl_logins.otchestvo,
     tbl_domain.domain
     FROM tbl_book
     JOIN tbl_biblioteka ON tbl_book.bibl_id=tbl_biblioteka.bibl_id
     JOIN tbl_logins ON tbl_book.user_id=tbl_logins.login_id
     JOIN tbl_domain ON tbl_logins.dom_id=tbl_domain.dom_id
     WHERE tbl_book.napominalka = '1' AND tbl_book.vidano ='1' ";
     $result_body = mysqli_query($dbcnx,$query_body);
     if(!$result_body) {exit();}
       $result_rows = mysqli_num_rows($result_body);
       if($result_rows > 0)
         {
          while($result_body_book= mysqli_fetch_array($result_body))
            {
               $date_day = date("d",$result_body_book['data_sdachi']);
               $date_month = date("m",$result_body_book['data_sdachi']);
               $date_year = date("Y",$result_body_book['data_sdachi']);
               $year = $TEXT['year2'];
               if($result_body_book['data_sdachi'] == "0") {$data_sd = "---";}
                else {$data_sd = (date("$date_day $month[$date_month] $date_year $year"));}

               $date_day2 = date("d",$result_body_book['data_vidachi']);
               $date_month2 = date("m",$result_body_book['data_vidachi']);
               $date_year2 = date("Y",$result_body_book['data_vidachi']);
               if($result_body_book['data_vidachi'] == "0") {$data_vid = "---";}
                else {$data_vid = (date("$date_day2 $month[$date_month2] $date_year2 $year"));}

               $date_day3 = date("d", time());
               $date_month3 = date("m", time());
               $date_year3 = date("Y", time());
               $date_time = date("H:i:s", time());
               $data3 = (date("$date_day3 $month[$date_month3] $date_year3 $year"))." ".$date_time;

               $bibl = $result_body_book['bibl_name'];
               $book_name = rep_back($result_body_book['book_name']);
               $usr_name = $result_body_book['name'];
               $usr_fam = $result_body_book['familiya'];
               $usr_otch = $result_body_book['otchestvo'];

               $query_zad = mysqli_query($dbcnx,"SELECT DATEDIFF(CURRENT_DATE(),data_sdachi) AS zad_date FROM tbl_book WHERE book_id = ".$result_body_book['book_id']."");
                $res_zad = mysqli_fetch_array($query_zad);

               if($res_zad['zad_date']>1)
               {
               $n++;

               if($result_body_book['e_mail'] !== '') {$address = $result_body_book['e_mail']."@".$result_body_book['domain'];}
                else {exit();}

               $podpis = $TEXT['nap_podp'].$famil_vlad." ".$name_vlad."<br />".$data3."<br /><a href=mailto:".$address_vlad.">".$address_vlad."</a>";
               $tema = $TEXT['nap_tema'];
               $msg = $TEXT['nap_head1'].$result_body_book['familiya']." ".$result_body_book['name']." ".$result_body_book['otchestvo'].".<br /><br />".$TEXT['nap_telo1'].$data_vid." ".$TEXT['nap_telo2'].rep_back($result_body_book['book_name']).$TEXT['nap_telo3'].$data_sd.$TEXT['nap_telo4']."<br />===============<br />".$podpis."<hr align='left' width='30%'/>";
               $usrs .= $result_body_book['familiya']." ".$result_body_book['name']." ".$result_body_book['otchestvo'].", ";

               $tema = stripslashes($tema);
               $msg = nl2br(stripslashes($msg));

               mail($address, $tema, $msg, join("\r\n", array(
               "From: ".$address_vlad."",
               "MIME-Version: 1.0",
               "Content-type: text/html; charset=utf-8",
               "Content-Transfer-Encoding: 8bit",
               "Date: ".date("D, d-M-Y H:i:s", time())."",
               "X-Mailer: Home library",
               "X-Spam-Flag: NO"
               )));
               }
               echo "mail # ".$n." (for ".$address.") sended\r\n";
            }
         }
   echo "\r\nOperation complete.\r\n";
  //*************************************
  $ip = "127.0.0.1";
  $doing = $TEXT['log-mail'];
  $what = $TEXT['log-mail3'].$n.$TEXT['log-mail4'].substr($usrs,0,-2);
  $who = "Robot";
  //*************************************
  $query2 = "INSERT INTO tbl_logs ( `log_id` , `data` , `ip` , `who` , `doing` , `what` ) VALUES ( NULL , NOW() , '$ip' , '$who' , '$doing' , '$what')";
   $query_log = mysqli_query($dbcnx,$query2);
    if(!$query_log) {exit();}
?>