| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273 |
- <?php
- namespace MGModule\DNSManager2\mgLibs\custom\dns\submodules;
- use \MGModule\DNSManager2\mgLibs\custom\dns;
- use \MGModule\DNSManager2\mgLibs\custom\dns\exceptions;
- use \MGModule\DNSManager2\mgLibs\custom\dns\interfaces;
- use \MGModule\DNSManager2\mgLibs\custom\dns\utils\Patterns;
- //TODO: wersja dla najnowszego whma
- class CPanelOldApi extends dns\SubmoduleAbstract implements interfaces\SubmoduleIPInterface, interfaces\SubmoduleRDNSInterface, interfaces\SubmoduleTTLInterface, interfaces\SubmoduleImportInterface {
- public $configFields = array(
- 'username' =>array (
- 'friendlyName' => 'Username',
- 'validators' => array(
- 'required' => 'required',
- )
- ),
- 'password' =>array (
- 'friendlyName' => 'User password',
- 'type'=> 'password',
- ),
- 'hash' =>array (
- 'friendlyName' => 'Access key',
- 'type'=> 'textarea',
- ),
- 'hostname' =>array (
- 'friendlyName' => 'Hostname/IP',
- 'validators' => array(
- 'required' => 'required',
- )
- ),
- 'ssl' =>array (
- 'friendlyName' => 'Enable SSL',
- 'type'=> 'yesno',
- ),
- 'default_ip' =>array (
- 'friendlyName' => 'Default IP',
- 'validators' => array(
- 'required' => 'required',
- 'pattern' => Patterns::IP4_OR_IP6,
- )
- ),
- 'allow_create' =>array (
- 'friendlyName' => 'In cPanel Zones',
- 'type' => 'yesno',
- 'help' => 'Allow to create zones when already in CPanel as account',
- ),
- 'template' =>array (
- 'friendlyName' => 'Template',
- ),
- );
-
- public $availableTypes = array('A', 'AAAA', 'NS', 'MX', 'CNAME', 'DNAME', 'LOC', 'TXT', 'SRV', 'PTR', 'AFSDB', 'NAPTR', 'RP');
-
- public function testConnection() {
- try {
- $this->get('listaccts');
- return true;
- } catch (exceptions\DNSSubmoduleException $e) {
- $this->get('listzones', array(), true);
- return true;
- }
- }
-
- public function getRecords($recordType = false) {
- $out = $this->get('dumpzone', array(
- 'domain' => $this->domain
- ));
- $return = array();
- if(isset($out->result->record)){
- foreach($out->result->record as $r) {
- if(in_array((string)$r->type, $recordType!==false ? array(strtoupper($recordType)) : $this->getAvailableRecordTypes())) {
- $return[] = dns\record\Record::tryToCreateFromArray((array)$r);
- }
- }
- } else {
- throw new exceptions\DNSSubmoduleException('Unable to fetch records from server', dns\SubmoduleExceptionCodes::COMMAND_ERROR);
- }
- return $return;
- }
-
- public function addRecord(dns\record\Record $record) {
- $input = $record->toMergedArray(false);
- $input['domain'] = $this->domain;
- $input['name'] = $record->nameToAbsolute($this->domain);
-
- $this->get('addzonerecord', $input);
- return true;
- }
-
- public function editRecord(dns\record\Record $record) {
- $records = $this->getRecords();
- if(empty($records))
- return false;
-
- $lines = array();
- foreach($records as $r) {
- $lines[$r['line']] = $r;
- }
- if(!isset($lines[$record['line']]) || !$record->isEqualTo($lines[$record['line']])) {
- $input = $record->toMergedArray(false);
- $input['domain'] = $this->domain;
- $input['name'] = $record->nameToAbsolute($this->domain);
- $this->get('editzonerecord', $input);
- }
- }
-
- public function deleteRecord(dns\record\Record $record) {
- $this->get('removezonerecord', array(
- 'zone' => $this->domain,
- 'line' => $record['line']
- ));
- }
-
- public function zoneExists() {
- try {
- $out = $this->get('listzones', array() , true);
- } catch (exceptions\DNSSubmoduleException $e) {
- if($e->getCode() == dns\SubmoduleExceptionCodes::COMMAND_ERROR) {
- return false;
- }
- throw $e;
- }
-
- if(isset($out->zone)) {
- $k=0;
- while($out->zone[$k]) {
- if((string)$out->zone[$k]->domain == $this->domain)
- return true;
- $k++;
- }
- }
- return false;
- }
-
- public function activateZone() {
- if($this->ip != '') {
- if(!filter_var($this->ip, FILTER_VALIDATE_IP)) {
- throw new exceptions\DNSSubmoduleException('IP is not valid!', dns\SubmoduleExceptionCodes::INVALID_PARAMETERS);
- }
- } else {
- $this->ip = $this->config['default_ip'];
- }
- $params = array(
- 'domain' => $this->domain,
- 'ip' => $this->ip
- );
- if($this->config['template'] != "")
- $params['template'] = $this->config['template'];
-
- $this->get('adddns', $params);
- }
-
- public function terminateZone() {
- $this->get('killdns', array(
- 'domain' => $this->domain
- ));
- }
-
- public function getZones() {
- $out = array();
-
- $res = $this->get('listzones', array(), true);
- foreach ($res->zone as $zone) {
- $out[(string)$zone->domain] = '';
- }
-
- try {
- $res = $this->get('listaccts', array('want' => 'domain,ip'), true);
-
- foreach ($res->acct as $zone) {
- $out[(string)$zone->domain] = (string)$zone->ip;
- }
- } catch (exceptions\DNSSubmoduleException $e) {
-
- }
- return $out;
- }
-
- private function get($function, $params=array(), $nothing = false) {
- $url = ($this->config['ssl']? 'https://'.$this->config['hostname'].':2087' : 'http://'.$this->config['hostname'].':2086').'/xml-api/'.$function;
- $ch = curl_init();
- if(is_array($params)) {
- $url .= '?';
- foreach($params as $key=>$value) {
- $value = urlencode($value);
- $url .= "{$key}={$value}&";
- }
- }
- $url = trim($url, '&');
-
- $this->log($url);
-
- $chOptions = array (
- CURLOPT_URL => $url,
- CURLOPT_RETURNTRANSFER => true,
- CURLOPT_SSL_VERIFYPEER => false,
- CURLOPT_SSL_VERIFYHOST => false,
- CURLOPT_TIMEOUT => 50
- );
-
- if($this->config['hash'] != ''){
- $header[0] = 'Authorization: WHM '.$this->config['username'].':'.str_replace(array("\r", "\n"),"", $this->config['hash']);
- $chOptions[CURLOPT_HTTPHEADER] = $header;
-
- }elseif($this->config['password'] != ''){
- $chOptions[CURLOPT_USERPWD] = $this->config['username'].':'.$this->config['password'];
- }else{
- throw new exceptions\DNSSubmoduleException('Password or Access key is required', dns\SubmoduleExceptionCodes::CONNECTION_PROBLEM);
- }
-
- curl_setopt_array($ch, $chOptions);
- $out = curl_exec($ch);
-
- //cpanel sometimes appends the following text to the ned of response XML: <!-- Web Host Manager 11.54.0.17 (c) cPanel, Inc. 2015 http://cpanel.net/ Unauthorized copying is prohibited. -->
- if(($tagPos = strpos($out, '<!--')) > 0){
- $out = trim(substr($out, 0, $tagPos));
- }
-
- $this->log('SERVER RETURNS: ' . $out);
-
- $out_info = curl_getinfo($ch);
- if($out_info['http_code'] != 200){
- if($out_info['http_code'] == 301 || $out_info['http_code'] == 302){
- throw new exceptions\DNSSubmoduleException('Module require SSL', dns\SubmoduleExceptionCodes::CONNECTION_PROBLEM);
- }
- }
-
- if($out === false || $out == '') {
- throw new exceptions\DNSSubmoduleException(ucwords(curl_error($ch)), dns\SubmoduleExceptionCodes::CONNECTION_PROBLEM);
- }
-
- curl_close($ch);
- if(strpos($out, 'SSL encryption is required for access to this server') !== FALSE) {
- throw new exceptions\DNSSubmoduleException('SSL encryption is required for access to this server', dns\SubmoduleExceptionCodes::CONNECTION_PROBLEM);
- }
- @$a = simplexml_load_string($out);
-
- if($a===FALSE) {
- throw new exceptions\DNSSubmoduleException('Unable to parse response', dns\SubmoduleExceptionCodes::INVALID_RESPONSE);
- } else {
- if(isset($a->status)){
- if($a->status == 1)
- return $a;
- else{
- throw new exceptions\DNSSubmoduleException($a->statusmsg, dns\SubmoduleExceptionCodes::COMMAND_ERROR);
-
- }
- }elseif(isset($a->data->reason)){
- throw new exceptions\DNSSubmoduleException($a->data->reason, dns\SubmoduleExceptionCodes::COMMAND_ERROR);
- }elseif((integer)$a->result->status == 1)
- return $a;
- elseif(isset($a->result->statusmsg)){
- if(preg_match('/is owned by another user/', $a->result->statusmsg) && $this->config['allow_create'] == 'on')// && $this->checkZoneInDB() === false)
- return true;
- else
- throw new exceptions\DNSSubmoduleException($a->result->statusmsg, dns\SubmoduleExceptionCodes::COMMAND_ERROR);
- }
- elseif(isset($a->statusmsg))
- throw new exceptions\DNSSubmoduleException($a->statusmsg, dns\SubmoduleExceptionCodes::COMMAND_ERROR);
- elseif($nothing)
- return $a;
- else
- throw new exceptions\DNSSubmoduleException('Unknown error', dns\SubmoduleExceptionCodes::INVALID_RESPONSE);
- }
- }
- }
|