2022-10-28 11:20:43 +02:00
< ? php
header ( " Content-Type:application/json " );
2022-10-31 18:11:48 +01:00
$host = " srv-sql.kmpr.local " ;
$user = " duplicates " ;
$password = " 2rqyc \$ Tnl0m00l4h " ;
2022-10-28 11:20:43 +02:00
$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! " );
}
2022-10-31 21:27:08 +01:00
} 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 {
2022-11-01 13:35:56 +01:00
$st = $db -> prepare ( " DELETE FROM no_duplicates WHERE pd_id = ?; " );
2022-10-31 21:27:08 +01:00
$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! " );
}
2022-10-28 11:20:43 +02:00
} 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! " );
}
2022-10-31 18:11:48 +01:00
} else if ( isset ( $_GET [ 'totalDuplicates' ])) {
try {
2022-10-31 18:47:50 +01:00
$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 );
2022-10-31 18:11:48 +01:00
} catch ( PDOException $e ) {
response ( 999 , " Database Error: " . $e -> getMessage ());
die ();
}
2022-12-02 12:56:51 +01:00
} 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 ();
}
2022-10-28 11:20:43 +02:00
} else {
response ( 400 , " Error: Invalid Request " );
}
2022-10-31 18:47:50 +01:00
function response ( $response_code , $response_desc , $payload = NULL , $totalDuplicates = NULL ) {
2022-10-28 11:20:43 +02:00
$response [ 'response_code' ] = $response_code ;
$response [ 'response_desc' ] = $response_desc ;
$response [ 'payload' ] = $payload ;
2022-10-31 18:47:50 +01:00
$response [ 'totalDuplicates' ] = $totalDuplicates ;
2022-10-28 11:20:43 +02:00
$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 ();
}
}
?>