-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path20i.php
More file actions
141 lines (131 loc) · 4.55 KB
/
20i.php
File metadata and controls
141 lines (131 loc) · 4.55 KB
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
<?php
/**
* Roundcube Password Driver for 20i resellers.
*
* This driver changes a E-Mail-Password via 20i REST API
* Deps: PHP-Curl
*
* @author Harry Youd <harry@harryyoud.co.uk>
* @copyright Harry Youd, 2018
*
* Config needed:
* $config['password_20i_token'] = 'FILL THIS';
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*/
class rcube_20i_password {
function save($currpass, $newpass) {
$rcmail = rcmail::get_instance();
$token = $rcmail->config->get('password_20i_token');
$rest = new rcube_20i_restapi($token);
$email = explode('@', $_SESSION['username']);
$local = $email[0];
$domain = $email[1];
$domains = $rest->getWithFields("https://api.20i.com/package/" . $domain . "/email/" . $domain);
foreach ($domains as $mailboxes) {
foreach ($mailboxes as $mailbox) {
if ($mailbox->local == $local && (substr($mailbox->id, 0, 1) === 'm')) {
rcube::console("Found ". $local ." as id=". $mailbox->id);
$id = $mailbox->id;
break;
}
continue;
}
}
if (!$id) {
return PASSWORD_ERROR;
}
$data = [
"update" => [
$id => [
"password" => $newpass
]
]
];
rcube::console($rest->postWithFields("https://api.20i.com/package/" . $domain . "/email/" . $domain, $data));
return PASSWORD_SUCCESS;
}
}
class rcube_20i_restapi {
private $bearerToken;
private function sendRequest($url, $options = [])
{
$original_headers = isset($options[CURLOPT_HTTPHEADER]) ?
$options[CURLOPT_HTTPHEADER] :
[];
unset($options[CURLOPT_HTTPHEADER]);
$ch = curl_init($url);
curl_setopt_array($ch, $options + [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => $original_headers + [
"Expect:",
// ^Otherwise Curl will add Expect: 100 Continue, which is wrong.
"Authorization: Bearer " . base64_encode($this->bearerToken),
],
]);
$response = curl_exec($ch);
if ($response === false) {
throw new \Exception("Curl error: " . curl_error($ch));
}
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if (preg_match('/^404/', $status)) {
trigger_error("404 on $url");
$response = null;
} elseif (preg_match('/^[45]/', $status)) {
throw new \Exception("HTTP error {$status} on {$url}");
}
curl_close($ch);
return $response;
}
public function __construct($bearer_token)
{
$this->bearerToken = $bearer_token;
}
public function getRawWithFields($url, $fields = [], $options = [])
{
if (count($fields) > 0) {
$query = array_reduce(
array_keys($fields),
function ($carry, $item) use ($fields) {
return ($carry ? "$carry&" : "?") .
urlencode($item) . "=" . urlencode($fields[$item]);
},
""
);
} else {
$query = "";
}
return $this->sendRequest($url . $query, $options);
}
public function getWithFields($url, $fields = [], $options = [])
{
$response = $this->getRawWithFields($url, $fields, $options);
return json_decode($response);
}
public function postWithFields($url, $fields, $options = [])
{
$original_headers = isset($options[CURLOPT_HTTPHEADER]) ?
$options[CURLOPT_HTTPHEADER] :
[];
unset($options[CURLOPT_HTTPHEADER]);
$response = $this->sendRequest($url, [
CURLOPT_HTTPHEADER => $original_headers + [
"Content-Type: application/json",
],
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => json_encode($fields),
] + $options);
return json_decode($response);
}
}