forked from linagora/esn-sabre
-
Notifications
You must be signed in to change notification settings - Fork 0
/
esn.php
259 lines (203 loc) · 9.13 KB
/
esn.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
<?php
require_once 'vendor/autoload.php';
use \PhpAmqpLib\Connection\AMQPStreamConnection;
define('CONFIG_PATH', 'config.json');
$config = json_decode(file_get_contents(CONFIG_PATH), true);
if (!$config) {
throw new Exception("Could not load config.json from " . realpath(CONFIG_PATH) . ", Error " . json_last_error());
}
$dbConfig = $config['database'];
define('SABRE_ENV_PRODUCTION', 'production');
define('SABRE_ENV_DEV', 'dev');
define('SABRE_ENV', (!empty($config['environment']) && !empty($config['environment']['SABRE_ENV'])) ? $config['environment']['SABRE_ENV'] : SABRE_ENV_PRODUCTION);
// settings
date_default_timezone_set('UTC');
define('PRINCIPALS_COLLECTION', 'principals');
define('PRINCIPALS_USERS', 'principals/users');
define('PRINCIPALS_TECHNICAL_USER', 'principals/technicalUser');
define('PRINCIPALS_COMMUNITIES', 'principals/communities');
define('PRINCIPALS_PROJECTS', 'principals/projects');
define('PRINCIPALS_RESOURCES', 'principals/resources');
define('PRINCIPALS_DOMAINS', 'principals/domains');
define('JSON_ROOT', 'json');
//Mapping PHP errors to exceptions
function exception_error_handler($errno, $errstr, $errfile, $errline ) {
throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
}
set_error_handler("exception_error_handler");
try {
$mongoEsn = new \MongoDB\Client($dbConfig['esn']['connectionString'], $dbConfig['esn']['connectionOptions']);
if ($dbConfig['esn']['connectionString'] == $dbConfig['sabre']['connectionString']) {
$mongoSabre = $mongoEsn;
} else {
$mongoSabre = new \MongoDB\Client($dbConfig['sabre']['connectionString'], $dbConfig['sabre']['connectionOptions']);
}
} catch (MongoConnectionException $e) {
// Create a fake server that will abort with the exception right away. This
// allows us to use SabreDAV's exception handler and output.
$server = new Sabre\DAV\Server([]);
$server->on('beforeMethod', function() use ($e) {
throw new Sabre\DAV\Exception\ServiceUnavailable($e->getMessage());
}, 1);
$server->exec();
return;
}
// Databases
$esnDb = $mongoEsn->{$dbConfig['esn']['db']};
$sabreDb = $mongoSabre->{$dbConfig['sabre']['db']};
// Backends
$authBackend = new ESN\DAV\Auth\Backend\Esn($config['esn']['apiRoot'], $config['webserver']['realm']);
$calendarBackend = new ESN\CalDAV\Backend\Esn($sabreDb);
$addressbookBackend = new ESN\CardDAV\Backend\Esn($sabreDb);
$principalBackend = new ESN\DAVACL\PrincipalBackend\EsnRequest($esnDb, $authBackend, $config['esn']['apiRoot']);
// listener
$authEmitter = $authBackend->getEventEmitter();
$authEmitter->on("auth:success", [$addressbookBackend, "getAddressBooksForUser"]);
$authEmitter->on("auth:success", [$calendarBackend, "getCalendarsForUser"]);
// Directory structure
$tree = [
new Sabre\DAV\SimpleCollection(PRINCIPALS_COLLECTION, [
new ESN\CalDAV\Principal\Collection($principalBackend, PRINCIPALS_USERS),
new Sabre\CalDAV\Principal\Collection($principalBackend, PRINCIPALS_COMMUNITIES),
new Sabre\CalDAV\Principal\Collection($principalBackend, PRINCIPALS_PROJECTS),
new Sabre\CalDAV\Principal\Collection($principalBackend, PRINCIPALS_RESOURCES),
new Sabre\CalDAV\Principal\Collection($principalBackend, PRINCIPALS_TECHNICAL_USER),
new Sabre\CalDAV\Principal\Collection($principalBackend, PRINCIPALS_DOMAINS)
]),
new ESN\CalDAV\CalendarRoot($principalBackend, $calendarBackend, $esnDb),
new ESN\CardDAV\AddressBookRoot($principalBackend, $addressbookBackend),
];
$server = new Sabre\DAV\Server($tree);
// Add stack trace to HTML response in dev mode
if (SABRE_ENV === SABRE_ENV_DEV) {
$server->debugExceptions = true;
}
if (isset($config['logger'])) {
$logger = ESN\Log\EsnLoggerFactory::initLogger($config['logger']);
if (!empty($logger)) {
$server->setLogger($logger);
}
}
$server->setBaseUri($config['webserver']['baseUri']);
// Server Plugins
$authPlugin = new Sabre\DAV\Auth\Plugin($authBackend);
$server->addPlugin($authPlugin);
$aclPlugin = new Sabre\DAVACL\Plugin();
$aclPlugin->principalCollectionSet = [
PRINCIPALS_USERS,
PRINCIPALS_COMMUNITIES,
PRINCIPALS_PROJECTS,
PRINCIPALS_RESOURCES,
PRINCIPALS_DOMAINS
];
$aclPlugin->adminPrincipals[] = PRINCIPALS_TECHNICAL_USER;
$server->addPlugin($aclPlugin);
// JSON api support
$jsonPlugin = new ESN\JSON\Plugin(JSON_ROOT);
$server->addPlugin($jsonPlugin);
// TEXT api support (iphone)
$textPlugin = new ESN\CalDAV\TextPlugin("text");
$server->addPlugin($textPlugin);
// FREEBUSY api support
$freeBusyPlugin = new ESN\JSON\FreeBusyPlugin();
$server->addPlugin($freeBusyPlugin);
// CalDAV support
$caldavPlugin = new ESN\CalDAV\Plugin();
$server->addPlugin($caldavPlugin);
// CardDAV support
$carddavPlugin = new Sabre\CardDAV\Plugin();
$server->addPlugin($carddavPlugin);
$carddavJsonPlugin = new ESN\CardDAV\Plugin();
$server->addPlugin($carddavJsonPlugin);
// vCard export plugin
$vcfPlugin = new Sabre\CardDAV\VCFExportPlugin();
$server->addPlugin($vcfPlugin);
// CardDAV subscription support
$carddavSubscriptionPlugin = new ESN\CardDAV\Subscriptions\Plugin();
$server->addPlugin($carddavSubscriptionPlugin);
// CardDAV sharing support
$carddavSharingPlugin = new ESN\CardDAV\Sharing\Plugin();
$server->addPlugin($carddavSharingPlugin);
$carddavSharingListenerPlugin = new ESN\CardDAV\Sharing\ListenerPlugin($addressbookBackend);
$server->addPlugin($carddavSharingListenerPlugin);
// Calendar subscription support
$server->addPlugin(
new Sabre\CalDAV\Subscriptions\Plugin()
);
// Calendar sharing support
$server->addPlugin(new ESN\DAV\Sharing\Plugin());
$server->addPlugin(new Sabre\CalDAV\SharingPlugin());
// Calendar scheduling support
$server->addPlugin(
new ESN\CalDAV\Schedule\Plugin()
);
$server->addPlugin(
new ESN\CalDAV\Schedule\IMipPlugin($config['esn']['calendarRoot'], $authBackend, $sabreDb)
);
// WebDAV-Sync plugin
$server->addPlugin(new Sabre\DAV\Sync\Plugin());
// Support for html frontend (only available in dev mode)
if (SABRE_ENV === SABRE_ENV_DEV) {
$browser = new Sabre\DAV\Browser\Plugin();
$server->addPlugin($browser);
}
// Calendar Ics Export support
$icsExportPlugin = new Sabre\CalDAV\ICSExportPlugin();
$server->addPlugin($icsExportPlugin);
// Support CORS
$corsPlugin = new ESN\DAV\CorsPlugin();
if (isset($config['webserver']['corsAllowMethods'])) {
$corsPlugin->allowMethods = $config['webserver']['corsAllowMethods'];
}
if (isset($config['webserver']['corsAllowHeaders'])) {
$corsPlugin->allowHeaders = $config['webserver']['corsAllowHeaders'];
}
if (isset($config['webserver']['corsAllowOrigin'])) {
$corsPlugin->allowOrigin = $config['webserver']['corsAllowOrigin'];
}
if (isset($config['webserver']['corsAllowCredentials'])) {
$corsPlugin->allowCredentials = $config['webserver']['corsAllowCredentials'];
}
if (isset($config['webserver']['corsExposeHeaders'])) {
$corsPlugin->exposeHeaders = $config['webserver']['corsExposeHeaders'];
}
// Regardless of the webserver settings, we need to support the ESNToken header
$corsPlugin->allowHeaders[] = 'ESNToken';
$server->addPlugin($corsPlugin);
$esnHookPlugin = new ESN\CalDAV\ESNHookPlugin($config['esn']['calendarRoot'], $authBackend);
$server->addPlugin($esnHookPlugin);
// Rabbit publisher plugin
if(!empty($config['amqp']['host'])){
$amqpLogin = !empty($config['amqp']['login']) ? $config['amqp']['login'] : 'guest';
$amqpPassword = !empty($config['amqp']['password']) ? $config['amqp']['password'] : 'guest';
$connection = new AMQPStreamConnection(
$config['amqp']['host'],
$config['amqp']['port'],
$amqpLogin,
$amqpPassword
);
$channel = $connection->channel();
$AMQPPublisher = new ESN\Publisher\AMQPPublisher($channel);
$eventRealTimePlugin = new ESN\Publisher\CalDAV\EventRealTimePlugin($AMQPPublisher, $calendarBackend);
$server->addPlugin($eventRealTimePlugin);
$calendarRealTimePlugin = new ESN\Publisher\CalDAV\CalendarRealTimePlugin($AMQPPublisher, $calendarBackend);
$server->addPlugin($calendarRealTimePlugin);
$subscriptionRealTimePlugin = new ESN\Publisher\CalDAV\SubscriptionRealTimePlugin($AMQPPublisher, $calendarBackend->getEventEmitter());
$server->addPlugin($subscriptionRealTimePlugin);
$contactRealTimePlugin = new ESN\Publisher\CardDAV\ContactRealTimePlugin($AMQPPublisher);
$server->addPlugin($contactRealTimePlugin);
$addressBookRealTimePlugin = new ESN\Publisher\CardDAV\AddressBookRealTimePlugin($AMQPPublisher, $addressbookBackend);
$server->addPlugin($addressBookRealTimePlugin);
$subscriptionRealTimePlugin = new ESN\Publisher\CardDAV\SubscriptionRealTimePlugin($AMQPPublisher, $addressbookBackend);
$server->addPlugin($subscriptionRealTimePlugin);
}
$communityMembersPlugin = new ESN\CalDAV\CollaborationMembersPlugin($esnDb, 'communities');
$server->addPlugin($communityMembersPlugin);
$projectMembersPlugin = new ESN\CalDAV\CollaborationMembersPlugin($esnDb, 'projects');
$server->addPlugin($projectMembersPlugin);
$server->addPlugin(new ESN\CalDAV\ParticipationPlugin());
$server->addPlugin(new ESN\CalDAV\MobileRequestPlugin());
$server->addPlugin(new ESN\CardDAV\MobileRequestPlugin());
$server->addPlugin(new ESN\CalDAV\ImportPlugin());
// And off we go!
$server->exec();