I want to use this module to drupal 5.x

dalvir - November 4, 2009 - 19:50
Project:Drupal Contact List Importer
Version:6.x-2.x-dev
Component:Code
Category:support request
Priority:normal
Assigned:dalvir
Status:active
Description

hello hadsie,

I want to use this module for drupal 5.x . So i want to convert it for drupal 5.x
But i m new so i need a guidance from you best way to convert a this module.
things to check etc.

Regrads
Dalvir

#1

hadsie - November 18, 2009 - 20:10

Hey Dalvir,

It would be great to have a D5 version of the module. I'm not sure the best approach to take for it though... you'd need to start by probably reading through the D5->D6 changes page and working backwards. Or else just by rebuilding the hook menu function and going from there.

Of course, D5 won't be supported for too much longer since D7 is probably less than half a year away from launch. Perhaps you should consider upgrading to D6?

- Scott

#2

kaakuu - November 24, 2009 - 01:10

+1
Many sites running 5x version of this find this completely broken now. It will be of immense help if there is something done so that the yahoo and gmail contacts be at least imported.
It appears that there was some problem for which the 5x version has been removed from the download list, as well as directory structures and scripts itself have changed a lot.
If someone is kind enough to post at least a hardcoded file or some changes that keeps it working for yahoo and gmail it will be of immense,immense help.

The old script used 'importgmailclass.php'
which is as follows

<?php

include_once ( 'baseclass.php' );

class
GMailer extends baseFunction
{
    var
$location = "";
    var
$cookiearr = array();

   
#Globals Section, $location and $cookiearr should be used in any script that uses
    #getAddressbook function
    #function getAddressbook, accepts as arguments $login (the username) and $password
    #returns array of: array of the names and array of the emails if login successful
    #otherwise returns 1 if login is invalid and 2 if username or password was not specified
  
   
function getAddressbook($login, $password)
    {
        
#the globals will be updated/used in the read_header function
        
global $location;
         global
$cookiearr;
         global
$ch;

       
#check if username and password was given:
       
if ((isset($login) && trim($login)=="") || (isset($password) && trim($password)==""))
        {
              return
false;
        }
       
       
#initialize the curl session
       
$ch = curl_init();
       
curl_setopt($ch, CURLOPT_URL,"http://mail.google.com/mail/");
       
curl_setopt($ch, CURLOPT_REFERER, "");
       
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
       
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
       
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
       
//curl_setopt($ch, CURLOPT_HEADERFUNCTION, 'read_header');
       
curl_setopt($ch, CURLOPT_HEADERFUNCTION, array($this, 'read_header')); 
       
       
#get the html from gmail.com
       
$html = curl_exec($ch);
       
$matches = array();
       
$actionarr = array();
       
       
$action = "https://www.google.com/accounts/ServiceLoginAuth";
   
       
#parse the login form:
        #parse all the hidden elements of the form
       
preg_match_all('/<input type\="hidden" name\="([^"]+)".*?value\="([^"]*)"[^>]*>/si', $html, $matches);
       
$values = $matches[2];
       
$params = "";
       
       
$i=0;
        foreach (
$matches[1] as $name)
        {
         
$params .= "$name=" . urlencode($values[$i]) . "&";
          ++
$i;
        }
       
       
$login = urlencode($login);
       
$password = urlencode($password);
       
       
curl_setopt($ch, CURLOPT_URL,$action);
        
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
       
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
       
curl_setopt($ch, CURLOPT_POST, 1);
       
curl_setopt($ch, CURLOPT_POSTFIELDS, $params ."Email=$login&Passwd=$password&PersistentCookie=");
       
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
       
curl_setopt($ch, CURLOPT_HEADER, true);
      
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
       
$html = curl_exec($ch);
   
       
#test if login was successful:
       
if (preg_match('/url=([^"]*)/', $html, $actionarr)!=0)
        {  
           
$location = $actionarr[1];
           
$location = urldecode($location); 
   
        }
        else
        {
            return
false;
        }
   
   
       
       
$location2=trim(html_entity_decode($location),"\'");
       
preg_match_all('/continue\=(.*)$/',$location2,$matches);
     
       
$finalLocation=urldecode($matches[1][0]);
      
        if(
$finalLocation!="")  
        {
           
$data=explode("?",$finalLocation);
        }
        else
        {
           
$data=explode("?",$location2);
        }
     
      
$ch1=curl_init();

      
curl_setopt($ch1, CURLOPT_URL, $data[0]);
       
curl_setopt($ch1, CURLOPT_SSL_VERIFYPEER, 0);
        
curl_setopt($ch1, CURLOPT_SSL_VERIFYHOST, 0);
      
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1);
      
curl_setopt($ch1, CURLOPT_HEADER, true);
      
curl_setopt($ch1, CURLOPT_POST, 1);
      
curl_setopt($ch1, CURLOPT_POSTFIELDS, $data[1]);
   
      
$html1 = curl_exec($ch1);
   
      
$cokkies1=$this->getCookies($html1);
      
$location1=$this->getLocation($html1);
    
      
$ch2=curl_init();

      
curl_setopt($ch2, CURLOPT_URL, "https://mail.google.com/mail/h/?v=cl&pnl=a&f=1");
      
curl_setopt($ch2, CURLOPT_SSL_VERIFYHOST, false);
      
curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, false);
      
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1);
      
curl_setopt($ch2, CURLOPT_HEADER, true);
      
curl_setopt($ch2, CURLOPT_COOKIE, $cokkies1);
      
curl_setopt($ch2, CURLOPT_FOLLOWLOCATION,1);
      
$html2 = curl_exec($ch2);
   
      
$html2 = str_replace("\n","",$html2);    
      
