| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294 |
- <?php
- namespace MGModule\DNSManager2\mgLibs\custom\dns\submodules;
- use \MGModule\DNSManager2\mgLibs\custom\dns;
- use \MGModule\DNSManager2\mgLibs\custom\dns\exceptions;
- use \MGModule\DNSManager2\mgLibs\custom\dns\interfaces;
- class DNSPod extends dns\SubmoduleAbstract implements interfaces\SubmoduleTTLInterface, interfaces\SubmoduleImportInterface {
- const API_URL_CHINESE = 'dnsapi.cn';
- const API_URL_ENGLISH = 'api.dnspod.com';
-
- public $configFields = array(
- 'auth_type' => array (
- 'friendlyName' => 'Account Is',
- 'type' => 'select',
- 'options' => array('ch' => 'Chinese', 'en' => 'English'),
- ),
- 'username' => array (
- 'friendlyName' => 'Email / Token ID',
- 'validators' => array(
- 'required' => 'required',
- ),
- 'help' => 'Email for English account or Token ID for Chinese account'
- ),
- 'password' => array (
- 'friendlyName' => 'Password / Token',
- 'type' => 'password',
- 'validators' => array(
- 'required' => 'required',
- ),
- 'help' => 'Password for English account or Token for Chinese account'
- ),
- 'ssl' => array(
- 'friendlyName' => 'Enable SSL',
- 'type' => 'yesno'
- )
- );
-
- private $domainid = false;
- private $record_line = 'default';
- private $record_line_get = false;
- private $grade = false;
-
- public $availableTypes = array('A', 'AAAA', 'NS', 'MX', 'CNAME', 'TXT', 'SRV');
-
- private function get($function, $params = array()) {
- $url = ($this->config['ssl'] == 'on' ? 'https://' : 'http://').($this->config['auth_type'] == 'ch' ? self::API_URL_CHINESE : self::API_URL_ENGLISH).'/';
-
- if($this->config['auth_type'] == 'en') {
- $post = 'login_email='.$this->config['username'].'&login_password='.$this->config['password'].'&format=json';
- $ch = curl_init();
- $chOptions = array (
- CURLOPT_URL => $url.'Auth',
- CURLOPT_RETURNTRANSFER => true,
- CURLOPT_SSL_VERIFYPEER => false,
- CURLOPT_SSL_VERIFYHOST => false,
- CURLOPT_TIMEOUT => 30,
- CURLOPT_USERAGENT => 'MJJ DDNS Client/1.0.0 ('.$this->config['username'].')',
- CURLOPT_POSTFIELDS => $post
- );
-
- curl_setopt_array($ch, $chOptions);
- $out = curl_exec($ch);
- $ch_err = curl_error($ch);
- curl_close($ch);
-
- if($out === false || !empty($ch_err)) {
- throw new exceptions\DNSSubmoduleException(empty($ch_err) ? 'Unknown API Error' : $ch_err, dns\SubmoduleExceptionCodes::CONNECTION_PROBLEM);
- }
-
- $ret = json_decode($out, true);
-
- if(empty($ret['user_token'])) {
- throw new exceptions\DNSSubmoduleException('Unable to get user token. '.(empty($ret['status']['message']) ? 'Unknown API Error' : $ret['status']['message']), dns\SubmoduleExceptionCodes::CONNECTION_PROBLEM);
- }
-
- $post .= '&user_token='.$ret['user_token'];
- }
- else
- {
- $post = 'login_token='.$this->config['username'].','.$this->config['password'].'&format=json';
- }
- if(is_array($params) && !empty($params))
- {
- $post .= '&';
- foreach($params as $key => $value)
- {
- $post .= "{$key}={$value}&";
- }
- $post = trim($post, '&');
- }
-
- $ch = curl_init();
- $chOptions = array (
- CURLOPT_URL => $url.$function,
- CURLOPT_RETURNTRANSFER => true,
- CURLOPT_SSL_VERIFYPEER => false,
- CURLOPT_SSL_VERIFYHOST => false,
- CURLOPT_TIMEOUT => 30,
- CURLOPT_POSTFIELDS => $post
- );
- if($this->config['auth_type'] == 'en') {
- $chOptions[CURLOPT_USERAGENT] = 'MJJ DDNS Client/1.0.0 ('.$this->config['username'].')';
- }
-
- curl_setopt_array($ch, $chOptions);
- $out = curl_exec($ch);
- $ch_err = curl_error($ch);
- curl_close($ch);
-
- if($out === false || !empty($ch_err) || strpos(htmlspecialchars($out), '<html')) {
- throw new exceptions\DNSSubmoduleException(empty($ch_err) ? 'Unknown API Error' : $ch_err, dns\SubmoduleExceptionCodes::CONNECTION_PROBLEM);
- }
-
- $ret = json_decode($out, true);
- if(empty($ret)) {
- throw new exceptions\DNSSubmoduleException(empty($ch_err) ? 'Unknown API Error' : $ch_err, dns\SubmoduleExceptionCodes::INVALID_RESPONSE);
- }
-
- if($ret['status']['code'] != 1) {
- throw new exceptions\DNSSubmoduleException($ret['status']['message'] ?: 'Unknown Error', dns\SubmoduleExceptionCodes::COMMAND_ERROR);
- }
- return $ret;
- }
-
- public function testConnection() {
- $this->get('Info.Version', array());
- return true;
- }
-
- public function zoneExists() {
- try {
- $out = $this->get('Domain.Info', array(
- 'domain' => $this->domain
- ));
- $this->grade = $out['domain']['grade'];
- } catch (exceptions\DNSSubmoduleException $e) {
- if($e->getCode() == dns\SubmoduleExceptionCodes::COMMAND_ERROR) {
- return false;
- }
- throw $e;
- }
- return true;
- }
- public function getRecords($recordType = false) {
- $this->domainid = $this->getDomainId();
- $out = $this->get('Record.List', array(
- 'domain_id' => $this->domainid,
- ));
-
- $records = array();
-
- foreach($out['records'] as $r) {
- $r['type'] = strtoupper($r['type']);
-
- if(in_array($r['type'], $recordType!==false ? array(strtoupper($recordType)) : $this->getAvailableRecordTypes())) {
- $record = new dns\record\Record();
- $record->line = $r['id'];
- $record->name = $r['name'];
- $record->type = $r['type'];
- $record->ttl = $r['ttl'];
- $record->createRDATAObject();
-
- switch($r['type']) {
- case 'MX':
- $record->rdata->exchange = $r['value'];
- $record->rdata->preference = $r['mx'];
- break;
- default:
- $record->rdata->fromString($r['value']);
- break;
- }
-
- $records[] = $record;
- }
- }
-
- return $records;
- }
- private function recordToParamsArray(dns\record\Record $record) {
- $this->domainid = $this->getDomainId();
- $this->setGradeLine();
- $params = array(
- 'domain_id' => $this->domainid,
- 'sub_domain' => $record->nameToRelative($this->domain)?:'@',
- 'record_type' => $record->type,
- 'record_line' => $this->record_line,
- 'ttl' => $record->ttl,
- );
-
- switch($record->type) {
- case 'MX':
- $params['mx'] = $record->rdata->preference;
- $params['value'] = rtrim($record->rdata->exchange, '.') . '.';
- break;
- case 'A':
- case 'AAAA':
- case 'TXT':
- $params['value'] = $record->rdata->toString();
- break;
- default :
- $params['value'] = rtrim($record->rdata->toString(),'.') . '.';
- break;
- }
-
- return $params;
- }
-
- public function addRecord(dns\record\Record $record) {
- $params = $this->recordToParamsArray($record);
-
- $this->get('Record.Create', $params);
- }
- public function editRecord(dns\record\Record $record) {
- $params = $this->recordToParamsArray($record);
- $params['record_id'] = $record->line;
- $this->get('Record.Modify', $params);
- }
- public function deleteRecord(dns\record\Record $record) {
- $this->domainid = $this->getDomainId();
- $params = array(
- 'domain_id' => $this->domainid,
- 'record_id' => $record->line,
- );
-
- $this->get('Record.Remove', $params);
- }
- public function activateZone() {
- $this->get('Domain.Create', array('domain' => $this->domain));
- }
- public function terminateZone() {
- $this->get('Domain.Remove', array('domain' => $this->domain));
- }
-
- private function getDomainId() {
- if($this->domainid !== false) {
- return $this->domainid;
- }
-
- $out = $this->get('Domain.Info', array(
- 'domain' => $this->domain
- ));
-
- $this->grade = $out['domain']['grade']?:false;
- $this->domainid = $out['domain']['id'];
- return $this->domainid;
- }
-
- private function setGradeLine() {
- if($this->config['auth_type'] == 'ch') {
- if($this->grade === false) {
- $out = $this->get('Domain.Info', array(
- 'domain' => $this->domain
- ));
- if(isset($out['domain']['grade'])) {
- $this->grade = $out['domain']['grade'];
- }
- }
-
- if($this->record_line_get === false) {
- $out = $this->get('Record.Line', array(
- 'domain_grade' => $this->grade,
- 'domain' => $this->domain
- ));
- $this->record_line_get = true;
- $this->record_line = empty($out['lines'][0]) ? false : $out['lines'][0];
- }
- }
- }
- public function getZones() {
- $ret = $this->get('Domain.List');
-
- $out = array();
- foreach($ret['domains'] as $domain) {
- $out[$domain] = '';
- }
-
- return $out;
- }
- }
|