comparison plugins/password/drivers/plesk.php @ 0:1e000243b222

vanilla 1.3.3 distro, I hope
author Charlie Root
date Thu, 04 Jan 2018 15:50:29 -0500
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:1e000243b222
1 <?php
2
3 /**
4 * Roundcube Password Driver for Plesk-RPC.
5 *
6 * This driver changes a E-Mail-Password via Plesk-RPC
7 * Deps: PHP-Curl, SimpleXML
8 *
9 * @author Cyrill von Wattenwyl <cyrill.vonwattenwyl@adfinis-sygroup.ch>
10 * @copyright Adfinis SyGroup AG, 2014
11 *
12 * Config needed:
13 * $config['password_plesk_host'] = '10.0.0.5';
14 * $config['password_plesk_user'] = 'admin';
15 * $config['password_plesk_pass'] = 'pass';
16 * $config['password_plesk_rpc_port'] = 8443;
17 * $config['password_plesk_rpc_path'] = enterprise/control/agent.php;
18 *
19 * This program is free software: you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation, either version 3 of the License, or
22 * (at your option) any later version.
23 *
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
28 *
29 * You should have received a copy of the GNU General Public License
30 * along with this program. If not, see http://www.gnu.org/licenses/.
31 */
32
33 /**
34 * Roundcube Password Driver Class
35 *
36 * See {ROUNDCUBE_ROOT}/plugins/password/README for API description
37 *
38 * @author Cyrill von Wattenwyl <cyrill.vonwattenwyl@adfinis-sygroup.ch>
39 */
40 class rcube_plesk_password
41 {
42 /**
43 * this method is called from roundcube to change the password
44 *
45 * roundcube allready validated the old password so we just need to change it at this point
46 *
47 * @author Cyrill von Wattenwyl <cyrill.vonwattenwyl@adfinis-sygroup.ch>
48 * @param string $curpass Current password
49 * @param string $newpass New password
50 * @returns int PASSWORD_SUCCESS|PASSWORD_ERROR
51 */
52 function save($currpass, $newpass)
53 {
54 // get config
55 $rcmail = rcmail::get_instance();
56 $host = $rcmail->config->get('password_plesk_host');
57 $user = $rcmail->config->get('password_plesk_user');
58 $pass = $rcmail->config->get('password_plesk_pass');
59 $port = $rcmail->config->get('password_plesk_rpc_port');
60 $path = $rcmail->config->get('password_plesk_rpc_path');
61
62 // create plesk-object
63 $plesk = new plesk_rpc;
64 $plesk->init($host, $port, $path, $user, $pass);
65
66 // try to change password and return the status
67 $result = $plesk->change_mailbox_password($_SESSION['username'], $newpass);
68 //$plesk->destroy();
69
70 if ($result) {
71 return PASSWORD_SUCCESS;
72 }
73
74 return PASSWORD_ERROR;
75 }
76 }
77
78
79 /**
80 * Plesk RPC-Class
81 *
82 * Striped down version of Plesk-RPC-Class
83 * Just functions for changing mail-passwords included
84 *
85 * Documentation of Plesk RPC-API: http://download1.parallels.com/Plesk/PP11/11.0/Doc/en-US/online/plesk-api-rpc/
86 *
87 * @author Cyrill von Wattenwyl <cyrill.vonwattenwyl@adfinis-sygroup.ch>
88 */
89 class plesk_rpc
90 {
91 /**
92 * init plesk-rpc via curl
93 *
94 * @param string $host plesk host
95 * @param string $port plesk rpc port
96 * @param string $path plesk rpc path
97 * @param string $user plesk user
98 * @param string $user plesk password
99 * @returns void
100 */
101 function init($host, $port, $path, $user, $pass)
102 {
103 $headers = array(
104 sprintf("HTTP_AUTH_LOGIN: %s", $user),
105 sprintf("HTTP_AUTH_PASSWD: %s", $pass),
106 "Content-Type: text/xml"
107 );
108
109 $url = sprintf("https://%s:%s/%s", $host, $port, $path);
110 $this->curl = curl_init();
111
112 curl_setopt($this->curl, CURLOPT_CONNECTTIMEOUT , 5);
113 curl_setopt($this->curl, CURLOPT_SSL_VERIFYHOST , 0);
114 curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER , false);
115 curl_setopt($this->curl, CURLOPT_HTTPHEADER , $headers);
116 curl_setopt($this->curl, CURLOPT_URL , $url);
117 }
118
119 /**
120 * send a request to the plesk
121 *
122 * @param string $packet XML-Packet to send to Plesk
123 * @returns bool request was successful or not
124 */
125 function send_request($packet)
126 {
127 curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
128 curl_setopt($this->curl, CURLOPT_POSTFIELDS, $packet);
129 $retval = curl_exec($this->curl);
130
131 return $retval;
132 }
133
134 /**
135 * close curl
136 */
137 function destroy(){
138 curl_close($this->curl);
139 }
140
141 /**
142 * Creates an Initial SimpleXML-Object for Plesk-RPC
143 *
144 * @returns object SimpleXML object
145 */
146 function get_request_obj()
147 {
148 $request = new SimpleXMLElement("<packet></packet>");
149 $request->addAttribute("version", "1.6.3.0");
150
151 return $request;
152 }
153
154 /**
155 * Get all hosting-information of a domain
156 *
157 * @param string $domain domain-name
158 * @returns object SimpleXML object
159 */
160 function domain_info($domain)
161 {
162 // build xml
163 $request = $this->get_request_obj();
164 $site = $request->addChild("site");
165 $get = $site->addChild("get");
166 $filter = $get->addChild("filter");
167
168 $filter->addChild("name", utf8_encode($domain));
169 $dataset = $get->addChild("dataset");
170
171 $dataset->addChild("hosting");
172 $packet = $request->asXML();
173
174 // send the request
175 $res = $this->send_request($packet);
176
177 // make it to simple-xml-object
178 $xml = new SimpleXMLElement($res);
179
180 return $xml;
181 }
182
183 /**
184 * Get psa-id of a domain
185 *
186 * @param string $domain domain-name
187 *
188 * @returns bool|int false if failed and integer if successed
189 */
190 function get_domain_id($domain)
191 {
192 $xml = $this->domain_info($domain);
193 $id = intval($xml->site->get->result->id);
194 $id = (is_int($id)) ? $id : false;
195
196 return $id;
197 }
198
199 /**
200 * Change Password of a mailbox
201 *
202 * @param string $mailbox full email-address (user@domain.tld)
203 * @param string $newpass new password of mailbox
204 *
205 * @returns bool
206 */
207 function change_mailbox_password($mailbox, $newpass)
208 {
209 list($user, $domain) = explode("@", $mailbox);
210 $domain_id = $this->get_domain_id($domain);
211
212 // if domain cannot be resolved to an id, do not continue
213 if (!$domain_id) {
214 return false;
215 }
216
217 // build xml-packet
218 $request = $this -> get_request_obj();
219 $mail = $request -> addChild("mail");
220 $update = $mail -> addChild("update");
221 $add = $update -> addChild("set");
222 $filter = $add -> addChild("filter");
223 $filter->addChild("site-id", $domain_id);
224
225 $mailname = $filter->addChild("mailname");
226 $mailname->addChild("name", $user);
227
228 $password = $mailname->addChild("password");
229 $password->addChild("value", $newpass);
230 $password->addChild("type", "plain");
231
232 $packet = $request->asXML();
233
234 // send the request to plesk
235 $res = $this->send_request($packet);
236 $xml = new SimpleXMLElement($res);
237 $res = strval($xml->mail->update->set->result->status);
238
239 return $res == "ok";
240 }
241 }