﻿<?php
exit();
if(!isset($argv))
    {
    exit();
    }

error_reporting(0);
include("config.php");
mysql_connect(DBHOST, DBUSER, DBPASS);
mysql_select_db(DBNAME);
require_once("PHPMailer/class.phpmailer.php");

$nextHetfo=date("Y-m-d",strtotime("next monday"));
$next_het=foodDatum::getHet($nextHetfo);
$next_ev=foodDatum::getEv($nextHetfo);
$q="select min(datum) as datum from etlap where ev='".$next_ev."' and Het=".$next_het." and Szun!='i'";
$r=mysql_query($q);
$s=mysql_fetch_assoc($r);
$kovdatum=$s["datum"];

$limited=false;
$limit=0;
if(isset($argv[1]))
    {
    $ar=explode("=",$argv[1]);
    if($ar[0]=="limit")
        {
        $limited=true;
        $limit=$ar[1];
        }
    }

if(isset($_GET["limit"]))
    {
    $limited=true;
    $limit=$_GET["limit"];
    }    
    
$mail = new PHPMailer();
$mail->IsMail();
$mail->CharSet = 'UTF-8';

$mail->From="hirlevel@interfood.hu";
$mail->FromName="Interfood";
$mail->Sender="hirlevel@interfood.hu";
$mail->AddReplyTo("hirlevel@interfood.hu", "Interfood");

//$mail->AddAddress("dittro@gmail.com"); //  //marketing@interfood.hu
$mail->Subject = "Rendelési emlékeztető";

$mail->IsHTML(false);



$message="
Kedves Ügyfelünk!

Ön emlékeztetőt kért honlapunktól a jövő heti rendelésének leadására, melyet ezúton kézbesítünk.

Kérjuk, kattintson a linkre: 

http://www.interfood.hu/rendeles?het=het_ccccccckdjhdkfhsdlfsdfhldsfdljhflgjlfgfgcccccccccc&em=emlekezteto_ccccccckdjhdkfhsdlfsdfhldsfdljhflgjlfgfgcccccccccc

Üdvözlettel,

Interfood Ügyfélszolgálat
";

//$mail->Body = $message;

//$mail->Send();
//$mail->ClearAddresses();

$ma=date("Y-m-d");
$q0="select datum from emlekezteto where datum='".$ma."'";
$r0 = mysql_query($q0);
$s0=mysql_fetch_assoc($r0);
if(!$s0)
    {
    $q1="insert into emlekezteto(datum) values('".$ma."')";
    $r1 = mysql_query($q1);
    $q2="update felhasznalok set emlekezteto_ok=0, emlekezteto_limit='".$kovdatum." 06:00:00'";
    $r2 = mysql_query($q2);
    }

$query = "SELECT DISTINCT idlogin,Email, loginNev FROM felhasznalok  WHERE Email IS not NULL and emlekezteto>=3 and emlekezteto_ok=0";
if($limited)
    {
    $query.=" limit 0,".$limit;
    }
	
//$query = "SELECT distinct Email FROM felhasznalok f Where  f.Email='melo.janous@gmail.com' ORDER BY Email";			


$result = mysql_query($query) or die("Adatbazis hiba: mysql hibauzenet:".mysql_error());

$nur=mysql_num_rows($result);
if($nur<1)
    {
    exit();
    }

while($sor0=mysql_fetch_assoc($result))
    {
    $sor1[$sor0["idlogin"]]=$sor0;
    }

foreach($sor1 as $idlogin => $sor)
    {
    $emlekezteto_uid0=md5(uniqid("emlekezteto_uid",true));
    $emlekezteto_uid="";
    for($i=0; $i<6; $i++)
        {
        $emlekezteto_uid.=substr($emlekezteto_uid0,$i*5,5).substr($sor["loginNev"],$i,1);
        }
    $emlekezteto_uid.=substr($emlekezteto_uid0,30,2);
    
    $q3="update felhasznalok set emlekezteto_uid='".$emlekezteto_uid."', emlekezteto_ok=1 where idlogin=".$idlogin;
    $r3 = mysql_query($q3);
    $sor1[$idlogin]["emlekezteto_uid"]=$emlekezteto_uid;
    }








foreach($sor1 as $row) 
	{  	
	$addr  = $row['Email'];
    //echo $addr;
	set_time_limit(0); 

	$message_akt=$message;
    $message_akt=str_replace('het_ccccccckdjhdkfhsdlfsdfhldsfdljhflgjlfgfgcccccccccc',$next_ev.$next_het,$message_akt);
    $message_akt=str_replace('emlekezteto_ccccccckdjhdkfhsdlfsdfhldsfdljhflgjlfgfgcccccccccc',$row["emlekezteto_uid"],$message_akt);
	$mail->AddAddress($addr);
	$mail->Body = $message_akt;
	$mail->Send();
	$mail->ClearAddresses();
	
					
	}

?>