forked from z-push-ox/z-push-ox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ox.php
520 lines (444 loc) · 17.5 KB
/
ox.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
<?php
include_once ('lib/OXConnector.php');
include_once ('lib/OXUtils.php');
include_once ('lib/TimezoneConverter/fake.php');
include_once ('lib/TimezoneConverter/DateTime.php');
include_once ('lib/TimezoneConverter/Exception.php');
include_once ('lib/TimezoneConverter/TimezoneNotFoundException.php');
include_once ('lib/TimezoneConverter/TimezoneConverter.php');
include_once ('mail/emails.php');
include_once ('contacts/contacts.php');
include_once ('calendar/calendar.php');
include_once ('lib/default/diffbackend/diffbackend.php');
include_once ('include/mimeDecode.php');
require_once ('include/z_RFC822.php');
require_once ('lib/utils/timezoneutil.php');
require_once ('HTTP/Request2.php');
class BackendOX extends BackendDiff {
private $session = false;
private $cookiejar = true;
private $root_folder = array();
private $OXConnector;
private $OXUtils;
private $TZconverter;
public function __construct() {
$this -> OXConnector = new OXConnector();
$this -> OXUtils = new OXUtils();
}
/**
* Indicates which AS version is supported by the backend.
*
* @access public
* @return string AS version constant
*/
public function GetSupportedASVersion() {
return ZPush::ASV_14;
}
/**
* Authenticates the user
*
* @param string $username
* @param string $domain
* @param string $password
*
* @access public
* @return boolean
*/
public function Logon($username, $domain, $password) {
ZLog::Write(LOGLEVEL_DEBUG, "BackendOX::Logon()");
if (!empty($domain)) {
$username = $username . "@" . $domain;
ZLog::Write(LOGLEVEL_DEBUG, "BackendOX::Logon() Found a domain, using '$username' as username.");
}
$response = $this -> OXConnector -> OXreqPOST('/ajax/login?action=login', array('name' => $username, 'password' => $password, ));
if ($response) {
if (array_key_exists("session", $response)) {
$this -> OXConnector -> setSession($response["session"]);
ZLog::Write(LOGLEVEL_DEBUG, "BackendOX::Logon() - Login successfully, get SessionID: " . $this -> session);
$this -> EmailSync = new OXEmailSync($this -> OXConnector, $this -> OXUtils);
$this -> ContactSync = new OXContactSync($this -> OXConnector, $this -> OXUtils);
$this -> CalendarSync = new OXCalendarSync($this -> OXConnector, $this -> OXUtils);
return true;
}
}
return false;
}
/**
* Logs off
*
* @access public
* @return boolean
*/
public function Logoff() {
ZLog::Write(LOGLEVEL_DEBUG, "BackendOX::Logoff()");
$response = $this -> OXConnector -> OXreqGET('/ajax/login', array('action' => 'logout', 'session' => $this -> OXConnector -> getSession(), ));
if ($response) {
ZLog::Write(LOGLEVEL_DEBUG, "BackendOX::Logoff() - Logged off successfully");
return true;
}
}
/**
* Returns a list (array) of folders.
* In simple implementations like this one, probably just one folder is returned.
*
* @access public
* @return array
*/
public function GetFolderList() {
ZLog::Write(LOGLEVEL_DEBUG, 'BackendOX::GetFolderList()');
// Get the list of calendar and contact folders:
$response = $this -> OXConnector -> OXreqGET('/ajax/folders', array('action' => 'root', 'session' => $this -> OXConnector -> getSession(), 'allowed_modules' => 'contacts,calendar',
//http://oxpedia.org/wiki/index.php?title=HTTP_API#CommonFolderData
'columns' => '1,', ));
if ($response) {
$folder_list = array();
foreach ($response["data"] as &$root_folder) {
$root_folder = $root_folder[0];
$this -> root_folder[] = $root_folder;
$folderlist = $this -> GetSubFolders($root_folder);
foreach ($folderlist as &$folderid) {
$folder = $this -> StatFolder($folderid);
# custom folders for non email folders is only supported by eas >= 12
# all folders with parent == 1 are the default folders
if (Request::GetProtocolVersion() >= 12.0 or $folder['parent'] == 1){
$folder_list[] = $folder;
}
}
}
}
// now get all mail folders:
$response = $this -> OXConnector -> OXreqGET('/ajax/folders', array('action' => 'list', 'parent' => 'default0', // personal email folder ?
'session' => $this -> OXConnector -> getSession(), 'allowed_modules' => 'mail',
//http://oxpedia.org/wiki/index.php?title=HTTP_API#CommonFolderData
'columns' => '1,', ));
ZLog::Write(LOGLEVEL_DEBUG, 'BackendOX::GetFolderList() - mailrepsonse: ' . print_r($response, true));
if ($response) {
foreach ($response["data"] as &$root_folder) {
$root_folder = $root_folder[0];
ZLog::Write(LOGLEVEL_DEBUG, "root_folder: " . $root_folder);
$this -> root_folder[] = $root_folder;
$folderlist = $this -> GetSubFolders($root_folder);
$folder_list[] = $this -> StatFolder($root_folder);
foreach ($folderlist as &$folderid) {
if (!is_numeric($folderid)) {
$folder_list[] = $this -> StatFolder($folderid);
ZLog::Write(LOGLEVEL_DEBUG, "folder: " . $folderid);
}
}
}
}
ZLog::Write(LOGLEVEL_DEBUG, 'BackendOX::GetFolderList() - folder_list: ' . print_r($folder_list, true));
return $folder_list;
}
private function GetSubFolders($id) {
ZLog::Write(LOGLEVEL_DEBUG, 'BackendOX::GetSubFolders(' . $id . ')');
$lst = array();
$response = $this -> OXConnector -> OXreqGET('/ajax/folders', array('action' => 'list', 'session' => $this -> OXConnector -> getSession(),
//http://oxpedia.org/wiki/index.php?title=HTTP_API#CommonFolderData
'parent' => $id, 'columns' => '1,301,', ));
ZLOG::Write(LOGLEVEL_DEBUG, 'BackendOX::GetSubFolder(' . $id . ') - response: ' . print_r($response, true));
foreach ($response["data"] as &$folder) {
// restrict to contacts | calendar | mail
if (in_array($folder[1], array("contacts", "calendar", "mail"))) {
$lst[] = $folder[0];
$subfolders = $this -> GetSubFolders($folder[0]);
foreach ($subfolders as &$subfolder) {
$lst[] = $subfolder;
}
}
}
ZLOG::Write(LOGLEVEL_DEBUG, 'BackendOX::GetSubFolder() - lst: ' . print_r($lst, true));
return $lst;
}
/**
* Returns an actual SyncFolder object
*
* @param string $id id of the folder
*
* @access public
* @return object SyncFolder with information
*/
public function GetFolder($id) {
ZLog::Write(LOGLEVEL_DEBUG, 'BackendOX::GetFolder(' . $id . ')');
$response = $this -> OXConnector -> OXreqGET('/ajax/folders', array('action' => 'get', 'session' => $this -> OXConnector -> getSession(), 'id' => $id,
//http://oxpedia.org/wiki/index.php?title=HTTP_API#CommonFolderData
'columns' => '1,20,300,301,316', //objectID�| parentfolderID | title | module/type
));
if ($response) {
ZLog::Write(LOGLEVEL_DEBUG, 'BackendOX::GetFolder(' . $id . ') title: ' . $response["data"]["title"] . ' module: ' . $response["data"]["module"]);
$folder = new SyncFolder();
$folder -> serverid = $id;
if (array_key_exists($response["data"]["folder_id"], $this -> root_folder) || $response["data"]["folder_id"] == "default0") {
$folder -> parentid = "0";
} else {
$folder -> parentid = $response["data"]["folder_id"];
}
$folder -> displayname = $response["data"]["title"];
switch ($response["data"]["module"]) {
case "contacts" :
$folder -> type = SYNC_FOLDER_TYPE_CONTACT;
break;
case "calendar" :
$folder -> type = SYNC_FOLDER_TYPE_APPOINTMENT;
break;
case "mail" :
ZLog::Write(LOGLEVEL_DEBUG, "Mail folder: " . $id . " FolderType : " . $response["data"]["standard_folder_type"]);
switch ( $response["data"]["standard_folder_type"] ) {
case "7" :
$folder -> type = SYNC_FOLDER_TYPE_INBOX;
break;
case "9" :
$folder -> type = SYNC_FOLDER_TYPE_DRAFTS;
break;
case "10" :
$folder -> type = SYNC_FOLDER_TYPE_SENTMAIL;
break;
case "12" :
$folder -> type = SYNC_FOLDER_TYPE_WASTEBASKET;
break;
default :
$folder -> type = SYNC_FOLDER_TYPE_USER_MAIL;
break;
}
break;
default :
return false;
break;
}
return $folder;
}
return false;
}
/**
* Returns folder stats. An associative array with properties is expected.
*
* @param string $id id of the folder
*
* @access public
* @return array
*/
public function StatFolder($id) {
ZLog::Write(LOGLEVEL_DEBUG, 'BackendOX::StatFolder(' . $id . ')');
$folder = $this -> GetFolder($id);
$stat = array();
$stat["id"] = $id;
$stat["parent"] = $folder -> parentid;
$stat["mod"] = $folder -> displayname;
return $stat;
}
/**
* Creates or modifies a folder
*
* @param string $folderid id of the parent folder
* @param string $oldid if empty -> new folder created, else folder is to be renamed
* @param string $displayname new folder name (to be created, or to be renamed to)
* @param int $type folder type
*
* @access public
* @return boolean status
* @throws StatusException could throw specific SYNC_FSSTATUS_* exceptions
*
*/
public function ChangeFolder($folderid, $oldid, $displayname, $type) {
ZLog::Write(LOGLEVEL_DEBUG, 'BackendOX::ChangeFolder(' . $folderid . ',' . $oldid . ',' . $displayname . ',' . $type . ')');
return false;
}
/**
* Deletes a folder
*
* @param string $id
* @param string $parent is normally false
*
* @access public
* @return boolean status - false if e.g. does not exist
* @throws StatusException could throw specific SYNC_FSSTATUS_* exceptions
*
*/
public function DeleteFolder($id, $parentid) {
ZLog::Write(LOGLEVEL_DEBUG, 'BackendOX::ChangeFolder(' . $id . ',' . $parentid . ')');
$folder = $this -> GetFolder($id);
return $this -> callModuleHandlerForFolder("DeleteFolder", $folder, array($folder, $id));
}
/**
* Returns a list (array) of messages
*
* @param string $folderid id of the parent folder
* @param long $cutoffdate timestamp in the past from which on messages should be returned
*
* @access public
* @return array/false array with messages or false if folder is not available
*/
public function GetMessageList($folderid, $cutoffdate) {
ZLog::Write(LOGLEVEL_DEBUG, 'BackendOX::GetMessageList(' . $folderid . ') cutoffdate: ' . $cutoffdate);
$folder = $this -> GetFolder($folderid);
return $this -> callModuleHandlerForFolder("GetMessageList", $folder, array($folder, $cutoffdate));
}
/**
* Returns the actual SyncXXX object type.
*
* @param string $folderid id of the parent folder
* @param string $id id of the message
* @param ContentParameters $contentparameters parameters of the requested message (truncation, mimesupport etc)
*
* @access public
* @return object/false false if the message could not be retrieved
*/
public function GetMessage($folderid, $id, $contentparameters) {
ZLog::Write(LOGLEVEL_DEBUG, 'BackendOX::GetMessage(' . $folderid . ', ' . $id . ', ..)');
$folder = $this -> GetFolder($folderid);
return $this -> callModuleHandlerForFolder("GetMessage", $folder, array($folder, $id, $contentparameters));
}
/**
* Returns message stats, analogous to the folder stats from StatFolder().
*
* @param string $folderid id of the folder
* @param string $id id of the message
*
* @access public
* @return array
*/
public function StatMessage($folderid, $id) {
ZLog::Write(LOGLEVEL_DEBUG, 'BackendOX::StatMessage(' . $folderid . ', ' . $id . ')');
$folder = $this -> GetFolder($folderid);
return $this -> callModuleHandlerForFolder("StatMessage", $folder, array($folder, $id));
}
/**
* Called when a message has been changed on the mobile.
* This functionality is not available for emails.
*
* @param string $folderid id of the folder
* @param string $id id of the message | if id not set create the message
* @param SyncXXX $message the SyncObject containing a message
*
* @access public
* @return array same return value as StatMessage()
* @throws StatusException could throw specific SYNC_STATUS_* exceptions
*/
public function ChangeMessage($folderid, $id, $message, $contentParameters ) {
ZLog::Write(LOGLEVEL_DEBUG, 'BackendOX::ChangeMessage(' . $folderid . ', ' . $id . ', message: ' . json_encode($message) . ')');
$folder = $this -> GetFolder($folderid);
return $this -> callModuleHandlerForFolder("ChangeMessage", $folder, array($folder, $id, $message));
}
/**
* Changes the 'read' flag of a message on disk
*
* @param string $folderid id of the folder
* @param string $id id of the message
* @param int $flags read flag of the message
*
* @access public
* @return boolean status of the operation
* @throws StatusException could throw specific SYNC_STATUS_* exceptions
*/
public function SetReadFlag($folderid, $id, $flags, $contentParameters ) {
ZLog::Write(LOGLEVEL_DEBUG, 'BackendOX::SetReadFlag(' . $folderid . ', ' . $id . ', ..)');
$folder = $this -> GetFolder($folderid);
return $this -> callModuleHandlerForFolder("SetReadFlag", $folder, array($folder, $id, $flags));
}
/**
* Called when the user has requested to delete (really delete) a message
*
* @param string $folderid id of the folder
* @param string $id id of the message
*
* @access public
* @return boolean status of the operation
* @throws StatusException could throw specific SYNC_STATUS_* exceptions
*/
public function DeleteMessage($folderid, $id, $contentParameters ) {
ZLog::Write(LOGLEVEL_DEBUG, 'BackendOX::DeleteMessage(' . $folderid . ', ' . $id . ')');
$folder = $this -> GetFolder($folderid);
return $this -> callModuleHandlerForFolder("DeleteMessage", $folder, array($folder, $id));
}
/**
* Called when the user moves an item on the PDA from one folder to another
* not implemented
*
* @param string $folderid id of the source folder
* @param string $id id of the message
* @param string $newfolderid id of the destination folder
*
* @access public
* @return boolean status of the operation
* @throws StatusException could throw specific SYNC_MOVEITEMSSTATUS_* exceptions
*/
public function MoveMessage($folderid, $id, $newfolderid, $contentParameters ) {
ZLog::Write(LOGLEVEL_DEBUG, 'BackendOX::MoveMessage(' . $folderid . ', ' . $id . '...)');
$folder = $this -> GetFolder($folderid);
return $this -> callModuleHandlerForFolder("MoveMessage", $folder, array($folder, $id, $newfolderid));
}
/**
* Sends an e-mail
* Not implemented here
*
* @param SyncSendMail $sm SyncSendMail object
*
* @access public
* @return boolean
* @throws StatusException
*/
public function SendMail($sm) {
ZLog::Write(LOGLEVEL_DEBUG, 'BackendOX::SendMail()');
return $this->EmailSync->SendMail($sm);
}
/**
* Returns the waste basket
*
* @access public
* @return string
*/
public function GetWasteBasket() {
ZLog::Write(LOGLEVEL_DEBUG, 'BackendOX::GetWasteBasket()');
return false;
}
/**
* Returns the content of the named attachment as stream
* not implemented
*
* @param string $attname
*
* @access public
* @return SyncItemOperationsAttachment
* @throws StatusException
*/
public function GetAttachmentData($attname) {
ZLog::Write(LOGLEVEL_DEBUG, 'BackendOX::GetAttachmentData(' . $attname . ')');
return false;
}
/**
* Calls a function for a specific module for a folder.
*
* @param string $functionName
* @param folder $folder
*
* @return anything
*/
private function callModuleHandlerForFolder($functionName, $folder, $args) {
switch ($folder->type) {
case SYNC_FOLDER_TYPE_CONTACT :
return call_user_func_array(array($this -> ContactSync, "$functionName"), $args);
break;
case SYNC_FOLDER_TYPE_APPOINTMENT :
return call_user_func_array(array($this -> CalendarSync, "$functionName"), $args);
break;
// These are all email: SYNC_FOLDER_TYPE_USER_MAIL, SYNC_FOLDER_TYPE_INBOX, SYNC_FOLDER_TYPE_WASTEBASKET
case SYNC_FOLDER_TYPE_USER_MAIL :
return call_user_func_array(array($this -> EmailSync, "$functionName"), $args);
break;
case SYNC_FOLDER_TYPE_INBOX :
return call_user_func_array(array($this -> EmailSync, "$functionName"), $args);
break;
case SYNC_FOLDER_TYPE_WASTEBASKET :
return call_user_func_array(array($this -> EmailSync, "$functionName"), $args);
break;
default :
break;
}
return false;
}
public static function getBackendVersion()
{
return "0.9";
}
}
?>