Pergunta

Drupal tem um módulo importador de contas de Gmail que tem sido oh tão perto de trabalhar por cerca de 7 meses. Eu não tenho as costeletas para depurar o problema, o mantenedor desapareceu, e eu acho que essa é uma peça importante ter trabalho. Então eu me viro para StackOverflow para obter ajuda.

O Problema:. Quando você preencher suas credenciais e dizer o script para trazer de volta seus contatos, por padrão, ele puxa 25. Isso funciona bem

Quando você diga a ele para procurar mais contatos alterando a URL que você está consultando a partir deste:

http://www.google.com/m8/feeds/contacts/default/thin  

a esta:

http://www.google.com/m8/feeds/contacts/default/thin?max-results=1000

Você recebe o seguinte erro fatal:

Erro fatal: Chamada a um getAttribute() função de membro em um não-objeto em path/to/site/sites/all/modules/dcl_importer/scripts/importGData.class.php na linha 97

Aqui está o script:

class GDataMailer {
    static $url_ClientLogin = 'https://www.google.com/accounts/ClientLogin';
    static $url_Feed = 'http://www.google.com/m8/feeds/contacts/default/thin?max-results=65535';

    function newCurlSession($URL, $auth = null) {
        $curl = curl_init();

        $opts = array(
            CURLOPT_URL => $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;
    }
}

Linha 97 é que a linha $email = $mnode->getAttribute('address'); perto do fim.

O que posso mudar aqui para não receber esse erro mais e começar este trabalho para a comunidade Drupal? Eu quero puxar em todos lista de contatos de uma pessoa, não apenas 25. Eu figura se eu enviar o alto o suficiente número, ele vai estar perto o suficiente para sobreviver.

Foi útil?

Solução

sem testá-lo diretamente, gostaria de tentar substituir linhas 96 - 104 com o seguinte:

$mnode = $enodes->item(0);
if (isset($mnode) && is_object($mnode)) {
    $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($email)) {
        if (empty($name)) {
            $contacts[] = $email;
        } else {
            $contacts[$name] = $email;
        }
    }
}

gd: e-mail é um elemento opcional de acordo com a Google Data API . É opcional na implementação do Gmail também. O módulo que você está usando assume que existe e falha quando isso não acontece.

Vários endereços de e-mail por contato permanecem sem tratamento, de acordo com a NOTA:. Comentário

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top