diff -uNr dcl_importer/dcl_importer.module dcl.new/dcl_importer.module --- dcl_importer/dcl_importer.module 2008-01-09 23:24:07.000000000 -0600 +++ dcl.new/dcl_importer.module 2008-06-03 12:12:51.000000000 -0500 @@ -86,10 +86,16 @@ $provider = $edit['provider']; $password = $edit['password']; $username = explode('@', $edit['username']); - if($provider == 'M' ) { - $username[0] = $username[0].'@hotmail.com'; - } elseif( $provider == 'S' ) { - $username[0] = $edit['username']; + + // TODO: WHY IS THIS DONE HERE? WHY NOT IN THE IMPORTER? + switch ($provider) { + case 'M': + $username[0] = $username[0].'@hotmail.com'; + break; + case 'S': + case 'G': + $username[0] = $edit['username']; + break; } $contacts = dcl_importer_get_contacts($username[0], $password, $provider); diff -uNr dcl_importer/dcl_wrapper.inc dcl.new/dcl_wrapper.inc --- dcl_importer/dcl_wrapper.inc 2008-01-11 18:20:00.000000000 -0600 +++ dcl.new/dcl_wrapper.inc 2008-06-03 12:09:39.000000000 -0500 @@ -25,10 +25,10 @@ * Import Gmail Contacts */ function importGmail($login, $password) { - include ('scripts/importGmail.class.php'); + include ('scripts/importGData.class.php'); global $names; global $emails; - $gmailer = new GMailer(); + $gmailer = new GDataMailer(); /* if($gmailer->created) { $gmailer->setLoginInfo($login, $password, $my_timezone, 0); @@ -46,9 +46,11 @@ } */ $contacts = $gmailer->getAddressbook($login, $password); - foreach($contacts as $name => $email) { - $names[] = $name; - $emails[] = $email; + if (is_array($contacts)) { + foreach($contacts as $name => $email) { + $names[] = $name; + $emails[] = $email; + } } return array($names, $emails); } diff -uNr dcl_importer/scripts/importGData.class.php dcl.new/scripts/importGData.class.php --- dcl_importer/scripts/importGData.class.php 1969-12-31 18:00:00.000000000 -0600 +++ dcl.new/scripts/importGData.class.php 2008-06-03 12:09:30.000000000 -0500 @@ -0,0 +1,110 @@ + $URL, + CURLOPT_REFERER => '', + CURLOPT_SSL_VERIFYPEER => false, + CURLOPT_SSL_VERIFYHOST => true, + CURLOPT_RETURNTRANSFER => true, + CURLOPT_FOLLOWLOCATION => true, + ); + if (null != $auth) { + $opts[CURLOPT_HTTPHEADER] = array( + 'Authorization: GoogleLogin auth='.$auth, + ); + } + curl_setopt_array($curl, $opts); + return $curl; + } + + function useCurlForPost($curl, $params) { + curl_setopt($curl, CURLOPT_POST, true); + curl_setopt($curl, CURLOPT_POSTFIELDS, $params); + return $curl; + } + + function getAuthToken($login, $password) { + $curl = $this->useCurlForPost($this->newCurlSession(self::$url_ClientLogin), array( + 'accountType' => 'HOSTED_OR_GOOGLE', + 'Email' => $login, + 'Passwd' => $password, + 'service' => 'cp', + 'source' => 'Drupal-Contact-Importer', + )); + $resp = curl_exec($curl); + + // Look for the important stuff: + preg_match_all('/Auth=([^\s]*)/si', $resp, $matches); + if (isset($matches[1][0])) { + return $matches[1][0]; + } else { + return false; + } + } + + function getAddressbook($login, $password) { + // check if username and password was given: + if ((isset($login) && trim($login)=="") || (isset($password) && trim($password)=="")) + { + $args = func_get_args(); + drupal_set_message('Incorrect login information given: '.print_r($args, true), 'error'); + return false; + } + + // Get the GData auth token: + $auth = $this->getAuthToken($login, $password); + if (false === $auth) { + drupal_set_message('Login failed', 'error'); + return false; + } + + $curl = $this->newCurlSession(self::$url_Feed, $auth); + $data = curl_exec($curl); + + $doc = new DOMDocument(); + $doc->loadXML($data); + $path = new DOMXPath($doc); + $path->registerNamespace('a', 'http://www.w3.org/2005/Atom'); + $path->registerNamespace('gd', 'http://schemas.google.com/g/2005'); + $nodes = $path->query('//a:entry'); + $num = $nodes->length; + + $contacts = array(); + for ($x = 0; $x < $num; $x++) { + $entry = $nodes->item($x); + $tnodes = $path->query('a:title', $entry); + $nnode = $tnodes->item(0); + $name = $nnode->textContent; + $enodes = $path->query('gd:email', $entry); + $mnode = $enodes->item(0); + $email = $mnode->getAttribute('address'); + // NOTE: Keep in mind that $mnode->getAttribute('rel') tells you what kind of email it is. + // NOTE: Also remember that there can be multiple emails per entry! + if (empty($name)) { + $contacts[] = $email; + } else { + $contacts[$name] = $email; + } + } + + return $contacts; + } +} +?>