<?
require_once "include/application.php"; 
$url="e-booking2u.com/tcrm";

function nextid($f){
	$result=mysql_query("select $f from nextid");
	while($myrow=MySQL_fetch_array($result))
	     return $myrow[$f];	
}
function last_insert_id($idtype){
	$sql = "update nextid set $idtype =LAST_INSERT_ID($idtype+1)";
	if(!@mysql_query($sql))$mysql_error = mysql_error();
}

function alert_birthday($id,$responder,$user,$dob,$type){
global $sc, $sf;

	$my_path = DOCROOT."/uploads/$user/";
	
	$stamp=nextid('n_eb');
	last_insert_id('n_eb');
	// search send 
	 $sql="select * from eb_alert left join eb_template on eba_template=ebt_id where eba_id='$id'";
	$result=mysql_query($sql);	
	while($myrow=MySQL_fetch_array($result,MYSQL_ASSOC))
	{
		$my_name = $myrow["eba_from"];
		$my_mail = $myrow["eba_from"];			
		$my_replyto = $myrow["eba_from"];
		$my_subject=$myrow["eba_subject"];				
		$cc=$myrow["eba_cc"];				
		$bcc=$myrow["eba_bcc"];				

		$my_message=$myrow["ebt_msg"];		
		$my_file=$myrow["ebt_attach"];		
		
		//person
		if($myrow["eba_pselect"]=="All")$psql="1";
		else if($myrow["eba_pselect"]=="Individual"){
			if($myrow["eba_title"]>1)$psql_ar[]="p_title='".$myrow["eba_title"]."'";
			if($myrow["eba_role"]>1)$psql_ar[]="p_role='".$myrow["eba_role"]."'";
			if($myrow["eba_race"]>1)$psql_ar[]="p_race='".$myrow["eba_race"]."'";
			if($myrow["eba_status"]>1)$psql_ar[]="p_status='".$myrow["eba_status"]."'";
			if($myrow["eba_gender"]>1)$psql_ar[]="p_gender='".$myrow["eba_gender"]."'";
			if($myrow["eba_ageformula"] && $myrow["eba_age"] > 0){
				$dob=date("Y-m-d",mktime(0,0,0,date("m"),date("d"),date("Y")-$myrow["eba_age"]));
				$psql_ar[]="p_dob ".$myrow["eba_ageformula"]." '$dob'";
			}
			if($myrow["eba_type"]>1)$psql_ar[]="p_type='".$myrow["eba_type"]."'";
			if($myrow["eba_industry"]>1)$psql_ar[]="p_industry='".$myrow["eba_industry"]."'";
			if($myrow["eba_source"]>1)$psql_ar[]="p_source='".$myrow["eba_source"]."'";
			if($myrow["eba_classification"]>1)$psql_ar[]="p_classification='".$myrow["eba_classification"]."'";
			if($myrow["eba_country"])$psql_ar[]="p_country='".$myrow["eba_country"]."'";
		}	
		
		$p_dear=$myrow["eba_pdear"];

	}	
	
	//send person
	if(count($psql_ar)>0)$psql=implode(" and ",$psql_ar);

	if($psql){

		if($type=="dob"){

			if($dob=="99"){
				$sql="select p_id,p_name,p_email1,p_email2,p_email3 from person where $psql and substring(p_dob,6)='".date("m-d")."' and p_active='Yes' and (p_email1<>'' or p_email2<>'' or p_email3<>'') and p_user='$user'"; 
			}else{
				$sql="select p_id,p_name,p_email1,p_email2,p_email3 from person where $psql and substring(DATE_SUB(p_dob, INTERVAL $dob DAY),6)='".date("m-d")."' and p_active='Yes' and (p_email1<>'' or p_email2<>'' or p_email3<>'') and p_user='$user'"; 
			}
		}else if($type=="spdob"){
			if($dob=="99"){
				$sql="select p_id,p_name,p_email1,p_email2,p_email3 from person where $psql and substring(p_spdob,6)='".date("m-d")."' and p_active='Yes' and (p_email1<>'' or p_email2<>'' or p_email3<>'') and p_user='$user'"; 
			}else{
				$sql="select p_id,p_name,p_email1,p_email2,p_email3 from person where $psql and substring(DATE_SUB(p_spdob, INTERVAL $dob DAY),6)='".date("m-d")."' and p_active='Yes' and (p_email1<>'' or p_email2<>'' or p_email3<>'') and p_user='$user'"; 
			}

		}	
		$result=mysql_query($sql) or die(mysql_error());
		$pnum=mysql_num_rows($result);
		while($myrow=MySQL_fetch_array($result,MYSQL_ASSOC))
		{
			$email=$myrow["p_email1"]."#".$myrow["p_email2"]."#".$myrow["p_email3"];
			$grprcp[]=$myrow["p_id"]."|".$myrow["p_name"]."|$email";
			
		}		
		if(count($grprcp)>0){
			for($i=0;$i<count($grprcp);$i++)
			{
				unset($gemail);
				list($pid,$pname,$pemail)=explode("|",$grprcp[$i]);
				$gemail=explode("#",$pemail);
				if($p_dear=="name")$disname=$pname;
				else if($p_dear=="default")$disname="Valued Customer";

				for($j=0;$j<count($gemail);$j++){
					if($gemail[$j]){
						$mailto=$gemail[$j];
						if($mailto){
							$header='<img src="http://$url/images/logo.jpg"><img src="http://$url/eb_open.php?stamp='.$stamp.'&eb='.$id.'&r='.$pid.'&e='.$mailto.'&user='.$user.'&type=person&responder='.$responder.'" width="0" height="0"><br><br>';
							$msg=$header."Dear $disname,<br><br>".$my_message;
		
							if($my_file)mail_attachment($my_file, $my_path, $mailto, $my_mail, $my_name, $my_replyto, $my_subject, $msg, $user,$cc,$bcc);
							else mail_sent($mailto, $my_mail, $my_name, $my_replyto, $my_subject, $msg, $user,$cc,$bcc);
						}
					}
				}
			}
		}
	}

	$sql="
		INSERT INTO `eb_alertsent` (
		`ebas_alert` ,`ebas_type` ,ebas_stamp,
		`ebas_created` ,ebas_record,ebas_failure
		)
		VALUES ('$id','Birthday Scheduler','$stamp',CURRENT_TIMESTAMP,'$sc','$sf'
		);
	";
	@mysql_query($sql) or die(mysql_error());
		
}

function mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message,$user,$cc,$bcc) {
global $sc, $sf;

	$message=str_replace("/tcrm/UserFiles/$user/","http://$url/UserFiles/$user/",$message);
	$message=str_replace('\"','"',$message);

    $file = $path.$filename;
    $file_size = filesize($file);
    $handle = fopen($file, "r");
    $content = fread($handle, $file_size);
    fclose($handle);
    $content = chunk_split(base64_encode($content));
    $uid = md5(uniqid(time()));
    $name = basename($file);
//    $header = "From: ".$from_name." <".$from_mail.">\r\n";
    $header = "From: ".$from_mail."\r\n";
    $header .= "Reply-To: ".$replyto."\r\n";
    if($cc)$header .= "Cc: ".$cc."\r\n";
    if($bcc)$header .= "Bcc: ".$bcc."\r\n";
    $header .= "MIME-Version: 1.0\r\n";
    $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
    $header .= "This is a multi-part message in MIME format.\r\n";
    $header .= "--".$uid."\r\n";
    $header .= "Content-type:text/html; charset=iso-8859-1\r\n";
    $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
    $header .= $message."\r\n\r\n";
    $header .= "--".$uid."\r\n";
    $header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"; // use different content types here
    $header .= "Content-Transfer-Encoding: base64\r\n";
    $header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
    $header .= $content."\r\n\r\n";
    $header .= "--".$uid."--";
    if (mail($mailto, $subject, "", $header)) {
		$sc++;
    } else {
		$sf++;
    }
}
function mail_sent($mailto, $from_mail, $from_name, $replyto, $subject, $message,$user,$cc,$bcc) {
global $sc, $sf;

	$message=str_replace("/tcrm/UserFiles/$user/","$url/UserFiles/$user/",$message);

	$message=str_replace('\"','"',$message);
    $uid = md5(uniqid(time()));

//    $header = "From: ".$from_name." <".$from_mail.">\r\n";
    $header = "From: ".$from_mail."\r\n";
    $header .= "Reply-To: ".$replyto."\r\n";
    if($cc)$header .= "Cc: ".$cc."\r\n";
    if($bcc)$header .= "Bcc: ".$bcc."\r\n";
    $header .= "MIME-Version: 1.0\r\n";
    $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
    $header .= "This is a multi-part message in MIME format.\r\n";
    $header .= "--".$uid."\r\n";
    $header .= "Content-type:text/html; charset=iso-8859-1\r\n";
    $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
    $header .= $message."\r\n\r\n";
    $header .= "--".$uid."\r\n";
	

	if (mail($mailto, $subject, "", $header)) {
		$sc++;
	} else {
		$sf++;
	}
	

}

$sc=0;
$sf=0;
if($id && $user)alert_birthday($id,$responder,$user,$dob,'dob');		

return 1;
?>