2016-01-24 14:13:33 +00:00
|
|
|
<?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'));
|
|
|
|
|
2016-01-29 15:52:23 +00:00
|
|
|
if(!isset($input->csrfToken) || $input->csrfToken !== $_SESSION['csrfToken']) {
|
|
|
|
echo "Permission denied!";
|
|
|
|
exit();
|
|
|
|
}
|
|
|
|
|
2016-01-25 16:18:15 +00:00
|
|
|
if(!isset($_SESSION['type']) || $_SESSION['type'] != "admin") {
|
|
|
|
echo "Permission denied!";
|
|
|
|
exit();
|
|
|
|
}
|
|
|
|
|
2016-01-24 14:13:33 +00:00
|
|
|
if(isset($input->action) && $input->action == "addDomain") {
|
|
|
|
$soaData = Array();
|
2017-01-07 10:29:36 +00:00
|
|
|
$soaData[] = trim($input->primary);
|
|
|
|
$soaData[] = trim(mail_to_soa($input->mail));
|
2016-01-24 14:13:33 +00:00
|
|
|
$soaData[] = date("Ymd") . "00";
|
|
|
|
$soaData[] = $input->refresh;
|
|
|
|
$soaData[] = $input->retry;
|
|
|
|
$soaData[] = $input->expire;
|
|
|
|
$soaData[] = $input->ttl;
|
|
|
|
|
2017-01-07 10:29:36 +00:00
|
|
|
$domainsName = trim($input->name);
|
|
|
|
|
2016-01-24 14:13:33 +00:00
|
|
|
$soaContent = implode(" ", $soaData);
|
|
|
|
|
2017-01-07 08:37:13 +00:00
|
|
|
$db->beginTransaction();
|
2016-01-24 14:13:33 +00:00
|
|
|
|
2017-01-07 08:37:13 +00:00
|
|
|
$stmt = $db->prepare("INSERT INTO domains(name,type) VALUES (:name,:type)");
|
2017-01-07 10:29:36 +00:00
|
|
|
$stmt->bindValue(':name', $domainsName, PDO::PARAM_STR);
|
2017-01-07 08:37:13 +00:00
|
|
|
$stmt->bindValue(':type', $input->type, PDO::PARAM_STR);
|
2016-01-24 14:13:33 +00:00
|
|
|
$stmt->execute();
|
|
|
|
|
2017-01-07 10:18:39 +00:00
|
|
|
$stmt = $db->prepare("SELECT MAX(id) FROM domains WHERE name=:name AND type=:type");
|
2017-01-07 10:29:36 +00:00
|
|
|
$stmt->bindValue(':name', $domainsName, PDO::PARAM_STR);
|
2017-01-07 08:37:13 +00:00
|
|
|
$stmt->bindValue(':type', $input->type, PDO::PARAM_STR);
|
2016-01-24 14:13:33 +00:00
|
|
|
$stmt->execute();
|
2017-01-07 08:37:13 +00:00
|
|
|
$newDomainId = $stmt->fetchColumn();
|
2016-01-24 14:13:33 +00:00
|
|
|
|
2017-01-07 08:37:13 +00:00
|
|
|
$stmt = $db->prepare("INSERT INTO records(domain_id,name,type,content,ttl) VALUES (:domain_id,:name,'SOA',:content,:ttl)");
|
|
|
|
$stmt->bindValue(':domain_id', $newDomainId, PDO::PARAM_INT);
|
2017-01-07 10:29:36 +00:00
|
|
|
$stmt->bindValue(':name', $domainsName, PDO::PARAM_STR);
|
2017-01-07 08:37:13 +00:00
|
|
|
$stmt->bindValue(':content', $soaContent, PDO::PARAM_STR);
|
|
|
|
$stmt->bindValue(':ttl', $input->ttl, PDO::PARAM_INT);
|
2016-01-24 14:13:33 +00:00
|
|
|
$stmt->execute();
|
|
|
|
|
|
|
|
$db->commit();
|
|
|
|
|
|
|
|
$retval = Array();
|
|
|
|
$retval['newId'] = $newDomainId;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(isset($retval)) {
|
|
|
|
echo json_encode($retval);
|
|
|
|
} else {
|
|
|
|
echo "{}";
|
|
|
|
}
|