[4898] | 1 | <?php |
---|
| 2 | /*********************************************** |
---|
| 3 | * File : wbxml.php |
---|
| 4 | * Project : Z-Push |
---|
| 5 | * Descr : WBXML mapping file |
---|
| 6 | * |
---|
| 7 | * Created : 01.10.2007 |
---|
| 8 | * |
---|
| 9 | * ᅵ Zarafa Deutschland GmbH, www.zarafaserver.de |
---|
| 10 | * This file is distributed under GPL v2. |
---|
| 11 | * Consult LICENSE file for details |
---|
| 12 | ************************************************/ |
---|
| 13 | include_once('debug.php'); |
---|
| 14 | |
---|
| 15 | define('WBXML_DEBUG', false); |
---|
| 16 | |
---|
| 17 | define('WBXML_SWITCH_PAGE', 0x00); |
---|
| 18 | define('WBXML_END', 0x01); |
---|
| 19 | define('WBXML_ENTITY', 0x02); |
---|
| 20 | define('WBXML_STR_I', 0x03); |
---|
| 21 | define('WBXML_LITERAL', 0x04); |
---|
| 22 | define('WBXML_EXT_I_0', 0x40); |
---|
| 23 | define('WBXML_EXT_I_1', 0x41); |
---|
| 24 | define('WBXML_EXT_I_2', 0x42); |
---|
| 25 | define('WBXML_PI', 0x43); |
---|
| 26 | define('WBXML_LITERAL_C', 0x44); |
---|
| 27 | define('WBXML_EXT_T_0', 0x80); |
---|
| 28 | define('WBXML_EXT_T_1', 0x81); |
---|
| 29 | define('WBXML_EXT_T_2', 0x82); |
---|
| 30 | define('WBXML_STR_T', 0x83); |
---|
| 31 | define('WBXML_LITERAL_A', 0x84); |
---|
| 32 | define('WBXML_EXT_0', 0xC0); |
---|
| 33 | define('WBXML_EXT_1', 0xC1); |
---|
| 34 | define('WBXML_EXT_2', 0xC2); |
---|
| 35 | define('WBXML_OPAQUE', 0xC3); |
---|
| 36 | define('WBXML_LITERAL_AC', 0xC4); |
---|
| 37 | |
---|
| 38 | define('EN_TYPE', 1); |
---|
| 39 | define('EN_TAG', 2); |
---|
| 40 | define('EN_CONTENT', 3); |
---|
| 41 | define('EN_FLAGS', 4); |
---|
| 42 | define('EN_ATTRIBUTES', 5); |
---|
| 43 | |
---|
| 44 | define('EN_TYPE_STARTTAG', 1); |
---|
| 45 | define('EN_TYPE_ENDTAG', 2); |
---|
| 46 | define('EN_TYPE_CONTENT', 3); |
---|
| 47 | |
---|
| 48 | define('EN_FLAGS_CONTENT', 1); |
---|
| 49 | define('EN_FLAGS_ATTRIBUTES', 2); |
---|
| 50 | |
---|
| 51 | class WBXMLDecoder { |
---|
| 52 | var $dtd; |
---|
| 53 | var $in; |
---|
| 54 | |
---|
| 55 | var $version; |
---|
| 56 | var $publicid; |
---|
| 57 | var $publicstringid; |
---|
| 58 | var $charsetid; |
---|
| 59 | var $stringtable; |
---|
| 60 | |
---|
| 61 | var $tagcp = 0; |
---|
| 62 | var $attrcp = 0; |
---|
| 63 | |
---|
| 64 | var $ungetbuffer; |
---|
| 65 | |
---|
| 66 | var $logStack = array(); |
---|
| 67 | |
---|
| 68 | function WBXMLDecoder($input, $dtd) { |
---|
| 69 | $this->in = $input; |
---|
| 70 | $this->dtd = $dtd; |
---|
| 71 | |
---|
| 72 | $this->version = $this->getByte(); |
---|
| 73 | $this->publicid = $this->getMBUInt(); |
---|
| 74 | if($this->publicid == 0) { |
---|
| 75 | $this->publicstringid = $this->getMBUInt(); |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | $this->charsetid = $this->getMBUInt(); |
---|
| 79 | $this->stringtable = $this->getStringTable(); |
---|
| 80 | |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | // Returns either start, content or end, and auto-concatenates successive content |
---|
| 84 | function getElement() |
---|
| 85 | { |
---|
| 86 | $element = $this->getToken(); |
---|
| 87 | |
---|
| 88 | switch($element[EN_TYPE]) { |
---|
| 89 | case EN_TYPE_STARTTAG: |
---|
| 90 | return $element; |
---|
| 91 | case EN_TYPE_ENDTAG: |
---|
| 92 | return $element; |
---|
| 93 | case EN_TYPE_CONTENT: |
---|
| 94 | while(1) { |
---|
| 95 | $next = $this->getToken(); |
---|
| 96 | if($next == false) |
---|
| 97 | return false; |
---|
| 98 | else if($next[EN_TYPE] == EN_CONTENT) { |
---|
| 99 | $element[EN_CONTENT] .= $next[EN_CONTENT]; |
---|
| 100 | } else { |
---|
| 101 | $this->ungetElement($next); |
---|
| 102 | break; |
---|
| 103 | } |
---|
| 104 | } |
---|
| 105 | return $element; |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | return false; |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | function peek() |
---|
| 112 | { |
---|
| 113 | $element = $this->getElement(); |
---|
| 114 | |
---|
| 115 | $this->ungetElement($element); |
---|
| 116 | |
---|
| 117 | return $element; |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | function getElementStartTag($tag) |
---|
| 121 | { |
---|
| 122 | $element = $this->getToken(); |
---|
| 123 | |
---|
| 124 | if($element[EN_TYPE] == EN_TYPE_STARTTAG && $element[EN_TAG] == $tag) |
---|
| 125 | return $element; |
---|
| 126 | else { |
---|
| 127 | debug("Unmatched tag $tag:"); |
---|
| 128 | debug(print_r($element,true)); |
---|
| 129 | $this->ungetElement($element); |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | return false; |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | function getElementEndTag() |
---|
| 136 | { |
---|
| 137 | $element = $this->getToken(); |
---|
| 138 | |
---|
| 139 | if($element[EN_TYPE] == EN_TYPE_ENDTAG) |
---|
| 140 | return $element; |
---|
| 141 | else { |
---|
| 142 | debug("Unmatched end tag:"); |
---|
| 143 | debug(print_r($element,true)); |
---|
| 144 | $bt = debug_backtrace(); |
---|
| 145 | $c = count($bt); |
---|
| 146 | debugLog(print_r($bt,true)); |
---|
| 147 | debug("From " . $bt[$c-2]["file"] . ":" . $bt[$c-2]["line"]); |
---|
| 148 | $this->ungetElement($element); |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | return false; |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | function getElementContent() |
---|
| 155 | { |
---|
| 156 | $element = $this->getToken(); |
---|
| 157 | |
---|
| 158 | if($element[EN_TYPE] == EN_TYPE_CONTENT) { |
---|
| 159 | return $element[EN_CONTENT]; |
---|
| 160 | } else { |
---|
| 161 | debug("Unmatched content:"); |
---|
| 162 | debug(print_r($element, true)); |
---|
| 163 | $this->ungetElement($element); |
---|
| 164 | } |
---|
| 165 | |
---|
| 166 | return false; |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | // ---------------------- Private functions ------------------------ |
---|
| 170 | |
---|
| 171 | function getToken() { |
---|
| 172 | // See if there's something in the ungetBuffer |
---|
| 173 | if($this->ungetbuffer) { |
---|
| 174 | $element = $this->ungetbuffer; |
---|
| 175 | $this->ungetbuffer = false; |
---|
| 176 | return $element; |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | $el = $this->_getToken(); |
---|
| 180 | |
---|
| 181 | $this->logToken($el); |
---|
| 182 | |
---|
| 183 | return $el; |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | function logToken($el) { |
---|
| 187 | if(!WBXML_DEBUG) |
---|
| 188 | return; |
---|
| 189 | $spaces = str_repeat(" ", count($this->logStack)); |
---|
| 190 | |
---|
| 191 | switch($el[EN_TYPE]) { |
---|
| 192 | case EN_TYPE_STARTTAG: |
---|
| 193 | if($el[EN_FLAGS] & EN_FLAGS_CONTENT) { |
---|
| 194 | debugLog("I " . $spaces . " <". $el[EN_TAG] . ">"); |
---|
| 195 | array_push($this->logStack, $el[EN_TAG]); |
---|
| 196 | } else |
---|
| 197 | debugLog("I " . $spaces . " <" . $el[EN_TAG] . "/>"); |
---|
| 198 | |
---|
| 199 | break; |
---|
| 200 | case EN_TYPE_ENDTAG: |
---|
| 201 | $tag = array_pop($this->logStack); |
---|
| 202 | debugLog("I " . $spaces . "</" . $tag . ">"); |
---|
| 203 | break; |
---|
| 204 | case EN_TYPE_CONTENT: |
---|
| 205 | debugLog("I " . $spaces . " " . $el[EN_CONTENT]); |
---|
| 206 | break; |
---|
| 207 | } |
---|
| 208 | } |
---|
| 209 | |
---|
| 210 | // Returns either a start tag, content or end tag |
---|
| 211 | function _getToken() { |
---|
| 212 | |
---|
| 213 | // Get the data from the input stream |
---|
| 214 | $element = array(); |
---|
| 215 | |
---|
| 216 | while(1) { |
---|
| 217 | $byte = $this->getByte(); |
---|
| 218 | |
---|
| 219 | if(!isset($byte)) |
---|
| 220 | break; |
---|
| 221 | |
---|
| 222 | switch($byte) { |
---|
| 223 | case WBXML_SWITCH_PAGE: |
---|
| 224 | $this->tagcp = $this->getByte(); |
---|
| 225 | continue; |
---|
| 226 | |
---|
| 227 | case WBXML_END: |
---|
| 228 | $element[EN_TYPE] = EN_TYPE_ENDTAG; |
---|
| 229 | return $element; |
---|
| 230 | |
---|
| 231 | case WBXML_ENTITY: |
---|
| 232 | $entity = $this->getMBUInt(); |
---|
| 233 | $element[EN_TYPE] = EN_TYPE_CONTENT; |
---|
| 234 | $element[EN_CONTENT] = $this->entityToCharset($entity); |
---|
| 235 | return $element; |
---|
| 236 | |
---|
| 237 | case WBXML_STR_I: |
---|
| 238 | $element[EN_TYPE] = EN_TYPE_CONTENT; |
---|
| 239 | $element[EN_CONTENT] = $this->getTermStr(); |
---|
| 240 | return $element; |
---|
| 241 | |
---|
| 242 | case WBXML_LITERAL: |
---|
| 243 | $element[EN_TYPE] = EN_TYPE_STARTTAG; |
---|
| 244 | $element[EN_TAG] = $this->getStringTableEntry($this->getMBUInt()); |
---|
| 245 | $element[EN_FLAGS] = 0; |
---|
| 246 | return $element; |
---|
| 247 | |
---|
| 248 | case WBXML_EXT_I_0: |
---|
| 249 | case WBXML_EXT_I_1: |
---|
| 250 | case WBXML_EXT_I_2: |
---|
| 251 | $this->getTermStr(); |
---|
| 252 | // Ignore extensions |
---|
| 253 | continue; |
---|
| 254 | |
---|
| 255 | case WBXML_PI: |
---|
| 256 | // Ignore PI |
---|
| 257 | $this->getAttributes(); |
---|
| 258 | continue; |
---|
| 259 | |
---|
| 260 | case WBXML_LITERAL_C: |
---|
| 261 | $element[EN_TYPE] = EN_TYPE_STARTTAG; |
---|
| 262 | $element[EN_TAG] = $this->getStringTableEntry($this->getMBUInt()); |
---|
| 263 | $element[EN_FLAGS] = EN_FLAGS_CONTENT; |
---|
| 264 | return $element; |
---|
| 265 | |
---|
| 266 | case WBXML_EXT_T_0: |
---|
| 267 | case WBXML_EXT_T_1: |
---|
| 268 | case WBXML_EXT_T_2: |
---|
| 269 | $this->getMBUInt(); |
---|
| 270 | // Ingore extensions; |
---|
| 271 | continue; |
---|
| 272 | |
---|
| 273 | case WBXML_STR_T: |
---|
| 274 | $element[EN_TYPE] = EN_TYPE_CONTENT; |
---|
| 275 | $element[EN_CONTENT] = $this->getStringTableEntry($this->getMBUInt()); |
---|
| 276 | return $element; |
---|
| 277 | |
---|
| 278 | case WBXML_LITERAL_A: |
---|
| 279 | $element[EN_TYPE] = EN_TYPE_STARTTAG; |
---|
| 280 | $element[EN_TAG] = $this->getStringTableEntry($this->getMBUInt()); |
---|
| 281 | $element[EN_ATTRIBUTES] = $this->getAttributes(); |
---|
| 282 | $element[EN_FLAGS] = EN_FLAGS_ATTRIBUTES; |
---|
| 283 | return $element; |
---|
| 284 | case WBXML_EXT_0: |
---|
| 285 | case WBXML_EXT_1: |
---|
| 286 | case WBXML_EXT_2: |
---|
| 287 | continue; |
---|
| 288 | |
---|
| 289 | case WBXML_OPAQUE: |
---|
| 290 | $length = $this->getMBUInt(); |
---|
| 291 | $element[EN_TYPE] = EN_TYPE_CONTENT; |
---|
| 292 | $element[EN_CONTENT] = $this->getOpaque($length); |
---|
| 293 | return $element; |
---|
| 294 | |
---|
| 295 | case WBXML_LITERAL_AC: |
---|
| 296 | $element[EN_TYPE] = EN_TYPE_STARTTAG; |
---|
| 297 | $element[EN_TAG] = $this->getStringTableEntry($this->getMBUInt()); |
---|
| 298 | $element[EN_ATTRIBUTES] = $this->getAttributes(); |
---|
| 299 | $element[EN_FLAGS] = EN_FLAGS_ATTRIBUTES | EN_FLAGS_CONTENT; |
---|
| 300 | return $element; |
---|
| 301 | |
---|
| 302 | default: |
---|
| 303 | $element[EN_TYPE] = EN_TYPE_STARTTAG; |
---|
| 304 | $element[EN_TAG] = $this->getMapping($this->tagcp, $byte & 0x3f); |
---|
| 305 | $element[EN_FLAGS] = ($byte & 0x80 ? EN_FLAGS_ATTRIBUTES : 0) | ($byte & 0x40 ? EN_FLAGS_CONTENT : 0); |
---|
| 306 | if($byte & 0x80) |
---|
| 307 | $element[EN_ATTRIBUTES] = $this->getAttributes(); |
---|
| 308 | return $element; |
---|
| 309 | } |
---|
| 310 | } |
---|
| 311 | } |
---|
| 312 | |
---|
| 313 | function ungetElement($element) { |
---|
| 314 | if($this->ungetbuffer) |
---|
| 315 | debugLog("Double unget!"); |
---|
| 316 | |
---|
| 317 | $this->ungetbuffer = $element; |
---|
| 318 | } |
---|
| 319 | |
---|
| 320 | function getAttributes() { |
---|
| 321 | $attributes = array(); |
---|
| 322 | $attr = ""; |
---|
| 323 | |
---|
| 324 | while(1) { |
---|
| 325 | $byte = $this->getByte(); |
---|
| 326 | |
---|
| 327 | if(count($byte) == 0) |
---|
| 328 | break; |
---|
| 329 | |
---|
| 330 | switch($byte) { |
---|
| 331 | case WBXML_SWITCH_PAGE: |
---|
| 332 | $this->attrcp = $this->getByte(); |
---|
| 333 | break; |
---|
| 334 | |
---|
| 335 | case WBXML_END: |
---|
| 336 | if($attr != "") |
---|
| 337 | $attributes += $this->splitAttribute($attr); |
---|
| 338 | |
---|
| 339 | return $attributes; |
---|
| 340 | |
---|
| 341 | case WBXML_ENTITY: |
---|
| 342 | $entity = $this->getMBUInt(); |
---|
| 343 | $attr .= $this->entityToCharset($entity); |
---|
| 344 | return $element; |
---|
| 345 | |
---|
| 346 | case WBXML_STR_I: |
---|
| 347 | $attr .= $this->getTermStr(); |
---|
| 348 | return $element; |
---|
| 349 | |
---|
| 350 | case WBXML_LITERAL: |
---|
| 351 | if($attr != "") |
---|
| 352 | $attributes += $this->splitAttribute($attr); |
---|
| 353 | |
---|
| 354 | $attr = $this->getStringTableEntry($this->getMBUInt()); |
---|
| 355 | return $element; |
---|
| 356 | |
---|
| 357 | case WBXML_EXT_I_0: |
---|
| 358 | case WBXML_EXT_I_1: |
---|
| 359 | case WBXML_EXT_I_2: |
---|
| 360 | $this->getTermStr(); |
---|
| 361 | continue; |
---|
| 362 | |
---|
| 363 | case WBXML_PI: |
---|
| 364 | case WBXML_LITERAL_C: |
---|
| 365 | // Invalid |
---|
| 366 | return false; |
---|
| 367 | |
---|
| 368 | case WBXML_EXT_T_0: |
---|
| 369 | case WBXML_EXT_T_1: |
---|
| 370 | case WBXML_EXT_T_2: |
---|
| 371 | $this->getMBUInt(); |
---|
| 372 | continue; |
---|
| 373 | |
---|
| 374 | case WBXML_STR_T: |
---|
| 375 | $attr .= $this->getStringTableEntry($this->getMBUInt()); |
---|
| 376 | return $element; |
---|
| 377 | |
---|
| 378 | case WBXML_LITERAL_A: |
---|
| 379 | return false; |
---|
| 380 | |
---|
| 381 | case WBXML_EXT_0: |
---|
| 382 | case WBXML_EXT_1: |
---|
| 383 | case WBXML_EXT_2: |
---|
| 384 | continue; |
---|
| 385 | |
---|
| 386 | case WBXML_OPAQUE: |
---|
| 387 | $length = $this->getMBUInt(); |
---|
| 388 | $attr .= $this->getOpaque($length); |
---|
| 389 | return $element; |
---|
| 390 | |
---|
| 391 | case WBXML_LITERAL_AC: |
---|
| 392 | return false; |
---|
| 393 | |
---|
| 394 | default: |
---|
| 395 | if($byte < 128) { |
---|
| 396 | if($attr != "") { |
---|
| 397 | $attributes += $this->splitAttribute($attr); |
---|
| 398 | $attr = ""; |
---|
| 399 | } |
---|
| 400 | } |
---|
| 401 | |
---|
| 402 | $attr .= $this->getMapping($this->attrcp, $byte); |
---|
| 403 | break; |
---|
| 404 | } |
---|
| 405 | } |
---|
| 406 | |
---|
| 407 | } |
---|
| 408 | |
---|
| 409 | function splitAttribute($attr) { |
---|
| 410 | $attributes = array(); |
---|
| 411 | |
---|
| 412 | $pos = strpos($attr,chr(61)); // equals sign |
---|
| 413 | |
---|
| 414 | if($pos) |
---|
| 415 | $attributes[substr($attr, 0, $pos)] = substr($attr, $pos+1); |
---|
| 416 | else |
---|
| 417 | $attributes[$attr] = null; |
---|
| 418 | |
---|
| 419 | return $attributes; |
---|
| 420 | } |
---|
| 421 | |
---|
| 422 | function getTermStr() { |
---|
| 423 | $str = ""; |
---|
| 424 | while(1) { |
---|
| 425 | $in = $this->getByte(); |
---|
| 426 | |
---|
| 427 | if($in == 0) |
---|
| 428 | break; |
---|
| 429 | else |
---|
| 430 | $str .= chr($in); |
---|
| 431 | } |
---|
| 432 | |
---|
| 433 | return $str; |
---|
| 434 | } |
---|
| 435 | |
---|
| 436 | function getOpaque($len) { |
---|
| 437 | return fread($this->in, $len); |
---|
| 438 | } |
---|
| 439 | |
---|
| 440 | function getByte() { |
---|
| 441 | $ch = fread($this->in, 1); |
---|
| 442 | if(strlen($ch) > 0) |
---|
| 443 | return ord($ch); |
---|
| 444 | else |
---|
| 445 | return; |
---|
| 446 | } |
---|
| 447 | |
---|
| 448 | function getMBUInt() { |
---|
| 449 | $uint = 0; |
---|
| 450 | |
---|
| 451 | while(1) { |
---|
| 452 | $byte = $this->getByte(); |
---|
| 453 | |
---|
| 454 | $uint |= $byte & 0x7f; |
---|
| 455 | |
---|
| 456 | if($byte & 0x80) |
---|
| 457 | $uint = $uint << 7; |
---|
| 458 | else |
---|
| 459 | break; |
---|
| 460 | } |
---|
| 461 | |
---|
| 462 | return $uint; |
---|
| 463 | } |
---|
| 464 | |
---|
| 465 | function getStringTable() { |
---|
| 466 | $stringtable = ""; |
---|
| 467 | |
---|
| 468 | $length = $this->getMBUInt(); |
---|
| 469 | if($length > 0) |
---|
| 470 | $stringtable = fread($this->in, $length); |
---|
| 471 | |
---|
| 472 | return $stringtable; |
---|
| 473 | } |
---|
| 474 | |
---|
| 475 | function getMapping($cp, $id) { |
---|
| 476 | if(!isset($this->dtd["codes"][$cp]) || !isset($this->dtd["codes"][$cp][$id])) |
---|
| 477 | return false; |
---|
| 478 | else { |
---|
| 479 | if(isset($this->dtd["namespaces"][$cp])) { |
---|
| 480 | return $this->dtd["namespaces"][$cp] . ":" . $this->dtd["codes"][$cp][$id]; |
---|
| 481 | } else |
---|
| 482 | return $this->dtd["codes"][$cp][$id]; |
---|
| 483 | } |
---|
| 484 | } |
---|
| 485 | } |
---|
| 486 | |
---|
| 487 | class WBXMLEncoder { |
---|
| 488 | var $_dtd; |
---|
| 489 | var $_out; |
---|
| 490 | |
---|
| 491 | var $_tagcp; |
---|
| 492 | var $_attrcp; |
---|
| 493 | |
---|
| 494 | var $logStack = array(); |
---|
| 495 | |
---|
| 496 | // We use a delayed output mechanism in which we only output a tag when it actually has something |
---|
| 497 | // in it. This can cause entire XML trees to disappear if they don't have output data in them; Ie |
---|
| 498 | // calling 'startTag' 10 times, and then 'endTag' will cause 0 bytes of output apart from the header. |
---|
| 499 | |
---|
| 500 | // Only when content() is called do we output the current stack of tags |
---|
| 501 | |
---|
| 502 | var $_stack; |
---|
| 503 | |
---|
| 504 | function WBXMLEncoder($output, $dtd) { |
---|
| 505 | $this->_out = $output; |
---|
| 506 | |
---|
| 507 | $this->_tagcp = 0; |
---|
| 508 | $this->_attrcp = 0; |
---|
| 509 | |
---|
| 510 | // reverse-map the DTD |
---|
| 511 | foreach($dtd["namespaces"] as $nsid => $nsname) { |
---|
| 512 | $this->_dtd["namespaces"][$nsname] = $nsid; |
---|
| 513 | } |
---|
| 514 | |
---|
| 515 | foreach($dtd["codes"] as $cp => $value) { |
---|
| 516 | $this->_dtd["codes"][$cp] = array(); |
---|
| 517 | foreach($dtd["codes"][$cp] as $tagid => $tagname) { |
---|
| 518 | $this->_dtd["codes"][$cp][$tagname] = $tagid; |
---|
| 519 | } |
---|
| 520 | } |
---|
| 521 | $this->_stack = array(); |
---|
| 522 | } |
---|
| 523 | |
---|
| 524 | function startWBXML() { |
---|
| 525 | header("Content-Type: application/vnd.ms-sync.wbxml"); |
---|
| 526 | |
---|
| 527 | $this->outByte(0x03); // WBXML 1.3 |
---|
| 528 | $this->outMBUInt(0x01); // Public ID 1 |
---|
| 529 | $this->outMBUInt(106); // UTF-8 |
---|
| 530 | $this->outMBUInt(0x00); // string table length (0) |
---|
| 531 | } |
---|
| 532 | |
---|
| 533 | function startTag($tag, $attributes = false, $nocontent = false) { |
---|
| 534 | $stackelem = array(); |
---|
| 535 | |
---|
| 536 | if(!$nocontent) { |
---|
| 537 | $stackelem['tag'] = $tag; |
---|
| 538 | $stackelem['attributes'] = $attributes; |
---|
| 539 | $stackelem['nocontent'] = $nocontent; |
---|
| 540 | $stackelem['sent'] = false; |
---|
| 541 | |
---|
| 542 | array_push($this->_stack, $stackelem); |
---|
| 543 | |
---|
| 544 | // If 'nocontent' is specified, then apparently the user wants to force |
---|
| 545 | // output of an empty tag, and we therefore output the stack here |
---|
| 546 | } else { |
---|
| 547 | $this->_outputStack(); |
---|
| 548 | $this->_startTag($tag, $attributes, $nocontent); |
---|
| 549 | } |
---|
| 550 | } |
---|
| 551 | |
---|
| 552 | function endTag() { |
---|
| 553 | $stackelem = array_pop($this->_stack); |
---|
| 554 | |
---|
| 555 | // Only output end tags for items that have had a start tag sent |
---|
| 556 | if($stackelem['sent']) { |
---|
| 557 | $this->_endTag(); |
---|
| 558 | } |
---|
| 559 | } |
---|
| 560 | |
---|
| 561 | function content($content) { |
---|
| 562 | // We need to filter out any \0 chars because it's the string terminator in WBXML. We currently |
---|
| 563 | // cannot send \0 characters within the XML content anywhere. |
---|
| 564 | $content = str_replace("\0","",$content); |
---|
| 565 | |
---|
| 566 | if("x" . $content == "x") |
---|
| 567 | return; |
---|
| 568 | $this->_outputStack(); |
---|
| 569 | $this->_content($content); |
---|
| 570 | } |
---|
| 571 | |
---|
| 572 | // Output any tags on the stack that haven't been output yet |
---|
| 573 | function _outputStack() { |
---|
| 574 | for($i=0;$i<count($this->_stack);$i++) { |
---|
| 575 | if(!$this->_stack[$i]['sent']) { |
---|
| 576 | $this->_startTag($this->_stack[$i]['tag'], $this->_stack[$i]['attributes'], $this->_stack[$i]['nocontent']); |
---|
| 577 | $this->_stack[$i]['sent'] = true; |
---|
| 578 | } |
---|
| 579 | } |
---|
| 580 | } |
---|
| 581 | |
---|
| 582 | // Outputs an actual start tag |
---|
| 583 | function _startTag($tag, $attributes = false, $nocontent = false) { |
---|
| 584 | $this->logStartTag($tag, $attributes, $nocontent); |
---|
| 585 | |
---|
| 586 | $mapping = $this->getMapping($tag); |
---|
| 587 | |
---|
| 588 | if(!$mapping) |
---|
| 589 | return false; |
---|
| 590 | |
---|
| 591 | if($this->_tagcp != $mapping["cp"]) { |
---|
| 592 | $this->outSwitchPage($mapping["cp"]); |
---|
| 593 | $this->_tagcp = $mapping["cp"]; |
---|
| 594 | } |
---|
| 595 | |
---|
| 596 | $code = $mapping["code"]; |
---|
| 597 | if(isset($attributes) && is_array($attributes) && count($attributes) > 0) { |
---|
| 598 | $code |= 0x80; |
---|
| 599 | } |
---|
| 600 | |
---|
| 601 | if(!isset($nocontent) || !$nocontent) |
---|
| 602 | $code |= 0x40; |
---|
| 603 | |
---|
| 604 | $this->outByte($code); |
---|
| 605 | |
---|
| 606 | if($code & 0x80) |
---|
| 607 | $this->outAttributes($attributes); |
---|
| 608 | } |
---|
| 609 | |
---|
| 610 | // Outputs actual data |
---|
| 611 | function _content($content) { |
---|
| 612 | $this->logContent($content); |
---|
| 613 | $this->outByte(WBXML_STR_I); |
---|
| 614 | $this->outTermStr($content); |
---|
| 615 | } |
---|
| 616 | |
---|
| 617 | // Outputs an actual end tag |
---|
| 618 | function _endTag() { |
---|
| 619 | $this->logEndTag(); |
---|
| 620 | $this->outByte(WBXML_END); |
---|
| 621 | } |
---|
| 622 | |
---|
| 623 | // --------------------------- Private |
---|
| 624 | |
---|
| 625 | function outByte($byte) { |
---|
| 626 | fwrite($this->_out, chr($byte)); |
---|
| 627 | } |
---|
| 628 | |
---|
| 629 | function outMBUInt($uint) { |
---|
| 630 | while(1) { |
---|
| 631 | $byte = $uint & 0x7f; |
---|
| 632 | $uint = $uint >> 7; |
---|
| 633 | if($uint == 0) { |
---|
| 634 | $this->outByte($byte); |
---|
| 635 | break; |
---|
| 636 | } else { |
---|
| 637 | $this->outByte($byte | 0x80); |
---|
| 638 | } |
---|
| 639 | } |
---|
| 640 | } |
---|
| 641 | |
---|
| 642 | function outTermStr($content) { |
---|
| 643 | fwrite($this->_out, $content); |
---|
| 644 | fwrite($this->_out, chr(0)); |
---|
| 645 | } |
---|
| 646 | |
---|
| 647 | function outAttributes() { |
---|
| 648 | // We don't actually support this, because to do so, we would have |
---|
| 649 | // to build a string table before sending the data (but we can't |
---|
| 650 | // because we're streaming), so we'll just send an END, which just |
---|
| 651 | // terminates the attribute list with 0 attributes. |
---|
| 652 | $this->outByte(WBXML_END); |
---|
| 653 | } |
---|
| 654 | |
---|
| 655 | function outSwitchPage($page) { |
---|
| 656 | $this->outByte(WBXML_SWITCH_PAGE); |
---|
| 657 | $this->outByte($page); |
---|
| 658 | } |
---|
| 659 | |
---|
| 660 | function getMapping($tag) { |
---|
| 661 | $mapping = array(); |
---|
| 662 | |
---|
| 663 | $split = $this->splitTag($tag); |
---|
| 664 | |
---|
| 665 | if(isset($split["ns"])) { |
---|
| 666 | $cp = $this->_dtd["namespaces"][$split["ns"]]; |
---|
| 667 | } else { |
---|
| 668 | $cp = 0; |
---|
| 669 | } |
---|
| 670 | |
---|
| 671 | $code = $this->_dtd["codes"][$cp][$split["tag"]]; |
---|
| 672 | |
---|
| 673 | $mapping["cp"] = $cp; |
---|
| 674 | $mapping["code"] = $code; |
---|
| 675 | |
---|
| 676 | return $mapping; |
---|
| 677 | } |
---|
| 678 | |
---|
| 679 | function splitTag($fulltag) { |
---|
| 680 | $ns = false; |
---|
| 681 | $pos = strpos($fulltag, chr(58)); // chr(58) == ':' |
---|
| 682 | |
---|
| 683 | if($pos) { |
---|
| 684 | $ns = substr($fulltag, 0, $pos); |
---|
| 685 | $tag = substr($fulltag, $pos+1); |
---|
| 686 | } else { |
---|
| 687 | $tag = $fulltag; |
---|
| 688 | } |
---|
| 689 | |
---|
| 690 | $ret = array(); |
---|
| 691 | if($ns) |
---|
| 692 | $ret["ns"] = $ns; |
---|
| 693 | $ret["tag"] = $tag; |
---|
| 694 | |
---|
| 695 | return $ret; |
---|
| 696 | } |
---|
| 697 | |
---|
| 698 | function logStartTag($tag, $attr, $nocontent) { |
---|
| 699 | if(!WBXML_DEBUG) |
---|
| 700 | return; |
---|
| 701 | |
---|
| 702 | $spaces = str_repeat(" ", count($this->logStack)); |
---|
| 703 | if($nocontent) |
---|
| 704 | debugLog("O " . $spaces . " <$tag/>"); |
---|
| 705 | else { |
---|
| 706 | array_push($this->logStack, $tag); |
---|
| 707 | debugLog("O " . $spaces . " <$tag>"); |
---|
| 708 | } |
---|
| 709 | } |
---|
| 710 | |
---|
| 711 | function logEndTag() { |
---|
| 712 | if(!WBXML_DEBUG) |
---|
| 713 | return; |
---|
| 714 | |
---|
| 715 | $spaces = str_repeat(" ", count($this->logStack)); |
---|
| 716 | $tag = array_pop($this->logStack); |
---|
| 717 | debugLog("O " . $spaces . "</$tag>"); |
---|
| 718 | } |
---|
| 719 | |
---|
| 720 | function logContent($content) { |
---|
| 721 | if(!WBXML_DEBUG) |
---|
| 722 | return; |
---|
| 723 | |
---|
| 724 | $spaces = str_repeat(" ", count($this->logStack)); |
---|
| 725 | debugLog("O " . $spaces . $content); |
---|
| 726 | } |
---|
| 727 | } |
---|