public function process($request, $contentType, $respURI = null)
{
$isWBXML = $contentType == 'application/vnd.syncml+wbxml';
$this->_respURI = $respURI;
/* Catch any errors/warnings/notices that may get thrown while
* processing. Don't want to let anything go to the client that's not
* part of the valid response. */
ob_start();
$GLOBALS['backend']->logFile(Horde_SyncMl_Backend::LOGFILE_CLIENTMESSAGE, $request, $isWBXML);
if (!$isWBXML) {
/* XML code. */
/* try to extract charset from XML text */
if (preg_match('/^\\s*<\\?xml[^>]*encoding\\s*=\\s*"([^"]*)"/i', $request, $m)) {
$charset = $m[1];
} else {
$charset = 'UTF-8';
}
$GLOBALS['backend']->setCharset($charset);
/* Init output handler. */
$this->_xmlWriter =& Horde_SyncMl_XmlOutput::singleton();
/* Horde_Xml_Wbxml_ContentHandler Is a class that produces plain XML
* output. */
$this->_xmlWriter->init(new Horde_Xml_Wbxml_ContentHandler());
/* Create the XML parser and set method references. */
$parser = xml_parser_create_ns($charset);
xml_set_object($parser, $this);
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, false);
xml_set_element_handler($parser, '_startElement', '_endElement');
xml_set_character_data_handler($parser, '_characters');
xml_set_processing_instruction_handler($parser, '');
xml_set_external_entity_ref_handler($parser, '');
/* Here we go: fire off events: */
if (!xml_parse($parser, $request)) {
$s = sprintf('XML error: %s at line %d', xml_error_string(xml_get_error_code($parser)), xml_get_current_line_number($parser));
$GLOBALS['backend']->logMessage($s, 'ERR');
xml_parser_free($parser);
return new PEAR_Error($s);
}
xml_parser_free($parser);
} else {
/* The decoder works like the parser in the XML code above: It
* parses the input and calls the callback functions of $this. */
$this->_wbxmlparser = new Horde_Xml_Wbxml_Decoder();
$this->_wbxmlparser->setContentHandler($this);
/* Init output handler. */
$this->_xmlWriter =& Horde_SyncMl_XmlOutput::singleton();
$this->_xmlWriter->init(new Horde_Xml_Wbxml_Encoder());
/* Here we go: fire off events: */
/* @todo catch exceptions */
$this->_wbxmlparser->decode($request);
}
$id = @session_id();
$sessionclose = empty($id);
$output = $this->getOutput();
if (!$isWBXML) {
$output = '<?xml version="1.0" encoding="' . $charset . '"?>' . $output;
}
$GLOBALS['backend']->logFile(Horde_SyncMl_Backend::LOGFILE_SERVERMESSAGE, $output, $isWBXML, $sessionclose);
/* Clear the output buffer that we started above, and log anything
* that came up for later debugging. */
$errorLogging = ob_get_clean();
if (!empty($errorLogging)) {
$GLOBALS['backend']->logMessage('Caught output: ' . $errorLogging, 'WARN');
}
return $output;
}