Added add-domain.php

This commit is contained in:
Lukas Metzger 2016-01-24 15:13:33 +01:00
parent fe77022836
commit 9db78f23df
4 changed files with 245 additions and 1 deletions

100
add-domain.php Normal file
View file

@ -0,0 +1,100 @@
<!DOCTYPE html>
<!--
Copyright 2016 Lukas Metzger <developer@lukas-metzger.com>.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<?php
require_once 'lib/session.php';
?>
<html>
<head>
<title>PDNS Manager - Domains</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="include/bootstrap/css/bootstrap.min.css" rel="stylesheet">
<link href="include/bootstrap/css/bootstrap-theme.min.css" rel="stylesheet">
<link href="include/custom.css" rel="stylesheet">
<script src="include/jquery.js"></script>
<script src="include/bootstrap/js/bootstrap.min.js"></script>
<script src="js/add-domain.js"></script>
</head>
<body>
<nav class="navbar navbar-inverse navbar-static-top">
<div class="container">
<div class="navbar-brand">
PDNS Manager
</div>
<ul class="nav navbar-nav">
<li><a href="domains.php">Domains</a></li>
<li><a href="logout.php">Logout</a></li>
</ul>
</div>
</nav>
<div class="container">
<row>
<h2 id="domain-name">Add Domain</h2>
</row>
<row>
<form>
<div class="col-md-3">
<div class="form-group">
<label for="zone-name" class="control-label">Name</label>
<input type="text" class="form-control" id="zone-name" placeholder="Primary" autocomplete="off" data-regex="^([^.]+\.)*[^.]+$" tabindex="1">
</div>
<div class="form-group">
<label for="zone-primary" class="control-label">Primary</label>
<input type="text" class="form-control" id="zone-primary" placeholder="Primary" autocomplete="off" data-regex="^([^.]+\.)*[^.]+$" tabindex="2">
</div>
<div class="form-group">
<label for="zone-mail" class="control-label">Email</label>
<input type="text" class="form-control" id="zone-mail" placeholder="Email" autocomplete="off" data-regex="^.+\@.+\.[^.]+$" tabindex="3">
</div>
<button id="zone-button-add" class="btn btn-primary" tabindex="8">Add</button>
</div>
<div class="col-md-2 col-md-offset-1">
<div class="form-group">
<label for="zone-refresh" class="control-label">Refresh</label>
<input type="text" class="form-control" id="zone-refresh" placeholder="Refresh" autocomplete="off" data-regex="^[0-9]+$" tabindex="4" value="3600">
</div>
<div class="form-group">
<label for="zone-retry" class="control-label">Retry</label>
<input type="text" class="form-control" id="zone-retry" placeholder="Retry" autocomplete="off" data-regex="^[0-9]+$" tabindex="5" value="900">
</div>
</div>
<div class="col-md-2 col-md-offset-1">
<div class="form-group">
<label for="zone-expire" class="control-label">Expire</label>
<input type="text" class="form-control" id="zone-expire" placeholder="Expire" autocomplete="off" data-regex="^[0-9]+$" tabindex="6" value="604800">
</div>
<div class="form-group">
<label for="zone-ttl" class="control-label">TTL</label>
<input type="text" class="form-control" id="zone-ttl" placeholder="TTL" autocomplete="off" data-regex="^[0-9]+$" tabindex="7" value="86400">
</div>
</div>
</form>
</row>
</div>
</body>
</html>

66
api/add-domain.php Normal file
View file

@ -0,0 +1,66 @@
<?php
/*
* Copyright 2016 Lukas Metzger <developer@lukas-metzger.com>.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
require_once '../config/config-default.php';
require_once '../lib/database.php';
require_once '../lib/session.php';
require_once '../lib/soa-mail.php';
$input = json_decode(file_get_contents('php://input'));
if(isset($input->action) && $input->action == "addDomain") {
$soaData = Array();
$soaData[] = $input->primary;
$soaData[] = mail_to_soa($input->mail);
$soaData[] = date("Ymd") . "00";
$soaData[] = $input->refresh;
$soaData[] = $input->retry;
$soaData[] = $input->expire;
$soaData[] = $input->ttl;
$soaContent = implode(" ", $soaData);
$db->autocommit(false);
$stmt = $db->prepare("INSERT INTO domains(name,type) VALUES (?,'MASTER')");
$stmt->bind_param("s", $input->name);
$stmt->execute();
$stmt->close();
$stmt = $db->prepare("SELECT LAST_INSERT_ID()");
$stmt->execute();
$stmt->bind_result($newDomainId);
$stmt->fetch();
$stmt->close();
$stmt = $db->prepare("INSERT INTO records(domain_id,name,type,content,ttl) VALUES (?,?,'SOA',?,?)");
$stmt->bind_param("issi", $newDomainId, $input->name, $soaContent, $input->ttl);
$stmt->execute();
$stmt->close();
$db->commit();
$retval = Array();
$retval['newId'] = $newDomainId;
}
if(isset($retval)) {
echo json_encode($retval);
} else {
echo "{}";
}

View file

@ -81,13 +81,15 @@ limitations under the License.
</tbody>
</table>
<a class="btn btn-success" href="add-domain.php">Add</a>
</div>
<div class="modal fade" id="deleteConfirm" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-body">
Are you shure to <strong>delete</strong> the zone <strong><span id="zoneToDelete"></span></strong>?
Are you sure you want to <strong class="text-danger">delete</strong> the zone <strong><span id="zoneToDelete"></span></strong> including all associated data?
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>

76
js/add-domain.js Normal file
View file

@ -0,0 +1,76 @@
/*
* Copyright 2016 Lukas Metzger <developer@lukas-metzger.com>.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
$(document).ready(function() {
$('#zone-button-add').click(function(evt){
evt.preventDefault();
if(validateData()) {
saveData(function(id) {
location.assign("edit-master.php#" + id);
});
}
});
$('form input').bind("paste keyup change", regexValidate);
});
function validateData() {
var error = 0;
$('form input').change();
$('form input').each(function() {
if($(this).val().length <= 0 || $(this).parent().hasClass('has-error')) {
error++;
$(this).parent().addClass('has-error');
}
});
return error<=0;
}
function regexValidate() {
var regex = new RegExp($(this).attr('data-regex'));
if(!regex.test($(this).val())) {
$(this).parent().addClass("has-error");
} else {
$(this).parent().removeClass("has-error");
}
}
function saveData(callback) {
var data = {
name: $('#zone-name').val(),
primary: $('#zone-primary').val(),
mail: $('#zone-mail').val(),
refresh: $('#zone-refresh').val(),
retry: $('#zone-retry').val(),
expire: $('#zone-expire').val(),
ttl: $('#zone-ttl').val(),
action: "addDomain"
};
$.post(
"api/add-domain.php",
JSON.stringify(data),
function(data) {
callback(data.newId);
},
"json"
);
}