· 4 years ago · Sep 08, 2021, 10:46 PM
1<?php
2
3/**
4 * @author Jeffro
5 * @copyright 2011
6 */
7
8class PleskApi{
9
10 private $ignoreerror = false;
11
12 /**
13 * PleskApi::curlInit()
14 *
15 * @param mixed $host
16 * @return
17 */
18 private function curlInit($host)
19 {
20 $security = new Security();
21 if($host['host_authmethod'] == '0'){
22 $password = $security->decode($host['host_pass']);
23 $httpheader = array("HTTP_AUTH_LOGIN: {$host['host_user']}",
24 "HTTP_AUTH_PASSWD: {$password}",
25 "HTTP_PRETTY_PRINT: TRUE",
26 "Content-Type: text/xml");
27 }else{
28 $key = $security->decode($host['host_key']);
29 $httpheader = array("KEY: {$key}",
30 "HTTP_PRETTY_PRINT: TRUE",
31 "Content-Type: text/xml");
32 }
33 $security = new Security();
34 $password = $security->decode($host['host_pass']);
35 $curl = curl_init();
36 curl_setopt($curl, CURLOPT_URL, "https://{$host['host_address']}:{$host['host_port']}/{$host['host_path']}");
37 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
38 curl_setopt($curl, CURLOPT_POST, true);
39 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
40 curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
41 curl_setopt($curl, CURLOPT_HTTPHEADER, $httpheader);
42 return $curl;
43 }
44
45 /**
46 * PleskApi::sendRequest()
47 *
48 * @param mixed $curl
49 * @param mixed $packet
50 * @return
51 */
52 private function sendRequest($curl, $packet){
53 curl_setopt($curl, CURLOPT_POSTFIELDS, $packet);
54 $result = curl_exec($curl);
55 if (curl_errno($curl)) {
56 $error = curl_error($curl);
57 $errorcode = curl_errno($curl);
58 curl_close($curl);
59 throw new Exception("Er is iets mis gegaan: <br /><br /> Foutcode: " . $errorcode . "<br /> Foutmelding: " . $error );
60 }
61 curl_close($curl);
62 if(DEBUG == true){
63 echo "<pre>";
64 echo htmlentities($packet);
65 echo "</pre><br /><pre>";
66 echo htmlentities($result);
67 echo "</pre>";
68 }
69 return $result;
70 }
71
72 /**
73 * PleskApi::toggleIgnoreError()
74 *
75 * @return
76 */
77 private function toggleIgnoreError(){
78 if($this->ignoreerror == false){
79 $this->ignoreerror = true;
80 }else{
81 $this->ignoreerror = false;
82 }
83 }
84
85 /**
86 * PleskApi::parseResponse()
87 *
88 * @param mixed $response
89 * @return
90 */
91 private function parseResponse($response){
92 $xml = new SimpleXMLElement($response);
93 $status = $xml->xpath('//status');
94 if($status[0] == "error" AND $this->ignoreerror == false){
95 $errorcode = $xml->xpath('//errcode');
96 $errortext = $xml->xpath('//errtext');
97 throw new Exception("Er is iets mis gegaan: <br /><br /> Foutcode: ". $errorcode[0] . "<br /> Foutmelding: " . $errortext[0]);
98 }else{
99 return $response;
100 }
101 }
102
103 /**
104 * PleskApi::getIpAddressesPacket()
105 *
106 * @return
107 */
108 private function getIpAddressesPacket(){
109 // Create new XML document.
110 $xml = new DomDocument('1.0', 'UTF-8');
111 $xml->formatOutput = true;
112
113 // Create packet
114 $packet = $xml->createElement('packet');
115 $packet->setAttribute('version', '1.4.2.0');
116 $xml->appendChild($packet);
117 $ip = $xml->createElement('ip');
118 $packet->appendChild($ip);
119 $get = $xml->createElement('get');
120 $ip->appendChild($get);
121 return $xml->saveXML();
122 }
123
124 /**
125 * PleskApi::getIpAddresses()
126 *
127 * @param mixed $host
128 * @return
129 */
130 public function getIpAddresses($host){
131 $curl = $this->curlInit($host);
132 $packet = $this->getIpAddressesPacket();
133 $response = $this->sendRequest($curl, $packet);
134 if($this->parseResponse($response)){
135 $xml = new SimpleXMLElement($response);
136 $result = $xml->ip->get->result;
137 return $result;
138 }
139 }
140
141 /**
142 * PleskApi::setIpForwardingPacket()
143 *
144 * @param mixed $host
145 * @param mixed $params
146 * @return
147 */
148 private function setIpForwardingPacket($host, $params){
149 // Create new XML document.
150 $xml = new DomDocument('1.0', 'UTF-8');
151 $xml->formatOutput = true;
152
153 // Create packet
154 $packet = $xml->createElement('packet');
155 $packet->setAttribute('version', '1.4.2.0');
156 $xml->appendChild($packet);
157 $dns = $xml->createElement('dns');
158 $packet->appendChild($dns);
159 $add_rec = $xml->createElement('add_rec');
160 $dns->appendChild($add_rec);
161 return $xml->saveXML();
162 }
163
164 /**
165 * PleskApi::testConnection()
166 *
167 * @param mixed $host
168 * @return
169 */
170 public function testConnection($host){
171 $result = $this->getIpAddresses($host);
172 $status = $result->xpath('//status');
173 if($status[0] == "ok"){
174 return true;
175 }else{
176 return false;
177 }
178 }
179
180 /**
181 * PleskApi::getSecretKeysPacket()
182 *
183 * @return
184 */
185 private function getSecretKeysPacket(){
186 // Create new XML document.
187 $xml = new DomDocument('1.0', 'UTF-8');
188 $xml->formatOutput = true;
189
190 // Create packet
191 $packet = $xml->createElement('packet');
192 $packet->setAttribute('version', '1.4.2.0');
193 $xml->appendChild($packet);
194 $secret_key = $xml->createElement('secret_key');
195 $packet->appendChild($secret_key);
196 }
197
198 /**
199 * PleskApi::createSecretKeyPacket()
200 *
201 * @param mixed $ip
202 * @return
203 */
204 private function createSecretKeyPacket($ip){
205 // Create new XML document.
206 $xml = new DomDocument('1.0', 'UTF-8');
207 $xml->formatOutput = true;
208
209 // Create packet
210 $packet = $xml->createElement('packet');
211 $packet->setAttribute('version', '1.4.2.0');
212 $xml->appendChild($packet);
213 $secret_key = $xml->createElement('secret_key');
214 $packet->appendChild($secret_key);
215 $create = $xml->createElement('create');
216 $secret_key->appendChild($create);
217 $ip_address = $xml->createElement('ip_address', $ip);
218 $create->appendChild($ip_address);
219 return $xml->saveXML();
220 }
221
222 /**
223 * PleskApi::createSecretKey()
224 *
225 * @param mixed $host
226 * @param mixed $ip
227 * @return
228 */
229 public function createSecretKey($host, $ip){
230 $curl = $this->curlInit($host);
231 $packet = $this->createSecretKeyPacket($ip);
232 $response = $this->sendRequest($curl, $packet);
233 if($this->parseResponse($response)){
234 $xml = new SimpleXMLElement($response);
235 $secretkey = $xml->xpath('//key');
236 $key = (string) $secretkey[0];
237 return $key;
238 }
239
240 }
241
242 /**
243 * PleskApi::clientInfoPacket()
244 *
245 * @param mixed $client_login
246 * @return
247 */
248 private function clientInfoPacket($client_login){
249 // Create new XML document.
250 $xml = new DomDocument('1.0', 'UTF-8');
251 $xml->formatOutput = true;
252
253 // Create packet
254 $packet = $xml->createElement('packet');
255 $packet->setAttribute('version', '1.4.2.0');
256 $xml->appendChild($packet);
257 $client = $xml->createElement('client');
258 $packet->appendChild($client);
259 $get = $xml->createElement('get');
260 $client->appendChild($get);
261 $filter = $xml->createElement('filter');
262 $get->appendChild($filter);
263 $login = $xml->createElement('login', $client_login);
264 $filter->appendChild($login);
265 $dataset = $xml->createElement('dataset');
266 $get->appendChild($dataset);
267 $gen_info = $xml->createElement('gen_info', 'true');
268 $dataset->appendChild($gen_info);
269 return $xml->saveXML();
270 }
271
272 /**
273 * PleskApi::getClientInfo()
274 *
275 * @param mixed $host
276 * @param mixed $login
277 * @return
278 */
279 public function getClientInfo($host, $login){
280 $curl = $this->curlInit($host);
281 $packet = $this->clientInfoPacket($login);
282 $response = $this->sendRequest($curl, $packet);
283 if($this->parseResponse($response)){
284 $xml = new SimpleXMLElement($response);
285 $result = $xml->client->get->result;
286 return $result;
287 }
288 }
289
290 /**
291 * PleskApi::clientExists()
292 *
293 * @param mixed $host
294 * @param mixed $login
295 * @return
296 */
297 public function clientExists($host, $login){
298 $this->toggleIgnoreError();
299 $result = $this->getClientInfo($host, $login);
300 $status = $result->xpath('//status');
301 if($status[0] == "error"){
302 $this->toggleIgnoreError();
303 return false;
304 }else{
305 $this->toggleIgnoreError();
306 return true;
307 }
308 }
309
310 /**
311 * PleskApi::domainInfoPacket()
312 *
313 * @param mixed $domain
314 * @return
315 */
316 private function domainInfoPacket($domain){
317 // Create new XML document.
318 $xml = new DomDocument('1.0', 'UTF-8');
319 $xml->formatOutput = true;
320
321 // Create packet
322 $packet = $xml->createElement('packet');
323 $packet->setAttribute('version', '1.4.2.0');
324 $xml->appendChild($packet);
325 $client = $xml->createElement('domain');
326 $packet->appendChild($client);
327 $get = $xml->createElement('get');
328 $client->appendChild($get);
329 $filter = $xml->createElement('filter');
330 $get->appendChild($filter);
331 $login = $xml->createElement('domain_name', $domain);
332 $filter->appendChild($login);
333 $dataset = $xml->createElement('dataset');
334 $get->appendChild($dataset);
335 $gen_info = $xml->createElement('gen_info');
336 $dataset->appendChild($gen_info);
337 return $xml->saveXML();
338 }
339
340 /**
341 * PleskApi::getDomainInfo()
342 *
343 * @param mixed $host
344 * @param mixed $domain
345 * @return
346 */
347 public function getDomainInfo($host, $domain){
348 $curl = $this->curlInit($host);
349 $packet = $this->domainInfoPacket($domain);
350 $response = $this->sendRequest($curl, $packet);
351 if($this->parseResponse($response)){
352 $xml = new SimpleXMLElement($response);
353 $result = $xml->domain->get->result;
354 return $result;
355 }
356
357 }
358
359 /**
360 * PleskApi::domainExists()
361 *
362 * @param mixed $host
363 * @param mixed $domain
364 * @return
365 */
366 public function domainExists($host, $domain){
367 $this->toggleIgnoreError();
368 $result = $this->getDomainInfo($host, $domain);
369 $status = $result->xpath('//status');
370 if($status[0] == "error"){
371 $this->toggleIgnoreError();
372 return false;
373 }else{
374 $this->toggleIgnoreError();
375 return true;
376 }
377 }
378
379 /**
380 * PleskApi::clientCreatePacket()
381 *
382 * @param mixed $params
383 * @return
384 */
385 private function clientCreatePacket($params){
386 // Create new XML document.
387 $xml = new DomDocument('1.0', 'UTF-8');
388 $xml->formatOutput = true;
389
390 // Create packet
391 $packet = $xml->createElement('packet');
392 $packet->setAttribute('version', '1.4.2.0');
393 $xml->appendChild($packet);
394 $client = $xml->createElement('client');
395 $packet->appendChild($client);
396 $add = $xml->createElement('add');
397 $client->appendChild($add);
398 foreach($params as $name => $pvalues) {
399 $node = $xml->createElement($name);
400 $add->appendChild($node);
401 foreach ($pvalues as $key => $value) {
402 $xmlelement = $xml->createElement($key, $value);
403 $node->appendChild($xmlelement);
404 }
405 }
406 return $xml->saveXML();
407 }
408
409 /**
410 * PleskApi::createClient()
411 *
412 * @param mixed $host
413 * @param mixed $params
414 * @return
415 */
416 public function createClient($host, $params){
417 $curl = $this->curlInit($host);
418 $packet = $this->clientCreatePacket($params);
419 $response = $this->sendRequest($curl, $packet);
420 if($this->parseResponse($response)){
421 return true;
422 }
423 }
424
425 /**
426 * PleskApi::clientSetOveruseLimitPacket()
427 *
428 * @param mixed $clientinfo
429 * @return
430 */
431 private function clientSetOveruseLimitPacket($clientinfo){
432 // Create new XML document.
433 $xml = new DomDocument('1.0', 'UTF-8');
434 $xml->formatOutput = true;
435
436 // Create packet
437 $packet = $xml->createElement('packet');
438 $packet->setAttribute('version', '1.6.0.0');
439 $xml->appendChild($packet);
440 $client = $xml->createElement('client');
441 $packet->appendChild($client);
442 $set = $xml->createElement('set');
443 $client->appendChild($set);
444 $filter = $xml->createElement('filter');
445 $set->appendChild($filter);
446 $id = $xml->createElement('id', $clientinfo->id);
447 $filter->appendChild($id);
448 $values = $xml->createElement('values');
449 $set->appendChild($values);
450 $limits = $xml->createElement('limits');
451 $values->appendChild($limits);
452 $resourcepolicy = $xml->createElement('resource-policy');
453 $limits->appendChild($resourcepolicy);
454 $overuse = $xml->createElement('overuse', 'notify');
455 $resourcepolicy->appendChild($overuse);
456
457 return $xml->saveXML();
458 }
459
460 /**
461 * PleskApi::setClientOveruseLimit()
462 *
463 * @param mixed $host
464 * @param mixed $client
465 * @return
466 */
467 public function setClientOveruseLimit($host, $client){
468 $curl = $this->curlInit($host);
469 $packet = $this->clientSetOveruseLimitPacket($client);
470 $response = $this->sendRequest($curl, $packet);
471 if($this->parseResponse($response)){
472 return true;
473 }
474 }
475
476 /**
477 * PleskApi::domainSetOveruseLimitPacket()
478 *
479 * @param mixed $domaininfo
480 * @return
481 */
482 private function domainSetOveruseLimitPacket($domaininfo){
483 // Create new XML document.
484 $xml = new DomDocument('1.0', 'UTF-8');
485 $xml->formatOutput = true;
486
487 // Create packet
488 $packet = $xml->createElement('packet');
489 $packet->setAttribute('version', '1.6.0.0');
490 $xml->appendChild($packet);
491 $domain = $xml->createElement('domain');
492 $packet->appendChild($domain);
493 $set = $xml->createElement('set');
494 $domain->appendChild($set);
495 $filter = $xml->createElement('filter');
496 $set->appendChild($filter);
497 $id = $xml->createElement('id', $domaininfo->id);
498 $filter->appendChild($id);
499 $values = $xml->createElement('values');
500 $set->appendChild($values);
501 $limits = $xml->createElement('limits');
502 $values->appendChild($limits);
503 $overuse = $xml->createElement('overuse', 'notify');
504 $limits->appendChild($overuse);
505
506 return $xml->saveXML();
507 }
508
509 /**
510 * PleskApi::setDomainOveruseLimit()
511 *
512 * @param mixed $host
513 * @param mixed $domain
514 * @return
515 */
516 public function setDomainOveruseLimit($host, $domain){
517 $curl = $this->curlInit($host);
518 $packet = $this->domainSetOveruseLimitPacket($domain);
519 $response = $this->sendRequest($curl, $packet);
520 if($this->parseResponse($response)){
521 return true;
522 }
523 }
524
525 /**
526 * PleskApi::clientAddIpPoolPacket()
527 *
528 * @param mixed $host
529 * @param mixed $clientinfo
530 * @return
531 */
532 private function clientAddIpPoolPacket($host, $clientinfo){
533 // Create new XML document.
534 $xml = new DomDocument('1.0', 'UTF-8');
535 $xml->formatOutput = true;
536
537 // Create packet
538 $packet = $xml->createElement('packet');
539 $packet->setAttribute('version', '1.4.2.0');
540 $xml->appendChild($packet);
541 $client = $xml->createElement('client');
542 $packet->appendChild($client);
543 $ippool_add_ip = $xml->createElement('ippool_add_ip');
544 $client->appendChild($ippool_add_ip);
545 $client_id = $xml->createElement('client_id', $clientinfo->id);
546 $ippool_add_ip->appendChild($client_id);
547 $ip_address = $xml->createElement('ip_address', $host['host_ipaddress']);
548 $ippool_add_ip->appendChild($ip_address);
549 return $xml->saveXML();
550 }
551
552 /**
553 * PleskApi::clientAddIpPool()
554 *
555 * @param mixed $host
556 * @param mixed $client
557 * @return
558 */
559 public function clientAddIpPool($host, $client){
560 $curl = $this->curlInit($host);
561 $packet = $this->clientAddIpPoolPacket($host, $client);
562 $response = $this->sendRequest($curl, $packet);
563 if($this->parseResponse($response)){
564 return true;
565 }
566 }
567
568 /**
569 * PleskApi::clientSetPacket()
570 *
571 * @param mixed $clientinfo
572 * @param mixed $params
573 * @return
574 */
575 private function clientSetPacket($clientinfo, $params){
576 // Create new XML document.
577 $xml = new DomDocument('1.0', 'UTF-8');
578 $xml->formatOutput = true;
579
580 // Create packet element
581 $packet = $xml->createElement('packet');
582
583 // Get host from database and set version as attribute to element packet.
584 $packet->setAttribute('version', '1.4.1.0');
585 $xml->appendChild($packet);
586 $client = $xml->createElement('client');
587 $packet->appendChild($client);
588 $set = $xml->createElement('set');
589 $client->appendChild($set);
590 $filter = $xml->createElement('filter');
591 $set->appendChild($filter);
592 $id = $xml->createElement('id', $clientinfo->id);
593 $filter->appendChild($id);
594 $values = $xml->createElement('values');
595 $set->appendChild($values);
596 foreach($params as $name => $pvalues) {
597 $node = $xml->createElement($name);
598 $values->appendChild($node);
599 foreach ($pvalues as $key => $value) {
600 $xmlelement = $xml->createElement($key, $value);
601 $node->appendChild($xmlelement);
602 }
603 }
604 return $xml->saveXML();
605 }
606
607 /**
608 * PleskApi::setClient()
609 *
610 * @param mixed $host
611 * @param mixed $client
612 * @param mixed $params
613 * @return
614 */
615 public function setClient($host, $client, $params){
616 $curl = $this->curlInit($host);
617 $packet = $this->clientSetPacket($client, $params);
618 $response = $this->sendRequest($curl, $packet);
619 if($this->parseResponse($response)){
620 return true;
621 }
622 }
623
624 /**
625 * PleskApi::domainCreatePacket()
626 *
627 * @param mixed $params
628 * @return
629 */
630 private function domainCreatePacket($params){
631 // Create new XML document.
632 $xml = new DomDocument('1.0', 'UTF-8');
633 $xml->formatOutput = true;
634
635 // Create packet
636 $packet = $xml->createElement('packet');
637 $packet->setAttribute('version', '1.4.2.0');
638 $xml->appendChild($packet);
639 $domain = $xml->createElement('domain');
640 $packet->appendChild($domain);
641 $add = $xml->createElement('add');
642 $domain->appendChild($add);
643
644 // Add general setup and optional parameters
645 foreach($params as $name => $values) {
646 $node = $xml->createElement($name);
647 $add->appendChild($node);
648 if($name == 'hosting'){
649 $node2 = $xml->createElement($params['gen_setup']['htype']);
650 $node->appendChild($node2);
651 $node = $node2;
652 }
653 foreach ($values as $key => $value) {
654 $xmlelement = $xml->createElement($key, $value);
655 $node->appendChild($xmlelement);
656 }
657 }
658 return $xml->saveXML();
659 }
660
661 /**
662 * PleskApi::createDomain()
663 *
664 * @param mixed $host
665 * @param mixed $params
666 * @return
667 */
668 public function createDomain($host, $params){
669 $curl = $this->curlInit($host);
670 $packet = $this->domainCreatePacket($params);
671 $response = $this->sendRequest($curl, $packet);
672 if($this->parseResponse($response)){
673 return true;
674 }
675 }
676
677 /**
678 * PleskApi::domainSetPacket()
679 *
680 * @param mixed $domain
681 * @param mixed $params
682 * @return
683 */
684 private function domainSetPacket($domain, $params){
685 // Create new XML document.
686 $xml = new DomDocument('1.0', 'UTF-8');
687 $xml->formatOutput = true;
688
689 // Create packet
690 $packet = $xml->createElement('packet');
691 $packet->setAttribute('version', '1.4.2.0');
692 $xml->appendChild($packet);
693 $client = $xml->createElement('domain');
694 $packet->appendChild($client);
695 $set = $xml->createElement('set');
696 $client->appendChild($set);
697 $filter = $xml->createElement('filter');
698 $set->appendChild($filter);
699 $domain_name = $xml->createElement('domain_name', $domain->data->gen_info->name);
700 $filter->appendChild($domain_name);
701 $values = $xml->createElement('values');
702 $set->appendChild($values);
703 foreach($params as $name => $pvalues) {
704 $node = $xml->createElement($name);
705 $values->appendChild($node);
706
707 if($name == 'hosting'){
708 $node2 = $xml->createElement($domain->data->gen_info->htype);
709 $node->appendChild($node2);
710 $node = $node2;
711 }
712
713 foreach ($pvalues as $key => $value) {
714 $xmlelement = $xml->createElement($key, $value);
715 $node->appendChild($xmlelement);
716 }
717 }
718 return $xml->saveXML();
719 }
720
721 /**
722 * PleskApi::setDomain()
723 *
724 * @param mixed $host
725 * @param mixed $domain
726 * @param mixed $params
727 * @return
728 */
729 public function setDomain($host, $domain, $params){
730 $curl = $this->curlInit($host);
731 $packet = $this->domainSetPacket($domain, $params);
732 $response = $this->sendRequest($curl, $packet);
733 if($this->parseResponse($response)){
734 return true;
735 }
736 }
737
738 /**
739 * PleskApi::domainSetScriptingPacket()
740 *
741 * @param mixed $domain
742 * @param mixed $params
743 * @return
744 */
745 private function domainSetScriptingPacket($domain, $params){
746 // Create new XML document.
747 $xml = new DomDocument('1.0', 'UTF-8');
748 $xml->formatOutput = true;
749
750 // Create packet
751 $packet = $xml->createElement('packet');
752 $packet->setAttribute('version', '1.5.0.0');
753 $xml->appendChild($packet);
754 $client = $xml->createElement('domain');
755 $packet->appendChild($client);
756 $set = $xml->createElement('set');
757 $client->appendChild($set);
758 $filter = $xml->createElement('filter');
759 $set->appendChild($filter);
760 $domain_name = $xml->createElement('domain_name', $domain->data->gen_info->name);
761 $filter->appendChild($domain_name);
762 $values = $xml->createElement('values');
763 $set->appendChild($values);
764 $hosting = $xml->createElement('hosting');
765 $values->appendChild($hosting);
766 $vrt_hst = $xml->createElement('vrt_hst');
767 $hosting->appendChild($vrt_hst);
768 $property = $xml->createElement('property');
769 $vrt_hst->appendChild($property);
770 $name = $xml->createElement('name', 'php');
771 $property->appendChild($name);
772 $value = $xml->createElement('value', $params['scripting']);
773 $property->appendChild($value);
774 $ip_address = $xml->createElement('ip_address', $params['ip_address']);
775 $vrt_hst->appendChild($ip_address);
776 return $xml->saveXML();
777 }
778
779 /**
780 * PleskApi::setDomainScripting()
781 *
782 * @param mixed $host
783 * @param mixed $domain
784 * @param mixed $params
785 * @return
786 */
787 public function setDomainScripting($host, $domain, $params){
788 $curl = $this->curlInit($host);
789 $packet = $this->domainSetScriptingPacket($domain, $params);
790 $response = $this->sendRequest($curl, $packet);
791 if($this->parseResponse($response)){
792 return true;
793 }
794 }
795
796 /**
797 * PleskApi::domainAliasCreatePacket()
798 *
799 * @param mixed $domain
800 * @param mixed $alias
801 * @return
802 */
803 private function domainAliasCreatePacket($domain, $alias){
804 // Create new XML document.
805 $xml = new DomDocument('1.0', 'UTF-8');
806 $xml->formatOutput = true;
807
808 // Create packet
809 $packet = $xml->createElement('packet');
810 $packet->setAttribute('version', '1.4.2.0');
811 $xml->appendChild($packet);
812 $domain_alias = $xml->createElement('domain_alias');
813 $packet->appendChild($domain_alias);
814 $create = $xml->createElement('create');
815 $domain_alias->appendChild($create);
816 $domain_id = $xml->createElement('domain_id', $domain->id);
817 $create->appendChild($domain_id);
818 $name = $xml->createElement('name', $alias);
819 $create->appendChild($name);
820 return $xml->saveXML();
821 }
822
823 /**
824 * PleskApi::createDomainAlias()
825 *
826 * @param mixed $host
827 * @param mixed $domain
828 * @param mixed $alias
829 * @return
830 */
831 public function createDomainAlias($host, $domain, $alias){
832 $curl = $this->curlInit($host);
833 $packet = $this->domainAliasCreatePacket($domain, $alias);
834 $response = $this->sendRequest($curl, $packet);
835 if($this->parseResponse($response)){
836 return true;
837 }
838 }
839
840 /**
841 * PleskApi::emailCreatePacket()
842 *
843 * @param mixed $domain
844 * @param mixed $params
845 * @return
846 */
847 private function emailCreatePacket($domain, $params){
848 // Create new XML document.
849 $xml = new DomDocument('1.0', 'UTF-8');
850 $xml->formatOutput = true;
851
852 // Create packet
853 $packet = $xml->createElement('packet');
854 $packet->setAttribute('version', '1.4.2.0');
855 $xml->appendChild($packet);
856 $mail = $xml->createElement('mail');
857 $packet->appendChild($mail);
858 $create = $xml->createElement('create');
859 $mail->appendChild($create);
860 $filter = $xml->createElement('filter');
861 $create->appendChild($filter);
862 $domain_id = $xml->createElement('domain_id', $domain->id);
863 $filter->appendChild($domain_id);
864 $mailname = $xml->createElement('mailname');
865 $filter->appendChild($mailname);
866 foreach ($params as $key => $value) {
867 $node = $mailname;
868 if(strpos($key, ':') !== false){
869 $split = explode(':', $key);
870 $key = $split[1];
871 $node = ${$split[0]};
872 if(!isset(${$split[0]})){
873 ${$split[0]} = $xml->createElement($split[0]);
874 $mailname->appendChild(${$split[0]});
875 }
876 $xmlelement = $xml->createElement($key, $value);
877 ${$split[0]}->appendChild($xmlelement);
878 }else{
879 $xmlelement = $xml->createElement($key, $value);
880 $node->appendChild($xmlelement);
881 }
882 }
883 return $xml->saveXML();
884 }
885
886 /**
887 * PleskApi::createEmail()
888 *
889 * @param mixed $host
890 * @param mixed $domain
891 * @param mixed $params
892 * @return
893 */
894 public function createEmail($host, $domain, $params){
895 $curl = $this->curlInit($host);
896 $packet = $this->emailCreatePacket($domain, $params);
897 $response = $this->sendRequest($curl, $packet);
898 if($this->parseResponse($response)){
899 return true;
900 }
901 }
902
903 /**
904 * PleskApi::emailSetPacket()
905 *
906 * @param mixed $domain
907 * @param mixed $params
908 * @return
909 */
910 private function emailSetPacket($domain, $params){
911 // Create new XML document.
912 $xml = new DomDocument('1.0', 'UTF-8');
913 $xml->formatOutput = true;
914
915 // Create packet
916 $packet = $xml->createElement('packet');
917 $packet->setAttribute('version', '1.4.2.0');
918 $xml->appendChild($packet);
919 $mail = $xml->createElement('mail');
920 $packet->appendChild($mail);
921 $create = $xml->createElement('create');
922 $mail->appendChild($create);
923 $filter = $xml->createElement('filter');
924 $create->appendChild($filter);
925 $domain_id = $xml->createElement('domain_id', $domain->id);
926 $filter->appendChild($domain_id);
927 $mailname = $xml->createElement('mailname');
928 $filter->appendChild($mailname);
929
930
931 foreach ($params as $key => $value) {
932 if(strpos($key, 'mailbox:') !== false){
933 $key = substr($key, strpos($key, ':')+1);
934 if(!isset($mailbox)){
935 $mailbox = $xml->createElement('mailbox');
936 $mailname->appendChild($mailbox);
937 }
938 $xmlelement = $xml->createElement($key, $value);
939 $mailbox->appendChild($xmlelement);
940 }else if(strpos($key, 'perm:') !== false){
941 $key = substr($key, strpos($key, ':')+1);
942 if(!isset($permissions)){
943 $permissions = $xml->createElement('permissions');
944 $mailname->appendChild($permissions);
945 }
946 $xmlelement = $xml->createElement($key, $value);
947 $permissions->appendChild($xmlelement);
948 }else{
949 $xmlelement = $xml->createElement($key, $value);
950 $mailname->appendChild($xmlelement);
951 }
952 }
953 return $xml->saveXML();
954 }
955
956 /**
957 * PleskApi::setEmail()
958 *
959 * @param mixed $host
960 * @param mixed $domain
961 * @param mixed $params
962 * @return
963 */
964 public function setEmail($host, $domain, $params){
965 $curl = $this->curlInit($host);
966 $packet = $this->emailCreatePacket($domain, $params);
967 $response = $this->sendRequest($curl, $packet);
968 if($this->parseResponse($response)){
969 return true;
970 }
971 }
972
973}
974
975?>