Example #1
0
 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 createRecord($data = array())
 {
     // data will contain:
     // Required: domainId, hostname, type
     // Optional: target, priority, port and weight
     $target = isset($data['target']) ? $data['target'] : null;
     $priority = isset($data['priority']) ? $data['priority'] : null;
     $port = isset($data['port']) ? $data['port'] : null;
     $weight = isset($data['weight']) ? $data['weight'] : null;
     $domain = Domain::find($data['domainId']);
     if ($domain->user_id != Auth::User()->id) {
         throw new \Exception('Access to domain denied.');
     }
     if (!$this->validateRecord($data['type'], $data['target'])) {
         throw new \Exception('Invalid destination for the chosen record type.');
     }
     $recordQuery = DomainRecord::where('domain_id', '=', $data['domainId'])->where('name', '=', $data['hostname'])->where('type', '=', $data['type']);
     if (!empty($target)) {
         $recordQuery = $recordQuery->where('target', '=', $target);
     }
     if (!empty($priority)) {
         $recordQuery = $recordQuery->where('priority', '=', $priority);
     }
     if (!empty($port)) {
         $recordQuery = $recordQuery->where('port', '=', $port);
     }
     if (!empty($weight)) {
         $recordQuery = $recordQuery->where('weight', '=', $weight);
     }
     $record = $recordQuery->first();
     if ($record instanceof DomainRecord) {
         throw new \Exception('Unable to create duplicate record.');
     }
     // If we're making an MX or NS record, name is the domain.
     $hostname = in_array($data['type'], ['MX', 'NS']) ? $domain->name : $data['hostname'] . '.' . $domain->name;
     $this->server->createRecord($hostname, $data['type'], $target, $priority, $port, $weight);
     DomainRecord::create(['domain_id' => $data['domainId'], 'name' => $data['hostname'], 'type' => $data['type'], 'target' => $target, 'priority' => $priority, 'port' => $port, 'weight' => $weight, 'user_id' => Auth::User()->id]);
     return true;
 }