162 lines
5.6 KiB
PHP
162 lines
5.6 KiB
PHP
<?php
|
|
header("Content-Type:application/json");
|
|
|
|
$host="srv-sql.kmpr.local";
|
|
$user="duplicates";
|
|
$password="2rqyc\$Tnl0m00l4h";
|
|
$database="image_duplicates";
|
|
$table="possible_duplicates";
|
|
|
|
$db = new PDO("mysql:host=$host;dbname=$database", $user, $password);
|
|
|
|
if (isset($_GET['insertDuplicate'])) {
|
|
$data = json_decode(file_get_contents('php://input'), true);
|
|
$file1 = trim($data['file1'] ?? '');
|
|
$file2 = trim($data['file2'] ?? '');
|
|
if ($file1 != "" || $file2 != ""){
|
|
if(!pairExists($file1, $file2)) {
|
|
try {
|
|
$db->query("INSERT INTO possible_duplicates (file1,file2) VALUES ('$file1', '$file2');");
|
|
response(200, "Duplicate pair successfully written to database.");
|
|
} catch (PDOException $e) {
|
|
response(999, "Database Error: " . $e->getMessage());
|
|
die();
|
|
}
|
|
} else {
|
|
response(907, "Error: Duplicate pair already exists in database!");
|
|
}
|
|
} else {
|
|
response(901, "Error: You have to provide file1 and file2 as possible duplicates!");
|
|
}
|
|
} else if (isset($_GET['getDuplicates'])) {
|
|
$data = json_decode(file_get_contents('php://input'), true);
|
|
$amount = trim($data['amount'] ?? '');
|
|
if ($amount != "") {
|
|
if (ctype_digit($amount)) {
|
|
try {
|
|
$res = $db->query("SELECT * FROM possible_duplicates WHERE id NOT IN (SELECT pd_id FROM no_duplicates) LIMIT $amount;")->fetchAll(PDO::FETCH_ASSOC);
|
|
response(200, "Database query successful.", $res);
|
|
} catch (PDOException $e) {
|
|
response(999, "Database Error: " . $e->getMessage());
|
|
die();
|
|
}
|
|
} else {
|
|
response(905, "Error: Variable 'amount' has to be an integer value!");
|
|
}
|
|
} else {
|
|
response(904, "You have to provide 'amount' to tell how much possible duplicates you want to recieve!");
|
|
}
|
|
} else if (isset($_GET['noDuplicate'])) {
|
|
$data = json_decode(file_get_contents('php://input'), true);
|
|
$duplicateId = trim($data['duplicateId'] ?? '');
|
|
if ($duplicateId != "") {
|
|
if (ctype_digit($duplicateId)) {
|
|
try {
|
|
$st = $db->prepare("INSERT INTO no_duplicates (pd_id) SELECT id FROM possible_duplicates WHERE id = ?;");
|
|
$st->execute(array($duplicateId));
|
|
if ($st->rowCount()) {
|
|
response(200, "Pair successfully marked as non-duplicate.");
|
|
} else {
|
|
response(940, "Error: duplicateId not found in database or pair already marked as non-duplicate.");
|
|
}
|
|
} catch (PDOException $e) {
|
|
response(999, "Database Error: " . $e->getMessage());
|
|
die();
|
|
}
|
|
} else {
|
|
response(906, "Error: Variable 'duplicateId' has to be an integer value!");
|
|
}
|
|
} else {
|
|
response(902, "Error: You have to provide 'duplicateId' to mark a file pair as non-duplicate!");
|
|
}
|
|
} else if (isset($_GET['undoDuplicate'])) {
|
|
$data = json_decode(file_get_contents('php://input'), true);
|
|
$duplicateId = trim($data['duplicateId'] ?? '');
|
|
if ($duplicateId != "") {
|
|
if (ctype_digit($duplicateId)) {
|
|
try {
|
|
$st = $db->prepare("DELETE FROM no_duplicates WHERE pd_id = ?;");
|
|
$st->execute(array($duplicateId));
|
|
if ($st->rowCount()) {
|
|
response(200, "Removed successfully the mark as non-duplicate.");
|
|
} else {
|
|
response(940, "Error: duplicateId not found in database or pair is not marked as non-duplicate.");
|
|
}
|
|
} catch (PDOException $e) {
|
|
response(999, "Database Error: " . $e->getMessage());
|
|
die();
|
|
}
|
|
} else {
|
|
response(906, "Error: Variable 'duplicateId' has to be an integer value!");
|
|
}
|
|
} else {
|
|
response(902, "Error: You have to provide 'duplicateId' to mark a file pair as non-duplicate!");
|
|
}
|
|
|
|
} else if(isset($_GET['isDuplicate'])) {
|
|
$data = json_decode(file_get_contents('php://input'), true);
|
|
$duplicateId = trim($data['duplicateId'] ?? '');
|
|
if ($duplicateId != "") {
|
|
if (ctype_digit($duplicateId)) {
|
|
try {
|
|
$st = $db->prepare("DELETE FROM possible_duplicates WHERE id = ?;");
|
|
$st->execute(array($duplicateId));
|
|
if ($st->rowCount()) {
|
|
response(200, "Duplicate pair successfully removed from database.");
|
|
} else {
|
|
response(940, "Error: duplicateId not found in database.");
|
|
}
|
|
} catch (PDOException $e) {
|
|
response(999, "Database Error: " . $e->getMessage());
|
|
die();
|
|
}
|
|
} else {
|
|
response(906, "Error: Variable 'duplicateId' has to be an integer value!");
|
|
}
|
|
} else {
|
|
response(903, "Error: You have to provide 'duplicateId' to mark a file pair as duplicate!");
|
|
}
|
|
} else if(isset($_GET['totalDuplicates'])) {
|
|
try {
|
|
$res = $db->query("SELECT COUNT(id) AS totalDuplicates FROM possible_duplicates WHERE id NOT IN (SELECT pd_id FROM no_duplicates);")->fetchColumn(0);
|
|
response(200, "Database query successful.", NULL, $res);
|
|
} catch (PDOException $e) {
|
|
response(999, "Database Error: " . $e->getMessage());
|
|
die();
|
|
}
|
|
} else if(isset($_GET['test'])) {
|
|
try {
|
|
$res = $db->query("select 'MySQL is working.' as Display;");
|
|
response(200, "API is working properly.", NULL, NULL);
|
|
} catch (PDOException $e) {
|
|
response(999, "Database Error: " . $e->getMessage());
|
|
die();
|
|
}
|
|
} else {
|
|
response(400, "Error: Invalid Request");
|
|
}
|
|
|
|
function response ($response_code, $response_desc, $payload=NULL, $totalDuplicates=NULL) {
|
|
$response['response_code'] = $response_code;
|
|
$response['response_desc'] = $response_desc;
|
|
$response['payload'] = $payload;
|
|
$response['totalDuplicates'] = $totalDuplicates;
|
|
$json_response = json_encode($response);
|
|
echo $json_response;
|
|
}
|
|
|
|
function pairExists($file1, $file2) {
|
|
try {
|
|
global $db;
|
|
$rows = $db->query("SELECT COUNT(id) FROM possible_duplicates WHERE (file1 LIKE '$file1' AND file2 LIKE '$file2') OR (file1 LIKE '$file2' AND file2 LIKE '$file1');")->fetchColumn();
|
|
if ($rows > 0) {
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
} catch (PDOException $e) {
|
|
response(999, "Database Error: " . $e->getMessage());
|
|
die();
|
|
}
|
|
}
|
|
?>
|