Rage4.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. <?php
  2. namespace MGModule\DNSManager2\mgLibs\custom\dns\submodules;
  3. use \MGModule\DNSManager2\mgLibs\custom\dns;
  4. use \MGModule\DNSManager2\mgLibs\custom\dns\exceptions;
  5. use \MGModule\DNSManager2\mgLibs\custom\dns\interfaces;
  6. class Rage4 extends dns\SubmoduleAbstract implements
  7. interfaces\SubmoduleRDNSInterface, interfaces\SubmoduleTTLInterface, interfaces\SubmoduleImportInterface
  8. {
  9. public $configFields = array(
  10. 'username' => array(
  11. 'friendlyName' => 'Username',
  12. 'validators' => array(
  13. 'required' => 'required',
  14. )
  15. ),
  16. 'apikey' => array(
  17. 'friendlyName' => 'API Key',
  18. 'validators' => array(
  19. 'required' => 'required',
  20. )
  21. ),
  22. 'priority' => array(
  23. 'friendlyName' => 'Default Priority',
  24. 'validators' => array(
  25. 'required' => 'required',
  26. )
  27. ),
  28. );
  29. public $availableTypes = array(0 => 'SOA', 1 => 'NS', 2 => 'A', 3 => 'AAAA', 4 => 'CNAME', 5 => 'MX', 6 => 'TXT', 7 => 'SRV', 8 => 'PTR', 9 => 'SPF');
  30. private $domainID = false;
  31. private function get($params)
  32. {
  33. $url = 'https://secure.rage4.com/rapi/' . $params[ 'action' ];
  34. unset($params[ 'action' ]);
  35. if (!empty($params))
  36. {
  37. $url .= '/' . (!empty($params[ 'line_id' ]) ? $params[ 'line_id' ] : '') . '?';
  38. unset($params[ 'line_id' ]);
  39. foreach ($params as $k => $v)
  40. {
  41. $url .= $k . '=' . urlencode($v) . '&';
  42. }
  43. }
  44. $url = trim($url, '&');
  45. $ch = curl_init();
  46. curl_setopt($ch, CURLOPT_URL, $url);
  47. curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  48. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  49. curl_setopt($ch, CURLOPT_HEADER, FALSE);
  50. curl_setopt($ch, CURLOPT_TIMEOUT, 60);
  51. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
  52. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  53. curl_setopt($ch, CURLOPT_USERPWD, $this->config[ 'username' ] . ":" . $this->config[ 'apikey' ]);
  54. $result = curl_exec($ch);
  55. if (curl_errno($ch) != 0)
  56. {
  57. throw new exceptions\DNSSubmoduleException('cURL error: ' . (curl_error($ch) ?: 'Unknown Error'), dns\SubmoduleExceptionCodes::CONNECTION_PROBLEM);
  58. }
  59. if (empty($result) || strpos($result, 'Object moved'))
  60. {
  61. throw new exceptions\DNSSubmoduleException('Invalid username or Password', dns\SubmoduleExceptionCodes::CONNECTION_PROBLEM);
  62. }
  63. curl_close($ch);
  64. $result = json_decode($result, true);
  65. if ($result === false || !is_array($result))
  66. {
  67. throw new exceptions\DNSSubmoduleException('Cannot parse response', dns\SubmoduleExceptionCodes::INVALID_RESPONSE);
  68. }
  69. if (!empty($result[ 'error' ]))
  70. {
  71. throw new exceptions\DNSSubmoduleException($result[ 'error' ], dns\SubmoduleExceptionCodes::COMMAND_ERROR);
  72. }
  73. return $result;
  74. }
  75. public function testConnection()
  76. {
  77. $params = array(
  78. 'action' => 'GetDomains',
  79. );
  80. $this->get($params);
  81. return true;
  82. }
  83. public function zoneExists()
  84. {
  85. try
  86. {
  87. $this->domain_id();
  88. return true;
  89. }
  90. catch (exceptions\DNSSubmoduleException $e)
  91. {
  92. if ($e->getCode() == dns\SubmoduleExceptionCodes::COMMAND_ERROR)
  93. {
  94. return false;
  95. }
  96. throw $e;
  97. }
  98. }
  99. public function activateZone()
  100. {
  101. $is_ipv6 = preg_match('/.ip6.arpa$/', $this->domain);
  102. $ex = explode('.', $this->domain);
  103. if (end($ex) == 'arpa')
  104. {
  105. $params = array(
  106. 'action' => $is_ipv6 ? 'CreateReverseDomain6' : 'CreateReverseDomain4',
  107. 'name' => $is_ipv6 ? '0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.' . $this->domain : $this->domain,
  108. 'subnet' => $is_ipv6 ? 64 : 8,
  109. 'email' => $this->config[ 'username' ]
  110. );
  111. }
  112. else
  113. {
  114. $params = array(
  115. 'action' => 'CreateRegularDomain',
  116. 'name' => $this->domain,
  117. 'email' => $this->config[ 'username' ]
  118. );
  119. }
  120. $result = $this->get($params, true);
  121. // $current_records = $this->getRecords();
  122. // foreach($current_records as $r)
  123. // if($r['type'] == 'NS')
  124. // $this->delete(array($r));
  125. }
  126. public function terminateZone()
  127. {
  128. $params = array(
  129. 'id' => $this->domain_id(),
  130. 'action' => 'DeleteDomain'
  131. );
  132. $this->get($params);
  133. }
  134. public function getRecords($recordType = false)
  135. {
  136. $params = array(
  137. 'action' => 'GetRecords',
  138. 'id' => $this->domain_id(),
  139. 'name' => $this->domain
  140. );
  141. $result = $this->get($params);
  142. $out = array();
  143. foreach ($result as $k => $r)
  144. {
  145. if (in_array((string) $r[ 'type' ], $recordType !== false ? array(strtoupper($recordType)) : $this->getAvailableRecordTypes()))
  146. {
  147. $record = new dns\record\Record();
  148. $record->line = $r[ 'id' ];
  149. $record->name = $r[ 'name' ];
  150. $record->type = (string) $r[ 'type' ];
  151. $record->ttl = intval((string) $r[ 'ttl' ]);
  152. $record->createRDATAObject();
  153. switch ($record[ 'type' ])
  154. {
  155. case 'MX':
  156. $record->rdata->exchange = (string) $r[ 'content' ];
  157. $record->rdata->preference = (string) $r[ 'priority' ];
  158. break;
  159. case 'SRV':
  160. $r[ 'content' ] = $r[ 'priority' ] . ' ' . (string) $r[ 'content' ];
  161. $record->rdata->fromString((string) $r[ 'content' ]);
  162. break;
  163. default:
  164. $record->rdata->fromString((string) $r[ 'content' ]);
  165. break;
  166. }
  167. $out[] = $record;
  168. }
  169. }
  170. return $out;
  171. }
  172. private function recordToParamsArray(dns\record\Record $record)
  173. {
  174. $type_int = array_flip($this->availableTypes);
  175. $params = array(
  176. 'line_id' => $this->domain_id(),
  177. 'name' => $record->nameToAbsolute($this->domain, false),
  178. 'type' => $type_int[ $record->type ],
  179. 'content' => $record->value,
  180. 'ttl' => $record->ttl,
  181. 'priority' => empty($record->priority) ? $this->config[ 'priority' ] : $record->priority,
  182. 'failover' => 'false',
  183. );
  184. switch ($record->type)
  185. {
  186. case 'MX':
  187. $prio = $record->rdata->preference;
  188. $value = $record->rdata->exchange;
  189. break;
  190. default:
  191. $prio = $this->config[ 'priority' ];
  192. $value = $record->rdata->toString();
  193. break;
  194. }
  195. $params[ 'priority' ] = $prio;
  196. $params[ 'content' ] = $value;
  197. return $params;
  198. }
  199. private function setSRVData(&$record)
  200. {
  201. if ($record->type == 'SRV')
  202. {
  203. #quick fix
  204. $priority = $record->rdata->priority;
  205. unset($record->rdata->priority);
  206. return $priority;
  207. }
  208. }
  209. public function addRecord(dns\record\Record $record)
  210. {
  211. $priority = $this->setSRVData($record);
  212. $params = $this->recordToParamsArray($record);
  213. $params[ 'failovercontent' ] = '';
  214. if (!is_null($priority))
  215. {
  216. $params[ 'priority' ] = $priority;
  217. }
  218. $params[ 'action' ] = 'CreateRecord';
  219. $this->get($params);
  220. }
  221. public function editRecord(dns\record\Record $record)
  222. {
  223. $priority = $this->setSRVData($record);
  224. $params = $this->recordToParamsArray($record);
  225. $params[ 'action' ] = 'UpdateRecord';
  226. $params[ 'line_id' ] = $params[ 'id' ] = $record->line;
  227. if (!is_null($priority))
  228. {
  229. $params[ 'priority' ] = $priority;
  230. }
  231. $this->get($params);
  232. }
  233. public function deleteRecord(dns\record\Record $record)
  234. {
  235. $params = array('id' => $record->line, 'action' => 'DeleteRecord');
  236. $this->get($params);
  237. }
  238. private function domain_id()
  239. {
  240. if ($this->domainID !== FALSE)
  241. {
  242. return $this->domainID;
  243. }
  244. $ret = $this->get(array('action' => 'getdomainbyname', 'name' => $this->domain));
  245. if (isset($ret[ 'id' ]))
  246. {
  247. $this->domainID = $ret[ 'id' ];
  248. return $this->domainID;
  249. }
  250. throw new exceptions\DNSSubmoduleException("Zone does not exist", dns\SubmoduleExceptionCodes::COMMAND_ERROR);
  251. }
  252. public function getZones()
  253. {
  254. $params = array(
  255. 'action' => 'GetDomains',
  256. );
  257. $ret = $this->get($params);
  258. $out = array();
  259. foreach ($ret as $domain)
  260. {
  261. $out[ (string) $domain[ 'name' ] ] = '';
  262. }
  263. return $out;
  264. }
  265. }