array ( 'friendlyName' => 'Environment', 'type' => 'select', 'options' => array('LIVE' => 'LIVE', 'TEST' => 'TEST'), 'validators' => array( 'required' => 'required', ), ), 'username' =>array ( 'friendlyName' => 'Username', 'validators' => array( 'required' => 'required', ), ), 'apikey' =>array ( 'friendlyName' => 'API Key', 'type' => 'password', 'validators' => array( 'required' => 'required', ), ), 'default_ip' =>array ( 'friendlyName' => 'Default IP', 'validators' => array( 'pattern' => Patterns::IP4_OR_IP6, ) ), ); public $availableTypes = array('A', 'NS', 'MX', 'CNAME', 'TXT', 'AAAA', 'SRV'); //NS?! public function setConfiguration($config) { parent::setConfiguration($config); if(!isset($this->config['environment'])) { $this->config['environment'] = self::ENVIRONMENT; } $this->config['protocol'] = self::PROTOCOL; $this->config['host'] = $this->config['environment'] == 'LIVE' ? self::LIVE_HOST : self::TEST_HOST; } private function get($function, $params, $rdata = array()) { $filename = dirname(__FILE__).DIRECTORY_SEPARATOR."opensrslib".DIRECTORY_SEPARATOR."openSRS_loader.php"; if(!file_exists($filename)){ throw new exceptions\DNSSubmoduleException('Unable to load openSRS_loader.php file', dns\SubmoduleExceptionCodes::CONNECTION_PROBLEM); } require_once ($filename); $callArray = array ( "func" => $function, "data" => $params ); $callstring = json_encode($callArray); $reporting = error_reporting(); $ini = ini_get('display_errors'); error_reporting(E_USER_WARNING | E_USER_ERROR); ini_set('display_errors', 'On'); //bo zgłaszanie wyjatków najelpiej robić za pomocą trigger_error -.- set_error_handler(function($errno, $errstr, $errfile, $errline, array $errcontext) { if($errno != E_USER_WARNING && $errno != E_USER_ERROR) { return true; } // error was suppressed with the @-operator if (0 === error_reporting()) { return false; } error_reporting($reporting); ini_set('display_errors', $ini); throw new exceptions\DNSSubmoduleException($errstr, dns\SubmoduleExceptionCodes::CONNECTION_PROBLEM); }); $osrsHandler = \DNSManager\opensrs\processOpenSRS ("json", $callstring, $this->config, $rdata); error_reporting($reporting); ini_set('display_errors', $ini); restore_error_handler(); $return = $osrsHandler->resultRaw; if(!$return) { throw new exceptions\DNSSubmoduleException('Unknown error (0)', dns\SubmoduleExceptionCodes::INVALID_RESPONSE); } if($return['is_success'] != '1') { if(strpos($return['response_text'], 'Authentication Error') !== FALSE) { throw new exceptions\DNSSubmoduleException('Domain is not registered on the server. Please contact administrator.', dns\SubmoduleExceptionCodes::COMMAND_ERROR); } throw new exceptions\DNSSubmoduleException($return['response_text']?:'Unknown error (0)', dns\SubmoduleExceptionCodes::COMMAND_ERROR); } return $return; } public function testConnection() { try { $this->get('dnsLookup', array('domain' => 'google.com')); } catch (exceptions\DNSSubmoduleException $e) { if(preg_match('/Unable to find domain a/', $e->getMessage())) { return true; } throw $e; } } public function zoneExists() { try { $res = $this->get('dnsGet', array( 'domain' => $this->domain )); if(isset($res['attributes'])) return true; } catch (exceptions\DNSSubmoduleException $e) { if($e->getCode() == dns\SubmoduleExceptionCodes::COMMAND_ERROR) { return false; } throw $e; } return false; } public function getRecords($recordType = false) { $res = $this->get('dnsGet', array( 'domain' => $this->domain )); $out = array(); $line = 0; foreach($res['attributes']['records'] as $type => $records) { foreach($records as $r) { if(in_array($type, $recordType!==false ? array(strtoupper($recordType)) : $this->getAvailableRecordTypes())) { $record = new dns\record\Record(); $record->line = $line; $record->name = $r['subdomain']; $record->type = strtoupper($type); $record->createRDATAObject(); switch (strtoupper($type)) { case 'A': $record->rdata->setFirstProperty($r['ip_address']); break; case 'AAAA': $record->rdata->setFirstProperty($r['ipv6_address']); break; case 'MX': $record->rdata->preference = $r['priority']; $record->rdata->exchange = $r['hostname']; break; case 'CNAME': $record->rdata->setFirstProperty($r['hostname']); break; case 'TXT': $record->rdata->fromString($r['text']); break; case 'SRV': $record->rdata->priority = $r['priority']; $record->rdata->weight = $r['weight']; $record->rdata->port = $r['port']; $record->rdata->target = $r['hostname']; break; } $line++; $out[] = $record; } } } return $out; } public function recordToParamsArray(dns\record\Record $record) { $params['subdomain'] = $record->nameToRelative($this->domain); switch($record->type) { case 'A': $params['ip_address'] = $record->rdata->toString(); break; case 'AAAA': $params['ipv6_address'] = $record->rdata->toString(); break; case 'MX': $params['hostname'] = $record->rdata->exchange; $params['priority'] = $record->rdata->preference; break; case 'CNAME': $params['hostname'] = $record->rdata->toString(); break; case 'TXT': $params['text'] = $record->rdata->toString(); break; case 'SRV': $params['priority'] = $record->rdata->priority; $params['weight'] = $record->rdata->weight; $params['port'] = $record->rdata->port; $params['hostname'] = $record->rdata->target; break; } return $params; } public function addRecord(dns\record\Record $record) { $ex_records = $this->getRecords(); $ex_records[] = $record; $records = array(); foreach($ex_records as $r) { $records[$r->type][] = $this->recordToParamsArray($r); } $res = $this->get('dnsSet', array( 'domain' => $this->domain, ), $records); } public function editRecord(dns\record\Record $record) { $ex_records = $this->getRecords(); $records = array(); foreach($ex_records as $r) { if($r->line == $record->line) { $records[$r->type][] = $this->recordToParamsArray($record); } else { $records[$r->type][] = $this->recordToParamsArray($r); } } $res = $this->get('dnsSet', array( 'domain' => $this->domain, ), $records); } public function deleteRecord(dns\record\Record $record) { $ex_records = $this->getRecords(); $records = array(); foreach($ex_records as $r) { if($r->line != $record->line) { $records[$r->type][] = $this->recordToParamsArray($r); } } $res = $this->get('dnsSet', array( 'domain' => $this->domain, ), $records); } public function activateZone() { $this->get('dnsCreate', array( 'domain' => $this->domain ), array()); } public function terminateZone() { $this->get('dnsDelete', array( 'domain' => $this->domain )); } }