SyncML support for PHP

What do we need for SyncML?

SyncML is a protocol based on XML, so we need a XML parser. As wirelesss devices often have low-bandwidth network connections, the experts came up with a compressed XML variant called WBXML (Wireless Binary XML), so we need a parser for that as well.

The content, the actual data sent back and forth with SyncML is formatted as vCal, vCard, vNote and vTodo items, so we need parsers for those.

Then we need a SyncML server of course, that handles connection, authentication, item ID mapping, ...

Last but not least there is the question of synchronisation, who 'wins' if items have been modified on both ends of a connection? So we need some synchronisation algorithm.

A short list of what we need

  • XML parser
  • WBXML decoder/encoder
  • vCal, vCard, vNote, vTodo parsers
  • SyncML server
  • Synchronisation algorithm

XML parsing and handling

This isn't a problem with PHP. There are several ways of handling XML available, that allow for very flexible solutions. We can consider this piece of the puzzle solved.

WBXML handling

This isn't as easy. If the inventors had not decided to come up with a new format, but had used gz compression instead, this would have been easier. Anyway, now we have WBXML and need to handle it. But I have not seen a single PHP implementation of a WBXML decoder/encoder. There are solutions available for Java and C, though. In fact I have already seen scripts that use command line conversion tools via exec() or system().

As this isn't the best solution, I have written a simple PHP extension as wrapper around the WBXML Library written by Aymerick Jéhanne. It is available for download below. This piece of the puzzle could therefore be considered as solved as well.

Download: WBXML PHP extension 0.1. This is an early alpha, it seems to work, but I am not really a C coder. I have sent this to Aymerick, but had no response yet, so if anybody experienced with coding PHP extensions in C wants to look at this... I'm very open for comments, results, experiences!

To compile this with FreeBSD you must remove "-lm -ldl" from config.m4. Otherwise you can't configure successfully, as there is no in FreeBSD. (Thanks to Yingbo Qiu for this hint!).

Parsing of the content items

There are parsers available for vCal and vCard at least, but those would need some improvements to be more reliable and tolerant with varying input data. We'd need to handle different encodings (up to UTF-16, which Apple uses by default), missing fields and implicit default values.

When it comes to vNote and vTodo I have only seen the code in the Horde project (they have vCal and vCard as well). It might be best to use those, if they seem to go into the right direction.

SyncML server

This one handles all the SyncML bit moving. Sets up the connection with the client, handles the requests, does the local to global ID mapping, keeps track of the last sync date for every client and much more.

The most promising try is probably SyncML tools written by Nicolas Bougues, but again the Horde people are working on this as well.

Synchronisation algorithm

I think this is one of the tricky parts here. You need to look at the last sync date and check for the last change to a given entry. The fun begins when an item is missing on one side - has it been created on one or deleted from the other? And what if changes have been made on both sides? How is that resolved, with a duplicate entry, is one deleted, do we try to merge them?