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; }