forked from postfixadmin/postfixadmin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xmlrpc.php
222 lines (200 loc) · 6.02 KB
/
xmlrpc.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
<?php
/**
* Requires the Zend framework is installed and in the include path.
*
* Usage example:
* require_once('Zend/XmlRpc/Client.php');
* $xmlrpc = new Zend_XmlRpc_Client('https://server/xmlrpc.php');
*
* $http_client = $xmlrpc->getHttpClient();
* $http_client->setCookieJar();
*
* $login_object = $xmlrpc->getProxy('login');
* $success = $login_object->login($email_address, $password);
*
* if($success) {
* echo "We're logged in";
* }
* else {
* die("Auth failed");
* }
* $user = $xmlrpc->getProxy('user');
* $alias = $xmlrpc->getProxy('alias');
* $vacation = $xmlrpc->getProxy('vacation');
*
* if($vacation->checkVacation()) {
* echo "Vacation turned on for user";
* }
*
* Note, the requirement that your XmlRpc client provides cookies with each request.
* If it does not do this, then your authentication details will not persist across requests, and
* this XMLRPC interface will not work.
*/
require_once('common.php');
if ($CONF['xmlrpc_enabled'] == false) {
die("xmlrpc support disabled");
}
require_once('Zend/XmlRpc/Server.php');
$server = new Zend_XmlRpc_Server();
/**
* @param string $username
* @param string $password
* @return boolean true on success, else false.
*/
function login($username, $password)
{
$login = new Login('mailbox');
if ($login->login($username, $password)) {
session_regenerate_id();
$_SESSION['authenticated'] = true;
$_SESSION['sessid'] = array();
$_SESSION['sessid']['username'] = $username;
return true;
}
return false;
}
if (!isset($_SESSION['authenticated'])) {
$server->addFunction('login', 'login');
} else {
$server->setClass('UserProxy', 'user');
$server->setClass('VacationProxy', 'vacation');
$server->setClass('AliasProxy', 'alias');
}
echo $server->handle();
class UserProxy
{
/**
* @param string $old_password
* @param string $new_password
* @return boolean true on success
*/
public function changePassword($old_password, $new_password)
{
$uh = new MailboxHandler();
$username = $_SESSION['sessid']['username'] ?? '';
if (empty($username)) {
throw new \Exception("not logged in? invalid session");
}
if (!$uh->init($username)) {
return false; // user doesn't exist.
}
$login = new Login('mailbox');
try {
return $login->changePassword($username, $new_password, $old_password);
} catch (\Exception $e) {
return false;
}
}
/**
* @param string $username
* @param string $password
* @return boolean true if successful.
*/
public function login($username, $password)
{
$login = new Login('mailbox');
return $login->login($username, $password);
}
}
class VacationProxy
{
/**
* @return boolean true if the vacation is removed successfully. Else false.
*/
public function remove()
{
$vh = new VacationHandler($_SESSION['sessid']['username']);
return $vh->remove();
}
/**
* @return boolean true if vacation stuff is enabled in this instance of postfixadmin
* and the user has the ability to make changes to it.
*/
public function isVacationSupported()
{
$vh = new VacationHandler($_SESSION['sessid']['username']);
return $vh->vacation_supported();
}
/**
* @return boolean true if the user has an active vacation record etc.
*/
public function checkVacation()
{
$vh = new VacationHandler($_SESSION['sessid']['username']);
return $vh->check_vacation();
}
/**
* @return array|bool - either array of vacation details or boolean false if the user has none.
*/
public function getDetails()
{
$vh = new VacationHandler($_SESSION['sessid']['username']);
return $vh->get_details();
}
/**
* @param string $subject
* @param string $body
* @param int $interval_time
* @param string $activeFrom
* @param string $activeUntil
* @return boolean true on success.
* Whatiis @replyType?? for
*/
public function setAway($subject, $body, $interval_time = 0, $activeFrom = '2000-01-01', $activeUntil = '2099-12-31')
{
$vh = new VacationHandler($_SESSION['sessid']['username']);
return $vh->set_away($subject, $body, $interval_time, $activeFrom, $activeUntil);
}
}
class AliasProxy
{
/**
* @return array - array of aliases this user has. Array may be empty.
*/
public function get()
{
$ah = new AliasHandler();
$ah->init($_SESSION['sessid']['username']);
/* I see no point in returning special addresses to the user. */
$ah->view();
$result = $ah->result;
return $result['goto'];
}
/**
* @param array of email addresses (Strings)
* @param string flag to set ('forward_and_store' or 'remote_only')
* @return boolean true
*/
public function update($addresses, $flags)
{
$ah = new AliasHandler();
$ah->init($_SESSION['sessid']['username']);
$values = ['goto' => $addresses];
if ($flags == 'forward_and_store') {
$values['goto_mailbox'] = 1;
} elseif ($flags == 'remote_only') {
$values['goto_mailbox'] = 0;
} else {
return false; # invalid parameter
}
if (!$ah->set($values)) {
//error_log('ah->set failed' . print_r($values, true));
return false;
}
$store = $ah->save();
return $store;
}
/**
* @return boolean true if the user has 'store_and_forward' set.
* (i.e. their email address is also in the alias table). IF it returns false, then it's 'remote_only'
*/
public function hasStoreAndForward()
{
$ah = new AliasHandler();
$ah->init($_SESSION['sessid']['username']);
$ah->view();
$result = $ah->result;
return $result['goto_mailbox'] == 1;
}
}
/* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */