DNSPod.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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 DNSPod extends dns\SubmoduleAbstract implements interfaces\SubmoduleTTLInterface, interfaces\SubmoduleImportInterface {
  7. const API_URL_CHINESE = 'dnsapi.cn';
  8. const API_URL_ENGLISH = 'api.dnspod.com';
  9. public $configFields = array(
  10. 'auth_type' => array (
  11. 'friendlyName' => 'Account Is',
  12. 'type' => 'select',
  13. 'options' => array('ch' => 'Chinese', 'en' => 'English'),
  14. ),
  15. 'username' => array (
  16. 'friendlyName' => 'Email / Token ID',
  17. 'validators' => array(
  18. 'required' => 'required',
  19. ),
  20. 'help' => 'Email for English account or Token ID for Chinese account'
  21. ),
  22. 'password' => array (
  23. 'friendlyName' => 'Password / Token',
  24. 'type' => 'password',
  25. 'validators' => array(
  26. 'required' => 'required',
  27. ),
  28. 'help' => 'Password for English account or Token for Chinese account'
  29. ),
  30. 'ssl' => array(
  31. 'friendlyName' => 'Enable SSL',
  32. 'type' => 'yesno'
  33. )
  34. );
  35. private $domainid = false;
  36. private $record_line = 'default';
  37. private $record_line_get = false;
  38. private $grade = false;
  39. public $availableTypes = array('A', 'AAAA', 'NS', 'MX', 'CNAME', 'TXT', 'SRV');
  40. private function get($function, $params = array()) {
  41. $url = ($this->config['ssl'] == 'on' ? 'https://' : 'http://').($this->config['auth_type'] == 'ch' ? self::API_URL_CHINESE : self::API_URL_ENGLISH).'/';
  42. if($this->config['auth_type'] == 'en') {
  43. $post = 'login_email='.$this->config['username'].'&login_password='.$this->config['password'].'&format=json';
  44. $ch = curl_init();
  45. $chOptions = array (
  46. CURLOPT_URL => $url.'Auth',
  47. CURLOPT_RETURNTRANSFER => true,
  48. CURLOPT_SSL_VERIFYPEER => false,
  49. CURLOPT_SSL_VERIFYHOST => false,
  50. CURLOPT_TIMEOUT => 30,
  51. CURLOPT_USERAGENT => 'MJJ DDNS Client/1.0.0 ('.$this->config['username'].')',
  52. CURLOPT_POSTFIELDS => $post
  53. );
  54. curl_setopt_array($ch, $chOptions);
  55. $out = curl_exec($ch);
  56. $ch_err = curl_error($ch);
  57. curl_close($ch);
  58. if($out === false || !empty($ch_err)) {
  59. throw new exceptions\DNSSubmoduleException(empty($ch_err) ? 'Unknown API Error' : $ch_err, dns\SubmoduleExceptionCodes::CONNECTION_PROBLEM);
  60. }
  61. $ret = json_decode($out, true);
  62. if(empty($ret['user_token'])) {
  63. throw new exceptions\DNSSubmoduleException('Unable to get user token. '.(empty($ret['status']['message']) ? 'Unknown API Error' : $ret['status']['message']), dns\SubmoduleExceptionCodes::CONNECTION_PROBLEM);
  64. }
  65. $post .= '&user_token='.$ret['user_token'];
  66. }
  67. else
  68. {
  69. $post = 'login_token='.$this->config['username'].','.$this->config['password'].'&format=json';
  70. }
  71. if(is_array($params) && !empty($params))
  72. {
  73. $post .= '&';
  74. foreach($params as $key => $value)
  75. {
  76. $post .= "{$key}={$value}&";
  77. }
  78. $post = trim($post, '&');
  79. }
  80. $ch = curl_init();
  81. $chOptions = array (
  82. CURLOPT_URL => $url.$function,
  83. CURLOPT_RETURNTRANSFER => true,
  84. CURLOPT_SSL_VERIFYPEER => false,
  85. CURLOPT_SSL_VERIFYHOST => false,
  86. CURLOPT_TIMEOUT => 30,
  87. CURLOPT_POSTFIELDS => $post
  88. );
  89. if($this->config['auth_type'] == 'en') {
  90. $chOptions[CURLOPT_USERAGENT] = 'MJJ DDNS Client/1.0.0 ('.$this->config['username'].')';
  91. }
  92. curl_setopt_array($ch, $chOptions);
  93. $out = curl_exec($ch);
  94. $ch_err = curl_error($ch);
  95. curl_close($ch);
  96. if($out === false || !empty($ch_err) || strpos(htmlspecialchars($out), '<html')) {
  97. throw new exceptions\DNSSubmoduleException(empty($ch_err) ? 'Unknown API Error' : $ch_err, dns\SubmoduleExceptionCodes::CONNECTION_PROBLEM);
  98. }
  99. $ret = json_decode($out, true);
  100. if(empty($ret)) {
  101. throw new exceptions\DNSSubmoduleException(empty($ch_err) ? 'Unknown API Error' : $ch_err, dns\SubmoduleExceptionCodes::INVALID_RESPONSE);
  102. }
  103. if($ret['status']['code'] != 1) {
  104. throw new exceptions\DNSSubmoduleException($ret['status']['message'] ?: 'Unknown Error', dns\SubmoduleExceptionCodes::COMMAND_ERROR);
  105. }
  106. return $ret;
  107. }
  108. public function testConnection() {
  109. $this->get('Info.Version', array());
  110. return true;
  111. }
  112. public function zoneExists() {
  113. try {
  114. $out = $this->get('Domain.Info', array(
  115. 'domain' => $this->domain
  116. ));
  117. $this->grade = $out['domain']['grade'];
  118. } catch (exceptions\DNSSubmoduleException $e) {
  119. if($e->getCode() == dns\SubmoduleExceptionCodes::COMMAND_ERROR) {
  120. return false;
  121. }
  122. throw $e;
  123. }
  124. return true;
  125. }
  126. public function getRecords($recordType = false) {
  127. $this->domainid = $this->getDomainId();
  128. $out = $this->get('Record.List', array(
  129. 'domain_id' => $this->domainid,
  130. ));
  131. $records = array();
  132. foreach($out['records'] as $r) {
  133. $r['type'] = strtoupper($r['type']);
  134. if(in_array($r['type'], $recordType!==false ? array(strtoupper($recordType)) : $this->getAvailableRecordTypes())) {
  135. $record = new dns\record\Record();
  136. $record->line = $r['id'];
  137. $record->name = $r['name'];
  138. $record->type = $r['type'];
  139. $record->ttl = $r['ttl'];
  140. $record->createRDATAObject();
  141. switch($r['type']) {
  142. case 'MX':
  143. $record->rdata->exchange = $r['value'];
  144. $record->rdata->preference = $r['mx'];
  145. break;
  146. default:
  147. $record->rdata->fromString($r['value']);
  148. break;
  149. }
  150. $records[] = $record;
  151. }
  152. }
  153. return $records;
  154. }
  155. private function recordToParamsArray(dns\record\Record $record) {
  156. $this->domainid = $this->getDomainId();
  157. $this->setGradeLine();
  158. $params = array(
  159. 'domain_id' => $this->domainid,
  160. 'sub_domain' => $record->nameToRelative($this->domain)?:'@',
  161. 'record_type' => $record->type,
  162. 'record_line' => $this->record_line,
  163. 'ttl' => $record->ttl,
  164. );
  165. switch($record->type) {
  166. case 'MX':
  167. $params['mx'] = $record->rdata->preference;
  168. $params['value'] = rtrim($record->rdata->exchange, '.') . '.';
  169. break;
  170. case 'A':
  171. case 'AAAA':
  172. case 'TXT':
  173. $params['value'] = $record->rdata->toString();
  174. break;
  175. default :
  176. $params['value'] = rtrim($record->rdata->toString(),'.') . '.';
  177. break;
  178. }
  179. return $params;
  180. }
  181. public function addRecord(dns\record\Record $record) {
  182. $params = $this->recordToParamsArray($record);
  183. $this->get('Record.Create', $params);
  184. }
  185. public function editRecord(dns\record\Record $record) {
  186. $params = $this->recordToParamsArray($record);
  187. $params['record_id'] = $record->line;
  188. $this->get('Record.Modify', $params);
  189. }
  190. public function deleteRecord(dns\record\Record $record) {
  191. $this->domainid = $this->getDomainId();
  192. $params = array(
  193. 'domain_id' => $this->domainid,
  194. 'record_id' => $record->line,
  195. );
  196. $this->get('Record.Remove', $params);
  197. }
  198. public function activateZone() {
  199. $this->get('Domain.Create', array('domain' => $this->domain));
  200. }
  201. public function terminateZone() {
  202. $this->get('Domain.Remove', array('domain' => $this->domain));
  203. }
  204. private function getDomainId() {
  205. if($this->domainid !== false) {
  206. return $this->domainid;
  207. }
  208. $out = $this->get('Domain.Info', array(
  209. 'domain' => $this->domain
  210. ));
  211. $this->grade = $out['domain']['grade']?:false;
  212. $this->domainid = $out['domain']['id'];
  213. return $this->domainid;
  214. }
  215. private function setGradeLine() {
  216. if($this->config['auth_type'] == 'ch') {
  217. if($this->grade === false) {
  218. $out = $this->get('Domain.Info', array(
  219. 'domain' => $this->domain
  220. ));
  221. if(isset($out['domain']['grade'])) {
  222. $this->grade = $out['domain']['grade'];
  223. }
  224. }
  225. if($this->record_line_get === false) {
  226. $out = $this->get('Record.Line', array(
  227. 'domain_grade' => $this->grade,
  228. 'domain' => $this->domain
  229. ));
  230. $this->record_line_get = true;
  231. $this->record_line = empty($out['lines'][0]) ? false : $out['lines'][0];
  232. }
  233. }
  234. }
  235. public function getZones() {
  236. $ret = $this->get('Domain.List');
  237. $out = array();
  238. foreach($ret['domains'] as $domain) {
  239. $out[$domain] = '';
  240. }
  241. return $out;
  242. }
  243. }