Edit file File name : MyCurlContacts021211.php Content :<?PHP class MyCurlContacts { var $getHeaders = true;//headers will be added to output var $getContent = true; //contens will be added to output var $followRedirects = true; //should the class go to another URL, if the current is "HTTP/1.1 302 Moved Temporarily" var $fCookieFile; var $fSocket; var $uername; var $password; function MyCurlContacts() { global $path; // echo "<br>a ".$path; // $this->fCookieFile = tempnam($path, "g_"); } function init() { return $this->fSocket = curl_init(); } function setopt($opt, $value) { return curl_setopt($this->fSocket, $opt, $value); } function load_defaults() { $this->setopt(CURLOPT_RETURNTRANSFER, 1); $this->setopt(CURLOPT_FOLLOWLOCATION, $this->followRedirects); $this->setopt(CURLOPT_REFERER, "http://google.com"); $this->setopt(CURLOPT_VERBOSE, false); $this->setopt(CURLOPT_SSL_VERIFYPEER, false); $this->setopt(CURLOPT_SSL_VERIFYHOST, false); $this->setopt(CURLOPT_HEADER, $this->getHeaders); $this->setopt(CURLOPT_NOBODY, !$this->getContent); $this->setopt(CURLOPT_COOKIEJAR, $this->fCookieFile); $this->setopt(CURLOPT_COOKIEFILE, $this->fCookieFile); $this->setopt(CURLOPT_USERAGENT, "MyCurl"); // enable or disable the next line if you don't get any response from google // $this->setopt(CURLOPT_POST, 1); $this->setopt(CURLOPT_CUSTOMREQUEST,'POST'); if($fp) $this->setopt(CURLOPT_STDERR, $fp); } function destroy() { return curl_close($this->fSocket); } function head($url) { $this->init(); if($this->fSocket) { $this->getHeaders = true; $this->getContent = false; $this->load_defaults(); $this->setopt(CURLOPT_POST, 0); $this->setopt(CURLOPT_CUSTOMREQUEST,'HEAD'); $this->setopt(CURLOPT_URL, $url); $result = curl_exec($this->fSocket); $this->destroy(); return $result; } return 0; } function get($url) { $this->init(); if($this->fSocket) { $this->load_defaults(); $this->setopt(CURLOPT_POST, 0); $this->setopt(CURLOPT_CUSTOMREQUEST,'GET'); $this->setopt(CURLOPT_URL, $url); $result = curl_exec($this->fSocket); $this->destroy(); return $result; } return 0; } function post_login($url, $post_data, $arr_headers=array(), $http_code="") { $this->init(); if($this->fSocket) { $post_data = $this->compile_post_dataGC($post_data); $this->load_defaults(); if(!empty($post_data)) $this->setopt(CURLOPT_POSTFIELDS, $post_data); if(!empty($arr_headers)) $this->setopt(CURLOPT_HTTPHEADER, $arr_headers); $this->setopt(CURLOPT_URL, $url); $result = curl_exec($this->fSocket); $nl = explode("Location:",$result); $nl1 = explode("Content-Type:",$nl[1]); $new_loc = $nl1[0]; $http_code = curl_getinfo($this->fSocket, CURLINFO_HTTP_CODE); $this->destroy(); return $result; } return 0; } //$user, $pword, $service='cl' function getAuthCode($user, $pword,$service='cp') { $buf=sslToHostGC('www.google.com', 'post', '/accounts/ClientLogin', '', 'Email='.$user.'&Passwd='.$pword.'&service='.$service.'&source=phord-gcal.php-1&accountType=HOSTED_OR_GOOGLE'); $code = returnCodeGC($buf); if ($code === 200) { $lines=explode("\n", $buf); foreach ($lines as $line) { if (preg_match("/^Auth=(\S+)/i", $line, $matches)) { return $matches[1]; } } } else { if (!isset($_REQUEST['pass'])){ echo "<br>The system could NOT connect to Google Contacts sucessfully. Please check your Google login credentials."; } //echo "<br>User : ".$user." Script aborted. Please check all login credentials of your plugin users. Finished"; debugg("Error ($code) retrieving auth code"); } } //dmc 250510 encode fields sent to google to support & chars function enctog($val){ //$val=htmlentities($val,ENT_QUOTES, 'UTF-8'); $val=html_entity_decode($val,ENT_QUOTES); // take care at single/double quotes sugar saves encoded to db google needs non ecoded $val=htmlspecialchars($val,ENT_NOQUOTES); return $val; } function insert_cnt_onl($bean_array,$user){ $old_row=$this->get_old_data($bean_array->id); $bean=new ContactsClass(); $bean->retrieve($bean_array->id); // print_r($bean); $newtoken=$user['token']; $em_addr=""; $count=0; $emails = array(); $emailStr = ''; while(isset($_POST['ContactsemailAddress' . $count])) { $emailStr = ($_POST['ContactsemailAddress' . $count++]); if ($emailStr!=""){ if ($count==1){ $em_addr .= "<gd:email rel='http://schemas.google.com/g/2005#work' address='$emailStr' />"; } if ($count==2){ $em_addr .= "<gd:email rel='http://schemas.google.com/g/2005#home' address='$emailStr' />"; } if ($count==3){ $em_addr .= "<gd:email rel='http://schemas.google.com/g/2005#other' address='$emailStr' />"; } } } //while $count=0; while(isset($_POST['emailAddress' . $count])) { $emailStr = ($_POST['emailAddress' . $count++]); if ($emailStr!=""){ if ($count==1){ $em_addr .= "<gd:email rel='http://schemas.google.com/g/2005#work' address='$emailStr' />"; } if ($count==2){ $em_addr .= "<gd:email rel='http://schemas.google.com/g/2005#home' address='$emailStr' />"; } if ($count==3){ $em_addr .= "<gd:email rel='http://schemas.google.com/g/2005#other' address='$emailStr' />"; } } } //while $count=0; // while(isset($_POST['emailAddress' . $count])) { if (1==2){ if ($bean->email1!=""){ echo "<br>".$bean->email1; $em_addr .= "<gd:email rel='http://schemas.google.com/g/2005#work' address='".$bean->email1."' />"; } if ($bean->email2!=""){ $em_addr .= "<gd:email rel='http://schemas.google.com/g/2005#home' address='".$bean->email2."' />"; } if ($bean->email3!=""){ $em_addr .= "<gd:email rel='http://schemas.google.com/g/2005#other' address='".$bean->email3."' />"; echo "<br>other ".$bean->email3; } } // } // } //while $ca=1; if (is_array($bean->emailAddress->addresses)){ foreach ($bean->emailAddress->addresses as $k=>$v){ if ($ca==1 and $v['email_address']!=""){ $em_addr .= "<gd:email rel='http://schemas.google.com/g/2005#work' address='".$v['email_address']."' />"; } if ($ca==2 and $v['email_address']!=""){ $em_addr .= "<gd:email rel='http://schemas.google.com/g/2005#home' address='".$v['email_address']."' />"; } if ($ca==3 and $v['email_address']!=""){ $em_addr .= "<gd:email rel='http://schemas.google.com/g/2005#other' address='".$v['email_address']."' />"; } $ca++; // echo "<br>adr ".$v['email_address']; } } $tel = ""; if(trim($bean->phone_home)!=""){ $tel .= "<gd:phoneNumber rel='http://schemas.google.com/g/2005#home' >".$this->enctog($bean->phone_home)."</gd:phoneNumber>"; } if(trim($bean->phone_work)!=""){ $tel .= "<gd:phoneNumber rel='http://schemas.google.com/g/2005#work' primary='true'>".$this->enctog($bean->phone_work)."</gd:phoneNumber>"; } if(trim($bean->phone_mobile)!=""){ $tel .= "<gd:phoneNumber rel='http://schemas.google.com/g/2005#mobile' >".$this->enctog($bean->phone_mobile)."</gd:phoneNumber>"; } if(trim($bean->phone_fax)!=""){ $tel .= "<gd:phoneNumber rel='http://schemas.google.com/g/2005#home_fax' >".$this->enctog($bean->phone_fax)."</gd:phoneNumber>"; } if(trim($bean->phone_other)!=""){ $tel .= "<gd:phoneNumber rel='http://schemas.google.com/g/2005#other' >".$this->enctog($bean->phone_other)."</gd:phoneNumber>"; } $p1add=""; $p2add=""; $post_address=""; if ($bean->primary_address_street!="" or $bean->primary_address_city!="" or $bean->primary_address_state!="" or $bean->primary_address_country!=""){ if ($bean->primary_address_street!=""){ $p1add .= $this->enctog($bean->primary_address_street)."\n"; } if ($bean->primary_address_city!=""){ $p1add .= $this->enctog($bean->primary_address_city); } if ($bean->primary_address_state!=""){ $p1add .= " ".$this->enctog($bean->primary_address_state); } if ($bean->primary_address_postalcode!=""){ $p1add .= " ".$this->enctog($bean->primary_address_postalcode); } if ( $bean->primary_address_city!="" or $bean->primary_address_state!="" or $bean->primary_address_postalcode!=""){ $p1add .= "\n"; } if ($bean->primary_address_country!=""){ $p1add .= $this->enctog($bean->primary_address_country)."\n"; } $new_address_work=" <gd:structuredPostalAddress rel='http://schemas.google.com/g/2005#work' primary='true'> <gd:city>".$this->enctog($bean->primary_address_city)."</gd:city> <gd:street>".$this->enctog($bean->primary_address_street)."</gd:street> <gd:region>".$this->enctog($bean->primary_address_state)."</gd:region> <gd:postcode>".$this->enctog($bean->primary_address_postalcode)."</gd:postcode> <gd:country>".$this->enctog($bean->primary_address_country)."</gd:country> <gd:formattedAddress> $p1add </gd:formattedAddress> </gd:structuredPostalAddress> "; # $p1add = $bean->primary_address_street." ".$bean->primary_address_city." ".$bean->primary_address_state." ".$bean->primary_address_postalcode." ".$bean->primary_address_country; } if ($bean->alt_address_street!="" or $bean->alt_address_city!="" or $bean->alt_address_state!="" or $bean->alt_address_country!=""){ if ($bean->alt_address_street!=""){ $p2add .= $this->enctog($bean->alt_address_street)."\n"; } if ($bean->alt_address_city!=""){ $p2add .= $this->enctog($bean->alt_address_city); } if ($bean->alt_address_state!=""){ $p2add .= " ".$this->enctog($bean->alt_address_state); } if ($bean->alt_address_postalcode!=""){ $p2add .= " ".$this->enctog($bean->alt_address_postalcode); } if ( $bean->alt_address_city!="" or $bean->alt_address_state!="" or $bean->alt_address_postalcode!=""){ $p2add .= "\n"; } if ($bean->alt_address_country!=""){ $p2add .= $this->enctog($bean->alt_address_country)."\n"; } # $p2add = $bean->alt_address_street." ".$bean->alt_address_city." ".$bean->alt_address_state." ".$bean->alt_address_postalcode." ".$bean->alt_address_country; } $new_address_other=" <gd:structuredPostalAddress rel='http://schemas.google.com/g/2005#other' primary='true'> <gd:city>".$this->enctog($bean->alt_address_city)."</gd:city> <gd:street>".$this->enctog($bean->alt_address_street)."</gd:street> <gd:region>".$this->enctog($bean->alt_address_state)."</gd:region> <gd:postcode>".$this->enctog($bean->alt_address_postalcode)."</gd:postcode> <gd:country>".$this->enctog($bean->alt_address_country)."</gd:country> <gd:formattedAddress> $p1add </gd:formattedAddress> </gd:structuredPostalAddress> "; $new_address_other=""; if ($p1add!=""){ $post_address .= "<gd:postalAddress rel='http://schemas.google.com/g/2005#work' primary='true'>$p1add</gd:postalAddress>"; } if ($p2add!=""){ $post_address .= "<gd:postalAddress rel='http://schemas.google.com/g/2005#other'>$p2add</gd:postalAddress>"; } debugg($emailStr); // exit; $title_account = "<gd:organization rel='http://schemas.google.com/g/2005#work'><gd:orgName> ".$this->enctog($bean->account_name)." </gd:orgName><gd:orgTitle> ".$this->enctog($bean->title)." </gd:orgTitle></gd:organization>"; $sugar_data=" <gd:extendedProperty name=\"sugar_id\" value=\"".$bean->id."\"/> <gd:extendedProperty name=\"sugar_owner_id\" value=\"".$bean->assigned_user_id."\"/> "; global $db; $sq="select * from contacts where id='".$bean->id."'"; $r=$db->query($sq); $rcts=$db->fetchByAssoc($r); $brt=$rcts['birthdate']; if ($brt!="" and $brt!="0000-00-00"){ $birthday="<gContact:birthday when='".$brt."'/>"; }else{ $birthday=""; } $new_name="<gd:name> <gd:givenName>".$this->enctog($bean->last_name)."</gd:givenName> <gd:familyName>".$this->enctog($bean->first_name)."</gd:familyName> <gd:fullName>".$this->enctog($bean->first_name." ".$bean->last_name)."</gd:fullName> </gd:name>"; $bean->old_link_edit_c=""; $bean->old_link_edit_c=$old_row['old_link_edit_c']; $bean->old_id_c=""; $bean->old_id_c=$old_row['old_id_c']; //$group="<gContact:systemGroup id='Contacts' />"; //$group="<gContact:groupMembershipInfo deleted='false' href='http://www.google.com/m8/feeds/groups/".trim($user['username'])."/base/6'/>"; //echo "\n <br> old link edit ".$bean->old_link_edit_c; //<title type='text'>".$bean->first_name." ".$bean->last_name." </title> //$post_address //$group1=$this->get_google_groups($user['username'],$newtoken); // $group.="<gContact:groupMembershipInfo deleted='false' href='".urldecode($group1)."'/>"; global $current_user; if ($current_user->google_put_in_mycontacts_c=="on" or $current_user->google_put_in_mycontacts_c==1){ $group="<gContact:groupMembershipInfo deleted='false' href='http://www.google.com/m8/feeds/groups/".trim($user['username'])."/base/6'/>"; } if ($current_user->google_put_in_sugar_c=="on" or $current_user->google_put_in_sugar_c==1){ $group1=$this->get_google_groups($user['username'],$newtoken); $group.="<gContact:groupMembershipInfo deleted='false' href='".urldecode($group1)."'/>"; } //$group="<gContact:groupMembershipInfo deleted='false' id='6c74073f0e7dec53'/>"; ///6c74073f0e7dec53 debugg(" group :".$group); if ($bean->old_link_edit_c==''){ $xml=" <entry xmlns=\"http://www.w3.org/2005/Atom\" xmlns:gd=\"http://schemas.google.com/g/2005\" xmlns:gContact='http://schemas.google.com/contact/2008'> $group <category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/contact/2008#contact' /> $new_name <content type='text'>".$this->enctog($bean->description)."</content> $tel $em_addr $new_address_work $new_address_other $title_account $sugar_data $birthday </entry>"; debugg($xml); }else{ $bean->old_id_c=str_replace("base","full",$bean->old_id_c) ; $bean->old_link_edit_c=str_replace("base","full",$bean->old_link_edit_c) ; //<title type='text'>".$bean->first_name." ".$bean->last_name." </title> //$post_address $xml=" <entry xmlns=\"http://www.w3.org/2005/Atom\" xmlns:gd=\"http://schemas.google.com/g/2005\" xmlns:gContact='http://schemas.google.com/contact/2008'> <id>".trim($bean->old_id_c)."</id> $group <category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/contact/2008#contact' /> $new_name <link rel='self' type='application/atom+xml' href='".trim($bean->old_id_c)."' /> <link rel='edit' type='application/atom+xml' href='".trim($bean->old_link_edit_c)."' /> $tel $em_addr $new_address_work $new_address_other $title_account $sugar_data $birthday </entry>"; } // echo "<br>\n".$xml; $headers = array(); $headers[] = "Host : www.google.com"; $itemURL = "http://www.google.com/m8/feeds/contacts/".trim($user['username'])."/full"; $itemURL.="/?v=3.0"; $header = array(); if ($bean->old_link_edit_c!=''){ $header[] = "If-Match: *"; } $header[] = "MIME-Version: 1.0"; $header[] = "Accept: text/xml"; $header[] = "Authorization: GoogleLogin auth=".$newtoken.""; $header[] = "Content-length: ".strlen($xml); $header[] = "Content-type: application/atom+xml"; $header[] = "Cache-Control: no-cache"; $header[] = "Connection: close \r\n"; // $header[]="GData-Version: 3"; $header[] = $xml; if ($bean->old_link_edit_c==''){ $entry=curlToHostGC($itemURL, 'POST',$header); }else{ $itemURL= $bean->old_link_edit_c; //echo "<br>iuaa \n".$itemURL; //$itemURL.="?v=3"; $itemURL=str_replace("base","full",$itemURL) ; $entry=curlToHostGC($itemURL, 'PUT',$header); } #echo "<br>\n".print_r($header); #echo "<br>\n ".$entry."\n"; // exit; /////CHECK IF CHANGED BEFORE if ($bean->old_link_edit_c!=''){ $xmlparse = &new ParseXMLGoogle(); $xml = $xmlparse->GetXMLTree($entry); //print_r($xml); $received_link=$xml['ENTRY'][0]['LINK'][2]['ATTRIBUTES']['HREF']; $received_link=str_replace("base","full",$received_link) ; //echo "<br>\n rec link ".$received_link; if ($bean->old_link_edit_c!=$received_link){ $xml=" <entry xmlns=\"http://www.w3.org/2005/Atom\" xmlns:gd=\"http://schemas.google.com/g/2005\"> <id>".trim($bean->old_id_c)."</id> <category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/contact/2008#contact' /> <title type='text'>".$this->enctog($bean->first_name." ".$bean->last_name)." </title> <link rel='self' type='application/atom+xml' href='".trim($bean->old_id_c)."' /> <link rel='edit' type='application/atom+xml' href='".trim($bean->old_link_edit_c)."' /> $tel $em_addr $post_address $t1 </entry>"; $itemURL=$received_link; $itemURL=str_replace("base","full",$itemURL) ; $entry=curlToHostGC($itemURL, 'PUT',$header); debugg("conflict detected put again to ".$itemURL); } } // debugg("--------contact to google------------"); debugg($xml); debugg("itmurl ".$itemURL); debugg("header ".var_export($header,true)); debugg("response ".$entry); $result=$entry; debugg("\n<br>$filename1 **upload document to google result : \n". $entry); if ($result=="An unknown error has occurred." or $result==''){ debugg("<br>******** Error for document ".$filename1." id ".$evid." Not all xls are compatible with google docs"); $sql = "update contacts_cstm set s_google_c='',google_mresponse_c=\"".$result."\" where id_c='".$saveresult->id."'"; $bean->s_google_c=''; }else{ $xmlparse = &new ParseXMLGoogle(); $xml = $xmlparse->GetXMLTree($result); $bean->old_id_c=$xml['ENTRY'][0]['ID'][0]['VALUE']; $bean->old_updated_c=$xml['ENTRY'][0]['UPDATED'][0]['VALUE']; $bean->old_link_alt_c=$xml['ENTRY'][0]['CONTENT'][0]['ATTRIBUTES']['SRC']; $bean->old_link_edit_c=$xml['ENTRY'][0]['LINK'][2]['ATTRIBUTES']['HREF']; $bean->google_mresponse_c= $result; $bean->s_google_c='1'; debugg("<br>old link "); if ($bean->old_id_c==""){ $sql = "update contacts_cstm set s_google_c='',google_mresponse_c=\"".$result."\" where id_c='".$saveresult->id."'"; }else{ $sql = "update contacts_cstm set s_google_c='1',google_mresponse_c=\"".$result."\" where id_c='".$saveresult->id."'"; } $bean->old_link_self_c =$id1[1]; } global $db,$current_user; $sq="delete from google_contacts_rel where google_contacts_rel.user_id='".$current_user->id."' and google_contacts_rel.obj_id='".$bean_array->id."'"; $db->query($sq); $sq="select date_modified,id from contacts where id='".$bean_array->id."' and deleted=0"; $r=$db->query($sq); $row=$db->fetchByAssoc($r); $sq = "insert into google_contacts_rel (id,user_id,obj_id,last_google_contacts_upload_c,old_id_c,old_link_edit_c) values ('".create_guid()."','".$current_user->id."','".$bean_array->id."','".$row['date_modified']."','".$bean->old_id_c."','".$bean->old_link_edit_c."') "; $db->query($sq); debugg("<br>sql update ".$sql); debugg("<br>sres ".$sres); } function search_group_in_xml($xml,$group_name){ $resp=$xml['FEED'][0]['ENTRY']; foreach ($resp as $k=>$v){ $gname=$v['TITLE'][0]['VALUE']; if ($gname==$group_name){ $gid=$v['ID'][0]['VALUE']; return $gid; } } return ""; } function get_google_groups($user,$newtoken){ global $default_sugar_folder; $group_name=$default_sugar_folder; if (isset($_REQUEST[$user]['google_group_id'])){ if ($_REQUEST[$user]['google_group_id']!=""){ return $_REQUEST[$user]['google_group_id']; } } //$newtoken=$this->getAuthCode($user_data[0],$user,'cp'); $headers = array(); $itemURL='http://www.google.com/m8/feeds/groups/'.urlencode($user).'/full'; $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); $xmlparse = &new ParseXMLGoogle(); $xml = $xmlparse->GetXMLTree($result); $existing_group=$this->search_group_in_xml($xml,$group_name); //print_r($xml); if ($existing_group==""){ $existing_group = $this->create_google_group($user,$newtoken,$group_name); }else{ } $_REQUEST[$user]['google_group_id']=$existing_group; //echo "<br>".$existing_group; // exit; return $existing_group; } function create_google_group($user,$newtoken,$group_name){ $xml='<atom:entry xmlns:gd="http://schemas.google.com/g/2005"><atom:title type="text">Salsa group</atom:title></atom:entry>'; $xml="<entry xmlns='http://www.w3.org/2005/Atom' xmlns:gd='http://schemas.google.com/g/2005'> <category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/contact/2008#group' /> <title>$group_name</title> <content>google</content> </entry>"; //<atom:category scheme="http://schemas.google.com/g/2005#kind" // term="http://schemas.google.com/contact/2008#group"/> /// <gd:extendedProperty name="more info about the group"> // <info>Nice people.</info> // </gd:extendedProperty> $headers = array(); $headers[] = "Host : www.google.com"; $itemURL = "http://www.google.com/m8/feeds/groups/".urlencode($user)."/full"; $itemURL = "http://www.google.com/m8/feeds/groups/".$user."/full"; //$itemURL.="/?v=3.0"; $header = array(); // if ($bean->old_link_edit_c!=''){ // $header[] = "If-Match: *"; // } $header[] = "MIME-Version: 1.0"; $header[] = "Accept: text/xml"; $header[] = "Authorization: GoogleLogin auth=".$newtoken.""; $header[] = "Content-length: ".strlen($xml); $header[] = "Content-type: application/atom+xml"; $header[] = "Cache-Control: no-cache"; $header[] = "GData-Version: 3.0"; $header[] = "Connection: close \r\n"; $header[] = $xml; $entry=curlToHostGC($itemURL, 'POST',$header); debugg("create new group: ".urldecode($entry)); $xmlparse = &new ParseXMLGoogle(); $xml = $xmlparse->GetXMLTree($entry); $group_id=$xml['ENTRY'][0]['ID'][0]['VALUE']; return $group_id; //print_r($xml); } function delete_contact_onl($bean_array,$user){ global $db,$current_user; $bean=new ContactsClass(); $bean->retrieve($bean_array->id); $newtoken=$user['token']; $old_row=$this->get_old_data($bean_array['id']); $xml=""; $headers = array(); $headers[] = "Host : www.google.com"; $itemURL = "http://www.google.com/m8/feeds/contacts/".trim($user['username'])."/full"; //$itemURL.="/?v=3"; $header = array(); $header[] = "MIME-Version: 1.0"; $header[] = "Accept: text/xml"; $header[] = "Authorization: GoogleLogin auth=".$newtoken.""; $header[] = "Content-type: application/atom+xml"; $header[] = "If-Match: *"; $header[] = "Cache-Control: no-cache"; $header[] = "Connection: close \r\n"; $bean->old_link_edit_c=$old_row['old_link_edit_c']; //echo "\n<br>old link edit ".$bean->old_link_edit_c; //exit; if ($bean->old_link_edit_c!=''){ $itemURL= $bean->old_link_edit_c; $itemURL=str_replace("base","full",$itemURL) ; //echo "\n<br>itemurl ".$itemURL; $entry=curlToHostGC($itemURL, 'DELETE',$header); } //echo "\n<br>".$entry."\n<br>"; //exit; debugg($xml); debugg("delete contact ".$itemURL); $sq="update google_contacts_rel set deleted_sent=1 where obj_id='".$bean_array['id']."' and user_id='".$current_user->id."'"; $db->query($sq); //echo "<br>".$sq; //$bean->google_contacts_delete_sent_c=1; //$bean->save(); $result=$entry; } function delete_contact_onl_bk($bean_array,$user){ global $db,$current_user; // $bean=new ContactsClass(); // $bean->retrieve($bean_array->id); $newtoken=$user['token']; $old_row=$this->get_old_data($bean_array['id']); $xml=""; $headers = array(); $headers[] = "Host : www.google.com"; $itemURL = "http://www.google.com/m8/feeds/contacts/".trim($user['username'])."/full"; //$itemURL.="/?v=3"; $header = array(); $header[] = "MIME-Version: 1.0"; $header[] = "Accept: text/xml"; $header[] = "Authorization: GoogleLogin auth=".$newtoken.""; $header[] = "Content-type: application/atom+xml"; $header[] = "Cache-Control: no-cache"; $header[] = "Connection: close \r\n"; $bean->old_link_edit_c=$old_row['old_link_edit_c']; echo "\n<br>old link edit ".$bean->old_link_edit_c; exit; if ($bean->old_link_edit_c!=''){ $itemURL= $bean->old_link_edit_c; $itemURL=str_replace("base","full",$itemURL) ; echo "\n<br>itemurl ".$itemURL; $entry=curlToHostGC($itemURL, 'DELETE',$header); } //echo "\n<br>".$entry."\n<br>"; debugg($xml); debugg($itemURL); $sq="update google_contacts_rel set deleted_sent=1 where obj_id='".$bean_array['id']."' and user_id='".$current_user->id."'"; $db->query($sq); //echo "<br>".$sq; //$bean->google_contacts_delete_sent_c=1; //$bean->save(); $result=$entry; } } //!function_exists("returnCode") function returnCodeGC($buf) { list($response) = explode("\n", $buf, 2); if (preg_match("|^HTTP/\d.\d (\d+)|", $response, $matches)) { return 0+$matches[1]; } return -1; } function dfputsGC($fp, $msg) { return fputs($fp, $msg); } function sslToHostGC($host,$method,$path,$headers='',$data='',$useragent=0) { $data .= "&accountType=HOSTED_OR_GOOGLE"; if (empty($method)) { $method = 'GET'; } $method = strtoupper($method); $fp = fsockopen("ssl://$host", 443, $errno, $err); if (FALSE === $fp) { debugg("Error $err opening socket!"); exit(1); } if ($method == 'GET') { $path .= '?' . $data; } dfputsGC($fp, "$method $path HTTP/1.0\r\n"); dfputsGC($fp, "Host: $host\r\n"); dfputsGC($fp,"Content-type: application/x-www-form-urlencoded\r\n"); dfputsGC($fp, "Content-length: " . strlen($data) . "\r\n"); dfputsGC($fp, "accountType: HOSTED_OR_GOOGLE "); if (strlen($headers)) { dfputsGC($fp, "$headers\r\n"); } if ($useragent) { dfputsGC($fp, "User-Agent: MSIE\r\n"); } dfputsGC($fp, "Connection: close\r\n\r\n"); if ($method == 'POST') { dfputsGC($fp, $data); } while (!feof($fp)) { $buf .= @fgets($fp,128); } fclose($fp); return $buf; } function sendToHostGC($host,$method,$path,$headers='',$data='',$useragent=0) { if (empty($method)) { $method = 'GET'; } $method = strtoupper($method); $fp = fsockopen($host, 80); if ($method == 'GET' && strlen($data)>0) { $path .= '?' . $data; } dfputsGC($fp, "$method $path HTTP/1.0\r\n"); dfputsGC($fp, "Host: $host\r\n"); if ($method == 'GET' && strlen($data)>0) { dfputsGC($fp,"Content-type: application/x-www-form-urlencoded\r\n"); } dfputsGC($fp, "Content-length: " . strlen($data) . "\r\n"); if (strlen($headers)) { dfputsGC($fp, "$headers\r\n"); } if ($useragent) { dfputsGC($fp, "User-Agent: MSIE\r\n"); } dfputsGC($fp, "Connection: close\r\n\r\n"); if ($method == 'POST') { dfputsGC($fp, $data); } while (!feof($fp)) { $buf .= fgets($fp,128); } fclose($fp); return $buf; } function curlToHostGC($url,$method,$headers=array('')) { ob_start(); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, strtoupper($method)); // curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_VERBOSE, 1); ########### debug curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1); // follow redirects recursively curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 16); // follow redirects recursively curl_setopt($ch, CURLOPT_TIMEOUT, 32); // follow redirects recursively $aa = curl_exec($ch); $rs=curl_getinfo($ch); curl_close($ch); $ret = ob_get_contents(); ob_end_clean(); debugg("send to google resp:".var_export($rs,true)); return $ret; } function compile_post_dataGC($post_data) { $o=""; if(!empty($post_data)) foreach ($post_data as $k=>$v) $o.= $k."=".urlencode($v)."&"; return substr($o,0,-1); } function read_headerGC($ch, $header) { $curl->headers[] = $header; return strlen($header); } // } ?>Save