source: trunk/zpush/lib/wbxml/wbxmlencoder.php @ 7589

Revision 7589, 13.4 KB checked in by douglas, 11 years ago (diff)

Ticket #3209 - Integrar módulo de sincronização Z-push ao Expresso

Line 
1<?php
2/***********************************************
3* File      :   wbxmlencoder.php
4* Project   :   Z-Push
5* Descr     :   WBXMLEncoder encodes to Wap Binary XML
6*
7* Created   :   01.10.2007
8*
9* Copyright 2007 - 2012 Zarafa Deutschland GmbH
10*
11* This program is free software: you can redistribute it and/or modify
12* it under the terms of the GNU Affero General Public License, version 3,
13* as published by the Free Software Foundation with the following additional
14* term according to sec. 7:
15*
16* According to sec. 7 of the GNU Affero General Public License, version 3,
17* the terms of the AGPL are supplemented with the following terms:
18*
19* "Zarafa" is a registered trademark of Zarafa B.V.
20* "Z-Push" is a registered trademark of Zarafa Deutschland GmbH
21* The licensing of the Program under the AGPL does not imply a trademark license.
22* Therefore any rights, title and interest in our trademarks remain entirely with us.
23*
24* However, if you propagate an unmodified version of the Program you are
25* allowed to use the term "Z-Push" to indicate that you distribute the Program.
26* Furthermore you may use our trademarks where it is necessary to indicate
27* the intended purpose of a product or service provided you use it in accordance
28* with honest practices in industrial or commercial matters.
29* If you want to propagate modified versions of the Program under the name "Z-Push",
30* you may only do so if you have a written permission by Zarafa Deutschland GmbH
31* (to acquire a permission please contact Zarafa at trademark@zarafa.com).
32*
33* This program is distributed in the hope that it will be useful,
34* but WITHOUT ANY WARRANTY; without even the implied warranty of
35* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36* GNU Affero General Public License for more details.
37*
38* You should have received a copy of the GNU Affero General Public License
39* along with this program.  If not, see <http://www.gnu.org/licenses/>.
40*
41* Consult LICENSE file for details
42************************************************/
43
44
45class WBXMLEncoder extends WBXMLDefs {
46    private $_dtd;
47    private $_out;
48
49    private $_tagcp;
50    private $_attrcp;
51
52    private $logStack = array();
53
54    // We use a delayed output mechanism in which we only output a tag when it actually has something
55    // in it. This can cause entire XML trees to disappear if they don't have output data in them; Ie
56    // calling 'startTag' 10 times, and then 'endTag' will cause 0 bytes of output apart from the header.
57
58    // Only when content() is called do we output the current stack of tags
59
60    private $_stack;
61
62    private $multipart; // the content is multipart
63    private $bodyparts;
64
65    public function WBXMLEncoder($output, $multipart = false) {
66        // make sure WBXML_DEBUG is defined. It should be at this point
67        if (!defined('WBXML_DEBUG')) define('WBXML_DEBUG', false);
68
69        $this->_out = $output;
70
71        $this->_tagcp = 0;
72        $this->_attrcp = 0;
73
74        // reverse-map the DTD
75        foreach($this->dtd["namespaces"] as $nsid => $nsname) {
76            $this->_dtd["namespaces"][$nsname] = $nsid;
77        }
78
79        foreach($this->dtd["codes"] as $cp => $value) {
80            $this->_dtd["codes"][$cp] = array();
81            foreach($this->dtd["codes"][$cp] as $tagid => $tagname) {
82                $this->_dtd["codes"][$cp][$tagname] = $tagid;
83            }
84        }
85        $this->_stack = array();
86        $this->multipart = $multipart;
87        $this->bodyparts = array();
88    }
89
90    /**
91     * Puts the WBXML header on the stream
92     *
93     * @access public
94     * @return
95     */
96    public function startWBXML() {
97        if ($this->multipart) {
98            header("Content-Type: application/vnd.ms-sync.multipart");
99            ZLog::Write(LOGLEVEL_DEBUG, "WBXMLEncoder->startWBXML() type: vnd.ms-sync.multipart");
100        }
101        else {
102            header("Content-Type: application/vnd.ms-sync.wbxml");
103            ZLog::Write(LOGLEVEL_DEBUG, "WBXMLEncoder->startWBXML() type: vnd.ms-sync.wbxml");
104        }
105
106        $this->outByte(0x03); // WBXML 1.3
107        $this->outMBUInt(0x01); // Public ID 1
108        $this->outMBUInt(106); // UTF-8
109        $this->outMBUInt(0x00); // string table length (0)
110    }
111
112    /**
113     * Puts a StartTag on the output stack
114     *
115     * @param $tag
116     * @param $attributes
117     * @param $nocontent
118     *
119     * @access public
120     * @return
121     */
122    public function startTag($tag, $attributes = false, $nocontent = false) {
123        $stackelem = array();
124
125        if(!$nocontent) {
126            $stackelem['tag'] = $tag;
127            $stackelem['attributes'] = $attributes;
128            $stackelem['nocontent'] = $nocontent;
129            $stackelem['sent'] = false;
130
131            array_push($this->_stack, $stackelem);
132
133            // If 'nocontent' is specified, then apparently the user wants to force
134            // output of an empty tag, and we therefore output the stack here
135        } else {
136            $this->_outputStack();
137            $this->_startTag($tag, $attributes, $nocontent);
138        }
139    }
140
141    /**
142     * Puts an EndTag on the stack
143     *
144     * @access public
145     * @return
146     */
147    public function endTag() {
148        $stackelem = array_pop($this->_stack);
149
150        // Only output end tags for items that have had a start tag sent
151        if($stackelem['sent']) {
152            $this->_endTag();
153
154            if(count($this->_stack) == 0)
155                ZLog::Write(LOGLEVEL_DEBUG, "WBXMLEncoder->endTag() WBXML output completed");
156
157            if(count($this->_stack) == 0 && $this->multipart == true) {
158                $this->processMultipart();
159            }
160        }
161    }
162
163    /**
164     * Puts content on the output stack
165     *
166     * @param $content
167     *
168     * @access public
169     * @return string
170     */
171    public function content($content) {
172        // We need to filter out any \0 chars because it's the string terminator in WBXML. We currently
173        // cannot send \0 characters within the XML content anywhere.
174        $content = str_replace("\0","",$content);
175
176        if("x" . $content == "x")
177            return;
178        $this->_outputStack();
179        $this->_content($content);
180    }
181
182    /**
183     * Gets the value of multipart
184     *
185     * @access public
186     * @return boolean
187     */
188    public function getMultipart() {
189        return $this->multipart;
190    }
191
192    /**
193     * Adds a bodypart
194     *
195     * @param Stream $bp
196     *
197     * @access public
198     * @return void
199     */
200    public function addBodypartStream($bp) {
201        if ($this->multipart)
202            $this->bodyparts[] = $bp;
203    }
204
205    /**
206     * Gets the number of bodyparts
207     *
208     * @access public
209     * @return int
210     */
211    public function getBodypartsCount() {
212        return count($this->bodyparts);
213    }
214
215    /**----------------------------------------------------------------------------------------------------------
216     * Private WBXMLEncoder stuff
217     */
218
219    /**
220     * Output any tags on the stack that haven't been output yet
221     *
222     * @access private
223     * @return
224     */
225    private function _outputStack() {
226        for($i=0;$i<count($this->_stack);$i++) {
227            if(!$this->_stack[$i]['sent']) {
228                $this->_startTag($this->_stack[$i]['tag'], $this->_stack[$i]['attributes'], $this->_stack[$i]['nocontent']);
229                $this->_stack[$i]['sent'] = true;
230            }
231        }
232    }
233
234    /**
235     * Outputs an actual start tag
236     *
237     * @access private
238     * @return
239     */
240    private function _startTag($tag, $attributes = false, $nocontent = false) {
241        $this->logStartTag($tag, $attributes, $nocontent);
242
243        $mapping = $this->getMapping($tag);
244
245        if(!$mapping)
246            return false;
247
248        if($this->_tagcp != $mapping["cp"]) {
249            $this->outSwitchPage($mapping["cp"]);
250            $this->_tagcp = $mapping["cp"];
251        }
252
253        $code = $mapping["code"];
254        if(isset($attributes) && is_array($attributes) && count($attributes) > 0) {
255            $code |= 0x80;
256        }
257
258        if(!isset($nocontent) || !$nocontent)
259            $code |= 0x40;
260
261        $this->outByte($code);
262
263        if($code & 0x80)
264            $this->outAttributes($attributes);
265    }
266
267    /**
268     * Outputs actual data
269     *
270     * @access private
271     * @return
272     */
273    private function _content($content) {
274        $this->logContent($content);
275        $this->outByte(WBXML_STR_I);
276        $this->outTermStr($content);
277    }
278
279    /**
280     * Outputs an actual end tag
281     *
282     * @access private
283     * @return
284     */
285    private function _endTag() {
286        $this->logEndTag();
287        $this->outByte(WBXML_END);
288    }
289
290    /**
291     * Outputs a byte
292     *
293     * @param $byte
294     *
295     * @access private
296     * @return
297     */
298    private function outByte($byte) {
299        fwrite($this->_out, chr($byte));
300    }
301
302    /**
303     * Outputs a string table
304     *
305     * @param $uint
306     *
307     * @access private
308     * @return
309     */
310    private function outMBUInt($uint) {
311        while(1) {
312            $byte = $uint & 0x7f;
313            $uint = $uint >> 7;
314            if($uint == 0) {
315                $this->outByte($byte);
316                break;
317            } else {
318                $this->outByte($byte | 0x80);
319            }
320        }
321    }
322
323    /**
324     * Outputs content with string terminator
325     *
326     * @param $content
327     *
328     * @access private
329     * @return
330     */
331    private function outTermStr($content) {
332        fwrite($this->_out, $content);
333        fwrite($this->_out, chr(0));
334    }
335
336    /**
337     * Output attributes
338     * We don't actually support this, because to do so, we would have
339     * to build a string table before sending the data (but we can't
340     * because we're streaming), so we'll just send an END, which just
341     * terminates the attribute list with 0 attributes.
342     *
343     * @access private
344     * @return
345     */
346    private function outAttributes() {
347        $this->outByte(WBXML_END);
348    }
349
350    /**
351     * Switches the codepage
352     *
353     * @param $page
354     *
355     * @access private
356     * @return
357     */
358    private function outSwitchPage($page) {
359        $this->outByte(WBXML_SWITCH_PAGE);
360        $this->outByte($page);
361    }
362
363    /**
364     * Get the mapping for a tag
365     *
366     * @param $tag
367     *
368     * @access private
369     * @return array
370     */
371    private function getMapping($tag) {
372        $mapping = array();
373
374        $split = $this->splitTag($tag);
375
376        if(isset($split["ns"])) {
377            $cp = $this->_dtd["namespaces"][$split["ns"]];
378        }
379        else {
380            $cp = 0;
381        }
382
383        $code = $this->_dtd["codes"][$cp][$split["tag"]];
384
385        $mapping["cp"] = $cp;
386        $mapping["code"] = $code;
387
388        return $mapping;
389    }
390
391    /**
392     * Split a tag from a the fulltag (namespace + tag)
393     *
394     * @param $fulltag
395     *
396     * @access private
397     * @return array        keys: 'ns' (namespace), 'tag' (tag)
398     */
399    private function splitTag($fulltag) {
400        $ns = false;
401        $pos = strpos($fulltag, chr(58)); // chr(58) == ':'
402
403        if($pos) {
404            $ns = substr($fulltag, 0, $pos);
405            $tag = substr($fulltag, $pos+1);
406        }
407        else {
408            $tag = $fulltag;
409        }
410
411        $ret = array();
412        if($ns)
413            $ret["ns"] = $ns;
414        $ret["tag"] = $tag;
415
416        return $ret;
417    }
418
419    /**
420     * Logs a StartTag to ZLog
421     *
422     * @param $tag
423     * @param $attr
424     * @param $nocontent
425     *
426     * @access private
427     * @return
428     */
429    private function logStartTag($tag, $attr, $nocontent) {
430        if(!WBXML_DEBUG)
431            return;
432
433        $spaces = str_repeat(" ", count($this->logStack));
434        if($nocontent)
435            ZLog::Write(LOGLEVEL_WBXML,"O " . $spaces . " <$tag/>");
436        else {
437            array_push($this->logStack, $tag);
438            ZLog::Write(LOGLEVEL_WBXML,"O " . $spaces . " <$tag>");
439        }
440    }
441
442    /**
443     * Logs a EndTag to ZLog
444     *
445     * @access private
446     * @return
447     */
448    private function logEndTag() {
449        if(!WBXML_DEBUG)
450            return;
451
452        $spaces = str_repeat(" ", count($this->logStack));
453        $tag = array_pop($this->logStack);
454        ZLog::Write(LOGLEVEL_WBXML,"O " . $spaces . "</$tag>");
455    }
456
457    /**
458     * Logs content to ZLog
459     *
460     * @param $content
461     *
462     * @access private
463     * @return
464     */
465    private function logContent($content) {
466        if(!WBXML_DEBUG)
467            return;
468
469        $spaces = str_repeat(" ", count($this->logStack));
470        ZLog::Write(LOGLEVEL_WBXML,"O " . $spaces . $content);
471    }
472
473    /**
474     * Processes the multipart response
475     *
476     * @access private
477     * @return void
478     */
479    private function processMultipart() {
480        ZLog::Write(LOGLEVEL_DEBUG, sprintf("WBXMLEncoder->processMultipart() with %d parts to be processed", $this->getBodypartsCount()));
481        $len = ob_get_length();
482        $buffer = ob_get_clean();
483        $nrBodyparts = $this->getBodypartsCount();
484        $blockstart = (($nrBodyparts + 1) * 2) * 4 + 4;
485
486        $data = pack("iii", ($nrBodyparts + 1), $blockstart, $len);
487
488        ob_start(null, 1048576);
489
490        foreach ($this->bodyparts as $bp) {
491            $blockstart = $blockstart + $len;
492            $len = fstat($bp);
493            $len = (isset($len['size'])) ? $len['size'] : 0;
494            $data .= pack("ii", $blockstart, $len);
495        }
496
497        fwrite($this->_out, $data);
498        fwrite($this->_out, $buffer);
499        foreach($this->bodyparts as $bp) {
500            while (!feof($bp)) {
501                fwrite($this->_out, fread($bp, 4096));
502            }
503        }
504    }
505}
506
507?>
Note: See TracBrowser for help on using the repository browser.