Plesk.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. <?php
  2. namespace MGModule\DNSManager2\mgLibs\custom\dns\submodules;
  3. use \MGModule\DNSManager2\mgLibs\custom\dns;
  4. use \MGModule\DNSManager2\mgLibs\custom\dns\exceptions;
  5. use \MGModule\DNSManager2\mgLibs\custom\dns\interfaces;
  6. use \MGModule\DNSManager2\mgLibs\custom\dns\utils\Patterns;
  7. class Plesk extends dns\SubmoduleAbstract implements interfaces\SubmoduleImportInterface {
  8. public $configFields = array(
  9. 'host' => array(
  10. 'friendlyName' => 'Host',
  11. 'validators' => array(
  12. 'required' => 'required',
  13. )
  14. ),
  15. 'username' => array(
  16. 'friendlyName' => 'Username',
  17. 'validators' => array(
  18. 'required' => 'required',
  19. )
  20. ),
  21. 'password' => array(
  22. 'friendlyName' => 'Password',
  23. 'type'=> 'password',
  24. 'validators' => array(
  25. 'required' => 'required',
  26. )
  27. ),
  28. 'substriction_domain' => array(
  29. 'friendlyName' => 'Subscription Domain',
  30. 'validators' => array(
  31. 'required' => 'required',
  32. )
  33. ),
  34. 'default_ip' => array(
  35. 'friendlyName' => 'Default IP',
  36. 'validators' => array(
  37. 'pattern' => Patterns::IP4_OR_IP6,
  38. )
  39. ),
  40. );
  41. public $availableTypes = array('A', 'AAAA', 'NS', 'MX', 'CNAME', 'TXT', 'SRV', 'PTR');
  42. private $webspace_id = null;
  43. private function get($packet) {
  44. $curl = curl_init();
  45. curl_setopt($curl, CURLOPT_URL, "https://{$this->config['host']}:8443/enterprise/control/agent.php");
  46. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  47. curl_setopt($curl, CURLOPT_POST, true);
  48. curl_setopt($curl, CURLOPT_POSTFIELDS, $packet);
  49. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  50. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  51. curl_setopt($curl, CURLOPT_HTTPHEADER, array(
  52. "HTTP_AUTH_LOGIN: {$this->config['username']}",
  53. "HTTP_AUTH_PASSWD: {$this->config['password']}",
  54. "HTTP_PRETTY_PRINT: TRUE",
  55. "Content-Type: text/xml")
  56. );
  57. $retval = curl_exec($curl);
  58. if (curl_errno($curl)) {
  59. throw new exceptions\DNSSubmoduleException("cURL Error: " . curl_errno($curl) . " - " . curl_error($curl), dns\SubmoduleExceptionCodes::CONNECTION_PROBLEM);
  60. }
  61. curl_close($curl);
  62. @$result = simplexml_load_string($retval);
  63. if($result===FALSE) {
  64. throw new exceptions\DNSSubmoduleException('Unable to parse response', dns\SubmoduleExceptionCodes::INVALID_RESPONSE);
  65. }
  66. if($result->system->status == 'error') {
  67. throw new exceptions\DNSSubmoduleException((string)$result->system->errtext, dns\SubmoduleExceptionCodes::COMMAND_ERROR);
  68. }
  69. return $result;
  70. }
  71. public function testConnection(){
  72. $this->webspace_id= $this->getWebspaceID();
  73. $ok = $this->webspace_id !== false && $this->webspace_id > 0;
  74. return true;
  75. }
  76. private function getWebspaceID(){
  77. if(empty($this->config['substriction_domain'])){
  78. throw new exceptions\DNSSubmoduleException('Invalid substriction domain in configuration', dns\SubmoduleExceptionCodes::INVALID_PARAMETERS);
  79. }
  80. $packet = '<packet>
  81. <webspace>
  82. <get>
  83. <filter>
  84. <name>'.$this->config['substriction_domain'].'</name>
  85. </filter>
  86. <dataset>
  87. <hosting/>
  88. </dataset>
  89. </get>
  90. </webspace>
  91. </packet>';
  92. $return = $this->get($packet);
  93. if(isset($return->webspace->get->result->id)){
  94. return (string)$return->webspace->get->result->id;
  95. }else{
  96. if($return->webspace->get->result->errcode == 1013){
  97. throw new exceptions\DNSSubmoduleException('Substriction domain does not exist', dns\SubmoduleExceptionCodes::COMMAND_ERROR);
  98. }
  99. $this->throwError($return->webspace->get->result);
  100. }
  101. }
  102. private function throwError($result) {
  103. if($result->status == 'error') {
  104. throw new exceptions\DNSSubmoduleException((string)$result->errtext, dns\SubmoduleExceptionCodes::COMMAND_ERROR);
  105. } else if (strpos($result, 'Please') !== false) {
  106. throw new exceptions\DNSSubmoduleException($result, dns\SubmoduleExceptionCodes::COMMAND_ERROR);
  107. } else {
  108. throw new exceptions\DNSSubmoduleException('Unexpected error', dns\SubmoduleExceptionCodes::COMMAND_ERROR);
  109. }
  110. }
  111. private function getDomainInfo(){
  112. $packet = '<packet>
  113. <site>
  114. <get>
  115. <filter>
  116. <name>'.$this->domain.'</name>
  117. </filter>
  118. <dataset>
  119. <hosting/>
  120. </dataset>
  121. </get>
  122. </site>
  123. </packet>';
  124. $return = $this->get($packet);
  125. if(isset($return->site->get->result->result->id)){
  126. return (int)$return->site->get->result->result->id;
  127. }elseif($return->site->get->result->status == 'ok'){
  128. return (int)$return->site->get->result->id;
  129. }else{
  130. $this->throwError($return->site->get->result);
  131. }
  132. }
  133. public function getRecords($recordType = false) {
  134. $domain_id = $this->getDomainInfo();
  135. if($domain_id === false || $domain_id <= 0)
  136. return false;
  137. $packet = '<packet>
  138. <dns>
  139. <get_rec>
  140. <filter>
  141. <site-id>'.$domain_id.'</site-id>
  142. </filter>
  143. </get_rec>
  144. </dns>
  145. </packet> ';
  146. $return = $this->get($packet);
  147. $out = array();
  148. if((string)$return->dns->get_rec->result->status == 'ok'){
  149. foreach($return->dns->get_rec->result as $record_data){
  150. $type = strtoupper((string)$record_data->data->type);
  151. if(in_array($type, $recordType!==false ? array(strtoupper($recordType)) : $this->getAvailableRecordTypes())) {
  152. $record = new dns\record\Record();
  153. $record->line = (string)$record_data->id;
  154. $record->name = (string)$record_data->data->host;
  155. $record->type = $type;
  156. $record->createRDATAObject($type);
  157. switch($type) {
  158. case 'MX':
  159. $record->rdata->preference = (string)$record_data->data->opt;
  160. $record->rdata->exchange = (string)$record_data->data->value;
  161. break;
  162. case 'SRV':
  163. $record->rdata->fromString((string)$record_data->data->opt . ' ' . (string)$record_data->data->value);
  164. break;
  165. case 'PTR':
  166. if($record_data->data->opt)
  167. $record->name .= "/" . $record_data->data->opt;
  168. $record->rdata->setFirstProperty((string)$record_data->data->value);
  169. break;
  170. default:
  171. $record->rdata->setFirstProperty((string)$record_data->data->value);
  172. break;
  173. }
  174. $out[] = $record;
  175. }
  176. }
  177. } else {
  178. $this->throwError($return->dns->get_rec->result);
  179. }
  180. return $out;
  181. }
  182. public function addRecord(dns\record\Record $record) {
  183. $domain_id = $this->getDomainInfo();
  184. if($domain_id === false || $domain_id <= 0)
  185. return false;
  186. switch($record->type) {
  187. case 'MX':
  188. $value = $record->rdata->exchange;
  189. $opt = $record->rdata->preference;
  190. break;
  191. case 'SRV':
  192. $value = $record->rdata->target;
  193. $opt = str_replace("\t", ' ', $record->rdata->toString(['priority','weight','port']));
  194. break;
  195. case 'TXT':
  196. $value = trim($record->rdata->toString(), '"');
  197. $opt = '';
  198. break;
  199. default:
  200. $value = $record->rdata->toString();
  201. $opt = '';
  202. break;
  203. }
  204. if($record->type === 'PTR') {
  205. $host = str_replace('@', '', $record->nameToRelative($this->domain));
  206. $opt = explode('/',$host)[1];
  207. if(!($opt == '8' || $opt =='16' || $opt =='24'))
  208. $this->throwError('Please specify the subnet mask for the IP. Right now Plesk supports only the 8, 16 and 24 masks.');
  209. $host = explode('/',$host)[0];
  210. $packet = '<packet>
  211. <dns>
  212. <add_rec>
  213. <site-id>' . $domain_id . '</site-id>
  214. <type>' . $record->type . '</type>
  215. <host>' . $host . '</host>
  216. <value>' . $value . '</value>
  217. ' . (!empty($opt) ? "<opt>$opt</opt>" : '') . '
  218. </add_rec>
  219. </dns>
  220. </packet>';
  221. }
  222. else
  223. {
  224. $packet = '<packet>
  225. <dns>
  226. <add_rec>
  227. <site-id>' . $domain_id . '</site-id>
  228. <type>' . $record->type . '</type>
  229. <host>' . str_replace('@', '', $record->nameToRelative($this->domain)) . '</host>
  230. <value>' . $value . '</value>
  231. ' . (!empty($opt) ? "<opt>$opt</opt>" : '') . '
  232. </add_rec>
  233. </dns>
  234. </packet>';
  235. }
  236. $return = $this->get($packet);
  237. if((string)$return->dns->add_rec->result->status === 'ok'){
  238. return true;
  239. }
  240. $this->throwError($return->dns->add_rec->result);
  241. }
  242. public function editRecord(dns\record\Record $record) {
  243. $this->deleteRecord($record);
  244. $record->nameToAbsolute($this->domain);
  245. $this->addRecord($record);
  246. }
  247. public function deleteRecord(dns\record\Record $record) {
  248. $packet = '<packet>
  249. <dns>
  250. <del_rec>
  251. <filter>
  252. <id>'.$record->line.'</id>
  253. </filter>
  254. </del_rec>
  255. </dns>
  256. </packet>';
  257. $return = $this->get($packet);
  258. if((string)$return->dns->del_rec->result->status == 'ok'){
  259. return true;
  260. }
  261. $this->throwError($return->dns->del_rec->result);
  262. }
  263. public function zoneExists() {
  264. try {
  265. $domain_id = $this->getDomainInfo();
  266. } catch (exceptions\DNSSubmoduleException $e) {
  267. if($e->getCode() == dns\SubmoduleExceptionCodes::COMMAND_ERROR) {
  268. return false;
  269. }
  270. throw $e;
  271. }
  272. return $domain_id !== false && $domain_id > 0;
  273. }
  274. public function activateZone() {
  275. if(empty($this->webspace_id)){
  276. $this->webspace_id = $this->getWebspaceID();
  277. }
  278. if(empty($this->webspace_id)){
  279. return false; //TODO: error?
  280. }
  281. $packet = '<packet>
  282. <site>
  283. <add>
  284. <gen_setup>
  285. <name>'.$this->domain.'</name>
  286. <webspace-id>'.$this->webspace_id.'</webspace-id>
  287. </gen_setup>
  288. </add>
  289. </site>
  290. </packet>';
  291. $return = $this->get($packet);
  292. if((string)$return->site->add->result->status == 'ok'){
  293. return true;
  294. }
  295. $this->throwError($return->site->add->result);
  296. }
  297. public function terminateZone() {
  298. if($this->domain == $this->config['substriction_domain']){
  299. throw new exceptions\DNSSubmoduleException('You cannot delete subscription domain', dns\SubmoduleExceptionCodes::INVALID_PARAMETERS);
  300. }
  301. if(empty($this->webspace_id)){
  302. $this->webspace_id = $this->getWebspaceID();
  303. }
  304. if(empty($this->webspace_id)){
  305. return false;
  306. }
  307. $packet = '<packet>
  308. <site>
  309. <del>
  310. <filter>
  311. <name>'.$this->domain.'</name>
  312. </filter>
  313. </del>
  314. </site>
  315. </packet>';
  316. $return = $this->get($packet);
  317. if((string)$return->site->del->result->status == 'ok'){
  318. return true;
  319. }
  320. $this->throwError($return->site->del->result);
  321. }
  322. public function getZones() {
  323. $packet = '<packet>
  324. <webspace>
  325. <get>
  326. <filter/>
  327. <dataset>
  328. <gen_info/>
  329. </dataset>
  330. </get>
  331. </webspace>
  332. </packet>';
  333. $return = $this->get($packet);
  334. $out = array();
  335. if((string)$return->webspace->get->result->status == 'ok'){
  336. foreach($return->webspace->get->result as $info) {
  337. $out[(string)$info->data->gen_info->name] = (string)$info->data->gen_info->dns_ip_address;
  338. }
  339. }
  340. return $out;
  341. }
  342. }