-
Notifications
You must be signed in to change notification settings - Fork 17
/
streamer.php
269 lines (243 loc) · 11.9 KB
/
streamer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
<?php
/***********************************************
* File : streamer.php
* Project : Z-Push
* Descr : This file handles streaming of
* WBXML objects. It must be
* subclassed so the internals of
* the object can be specified via
* $mapping. Basically we set/read
* the object variables of the
* subclass according to the mappings
*
*
* Created : 01.10.2007
*
* Copyright 2007 - 2010 Zarafa Deutschland GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation with the following additional
* term according to sec. 7:
*
* According to sec. 7 of the GNU Affero General Public License, version 3,
* the terms of the AGPL are supplemented with the following terms:
*
* "Zarafa" is a registered trademark of Zarafa B.V.
* "Z-Push" is a registered trademark of Zarafa Deutschland GmbH
* The licensing of the Program under the AGPL does not imply a trademark license.
* Therefore any rights, title and interest in our trademarks remain entirely with us.
*
* However, if you propagate an unmodified version of the Program you are
* allowed to use the term "Z-Push" to indicate that you distribute the Program.
* Furthermore you may use our trademarks where it is necessary to indicate
* the intended purpose of a product or service provided you use it in accordance
* with honest practices in industrial or commercial matters.
* If you want to propagate modified versions of the Program under the name "Z-Push",
* you may only do so if you have a written permission by Zarafa Deutschland GmbH
* (to acquire a permission please contact Zarafa at [email protected]).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Consult LICENSE file for details
************************************************/
include_once("zpushdtd.php");
define('STREAMER_VAR', 1);
define('STREAMER_ARRAY', 2);
define('STREAMER_TYPE', 3);
define('STREAMER_TYPE_DATE', 1);
define('STREAMER_TYPE_HEX', 2);
define('STREAMER_TYPE_DATE_DASHES', 3);
define('STREAMER_TYPE_MAPI_STREAM', 4);
class Streamer {
var $_mapping;
var $content;
var $attributes;
var $flags;
function Streamer($mapping) {
$this->_mapping = $mapping;
$this->flags = false;
}
// Decodes the WBXML from $input until we reach the same depth level of WBXML. This
// means that if there are multiple objects at this level, then only the first is decoded
// SubOjects are auto-instantiated and decoded using the same functionality
function decode(&$decoder) {
while(1) {
$entity = $decoder->getElement();
if($entity[EN_TYPE] == EN_TYPE_STARTTAG) {
if(! ($entity[EN_FLAGS] & EN_FLAGS_CONTENT)) {
$map = $this->_mapping[$entity[EN_TAG]];
if (isset($map[STREAMER_ARRAY])) {
$this->$map[STREAMER_VAR] = array();
} else if(!isset($map[STREAMER_TYPE])) {
$this->$map[STREAMER_VAR] = "";
} else if ($map[STREAMER_TYPE] == STREAMER_TYPE_DATE || $map[STREAMER_TYPE] == STREAMER_TYPE_DATE_DASHES ) {
$this->$map[STREAMER_VAR] = "";
}
continue;
}
// Found a start tag
if(!isset($this->_mapping[$entity[EN_TAG]])) {
// This tag shouldn't be here, abort
debug("Tag " . $entity[EN_TAG] . " unexpected in type XML type " . get_class($this));
return false;
} else {
$map = $this->_mapping[$entity[EN_TAG]];
// Handle an array
if(isset($map[STREAMER_ARRAY])) {
while(1) {
if(!$decoder->getElementStartTag($map[STREAMER_ARRAY]))
break;
if(isset($map[STREAMER_TYPE])) {
$decoded = new $map[STREAMER_TYPE];
$decoded->decode($decoder);
} else {
$decoded = $decoder->getElementContent();
}
if(!isset($this->$map[STREAMER_VAR]))
$this->$map[STREAMER_VAR] = array($decoded);
else
array_push($this->$map[STREAMER_VAR], $decoded);
if(!$decoder->getElementEndTag())
return false;
}
if(!$decoder->getElementEndTag())
return false;
} else { // Handle single value
if(isset($map[STREAMER_TYPE])) {
// Complex type, decode recursively
if($map[STREAMER_TYPE] == STREAMER_TYPE_DATE || $map[STREAMER_TYPE] == STREAMER_TYPE_DATE_DASHES) {
$decoded = $this->parseDate($decoder->getElementContent());
if(!$decoder->getElementEndTag())
return false;
} else if($map[STREAMER_TYPE] == STREAMER_TYPE_HEX) {
$decoded = hex2bin($decoder->getElementContent());
if(!$decoder->getElementEndTag())
return false;
} else {
$subdecoder = new $map[STREAMER_TYPE]();
if($subdecoder->decode($decoder) === false)
return false;
$decoded = $subdecoder;
if(!$decoder->getElementEndTag()) {
debug("No end tag for " . $entity[EN_TAG]);
return false;
}
}
} else {
// Simple type, just get content
$decoded = $decoder->getElementContent();
if($decoded === false) {
// the tag is declared to have content, but no content is available.
// set an empty content
$decoded = "";
}
if(!$decoder->getElementEndTag()) {
debug("Unable to get end tag for " . $entity[EN_TAG]);
return false;
}
}
// $decoded now contains data object (or string)
$this->$map[STREAMER_VAR] = $decoded;
}
}
}
else if($entity[EN_TYPE] == EN_TYPE_ENDTAG) {
$decoder->ungetElement($entity);
break;
}
else {
debug("Unexpected content in type");
break;
}
}
}
// Encodes this object and any subobjects - output is ordered according to mapping
function encode(&$encoder) {
$attributes = isset($this->attributes) ? $this->attributes : array();
foreach($this->_mapping as $tag => $map) {
if(isset($this->$map[STREAMER_VAR])) {
// Variable is available
if(is_object($this->$map[STREAMER_VAR])) {
// Subobjects can do their own encoding
$encoder->startTag($tag);
$this->$map[STREAMER_VAR]->encode($encoder);
$encoder->endTag();
} else if(isset($map[STREAMER_ARRAY])) {
// Array of objects
$encoder->startTag($tag); // Outputs array container (eg Attachments)
foreach ($this->$map[STREAMER_VAR] as $element) {
if(is_object($element)) {
$encoder->startTag($map[STREAMER_ARRAY]); // Outputs object container (eg Attachment)
$element->encode($encoder);
$encoder->endTag();
} else {
if(strlen($element) == 0)
// Do not output empty items. Not sure if we should output an empty tag with $encoder->startTag($map[STREAMER_ARRAY], false, true);
;
else {
$encoder->startTag($map[STREAMER_ARRAY]);
$encoder->content($element);
$encoder->endTag();
}
}
}
$encoder->endTag();
} else {
// Simple type
if(strlen($this->$map[STREAMER_VAR]) == 0) {
// Do not output empty items. See above: $encoder->startTag($tag, false, true);
continue;
} else
$encoder->startTag($tag);
if(isset($map[STREAMER_TYPE]) && ($map[STREAMER_TYPE] == STREAMER_TYPE_DATE || $map[STREAMER_TYPE] == STREAMER_TYPE_DATE_DASHES)) {
if($this->$map[STREAMER_VAR] != 0) // don't output 1-1-1970
$encoder->content($this->formatDate($this->$map[STREAMER_VAR], $map[STREAMER_TYPE]));
} else if(isset($map[STREAMER_TYPE]) && $map[STREAMER_TYPE] == STREAMER_TYPE_HEX) {
$encoder->content(strtoupper(bin2hex($this->$map[STREAMER_VAR])));
} else if(isset($map[STREAMER_TYPE]) && $map[STREAMER_TYPE] == STREAMER_TYPE_MAPI_STREAM) {
$encoder->content($this->$map[STREAMER_VAR]);
} else {
$encoder->content($this->$map[STREAMER_VAR]);
}
$encoder->endTag();
}
}
}
// Output our own content
if(isset($this->content))
$encoder->content($this->content);
}
// Oh yeah. This is beautiful. Exchange outputs date fields differently in calendar items
// and emails. We could just always send one or the other, but unfortunately nokia's 'Mail for
// exchange' depends on this quirk. So we have to send a different date type depending on where
// it's used. Sigh.
function formatDate($ts, $type) {
if($type == STREAMER_TYPE_DATE)
return gmstrftime("%Y%m%dT%H%M%SZ", $ts);
else if($type == STREAMER_TYPE_DATE_DASHES)
return gmstrftime("%Y-%m-%dT%H:%M:%S.000Z", $ts);
}
function parseDate($ts) {
if(preg_match("/(\d{4})[^0-9]*(\d{2})[^0-9]*(\d{2})(T(\d{2})[^0-9]*(\d{2})[^0-9]*(\d{2})(.\d+)?Z){0,1}$/", $ts, $matches)) {
if ($matches[1] >= 2038){
$matches[1] = 2038;
$matches[2] = 1;
$matches[3] = 18;
$matches[5] = $matches[6] = $matches[7] = 0;
}
if (!isset($matches[5])) $matches[5] = 0;
if (!isset($matches[6])) $matches[6] = 0;
if (!isset($matches[7])) $matches[7] = 0;
return gmmktime($matches[5], $matches[6], $matches[7], $matches[2], $matches[3], $matches[1]);
}
return 0;
}
};
?>