$tempname = split('<b>All&nbsp;Contacts</b>',$html2,2);
      
preg_match_all('/<b>([^\<]*)/s',$tempname[1],$name);
      
$tempemail = preg_replace('/<b>([^\<]*)/s','',$tempname[1]);
      
$emails = explode("&nbsp;",strip_tags($tempemail));
      
$tot_contacts = strip_tags($name[0][0]) + 2;
      
       for(
$i=2,$j=4;$i<$tot_contacts;$i++,$j++)
       {
          
/*
            $result['name'][]=trim(strip_tags($name[0][$i]));
           $result['email'][]=trim($emails[$j]);
    */
      
$result[trim(strip_tags($name[0][$i]))] = trim($emails[$j]);
       }
       return
$result;
    }


   
}
?>

The new script should have something like below but direct copy pasting obviously breaks things with WOD, replacing necessary gmail uri that has changed also does not help but an experienced coder may probably be able to tell what and how to incorporate from below to the above to make things work.

<?php
$location
= "";
$cookiearr = array();
$csv_source_encoding='utf-8';

#function get_contacts, accepts as arguments $login (the username) and $password
#returns array of: array of the names and array of the emails if login successful
#otherwise returns 1 if login is invalid and 2 if username or password was not specified
function get_contacts($login, $password)
{
 
#the globals will be updated/used in the read_header function
 
global $csv_source_encoding;
  global
$location;
  global
$cookiearr;
  global
$ch;

 
#check if username and password was given:
   
if ((isset($login) && trim($login)=="") || (isset($password) && trim($password)==""))
    {
     
#return error code if they weren't
       
return 2;
    }
   
   
#initialize the curl session
   
$ch = curl_init();
   
curl_setopt($ch, CURLOPT_URL,"https://www.google.com/accounts/ServiceLoginAuth?service=mail");
   
curl_setopt($ch, CURLOPT_REFERER, "");
   
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
   
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
   
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
   
curl_setopt($ch, CURLOPT_HEADERFUNCTION, 'read_header');
   
   
#get the html from gmail.com
 
$html = curl_exec($ch);
   
   
$matches = array();
   
$actionarr = array();
   
   
$action = "https://www.google.com/accounts/ServiceLoginAuth?service=mail";

   
#parse the login form:
    #parse all the hidden elements of the form
   
preg_match_all('/<input type="hidden"[^>]*name\="([^"]+)"[^>]*value\="([^"]*)"[^>]*>/si', $html, $matches);
   
$values = $matches[2];
   
$params = "";
   
   
$i=0;
    foreach (
$matches[1] as $name)
    {
     
$params .= "$name=" . urlencode($values[$i]) . "&";
      ++
$i;
    }

 
$login = urlencode($login);
 
$password = urlencode($password);
 
 
#submit the login form:
   
curl_setopt($ch, CURLOPT_URL,$action);
   
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
   
curl_setopt($ch, CURLOPT_POST, 1);
   
curl_setopt($ch, CURLOPT_POSTFIELDS, $params ."Email=$login&Passwd=$password&PersistentCookie=");

   
$html = curl_exec($ch);

 
#test if login was successful:
 
if (!isset($cookiearr['GX']) && (!isset($cookiearr['LSID']) || $cookiearr['LSID'] == "EXPIRED"))
    {
        return
1;
    }

 
#this is the new csv url:
 
curl_setopt($ch, CURLOPT_URL, "http://mail.google.com/mail/contacts/data/export?exportType=ALL&groupToExport=&out=GMAIL_CSV");
 
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
 
curl_setopt($ch, CURLOPT_HTTPGET, 1);

 
$html = curl_exec($ch);
 
$html = iconv ($csv_source_encoding,'utf-8',$html);

 
$csvrows = explode("\n", $html);
 
array_shift($csvrows);
 
 
$names = array();
 
$emails = array();
    foreach (
$csvrows as $row)
    {
        if (
preg_match('/^((?:"[^"]*")|(?:[^,]*)).*?([^,@]+@[^,]+)/', $row, $matches))
        {
           
$names[] = trim( ( trim($matches[1] )=="" ) ? current(explode("@",$matches[2])) : $matches[1] , '" ');
           
$emails[] = trim( $matches[2] );
        }
    }
     

    return array(
$names, $emails);
}

#read_header is essential as it processes all cookies and keeps track of the current location url
#leave unchanged, include it with get_contacts
function read_header($ch, $string)
{
    global
$location;
    global
$cookiearr;
    global
$ch;
    global
$csv_source_encoding;

   
   
$length = strlen($string);
   
    if (
preg_match("/Content-Type: text\\/csv; charset=([^\s;$]+)/",$string,$matches))
       
$csv_source_encoding=$matches[1];

    if(!
strncmp($string, "Location:", 9))
    {
     
$location = trim(substr($string, 9, -1));
    }
    if(!
strncmp($string, "Set-Cookie:", 11))
    {
     
$cookiestr = trim(substr($string, 11, -1));
     
$cookie = explode(';', $cookiestr);
     
$cookie = explode('=', $cookie[0]);
     
$cookiename = trim(array_shift($cookie));
     
$cookiearr[$cookiename] = trim(implode('=', $cookie));
    }
   
$cookie = "";
    if(
trim($string) == "")
    {
      foreach (
$cookiearr as $key=>$value)
      {
       
$cookie .= "$key=$value; ";
      }
     
curl_setopt($ch, CURLOPT_COOKIE, $cookie);
    }

    return
$length;
}

#function to trim the whitespace around names and email addresses
#used by get_contacts when parsing the csv file
function trimvals($val)
{
  return
trim ($val, "\" \n");
}
?>

 
 

Drupal is a registered trademark of Dries Buytaert.