Hello All,
sorry, but I couldn't resist to choose that name!
For anyone willing to import contacts from an IMP/Horde db, this is a little and dirty plugin...
Very, very, very basic.
<?php
/**
* IMPorter Plugin
*
* Populate user address book with Turba address book from IMP
*
* @version 1.0
* @author Manuele Cappelleri
*
* Example configuration:
* $_turba_db_name = "";
* $_turba_db_host = "";
* $_turba_db_type = "";
* $_turba_db_username = "";
* $_turba_db_password = "";
* $_turba_db_table_name = "";
*
*/
class imp_orter extends rcube_plugin
{
function init()
{
$this->_turba_db_name = "XX";
$this->_turba_db_host = "XX";
$this->_turba_db_type = "XX";
$this->_turba_db_username = "XX";
$this->_turba_db_password = "XX";
$this->_turba_db_table_name = "turba_objects";
$this->_log_file = "/tmp/imp_orter.log";
$this->add_hook('create_identity', array($this, 'importAll'));
}
function importAll($args)
{
$rcmail = rcmail::get_instance();
$ser = serialize($rcmail->user);
$curUser = $rcmail->user;
$username = $curUser->data['username'];
$userid = $curUser->ID;
$contacts = $rcmail->get_address_book(0, true);
//
$dsn = $this->_turba_db_type . "://" . $this->_turba_db_username . ":" . $this->_turba_db_password . "@" . $this->_turba_db_host . "/" . $this->_turba_db_name;
$options = array(
'result_buffering' => false,
);
$db =& MDB2::factory($dsn, $options);
if (PEAR::isError($db)) {
die($db->getMessage());
} else {
$db->setFetchMode(MDB2_FETCHMODE_ASSOC);
}
$sql = "SELECT object_name,object_alias,object_email FROM " . $this->_turba_db_table_name . " WHERE owner_id = '" . $username . "'";
$this->debug("DB Query: $sql");
$q = $db->query($sql);
if (PEAR::isError($q)) {
die($q->getMessage());
}
while($r = $q->fetchRow()) {
$rec = array();
$rec['email'] = $r['object_email'];
if ($r['object_alias'] != '') {
$rec['name'] = ltrim(rtrim($r['object_alias']));
} else {
$rec['name'] = ltrim(rtrim($r['object_name']));
}
$tmp = $r['object_name'];
$this->debug("Name is now: $tmp ");
$tmp = ucwords(strtolower(ltrim(rtrim($tmp))));
$this->debug("Name is now: $tmp ");
$parts = explode(" ",$tmp);
$this->debug("Name converted to: " . serialize($parts));
$rec['firstname'] = $parts[0];
for ($i = 1; $i < count($parts); $i++) {
$rec['surname'] .= $parts[$i];
if ($i < count($parts)-1) {
$rec['surname'] .= " ";
}
}
$this->debug($username . " <- " . serialize($rec));
$contacts->insert($rec, true);
}
return $args;
}
function debug($string) {
if ($this->_log_file) {
$log = fopen($this->_log_file,'a');
fwrite($log,"\n".$string,strlen("\n".$string));
fclose($log);
}
}
}
?>