6/01/2019

Grandstream XML Address Book from OwnCloud

I have all my contacts stored in OwnCloud. Obviously it would be nice to have them available on my Grandstream phones.

I wrote the below script to read the contacts from OwnCloud (VCard format) and create an XML structure that Grandstream phones can process.

If you need something similar help yourself to the code below. I am sure it is far from perfect, but ir works for me. No warranty, of course.

If you want to use it, just replace the URL, username and password and put it on a web server runnung PHP.  Finally point your phone to the URL of the file and have it load your contacts.

This may or may not work with NextCloud too. My guess is that is will.

The nice thing about this approach is that it does not involve exporting and importing, it just grabs the latest information whenever you choose lo load the address book on your phone.

function geturl($url, $username, $password)
{
    $ch = curl_init();
    
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, TRUE);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_VERBOSE, 0);
    curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    $a = curl_exec($ch);


    if(false === $a)
    {
        var_dump(curl_error($ch), curl_errno($ch));
    }


    /*
    $info = curl_getinfo($ch);
    print_r($info)
    */
    
    return $a;
}


$data = geturl("https://owncloud.torabli.eu:444/remote.php/dav/addressbooks/users/kian/contacts/?export", "kian", "Omposele1");
//$data = fakeData();
//var_dump($data);


if(false === $data)
{
    echo 'No data';
    exit('No data');
}


header('Content-Type: application/xml; charset=utf-8');
echo '' . "\r\n";
echo '' . "\r\n";


$vcards = null;
preg_match_all("/BEGIN:VCARD.*?END:VCARD/s",
        $data, $vcards);
//echo "number of vcards: " . strval(count($vcards[0]));


foreach($vcards[0] as $vcard)
{
    processvcard($vcard);
}


function processvcard($vc)
{
    $vc = str_replace('\r\n', "\r\n", $vc);
    $names = null;
    preg_match_all('/^N:(.*?);(.*?);.*$/m',
        $vc, $names);
    $firstname = $names[2][0];
    $lastname = $names[1][0];


    $phones = null;
    preg_match_all('/^TEL;TYPE=(.*?)((,|;).*)?:(.*)\r?$/m',
        $vc, $phones);
    
    /*
    match first group using
    /^CATEGORIES:(.*?)(,.*)?$/m
    */
    
    foreach($phones[0] as $i => $phone)
    {
        $phonetype = strtolower($phones[1][$i]);
        $type = strtoupper(substr($phonetype, 0, 1));
        $phonenumber = my_replace($phones[4][$i]);
        
        if(in_array($phonetype, array("work", "cell", "home")) &&
            (trim($lastname) !== "" || trim($firstname) !== ""))
        {
            echo "\r\n";
            echo " $lastname ($type)
\r\n";
            echo " $firstname
\r\n";
            echo " \r\n";
            echo " $phonenumber
\r\n";
            echo " 1\r\n";
            echo "
\r\n";
            /*
            echo " \r\n";
            echo " Enter group ID here\r\n";
            echo "
\r\n";
            */
            echo "
\r\n";
        }
    }
}


echo '
' . "\r\n";

function my_replace($s)
{
    $r = str_replace("\n", '', $s);
    $r = str_replace("\r", '', $r);
    
    return $r;
}


?>


function fakeData()
{
return
'BEGIN:VCARD\r\n' .
'VERSION:3.0\r\n' .
'UID:02fe604a-ea63-49fb-8c28-47c37062cd2a\r\n' .
'FN:Heinz Maschke\r\n' .
'N:Maschke;Heinz;;;\r\n' .
'TEL;TYPE=work,pref:+49 8206 961993\r\n' .
'TEL;TYPE=fax,work:+49 8206 - 961994\r\n' .
'EMAIL;TYPE=home:bioexpress-hmaschke@t-online.de\r\n' .
'NOTE:Biokiste\r\n' .
'ADR;TYPE=work:;;Ulrichstr. 10 \n86492 Egling a.d. Paar;;;;\r\n' .
'LABEL;TYPE=work:Ulrichstr. 10 \n86492 Egling a.d. Paar\r\n' .
'PRODID:DAVdroid/1.0-beta (ez-vcard/0.9.3)\r\n' .
'REV:20170105T113243Z\r\n' .
'END:VCARD\r\n' .
'BEGIN:VCARD\r\n' .
'VERSION:4.0\r\n' .
'PRODID:+//IDN bitfire.at//DAVx5/2.2.3.1-ose ez-vcard/0.10.5\r\n' .
'UID:045fa2c0-03ce-47fe-a264-4bdd9301a193\r\n' .
'FN:Martin Schütz\r\n' .
'N:Schütz;Martin;;;\r\n' .
'TEL;TYPE=work:+49 151 12551804\r\n' .
'TEL;TYPE=cell:+49 176 72678207\r\n' .
'EMAIL;TYPE=home:schuetzmm@gmail.com\r\n' .
'URL;TYPE=x-homepage;VALUE=URI:http://www.google.com/profiles/10952715549372\r\n' .
' 9506703\r\n' .
'CATEGORIES:Family\r\n' .
'BDAY:--0301\r\n' .
'REV:20190302T104435Z\r\n' .
'END:VCARD\r\n' .
'BEGIN:VCARD\r\n' .
'VERSION:3.0\r\n' .
'UID:06011ab7-cdd1-4212-8339-eae8f77e1a7b\r\n' .
'FN:Crissi Schöpp\r\n' .
'N:Schöpp;Crissi;;;\r\n' .
'TEL;TYPE=home:+49 821 154562\r\n' .
'TEL;TYPE=cell:+49 1515 1256215\r\n' .
'NOTE:Mutter Magali\r\n' .
'PRODID:DAVdroid/1.0-beta (ez-vcard/0.9.3)\r\n' .
'REV:20170803T054608Z\r\n' .
'END:VCARD\r\n' .
'BEGIN:VCARD\r\n' .
'VERSION:3.0\r\n' .
'PRODID:DAVdroid/1.0-beta (ez-vcard/0.9.3)\r\n' .
'UID:0648bfc9-255d-4f7f-841e-be4c17ce12b3\r\n' .
'CATEGORIES:Egling,Weihnachtskarte\r\n' .
'FN:Familie Sießmeir\r\n' .
'N:Sießmeir;;;Familie;\r\n' .
'REV:2018-12-11T21:51:02Z\r\n' .
'TEL;TYPE=HOME:+49 (8206) 96 15 87\r\n' .
'ADR;TYPE=HOME:;;Hauptstr. 39;Egling 86492;;;\r\n' .
'NOTE:\nTanja\, Manni\r\n' .
'LABEL;TYPE=home:Hauptstr. 39\nEgling 86492\r\n' .
'END:VCARD\r\n'
;
}
?>

No comments:

adaxas Web Directory