Author Topic: IMPorter Plugin  (Read 5652 times)

Offline manuxio

  • Newbie
  • *
  • Posts: 1
IMPorter Plugin
« on: December 17, 2009, 10:05:53 PM »
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.

Code: [Select]
<?php
/**
 * IMPorter Plugin
 *
 * Populate user address book with Turba address book from IMP
 *
 * @version 1.0
 * @author Manuele Cappelleri
 *
 * Example configuration:
 * $_turba_db_name = &quot;&quot;;
 * $_turba_db_host = &quot;&quot;;
 * $_turba_db_type = &quot;&quot;;
 * $_turba_db_username = &quot;&quot;;
 * $_turba_db_password = &quot;&quot;;
 * $_turba_db_table_name = &quot;&quot;;
 *
 */
class imp_orter extends rcube_plugin
{
    function 
init()
    {
$this->_turba_db_name = &quot;XX&quot;;
 
$this->_turba_db_host = &quot;XX&quot;;
        
$this->_turba_db_type = &quot;XX&quot;;
        
$this->_turba_db_username = &quot;XX&quot;;
        
$this->_turba_db_password = &quot;XX&quot;;
        
$this->_turba_db_table_name = &quot;turba_objects&quot;;
        
$this->_log_file = &quot;/tmp/imp_orter.log&quot;;
        
$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(0true);
        
        
//
        
$dsn $this->_turba_db_type . &quot;://&quot; . $this->_turba_db_username . &quot;:&quot; . $this->_turba_db_password . &quot;@&quot; . $this->_turba_db_host . &quot;/&quot; . $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 = &quot;SELECT object_name,object_alias,object_email FROM &quot; . $this->_turba_db_table_name . &quotWHERE owner_id '&quot; . $username . &quot;'&quot;;
        
$this->debug(&quot;DB Query$sql&quot;);

        
$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(&quot;Name is now$tmp &quot;);
                
$tmp ucwords(strtolower(ltrim(rtrim($tmp))));
                
$this->debug(&quot;Name is now$tmp &quot;);
                
                
$parts explode(&quot; &quot;,$tmp);
                
$this->debug(&quot;Name converted to: &quot; . serialize($parts));
                
$rec['firstname'] = $parts[0];
                for (
$i 1$i count($parts); $i++) {
                        
$rec['surname'] .= $parts[$i];
                        if (
$i count($parts)-1) {
                                
$rec['surname'] .= &quot; &quot;;
                        }
                }
                
                
$this->debug($username . &quot; <- &quot; . serialize($rec));
                
$contacts->insert($rectrue);
                
        }
        
        
        
        
        
        return 
$args;
    }
    
        function 
debug($string) {
                if (
$this->_log_file) {
                        
$log fopen($this->_log_file,'a');
                        
fwrite($log,&quot;\n&quot;.$string,strlen(&quot;\n&quot;.$string));
                        
fclose($log);
                }        
        }
}
?>