@@ -33,17 +33,17 @@ discard block |
||
33 | 33 | use OCA\DAV\Connector\Sabre\Principal; |
34 | 34 | |
35 | 35 | $authBackend = new Auth( |
36 | - \OC::$server->getSession(), |
|
37 | - \OC::$server->getUserSession(), |
|
38 | - \OC::$server->getRequest(), |
|
39 | - \OC::$server->getTwoFactorAuthManager(), |
|
40 | - \OC::$server->getBruteForceThrottler(), |
|
41 | - 'principals/' |
|
36 | + \OC::$server->getSession(), |
|
37 | + \OC::$server->getUserSession(), |
|
38 | + \OC::$server->getRequest(), |
|
39 | + \OC::$server->getTwoFactorAuthManager(), |
|
40 | + \OC::$server->getBruteForceThrottler(), |
|
41 | + 'principals/' |
|
42 | 42 | ); |
43 | 43 | $principalBackend = new Principal( |
44 | - \OC::$server->getUserManager(), |
|
45 | - \OC::$server->getGroupManager(), |
|
46 | - 'principals/' |
|
44 | + \OC::$server->getUserManager(), |
|
45 | + \OC::$server->getGroupManager(), |
|
46 | + 'principals/' |
|
47 | 47 | ); |
48 | 48 | $db = \OC::$server->getDatabaseConnection(); |
49 | 49 | $userManager = \OC::$server->getUserManager(); |
@@ -62,8 +62,8 @@ discard block |
||
62 | 62 | $addressBookRoot->disableListing = !$debugging; // Disable listing |
63 | 63 | |
64 | 64 | $nodes = array( |
65 | - $principalCollection, |
|
66 | - $addressBookRoot, |
|
65 | + $principalCollection, |
|
66 | + $addressBookRoot, |
|
67 | 67 | ); |
68 | 68 | |
69 | 69 | // Fire up server |
@@ -79,7 +79,7 @@ discard block |
||
79 | 79 | |
80 | 80 | $server->addPlugin(new LegacyDAVACL()); |
81 | 81 | if ($debugging) { |
82 | - $server->addPlugin(new Sabre\DAV\Browser\Plugin()); |
|
82 | + $server->addPlugin(new Sabre\DAV\Browser\Plugin()); |
|
83 | 83 | } |
84 | 84 | |
85 | 85 | $server->addPlugin(new \Sabre\DAV\Sync\Plugin()); |
@@ -87,7 +87,7 @@ discard block |
||
87 | 87 | $server->addPlugin(new \OCA\DAV\CalDAV\Schedule\Plugin()); |
88 | 88 | |
89 | 89 | if ($sendInvitations) { |
90 | - $server->addPlugin(\OC::$server->query(\OCA\DAV\CalDAV\Schedule\IMipPlugin::class)); |
|
90 | + $server->addPlugin(\OC::$server->query(\OCA\DAV\CalDAV\Schedule\IMipPlugin::class)); |
|
91 | 91 | } |
92 | 92 | $server->addPlugin(new ExceptionLoggerPlugin('caldav', \OC::$server->getLogger())); |
93 | 93 |
@@ -60,218 +60,218 @@ |
||
60 | 60 | |
61 | 61 | class Server { |
62 | 62 | |
63 | - /** @var IRequest */ |
|
64 | - private $request; |
|
63 | + /** @var IRequest */ |
|
64 | + private $request; |
|
65 | 65 | |
66 | - /** @var string */ |
|
67 | - private $baseUri; |
|
66 | + /** @var string */ |
|
67 | + private $baseUri; |
|
68 | 68 | |
69 | - /** @var Connector\Sabre\Server */ |
|
70 | - private $server; |
|
69 | + /** @var Connector\Sabre\Server */ |
|
70 | + private $server; |
|
71 | 71 | |
72 | - public function __construct(IRequest $request, $baseUri) { |
|
73 | - $this->request = $request; |
|
74 | - $this->baseUri = $baseUri; |
|
75 | - $logger = \OC::$server->getLogger(); |
|
76 | - $mailer = \OC::$server->getMailer(); |
|
77 | - $dispatcher = \OC::$server->getEventDispatcher(); |
|
78 | - $timezone = new TimeFactory(); |
|
79 | - $sendInvitations = \OC::$server->getConfig()->getAppValue('dav', 'sendInvitations', 'yes') === 'yes'; |
|
80 | - $l10nFactory = \OC::$server->getL10NFactory(); |
|
72 | + public function __construct(IRequest $request, $baseUri) { |
|
73 | + $this->request = $request; |
|
74 | + $this->baseUri = $baseUri; |
|
75 | + $logger = \OC::$server->getLogger(); |
|
76 | + $mailer = \OC::$server->getMailer(); |
|
77 | + $dispatcher = \OC::$server->getEventDispatcher(); |
|
78 | + $timezone = new TimeFactory(); |
|
79 | + $sendInvitations = \OC::$server->getConfig()->getAppValue('dav', 'sendInvitations', 'yes') === 'yes'; |
|
80 | + $l10nFactory = \OC::$server->getL10NFactory(); |
|
81 | 81 | |
82 | - $root = new RootCollection(); |
|
83 | - $this->server = new \OCA\DAV\Connector\Sabre\Server(new CachingTree($root)); |
|
82 | + $root = new RootCollection(); |
|
83 | + $this->server = new \OCA\DAV\Connector\Sabre\Server(new CachingTree($root)); |
|
84 | 84 | |
85 | - // Add maintenance plugin |
|
86 | - $this->server->addPlugin(new \OCA\DAV\Connector\Sabre\MaintenancePlugin(\OC::$server->getConfig())); |
|
85 | + // Add maintenance plugin |
|
86 | + $this->server->addPlugin(new \OCA\DAV\Connector\Sabre\MaintenancePlugin(\OC::$server->getConfig())); |
|
87 | 87 | |
88 | - // Backends |
|
89 | - $authBackend = new Auth( |
|
90 | - \OC::$server->getSession(), |
|
91 | - \OC::$server->getUserSession(), |
|
92 | - \OC::$server->getRequest(), |
|
93 | - \OC::$server->getTwoFactorAuthManager(), |
|
94 | - \OC::$server->getBruteForceThrottler() |
|
95 | - ); |
|
88 | + // Backends |
|
89 | + $authBackend = new Auth( |
|
90 | + \OC::$server->getSession(), |
|
91 | + \OC::$server->getUserSession(), |
|
92 | + \OC::$server->getRequest(), |
|
93 | + \OC::$server->getTwoFactorAuthManager(), |
|
94 | + \OC::$server->getBruteForceThrottler() |
|
95 | + ); |
|
96 | 96 | |
97 | - // Set URL explicitly due to reverse-proxy situations |
|
98 | - $this->server->httpRequest->setUrl($this->request->getRequestUri()); |
|
99 | - $this->server->setBaseUri($this->baseUri); |
|
97 | + // Set URL explicitly due to reverse-proxy situations |
|
98 | + $this->server->httpRequest->setUrl($this->request->getRequestUri()); |
|
99 | + $this->server->setBaseUri($this->baseUri); |
|
100 | 100 | |
101 | - $this->server->addPlugin(new BlockLegacyClientPlugin(\OC::$server->getConfig())); |
|
102 | - $authPlugin = new Plugin(); |
|
103 | - $authPlugin->addBackend(new PublicAuth()); |
|
104 | - $this->server->addPlugin($authPlugin); |
|
101 | + $this->server->addPlugin(new BlockLegacyClientPlugin(\OC::$server->getConfig())); |
|
102 | + $authPlugin = new Plugin(); |
|
103 | + $authPlugin->addBackend(new PublicAuth()); |
|
104 | + $this->server->addPlugin($authPlugin); |
|
105 | 105 | |
106 | - // allow setup of additional auth backends |
|
107 | - $event = new SabrePluginEvent($this->server); |
|
108 | - $dispatcher->dispatch('OCA\DAV\Connector\Sabre::authInit', $event); |
|
106 | + // allow setup of additional auth backends |
|
107 | + $event = new SabrePluginEvent($this->server); |
|
108 | + $dispatcher->dispatch('OCA\DAV\Connector\Sabre::authInit', $event); |
|
109 | 109 | |
110 | - $bearerAuthBackend = new BearerAuth( |
|
111 | - \OC::$server->getUserSession(), |
|
112 | - \OC::$server->getSession(), |
|
113 | - \OC::$server->getRequest() |
|
114 | - ); |
|
115 | - $authPlugin->addBackend($bearerAuthBackend); |
|
116 | - // because we are throwing exceptions this plugin has to be the last one |
|
117 | - $authPlugin->addBackend($authBackend); |
|
110 | + $bearerAuthBackend = new BearerAuth( |
|
111 | + \OC::$server->getUserSession(), |
|
112 | + \OC::$server->getSession(), |
|
113 | + \OC::$server->getRequest() |
|
114 | + ); |
|
115 | + $authPlugin->addBackend($bearerAuthBackend); |
|
116 | + // because we are throwing exceptions this plugin has to be the last one |
|
117 | + $authPlugin->addBackend($authBackend); |
|
118 | 118 | |
119 | - // debugging |
|
120 | - if(\OC::$server->getConfig()->getSystemValue('debug', false)) { |
|
121 | - $this->server->addPlugin(new \Sabre\DAV\Browser\Plugin()); |
|
122 | - } else { |
|
123 | - $this->server->addPlugin(new DummyGetResponsePlugin()); |
|
124 | - } |
|
119 | + // debugging |
|
120 | + if(\OC::$server->getConfig()->getSystemValue('debug', false)) { |
|
121 | + $this->server->addPlugin(new \Sabre\DAV\Browser\Plugin()); |
|
122 | + } else { |
|
123 | + $this->server->addPlugin(new DummyGetResponsePlugin()); |
|
124 | + } |
|
125 | 125 | |
126 | - $this->server->addPlugin(new \OCA\DAV\Connector\Sabre\ExceptionLoggerPlugin('webdav', $logger)); |
|
127 | - $this->server->addPlugin(new \OCA\DAV\Connector\Sabre\LockPlugin()); |
|
128 | - $this->server->addPlugin(new \Sabre\DAV\Sync\Plugin()); |
|
126 | + $this->server->addPlugin(new \OCA\DAV\Connector\Sabre\ExceptionLoggerPlugin('webdav', $logger)); |
|
127 | + $this->server->addPlugin(new \OCA\DAV\Connector\Sabre\LockPlugin()); |
|
128 | + $this->server->addPlugin(new \Sabre\DAV\Sync\Plugin()); |
|
129 | 129 | |
130 | - // acl |
|
131 | - $acl = new DavAclPlugin(); |
|
132 | - $acl->principalCollectionSet = [ |
|
133 | - 'principals/users', 'principals/groups' |
|
134 | - ]; |
|
135 | - $acl->defaultUsernamePath = 'principals/users'; |
|
136 | - $this->server->addPlugin($acl); |
|
130 | + // acl |
|
131 | + $acl = new DavAclPlugin(); |
|
132 | + $acl->principalCollectionSet = [ |
|
133 | + 'principals/users', 'principals/groups' |
|
134 | + ]; |
|
135 | + $acl->defaultUsernamePath = 'principals/users'; |
|
136 | + $this->server->addPlugin($acl); |
|
137 | 137 | |
138 | - // calendar plugins |
|
139 | - $this->server->addPlugin(new \OCA\DAV\CalDAV\Plugin()); |
|
140 | - $this->server->addPlugin(new \Sabre\CalDAV\ICSExportPlugin()); |
|
141 | - $this->server->addPlugin(new \OCA\DAV\CalDAV\Schedule\Plugin()); |
|
142 | - if ($sendInvitations) { |
|
143 | - $this->server->addPlugin(\OC::$server->query(\OCA\DAV\CalDAV\Schedule\IMipPlugin::class)); |
|
144 | - } |
|
145 | - $this->server->addPlugin(new \Sabre\CalDAV\Subscriptions\Plugin()); |
|
146 | - $this->server->addPlugin(new \Sabre\CalDAV\Notifications\Plugin()); |
|
147 | - $this->server->addPlugin(new DAV\Sharing\Plugin($authBackend, \OC::$server->getRequest())); |
|
148 | - $this->server->addPlugin(new \OCA\DAV\CalDAV\Publishing\PublishPlugin( |
|
149 | - \OC::$server->getConfig(), |
|
150 | - \OC::$server->getURLGenerator() |
|
151 | - )); |
|
138 | + // calendar plugins |
|
139 | + $this->server->addPlugin(new \OCA\DAV\CalDAV\Plugin()); |
|
140 | + $this->server->addPlugin(new \Sabre\CalDAV\ICSExportPlugin()); |
|
141 | + $this->server->addPlugin(new \OCA\DAV\CalDAV\Schedule\Plugin()); |
|
142 | + if ($sendInvitations) { |
|
143 | + $this->server->addPlugin(\OC::$server->query(\OCA\DAV\CalDAV\Schedule\IMipPlugin::class)); |
|
144 | + } |
|
145 | + $this->server->addPlugin(new \Sabre\CalDAV\Subscriptions\Plugin()); |
|
146 | + $this->server->addPlugin(new \Sabre\CalDAV\Notifications\Plugin()); |
|
147 | + $this->server->addPlugin(new DAV\Sharing\Plugin($authBackend, \OC::$server->getRequest())); |
|
148 | + $this->server->addPlugin(new \OCA\DAV\CalDAV\Publishing\PublishPlugin( |
|
149 | + \OC::$server->getConfig(), |
|
150 | + \OC::$server->getURLGenerator() |
|
151 | + )); |
|
152 | 152 | |
153 | - // addressbook plugins |
|
154 | - $this->server->addPlugin(new \OCA\DAV\CardDAV\Plugin()); |
|
155 | - $this->server->addPlugin(new VCFExportPlugin()); |
|
156 | - $this->server->addPlugin(new ImageExportPlugin(new PhotoCache(\OC::$server->getAppDataDir('dav-photocache')))); |
|
153 | + // addressbook plugins |
|
154 | + $this->server->addPlugin(new \OCA\DAV\CardDAV\Plugin()); |
|
155 | + $this->server->addPlugin(new VCFExportPlugin()); |
|
156 | + $this->server->addPlugin(new ImageExportPlugin(new PhotoCache(\OC::$server->getAppDataDir('dav-photocache')))); |
|
157 | 157 | |
158 | - // system tags plugins |
|
159 | - $this->server->addPlugin(new SystemTagPlugin( |
|
160 | - \OC::$server->getSystemTagManager(), |
|
161 | - \OC::$server->getGroupManager(), |
|
162 | - \OC::$server->getUserSession() |
|
163 | - )); |
|
158 | + // system tags plugins |
|
159 | + $this->server->addPlugin(new SystemTagPlugin( |
|
160 | + \OC::$server->getSystemTagManager(), |
|
161 | + \OC::$server->getGroupManager(), |
|
162 | + \OC::$server->getUserSession() |
|
163 | + )); |
|
164 | 164 | |
165 | - // comments plugin |
|
166 | - $this->server->addPlugin(new CommentsPlugin( |
|
167 | - \OC::$server->getCommentsManager(), |
|
168 | - \OC::$server->getUserSession() |
|
169 | - )); |
|
165 | + // comments plugin |
|
166 | + $this->server->addPlugin(new CommentsPlugin( |
|
167 | + \OC::$server->getCommentsManager(), |
|
168 | + \OC::$server->getUserSession() |
|
169 | + )); |
|
170 | 170 | |
171 | - $this->server->addPlugin(new CopyEtagHeaderPlugin()); |
|
171 | + $this->server->addPlugin(new CopyEtagHeaderPlugin()); |
|
172 | 172 | |
173 | - // allow setup of additional plugins |
|
174 | - $dispatcher->dispatch('OCA\DAV\Connector\Sabre::addPlugin', $event); |
|
173 | + // allow setup of additional plugins |
|
174 | + $dispatcher->dispatch('OCA\DAV\Connector\Sabre::addPlugin', $event); |
|
175 | 175 | |
176 | - // Some WebDAV clients do require Class 2 WebDAV support (locking), since |
|
177 | - // we do not provide locking we emulate it using a fake locking plugin. |
|
178 | - if($request->isUserAgent([ |
|
179 | - '/WebDAVFS/', |
|
180 | - '/Microsoft Office OneNote 2013/', |
|
181 | - '/^Microsoft-WebDAV/',// Microsoft-WebDAV-MiniRedir/6.1.7601 |
|
182 | - ])) { |
|
183 | - $this->server->addPlugin(new FakeLockerPlugin()); |
|
184 | - } |
|
176 | + // Some WebDAV clients do require Class 2 WebDAV support (locking), since |
|
177 | + // we do not provide locking we emulate it using a fake locking plugin. |
|
178 | + if($request->isUserAgent([ |
|
179 | + '/WebDAVFS/', |
|
180 | + '/Microsoft Office OneNote 2013/', |
|
181 | + '/^Microsoft-WebDAV/',// Microsoft-WebDAV-MiniRedir/6.1.7601 |
|
182 | + ])) { |
|
183 | + $this->server->addPlugin(new FakeLockerPlugin()); |
|
184 | + } |
|
185 | 185 | |
186 | - if (BrowserErrorPagePlugin::isBrowserRequest($request)) { |
|
187 | - $this->server->addPlugin(new BrowserErrorPagePlugin()); |
|
188 | - } |
|
186 | + if (BrowserErrorPagePlugin::isBrowserRequest($request)) { |
|
187 | + $this->server->addPlugin(new BrowserErrorPagePlugin()); |
|
188 | + } |
|
189 | 189 | |
190 | - // wait with registering these until auth is handled and the filesystem is setup |
|
191 | - $this->server->on('beforeMethod', function () use ($root) { |
|
192 | - // custom properties plugin must be the last one |
|
193 | - $userSession = \OC::$server->getUserSession(); |
|
194 | - $user = $userSession->getUser(); |
|
195 | - if ($user !== null) { |
|
196 | - $view = \OC\Files\Filesystem::getView(); |
|
197 | - $this->server->addPlugin( |
|
198 | - new FilesPlugin( |
|
199 | - $this->server->tree, |
|
200 | - \OC::$server->getConfig(), |
|
201 | - $this->request, |
|
202 | - \OC::$server->getPreviewManager(), |
|
203 | - false, |
|
204 | - !\OC::$server->getConfig()->getSystemValue('debug', false) |
|
205 | - ) |
|
206 | - ); |
|
190 | + // wait with registering these until auth is handled and the filesystem is setup |
|
191 | + $this->server->on('beforeMethod', function () use ($root) { |
|
192 | + // custom properties plugin must be the last one |
|
193 | + $userSession = \OC::$server->getUserSession(); |
|
194 | + $user = $userSession->getUser(); |
|
195 | + if ($user !== null) { |
|
196 | + $view = \OC\Files\Filesystem::getView(); |
|
197 | + $this->server->addPlugin( |
|
198 | + new FilesPlugin( |
|
199 | + $this->server->tree, |
|
200 | + \OC::$server->getConfig(), |
|
201 | + $this->request, |
|
202 | + \OC::$server->getPreviewManager(), |
|
203 | + false, |
|
204 | + !\OC::$server->getConfig()->getSystemValue('debug', false) |
|
205 | + ) |
|
206 | + ); |
|
207 | 207 | |
208 | - $this->server->addPlugin( |
|
209 | - new \Sabre\DAV\PropertyStorage\Plugin( |
|
210 | - new CustomPropertiesBackend( |
|
211 | - $this->server->tree, |
|
212 | - \OC::$server->getDatabaseConnection(), |
|
213 | - \OC::$server->getUserSession()->getUser() |
|
214 | - ) |
|
215 | - ) |
|
216 | - ); |
|
217 | - if ($view !== null) { |
|
218 | - $this->server->addPlugin( |
|
219 | - new QuotaPlugin($view, false)); |
|
220 | - } |
|
221 | - $this->server->addPlugin( |
|
222 | - new TagsPlugin( |
|
223 | - $this->server->tree, \OC::$server->getTagManager() |
|
224 | - ) |
|
225 | - ); |
|
226 | - // TODO: switch to LazyUserFolder |
|
227 | - $userFolder = \OC::$server->getUserFolder(); |
|
228 | - $this->server->addPlugin(new SharesPlugin( |
|
229 | - $this->server->tree, |
|
230 | - $userSession, |
|
231 | - $userFolder, |
|
232 | - \OC::$server->getShareManager() |
|
233 | - )); |
|
234 | - $this->server->addPlugin(new CommentPropertiesPlugin( |
|
235 | - \OC::$server->getCommentsManager(), |
|
236 | - $userSession |
|
237 | - )); |
|
238 | - $this->server->addPlugin(new \OCA\DAV\CalDAV\Search\SearchPlugin()); |
|
239 | - if ($view !== null) { |
|
240 | - $this->server->addPlugin(new FilesReportPlugin( |
|
241 | - $this->server->tree, |
|
242 | - $view, |
|
243 | - \OC::$server->getSystemTagManager(), |
|
244 | - \OC::$server->getSystemTagObjectMapper(), |
|
245 | - \OC::$server->getTagManager(), |
|
246 | - $userSession, |
|
247 | - \OC::$server->getGroupManager(), |
|
248 | - $userFolder |
|
249 | - )); |
|
250 | - $this->server->addPlugin(new SearchPlugin(new \OCA\DAV\Files\FileSearchBackend( |
|
251 | - $this->server->tree, |
|
252 | - $user, |
|
253 | - \OC::$server->getRootFolder(), |
|
254 | - \OC::$server->getShareManager(), |
|
255 | - $view |
|
256 | - ))); |
|
257 | - } |
|
258 | - } |
|
208 | + $this->server->addPlugin( |
|
209 | + new \Sabre\DAV\PropertyStorage\Plugin( |
|
210 | + new CustomPropertiesBackend( |
|
211 | + $this->server->tree, |
|
212 | + \OC::$server->getDatabaseConnection(), |
|
213 | + \OC::$server->getUserSession()->getUser() |
|
214 | + ) |
|
215 | + ) |
|
216 | + ); |
|
217 | + if ($view !== null) { |
|
218 | + $this->server->addPlugin( |
|
219 | + new QuotaPlugin($view, false)); |
|
220 | + } |
|
221 | + $this->server->addPlugin( |
|
222 | + new TagsPlugin( |
|
223 | + $this->server->tree, \OC::$server->getTagManager() |
|
224 | + ) |
|
225 | + ); |
|
226 | + // TODO: switch to LazyUserFolder |
|
227 | + $userFolder = \OC::$server->getUserFolder(); |
|
228 | + $this->server->addPlugin(new SharesPlugin( |
|
229 | + $this->server->tree, |
|
230 | + $userSession, |
|
231 | + $userFolder, |
|
232 | + \OC::$server->getShareManager() |
|
233 | + )); |
|
234 | + $this->server->addPlugin(new CommentPropertiesPlugin( |
|
235 | + \OC::$server->getCommentsManager(), |
|
236 | + $userSession |
|
237 | + )); |
|
238 | + $this->server->addPlugin(new \OCA\DAV\CalDAV\Search\SearchPlugin()); |
|
239 | + if ($view !== null) { |
|
240 | + $this->server->addPlugin(new FilesReportPlugin( |
|
241 | + $this->server->tree, |
|
242 | + $view, |
|
243 | + \OC::$server->getSystemTagManager(), |
|
244 | + \OC::$server->getSystemTagObjectMapper(), |
|
245 | + \OC::$server->getTagManager(), |
|
246 | + $userSession, |
|
247 | + \OC::$server->getGroupManager(), |
|
248 | + $userFolder |
|
249 | + )); |
|
250 | + $this->server->addPlugin(new SearchPlugin(new \OCA\DAV\Files\FileSearchBackend( |
|
251 | + $this->server->tree, |
|
252 | + $user, |
|
253 | + \OC::$server->getRootFolder(), |
|
254 | + \OC::$server->getShareManager(), |
|
255 | + $view |
|
256 | + ))); |
|
257 | + } |
|
258 | + } |
|
259 | 259 | |
260 | - // register plugins from apps |
|
261 | - $pluginManager = new PluginManager( |
|
262 | - \OC::$server, |
|
263 | - \OC::$server->getAppManager() |
|
264 | - ); |
|
265 | - foreach ($pluginManager->getAppPlugins() as $appPlugin) { |
|
266 | - $this->server->addPlugin($appPlugin); |
|
267 | - } |
|
268 | - foreach ($pluginManager->getAppCollections() as $appCollection) { |
|
269 | - $root->addChild($appCollection); |
|
270 | - } |
|
271 | - }); |
|
272 | - } |
|
260 | + // register plugins from apps |
|
261 | + $pluginManager = new PluginManager( |
|
262 | + \OC::$server, |
|
263 | + \OC::$server->getAppManager() |
|
264 | + ); |
|
265 | + foreach ($pluginManager->getAppPlugins() as $appPlugin) { |
|
266 | + $this->server->addPlugin($appPlugin); |
|
267 | + } |
|
268 | + foreach ($pluginManager->getAppCollections() as $appCollection) { |
|
269 | + $root->addChild($appCollection); |
|
270 | + } |
|
271 | + }); |
|
272 | + } |
|
273 | 273 | |
274 | - public function exec() { |
|
275 | - $this->server->exec(); |
|
276 | - } |
|
274 | + public function exec() { |
|
275 | + $this->server->exec(); |
|
276 | + } |
|
277 | 277 | } |
@@ -89,7 +89,7 @@ |
||
89 | 89 | * @param ILogger $logger |
90 | 90 | * @param ITimeFactory $timeFactory |
91 | 91 | * @param L10NFactory $l10nFactory |
92 | - * @param IUrlGenerator $urlGenerator |
|
92 | + * @param IURLGenerator $urlGenerator |
|
93 | 93 | * @param string $userId |
94 | 94 | */ |
95 | 95 | public function __construct(IConfig $config, IMailer $mailer, ILogger $logger, ITimeFactory $timeFactory, L10NFactory $l10nFactory, IURLGenerator $urlGenerator, $userId) { |
@@ -32,7 +32,6 @@ |
||
32 | 32 | use OCP\Mail\IEMailTemplate; |
33 | 33 | use OCP\Mail\IMailer; |
34 | 34 | use Sabre\CalDAV\Schedule\IMipPlugin as SabreIMipPlugin; |
35 | -use Sabre\DAV\Xml\Element\Prop; |
|
36 | 35 | use Sabre\VObject\Component\VCalendar; |
37 | 36 | use Sabre\VObject\Component\VEvent; |
38 | 37 | use Sabre\VObject\DateTimeParser; |
@@ -56,403 +56,403 @@ |
||
56 | 56 | */ |
57 | 57 | class IMipPlugin extends SabreIMipPlugin { |
58 | 58 | |
59 | - /** @var string */ |
|
60 | - private $userId; |
|
61 | - |
|
62 | - /** @var IConfig */ |
|
63 | - private $config; |
|
64 | - |
|
65 | - /** @var IMailer */ |
|
66 | - private $mailer; |
|
67 | - |
|
68 | - /** @var ILogger */ |
|
69 | - private $logger; |
|
70 | - |
|
71 | - /** @var ITimeFactory */ |
|
72 | - private $timeFactory; |
|
73 | - |
|
74 | - /** @var L10NFactory */ |
|
75 | - private $l10nFactory; |
|
76 | - |
|
77 | - /** @var IURLGenerator */ |
|
78 | - private $urlGenerator; |
|
79 | - |
|
80 | - const MAX_DATE = '2038-01-01'; |
|
81 | - |
|
82 | - const METHOD_REQUEST = 'request'; |
|
83 | - const METHOD_REPLY = 'reply'; |
|
84 | - const METHOD_CANCEL = 'cancel'; |
|
85 | - |
|
86 | - /** |
|
87 | - * @param IConfig $config |
|
88 | - * @param IMailer $mailer |
|
89 | - * @param ILogger $logger |
|
90 | - * @param ITimeFactory $timeFactory |
|
91 | - * @param L10NFactory $l10nFactory |
|
92 | - * @param IUrlGenerator $urlGenerator |
|
93 | - * @param string $userId |
|
94 | - */ |
|
95 | - public function __construct(IConfig $config, IMailer $mailer, ILogger $logger, ITimeFactory $timeFactory, L10NFactory $l10nFactory, IURLGenerator $urlGenerator, $userId) { |
|
96 | - parent::__construct(''); |
|
97 | - $this->userId = $userId; |
|
98 | - $this->config = $config; |
|
99 | - $this->mailer = $mailer; |
|
100 | - $this->logger = $logger; |
|
101 | - $this->timeFactory = $timeFactory; |
|
102 | - $this->l10nFactory = $l10nFactory; |
|
103 | - $this->urlGenerator = $urlGenerator; |
|
104 | - } |
|
105 | - |
|
106 | - /** |
|
107 | - * Event handler for the 'schedule' event. |
|
108 | - * |
|
109 | - * @param Message $iTipMessage |
|
110 | - * @return void |
|
111 | - */ |
|
112 | - public function schedule(Message $iTipMessage) { |
|
113 | - |
|
114 | - // Not sending any emails if the system considers the update |
|
115 | - // insignificant. |
|
116 | - if (!$iTipMessage->significantChange) { |
|
117 | - if (!$iTipMessage->scheduleStatus) { |
|
118 | - $iTipMessage->scheduleStatus = '1.0;We got the message, but it\'s not significant enough to warrant an email'; |
|
119 | - } |
|
120 | - return; |
|
121 | - } |
|
122 | - |
|
123 | - $summary = $iTipMessage->message->VEVENT->SUMMARY; |
|
124 | - |
|
125 | - if (parse_url($iTipMessage->sender, PHP_URL_SCHEME) !== 'mailto') { |
|
126 | - return; |
|
127 | - } |
|
128 | - |
|
129 | - if (parse_url($iTipMessage->recipient, PHP_URL_SCHEME) !== 'mailto') { |
|
130 | - return; |
|
131 | - } |
|
132 | - |
|
133 | - // don't send out mails for events that already took place |
|
134 | - if ($this->isEventInThePast($iTipMessage->message)) { |
|
135 | - return; |
|
136 | - } |
|
137 | - |
|
138 | - // Strip off mailto: |
|
139 | - $sender = substr($iTipMessage->sender, 7); |
|
140 | - $recipient = substr($iTipMessage->recipient, 7); |
|
141 | - |
|
142 | - $senderName = $iTipMessage->senderName ?: null; |
|
143 | - $recipientName = $iTipMessage->recipientName ?: null; |
|
144 | - |
|
145 | - /** @var VEvent $vevent */ |
|
146 | - $vevent = $iTipMessage->message->VEVENT; |
|
147 | - |
|
148 | - $attendee = $this->getCurrentAttendee($iTipMessage); |
|
149 | - $defaultLang = $this->config->getUserValue($this->userId, 'core', 'lang', $this->l10nFactory->findLanguage()); |
|
150 | - $lang = $this->getAttendeeLangOrDefault($defaultLang, $attendee); |
|
151 | - $l10n = $this->l10nFactory->get('dav', $lang); |
|
152 | - |
|
153 | - $meetingAttendeeName = $recipientName ?: $recipient; |
|
154 | - $meetingInviteeName = $senderName ?: $sender; |
|
155 | - |
|
156 | - $meetingTitle = $vevent->SUMMARY; |
|
157 | - $meetingDescription = $vevent->DESCRIPTION; |
|
158 | - |
|
159 | - $start = $vevent->DTSTART; |
|
160 | - if (isset($vevent->DTEND)) { |
|
161 | - $end = $vevent->DTEND; |
|
162 | - } elseif (isset($vevent->DURATION)) { |
|
163 | - $isFloating = $vevent->DTSTART->isFloating(); |
|
164 | - $end = clone $vevent->DTSTART; |
|
165 | - $endDateTime = $end->getDateTime(); |
|
166 | - $endDateTime = $endDateTime->add(DateTimeParser::parse($vevent->DURATION->getValue())); |
|
167 | - $end->setDateTime($endDateTime, $isFloating); |
|
168 | - } elseif (!$vevent->DTSTART->hasTime()) { |
|
169 | - $isFloating = $vevent->DTSTART->isFloating(); |
|
170 | - $end = clone $vevent->DTSTART; |
|
171 | - $endDateTime = $end->getDateTime(); |
|
172 | - $endDateTime = $endDateTime->modify('+1 day'); |
|
173 | - $end->setDateTime($endDateTime, $isFloating); |
|
174 | - } else { |
|
175 | - $end = clone $vevent->DTSTART; |
|
176 | - } |
|
177 | - |
|
178 | - $meetingWhen = $this->generateWhenString($l10n, $start, $end); |
|
179 | - |
|
180 | - $meetingUrl = $vevent->URL; |
|
181 | - $meetingLocation = $vevent->LOCATION; |
|
182 | - |
|
183 | - $defaultVal = '--'; |
|
184 | - |
|
185 | - $method = self::METHOD_REQUEST; |
|
186 | - switch (strtolower($iTipMessage->method)) { |
|
187 | - case self::METHOD_REPLY: |
|
188 | - $method = self::METHOD_REPLY; |
|
189 | - break; |
|
190 | - case self::METHOD_CANCEL: |
|
191 | - $method = self::METHOD_CANCEL; |
|
192 | - break; |
|
193 | - } |
|
194 | - |
|
195 | - $data = array( |
|
196 | - 'attendee_name' => (string)$meetingAttendeeName ?: $defaultVal, |
|
197 | - 'invitee_name' => (string)$meetingInviteeName ?: $defaultVal, |
|
198 | - 'meeting_title' => (string)$meetingTitle ?: $defaultVal, |
|
199 | - 'meeting_description' => (string)$meetingDescription ?: $defaultVal, |
|
200 | - 'meeting_url' => (string)$meetingUrl ?: $defaultVal, |
|
201 | - ); |
|
202 | - |
|
203 | - $message = $this->mailer->createMessage() |
|
204 | - ->setReplyTo([$sender => $senderName]) |
|
205 | - ->setTo([$recipient => $recipientName]); |
|
206 | - |
|
207 | - $template = $this->mailer->createEMailTemplate('dav.calendarInvite.' . $method, $data); |
|
208 | - $template->addHeader(); |
|
209 | - |
|
210 | - $this->addSubjectAndHeading($template, $l10n, $method, $summary, |
|
211 | - $meetingAttendeeName, $meetingInviteeName); |
|
212 | - $this->addBulletList($template, $l10n, $meetingWhen, $meetingLocation, |
|
213 | - $meetingDescription, $meetingUrl); |
|
214 | - |
|
215 | - $template->addFooter(); |
|
216 | - $message->useTemplate($template); |
|
217 | - |
|
218 | - $attachment = $this->mailer->createAttachment( |
|
219 | - $iTipMessage->message->serialize(), |
|
220 | - 'event.ics',// TODO(leon): Make file name unique, e.g. add event id |
|
221 | - 'text/calendar; method=' . $iTipMessage->method |
|
222 | - ); |
|
223 | - $message->attach($attachment); |
|
224 | - |
|
225 | - try { |
|
226 | - $failed = $this->mailer->send($message); |
|
227 | - $iTipMessage->scheduleStatus = '1.1; Scheduling message is sent via iMip'; |
|
228 | - if ($failed) { |
|
229 | - $this->logger->error('Unable to deliver message to {failed}', ['app' => 'dav', 'failed' => implode(', ', $failed)]); |
|
230 | - $iTipMessage->scheduleStatus = '5.0; EMail delivery failed'; |
|
231 | - } |
|
232 | - } catch(\Exception $ex) { |
|
233 | - $this->logger->logException($ex, ['app' => 'dav']); |
|
234 | - $iTipMessage->scheduleStatus = '5.0; EMail delivery failed'; |
|
235 | - } |
|
236 | - } |
|
237 | - |
|
238 | - /** |
|
239 | - * check if event took place in the past already |
|
240 | - * @param VCalendar $vObject |
|
241 | - * @return bool |
|
242 | - */ |
|
243 | - private function isEventInThePast(VCalendar $vObject) { |
|
244 | - /** @var VEvent $component */ |
|
245 | - $component = $vObject->VEVENT; |
|
246 | - |
|
247 | - $firstOccurrence = $component->DTSTART->getDateTime()->getTimeStamp(); |
|
248 | - // Finding the last occurrence is a bit harder |
|
249 | - if (!isset($component->RRULE)) { |
|
250 | - if (isset($component->DTEND)) { |
|
251 | - $lastOccurrence = $component->DTEND->getDateTime()->getTimeStamp(); |
|
252 | - } elseif (isset($component->DURATION)) { |
|
253 | - /** @var \DateTime $endDate */ |
|
254 | - $endDate = clone $component->DTSTART->getDateTime(); |
|
255 | - // $component->DTEND->getDateTime() returns DateTimeImmutable |
|
256 | - $endDate = $endDate->add(DateTimeParser::parse($component->DURATION->getValue())); |
|
257 | - $lastOccurrence = $endDate->getTimestamp(); |
|
258 | - } elseif (!$component->DTSTART->hasTime()) { |
|
259 | - /** @var \DateTime $endDate */ |
|
260 | - $endDate = clone $component->DTSTART->getDateTime(); |
|
261 | - // $component->DTSTART->getDateTime() returns DateTimeImmutable |
|
262 | - $endDate = $endDate->modify('+1 day'); |
|
263 | - $lastOccurrence = $endDate->getTimestamp(); |
|
264 | - } else { |
|
265 | - $lastOccurrence = $firstOccurrence; |
|
266 | - } |
|
267 | - } else { |
|
268 | - $it = new EventIterator($vObject, (string)$component->UID); |
|
269 | - $maxDate = new \DateTime(self::MAX_DATE); |
|
270 | - if ($it->isInfinite()) { |
|
271 | - $lastOccurrence = $maxDate->getTimestamp(); |
|
272 | - } else { |
|
273 | - $end = $it->getDtEnd(); |
|
274 | - while($it->valid() && $end < $maxDate) { |
|
275 | - $end = $it->getDtEnd(); |
|
276 | - $it->next(); |
|
277 | - |
|
278 | - } |
|
279 | - $lastOccurrence = $end->getTimestamp(); |
|
280 | - } |
|
281 | - } |
|
282 | - |
|
283 | - $currentTime = $this->timeFactory->getTime(); |
|
284 | - return $lastOccurrence < $currentTime; |
|
285 | - } |
|
286 | - |
|
287 | - |
|
288 | - /** |
|
289 | - * @param Message $iTipMessage |
|
290 | - * @return null|Property |
|
291 | - */ |
|
292 | - private function getCurrentAttendee(Message $iTipMessage) { |
|
293 | - /** @var VEvent $vevent */ |
|
294 | - $vevent = $iTipMessage->message->VEVENT; |
|
295 | - $attendees = $vevent->select('ATTENDEE'); |
|
296 | - foreach ($attendees as $attendee) { |
|
297 | - /** @var Property $attendee */ |
|
298 | - if (strcasecmp($attendee->getValue(), $iTipMessage->recipient) === 0) { |
|
299 | - return $attendee; |
|
300 | - } |
|
301 | - } |
|
302 | - return null; |
|
303 | - } |
|
304 | - |
|
305 | - /** |
|
306 | - * @param string $default |
|
307 | - * @param Property|null $attendee |
|
308 | - * @return string |
|
309 | - */ |
|
310 | - private function getAttendeeLangOrDefault($default, Property $attendee = null) { |
|
311 | - if ($attendee !== null) { |
|
312 | - $lang = $attendee->offsetGet('LANGUAGE'); |
|
313 | - if ($lang instanceof Parameter) { |
|
314 | - return $lang->getValue(); |
|
315 | - } |
|
316 | - } |
|
317 | - return $default; |
|
318 | - } |
|
319 | - |
|
320 | - /** |
|
321 | - * @param IL10N $l10n |
|
322 | - * @param Property $dtstart |
|
323 | - * @param Property $dtend |
|
324 | - */ |
|
325 | - private function generateWhenString(IL10N $l10n, Property $dtstart, Property $dtend) { |
|
326 | - $isAllDay = $dtstart instanceof Property\ICalendar\Date; |
|
327 | - |
|
328 | - /** @var Property\ICalendar\Date | Property\ICalendar\DateTime $dtstart */ |
|
329 | - /** @var Property\ICalendar\Date | Property\ICalendar\DateTime $dtend */ |
|
330 | - /** @var \DateTimeImmutable $dtstartDt */ |
|
331 | - $dtstartDt = $dtstart->getDateTime(); |
|
332 | - /** @var \DateTimeImmutable $dtendDt */ |
|
333 | - $dtendDt = $dtend->getDateTime(); |
|
334 | - |
|
335 | - $diff = $dtstartDt->diff($dtendDt); |
|
336 | - |
|
337 | - $dtstartDt = new \DateTime($dtstartDt->format(\DateTime::ATOM)); |
|
338 | - $dtendDt = new \DateTime($dtendDt->format(\DateTime::ATOM)); |
|
339 | - |
|
340 | - if ($isAllDay) { |
|
341 | - // One day event |
|
342 | - if ($diff->days === 1) { |
|
343 | - return $l10n->l('date', $dtstartDt, ['width' => 'medium']); |
|
344 | - } |
|
345 | - |
|
346 | - //event that spans over multiple days |
|
347 | - $localeStart = $l10n->l('date', $dtstartDt, ['width' => 'medium']); |
|
348 | - $localeEnd = $l10n->l('date', $dtendDt, ['width' => 'medium']); |
|
349 | - |
|
350 | - return $localeStart . ' - ' . $localeEnd; |
|
351 | - } |
|
352 | - |
|
353 | - /** @var Property\ICalendar\DateTime $dtstart */ |
|
354 | - /** @var Property\ICalendar\DateTime $dtend */ |
|
355 | - $isFloating = $dtstart->isFloating(); |
|
356 | - $startTimezone = $endTimezone = null; |
|
357 | - if (!$isFloating) { |
|
358 | - $prop = $dtstart->offsetGet('TZID'); |
|
359 | - if ($prop instanceof Parameter) { |
|
360 | - $startTimezone = $prop->getValue(); |
|
361 | - } |
|
362 | - |
|
363 | - $prop = $dtend->offsetGet('TZID'); |
|
364 | - if ($prop instanceof Parameter) { |
|
365 | - $endTimezone = $prop->getValue(); |
|
366 | - } |
|
367 | - } |
|
368 | - |
|
369 | - $localeStart = $l10n->l('datetime', $dtstartDt, ['width' => 'medium']); |
|
370 | - |
|
371 | - // always show full date with timezone if timezones are different |
|
372 | - if ($startTimezone !== $endTimezone) { |
|
373 | - $localeEnd = $l10n->l('datetime', $dtendDt, ['width' => 'medium']); |
|
374 | - |
|
375 | - return $localeStart . ' (' . $startTimezone . ') - ' . |
|
376 | - $localeEnd . ' (' . $endTimezone . ')'; |
|
377 | - } |
|
378 | - |
|
379 | - // show only end time if date is the same |
|
380 | - if ($this->isDayEqual($dtstartDt, $dtendDt)) { |
|
381 | - $localeEnd = $l10n->l('time', $dtendDt, ['width' => 'medium']); |
|
382 | - } else { |
|
383 | - $localeEnd = $l10n->l('datetime', $dtendDt, ['width' => 'medium']); |
|
384 | - } |
|
385 | - |
|
386 | - return $localeStart . ' - ' . $localeEnd . ' (' . $startTimezone . ')'; |
|
387 | - } |
|
388 | - |
|
389 | - /** |
|
390 | - * @param \DateTime $dtStart |
|
391 | - * @param \DateTime $dtEnd |
|
392 | - * @return bool |
|
393 | - */ |
|
394 | - private function isDayEqual(\DateTime $dtStart, \DateTime $dtEnd) { |
|
395 | - return $dtStart->format('Y-m-d') === $dtEnd->format('Y-m-d'); |
|
396 | - } |
|
397 | - |
|
398 | - /** |
|
399 | - * @param IEMailTemplate $template |
|
400 | - * @param IL10N $l10n |
|
401 | - * @param string $method |
|
402 | - * @param string $summary |
|
403 | - * @param string $attendeeName |
|
404 | - * @param string $inviteeName |
|
405 | - */ |
|
406 | - private function addSubjectAndHeading(IEMailTemplate $template, IL10N $l10n, |
|
407 | - $method, $summary, $attendeeName, $inviteeName) { |
|
408 | - if ($method === self::METHOD_CANCEL) { |
|
409 | - $template->setSubject('Cancelled: ' . $summary); |
|
410 | - $template->addHeading($l10n->t('Invitation canceled'), $l10n->t('Hello %s,', [$attendeeName])); |
|
411 | - $template->addBodyText($l10n->t('The meeting »%s« with %s was canceled.', [$summary, $inviteeName])); |
|
412 | - } else if ($method === self::METHOD_REPLY) { |
|
413 | - $template->setSubject('Re: ' . $summary); |
|
414 | - $template->addHeading($l10n->t('Invitation updated'), $l10n->t('Hello %s,', [$attendeeName])); |
|
415 | - $template->addBodyText($l10n->t('The meeting »%s« with %s was updated.', [$summary, $inviteeName])); |
|
416 | - } else { |
|
417 | - $template->setSubject('Invitation: ' . $summary); |
|
418 | - $template->addHeading($l10n->t('%s invited you to »%s«', [$inviteeName, $summary]), $l10n->t('Hello %s,', [$attendeeName])); |
|
419 | - } |
|
420 | - |
|
421 | - } |
|
422 | - |
|
423 | - /** |
|
424 | - * @param IEMailTemplate $template |
|
425 | - * @param IL10N $l10n |
|
426 | - * @param string $time |
|
427 | - * @param string $location |
|
428 | - * @param string $description |
|
429 | - * @param string $url |
|
430 | - */ |
|
431 | - private function addBulletList(IEMailTemplate $template, IL10N $l10n, $time, $location, $description, $url) { |
|
432 | - $template->addBodyListItem($time, $l10n->t('When:'), |
|
433 | - $this->getAbsoluteImagePath('filetypes/text-calendar.svg')); |
|
434 | - |
|
435 | - if ($location) { |
|
436 | - $template->addBodyListItem($location, $l10n->t('Where:'), |
|
437 | - $this->getAbsoluteImagePath('filetypes/location.svg')); |
|
438 | - } |
|
439 | - if ($description) { |
|
440 | - $template->addBodyListItem((string)$description, $l10n->t('Description:'), |
|
441 | - $this->getAbsoluteImagePath('filetypes/text.svg')); |
|
442 | - } |
|
443 | - if ($url) { |
|
444 | - $template->addBodyListItem((string)$url, $l10n->t('Link:'), |
|
445 | - $this->getAbsoluteImagePath('filetypes/link.svg')); |
|
446 | - } |
|
447 | - } |
|
448 | - |
|
449 | - /** |
|
450 | - * @param string $path |
|
451 | - * @return string |
|
452 | - */ |
|
453 | - private function getAbsoluteImagePath($path) { |
|
454 | - return $this->urlGenerator->getAbsoluteURL( |
|
455 | - $this->urlGenerator->imagePath('core', $path) |
|
456 | - ); |
|
457 | - } |
|
59 | + /** @var string */ |
|
60 | + private $userId; |
|
61 | + |
|
62 | + /** @var IConfig */ |
|
63 | + private $config; |
|
64 | + |
|
65 | + /** @var IMailer */ |
|
66 | + private $mailer; |
|
67 | + |
|
68 | + /** @var ILogger */ |
|
69 | + private $logger; |
|
70 | + |
|
71 | + /** @var ITimeFactory */ |
|
72 | + private $timeFactory; |
|
73 | + |
|
74 | + /** @var L10NFactory */ |
|
75 | + private $l10nFactory; |
|
76 | + |
|
77 | + /** @var IURLGenerator */ |
|
78 | + private $urlGenerator; |
|
79 | + |
|
80 | + const MAX_DATE = '2038-01-01'; |
|
81 | + |
|
82 | + const METHOD_REQUEST = 'request'; |
|
83 | + const METHOD_REPLY = 'reply'; |
|
84 | + const METHOD_CANCEL = 'cancel'; |
|
85 | + |
|
86 | + /** |
|
87 | + * @param IConfig $config |
|
88 | + * @param IMailer $mailer |
|
89 | + * @param ILogger $logger |
|
90 | + * @param ITimeFactory $timeFactory |
|
91 | + * @param L10NFactory $l10nFactory |
|
92 | + * @param IUrlGenerator $urlGenerator |
|
93 | + * @param string $userId |
|
94 | + */ |
|
95 | + public function __construct(IConfig $config, IMailer $mailer, ILogger $logger, ITimeFactory $timeFactory, L10NFactory $l10nFactory, IURLGenerator $urlGenerator, $userId) { |
|
96 | + parent::__construct(''); |
|
97 | + $this->userId = $userId; |
|
98 | + $this->config = $config; |
|
99 | + $this->mailer = $mailer; |
|
100 | + $this->logger = $logger; |
|
101 | + $this->timeFactory = $timeFactory; |
|
102 | + $this->l10nFactory = $l10nFactory; |
|
103 | + $this->urlGenerator = $urlGenerator; |
|
104 | + } |
|
105 | + |
|
106 | + /** |
|
107 | + * Event handler for the 'schedule' event. |
|
108 | + * |
|
109 | + * @param Message $iTipMessage |
|
110 | + * @return void |
|
111 | + */ |
|
112 | + public function schedule(Message $iTipMessage) { |
|
113 | + |
|
114 | + // Not sending any emails if the system considers the update |
|
115 | + // insignificant. |
|
116 | + if (!$iTipMessage->significantChange) { |
|
117 | + if (!$iTipMessage->scheduleStatus) { |
|
118 | + $iTipMessage->scheduleStatus = '1.0;We got the message, but it\'s not significant enough to warrant an email'; |
|
119 | + } |
|
120 | + return; |
|
121 | + } |
|
122 | + |
|
123 | + $summary = $iTipMessage->message->VEVENT->SUMMARY; |
|
124 | + |
|
125 | + if (parse_url($iTipMessage->sender, PHP_URL_SCHEME) !== 'mailto') { |
|
126 | + return; |
|
127 | + } |
|
128 | + |
|
129 | + if (parse_url($iTipMessage->recipient, PHP_URL_SCHEME) !== 'mailto') { |
|
130 | + return; |
|
131 | + } |
|
132 | + |
|
133 | + // don't send out mails for events that already took place |
|
134 | + if ($this->isEventInThePast($iTipMessage->message)) { |
|
135 | + return; |
|
136 | + } |
|
137 | + |
|
138 | + // Strip off mailto: |
|
139 | + $sender = substr($iTipMessage->sender, 7); |
|
140 | + $recipient = substr($iTipMessage->recipient, 7); |
|
141 | + |
|
142 | + $senderName = $iTipMessage->senderName ?: null; |
|
143 | + $recipientName = $iTipMessage->recipientName ?: null; |
|
144 | + |
|
145 | + /** @var VEvent $vevent */ |
|
146 | + $vevent = $iTipMessage->message->VEVENT; |
|
147 | + |
|
148 | + $attendee = $this->getCurrentAttendee($iTipMessage); |
|
149 | + $defaultLang = $this->config->getUserValue($this->userId, 'core', 'lang', $this->l10nFactory->findLanguage()); |
|
150 | + $lang = $this->getAttendeeLangOrDefault($defaultLang, $attendee); |
|
151 | + $l10n = $this->l10nFactory->get('dav', $lang); |
|
152 | + |
|
153 | + $meetingAttendeeName = $recipientName ?: $recipient; |
|
154 | + $meetingInviteeName = $senderName ?: $sender; |
|
155 | + |
|
156 | + $meetingTitle = $vevent->SUMMARY; |
|
157 | + $meetingDescription = $vevent->DESCRIPTION; |
|
158 | + |
|
159 | + $start = $vevent->DTSTART; |
|
160 | + if (isset($vevent->DTEND)) { |
|
161 | + $end = $vevent->DTEND; |
|
162 | + } elseif (isset($vevent->DURATION)) { |
|
163 | + $isFloating = $vevent->DTSTART->isFloating(); |
|
164 | + $end = clone $vevent->DTSTART; |
|
165 | + $endDateTime = $end->getDateTime(); |
|
166 | + $endDateTime = $endDateTime->add(DateTimeParser::parse($vevent->DURATION->getValue())); |
|
167 | + $end->setDateTime($endDateTime, $isFloating); |
|
168 | + } elseif (!$vevent->DTSTART->hasTime()) { |
|
169 | + $isFloating = $vevent->DTSTART->isFloating(); |
|
170 | + $end = clone $vevent->DTSTART; |
|
171 | + $endDateTime = $end->getDateTime(); |
|
172 | + $endDateTime = $endDateTime->modify('+1 day'); |
|
173 | + $end->setDateTime($endDateTime, $isFloating); |
|
174 | + } else { |
|
175 | + $end = clone $vevent->DTSTART; |
|
176 | + } |
|
177 | + |
|
178 | + $meetingWhen = $this->generateWhenString($l10n, $start, $end); |
|
179 | + |
|
180 | + $meetingUrl = $vevent->URL; |
|
181 | + $meetingLocation = $vevent->LOCATION; |
|
182 | + |
|
183 | + $defaultVal = '--'; |
|
184 | + |
|
185 | + $method = self::METHOD_REQUEST; |
|
186 | + switch (strtolower($iTipMessage->method)) { |
|
187 | + case self::METHOD_REPLY: |
|
188 | + $method = self::METHOD_REPLY; |
|
189 | + break; |
|
190 | + case self::METHOD_CANCEL: |
|
191 | + $method = self::METHOD_CANCEL; |
|
192 | + break; |
|
193 | + } |
|
194 | + |
|
195 | + $data = array( |
|
196 | + 'attendee_name' => (string)$meetingAttendeeName ?: $defaultVal, |
|
197 | + 'invitee_name' => (string)$meetingInviteeName ?: $defaultVal, |
|
198 | + 'meeting_title' => (string)$meetingTitle ?: $defaultVal, |
|
199 | + 'meeting_description' => (string)$meetingDescription ?: $defaultVal, |
|
200 | + 'meeting_url' => (string)$meetingUrl ?: $defaultVal, |
|
201 | + ); |
|
202 | + |
|
203 | + $message = $this->mailer->createMessage() |
|
204 | + ->setReplyTo([$sender => $senderName]) |
|
205 | + ->setTo([$recipient => $recipientName]); |
|
206 | + |
|
207 | + $template = $this->mailer->createEMailTemplate('dav.calendarInvite.' . $method, $data); |
|
208 | + $template->addHeader(); |
|
209 | + |
|
210 | + $this->addSubjectAndHeading($template, $l10n, $method, $summary, |
|
211 | + $meetingAttendeeName, $meetingInviteeName); |
|
212 | + $this->addBulletList($template, $l10n, $meetingWhen, $meetingLocation, |
|
213 | + $meetingDescription, $meetingUrl); |
|
214 | + |
|
215 | + $template->addFooter(); |
|
216 | + $message->useTemplate($template); |
|
217 | + |
|
218 | + $attachment = $this->mailer->createAttachment( |
|
219 | + $iTipMessage->message->serialize(), |
|
220 | + 'event.ics',// TODO(leon): Make file name unique, e.g. add event id |
|
221 | + 'text/calendar; method=' . $iTipMessage->method |
|
222 | + ); |
|
223 | + $message->attach($attachment); |
|
224 | + |
|
225 | + try { |
|
226 | + $failed = $this->mailer->send($message); |
|
227 | + $iTipMessage->scheduleStatus = '1.1; Scheduling message is sent via iMip'; |
|
228 | + if ($failed) { |
|
229 | + $this->logger->error('Unable to deliver message to {failed}', ['app' => 'dav', 'failed' => implode(', ', $failed)]); |
|
230 | + $iTipMessage->scheduleStatus = '5.0; EMail delivery failed'; |
|
231 | + } |
|
232 | + } catch(\Exception $ex) { |
|
233 | + $this->logger->logException($ex, ['app' => 'dav']); |
|
234 | + $iTipMessage->scheduleStatus = '5.0; EMail delivery failed'; |
|
235 | + } |
|
236 | + } |
|
237 | + |
|
238 | + /** |
|
239 | + * check if event took place in the past already |
|
240 | + * @param VCalendar $vObject |
|
241 | + * @return bool |
|
242 | + */ |
|
243 | + private function isEventInThePast(VCalendar $vObject) { |
|
244 | + /** @var VEvent $component */ |
|
245 | + $component = $vObject->VEVENT; |
|
246 | + |
|
247 | + $firstOccurrence = $component->DTSTART->getDateTime()->getTimeStamp(); |
|
248 | + // Finding the last occurrence is a bit harder |
|
249 | + if (!isset($component->RRULE)) { |
|
250 | + if (isset($component->DTEND)) { |
|
251 | + $lastOccurrence = $component->DTEND->getDateTime()->getTimeStamp(); |
|
252 | + } elseif (isset($component->DURATION)) { |
|
253 | + /** @var \DateTime $endDate */ |
|
254 | + $endDate = clone $component->DTSTART->getDateTime(); |
|
255 | + // $component->DTEND->getDateTime() returns DateTimeImmutable |
|
256 | + $endDate = $endDate->add(DateTimeParser::parse($component->DURATION->getValue())); |
|
257 | + $lastOccurrence = $endDate->getTimestamp(); |
|
258 | + } elseif (!$component->DTSTART->hasTime()) { |
|
259 | + /** @var \DateTime $endDate */ |
|
260 | + $endDate = clone $component->DTSTART->getDateTime(); |
|
261 | + // $component->DTSTART->getDateTime() returns DateTimeImmutable |
|
262 | + $endDate = $endDate->modify('+1 day'); |
|
263 | + $lastOccurrence = $endDate->getTimestamp(); |
|
264 | + } else { |
|
265 | + $lastOccurrence = $firstOccurrence; |
|
266 | + } |
|
267 | + } else { |
|
268 | + $it = new EventIterator($vObject, (string)$component->UID); |
|
269 | + $maxDate = new \DateTime(self::MAX_DATE); |
|
270 | + if ($it->isInfinite()) { |
|
271 | + $lastOccurrence = $maxDate->getTimestamp(); |
|
272 | + } else { |
|
273 | + $end = $it->getDtEnd(); |
|
274 | + while($it->valid() && $end < $maxDate) { |
|
275 | + $end = $it->getDtEnd(); |
|
276 | + $it->next(); |
|
277 | + |
|
278 | + } |
|
279 | + $lastOccurrence = $end->getTimestamp(); |
|
280 | + } |
|
281 | + } |
|
282 | + |
|
283 | + $currentTime = $this->timeFactory->getTime(); |
|
284 | + return $lastOccurrence < $currentTime; |
|
285 | + } |
|
286 | + |
|
287 | + |
|
288 | + /** |
|
289 | + * @param Message $iTipMessage |
|
290 | + * @return null|Property |
|
291 | + */ |
|
292 | + private function getCurrentAttendee(Message $iTipMessage) { |
|
293 | + /** @var VEvent $vevent */ |
|
294 | + $vevent = $iTipMessage->message->VEVENT; |
|
295 | + $attendees = $vevent->select('ATTENDEE'); |
|
296 | + foreach ($attendees as $attendee) { |
|
297 | + /** @var Property $attendee */ |
|
298 | + if (strcasecmp($attendee->getValue(), $iTipMessage->recipient) === 0) { |
|
299 | + return $attendee; |
|
300 | + } |
|
301 | + } |
|
302 | + return null; |
|
303 | + } |
|
304 | + |
|
305 | + /** |
|
306 | + * @param string $default |
|
307 | + * @param Property|null $attendee |
|
308 | + * @return string |
|
309 | + */ |
|
310 | + private function getAttendeeLangOrDefault($default, Property $attendee = null) { |
|
311 | + if ($attendee !== null) { |
|
312 | + $lang = $attendee->offsetGet('LANGUAGE'); |
|
313 | + if ($lang instanceof Parameter) { |
|
314 | + return $lang->getValue(); |
|
315 | + } |
|
316 | + } |
|
317 | + return $default; |
|
318 | + } |
|
319 | + |
|
320 | + /** |
|
321 | + * @param IL10N $l10n |
|
322 | + * @param Property $dtstart |
|
323 | + * @param Property $dtend |
|
324 | + */ |
|
325 | + private function generateWhenString(IL10N $l10n, Property $dtstart, Property $dtend) { |
|
326 | + $isAllDay = $dtstart instanceof Property\ICalendar\Date; |
|
327 | + |
|
328 | + /** @var Property\ICalendar\Date | Property\ICalendar\DateTime $dtstart */ |
|
329 | + /** @var Property\ICalendar\Date | Property\ICalendar\DateTime $dtend */ |
|
330 | + /** @var \DateTimeImmutable $dtstartDt */ |
|
331 | + $dtstartDt = $dtstart->getDateTime(); |
|
332 | + /** @var \DateTimeImmutable $dtendDt */ |
|
333 | + $dtendDt = $dtend->getDateTime(); |
|
334 | + |
|
335 | + $diff = $dtstartDt->diff($dtendDt); |
|
336 | + |
|
337 | + $dtstartDt = new \DateTime($dtstartDt->format(\DateTime::ATOM)); |
|
338 | + $dtendDt = new \DateTime($dtendDt->format(\DateTime::ATOM)); |
|
339 | + |
|
340 | + if ($isAllDay) { |
|
341 | + // One day event |
|
342 | + if ($diff->days === 1) { |
|
343 | + return $l10n->l('date', $dtstartDt, ['width' => 'medium']); |
|
344 | + } |
|
345 | + |
|
346 | + //event that spans over multiple days |
|
347 | + $localeStart = $l10n->l('date', $dtstartDt, ['width' => 'medium']); |
|
348 | + $localeEnd = $l10n->l('date', $dtendDt, ['width' => 'medium']); |
|
349 | + |
|
350 | + return $localeStart . ' - ' . $localeEnd; |
|
351 | + } |
|
352 | + |
|
353 | + /** @var Property\ICalendar\DateTime $dtstart */ |
|
354 | + /** @var Property\ICalendar\DateTime $dtend */ |
|
355 | + $isFloating = $dtstart->isFloating(); |
|
356 | + $startTimezone = $endTimezone = null; |
|
357 | + if (!$isFloating) { |
|
358 | + $prop = $dtstart->offsetGet('TZID'); |
|
359 | + if ($prop instanceof Parameter) { |
|
360 | + $startTimezone = $prop->getValue(); |
|
361 | + } |
|
362 | + |
|
363 | + $prop = $dtend->offsetGet('TZID'); |
|
364 | + if ($prop instanceof Parameter) { |
|
365 | + $endTimezone = $prop->getValue(); |
|
366 | + } |
|
367 | + } |
|
368 | + |
|
369 | + $localeStart = $l10n->l('datetime', $dtstartDt, ['width' => 'medium']); |
|
370 | + |
|
371 | + // always show full date with timezone if timezones are different |
|
372 | + if ($startTimezone !== $endTimezone) { |
|
373 | + $localeEnd = $l10n->l('datetime', $dtendDt, ['width' => 'medium']); |
|
374 | + |
|
375 | + return $localeStart . ' (' . $startTimezone . ') - ' . |
|
376 | + $localeEnd . ' (' . $endTimezone . ')'; |
|
377 | + } |
|
378 | + |
|
379 | + // show only end time if date is the same |
|
380 | + if ($this->isDayEqual($dtstartDt, $dtendDt)) { |
|
381 | + $localeEnd = $l10n->l('time', $dtendDt, ['width' => 'medium']); |
|
382 | + } else { |
|
383 | + $localeEnd = $l10n->l('datetime', $dtendDt, ['width' => 'medium']); |
|
384 | + } |
|
385 | + |
|
386 | + return $localeStart . ' - ' . $localeEnd . ' (' . $startTimezone . ')'; |
|
387 | + } |
|
388 | + |
|
389 | + /** |
|
390 | + * @param \DateTime $dtStart |
|
391 | + * @param \DateTime $dtEnd |
|
392 | + * @return bool |
|
393 | + */ |
|
394 | + private function isDayEqual(\DateTime $dtStart, \DateTime $dtEnd) { |
|
395 | + return $dtStart->format('Y-m-d') === $dtEnd->format('Y-m-d'); |
|
396 | + } |
|
397 | + |
|
398 | + /** |
|
399 | + * @param IEMailTemplate $template |
|
400 | + * @param IL10N $l10n |
|
401 | + * @param string $method |
|
402 | + * @param string $summary |
|
403 | + * @param string $attendeeName |
|
404 | + * @param string $inviteeName |
|
405 | + */ |
|
406 | + private function addSubjectAndHeading(IEMailTemplate $template, IL10N $l10n, |
|
407 | + $method, $summary, $attendeeName, $inviteeName) { |
|
408 | + if ($method === self::METHOD_CANCEL) { |
|
409 | + $template->setSubject('Cancelled: ' . $summary); |
|
410 | + $template->addHeading($l10n->t('Invitation canceled'), $l10n->t('Hello %s,', [$attendeeName])); |
|
411 | + $template->addBodyText($l10n->t('The meeting »%s« with %s was canceled.', [$summary, $inviteeName])); |
|
412 | + } else if ($method === self::METHOD_REPLY) { |
|
413 | + $template->setSubject('Re: ' . $summary); |
|
414 | + $template->addHeading($l10n->t('Invitation updated'), $l10n->t('Hello %s,', [$attendeeName])); |
|
415 | + $template->addBodyText($l10n->t('The meeting »%s« with %s was updated.', [$summary, $inviteeName])); |
|
416 | + } else { |
|
417 | + $template->setSubject('Invitation: ' . $summary); |
|
418 | + $template->addHeading($l10n->t('%s invited you to »%s«', [$inviteeName, $summary]), $l10n->t('Hello %s,', [$attendeeName])); |
|
419 | + } |
|
420 | + |
|
421 | + } |
|
422 | + |
|
423 | + /** |
|
424 | + * @param IEMailTemplate $template |
|
425 | + * @param IL10N $l10n |
|
426 | + * @param string $time |
|
427 | + * @param string $location |
|
428 | + * @param string $description |
|
429 | + * @param string $url |
|
430 | + */ |
|
431 | + private function addBulletList(IEMailTemplate $template, IL10N $l10n, $time, $location, $description, $url) { |
|
432 | + $template->addBodyListItem($time, $l10n->t('When:'), |
|
433 | + $this->getAbsoluteImagePath('filetypes/text-calendar.svg')); |
|
434 | + |
|
435 | + if ($location) { |
|
436 | + $template->addBodyListItem($location, $l10n->t('Where:'), |
|
437 | + $this->getAbsoluteImagePath('filetypes/location.svg')); |
|
438 | + } |
|
439 | + if ($description) { |
|
440 | + $template->addBodyListItem((string)$description, $l10n->t('Description:'), |
|
441 | + $this->getAbsoluteImagePath('filetypes/text.svg')); |
|
442 | + } |
|
443 | + if ($url) { |
|
444 | + $template->addBodyListItem((string)$url, $l10n->t('Link:'), |
|
445 | + $this->getAbsoluteImagePath('filetypes/link.svg')); |
|
446 | + } |
|
447 | + } |
|
448 | + |
|
449 | + /** |
|
450 | + * @param string $path |
|
451 | + * @return string |
|
452 | + */ |
|
453 | + private function getAbsoluteImagePath($path) { |
|
454 | + return $this->urlGenerator->getAbsoluteURL( |
|
455 | + $this->urlGenerator->imagePath('core', $path) |
|
456 | + ); |
|
457 | + } |
|
458 | 458 | } |
@@ -193,18 +193,18 @@ discard block |
||
193 | 193 | } |
194 | 194 | |
195 | 195 | $data = array( |
196 | - 'attendee_name' => (string)$meetingAttendeeName ?: $defaultVal, |
|
197 | - 'invitee_name' => (string)$meetingInviteeName ?: $defaultVal, |
|
198 | - 'meeting_title' => (string)$meetingTitle ?: $defaultVal, |
|
199 | - 'meeting_description' => (string)$meetingDescription ?: $defaultVal, |
|
200 | - 'meeting_url' => (string)$meetingUrl ?: $defaultVal, |
|
196 | + 'attendee_name' => (string) $meetingAttendeeName ?: $defaultVal, |
|
197 | + 'invitee_name' => (string) $meetingInviteeName ?: $defaultVal, |
|
198 | + 'meeting_title' => (string) $meetingTitle ?: $defaultVal, |
|
199 | + 'meeting_description' => (string) $meetingDescription ?: $defaultVal, |
|
200 | + 'meeting_url' => (string) $meetingUrl ?: $defaultVal, |
|
201 | 201 | ); |
202 | 202 | |
203 | 203 | $message = $this->mailer->createMessage() |
204 | 204 | ->setReplyTo([$sender => $senderName]) |
205 | 205 | ->setTo([$recipient => $recipientName]); |
206 | 206 | |
207 | - $template = $this->mailer->createEMailTemplate('dav.calendarInvite.' . $method, $data); |
|
207 | + $template = $this->mailer->createEMailTemplate('dav.calendarInvite.'.$method, $data); |
|
208 | 208 | $template->addHeader(); |
209 | 209 | |
210 | 210 | $this->addSubjectAndHeading($template, $l10n, $method, $summary, |
@@ -217,8 +217,8 @@ discard block |
||
217 | 217 | |
218 | 218 | $attachment = $this->mailer->createAttachment( |
219 | 219 | $iTipMessage->message->serialize(), |
220 | - 'event.ics',// TODO(leon): Make file name unique, e.g. add event id |
|
221 | - 'text/calendar; method=' . $iTipMessage->method |
|
220 | + 'event.ics', // TODO(leon): Make file name unique, e.g. add event id |
|
221 | + 'text/calendar; method='.$iTipMessage->method |
|
222 | 222 | ); |
223 | 223 | $message->attach($attachment); |
224 | 224 | |
@@ -229,7 +229,7 @@ discard block |
||
229 | 229 | $this->logger->error('Unable to deliver message to {failed}', ['app' => 'dav', 'failed' => implode(', ', $failed)]); |
230 | 230 | $iTipMessage->scheduleStatus = '5.0; EMail delivery failed'; |
231 | 231 | } |
232 | - } catch(\Exception $ex) { |
|
232 | + } catch (\Exception $ex) { |
|
233 | 233 | $this->logger->logException($ex, ['app' => 'dav']); |
234 | 234 | $iTipMessage->scheduleStatus = '5.0; EMail delivery failed'; |
235 | 235 | } |
@@ -265,13 +265,13 @@ discard block |
||
265 | 265 | $lastOccurrence = $firstOccurrence; |
266 | 266 | } |
267 | 267 | } else { |
268 | - $it = new EventIterator($vObject, (string)$component->UID); |
|
268 | + $it = new EventIterator($vObject, (string) $component->UID); |
|
269 | 269 | $maxDate = new \DateTime(self::MAX_DATE); |
270 | 270 | if ($it->isInfinite()) { |
271 | 271 | $lastOccurrence = $maxDate->getTimestamp(); |
272 | 272 | } else { |
273 | 273 | $end = $it->getDtEnd(); |
274 | - while($it->valid() && $end < $maxDate) { |
|
274 | + while ($it->valid() && $end < $maxDate) { |
|
275 | 275 | $end = $it->getDtEnd(); |
276 | 276 | $it->next(); |
277 | 277 | |
@@ -347,7 +347,7 @@ discard block |
||
347 | 347 | $localeStart = $l10n->l('date', $dtstartDt, ['width' => 'medium']); |
348 | 348 | $localeEnd = $l10n->l('date', $dtendDt, ['width' => 'medium']); |
349 | 349 | |
350 | - return $localeStart . ' - ' . $localeEnd; |
|
350 | + return $localeStart.' - '.$localeEnd; |
|
351 | 351 | } |
352 | 352 | |
353 | 353 | /** @var Property\ICalendar\DateTime $dtstart */ |
@@ -372,8 +372,8 @@ discard block |
||
372 | 372 | if ($startTimezone !== $endTimezone) { |
373 | 373 | $localeEnd = $l10n->l('datetime', $dtendDt, ['width' => 'medium']); |
374 | 374 | |
375 | - return $localeStart . ' (' . $startTimezone . ') - ' . |
|
376 | - $localeEnd . ' (' . $endTimezone . ')'; |
|
375 | + return $localeStart.' ('.$startTimezone.') - '. |
|
376 | + $localeEnd.' ('.$endTimezone.')'; |
|
377 | 377 | } |
378 | 378 | |
379 | 379 | // show only end time if date is the same |
@@ -383,7 +383,7 @@ discard block |
||
383 | 383 | $localeEnd = $l10n->l('datetime', $dtendDt, ['width' => 'medium']); |
384 | 384 | } |
385 | 385 | |
386 | - return $localeStart . ' - ' . $localeEnd . ' (' . $startTimezone . ')'; |
|
386 | + return $localeStart.' - '.$localeEnd.' ('.$startTimezone.')'; |
|
387 | 387 | } |
388 | 388 | |
389 | 389 | /** |
@@ -406,15 +406,15 @@ discard block |
||
406 | 406 | private function addSubjectAndHeading(IEMailTemplate $template, IL10N $l10n, |
407 | 407 | $method, $summary, $attendeeName, $inviteeName) { |
408 | 408 | if ($method === self::METHOD_CANCEL) { |
409 | - $template->setSubject('Cancelled: ' . $summary); |
|
409 | + $template->setSubject('Cancelled: '.$summary); |
|
410 | 410 | $template->addHeading($l10n->t('Invitation canceled'), $l10n->t('Hello %s,', [$attendeeName])); |
411 | 411 | $template->addBodyText($l10n->t('The meeting »%s« with %s was canceled.', [$summary, $inviteeName])); |
412 | 412 | } else if ($method === self::METHOD_REPLY) { |
413 | - $template->setSubject('Re: ' . $summary); |
|
413 | + $template->setSubject('Re: '.$summary); |
|
414 | 414 | $template->addHeading($l10n->t('Invitation updated'), $l10n->t('Hello %s,', [$attendeeName])); |
415 | 415 | $template->addBodyText($l10n->t('The meeting »%s« with %s was updated.', [$summary, $inviteeName])); |
416 | 416 | } else { |
417 | - $template->setSubject('Invitation: ' . $summary); |
|
417 | + $template->setSubject('Invitation: '.$summary); |
|
418 | 418 | $template->addHeading($l10n->t('%s invited you to »%s«', [$inviteeName, $summary]), $l10n->t('Hello %s,', [$attendeeName])); |
419 | 419 | } |
420 | 420 | |
@@ -437,11 +437,11 @@ discard block |
||
437 | 437 | $this->getAbsoluteImagePath('filetypes/location.svg')); |
438 | 438 | } |
439 | 439 | if ($description) { |
440 | - $template->addBodyListItem((string)$description, $l10n->t('Description:'), |
|
440 | + $template->addBodyListItem((string) $description, $l10n->t('Description:'), |
|
441 | 441 | $this->getAbsoluteImagePath('filetypes/text.svg')); |
442 | 442 | } |
443 | 443 | if ($url) { |
444 | - $template->addBodyListItem((string)$url, $l10n->t('Link:'), |
|
444 | + $template->addBodyListItem((string) $url, $l10n->t('Link:'), |
|
445 | 445 | $this->getAbsoluteImagePath('filetypes/link.svg')); |
446 | 446 | } |
447 | 447 | } |
@@ -29,196 +29,196 @@ |
||
29 | 29 | |
30 | 30 | class L10N implements IL10N { |
31 | 31 | |
32 | - /** @var IFactory */ |
|
33 | - protected $factory; |
|
34 | - |
|
35 | - /** @var string App of this object */ |
|
36 | - protected $app; |
|
37 | - |
|
38 | - /** @var string Language of this object */ |
|
39 | - protected $lang; |
|
40 | - |
|
41 | - /** @var string Plural forms (string) */ |
|
42 | - private $pluralFormString = 'nplurals=2; plural=(n != 1);'; |
|
43 | - |
|
44 | - /** @var string Plural forms (function) */ |
|
45 | - private $pluralFormFunction = null; |
|
46 | - |
|
47 | - /** @var string[] */ |
|
48 | - private $translations = []; |
|
49 | - |
|
50 | - /** |
|
51 | - * @param IFactory $factory |
|
52 | - * @param string $app |
|
53 | - * @param string $lang |
|
54 | - * @param array $files |
|
55 | - */ |
|
56 | - public function __construct(IFactory $factory, $app, $lang, array $files) { |
|
57 | - $this->factory = $factory; |
|
58 | - $this->app = $app; |
|
59 | - $this->lang = $lang; |
|
60 | - |
|
61 | - $this->translations = []; |
|
62 | - foreach ($files as $languageFile) { |
|
63 | - $this->load($languageFile); |
|
64 | - } |
|
65 | - } |
|
66 | - |
|
67 | - /** |
|
68 | - * The code (en, de, ...) of the language that is used for this instance |
|
69 | - * |
|
70 | - * @return string language |
|
71 | - */ |
|
72 | - public function getLanguageCode() { |
|
73 | - return $this->lang; |
|
74 | - } |
|
75 | - |
|
76 | - /** |
|
77 | - * Translating |
|
78 | - * @param string $text The text we need a translation for |
|
79 | - * @param array $parameters default:array() Parameters for sprintf |
|
80 | - * @return string Translation or the same text |
|
81 | - * |
|
82 | - * Returns the translation. If no translation is found, $text will be |
|
83 | - * returned. |
|
84 | - */ |
|
85 | - public function t($text, $parameters = array()) { |
|
86 | - return (string) new L10NString($this, $text, $parameters); |
|
87 | - } |
|
88 | - |
|
89 | - /** |
|
90 | - * Translating |
|
91 | - * @param string $text_singular the string to translate for exactly one object |
|
92 | - * @param string $text_plural the string to translate for n objects |
|
93 | - * @param integer $count Number of objects |
|
94 | - * @param array $parameters default:array() Parameters for sprintf |
|
95 | - * @return string Translation or the same text |
|
96 | - * |
|
97 | - * Returns the translation. If no translation is found, $text will be |
|
98 | - * returned. %n will be replaced with the number of objects. |
|
99 | - * |
|
100 | - * The correct plural is determined by the plural_forms-function |
|
101 | - * provided by the po file. |
|
102 | - * |
|
103 | - */ |
|
104 | - public function n($text_singular, $text_plural, $count, $parameters = array()) { |
|
105 | - $identifier = "_${text_singular}_::_${text_plural}_"; |
|
106 | - if (isset($this->translations[$identifier])) { |
|
107 | - return (string) new L10NString($this, $identifier, $parameters, $count); |
|
108 | - } else { |
|
109 | - if ($count === 1) { |
|
110 | - return (string) new L10NString($this, $text_singular, $parameters, $count); |
|
111 | - } else { |
|
112 | - return (string) new L10NString($this, $text_plural, $parameters, $count); |
|
113 | - } |
|
114 | - } |
|
115 | - } |
|
116 | - |
|
117 | - /** |
|
118 | - * Localization |
|
119 | - * @param string $type Type of localization |
|
120 | - * @param \DateTime|int|string $data parameters for this localization |
|
121 | - * @param array $options |
|
122 | - * @return string|int|false |
|
123 | - * |
|
124 | - * Returns the localized data. |
|
125 | - * |
|
126 | - * Implemented types: |
|
127 | - * - date |
|
128 | - * - Creates a date |
|
129 | - * - params: timestamp (int/string) |
|
130 | - * - datetime |
|
131 | - * - Creates date and time |
|
132 | - * - params: timestamp (int/string) |
|
133 | - * - time |
|
134 | - * - Creates a time |
|
135 | - * - params: timestamp (int/string) |
|
136 | - * - firstday: Returns the first day of the week (0 sunday - 6 saturday) |
|
137 | - * - jsdate: Returns the short JS date format |
|
138 | - */ |
|
139 | - public function l($type, $data = null, $options = array()) { |
|
140 | - // Use the language of the instance |
|
141 | - $locale = $this->getLanguageCode(); |
|
142 | - if ($locale === 'sr@latin') { |
|
143 | - $locale = 'sr_latn'; |
|
144 | - } |
|
145 | - |
|
146 | - if ($type === 'firstday') { |
|
147 | - return (int) Calendar::getFirstWeekday($locale); |
|
148 | - } |
|
149 | - if ($type === 'jsdate') { |
|
150 | - return (string) Calendar::getDateFormat('short', $locale); |
|
151 | - } |
|
152 | - |
|
153 | - $value = new \DateTime(); |
|
154 | - if ($data instanceof \DateTime) { |
|
155 | - $value = $data; |
|
156 | - } else if (is_string($data) && !is_numeric($data)) { |
|
157 | - $data = strtotime($data); |
|
158 | - $value->setTimestamp($data); |
|
159 | - } else if ($data !== null) { |
|
160 | - $value->setTimestamp($data); |
|
161 | - } |
|
162 | - |
|
163 | - $options = array_merge(array('width' => 'long'), $options); |
|
164 | - $width = $options['width']; |
|
165 | - switch ($type) { |
|
166 | - case 'date': |
|
167 | - return (string) Calendar::formatDate($value, $width, $locale); |
|
168 | - case 'datetime': |
|
169 | - return (string) Calendar::formatDatetime($value, $width, $locale); |
|
170 | - case 'time': |
|
171 | - return (string) Calendar::formatTime($value, $width, $locale); |
|
172 | - case 'weekdayName': |
|
173 | - return (string) Calendar::getWeekdayName($value, $width, $locale); |
|
174 | - default: |
|
175 | - return false; |
|
176 | - } |
|
177 | - } |
|
178 | - |
|
179 | - /** |
|
180 | - * Returns an associative array with all translations |
|
181 | - * |
|
182 | - * Called by \OC_L10N_String |
|
183 | - * @return array |
|
184 | - */ |
|
185 | - public function getTranslations() { |
|
186 | - return $this->translations; |
|
187 | - } |
|
188 | - |
|
189 | - /** |
|
190 | - * Returnsed function accepts the argument $n |
|
191 | - * |
|
192 | - * Called by \OC_L10N_String |
|
193 | - * @return string the plural form function |
|
194 | - */ |
|
195 | - public function getPluralFormFunction() { |
|
196 | - if (is_null($this->pluralFormFunction)) { |
|
197 | - $lang = $this->getLanguageCode(); |
|
198 | - $this->pluralFormFunction = function($n) use ($lang) { |
|
199 | - return PluralizationRules::get($n, $lang); |
|
200 | - }; |
|
201 | - } |
|
202 | - |
|
203 | - return $this->pluralFormFunction; |
|
204 | - } |
|
205 | - |
|
206 | - /** |
|
207 | - * @param $translationFile |
|
208 | - * @return bool |
|
209 | - */ |
|
210 | - protected function load($translationFile) { |
|
211 | - $json = json_decode(file_get_contents($translationFile), true); |
|
212 | - if (!is_array($json)) { |
|
213 | - $jsonError = json_last_error(); |
|
214 | - \OC::$server->getLogger()->warning("Failed to load $translationFile - json error code: $jsonError", ['app' => 'l10n']); |
|
215 | - return false; |
|
216 | - } |
|
217 | - |
|
218 | - if (!empty($json['pluralForm'])) { |
|
219 | - $this->pluralFormString = $json['pluralForm']; |
|
220 | - } |
|
221 | - $this->translations = array_merge($this->translations, $json['translations']); |
|
222 | - return true; |
|
223 | - } |
|
32 | + /** @var IFactory */ |
|
33 | + protected $factory; |
|
34 | + |
|
35 | + /** @var string App of this object */ |
|
36 | + protected $app; |
|
37 | + |
|
38 | + /** @var string Language of this object */ |
|
39 | + protected $lang; |
|
40 | + |
|
41 | + /** @var string Plural forms (string) */ |
|
42 | + private $pluralFormString = 'nplurals=2; plural=(n != 1);'; |
|
43 | + |
|
44 | + /** @var string Plural forms (function) */ |
|
45 | + private $pluralFormFunction = null; |
|
46 | + |
|
47 | + /** @var string[] */ |
|
48 | + private $translations = []; |
|
49 | + |
|
50 | + /** |
|
51 | + * @param IFactory $factory |
|
52 | + * @param string $app |
|
53 | + * @param string $lang |
|
54 | + * @param array $files |
|
55 | + */ |
|
56 | + public function __construct(IFactory $factory, $app, $lang, array $files) { |
|
57 | + $this->factory = $factory; |
|
58 | + $this->app = $app; |
|
59 | + $this->lang = $lang; |
|
60 | + |
|
61 | + $this->translations = []; |
|
62 | + foreach ($files as $languageFile) { |
|
63 | + $this->load($languageFile); |
|
64 | + } |
|
65 | + } |
|
66 | + |
|
67 | + /** |
|
68 | + * The code (en, de, ...) of the language that is used for this instance |
|
69 | + * |
|
70 | + * @return string language |
|
71 | + */ |
|
72 | + public function getLanguageCode() { |
|
73 | + return $this->lang; |
|
74 | + } |
|
75 | + |
|
76 | + /** |
|
77 | + * Translating |
|
78 | + * @param string $text The text we need a translation for |
|
79 | + * @param array $parameters default:array() Parameters for sprintf |
|
80 | + * @return string Translation or the same text |
|
81 | + * |
|
82 | + * Returns the translation. If no translation is found, $text will be |
|
83 | + * returned. |
|
84 | + */ |
|
85 | + public function t($text, $parameters = array()) { |
|
86 | + return (string) new L10NString($this, $text, $parameters); |
|
87 | + } |
|
88 | + |
|
89 | + /** |
|
90 | + * Translating |
|
91 | + * @param string $text_singular the string to translate for exactly one object |
|
92 | + * @param string $text_plural the string to translate for n objects |
|
93 | + * @param integer $count Number of objects |
|
94 | + * @param array $parameters default:array() Parameters for sprintf |
|
95 | + * @return string Translation or the same text |
|
96 | + * |
|
97 | + * Returns the translation. If no translation is found, $text will be |
|
98 | + * returned. %n will be replaced with the number of objects. |
|
99 | + * |
|
100 | + * The correct plural is determined by the plural_forms-function |
|
101 | + * provided by the po file. |
|
102 | + * |
|
103 | + */ |
|
104 | + public function n($text_singular, $text_plural, $count, $parameters = array()) { |
|
105 | + $identifier = "_${text_singular}_::_${text_plural}_"; |
|
106 | + if (isset($this->translations[$identifier])) { |
|
107 | + return (string) new L10NString($this, $identifier, $parameters, $count); |
|
108 | + } else { |
|
109 | + if ($count === 1) { |
|
110 | + return (string) new L10NString($this, $text_singular, $parameters, $count); |
|
111 | + } else { |
|
112 | + return (string) new L10NString($this, $text_plural, $parameters, $count); |
|
113 | + } |
|
114 | + } |
|
115 | + } |
|
116 | + |
|
117 | + /** |
|
118 | + * Localization |
|
119 | + * @param string $type Type of localization |
|
120 | + * @param \DateTime|int|string $data parameters for this localization |
|
121 | + * @param array $options |
|
122 | + * @return string|int|false |
|
123 | + * |
|
124 | + * Returns the localized data. |
|
125 | + * |
|
126 | + * Implemented types: |
|
127 | + * - date |
|
128 | + * - Creates a date |
|
129 | + * - params: timestamp (int/string) |
|
130 | + * - datetime |
|
131 | + * - Creates date and time |
|
132 | + * - params: timestamp (int/string) |
|
133 | + * - time |
|
134 | + * - Creates a time |
|
135 | + * - params: timestamp (int/string) |
|
136 | + * - firstday: Returns the first day of the week (0 sunday - 6 saturday) |
|
137 | + * - jsdate: Returns the short JS date format |
|
138 | + */ |
|
139 | + public function l($type, $data = null, $options = array()) { |
|
140 | + // Use the language of the instance |
|
141 | + $locale = $this->getLanguageCode(); |
|
142 | + if ($locale === 'sr@latin') { |
|
143 | + $locale = 'sr_latn'; |
|
144 | + } |
|
145 | + |
|
146 | + if ($type === 'firstday') { |
|
147 | + return (int) Calendar::getFirstWeekday($locale); |
|
148 | + } |
|
149 | + if ($type === 'jsdate') { |
|
150 | + return (string) Calendar::getDateFormat('short', $locale); |
|
151 | + } |
|
152 | + |
|
153 | + $value = new \DateTime(); |
|
154 | + if ($data instanceof \DateTime) { |
|
155 | + $value = $data; |
|
156 | + } else if (is_string($data) && !is_numeric($data)) { |
|
157 | + $data = strtotime($data); |
|
158 | + $value->setTimestamp($data); |
|
159 | + } else if ($data !== null) { |
|
160 | + $value->setTimestamp($data); |
|
161 | + } |
|
162 | + |
|
163 | + $options = array_merge(array('width' => 'long'), $options); |
|
164 | + $width = $options['width']; |
|
165 | + switch ($type) { |
|
166 | + case 'date': |
|
167 | + return (string) Calendar::formatDate($value, $width, $locale); |
|
168 | + case 'datetime': |
|
169 | + return (string) Calendar::formatDatetime($value, $width, $locale); |
|
170 | + case 'time': |
|
171 | + return (string) Calendar::formatTime($value, $width, $locale); |
|
172 | + case 'weekdayName': |
|
173 | + return (string) Calendar::getWeekdayName($value, $width, $locale); |
|
174 | + default: |
|
175 | + return false; |
|
176 | + } |
|
177 | + } |
|
178 | + |
|
179 | + /** |
|
180 | + * Returns an associative array with all translations |
|
181 | + * |
|
182 | + * Called by \OC_L10N_String |
|
183 | + * @return array |
|
184 | + */ |
|
185 | + public function getTranslations() { |
|
186 | + return $this->translations; |
|
187 | + } |
|
188 | + |
|
189 | + /** |
|
190 | + * Returnsed function accepts the argument $n |
|
191 | + * |
|
192 | + * Called by \OC_L10N_String |
|
193 | + * @return string the plural form function |
|
194 | + */ |
|
195 | + public function getPluralFormFunction() { |
|
196 | + if (is_null($this->pluralFormFunction)) { |
|
197 | + $lang = $this->getLanguageCode(); |
|
198 | + $this->pluralFormFunction = function($n) use ($lang) { |
|
199 | + return PluralizationRules::get($n, $lang); |
|
200 | + }; |
|
201 | + } |
|
202 | + |
|
203 | + return $this->pluralFormFunction; |
|
204 | + } |
|
205 | + |
|
206 | + /** |
|
207 | + * @param $translationFile |
|
208 | + * @return bool |
|
209 | + */ |
|
210 | + protected function load($translationFile) { |
|
211 | + $json = json_decode(file_get_contents($translationFile), true); |
|
212 | + if (!is_array($json)) { |
|
213 | + $jsonError = json_last_error(); |
|
214 | + \OC::$server->getLogger()->warning("Failed to load $translationFile - json error code: $jsonError", ['app' => 'l10n']); |
|
215 | + return false; |
|
216 | + } |
|
217 | + |
|
218 | + if (!empty($json['pluralForm'])) { |
|
219 | + $this->pluralFormString = $json['pluralForm']; |
|
220 | + } |
|
221 | + $this->translations = array_merge($this->translations, $json['translations']); |
|
222 | + return true; |
|
223 | + } |
|
224 | 224 | } |