[ Index ] |
|
Code source de eGroupWare 1.2.106-2 |
1 <?php 2 3 require_once 'Horde/SyncML.php'; 4 #require_once 'Horde/SyncML/State.php'; 5 require_once 'Horde/SyncML/State_egw.php'; 6 require_once 'Horde/SyncML/Command/Status.php'; 7 8 /** 9 * The Horde_RPC_syncml class provides a SyncML implementation of the Horde 10 * RPC system. 11 * 12 * $Horde: framework/RPC/RPC/syncml.php,v 1.27 2006/01/01 21:10:11 jan Exp $ 13 * 14 * Copyright 2003-2006 Chuck Hagenbuch <chuck@horde.org> 15 * Copyright 2003-2006 Anthony Mills <amills@pyramid6.com> 16 * 17 * See the enclosed file COPYING for license information (LGPL). If you 18 * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html. 19 * 20 * @author Chuck Hagenbuch <chuck@horde.org> 21 * @author Anthony Mills <amills@pyramid6.com> 22 * @since Horde 3.0 23 * @package Horde_RPC 24 */ 25 class Horde_RPC_syncml extends Horde_RPC { 26 27 /** 28 * Output ContentHandler used to output XML events. 29 * 30 * @var object 31 */ 32 var $_output; 33 34 /** 35 * @var integer 36 */ 37 var $_xmlStack = 0; 38 39 /** 40 * Debug directory, if set will store copies of all packets. 41 * 42 * @var string 43 */ 44 var $_debugDir = '/tmp/sync'; 45 46 /** 47 * Default character set. Only supports UTF-8(ASCII?). 48 * 49 * @var string 50 */ 51 var $_charset = 'UTF-8'; 52 53 /** 54 * SyncML handles authentication internally, so bypass the RPC framework 55 * auth check by just returning true here. 56 */ 57 function authorize() 58 { 59 return true; 60 } 61 62 /** 63 * Sends an RPC request to the server and returns the result. 64 * 65 * @param string $request The raw request string. 66 * 67 * @return string The XML encoded response from the server. 68 */ 69 function getResponse($request) 70 { 71 /* Catch any errors/warnings/notices that may get thrown while 72 * processing. Don't want to let anything go to the client that's not 73 * part of the valid response. */ 74 ob_start(); 75 76 /* Very useful for debugging. Logs WBXML packets to 77 * $this->_debugDir. */ 78 if (!empty($this->_debugDir) && is_dir($this->_debugDir)) { 79 $packetNum = @intval(file_get_contents($this->_debugDir . '/syncml.packetnum')); 80 if (!isset($packetNum)) { 81 $packetNum = 0; 82 } 83 84 $f = @fopen($this->_debugDir . '/syncml_client_' . $packetNum . '.xml', 'wb'); 85 if ($f) { 86 fwrite($f, $request); 87 fclose($f); 88 } 89 } 90 91 require_once 'XML/WBXML/ContentHandler.php'; 92 $this->_output = &new XML_WBXML_ContentHandler(); 93 94 $this->_parse($request); 95 $response = $this->_output->getOutput(); 96 97 /* Very useful for debugging. */ 98 if (!empty($this->_debugDir) && is_dir($this->_debugDir)) { 99 $f = @fopen($this->_debugDir . '/syncml_server_' . $packetNum . '.xml', 'wb'); 100 if ($f) { 101 fwrite($f, $response); 102 fclose($f); 103 } 104 105 $fp = @fopen($this->_debugDir . '/syncml.packetnum', 'w'); 106 if ($fp) { 107 fwrite($fp, ++$packetNum); 108 fclose($fp); 109 } 110 } 111 112 /* Clear the output buffer that we started above, and log anything 113 * that came up for later debugging. */ 114 $errorLogging = ob_get_clean(); 115 if (!empty($errorLogging)) { 116 Horde::logMessage('SyncML: caught output=' . 117 $errorLogging, __FILE__, __LINE__, PEAR_LOG_DEBUG); 118 } 119 120 return $response; 121 } 122 123 function _parse($xml) 124 { 125 /* try to extract charset from XML text */ 126 if(preg_match('/^\s*<\?xml[^>]*encoding\s*=\s*"([^"]*)"/i', 127 $xml, $m)) { 128 $this->_charset = $m[1]; 129 } 130 #NLS::setCharset($this->_charset); 131 #String::setDefaultCharset($this->_charset); 132 133 /* Create the XML parser and set method references. */ 134 $this->_parser = xml_parser_create_ns($this->_charset); 135 xml_set_object($this->_parser, $this); 136 xml_parser_set_option($this->_parser, XML_OPTION_CASE_FOLDING, false); 137 xml_set_element_handler($this->_parser, '_startElement', '_endElement'); 138 xml_set_character_data_handler($this->_parser, '_characters'); 139 xml_set_processing_instruction_handler($this->_parser, ''); 140 xml_set_external_entity_ref_handler($this->_parser, ''); 141 142 if (!xml_parse($this->_parser, $xml)) { 143 return $this->raiseError(sprintf('XML error: %s at line %d', 144 xml_error_string(xml_get_error_code($this->_parser)), 145 xml_get_current_line_number($this->_parser))); 146 } 147 148 xml_parser_free($this->_parser); 149 } 150 151 function _startElement($parser, $tag, $attributes) 152 { 153 list($uri, $name) = $this->_splitURI($tag); 154 155 $this->startElement($uri, $name, $attributes); 156 } 157 158 function _characters($parser, $chars) 159 { 160 $this->characters($chars); 161 } 162 163 function _endElement($parser, $tag) 164 { 165 list($uri, $name) = $this->_splitURI($tag); 166 167 $this->endElement($uri, $name); 168 } 169 170 function _splitURI($tag) 171 { 172 $parts = explode(':', $tag); 173 $name = array_pop($parts); 174 $uri = implode(':', $parts); 175 return array($uri, $name); 176 } 177 178 /** 179 * Returns the Content-Type of the response. 180 * 181 * @return string The MIME Content-Type of the RPC response. 182 */ 183 function getResponseContentType() 184 { 185 return 'application/vnd.syncml+xml'; 186 } 187 188 function startElement($uri, $element, $attrs) 189 { 190 $this->_xmlStack++; 191 192 switch ($this->_xmlStack) { 193 case 1: 194 // <SyncML> 195 // Defined in SyncML Representation Protocol, version 1.1 5.2.1 196 $this->_output->startElement($uri, $element, $attrs); 197 break; 198 199 case 2: 200 // Either <SyncML><SyncHdr> or <SyncML><SyncBody> 201 if (!isset($this->_contentHandler)) { 202 // If not defined then create SyncHdr. 203 $this->_contentHandler = &new Horde_SyncML_SyncmlHdr(); 204 $this->_contentHandler->setOutput($this->_output); 205 } 206 207 $this->_contentHandler->startElement($uri, $element, $attrs); 208 break; 209 210 default: 211 if (isset($this->_contentHandler)) { 212 $this->_contentHandler->startElement($uri, $element, $attrs); 213 } 214 break; 215 } 216 } 217 218 function endElement($uri, $element) 219 { 220 switch ($this->_xmlStack) { 221 case 1: 222 // </SyncML> 223 // Defined in SyncML Representation Protocol, version 1.1 5.2.1 224 $this->_output->endElement($uri, $element); 225 break; 226 227 case 2: 228 // Either </SyncHdr></SyncML> or </SyncBody></SyncML> 229 if ($element == 'SyncHdr') { 230 // Then we get the state from SyncMLHdr, and create a new 231 // SyncMLBody. 232 $this->_contentHandler->endElement($uri, $element); 233 234 unset($this->_contentHandler); 235 236 $this->_contentHandler = &new Horde_SyncML_SyncmlBody(); 237 $this->_contentHandler->setOutput($this->_output); 238 } else { 239 // No longer used. 240 $this->_contentHandler->endElement($uri, $element); 241 unset($this->_contentHandler); 242 } 243 break; 244 245 default: 246 // </*></SyncHdr></SyncML> or </*></SyncBody></SyncML> 247 if (isset($this->_contentHandler)) { 248 $this->_contentHandler->endElement($uri, $element); 249 } 250 break; 251 } 252 253 if (isset($this->_chars)) { 254 unset($this->_chars); 255 } 256 257 $this->_xmlStack--; 258 } 259 260 function characters($str) 261 { 262 if (isset($this->_contentHandler)) { 263 $this->_contentHandler->characters($str); 264 } 265 } 266 267 function raiseError($str) 268 { 269 return Horde::logMessage($str, __FILE__, __LINE__, PEAR_LOG_ERR); 270 } 271 272 }
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sun Feb 25 17:20:01 2007 | par Balluche grâce à PHPXref 0.7 |