Categories

Converting Opera contacts file

After using Operas mail client since 2003, I have become tired of the mess in the configuration files during the many upgrades between the versions. I have spent many hours trying to figure out things, but you need to be an expert to sort it out. The mail is stored in *.mbs files, right now I have 36502 of them. I easily imported them into Thunderbird, wich is my email client of choice this time. I will continue using Opera as my #1 web browser, but I think I have given Opera enough time trying to prove their email client is something for me.

Then a problem occurrs, I need to import all my contacts into Thunderbird. Great problems, I can’t find any tool that does the job! The Opera contacts file has a special format, take a look here:

Opera Hotlist version 2.0
Options: encoding = utf8, version=3

#FOLDER
   ID=11
   NAME=Friends at school
   CREATED=1050344163
   UNIQUEID=1F6DE415A8128F4FB6A77E3D86D09CEF

#CONTACT
   ID=12
   NAME=John Doe
   CREATED=1200728119
   MAIL=john@blabla.com
   ICON=Contact0

#CONTACT
   ID=13
   NAME=Sandra B
   CREATED=1196975050
   MAIL=sbull@blabla.com
   ICON=Contact1

The csv files need all values to be enclosed with the sign “, and using commas. I figured out I’ll make it myself, so hopefully someone can make use of the script I made.

Sadly you need to be a kind of expert to use this script as well, you need a webserver with PHP running to make use of it.

<?php
/**
 * Convert Opera contact file to another format
 *
 * <p>This class does not preserve the folder structure in the Opera contact file, neither
 * the icons for each contact.
 *
 * <p>This piece of software was made because I could not find a good converter to export the
 * Opera contacts for import to Thunderbird. Thunderbird
 *
 * <p>Find your Opera contacts file by reading here: http://www.opera.com/support/kb/view/313/
 *
 * @author      Pål Gjerde Gammelsæter
 * @copyright   Gammelsæter Data
 * @version     26th February 2011
 * @license     Free :)
 */

class OperaContacts {

    protected $contents;

    /**
     * The contents splitted into lines in an array
     * @var array
     */

    protected $parsed;

    /**
     * The fields we want to preserve from Opera contact list
     * @var array
     */

    protected $preserve = array('NAME', 'MAIL', 'DESCRIPTION', 'URL', 'PHONE', 'FAX', 'POSTALADDRESS');

    /**
     * Make conversion object
     * @param string  $importFile The path and filename of importfile
     * @param boolean $utf8       If the import file is encoded as utf8 (default to true)
     */

    public function  __construct($importFile = 'contacts.adr', $utf8 = true) {
        $contents = file_get_contents($importFile);

        // In some fields, Opera is using hex 02 02 (\02\02) for line feed, we replace it
        // with \r
        $operaLinefeed = chr(02).chr(02);
        $contents = str_replace($operaLinefeed, "\r", $contents);

        if ($utf8) { $contents = utf8_decode($contents); }
        $this->contents = $contents;
               
        $lines = explode("\n", $this->contents);
        $this->parsed = $this->parse($lines);
    }

    /**
     * Check if given line starts with given string
     * @param string $string
     * @param string $line
     * @return boolean
     */

    protected function startsWith($string, $line) {
        if (substr($line, 0, strlen($string)) == $string) { return true; }
        else { return false; }
    }

    /**
     * Get the variable name and value in given line
     * @param string $line
     * @return array Key is variable name, value is value
     */

    protected function getVariable($line) {
        $line = trim($line);        // trim away whitespace at beginning and end
        return explode('=', $line); // split into parts
    }

    /**
     * Parse the data
     * @param array $lines Array of strings
     * @return array
     */

    protected function parse($lines) {
        $contactIt = 0;
        $contacts = array();

        // Go through each line
        $inContact = false;
        foreach ($lines as $line) {
            if ($this->startsWith('#CONTACT', $line)) { $inContact = true; $contactIt++; }
            else if ($this->startsWith('#FOLDER', $line)) { $inContact = false; }
            else if ($inContact) {
                $var = $this->getVariable($line);
                // if this variable is one to preserve, and the value is not empty, then save it
                if (in_array($var[0], $this->preserve) && trim($var[1]) != '') {
                    $contacts[$contactIt][$var[0]] = $var[1];
                }
            }
        }

        return $contacts;
    }

    /**
     * Save the contacts to csv
     *
     * @param string  $exportFile     The path and name of the file to export to
     * @param string  $separator      The separator between the fields
     * @param boolean $encloseStrings If enclosing the strings in '"'
     * @param boolean $utf8           If the csv file should be saved in utf8 encoding
     */

    public function saveCSV($exportFile, $separator, $encloseStrings = true, $utf8 = false) {
        $contents = implode($separator, $this->preserve)."\n";
        foreach ($this->parsed as $id => $data) {
            $row = array();
            foreach ($this->preserve as $key) {
                if (array_key_exists($key, $data)) {
                    $value = $data[$key];
                    if ($encloseStrings) { $value = '"'.$value.'"'; } // if enclose
                    if ($utf8) { $value = utf8_encode($value); }      // if utf8 encosing
                    $row[$key] = $value;
                }
                else { $row[$key] = ''; }
            }
            $contents .= implode($separator, $row)."\n";
        }
        file_put_contents($exportFile, $contents);
    }

    /**
     * Save contacts in the format Thunderbird wants it
     * @param string $exportFile
     */

    public function saveThunderbirdCSV($exportFile) {
        $this->saveCSV($exportFile, ',', true, false);
    }

}

$obj = new OperaContacts('contacts.adr');
$obj->saveThunderbirdCSV('exported_contacts.csv');
   
?>

Please give me a note if this helped you!

1 comment to Converting Opera contacts file

Leave a Reply

  

  

  

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>