Author Topic: Import Contacts from Horde  (Read 7064 times)

Offline maltek

  • Newbie
  • *
  • Posts: 3
Import Contacts from Horde
« on: December 02, 2011, 03:46:03 AM »
Hi there!

I'm currently migrating from a horde webmail to rc. I found the horde import script from the plugin directory, which does a really nice job. My problem is, that I need to import some additional field like "address" "phone" etc. I tried creating a vcard and inserting it into the vcard column which doesnt work and gives a mixup of entered data.  When I add the attributes "address" etc to the database record an import it, it is successfully written and shown in the "words" column.
Can anyone tell me, how to import these additional infos? Thanks a lot!

Offline maltek

  • Newbie
  • *
  • Posts: 3
Import Contacts from Horde
« Reply #1 on: December 07, 2011, 05:46:41 AM »
I finally developed a solution to import the complete horde structure including groups and additional fields to RC based on the import_horde_contacts Plugin from Jason Meinzer.
For those who are interested, here is the dirty code:

Code: [Select]
<?php

/**

 * Import horde contacts

 *

 * Populates a new user's contacts with entries from Horde (Turba).

 *

 * Users with contacts already in Roundcube are skipped.

 *

 * You must configure your Horde database credentials in main.inc.php:

 *

 *  $rcmail_config['horde_dsn']  = 'pgsql:host=db.example.com;dbname=horde';

 *  $rcmail_config['horde_user'] = 'horde';

 *  $rcmail_config['horde_pass'] = 'password';

 *

 * See also: https://github.com/bithive/import_horde_identities

 * 

 * @version 1.1

 * @author Jason Meinzer

 * 

 * Modified and extended by Malte Kueppers - Douglas Informatik & Service GmbH 
 * Date 07/12/2011

 */

class import_horde_contacts extends rcube_plugin

{

public $task 'login';

private $log 'import_horde';



function init()

{

$this->add_hook('login_after', array($this'fetch_turba_objects'));

}





function fetch_turba_objects()

{

        
$this->rc rcmail::get_instance();

        
$contacts $this->rc->get_address_book(nulltrue);

        
$this->load_config();



        
if($contacts->count()->count 0) return true// exit early if user already has contacts 



        $db_dsn  $this->rc->config->get('horde_dsn');

        $db_user $this->rc->config->get('horde_user');

        $db_pass $this->rc->config->get('horde_pass');



        try {

         $db = new PDO($db_dsn$db_user$db_pass);

        } catch(PDOException $e) {

return false;

}



$sth $db->prepare('select object_name, object_email, object_homeaddress, object_workaddress,

object_homephone, object_workphone, object_cellphone, object_fax, object_title, object_company,

object_notes, object_type, object_id, object_members from turba_objects where owner_id = :uid'
);



        $uid explode('@'$this->rc->user->get_username());

        $uid $uid[0];



        $sth->bindParam(':uid'$uid);

        $sth->execute();



        $result $sth->fetchAll(PDO::FETCH_ASSOC);

        $count 0;

$i=0;
$gcounter 0;



        
foreach($result as $turba_object) {

$record = array(

                
'email'     => $turba_object['object_email'],

                
'firstname' => utf8_encode($turba_object['object_alias']),

                
'surname'   => utf8_encode($turba_object['object_name']),

'name' =>  utf8_encode($turba_object['object_name']),

'department' => utf8_encode($turba_object['object_company']),

//'address' => utf8_encode($turba_object['object_workaddress']),

'street:work' =>  utf8_encode($turba_object['object_workaddress']),

//'homeaddress' => $turba_object['object_homeaddress'],

                
//'workaddress' => $turba_object['object_workaddress'],

                
'phone:home' => utf8_encode($turba_object['object_homephone']),

                
'phone:work' => utf8_encode(($turba_object['object_workphone']),

                
'phone:mobile' => utf8_encode($turba_object['object_cellphone']),

                
'phone:workfax' => utf8_encode($turba_object['object_fax']),

                
'jobtitle' => utf8_encode($turba_object['object_title']),

                
//'company' => $turba_object['object_company'],

                
'notes' => utf8_encode($turba_object['object_notes']),

);

if (check_email(idn_to_ascii($record['email']))) {

$record['email'] = idn_to_utf8($record['email']);

$cid$contacts->insert($recordtrue);

$count++;
// Map Horde OjectID to RC ContactID

$usermap[$turba_object['object_id']] = $cid;

            
}

}

//Users have been imported, now check groups
foreach($result as $turba_object){
if (isset($turba_object['object_members'])){ // actual object is group


                                
$gid $contacts->create_group($turba_object['object_name']);
$gcounter++;

$gid $gid['id']; // We only need the group ID from group array

$member explode(':',$turba_object['object_members']);
$j=5;

for($i=0;$j count($member);$i++){
$mneu[$i]=substr($member[$j],1,32); // extract Horde UID from String
$mneu[$i]=$usermap[$mneu[$i]];   //
$mneu[$i]=preg_replace(&quot;/\r|\n/s&quot;, &quot;&quot;, $mneu[$i]); //remove linebreaks
$j+=3;
}

$contacts->add_to_group($gid,$mneu); //add contacts to group
}

     


}
write_log($log, &quot;Imported $count Horde contacts and $gcounter groups for $uid&quot;);

        return 
true;

    }



}

?>



Offline nbensa

  • Newbie
  • *
  • Posts: 1
Re: Import Contacts from Horde
« Reply #2 on: September 30, 2013, 04:42:58 PM »
Hello

Thanks for your modifications. It worked for me but I have added unset($mneu); just before the group loop.

Best regards,
Norberto