2016-01-16 14:46:43 +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';
|
2016-01-16 15:13:14 +00:00
|
|
|
require_once '../lib/session.php';
|
2016-01-16 14:46:43 +00:00
|
|
|
$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-23 19:38:01 +00:00
|
|
|
if(isset($input->action) && $input->action == "getDomains") {
|
2016-06-07 17:00:35 +00:00
|
|
|
// Check if the requested page is a number
|
|
|
|
if(!(isset($input->page) && is_int($input->page) && $input->page > 0)) {
|
|
|
|
echo "Requested page must be a positive number!";
|
|
|
|
exit();
|
|
|
|
}
|
|
|
|
// Here we get the number of matching records
|
|
|
|
$sql = "
|
|
|
|
SELECT COUNT(*) AS anzahl
|
|
|
|
FROM domains D
|
|
|
|
LEFT OUTER JOIN permissions P ON D.id = P.domain
|
2017-01-08 04:15:31 +00:00
|
|
|
WHERE (P.userid=:user1 OR :user2) AND
|
2017-01-07 13:59:03 +00:00
|
|
|
(D.name LIKE :name1 OR :name2) AND
|
2017-01-07 10:18:39 +00:00
|
|
|
(D.type=:type1 OR :type2)
|
2016-06-07 17:00:35 +00:00
|
|
|
";
|
|
|
|
$stmt = $db->prepare($sql);
|
|
|
|
if(isset($input->name)) {
|
|
|
|
$name_filter = "%" . $input->name . "%";
|
|
|
|
$name_filter_used = 0;
|
|
|
|
} else {
|
|
|
|
$name_filter = "";
|
|
|
|
$name_filter_used = 1;
|
|
|
|
}
|
|
|
|
$id_filter = $_SESSION['id'];
|
|
|
|
$id_filter_used = (int)($_SESSION['type'] == "admin" ? 1 : 0);
|
|
|
|
if(isset($input->type)) {
|
|
|
|
$type_filter = $input->type;
|
|
|
|
$type_filter_used = 0;
|
|
|
|
} else {
|
|
|
|
$type_filter = "";
|
|
|
|
$type_filter_used = 1;
|
|
|
|
}
|
2017-02-05 16:12:38 +00:00
|
|
|
$stmt->bindValue(':user1', $id_filter, PDO::PARAM_STR);
|
|
|
|
$stmt->bindValue(':user2', $id_filter_used, PDO::PARAM_INT);
|
|
|
|
$stmt->bindValue(':name1', $name_filter, PDO::PARAM_STR);
|
|
|
|
$stmt->bindValue(':name2', $name_filter_used, PDO::PARAM_INT);
|
|
|
|
$stmt->bindValue(':type1', $type_filter, PDO::PARAM_INT);
|
|
|
|
$stmt->bindValue(':type2', $type_filter_used, PDO::PARAM_INT);
|
2016-06-07 17:00:35 +00:00
|
|
|
$stmt->execute();
|
2017-02-05 16:12:38 +00:00
|
|
|
$result = $stmt->fetchColumn();
|
|
|
|
if ($result == 0) {
|
|
|
|
$result = 1;
|
|
|
|
}
|
2016-06-07 17:00:35 +00:00
|
|
|
// Initialize the return value
|
|
|
|
$retval = Array();
|
|
|
|
$retval['pages']['current'] = $input->page;
|
2017-01-07 10:18:39 +00:00
|
|
|
$retval['pages']['total'] = ceil($result / $config['domain_rows']);
|
2016-06-07 17:00:35 +00:00
|
|
|
// Now the real search is done on the database
|
2016-01-23 19:38:01 +00:00
|
|
|
$sql = "
|
|
|
|
SELECT D.id,D.name,D.type,count(R.domain_id) AS records
|
|
|
|
FROM domains D
|
|
|
|
LEFT OUTER JOIN records R ON D.id = R.domain_id
|
|
|
|
LEFT OUTER JOIN permissions P ON D.id = P.domain
|
2017-01-08 04:15:31 +00:00
|
|
|
WHERE (P.userid=:user1 OR :user2)
|
2016-05-14 10:03:06 +00:00
|
|
|
GROUP BY D.id, D.name, D.type
|
2016-01-23 19:38:01 +00:00
|
|
|
HAVING
|
2017-01-07 10:18:39 +00:00
|
|
|
(D.name LIKE :name1 OR :name2) AND
|
|
|
|
(D.type=:type1 OR :type2)
|
2016-01-23 19:38:01 +00:00
|
|
|
";
|
|
|
|
if(isset($input->sort->field) && $input->sort->field != "") {
|
|
|
|
if($input->sort->field == "id") {
|
|
|
|
$sql .= "ORDER BY id";
|
|
|
|
} else if($input->sort->field == "name") {
|
|
|
|
$sql .= "ORDER BY name";
|
|
|
|
} else if($input->sort->field == "type") {
|
|
|
|
$sql .= "ORDER BY type";
|
|
|
|
} else if($input->sort->field == "records") {
|
|
|
|
$sql .= "ORDER BY records";
|
|
|
|
}
|
|
|
|
if(isset($input->sort->order)) {
|
|
|
|
if($input->sort->order == 0) {
|
|
|
|
$sql .= " DESC";
|
|
|
|
} else if($input->sort->order == 1) {
|
|
|
|
$sql .= " ASC";
|
|
|
|
}
|
2016-01-16 14:46:43 +00:00
|
|
|
}
|
|
|
|
}
|
2016-06-07 17:00:35 +00:00
|
|
|
/*
|
|
|
|
* Now the number of entries gets limited to the domainRows config value.
|
2017-01-07 16:51:16 +00:00
|
|
|
* SQL LIMIT and OFFSET is used for that:
|
|
|
|
* LIMIT upper OFFSET lower
|
|
|
|
* Note that LIMIT 5 OFFSET 0 returns the first five rows!
|
2016-06-07 17:00:35 +00:00
|
|
|
*/
|
|
|
|
$lower_limit = ($config['domain_rows'] * ($input->page - 1));
|
2017-01-07 16:51:16 +00:00
|
|
|
$sql .= " LIMIT " . $config['domain_rows'] . " OFFSET " . $lower_limit;
|
2016-01-23 19:38:01 +00:00
|
|
|
$stmt = $db->prepare($sql);
|
|
|
|
if(isset($input->name)) {
|
|
|
|
$name_filter = "%" . $input->name . "%";
|
|
|
|
$name_filter_used = 0;
|
|
|
|
} else {
|
|
|
|
$name_filter = "";
|
|
|
|
$name_filter_used = 1;
|
|
|
|
}
|
|
|
|
$id_filter = $_SESSION['id'];
|
|
|
|
$id_filter_used = (int)($_SESSION['type'] == "admin" ? 1 : 0);
|
|
|
|
if(isset($input->type)) {
|
|
|
|
$type_filter = $input->type;
|
|
|
|
$type_filter_used = 0;
|
|
|
|
} else {
|
|
|
|
$type_filter = "";
|
|
|
|
$type_filter_used = 1;
|
|
|
|
}
|
2017-02-05 16:12:38 +00:00
|
|
|
$stmt->bindValue(':user1', $id_filter, PDO::PARAM_STR);
|
|
|
|
$stmt->bindValue(':user2', $id_filter_used, PDO::PARAM_INT);
|
|
|
|
$stmt->bindValue(':name1', $name_filter, PDO::PARAM_STR);
|
|
|
|
$stmt->bindValue(':name2', $name_filter_used, PDO::PARAM_INT);
|
|
|
|
$stmt->bindValue(':type1', $type_filter, PDO::PARAM_INT);
|
|
|
|
$stmt->bindValue(':type2', $type_filter_used, PDO::PARAM_INT);
|
2016-01-23 19:38:01 +00:00
|
|
|
$stmt->execute();
|
2017-01-07 10:18:39 +00:00
|
|
|
while($obj = $stmt->fetchObject()) {
|
2016-06-07 17:00:35 +00:00
|
|
|
$retval['data'][] = $obj;
|
2016-01-23 19:38:01 +00:00
|
|
|
}
|
2016-01-16 14:46:43 +00:00
|
|
|
}
|
2016-01-23 19:38:01 +00:00
|
|
|
if(isset($input->action) && $input->action == "deleteDomain") {
|
|
|
|
$domainId = $input->id;
|
2017-01-07 10:18:39 +00:00
|
|
|
$db->beginTransaction();
|
|
|
|
$stmt = $db->prepare("DELETE FROM permissions WHERE domain=:domain_id");
|
2017-02-05 16:12:38 +00:00
|
|
|
$stmt->bindValue(':domain_id', $domainId, PDO::PARAM_INT);
|
2016-01-23 19:38:01 +00:00
|
|
|
$stmt->execute();
|
2017-01-07 10:18:39 +00:00
|
|
|
$stmt = $db->prepare("DELETE FROM remote WHERE record IN (SELECT id FROM records WHERE domain_id=:domain_id)");
|
|
|
|
$stmt->bindValue(':domain_id', $domainId, PDO::PARAM_INT);
|
2016-02-07 17:48:05 +00:00
|
|
|
$stmt->execute();
|
2017-01-07 10:18:39 +00:00
|
|
|
$stmt = $db->prepare("DELETE FROM records WHERE domain_id=:domain_id");
|
|
|
|
$stmt->bindValue(':domain_id', $domainId, PDO::PARAM_INT);
|
2016-01-23 19:38:01 +00:00
|
|
|
$stmt->execute();
|
2017-01-07 10:18:39 +00:00
|
|
|
$stmt = $db->prepare("DELETE FROM domains WHERE id=:domain_id");
|
|
|
|
$stmt->bindValue(':domain_id', $domainId, PDO::PARAM_INT);
|
2016-01-23 19:38:01 +00:00
|
|
|
$stmt->execute();
|
|
|
|
$db->commit();
|
|
|
|
}
|
|
|
|
if(isset($retval)) {
|
|
|
|
echo json_encode($retval);
|
|
|
|
} else {
|
|
|
|
echo "{}";
|
|
|
|
}
|