function validateEntry($action, $id, $data, $record_type) { $messages = null; $html = null; $append = array('CNAME', 'NS', 'MX', 'SRV', 'DNAME', 'CERT', 'RP', 'NAPTR'); if ($action == 'create' && !isset($data['record_append']) && in_array($record_type, $append) && substr($data['record_value'], -1) != '.') { $data['record_append'] = 'yes'; } elseif (!isset($data['record_append']) && in_array($record_type, $append)) { $data['record_append'] = 'no'; } if (!empty($data['record_value'])) { $data['record_value'] = str_replace(array('"', "'"), '', $data['record_value']); foreach ($data as $key => $val) { $data[$key] = trim($val, '"\''); if ($key == 'record_name' && $record_type != 'PTR') { if (!$val) { $val = '@'; $data[$key] = $val; } if (!verifyName($val, $id, true, $record_type)) { $messages['errors'][$key] = __('Invalid'); } } if (in_array($key, array('record_ttl', 'record_priority', 'record_weight', 'record_port'))) { if (!empty($val) && verifyNumber($val) === false) { $messages['errors'][$key] = __('Invalid'); } } if ($record_type == 'A') { if ($key == 'record_value') { if (verifyIPAddress($val) === false) { $messages['errors'][$key] = __('Invalid IP'); } } if ($key == 'PTR') { global $domain_id; $retval = checkPTRZone($data['record_value'], $domain_id); list($val, $error_msg) = $retval; if ($val == null) { $messages['errors']['record_value'] = $error_msg; } else { $messages['info']['record_value'] = $error_msg; } } } if ($record_type == 'PTR') { if ($key == 'record_name') { if ($domain_map == 'reverse') { if (verifyIPAddress(buildFullIPAddress($data['record_name'], $domain)) === false) { $messages['errors'][$key] = __('Invalid record'); } } else { if (!verifyCNAME('yes', $data['record_name'], false, true)) { $messages['errors'][$key] = __('Invalid record'); } } } } if (in_array($record_type, array('CNAME', 'DNAME', 'MX', 'NS', 'SRV', 'NAPTR')) || $record_type == 'PTR' && $key == 'record_value') { if ($key == 'record_value') { $val = $data['record_append'] == 'yes' || $val == '@' ? trim($val, '.') : trim($val, '.') . '.'; $data[$key] = $val; if (!verifyCNAME($data['record_append'], $val) || $record_type == 'NS' && !validateHostname($val)) { $messages['errors'][$key] = __('Invalid value'); } } } if (!count($messages['errors'])) { $html .= buildInputReturn($action, $id, $key, $val); } else { $html = null; } } } elseif ($record_type == 'SOA') { if ($_POST['create']['soa_template_chosen']) { global $fm_dns_records; // Save the soa_template_chosen in domains table and end include_once ABSPATH . 'fm-modules/fmDNS/classes/class_records.php'; $fm_dns_records->assignSOA($_POST['create']['soa_template_chosen'], $_POST['domain_id']); header('Location: zone-records.php?map=' . $_POST['map'] . '&domain_id=' . $_POST['domain_id'] . '&record_type=SOA'); } if (!isset($data['soa_append'])) { $data['soa_append'] = 'no'; } foreach ($data as $key => $val) { if (in_array($key, array('domain_id', 'soa_status'))) { continue; } if ($key == 'soa_email_address') { $val = strpos($val, '@') ? str_replace('@', '.', rtrim($val, '.') . '.') : $val; $data[$key] = $val; } if (in_array($key, array('soa_master_server', 'soa_email_address'))) { $val = rtrim($val, '.'); if (strpos($_POST['update'][$id]['soa_master_server'], $domain) && strpos($_POST['update'][$id]['soa_email_address'], $domain)) { $new_val = rtrim(str_replace($domain, '', $val), '.'); if ($new_val != rtrim($val, '.')) { $data['soa_append'] = 'yes'; } $val = $new_val; } if ($data['soa_append'] == 'no') { $val .= '.'; } } if ($key != 'soa_append') { if (in_array($key, array('soa_master_server', 'soa_email_address'))) { $val = $data['soa_append'] == 'yes' ? trim($val, '.') : trim($val, '.') . '.'; $data[$key] = $val; if (!verifyCNAME($data['soa_append'], $val, false) || $key == 'soa_master_server' && !validateHostname($val)) { $messages['errors'][$key] = __('Invalid'); } } else { if (array_key_exists('soa_template', $data) && $data['soa_template'] == 'yes') { if (!verifyNAME($val, $id, false)) { $messages['errors'][$key] = __('Invalid'); } } } } if (!count($messages['errors'])) { $html .= buildInputReturn($action, $id, $key, $val); } else { $html = null; } } } else { unset($data); } return array($data, $html, $messages); }
function validateDomainName($domain_name, $domain_mapping) { if (substr($domain_name, -5) == '.arpa') { /** .arpa is only for reverse zones */ if ($domain_mapping == 'forward') { return false; } $domain_pieces = explode('.', $domain_name); $domain_parts = count($domain_pieces); /** IPv4 checks */ if ($domain_pieces[$domain_parts - 2] == 'in-addr') { /** The first digit of a reverse zone must be numeric */ if (!is_numeric(substr($domain_name, 0, 1))) { return false; } /** Reverse zones with arpa must have at least three octets */ if ($domain_parts < 3) { return false; } /** Second to last octet must be valid for arpa */ if (!in_array($domain_pieces[$domain_parts - 2], array('e164', 'in-addr-servers', 'in-addr', 'ip6-servers', 'ip6', 'iris', 'uri', 'urn'))) { return false; } for ($i = 0; $i < $domain_parts - 2; $i++) { /** Check if using classless */ if ($i == 0) { if (preg_match("/^(\\d{1,3})\\-(\\d{1,3})\$/", $domain_pieces[$i])) { /** Validate octet range */ $octet_range = explode('-', $domain_pieces[$i]); if ($octet_range[0] >= $octet_range[1]) { return false; } foreach ($octet_range as $octet) { if (filter_var($octet, FILTER_VALIDATE_INT, array('options' => array('min_range' => 0, 'max_range' => 255))) === false) { return false; } } continue; } } /** Remaining octects must be numeric */ if (filter_var($domain_pieces[$i], FILTER_VALIDATE_INT, array('options' => array('min_range' => 0, 'max_range' => 255))) === false) { return false; } } /** IPv6 checks */ } elseif ($domain_pieces[$domain_parts - 2] == 'ip6') { return true; return verifyIPAddress(buildFullIPAddress(0, $domain_name)); } } elseif ($domain_mapping == 'reverse') { /** If reverse zone does not contain arpa then it must only contain numbers, periods, letters, and colons */ $domain_pieces = explode('.', $domain_name); /** IPv4 checks */ if (strpos($domain_name, ':') === false) { foreach ($domain_pieces as $number) { if (filter_var($number, FILTER_VALIDATE_INT, array('options' => array('min_range' => 0, 'max_range' => 255))) === false) { return false; } } /** IPv6 checks */ } elseif (!preg_match('/^[a-z\\d\\:]+$/i', $domain_name)) { return false; } } else { /** Forward zones should only contain letters, numbers, periods, and hyphens */ return preg_match("/^(_*[a-z\\d](-*[a-z\\d])*)(\\.([a-z\\d](-*[a-z\\d])*))*\$/i", $domain_name) && preg_match("/^.{1,253}\$/", $domain_name) && preg_match("/^[^\\.]{1,63}(\\.[^\\.]{1,63})*\$/", $domain_name); // length of each label } return true; }