<?php
mb_internal_encoding("UTF-8");
function sprawdz_czy_od_klienta($od,$id,$do=false,$uid){
	$r=mysql_query("select adres.id as email_adres_od ,rel.bean_id as id_kontra,rel.bean_module as modul from email_addresses as adres
INNER join email_addr_bean_rel as rel on adres.id=rel.email_address_id
 where rel.deleted=0 and adres.email_address='".$od."' 
or adres.email_address_caps='".strtoupper($od)."' and rel.deleted=0");
//echo 'loop';
	if(mysql_num_rows($r)>0 && $do==true){
	$dane=mysql_fetch_assoc($r);
				mysql_query("INSERT INTO emails_email_addr_rel
(`id`,
`email_id`,
`address_type`,
`email_address_id`,
`deleted`,
`user_id`)
VALUES
('".create_guid()."',
'".$id."',
'from',
'".$dane['email_adres_od']."',
'0',
'".$uid."');");
return $dane;
}
	if(mysql_num_rows($r)>0){
		return true;
	} else{
		return false;
	}
	
}
function sprawdz_czy_od_klienta2($od,$id,$do=false,$uid){
	$r=mysql_query("select adres.id as email_adres_od ,rel.bean_id as id_kontra,rel.bean_module as modul from email_addresses as adres
INNER join email_addr_bean_rel as rel on adres.id=rel.email_address_id
 where rel.deleted=0 and adres.email_address='".$od."' 
or adres.email_address_caps='".strtoupper($od)."' and rel.deleted=0");

	if(mysql_num_rows($r)>0 && $do==true){
	$dane=mysql_fetch_assoc($r);
			mysql_query("INSERT INTO emails_email_addr_rel
(`id`,
`email_id`,
`address_type`,
`email_address_id`,
`deleted`,
`user_id`)
VALUES
('".create_guid()."',
'".$id."',
'to',
'".$dane['email_adres_od']."',
'0',
'".$uid."');");
return $dane;
}
	if(mysql_num_rows($r)>0){
		return true;
	} else{
		return false;
	}
	
}

function sprawdz_czy_od_klienta3($od,$id,$do=false,$uid){
	$r=mysql_query("select adres.id as email_adres_od ,rel.bean_id as id_kontra,rel.bean_module as modul from email_addresses as adres
INNER join email_addr_bean_rel as rel on adres.id=rel.email_address_id
inner join accounts as ac on ac.id=rel.bean_id
 where rel.deleted=0 and adres.email_address='".$od."' 
or adres.email_address_caps='".strtoupper($od)."' and rel.deleted=0");

	if(mysql_num_rows($r)>0 && $do==true){
	$dane=mysql_fetch_assoc($r);
				mysql_query("INSERT INTO emails_email_addr_rel
(`id`,
`email_id`,
`address_type`,
`email_address_id`,
`deleted`,
`user_id`)
VALUES
('".create_guid()."',
'".$id."',
'to',
'".$dane['email_adres_od']."',
'0',
'".$uid."');");
return $dane;
}
	if(mysql_num_rows($r)>0){
		return true;
	} else{
		return false;
	}
	
}

function get_emails ($str)
{
$emails = array();
preg_match_all("/([\s]*)([_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*([ ]+|)@{1,2}([ ]+|)([ a-zA-Z0-9-]+\.)+([a-zA-Z]{2,}))([\s]*)/", $str, $output);
foreach($output[0] as $email) array_push ($emails, strtolower($email));
if (count ($emails) >= 1) return $emails;
else return false;
}

include_once("../import/class.dbf.php");
include_once("../config.php");
include_once("../import/helper.php");

global $sugar_config;
$sql=mysql_connect($sugar_config['dbconfig']['db_host_name'],$sugar_config['dbconfig']['db_user_name'],$sugar_config['dbconfig']['db_password']);
mysql_select_db($sugar_config['dbconfig']['db_name']);


// dopasuj nowy kontakty do pojedynczych odbiorców
$r=mysql_query("select emails.id,rel.address_type,txt.user_id,txt.to_addrs,txt.from_addr,emails.type from emails as emails
inner join emails_email_addr_rel as rel on emails.id=rel.email_id
inner join users as us on us.id=emails.created_by
inner join emails_text as txt on txt.email_id=emails.id
where txt.gid is not null group by rel.email_id having count(rel.email_id)<2;");
echo "Znaleziono: ".mysql_num_rows($r)." nie dopasowanych emaili<br>";
while($dane=mysql_fetch_array($r)){
		//echo 'asa';
		
					//explode(",",substr($dane['from_addr'],strpos($dane['from_addr'],"<"),strpos($dane['from_addr'],"<")-strpos($dane['from_addr'],">")));
					if($dane['type']=='inbound'){
					$emails = get_emails ($dane['from_addr']);
						if(false!=sprawdz_czy_od_klienta($emails[0],$dane['id'],false,$dane['user_id'])){
							$check=sprawdz_czy_od_klienta($emails[0],$dane['id'],true,$dane['user_id']);
							$modul=$check['modul'];
							$id_kontra=$check['id_kontra'];
							mysql_query("UPDATE emails SET  `parent_type`='".$modul."', `parent_id`='".$id_kontra."' WHERE id='".$dane['id']."';");
						}
						
						
					} if($dane['type']=='out') {
						$emails = get_emails ($dane['to_addrs']);
						foreach ($emails as &$value) {
							if(false!=sprawdz_czy_od_klienta2($value,$dane['id'],false,$dane['user_id'])){
								$check=sprawdz_czy_od_klienta2($value,$dane['id'],true,$dane['user_id']);
								$modul=$check['modul'];
								$id_kontra=$check['id_kontra'];
									mysql_query("UPDATE emails SET  `parent_type`='".$modul."', `parent_id`='".$id_kontra."' WHERE id='".$dane['id']."';");
							}
						}
					}
					

}
// dopasuj nowe kontakty do wiadomosci z instniejacymi juz odbiorcami

$q=mysql_query("select emails.tid,emails.id,rel.email_id,rel.address_type,txt.user_id,txt.to_addrs,txt.from_addr,emails.type from emails as emails
inner join emails_email_addr_rel as rel on emails.id=rel.email_id
inner join emails_text as txt on txt.email_id=emails.id
inner join users as us on us.id=emails.created_by
where emails.tid is not null and rel.address_type='to'  group by rel.email_id having count(rel.email_id)<emails.tid;");
echo "Znaleziono: ".mysql_num_rows($q)." nie dopasowanych emaili<br>";
while($dane=mysql_fetch_array($q)){
		//echo 'asa';
		
					//explode(",",substr($dane['from_addr'],strpos($dane['from_addr'],"<"),strpos($dane['from_addr'],"<")-strpos($dane['from_addr'],">")));
					if($dane['type']=='out') {
						$emails = get_emails ($dane['to_addrs']);
					//	print_r($emails );
						foreach ($emails as &$value) {
							$zz=mysql_query("select rel.email_id from emails_email_addr_rel as rel
inner join email_addresses as addres on rel.email_address_id=addres.id
where addres.email_address='".$value."' and rel.email_id='8a3f4ce8-2040-b361-c020-5334480315a1' and rel.deleted=0 or
addres.email_address_caps='".strtoupper($value)."' and rel.email_id='".$dane['id']."' and rel.deleted=0;
");

							if(mysql_num_rows($zz)==0){
							if(false!=sprawdz_czy_od_klienta3($value,$dane['id'],false,$dane['user_id'])){
								$check=sprawdz_czy_od_klienta3($value,$dane['id'],true,$dane['user_id']);
								$modul=$check['modul'];
								$id_kontra=$check['id_kontra'];
									mysql_query("UPDATE emails SET  `parent_type`='".$modul."', `parent_id`='".$id_kontra."' WHERE id='".$dane['id']."';");
							}
							}
						}
					}
					

}
//var_dump($mail->getAttachments());

