| Total Complexity | 89 |
| Total Lines | 484 |
| Duplicated Lines | 0 % |
| Changes | 7 | ||
| Bugs | 2 | Features | 0 |
Complex classes like WBXMLDecoder often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use WBXMLDecoder, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | class WBXMLDecoder extends WBXMLDefs { |
||
| 12 | private $in; |
||
| 13 | private $inLog; |
||
|
|
|||
| 14 | private $tagcp = 0; |
||
| 15 | private $ungetbuffer; |
||
| 16 | private $log = false; |
||
| 17 | private $logStack = []; |
||
| 18 | private $inputBuffer = ""; |
||
| 19 | private $isWBXML = true; |
||
| 20 | private static $loopCounter = []; |
||
| 21 | public const MAXLOOP = 5000; |
||
| 22 | public const VERSION = 0x03; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Counts the amount of times a code part has been executed. |
||
| 26 | * When being executed too often, the code throws a WBMXLException. |
||
| 27 | * |
||
| 28 | * @param string $name |
||
| 29 | * |
||
| 30 | * @return bool |
||
| 31 | * |
||
| 32 | * @throws WBXMLException |
||
| 33 | */ |
||
| 34 | public static function InWhile($name) { |
||
| 35 | if (!isset(self::$loopCounter[$name])) { |
||
| 36 | self::$loopCounter[$name] = 0; |
||
| 37 | } |
||
| 38 | else { |
||
| 39 | ++self::$loopCounter[$name]; |
||
| 40 | } |
||
| 41 | |||
| 42 | if (self::$loopCounter[$name] > self::MAXLOOP) { |
||
| 43 | throw new WBXMLException(sprintf("Loop count in while too high, code '%s' exceeded max. amount of permitted loops", $name)); |
||
| 44 | } |
||
| 45 | |||
| 46 | return true; |
||
| 47 | } |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Resets the inWhile counter. |
||
| 51 | * |
||
| 52 | * @param string $name |
||
| 53 | * |
||
| 54 | * @return bool |
||
| 55 | */ |
||
| 56 | public static function ResetInWhile($name) { |
||
| 57 | if (isset(self::$loopCounter[$name])) { |
||
| 58 | unset(self::$loopCounter[$name]); |
||
| 59 | } |
||
| 60 | |||
| 61 | return true; |
||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * WBXML Decode Constructor |
||
| 66 | * We only handle ActiveSync WBXML, which is a subset of WBXML. |
||
| 67 | * |
||
| 68 | * @param stream $input the incoming data stream |
||
| 69 | */ |
||
| 70 | public function __construct($input) { |
||
| 71 | $this->log = SLog::IsWbxmlDebugEnabled(); |
||
| 72 | |||
| 73 | $this->in = $input; |
||
| 74 | |||
| 75 | $version = $this->getByte(); |
||
| 76 | if ($version != self::VERSION) { |
||
| 77 | $this->inputBuffer .= chr($version); |
||
| 78 | $this->isWBXML = false; |
||
| 79 | |||
| 80 | return; |
||
| 81 | } |
||
| 82 | |||
| 83 | $publicid = $this->getMBUInt(); |
||
| 84 | if ($publicid !== 1) { |
||
| 85 | throw new WBXMLException("Wrong publicid : " . $publicid); |
||
| 86 | } |
||
| 87 | |||
| 88 | $charsetid = $this->getMBUInt(); |
||
| 89 | if ($charsetid !== 106) { |
||
| 90 | throw new WBXMLException("Wrong charset : " . $charsetid); |
||
| 91 | } |
||
| 92 | |||
| 93 | $stringtablesize = $this->getMBUInt(); |
||
| 94 | if ($stringtablesize !== 0) { |
||
| 95 | throw new WBXMLException("Wrong string table size : " . $stringtablesize); |
||
| 96 | } |
||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Returns either start, content or end, and auto-concatenates successive content. |
||
| 101 | * |
||
| 102 | * @return element|value |
||
| 103 | */ |
||
| 104 | public function getElement() { |
||
| 105 | $element = $this->getToken(); |
||
| 106 | if (is_null($element)) { |
||
| 107 | return false; |
||
| 108 | } |
||
| 109 | |||
| 110 | switch ($element[EN_TYPE]) { |
||
| 111 | case EN_TYPE_STARTTAG: |
||
| 112 | return $element; |
||
| 113 | |||
| 114 | case EN_TYPE_ENDTAG: |
||
| 115 | return $element; |
||
| 116 | |||
| 117 | case EN_TYPE_CONTENT: |
||
| 118 | WBXMLDecoder::ResetInWhile("decoderGetElement"); |
||
| 119 | while (WBXMLDecoder::InWhile("decoderGetElement")) { |
||
| 120 | $next = $this->getToken(); |
||
| 121 | if ($next == false) { |
||
| 122 | return false; |
||
| 123 | } |
||
| 124 | if ($next[EN_TYPE] == EN_CONTENT) { |
||
| 125 | $element[EN_CONTENT] .= $next[EN_CONTENT]; |
||
| 126 | } |
||
| 127 | else { |
||
| 128 | $this->ungetElement($next); |
||
| 129 | |||
| 130 | break; |
||
| 131 | } |
||
| 132 | } |
||
| 133 | |||
| 134 | return $element; |
||
| 135 | } |
||
| 136 | |||
| 137 | return false; |
||
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Get a peek at the next element. |
||
| 142 | * |
||
| 143 | * @return element |
||
| 144 | */ |
||
| 145 | public function peek() { |
||
| 146 | $element = $this->getElement(); |
||
| 147 | $this->ungetElement($element); |
||
| 148 | |||
| 149 | return $element; |
||
| 150 | } |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Get the element of a StartTag. |
||
| 154 | * |
||
| 155 | * @param mixed $tag |
||
| 156 | * |
||
| 157 | * @return bool|element returns false if not available |
||
| 158 | */ |
||
| 159 | public function getElementStartTag($tag) { |
||
| 160 | $element = $this->getToken(); |
||
| 161 | |||
| 162 | if (!$element) { |
||
| 163 | return false; |
||
| 164 | } |
||
| 165 | |||
| 166 | if ($element[EN_TYPE] == EN_TYPE_STARTTAG && $element[EN_TAG] == $tag) { |
||
| 167 | return $element; |
||
| 168 | } |
||
| 169 | |||
| 170 | SLog::Write(LOGLEVEL_WBXMLSTACK, sprintf("WBXMLDecoder->getElementStartTag(): unmatched WBXML tag: '%s' matching '%s' type '%s' flags '%s'", $tag, $element[EN_TAG] ?? "", $element[EN_TYPE] ?? "", $element[EN_FLAGS] ?? "")); |
||
| 171 | $this->ungetElement($element); |
||
| 172 | |||
| 173 | return false; |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Get the element of a EndTag. |
||
| 178 | * |
||
| 179 | * @return bool|element returns false if not available |
||
| 180 | */ |
||
| 181 | public function getElementEndTag() { |
||
| 182 | $element = $this->getToken(); |
||
| 183 | |||
| 184 | if ($element[EN_TYPE] == EN_TYPE_ENDTAG) { |
||
| 185 | return $element; |
||
| 186 | } |
||
| 187 | |||
| 188 | SLog::Write(LOGLEVEL_WBXMLSTACK, sprintf("WBXMLDecoder->getElementEndTag(): unmatched WBXML tag: '%s' type '%s' flags '%s'", $element[EN_TAG] ?? "", $element[EN_TYPE] ?? "", $element[EN_FLAGS] ?? "")); |
||
| 189 | |||
| 190 | $bt = debug_backtrace(); |
||
| 191 | SLog::Write(LOGLEVEL_ERROR, sprintf("WBXMLDecoder->getElementEndTag(): could not read end tag in '%s'. Please enable the LOGLEVEL_WBXML and send the log to the grommunio-sync dev team.", $bt[0]["file"] . ":" . $bt[0]["line"])); |
||
| 192 | |||
| 193 | // log the remaining wbxml content |
||
| 194 | $this->ungetElement($element); |
||
| 195 | while ($el = $this->getElement()); |
||
| 196 | |||
| 197 | return false; |
||
| 198 | } |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Get the content of an element. |
||
| 202 | * |
||
| 203 | * @return bool|string returns false if not available |
||
| 204 | */ |
||
| 205 | public function getElementContent() { |
||
| 206 | $element = $this->getToken(); |
||
| 207 | |||
| 208 | if ($element[EN_TYPE] == EN_TYPE_CONTENT) { |
||
| 209 | return $element[EN_CONTENT]; |
||
| 210 | } |
||
| 211 | |||
| 212 | SLog::Write(LOGLEVEL_WBXMLSTACK, sprintf("WBXMLDecoder->getElementContent(): unmatched WBXML content: '%s' type '%s' flags '%s'", $element[EN_TAG] ?? "", $element[EN_TYPE] ?? "", $element[EN_FLAGS] ?? "")); |
||
| 213 | $this->ungetElement($element); |
||
| 214 | |||
| 215 | return false; |
||
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * 'Ungets' an element writing it into a buffer to be 'get' again. |
||
| 220 | * |
||
| 221 | * @param element $element the element to get ungetten |
||
| 222 | */ |
||
| 223 | public function ungetElement($element) { |
||
| 224 | if ($this->ungetbuffer) { |
||
| 225 | SLog::Write(LOGLEVEL_ERROR, sprintf("WBXMLDecoder->ungetElement(): WBXML double unget on tag: '%s' type '%s' flags '%s'", $element[EN_TAG] ?? "", $element[EN_TYPE] ?? "", $element[EN_FLAGS] ?? "")); |
||
| 226 | } |
||
| 227 | |||
| 228 | $this->ungetbuffer = $element; |
||
| 229 | } |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Returns the plain input stream. |
||
| 233 | * |
||
| 234 | * @return string |
||
| 235 | */ |
||
| 236 | public function GetPlainInputStream() { |
||
| 237 | return $this->inputBuffer . stream_get_contents($this->in); |
||
| 238 | } |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Returns if the input is WBXML. |
||
| 242 | * |
||
| 243 | * @return bool |
||
| 244 | */ |
||
| 245 | public function IsWBXML() { |
||
| 246 | return $this->isWBXML; |
||
| 247 | } |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Reads the remaining data from the input stream. |
||
| 251 | */ |
||
| 252 | public function readRemainingData() { |
||
| 255 | } |
||
| 256 | |||
| 257 | /*---------------------------------------------------------------------------------------------------------- |
||
| 258 | * Private WBXMLDecoder stuff |
||
| 259 | */ |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Returns the next token. |
||
| 263 | * |
||
| 264 | * @return token |
||
| 265 | */ |
||
| 266 | private function getToken() { |
||
| 281 | } |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Log the a token to SLog. |
||
| 285 | * |
||
| 286 | * @param string $el token |
||
| 287 | */ |
||
| 288 | private function logToken($el) { |
||
| 289 | $spaces = str_repeat(" ", count($this->logStack)); |
||
| 290 | |||
| 291 | switch ($el[EN_TYPE]) { |
||
| 292 | case EN_TYPE_STARTTAG: |
||
| 293 | if ($el[EN_FLAGS] & EN_FLAGS_CONTENT) { |
||
| 294 | SLog::Write(LOGLEVEL_WBXML, sprintf("I %s <%s>", $spaces, $el[EN_TAG])); |
||
| 295 | array_push($this->logStack, $el[EN_TAG]); |
||
| 296 | } |
||
| 297 | else { |
||
| 298 | SLog::Write(LOGLEVEL_WBXML, sprintf("I %s <%s/>", $spaces, $el[EN_TAG])); |
||
| 299 | } |
||
| 300 | break; |
||
| 301 | |||
| 302 | case EN_TYPE_ENDTAG: |
||
| 303 | $tag = array_pop($this->logStack); |
||
| 304 | SLog::Write(LOGLEVEL_WBXML, sprintf("I %s</%s>", $spaces, $tag)); |
||
| 305 | break; |
||
| 306 | |||
| 307 | case EN_TYPE_CONTENT: |
||
| 308 | $messagesize = strlen($el[EN_CONTENT]); |
||
| 309 | // don't log binary data |
||
| 310 | if (mb_detect_encoding($el[EN_CONTENT], null, true) === false) { |
||
| 311 | $content = sprintf("(BINARY DATA: %d bytes long)", $messagesize); |
||
| 312 | } |
||
| 313 | // truncate logged data to 10K |
||
| 314 | elseif ($messagesize > 10240 && !defined('WBXML_DEBUGGING')) { |
||
| 315 | $content = sprintf("%s (log message with %d bytes truncated)", substr($el[EN_CONTENT], 0, 10240), $messagesize); |
||
| 316 | } |
||
| 317 | else { |
||
| 318 | $content = $el[EN_CONTENT]; |
||
| 319 | } |
||
| 320 | |||
| 321 | SLog::Write(LOGLEVEL_WBXML, sprintf("I %s %s", $spaces, $content), false); |
||
| 322 | break; |
||
| 323 | } |
||
| 324 | } |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Returns either a start tag, content or end tag. |
||
| 328 | */ |
||
| 329 | private function _getToken() { |
||
| 391 | } |
||
| 392 | } |
||
| 393 | } |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Reads from the stream until getting a string terminator. |
||
| 397 | * |
||
| 398 | * @return string |
||
| 399 | */ |
||
| 400 | private function getTermStr() { |
||
| 401 | if (defined('WBXML_DEBUGGING') && WBXML_DEBUGGING === true) { |
||
| 402 | $str = ""; |
||
| 403 | while (1) { |
||
| 404 | $in = $this->getByte(); |
||
| 405 | if ($in == 0) { |
||
| 406 | break; |
||
| 407 | } |
||
| 408 | |||
| 409 | $str .= chr($in); |
||
| 410 | } |
||
| 411 | |||
| 412 | return $str; |
||
| 413 | } |
||
| 414 | |||
| 415 | // there is no unlimited "length" for stream_get_line, |
||
| 416 | // so we use a huge value for "length" param (1Gb) |
||
| 417 | // (0 == PHP_SOCK_CHUNK_SIZE (8192)) |
||
| 418 | // internally php read at most PHP_SOCK_CHUNK_SIZE at a time, |
||
| 419 | // so we can use a huge value for "length" without problem |
||
| 420 | return stream_get_line($this->in, 1073741824, "\0"); |
||
| 421 | } |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Reads $len from the input stream. |
||
| 425 | * |
||
| 426 | * @param int $len |
||
| 427 | * |
||
| 428 | * @return string |
||
| 429 | */ |
||
| 430 | private function getOpaque($len) { |
||
| 441 | } |
||
| 442 | |||
| 443 | /** |
||
| 444 | * Reads one byte from the input stream. |
||
| 445 | * |
||
| 446 | * @return int|void |
||
| 447 | */ |
||
| 448 | private function getByte() { |
||
| 452 | } |
||
| 453 | } |
||
| 454 | |||
| 455 | /** |
||
| 456 | * Reads string length from the input stream. |
||
| 457 | */ |
||
| 458 | private function getMBUInt() { |
||
| 459 | $uint = 0; |
||
| 460 | |||
| 461 | while (1) { |
||
| 462 | $byte = $this->getByte(); |
||
| 463 | |||
| 464 | $uint |= $byte & 0x7F; |
||
| 465 | |||
| 466 | if ($byte & 0x80) { |
||
| 467 | $uint = $uint << 7; |
||
| 468 | } |
||
| 469 | else { |
||
| 470 | break; |
||
| 471 | } |
||
| 472 | } |
||
| 473 | |||
| 474 | return $uint; |
||
| 475 | } |
||
| 476 | |||
| 477 | /** |
||
| 478 | * Returns the mapping for a specified codepage and id. |
||
| 479 | * |
||
| 480 | * @param $cp codepage |
||
| 481 | * @param mixed $id |
||
| 482 | * |
||
| 483 | * @return string |
||
| 484 | */ |
||
| 485 | private function getMapping($cp, $id) { |
||
| 495 | } |
||
| 496 | } |
||
| 497 |