-
Notifications
You must be signed in to change notification settings - Fork 4
/
actions.php
110 lines (94 loc) · 4 KB
/
actions.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
<?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;
trait Actions
{
/**
* Action to check nextcloud login status
* @return void
*/
public function check_login(): void
{
$this->rcmail->output->command('plugin.nextcloud_login_result', $this->__check_login());
}
/**
* Action to start nextcloud login process
* @return void
*/
public function login(): void
{
$server = $this->rcmail->config->get(__("server"));
if (empty($server)) {
return;
}
//start login flow
try {
$res = $this->client->post($server . "/index.php/login/v2");
$body = $res->getBody()->getContents();
$data = json_decode($body, true);
if ($res->getStatusCode() !== 200) {
self::log($this->rcmail->get_user_name() . " login check request failed: " . print_r($data, true));
$this->rcmail->output->command('plugin.nextcloud_login', [
'status' => null, "message" => $res->getReasonPhrase(), "response" => $data]);
return;
}
//save poll endpoint and token to session
$_SESSION['plugins']['nextcloud_attachments'] = $data['poll'];
unset($_SESSION['plugins']['nextcloud_attachments']['login_result']);
$this->rcmail->output->command('plugin.nextcloud_login', ['status' => "ok", "url" => $data["login"]]);
} catch (GuzzleException $e) {
self::log($this->rcmail->get_user_name() . " login request failed: " . print_r($e, true));
$this->rcmail->output->command('plugin.nextcloud_login', ['status' => null]);
}
}
/**
* Action to log out and delete app password if possible
* @return void
*/
public function logout(): void
{
$prefs = $this->rcmail->user->get_prefs();
$username = isset($prefs["nextcloud_login"]) ? $prefs["nextcloud_login"]["loginName"] : $this->resolve_username($this->rcmail->get_user_name());
$password = $prefs["nextcloud_login"]["appPassword"];
if (isset($password)) {
$server = $this->rcmail->config->get(__("server"));
if (!empty($server)) {
try {
/** @noinspection SpellCheckingInspection */
$this->client->delete($server . "/ocs/v2.php/core/apppassword", [
'headers' => [
'OCS-APIRequest' => 'true'
],
'auth' => [$username, $password]
]);
} catch (GuzzleException) {
}
}
}
$prefs["nextcloud_login"] = null;
unset($_SESSION['plugins']['nextcloud_attachments']);
$this->rcmail->user->save_prefs($prefs);
$this->rcmail->output->command('command', 'save');
}
}