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:
<?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(null, true);
$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($record, true);
$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("/\r|\n/s", "", $mneu[$i]); //remove linebreaks
$j+=3;
}
$contacts->add_to_group($gid,$mneu); //add contacts to group
}
}
write_log($log, "Imported $count Horde contacts and $gcounter groups for $uid");
return true;
}
}
?>