$length)
{
$string = substr($string, 0, $length);
}
}
function create_guid()
{
$microTime = microtime();
list($a_dec, $a_sec) = explode(" ", $microTime);
$dec_hex = sprintf("%x", $a_dec* 1000000);
$sec_hex = sprintf("%x", $a_sec);
ensure_length($dec_hex, 5);
ensure_length($sec_hex, 6);
$guid = "";
$guid .= $dec_hex;
$guid .= create_guid_section(3);
$guid .= '-';
$guid .= create_guid_section(4);
$guid .= '-';
$guid .= create_guid_section(4);
$guid .= '-';
$guid .= create_guid_section(4);
$guid .= '-';
$guid .= $sec_hex;
$guid .= create_guid_section(6);
return $guid;
}
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");
if(mysql_num_rows($r)>0){
if($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;
}
} 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){
if($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;
}
} else {
return false;
}
}
function pobierz_moj_email_dodaj_relacje($to,$id,$uid){
$r=mysql_query("SELECT adres.id as id FROM email_addresses as adres
where adres.email_address='".$to."'
or adres.email_address_caps='".strtoupper($to)."'");
if(mysql_num_rows($r)>0){
while($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['id']."',
'0',
'".$uid."');");
}
return true;
} else {
return false;
}
}
function pobierz_moj_email_dodaj_relacje2($to,$id,$uid){
$r=mysql_query("SELECT adres.id as id FROM email_addresses as adres
where adres.email_address='".$to."'
or adres.email_address_caps='".strtoupper($to)."'");
if(mysql_num_rows($r)>0){
while($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['id']."',
'0',
'".$uid."');");
}
return true;
} else {
return false;
}
}
include_once("../import/class.dbf.php");
include_once("../config.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']);
require_once('ImapMailbox.php');
define('ATTACHMENTS_DIR', dirname(__FILE__) . '/attachments');
mysql_query ('SET NAMES utf8');
mysql_query ('SET CHARACTER_SET utf8_unicode_ci');
$r=mysql_query("select id,google_login,google_skrzynka,google_lastupdate,CAST(AES_DECRYPT(google_password, 'jakistamhash123') as CHAR)
AS google_password,google_calendar_name FROM users where google_login
is not null and status='Active' order by id");
while($dane=mysql_fetch_array($r)){
if($dane['google_login']!='' && $dane['google_password']!=''){
// IMAP must be enabled in Google Mail Settings
echo "Dla konta: ".$dane['google_login']." znaleziono: ";
$mailbox = new ImapMailbox('{imap.gmail.com:993/imap/ssl}'.$dane['google_skrzynka'].'',$dane['google_login'], $dane['google_password'], ATTACHMENTS_DIR, 'utf-8');
$mails = array();
// pobierz idki maili
if($dane['google_lastupdate']!='' && $dane['google_lastupdate']!='0000-00-00 00:00:00'){
$da=date("d M Y",strtotime($dane['google_lastupdate'])-86400);
}else {
$da=date("d M Y",strtotime("-1 day"));
}
// echo $da;
$mailsIds = $mailbox->searchMailBox("SINCE \"$da\"");
if(count($mailsIds)==0)continue;
//echo count($mailsIds)." emaili
";
///print_r($mailsIds);
//$mailsIds = array_slice($mailsIds, 1, 2);
$i=0;
// print_r($mailbox->getMailBoxes());
$bc=0;
// if($dane['google_login']!='outtervision@gmail.com') continue;
foreach ($mailsIds as &$value) {
// pobierz dane o emailu id flagi itp
$udate=$mailbox->getMailInfo($value);
//echo "
"; // print_r($udate); //echo ""; //$mail=$mailbox->getMail($value); //sprawdź czy email jest w bazie danych $dodaj=0; if($dane['google_lastupdate']!='' && $dane['google_lastupdate']!='0000-00-00 00:00:00'){ $dzis=strtotime($dane['google_lastupdate']); if($udate[0]->udate > $dzis-820){ $dodaj=1; } else { continue; } } else { $dzis=strtotime(date("Y-m-d 00:00:01")); if($udate[0]->udate > $dzis){ $dodaj=1; } else { continue; } } $gen_id=md5($udate[0]->udate."".$udate[0]->date."".$udate[0]->to."".$udate[0]->subject); $r2=mysql_query("select id from emails where message_id='".$gen_id."'"); // jesli nie dodaj if(mysql_num_rows($r2)==0 && $dodaj==1){ $rel=0; $rel2=0; $dodano=0; // echo '