public function createRecord($host, $type, $target = null, $priority = null, $port = null, $weight = null) { $domain = $this->getDomain($host); $content = $this->makeContent($type, $target, $priority, $port, $weight); $records = [['content' => $content, 'disabled' => false, 'name' => $host, 'ttl' => 86400, 'type' => $type]]; $domainId = Domain::where('name', '=', $domain)->first()->id; // Do we need to add any additional records into this call? $existingRecordsQuery = DomainRecord::where('type', '=', $type)->where('target', '!=', $target)->where('domain_id', '=', $domainId); if (in_array($type, ['MX', 'NS'])) { $existingRecordsQuery = $existingRecordsQuery->whereNull('name'); } else { if ($type == 'SRV') { $existingRecordsQuery = $existingRecordsQuery->where('name', '=', substr($host, 0, strpos($host, '.', strpos($host, '.') + 1))); } else { $existingRecordsQuery = $existingRecordsQuery->where('name', '=', current(explode('.', $host))); } } $existingRecords = $existingRecordsQuery->get(); if (count($existingRecords) > 0) { foreach ($existingRecords as $existingRecord) { $records[] = ['content' => $this->makeContent($existingRecord->type, $existingRecord->target, $existingRecord->priority, $existingRecord->port, $existingRecord->weight), 'disabled' => false, 'name' => $host, 'ttl' => 86400, 'type' => $existingRecord->type]; } } $data = ['rrsets' => [['name' => $host, 'type' => $type, 'changetype' => 'REPLACE', 'records' => $records]]]; try { $response = $this->client->patch('servers/localhost/zones/' . $domain, ['json' => $data]); } catch (\Exception $e) { return $e->getMessage(); } return true; }
public function deleteRecord($recordId) { $record = DomainRecord::find($recordId); if ($record->user_id != Auth::User()->id) { throw new \Exception('Access to record denied.'); } $this->server->deleteRecord($record->name . '.' . $record->domain->name, $record->type); $record->delete(); }