OpenSRS.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. <?php
  2. namespace MGModule\DNSManager2\mgLibs\custom\dns\submodules;
  3. use \MGModule\DNSManager2\mgLibs\custom\dns;
  4. use \MGModule\DNSManager2\mgLibs\custom\dns\interfaces;
  5. use \MGModule\DNSManager2\mgLibs\custom\dns\exceptions;
  6. use \MGModule\DNSManager2\mgLibs\custom\dns\utils\Patterns;
  7. class OpenSRS extends dns\SubmoduleAbstract {
  8. const ENVIRONMENT = 'TEST';
  9. const PROTOCOL = 'XCP';
  10. const LIVE_HOST = 'rr-n1-tor.opensrs.net';
  11. const TEST_HOST = 'horizon.opensrs.net';
  12. public $configFields = array(
  13. 'environment' =>array (
  14. 'friendlyName' => 'Environment',
  15. 'type' => 'select',
  16. 'options' => array('LIVE' => 'LIVE', 'TEST' => 'TEST'),
  17. 'validators' => array(
  18. 'required' => 'required',
  19. ),
  20. ),
  21. 'username' =>array (
  22. 'friendlyName' => 'Username',
  23. 'validators' => array(
  24. 'required' => 'required',
  25. ),
  26. ),
  27. 'apikey' =>array (
  28. 'friendlyName' => 'API Key',
  29. 'type' => 'password',
  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', 'NS', 'MX', 'CNAME', 'TXT', 'AAAA', 'SRV'); //NS?!
  42. public function setConfiguration($config) {
  43. parent::setConfiguration($config);
  44. if(!isset($this->config['environment'])) {
  45. $this->config['environment'] = self::ENVIRONMENT;
  46. }
  47. $this->config['protocol'] = self::PROTOCOL;
  48. $this->config['host'] = $this->config['environment'] == 'LIVE' ? self::LIVE_HOST : self::TEST_HOST;
  49. }
  50. private function get($function, $params, $rdata = array()) {
  51. $filename = dirname(__FILE__).DIRECTORY_SEPARATOR."opensrslib".DIRECTORY_SEPARATOR."openSRS_loader.php";
  52. if(!file_exists($filename)){
  53. throw new exceptions\DNSSubmoduleException('Unable to load openSRS_loader.php file', dns\SubmoduleExceptionCodes::CONNECTION_PROBLEM);
  54. }
  55. require_once ($filename);
  56. $callArray = array (
  57. "func" => $function,
  58. "data" => $params
  59. );
  60. $callstring = json_encode($callArray);
  61. $reporting = error_reporting();
  62. $ini = ini_get('display_errors');
  63. error_reporting(E_USER_WARNING | E_USER_ERROR);
  64. ini_set('display_errors', 'On');
  65. //bo zgłaszanie wyjatków najelpiej robić za pomocą trigger_error -.-
  66. set_error_handler(function($errno, $errstr, $errfile, $errline, array $errcontext) {
  67. if($errno != E_USER_WARNING && $errno != E_USER_ERROR) {
  68. return true;
  69. }
  70. // error was suppressed with the @-operator
  71. if (0 === error_reporting()) {
  72. return false;
  73. }
  74. error_reporting($reporting);
  75. ini_set('display_errors', $ini);
  76. throw new exceptions\DNSSubmoduleException($errstr, dns\SubmoduleExceptionCodes::CONNECTION_PROBLEM);
  77. });
  78. $osrsHandler = \DNSManager\opensrs\processOpenSRS ("json", $callstring, $this->config, $rdata);
  79. error_reporting($reporting);
  80. ini_set('display_errors', $ini);
  81. restore_error_handler();
  82. $return = $osrsHandler->resultRaw;
  83. if(!$return) {
  84. throw new exceptions\DNSSubmoduleException('Unknown error (0)', dns\SubmoduleExceptionCodes::INVALID_RESPONSE);
  85. }
  86. if($return['is_success'] != '1') {
  87. if(strpos($return['response_text'], 'Authentication Error') !== FALSE) {
  88. throw new exceptions\DNSSubmoduleException('Domain is not registered on the server. Please contact administrator.', dns\SubmoduleExceptionCodes::COMMAND_ERROR);
  89. }
  90. throw new exceptions\DNSSubmoduleException($return['response_text']?:'Unknown error (0)', dns\SubmoduleExceptionCodes::COMMAND_ERROR);
  91. }
  92. return $return;
  93. }
  94. public function testConnection() {
  95. try {
  96. $this->get('dnsLookup', array('domain' => 'google.com'));
  97. } catch (exceptions\DNSSubmoduleException $e) {
  98. if(preg_match('/Unable to find domain a/', $e->getMessage())) {
  99. return true;
  100. }
  101. throw $e;
  102. }
  103. }
  104. public function zoneExists() {
  105. try {
  106. $res = $this->get('dnsGet', array(
  107. 'domain' => $this->domain
  108. ));
  109. if(isset($res['attributes'])) return true;
  110. } catch (exceptions\DNSSubmoduleException $e) {
  111. if($e->getCode() == dns\SubmoduleExceptionCodes::COMMAND_ERROR) {
  112. return false;
  113. }
  114. throw $e;
  115. }
  116. return false;
  117. }
  118. public function getRecords($recordType = false) {
  119. $res = $this->get('dnsGet', array(
  120. 'domain' => $this->domain
  121. ));
  122. $out = array();
  123. $line = 0;
  124. foreach($res['attributes']['records'] as $type => $records) {
  125. foreach($records as $r) {
  126. if(in_array($type, $recordType!==false ? array(strtoupper($recordType)) : $this->getAvailableRecordTypes())) {
  127. $record = new dns\record\Record();
  128. $record->line = $line;
  129. $record->name = $r['subdomain'];
  130. $record->type = strtoupper($type);
  131. $record->createRDATAObject();
  132. switch (strtoupper($type)) {
  133. case 'A':
  134. $record->rdata->setFirstProperty($r['ip_address']);
  135. break;
  136. case 'AAAA':
  137. $record->rdata->setFirstProperty($r['ipv6_address']);
  138. break;
  139. case 'MX':
  140. $record->rdata->preference = $r['priority'];
  141. $record->rdata->exchange = $r['hostname'];
  142. break;
  143. case 'CNAME':
  144. $record->rdata->setFirstProperty($r['hostname']);
  145. break;
  146. case 'TXT':
  147. $record->rdata->fromString($r['text']);
  148. break;
  149. case 'SRV':
  150. $record->rdata->priority = $r['priority'];
  151. $record->rdata->weight = $r['weight'];
  152. $record->rdata->port = $r['port'];
  153. $record->rdata->target = $r['hostname'];
  154. break;
  155. }
  156. $line++;
  157. $out[] = $record;
  158. }
  159. }
  160. }
  161. return $out;
  162. }
  163. public function recordToParamsArray(dns\record\Record $record) {
  164. $params['subdomain'] = $record->nameToRelative($this->domain);
  165. switch($record->type) {
  166. case 'A':
  167. $params['ip_address'] = $record->rdata->toString();
  168. break;
  169. case 'AAAA':
  170. $params['ipv6_address'] = $record->rdata->toString();
  171. break;
  172. case 'MX':
  173. $params['hostname'] = $record->rdata->exchange;
  174. $params['priority'] = $record->rdata->preference;
  175. break;
  176. case 'CNAME':
  177. $params['hostname'] = $record->rdata->toString();
  178. break;
  179. case 'TXT':
  180. $params['text'] = $record->rdata->toString();
  181. break;
  182. case 'SRV':
  183. $params['priority'] = $record->rdata->priority;
  184. $params['weight'] = $record->rdata->weight;
  185. $params['port'] = $record->rdata->port;
  186. $params['hostname'] = $record->rdata->target;
  187. break;
  188. }
  189. return $params;
  190. }
  191. public function addRecord(dns\record\Record $record) {
  192. $ex_records = $this->getRecords();
  193. $ex_records[] = $record;
  194. $records = array();
  195. foreach($ex_records as $r) {
  196. $records[$r->type][] = $this->recordToParamsArray($r);
  197. }
  198. $res = $this->get('dnsSet', array(
  199. 'domain' => $this->domain,
  200. ), $records);
  201. }
  202. public function editRecord(dns\record\Record $record) {
  203. $ex_records = $this->getRecords();
  204. $records = array();
  205. foreach($ex_records as $r) {
  206. if($r->line == $record->line) {
  207. $records[$r->type][] = $this->recordToParamsArray($record);
  208. } else {
  209. $records[$r->type][] = $this->recordToParamsArray($r);
  210. }
  211. }
  212. $res = $this->get('dnsSet', array(
  213. 'domain' => $this->domain,
  214. ), $records);
  215. }
  216. public function deleteRecord(dns\record\Record $record) {
  217. $ex_records = $this->getRecords();
  218. $records = array();
  219. foreach($ex_records as $r) {
  220. if($r->line != $record->line) {
  221. $records[$r->type][] = $this->recordToParamsArray($r);
  222. }
  223. }
  224. $res = $this->get('dnsSet', array(
  225. 'domain' => $this->domain,
  226. ), $records);
  227. }
  228. public function activateZone() {
  229. $this->get('dnsCreate', array(
  230. 'domain' => $this->domain
  231. ), array());
  232. }
  233. public function terminateZone() {
  234. $this->get('dnsDelete', array(
  235. 'domain' => $this->domain
  236. ));
  237. }
  238. }