| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282 |
- <?php
- namespace MGModule\DNSManager2\mgLibs\custom\dns\submodules;
- use \MGModule\DNSManager2\mgLibs\custom\dns;
- use \MGModule\DNSManager2\mgLibs\custom\dns\interfaces;
- use \MGModule\DNSManager2\mgLibs\custom\dns\exceptions;
- use \MGModule\DNSManager2\mgLibs\custom\dns\utils\Patterns;
- class OpenSRS extends dns\SubmoduleAbstract {
- const ENVIRONMENT = 'TEST';
- const PROTOCOL = 'XCP';
- const LIVE_HOST = 'rr-n1-tor.opensrs.net';
- const TEST_HOST = 'horizon.opensrs.net';
-
- public $configFields = array(
- 'environment' =>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
- ));
- }
- }
|