You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

plesk.php 7.1KB

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