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), '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; } }