Skip to content

Commit

Permalink
sisendite valideerimine
Browse files Browse the repository at this point in the history
  • Loading branch information
andris9 committed Apr 15, 2011
1 parent 5e67c03 commit f4bca32
Showing 1 changed file with 72 additions and 33 deletions.
105 changes: 72 additions & 33 deletions drupal7-module/dns.module
Expand Up @@ -110,7 +110,7 @@ function dns_domain_list(){
"header"=>$header,
"rows"=>$rows,
"attributes"=>array(),
"caption"=>"Domain list for <strong>{$user->name}</strong>",
"caption"=>"Domain list for <strong>".htmlspecialchars($user->name)."</strong>",
"colgroups"=>array(),
"sticky"=>true,
"empty"=>t("No domains added, click ".l("here","dns/add")." to add one")
Expand Down Expand Up @@ -149,7 +149,7 @@ function dns_domain_edit(){

$types = array("A"=>"IP address", "CNAME"=>"Domain alias", "MX"=>"Mail server", "NS"=>"Name server");

$header = array(array("data"=>"#","width"=>"30"), t("Name"), t("Value"), array("data"=>"&nbsp;","width"=>"50"));
$header = array(array("data"=>"#","width"=>"30"), t("Name"), t("TTL"), t("Value"), array("data"=>"&nbsp;","width"=>"50"));
$rows = array();

$tables = array(
Expand All @@ -168,13 +168,14 @@ function dns_domain_edit(){
foreach($block as $row){
$rows[] = array(
++$i,
$row["name"],
($type=="MX"?intval($row["value"][1])." ":"").$row["value"][0],
htmlspecialchars($row["name"]),
intval($row["ttl"]),
($type=="MX"?intval($row["value"][1])." ":"").htmlspecialchars($row["value"][0]),
array(
"data"=>l("Delete", "dns/remove-record",array(
"query"=>array(
"row"=>intval($row["id"]),
"domain"=>$domain
"domain"=>htmlspecialchars($domain)
)
)),
"style"=>"text-align: center",
Expand All @@ -190,7 +191,7 @@ function dns_domain_edit(){
"header"=>$header,
"rows"=>$rows,
"attributes"=>array(),
"caption"=>"<strong>{$types["$type"]} ($type)</strong> records for <strong>{$domain}</strong>",
"caption"=>"<strong>{$types["$type"]} ($type)</strong> records for <strong>".htmlspecialchars($domain)."</strong>",
"colgroups"=>array(),
"empty"=>t("No records added")
));
Expand All @@ -204,8 +205,8 @@ function dns_domain_edit(){

$records_field = theme_fieldset(array(
"element"=> array(
"#title"=>"DNS records for <strong>$domain</strong>",
"#description"=>"<p>Here are listed all the DNS records of <strong>$domain</strong>. To use the DNS service, set the nameservers for your domain to:</p><ul><li><strong>ns11.node.ee</strong></li><li><strong>ns22.node.ee</strong></li></ul>",
"#title"=>"DNS records for <strong>".htmlspecialchars($domain)."</strong>",
"#description"=>"<p>Here are listed all the DNS records of <strong>".htmlspecialchars($domain)."</strong>. To use the DNS service, set the nameservers for your domain to:</p><ul><li><strong>ns11.node.ee</strong></li><li><strong>ns22.node.ee</strong></li></ul>",
"#children"=>$table
)
));
Expand All @@ -221,16 +222,16 @@ function dns_record_add_form($form, &$form_state){
$form['general'] = array(
'#type' => 'fieldset',
'#title' => t('Add new record'),
"#description"=>t(sprintf("Add a new DNS record for %s",$domain)),
"#description"=>t(sprintf("Add a new DNS record for %s",htmlspecialchars($domain))),
"#collapsible"=>TRUE,
"#collapsed"=>TRUE
"#collapsed"=>!form_get_errors()?TRUE:FALSE
);

$form['general']['name'] = array(
'#type' => 'textfield',
'#title' => t('Name'),
'#size' => 80,
'#description' => t('Subdomain, for example <strong>www</strong>, unicode characters are allowed. Use <strong>@</strong> for '.$domain.'<br/>Regular expressions and <em>wildcards</em> are allowed. Enclose regular expressions in slashes (<code>/regexp+/</code>).'),
'#description' => t('Subdomain, for example <strong>www</strong>, unicode characters are allowed. Use <strong>@</strong> for '.htmlspecialchars($domain).'<br/>Regular expressions and <em>wildcards</em> are allowed. Enclose regular expressions in slashes (<code>/regexp+/</code>).'),
'#required' => TRUE
);

Expand Down Expand Up @@ -272,7 +273,7 @@ function dns_record_add_form($form, &$form_state){
'#markup'=> '<p><b>Wildcard support</b><br/>When using <em>wildcards</em> or regular expressions in the <em>name</em> field, substitutions for <em>value</em> can be used. '.
'When using <em>wildcards</em>, <code>$1</code> indicates the whole match, <code>$2</code> first wildcard, <code>$3</code> second wildcard etc. '.
'When using regular expressions, <code>$1</code> indicates the first capture group, <code>$2</code> second capture group etc.</p>'.
'<p><b>For example</b><br/><em>Name:</em> <code>users.*</code>, <em>Value:</em> <code>$2.example.com</code> converts <code>users.<em>test</em>.'.$domain.'</code> to <code><em>test</em>.example.com</code>.<br/>'.
'<p><b>For example</b><br/><em>Name:</em> <code>users.*</code>, <em>Value:</em> <code>$2.example.com</code> converts <code>users.<em>test</em>.'.htmlspecialchars($domain).'</code> to <code><em>test</em>.example.com</code>.<br/>'.
'<em>Name:</em> <code>/^r/</code>, <em>Value:</em> <code>example.com</code> converts all subdomains beginning with <code>r</code> to <code>example.com</code>.</p>'
);

Expand All @@ -291,36 +292,62 @@ function dns_record_add_form_validate($form, &$form_state) {
$errors = false;

if(!trim($form_state['values']['name'])){
form_set_error('general][name', t('Name is required!'));
form_set_error('name', t('Name is required!'));
$errors = true;
}

if(!in_array($form_state['values']['type'], array("A", "CNAME", "NS", "MX"))){
form_set_error('general][type', t('Type is required!'));
form_set_error('type', t('Type is required!'));
$errors = true;
}

mb_regex_encoding('UTF-8');

$name = trim($form_state['values']['name']);
if(substr($name,0,1)!="/" || substr($name,-1)!="/"){
if(mb_ereg_match(".*[^[:alnum:]\.\-\*]", $name)){
form_set_error('name', t('Name includes illegal characters!'));
$errors = true;
}
}

$value = trim($form_state['values']['value']);
if(mb_ereg_match('.*[^[:alnum:]\.\-\*\$]', $value)){
form_set_error('value', t('Value includes illegal characters!'));
$errors = true;
}

$type = strtoupper(trim($form_state['values']['type']));
if($type=="A"){
if(!preg_match('/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/', $value)){
form_set_error('value', t('Value must be IP formatted (xxx.xxx.xxx.xxx)'));
$errors = true;
}
}

$ttl = abs(intval(trim($form_state['values']['type'])));

$record = array(
"name"=> trim($form_state['values']['name']),
"type"=> trim($form_state['values']['type']),
"name"=> $name,
"type"=> $type,
"ttl"=> $ttl?$ttl:60,
"value"=> array(trim($form_state['values']['value']))
"value"=> array($value)
);

if($record["type"]=="MX"){
$record["value"][] = intval(trim($form_state['values']['priority']));
$record["value"][] = abs(intval(trim($form_state['values']['priority'])));
}

if($data = load_from_url(variable_get('dns_api_access_point', "http://node.ee/api/dns")."/update?user=".urlencode($user->name)."&domain=".urlencode($domain),json_encode($record))){
$response = @json_decode($data, true);
if($response["status"]!="OK" || !$response["data"]){
form_set_error(null, t('Error saving data'));
if(!$errors){
if($data = load_from_url(variable_get('dns_api_access_point', "http://node.ee/api/dns")."/update?user=".urlencode($user->name)."&domain=".urlencode($domain),json_encode($record))){
$response = @json_decode($data, true);
if($response["status"]!="OK" || !$response["data"]){
form_set_error(null, t('Error saving data'));
}

}else{
form_set_error(null, t('Error connecting to server'));
}

}else{
form_set_error(null, t('Error connecting to server'));
}

}
Expand Down Expand Up @@ -380,17 +407,29 @@ function dns_domain_add_form($form, &$form_state){
function dns_domain_add_form_validate($form, &$form_state) {
global $user;

if(!trim($form_state['values']['dns_domain_name'])){
$errors = false;
$domain = mb_strtolower(trim($form_state['values']['dns_domain_name']));

if(!$domain){
form_set_error('dns_domain_name', t('Required field!'));
$errors = true;
}

if($data = load_from_url(variable_get('dns_api_access_point', "http://node.ee/api/dns")."/add?user=".urlencode($user->name)."&domain=".urlencode(trim($form_state['values']['dns_domain_name'])))){
$response = @json_decode($data, true);
if($response["status"]!="OK" || !$response["data"]){
form_set_error('dns_domain_name', t('This domain name is already listed on the system'));

mb_regex_encoding('UTF-8');
if(mb_ereg_match(".*[^[:alnum:]\.\-]", $domain)){
form_set_error('dns_domain_name', t('Domain name includes illegal characters!'));
$errors = true;
}

if(!$errors){
if($data = load_from_url(variable_get('dns_api_access_point', "http://node.ee/api/dns")."/add?user=".urlencode($user->name)."&domain=".urlencode($domain))){
$response = @json_decode($data, true);
if($response["status"]!="OK" || !$response["data"]){
form_set_error('dns_domain_name', t('This domain name is already listed on the system'));
}
}else{
form_set_error('dns_domain_name', t('Failed to check domain name'));
}
}else{
form_set_error('dns_domain_name', t('Failed to check domain name'));
}

}
Expand Down

0 comments on commit f4bca32

Please sign in to comment.