-
Notifications
You must be signed in to change notification settings - Fork 4
/
utility.php
370 lines (330 loc) · 15.4 KB
/
utility.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
<?php
/*
* Copyright (c) 2024 Bennet Becker <dev@bennet.cc>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
namespace NextcloudAttachments;
use GuzzleHttp\Exception\GuzzleException;
use IntlDateFormatter;
use rcmail;
function __(string $val): string
{
return NC_ATTACH_PREFIX . "_" . $val;
}
trait Utility
{
private static function log(...$lines): void
{
foreach ($lines as $line) {
$bt = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[1];
$func = $bt["function"];
$cls = $bt["class"];
if (!is_string($line)) {
$line = print_r($line, true);
}
$llines = explode(PHP_EOL, $line);
rcmail::write_log(NC_ATTACH_LOG_FILE, "[" . NC_ATTACH_PREFIX . "] {" . $cls . "::" . $func . "} " . $llines[0]);
unset($llines[0]);
if (count($llines) > 0) {
foreach ($llines as $l) {
rcmail::write_log(NC_ATTACH_LOG_FILE, str_pad("...", strlen("[" . NC_ATTACH_PREFIX . "] "), " ", STR_PAD_BOTH) . "{" . $cls . "::" . $func . "} " . $l);
}
}
}
}
/**
* Detect whether the user should be allowed to interact with the plugin
*
* @return bool true if use is excluded
*/
private function is_disabled(): bool
{
$ex = $this->rcmail->config->get(__("exclude_users"), []);
$exg = $this->rcmail->config->get(__("exclude_users_in_addr_books"), []);
$exa = $this->rcmail->config->get(__("exclude_users_with_addr_book_value"), []);
/** @noinspection SpellCheckingInspection */
$exag = $this->rcmail->config->get(__("exclude_users_in_addr_book_group"), []);
// exclude directly deny listed users
if (is_array($ex) && (in_array($this->rcmail->get_user_name(), $ex) || in_array($this->resolve_username(), $ex) || in_array($this->rcmail->get_user_email(), $ex))) {
self::log("access for " . $this->resolve_username() . " disabled via direct deny list");
return true;
}
// exclude directly deny listed address books
if (is_array($exg) && count($exg) > 0) {
foreach ($exg as $book) {
/** @noinspection SpellCheckingInspection */
$abook = $this->rcmail->get_address_book($book);
if ($abook) {
if (array_key_exists("uid", $book->coltypes)) {
$entries = $book->search(["email", "uid"], [$this->rcmail->get_user_email(), $this->resolve_username()]);
} else {
$entries = $book->search("email", $this->rcmail->get_user_email());
}
if ($entries) {
self::log("access for " . $this->resolve_username() .
" disabled in " . $book->get_name() . " because they exist in there");
return true;
}
}
}
}
// exclude users with a certain attribute in an address book
if (is_array($exa) && count($exa) > 0) {
// value not properly formatted
if (!is_array($exa[0])) {
$exa = [$exa];
}
foreach ($exa as $val) {
if (count($val) == 3) {
$book = $this->rcmail->get_address_book($val[0]);
$attr = $val[1];
$match = $val[2];
if (array_key_exists("uid", $book->coltypes)) {
$entries = $book->search(["email", "uid"], [$this->rcmail->get_user_email(), $this->resolve_username()]);
} else {
$entries = $book->search("email", $this->rcmail->get_user_email());
}
if ($entries) {
while ($e = $entries->iterate()) {
if (array_key_exists($attr, $e) && ($e[$attr] == $match ||
(is_array($e[$attr]) && in_array($match, $e[$attr])))) {
self::log("access for " . $this->resolve_username() .
" disabled in " . $book->get_name() . " because of " . $attr . "=" . $match);
return true;
}
}
}
}
}
}
// exclude users in groups
if (is_array($exag) && count($exag) > 0) {
if (!is_array($exag[0])) {
/** @noinspection SpellCheckingInspection */
$exag = [$exag];
}
foreach ($exag as $val) {
if (count($val) == 2) {
$book = $this->rcmail->get_address_book($val[0]);
$group = $val[1];
$groups = $book->get_record_groups(base64_encode($this->resolve_username()));
if (in_array($group, $groups)) {
self::log("access for " . $this->resolve_username() .
" disabled in " . $book->get_name() . " because of group membership " . $group);
return true;
}
}
}
}
return false;
}
private function __check_login(): array
{
//Cached Result
if (isset($_SESSION['plugins']['nextcloud_attachments']) &&
$_SESSION['plugins']['nextcloud_attachments']['login_result']) {
return $_SESSION['plugins']['nextcloud_attachments']['login_result'];
}
$prefs = $this->rcmail->user->get_prefs();
$server = $this->rcmail->config->get(__("server"));
$username = $this->resolve_username();
//missing config
if (empty($server) || empty($username)) {
return ['status' => null];
}
//always prompt for app password, as mail passwords are determined to not work regardless
if ($this->rcmail->config->get(__("dont_try_mail_password"), false)) {
if (!isset($prefs["nextcloud_login"]) ||
empty($prefs["nextcloud_login"]["loginName"]) ||
empty($prefs["nextcloud_login"]["appPassword"])) {
return ['status' => 'login_required'];
}
}
//get app password and username or use rc ones
$username = isset($prefs["nextcloud_login"]) ? $prefs["nextcloud_login"]["loginName"] : $this->resolve_username($this->rcmail->get_user_name());
$password = isset($prefs["nextcloud_login"]) ? $prefs["nextcloud_login"]["appPassword"] : $this->rcmail->get_user_password();
//test webdav login
try {
$res = $this->client->request("PROPFIND", $server . "/remote.php/dav/files/" . $username, ['auth' => [$username, $password]]);
/** @noinspection SpellCheckingInspection */
$scode = $res->getStatusCode();
switch ($scode) {
case 401:
case 403:
unset($prefs["nextcloud_login"]);
$this->rcmail->user->save_prefs($prefs);
//we can't use the password
$_SESSION['plugins']['nextcloud_attachments']['login_result'] = ['status' => 'login_required'];
return ['status' => 'login_required'];
case 404:
unset($prefs["nextcloud_login"]);
$this->rcmail->user->save_prefs($prefs);
//the username does not exist
$_SESSION['plugins']['nextcloud_attachments']['login_result'] = ['status' => 'invalid_user'];
return ['status' => 'invalid_user'];
case 200:
case 207:
//we can log in
return ['status' => 'ok'];
default:
// Persist for client error codes. keep trying for server errors
if ($scode < 500) {
$_SESSION['plugins']['nextcloud_attachments']['login_result'] =
['status' => null, 'code' => $scode, 'message' => $res->getReasonPhrase()];
}
//Probably bad idea as a single 500 error will kill the logins of all active users
//unset($prefs["nextcloud_login"]);
//$this->rcmail->user->save_prefs($prefs);
//something weired happened
return ['status' => null, 'code' => $scode, 'message' => $res->getReasonPhrase()];
}
} catch (GuzzleException $e) {
self::log($this->rcmail->get_user_name() . " login check request failed: " . print_r($e, true));
return ['status' => null];
}
}
/**
* Helper to resolve Roundcube username (email) to Nextcloud username
*
* Returns resolved name.
*
* @param $user string The username
* @return string
*/
private function resolve_username(string $user = ""): string
{
if (empty($user)) {
// verbatim roundcube username
$user = $this->rcmail->user->get_username();
}
$username_tmpl = $this->rcmail->config->get(__("username"));
$mail = $this->rcmail->user->get_username("mail");
$mail_local = $this->rcmail->user->get_username("local");
$mail_domain = $this->rcmail->user->get_username("domain");
$imap_user = empty($_SESSION['username']) ? $mail_local : $_SESSION['username'];
return str_replace(["%s", "%i", "%e", "%l", "%u", "%d", "%h"],
[$user, $imap_user, $mail, $mail_local, $mail_local, $mail_domain, $_SESSION['storage_host'] ?? ""],
$username_tmpl);
}
/**
* Helper to find unique filename in upload folder.
*
* Returns filename or false if resolution failed.
* Resolution fails after >100 iterations or on server error
*
* @param $folder_uri string base uri
* @param $filename string start filename
* @param $username string login
* @param $password string login
* @return bool|string unique filename or false on error
*/
private function unique_filename(string $folder_uri, string $filename, string $username, string $password): bool|string
{
$fn = $filename;
$i = 0;
try {
//iterate the folder until the filename is unique.
while (($code = $this->client->request("PROPFIND", $folder_uri . "/" . rawurlencode($fn),
['auth' => [$username, $password]])->getStatusCode()) != 404) {
$d = strrpos($filename, ".");
$fn = substr($filename, 0, $d) . " " . ++$i . substr($filename, $d);
if ($i > 100 || $code >= 500) {
return false;
}
}
} catch (GuzzleException $e) {
self::log($username . " file request failed: " . print_r($e, true));
return false;
}
return $fn;
}
/** @noinspection PhpUnusedPrivateMethodInspection */
private function resolve_destination_folder(string $folder_uri, string $file_path, string $username, string $password) : string | false
{
$folder_layout_locked = $this->rcmail->config->get(__("folder_layout_locked"), true);
if ($folder_layout_locked) {
$folder_layout = $this->rcmail->config->get(__("folder_layout"), "flat");
} else {
$prefs = $this->rcmail->user->get_prefs();
$user_folder_layout = $prefs[__("user_folder_layout")];
$folder_layout = match ($user_folder_layout) {
"default", null => $this->rcmail->config->get(__("folder_layout"), "flat"),
default => $prefs[__("user_folder_layout")]
};
}
if (str_starts_with($folder_layout, "hash")) {
$tokens = explode(":", $folder_layout);
$path_checksum = count($tokens) > 1 ? $tokens[1] : "sha1";
$path_hash = strtolower(hash_file($path_checksum, $file_path));
$hash_byte_len = strlen($path_hash) / 2;
if ($hash_byte_len != floor($hash_byte_len)) {
return false;
}
$depth = min(count($tokens) > 2 ? $tokens[2] : 2, $hash_byte_len);
$path_tokens = array_slice(str_split($path_hash, 2), 0, $depth);
//drop to folder creation below
} else if (str_starts_with($folder_layout, "date")) {
$tokens = explode(":", $folder_layout);
$date_format = datefmt_create(
$this->rcmail->get_user_language(),
IntlDateFormatter::FULL,
IntlDateFormatter::FULL,
null,
IntlDateFormatter::GREGORIAN,
$tokens[1] ?? "Y/LLLL"
);
$path = datefmt_format($date_format, time());
$path_tokens = explode("/", trim($path, "/"));
//drop to folder creation below
} else {
return "";
}
for($i=1; $i <= count($path_tokens); $i++) {
$path = implode("/", array_map(function($t){return rawurlencode($t);}, array_slice($path_tokens,0, $i)));
$path = str_ends_with($path, "/") ? $path : $path."/";
$res = $this->client->request("PROPFIND", $folder_uri."/".$path, ['auth' => [$username, $password]]);
if ($res->getStatusCode() == 404) {
$mkdir_res = $this->client->request("MKCOL", $folder_uri."/".$path, ['auth' => [$username, $password]]);
if ($mkdir_res->getStatusCode() >= 400) {
return false;
}
} elseif ($res->getStatusCode() >= 400) {
return false;
}
}
return implode("/", $path_tokens);
}
private function update_exclude(string $username, string $password, string $base_uri, string $folder): void
{
try{
$res = $this->client->get( $base_uri. "/.sync-exclude.lst", ['auth' => [$username, $password]]);
$list = [$folder];
if ($res->getStatusCode() < 300) {
$list = explode(PHP_EOL, $res->getBody()->getContents());
if (!preg_grep("/^".$folder."\/?$/", $list)) {
$list[] = $folder;
}
}
$this->client->put($base_uri. "/.sync-exclude.lst", ['auth' => [$username, $password], 'body' => implode(PHP_EOL, $list)]);
} catch (GuzzleException $e) {
self::log($e);
}
}
}