GroupWise 6.5 to Zimbra AddressBook August 22, 2009

Here is some code to translate a GroupWise 6.5 address book into a format that Zimbra can understand.

nab2zimbra.php

<?php
function get_value_by_mapi($zimbraid, $nabid,$nabvalues,$nabfields)
    {
    if ($nabid == "")
        {
        return  "";
        }
    for ($i=0;$i<sizeof($nabfields);$i++)
        {
        if ($nabfields[$i][mapitag] == $nabid)
            {
            return $nabvalues[$i];
            }
        }
    }
 
if (count($argv) < 2)
    {
    echo "Usage: php nab2zimbra.php <input_file.nab> \n";
    }
else
    {
    $inputfile = trim($argv[1]);
    if (file_exists($inputfile))
        {
        $row = 1;
        $handle = fopen($inputfile, "r");
 
        /* 
        :::TAGMAP:::0FFE0003:***,
        3001001E:Name,
        3A08001E:Office Phone Number,
        3A18001E:Department,
        3A23001E:Fax Number,
        3003001E:E-Mail Address,
        3A06001E:First Name,
        3A11001E:Last Name,
        3A17001E:Title,
        3A29001E:Address,
        3A27001E:City,
        3A28001E:State,
        3A26001E:Country,
        3A2A001E:ZIP Code,
        3002001E:E-Mail Type,
        3A19001E:Mailstop,
        3A09001E:Home Phone Number,
        3A1C001E:Cellular Phone Number,
        3A21001E:Pager Number,
        3A1A001E:Phone Number,
        600B001E:Greeting,
        600F001E:Owner,
        3A16001E:Organization,
        3004001E:Comments,
        3A00001E:User ID,
        6604001E:Domain,
        6609001E:Additional Routing,
        6605001E:Post Office,
        6603001E:GUID,
        6607001E:eDirectory Distinguished Name,
        6608001E:Network ID,
        660D001E:Internet Domain,
        660E001E:AIM/IM Screen Name,
        3A45001E:Prefix,
        3A44001E:Middle Name,
        3A05001E:Generation,
        3A5D001E:Home Address,
        3A59001E:Home City,
        3A5C001E:Home State,
        3A5B001E:Home ZIP,
        3A5A001E:Home Country,
        3A50001E:Personal Web Site,
        3A51001E:Office Web Site,
        6612001E:Resource Type,
        6615001E:Primary Contact Name
        */        
 
        // First go through the input file and load an associative array with mapped MAPI tags        
 
 
 
        $fields = array();
 
        while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) 
            {
            if ($row==1)
                {
                $num = count($data);
                $row++;
                // This should be the header line
                $fieldmap = array();
                for ($c=0; $c < $num; $c++) 
                    {
                    $thismapitag = "";
                    $exploded = explode(":",$data[$c]);
                    $thismapitag = $exploded[0];                     
                    $fieldmap[$c][value] = $exploded[1];
                    $fieldmap[$c][mapitag] = $thismapitag;
                    }
                }
            else
                {                
                $num = count($data);
                $row++;
                for ($c=0; $c < $num; $c++) 
                    {
                    // Zimbra mapping
                    /*
                    birthday	
                    company	= 3A16001E
                    department = 3A18001E
                    dlist	
                    email	= 3003001E
                    email2	
                    email3	
                    fileAs = 1,2,3	
                    firstName	= 3A06001E
                    fullName = 3001001E
                    homeCity = 3A59001E
                    homeCountry	= 3A5A001E
                    homeFax	= 
                    homePhone	= 3A09001E
                    homePhone2	
                    homePostalCode = 3A5B001E
                    homeState	= 3A5C001E
                    homeStreet = 3A5D001E
                    homeURL	= 3A50001E
                    initials	
                    jobTitle = 3A17001E
                    lastName = 3A11001E
                    middleName = 3A44001E
                    mobilePhone	= 3A1C001E
                    nickname	
                    notes	= 3004001E
                    pager	= 3A21001E
                    type	
                    workCity = 3A27001E
                    workFax	= 3A23001E
                    workPhone	= 3A08001E
                    workPhone2	
                    workPostalCode = 3A2A001E	
                    workState	= 3A28001E
                    workStreet = 3A29001E
                    workURL = 3A51001E
                    */
                    $fields[$row][$c] = $data[$c];
                    }
                }
            }
        fclose($handle);
 
        // Ok, now build the header row:
        $zimbraheader = array("birthday","company","department","dlist","email","email2","email3","fileAs","firstName",
        "fullName","homeCity","homeCountry","homeFax","homePhone","homePhone2","homePostalCode","homeState","homeStreet","homeURL","initials","jobTitle","lastName","middleName","mobilePhone",
        "nickname","notes","pager","type","workCity","workFax","workPhone","workPhone2","workPostalCode","workState","workStreet","workURL");
 
        for ($i=0;$i<sizeof($zimbraheader);$i++)
            {
            echo $zimbraheader[$i] . ",";
            }
 
        echo "\n";
 
        // Loop through all the input rows
        foreach ($fields as $key => $val)
            {
            $nabfields = $fieldmap;
            $nabvalues = $fields[$key];
 
            // Only import people objects, not company objects.
            if ($nabvalues[0] == "U")
                {
                echo get_value_by_mapi("birthday", "",$nabvalues,$nabfields) . ",";            
                echo get_value_by_mapi("company", "3A16001E",$nabvalues,$nabfields) . ",";                                                                 
                echo get_value_by_mapi("department", "3A18001E",$nabvalues,$nabfields) . ",";
                echo get_value_by_mapi("dlist", "",$nabvalues,$nabfields) . ",";
                echo get_value_by_mapi("email", "3003001E",$nabvalues,$nabfields) . ",";
                echo get_value_by_mapi("email2", "",$nabvalues,$nabfields) . ",";
                echo get_value_by_mapi("email3", "",$nabvalues,$nabfields) . ",";
                echo get_value_by_mapi("fileAs", "",$nabvalues,$nabfields) . ",";
                echo get_value_by_mapi("firstName", "3A06001E",$nabvalues,$nabfields) . ",";
                echo get_value_by_mapi("fullName", "3001001E",$nabvalues,$nabfields) . ",";
                echo get_value_by_mapi("homeCity", "3A59001E",$nabvalues,$nabfields) . ",";
                echo get_value_by_mapi("homeCountry", "3A5A001E",$nabvalues,$nabfields) . ",";
                echo get_value_by_mapi("homeFax", "",$nabvalues,$nabfields) . ",";
                echo get_value_by_mapi("homePhone", "3A09001E",$nabvalues,$nabfields) . ",";
                echo get_value_by_mapi("homePhone2", "",$nabvalues,$nabfields) . ",";
                echo get_value_by_mapi("homePostalCode", "3A5B001E",$nabvalues,$nabfields) . ",";
                echo get_value_by_mapi("homeState", "3A5C001E",$nabvalues,$nabfields) . ",";
                echo get_value_by_mapi("homeStreet", "3A5D001E",$nabvalues,$nabfields) . ",";
                echo get_value_by_mapi("homeURL", "3A50001E",$nabvalues,$nabfields) . ",";
                echo get_value_by_mapi("initials", "",$nabvalues,$nabfields) . ",";
                echo get_value_by_mapi("jobTitle", "3A17001E",$nabvalues,$nabfields) . ",";
                echo get_value_by_mapi("lastName", "3A11001E",$nabvalues,$nabfields) . ",";
                echo get_value_by_mapi("middleName", "3A44001E",$nabvalues,$nabfields) . ",";
                echo get_value_by_mapi("mobilePhone", "3A1C001E",$nabvalues,$nabfields) . ",";
                echo get_value_by_mapi("nickname", "",$nabvalues,$nabfields) . ",";
                echo get_value_by_mapi("notes", "3004001E",$nabvalues,$nabfields) . ",";
                echo get_value_by_mapi("pager", "3A21001E",$nabvalues,$nabfields) . ",";
                echo get_value_by_mapi("type", "",$nabvalues,$nabfields) . ",";
                echo get_value_by_mapi("workCity", "3A27001E",$nabvalues,$nabfields) . ",";
                echo get_value_by_mapi("workFax", "3A23001E",$nabvalues,$nabfields) . ",";
                echo get_value_by_mapi("workPhone", "3A08001E",$nabvalues,$nabfields) . ",";
                echo get_value_by_mapi("workPhone2", "",$nabvalues,$nabfields) . ",";
                echo get_value_by_mapi("workPostalCode", "3A2A001E",$nabvalues,$nabfields) . ",";
                echo get_value_by_mapi("workState", "3A28001E",$nabvalues,$nabfields) . ",";
                echo get_value_by_mapi("workStreet", "3A29001E",$nabvalues,$nabfields) . ",";
                echo get_value_by_mapi("workURL", "3A51001E",$nabvalues,$nabfields) . ",";
 
                echo "\n";
                }
            }
 
 
        }
    else
        {
        echo "Could not find input file: " . $inputfile . " \n";
        }
    }    
?>
Leave a Reply