/
Importer.php
404 lines (374 loc) · 13.4 KB
/
Importer.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
<?php
/**
* Horde_ActiveSync_Connector_Importer::
*
* 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 2011-2013 Horde LLC (http://www.horde.org)
* @author Michael J Rubinsky <mrubinsk@horde.org>
* @package ActiveSync
*/
/**
* Horde_ActiveSync_Connector_Imports:: Receives Wbxml from device.
*
* @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 2011-2013 Horde LLC (http://www.horde.org)
* @author Michael J Rubinsky <mrubinsk@horde.org>
* @package ActiveSync
*/
class Horde_ActiveSync_Connector_Importer
{
/**
* State machine
*
* @var Horde_ActiveSync_State_Base
*/
protected $_state;
/**
* The server object.
*
* @var Horde_ActiveSync
*/
protected $_as;
/**
* Conflict resolution flags
*
* @var integer
*/
protected $_flags;
/**
* The backend specific folder id
*
* @var string
*/
protected $_folderId;
/**
* The EAS folder uid
*
* @var string
*/
protected $_folderUid;
/**
* Logger
*
* @var Horde_Log_Logger
*/
protected $_logger;
/**
* Process id for logging.
*
* @var integer
*/
protected $_procid;
/**
* Const'r
*
* @param Horde_ActiveSync $as The server object.
*/
public function __construct(Horde_ActiveSync $as)
{
$this->_as = $as;
$this->_procid = getmypid();
}
/**
* Initialize the exporter for this collection
*
* @param Horde_ActiveSync_State_Base $state The state machine.
* @param string $folderId The collection's uid.
* @param integer $flags Conflict resolution flags.
*/
public function init(Horde_ActiveSync_State_Base $state, $folderId = null, $flags = 0)
{
$this->_state = $state;
$this->_flags = $flags;
if (!empty($folderId)) {
$this->_folderId = $this->_as->getCollectionsObject()->getBackendIdForFolderUid($folderId);
$this->_folderUid = $folderId;
}
}
/**
* Setter for a logger instance
*
* @param Horde_Log_Logger $logger The logger
*/
public function setLogger($logger)
{
$this->_logger = $logger;
}
/**
* Import a message change from the wbxml stream
*
* @param string|boolean $id A server message id or
* false if a new message.
* @param Horde_ActiveSync_Message_Base $message A message object
* @param Horde_ActiveSync_Device $device A device descriptor
* @param integer $clientid Client id sent from PIM
* on message addition.
* @param string $class The collection class (only needed for SMS).
* @since 2.6.0
*
* @todo Revisit passing $class for SMS. Probably pass class in the
* const'r.
*
* @return string|array|boolean The server message id, an array containing
* the serverid and failure code, or false
*/
public function importMessageChange(
$id, Horde_ActiveSync_Message_Base $message,
Horde_ActiveSync_Device $device, $clientid, $class = null)
{
// Don't support SMS, but can't tell client that. Send back a phoney
// UID for any imported SMS objects.
if ($class == Horde_ActiveSync::CLASS_SMS) {
return 'IGNORESMS_' . $clientid;
}
// Changing an existing object
if ($id) {
$conflict = $this->_isConflict(
Horde_ActiveSync::CHANGE_TYPE_CHANGE,
$this->_folderId,
$id);
if ($conflict && $this->_flags == Horde_ActiveSync::CONFLICT_OVERWRITE_PIM) {
$this->_logger->notice(sprintf(
'[%s] Conflict when updating %s, will overwrite client version on next sync.',
$this->_procid, $id)
);
return array($id, Horde_ActiveSync_Request_Sync::STATUS_CONFLICT);
}
} else {
if ($uid = $this->_state->isDuplicatePIMAddition($clientid)) {
// Already saw this addition, but PIM never received UID
$this->_logger->notice(sprintf(
'[%s] Duplicate addition for %s',
$this->_procid, $uid)
);
return $uid;
}
}
// Tell the backend about the change
if (!$stat = $this->_as->driver->changeMessage($this->_folderId, $id, $message, $device)) {
$this->_logger->err(sprintf(
'[%s] Change message failed when updating %s',
$this->_procid, $id)
);
return false;
}
$stat['serverid'] = $this->_folderId;
// Record the state of the message
$this->_state->updateState(
($message instanceof Horde_ActiveSync_Message_Mail
? Horde_ActiveSync::CHANGE_TYPE_FLAGS
: Horde_ActiveSync::CHANGE_TYPE_CHANGE),
$stat,
Horde_ActiveSync::CHANGE_ORIGIN_PIM,
$this->_as->driver->getUser(),
$clientid);
return $stat['id'];
}
/**
* Import message deletions. This may conflict if the local object has been
* modified.
*
* @param array $ids Server message uids to delete
* @param string $collection The server collection type.
*/
public function importMessageDeletion(array $ids, $collection)
{
// Don't support SMS, but can't tell client that.
if ($collection == Horde_ActiveSync::CLASS_SMS) {
return;
}
// Tell backend about the deletion
$mod = $this->_as->driver->getSyncStamp($this->_folderId);
$ids = $this->_as->driver->deleteMessage($this->_folderId, $ids);
foreach ($ids as $id) {
// Update client state. Note this only modifies the various map
// tables to prevent mirroring back any changes.
$change = array();
$change['id'] = $id;
$change['mod'] = $mod;
$change['serverid'] = $this->_folderId;
$this->_state->updateState(
Horde_ActiveSync::CHANGE_TYPE_DELETE,
$change,
Horde_ActiveSync::CHANGE_ORIGIN_PIM,
$this->_as->driver->getUser());
}
}
/**
* Import a change in 'read' flags. This can never conflict.
*
* @param integer $id Server message id (The IMAP UID).
* @param string $flag The state of the /seen flag
*/
public function importMessageReadFlag($id, $flag)
{
$change = array();
$change['id'] = $id;
$change['flags'] = array('read' => $flag);
$change['parent'] = $this->_folderId;
$this->_state->updateState(
Horde_ActiveSync::CHANGE_TYPE_FLAGS,
$change,
Horde_ActiveSync::CHANGE_ORIGIN_PIM,
$this->_as->driver->getUser());
$this->_as->driver->setReadFlag($this->_folderId, $id, $flag);
}
/**
* Perform a message move initiated on the PIM
*
* @param array $uids The source message ids.
* @param string $dst The destination folder uid.
*
* @return array An array containing the following keys:
* - results: An array with old uids as keys and new uids as values.
* - missing: An array containing source uids that were not found on the
* IMAP server.
*/
public function importMessageMove(array $uids, $dst)
{
$collections = $this->_as->getCollectionsObject();
$dst = $collections->getBackendIdForFolderUid($dst);
$results = $this->_as->driver->moveMessage($this->_folderId, $uids, $dst);
// Check for any missing (not found) source messages.
if (count($results) != count($uids)) {
$missing = array_diff($uids, array_keys($results));
} else {
$missing = array();
}
// Update client state. For MOVES, we treat it as a delete from the
// SRC folder.
$mod = $this->_as->driver->getSyncStamp($this->_folderId);
foreach ($uids as $uid) {
$change = array();
$change['id'] = $uid;
$change['mod'] = $mod;
$change['serverid'] = $this->_folderId;
$change['class'] = Horde_ActiveSync::CLASS_EMAIL;
$change['folderuid'] = $this->_folderUid;
$this->_state->updateState(
Horde_ActiveSync::CHANGE_TYPE_DELETE,
$change,
Horde_ActiveSync::CHANGE_ORIGIN_PIM,
$this->_as->driver->getUser());
}
return array('results' => $results, 'missing' => $missing);
}
/**
* Import a folder change from the wbxml stream
*
* @param string $uid The folder uid
* @param string $displayname The folder display name
* @param string $parent The parent folder id.
* @param integer $type The EAS Folder type. @since 2.9.0
*
* @return string The new serverid if successful.
*
* @todo Horde 6 - This should take and return a Horde_ActiveSync_Message_Folder object.
*/
public function importFolderChange($uid, $displayname, $parent = Horde_ActiveSync::FOLDER_ROOT, $type = null)
{
// TODO: BC HACK. For now, we need to convert the uid -> folderid.
$collections = $this->_as->getCollectionsObject();
if (!empty($parent)) {
$parent_sid = $collections->getBackendIdForFolderUid($parent);
} else {
$parent_sid = $parent;
}
if (!empty($uid)) {
$folderid = $collections->getBackendIdForFolderUid($uid);
} else {
$folderid = false;
}
try {
$new_uid = $this->_as->driver->changeFolder($folderid, $displayname, $parent_sid, $uid, $type);
} catch (Horde_Exception_PermissionDenied $e) {
$this->_logger->err($e->getMessage());
throw new Horde_ActiveSync_Exception($e->getMessage(), Horde_ActiveSync_Exception::UNSUPPORTED);
} catch (Horde_ActiveSync_Exception $e) {
$this->_logger->err($e->getMessage());
throw $e;
}
$change = array();
$change['id'] = $new_uid;
$change['folderid'] = $folderid;
$change['mod'] = $displayname;
$change['parent'] = $parent;
$this->_state->updateState(
Horde_ActiveSync::CHANGE_TYPE_CHANGE,
$change,
Horde_ActiveSync::CHANGE_ORIGIN_PIM);
return $new_uid;
}
/**
* Imports a folder deletion from the PIM
*
* @param string $uid The folder uid
* @param string $parent The folder id of the parent folder.
*/
public function importFolderDeletion($uid, $parent = Horde_ActiveSync::FOLDER_ROOT)
{
$collections = $this->_as->getCollectionsObject();
if (!empty($parent)) {
$parent_sid = $collections->getBackendIdForFolderUid($parent);
} else {
$parent_sid = $parent;
}
$folderid = $collections->getBackendIdForFolderUid($uid);
$change = array();
$change['id'] = $uid;
$this->_as->driver->deleteFolder($folderid, $parent_sid);
$this->_state->updateState(
Horde_ActiveSync::CHANGE_TYPE_DELETE,
$change,
Horde_ActiveSync::CHANGE_ORIGIN_NA);
}
/**
* Check if this change conflicts with server changes
* This is only true in the following situations:
*
* Changed here and changed there
* Changed here and deleted there
* Deleted here and changed there
*
* Any other combination of operations can be done
* (e.g. change flags & move or move & delete)
*
* @param string $type The type of change('change', 'delete' etc...)
* @param string $folderid The id of the folder this change is from.
* @param string $id The uid for the changed message.
*
* @return boolean
*/
protected function _isConflict($type, $folderid, $id)
{
$stat = $this->_as->driver->statMessage($folderid, $id);
if (!$stat) {
/* Message is gone, if type is change, this is a conflict */
if ($type == Horde_ActiveSync::CHANGE_TYPE_CHANGE) {
return true;
} else {
return false;
}
}
return $this->_state->isConflict($stat, $type);
}
}