/
Base.php
608 lines (568 loc) · 23.2 KB
/
Base.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
<?php
/**
* Horde_ActiveSync_Message_Base::
*
* Portions of this class were ported from the Z-Push project:
* File : wbxml.php
* Project : Z-Push
* Descr : WBXML mapping file
*
* Created : 01.10.2007
*
* © Zarafa Deutschland GmbH, www.zarafaserver.de
* This file is distributed under GPL-2.0.
* Consult COPYING file for details
*
* @license http://www.horde.org/licenses/gpl GPLv2
* NOTE: According to sec. 8 of the GENERAL PUBLIC LICENSE (GPL),
* Version 2, the distribution of the Horde_ActiveSync module in or
* to the United States of America is excluded from the scope of this
* license.
* @copyright 2009-2015 Horde LLC (http://www.horde.org)
* @author Michael J Rubinsky <mrubinsk@horde.org>
* @package ActiveSync
*/
/**
* Horde_ActiveSync_Message_Base:: Base class for all ActiveSync message
* objects.
*
* @license http://www.horde.org/licenses/gpl GPLv2
* NOTE: According to sec. 8 of the GENERAL PUBLIC LICENSE (GPL),
* Version 2, the distribution of the Horde_ActiveSync module in or
* to the United States of America is excluded from the scope of this
* license.
* @copyright 2009-2015 Horde LLC (http://www.horde.org)
* @author Michael J Rubinsky <mrubinsk@horde.org>
* @package ActiveSync
*/
class Horde_ActiveSync_Message_Base
{
/* Attribute Keys */
const KEY_ATTRIBUTE = 1;
const KEY_VALUES = 2;
const KEY_TYPE = 3;
const KEY_PROPERTY = 4;
/* Types */
const TYPE_DATE = 1;
const TYPE_HEX = 2;
const TYPE_DATE_DASHES = 3;
const TYPE_MAPI_STREAM = 4;
const TYPE_MAPI_GOID = 5;
const PROPERTY_NO_CONTAINER = 7;
/**
* Holds the mapping for object properties
*
* @var array
*/
protected $_mapping;
/**
* Holds property values
*
* @var array
*/
protected $_properties = array();
/**
* Message flags
*
* @var Horde_ActiveSync::FLAG_* constant
*/
public $flags = false;
/**
* Logger
*
* @var Horde_Log_Logger
*/
protected $_logger;
/**
* An array describing the non-ghosted elements this message supports.
*
* @var array
*/
protected $_supported = array();
/**
* Existence cache, used for working with ghosted properties.
*
* @var array
*/
protected $_exists = array();
/**
* The version of EAS we are to support.
*
* @var float
*/
protected $_version = Horde_ActiveSync::VERSION_TWOFIVE;
/**
* The device object
*
* @var Horde_ActiveSync_Device
* @since 2.9.2
*/
protected $_device;
protected $_streamFilters = array();
/**
* Const'r
*
* @param array $options Configuration options for the message:
* - logger: (Horde_Log_Logger) A logger instance
* DEFAULT: none (No logging).
* - protocolversion: (float) The version of EAS to support.
* DEFAULT: Horde_ActiveSync::VERSION_TWOFIVE (2.5)
* - device: (Horde_ActiveSync_Device) The device object. @since 2.9.2
*
* @return Horde_ActiveSync_Message_Base
*/
public function __construct(array $options = array())
{
if (!empty($options['logger'])) {
$this->_logger = $options['logger'];
} else {
$this->_logger = new Horde_Log_Logger(new Horde_Log_Handler_Null());
}
if (!empty($options['protocolversion'])) {
$this->_version = $options['protocolversion'];
}
if (!empty($options['device'])) {
$this->_device = $options['device'];
}
}
public function __destruct()
{
foreach ($this->_streamFilters as $filter) {
stream_filter_remove($filter);
}
}
/**
* Return the EAS version this object supports.
*
* @return float A Horde_ActiveSync::VERSION_* constant.
*/
public function getProtocolVersion()
{
return $this->_version;
}
/**
* Check the existence of a property in this message.
*
* @param string $property The property name
*
* @return boolean
*/
public function propertyExists($property)
{
return array_key_exists($property, $this->_properties);
}
/**
* Accessor
*
* @param string $property Property to get.
*
* @return mixed The value of the requested property.
* @todo: Return boolean false if not set. Not BC to change it.
*/
public function &__get($property)
{
if ($this->_properties[$property] !== false) {
return $this->_properties[$property];
} else {
$string = '';
return $string;
}
}
/**
* Setter
*
* @param string $property The property to set.
* @param mixed $value The value to set it to.
*
* @throws InvalidArgumentException
*/
public function __set($property, $value)
{
if (!array_key_exists($property, $this->_properties)) {
$this->_logger->err('Unknown property: ' . $property);
throw new InvalidArgumentException(get_class($this) . ' Unknown property: ' . $property);
}
$this->_properties[$property] = $value;
$this->_exists[$property] = true;
}
/**
* Magic caller method.
*
* @param mixed $method The method to call.
* @param array $arg Method arguments.
*
* @return mixed
*/
public function __call($method, $arg)
{
/* Support calling set{Property}() */
if (strpos($method, 'set') === 0) {
$property = Horde_String::lower(substr($method, 3));
$this->_properties[$property] = $arg;
} elseif (strpos($method, 'get') === 0) {
return $this->_getAttribute(Horde_String::lower(substr($method, 3)));
}
throw new BadMethodCallException('Unknown method: ' . $method . ' in class: ' . __CLASS__);
}
/**
* Magic method.
*
* @param string $property The property name to check.
*
* @return boolean.
*/
public function __isset($property)
{
return isset($this->_properties[$property]);
}
/**
* Set the list of non-ghosted fields for this message.
*
* @param array $fields The array of fields.
*/
public function setSupported(array $fields)
{
$this->_supported = array();
foreach ($fields as $field) {
$this->_supported[] = $this->_mapping[$field][self::KEY_ATTRIBUTE];
}
}
/**
* Get the list of non-ghosted properties for this message.
*
* @return array The array of non-ghosted properties
*/
public function getSupported()
{
return $this->_supported;
}
/**
* Determines if the property specified has been ghosted by the client.
* A ghosted property 1) IS listed in the supported list and 2) NOT
* present in the current message. If it's IN the supported list and NOT
* in the current message, then it IS ghosted and the server should keep
* the field's current value when performing any change action due to this
* message.
*
* @param string $property The property to check
*
* @return boolean
*/
public function isGhosted($property)
{
if (array_search($property, $this->_supported) === false) {
return false;
} elseif (empty($this->_exists[$property])) {
return true;
}
return false;
}
/**
* Recursively decodes the WBXML from input stream. This means that if this
* message contains complex types (like Appointment.Recuurence for example)
* the sub-objects are auto-instantiated and decoded as well. Places the
* decoded objects in the local properties array.
*
* @param Horde_ActiveSync_Wbxml_Decoder The stream decoder
*
* @throws Horde_ActiveSync_Exception
*/
public function decodeStream(Horde_ActiveSync_Wbxml_Decoder &$decoder)
{
while (1) {
$entity = $decoder->getElement();
if ($entity[Horde_ActiveSync_Wbxml::EN_TYPE] == Horde_ActiveSync_Wbxml::EN_TYPE_STARTTAG) {
if (!($entity[Horde_ActiveSync_Wbxml::EN_FLAGS] & Horde_ActiveSync_Wbxml::EN_FLAGS_CONTENT)) {
$map = $this->_mapping[$entity[Horde_ActiveSync_Wbxml::EN_TAG]];
if (!isset($map[self::KEY_TYPE])) {
$this->{$map[self::KEY_ATTRIBUTE]} = '';
} elseif ($map[self::KEY_TYPE] == self::TYPE_DATE || $map[self::KEY_TYPE] == self::TYPE_DATE_DASHES ) {
$this->{$map[self::KEY_ATTRIBUTE]} = '';
}
continue;
}
// Found start tag
if (!isset($this->_mapping[$entity[Horde_ActiveSync_Wbxml::EN_TAG]])) {
$this->_logger->err('Tag ' . $entity[Horde_ActiveSync_Wbxml::EN_TAG] . ' unexpected in type XML type ' . get_class($this));
throw new Horde_ActiveSync_Exception('Unexpected tag');
} else {
$map = $this->_mapping[$entity[Horde_ActiveSync_Wbxml::EN_TAG]];
if (isset($map[self::KEY_VALUES])) {
// Handle arrays of attribute values
while (1) {
//do not get start tag for an array without a container
if (!(isset($map[self::KEY_PROPERTY]) && $map[self::KEY_PROPERTY] == self::PROPERTY_NO_CONTAINER)) {
if (!$decoder->getElementStartTag($map[self::KEY_VALUES])) {
break;
}
}
if (isset($map[self::KEY_TYPE])) {
$class = $map[self::KEY_TYPE];
$decoded = new $class(array(
'protocolversion' => $this->_version,
'logger' => $this->_logger)
);
$decoded->decodeStream($decoder);
} else {
$decoded = $decoder->getElementContent();
}
if (!isset($this->{$map[self::KEY_ATTRIBUTE]})) {
$this->{$map[self::KEY_ATTRIBUTE]} = array($decoded);
} else {
$this->{$map[self::KEY_ATTRIBUTE]}[] = $decoded;
}
if (!$decoder->getElementEndTag()) {
throw new Horde_ActiveSync_Exception('Missing expected wbxml end tag');
}
if (isset($map[self::KEY_PROPERTY]) && $map[self::KEY_PROPERTY] == self::PROPERTY_NO_CONTAINER) {
$e = $decoder->peek();
//go back to the initial while if another block of no container elements is found
if ($e[Horde_ActiveSync_Wbxml::EN_TYPE] == Horde_ActiveSync_Wbxml::EN_TYPE_STARTTAG) {
continue 2;
}
//break on end tag because no container elements block end is reached
if ($e[Horde_ActiveSync_Wbxml::EN_TYPE] == Horde_ActiveSync_Wbxml::EN_TYPE_ENDTAG)
break;
if (empty($e))
break;
}
}
//do not get container end tag for an array without a container
if (!(isset($map[self::KEY_PROPERTY]) && $map[self::KEY_PROPERTY] == self::PROPERTY_NO_CONTAINER)) {
if (!$decoder->getElementEndTag()) {
return false;
}
}
} else {
// Handle a simple attribute value
if (isset($map[self::KEY_TYPE])) {
// Complex type, decode recursively
if ($map[self::KEY_TYPE] == self::TYPE_DATE || $map[self::KEY_TYPE] == self::TYPE_DATE_DASHES) {
$decoded = $this->_parseDate($decoder->getElementContent());
} elseif ($map[self::KEY_TYPE] == self::TYPE_HEX) {
$decoded = self::hex2bin($decoder->getElementContent());
} else {
$class = $map[self::KEY_TYPE];
$subdecoder = new $class(array(
'protocolversion' => $this->_version,
'logger' => $this->_logger)
);
$subdecoder->decodeStream($decoder);
$decoded = $subdecoder;
}
} else {
// Simple type, just get content
$decoded = $decoder->getElementContent();
if ($decoded === false) {
$decoded = '';
$this->_logger->notice('Unable to get expected content for ' . $entity[Horde_ActiveSync_Wbxml::EN_TAG] . ': Setting to an empty string.');
}
}
if (!$decoder->getElementEndTag()) {
$this->_logger->err('Unable to get end tag for ' . $entity[Horde_ActiveSync_Wbxml::EN_TAG]);
throw new Horde_ActiveSync_Exception('Missing expected wbxml end tag');
}
$this->{$map[self::KEY_ATTRIBUTE]} = $decoded;
}
}
} elseif ($entity[Horde_ActiveSync_Wbxml::EN_TYPE] == Horde_ActiveSync_Wbxml::EN_TYPE_ENDTAG) {
$decoder->_ungetElement($entity);
break;
} else {
$this->_logger->err('Unexpected content in type');
break;
}
}
}
/**
* Encodes this object (and any sub-objects) as wbxml to the output stream.
* Output is ordered according to $_mapping
*
* @param Horde_ActiveSync_Wbxml_Encoder $encoder The wbxml stream encoder
*/
public function encodeStream(Horde_ActiveSync_Wbxml_Encoder &$encoder)
{
foreach ($this->_mapping as $tag => $map) {
if (isset($this->{$map[self::KEY_ATTRIBUTE]})) {
// Variable is available
if (is_object($this->{$map[self::KEY_ATTRIBUTE]}) &&
!($this->{$map[self::KEY_ATTRIBUTE]} instanceof Horde_Date)) {
// Subobjects can do their own encoding
$encoder->startTag($tag);
$this->{$map[self::KEY_ATTRIBUTE]}->encodeStream($encoder);
$encoder->endTag();
} elseif (isset($map[self::KEY_VALUES]) &&
is_array($this->{$map[self::KEY_ATTRIBUTE]})) {
// Array of objects. Note that some array values must be
// send as an empty tag if they contain no elements.
if (count($this->{$map[self::KEY_ATTRIBUTE]})) {
if (!isset($map[self::KEY_PROPERTY]) ||
$map[self::KEY_PROPERTY] != self::PROPERTY_NO_CONTAINER) {
$encoder->startTag($tag);
}
foreach ($this->{$map[self::KEY_ATTRIBUTE]} as $element) {
if (is_object($element)) {
// Outputs object container (eg Attachment)
$encoder->startTag($map[self::KEY_VALUES]);
$element->encodeStream($encoder);
$encoder->endTag();
} else {
// Do not ever output empty items here
if(strlen($element) > 0) {
$encoder->startTag($map[self::KEY_VALUES]);
$encoder->content($element);
$encoder->endTag();
}
}
}
if (!isset($map[self::KEY_PROPERTY]) || $map[self::KEY_PROPERTY] != self::PROPERTY_NO_CONTAINER) {
$encoder->endTag();
}
} elseif ($this->_checkSendEmpty($tag)) {
$encoder->startTag($tag, null, true);
}
} else {
// Simple type
if (!is_resource($this->{$map[self::KEY_ATTRIBUTE]}) &&
strlen($this->{$map[self::KEY_ATTRIBUTE]}) == 0) {
// Do not output empty items except for the following:
if ($this->_checkSendEmpty($tag)) {
$encoder->startTag($tag, $this->{$map[self::KEY_ATTRIBUTE]}, true);
}
continue;
} elseif ($encoder->multipart &&
in_array($tag, array(
Horde_ActiveSync::SYNC_DATA,
Horde_ActiveSync::AIRSYNCBASE_DATA,
Horde_ActiveSync_Request_ItemOperations::ITEMOPERATIONS_DATA)
)) {
$this->_logger->info('HANDLING MULTIPART OUTPUT');
$encoder->addPart($this->{$map[self::KEY_ATTRIBUTE]});
$encoder->startTag(Horde_ActiveSync_Request_ItemOperations::ITEMOPERATIONS_PART);
$encoder->content((string)(count($encoder->getParts()) - 1));
$encoder->endTag();
continue;
}
$encoder->startTag($tag);
if (isset($map[self::KEY_TYPE]) && ($map[self::KEY_TYPE] == self::TYPE_DATE || $map[self::KEY_TYPE] == self::TYPE_DATE_DASHES)) {
if (!empty($this->{$map[self::KEY_ATTRIBUTE]})) { // don't output 1-1-1970
$encoder->content($this->_formatDate($this->{$map[self::KEY_ATTRIBUTE]}, $map[self::KEY_TYPE]));
}
} elseif (isset($map[self::KEY_TYPE]) && $map[self::KEY_TYPE] == self::TYPE_HEX) {
$encoder->content(Horde_String::upper(bin2hex($this->{$map[self::KEY_ATTRIBUTE]})));
} elseif (isset($map[self::KEY_TYPE]) && $map[self::KEY_TYPE] == self::TYPE_MAPI_STREAM) {
$encoder->content($this->{$map[self::KEY_ATTRIBUTE]});
} else {
$encoder->content(
$this->_checkEncoding($this->{$map[self::KEY_ATTRIBUTE]}, $tag));
}
$encoder->endTag();
}
}
}
}
/**
* Checks if the data needs to be encoded like e.g., when outputing binary
* data in-line during ITEMOPERATIONS requests. Concrete classes should
* override this if needed.
*
* @param mixed $data The data to check. A string or stream resource.
* @param string $tag The tag we are outputing.
*
* @return mixed The encoded data. A string or stream resource with
* a filter attached.
*/
protected function _checkEncoding($data, $tag)
{
if (is_resource($data)) {
stream_filter_register('horde_null', 'Horde_Stream_Filter_Null');
stream_filter_register('horde_eol', 'Horde_Stream_Filter_Eol');
$this->_streamFilters[] = stream_filter_prepend($data, 'horde_null', STREAM_FILTER_READ);
$this->_streamFilters[] = stream_filter_prepend($data, 'horde_eol', STREAM_FILTER_READ);
}
return $data;
}
/**
* Checks to see if we should send an empty value.
*
* @param string $tag The tag name
*
* @return boolean
*/
protected function _checkSendEmpty($tag)
{
return false;
}
/**
* Helper method to allow default values for unset properties.
*
* @param string $name The property name
* @param stting $default The default value to return if $property is empty
*
* @return mixed
*/
protected function _getAttribute($name, $default = null)
{
if ((!is_array($this->_properties[$name]) && $this->_properties[$name] !== false) ||
is_array($this->_properties[$name])) {
return $this->_properties[$name];
} else {
return $default;
}
}
/**
* 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.
* Used when encoding a date value to send to the client.
*
* @param Horde_Date $dt The Horde_Date object to format
* (should normally be in local tz).
* @param integer $type The type to format as (TYPE_DATE or TYPE_DATE_DASHES)
*
* @return string The formatted date
*/
protected function _formatDate(Horde_Date $dt, $type)
{
if ($type == Horde_ActiveSync_Message_Base::TYPE_DATE) {
return $dt->setTimezone('UTC')->format('Ymd\THis\Z');
} elseif ($type == Horde_ActiveSync_Message_Base::TYPE_DATE_DASHES) {
return $dt->setTimezone('UTC')->format('Y-m-d\TH:i:s\.000\Z');
}
}
/**
* Get a Horde_Date from a timestamp, ensuring it's in the correct format.
* Used when decoding an incoming date value from the client.
*
* @param string $ts The timestamp
*
* @return Horde_Date|boolean The Horde_Date or false if unable to decode.
*/
protected 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)) {
try {
return new Horde_Date($ts);
} catch (Horde_Date_Exception $e) {}
}
return false;
}
/**
* Function which converts a hex entryid to a binary entryid.
*
* @param string $data The hexadecimal string
*
* @return string The binary data
*/
private static function hex2bin($data)
{
$len = strlen($data);
$newdata = '';
for($i = 0; $i < $len; $i += 2)
{
$newdata .= pack('C', hexdec(substr($data, $i, 2)));
}
return $newdata;
}
}