Edit file File name : GoogleContactsPro.php Content :<?php class GoogleContactsPro extends GoogleContactsGC { function get_all_contacts_list($only_user=false){ global $current_user; if ($only_user==true){ if ($_SESSION['authenticated_user_id']==""){ echo "<br>Login error"; exit; }else{ $cu = $_SESSION['authenticated_user_id']; $sql_user=" 1 and users.id='$cu' "; if ($crdatefilt!=''){ } } } $query= $sql_user; $td = new TimeDate(); require_once("modules/Users/User.php"); $seed = new User(); $response = $seed->get_list($order_by,$query , $offset,10000,10000,false); for ($tn1=0;$tn1<(count($response['list']));$tn1++){ $al = $response['list'][$tn1]; $a=new User(); //echo "<br>AID".$al->id; $a->retrieve($al->id); //echo "<br>AID".$al->id; debugg("<br>".$a->name." ".$a->google_mmail_cts_c." ".$a->google_mpass_cts_c." ".$a->google_mctsreceive_c." contacts after date ".$a->google_mctsafterdater_c); $user_data[0] = $a->google_mmail_cts_contacts_c; $user_data[1]=$a->google_mpass_cts_contacts_c; $current_user=new User(); $current_user->retrieve($a->id); $user_google_mafterdate_c=$td->swap_formats($a->google_mctsafterdater_c, $td->get_date_format(true, $current_user), "Y-m-d"); // $a->google_contacts_ldrcv_c="2009-08-12T13:28:23.091Z"; $user_google_mafterdate_c=$a->google_contacts_ldrcv_c; debugg($oldrecords); if ($oldrecords==''){ $oldrecords=1; }else{ $oldrecords = $oldrecords+1; } // $a->google_mctsreceive_c=1; if ($user_data[0]!="" and $user_data[1]!="" and ($a->google_mctsreceive_c=="on" or $a->google_mctsreceive_c=="1")){ $max_results = 500; if ($user_google_mafterdate_c!=''){ $newfilterdate =$user_google_mafterdate_c; $datefilter = "&updated-min=".$newfilterdate; }else{ $newfilterdate ="2001-01-01T13:22:52.080"; $datefilter = "&updated-min=".$newfilterdate; } $max_results = 500; $oldrecords =1; $limit="/?start-index=$oldrecords&max-results=$max_results".$datefilter; $this->get_google_contacts_list(array($user_data[0],$user_data[1]),$existing_doc_id1,$sugarid,$user_google_mafterdate_c,$limit,$current_user); } } } function get_google_contacts_list($user_data,$docid,$sugarid,$google_mdocsafterdater_c,$limit,$current_user){ require_once('modules/Contacts/Contact.php'); require_once('include/formbase.php'); require_once('include/upload_file.php'); require_once('include/SugarEmailAddress/SugarEmailAddress.php'); $_SESSION["called_from_sync"]=true; global $path; debugg("<br>$user_data[0]"); $newtoken=$this->getAuthCode($user_data[0],$user_data[1],'cp'); $headers = array(); $headers[] = "MIME-Version: 1.0"; $headers[] = "Host:www.google.com"; $headers[] = "Accept: text/xml"; $headers[] = "GET / HTTP/1.1"; $headers[] = "Cache-Control: no-cache"; $headers[] = "Authorization: GoogleLogin auth=".$newtoken.""; $curl = curl_init(); curl_setopt($curl,CURLOPT_URL,$itemURL); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "GET"); curl_setopt($curl, CURLOPT_COOKIEJAR, "cookieFileName"); curl_setopt($curl, CURLOPT_COOKIEFILE, "cookieFileName"); curl_setopt ($curl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)"); curl_setopt($curl, CURLOPT_FOLLOWLOCATION,4); // follow redirects recursively $result = curl_exec($curl); $info = curl_getinfo($curl); $headers = ""; curl_close($curl); $headers = array(); $headers[] = "MIME-Version: 1.0"; $headers[] = "Host:www.google.com"; # $headers[] = "Content-length: ".strlen($_entry); $headers[] = "Content-type: application/atom+xml"; $headers[] = "Cache-Control: no-cache"; $headers[] = "Authorization: GoogleLogin auth=".$newtoken.""; $curl = curl_init(); curl_setopt($curl,CURLOPT_URL,$itemURL); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "GET"); curl_setopt($curl, CURLOPT_COOKIEJAR, "cookieFileName"); curl_setopt($curl, CURLOPT_COOKIEFILE, "cookieFileName"); curl_setopt ($curl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt ($curl, CURLOPT_CRLF, TRUE); curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)"); curl_setopt($curl, CURLOPT_FOLLOWLOCATION,4); // follow redirects recursively $itemURL = "http://www.google.com/m8/feeds/contacts/".trim($user_data[0])."/full".$limit; $itemURL.="&v=3"; //echo $itemURL; debugg($itemURL); curl_setopt($curl,CURLOPT_URL,$itemURL); $result = curl_exec($curl); //echo $result; //exit; $xmlparse = new ParseXMLGoogle(); $xml = $xmlparse->GetXMLTree($result); //print_r($xml); //exit; $last_update_date= $xml['FEED'][0]['UPDATED'][0]['VALUE']; $last_update_date_char=$last_update_date; $last_update_date = explode("T",$last_update_date); $last_update_date=$last_update_date[0]; //print_r($xml); //exit; $ad = $xml['FEED'][0]['ENTRY']; $totnr=count($xml['FEED'][0]['ENTRY']); debugg("<br>total contacts number to download ".$totnr); $_SESSION["called_from_sync"]=true; for ($n=0;$n<=$totnr;$n++){ $phone_home=""; $phone_work=""; $phone_mobile=""; $phone_home_fax=""; $phone_other=""; $birthday=""; $s=array(); $docref = $ad[$n]; $adr = $docref['ID'][0]['VALUE']; $sent_sugar_id=""; $sugar_owner_id=""; $allow_change_insugar=false; //print_r($docref); //exit; $title=$docref['GD:ORGANIZATION'][0]['GD:ORGTITLE'][0]['VALUE']; $birthday=$docref['GCONTACT:BIRTHDAY'][0]['ATTRIBUTES']['WHEN']; $ext_prop= $docref['GD:EXTENDEDPROPERTY']; if (is_array($ext_prop)){ foreach ($ext_prop as $kk1=>$vv1){ if ($vv1['ATTRIBUTES']['NAME']=="sugar_id"){ $sent_sugar_id=$vv1['ATTRIBUTES']['VALUE']; } if ($vv1['ATTRIBUTES']['NAME']=="sugar_owner_id"){ $sugar_owner_id=$vv1['ATTRIBUTES']['VALUE']; } } // print_r($ext_prop); // exit; } if ($sugar_owner_id==$current_user->id){ $allow_change_insugar=true; } $sq = "select * from config where category='info' and name='sync_cts_back_all_users'"; global $db; $result= $db->query($sq, true); while($row = $db->fetchByAssoc($result)) { $sync_cts_back_all_users = $row['value']; } if ($sync_cts_back_all_users==""){ $sq = "insert into config (category,name,value) values ('info','sync_cts_back_all_users',0)"; $result= $db->query($sq, 1); $sync_cts_back_all_users = false; } if ($sync_cts_back_all_users==true or $sync_cts_back_all_users==1){ $allow_change_insugar=true; } debugg("allow change in sugar ".$allow_change_insugar." sugar_owner_id ".$sugar_owner_id." sent_sugar_id ".$sent_sugar_id." current user ".$current_user->id); //exit; $phone_arr = $docref['GD:PHONENUMBER']; $totnremail=count($phone_arr); for ($nemail=0;$nemail<=$totnremail;$nemail++){ $array_number = $nemail-1; $mailadd = $phone_arr[$array_number]['ATTRIBUTES']; if ($mailadd['REL']=="http://schemas.google.com/g/2005#home"){ $phone_home = $phone_arr[$array_number]['VALUE']; } if ($mailadd['REL']=="http://schemas.google.com/g/2005#work"){ $phone_work = $phone_arr[$array_number]['VALUE']; } if ($mailadd['REL']=="http://schemas.google.com/g/2005#mobile"){ $phone_mobile = $phone_arr[$array_number]['VALUE']; } if ($mailadd['REL']=="http://schemas.google.com/g/2005#home_fax"){ $phone_home_fax = $phone_arr[$array_number]['VALUE']; } if ($mailadd['REL']=="http://schemas.google.com/g/2005#other"){ $phone_other = $phone_arr[$array_number]['VALUE']; } } ///$bean->old_link_edit_c=$xml['ENTRY'][0]['LINK'][2]['ATTRIBUTES']['HREF']; $mail_arr = $docref['GD:EMAIL']; $totnremail=count($mail_arr); $_REQUEST['emailAddress1']=""; $_REQUEST['emailAddress2']=""; $_REQUEST['emailAddress3']=""; for ($nemail=0;$nemail<=$totnremail;$nemail++){ $array_number = $nemail-1; $mailadd = $mail_arr[$array_number]['ATTRIBUTES']; if ($mailadd['REL']=="http://schemas.google.com/g/2005#work"){ $mailadd_work = $mailadd['ADDRESS']; $new_addrs[]=$mailadd['ADDRESS']; if ($_REQUEST['emailAddress1']==""){ $_REQUEST['emailAddress1']= $mailadd_work; }else{ if ($_REQUEST['emailAddress2']==""){ $_REQUEST['emailAddress2']= $mailadd_work; }else{ $_REQUEST['emailAddress3']= $mailadd_work; } } } if ($mailadd['REL']=="http://schemas.google.com/g/2005#other"){ $mailadd_other = $mailadd['ADDRESS']; $new_addrs[]=$mailadd['ADDRESS']; if ($_REQUEST['emailAddress1']==""){ $_REQUEST['emailAddress1']= $mailadd_other; }else{ if ($_REQUEST['emailAddress2']==""){ $_REQUEST['emailAddress2']= $mailadd_other; }else{ $_REQUEST['emailAddress3']= $mailadd_other; } } } if ($mailadd['REL']=="http://schemas.google.com/g/2005#home"){ $mailadd_home = $mailadd['ADDRESS']; $new_addrs[]=$mailadd['ADDRESS']; if ($_REQUEST['emailAddress1']==""){ $_REQUEST['emailAddress1']= $mailadd_home; }else{ if ($_REQUEST['emailAddress2']==""){ $_REQUEST['emailAddress2']= $mailadd_home; }else{ $_REQUEST['emailAddress3']= $mailadd_home; } } } } $s["last_upd"]= $docref['UPDATED'][0]['VALUE']; $s["title"]= $docref['TITLE'][0]['VALUE']; //090509 $s['old_link_edit']= $docref['LINK'][2]['ATTRIBUTES']['HREF']; $s["title"]=$s["title"]; $newname =preg_split ("/\s+/", $s["title"]); $newname_first =$newname[0]; $newname[0]=""; $newname_last =implode(" ",$newname); $ct = explode("T",$s["last_upd"]); $ct = $ct[0]; $dateok=true; if ($dateok==true){ unset($Contact); $Contact = new Contact(); $Contact->disable_row_level_security=true; unset($Document); $Document = new Contact(); $Document->disable_row_level_security=true; $sq = "select google_contacts_rel.*,contacts_cstm.id_c from google_contacts_rel left join contacts_cstm on contacts_cstm.id_c=google_contacts_rel.obj_id where google_contacts_rel.old_id_c='$adr' and google_contacts_rel.user_id='".$current_user->id."'"; debugg("sql find local id ".$sq); $r = $Document->db->query($sq); $skip_doc = false; $islocal = 0; $nosugarfile=true; while($row = $Document->db->fetchByAssoc($r, -1, false)) { //050110 if nothing in contacts_cstm but the contact exists if ($row["id_c"]=="" and $row["obj_id"]!=""){ $sq2 = "select google_contacts_rel.*,contacts.id as id_c from google_contacts_rel left join contacts on contacts.id=google_contacts_rel.obj_id where google_contacts_rel.old_id_c='$adr' and google_contacts_rel.user_id='".$current_user->id."'"; debugg("sql find local id case2: ".$sq2); $r2=$db->query($sq2); $row=$db->fetchByAssoc($r2); } // print_r($row); if ($row['old_updated_c']==$s["last_upd"]){ $skip_doc = true; debugg("<br>skip contact save for ".$newname_first." ".$s["title"]." ... no changes since ".$s["last_upd"]." ".$row['old_updated_c']); } if ($row["id_c"]!=""){ $islocal = $row["id_c"]; $nosugarfile=false; debugg("contact array ".var_export($row,true)); debugg("<br>contact created in sugar.sugar id ".$islocal); } else{ $nosugarfile=true; } } if ($nosugarfile==true){ debugg("<br>contact created in google ... do nothing<br>"); } ############################################################### ############################################################### if ($skip_doc == false){ if ($nosugarfile==false){ if ($islocal!==0){ unset($sugarEmailAddress); $sugarEmailAddress = new SugarEmailAddress(); $sugarEmailAddress->save($islocal, "Contacts"); $Contact->retrieve($islocal); $fullname_sugar = $Contact->first_name." ".$Contact->last_name; $fullname_google = $newname_first." ".$newname_last; if ($fullname_sugar!=$fullname_google){ $Contact->first_name=utf8_encode($newname_first); $Contact->last_name=utf8_encode($newname_last); debugg("<br>Contact name changed : Full name sugar ".$fullname_sugar." Full name google ".$fullname_google." sugar id ".$Contact->id); } $Contact->phone_home = $phone_home; $Contact->phone_work = $phone_work; $Contact->phone_mobile = $phone_mobile; $Contact->phone_fax = $phone_home_fax; $Contact->phone_other = $phone_other; $Contact->old_updated_c=$s["last_upd"]; $Contact->title=utf8_encode($title); //290111 dan $Contact->email1= $_REQUEST['emailAddress1']; $Contact->email2= $_REQUEST['emailAddress2']; $Contact->email3= $_REQUEST['emailAddress3']; //090509 $Contact->old_link_edit_c=$s['old_link_edit']; debugg("old updated c ".$Contact->old_updated_c." old link edit ".$Contact->old_link_edit_c); if ($allow_change_insugar==true){ $Contact->save(); global $db; $sq = "update contacts set birthdate='".$birthday."' where id='".$Contact->id."' and deleted=0"; $db->query($sq); } debugg("<br>is local <br>"); }else{ } } } } } $sq = "update users_cstm set google_mctsafterdater_c='".$last_update_date."',google_contacts_ldrcv_c='".$last_update_date_char."' where id_c='".$current_user->id."'"; debugg("last user received date sql".$sq); $Document->db->query($sq); $filename = $sugarid; curl_close($curl); ## $_SESSION["called_from_sync"] = false; } } ?>Save