示例#1
0
 public function getContents($config_contents = false)
 {
     $this->loadRecords();
     $this->soaSerial = Scalr_Net_Dns_SOARecord::raiseSerial($this->soaSerial);
     $this->save();
     $soaRecord = new Scalr_Net_Dns_SOARecord($this->zoneName, $this->soaParent, $this->soaOwner, $this->soaTtl, $this->soaSerial, $this->soaRefresh, $this->soaRetry, $this->soaExpire, $this->soaMinTtl);
     $zone = new Scalr_Net_Dns_Zone();
     $zone->addRecord($soaRecord);
     if (!$config_contents) {
         $rCache = array();
         foreach ($this->records as $record) {
             if (!$rCache[$record['type']]) {
                 $r = new ReflectionClass("Scalr_Net_Dns_{$record['type']}Record");
                 $params = array();
                 foreach ($r->getConstructor()->getParameters() as $p) {
                     $params[] = $p->name;
                 }
                 $rCache[$record['type']] = array('reflect' => $r, 'params' => $params);
             }
             $args = array();
             foreach ($rCache[$record['type']]['params'] as $p) {
                 $args[$p] = $record[$p];
             }
             try {
                 $r = $rCache[$record['type']]['reflect']->newInstanceArgs($args);
                 $zone->addRecord($r);
             } catch (Exception $e) {
             }
         }
     }
     return $zone->generate($this->axfrAllowedHosts, $config_contents);
 }
 public function DNSZoneRecordAdd($ZoneName, $Type, $TTL, $Name, $Value, $Priority = 0, $Weight = 0, $Port = 0)
 {
     $this->restrictAccess(Acl::RESOURCE_DNS_ZONES);
     if (!Scalr::config('scalr.dns.global.enabled')) {
         throw new Exception("DNS functionality is not enabled. Please contact your Scalr administrator.");
     }
     $zoneinfo = $this->DB->GetRow("SELECT id, env_id FROM dns_zones WHERE zone_name=? LIMIT 1", array($ZoneName));
     if (!$zoneinfo || $zoneinfo['env_id'] != $this->Environment->id) {
         throw new Exception(sprintf("Zone '%s' not found in database", $ZoneName));
     }
     if (!in_array($Type, array("A", "MX", "CNAME", "NS", "TXT", "SRV"))) {
         throw new Exception(sprintf("Unknown record type '%s'", $Type));
     }
     $record = array('name' => $Name, 'value' => $Value, 'type' => $Type, 'ttl' => $TTL, 'priority' => $Priority, 'weight' => $Weight, 'port' => $Port);
     $recordsValidation = Scalr_Net_Dns_Zone::validateRecords(array($record));
     if ($recordsValidation === true) {
         $DBDNSZone = DBDNSZone::loadById($zoneinfo['id']);
         $records = $DBDNSZone->getRecords(false);
         array_push($records, $record);
         $DBDNSZone->setRecords($records);
         $DBDNSZone->save(false);
     } else {
         throw new Exception($recordsValidation[0]);
     }
     $response = $this->CreateInitialResponse();
     $response->Result = 1;
     return $response;
 }
 public function DNSZoneRecordAdd($ZoneName, $Type, $TTL, $Name, $Value, $Priority = 0, $Weight = 0, $Port = 0)
 {
     $zoneinfo = $this->DB->GetRow("SELECT id, env_id FROM dns_zones WHERE zone_name=?", array($ZoneName));
     if (!$zoneinfo || $zoneinfo['env_id'] != $this->Environment->id) {
         throw new Exception(sprintf("Zone '%s' not found in database", $ZoneName));
     }
     if (!in_array($Type, array("A", "MX", "CNAME", "NS", "TXT", "SRV"))) {
         throw new Exception(sprintf("Unknown record type '%s'", $Type));
     }
     $record = array('name' => $Name, 'value' => $Value, 'type' => $Type, 'ttl' => $TTL, 'priority' => $Priority, 'weight' => $Weight, 'port' => $Port);
     $recordsValidation = Scalr_Net_Dns_Zone::validateRecords(array($record));
     if ($recordsValidation === true) {
         $DBDNSZone = DBDNSZone::loadById($zoneinfo['id']);
         $records = $DBDNSZone->getRecords(false);
         array_push($records, $record);
         $DBDNSZone->setRecords($records);
         $DBDNSZone->save(false);
     } else {
         throw new Exception($recordsValidation[0]);
     }
     $response = $this->CreateInitialResponse();
     $response->Result = 1;
     return $response;
 }
示例#4
0
 public function xSaveAction()
 {
     $this->request->defineParams(array('domainId' => array('type' => 'int'), 'domainName', 'domainType', 'domainFarm' => array('type' => 'int'), 'domainFarmRole' => array('type' => 'int'), 'soaRefresh' => array('type' => 'int'), 'soaExpire' => array('type' => 'int'), 'soaRetry' => array('type' => 'int'), 'records' => array('type' => 'json')));
     $errors = array();
     // validate farmId, farmRoleId
     $farmId = 0;
     $farmRoleId = 0;
     if ($this->getParam('domainFarm')) {
         $DBFarm = DBFarm::LoadByID($this->getParam('domainFarm'));
         if (!$this->user->getPermissions()->check($DBFarm)) {
             $errors['domainFarm'] = _('Farm not found');
         } else {
             $farmId = $DBFarm->ID;
             if ($this->getParam('domainFarmRole')) {
                 $DBFarmRole = DBFarmRole::LoadByID($this->getParam('domainFarmRole'));
                 if ($DBFarmRole->FarmID != $DBFarm->ID) {
                     $errors['domainFarmRole'] = _('Role not found');
                 } else {
                     $farmRoleId = $DBFarmRole->ID;
                 }
             }
         }
     }
     // validate domain name
     $domainName = '';
     if (!$this->getParam('domainId')) {
         if ($this->getParam('domainType') == 'own') {
             $Validator = new Validator();
             if (!$Validator->IsDomain($this->getParam('domainName'))) {
                 $errors['domainName'] = _("Invalid domain name");
             } else {
                 $domainChunks = explode(".", $this->getParam('domainName'));
                 $chkDmn = '';
                 while (count($domainChunks) > 0) {
                     $chkDmn = trim(array_pop($domainChunks) . ".{$chkDmn}", ".");
                     $chkDomainId = $this->db->GetOne("SELECT id FROM dns_zones WHERE zone_name=? AND client_id != ?", array($chkDmn, $this->user->getAccountId()));
                     if ($chkDomainId) {
                         if ($chkDmn == $this->getParam('domainName')) {
                             $errors['domainName'] = sprintf(_("%s already exists on scalr nameservers"), $this->getParam('domainName'));
                         } else {
                             $chkDnsZone = DBDNSZone::loadById($chkDomainId);
                             $access = false;
                             foreach (explode(";", $chkDnsZone->allowedAccounts) as $email) {
                                 if ($email == $this->user->getEmail()) {
                                     $access = true;
                                 }
                             }
                             if (!$access) {
                                 $errors['domainName'] = sprintf(_("You cannot use %s domain name because top level domain %s does not belong to you"), $this->getParam('domainName'), $chkDmn);
                             }
                         }
                     }
                 }
                 //if (! $errors['domainName'])
                 $domainName = $this->getParam('domainName');
             }
         } else {
             $domainName = Scalr::GenerateUID() . '.' . CONFIG::$DNS_TEST_DOMAIN_NAME;
         }
         // check in DB
         $rez = $this->db->GetOne("SELECT id FROM dns_zones WHERE zone_name = ?", array($domainName));
         if ($rez) {
             $errors['domainName'] = 'Domain name already exist in database';
         }
     }
     $records = array();
     foreach ($this->getParam('records') as $key => $r) {
         if (($r['name'] || $r['value']) && $r['issystem'] == 0) {
             $r['name'] = str_replace("%hostname%", "{$domainName}", $r['name']);
             $r['value'] = str_replace("%hostname%", "{$domainName}", $r['value']);
             $records[$key] = $r;
         }
     }
     $recordsValidation = Scalr_Net_Dns_Zone::validateRecords($records);
     if ($recordsValidation !== true) {
         $errors = array_merge($errors, $recordsValidation);
     }
     if (count($errors) == 0) {
         if ($this->getParam('domainId')) {
             $DBDNSZone = DBDNSZone::loadById($this->getParam('domainId'));
             $this->user->getPermissions()->validate($DBDNSZone);
             $DBDNSZone->soaRefresh = $this->getParam('soaRefresh');
             $DBDNSZone->soaExpire = $this->getParam('soaExpire');
             $DBDNSZone->soaRetry = $this->getParam('soaRetry');
             $this->response->success("DNS zone successfully updated. It could take up to 5 minutes to update it on NS servers.");
         } else {
             $DBDNSZone = DBDNSZone::create($domainName, $this->getParam('soaRefresh'), $this->getParam('soaExpire'), str_replace('@', '.', $this->user->getEmail()), $this->getParam('soaRetry'));
             $DBDNSZone->clientId = $this->user->getAccountId();
             $DBDNSZone->envId = $this->getEnvironmentId();
             $this->response->success("DNS zone successfully added to database. It could take up to 5 minutes to setup it on NS servers.");
         }
         if ($DBDNSZone->farmRoleId != $farmRoleId || $DBDNSZone->farmId != $farmId) {
             $DBDNSZone->farmId = 0;
             $DBDNSZone->updateSystemRecords();
         }
         $DBDNSZone->farmRoleId = $farmRoleId;
         $DBDNSZone->farmId = $farmId;
         $DBDNSZone->setRecords($records);
         $DBDNSZone->save(true);
     } else {
         $this->response->failure();
         $this->response->data(array('errors' => $errors));
     }
 }